d3d12: keep live userptr heap pages accessible to lazy publication #640

Merged
DrArroway merged 1 commit from upstream-pr/640 into main 2026-12-01 22:24:10 +00:00
DrArroway commented 2026-12-01 21:36:16 +00:00 (Migrated from github.com)

Imported from brcly/TuxBox. Originally posted by DrArroway on 2026-12-01T21:36:16Z.


Summary

Keep all 4 KiB pages covered by live OpenExistingHeapFromAddress* userptr imports accessible (PAGE_READWRITE) while lazy publication is active, fixing the radv/amdgpu -EPERM (-1) CS rejections introduced when lazy publication became default on.

Root Cause

On Linux amdgpu, RADV attaches every live AMDGPU_GEM_USERPTR allocation (OpenExistingHeapFromAddress*) to the context's global BO list, which amdgpu_cs_parser_bos validates via amdgpu_hmm_range_get_pages on every CS submission.

Previously, lazy::defer could call VirtualProtect(..., PAGE_NOACCESS) on any placed-texture page unless gpu_writes::any_import_overlaps saw an active byte range—which missed:

  1. Full 64 KiB-aligned userptr heap extents and 4 KiB partial-page sharing at the edges.
  2. Scattered buffer PieceHeap extents.
  3. The full lifetime of the underlying native ID3D12Heap (including vkd3d-proton's deferred queue/descriptor references after TuxBox drops its wrapper).
  4. Future userptr imports opened on pages already deferred by lazy::defer.

A single PAGE_NOACCESS page inside any live userptr BO causes every subsequent vkQueueSubmit to fail in the kernel with -EPERM (-1) / VK_ERROR_DEVICE_LOST.

Changes

  • runtime/d3d12/src/device/placement/lazy.rs:
    • Add refcounted UserptrRanges tracking 4 KiB page intervals in STATE and an RAII UserptrPin guard that unpins on final drop.
    • Add pin_userptr(range) which inserts pages_of(&range) into STATE.userptr first, collects any overlapping live state.held textures (cleaning dead Weak entries), drops STATE lock, publishes via publish_now under the PUBLISHING reentrancy guard, and unconditionally restores page access via release(&texture).
    • Reject deferral in reason() / defer() for any 4 KiB page range overlapping STATE.userptr, and pass pages_of(range) to gpu_writes::any_import_overlaps.
    • Leave Pages::protect and Pages::release untouched so this remains cleanly orthogonal to #625.
  • runtime/d3d12/src/device/heap.rs, heap/pages.rs, heap/import.rs:
    • Attach UserptrPin as IUnknown private data (SetPrivateDataInterface) on the native ID3D12Heap when OpenExistingHeapFromAddress1 / OpenExistingHeapFromAddress succeeds (non-mirrored paths), holding the pin until vkd3d-proton releases its final reference; dropped immediately on cache hits, shadow mirrors, or errors.
    • Acquire pin_userptr before validate_shared_range in import_buffer and import_scattered_buffer.
  • Tests (runtime/d3d12/src/tests/device/placement/lazy.rs, heap_backing.rs):
    • Unit tests covering 4 KiB page-edge overlap with imports, UserptrRanges refcounting, and pin_userptr publishing/releasing held textures while keeping pages accessible.
    • Hardware test (userptr_pin_attached_to_heap_outlives_wrapper_and_submits_cleanly) verifying the pin outlives the local ID3D12Heap wrapper while placed resources remain alive, releases cleanly once dropped, and submits cleanly without CS rejection.

Verification

  • Host checks (fix/lazy-publication-userptr-634 at cb4631ba):
    • cargo fmt --all --check, tests/test_repository_hygiene.py (7/7), cargo clippy --locked --target x86_64-pc-windows-msvc -p tuxbox-d3d12 --all-targets -- -D warnings, and RUSTDOCFLAGS="-D warnings" cargo doc --locked --no-deps --target x86_64-pc-windows-msvc -p tuxbox-d3d12 --document-private-items: all passed.
  • Windows unit tests under GE-Proton Wine (AMD Radeon RX 6900 XT (1002:73bf) & AMD Raphael Radeon 610M iGPU (1002:164e)):
    • device::placement::lazy::tests: 11/11 passed on both adapters.
    • userptr_pin_attached_to_heap_outlives_wrapper_and_submits_cleanly: passed on both AMD Radeon RX 6900 XT (1002:73bf) (0.25s) and AMD Raphael Radeon 610M iGPU (1002:164e) (0.27s).
  • Real-world ProjectScarlett (AMD Radeon RX 6900 XT (1002:73bf), Mesa 26.3.1 RADV, TUXBOX_LAZY_PUBLICATION=1 ON):
    • Run 1: 1,110.1s, 10,031 presents (Frame: 1437), 39.8 MiB/s texture readback — 0 CS rejections (-EPERM), 0 GPUVM faults, 0 xmem errors (vs 100% -EPERM CS rejection in seconds without this fix).
    • Run 2: 874.9s, 10,201 presents (Frame: 1531), 33.1 MiB/s readback, 2 live published on access VEH fault recoveries — 0 CS rejections, 0 GPUVM faults, 0 xmem errors. (Both runs later hit the unrelated game-side Unsignaled CrossQueueBarrier breakpoint while streaming.)
  • Dual-GPU tuxbox.py check --changed origin/main:
    • AMD Radeon RX 6900 XT (1002:73bf): 23/23 D3D12 regression controls passed (including mesh, buffer-remap, buffer-unmap-alive, buffer-unmap-released, alias-deferred, copy-resource, placement, heaps, tables); 122/123 total checks passed — the sole failure is the pre-existing #625 lanes-selftest-texture-publication crash (confirmed unrelated: passes with TUXBOX_LAZY_PUBLICATION=0).
    • AMD Raphael Radeon 610M iGPU (1002:164e): 23/23 D3D12 regression controls passed; 121/122 total checks passed — same sole pre-existing #625 lanes-selftest-texture-publication failure.

Evidence: TuxBox check of cb4631ba67, no uncommitted changes, GPU 1002:73bf: 15 passed, 1 failed, 0 not run
Evidence: TuxBox check of cb4631ba67, no uncommitted changes, GPU 1002:164e: 15 passed, 1 failed, 0 not run

Fixes #634

<!-- greyfinch-sync:brcly/TuxBox:issue:5594992646 --> Imported from [brcly/TuxBox](https://github.com/brcly/TuxBox/pull/640). Originally posted by [DrArroway](https://github.com/DrArroway) on 2026-12-01T21:36:16Z. --- ## Summary Keep all 4 KiB pages covered by live `OpenExistingHeapFromAddress*` userptr imports accessible (`PAGE_READWRITE`) while lazy publication is active, fixing the `radv/amdgpu` `-EPERM` (`-1`) CS rejections introduced when lazy publication became default on. ### Root Cause On Linux `amdgpu`, RADV attaches every live `AMDGPU_GEM_USERPTR` allocation (`OpenExistingHeapFromAddress*`) to the context's global BO list, which `amdgpu_cs_parser_bos` validates via `amdgpu_hmm_range_get_pages` on every CS submission. Previously, `lazy::defer` could call `VirtualProtect(..., PAGE_NOACCESS)` on any placed-texture page unless `gpu_writes::any_import_overlaps` saw an active byte range—which missed: 1. Full 64 KiB-aligned userptr heap extents and 4 KiB partial-page sharing at the edges. 2. Scattered buffer `PieceHeap` extents. 3. The full lifetime of the underlying native `ID3D12Heap` (including `vkd3d-proton`'s deferred queue/descriptor references after TuxBox drops its wrapper). 4. Future userptr imports opened on pages already deferred by `lazy::defer`. A single `PAGE_NOACCESS` page inside any live userptr BO causes every subsequent `vkQueueSubmit` to fail in the kernel with `-EPERM` (`-1`) / `VK_ERROR_DEVICE_LOST`. ### Changes - **`runtime/d3d12/src/device/placement/lazy.rs`:** - Add refcounted `UserptrRanges` tracking 4 KiB page intervals in `STATE` and an RAII `UserptrPin` guard that unpins on final drop. - Add `pin_userptr(range)` which inserts `pages_of(&range)` into `STATE.userptr` first, collects any overlapping live `state.held` textures (cleaning dead `Weak` entries), drops `STATE` lock, publishes via `publish_now` under the `PUBLISHING` reentrancy guard, and unconditionally restores page access via `release(&texture)`. - Reject deferral in `reason()` / `defer()` for any 4 KiB page range overlapping `STATE.userptr`, and pass `pages_of(range)` to `gpu_writes::any_import_overlaps`. - Leave `Pages::protect` and `Pages::release` untouched so this remains cleanly orthogonal to `#625`. - **`runtime/d3d12/src/device/heap.rs`, `heap/pages.rs`, `heap/import.rs`:** - Attach `UserptrPin` as `IUnknown` private data (`SetPrivateDataInterface`) on the native `ID3D12Heap` when `OpenExistingHeapFromAddress1` / `OpenExistingHeapFromAddress` succeeds (non-mirrored paths), holding the pin until `vkd3d-proton` releases its final reference; dropped immediately on cache hits, shadow mirrors, or errors. - Acquire `pin_userptr` before `validate_shared_range` in `import_buffer` and `import_scattered_buffer`. - **Tests (`runtime/d3d12/src/tests/device/placement/lazy.rs`, `heap_backing.rs`):** - Unit tests covering 4 KiB page-edge overlap with imports, `UserptrRanges` refcounting, and `pin_userptr` publishing/releasing held textures while keeping pages accessible. - Hardware test (`userptr_pin_attached_to_heap_outlives_wrapper_and_submits_cleanly`) verifying the pin outlives the local `ID3D12Heap` wrapper while placed resources remain alive, releases cleanly once dropped, and submits cleanly without CS rejection. ## Verification - **Host checks (`fix/lazy-publication-userptr-634` at `cb4631ba`):** - `cargo fmt --all --check`, `tests/test_repository_hygiene.py` (`7/7`), `cargo clippy --locked --target x86_64-pc-windows-msvc -p tuxbox-d3d12 --all-targets -- -D warnings`, and `RUSTDOCFLAGS="-D warnings" cargo doc --locked --no-deps --target x86_64-pc-windows-msvc -p tuxbox-d3d12 --document-private-items`: all passed. - **Windows unit tests under GE-Proton Wine (`AMD Radeon RX 6900 XT (1002:73bf)` & `AMD Raphael Radeon 610M iGPU (1002:164e)`):** - `device::placement::lazy::tests`: `11/11` passed on both adapters. - `userptr_pin_attached_to_heap_outlives_wrapper_and_submits_cleanly`: passed on both `AMD Radeon RX 6900 XT (1002:73bf)` (`0.25s`) and `AMD Raphael Radeon 610M iGPU (1002:164e)` (`0.27s`). - **Real-world ProjectScarlett (`AMD Radeon RX 6900 XT (1002:73bf)`, Mesa `26.3.1` RADV, `TUXBOX_LAZY_PUBLICATION=1` ON):** - **Run 1:** `1,110.1s`, `10,031` presents (`Frame: 1437`), `39.8 MiB/s` texture readback — `0` CS rejections (`-EPERM`), `0` GPUVM faults, `0` `xmem` errors (vs 100% `-EPERM` CS rejection in seconds without this fix). - **Run 2:** `874.9s`, `10,201` presents (`Frame: 1531`), `33.1 MiB/s` readback, `2` live `published on access` VEH fault recoveries — `0` CS rejections, `0` GPUVM faults, `0` `xmem` errors. (Both runs later hit the unrelated game-side `Unsignaled CrossQueueBarrier` breakpoint while streaming.) - **Dual-GPU `tuxbox.py check --changed origin/main`:** - **`AMD Radeon RX 6900 XT (1002:73bf)`:** `23/23` D3D12 regression controls passed (including `mesh`, `buffer-remap`, `buffer-unmap-alive`, `buffer-unmap-released`, `alias-deferred`, `copy-resource`, `placement`, `heaps`, `tables`); `122/123` total checks passed — the sole failure is the pre-existing `#625` `lanes-selftest-texture-publication` crash (confirmed unrelated: passes with `TUXBOX_LAZY_PUBLICATION=0`). - **`AMD Raphael Radeon 610M iGPU (1002:164e)`:** `23/23` D3D12 regression controls passed; `121/122` total checks passed — same sole pre-existing `#625` `lanes-selftest-texture-publication` failure. Evidence: TuxBox check of cb4631ba67c4, no uncommitted changes, GPU 1002:73bf: 15 passed, 1 failed, 0 not run Evidence: TuxBox check of cb4631ba67c4, no uncommitted changes, GPU 1002:164e: 15 passed, 1 failed, 0 not run Fixes #634
Sign in to join this conversation.
No reviewers
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
brcly/TuxBox!640
No description provided.