xmem: chunk contiguous private spans and hand off private backing on held-span secondary aliases #600

Merged
DrArroway merged 1 commit from upstream-pr/600 into main 2026-11-30 23:16:34 +00:00
DrArroway commented 2026-11-30 21:19:20 +00:00 (Migrated from github.com)

Imported from brcly/TuxBox. Originally posted by DrArroway on 2026-11-30T21:19:20Z.


Follow-up to #501 (TUXBOX_XMEM_PRIVATE_VIEWS=1).

Problem

  1. Unchunked multi-gigabyte contiguous private spans: Pool::map_private_contiguous committed a contiguous private range as a single PrivateSpan regardless of size, rather than splitting at PRIVATE_SPAN_CHUNK (64 pages = 4 MiB). When a title maps a large contiguous pool (in ProjectScarlett, a 99,776-page / 6.09 GiB pool at 0xf00200000..0x1085e00000), a single GPU import hold anywhere inside that pool locks the entire span (page_imp=false, span_imp=true) and blocks secondary-alias write promotion on unrelated pages with ERROR_BUSY (170).
  2. Sibling holds inside 4 MiB spans and PrivateArena backings: Even with 4 MiB chunks, a sibling page held inside the same 4 MiB PrivateSpan still prevents detaching the canonical PrivateView slot (promote_token_to_section fails with ERROR_BUSY), and Bink2Scarlett.dll also triggers secondary-alias write faults on tokens whose canonical backing is a live (sometimes import-held) PrivateArena sitting inside a held 4 MiB span.
  3. Eager promotion and recycled shared-section tokens: eager_promote_dual_cpu_aliases tried to promote to SharedSection even when the destination slot lay inside a held/deferred PrivateSpan, and clear_page_mapping left recycled aliases == 0 tokens on PageBacking::SharedSection under private-views mode instead of returning them to PageBacking::Unbacked.

Changes

  • runtime/xmem/src/physical_pages/private_views.rs
    • Chunk map_private_contiguous at PRIVATE_SPAN_CHUNK (64 pages / 4 MiB) boundaries with upfront validation and atomic rollback via ChunkUndo, while keeping the Ok(false) fallback for plain non-placeholder VirtualAlloc reservations.
    • Extend unmap_private_view_range with a zero-allocation single-span fast path (unmap_single_private_span_range) plus upfront-validated multi-span contiguous unmapping.
    • Add Pool::handoff_private_backing, called from secondary_alias_veh on Err(ERROR_BUSY) before mirror_readonly_secondary_alias:
      • For PageBacking::PrivateView(old_va): freezes old_va to PAGE_READONLY, copies the 64 KiB page to fault_page (PAGE_READWRITE), demotes old_va and any stale readonly_secondary_aliases to PAGE_NOACCESS, transfers page.backing = PageBacking::PrivateView(fault_page), and calls self.touch(token).
      • For PageBacking::PrivateArena { arena, offset } (both unheld and import-held): copies the 64 KiB page to fault_page (PAGE_READWRITE), switches page.backing = PageBacking::PrivateView(fault_page) (existing HoldRecord.arenas keeps the held Arc<PrivateArena> alive), calls self.touch(token), and when was_held, returns the held VAs so secondary_alias_veh drops POOL and calls notify_backing_invalidated.
    • Skip eager_promote_dual_cpu_aliases when the destination VA lies in a held/deferred PrivateSpan.
    • Reset SharedSection tokens with aliases == 0 back to PageBacking::Unbacked in clear_page_mapping when private-views mode is active.
  • runtime/xmem/src/physical_pages.rs
    • Add rate-limited diagnostic logging (first 64 failures) on XMemMapPhysicalPages errors showing at, count, unmap, err, shared_tokens, max_aliases, and span_held.
  • runtime/xmem/src/tests/private_views.rs
    • Add 4 unit tests covering multi-chunk contiguous mapping/unmapping, independent secondary-alias promotion across 4 MiB chunk boundaries, PrivateView handoff inside a sibling-held 4 MiB span, and held-PrivateArena handoff with invalidation notification.

Verification

Evidence: TuxBox check of 6e1f2a1eb0, no uncommitted changes, GPU 1002:73bf: 17 passed, 0 failed, 0 not run
Evidence: TuxBox check of 6e1f2a1eb0, no uncommitted changes, GPU 1002:164e: 17 passed, 0 failed, 0 not run

  • tuxbox-xmem Windows unit tests under GE-Proton Wine on AMD Radeon RX 6900 XT (1002:73bf) (physical_ --test-threads=1): 40 passed; 0 failed; 1 ignored (scan_cost), including all 4 new private_views tests.
  • Explicit TUXBOX_XMEM_PRIVATE_VIEWS=1 checks on both GPUs — AMD Radeon RX 6900 XT (1002:73bf) and AMD Raphael Radeon 610M iGPU (1002:164e):
    • tuxbox.py verify --only xmem: 12/12 PASS on AMD Radeon RX 6900 XT (1002:73bf), 12/12 PASS on AMD Raphael Radeon 610M iGPU (1002:164e).
    • d3d12-buffer-import.exe (buffer-import, buffer-remap, buffer-unmap-alive, buffer-unmap-released): 4/4 PASS on AMD Radeon RX 6900 XT (1002:73bf), 4/4 PASS on AMD Raphael Radeon 610M iGPU (1002:164e).
  • Host RUSTDOCFLAGS="-D warnings" cargo doc --no-deps --target x86_64-pc-windows-msvc -p tuxbox-xmem --document-private-items, cargo clippy --locked --target x86_64-pc-windows-msvc -p tuxbox-xmem --all-targets -- -D warnings, cargo fmt --all -- --check, and tests.test_repository_hygiene (7/7): all passed.

Live ProjectScarlett runs (AMD Radeon RX 6900 XT (1002:73bf), GE-Proton11-14, TUXBOX_XMEM_PRIVATE_VIEWS=1, TUXBOX_CHECK_ONCE_PER_FRAME=1, VKD3D_CONFIG=no_upload_hvv)

All 3 confirmation runs also carried open PR #389 in d3d12_xs.dll to get past unrelated RADV GPUVM faults once inside the 3D world:

  • Run 1 (490.8s, Frame 1644, 83 3D mission frames): 4 live held-PrivateArena secondary-alias handoffs saved (0x33d2b0000, 0x779640000, 0x779660000, 0x36ddd0000), 0 err=170, 0 XMemMapPhysicalPages failures, 0 GPUVM faults.
  • Run 2 (407.7s, Frame 1269, 49 3D mission frames): 0 err=170, 0 XMemMapPhysicalPages failures, 0 GPUVM faults.
  • Run 3 (213.7s, Frame 967, 16 3D mission frames): 3 live held-PrivateArena secondary-alias handoffs saved (0x327600000, 0x327670000, 0x8f3120000), 0 err=170, 0 XMemMapPhysicalPages failures, 0 GPUVM faults.

All 3 runs ended on the pre-existing 0x80000003 breakpoint at game_scarlett_master.exe + 0x4DA6315 (seen identically on earlier runs with shared-section backing).

<!-- greyfinch-sync:brcly/TuxBox:issue:5573954178 --> Imported from [brcly/TuxBox](https://github.com/brcly/TuxBox/pull/600). Originally posted by [DrArroway](https://github.com/DrArroway) on 2026-11-30T21:19:20Z. --- Follow-up to #501 (`TUXBOX_XMEM_PRIVATE_VIEWS=1`). ### Problem 1. **Unchunked multi-gigabyte contiguous private spans:** `Pool::map_private_contiguous` committed a contiguous private range as a single `PrivateSpan` regardless of size, rather than splitting at `PRIVATE_SPAN_CHUNK` (64 pages = 4 MiB). When a title maps a large contiguous pool (in ProjectScarlett, a 99,776-page / 6.09 GiB pool at `0xf00200000..0x1085e00000`), a single GPU import hold anywhere inside that pool locks the entire span (`page_imp=false, span_imp=true`) and blocks secondary-alias write promotion on unrelated pages with `ERROR_BUSY` (`170`). 2. **Sibling holds inside 4 MiB spans and `PrivateArena` backings:** Even with 4 MiB chunks, a sibling page held inside the same 4 MiB `PrivateSpan` still prevents detaching the canonical `PrivateView` slot (`promote_token_to_section` fails with `ERROR_BUSY`), and `Bink2Scarlett.dll` also triggers secondary-alias write faults on tokens whose canonical backing is a live (sometimes import-held) `PrivateArena` sitting inside a held 4 MiB span. 3. **Eager promotion and recycled shared-section tokens:** `eager_promote_dual_cpu_aliases` tried to promote to `SharedSection` even when the destination slot lay inside a held/deferred `PrivateSpan`, and `clear_page_mapping` left recycled `aliases == 0` tokens on `PageBacking::SharedSection` under private-views mode instead of returning them to `PageBacking::Unbacked`. ### Changes - **`runtime/xmem/src/physical_pages/private_views.rs`** - Chunk `map_private_contiguous` at `PRIVATE_SPAN_CHUNK` (64 pages / 4 MiB) boundaries with upfront validation and atomic rollback via `ChunkUndo`, while keeping the `Ok(false)` fallback for plain non-placeholder `VirtualAlloc` reservations. - Extend `unmap_private_view_range` with a zero-allocation single-span fast path (`unmap_single_private_span_range`) plus upfront-validated multi-span contiguous unmapping. - Add `Pool::handoff_private_backing`, called from `secondary_alias_veh` on `Err(ERROR_BUSY)` before `mirror_readonly_secondary_alias`: - For `PageBacking::PrivateView(old_va)`: freezes `old_va` to `PAGE_READONLY`, copies the 64 KiB page to `fault_page` (`PAGE_READWRITE`), demotes `old_va` and any stale `readonly_secondary_aliases` to `PAGE_NOACCESS`, transfers `page.backing = PageBacking::PrivateView(fault_page)`, and calls `self.touch(token)`. - For `PageBacking::PrivateArena { arena, offset }` (both unheld and import-held): copies the 64 KiB page to `fault_page` (`PAGE_READWRITE`), switches `page.backing = PageBacking::PrivateView(fault_page)` (existing `HoldRecord.arenas` keeps the held `Arc<PrivateArena>` alive), calls `self.touch(token)`, and when `was_held`, returns the held VAs so `secondary_alias_veh` drops `POOL` and calls `notify_backing_invalidated`. - Skip `eager_promote_dual_cpu_aliases` when the destination VA lies in a held/deferred `PrivateSpan`. - Reset `SharedSection` tokens with `aliases == 0` back to `PageBacking::Unbacked` in `clear_page_mapping` when private-views mode is active. - **`runtime/xmem/src/physical_pages.rs`** - Add rate-limited diagnostic logging (first 64 failures) on `XMemMapPhysicalPages` errors showing `at`, `count`, `unmap`, `err`, `shared_tokens`, `max_aliases`, and `span_held`. - **`runtime/xmem/src/tests/private_views.rs`** - Add 4 unit tests covering multi-chunk contiguous mapping/unmapping, independent secondary-alias promotion across 4 MiB chunk boundaries, `PrivateView` handoff inside a sibling-held 4 MiB span, and held-`PrivateArena` handoff with invalidation notification. ### Verification Evidence: TuxBox check of 6e1f2a1eb0dd, no uncommitted changes, GPU 1002:73bf: 17 passed, 0 failed, 0 not run Evidence: TuxBox check of 6e1f2a1eb0dd, no uncommitted changes, GPU 1002:164e: 17 passed, 0 failed, 0 not run - `tuxbox-xmem` Windows unit tests under GE-Proton Wine on **AMD Radeon RX 6900 XT (1002:73bf)** (`physical_ --test-threads=1`): `40 passed; 0 failed; 1 ignored` (`scan_cost`), including all 4 new `private_views` tests. - Explicit `TUXBOX_XMEM_PRIVATE_VIEWS=1` checks on **both** GPUs — **AMD Radeon RX 6900 XT (1002:73bf)** and **AMD Raphael Radeon 610M iGPU (1002:164e)**: - `tuxbox.py verify --only xmem`: `12/12` PASS on **AMD Radeon RX 6900 XT (1002:73bf)**, `12/12` PASS on **AMD Raphael Radeon 610M iGPU (1002:164e)**. - `d3d12-buffer-import.exe` (`buffer-import`, `buffer-remap`, `buffer-unmap-alive`, `buffer-unmap-released`): `4/4` PASS on **AMD Radeon RX 6900 XT (1002:73bf)**, `4/4` PASS on **AMD Raphael Radeon 610M iGPU (1002:164e)**. - Host `RUSTDOCFLAGS="-D warnings" cargo doc --no-deps --target x86_64-pc-windows-msvc -p tuxbox-xmem --document-private-items`, `cargo clippy --locked --target x86_64-pc-windows-msvc -p tuxbox-xmem --all-targets -- -D warnings`, `cargo fmt --all -- --check`, and `tests.test_repository_hygiene` (`7/7`): all passed. #### Live ProjectScarlett runs (AMD Radeon RX 6900 XT (1002:73bf), GE-Proton11-14, `TUXBOX_XMEM_PRIVATE_VIEWS=1`, `TUXBOX_CHECK_ONCE_PER_FRAME=1`, `VKD3D_CONFIG=no_upload_hvv`) All 3 confirmation runs also carried open PR #389 in `d3d12_xs.dll` to get past unrelated RADV GPUVM faults once inside the 3D world: - **Run 1** (`490.8s`, Frame 1644, 83 3D mission frames): 4 live held-`PrivateArena` secondary-alias handoffs saved (`0x33d2b0000`, `0x779640000`, `0x779660000`, `0x36ddd0000`), `0` `err=170`, `0` `XMemMapPhysicalPages` failures, `0` GPUVM faults. - **Run 2** (`407.7s`, Frame 1269, 49 3D mission frames): `0` `err=170`, `0` `XMemMapPhysicalPages` failures, `0` GPUVM faults. - **Run 3** (`213.7s`, Frame 967, 16 3D mission frames): 3 live held-`PrivateArena` secondary-alias handoffs saved (`0x327600000`, `0x327670000`, `0x8f3120000`), `0` `err=170`, `0` `XMemMapPhysicalPages` failures, `0` GPUVM faults. All 3 runs ended on the pre-existing `0x80000003` breakpoint at `game_scarlett_master.exe + 0x4DA6315` (seen identically on earlier runs with shared-section backing).
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!600
No description provided.