Skip to content

docs: who makes the provider's mapping, and the trap that makes it silent (Refs #753) - #1424

Merged
bong-water-water-bong merged 2 commits into
mainfrom
1bit/753-provider-mapping-docs
Nov 8, 2026
Merged

bong-water-water-bong merged 2 commits into
mainfrom
1bit/753-provider-mapping-docs

Conversation

@bong-water-water-bong

Copy link
Copy Markdown
Collaborator

What this is

Two things, both discovered after #1317 merged, both fitting in the same two paragraphs:

  1. The nit docs: what a device-side KV provider has to write, now that the writer exists (Refs #753) #1317's review left. hybrid.md said the provider writes through "its own mapping" and
    zero-copy.md said a driver-made mapping poisons the submission; the pair only reads consistently once a reader
    works out that the distinction is who made the mapping. Both pages now say so.
  2. A trap the cross-process measurement found, which would cost a provider author a day. A provider can
    mmap the dma-buf fd it is handed and gets the decoder's pages (measured: the mapping aliases the exporter's,
    checked with a sentinel written before the fork). But that mapping is lazy: a device write into pages the
    provider has not touched is dropped with no error, no fault, and a completed launch. My first cross-process
    run touched only the first 4 KiB, saw the K plane written and V zero, and blamed the import for it; touching the
    whole region through the mapping first made all four planes bit-exact. The contract now says: touch the whole
    region before the device writes it.

Engine#753's writer is measured bit-exact both in-process and across a process boundary (SCM_RIGHTS, the shape
this route uses): 262 144 B of cache, decoder's layout, read back through the decoder's own mapping. Details and
the three traps stay in the private tree (npu-kernels#29, npu-kernels#30).

Docs only: no engine behaviour changes, nothing to gate. copyright.py --check clean.

Refs #753

…lent (Refs #753)

#1317's review left one nit: hybrid.md said the provider writes through "its own mapping" while
zero-copy.md said a driver-made mapping poisons the submission, and the pair is only consistent once a
reader works out that the distinction is *who made the mapping*. Both pages now say that outright.

The cross-process measurement behind the writer added a second, sharper fact, so it goes in the same
paragraph: a provider can mmap the dma-buf fd it is handed (measured; it aliases the decoder's pages),
but that mapping is LAZY. A device write into pages the provider has not touched is dropped with no
error, no fault and a completed launch -- the write looks like it never happened, and the first
cross-process run blamed the import for exactly that. The contract now says: touch the whole region
before the device writes it.

No kernel internals; the design, the measurement and the traps are in the private tree (npu-kernels#29,
#30).

Refs #753

@bong-water-water-bong bong-water-water-bong left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

VERDICT: READY at e0abe565.

The trap it adds is real and the two pages agree about it — I checked that specifically, because the same paragraph now lives in hybrid.md and
zero-copy.md and drift between them would be worse than neither:

hybrid.md    lazy-mapping trap described, cites engine#753 (measured both) and engine#812 (the poisoning route)
zero-copy.md same trap, same two issues

A device DMA into pages the process has not faulted in is dropped with no error and no fault, so the write "looks like it never happened" — that
is exactly the failure mode, and pairing it with the #812 rule (never write through a driver-made mapping of the imported buffer) is right: one
rule says which mapping, the other says prepare it.

One addition that would make it actionable rather than correct: "touch the whole region before the device writes it" leaves the obvious
wrong implementation available — a reader can touch one byte and believe they are done, because the pages after it are not faulted. Naming the
per-page shape ("one byte per page, e.g. for (p = buf; p < buf+len; p += 4096) *p = *p;", or madvise/MAP_POPULATE if the mapping is made
for this purpose) turns a rule into a recipe. Non-blocking; the paragraph is true as written and the reader who knows mmap will implement it
correctly.

Also good: "the engine side of this contract needs no change for it" with the three touchpoints named (?action=share, ?action=adopt,
llama_kv_cells_adopt) — that is the sentence that stops someone opening a PR against the engine for a provider-side trap.

@github-actions

github-actions Bot commented Nov 8, 2026

Copy link
Copy Markdown

PR Reviewer Guide 🔍

Here are some key observations to aid the review process:

🎫 Ticket compliance analysis 🔶

753 - Partially compliant

Compliant requirements:

  • Document the distinction between mappings made by the provider vs. the driver for dma-buf imports
  • Explain the trap of lazy mapping that causes silent data loss when writing to imported dma-bufs
  • Clarify that the provider must use its own mapping to avoid poisoning submissions

Non-compliant requirements:

  • Ensure documentation reflects the measured behavior of zero-copy hybrid/split paths

Requires further human verification:

  • Verification that the documented behavior matches actual hardware behavior in the Strix Halo environment
⏱️ Estimated effort to review: 2 🔵🔵⚪⚪⚪
🧪 No relevant tests
🔒 No security concerns identified
⚡ Recommended focus areas for review

Documentation Clarity

The documentation now correctly distinguishes between mappings made by the provider versus the driver, which is crucial for avoiding submission poisoning. However, the explanation of the lazy mapping trap could be more explicit about how to detect it in practice, especially for developers who may not have encountered this issue before.

inside a tensor is `n_embd_k_gqa`. The provider writes through **a mapping it made itself** of the dma-buf it was
handed — `mmap` of the fd it received is enough, and it aliases the decoder's pages (engine#753 measured both) —
and never through a mapping the *driver* makes of the imported buffer, which is the route that poisons the
submission (engine#812). One trap that costs a silent wrong answer: that mapping is lazy, so **touch the whole
region before the device writes it**; a device DMA into pages the process has not faulted in is dropped with no
error and no fault, and the write looks like it never happened. The bytes the provider writes are the bytes the
decoder reads — the handoff is `llama_kv_cells_adopt`, so nothing re-checks them. Engine#753 is that writer; the
engine side of this contract (`?action=share`, `?action=adopt`, `llama_kv_cells_adopt`) needs no change for it.
Documentation Clarity

Similar to hybrid.md, this section clarifies the need for the provider to make its own mapping and warns about the silent failure of lazy mappings. The addition of the lazy mapping trap explanation is valuable, but it would benefit from a more concrete example or a clearer warning to developers about how to test for this behavior.

- it must write through **a mapping it makes itself** of the dma-buf. Asking the driver for a mapping of the
  import is the route that makes every later submission fail (engine#812); the pages are the same either way. A
  second process can `mmap` the fd it is handed and gets the decoder's pages, but that mapping is lazy: pages it
  has not touched are not there, and a device write into them is dropped silently, so the region has to be
  touched first (both measured, engine#753).

The review is right that "touch the whole region" leaves the obvious wrong implementation available -- touch one
byte and believe you are done, because the pages after it are not faulted. Both pages now name the per-page shape
(one byte per page, MAP_POPULATE, or MADV_WILLNEED) and say outright that a single byte is not enough, which is the
mistake that cost the first cross-process run.

Refs #753
@bong-water-water-bong

Copy link
Copy Markdown
Collaborator Author

Taken, at head f9d973dd (was e0abe565) — your point that "touch the whole region" leaves the wrong
implementation available is exactly right, and it is the mistake I made: the first cross-process run touched 4 KiB of
a 256 KiB region and read the result as an import failure.

Both pages now give the recipe rather than the rule: one byte per page (for (p = buf; p < buf + len; p += 4096) *p = *p;), or MAP_POPULATE, or madvise(MADV_WILLNEED), plus the sentence that touching a single byte is not
enough and looks identical to having done it properly.

@github-actions

github-actions Bot commented Nov 8, 2026

Copy link
Copy Markdown

Preparing review...

@bong-water-water-bong bong-water-water-bong left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

VERDICT: READY at f9d973dd.

The per-page recipe I suggested is in, in both pages, and worded better than the suggestion:

hybrid.md    "fault the whole region in … one byte per page is enough (for (p = buf; p < buf + len; p += 4096) *p = *p;),
              or mmap it with MAP_POPULATE, or madvise(MADV_WILLNEED) … touching a single byte of the region is *not* enough,
              and looks identical to having done the right thing"
zero-copy.md "every page of the region has to be faulted first — one byte per page, or MAP_POPULATE / MADV_WILLNEED;
              touching one byte of the region [is not enough]"

The clause "looks identical to having done the right thing" is the part that earns its place: it is the reason the trap survives code review,
and it is the same failure the measurement tool had to be fixed for (#30's :195, where the 4 KiB marker was exactly that "touching one byte"
shape and std::memset(mine, 0, REGION_BYTES) was the actual fix). Naming both the recipe and the false-confidence mode means the next reader
cannot implement the wrong half of it and believe they are done.

Both pages still agree with each other and with #753/#812; nothing else moved.

@bong-water-water-bong
bong-water-water-bong merged commit 867581d into main Nov 8, 2026
8 of 9 checks passed
@bong-water-water-bong
bong-water-water-bong deleted the 1bit/753-provider-mapping-docs branch November 8, 2026 10:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant