d3d12: import XMem physical pages through their backing #218

Open
github-bot wants to merge 1 commit from github-bot/gh-218 into main AGit
Owner

@⁠Exorion1er on GitHub · 2026-11-26 09:13 UTC · original
Exorion1er:d3d12-xmem-backing → main · +473 −28 · 6 files · 1 commits · open

This is the d3d12 half of the direction in #191 (one private backing per physical page, GPU-side aliases). It does not change XMem. Without the two XMem exports below, imports behave exactly as on main.

What it does

  • Before importing a guest range, the provider asks XMem which host memory holds those bytes (TuxBoxAcquirePhysicalBacking) and imports that memory instead of the guest address. An alias address with no CPU memory behind it can then still be imported, and every alias of a page shares the same pages.
  • The import keeps an XMem hold on that memory until its native heap is released (TuxBoxReleasePhysicalBacking). That covers the -14 (EFAULT) crash @⁠JeffreyBytes found and @⁠DrArroway reproduced: on amdgpu, one live host-pointer import whose pages are gone makes every later submit fail.
  • Import caches stay keyed on the guest address, so remaps still retire imports as before. A cached import is reused only if the page is still backed by the same memory.

Contract for XMem (doc comment in runtime/d3d12/src/device/heap/backing.rs)

BOOL TuxBoxAcquirePhysicalBacking(UINT64 address, UINT64 bytes, UINT64* backing, UINT64* hold);
void TuxBoxReleasePhysicalBacking(UINT64 hold);
  • Success: *backing is the start of the contiguous host range holding the same bytes (it may equal address), and *hold is a cookie.
  • While a hold lives, XMem keeps that host range mapped to private memory, even if the title unmaps, remaps or frees the pages. Its contents after a remap don't matter, because the queue already retires imports of remapped pages.
  • ERROR_NOT_FOUND: no XMem physical page in the range, so the provider imports the address itself.
  • Any other error fails the import. That includes pages whose backings are not contiguous.

Not covered

  • Backings that are not contiguous. @⁠DrArroway's probe shows CreateReservedResource + UpdateTileMappings works for them. I left it out until we know how often it happens: @⁠gtb-sbe measured 100 of 119 alias imports as contiguous in guest addresses, but backing contiguity is still unmeasured.
  • The XMem side itself (@⁠gtb-sbe's plan).
  • NVIDIA. The code has no vendor path, but I haven't run it on an NVIDIA card.

Tests

New runtime/d3d12/src/tests/device/heap_backing.rs, with a fake XMem:

  • Contract: no exports means the address is imported; ERROR_NOT_FOUND falls back to the address; other errors fail the import; a hold is released once, when dropped.
  • GPU: one private page behind two PAGE_NOACCESS alias addresses. The GPU reads the CPU's bytes through the first alias. A GPU write through the first alias is visible through the second and to the CPU. Holds are released only when each heap is dropped. A page whose backing moved is imported again rather than reused.
  • Negative control: backing the same test with a pagefile section view (what XMem uses today) fails on RADV. The GPU reads zeros, so the test really catches the substitution.

Results, Radeon RX 9070 series (1002:7550), Mesa RADV 26.2.7, Linux 7.3.5, Proton Experimental, main 31dd5a3 + this change:

  • cargo test --locked --target x86_64-pc-windows-msvc -p tuxbox-d3d12, run through umu (Steam Linux Runtime 4): 148/149. The one failure, commands::memory_wait::tests::regressions::gpu_failed_mid_list_split_close_does_not_double_close_or_clobber_error, also fails on main.
  • D3D12 regression controls: buffer-import, buffer-import-reject, buffer-layout, buffer-remap, heaps all PASS.
  • python tuxbox.py check --proton … --prefix … --windows-dxc …: all checks passed (70/70 GPU gates: Lanes and fixtures, D3D12 regression controls, shader translator on the GPU).

🤖 Generated with Claude Code

**@⁠Exorion1er** on GitHub · 2026-11-26 09:13 UTC · [original](https://github.com/brcly/TuxBox/pull/218) `Exorion1er:d3d12-xmem-backing` → `main` · +473 −28 · 6 files · 1 commits · open This is the d3d12 half of the direction in #191 (one private backing per physical page, GPU-side aliases). It does not change XMem. Without the two XMem exports below, imports behave exactly as on `main`. **What it does** - Before importing a guest range, the provider asks XMem which host memory holds those bytes (`TuxBoxAcquirePhysicalBacking`) and imports that memory instead of the guest address. An alias address with no CPU memory behind it can then still be imported, and every alias of a page shares the same pages. - The import keeps an XMem hold on that memory until its native heap is released (`TuxBoxReleasePhysicalBacking`). That covers the `-14` (`EFAULT`) crash @⁠JeffreyBytes found and @⁠DrArroway reproduced: on amdgpu, one live host-pointer import whose pages are gone makes every later submit fail. - Import caches stay keyed on the guest address, so remaps still retire imports as before. A cached import is reused only if the page is still backed by the same memory. **Contract for XMem** (doc comment in `runtime/d3d12/src/device/heap/backing.rs`) ```text BOOL TuxBoxAcquirePhysicalBacking(UINT64 address, UINT64 bytes, UINT64* backing, UINT64* hold); void TuxBoxReleasePhysicalBacking(UINT64 hold); ``` - Success: `*backing` is the start of the contiguous host range holding the same bytes (it may equal `address`), and `*hold` is a cookie. - While a hold lives, XMem keeps that host range mapped to private memory, even if the title unmaps, remaps or frees the pages. Its contents after a remap don't matter, because the queue already retires imports of remapped pages. - `ERROR_NOT_FOUND`: no XMem physical page in the range, so the provider imports the address itself. - Any other error fails the import. That includes pages whose backings are not contiguous. **Not covered** - Backings that are not contiguous. @⁠DrArroway's probe shows `CreateReservedResource` + `UpdateTileMappings` works for them. I left it out until we know how often it happens: @⁠gtb-sbe measured 100 of 119 alias imports as contiguous in guest addresses, but backing contiguity is still unmeasured. - The XMem side itself (@⁠gtb-sbe's plan). - NVIDIA. The code has no vendor path, but I haven't run it on an NVIDIA card. **Tests** New `runtime/d3d12/src/tests/device/heap_backing.rs`, with a fake XMem: - Contract: no exports means the address is imported; `ERROR_NOT_FOUND` falls back to the address; other errors fail the import; a hold is released once, when dropped. - GPU: one private page behind two `PAGE_NOACCESS` alias addresses. The GPU reads the CPU's bytes through the first alias. A GPU write through the first alias is visible through the second and to the CPU. Holds are released only when each heap is dropped. A page whose backing moved is imported again rather than reused. - Negative control: backing the same test with a pagefile section view (what XMem uses today) fails on RADV. The GPU reads zeros, so the test really catches the substitution. Results, Radeon RX 9070 series (`1002:7550`), Mesa RADV 26.2.7, Linux 7.3.5, Proton Experimental, `main` `31dd5a3` + this change: - `cargo test --locked --target x86_64-pc-windows-msvc -p tuxbox-d3d12`, run through umu (Steam Linux Runtime 4): 148/149. The one failure, `commands::memory_wait::tests::regressions::gpu_failed_mid_list_split_close_does_not_double_close_or_clobber_error`, also fails on `main`. - D3D12 regression controls: `buffer-import`, `buffer-import-reject`, `buffer-layout`, `buffer-remap`, `heaps` all PASS. - `python tuxbox.py check --proton … --prefix … --windows-dxc …`: all checks passed (70/70 GPU gates: Lanes and fixtures, D3D12 regression controls, shader translator on the GPU). 🤖 Generated with [Claude Code](https://claude.com/claude-code)
Ask XMem which host memory holds a guest range before importing it
(TuxBoxAcquirePhysicalBacking) and import that memory, so GPU aliases
without CPU memory can be imported and every alias shares the same
pages. Keep an XMem hold on the memory until the native heap is
released (TuxBoxReleasePhysicalBacking): on amdgpu, a live host-pointer
import whose pages are gone makes every later submit fail with EFAULT.

Caches stay keyed on the guest address; a cached import is reused only
while the page is still backed by the same memory. Without the XMem
exports, imports behave as before.

Refs #191.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 10:55:54 +00:00
Author
Owner

@⁠JeffreyBytes on GitHub · 2026-11-26 09:31 UTC · original

Two things from the RX 6700 XT (1002:73df, Mesa RADV 26.3.1, GE-Proton11-14) that may help test this once the XMem half exists.

Baselines for the device loss in ProjectScarlett. With the ANONONLY preload (host profile section-import=aliased) every run that gets past the Account Picker ends the same way: one CopyPageMappingsBatchX batch logs page remap replaced imported range … retired stale GPU import (24 lines, which is the log cap, not the count), the next submit gets radv/amdgpu: The CS has been rejected (-14), then VK_ERROR_DEVICE_LOST. Five runs so far:

main time to device loss exit code
earlier main, cold shaders 96 min 3
ac4f099 814.9 s 3
ac4f099 214.3 s 5
773278a 340.9 s 5
773278a 138.8 s 5

It comes sooner the warmer the caches are, so it is a point in the title's loading rather than a timer. With shaders prepared I can reach it in two to six minutes, so I can run this PR plus the XMem side without the preload and say whether the game gets past that batch.

A control for the hold. In page_mapping.rs, apply() applies the remap and then only marks the import live = false; the native heap stays alive while placed resources hold it. That is the window this PR's hold closes. I have a small guest test for the same thing through slots 74 and 44, with private VirtualAlloc memory and the default host profile:

  • --unmap-alive: import, copy from it, VirtualFree the pages with the buffer still alive, then three unrelated upload-to-readback copies. RADV prints the -14 line and the copies read back 0 of 256 bytes.
  • --unmap-released: release the buffer first, then free the pages. The same copies read back 256 of 256.

It does not go through XMem, so this PR alone does not change its result. A variant on XMem pages (unmap or remap with the import alive, expect later submits to still work) would exercise the hold directly. I can open a PR with the test, or hand it over to be folded in here, whichever is more useful.

**@⁠JeffreyBytes** on GitHub · 2026-11-26 09:31 UTC · [original](https://github.com/brcly/TuxBox/pull/218#issuecomment-7188116614) Two things from the RX 6700 XT (`1002:73df`, Mesa RADV 26.3.1, GE-Proton11-14) that may help test this once the XMem half exists. **Baselines for the device loss in ProjectScarlett.** With the `ANONONLY` preload (host profile `section-import=aliased`) every run that gets past the Account Picker ends the same way: one `CopyPageMappingsBatchX` batch logs `page remap replaced imported range … retired stale GPU import` (24 lines, which is the log cap, not the count), the next submit gets `radv/amdgpu: The CS has been rejected (-14)`, then `VK_ERROR_DEVICE_LOST`. Five runs so far: | main | time to device loss | exit code | | --- | --- | --- | | earlier main, cold shaders | 96 min | 3 | | `ac4f099` | 814.9 s | 3 | | `ac4f099` | 214.3 s | 5 | | `773278a` | 340.9 s | 5 | | `773278a` | 138.8 s | 5 | It comes sooner the warmer the caches are, so it is a point in the title's loading rather than a timer. With shaders prepared I can reach it in two to six minutes, so I can run this PR plus the XMem side without the preload and say whether the game gets past that batch. **A control for the hold.** In `page_mapping.rs`, `apply()` applies the remap and then only marks the import `live = false`; the native heap stays alive while placed resources hold it. That is the window this PR's hold closes. I have a small guest test for the same thing through slots 74 and 44, with private `VirtualAlloc` memory and the default host profile: - `--unmap-alive`: import, copy from it, `VirtualFree` the pages with the buffer still alive, then three unrelated upload-to-readback copies. RADV prints the `-14` line and the copies read back 0 of 256 bytes. - `--unmap-released`: release the buffer first, then free the pages. The same copies read back 256 of 256. It does not go through XMem, so this PR alone does not change its result. A variant on XMem pages (unmap or remap with the import alive, expect later submits to still work) would exercise the hold directly. I can open a PR with the test, or hand it over to be folded in here, whichever is more useful.
Author
Owner

@⁠Exorion1er on GitHub · 2026-11-26 09:40 UTC · original

Thanks, both are useful. I'd keep the test in its own PR: it covers a hazard main has today and doesn't depend on #218, and the XMem-page variant (unmap or remap with the import alive, later submits still work) fits best alongside the XMem half, since that's the side that has to honour the hold. Your baselines and the 2–6 minute repro will be the real test of this plus the XMem side without the preload, so thanks for offering to run it.

**@⁠Exorion1er** on GitHub · 2026-11-26 09:40 UTC · [original](https://github.com/brcly/TuxBox/pull/218#issuecomment-7188323067) Thanks, both are useful. I'd keep the test in its own PR: it covers a hazard `main` has today and doesn't depend on #218, and the XMem-page variant (unmap or remap with the import alive, later submits still work) fits best alongside the XMem half, since that's the side that has to honour the hold. Your baselines and the 2–6 minute repro will be the real test of this plus the XMem side without the preload, so thanks for offering to run it.
Author
Owner

@⁠NickStallman on GitHub · 2026-11-26 09:57 UTC · original

NVIDIA run of python tuxbox.py check on this PR's head (4461c8f), since the description lists NVIDIA as not yet covered:

  • GPU: RTX 5090 (10de:2b85), driver 595.99.02, GE-Proton11-14, Linux 7.0.0. Host profile wave 32-32.
  • All groups passed: 100 gates. That includes Lanes and fixtures on the GPU, the D3D12 regression controls (buffer-remap among them) and the shader translator on the GPU.
  passed   Build the shader compiler
  passed   Python tests
  passed   Shader compiler tests
  passed   DXBC tests
  passed   XG layout tests
  passed   D3D12 Windows typecheck
  passed   Build
  passed   Lanes and fixtures on the GPU
  passed   D3D12 regression controls
  passed   Shader translator on the GPU

All checks passed.
**@⁠NickStallman** on GitHub · 2026-11-26 09:57 UTC · [original](https://github.com/brcly/TuxBox/pull/218#issuecomment-7188703749) NVIDIA run of `python tuxbox.py check` on this PR's head (`4461c8f`), since the description lists NVIDIA as not yet covered: - GPU: RTX 5090 (10de:2b85), driver 595.99.02, GE-Proton11-14, Linux 7.0.0. Host profile wave 32-32. - All groups passed: 100 gates. That includes Lanes and fixtures on the GPU, the D3D12 regression controls (`buffer-remap` among them) and the shader translator on the GPU. ``` passed Build the shader compiler passed Python tests passed Shader compiler tests passed DXBC tests passed XG layout tests passed D3D12 Windows typecheck passed Build passed Lanes and fixtures on the GPU passed D3D12 regression controls passed Shader translator on the GPU All checks passed. ```
Author
Owner

@⁠DrArroway on GitHub · 2026-11-26 11:22 UTC · original

Tested 4461c8f on RX 6900 XT (1002:73bf, Mesa RADV 26.3.1, GE-Proton11-14):

  • python tuxbox.py check --adapter 1002:73bf: all CI/build steps + 70/70 GPU gates pass (1795427215 GE-Proton11-14).
  • tuxbox-d3d12 tests under GE-Proton11-14: 6/6 device::heap::backing::tests pass (0.10s, including aliases_import_their_private_backing, a_moved_backing_is_imported_again, and a_backing_is_held_until_dropped_and_released_once), and 148/149 overall (only the pre-existing gpu_failed_mid_list_split_close_does_not_double_close_or_clobber_error).
  • buffer-remap, buffer-import, buffer-import-reject, buffer-layout (TUXBOX_REQUIRE_BUFFER_IMPORT=1, TUXBOX_TRACE_BUFFER_BACKING=1): all 4/4 PASS.
**@⁠DrArroway** on GitHub · 2026-11-26 11:22 UTC · [original](https://github.com/brcly/TuxBox/pull/218#issuecomment-7190652988) Tested `4461c8f` on RX 6900 XT (`1002:73bf`, Mesa RADV `26.3.1`, `GE-Proton11-14`): - `python tuxbox.py check --adapter 1002:73bf`: all CI/build steps + **70/70** GPU gates pass (`1795427215 GE-Proton11-14`). - `tuxbox-d3d12` tests under `GE-Proton11-14`: **6/6** `device::heap::backing::tests` pass (`0.10s`, including `aliases_import_their_private_backing`, `a_moved_backing_is_imported_again`, and `a_backing_is_held_until_dropped_and_released_once`), and **148/149** overall (only the pre-existing `gpu_failed_mid_list_split_close_does_not_double_close_or_clobber_error`). - `buffer-remap`, `buffer-import`, `buffer-import-reject`, `buffer-layout` (`TUXBOX_REQUIRE_BUFFER_IMPORT=1`, `TUXBOX_TRACE_BUFFER_BACKING=1`): all **4/4 PASS**.
Author
Owner

@⁠ReverendOzmund on GitHub · 2026-11-26 11:53 UTC · original

Reviewed on the NVIDIA side. As far as I can tell this is dormant until XMem gains the two exports: neither TuxBoxAcquirePhysicalBacking nor TuxBoxReleasePhysicalBacking exists in xmem on main or in this branch, so lookup() returns None and every import keeps its guest address. The drop order in ImportedHeap (native, then shadow, then the hold) looks right for the EFAULT case. I'll run the D3D12 regression controls and Lanes on an RTX 3080 Ti (GE-Proton11-14) and post the result here.

**@⁠ReverendOzmund** on GitHub · 2026-11-26 11:53 UTC · [original](https://github.com/brcly/TuxBox/pull/218#issuecomment-7191300687) Reviewed on the NVIDIA side. As far as I can tell this is dormant until XMem gains the two exports: neither TuxBoxAcquirePhysicalBacking nor TuxBoxReleasePhysicalBacking exists in xmem on main or in this branch, so lookup() returns None and every import keeps its guest address. The drop order in ImportedHeap (native, then shadow, then the hold) looks right for the EFAULT case. I'll run the D3D12 regression controls and Lanes on an RTX 3080 Ti (GE-Proton11-14) and post the result here.
Author
Owner

@⁠gtb-sbe on GitHub · 2026-11-26 13:46 UTC · original · edited 2026-11-26 13:54 UTC

Thanks for building the d3d12 half against that plan. I can't take on the XMem half right now, so it's open for anyone who wants it. I can still provide the measurements below and AMD runs.

Two things I can measure on ProjectScarlett (RX 9060 XT, RADV) before or while it's written. I have a local trace branch that describes every page behind an import (views, reservations, which views the CPU touches, through /proc/<pid>/pagemap):

  1. Backing contiguity: You noted it is still unmeasured. In my last census, CPU-used views were always in plain read-write reservations, and 11,771 of the 14,967 multi-view pages had no CPU-used view at all. So the backing would be the CPU view where one exists and a new private arena otherwise. Whether one import's backing is contiguous then depends on how the arena hands out pages. I can report, per import, whether the existing CPU views of its pages are contiguous. That shows how often CreateReservedResource + UpdateTileMappings would be needed if XMem keeps CPU views where they are.
  2. How often a page changes backing: Every remap that moves a page's CPU view would retire its imports. I can count those per minute up to the 22-draws-per-frame state, so the cost of retiring is known before tuning it.

For testing: this card runs main (9b31fd8) with the full check passing on the #200 copy path (section-import=substituted), and ProjectScarlett reaches a the Account Picker without the preload. So it's a usable baseline to compare against once the XMem exports exist.

**@⁠gtb-sbe** on GitHub · 2026-11-26 13:46 UTC · [original](https://github.com/brcly/TuxBox/pull/218#issuecomment-7193820560) · edited 2026-11-26 13:54 UTC Thanks for building the d3d12 half against that plan. I can't take on the XMem half right now, so it's open for anyone who wants it. I can still provide the measurements below and AMD runs. Two things I can measure on ProjectScarlett (RX 9060 XT, RADV) before or while it's written. I have a local trace branch that describes every page behind an import (views, reservations, which views the CPU touches, through `/proc/<pid>/pagemap`): 1. **Backing contiguity:** You noted it is still unmeasured. In my last census, CPU-used views were always in plain read-write reservations, and 11,771 of the 14,967 multi-view pages had no CPU-used view at all. So the backing would be the CPU view where one exists and a new private arena otherwise. Whether one import's backing is contiguous then depends on how the arena hands out pages. I can report, per import, whether the existing CPU views of its pages are contiguous. That shows how often `CreateReservedResource` + `UpdateTileMappings` would be needed if XMem keeps CPU views where they are. 2. **How often a page changes backing:** Every remap that moves a page's CPU view would retire its imports. I can count those per minute up to the 22-draws-per-frame state, so the cost of retiring is known before tuning it. For testing: this card runs `main` (`9b31fd8`) with the full `check` passing on the #200 copy path (`section-import=substituted`), and ProjectScarlett reaches a the Account Picker without the preload. So it's a usable baseline to compare against once the XMem exports exist.
This pull request can be merged automatically.
This branch is out-of-date with the base branch
You are not authorized to merge this pull request.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin '+refs/pull/218/head:github-bot/gh-218'
git switch 'github-bot/gh-218'

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-218'
git switch 'github-bot/gh-218'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-218'
git switch 'github-bot/gh-218'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-218'
git switch 'main'
git merge --squash 'github-bot/gh-218'
git switch 'main'
git merge --ff-only 'github-bot/gh-218'
git switch 'main'
git merge 'github-bot/gh-218'
git push origin 'main'
This discussion has been locked. Commenting is limited to contributors.
No description provided.