RT-RADV-IMPORT-PINS: d3d12: free declined host imports at once and pin the pages of live ones (RADV CS rejection) #1539

Manually merged
github merged 4 commits from gh-pr/1539/head into gh-pr/1539/base 2026-11-26 09:55:49 +00:00
Owner

@⁠aslan2142 on GitHub · 2026-11-25 03:52 UTC · original
aslan2142:radv-import-pins → main · +255 −12 · 5 files · 4 commits · merged 2026-11-25 08:28 UTC

Item: RT-RADV-IMPORT-PINS, NateWeav's claim (#14). The finding, the measurements and the design are theirs (that comment and #14). This implements their description on current main, as offered in #14. @⁠NateWeav, if you'd rather open your own patch, say so and this one closes.

Not included: RT-SHADOW-ALIAS-GUARD (their second claim, the copy cost after the fix).

What and why

On RADV, making a page read-only while a VK_EXT_external_memory_host import of it is live makes the kernel reject that submission and every later one: radv/amdgpu: The CS has been rejected, then VK_ERROR_DEVICE_LOST. The slot-133 shadow trackers guard copied title pages exactly that way, so main loses its device right after the first present on RADV.

  • Declined imports are freed at once. create_host_buffer (runtime/shims/d3d12_vulkan_device.cpp) now destroys the buffer and frees the memory on every decline after vkCreateBuffer: below the arena, host-pointer properties, a type or offset mismatch, a failed allocation or bind. Before, the memory waited in the deferred queue while the shadow made instead guarded the same page.
  • Live imports pin their pages. The slot-133 and console-heap imports take a kernel_writes::hold pin over their pages (runtime/src/title_page_watch.h, new hold/release) before vkAllocateMemory, and release it on every path that frees the memory (decline, fail(), the resource destructor, remove_console_heap). ShadowPages and the geometry watch already pass over pinned pages. A hold records the pin even before a tracker exists and lifts a guard already on the range. Taking it before the allocation means no guard can land between the writability check and the import.
  • The pin is unconditional; a driver gate (pin only where the driver does not pin at import) can follow, as NateWeav noted.

Tests

  • kernel_writes_test: 9 checks of hold and release.
  • d3d12_title_va_test (imports-on mode, d3d12_geometry_views):
    • check_declined_imports: no import memory outlives a declined import across a submission (on the RDNA2 iGPU both buffers land below the arena, the path the first change fixes);
    • check_import_over_shadow: a shadow's tracker guards its pages (control), an import of the same pages keeps them writable across two submissions, the pin ends with the import and the guard returns. On llvmpipe the import is taken and pinned; on the iGPU it is declined and no pin is left.
  • Windows suite under Wine 11.22 (clang-cl build), llvmpipe and an RDNA2 iGPU (RADV): no test goes from pass to fail; d3d12_geometry_views goes from fail to pass on the iGPU.

Measured on the retail boot (RDNA2 iGPU, RADV, Wine; counts only)

main this branch
Run time / end 14 s, title int3 after the device loss 22-58 s, the title's crash reporter (a separate stop, under investigation)
D3D12 calls 62,677 87,892 to 99,457
Presents ok / failed 1 / 1 89 to 126 / 0
CS rejected / device lost yes / yes 0 / 0 in 5 of 5 runs

AI-assisted (Claude Code, Claude Opus 5.5).

**@⁠aslan2142** on GitHub · 2026-11-25 03:52 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/1539) `aslan2142:radv-import-pins` → `main` · +255 −12 · 5 files · 4 commits · merged 2026-11-25 08:28 UTC Item: `RT-RADV-IMPORT-PINS`, **NateWeav's claim** (https://git.gtahub.st/github/ubiquitarianism--leonida/issues/14). The finding, the measurements and the design are theirs (that comment and https://git.gtahub.st/github/ubiquitarianism--leonida/issues/14). This implements their description on current `main`, as offered in https://git.gtahub.st/github/ubiquitarianism--leonida/issues/14. @⁠NateWeav, if you'd rather open your own patch, say so and this one closes. Not included: `RT-SHADOW-ALIAS-GUARD` (their second claim, the copy cost after the fix). ### What and why On RADV, making a page read-only while a `VK_EXT_external_memory_host` import of it is live makes the kernel reject that submission and every later one: `radv/amdgpu: The CS has been rejected`, then `VK_ERROR_DEVICE_LOST`. The slot-133 shadow trackers guard copied title pages exactly that way, so `main` loses its device right after the first present on RADV. - **Declined imports are freed at once.** `create_host_buffer` (`runtime/shims/d3d12_vulkan_device.cpp`) now destroys the buffer and frees the memory on every decline after `vkCreateBuffer`: below the arena, host-pointer properties, a type or offset mismatch, a failed allocation or bind. Before, the memory waited in the deferred queue while the shadow made instead guarded the same page. - **Live imports pin their pages.** The slot-133 and console-heap imports take a `kernel_writes::hold` pin over their pages (`runtime/src/title_page_watch.h`, new `hold`/`release`) before `vkAllocateMemory`, and release it on every path that frees the memory (decline, `fail()`, the resource destructor, `remove_console_heap`). `ShadowPages` and the geometry watch already pass over pinned pages. A hold records the pin even before a tracker exists and lifts a guard already on the range. Taking it before the allocation means no guard can land between the writability check and the import. - The pin is unconditional; a driver gate (pin only where the driver does not pin at import) can follow, as NateWeav noted. ### Tests - `kernel_writes_test`: 9 checks of hold and release. - `d3d12_title_va_test` (imports-on mode, `d3d12_geometry_views`): - `check_declined_imports`: no import memory outlives a declined import across a submission (on the RDNA2 iGPU both buffers land below the arena, the path the first change fixes); - `check_import_over_shadow`: a shadow's tracker guards its pages (control), an import of the same pages keeps them writable across two submissions, the pin ends with the import and the guard returns. On llvmpipe the import is taken and pinned; on the iGPU it is declined and no pin is left. - Windows suite under Wine 11.22 (clang-cl build), llvmpipe and an RDNA2 iGPU (RADV): no test goes from pass to fail; `d3d12_geometry_views` goes from fail to pass on the iGPU. ### Measured on the retail boot (RDNA2 iGPU, RADV, Wine; counts only) | | `main` | this branch | |---|---|---| | Run time / end | 14 s, title `int3` after the device loss | 22-58 s, the title's crash reporter (a separate stop, under investigation) | | D3D12 calls | 62,677 | 87,892 to 99,457 | | Presents ok / failed | 1 / 1 | 89 to 126 / 0 | | CS rejected / device lost | yes / yes | 0 / 0 in 5 of 5 runs | AI-assisted (Claude Code, Claude Opus 5.5).
create_host_buffer returned E_NOTIMPL for an import the driver put below
the arena with its memory still allocated. The resource's destructor then
handed that memory to the deferred queue, so it outlived the next
submission while the shadow buffer made instead over the same address
guarded its page read-only. On RADV a read-only page under a live
VK_EXT_external_memory_host import makes the kernel reject that
submission and every later one ("The CS has been rejected", then
VK_ERROR_DEVICE_LOST).

Every return after vkCreateBuffer that declines the import now destroys
the buffer and frees the memory before it returns; nothing was submitted
with them, since the resource is registered only on success. A counter of
live slot-133 imports (rt::d3d12::title_host_imports) lets a test see it.

The finding and the design are NateWeav's (coordination issue #14,
comments 7141859164 and 7142359977); this implements their description.

Test: d3d12_title_va_test in its imports-on mode (d3d12_geometry_views)
places two buffers, at a page start and 4 bytes into a page, submits with
each alive and checks that no import memory outlives a declined import.
On the RDNA2 iGPU under Wine both land below the arena, the path fixed
here.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
RADV checks the pages of a VK_EXT_external_memory_host import again at
every submission, and once one of them is read-only the kernel rejects
that submission and every later one. The slot-133 shadow trackers and the
geometry watch make copied title pages read-only to see the next write,
and a page can be both under a shadow and under a live import.

A new leonida::kernel_writes::hold pins a range until release() and opens
it. Unlike begin_write it records the pin when no opener is set yet (no
tracker exists), so a tracker made later passes over the pages too;
ShadowPages and the geometry watch already pass over pinned pages. The
slot-133 import (create_host_buffer) and the console descriptor heap
import take the pin before vkAllocateMemory, so no guard lands between
the writability check and the import, and end it on every path that
frees the memory: a declined or failed import and the release.

The finding and the design are NateWeav's (coordination issue #14,
comments 7141859164 and 7142359977); this implements their description.
As in their tree the pin is unconditional; a driver gate is not done.

Tests: kernel_writes_test checks hold and release on the pin set (no
opener, with an opener, alongside a file read's pin). d3d12_title_va_test
in its imports-on mode makes a shadow over pages, checks that its tracker
guards them, then imports the same pages and checks that two submissions
leave them writable, that the pin ends with the import and that the
tracker guards them again afterwards.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 09:55:48 +00:00
github manually merged commit 8665eee910 into gh-pr/1539/base 2026-11-26 09:55:49 +00:00
Author
Owner

@⁠aslan2142 on GitHub · 2026-11-25 08:28 UTC · original

Merged, with the maintainer's approval, after merging main into it. @⁠NateWeav, thank you: the finding, the measurements and the design are yours. This only implements your description, and the commits say so.

  • Why now: without it, main loses its Vulkan device right after the first present on RADV. With it, main runs the retail boot for 15 minutes on an RDNA2 iGPU without a crash (counts in the description).
  • Tests:
    • Windows suite under Wine on main + this branch (llvmpipe and an RDNA2 iGPU): nothing goes from pass to fail. d3d12_geometry_views goes from fail to pass on the iGPU.
    • kernel_writes_test and d3d12_title_va_test cover the hold/release pin and the declined import.
  • Next:
    • Your second claim, RT-SHADOW-ALIAS-GUARD (the copy cost, about 600 MiB per submission here), is being carried the same way, with credit, from your description in #14.
    • The driver gate (pin only where a driver needs it) is still open for whoever wants it.
  • Checked on main 3f81d46ac + 8665eee91: it merges cleanly; the semantic suite passes (487 files, 6,279 tests, 0 files failed); the 14 docs and writer tests pass; check_no_game_data.py is clean on the 5 changed files.

AI-assisted (Claude Code, Claude Opus 5.5).

**@⁠aslan2142** on GitHub · 2026-11-25 08:28 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/1539#issuecomment-7157078099) Merged, with the maintainer's approval, after merging `main` into it. @⁠NateWeav, thank you: the finding, the measurements and the design are yours. This only implements your description, and the commits say so. - **Why now:** without it, `main` loses its Vulkan device right after the first present on RADV. With it, `main` runs the retail boot for 15 minutes on an RDNA2 iGPU without a crash (counts in the description). - **Tests:** - Windows suite under Wine on `main` + this branch (llvmpipe and an RDNA2 iGPU): nothing goes from pass to fail. `d3d12_geometry_views` goes from fail to pass on the iGPU. - `kernel_writes_test` and `d3d12_title_va_test` cover the hold/release pin and the declined import. - **Next:** - Your second claim, `RT-SHADOW-ALIAS-GUARD` (the copy cost, about 600 MiB per submission here), is being carried the same way, with credit, from your description in #14. - The driver gate (pin only where a driver needs it) is still open for whoever wants it. - **Checked on `main` `3f81d46ac` + `8665eee91`:** it merges cleanly; the semantic suite passes (487 files, 6,279 tests, 0 files failed); the 14 docs and writer tests pass; `check_no_game_data.py` is clean on the 5 changed files. AI-assisted (Claude Code, Claude Opus 5.5).
Author
Owner

@⁠adrianmjim on GitHub · 2026-11-25 09:04 UTC · original

Post-merge note. I tested d4f3e0bbc applied onto main 42537e7b6; the merged head 8665eee91 only adds a merge of main on top. The test machine is Windows with an AMD Radeon RX 7900 XT (RDNA3) and AMD driver 32.0.32029.2010 (26.11.2). The card has no Resizable BAR, the OS is Windows 11 build 26300, and the build is VS 2022 RelWithDebInfo. Host-pointer imports are taken on this driver, so this is the pin path on real AMD hardware, not llvmpipe. No game files.

The new checks come from d3d12_title_va_test.exe with LEONIDA_GEOMETRY_MB=16 LEONIDA_VK_VALIDATION=1:

slot-133 buffer at page offset 0: imported, 1 import(s) live
slot-133 buffer at page offset 4: declined (shadow), 0 import(s) live
declined imports: 1 of 2 placements took the shadow, none left import memory behind
import over a shadow's pages: the tracker guarded them alone; the second buffer imported, 64 KiB pinned

So the import over a shadow's pages was taken, and the pin held 64 KiB. There were no validation messages.

d3d12_geometry_views: with the default arena this test fails on this card on main too. On a GPU without Resizable BAR the 256 MiB arena fills the 256 MiB BAR heap, so the views can't be allocated ("CPU-written table V# 0/64"); that is #1678, unrelated to this PR. With LEONIDA_ARENA_MB=240 the whole test passes with this PR: 64/64 for the table V#, the CBV and the root view, and the four lines above unchanged.

Other ctests:

  • I ran 23 tests related to the change, and 22 of 23 pass. The exception is d3d12_geometry_views with the default arena, as above. The 23 are:
    • kernel_writes, xmem_write_watch, title_write_watch, title_write_watch_on
    • vk_host_at_aliasing, vk_host_region_import
    • d3d12_existing_heap (plus _no_write_watch and _write_watch), d3d12_xmem_placed, d3d12_console_descriptor_heap
    • xbox_linear_texture_map, xbox_linear_gpu_staging
    • d3d12_title_va, d3d12_geometry, d3d12_geometry_views, d3d12_title_index_window, d3d12_title_vertex_window
    • d3d12_api_descriptor_copy (and _off), d3d12_rebound_tables (and _off), d3d12_rebound_second_device
  • Whole ctest -j 6: 398 of 402 without failures (385 passed, 13 skipped), 4 failed. Three failures are the same as on main: d3d12_texture_tiles, d3d12_clear and d3d12_geometry_views. The fourth, xmem_no_write_watch, failed once under -j 6 and passes 3 of 3 alone, on this PR and on main.

I didn't measure the cost of keeping every live import pinned for its whole lifetime, because that needs a retail boot. On this driver page-aligned slot-133 buffers are imported, and every live import now holds a pin. The pin count is global, so for_each_pin, called by every shadow-tracker refresh and every geometry-watch arm, now takes its mutex and walks every live pin, even for ranges no import touches. Before, it returned at once on a zero count. A boot comparing the shadow and descriptor-heap refresh times before and after would show whether that matters.

**@⁠adrianmjim** on GitHub · 2026-11-25 09:04 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/1539#issuecomment-7157844993) Post-merge note. I tested `d4f3e0bbc` applied onto `main` `42537e7b6`; the merged head `8665eee91` only adds a merge of `main` on top. The test machine is Windows with an AMD Radeon RX 7900 XT (RDNA3) and AMD driver 32.0.32029.2010 (26.11.2). The card has no Resizable BAR, the OS is Windows 11 build 26300, and the build is VS 2022 RelWithDebInfo. Host-pointer imports are taken on this driver, so this is the pin path on real AMD hardware, not llvmpipe. No game files. **The new checks** come from `d3d12_title_va_test.exe` with `LEONIDA_GEOMETRY_MB=16 LEONIDA_VK_VALIDATION=1`: ``` slot-133 buffer at page offset 0: imported, 1 import(s) live slot-133 buffer at page offset 4: declined (shadow), 0 import(s) live declined imports: 1 of 2 placements took the shadow, none left import memory behind import over a shadow's pages: the tracker guarded them alone; the second buffer imported, 64 KiB pinned ``` So the import over a shadow's pages was taken, and the pin held 64 KiB. There were no validation messages. **`d3d12_geometry_views`:** with the default arena this test fails on this card on `main` too. On a GPU without Resizable BAR the 256 MiB arena fills the 256 MiB BAR heap, so the views can't be allocated ("CPU-written table V# 0/64"); that is #1678, unrelated to this PR. With `LEONIDA_ARENA_MB=240` the whole test passes with this PR: 64/64 for the table V#, the CBV and the root view, and the four lines above unchanged. **Other ctests:** - I ran 23 tests related to the change, and 22 of 23 pass. The exception is `d3d12_geometry_views` with the default arena, as above. The 23 are: - `kernel_writes`, `xmem_write_watch`, `title_write_watch`, `title_write_watch_on` - `vk_host_at_aliasing`, `vk_host_region_import` - `d3d12_existing_heap` (plus `_no_write_watch` and `_write_watch`), `d3d12_xmem_placed`, `d3d12_console_descriptor_heap` - `xbox_linear_texture_map`, `xbox_linear_gpu_staging` - `d3d12_title_va`, `d3d12_geometry`, `d3d12_geometry_views`, `d3d12_title_index_window`, `d3d12_title_vertex_window` - `d3d12_api_descriptor_copy` (and `_off`), `d3d12_rebound_tables` (and `_off`), `d3d12_rebound_second_device` - Whole `ctest -j 6`: 398 of 402 without failures (385 passed, 13 skipped), 4 failed. Three failures are the same as on `main`: `d3d12_texture_tiles`, `d3d12_clear` and `d3d12_geometry_views`. The fourth, `xmem_no_write_watch`, failed once under `-j 6` and passes 3 of 3 alone, on this PR and on `main`. I didn't measure the cost of keeping every live import pinned for its whole lifetime, because that needs a retail boot. On this driver page-aligned slot-133 buffers are imported, and every live import now holds a pin. The pin count is global, so `for_each_pin`, called by every shadow-tracker refresh and every geometry-watch arm, now takes its mutex and walks every live pin, even for ranges no import touches. Before, it returned at once on a zero count. A boot comparing the shadow and descriptor-heap refresh times before and after would show whether that matters.
Author
Owner

@⁠djrolfes on GitHub · 2026-11-25 11:20 UTC · original

Independent NVIDIA/Wine follow-up of the exact two implementation commits (599fe9f22 + 9f82f136f) is now building locally. RTX 3070 Ti 8 GiB, NVIDIA 620.73.04, Wine 11.22, clang-cl/xwin; main 42537e7b6 + owned startup/FSPX/input fixes, isolated task worktree.

The pre-pinning control intermittently logs VK_ERROR_OUT_OF_HOST_MEMORY for 512-byte to small-MiB allocations despite substantial free memory. Latest bounded run stopped at 33.336 s on a title assertion after a format-19 1280x720 depth placement returned E_OUTOFMEMORY, before any scripted input. Peak sampled total GPU use 2,347 MiB; minimum available RAM 13.97 GiB. Kernel journal in that run records three host-memory mapping failures. Main’s allocation mutex and six-retry policy are already present in this control. This differs from #1558’s original unsupported D16S8-UAV refusal, which is fixed in the candidate.

This is consistent with (not proof of) the guarded-page/import interaction your patch addresses. I am testing kernel_writes and title-VA/geometry cases before repeating the bounded retail control. No skipped compute, assertion or watchdog bypass; package read-only and network isolated. No new implementation claim; raw logs/addresses remain local. AI-assisted.

**@⁠djrolfes** on GitHub · 2026-11-25 11:20 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/1539#issuecomment-7160914012) Independent NVIDIA/Wine follow-up of the exact two implementation commits (599fe9f22 + 9f82f136f) is now building locally. RTX 3070 Ti 8 GiB, NVIDIA 620.73.04, Wine 11.22, clang-cl/xwin; main 42537e7b6 + owned startup/FSPX/input fixes, isolated task worktree. The pre-pinning control intermittently logs VK_ERROR_OUT_OF_HOST_MEMORY for 512-byte to small-MiB allocations despite substantial free memory. Latest bounded run stopped at 33.336 s on a title assertion after a format-19 1280x720 depth placement returned E_OUTOFMEMORY, before any scripted input. Peak sampled total GPU use 2,347 MiB; minimum available RAM 13.97 GiB. Kernel journal in that run records three host-memory mapping failures. Main’s allocation mutex and six-retry policy are already present in this control. This differs from #1558’s original unsupported D16S8-UAV refusal, which is fixed in the candidate. This is consistent with (not proof of) the guarded-page/import interaction your patch addresses. I am testing kernel_writes and title-VA/geometry cases before repeating the bounded retail control. No skipped compute, assertion or watchdog bypass; package read-only and network isolated. No new implementation claim; raw logs/addresses remain local. AI-assisted.
Author
Owner

@⁠djrolfes on GitHub · 2026-11-25 11:26 UTC · original · edited 2026-11-25 19:14 UTC

First NVIDIA follow-up at local head 02f20b221 (exact 599fe9f22 + 9f82f136f implementation commits) is green: kernel_writes, d3d12_title_va, d3d12_geometry_views, gameinput and gameinput_script pass 5/5 under isolated Wine. The subsequent 180-second bounded retail run completes its limit with no vkAllocateMemory failure log and zero kernel host-memory mapping failures in that interval (the preceding unpinned run had 16 rate-limited allocation-error logs and three kernel mapping failures before a fatal depth-allocation assertion).

25,450/25,450 native graphics PSOs and 16,852 presents succeeded; no fatal title exception or device-loss log. Visually still first-run setup, not a world frame. Assertions/watchdog enabled, no skips/stubs. This is one before/after observation, not a reliability claim or proof that every earlier allocation failure had the same cause. Continuing the bounded comparison. RTX 3070 Ti / NVIDIA 620.73.04 / Wine 11.22. AI-assisted; raw evidence stays local.

Count correction: the earlier total accidentally summed the presentation and queue trace layers. This count now uses only XboxPresentation::PresentX, excluding the duplicate queue-layer event. Other observations and limitations are unchanged.

**@⁠djrolfes** on GitHub · 2026-11-25 11:26 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/1539#issuecomment-7161054161) · edited 2026-11-25 19:14 UTC First NVIDIA follow-up at local head 02f20b221 (exact 599fe9f22 + 9f82f136f implementation commits) is green: kernel_writes, d3d12_title_va, d3d12_geometry_views, gameinput and gameinput_script pass 5/5 under isolated Wine. The subsequent 180-second bounded retail run completes its limit with no vkAllocateMemory failure log and zero kernel host-memory mapping failures in that interval (the preceding unpinned run had 16 rate-limited allocation-error logs and three kernel mapping failures before a fatal depth-allocation assertion). 25,450/25,450 native graphics PSOs and 16,852 presents succeeded; no fatal title exception or device-loss log. Visually still first-run setup, not a world frame. Assertions/watchdog enabled, no skips/stubs. This is one before/after observation, not a reliability claim or proof that every earlier allocation failure had the same cause. Continuing the bounded comparison. RTX 3070 Ti / NVIDIA 620.73.04 / Wine 11.22. AI-assisted; raw evidence stays local. Count correction: the earlier total accidentally summed the presentation and queue trace layers. This count now uses only XboxPresentation::PresentX, excluding the duplicate queue-layer event. Other observations and limitations are unchanged.
This discussion has been locked. Commenting is limited to contributors.
No description provided.