Files
metal2vulkan/docs
Anees Iqbal 52bf0c4f13 dma-buf: hand the driver its descriptor at a kept number, and try first the type a kind last imported as
dma_buf_leak_4_threads ended in SIGABRT and dma_buf_leak_8_threads in a GPU recovery
(GpuRecoveryVictim) on the RTX 5080 Laptop GPU (NVIDIA 615.71.09, round job m3-max-16620 at
600b535875). On the RTX 3090 (620.70.06, round job m3-max-16728) dma_buf_leak_8_threads ended on
Rust's I/O safety check, with "owned fd already closed" in import_dma_buf's drop of its clone.

The cause is the driver's handling of a failed import. NVIDIA lists a udmabuf of host pages as
two memory types, 0 and 4 (host-visible, coherent, cached), and refuses every import as type 4
as out of device memory. The runtime ranked type 4 first. strace -f -k under the eight-thread
probe on the RTX 2080 (620.70.06, potluck m3-max-16817) showed that every failed import closed
the descriptor it was handed. Vulkan gives the descriptor to the driver only on success. In 3
of 10 failed imports the driver closed the same number twice within the one call, with two
more opens of /dev/nvidiactl between the closes. The kernel gives a new descriptor the lowest
free number, so between the two closes another thread's new udmabuf took the number, and the
second close closed it. That thread then imported a third thread's dma-buf under the number,
and the process aborted on the I/O safety check.

- handed_descriptor.rs: an import hands the driver a clone at a number kept for imports in
  flight. The range is the 64 numbers below min(soft RLIMIT_NOFILE, 65536). The clone is taken
  with fcntl(F_DUPFD_CLOEXEC, n) and kept only where it landed on n itself, never by dup2, and
  no two imports in flight hold the same number. So a number the driver closes stays free until
  the import returns, and a second close reaches nothing. After a failure, the number is still
  the import's alone, so whether the driver left it open is read without a race and, where it
  did, it is closed. That also ends the leak of one descriptor per failed import on a driver
  that leaves it open. Where the limit is under 256, the process holds the range itself, or
  more than 64 imports are in flight, the clone is an ordinary one: correct, but unprotected,
  and a failed import lets it go rather than close a number that may be another's by then.
- memory.rs: each kind of import (what it backs, and its memory class) remembers the type it
  last imported as, and the next import of that kind tries that type first (worked_first). Every
  other type is still tried after it, in today's order. In steady state this avoids the import
  the driver refuses, along with its closes and the stall below.
- docs/FAST-PATHS.md, known gaps: the type listing and refusal, the closes, the kept numbers and
  their fallback, and the stall. A failed import can spin inside vkAllocateMemory holding the
  driver's lock: 3 of 7 four-thread runs on the RTX 2080 alone, and one on the RTX 3090. It
  stays open for the first imports of each kind, which still try type 4.
- tests/dma_buf_fds.rs (device test): what a driver does with the handed descriptor, for each
  type, size and form. It checks that no number the driver let go of is closed again later. A
  let-go number is taken by F_DUPFD_CLOEXEC and watched only where the clone landed on it.

Tests: handed_descriptor (CPU) drives Handed with stand-in imports. One driver closes the
number twice with another thread's open between the closes. Another leaves the clone open on a
failure. Imports in flight each hold a number of their own. A test covers the range for low,
high and unlimited limits. Two cover the fallback: the clone with no number kept, and a range
the process holds itself. memory::a_dma_buf_import_tries_the_type_its_kind_last_imported_as_first
covers the ordering.

Verified:
- The handed_descriptor tests fail with ordinary clones (potluck m3-max-17034, a scratch commit)
  and pass with the kept numbers on CPU (m3-max-16978) and in the RTX 3090 (m3-max-17024) and
  RTX 2080 (m3-max-17076) runs, NVIDIA 620.70.06. Each of those runs passed 15 of 15 touched
  tests. dma_buf_leak_4_threads ran its five rounds in 39.6 s on the 3090 and 31.2 s on the 2080,
  and dma_buf_leak_8_threads in 64.9 s and 58.2 s. dma_buf_fds on the 2080 took 12 numbers the
  driver let go of: none was taken by another descriptor first, and none was closed again.
- The stall, with the type order: eight four-thread runs in a row ended 8 of 8 on the RTX 2080
  (m3-max-17077) and 8 of 8 on the RTX 3090 (m3-max-17065). Without it, 3 of 7 stalled on the
  2080, and the 3090 stalled in m3-max-16941. The kernel logged 28 and 30 refusals of the
  host-visible import over the eight runs (3 to 4 a run): the first imports of each kind.
- Lint: clippy with all features, with --cfg m2v_gpu and with --cfg m2v_cpu_icd, and Rustdoc,
  all clean (m3-max-17066).
2026-11-11 12:48:58 -08:00
..