Repository navigation
kv share: an optional guard page after an imported region (target 1 bounds check) - #171
Conversation
…that writes past it faults instead of corrupting
The importer maps a shared KV region with plain mmap(size). Two measured facts make an overrun
silent: every tensor in the region is GGML_PAD-ed to SHARE_ALIGN (4096), so mmap is page-exact, and
the chunks land exactly adjacent in the importing process (2026-11-01, 12,722-token region:
0x7fcc35aac000 + 240 MiB == 0x7fcc44aac000, that + 1000 MiB == 0x7fcc832ac000). A write one byte
past a chunk therefore lands in another chunk of the same cache with no fault and no log.
ONEBIT_KVSHARE_GUARD=1 reserves size + one page PROT_NONE, maps the dma-buf over the front with
MAP_FIXED, and leaves the tail a guard by construction. Placing the guard after an ordinary mmap
cannot work: the page past a chunk is already mapped (measured - the first attempt refused to arm).
Default off; the region size the KV tensors see is unchanged.
Measured (Qwen3-0.6B-Q4_K_M, HRX decoder at the pinned fork line, gpu-hold, no-SVM trio):
3,189 tokens guard armed on all 3 chunks, share 0.633 ms + adopt 1.0 ms vs 55 ms file restore,
identical answer to the file transport, 0 gfxhub faults
12,722 tokens guard armed, share 1.253 ms + adopt 2.0 ms vs 180 ms, identical answer, 0 faults
bong-water-water-bong
left a comment
There was a problem hiding this comment.
VERDICT: READY
Compiled at the head and read it: a diagnostic switch, default-off, with the failure mode spelled out.
$ ninja -C build llama-cli rc 0, links; no warnings from ggml-cpu.cpp
The mechanism is right and the reserve-then-fill order is the whole trick. ONEBIT_KVSHARE_GUARD=1 first reserves
size + page with PROT_NONE|MAP_ANONYMOUS, then maps the dma-buf over the front with MAP_SHARED|MAP_FIXED — so the tail
page is a guard by construction, and it avoids the trap the body describes (a guard placed after a plain mmap cannot be
armed, because the kernel packs the chunks back to back and the page past a chunk is already mapped). Failure paths are
handled: a failed reservation returns nullptr, and a MAP_FIXED that does not land on res logs and munmap(res, size+page). Default off leaves the previous single mmap untouched, byte for byte.
Two things worth recording, neither blocking:
- the buffer is still created with
size(ggml_backend_cpu_buffer_from_ptr(ptr, size)), so the guard page is outside what
the buffer knows about. That is correct today because this path deliberately never unmaps (the comment at line 432 says
the region is not released from an atexit handler any more), but if anyone ever adds an unmap it must usesize + page
when the guard is armed, or it will unmap a neighbour's page; - it is a trap for writes past the region, not for a write that stays inside it — as the body says, the layout hash and the
identity gate remain the checks for that. Worth keeping both sentences in the docs when this is written up, because
"guard" invites the assumption of full bounds checking.
Not verified by me: the measured runs (3,189 and 12,722 tokens on the 0.6B, share/adopt/restore times, identical answers,
0 gfxhub faults) — that is a device run under npu-hold/gpu-hold and the lane's numbers; the code path is what I checked.
What this adds. One opt-in switch in the dma-buf importer:
ONEBIT_KVSHARE_GUARD=1makes an imported shared-KV region end in aPROT_NONEpage, so a tile that writes past the region faults at the exact byte instead of silently corrupting a neighbouring chunk. Default off, region size unchanged, no behaviour change without the env var.Why it is worth having. Two measured facts make an overrun silent today:
GGML_PAD-ed toSHARE_ALIGN(4096) insrc/llama-kv-share.cpp, so the importmmap(size)is page-exact and there is no slack to absorb one;0x7fcc35aac000 + 240 MiB == 0x7fcc44aac000,0x7fcc44aac000 + 1000 MiB == 0x7fcc832ac000.So a write one byte past chunk 2 lands in chunk 1's KV: no fault, no log, wrong answer much later.
Why the guard is placed the way it is. The first attempt placed it after an ordinary
mmapwithMAP_FIXED_NOREPLACEand refused to arm - the page past a chunk is already mapped, because the kernel packs the chunks back to back. This version reservessize + pagePROT_NONEfirst, then maps the dma-buf over the front withMAP_FIXED; the tail is a guard by construction.Measured (strixhalo, Qwen3-0.6B-Q4_K_M, HRX decoder at this pin,
gpu-hold, the no-SVM trio on every arm):So the provider's prefill writes up to 12,722 positions of KV into a region whose every chunk ends in a guard page, and nothing crosses it.
Honest limits. It is a debugging/diagnostic switch, not a performance feature (it costs one extra page of VA per chunk). The measured runs are 3,189 and 12,722 tokens on a 0.6B full-attention model; longer contexts were not run with the guard armed. It cannot see an overrun that stays inside the region - for that the layout hash and the identity gate remain the checks.
Our own file, full Apache notice already in the header of the file it touches, +33/-1.