Skip to content

xmem: a slot-31 copy leaves the title's own mappings at the paired CPU range (RT-SLOT31-CPU-TWIN-TITLE-PAGES) - #1972

Closed
kurrutako wants to merge 2 commits into
ProjectLeonida:mainfrom
kurrutako:rt/slot31-cpu-twin-title-pages
Closed

kurrutako wants to merge 2 commits into
ProjectLeonida:mainfrom
kurrutako:rt/slot31-cpu-twin-title-pages

Conversation

@kurrutako

@kurrutako kurrutako commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor

Claimed in the coordination issue as: RT-SLOT31-CPU-TWIN-TITLE-PAGES

What and why

copy_page_mappings (queue slot 31, CopyPageMappingsBatchX) mirrors every page it maps or unmaps at a GPU-only destination onto the paired CPU range (title_gpu_views::to_cpu, from 255f170). It did this even where the title had mapped that CPU page itself. A copy replaced the title's page, and an unmap (kind 2) left the title's CPU page only reserved. The title's next read of it was a fault in a window, left to the title, and the run ended. The commit that added the mirror names this risk: if the pairing is wrong, "the mirror replaces the title's own mappings in that CPU range".

The console's CopyPageMappingsBatchX edits only the GPU-only range's page table. So a CPU page that holds one of the title's own mappings (XMemMapPhysicalPages, XMemMapPhysicalPagesScatter, or a slot-31 copy onto that address) is now left as it is. Pages the mirror made itself, zero plants and pages with no mapping still follow the GPU-only range, so the D3D12 layer's reads at the CPU view still work. Each page left alone is counted, and the first ones are logged.

The unmaps on these paths now carry their own label in the read-fault report, instead of the default unmap:

  • CopyPageMappingsBatchX: unmap, paired CPU page, remap, hole
  • XMemMapPhysicalPagesScatter: unmap, remap

Tested

GPUs tested: NVIDIA RTX 5070

AI-assisted.

Checklist

  • Based on current main and merges cleanly.
  • One topic.
  • No game data, keys or decryption.

🤖 Generated with Claude Code

…U range

copy_page_mappings (queue slot 31, CopyPageMappingsBatchX) mirrors every page
it maps or unmaps at a GPU-only destination onto the paired CPU range
(title_gpu_views::to_cpu). It did so even where the title had mapped that CPU
page itself with XMemMapPhysicalPages or XMemMapPhysicalPagesScatter: a copy
replaced the title's page, and an unmap (kind 2) left the title's CPU page
reserved. The title's next read of it faulted, and the run ended.

The console's CopyPageMappingsBatchX edits only the GPU-only range's page
table, so a CPU page holding a mapping of the title's (physical, scatter or a
slot-31 copy onto that address) is now left as it is. Pages the mirror made
itself, zero plants and pages with no mapping still follow the GPU-only range,
so the D3D12 layer's reads at the CPU view keep working. Each page kept is
counted and the first ones are logged.

The unmaps these paths make are now labelled in the read-fault report
(CopyPageMappingsBatchX unmap / paired CPU page / remap / hole,
XMemMapPhysicalPagesScatter unmap / remap) instead of the default "unmap".

xmem_test: test_copy_page_mappings_cpu_view now checks that a copy onto, and
an unmap of, a GPU-only page whose paired CPU page the title mapped keep the
title's bytes at the CPU page. Both checks fail on the old code.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@DadaCap

DadaCap commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor

Retail result for this in the world phase (RTX 5080, NVIDIA 621.24, Windows 11), with LEONIDA_POOL_ZERO_PAGES unset. Without it, two runs with fast submissions ended the way you describe (exe+0x465a75 on an uncommitted page at 154 s; with LEONIDA_POOL_ZERO_PAGES=1, +0x17df6c with "unmap ... ms ago 1016" and then a null read at +0x22bfc0 at 139 s), and a third had 18 zero plants before I stopped it. With this: no title read fault and no zero plant in 417 s, stopped by me. One run.

A helper of mine came to the same function from the logs and wrote a version with a clock instead of the owner check (keep only pages the title mapped after the remap was queued); yours was open first and is simpler, so I use yours and will not send mine. In the runs that failed, the log counted deferred remaps (7, 8 and 4 in three of them, 0 in the runs that did not fail), which fits a remap that runs late. xmem_test passes with this merged. AI-assisted (Claude Code).

clquwu added a commit to clquwu/leonida-ultimate that referenced this pull request Nov 25, 2026
…plit-view write-tracking, trim on xmem OOM, map DispatchMesh slot 129

- xmem: port ProjectLeonida/mandy#1972 (GPL-3.0, author kurrutako): track
  MapOwner (physical/scatter/slot31/mirror) on MappedPage, add trivial-POD
  UnmapCause labels on XMemMapPhysicalPagesScatter and copy_page_mappings,
  and add title_mapped_twin() so CopyPageMappingsBatchX leaves title-owned
  paired CPU mappings untouched on copy and unmap.
- xmem: preserve PageTrack (views, locked, armed, stamp) and read-only
  protection across coalesced-view splits in unmap_range(), fixing the
  title_shadow_sync_test split-unmap regression (now 85/85).
- xmem: on _aligned_malloc OOM in title_block(), invoke
  leonida::working_set::maybe_trim_process_working_set(), retry once, and
  emit a rate-limited OOM diagnostic if still null.
- d3d12_xs: map console command-list slot 129 -> PC GCL6 slot 79
  (ID3D12GraphicsCommandList6::DispatchMesh) with mock routing coverage for
  both 60-slot refusal and GCL6/GCL7 (x, y, z) forwarding.
- d3d12_vulkan: mark staging_pool_* mutable and trim_staging_pool()/
  destroy_staging_entry() const so allocate() const compiles cleanly.
- Verified Marvisak/VIctory#2618 (XAsyncGetResultSize) is already handled by
  vendored libHttpClient AsyncLib.cpp (writes 0 on pending, failed, taken).

AI-assisted: implemented and verified with Ninja RelWithDebInfo build and
CTest (9/9 passed under LEONIDA_VK_VALIDATION=1 on RTX 3080).
@kurrutako

Copy link
Copy Markdown
Contributor Author

Second retail boot with this change, same setup: it ran 1,739 s with the game still submitting (about 30,000 submissions in all) until I closed the window, and the process exited with code 0. It logged no title read faults and 9 faults left to the title. The five boots before the change ended by themselves after 210 to 1,308 s.

@kurrutako

Copy link
Copy Markdown
Contributor Author

Thanks for the review. Two points I checked in the code:

  • Which route a retail boot takes. Only tests call set_copy_page_mappings_hook (d3d12_xmem_placed_test.cpp, xs_proxy_mock_test.cpp); nothing in runtime/src or the shims installs it. So in a retail run queue_copy_page_mappings falls back to copy_page_mappings_default, which calls rt::xmem::copy_page_mappings_fn, the per-range function this PR changes. The batch rt::xmem::copy_page_mappings mirrors onto the CPU view only with LEONIDA_SLOT31_CPU_TWIN=1, and its changes check already skips CPU pages whose view isn't MapOwner::mirror. So the boots in the description ran the fixed route. Logging the route once is a good idea, and I'll add it.
  • Deferred remaps. When the remap is deferred, the list-value pump's action calls the same map once the queue reaches the point. So the owner check runs when the remap is applied, not when it is queued: a CPU page the title maps in between is seen as the title's.

I'll add a regression through the queue proxy's default adapter (no hook installed) that covers the case you describe: the source backed only through its CPU alias, a title-owned page at the destination's CPU twin, then a copy and an unmap. If the list-value fences can be held in that test, I'll cover the deferred order too. AI-assisted.

@aslan2142

Copy link
Copy Markdown
Collaborator

Tested on an RX 7900 XTX (RADV, Mesa 26.3.1, Wine) with the retail game, two runs each, same settings:

  • Without this PR: both runs reached about 22 minutes, then one ended on a read of a CPU page our mirror had unmapped 2.6 s earlier, which is exactly what this PR targets.
  • With this PR: that read is gone (0 title read faults), but both runs lost the device early. At about 648 s a graphics-ring timeout with no page fault; at about 281 s a GPU page fault past the end of the arena, then a compute-ring timeout.

So the mirror seems to keep something on the GPU side consistent too. We're tracing which data goes stale; until then, this probably shouldn't merge as is.

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

…ail route

queue_copy_page_mappings logs once whether CopyPageMappingsBatchX goes to an
installed batch hook or to the default adapter over xmem's range callback
(copy_page_mappings_fn), so a boot's read-fault reports can be matched to the
implementation the tests cover. Nothing in the runtime installs a hook, so a
retail run takes the default adapter.

d3d12_xmem_placed_test gains queue_remap_default_route, for that route: no
hook, a legacy-paired GPU-only destination, and a GPU-only pool backed only
through its CPU alias. The title maps a page of its own at one CPU twin while
the remap waits behind queue work (deferred with list values, waited for with
LEONIDA_LIST_VALUES=0), so ownership is checked when the remap is applied.
After the copy the GPU-only pages hold the pool's bytes, the other twin follows
them and the title's page keeps its bytes; after an unmap the GPU-only pages
and the mirrored twin are reserved and the title's page still holds its bytes.
On the code before this pull request the title's page check fails.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@kurrutako

Copy link
Copy Markdown
Contributor Author

Pushed 28fc026, as said above:

  • Route log: queue_copy_page_mappings logs once which route remaps (installed batch hook, or the default adapter over copy_page_mappings_fn).
  • New test: d3d12_xmem_placed_test gains queue_remap_default_route. It runs with no hook, a legacy-paired destination and a pool backed only through its CPU alias. The title maps its own page at one CPU twin while the remap waits behind held queue work. After the copy and then an unmap, the GPU-only pages change and the title's page keeps its bytes.
  • Results: it passes with and without LEONIDA_LIST_VALUES=0, and on the code before this PR it fails at the title-page check. xmem and xs_proxy_mock pass too.

AI-assisted.

@aslan2142

Copy link
Copy Markdown
Collaborator

Thanks for the new test. It shows the mechanism we suspect behind the device losses: after a copy, the GPU-only pages change while the paired CPU page keeps the title's bytes.

But the layer reads the title's GPU-only memory through that CPU view (title_gpu_views::to_cpu, used by geometry_va and rebase_title_vsharp; set_independent_pages is never called, so to_cpu always returns the CPU twin). With this PR those reads get the title's page instead of what slot 31 mapped, so geometry copies and rebased V#s can carry the wrong data: addresses or counts. That matches the wild read and the runaway work we saw. The update notices also go only to where the page is mapped (note_mapping), so those copies never refresh.

A way to keep your protection of the title's pages without this: give the layer a private copy of each pair that the title never sees, with every slot-31 copy and unmap mapped there, and point to_cpu and the update notices at it. We're adding a log of diverging pages to measure how often it happens.

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

@kurrutako

Copy link
Copy Markdown
Contributor Author

Agreed, that is a real gap: with this PR, a layer read through to_cpu at a pair whose CPU page is the title's gets the title's page, not what slot 31 mapped, and note_mapping refreshes only the address that was mapped. Before it, the title lost its own page instead. Neither is right where the two diverge.

A private layer view per pair fits both: it has every slot-31 copy and unmap at the GPU address, and the CPU twin's page wherever slot 31 has mapped nothing (the legacy pairs, read through the CPU view today). to_cpu and the update notices point at it, and the title's CPU range stays the title's. I can do that in this PR, unless you are writing it: tell me and I'll leave it to you.

Before that, I'll make the "left the title's mapping" log say whether the title's page holds the same physical page as the copy, so a boot shows how often they really diverge. In the boots in the description every kept page was a scatter mapping. AI-assisted.

@aslan2142

Copy link
Copy Markdown
Collaborator

I measured how often the kept CPU page really differs, using a logging-only patch on top of this PR (head 28fc0262d). At each slot-31 copy or unmap that leaves the title's mapping in place, the patch compares the physical page the title has at the CPU twin with the page the copy maps at the GPU-only address.

Setup: a retail boot on the Ryzen 7000 iGPU (gfx1036, RADV, Mesa 26.3.1, Wine), headless. So far it has run 25 minutes and about 35,000 submissions, and it is still running.

  • Copies where both hold the same page: 24,176. For these, reading through to_cpu is right.
  • Copies where they differ: at least one, with a different physical page on each side. The patch logs only a sample (the first 16, then powers of two), so it can't give the exact count. Your planned log would.
  • Unmaps that left the title's page: most of the about 4,100 divergent events. The CPU twin keeps its page while the GPU address becomes a hole, which is what this PR intends.
  • No device loss and no title read faults so far.

So the gap is real but rare in this boot. A private layer view per pair, as you describe, covers both cases. Please go ahead with it in this PR; we aren't writing it.

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

@kurrutako

Copy link
Copy Markdown
Contributor Author

Closing for now under the new rule of one open PR until two are merged; I am keeping #1722 open. The private layer view discussed above is written, but in my retail boots it moved the faults left to the title into the GPU-only range, and one boot ended in a device loss. I will reopen this once I have found out why. Thanks for the measurements and the review.

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

@kurrutako kurrutako closed this Nov 25, 2026
aslan2142 added a commit to aslan2142/leonida that referenced this pull request Nov 26, 2026
…itle's own mappings at the paired CPU range (RT-SLOT31-CPU-TWIN-TITLE-PAGES)

Head 28fc026. Closed by its author as incomplete; merged because world run 41 had it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
aslan2142 added a commit to aslan2142/leonida that referenced this pull request Nov 26, 2026
…hers

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@aslan2142

Copy link
Copy Markdown
Collaborator

Thank you, @kurrutako. Although you closed this, its commits went into main through #2350 (RT-LINUX-AMD-WORLD), merged at your head 28fc0262d with your authorship kept. The build that first drew the world on Linux with an AMD GPU had it, so we kept it in.

The gap discussed above is still open: a layer read through the CPU twin can see the title's page where slot 31 mapped a different one. Your private-layer-view follow-up, or another fix for that gap, is welcome as a new PR.

AI-assisted (Claude Code, Claude Opus 5.5 and Claude Fable 5.2)

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