fix(xmem): avoid global remapping lock during read-only alias source validation #4143

Open
Andreww2009 wants to merge 1 commit from upstream-pr/4143 into main
Andreww2009 commented 2026-11-30 16:40:52 +00:00 (Migrated from github.com)

Imported from trustno1-coder/VIctory-NVIDIA. Originally posted by Andreww2009 on 2026-11-30T16:40:52Z.


Summary

Remove unnecessary acquisition of the global victory_gpu::title_memory_remapping() (ALL) lock during the read-only missing_source pre-check in map_aliases_by_page (crates/victory-hle/src/modules/xmem.rs).

Problem

map_aliases_by_page acquired the process-wide title_memory_remapping() (ALL) lock before verifying that all source pages are mapped, even though no virtual page mappings are modified until the subsequent per-group remapping_and_views(&next) loop. Under VICTORY_RANGE_LOCK=1, this stalls all concurrent range-locked GPU readers across the entire address space during read-only validation and causes modules::xmem::tests::a_read_by_range_waits_only_for_a_remapping_that_meets_it to fail intermittently under parallel cargo test runs (also noted in #3607 and worked around via subprocess isolation in #3902).

Root Cause

missing_source only inspects physical.slice_at(page).is_some() under PHYSICAL and does not touch virtual address mappings. Acquiring the global ALL remapping 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 false before mutating the group).

Implementation

  • Dropped let _changing = victory_gpu::title_memory_remapping(); from the read-only missing_source pre-check block in crates/victory-hle/src/modules/xmem.rs while keeping Busy::begin() and physical().
  • No changes to the per-group mutation loop, which still acquires remapping_and_views(&next) before calling change().

Testing

  • cargo test --locked -p victory-hle --lib: 423 passed, 0 failed, 5 ignored (including a_read_by_range_waits_only_for_a_remapping_that_meets_it passing in parallel without subprocess isolation).
  • cargo clippy adds zero warnings to victory-hle.
  • Checked staged file against asset policy (1 file touched: crates/victory-hle/src/modules/xmem.rs, +3 / -1 lines).

Performance Before and After

  • Eliminates spurious process-wide GPU reader stalls across all ranges during XMemMapPhysicalPages alias pre-validation when VICTORY_RANGE_LOCK=1 is active.
  • Eliminates the root cause of the parallel xmem range-lock test race without spawning a child process.

Hardware Configuration

  • CPU: Intel Core Ultra 9 275HX (8P+16E, 24 logical)
  • GPU: NVIDIA GeForce RTX 5080 Laptop GPU (16 GB GDDR7, driver 621.14)
  • RAM: 64 GB DDR5-4800
  • OS: Windows 11 x64 (build 26200)

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 under PHYSICAL.

  • Searched open pull requests for this issue and file (crates/victory-hle/src/modules/xmem.rs). Related: #3902 (test subprocess workaround only; does not change map_aliases_by_page), #3607 (observed the same xmem test flake on main).
<!-- greyfinch-sync:trustno1-coder/VIctory-NVIDIA:issue:5569552795 --> Imported from [trustno1-coder/VIctory-NVIDIA](https://github.com/trustno1-coder/VIctory-NVIDIA/pull/4143). Originally posted by [Andreww2009](https://github.com/Andreww2009) on 2026-11-30T16:40:52Z. --- ## Summary Remove unnecessary acquisition of the global `victory_gpu::title_memory_remapping()` (`ALL`) lock during the read-only `missing_source` pre-check in `map_aliases_by_page` (`crates/victory-hle/src/modules/xmem.rs`). ## Problem `map_aliases_by_page` acquired the process-wide `title_memory_remapping()` (`ALL`) lock before verifying that all source pages are mapped, even though no virtual page mappings are modified until the subsequent per-group `remapping_and_views(&next)` loop. Under `VICTORY_RANGE_LOCK=1`, this stalls all concurrent range-locked GPU readers across the entire address space during read-only validation and causes `modules::xmem::tests::a_read_by_range_waits_only_for_a_remapping_that_meets_it` to fail intermittently under parallel `cargo test` runs (also noted in #3607 and worked around via subprocess isolation in #3902). ## Root Cause `missing_source` only inspects `physical.slice_at(page).is_some()` under `PHYSICAL` and does not touch virtual address mappings. Acquiring the global `ALL` remapping 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 false` before mutating the group). ## Implementation - Dropped `let _changing = victory_gpu::title_memory_remapping();` from the read-only `missing_source` pre-check block in `crates/victory-hle/src/modules/xmem.rs` while keeping `Busy::begin()` and `physical()`. - No changes to the per-group mutation loop, which still acquires `remapping_and_views(&next)` before calling `change()`. ## Testing - `cargo test --locked -p victory-hle --lib`: **423 passed, 0 failed, 5 ignored** (including `a_read_by_range_waits_only_for_a_remapping_that_meets_it` passing in parallel without subprocess isolation). - `cargo clippy` adds zero warnings to `victory-hle`. - Checked staged file against asset policy (1 file touched: `crates/victory-hle/src/modules/xmem.rs`, +3 / -1 lines). ## Performance Before and After - Eliminates spurious process-wide GPU reader stalls across all ranges during `XMemMapPhysicalPages` alias pre-validation when `VICTORY_RANGE_LOCK=1` is active. - Eliminates the root cause of the parallel `xmem` range-lock test race without spawning a child process. ## Hardware Configuration - CPU: Intel Core Ultra 9 275HX (8P+16E, 24 logical) - GPU: NVIDIA GeForce RTX 5080 Laptop GPU (16 GB GDDR7, driver 621.14) - RAM: 64 GB DDR5-4800 - OS: Windows 11 x64 (build 26200) ## 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 under `PHYSICAL`. ## Related Issues / Overlap Check - [x] Searched open pull requests for this issue and file (`crates/victory-hle/src/modules/xmem.rs`). Related: #3902 (test subprocess workaround only; does not change `map_aliases_by_page`), #3607 (observed the same `xmem` test flake on `main`).
babuakhil commented 2026-11-30 19:45:45 +00:00 (Migrated from github.com)

Imported from trustno1-coder/VIctory-NVIDIA. Originally posted by babuakhil on 2026-11-30T19:45:45Z.


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:

result
main 5 out of 5 fail: 49 passed, 1 failed, a_read_by_range_waits_only_for_a_remapping_that_meets_it every time
this PR 5 out of 5 pass: 50 passed, 0 failed

The whole victory-hle lib passes with it (469 passed, 0 failed, 13 ignored), and Clippy's one note in xmem.rs (unwrap_or_default in port_on, line 1933) is on main too. That test also failed in my run of #4317, so this would clear it there.

And it doesn't open a race that wasn't there. The pre-check only reads physical, under physical()'s own lock, and every change to it is made holding that lock too (remapping_and_views gives both), so the check sees a whole state either way. The window between check and change is the same as on main, since the ALL guard ended with the pre-check's block, and the loop already re-checks each source under its group's lock (None => return false). Taking physical() without ALL first can't invert a lock order either, since nothing else is held at that point.

<!-- greyfinch-sync:trustno1-coder/VIctory-NVIDIA:comment:7289900969 --> Imported from [trustno1-coder/VIctory-NVIDIA](https://github.com/trustno1-coder/VIctory-NVIDIA/pull/4143#issuecomment-7289900969). Originally posted by [babuakhil](https://github.com/babuakhil) on 2026-11-30T19:45:45Z. --- 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: | | result | |---|---| | `main` | 5 out of 5 fail: 49 passed, 1 failed, `a_read_by_range_waits_only_for_a_remapping_that_meets_it` every time | | this PR | 5 out of 5 pass: 50 passed, 0 failed | The whole `victory-hle` lib passes with it (469 passed, 0 failed, 13 ignored), and Clippy's one note in `xmem.rs` (`unwrap_or_default` in `port_on`, line 1933) is on `main` too. That test also failed in my run of #4317, so this would clear it there. **And it doesn't open a race that wasn't there.** The pre-check only reads `physical`, under `physical()`'s own lock, and every change to it is made holding that lock too (`remapping_and_views` gives both), so the check sees a whole state either way. The window between check and change is the same as on `main`, since the `ALL` guard ended with the pre-check's block, and the loop already re-checks each source under its group's lock (`None => return false`). Taking `physical()` without `ALL` first can't invert a lock order either, since nothing else is held at that point.
upipibi commented 2026-12-01 03:55:36 +00:00 (Migrated from github.com)

Imported from trustno1-coder/VIctory-NVIDIA. Originally posted by upipibi on 2026-12-01T03:55:36Z.


Race review of #4143 (head 668eb360): removing the lock is safe; I found no new window. CPU only, no game files.

Why it holds.

  • On main the pre-check's title_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 under remapping_and_views and returns false before touching the group (None => return false).
  • What the pre-check reads (physical.slice_at) is the Physical books, and those change only under PHYSICAL. Each writer makes its change within one hold of PHYSICAL (alias_held, change, the per-group loops). The range-lock retry in remapping_and_views drops both locks only before it changes anything. So PHYSICAL alone gives the check a consistent snapshot. The remapping lock added nothing to it, apart from keeping in-place readers out.
  • Lock order is unaffected: the check now takes one lock, PHYSICAL, instead of remapping then PHYSICAL.
  • With the lock gone, the check no longer bumps 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_LOCK unset) every remap holds the whole-memory lock anyway, and the loop right after takes it again. The stall this removes matters mostly with VICTORY_RANGE_LOCK=1.

Tests. Ryzen 9 5950X, Windows 11, main 5fabb46e with and without this PR (it merges cleanly), cargo test -p victory-hle --lib --locked -- modules::xmem, 10 runs each:

tree a_read_by_range_waits_only_for_a_remapping_that_meets_it failed
main 10 of 10
this PR 0 of 10

Whole victory-hle lib, one run each: main 468 passed, 1 failed (that test); this PR 469 passed, 0 failed.

Two other xmem tests failed now and then in the loops:

  • explicit_placeholder_reserves_splits_and_replaces: once on main, once on the PR.
  • large_default_allocations_honour_write_watch_switch: once, on the PR. It checks that the freed reservation reads MEM_FREE and got MEM_RESERVE. Another test running in parallel reserved the same addresses in between. It doesn't reach map_aliases_by_page.

Neither touches this change.

Tested with the help of Claude Code (Claude Opus 5.5).

<!-- greyfinch-sync:trustno1-coder/VIctory-NVIDIA:comment:7300202273 --> Imported from [trustno1-coder/VIctory-NVIDIA](https://github.com/trustno1-coder/VIctory-NVIDIA/pull/4143#issuecomment-7300202273). Originally posted by [upipibi](https://github.com/upipibi) on 2026-12-01T03:55:36Z. --- Race review of #4143 (head 668eb360): **removing the lock is safe; I found no new window.** CPU only, no game files. **Why it holds.** - On `main` the pre-check's `title_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 under `remapping_and_views` and returns false before touching the group (`None => return false`). - What the pre-check reads (`physical.slice_at`) is the `Physical` books, and those change only under `PHYSICAL`. Each writer makes its change within one hold of `PHYSICAL` (`alias_held`, `change`, the per-group loops). The range-lock retry in `remapping_and_views` drops both locks only before it changes anything. So `PHYSICAL` alone gives the check a consistent snapshot. The remapping lock added nothing to it, apart from keeping in-place readers out. - Lock order is unaffected: the check now takes one lock, `PHYSICAL`, instead of remapping then `PHYSICAL`. - With the lock gone, the check no longer bumps `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_LOCK` unset) every remap holds the whole-memory lock anyway, and the loop right after takes it again. The stall this removes matters mostly with `VICTORY_RANGE_LOCK=1`. **Tests.** Ryzen 9 5950X, Windows 11, `main` 5fabb46e with and without this PR (it merges cleanly), `cargo test -p victory-hle --lib --locked -- modules::xmem`, 10 runs each: | tree | `a_read_by_range_waits_only_for_a_remapping_that_meets_it` failed | |---|---| | `main` | 10 of 10 | | this PR | 0 of 10 | Whole `victory-hle` lib, one run each: `main` 468 passed, 1 failed (that test); this PR 469 passed, 0 failed. Two other `xmem` tests failed now and then in the loops: - `explicit_placeholder_reserves_splits_and_replaces`: once on `main`, once on the PR. - `large_default_allocations_honour_write_watch_switch`: once, on the PR. It checks that the freed reservation reads `MEM_FREE` and got `MEM_RESERVE`. Another test running in parallel reserved the same addresses in between. It doesn't reach `map_aliases_by_page`. Neither touches this change. *Tested with the help of Claude Code (Claude Opus 5.5).*
This pull request cannot be merged automatically due to conflicts.
Merge manually to resolve the conflicts.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin 'upstream-pr/4143:upstream-pr/4143'
git switch 'upstream-pr/4143'

Merge

Merge the changes and update on Forgejo.

Warning: The "Autodetect manual merge" setting is not enabled for this repository, you will have to mark this pull request as manually merged afterwards.

git switch 'main'
git merge --no-ff 'upstream-pr/4143'
git switch 'upstream-pr/4143'
git rebase 'main'
git switch 'main'
git merge --ff-only 'upstream-pr/4143'
git switch 'upstream-pr/4143'
git rebase 'main'
git switch 'main'
git merge --no-ff 'upstream-pr/4143'
git switch 'main'
git merge --squash 'upstream-pr/4143'
git switch 'main'
git merge --ff-only 'upstream-pr/4143'
git switch 'main'
git merge 'upstream-pr/4143'
git push origin 'main'
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
trustno1-coder/VIctory-NVIDIA!4143
No description provided.