Fiz um driver de bloco em Rust que usa VRAM ociosa da GPU como storage de 8.53 GB/s no Linux (ublk + io_uring)
▲ 33 r/bashonubuntuonwindows+3 crossposts

Fiz um driver de bloco em Rust que usa VRAM ociosa da GPU como storage de 8.53 GB/s no Linux (ublk + io_uring)

Quem roda workloads pesados no Linux ou no WSL2 — compilação de projetos grandes em Rust ou C++, containers Docker, inferência de modelos locais — já passou por aquele travamento completo do sistema quando a RAM física esgota e o kernel cai em swap thrashing no disco.

Ao mesmo tempo, olhando no `nvidia-smi`, você vê uma GPU com 6GB ou 8GB de memória GDDR6 ultrarrápida ficando completamente ociosa durante a compilação.

Para resolver isso sem precisar alterar ou recompilar o kernel do host, escrevi o **RamShared**, um driver userspace em Rust que aloca a VRAM ociosa via CUDA Zero-Copy Direct DMA e a expõe para o Linux como um dispositivo de bloco real (`/dev/ublkb0`) utilizando o subsistema `ublk` (Linux 6.0+) e `io_uring`.


Benchmarks Reais Medidos com `fio` (4KB Random Read, QD1 no WSL2):

* **Swap em disco VHDX/SSD padrão:** ~2.114 µs (2.1 ms) de latência | ~336 IOPS * **RamShared NBD (VRAM):** ~326 µs de latência | ~9.6k IOPS * **RamShared ublk (io_uring):** **~8 µs** de latência (264x mais rápido / queda de 99.6%) | **~22k+ IOPS** * **Throughput de Leitura Contínua:** **8.53 GB/s** (saturação do barramento PCIe 3.0/4.0 x16).


Por que isso faz diferença na prática:

No WSL2, o swap padrão em disco passa por uma cadeia pesada de virtualização: `ext4 -> VHDX -> Hyper-V -> NTFS -> Host SSD`.

Cada page fault síncrono leva mais de 2ms, o que faz a interface e o terminal congelarem inteiros durante picos de memória. Ao desviar esse tráfego diretamente para a VRAM da GPU via PCIe DMA com `ublk`, a latência cai para microsegundos de um dígito (8 µs), eliminando os travamentos e impedindo o kernel OOM Killer de matar a compilação.


Proposta Upstream no WSL2 (Microsoft):

Abrimos uma RFC formal diretamente no repositório oficial da Microsoft pro WSL2 propondo suporte nativo a dispositivos de bloco backed por VRAM em máquinas virtuais Hyper-V: 👉 **RFC Issue:** https://github.com/microsoft/WSL/issues/41054

O projeto é 100% Rust, de código aberto: 👉 **GitHub:** https://github.com/emersonbusson/ramshared

Se alguém tiver dúvidas sobre a implementação com `ublk`, `io_uring` em userspace ou tradeoffs de latência via DMA, fico à disposição nos comentários!

u/bussondev — 8 hours ago
▲ 21 r/brdev

[Show] RamShared — quando a RAM aperta, usa memória ociosa da GPU (Linux/WSL2) e devolve se a placa precisar

Quando a RAM aperta, o RamShared usa memória ociosa da placa de vídeo — e devolve se a GPU precisar.

Montei o RamShared (Rust, Linux/WSL2, NVIDIA): emprestar memória ociosa da GPU quando a RAM do sistema aperta, sem fingir que a memória da placa é tão segura/rápida quanto a RAM principal.

Problema (humano)

Compile, containers, mil abas. A RAM acaba. O PC engasga no SSD. Enquanto isso a memória da placa de vídeo está quase vazia — e você já pagou por ela.

Por que não “jogar todo o swap na GPU”?

Quando o Windows recupera memória da GPU sob pressão, essa memória pode ficar muito lenta (medimos cerca de 1,2 s numa leitura pequena no pior caso). Se isso for o primeiro recurso de emergência, a máquina trava. Por isso a GPU entra só como segunda opção — e dá para devolver.

Design (curto)

Precisa de memória?  →  1) RAM comprimida     — primeiro, rápido
                     →  2) GPU ociosa         — segundo
                     →  3) disco (SSD/VHDX)   — último

Se a latência disparar: paramos de usar a memória da GPU, os dados vão pro disco, os apps continuam.

Números (medidos)

  • Pior caso sob pressão da GPU no host: até ~1,2 s numa leitura pequena.
  • Caminho mais rápido ~241 µs vs caminho antigo ~326 µs.
  • Stress: ~500 MB na GPU, ~480 MB de volta, 0 corrupção.

Experimentar

./scripts/quickstart.sh
sudo ./target/release/ramshared check
sudo ./target/release/ramshared up --vram 1024 --zram 1024
swapon --show   # sucesso ≈ três linhas

Limites honestos

  • Dia 1: Linux/WSL2 + NVIDIA.
  • Não é RAM grátis para jogo no talo.
  • Não thrashamos WSL2 do dia a dia de propósito.

Feedback

  1. GPU como segunda opção + devolver vs outras abordagens.
  2. O que falta para “só funciona”.
  3. Onde a segurança ainda parece frágil.

Repo + FAQ: https://github.com/emersonbusson/ramshared

ajudem no desenvolvimento....

reddit.com
u/bussondev — 1 month ago
▲ 2 r/tech_x

[Show & Tell] RamShared — idle GPU memory as a backup cushion on Linux/WSL2 (when RAM is tight, borrow the GPU — give it back if the GPU needs it)

When your PC runs out of RAM, use idle GPU memory as a safety cushion — and give it back if the GPU gets busy.

I built RamShared (Rust, Linux/WSL2, NVIDIA): a practical way to borrow idle graphics memory when system RAM is tight, without pretending GPU memory is as safe/fast as main RAM.

Problem (human)

You’re compiling / running containers / drowning in tabs. RAM is gone. The machine starts thrashing the SSD. Meanwhile the GPU memory is often almost empty. You already paid for that silicon.

Why not “just put all swap on the GPU”?

When Windows reclaims graphics memory under pressure, that memory can get very slow. We measured about 1.2 seconds for a tiny read in the bad case. If that were your first emergency store, the whole machine freezes. So GPU memory is only a second cushion — and we can give it back.

Design (still short)

Need memory?  →  1) compressed RAM (zram)     — first, fast
              →  2) idle GPU memory           — second, colder
              →  3) disk (SSD / VHDX)         — last resort

If latency spikes / host pressure: stop using the GPU cushion, data slides to disk, apps keep running.

Numbers (measured)

  • Bad case under host GPU reclaim: up to ~1.2 s for a small read (why GPU is second, not first).
  • Faster path ~241 µs median vs older path ~326 µs (same window, multi-run).
  • Stress drill: ~500 MB on GPU tier, ~480 MB moved back, 0 corruption.

Try it

./scripts/quickstart.sh
sudo ./target/release/ramshared check
sudo ./target/release/ramshared up --vram 1024 --zram 1024
swapon --show   # success ≈ three lines: zram + GPU + disk

Honest limits

  • Day-1 path is Linux/WSL2 + NVIDIA, not “every GPU / every OS.”
  • Not free RAM for maxed-out games.
  • We don’t thrash live WSL2 on purpose; heavy tests use isolated VMs.
  • Not bare-metal CXL magic — practical workstation tool.

Looking for feedback

Especially from people who’ve fought swap, block devices, CUDA, or WSL2:

  1. Second-cushion + give-back vs other APIs under Windows GPU reclaim.
  2. What you’d want in a “it just works” install.
  3. Where the safety story still feels thin.

Repo + plain FAQ: https://github.com/emersonbusson/ramshared

https://preview.redd.it/vuwgjt6897ch1.png?width=1200&format=png&auto=webp&s=cfdc6a966ea98938cf3c81f28bd6c3506b8721ba

reddit.com
u/bussondev — 1 month ago
▲ 11 r/CUDA+3 crossposts

[Show & Tell] RamShared — idle GPU memory as a backup cushion on Linux/WSL2 (when RAM is tight, borrow the GPU — give it back if the GPU needs it)

**When your PC runs out of RAM, use idle GPU memory as a safety cushion — and give it back if the GPU gets busy.**

I built **RamShared** (Rust, Linux/WSL2, NVIDIA): a practical way to borrow **idle graphics memory** when system RAM is tight, without pretending GPU memory is as safe/fast as main RAM.

## Problem (human)

You’re compiling / running containers / drowning in tabs. RAM is gone. The machine starts thrashing the **SSD**. Meanwhile the **GPU memory** is often almost empty. You already paid for that silicon.

## Why not “just put all swap on the GPU”?

When Windows reclaims graphics memory under pressure, that memory can get **very slow**. We measured about **1.2 seconds** for a tiny read in the bad case. If that were your *first* emergency store, the whole machine freezes. So GPU memory is only a **second** cushion — and we can **give it back**.

## Design (still short)

```text

Need memory? → 1) compressed RAM (zram) — first, fast

→ 2) idle GPU memory — second, colder

→ 3) disk (SSD / VHDX) — last resort

When your PC runs out of RAM, use idle GPU memory as a safety cushion — and give it back if the GPU gets busy.

I built RamShared (Rust, Linux/WSL2, NVIDIA): a practical way to borrow idle graphics memory when system RAM is tight, without pretending GPU memory is as safe/fast as main RAM.

Problem (human)

You’re compiling / running containers / drowning in tabs. RAM is gone. The machine starts thrashing the SSD. Meanwhile the GPU memory is often almost empty. You already paid for that silicon.

Why not “just put all swap on the GPU”?

When Windows reclaims graphics memory under pressure, that memory can get very slow. We measured about 1.2 seconds for a tiny read in the bad case. If that were your first emergency store, the whole machine freezes. So GPU memory is only a second cushion — and we can give it back.

Design (still short)

Need memory?  →  1) compressed RAM (zram)     — first, fast
              →  2) idle GPU memory           — second, colder
              →  3) disk (SSD / VHDX)         — last resort

If latency spikes / host pressure: stop using the GPU cushion, data slides to disk, apps keep running.

Numbers (measured)

  • Bad case under host GPU reclaim: up to ~1.2 s for a small read (why GPU is second, not first).
  • Faster path ~241 µs median vs older path ~326 µs (same window, multi-run).
  • Stress drill: ~500 MB on GPU tier, ~480 MB moved back, 0 corruption.

Try it

./scripts/quickstart.sh
sudo ./target/release/ramshared check
sudo ./target/release/ramshared up --vram 1024 --zram 1024
swapon --show   # success ≈ three lines: zram + GPU + disk

Honest limits

  • Day-1 path is Linux/WSL2 + NVIDIA, not “every GPU / every OS.”
  • Not free RAM for maxed-out games.
  • We don’t thrash live WSL2 on purpose; heavy tests use isolated VMs.
  • Not bare-metal CXL magic — practical workstation tool.

Looking for feedback

Especially from people who’ve fought swap, block devices, CUDA, or WSL2:

  1. Second-cushion + give-back vs other APIs under Windows GPU reclaim.
  2. What you’d want in a “it just works” install.
  3. Where the safety story still feels thin.

Repo + plain FAQ: https://github.com/emersonbusson/ramshared

u/bussondev — 1 month ago