fix(xmem): avoid global remapping lock during read-only alias source validation #4143
No reviewers
Labels
No labels
AMD
audio
bug
confirmed
crash
cutscene
dlss
documentation
duplicate
enhancement
fixed-on-main
frame-generation
good first issue
help wanted
input
INTEL
invalid
lighting
linux-proton
memory
needs-info
needs-logs
needs-rebase
NVIDIA
performance
pr-diagnostics
pr-docs
pr-feature
pr-fix
pr-performance
pr-refactor
pr-tests
question
RAYTRACING
rendering
report
series-s
shaders
spam
startup
upstream
wontfix
No milestone
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
github/trustno1-coder--VIctory-NVIDIA!4143
Loading…
Reference in a new issue
No description provided.
Delete branch "github-bot/gh-4143"
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?
@Andreww2009 on GitHub · 2026-11-30 16:40 UTC · original
Andreww2009:fix/xmem-read-only-missing-source→main· +3 −1 · 1 files · 1 commits · openSummary
Remove unnecessary acquisition of the global
victory_gpu::title_memory_remapping()(ALL) lock during the read-onlymissing_sourcepre-check inmap_aliases_by_page(crates/victory-hle/src/modules/xmem.rs).Problem
map_aliases_by_pageacquired the process-widetitle_memory_remapping()(ALL) lock before verifying that all source pages are mapped, even though no virtual page mappings are modified until the subsequent per-groupremapping_and_views(&next)loop. UnderVICTORY_RANGE_LOCK=1, this stalls all concurrent range-locked GPU readers across the entire address space during read-only validation and causesmodules::xmem::tests::a_read_by_range_waits_only_for_a_remapping_that_meets_itto fail intermittently under parallelcargo testruns (also noted in #3607 and worked around via subprocess isolation in #3902).Root Cause
missing_sourceonly inspectsphysical.slice_at(page).is_some()underPHYSICALand does not touch virtual address mappings. Acquiring the globalALLremapping lock there unnecessarily blocks unrelated GPU readers. Any concurrent unmap between the pre-check and the mutation loop is already handled safely inside the per-group loop (None => return falsebefore mutating the group).Implementation
let _changing = victory_gpu::title_memory_remapping();from the read-onlymissing_sourcepre-check block incrates/victory-hle/src/modules/xmem.rswhile keepingBusy::begin()andphysical().remapping_and_views(&next)before callingchange().Testing
cargo test --locked -p victory-hle --lib: 423 passed, 0 failed, 5 ignored (includinga_read_by_range_waits_only_for_a_remapping_that_meets_itpassing in parallel without subprocess isolation).cargo clippyadds zero warnings tovictory-hle.crates/victory-hle/src/modules/xmem.rs, +3 / -1 lines).Performance Before and After
XMemMapPhysicalPagesalias pre-validation whenVICTORY_RANGE_LOCK=1is active.xmemrange-lock test race without spawning a child process.Hardware Configuration
Compatibility
Universal across all CPUs, GPUs, and Windows versions.
Risks and Limitations
Minimal. Each group in the subsequent loop still acquires
remapping_and_views(&next)before mutating mappings and re-validates every source page underPHYSICAL.Related Issues / Overlap Check
crates/victory-hle/src/modules/xmem.rs). Related: #3902 (test subprocess workaround only; does not changemap_aliases_by_page), #3607 (observed the samexmemtest flake onmain).@babuakhil on GitHub · 2026-11-30 19:45 UTC · original
Checked on Windows 11 (Rust 1.98.1, pinned), applied on current
main(e1251c31); cherry-picks cleanly. Not tested in the game.It fixes the xmem flake.
cargo test -p victory-hle --lib modules::xmem, five runs each:maina_read_by_range_waits_only_for_a_remapping_that_meets_itevery timeThe whole
victory-hlelib passes with it (469 passed, 0 failed, 13 ignored), and Clippy's one note inxmem.rs(unwrap_or_defaultinport_on, line 1933) is onmaintoo. That test also failed in my run of #4318, so this would clear it there.And it doesn't open a race that wasn't there. The pre-check only reads
physical, underphysical()'s own lock, and every change to it is made holding that lock too (remapping_and_viewsgives both), so the check sees a whole state either way. The window between check and change is the same as onmain, since theALLguard ended with the pre-check's block, and the loop already re-checks each source under its group's lock (None => return false). Takingphysical()withoutALLfirst can't invert a lock order either, since nothing else is held at that point.@upipibi on GitHub · 2026-12-01 03:55 UTC · original
Race review of #4143 (head
668eb360): removing the lock is safe; I found no new window. CPU only, no game files.Why it holds.
mainthe pre-check'stitle_memory_remapping()is dropped at the end of its block, before the per-group loop. So it never kept the pages from changing between the check and the change. The loop already handles that: it looks the source up again underremapping_and_viewsand returns false before touching the group (None => return false).physical.slice_at) is thePhysicalbooks, and those change only underPHYSICAL. Each writer makes its change within one hold ofPHYSICAL(alias_held,change, the per-group loops). The range-lock retry inremapping_and_viewsdrops both locks only before it changes anything. SoPHYSICALalone gives the check a consistent snapshot. The remapping lock added nothing to it, apart from keeping in-place readers out.PHYSICAL, instead of remapping thenPHYSICAL.watch::CHANGES. That counter is read only to retry a read that failed across a change of mapping (again_after_change,texture.rs:2385), and the check changes no mapping. So fewer reads are retried needlessly, nothing more.One caveat on the description: by default (
VICTORY_RANGE_LOCKunset) every remap holds the whole-memory lock anyway, and the loop right after takes it again. The stall this removes matters mostly withVICTORY_RANGE_LOCK=1.Tests. Ryzen 9 5950X, Windows 11,
main5fabb46ewith and without this PR (it merges cleanly),cargo test -p victory-hle --lib --locked -- modules::xmem, 10 runs each:a_read_by_range_waits_only_for_a_remapping_that_meets_itfailedmainWhole
victory-hlelib, one run each:main468 passed, 1 failed (that test); this PR 469 passed, 0 failed.Two other
xmemtests failed now and then in the loops:explicit_placeholder_reserves_splits_and_replaces: once onmain, once on the PR.large_default_allocations_honour_write_watch_switch: once, on the PR. It checks that the freed reservation readsMEM_FREEand gotMEM_RESERVE. Another test running in parallel reserved the same addresses in between. It doesn't reachmap_aliases_by_page.Neither touches this change.
Tested with the help of Claude Code (Claude Opus 5.5).
View command line instructions
Checkout
From your project repository, check out a new branch and test the changes.Merge
Merge the changes and update on Forgejo.