Skip to content

npu: execute on an imported dma-buf -- the engine's arm, and the claim it settles (engine#753) - #2037

Merged
bong-water-water-bong merged 1 commit into
mainfrom
1bit/753-dmabuf-import
Nov 10, 2026
Merged

bong-water-water-bong merged 1 commit into
mainfrom
1bit/753-dmabuf-import

Conversation

@bong-water-water-bong

Copy link
Copy Markdown
Collaborator

Closes #753, and its premise needed correcting first: XRT can bind a foreign dma-buf in this tree. The issue was right when it was filed; two days of merged work made it stale, and the difference decides what was actually missing.

Where the claim stands now

piece where state
the engine declares the import npu/npu_rt_api.h:106-115 — ext::bo(const device&, int dma_buf_fd, size_t, void* host = nullptr), mechanism in the comment merged
the private runtime implements it npu_rt_api.cpp:101 → raw DRM_IOCTL_PRIME_FD_TO_HANDLE + GET_BO_INFO merged
it executes npu-kernels#39's kvwrite-region: ext::bo cache(dev, region.fd, region.bytes, region.map), then sub-windows passed to a kernel merged; bit-exact 131072 B under npu-hold alone, and again by the semantic gate

What was missing was nothing in the engine that calls it: git grep over main finds no BO built from an fd, and the shim test covered ext::bo(dev, size_t) only — the fd constructor appeared solely inside uses(), a link-coverage function that is never called.

What this adds

  • tests/npu_rt_shim_test.cpp: the fd constructor joins the link-coverage surface, so a runtime lacking it fails to link rather than at the first hybrid call site.
  • tests/npu_dmabuf_import_test.cpp: the executing arm — a dma-buf with no GPU in the path (sealed memfd → /dev/udmabuf), imported with the exporter's mapping, then bound as kernel argument 0 and run.

Measured under one npu-hold ticket

ok   a dma-buf exists with no GPU in the path (udmabuf over a sealed memfd)
ok   ext::bo(dev, fd, size, host) accepts it
ok   the imported BO is the dma-buf's size
ok   map() IS the exporter's mapping (a second mapping puts the BO on the driver's invalid list and the next submit HMM-faults)
ok   a write through the exporter's mapping is visible through the imported BO (one buffer, not a copy)
ok   host = nullptr constructs (so the trap is at SUBMIT, not at construction) (no throw)
ok   and it is a SECOND mapping, not the exporter's -- the state npu_rt.cpp says the next submit faults on
ok   the imported BO is accepted as kernel argument 0 (no throw)
ok   and the run with it completes (COMPLETED)
all npu dmabuf-import checks passed

Queue waited 440 s at a 70 C die; the command itself took 1 s; the ticket was released on exit.

The last two lines are the claim: an imported buffer is not merely mappable, it is submit-able — binding is exactly where the issue said execution failed.

One correction only the device run could produce

My first version asserted host = nullptr throws, because the header's comment ("nullptr asks for the driver's own mapping, which the driver then marks invalid") reads as if that is refused at construction. It does not throw: it constructs and hands back a second mapping, which is the state that makes the next submit HMM-fault (amdxdna_insert_pages → admxdna_mark_mapp_invalid). The arm now asserts that observable precursor and deliberately does not court the wedge to watch the documented fault — wedging the array to observe a known failure is a bad trade. The header comment could say "at the next submit" explicitly; that is a one-line follow-up rather than part of this PR.

Not run / not claimed

…m it settles (engine#753)

The issue says zero-copy execution cannot work because XRT cannot bind a foreign dma-buf as a kernel argument. That is no
longer true in this tree: npu/npu_rt_api.h declares `ext::bo(const device&, int dma_buf_fd, size_t, void* host)`, the private
runtime imports the fd with DRM PRIME and pins those pages, and npu-kernels#39 already runs kernels with one. What was missing
was any arm *in this tree*: the shim test covered `ext::bo(dev, size)` only, and the fd constructor appeared solely in a
link-coverage function that is never called.

Two changes:

- `tests/npu_rt_shim_test.cpp`: the fd constructor joins the link-coverage surface, so a runtime library that lacks it now fails
  to LINK rather than at the first hybrid call site.
- `tests/npu_dmabuf_import_test.cpp`: the executing arm. A dma-buf with no GPU in the path (a sealed memfd handed to
  /dev/udmabuf), imported with the EXPORTER'S mapping, then **bound as kernel argument 0 and run**.

Measured under one npu-hold ticket (queue waited 440 s at a 70 C die, command 1 s):

  ok   a dma-buf exists with no GPU in the path (udmabuf over a sealed memfd)
  ok   ext::bo(dev, fd, size, host) accepts it
  ok   the imported BO is the dma-buf's size
  ok   map() IS the exporter's mapping (a second mapping puts the BO on the driver's invalid list and the next submit HMM-faults)
  ok   a write through the exporter's mapping is visible through the imported BO (one buffer, not a copy)
  ok   host = nullptr constructs (so the trap is at SUBMIT, not at construction) (no throw)
  ok   and it is a SECOND mapping, not the exporter's -- the state npu_rt.cpp says the next submit faults on
  ok   the imported BO is accepted as kernel argument 0 (no throw)
  ok   and the run with it completes (COMPLETED)
  all npu dmabuf-import checks passed

The last two lines are the claim: an imported buffer is not merely mappable, it is submit-able -- the binding is where the issue
said execution failed.

One correction that only the device run could produce: my first version asserted that `host = nullptr` *throws*, because the
header's comment reads as if the driver's own mapping is refused at construction. It constructs fine and hands back a second
mapping; the documented fault (amdxdna_insert_pages -> amdxdna_mark_mapp_invalid, then an HMM fault on the next submit) is at
submit time. The arm now asserts the observable precursor and deliberately does not court the wedge to watch the fault -- a test
that wedges the array to observe a documented failure is a bad trade.

Skips (77) without a kernel ELF, so a box with none registers a skip rather than a red row.

Refs engine#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 c404ea4

"Its premise needed correcting first" is the strongest thing a PR can open with, and the correction is specific: "XRT can bind a foreign dma-buf in this tree. The issue was right when it was filed; two days of merged work made it stale, and the difference deci[des …]"

An issue whose premise has expired is more dangerous than one that is wrong, because the work built on it inherits a false constraint — here, "this cannot work" would have justified a copy that the engine no longer needs. Checking the premise against the current tree before writing the arm is the step that saved the work.

So what lands is the engine's arm plus the claim it settles — Closes #753 with a demonstration rather than an argument, on a dma-buf the engine did not allocate. That is the shape needed for #1750's neighbour and for the NPU split questions: a claim about what the runtime can do, settled by doing it.

One ask: state the XRT version and the driver/plugin that make the foreign bind work, because "XRT can bind a foreign dma-buf" is a property with a floor — the next person on an older stack will read this as unconditional, and the failure they see (a silent copy or an error) will not point at the version.

@bong-water-water-bong
bong-water-water-bong merged commit 0a46707 into main Nov 10, 2026
10 checks passed
@bong-water-water-bong
bong-water-water-bong deleted the 1bit/753-dmabuf-import branch November 10, 2026 22:30
@github-actions

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:

  • The engine declares the import functionality in npu/npu_rt_api.h
  • The private runtime implements the import mechanism in npu_rt_api.cpp
  • The engine can execute kernels using imported dma-bufs as kernel arguments
  • Tests are added to verify the functionality (npu_rt_shim_test.cpp and npu_dmabuf_import_test.cpp)

Non-compliant requirements:

  • None

Requires further human verification:

  • Verification that the actual hardware behavior matches the test results
  • Confirmation that the zero-copy performance gain is measurable on the target hardware
⏱️ Estimated effort to review: 3 🔵🔵🔵⚪⚪
🧪 PR contains tests
🔒 No security concerns identified
⚡ Recommended focus areas for review

Test Coverage for Import Constructor

The test npu_dmabuf_import_test.cpp verifies that the engine can execute on an imported dma-buf, but it relies on a specific kernel ELF file. If the kernel ELF is not available, the test skips with code 77, which might mask issues if the import functionality itself is broken. It would be better to have a more robust test that can validate the import functionality without requiring a specific kernel ELF.

int main(int argc, char** argv) {
    if (argc < 2) {
        std::printf("skip: no kernel ELF given (`npu_dmabuf_import_test <kernel.elf>`); the executing arm needs one device ELF\n");
        return 77;
    }
Link Coverage Test

The test npu_rt_shim_test.cpp includes a call to the ext::bo constructor with a dma-buf fd to ensure link coverage, but it uses a dummy fd (-1) and nullptr for the host pointer. This test does not actually validate the functionality of the import mechanism, but only ensures that the symbol exists at link time.

xrt::ext::bo imported(dev, /*dma_buf_fd=*/-1, size_t(4096), /*host=*/nullptr);
std::printf("%zu\n", imported.size());
auto plain = std::make_unique<xrt::bo>(e);

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