xmem: end protection runs at private-span boundaries #662

Merged
snoozedDev merged 1 commit from upstream-pr/662 into main 2026-12-02 21:30:34 +00:00
snoozedDev commented 2026-12-02 20:35:49 +00:00 (Migrated from github.com)

Imported from brcly/TuxBox. Originally posted by snoozedDev on 2026-12-02T20:35:49Z.


Summary

With TUXBOX_XMEM_PRIVATE_VIEWS=1, XMemMapPhysicalPages returns NULL with ERROR_INVALID_PARAMETER when a second mapping of pages crosses a 4 MiB boundary. A plain 8 MiB double mapping, chunk-aligned, fails every time, in either order.

Cause: since #600 a contiguous map is cut into one private span per PRIVATE_SPAN_CHUNK (4 MiB), and each span is its own OS allocation. When the second mapping masks the new alias or demotes the old canonical pages, protect_runs merges adjacent pages into one VirtualProtect call. VirtualProtect refuses a range over two allocations, so the map is rolled back and fails. A #[track_caller] print in last_error (not in this PR) put the failing call in protect_private_range, reached from the mask_new/demote_old pair at the end of map_private_contiguous.

The change: protect_runs becomes a Pool method and also ends a run where the page's private span changes. It splits by span, not by chunk arithmetic, because single-page maps can make spans that aren't chunk-aligned. Its three callers are otherwise unchanged. Default (section) mode never reaches this code; only private views makes private spans.

This is also one of the two causes of the rare gpu_pool_then_plain_cpu_view_upgrades_backing_and_keeps_mem_private failure I left untraced in #659: when the OS puts its 4-page second GPU alias across a 4 MiB boundary, its map fails (physical_backing.rs:372). The other cause is the failure #659 quoted at :383. That one comes from the CPU view landing across a boundary (see below). In #659 I misnamed it as the acquire on the second GPU alias; line 383 is the acquire on the GPU pool range, whose backing is the CPU view.

I didn't run the game, so I don't know whether the title makes such a call. When it does, the provider logs XMemMapPhysicalPages failed ... err=87.

This contribution was written by an AI agent (Claude Opus 5.5) operating under my supervision. I am accountable for it and will respond to review.

Tested

Run the checks relevant to this change.

  • The checks relevant to this change passed
  • Changed or new behaviour is covered by a Lanes scenario, or the reason it cannot be is stated below
  • Where the table calls for AMD and NVIDIA, they passed on both. Otherwise, say which vendor still needs to run them: both. I didn't run the xmem, lanes-selftest or D3D12 buffer controls on either card.

The first box is unticked because the GPU suites the table names for runtime/xmem/ were not run. What passed is the unit tests under Wine and tuxbox.py check --no-gpu, below.

No Lanes scenario or guest suite turns on private views (nothing under tools/ or examples/ sets TUXBOX_XMEM_PRIVATE_VIEWS), so by reading none of them reaches this code. The new unit tests cover it instead.

GPU, driver and backend: none (CPU-only unit tests under Wine).

The Evidence: line tuxbox.py check prints last, one per card (it names the commit it tested; run it on this pull request's head):

Evidence: TuxBox check of b698ea5d8e36, no uncommitted changes, no GPU checks: 12 passed, 0 failed, 1 not run

(The one not run is the synthetic shaders through the runtime's DXC flags; no Linux DXC was on PATH.)

Commands run and their results:

cargo +1.95.0 xwin test --locked --no-run --target x86_64-pc-windows-msvc -p tuxbox-xmem
WINEPREFIX=<scratch> WINEDEBUG=-all WINEDLLOVERRIDES="mscoree,mshtml=d" \
  <GE-Proton11-14>/files/bin/wine <test exe> [filter] [--test-threads=1]

The new tests in src/tests/private_views.rs reserve a free 64 MiB range, release it, and place both views at chosen offsets from a 4 MiB boundary inside it (retrying if another test thread takes the range first). Each test maps, writes a value into every page, maps again, and then checks that the canonical view is read-write with the values intact and the other view is PAGE_NOACCESS. After unmapping both, it checks that no private span is left in either range.

case main (c1139ab2) this PR
128 pages chunk-aligned, GPU aperture then CPU view second map NULL pass
128 pages chunk-aligned, CPU view then GPU aperture second map NULL pass
2 pages at chunk offset 63, both orders second map NULL pass
4-page second GPU alias at chunk offset 62 second map NULL pass
4-page GPU range at chunk offset 62, then a CPU view second map NULL pass

Whole xmem test binary with this PR, serial and default threads: 53 passed, 2 ignored.

Loaded runs: 600 runs per binary, four at a time, each in its own directory, with #659's test change applied to both binaries so its known failure doesn't hide others. The binary ran from a 95-character path, where ranges land across 4 MiB boundaries more often here:

main this PR
runs failed 15 of 600 7 of 600
gpu_pool_then_plain_cpu_view...: second GPU alias map returns NULL (physical_backing.rs:372) 2 0
gpu_pool_then_plain_cpu_view...: single-run acquire returns 0 with the CPU view across a boundary (:383) 5 4
mapping_state_seqlock_tracks_active_and_completed_mutations (:778 to :806) 8 1
watched_pages_of_one_allocation_are_scanned_as_one_run_across_small_gaps (physical_tracking.rs:685) 0 2

Every failed run has one of these. The other tests that failed in those runs did so only because of it: a poisoned POOL lock, or spans and apertures the failed test left behind. What's left isn't fixed here:

  • :383 is by design: when the OS puts the 4-page CPU view across a boundary, TuxBoxAcquirePhysicalBacking (one run) answers ERROR_NOT_SUPPORTED, and d3d12 uses the runs variant for that. The test could place that view inside one chunk.
  • physical_tracking.rs:685 is the same kind of test assumption as #659's: the test maps 40 pages at an OS-chosen address and expects the runs [(0, 8), (30, 1)]. When the range lands across a 4 MiB boundary it gets two spans, and the runs came out as [(0, 3), (7, 1), (30, 1)]. That test maps once with no second view, so protect_runs only gets empty lists there and behaves the same before and after this change. It shows up here because the binary's address layout differs.
  • The seqlock test reads the process-wide mutation counter while other tests may be mapping. I haven't traced it.

From a short path, main failed 1 of 600 (the seqlock test) and this PR 4 of 600: the seqlock test 3 times, and once a_released_graphics_reservation_loses_its_id_and_a_smaller_one_does_not_take_it (physical_backing.rs:1029), whose released address another test can reserve before its query. The seqlock test runs in section mode and the reservation test maps nothing, so neither reaches the changed code. I haven't traced why the counts differ between the two binaries.

Also run:

  • cargo fmt --all --check and cargo +1.95.0 clippy --locked --target x86_64-pc-windows-msvc -p tuxbox-xmem --all-targets -- -D warnings: pass.
  • CI's Miri line for -p tuxbox-xmem (local nightly): stops on main's two VirtualAlloc2 tests, so the Miri job on this PR stays red until #658 lands; with #658 applied, 0 failed; 55 ignored. The new tests carry the GlobalMemoryStatusEx ignore like their neighbours.

Checks

CI runs the code checks: the Python and Rust unit tests, the translator tests through DXC, a Windows typecheck, clippy, formatting, rustdoc, and Miri on the providers. It has no GPU.

If this pull request changes evidence JSON, regenerate the results table and the progress map and include both files:

python tuxbox.py status > docs/RESULTS.md
python tuxbox.py status --svg docs/progress.svg

This pull request changes no evidence JSON.

<!-- greyfinch-sync:brcly/TuxBox:issue:5614257603 --> Imported from [brcly/TuxBox](https://github.com/brcly/TuxBox/pull/662). Originally posted by [snoozedDev](https://github.com/snoozedDev) on 2026-12-02T20:35:49Z. --- ## Summary With `TUXBOX_XMEM_PRIVATE_VIEWS=1`, `XMemMapPhysicalPages` returns NULL with `ERROR_INVALID_PARAMETER` when a second mapping of pages crosses a 4 MiB boundary. A plain 8 MiB double mapping, chunk-aligned, fails every time, in either order. Cause: since #600 a contiguous map is cut into one private span per `PRIVATE_SPAN_CHUNK` (4 MiB), and each span is its own OS allocation. When the second mapping masks the new alias or demotes the old canonical pages, `protect_runs` merges adjacent pages into one `VirtualProtect` call. `VirtualProtect` refuses a range over two allocations, so the map is rolled back and fails. A `#[track_caller]` print in `last_error` (not in this PR) put the failing call in `protect_private_range`, reached from the `mask_new`/`demote_old` pair at the end of `map_private_contiguous`. The change: `protect_runs` becomes a `Pool` method and also ends a run where the page's private span changes. It splits by span, not by chunk arithmetic, because single-page maps can make spans that aren't chunk-aligned. Its three callers are otherwise unchanged. Default (section) mode never reaches this code; only private views makes private spans. This is also one of the two causes of the rare `gpu_pool_then_plain_cpu_view_upgrades_backing_and_keeps_mem_private` failure I left untraced in #659: when the OS puts its 4-page second GPU alias across a 4 MiB boundary, its `map` fails (`physical_backing.rs:372`). The other cause is the failure #659 quoted at `:383`. That one comes from the CPU view landing across a boundary (see below). In #659 I misnamed it as the acquire on the second GPU alias; line 383 is the acquire on the GPU pool range, whose backing is the CPU view. I didn't run the game, so I don't know whether the title makes such a call. When it does, the provider logs `XMemMapPhysicalPages failed ... err=87`. This contribution was written by an AI agent (Claude Opus 5.5) operating under my supervision. I am accountable for it and will respond to review. ## Tested Run the [checks relevant to this change](../CONTRIBUTING.md#test-what-your-change-touches). - [ ] The checks relevant to this change passed - [x] Changed or new behaviour is covered by a Lanes scenario, or the reason it cannot be is stated below - [ ] Where the table calls for AMD and NVIDIA, they passed on both. Otherwise, say which vendor still needs to run them: both. I didn't run the `xmem`, `lanes-selftest` or D3D12 buffer controls on either card. The first box is unticked because the GPU suites the table names for `runtime/xmem/` were not run. What passed is the unit tests under Wine and `tuxbox.py check --no-gpu`, below. No Lanes scenario or guest suite turns on private views (nothing under `tools/` or `examples/` sets `TUXBOX_XMEM_PRIVATE_VIEWS`), so by reading none of them reaches this code. The new unit tests cover it instead. GPU, driver and backend: none (CPU-only unit tests under Wine). The `Evidence:` line `tuxbox.py check` prints last, one per card (it names the commit it tested; run it on this pull request's head): ```text Evidence: TuxBox check of b698ea5d8e36, no uncommitted changes, no GPU checks: 12 passed, 0 failed, 1 not run ``` (The one not run is the synthetic shaders through the runtime's DXC flags; no Linux DXC was on `PATH`.) Commands run and their results: ```text cargo +1.95.0 xwin test --locked --no-run --target x86_64-pc-windows-msvc -p tuxbox-xmem WINEPREFIX=<scratch> WINEDEBUG=-all WINEDLLOVERRIDES="mscoree,mshtml=d" \ <GE-Proton11-14>/files/bin/wine <test exe> [filter] [--test-threads=1] ``` The new tests in `src/tests/private_views.rs` reserve a free 64 MiB range, release it, and place both views at chosen offsets from a 4 MiB boundary inside it (retrying if another test thread takes the range first). Each test maps, writes a value into every page, maps again, and then checks that the canonical view is read-write with the values intact and the other view is `PAGE_NOACCESS`. After unmapping both, it checks that no private span is left in either range. | case | main (c1139ab2) | this PR | |---|---|---| | 128 pages chunk-aligned, GPU aperture then CPU view | second `map` NULL | pass | | 128 pages chunk-aligned, CPU view then GPU aperture | second `map` NULL | pass | | 2 pages at chunk offset 63, both orders | second `map` NULL | pass | | 4-page second GPU alias at chunk offset 62 | second `map` NULL | pass | | 4-page GPU range at chunk offset 62, then a CPU view | second `map` NULL | pass | Whole xmem test binary with this PR, serial and default threads: 53 passed, 2 ignored. Loaded runs: 600 runs per binary, four at a time, each in its own directory, with #659's test change applied to both binaries so its known failure doesn't hide others. The binary ran from a 95-character path, where ranges land across 4 MiB boundaries more often here: | | main | this PR | |---|---|---| | runs failed | 15 of 600 | 7 of 600 | | `gpu_pool_then_plain_cpu_view...`: second GPU alias `map` returns NULL (`physical_backing.rs:372`) | 2 | 0 | | `gpu_pool_then_plain_cpu_view...`: single-run acquire returns 0 with the CPU view across a boundary (`:383`) | 5 | 4 | | `mapping_state_seqlock_tracks_active_and_completed_mutations` (`:778` to `:806`) | 8 | 1 | | `watched_pages_of_one_allocation_are_scanned_as_one_run_across_small_gaps` (`physical_tracking.rs:685`) | 0 | 2 | Every failed run has one of these. The other tests that failed in those runs did so only because of it: a poisoned `POOL` lock, or spans and apertures the failed test left behind. What's left isn't fixed here: - `:383` is by design: when the OS puts the 4-page CPU view across a boundary, `TuxBoxAcquirePhysicalBacking` (one run) answers `ERROR_NOT_SUPPORTED`, and d3d12 uses the runs variant for that. The test could place that view inside one chunk. - `physical_tracking.rs:685` is the same kind of test assumption as #659's: the test maps 40 pages at an OS-chosen address and expects the runs `[(0, 8), (30, 1)]`. When the range lands across a 4 MiB boundary it gets two spans, and the runs came out as `[(0, 3), (7, 1), (30, 1)]`. That test maps once with no second view, so `protect_runs` only gets empty lists there and behaves the same before and after this change. It shows up here because the binary's address layout differs. - The seqlock test reads the process-wide mutation counter while other tests may be mapping. I haven't traced it. From a short path, main failed 1 of 600 (the seqlock test) and this PR 4 of 600: the seqlock test 3 times, and once `a_released_graphics_reservation_loses_its_id_and_a_smaller_one_does_not_take_it` (`physical_backing.rs:1029`), whose released address another test can reserve before its query. The seqlock test runs in section mode and the reservation test maps nothing, so neither reaches the changed code. I haven't traced why the counts differ between the two binaries. Also run: - `cargo fmt --all --check` and `cargo +1.95.0 clippy --locked --target x86_64-pc-windows-msvc -p tuxbox-xmem --all-targets -- -D warnings`: pass. - CI's Miri line for `-p tuxbox-xmem` (local nightly): stops on main's two `VirtualAlloc2` tests, so the Miri job on this PR stays red until #658 lands; with #658 applied, `0 failed; 55 ignored`. The new tests carry the `GlobalMemoryStatusEx` ignore like their neighbours. ## Checks CI runs the [code checks](../CONTRIBUTING.md#code-checks): the Python and Rust unit tests, the translator tests through DXC, a Windows typecheck, clippy, formatting, rustdoc, and Miri on the providers. It has no GPU. If this pull request changes evidence JSON, regenerate the results table and the progress map and include both files: ```sh python tuxbox.py status > docs/RESULTS.md python tuxbox.py status --svg docs/progress.svg ``` This pull request changes no evidence JSON.
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!662
No description provided.