xmem: end protection runs at private-span boundaries #662
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/662"
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 snoozedDev on 2026-12-02T20:35:49Z.
Summary
With
TUXBOX_XMEM_PRIVATE_VIEWS=1,XMemMapPhysicalPagesreturns NULL withERROR_INVALID_PARAMETERwhen 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_runsmerges adjacent pages into oneVirtualProtectcall.VirtualProtectrefuses a range over two allocations, so the map is rolled back and fails. A#[track_caller]print inlast_error(not in this PR) put the failing call inprotect_private_range, reached from themask_new/demote_oldpair at the end ofmap_private_contiguous.The change:
protect_runsbecomes aPoolmethod 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_privatefailure I left untraced in #659: when the OS puts its 4-page second GPU alias across a 4 MiB boundary, itsmapfails (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.
xmem,lanes-selftestor 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 andtuxbox.py check --no-gpu, below.No Lanes scenario or guest suite turns on private views (nothing under
tools/orexamples/setsTUXBOX_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:linetuxbox.py checkprints last, one per card (it names the commit it tested; run it on this pull request's head):(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:
The new tests in
src/tests/private_views.rsreserve 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 isPAGE_NOACCESS. After unmapping both, it checks that no private span is left in either range.c1139ab2)mapNULLmapNULLmapNULLmapNULLmapNULLWhole 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:
gpu_pool_then_plain_cpu_view...: second GPU aliasmapreturns NULL (physical_backing.rs:372)gpu_pool_then_plain_cpu_view...: single-run acquire returns 0 with the CPU view across a boundary (:383)mapping_state_seqlock_tracks_active_and_completed_mutations(:778to:806)watched_pages_of_one_allocation_are_scanned_as_one_run_across_small_gaps(physical_tracking.rs:685)Every failed run has one of these. The other tests that failed in those runs did so only because of it: a poisoned
POOLlock, or spans and apertures the failed test left behind. What's left isn't fixed here::383is by design: when the OS puts the 4-page CPU view across a boundary,TuxBoxAcquirePhysicalBacking(one run) answersERROR_NOT_SUPPORTED, and d3d12 uses the runs variant for that. The test could place that view inside one chunk.physical_tracking.rs:685is 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, soprotect_runsonly gets empty lists there and behaves the same before and after this change. It shows up here because the binary's address layout differs.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 --checkandcargo +1.95.0 clippy --locked --target x86_64-pc-windows-msvc -p tuxbox-xmem --all-targets -- -D warnings: pass.-p tuxbox-xmem(local nightly): stops on main's twoVirtualAlloc2tests, so the Miri job on this PR stays red until #658 lands; with #658 applied,0 failed; 55 ignored. The new tests carry theGlobalMemoryStatusExignore 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:
This pull request changes no evidence JSON.