Skip to content

xmem: back physical pages with private memory and export backing holds - #258

Open
DrArroway wants to merge 4 commits into
brcly:mainfrom
DrArroway:feat/xmem-physical-backing
Open

DrArroway wants to merge 4 commits into
brcly:mainfrom
DrArroway:feat/xmem-physical-backing

Conversation

@DrArroway

Copy link
Copy Markdown
Contributor

Implements the runtime/xmem/ half of #191 so RADV can import XMem physical pages directly via VK_EXT_external_memory_host without hitting libdrm's AMDGPU_GEM_USERPTR_ANONONLY rejection or falling back to the #200 shadow copy. Pairs with #218 (already on main) and #253.

Part of #191

Summary

  • Private anonymous physical backings (MEM_PRIVATE, PAGE_READWRITE):
    • Single CPU views and GPU aliases allocate MEM_PRIVATE pages inside placeholder apertures rather than section views (runtime/xmem/src/physical_pages/private_views.rs, runtime/xmem/src/shared_pages.rs).
    • Mapping a second CPU view of the same token lazily promotes it to SharedSection so existing multi-view CPU aliases remain coherent.
    • Pure GPU aliases (or contiguous ranges with non-contiguous/conflicting backing order) promote unbacked tokens on demand into a shared PrivateArena (VirtualAlloc MEM_PRIVATE).
  • Lifetime holds & deferred unmaps (runtime/xmem/src/physical_pages/backing.rs):
    • Exports TuxBoxAcquirePhysicalBacking, TuxBoxAcquirePhysicalBackingRuns, and TuxBoxReleasePhysicalBacking as PRIVATE symbols in runtime/xmem/xmem.def.
    • Unmapping, remapping, or freeing held backing ranges defers virtual teardown until TuxBoxReleasePhysicalBacking(hold) drops the last reference, keeping imported MEM_PRIVATE host pages valid across live GPU submissions.
  • Unit tests (runtime/xmem/src/tests/physical_backing.rs):
    • Covers private CPU view acquisition + ERROR_NOT_FOUND on foreign memory, held CPU views surviving unmap/free, PrivateArena + conflicting-order BackingRun splitting + ERROR_MORE_DATA, and lazy SharedSection promotion on a second CPU view.

Verification

Tested on RX 6900 XT (1002:73bf), Linux (7.3.6-1-cachyos), GE-Proton11-14 (1795427215), RADV:

  • cargo check --locked --target x86_64-pc-windows-msvc --manifest-path runtime/xmem/Cargo.toml: passed (0 warnings)
  • Wine cargo test --locked --target x86_64-pc-windows-msvc --manifest-path runtime/xmem/Cargo.toml -- --test-threads=1: 4/4 passed (physical_pages::backing::tests)
  • python -m unittest tests.test_repository_hygiene: 5/5 passed
  • tools/check_d3d12_regressions.py --case buffer-remap --adapter 1002:73bf: PASS (4.418s, d3d12-buffer-remap PASS pci=1002:73bf words=768/768 identical=1 replacement=1, null_fallback_enabled: false)
    • Log confirmed TuxBox d3d12_xs: game memory is imported through its XMem backing and three path=imported entries, with zero copied for the GPU messages.
  • Full tuxbox.py check --adapter 1002:73bf: all 10 groups passed:
    • Lanes and fixtures on the GPU: 72/72 passed (170s, including xmem-pages and xmem-scatter)
    • D3D12 regression controls: 19/19 passed (85s)
    • Shader translator on the GPU: 40/40 passed (238s)

@JeffreyBytes

JeffreyBytes commented Nov 26, 2026 •

Copy link
Copy Markdown
Contributor

ProjectScarlett run on the RX 6700 XT without the preload, as asked on #191. Short version: with this PR's xmem.dll the title hangs during start-up, before the first frame, so I can't say anything yet about the remap.

Setup: RX 6700 XT (1002:73df), Mesa RADV 26.3.1, Linux 7.3.6, GE-Proton11-14, ProjectScarlett 1.0.2.1 offline, shaders prepared, no LD_PRELOAD. Both xmem.dll and d3d12_xs.dll rebuilt and staged for each row.

tree result
main 5c5677b (this PR's base) starts normally on the copy path, 6 presents per 11 s, still running when I stopped it at 240 s
5c5677b + this PR (cd8ad92) exit code 5 after 57.5 s, no present
5c5677b + this PR + #253 (f9b630d) exit code 5 after 64.2 s and 60.6 s (two runs), no present

What the failing runs have in common

  • The title's crash log says Game State: System Init, Frame: 0. The stack is game_scarlett_master.exe+0x2215C35 ← +0x2215ECA, which is the 30 s main-thread watchdog from ProjectScarlett: 30 s main-thread watchdog (exe+0x23CF683) at the story intro from cross-queue label-wait deadlocks #215. So the main thread stopped making progress about 20 to 30 s after launch and the watchdog ended the run.
  • The last line from the provider is the same every time:
    TuxBox d3d12_xs: created pipeline from Xbox slot 86 stream (688 bytes)
    
    On the base commit the next lines are prepared compute shader loaded …, created compute pipeline with 6 reflected FSSM descriptor slots, memory wait trace #1 … and submitted first guest batch. With this PR none of them appear.
  • game memory is imported through its XMem backing is never logged, the host profile is section-import=substituted, and there is no page remap and no rejected submit. It doesn't get as far as an import.

I don't have a stack for the hung thread, only the watchdog's, so I can't say where it is waiting. Since the provider is identical in rows 1 and 2, it is something the title does through XMem between creating that first raster pipeline and its first compute pipeline.

tuxbox.py check was not part of this; I only ran the game. If it helps I can rerun with any tracing you want switched on (for example a log line per XMem export call), or try a branch.

Update, same day: the runs above shared the machine with other GPU tests of mine, so I repeated them with nothing else running (no other Proton guest or compile in any 5 s sample, load below 5), alternating the two trees:

tree result
5c5677b + this PR exit code 5 after 41.3 s, no present
5c5677b normal, 6 presents per 10 s, stopped at 150 s
5c5677b + this PR exit code 5 after 40.9 s, no present

Same crash log and same last provider line. So it is not a load effect: five of five runs with this PR's xmem.dll hang at start-up, and both runs without it don't.

@DrArroway
DrArroway marked this pull request as draft November 26, 2026 18:53
@DrArroway

Copy link
Copy Markdown
Contributor Author

Thanks @JeffreyBytes for running that and catching the System Init hang! We reproduced the exact same 30 s watchdog crash (game_scarlett_master.exe+0x2215C35 right after created pipeline from Xbox slot 86 stream) locally on RX 6900 XT with cd8ad92.

Moving this PR to Draft while we track down the root cause in xmem and verify the fix locally before marking it ready again.

@DrArroway

Copy link
Copy Markdown
Contributor Author

Updated in 80e55e1 — @JeffreyBytes this is ready for another test on your RX 6700 XT (keeping the PR as Draft until confirmed).

Root causes & fixes

  1. System Init hang (runtime/xmem): map_cpu_page was eagerly promoting ~99,776 pages out of the large GPU allocation in a loop. Switched partial unmaps/remaps inside PrivateSpan to in-place MEM_DECOMMIT/MEM_COMMIT, tracked CPU vs GPU apertures via XMemVirtualAlloc, and left secondary views as PAGE_NOACCESS with a Vectored Exception Handler (AddVectoredExceptionHandler) for lazy SharedSection promotion on CPU access.
  2. Placed-texture & mirror CPU reads (runtime/d3d12): A/B testing against main caught a green video screen when d3d12_xs read or validated GPU-aperture addresses directly via ReadProcessMemory / WriteProcessMemory / VirtualQuery against GpuAlias or PAGE_NOACCESS views. Updated snapshot_mip, write_mapped, validate_shared_range, and CBV/resource mirror snapshots to resolve XMem physical backings (TuxBoxAcquirePhysicalBackingRuns / TuxBoxAcquirePhysicalBacking).

Verification (80e55e1)

  • cargo check (tuxbox-xmem and tuxbox-d3d12, x86_64-pc-windows-msvc): passed (0 warnings)
  • Wine unit tests: tuxbox-xmem (7/7 passed) and tuxbox-d3d12 device::heap::backing (6/6 passed)
  • python -m unittest tests.test_repository_hygiene: 5/5 passed
  • RADV buffer-remap on RX 6900 XT (1002:73bf, GE-Proton11-14): PASS (words=768/768, null_fallback_enabled: false, game memory is imported through its XMem backing)
  • Local ProjectScarlett test on RX 6900 XT (GE-Proton11-14): passed System Init, imports game memory through its XMem backing, and UI + video launch cleanly.

@DrArroway
DrArroway force-pushed the feat/xmem-physical-backing branch from 80e55e1 to 5a795cb Compare November 26, 2026 22:06
@DrArroway
DrArroway marked this pull request as ready for review November 26, 2026 22:07
@DrArroway

Copy link
Copy Markdown
Contributor Author

Rebased cleanly onto origin/main (e76ff6a) as 5a795cb (reusing #253's TuxBoxAcquirePhysicalBackingRuns in runtime/d3d12/src/device/heap/backing.rs) and marked Ready for review.

First, sorry @JeffreyBytes for asking you to test the initial revision before we had run it locally ourselves, and thank you for catching the System Init hang and calling out snapshot_mip (ReadProcessMemory) on #191 — our A/B test against main caught a green screen on video playback from that exact path (runtime/d3d12 now resolves XMem physical backings for snapshot_mip, write_mapped, validate_shared_range, and CBV/resource mirrors).

Real-world RX 6900 XT (GE-Proton11-14) results vs main (5c5677b)

  • Menu & intro video frame pacing: on main (shadow-copy fallback game memory is copied for the GPU), presentation dropped to 9 frames in 10s (~0.9 FPS) once hitting the intro; with zero-copy XMem backing import (game memory is imported through its XMem backing), it sustained 464–503 frames in 10s (~46–50 FPS) through the menu and intro video.
  • World streaming & gameplay: passed the menu and intro video cleanly, streamed the world (DispatchMesh), and reached Game Running (Frame: 5215, TileHeap: 2112Mb / 6236Mb, Streaming Requests: 3608, 7,319 buildings, 213 vehicles, 48 peds, 4,274 objects).
  • Later crash in Game Running (exit code 3): hit VK_ERROR_DEVICE_LOST (vr -4) from radv: GPUVM fault detected at address 0x8004914e0000 right after vkd3d-proton:vkd3d_allocate_device_memory: Memory allocation failed, falling back to system memory, with zero amdgpu kernel hang/reset and no host-pointer EFAULT — confirming XMem backings and holds stayed intact into Game Running.

Verification (5a795cb)

  • cargo check --locked --target x86_64-pc-windows-msvc (runtime/xmem & runtime/d3d12): passed (0 warnings)
  • Wine unit tests: tuxbox-xmem (7/7 passed) and tuxbox-d3d12 device::heap::backing (11/11 passed, 162 filtered)
  • python -m unittest tests.test_repository_hygiene: 5/5 passed
  • RADV buffer-remap on RX 6900 XT (1002:73bf, GE-Proton11-14): PASS (4.374 s, words=768/768 identical=1 replacement=1, null_fallback_enabled: false, game memory is imported through its XMem backing, buffer backing path=imported)

@Valdi320

Copy link
Copy Markdown
Contributor

Independent NVIDIA control run for 5a795cb on main e76ff6a: RTX 4080 (10de:2704), NVIDIA 620.73.4, GE-Proton11-14. Rebuilt XMem, D3D12 provider, XMem guests and Lanes. Six isolated D3D12 memory controls passed: buffer-remap, buffer-layout, buffer-import, buffer-import-reject, buffer-unmap-alive, buffer-unmap-released (2,816 checked words in first three, no stand-ins). Fresh-prefix 15/15 selected guest gates: all 12 XMem gates, heaps, tables, Lanes replay. This verifies NVIDIA synthetic compatibility, not the AMD performance claim or retail execution. No changes to your PR branch.

@JeffreyBytes

Copy link
Copy Markdown
Contributor

Retest on the RX 6700 XT, no preload, nothing else running on the machine: 80e55e1 once, then 5a795cb (on e76ff6a, with #253) twice. The start-up hang is gone and the game runs at full speed without the preload for the first time on this card. All three runs still lose the device on the story loading screen, with a different error than the preload gave.

Setup: as above (1002:73df, Mesa RADV 26.3.1, Linux 7.3.6, GE-Proton11-14, ProjectScarlett 1.0.2.1 offline, shaders prepared, 3840x2160). xmem.dll and d3d12_xs.dll rebuilt and staged for each commit.

What works now

  • Passes System Init, reaches the Account Picker and, after A, the story loading screen with its artwork.
  • game memory is imported through its XMem backing and, on 5a795cb, game memory is imported through non-contiguous XMem backings are logged.
  • Presents per 10 s, every present sample lit:
run picker loading screen
80e55e1 887 417, 424, 323, 288, 252, 148
5a795cb run 1 1028 471, 270, 300, 260, 123, 100
5a795cb run 2 984 458, 439, 358, 300, 300, 189, 121

For comparison, the copy path on main gives about 6 per 10 s here.

What still fails

run device lost at exit code
80e55e1 141 s 5
5a795cb run 1 139 s 3
5a795cb run 2 106 s 3

The order of events is the same in all three:

page remap replaced imported range 0xd751e0000..0xd751f0000 (0x10000 bytes) retired stale GPU import verdict=HRESULT(0x80004001) batches=193 …
  (24 of these, which is the log cap)
  … 20 to 30 s and two or three more present intervals, at a falling rate …
radv/amdgpu: The CS has been rejected, see dmesg for more information (-1).
radv/amdgpu: The CS has been rejected, see dmesg for more information (-1).
vkd3d-proton: Device … is lost (reason 0x887a0005, "VK_ERROR_DEVICE_LOST").

There is no GPUVM fault line in any of the three, so this is not the fault from your RX 6900 XT run, and it happens earlier: none of these runs left the loading screen.

The error code is the difference from the preload runs, which stopped at the same remap batch with -14 (EFAULT, nothing mapped under a live import). Here it is -1 (EPERM). My reading, not checked against your code: the ranges under live imports stay mapped now, so the hold is doing its part, but at some point after the remap batch one of them has a file behind it (a section view, or a page promoted to SharedSection on a CPU access), and the kernel's anonymous-only check refuses it when it revalidates that userptr buffer on a later submit. That is the ANONONLY branch in amdgpu_ttm_tt_get_user_pages(), which returns -EPERM; every submit carries every live import, so one such range is enough. The delay after the remap batch would fit a lazy promotion more than the remap itself.

If that is right, the rule would be that an address a held backing still covers stays private memory until the hold is released, whatever a remap or a promotion wants to put there.

Two other things from the logs, in case they are useful:

  • RtlReleaseSRWLockExclusive Lock … is not owned exclusive! appears about 2,400 to 3,200 times per run with this PR, against about 520 per run on main with the preload. I have not looked at which lock it is.
  • vkd3d_import_host_memory: Failed to import host memory, hr 0x8007000e once per run, about a second after device creation and before the first backing import. I assume that is the host profile's section probe. Run 1 on 5a795cb had a second one between the remap batch and the device loss; run 2 did not.

Happy to rerun on a new commit, or with extra logging if you want to see which range is refused; the whole cycle takes about five minutes.

@DrArroway

Copy link
Copy Markdown
Contributor Author

Thanks @JeffreyBytes for the detailed 3-run RX 6700 XT logs — especially noting that 5a795cb run 2 hit CS has been rejected (-1) with no second vkd3d_import_host_memory warning. That pointed directly to a backing-lifetime / VMA-protection race on already-imported userptr BOs rather than a failed import.

Pushed 0e927be with four fixes addressing that:

  1. Attach backing holds to each native ID3D12Heap (d3d12 BackingGuard): vkd3d-proton's d3d12_resource_destroy tears down resource->private_store before calling d3d12_heap_decref(resource->heap) (vkFreeMemory). Keeping Hold / ShadowPages only on ImportedHeap (attached to the resource) allowed host backings to be decommitted or freed while the RADV userptr BO was still in the global BO list, causing subsequent CS submissions to fail in the kernel with -EPERM (-1). BackingGuard is now attached via a cycle-free IUnknown marker directly on each native ID3D12Heap (including every piece heap in scattered CreateReservedResource imports) via SetPrivateDataInterface.
  2. Guard held slots against PAGE_NOACCESS (xmem private_views.rs): When a deferred-unmap or remapped slot still has an active GPU hold (has_hold(va, va + PAGE)), apply_slot_role, map_private_page, and map_private_contiguous now leave it PAGE_READWRITE and out of secondary_aliases instead of flipping a live userptr page to PROT_NONE.
  3. Prevent unpinned imports over doomed pages & cross-origin coalescing (xmem backing.rs): resolve_backings now returns ERROR_INVALID_ADDRESS (instead of ERROR_NOT_FOUND) when an unmapped range falls inside deferred_unmaps or an XMem aperture so d3d12 cannot create an unpinned hold: None direct import over a page about to be decommitted; runs no longer coalesce across distinct backing origins (PrivateView, distinct PrivateArenas, SharedSection); and flush_deferred_unmaps restores deferred_unmaps / span.deferred state if unmap/decommit fails.
  4. Restore strict validate_shared_range (d3d12 heap.rs): Reverted validate_shared_range to strict MEM_COMMIT + PAGE_READWRITE, keeping the XMem backing exemption scoped to place_gpu_target.

Verification (0e927be)

  • Unit & hygiene tests: tuxbox-xmem (10/10 pass), tuxbox-d3d12 device::heap::backing on RADV (13/13 pass, including placed_resource_keeps_backing_hold_alive_after_imported_heap_drops and adjacent_placeholder_private_spans_import_check), tests.test_repository_hygiene (5/5 pass).
  • RADV synthetic regressions (tools/check_d3d12_regressions.py, RX 6900 XT 1002:73bf, GE-Proton11-14): buffer-remap PASS (words=768/768, zero-copy XMem backing) and buffer-unmap-released PASS (later-submits=ok).
  • 3/3 full title runs on RX 6900 XT (1002:73bf, Mesa RADV 26.3.1, Linux 7.3.6-1-cachyos, GE-Proton11-14):
    • 0 radv/amdgpu: The CS has been rejected, 0 VK_ERROR_DEVICE_LOST, 0 GPUVM faults across all 3 runs.
    • All 3 runs passed the page remap batches cleanly into the world load; 2/3 rendered live 3D world frames (Run 3 reached 2,853 presented frames and stayed alive rendering until manually stopped).
    • RtlReleaseSRWLockExclusive dropped from ~2,400–3,200/run down to 3–5/run, matching vkd3d_allocate_device_memory: Memory allocation failed, falling back to system memory 1:1.

Would be great to hear how 0e927be behaves on your RX 6700 XT when you get a chance to re-test.

@Valdi320

Copy link
Copy Markdown
Contributor

I found a separate, reproducible coherence failure on your current head 0e927be, using only synthetic XMem pages (no game or AMD GPU required).

Two small regression tests in runtime/xmem/src/tests/physical_backing.rs:

  • Map one CPU page, create a GPU alias, unmap the CPU view, acquire the GPU alias (now backed by a PrivateArena), keep that hold live, map a new CPU view, read to trigger lazy section promotion, then write through the still-held arena pointer.
  • GE-Proton11-14 / Windows x64 Rust test: initial CPU value 0x12345678, CPU Type=0x40000 (MEM_MAPPED), write 0xaabbccdd through the held arena, CPU still reads 0x12345678. The same test retiring the hold before promotion passes, including write/read through the subsequent acquired backing. Existing arena split test also passes.

The arena hold retains an Arc, not a view_holds entry. promote_faulting_alias checks the mapped VA spans, but can change the canonical backing away from a still-held PrivateArena. Old imports may then write storage the CPU no longer sees. A plain ERROR_BUSY in the VEH would not be a complete solution (it would reject a legitimate CPU access); the ownership transition needs coordination with import retirement or a coherent fallback.

This is a demonstrated CPU-side model of stale backing writes, not proof of the AMD EPERM/device-loss cause. The tests are on my fork only; no new PR or change to your branch.

…slots

- Attach BackingGuard (ShadowPages + Hold) directly to each native ID3D12Heap via SetPrivateDataInterface so host memory remains pinned past vkd3d-proton's d3d12_resource_destroy (private_store teardown before d3d12_heap_decref / vkFreeMemory).
- Keep held PrivateSpan slots PAGE_READWRITE instead of PAGE_NOACCESS when remapped as SecondaryAlias.
- Return ERROR_INVALID_ADDRESS (not ERROR_NOT_FOUND) from resolve_backings for unmapped ranges in deferred_unmaps / XMem apertures, prevent coalescing across distinct BackingOrigins, and restore bookkeeping on deferred unmap/decommit failure.
- Restore strict MEM_COMMIT + PAGE_READWRITE check in validate_shared_range, keeping the XMem-backed exemption scoped to place_gpu_target.
- Add unit tests in heap_backing.rs and physical_backing.rs.
@JeffreyBytes

Copy link
Copy Markdown
Contributor

Retest of 0e927be on the RX 6700 XT: the -1 rejection is gone. Three no-preload runs on a quiet machine, and all three pass the page-remap batch with no CS has been rejected line at all. They end about a minute later for a different reason, still on the story loading screen.

Setup: as before (1002:73df, 12 GB, Mesa RADV 26.3.1, Linux 7.3.6, GE-Proton11-14, 32 GB RAM, 3840x2160, shaders prepared, no LD_PRELOAD).

run picker, presents / 10 s loading screen, presents / 10 s ended at how
1 1380 662, 472, 448, 327, 300, 289, 175, 175, 121, 73, 100, 84, 87, 70 208 s radv/amdgpu: Not enough memory for command submission., device lost, exit code 3
2 1199 487, 439, 404, 301, 300, 225, 151, 89, 94, 85, 62, 77, 36 198 s same line twice, device lost, exit code 5
3 978 472, 450, 361, 300, 300, 186, 117, 101, 79, 84, 80, 67, 62, 67 212 s no device loss; label waits stall and the title's watchdog ends it, exit code 5

In all three the present rate falls steadily, then the last intervals are one to four frames in 14 to 18 s with lines like:

GPU memory wait released after 5s without reaching its target (#6): address=0x4f8f980 op=>= target=0x11f6 value=0x11f5
stalled label 0x4f8f980 >= 0x11f6: waited by list … (order 4681), submitted to a type 2 queue, after waits []
radv/amdgpu: Not enough memory for command submission.
vkd3d-proton: d3d12_command_queue_execute: Failed to submit queue(s), vr -4.

The kernel log has the matching amdgpu: [drm] *ERROR* Not enough memory for command submission! for runs 1 and 2. No GPUVM fault in any run.

Memory. For run 3 I sampled the amdgpu counters (mem_info_vram_used, mem_info_gtt_used) and MemAvailable every 3 s, and then did the same for main (8c56463) with the preload as a control. MiB, time from launch:

45 s 75 s 105 s 120 s 135 s 180 s peak
0e927be VRAM 8733 9079 12236 12239 12241 12256 12263 (card total 12272)
0e927be GTT 1237 1271 2084 3142 5693 7950 8488 (total 15913)
0e927be MemAvailable 17508 16140 13646 12480 9941 6506
main + preload VRAM 8442 8899 9179 9188 9188 – 9188
main + preload GTT 1238 1277 1277 1280 1280 – 1295
main + preload MemAvailable 18674 17731 16982 16774 16849 –

The control dies at 144 s at the remap batch (-14, as always with the preload), so the comparison only holds up to there. Up to that point the two start out the same, and from about 90 s this PR uses roughly 3 GB more VRAM, fills the card, and then GTT and system memory keep growing where the control is flat. I have not found what the extra memory is; one run each, so treat the numbers as a first look. A 16 GB card would have more room before this shows.

Lock errors: RtlReleaseSRWLockExclusive Lock … is not owned exclusive! went up here, not down: 5,084 / 5,289 / 5,877 per run on 0e927be, against 2,400 to 3,200 on 5a795cb and about 520 on main with the preload. Memory allocation failed, falling back to system memory is about 470 per run on every build including main, so the two are not 1:1 on this card.

Happy to rerun with extra logging (for example the live hold count and total held bytes over time) if that helps.

@JeffreyBytes

Copy link
Copy Markdown
Contributor

Update, with a correction to the memory comparison above. I reran both sides with TUXBOX_TRACE_BUFFER_BACKING=1 and sampled the title's RssAnon as well. The growth starts at the page-remap batch, which is exactly where the main + preload control dies, so the control cannot tell this PR's overhead apart from what the title does after the remap. Please read the "3 GB more VRAM than main" above as "3 GB more than main had when it died", not as overhead of this PR.

What the traced runs show (0e927be no preload, 229 s; main 8c56463 + preload, 113 s; the trace is rate-limited to powers of two, so these are floors):

buffer backing path 0e927be before the remap batch main before the remap batch 0e927be at the end
imported / view 32768 32768 65536
page_hit / view 16384 16384 16384
span_hit / view 8192 8192 16384
page_open / view 512 512 8192
span_open / view 512 256 4096
imported_scattered / place_virtual_layout 16 – 16
  • Up to the remap batch the two builds are the same in every row, and so are VRAM and GTT.
  • After it, on 0e927be, fresh opens from views go up by 8 to 16 times while hits barely move. VRAM goes from about 9.1 GB to full (12.2 GB) within 30 s of the batch, GTT then climbs from 1.5 GB to 6 to 7 GB over the next two minutes, and in the last 10 s it jumps to exactly mem_info_gtt_total (15,913 MiB), which is when Not enough memory for command submission appears.
  • The title's RssAnon levels off at about 10 GB and stays flat while GTT keeps growing, so this is not the process's own private memory ballooning.
  • Scattered imports are rare here (16 to 31 in the run), so they are not the volume.

What I can't tell from this: whether the opens after the remap are the title legitimately streaming more than a 12 GB card can hold at 3840x2160, or imports that were retired by the remap staying alive (as far as I can read commands/page_mapping.rs, retiring only marks the pin not live, and the old heap, guard and hold stay until the title releases the resource) while their replacements are opened next to them. A count of live imported heaps and their total bytes, logged every few seconds, would settle it; I can run that if you add it or tell me where you'd want it.

…alias memory

- Track PrivateArena hold counts in Pool::arena_holds, adopt unheld PrivateArenas directly as PrivateView(cpu) (MEM_PRIVATE), and export TuxBoxSetBackingInvalidationCallback wired to GPU_APERTURE.retire_range so held-arena promotions retire stale d3d12 imports outside POOL's lock.
- Skip populate_page_bytes and snapshot-copying for SlotRole::GpuAlias so GPU alias pages stay untouched zero-pages instead of dirtying duplicate physical RAM.
- Export read-only TuxBoxIsPhysicalBacked for d3d12's is_backed_range check so place_gpu_target does not allocate phantom PrivateArenas.
- Cache scattered import_scattered_buffer piece heaps in PagePools::pieces.
- Add opt-in TUXBOX_TRACE_XMEM_BACKING=1 periodic hold/arena/deferred diagnostics.
- Add regression tests in physical_backing.rs and heap_backing.rs.
@DrArroway
DrArroway force-pushed the feat/xmem-physical-backing branch from 0e927be to d50a51c Compare November 27, 2026 01:00
@DrArroway

Copy link
Copy Markdown
Contributor Author

Rebased onto latest main (12e4b06) and pushed d50a51c addressing both @Valdi320's PrivateArena finding and @JeffreyBytes's memory/diagnostics follow-up:

Re: @Valdi320 (PrivateArena hold tracking & import retirement)

Good catch — HoldRecord.arenas wasn't tracked in Pool, so PrivateArena neither adopted a new CPU view when unheld nor notified d3d12 when promoted under a live hold. In d50a51c:

  • Pool::arena_holds tracking (xmem backing.rs): commit_hold / release_hold now maintain per-arena hold counts.
  • Unheld arena -> zero-copy PrivateView(cpu) (xmem private_views.rs): When a PrivateArena has no live hold (arena_holds == 0), mapping a new CPU view adopts it directly as SlotRole::Canonical (MEM_PRIVATE + PAGE_READWRITE), preserving bytes without a VEH fault or SharedSection downgrade (tested in unheld_private_arena_adopts_new_cpu_view_without_section_promotion).
  • Held-arena promotion retires d3d12 imports (xmem + d3d12): Exported TuxBoxSetBackingInvalidationCallback (registered once by d3d12 -> GPU_APERTURE.retire_range). When promote_faulting_alias transitions a still-held PrivateArena to SharedSection, xmem drops POOL and notifies all mapped guest VAs so ImportedHeap::is_live() flips to false, TextureAddress::is_stale() triggers re-import of the promoted backing, and PagePools never reuses the stale arena heap — while the old PrivateArena remains committed MEM_PRIVATE until release_physical_backing(hold) (tested in held_private_arena_promotion_notifies_invalidation_and_keeps_hold_live and retire_range_invalidates_imported_heap_and_prevents_cache_reuse).
  • (OS boundary note: once acquire_physical_backing hands out an anonymous MEM_PRIVATE arena pointer for amdgpu userptr, Windows/Wine cannot hardware-alias a different VA cpu != backing to that same private page mid-hold without retiring the old import — which the invalidation callback now coordinates cleanly.)

Re: @JeffreyBytes (memory reductions & live hold/arena trace)

Glad to hear CS has been rejected (-1) is 0/3 on the RX 6700 XT, and thanks for the TUXBOX_TRACE_BUFFER_BACKING=1 follow-up. d50a51c also adds three memory cleanups plus opt-in live hold/arena stats (TUXBOX_TRACE_XMEM_BACKING=1, logged every 5 s):

  • Skip GpuAlias RAM dirtying (xmem): map_private_page, map_private_contiguous, apply_alias, and detach_private_slot no longer populate or snapshot-copy SlotRole::GpuAlias pages, keeping GPU-only alias slots as untouched OS zero-pages instead of dirtying a duplicate copy of the GPU aperture.
  • Read-only TuxBoxIsPhysicalBacked (xmem + d3d12): place_gpu_target's is_backed_range check now queries Pool.mappings read-only instead of calling acquire_backing and allocating phantom PrivateArenas for unbacked GPU-only targets (is_physical_backed_does_not_allocate_private_arena_for_unbacked_gpu_alias).
  • Cache scattered piece heaps (d3d12 PagePools::pieces): import_scattered_buffer now reuses live piece ID3D12Heaps across scattered imports (scattered_imports_reuse_cached_piece_heaps).

Live TUXBOX_TRACE_XMEM_BACKING=1 numbers on RX 6900 XT (16 GB VRAM / 64 GB RAM, 3840x2160, d50a51c):

  • Pre-remap: pages=114,096 (7,131 MiB) | holds=458 | view_holds=9,149 (571 MiB) | live_arenas=0 (0 MiB) | deferred=0.
  • Post-remap -> 3D world (235.4 s, 0 CS rejections): holds grows to 12,865 (matching page_open=8192+ + span_open=4096+), while view_holds totals only 1,498 MiB (~1.5 GiB), live_arenas = 0 (0 MiB), and deferred drains cleanly across the remap (107 -> 1) before sitting at 1,531 pages (95 MiB) during world streaming.
  • On the 16 GB / 64 GB card, total VRAM + GTT rises from 14.8 GiB VRAM + 2.7 GiB GTT at DispatchMesh to 16.2 GiB VRAM + 12.3 GiB GTT (~28.5 GiB total) once 4K world textures stream and spill out of VRAM via vkd3d_allocate_device_memory. That sits just over a 12 GB VRAM / 32 GB RAM card's ~27.5 GiB combined VRAM+GTT ceiling (12,272 + 15,913 MiB), which matches the Not enough memory for command submission failure you saw — with XMem holds themselves accounting for ~1.5 GiB of that total rather than leaking unboundedly.

(Unit & hygiene checks on d50a51c: tuxbox-xmem 13/13 pass, tuxbox-d3d12 device::heap::backing on RADV 15/15 pass, tests.test_repository_hygiene 5/5 pass.)

@JeffreyBytes

Copy link
Copy Markdown
Contributor

Retest of d50a51c on the RX 6700 XT, two no-preload runs with TUXBOX_TRACE_XMEM_BACKING=1. The trace agrees with your reading: the holds are small and nothing accumulates, and what ends the runs on this card is total GPU memory at 3840x2160.

run ended at how CS has been rejected
1 243 s Not enough memory for command submission (3 lines), device lost, exit code 3 0
2 171 s label-wait stall, title watchdog, exit code 5, no device loss 0

backing stats, run 1 (run 2 is the same shape):

pages=14320 (895 MiB)    holds=1    view_holds=2784 (174 MiB)   arena_holds=0 live_arenas=0 (0 MiB) deferred=0
pages=114096 (7131 MiB)  holds=147  view_holds=8986 (561 MiB)   arena_holds=0 live_arenas=0 (0 MiB) deferred=0      before the remap batch
pages=114096 (7131 MiB)  holds=907  view_holds=9892 (618 MiB)   arena_holds=0 live_arenas=0 (0 MiB) deferred=139
pages=114096 (7131 MiB)  holds=5650 view_holds=17456 (1091 MiB) arena_holds=0 live_arenas=0 (0 MiB) deferred=4
pages=114096 (7131 MiB)  holds=9938 view_holds=22039 (1377 MiB) arena_holds=0 live_arenas=0 (0 MiB) deferred=382    last sample

So about 1.4 GiB under hold at the end, no live arenas, and deferred drains. Meanwhile the amdgpu counters go the same way as on 0e927be: VRAM is full (12.2 GB) about 30 s after the remap batch, GTT climbs from 2 GB to 6 to 9 GB, and in run 1 it reaches mem_info_gtt_total (15,913 MiB) at the moment the submit is refused. That is the 12,272 + 15,913 MiB ceiling you worked out. Neither run left the loading screen, so I can't say how far past it a 12 GB / 32 GB machine would get; the three memory reductions did not visibly move the point where VRAM fills here.

From this card's side I have nothing left against the PR: the -1 and -14 rejections are gone in 7 of 7 no-preload runs since 0e927be, the page-remap batch passes every time, and the menu and loading screen run at 300 to 1,000 presents per 10 s where the copy path on main gives about 6. What remains here is the title's memory demand at this resolution, which is a separate problem from this PR.

One thing that is still different from your card, in case it means something: RtlReleaseSRWLockExclusive Lock … is not owned exclusive! is 6,571 and 5,331 per run here on d50a51c (about 520 on main with the preload), where you saw 3 to 5.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants