Skip to main content

Module backing

Module backing 

Source
Expand description

What a virtual address range is made of.

VirtualBuffer owns the interesting part: growth, leasing, granule rounding, and the promise that the base address never moves. None of that is platform-specific. What is platform-specific is three operations — reserve address space, commit a block into part of it, release that block — plus the granularity everything must be a multiple of.

Splitting those out is what stops the device implementation from being a second copy of the growth and leasing logic.

§Host and device are the same shape

hostCUDA
reserveVirtualAlloc2 placeholder / mmap(PROT_NONE)cuMemAddressReserve
commitMapViewOfFile3 / mmap(MAP_FIXED)cuMemCreate + cuMemMap + cuMemSetAccess
releaseUnmapViewOfFile2 / mmap(PROT_NONE)cuMemUnmap; pool or cuMemRelease
granularity64 KiB / page sizecuMemGetAllocationGranularity

Measured on the hardware this was developed against: 64 KiB on Windows, 2 MiB for CUDA on an RTX 4060 — where 2 MiB is roughly a thousand tokens of one KV tensor at Llama-3-8B geometry.

§Why there is an associated Reservation

A backing cannot be stateless. Windows requires a placeholder to be split before a block is mapped into part of it, and whether a split is needed depends on the block’s already-mapped neighbours — so committing needs to know what else is mapped in the same reservation. CUDA needs the same shape because mappings still belong to a reservation even when their physical handles outlive them in a shared pool.

Putting that state in an associated type rather than in the backing keeps one backing able to serve many reservations, and keeps the state next to the thing it describes.

The cost is that VirtualBacking is not dyn-safe. That is deliberate and it costs nothing: what callers hold is a VirtualBuffer, and that can be boxed behind an object-safe trait if it ever needs to be. Nobody needs to hold a backing.

§Why addresses are usize

A device address is not a host pointer and must never be dereferenced on the CPU. Typing both as *mut u8 would invite exactly that.

Structs§

HostBacking
The process’s own address space.

Enums§

PhysicalMemoryAccounting
Who charges physical memory committed by a VirtualBacking.

Traits§

VirtualBacking
The platform operations a VirtualBuffer is built from.