xmem: adopt held private backings on held deferred slots and retry concurrent VEH faults #608

Merged
DrArroway merged 1 commit from upstream-pr/608 into main 2026-12-01 13:56:01 +00:00
DrArroway commented 2026-12-01 01:21:11 +00:00 (Migrated from github.com)

Imported from brcly/TuxBox. Originally posted by DrArroway on 2026-12-01T01:21:11Z.


Problem & Root Cause

Under TUXBOX_XMEM_PRIVATE_VIEWS=1, mapping a physical page into a held deferred PrivatePlaceholder slot while its token already had either a held PrivateArena or an unheld PrivateView triggered a split-brain coherent-memory bug in runtime/xmem:

  1. classify_private_slot + mask_slot mismatch:

    • When the token had a held PrivateArena (or an unheld PrivateView) and the destination VA was a held deferred PrivatePlaceholder slot (has_hold(at, at + PAGE)), classify_private_slot returned SlotRole::SecondaryAlias.
    • Meanwhile mask_slot was false because at itself was held, so map_private_page / map_private_contiguous left the slot PAGE_READWRITE, skipped populate_page_bytes, and did not insert at into secondary_aliases.
    • Subsequent xcrdapi DMA writes via acquire_physical_access wrote directly to PrivateArena while the CPU read ancient unpopulated bytes from the held PrivatePlaceholder slot without trapping, crashing ProjectScarlett (0x80000003 at game_scarlett_master.exe + 0x4DA6315 / +0x9DFC18).
  2. Two companion coherence issues exposed by the fix:

    • Held non-CPU PrivateView upgrade: When a CPU aperture maps a token whose canonical backing is already a held non-CPU PrivateView, blocking the upgrade on !has_hold(old_va) left the CPU view masked as a SecondaryAlias (err=170 on write when both ends are held), while upgrading without invalidation broke coherence with the old held view. Now the CPU view upgrades cleanly (SlotRole::Upgrade(old_va)), pushes old_va to invalidated so Pool::map calls notify_backing_invalidated, and defers PAGE_NOACCESS masking until release_hold drops its last view_hold (mask_released_secondary_alias).
    • Concurrent secondary_alias_veh fault race (0xc0000005): When another thread concurrently promotes or hands off fault_page and removes it from secondary_aliases before a faulting thread acquires POOL, returning 0 surfaced an unhandled access violation. Retrying (-1) when pool.mappings.contains_key(&fault_page) and VirtualQuery confirms compatible committed protection resolves the race cleanly.

Changes

  • runtime/xmem/src/physical_pages/private_views.rs:
    • classify_private_slot: adopts held PrivateArena as SlotRole::Canonical when at_held is true, and upgrades PrivateView(old_va) when (is_cpu && !page.backing_is_cpu) || (at_held && !self.has_hold(*old_va, *old_va + PAGE)).
    • apply_slot_role: preserves #573's self.set_mapping(at, token), threads invalidated: &mut Vec<usize> to notify notify_backing_invalidated when adopting a held PrivateArena or upgrading from a held PrivateView, updates page.backing_is_cpu = is_cpu, demotes stale readonly_secondary_aliases via demote_readonly_secondary_aliases, and logs a capped warning if a double-held SecondaryAlias is left unmasked.
    • map_private_page & map_private_contiguous: thread invalidated, gate populate_page_bytes / deferred reprotection on !matches!(role, SlotRole::SecondaryAlias) || self.has_hold(va, va + PAGE), and pass invalidated to apply_slot_role.
    • mask_released_secondary_alias: masks live non-canonical private views to PAGE_NOACCESS and registers them in secondary_aliases once their last view_hold releases.
    • Re-export pub(super) use super::private_alias_veh::ensure_secondary_alias_veh; for section_watch.rs (#573).
  • runtime/xmem/src/physical_pages/backing.rs (release_hold), runtime/xmem/src/physical_pages.rs, runtime/xmem/src/physical_pages/copy_mappings.rs:
    • Call mask_released_secondary_alias on live unheld private views in release_hold and thread invalidated through map_cpu_page / map_private_page / map_private_contiguous.
  • runtime/xmem/src/physical_pages/private_alias_veh.rs:
    • Split secondary-alias VEH, promotion, read-only mirroring, and private-backing handoff out of private_views.rs to stay under the 1,200-line repository limit.
    • Add page_allows_access transient retry in secondary_alias_veh and call demote_readonly_secondary_aliases from handoff_private_backing.
  • runtime/xmem/src/tests/private_views.rs:
    • Add 3 regression tests (held_deferred_slot_adopts_held_private_arena_and_notifies_invalidation, held_deferred_slot_upgrades_unheld_private_view_without_split_brain, and cpu_upgrade_from_held_non_cpu_view_invalidates_and_masks_on_release).

Verification (rebased onto 717a74c4, including #573)

  • Repository hygiene, formatting & Clippy: cargo fmt --all --check, cargo clippy --locked --target x86_64-pc-windows-msvc --workspace --all-targets -- -D warnings, and tests.test_repository_hygiene (7/7) passed.
  • Full tuxbox.py check on both AMD Radeon RX 6900 XT (1002:73bf) and AMD Raphael Radeon 610M iGPU (1002:164e):

Evidence: TuxBox check of 2d578f8fe4, no uncommitted changes, GPU 1002:73bf: 17 passed, 0 failed, 0 not run

Evidence: TuxBox check of 2d578f8fe4, no uncommitted changes, GPU 1002:164e: 17 passed, 0 failed, 0 not run

  • Explicit TUXBOX_XMEM_PRIVATE_VIEWS=1 checks & Windows unit tests on commit 2d578f8fe4a2 across both AMD Radeon RX 6900 XT (1002:73bf) and AMD Raphael Radeon 610M iGPU (1002:164e):
    • tuxbox.py verify --only xmem: 12/12 PASS on both adapters (xmem-heaps through xmem-pages).
    • tools/check_d3d12_regressions.py (buffer-import, buffer-remap, buffer-unmap-alive, buffer-unmap-released, alias-deferred): 5/5 PASS on both adapters.
    • tuxbox-xmem Windows unit tests under GE-Proton11-14 wine: 45 passed; 0 failed; 2 ignored on both adapters (including #573's section_views_are_watched_by_protection and all 3 new regression tests).
  • Real-world ProjectScarlett verification (TUXBOX_XMEM_PRIVATE_VIEWS=1) on AMD Radeon RX 6900 XT (1002:73bf):
    • 3 consecutive runs on the fix (222.4s / 8,216 presents, 324.5s / 8,267 presents, 427.8s / 8,298 presents; 5/5 runs total) exercised both adopted held PrivateArena and upgraded PrivateView with 0 0x80000003 (+0x4DA6315 / +0x9DFC18) crashes, 0 err=170, 0 unhandled 0xc0000005, and 0 left unmasked warnings.
<!-- greyfinch-sync:brcly/TuxBox:issue:5576960016 --> Imported from [brcly/TuxBox](https://github.com/brcly/TuxBox/pull/608). Originally posted by [DrArroway](https://github.com/DrArroway) on 2026-12-01T01:21:11Z. --- ### Problem & Root Cause Under `TUXBOX_XMEM_PRIVATE_VIEWS=1`, mapping a physical page into a held deferred `PrivatePlaceholder` slot while its token already had either a held `PrivateArena` or an unheld `PrivateView` triggered a split-brain coherent-memory bug in `runtime/xmem`: 1. **`classify_private_slot` + `mask_slot` mismatch:** - When the token had a held `PrivateArena` (or an unheld `PrivateView`) and the destination VA was a held deferred `PrivatePlaceholder` slot (`has_hold(at, at + PAGE)`), `classify_private_slot` returned `SlotRole::SecondaryAlias`. - Meanwhile `mask_slot` was `false` because `at` itself was held, so `map_private_page` / `map_private_contiguous` left the slot `PAGE_READWRITE`, skipped `populate_page_bytes`, and did not insert `at` into `secondary_aliases`. - Subsequent `xcrdapi` DMA writes via `acquire_physical_access` wrote directly to `PrivateArena` while the CPU read ancient unpopulated bytes from the held `PrivatePlaceholder` slot without trapping, crashing ProjectScarlett (`0x80000003` at `game_scarlett_master.exe + 0x4DA6315` / `+0x9DFC18`). 2. **Two companion coherence issues exposed by the fix:** - **Held non-CPU `PrivateView` upgrade:** When a CPU aperture maps a token whose canonical backing is already a held non-CPU `PrivateView`, blocking the upgrade on `!has_hold(old_va)` left the CPU view masked as a `SecondaryAlias` (`err=170` on write when both ends are held), while upgrading without invalidation broke coherence with the old held view. Now the CPU view upgrades cleanly (`SlotRole::Upgrade(old_va)`), pushes `old_va` to `invalidated` so `Pool::map` calls `notify_backing_invalidated`, and defers `PAGE_NOACCESS` masking until `release_hold` drops its last `view_hold` (`mask_released_secondary_alias`). - **Concurrent `secondary_alias_veh` fault race (`0xc0000005`):** When another thread concurrently promotes or hands off `fault_page` and removes it from `secondary_aliases` before a faulting thread acquires `POOL`, returning `0` surfaced an unhandled access violation. Retrying (`-1`) when `pool.mappings.contains_key(&fault_page)` and `VirtualQuery` confirms compatible committed protection resolves the race cleanly. ### Changes - `runtime/xmem/src/physical_pages/private_views.rs`: - `classify_private_slot`: adopts held `PrivateArena` as `SlotRole::Canonical` when `at_held` is true, and upgrades `PrivateView(old_va)` when `(is_cpu && !page.backing_is_cpu) || (at_held && !self.has_hold(*old_va, *old_va + PAGE))`. - `apply_slot_role`: preserves `#573`'s `self.set_mapping(at, token)`, threads `invalidated: &mut Vec<usize>` to notify `notify_backing_invalidated` when adopting a held `PrivateArena` or upgrading from a held `PrivateView`, updates `page.backing_is_cpu = is_cpu`, demotes stale `readonly_secondary_aliases` via `demote_readonly_secondary_aliases`, and logs a capped warning if a double-held `SecondaryAlias` is left unmasked. - `map_private_page` & `map_private_contiguous`: thread `invalidated`, gate `populate_page_bytes` / deferred reprotection on `!matches!(role, SlotRole::SecondaryAlias) || self.has_hold(va, va + PAGE)`, and pass `invalidated` to `apply_slot_role`. - `mask_released_secondary_alias`: masks live non-canonical private views to `PAGE_NOACCESS` and registers them in `secondary_aliases` once their last `view_hold` releases. - Re-export `pub(super) use super::private_alias_veh::ensure_secondary_alias_veh;` for `section_watch.rs` (`#573`). - `runtime/xmem/src/physical_pages/backing.rs` (`release_hold`), `runtime/xmem/src/physical_pages.rs`, `runtime/xmem/src/physical_pages/copy_mappings.rs`: - Call `mask_released_secondary_alias` on live unheld private views in `release_hold` and thread `invalidated` through `map_cpu_page` / `map_private_page` / `map_private_contiguous`. - `runtime/xmem/src/physical_pages/private_alias_veh.rs`: - Split secondary-alias VEH, promotion, read-only mirroring, and private-backing handoff out of `private_views.rs` to stay under the 1,200-line repository limit. - Add `page_allows_access` transient retry in `secondary_alias_veh` and call `demote_readonly_secondary_aliases` from `handoff_private_backing`. - `runtime/xmem/src/tests/private_views.rs`: - Add 3 regression tests (`held_deferred_slot_adopts_held_private_arena_and_notifies_invalidation`, `held_deferred_slot_upgrades_unheld_private_view_without_split_brain`, and `cpu_upgrade_from_held_non_cpu_view_invalidates_and_masks_on_release`). ### Verification (rebased onto `717a74c4`, including `#573`) - **Repository hygiene, formatting & Clippy:** `cargo fmt --all --check`, `cargo clippy --locked --target x86_64-pc-windows-msvc --workspace --all-targets -- -D warnings`, and `tests.test_repository_hygiene` (7/7) passed. - **Full `tuxbox.py check` on both AMD Radeon RX 6900 XT (1002:73bf) and AMD Raphael Radeon 610M iGPU (1002:164e):** Evidence: TuxBox check of 2d578f8fe4a2, no uncommitted changes, GPU 1002:73bf: 17 passed, 0 failed, 0 not run Evidence: TuxBox check of 2d578f8fe4a2, no uncommitted changes, GPU 1002:164e: 17 passed, 0 failed, 0 not run - **Explicit `TUXBOX_XMEM_PRIVATE_VIEWS=1` checks & Windows unit tests on commit `2d578f8fe4a2` across both AMD Radeon RX 6900 XT (1002:73bf) and AMD Raphael Radeon 610M iGPU (1002:164e):** - `tuxbox.py verify --only xmem`: 12/12 `PASS` on both adapters (`xmem-heaps` through `xmem-pages`). - `tools/check_d3d12_regressions.py` (`buffer-import`, `buffer-remap`, `buffer-unmap-alive`, `buffer-unmap-released`, `alias-deferred`): 5/5 `PASS` on both adapters. - `tuxbox-xmem` Windows unit tests under GE-Proton11-14 `wine`: `45 passed; 0 failed; 2 ignored` on both adapters (including `#573`'s `section_views_are_watched_by_protection` and all 3 new regression tests). - **Real-world ProjectScarlett verification (`TUXBOX_XMEM_PRIVATE_VIEWS=1`) on AMD Radeon RX 6900 XT (1002:73bf):** - 3 consecutive runs on the fix (`222.4s` / `8,216` presents, `324.5s` / `8,267` presents, `427.8s` / `8,298` presents; 5/5 runs total) exercised both `adopted held PrivateArena` and `upgraded PrivateView` with **0** `0x80000003` (`+0x4DA6315` / `+0x9DFC18`) crashes, **0** `err=170`, **0** unhandled `0xc0000005`, and **0** `left unmasked` warnings.
brcly commented 2026-12-01 03:24:47 +00:00 (Migrated from github.com)

Imported from brcly/TuxBox. Originally posted by brcly on 2026-12-01T03:24:47Z.


@DrArroway thanks, the diagnosis and the runs are clear. #573 has just been merged to main, and it changes the same XMem page code, so this now conflicts. Could you rebase onto current main? It's ready to merge once it's clean and CI passes.

<!-- greyfinch-sync:brcly/TuxBox:comment:7299678394 --> Imported from [brcly/TuxBox](https://github.com/brcly/TuxBox/pull/608#issuecomment-7299678394). Originally posted by [brcly](https://github.com/brcly) on 2026-12-01T03:24:47Z. --- @DrArroway thanks, the diagnosis and the runs are clear. #573 has just been merged to main, and it changes the same XMem page code, so this now conflicts. Could you rebase onto current main? It's ready to merge once it's clean and CI passes.
DrArroway commented 2026-12-01 07:40:50 +00:00 (Migrated from github.com)

Imported from brcly/TuxBox. Originally posted by DrArroway on 2026-12-01T07:40:50Z.


@brcly Rebased onto current main (717a74c4, preserving #573's set_mapping/clear_mapping and re-exporting ensure_secondary_alias_veh from private_views.rs for section_watch.rs).

Re-ran full dual-GPU verification on commit 2d578f8fe4a2 across both AMD Radeon RX 6900 XT (1002:73bf) and AMD Raphael Radeon 610M iGPU (1002:164e) (tuxbox.py check 17/17, explicit TUXBOX_XMEM_PRIVATE_VIEWS=1 xmem guest suite 12/12 + D3D12 regression controls 5/5, and tuxbox-xmem Windows unit tests 45/45), and all 5/5 CI checks are green. Ready to merge!

<!-- greyfinch-sync:brcly/TuxBox:comment:7304350965 --> Imported from [brcly/TuxBox](https://github.com/brcly/TuxBox/pull/608#issuecomment-7304350965). Originally posted by [DrArroway](https://github.com/DrArroway) on 2026-12-01T07:40:50Z. --- @brcly Rebased onto current `main` (`717a74c4`, preserving `#573`'s `set_mapping`/`clear_mapping` and re-exporting `ensure_secondary_alias_veh` from `private_views.rs` for `section_watch.rs`). Re-ran full dual-GPU verification on commit `2d578f8fe4a2` across both **AMD Radeon RX 6900 XT (1002:73bf)** and **AMD Raphael Radeon 610M iGPU (1002:164e)** (`tuxbox.py check` 17/17, explicit `TUXBOX_XMEM_PRIVATE_VIEWS=1` `xmem` guest suite 12/12 + D3D12 regression controls 5/5, and `tuxbox-xmem` Windows unit tests 45/45), and all 5/5 CI checks are green. Ready to merge!
NickStallman commented 2026-12-01 07:49:18 +00:00 (Migrated from github.com)

Imported from brcly/TuxBox. Originally posted by NickStallman on 2026-12-01T07:49:18Z.


@DrArroway one data point from NVIDIA, for the record (one run, and on an integration build, so not a review of this head): with the previous head of this PR (b80aaee0, on main b3873ec4 with other patches) and TUXBOX_XMEM_PRIVATE_VIEWS=1, ProjectScarlett on an RTX 5090 (620.70.10, GE-Proton11-15) stopped presenting about 3 s into the world: 12 Vulkan wait timeouts in the log, no Xid (and 0x80000003 breakpoints in the log, but every run of ours has those), then the title's own watchdog ended it at 157 s. The same build with the variable unset ran 20 minutes. I haven't repeated it, and I haven't run your rebased head.

<!-- greyfinch-sync:brcly/TuxBox:comment:7304520229 --> Imported from [brcly/TuxBox](https://github.com/brcly/TuxBox/pull/608#issuecomment-7304520229). Originally posted by [NickStallman](https://github.com/NickStallman) on 2026-12-01T07:49:18Z. --- @DrArroway one data point from NVIDIA, for the record (one run, and on an integration build, so not a review of this head): with the previous head of this PR (b80aaee0, on main b3873ec4 with other patches) and `TUXBOX_XMEM_PRIVATE_VIEWS=1`, ProjectScarlett on an RTX 5090 (620.70.10, GE-Proton11-15) stopped presenting about 3 s into the world: 12 Vulkan wait timeouts in the log, no Xid (and `0x80000003` breakpoints in the log, but every run of ours has those), then the title's own watchdog ended it at 157 s. The same build with the variable unset ran 20 minutes. I haven't repeated it, and I haven't run your rebased head.
DrArroway commented 2026-12-01 07:54:20 +00:00 (Migrated from github.com)

Imported from brcly/TuxBox. Originally posted by DrArroway on 2026-12-01T07:54:20Z.


@NickStallman Thanks for sharing that RTX 5090 data point.

Across our AMD Radeon RX 6900 XT (1002:73bf) TUXBOX_XMEM_PRIVATE_VIEWS=1 runs with #608 we didn't hit Vulkan wait timeouts or early world stalls (8,200+ presents per run). If you still have that log around, did any TuxBox xmem: lines (such as secondary alias ... fault not promoted, left unmasked, or adopted/upgraded) or GPU memory wait held past 5s lines show up right before the wait timeouts? That would help tell if something in xmem stalled there or if it's an NVIDIA / integration-patch interaction.

<!-- greyfinch-sync:brcly/TuxBox:comment:7304629300 --> Imported from [brcly/TuxBox](https://github.com/brcly/TuxBox/pull/608#issuecomment-7304629300). Originally posted by [DrArroway](https://github.com/DrArroway) on 2026-12-01T07:54:20Z. --- @NickStallman Thanks for sharing that RTX 5090 data point. Across our **AMD Radeon RX 6900 XT (1002:73bf)** `TUXBOX_XMEM_PRIVATE_VIEWS=1` runs with #608 we didn't hit Vulkan wait timeouts or early world stalls (`8,200+` presents per run). If you still have that log around, did any `TuxBox xmem:` lines (such as `secondary alias ... fault not promoted`, `left unmasked`, or `adopted`/`upgraded`) or `GPU memory wait held past 5s` lines show up right before the wait timeouts? That would help tell if something in `xmem` stalled there or if it's an NVIDIA / integration-patch interaction.
NickStallman commented 2026-12-01 08:19:20 +00:00 (Migrated from github.com)

Imported from brcly/TuxBox. Originally posted by NickStallman on 2026-12-01T08:19:20Z.


@DrArroway the log is here: no TuxBox xmem: line at all in that run, and no GPU memory wait held past 5s line; so nothing in it names a page or an alias. The timeouts begin a few seconds into the world (the first one lands around some pipeline creation failing, which other runs have too), and from there nothing more is submitted. So I can't say whether xmem stalled there or it's NVIDIA or our patches; I'd treat it as unconfirmed. I'm happy to repeat it on your rebased head without our other patches when the machine is free.

<!-- greyfinch-sync:brcly/TuxBox:comment:7305165058 --> Imported from [brcly/TuxBox](https://github.com/brcly/TuxBox/pull/608#issuecomment-7305165058). Originally posted by [NickStallman](https://github.com/NickStallman) on 2026-12-01T08:19:20Z. --- @DrArroway the log is here: no `TuxBox xmem:` line at all in that run, and no `GPU memory wait held past 5s` line; so nothing in it names a page or an alias. The timeouts begin a few seconds into the world (the first one lands around some pipeline creation failing, which other runs have too), and from there nothing more is submitted. So I can't say whether xmem stalled there or it's NVIDIA or our patches; I'd treat it as unconfirmed. I'm happy to repeat it on your rebased head without our other patches when the machine is free.
Sign in to join this conversation.
No reviewers
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
brcly/TuxBox!608
No description provided.