d3d12: map title memory far past root SRVs and UAVs #389
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/389"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Imported from brcly/TuxBox. Originally posted by MathiewMay on 2026-11-28T09:20:16Z.
Summary
A root SRV or UAV in title memory was bound through a one-page import:
root_bufferaskedcommand_buffer_addressfor one byte, which imports the 64 KiB page holding the address. A root descriptor has no size, and a console shader reads as far past it as the title placed data. ProjectScarlett lost the device on an instanced draw (4 vertices × 4,029 instances) whose per-instance records run about 80 KiB past its root SRV: the fault was a read 32 KiB past the import.root_view_address,runtime/d3d12/src/device/views/addressed.rs) now get an import of up to 4 MiB (ROOT_VIEW_REACH) behind their address. It starts at the 4 MiB boundary below the address when the memory from there is committed, at the address's own page otherwise, and spans 8 MiB clipped to the committed run, so that the bindings after it reuse it. Root CBVs keep their 64 KiB window, and an address outside committed memory is resolved as before.covering_bufferchecks up to 8 preceding live entries (Views::preceding_entries).CommandImportskeeps the imports commands make (root views, andcommand_buffer_address's, such as fence labels) in a 1024-entry LRU. It keeps only imports whose pages stay mapped while they live, a host copy or title pages under an xmem hold (ImportedHeap::pages_held). An import of the title's ownVirtualAllocwould outlive memory the title may release, and later submissions would name the freed pages.A kept import holds its pages until 1,024 newer imports replace it, also after the title remaps those pages: lookups skip it then, as they skip every stale import, but the LRU still holds it.
In game I tested this on an older
maintogether with other fixes of mine, not on this branch alone. The fault 32 KiB past a root SRV's import did not recur; every later device loss had the masked-lane pattern of #386 (fixed there), and with all my fixes a 30-minute run had no fault.Tested
Not a Lanes scenario: it is a new D3D12 regression control,
root-srv-reach(runtime/d3d12/root_srv_guest.cpp,root_srv.hlsl), next toroot-cbv-window, which covers the CBV side of the same function. It binds a root SRV 0x4480 intoVirtualAlloced memory with 2 MiB after it, reads 16 bytes at 0, 0xff00, 64 KiB, 80 KiB and 1 MiB past it into adeadbeef-seeded result, and compares them with the memory. Onmainit fails:The shader ran (the seed was overwritten) and read zero at every offset, including offset 0, inside the imported page. I don't know why offset 0 reads zero too. The device was not lost and nothing in the test's log reports a fault; I could not read the kernel log on this machine.
Unit tests in
runtime/d3d12/src/tests/device/views.rscover the window (root_view_window: the control's layout, a 4 MiB-aligned start, memory not committed back to it, an allocation that starts above it, the clip to whole pages), the LRU (only held imports are kept; the least recently used goes first) andpreceding_entries(it finds live entries behind a released one, whereprecedingreturns nothing).GPU, driver and backend: AMD Radeon RX 9070 (1002:7550, RADV GFX1201), Mesa 26.2.7, Linux 7.3, GE-Proton11-14 (
--backend proton).Commands run and their results:
The D3D12 controls ran with the tool's own case table, staging and
validate, but outside its Bubblewrap/xvfb-runsandbox, which stalls on this machine (it has no Xvfb).Checks
No evidence JSON changes.
Imported from brcly/TuxBox. Originally posted by DrArroway on 2026-11-28T15:28:07Z.
Tested in ProjectScarlett 1.0.2.1 on an AMD Radeon RX 6900 XT (
1002:73bf, RADV NAVI21), Mesa 26.3.1, Linux 7.3.6, GE-Proton11-14 (--backend proton), warm shader caches.mainat6f89916): Two runs both faulted immediately on entering the 3D world withradv: GPUVM fault(CLIENT_ID: TCP (0x8), at0x8007f1c50000and0x80011d73c000) and reset the GPU.911cc04c) stacked with #373, #393 and #398 on6f89916: The 3D-world entry GPUVM fault did not occur across a 7m 12s run (2,105frames total,13+world frames,0GPUVM faults,0device losses; the run ended on a title CPUEXCEPTION_BREAKPOINTin resource loading). Two further world runs rebased ontoe380da1also cleared world entry with no root-view faults.One small code note while reading
root_buffer(runtime/d3d12/src/commands/compute.rs) androot_view_address(runtime/d3d12/src/device/views/addressed.rs):root_buffercallsroot_view_addressfor bothBufferView::SrvandBufferView::Uav, androot_view_addressimports/reuses buffers withD3D12_RESOURCE_FLAG_NONE. RADV accepts a raw-VA root UAV over that buffer, so it didn't affect our runs, but a host or validation layer that checksD3D12_RESOURCE_FLAG_ALLOW_UNORDERED_ACCESSonSet*RootUnorderedAccessViewtargets would needroot_view_addressto forwardD3D12_RESOURCE_FLAG_ALLOW_UNORDERED_ACCESSwhenBufferView::Uavis bound (matchingcommand_buffer_address_flagsonmainandaddress_uav).Imported from brcly/TuxBox. Originally posted by brcly on 2026-11-28T16:04:23Z.
Trial results for this PR on NVIDIA. We're holding it back for now because of memory use, pending one confirming run.
Setup: RTX 4070 Ti SUPER 16 GB, driver 620.73.04, Linux 7.3.5, GE-Proton11-14, 30 GB RAM, ProjectScarlett offline. Build:
mainat2af42c94plus #373, #391, #393 and this PR (911cc04c), conflicts indevice.rsandviews.rsresolved by keeping both export lists.Checks: all D3D12 regression controls (including
root-srv-reach) and the Lanes tests we ran passed. Unit tests: 295 passed, 1 failed (aliases_import_their_private_backing, which also fails on unchangedmainhere).Two game runs, against four earlier runs the same day on builds without these four PRs:
root_view_addresswhencovering_bufferfound nothing: more than 8,700, about 20 GB requested in total. That is cumulative, not live memory. With a 1,024-entry limit, our guess is that the LRU churns and holds a lot of host memory at once.Not yet confirmed: we haven't run the same build without this PR. That is the next run, and we'll report the numbers here.
Ideas if it holds up: cap the kept imports by bytes as well as count, or reuse a covering import over a wider search than the 8 preceding entries.
Imported from brcly/TuxBox. Originally posted by MathiewMay on 2026-11-28T16:19:43Z.
Thanks for testing this on the 6900 XT, and for the note on the flags.
Rebased onto
2af42c9(now3ea19fe).root_view_addressnow takes the flags the view needs: a root UAV is mapped in a buffer withD3D12_RESOURCE_FLAG_ALLOW_UNORDERED_ACCESS, ascommand_buffer_address_flagsand address UAVs are, and a root SRV keepsD3D12_RESOURCE_FLAG_NONE(root_view_flagsincommands/compute.rs, with a unit test).After the rebase, on an RX 9070 (RADV GFX1201, Mesa 26.2.7, GE-Proton11-14): 296 crate tests pass, all 22 D3D12 regression cases pass including
root-srv-reach, andtuxbox.py check --no-gpupasses.Imported from brcly/TuxBox. Originally posted by gtb-sbe on 2026-11-29T02:11:06Z.
Tested this PR in ProjectScarlett 1.0.2.1 (offline) on an RX 9060 XT (
1002:7590, RADV GFX1200), Mesa 26.2.6, Linux 6.18.60, GE-Proton11-14, 32 GB RAM.Without it (
b8355fa+ #409 + #424 + #428), my run ended at world entry on a gfx-ring fault:TCP, read,MAPPING_ERROR 0x1, at native pages0x800361e40000-0x800361e47000. #424's trace listed 10 unfinished draws, all with the same vertex/pixel pair. Offline, that vertex shader reads its root SRV (t1, space3, root parameter 2) as a raw buffer: a header at bytes 0/4/12, then per-instance records at((h >> 7) & 0x1fffffc) + ((h >> 3) & 60) * SV_InstanceID. The faulting range starts on a 64 KiB boundary, which fits a read running off the one-page import thatmainmakes for a root SRV. I've had the same fault kind on two earlier builds.With it (
7d26508+ #409 + #424 + this PR at3ea19fe+ #428's runtime part, all applied cleanly; everything but this PR is onmainnow, and GitHub still shows it as mergeable), five runs and no GPU fault in any:Unsignaled CrossQueueBarrier), at about the same title frame (around 1,400) where the faulting runs had stopped. Before it, the provider held five label waits for writes that hadn't run yet, and #424's trace showed nothing unfinished on the GPU. I've seen the same barrier end a run without this PR, so I don't think it's related.root-srv-reach,root-cbv-window,address-uavandbuffer-importpass on this card, and so does the Lanes self-test. With #430 added,buffer-unmap-alive,buffer-unmap-released,placementandcopy-resourcepass too.Memory, since that's what's holding it. I added a temporary counter, not part of this PR, for the imports
root_view_addressmakes whencovering_bufferfinds nothing. In the first run:Over all five runs the counter ended between 1,024 new imports (2.5 GiB requested, in the short second run) and 19,000 (43.9 GiB requested over 67,823 bindings, in the fifth). The kept imports never went past 667 MiB. Peak requested VRAM was 12.1-12.8 GiB in every run. GTT and swap were higher in the two runs with #430 on top (requested GTT up to 12.3 GiB, swap up to 9.9 GiB), and I can't attribute that to either PR from these runs.
So on this card the LRU itself stays below 0.7 GiB. That's far below what you saw on NVIDIA, though the import churn is of the same order (7,000 imports and 14.1 GiB in the first 15 minutes here, 19,000 and 43.9 GiB in the fifth run, against more than 8,700 and about 20 GB there). If you cap the kept imports by bytes, the churn is what I'd watch: each new import is a fresh mapping of up to 8 MiB.
Imported from brcly/TuxBox. Originally posted by brcly on 2026-11-29T15:08:15Z.
Thanks to everyone who tested this. The AMD reports (RX 6900 XT, RX 9060 XT, RX 9070) of the fault at world entry going away are convincing. Two things hold it back for us, and they apply to #441 too, which carries the same
root_view_addresscode.1. Memory on our machine. ProjectScarlett, RTX 4070 Ti SUPER, NVIDIA 620.x, GE-Proton11-14, 30 GB RAM; trial branch on
main2af42c94:2. The ordering question NickStallman raised on #441:
root_view_addressqueries the regions and pickscovering_bufferbefore anything waits for a pendingCopyPageMappings. Is that window safe, or should it wait first, asbuffer_atdoes?What would help:
Please also settle between #389 and #441 which one carries on; we'll only merge one.
Imported from brcly/TuxBox. Originally posted by NickStallman on 2026-11-29T15:19:30Z.
Counts from my builds that have #441 (RTX 5090, 24 GB, 620.70.10, GE-Proton11-14;
mainatd2eb99edor6e2c5dabwith #462, #463, #461 and others; ProjectScarlett past the story load). They are all imports of title memory, not only root views (my ring doesn't separate them), so they bound your number from above and don't answer it:At the end of 65b, the memory ledger read: card 13.3 of 24 GB, adapter system memory 2.4 of 70.6 GB, title pages 7.1 GB, process commit 9.9 GB. The adapter system memory doesn't show your 9.5 GB, but the machine has more VRAM and I have no run without #441 to compare, so I can't say what it costs. If a counter of root-view imports and bytes would help I can add one and run it, with and without #441.
I don't have an answer on the ordering; it was a question from reading.
Imported from brcly/TuxBox. Originally posted by JeffreyBytes on 2026-11-29T16:27:05Z.
Numbers for the two things asked above, from a 12 GB AMD card. RX 6700 XT (
1002:73df), RADV, Mesa 26.3.1, Linux 7.3.6, GE-Proton11-14, 32 GB RAM, ProjectScarlett offline, warm caches, default XMem mode (section backing). I ran #441's commits since they're the ones on newermain; it's the sameroot_view_address.Both builds are on
main6e2c5dab. "With" is #441 atf244a687merged in. One run each.Without it, the run ends at world entry on the fault this PR describes, and the address lines up exactly. I added a local trace of root SRV/UAV binds that import their own buffer:
Same list as the 17 unfinished draws in the GPU fault trace, all one VS/PS pair. That list binds root SRV 2 dozens of times at addresses 0x17b00 (97 KiB) apart, each with 16 to 58 KiB left in its one page. The ring reset recovered.
With #441 there's no GPU fault. The run goes through world entry and ends at 652 s in the title's own crash handler, which is where runs here end now.
6e2c5dab)mem_info_gtt_used)So on this card the cumulative import is large, 5.3 GiB in about five minutes of world, but it doesn't show up as memory: adapter system memory and GTT are within a run's noise of the run without it. I can't say why NVIDIA shows 2.7 GB more; I have no NVIDIA card.
Two details from the counter that bear on the cache question:
CommandImportsdoesn't keep (pages_held()false), so the next bind outside a kept window imports its 8 MiB again. 409 of the 680 (3.2 GiB) had been released again by the end. If that reading is right, it's not the 1,024-entry cache thrashing, it's these imports never entering it. The cache itself was full at 1,024 entries by the end, shared with the other command imports. I lost the eviction count to a truncated log line and can rerun for it.The counter is my own local commit on top of #441, not part of either PR, and this was its first run, so treat the cache breakdown as needing a second look by someone who knows that code. I can share the patch.
One comparison point, in case it helps with the memory side: before I found these PRs I'd tried the same idea with a different window, 32 MiB cells clipped to the committed run and held by the command allocator until Reset, no LRU. Same base, same scene: 11 new imports in the whole run, no fault, adapter system memory 7,499 MiB. I'm not proposing a third PR, just noting that far fewer, larger imports got through the same scene.
I haven't looked at the ordering question.
Imported from brcly/TuxBox. Originally posted by glovepost on 2026-11-29T17:01:10Z.
We're carrying on with this pull request and closing #441, which carried the same
root_view_addresschange. #441 differed from this one by about 30 lines inruntime/d3d12/src/device/views/addressed.rs; the other 12 files are identical. @MathiewMay, here are the two additions in case you want to fold them in.1. Wait for a pending
CopyPageMappingsbefore choosing the window. This is the ordering question @NickStallman raised and @brcly repeated. For a non-null address,root_view_addresscalls this beforeVirtualQueryandcovering_buffer:Without it, a mapping that is still pending leaves
VirtualQueryseeing the old state, so the window can be one page or end early, and an import the remap is about to retire can be reused.2. Bind an existing buffer when the address is a
GetGPUVirtualAddressvalue. Widening such an address to the 8 MiB window imports whatever committed range that host number happens to fall in. On NVIDIA that madelanes-selftest-thread-dimensionsfail withclose list failed: 0x80070057. #441 first looks through up to 8 preceding registry entries (preceding_entries) for a live buffer that is not an import, has matching flags and contains the address, and binds that buffer directly.On that version,
root-srv-reach(20/20 words) androot-cbv-window(120/120 words) passed, and so did the 72 guest and Lanes checks oftuxbox.py verify. This was on an RTX 2070 SUPER (10de:1e84, driver 620.73.04) and on RADV (1002:13c0, Mesa 26.3.1), both with GE-Proton11-14.On the memory question: from reading the code,
CommandImportsis limited by entry count (1,024) but not by bytes, and each kept import can span up to 8 MiB. A byte budget next to the count limit, and/or a wider search than the 8 preceding entries incovering_buffer, would be the first things I'd try. I can re-run the NVIDIA checks on this branch once it's updated.Imported from brcly/TuxBox. Originally posted by DrArroway on 2026-11-29T19:26:59Z.
@MathiewMay @glovepost While testing #389 alongside #501 (
TUXBOX_XMEM_PRIVATE_VIEWS=1) on an RX 6900 XT, I ran into an interaction inroot_view_address(runtime/d3d12/src/device/views/addressed.rs) that is worth considering alongside #441's additions during the rebase ontomain:What happens with single-
VirtualQueryinroot_view_addressMEM_PRIVATE(mix ofPAGE_READONLY/PAGE_READWRITE, plusPAGE_NOACCESSon unpromoted secondary aliases), a singleVirtualQuery(address)stops at the first protection boundary, sorun_endcan clip well short of2 * ROOT_VIEW_REACH(even to a single 64 KiB page) even though the surrounding pages are committed and backed.PrivateSpanAllocationBaseboundaries: UnderTUXBOX_XMEM_PRIVATE_VIEWS=1, XMem commitsMEM_PRIVATEranges in separatePrivateSpanOS allocations (aligned up to 4 MiB chunks viaPRIVATE_SPAN_CHUNK, each with its ownAllocationBase; and #501 splitsBackingOrigin::PrivateView(span_id)perPrivateSpanso userptr runs never cross VMAs). Clamping to a singleregion.AllocationBasestops an 8 MiB (2 * ROOT_VIEW_REACH) window at thePrivateSpanboundary even when adjacentPrivateSpans are contiguous and XMem-backed.Approach tested (
walk_committed_run)Replacing the single
VirtualQueryend calculation inroot_view_addresswith a smallwalk_committed_runhelper resolved both cases cleanly:MEM_COMMITsegments up tolimitwhilenext_alloc == alloc(crosses protection splits within the sameAllocationBase, stops at a differentAllocationBase).super::super::heap::backing::is_backed_range(page, page + 0x10000)): Walk contiguousMEM_COMMITsegments across adjacentPrivateSpanAllocationBases up tolimit, verifyingis_backed_range(binary-searching to the last backed 64 KiB page if a segment extends past backed XMem) and passingallocation = 0intoroot_view_windowsostartcan extend backward across a preceding backedPrivateSpanas well.import_scattered_bufferand #501'sPrivateSpanbacking-run split: the root view still spans the full 8 MiBROOT_VIEW_REACHwindow whileimport_scattered_bufferimports each 4 MiBPrivateSpanbacking piece separately.(Note: when folding in #441's
GPU_APERTURE.wait(start, 2 * ROOT_VIEW_REACH)?before querying, waiting first is also needed sois_backed_rangeandVirtualQuerysee the settled mapping state.)On
main+ #389 (3ea19fe, rebased locally with #441's two additions +walk_committed_run) + #501 (TUXBOX_XMEM_PRIVATE_VIEWS=1), an offline ProjectScarlett run on RX 6900 XT (1002:73bf, RADV, GE-Proton11-14) ran 555.1s (15,497presents) with 0 GPUVM faults, 0heap import failures, 0secondary aliaserrors, andsection=0/0.0MiBthroughout.Imported from brcly/TuxBox. Originally posted by brcly on 2026-11-29T19:56:00Z.
Thanks again to everyone who tested and traced this. The fault is clearly real, and JeffreyBytes's trace ties it to the one-page import at the exact faulting address. Where we stand:
Before we can merge it
Rebase onto
main. Only one hunk inruntime/d3d12/src/tests/device/views.rsconflicts now.Fold in glovepost's two additions from #441:
GPU_APERTURE.wait(start, 2 * ROOT_VIEW_REACH)beforeVirtualQueryandcovering_buffer(the ordering question);GetGPUVirtualAddressvalue. Without it,lanes-selftest-thread-dimensionsfails on NVIDIA.DrArroway's
walk_committed_run, for windows clipped at protection splits andPrivateSpanboundaries, would be welcome too.The memory cost. From JeffreyBytes's counter: the imports made in the world don't enter
CommandImports(pages_held()is false). So every bind outside a kept window imports its 8 MiB again: 680 new imports and 5.3 GiB in about five minutes of world on an RX 6700 XT, more than 8,700 on our RTX 4070 Ti SUPER. On NVIDIA that came with about +2.7 GB of adapter system memory and +4 GB of kernel memory (one run each way). That points at the import lifetime more than at the 1,024-entry limit. JeffreyBytes's comparison, with 32 MiB cells clipped to the committed run and held by the command allocator untilReset, made 11 imports in the whole run, with no fault and no memory rise. Something along those lines, tied to the allocator rather than an LRU, would address our concern.@MathiewMay, are you able to pick this up? If we don't hear back in a few days, @JeffreyBytes, would you post your allocator-lifetime version as its own PR? We'd review it in place of this one. We'll only merge one of them.
Imported from brcly/TuxBox. Originally posted by brcly on 2026-11-30T04:12:29Z.
Heads-up: main just landed a large cleanup (up to
e5d2e913), so this PR needs a rebase.rustfmt.toml).unwrap(), lossyascasts and undocumentedunsafeoutside tests.cargo fmt --check, rustdoc with warnings denied, and Miri on the providers' unit tests.The least painful way is to format your branch first, so the rebase only shows real changes:
Then run
python tuxbox.py check --no-gpu, which now includes the format step.If clippy flags something new:
Fromfor widening,TryFromfor narrowing, and a mask for bit fields.unwrap(): use?, orexpectwith the reason it can't fail.unsafeblock: it needs a// SAFETY:comment.CONTRIBUTING's "Code checks" section lists the commands. Thanks!
Imported from brcly/TuxBox. Originally posted by brcly on 2026-11-30T20:53:21Z.
I've rebased this onto
mainand pushed it here, so it can go in. What changed:mainafter the formatting and lint cleanup. The conflicts were in the regression lists (both kept), the exports, andbuffer_at, which now uses yourcovering_buffer.GPU_APERTURE.waitover the whole window before the query, andrecorded_buffer, which binds the existing buffer when the address is aGetGPUVirtualAddressvalue.SAFETYcomments, a# Safetysection onroot_view_address, and a checkedusizeconversion.Measured on an RTX 4070 Ti SUPER, GE-Proton11-15:
root-srv-reach,cbv-viewsandroot-cbv-windowpass.lanes-selftestscenarios pass, includingthread-dimensions, and so doeslanes-compute-root-srv.VS 281b4977, reading past its import) didn't recur in that run. The run was too short to prove it's gone.Still open: graphics and mesh root views keep the one-page import, because this change resolves compute root views only. That run ended on a mesh-draw fault, which may be the same kind of read. I'll follow that up separately.