Skip to content

kv share: an optional guard page after an imported region (target 1 bounds check) - #171

Merged
bong-water-water-bong merged 1 commit into
1bit/hrx-vulkan-patchedfrom
1bit/kv-share-guard-pin
Nov 2, 2026
Merged

bong-water-water-bong merged 1 commit into
1bit/hrx-vulkan-patchedfrom
1bit/kv-share-guard-pin

Conversation

@bong-water-water-bong

Copy link
Copy Markdown

What this adds. One opt-in switch in the dma-buf importer: ONEBIT_KVSHARE_GUARD=1 makes an imported shared-KV region end in a PROT_NONE page, 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:

  1. every tensor in a region is GGML_PAD-ed to SHARE_ALIGN (4096) in src/llama-kv-share.cpp, so the import mmap(size) is page-exact and there is no slack to absorb one;
  2. the chunks land exactly adjacent in the importing process - from a real 12,722-token run:
    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 mmap with MAP_FIXED_NOREPLACE and refused to arm - the page past a chunk is already mapped, because the kernel packs the chunks back to back. This version reserves size + page PROT_NONE first, then maps the dma-buf over the front with MAP_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):

prompt tokens guard armed on share adopt file-route restore answer vs file route gfxhub faults
3 189 all 3 chunks 0.633 ms 1.0 ms 55 ms identical 0
12 722 all 3 chunks 1.253 ms 2.0 ms 180 ms identical 0

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.

…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 bong-water-water-bong left a comment

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 use size + 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.

@bong-water-water-bong bong-water-water-bong added the tprs-ready TPRS: review verdict READY posted by the standing reviewer; merge gate satisfied label Nov 1, 2026
@bong-water-water-bong
bong-water-water-bong merged commit 197e775 into 1bit/hrx-vulkan-patched Nov 2, 2026
@bong-water-water-bong
bong-water-water-bong deleted the 1bit/kv-share-guard-pin branch November 2, 2026 01:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

tprs-ready TPRS: review verdict READY posted by the standing reviewer; merge gate satisfied

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant