GPU: parallelize marked page copy-backs across disjoint pages #3341

Open
danielibabet wants to merge 2 commits from upstream-pr/3341 into main
danielibabet commented 2026-11-26 19:16:59 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by danielibabet on 2026-11-26T19:16:59Z.


Summary

  • Parallelize Shadow::copy_back_marks across up to 8 worker threads (std::thread::scope) over disjoint 64 KiB title memory pages (unique sorted by page, with any aliased duplicate page addresses handled sequentially).
  • Reuse one pooled(PAGE) scratch buffer per worker thread instead of allocating/zeroing vec![0u8; n] per page, and batch Tracked::taken(&list) once per Shadow after the parallel pass.
  • Add unit test parallel_marked_pages_go_back_and_retake_guards exercising copy_back_marks across 20 marked pages.

Motivation

In GTA VI open-world frames, copy_back_marks processes ~1,800–1,900 marked 64 KiB pages (~118 MB/frame) sequentially on the submission thread (~38–40 ms/frame), doing per-page 64 KiB heap allocations, comparisons, merges, and VirtualProtect syscalls. Parallelizing disjoint page writebacks and batching Tracked::taken removes that single-thread CPU bottleneck.

Verification

  • cargo test -p victory-gpu --locked -- --test-threads=1 --skip dynamic_slots_are_held_until_released --skip lists_share_a_table_and_copies_keep_their_views: 230 passed; 0 failed; 1 ignored; 2 filtered out (including parallel_marked_pages_go_back_and_retake_guards)
  • cargo clippy --workspace --all-targets --all-features --locked -- -D warnings: 0 warnings
  • cargo run -p xtask --locked -- policy: asset policy: 191 files ok
<!-- greyfinch-sync:Marvisak/VIctory:issue:5515865857 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/3341). Originally posted by [danielibabet](https://github.com/danielibabet) on 2026-11-26T19:16:59Z. --- ### Summary - Parallelize `Shadow::copy_back_marks` across up to 8 worker threads (`std::thread::scope`) over disjoint 64 KiB title memory pages (`unique` sorted by page, with any aliased duplicate page addresses handled sequentially). - Reuse one `pooled(PAGE)` scratch buffer per worker thread instead of allocating/zeroing `vec![0u8; n]` per page, and batch `Tracked::taken(&list)` once per `Shadow` after the parallel pass. - Add unit test `parallel_marked_pages_go_back_and_retake_guards` exercising `copy_back_marks` across 20 marked pages. ### Motivation In GTA VI open-world frames, `copy_back_marks` processes ~1,800–1,900 marked 64 KiB pages (~118 MB/frame) sequentially on the submission thread (~38–40 ms/frame), doing per-page 64 KiB heap allocations, comparisons, merges, and `VirtualProtect` syscalls. Parallelizing disjoint page writebacks and batching `Tracked::taken` removes that single-thread CPU bottleneck. ### Verification - `cargo test -p victory-gpu --locked -- --test-threads=1 --skip dynamic_slots_are_held_until_released --skip lists_share_a_table_and_copies_keep_their_views`: `230 passed; 0 failed; 1 ignored; 2 filtered out` (including `parallel_marked_pages_go_back_and_retake_guards`) - `cargo clippy --workspace --all-targets --all-features --locked -- -D warnings`: 0 warnings - `cargo run -p xtask --locked -- policy`: `asset policy: 191 files ok`
AurumCarlos commented 2026-11-26 19:18:24 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by AurumCarlos on 2026-11-26T19:18:24Z.

Review: COMMENTED


This is the same change as #2914 (kotekompotek, "marked pages go back to title memory on several threads", from issue #2826), which is still open with reviews and a changes-requested. It would be worth linking it, checking whether that review's points apply here too, and agreeing which one goes forward.

One question on the batching: on main, copy_back_marked calls t.taken(&[page]) right after each page's writeback. Here every page's taken waits until the whole parallel pass is done. What keeps a title write that lands on an already-written page during that window from being dropped when the page is then marked equal to title memory? If the guard makes that impossible, a short comment saying so would help, since the window is now the whole pass instead of one page.

<!-- greyfinch-sync:Marvisak/VIctory:review:6079438895 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/3341#pullrequestreview-6079438895). Originally posted by [AurumCarlos](https://github.com/AurumCarlos) on 2026-11-26T19:18:24Z. **Review:** COMMENTED --- This is the same change as #2914 (kotekompotek, "marked pages go back to title memory on several threads", from issue #2826), which is still open with reviews and a changes-requested. It would be worth linking it, checking whether that review's points apply here too, and agreeing which one goes forward. One question on the batching: on `main`, `copy_back_marked` calls `t.taken(&[page])` right after each page's writeback. Here every page's `taken` waits until the whole parallel pass is done. What keeps a title write that lands on an already-written page during that window from being dropped when the page is then marked equal to title memory? If the guard makes that impossible, a short comment saying so would help, since the window is now the whole pass instead of one page.
danielibabet commented 2026-11-26 19:27:56 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by danielibabet on 2026-11-26T19:27:56Z.


Good catch @AurumCarlos (and thanks for pointing out #2914)! Updated #3341 to address both:

  1. Moved t.taken(&[page / PAGE]) back inside copy_back_marked_with right after write_mapped (matching main's per-page window instead of deferring taken across the whole parallel pass).
  2. Added watch::has_aliases so any marked page with physical-memory aliases (alias_of) is routed to the sequential path alongside duplicate virtual pages, preventing concurrent writebacks to different virtual mappings of the same physical page (the issue raised in #2914).

Re-ran victory-gpu tests (230 passed, 0 failed; 2 skipped), strict clippy (--all-targets --all-features -- -D warnings), and xtask policy (191 files ok).

<!-- greyfinch-sync:Marvisak/VIctory:comment:7201618914 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/3341#issuecomment-7201618914). Originally posted by [danielibabet](https://github.com/danielibabet) on 2026-11-26T19:27:56Z. --- Good catch @AurumCarlos (and thanks for pointing out #2914)! Updated #3341 to address both: 1. Moved `t.taken(&[page / PAGE])` back inside `copy_back_marked_with` right after `write_mapped` (matching `main`'s per-page window instead of deferring `taken` across the whole parallel pass). 2. Added `watch::has_aliases` so any marked page with physical-memory aliases (`alias_of`) is routed to the sequential path alongside duplicate virtual pages, preventing concurrent writebacks to different virtual mappings of the same physical page (the issue raised in #2914). Re-ran `victory-gpu` tests (230 passed, 0 failed; 2 skipped), strict clippy (`--all-targets --all-features -- -D warnings`), and `xtask policy` (191 files ok).
cloph-dsp commented 2026-11-26 19:55:31 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by cloph-dsp on 2026-11-26T19:55:31Z.


Independent local verification of PR #3341 (GPU: parallelize marked page copy-backs across disjoint pages), head 586102fb60.

  • Windows, Rust 1.100.0/MSVC, cargo test --locked, CARGO_BUILD_JOBS=2, per-worktree CARGO_TARGET_DIR (D:\Victory-Contrib\target-3341).
  • cargo test --locked -p victory-gpu --lib -- --exact shadow::tests::parallel_marked_pages_go_back_and_retake_guards --test-threads=1 -> 1 passed, 0 failed, 232 filtered, exit 0.
  • git diff --check origin/main HEAD -> clean.
  • Negative control: the test name is absent from the merge base 63bcca43..., so it is genuinely added by this PR.
  • CPU-side synthetic test; the in-game/D3D12 page-copy scenario reported by the reviewer was not independently reproduced.

AI-assisted (Hy4 flash). GPU/driver: N/A for this CPU test. No game data used.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7202106582 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/3341#issuecomment-7202106582). Originally posted by [cloph-dsp](https://github.com/cloph-dsp) on 2026-11-26T19:55:31Z. --- Independent local verification of PR #3341 (`GPU: parallelize marked page copy-backs across disjoint pages`), head 586102fb60dec9413da8bb588425aa04b3dc5782. - Windows, Rust 1.100.0/MSVC, `cargo test --locked`, `CARGO_BUILD_JOBS=2`, per-worktree `CARGO_TARGET_DIR` (`D:\Victory-Contrib\target-3341`). - `cargo test --locked -p victory-gpu --lib -- --exact shadow::tests::parallel_marked_pages_go_back_and_retake_guards --test-threads=1` -> 1 passed, 0 failed, 232 filtered, exit 0. - `git diff --check origin/main HEAD` -> clean. - Negative control: the test name is absent from the merge base 63bcca43..., so it is genuinely added by this PR. - CPU-side synthetic test; the in-game/D3D12 page-copy scenario reported by the reviewer was not independently reproduced. AI-assisted (Hy4 flash). GPU/driver: N/A for this CPU test. No game data used.
fuzeofficlal commented 2026-11-26 20:24:31 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by fuzeofficlal on 2026-11-26T20:24:31Z.

Review: APPROVED


Verified locally merged onto origin/main (63bcca43) at PR head 586102fb (Windows 11 x64, Ryzen 9 9950X, NVIDIA RTX 5090 + Microsoft WARP, VICTORY_D3D12_DEBUG=1):

  • crates/victory-gpu/src/{shadow.rs, watch.rs}:
    • Soundness / aliasing: copy_back_marks (shadow.rs:957-1052) sorts marked entries by 64 KiB-aligned page, routes all physically aliased pages (via watch::has_aliases, watch.rs:298-304) and duplicate virtual-page entries to seq, and only runs seq after std::thread::scope joins. Disjoint pages in unique yield strictly non-overlapping &mut [u8] slices into Shadow::data, host::read_into/write_mapped use stack-local ReadProcessMemory calls (thread-safe), and lock ordering (GUARDS -> changed.0 -> changed.1, POOL independent) is deadlock-free.
    • Keeping per-page t.taken(&[page / PAGE]) immediately after write_mapped inside copy_back_marked_with (shadow.rs:549-551) preserves main's race window for concurrent title writes, and reusing a per-worker pooled(PAGE) scratch buffer sliced to &mut title_buf[..n] (1 <= n <= PAGE) avoids per-page allocations without stale-byte leaks.
  • cargo xtask policy: 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 -- shadow:: watch:: --test-threads=1: 26/26 passed on both RTX 5090 (0.38s) and WARP (0.08s), including parallel_marked_pages_go_back_and_retake_guards.
  • Extra adversarial synthetic probe (not committed):
    • Parallel copy-back across 256 marked 64 KiB pages (16 MiB) with concurrent CPU writes on 128 pages (merge_writeback): completed in 9.61 ms on Ryzen 9 9950X with 100% byte-exact shader + title merge and all 256 pages re-armed (copy.clean()).
    • Unaligned partial first/last pages (start = p + 0x100, len = 20 * PAGE - 0x200) plus a second overlapping Shadow on page 0: verified clean serialization of page 0, exact partial-page bounds, and untouched bytes past the shadow end.
  • Also verified a clean co-merge with #3343 (both touch watch.rs): 0 clippy warnings and 26/26 watch:: + shadow:: tests passing.

Synthetic tests only; looks ready to merge.

<!-- greyfinch-sync:Marvisak/VIctory:review:6080119085 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/3341#pullrequestreview-6080119085). Originally posted by [fuzeofficlal](https://github.com/fuzeofficlal) on 2026-11-26T20:24:31Z. **Review:** APPROVED --- Verified locally merged onto `origin/main` (`63bcca43`) at PR head `586102fb` (Windows 11 x64, Ryzen 9 9950X, NVIDIA RTX 5090 + Microsoft WARP, `VICTORY_D3D12_DEBUG=1`): - `crates/victory-gpu/src/{shadow.rs, watch.rs}`: - Soundness / aliasing: `copy_back_marks` (`shadow.rs:957-1052`) sorts marked entries by 64 KiB-aligned `page`, routes all physically aliased pages (via `watch::has_aliases`, `watch.rs:298-304`) and duplicate virtual-page entries to `seq`, and only runs `seq` after `std::thread::scope` joins. Disjoint pages in `unique` yield strictly non-overlapping `&mut [u8]` slices into `Shadow::data`, `host::read_into`/`write_mapped` use stack-local `ReadProcessMemory` calls (thread-safe), and lock ordering (`GUARDS` -> `changed.0` -> `changed.1`, `POOL` independent) is deadlock-free. - Keeping per-page `t.taken(&[page / PAGE])` immediately after `write_mapped` inside `copy_back_marked_with` (`shadow.rs:549-551`) preserves `main`'s race window for concurrent title writes, and reusing a per-worker `pooled(PAGE)` scratch buffer sliced to `&mut title_buf[..n]` (`1 <= n <= PAGE`) avoids per-page allocations without stale-byte leaks. - `cargo xtask policy`: 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 -- shadow:: watch:: --test-threads=1`: 26/26 passed on both RTX 5090 (0.38s) and WARP (0.08s), including `parallel_marked_pages_go_back_and_retake_guards`. - Extra adversarial synthetic probe (not committed): - Parallel copy-back across 256 marked 64 KiB pages (16 MiB) with concurrent CPU writes on 128 pages (`merge_writeback`): completed in 9.61 ms on Ryzen 9 9950X with 100% byte-exact shader + title merge and all 256 pages re-armed (`copy.clean()`). - Unaligned partial first/last pages (`start = p + 0x100`, `len = 20 * PAGE - 0x200`) plus a second overlapping `Shadow` on page 0: verified clean serialization of page 0, exact partial-page bounds, and untouched bytes past the shadow end. - Also verified a clean co-merge with `#3343` (both touch `watch.rs`): 0 clippy warnings and 26/26 `watch::` + `shadow::` tests passing. Synthetic tests only; looks ready to merge.
neonbyte1 commented 2026-11-27 07:06:04 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by neonbyte1 on 2026-11-27T07:06:04Z.


Measured in game on an RTX 5090 (driver 620.73.04), Linux, GE-Proton11-14 Wine with its vkd3d-proton, GTA VI's opening prison yard, warm cache; base is main plus the unmerged PRs this setup needs (#3312, #3318, #2472 and others, listed in #3425). This PR merged on top, 600 s runs, medians of the last 30 frame-rate lines, against a control run of the same base taken next to it (frame times drift by up to ~25 ms between runs hours apart).

Pair Frame Copy-back
base + #3312 + #3318 + #2848 412 ms 46 ms
same + this PR 418 ms 53 ms
base + #3312 + #3318 372 ms 49 ms
same + this PR 392 ms 54.5 ms

Copy-back got 5-7 ms slower with this PR in both pairs, and in later same-period controls the stack with this PR kept ~5 ms more copy-back than the one without (52-53 vs 47.8 ms). My reading [inference]: copy-back here is dominated by page writes and guard lease rounds (~2,800 lease/protect rounds per frame, ~94% of the thread time), so splitting it over 8 threads mostly adds queueing on watch::GUARDS (111 thread-ms over 8 workers came out only 2.2x parallel in our counters). #3246's pooled buffer measured 3.4 ms better on the same base.

AI-assisted (Claude); measured on my machine, no game data shared.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7212142663 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/3341#issuecomment-7212142663). Originally posted by [neonbyte1](https://github.com/neonbyte1) on 2026-11-27T07:06:04Z. --- Measured in game on an RTX 5090 (driver 620.73.04), Linux, GE-Proton11-14 Wine with its vkd3d-proton, GTA VI's opening prison yard, warm cache; base is `main` plus the unmerged PRs this setup needs (#3312, #3318, #2472 and others, listed in #3425). This PR merged on top, 600 s runs, medians of the last 30 frame-rate lines, against a control run of the same base taken next to it (frame times drift by up to ~25 ms between runs hours apart). | Pair | Frame | Copy-back | |---|---|---| | base + #3312 + #3318 + #2848 | 412 ms | 46 ms | | same + this PR | 418 ms | 53 ms | | base + #3312 + #3318 | 372 ms | 49 ms | | same + this PR | 392 ms | 54.5 ms | Copy-back got 5-7 ms slower with this PR in both pairs, and in later same-period controls the stack with this PR kept ~5 ms more copy-back than the one without (52-53 vs 47.8 ms). My reading [inference]: copy-back here is dominated by page writes and guard lease rounds (~2,800 lease/protect rounds per frame, ~94% of the thread time), so splitting it over 8 threads mostly adds queueing on `watch::GUARDS` (111 thread-ms over 8 workers came out only 2.2x parallel in our counters). #3246's pooled buffer measured 3.4 ms better on the same base. _AI-assisted (Claude); measured on my machine, no game data shared._
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/3341:upstream-pr/3341'
git switch 'upstream-pr/3341'

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