GPU: page mapping copies wait for the submission that is running #2301

Open
denkencar-sketch wants to merge 1 commit from upstream-pr/2303 into main
denkencar-sketch commented 2026-11-25 10:05:46 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by denkencar-sketch on 2026-11-25T10:05:46Z.


Summary

Remapping title pages under a submission's reads and copy-backs could fault it. The host's submission lock now covers the page mapping copies, so they wait for the submission that is running. Lock order is submitted then mapper, and no reverse order was found in map_alias; other xmem locks were not audited. Cost: a remap waits up to a frame; not measured.

Testing

Test page_mappings_wait_for_a_running_submission fails without the lock and passes with this change. cargo test --workspace --locked passes apart from known flakes: modules::dstorage::tests::a_page_list_filled_after_its_request_starts fails now and then on main too, and so does a parallel-run flake in victory-gpu's lib tests (watch:: and descriptor::host:: tests, 2 of 40 runs on pristine main).

Not tried: streaming throughput, or a run in the game.

AI-assisted.

<!-- greyfinch-sync:Marvisak/VIctory:issue:5489194294 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2303). Originally posted by [denkencar-sketch](https://github.com/denkencar-sketch) on 2026-11-25T10:05:46Z. --- ## Summary Remapping title pages under a submission's reads and copy-backs could fault it. The host's submission lock now covers the page mapping copies, so they wait for the submission that is running. Lock order is submitted then mapper, and no reverse order was found in `map_alias`; other `xmem` locks were not audited. Cost: a remap waits up to a frame; not measured. ## Testing Test `page_mappings_wait_for_a_running_submission` fails without the lock and passes with this change. `cargo test --workspace --locked` passes apart from known flakes: `modules::dstorage::tests::a_page_list_filled_after_its_request_starts` fails now and then on `main` too, and so does a parallel-run flake in `victory-gpu`'s lib tests (`watch::` and `descriptor::host::` tests, 2 of 40 runs on pristine `main`). Not tried: streaming throughput, or a run in the game. AI-assisted.
catminou4 commented 2026-11-25 10:19:19 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by catminou4 on 2026-11-25T10:19:19Z.

Review: COMMENTED


Page mapping copies wait for the running submission (hold_submissions lock). Static review only: victory-gpu/victory-hle are Windows-only (D3D12/windows-sys); not compiled or tested on this macOS arm64 host. rustc 1.98.1. Race fix between remap and in-flight reads/writes.

<!-- greyfinch-sync:Marvisak/VIctory:review:6055713891 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2303#pullrequestreview-6055713891). Originally posted by [catminou4](https://github.com/catminou4) on 2026-11-25T10:19:19Z. **Review:** COMMENTED --- Page mapping copies wait for the running submission (hold_submissions lock). Static review only: victory-gpu/victory-hle are Windows-only (D3D12/windows-sys); not compiled or tested on this macOS arm64 host. rustc 1.98.1. Race fix between remap and in-flight reads/writes.
Marvisak commented 2026-11-25 16:09:09 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Marvisak on 2026-11-25T15:21:33Z.


Automated triage for the maintainers.

Merge candidate. copy_page_mappings (crates/victory-gpu/src/queue.rs) now holds Host::submitted (through the new Host::hold_submissions in host.rs) while it remaps pages, so a remap waits for the running submission instead of unmapping title memory under its reads and copy-backs. The held().holds_work(..) lock is taken and released before the new lock, and Host::execute takes submitted first, so the order is the same on both sides. The items left open last time are done: the doc comment is updated, the test has #[cfg(windows)], and the lock order is stated.

On your reply: the lock-order reasoning matches what I read in execute and copy_page_mappings. tumbubu's in-game samples in this thread show the race on main (18 of 74 dumps) and show it turning into waits with this change, with no measurable frame-time cost. CI: passing.


Generated by Claude Code

<!-- greyfinch-sync:Marvisak/VIctory:comment:7166717174 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2303#issuecomment-7166717174). Originally posted by [Marvisak](https://github.com/Marvisak) on 2026-11-25T15:21:33Z. --- <!-- victory-triage v1 head=63f6881cebad990dcc00e881afa62c657b8659fc verdict=merge-candidate at=2026-11-26T00:20:13Z --> Automated triage for the maintainers. **Merge candidate.** `copy_page_mappings` (`crates/victory-gpu/src/queue.rs`) now holds `Host::submitted` (through the new `Host::hold_submissions` in `host.rs`) while it remaps pages, so a remap waits for the running submission instead of unmapping title memory under its reads and copy-backs. The `held().holds_work(..)` lock is taken and released before the new lock, and `Host::execute` takes `submitted` first, so the order is the same on both sides. The items left open last time are done: the doc comment is updated, the test has `#[cfg(windows)]`, and the lock order is stated. On your reply: the lock-order reasoning matches what I read in `execute` and `copy_page_mappings`. tumbubu's in-game samples in this thread show the race on `main` (18 of 74 dumps) and show it turning into waits with this change, with no measurable frame-time cost. CI: passing. --- _Generated by [Claude Code](https://claude.ai/code)_
tumbubu commented 2026-11-25 22:04:36 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by tumbubu on 2026-11-25T21:50:41Z.


Evidence for the race from a run in the game on an RX 7900 XT. Then this PR, cherry-picked onto 13f91ca, measured the same way: remaps and submissions no longer overlap, and frame time shows no cost within run-to-run spread.

This answers the triage's first request: does the title call CopyPageMappingsBatchX while a submission runs on another thread?

  • Setup: AMD Radeon RX 7900 XT, driver 32.0.31036.15, Windows 11 Pro 26300.
  • Builds: diag builds (--profile diag --features diagnostics) of main 13f91ca, and of this PR cherry-picked onto it (clean, 31416d0).
  • Runs: warm shader cache and pipeline library, 180 s each, Enter every 3 s from 8 s to 47 s.
  • Sampling: VICTORY_STACKS, a dump every 0.25 s from 105 s to 175 s, which came to 74 dumps on main and 82 here. I symbolised victory.exe frames and source lines from the build's PDB.

On main: the title remaps under running submissions.

per dump (main, 74) count
submission thread inside Host::execute 70
a title thread inside copy_page_mappings → xmem::map_alias → UnmapViewOfFile2 / MapViewOfFile3 19
both at once 18
  • In those 18 dumps the submission thread was working on title memory: watch::protect_noting and changed, copy_mapped, write_mapped, texture::refresh_all, shadow::copy_back_marks, memory::Shared::rewrite. 12 of the 18 remaps were unmaps.
  • Caveat: VICTORY_STACKS suspends one thread at a time, and a dump of 96 threads takes about 1 s, so two threads in a dump are not captured at the same instant. But with the submission thread inside Host::execute in 70 of 74 dumps, a remap that does not overlap a submission would be the exception.

With this PR: the overlaps become waits.

per dump main (74) this PR (82)
remap and Host::execute in the same dump 18 17
…of which the submission thread is waiting at host.rs:1136 (submitted) 0 13
submission thread blocked on any lock 1 14

The 4 dumps left, where the submission seems to be working during a remap, are what the non-atomic dumps above allow. execute takes submitted at its first line.

Cost.

  • The waits move onto the submission thread, which is the critical path here: 14 of 82 dumps against 1 of 74.
  • In the frame lines it does not show beyond the spread. World lines are frame-rate lines under 10 fps with at least 500 draws:
run fps ms/frame preparing on the GPU copying back draws/frame
main, 3 runs 1.53 / 1.54 / 1.66 918 / 760 / 712 379 / 414 / 409 199 / 202 / 178 93 / 107 / 86 3913 / 3535 / 4370
this PR, 2 runs 1.45 / 1.47 783 / 824 411 / 399 222 / 191 90 / 100 3740 / 3661
  • Two of the runs above were sampled. One main run had VICTORY_SHADER_LOOP_LIMIT=4096, which only changes shaders.
  • Neither run of this PR hung or removed the device. I did not audit the lock order against the other xmem locks.

One more thing the samples show on main. The title's mapping thread is already blocked at queue.rs:636 in 31 of 74 dumps, and in 35 of 82 with this PR. That line is held().holds_work(..) for the mappings count of the held-work frame line. pump holds HELD for each whole pump, and so for the submissions it runs, so this diagnostic already makes most page mappings wait for pumped submissions. That is why the race only shows up for some of them. A try_lock there would stop the title waiting for a counter, but this PR's lock would then have to carry the ordering alone.

Tests. cargo test --locked -p victory-gpu --lib -- --test-threads=1 on 31416d0: 196 passed, 0 failed, 1 ignored, page_mappings_wait_for_a_running_submission included.

AI-assisted (Claude Code); every run was on this machine. Only timings, counts and VIctory's own function names are posted, no game files.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7175610687 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2303#issuecomment-7175610687). Originally posted by [tumbubu](https://github.com/tumbubu) on 2026-11-25T21:50:41Z. --- **Evidence for the race from a run in the game on an RX 7900 XT. Then this PR, cherry-picked onto `13f91ca`, measured the same way: remaps and submissions no longer overlap, and frame time shows no cost within run-to-run spread.** This answers the triage's first request: does the title call `CopyPageMappingsBatchX` while a submission runs on another thread? - **Setup:** AMD Radeon RX 7900 XT, driver 32.0.31036.15, Windows 11 Pro 26300. - **Builds:** diag builds (`--profile diag --features diagnostics`) of `main` `13f91ca`, and of this PR cherry-picked onto it (clean, `31416d0`). - **Runs:** warm shader cache and pipeline library, 180 s each, Enter every 3 s from 8 s to 47 s. - **Sampling:** `VICTORY_STACKS`, a dump every 0.25 s from 105 s to 175 s, which came to 74 dumps on `main` and 82 here. I symbolised `victory.exe` frames and source lines from the build's PDB. **On `main`: the title remaps under running submissions.** | per dump (`main`, 74) | count | |---|---:| | submission thread inside `Host::execute` | 70 | | a title thread inside `copy_page_mappings` → `xmem::map_alias` → `UnmapViewOfFile2` / `MapViewOfFile3` | 19 | | both at once | 18 | - In those 18 dumps the submission thread was working on title memory: `watch::protect_noting` and `changed`, `copy_mapped`, `write_mapped`, `texture::refresh_all`, `shadow::copy_back_marks`, `memory::Shared::rewrite`. 12 of the 18 remaps were unmaps. - **Caveat:** `VICTORY_STACKS` suspends one thread at a time, and a dump of 96 threads takes about 1 s, so two threads in a dump are not captured at the same instant. But with the submission thread inside `Host::execute` in 70 of 74 dumps, a remap that does not overlap a submission would be the exception. **With this PR: the overlaps become waits.** | per dump | `main` (74) | this PR (82) | |---|---:|---:| | remap and `Host::execute` in the same dump | 18 | 17 | | …of which the submission thread is waiting at `host.rs:1136` (`submitted`) | 0 | **13** | | submission thread blocked on any lock | 1 | 14 | The 4 dumps left, where the submission seems to be working during a remap, are what the non-atomic dumps above allow. `execute` takes `submitted` at its first line. **Cost.** - The waits move onto the submission thread, which is the critical path here: 14 of 82 dumps against 1 of 74. - In the frame lines it does not show beyond the spread. World lines are frame-rate lines under 10 fps with at least 500 draws: | run | fps | ms/frame | preparing | on the GPU | copying back | draws/frame | |---|---:|---:|---:|---:|---:|---:| | `main`, 3 runs | 1.53 / 1.54 / 1.66 | 918 / 760 / 712 | 379 / 414 / 409 | 199 / 202 / 178 | 93 / 107 / 86 | 3913 / 3535 / 4370 | | this PR, 2 runs | 1.45 / 1.47 | 783 / 824 | 411 / 399 | 222 / 191 | 90 / 100 | 3740 / 3661 | - Two of the runs above were sampled. One `main` run had `VICTORY_SHADER_LOOP_LIMIT=4096`, which only changes shaders. - Neither run of this PR hung or removed the device. I did not audit the lock order against the other `xmem` locks. **One more thing the samples show on `main`.** The title's mapping thread is already blocked at `queue.rs:636` in 31 of 74 dumps, and in 35 of 82 with this PR. That line is `held().holds_work(..)` for the `mappings` count of the held-work frame line. `pump` holds `HELD` for each whole pump, and so for the submissions it runs, so this diagnostic already makes most page mappings wait for pumped submissions. That is why the race only shows up for some of them. A `try_lock` there would stop the title waiting for a counter, but this PR's lock would then have to carry the ordering alone. **Tests.** `cargo test --locked -p victory-gpu --lib -- --test-threads=1` on `31416d0`: 196 passed, 0 failed, 1 ignored, `page_mappings_wait_for_a_running_submission` included. AI-assisted (Claude Code); every run was on this machine. Only timings, counts and VIctory's own function names are posted, no game files.
denkencar-sketch commented 2026-11-26 00:03:32 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by denkencar-sketch on 2026-11-25T23:30:23Z.


Rebased onto current \main\ (\842b0cb) and addressed the remaining triage items:

  • Updated the doc comment on \copy_page_mappings.
  • Added #[cfg(windows)]\ on the new test.
  • Checked lock ordering against \xmem: \copy_page_mappings\ takes \Host::submitted\ → \xmem::PHYSICAL\ (\map_alias) → \watch::GUARDS. \Host::execute\ takes \Host::submitted\ → \watch::GUARDS\ and nothing in \xmem\ ever acquires \Host::submitted, so \Host::submitted\ stays strictly outermost.

\queue::tests\ and \clippy\ pass.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7177654107 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2303#issuecomment-7177654107). Originally posted by [denkencar-sketch](https://github.com/denkencar-sketch) on 2026-11-25T23:30:23Z. --- Rebased onto current \main\ (\842b0cb\) and addressed the remaining triage items: - Updated the doc comment on \copy_page_mappings\. - Added \#[cfg(windows)]\ on the new test. - Checked lock ordering against \xmem\: \copy_page_mappings\ takes \Host::submitted\ → \xmem::PHYSICAL\ (\map_alias\) → \watch::GUARDS\. \Host::execute\ takes \Host::submitted\ → \watch::GUARDS\ and nothing in \xmem\ ever acquires \Host::submitted\, so \Host::submitted\ stays strictly outermost. \queue::tests\ and \clippy\ pass.
fuzeofficlal commented 2026-11-26 19:51:54 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by fuzeofficlal on 2026-11-26T19:51:54Z.

Review: APPROVED


Reviewed and verified 63f6881cebad990dcc00e881afa62c657b8659fc merged onto origin/main (63bcca43c6a00a758510398fa43096ce7e4f7df0) on Windows 11 x64 (RTX 5090 + Microsoft WARP, --features diagnostics with VICTORY_D3D12_DEBUG=1, Rust 1.98.1 MSVC).

Lock-Order & Code Audit

  • In queue::copy_page_mappings (crates/victory-gpu/src/queue.rs:667-678), held().holds_work(...) drops the HELD guard before Host::hold_submissions (crates/victory-gpu/src/host.rs:1157-1159) acquires Host::submitted.
  • Audited all callers of resource::page_mapper() and Host::submitted across victory-gpu and victory-hle:
    • queue::pump: HELD -> Host::submitted -> watch::GUARDS.
    • queue::copy_page_mappings: Host::submitted -> xmem::PHYSICAL (map_alias) -> watch::GUARDS.
    • page_mapper() is only invoked inside copy_page_mappings, never inside Host::execute, and nothing in xmem acquires Host::submitted or HELD. The global lock order (HELD -> Host::submitted -> xmem::PHYSICAL -> watch::GUARDS) is cycle-free.

Local Verification

  • cargo xtask policy — pass (191 files ok).
  • cargo clippy --locked -p victory-gpu --all-targets -- -D warnings (default and --all-features) — 0 warnings.
  • cargo test --locked -p victory-gpu --features diagnostics --lib page_mappings_wait_for_a_running_submission (RTX 5090 and WARP) — passed.
  • Additional multi-threaded stress probe racing 40 Host::execute submissions concurrently against tight-loop copy_page_mappings calls: completed cleanly with zero deadlock or debug-layer errors on both RTX 5090 (40 execs, 35,565 maps) and WARP (40 execs, 23,395 maps).

Looks good to merge.

<!-- greyfinch-sync:Marvisak/VIctory:review:6079792478 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2303#pullrequestreview-6079792478). Originally posted by [fuzeofficlal](https://github.com/fuzeofficlal) on 2026-11-26T19:51:54Z. **Review:** APPROVED --- Reviewed and verified `63f6881cebad990dcc00e881afa62c657b8659fc` merged onto `origin/main` (`63bcca43c6a00a758510398fa43096ce7e4f7df0`) on Windows 11 x64 (RTX 5090 + Microsoft WARP, `--features diagnostics` with `VICTORY_D3D12_DEBUG=1`, Rust 1.98.1 MSVC). ### Lock-Order & Code Audit - In `queue::copy_page_mappings` (`crates/victory-gpu/src/queue.rs:667-678`), `held().holds_work(...)` drops the `HELD` guard before `Host::hold_submissions` (`crates/victory-gpu/src/host.rs:1157-1159`) acquires `Host::submitted`. - Audited all callers of `resource::page_mapper()` and `Host::submitted` across `victory-gpu` and `victory-hle`: - `queue::pump`: `HELD` -> `Host::submitted` -> `watch::GUARDS`. - `queue::copy_page_mappings`: `Host::submitted` -> `xmem::PHYSICAL` (`map_alias`) -> `watch::GUARDS`. - `page_mapper()` is only invoked inside `copy_page_mappings`, never inside `Host::execute`, and nothing in `xmem` acquires `Host::submitted` or `HELD`. The global lock order (`HELD` -> `Host::submitted` -> `xmem::PHYSICAL` -> `watch::GUARDS`) is cycle-free. ### Local Verification - `cargo xtask policy` — pass (`191 files ok`). - `cargo clippy --locked -p victory-gpu --all-targets -- -D warnings` (default and `--all-features`) — 0 warnings. - `cargo test --locked -p victory-gpu --features diagnostics --lib page_mappings_wait_for_a_running_submission` (RTX 5090 and WARP) — passed. - Additional multi-threaded stress probe racing 40 `Host::execute` submissions concurrently against tight-loop `copy_page_mappings` calls: completed cleanly with zero deadlock or debug-layer errors on both RTX 5090 (`40 execs`, `35,565 maps`) and WARP (`40 execs`, `23,395 maps`). Looks good to merge.
Dukestar69 commented 2026-11-26 21:49:31 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Dukestar69 on 2026-11-26T21:49:31Z.

Review: APPROVED


LGTM — verified on RTX 5090 (Windows / D3D12) against origin/main (63bcca4).

  • Waiting for any running submission before page-mapping copies cleanly prevents racing with in-flight GPU work.
  • cargo test -p victory-gpu --lib queue:: -- --test-threads=1: 6 passed, 0 failed (including page_mappings_wait_for_a_running_submission).
  • cargo clippy -p victory-gpu --all-targets -- -D warnings: clean.
  • cargo xtask policy: 191 files ok.

Synthetic tests only (no game files used). AI-assisted (Gemini 4 Neon).

<!-- greyfinch-sync:Marvisak/VIctory:review:6080856151 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2303#pullrequestreview-6080856151). Originally posted by [Dukestar69](https://github.com/Dukestar69) on 2026-11-26T21:49:31Z. **Review:** APPROVED --- LGTM — verified on RTX 5090 (Windows / D3D12) against `origin/main` (`63bcca4`). - Waiting for any running submission before page-mapping copies cleanly prevents racing with in-flight GPU work. - `cargo test -p victory-gpu --lib queue:: -- --test-threads=1`: 6 passed, 0 failed (including `page_mappings_wait_for_a_running_submission`). - `cargo clippy -p victory-gpu --all-targets -- -D warnings`: clean. - `cargo xtask policy`: 191 files ok. Synthetic tests only (no game files used). AI-assisted (Gemini 4 Neon).
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/2303:upstream-pr/2303'
git switch 'upstream-pr/2303'

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/2303'
git switch 'upstream-pr/2303'
git rebase 'main'
git switch 'main'
git merge --ff-only 'upstream-pr/2303'
git switch 'upstream-pr/2303'
git rebase 'main'
git switch 'main'
git merge --no-ff 'upstream-pr/2303'
git switch 'main'
git merge --squash 'upstream-pr/2303'
git switch 'main'
git merge --ff-only 'upstream-pr/2303'
git switch 'main'
git merge 'upstream-pr/2303'
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
Marvisak/VIctory!2301
No description provided.