GPU, HLE: marked pages go back beside the guards (VICTORY_COPY_BACK_BESIDE, opt-in) #3069

Open
github-bot wants to merge 1 commit from github-bot/gh-3069 into main AGit
Owner

@⁠moe-miau on GitHub · 2026-11-26 06:17 UTC · original
moe-miau:perf/land → main · +212 −15 · 5 files · 1 commits · open

What

After a submission, every 64 KiB page shaders marked as written goes back into title memory in Shadow::copy_back_marked. Each page opens its guards (watch::writing), gets copied in by the kernel (host::write_mapped), then is guarded again (Tracked::taken). On the stack below that is about 2,250 pages a frame, 45 ms of a 60 ms copy-back: two protection changes per page, first-write faults on the newly writable page, and a kernel copy.

With VICTORY_COPY_BACK_BESIDE=1, a page that a view of physical pages shows is written through a view of our own of the whole section instead (xmem::write_beside, which victory_gpu::use_page_writer registers). No guard covers that view. The title's views stay guarded and show the new bytes, since they are the same pages. No protection calls, no first-write faults, no kernel write.

  • The write runs under the mapping lock (PHYSICAL), so no view there is mapped, unmapped or freed while it happens. The lock order PHYSICAL → GUARDS is the one map/unmap already use, when they call title_memory_remapped/title_memory_aliased.
  • As around a kernel write, the page and its other mappings (alias_of) are marked changed in every tracker holding them. The first mark comes before the write: armed trackers save their bytes. The second comes after it, for a tracker that took the page in between. changed() now shares that marking through mark_with_aliases. The copy's own tracker is unarmed first and taken afterwards, as before.
  • Memory no physical-page view shows (private memory, holes, a range past a view) and a failed whole-section mapping go back the old way. The whole view is mapped on first use and unmapped when the section is dropped.

The switch is off by default: nothing changes unless it is set.

The idea comes from avatarmanz/VIctory-7900XTX's landing beside the guards (66afb2b, 0d5e4c8, 2c982ee; GPL-3.0-or-later). This is a new, smaller implementation on main, not their code. It has no dword merge or worker pool. It works alongside #2914, which spreads the same loop over threads.

Measured

RTX 4070 SUPER, Windows 11. Local stack: main + #2464 + #2098 + #2626 + #2628 + #2711 + #2851 + #2705 + #2925 + #2958, with VICTORY_CARD_COPIES=1 and VICTORY_WAVE32_APPROX=1. Prologue cell, medians of the 5 s reports from 540 to 900 s of a 15-minute run (about 62 reports each):

run ms/frame copying back marked pages: ms protect calls/frame
stack (baseline) 291.6 60.6 45.4 (2,222 pages) 5,366
stack + #2914 299.8 62.6 45.2 (2,272) 5,528
stack + this, switch on 272.5 47.5 31.2 (2,232) 1,556
  • Copy-back is 13 ms lower a frame and the frame is about 7% shorter. In the same scene #2914's threads did not shorten the loop here, which suggests the per-page work is bound by the guard lock and protection calls.
  • Correctness check: a local diagnostic build (not in this PR) read every page back through the title's own address right after each write beside. 860,000 writes over 15 minutes, 0 differences.
  • Pictures: in this scene the stack has an intermittent artifact (stretched triangles), switch on or off. It shows at 720 and 780 s in the baseline run, at 900 s with #2914, and at 900 s in a second run with this change. The first run with this change showed it at more of the sampled times, the second at one. I see no difference in kind.

Tests

  • watch::tests::a_write_beside_keeps_the_guards_and_marks_the_trackers: three views of one section, two of them title mappings with trackers (one armed). A write beside leaves both mappings PAGE_READONLY, shows in both and marks both trackers. The armed one keeps the bytes from before. A writer that cannot write marks nothing.
  • xmem::tests::writes_beside_land_through_guarded_views: a write lands through a read-only title view and shows at the page's other mapping. A range past a view, or private memory, is not written.
  • cargo test -p victory-gpu --lib --locked: 230 passed. The 2 failures are the known heap-size pair on NVIDIA (dynamic_slots_are_held_until_released, lists_share_a_table_and_copies_keep_their_views), which fail on main too. cargo test -p victory-hle --lib --locked: 153 passed. cargo clippy --workspace --all-targets --locked -- -D warnings and cargo xtask policy are clean.

AI-assisted (Claude Code). Runs done on my machine.

🤖 Generated with Claude Code

**@⁠moe-miau** on GitHub · 2026-11-26 06:17 UTC · [original](https://github.com/Marvisak/VIctory/pull/3069) `moe-miau:perf/land` → `main` · +212 −15 · 5 files · 1 commits · open ## What After a submission, every 64 KiB page shaders marked as written goes back into title memory in `Shadow::copy_back_marked`. Each page opens its guards (`watch::writing`), gets copied in by the kernel (`host::write_mapped`), then is guarded again (`Tracked::taken`). On the stack below that is about 2,250 pages a frame, 45 ms of a 60 ms copy-back: two protection changes per page, first-write faults on the newly writable page, and a kernel copy. With `VICTORY_COPY_BACK_BESIDE=1`, a page that a view of physical pages shows is written through a view of our own of the whole section instead (`xmem::write_beside`, which `victory_gpu::use_page_writer` registers). No guard covers that view. The title's views stay guarded and show the new bytes, since they are the same pages. No protection calls, no first-write faults, no kernel write. - The write runs under the mapping lock (`PHYSICAL`), so no view there is mapped, unmapped or freed while it happens. The lock order PHYSICAL → GUARDS is the one `map`/`unmap` already use, when they call `title_memory_remapped`/`title_memory_aliased`. - As around a kernel write, the page and its other mappings (`alias_of`) are marked changed in every tracker holding them. The first mark comes before the write: armed trackers save their bytes. The second comes after it, for a tracker that took the page in between. `changed()` now shares that marking through `mark_with_aliases`. The copy's own tracker is unarmed first and taken afterwards, as before. - Memory no physical-page view shows (private memory, holes, a range past a view) and a failed whole-section mapping go back the old way. The whole view is mapped on first use and unmapped when the section is dropped. The switch is off by default: nothing changes unless it is set. The idea comes from avatarmanz/VIctory-7900XTX's landing beside the guards (66afb2b, 0d5e4c8, 2c982ee; GPL-3.0-or-later). This is a new, smaller implementation on `main`, not their code. It has no dword merge or worker pool. It works alongside #2914, which spreads the same loop over threads. ## Measured RTX 4070 SUPER, Windows 11. Local stack: main + #2464 + #2098 + #2626 + #2628 + #2711 + #2851 + #2705 + #2925 + #2958, with `VICTORY_CARD_COPIES=1` and `VICTORY_WAVE32_APPROX=1`. Prologue cell, medians of the 5 s reports from 540 to 900 s of a 15-minute run (about 62 reports each): | run | ms/frame | copying back | marked pages: ms | protect calls/frame | |---|---|---|---|---| | stack (baseline) | 291.6 | 60.6 | 45.4 (2,222 pages) | 5,366 | | stack + #2914 | 299.8 | 62.6 | 45.2 (2,272) | 5,528 | | stack + this, switch on | **272.5** | **47.5** | **31.2** (2,232) | **1,556** | - Copy-back is 13 ms lower a frame and the frame is about 7% shorter. In the same scene #2914's threads did not shorten the loop here, which suggests the per-page work is bound by the guard lock and protection calls. - Correctness check: a local diagnostic build (not in this PR) read every page back through the title's own address right after each write beside. 860,000 writes over 15 minutes, 0 differences. - Pictures: in this scene the stack has an intermittent artifact (stretched triangles), switch on or off. It shows at 720 and 780 s in the baseline run, at 900 s with #2914, and at 900 s in a second run with this change. The first run with this change showed it at more of the sampled times, the second at one. I see no difference in kind. ## Tests - `watch::tests::a_write_beside_keeps_the_guards_and_marks_the_trackers`: three views of one section, two of them title mappings with trackers (one armed). A write beside leaves both mappings `PAGE_READONLY`, shows in both and marks both trackers. The armed one keeps the bytes from before. A writer that cannot write marks nothing. - `xmem::tests::writes_beside_land_through_guarded_views`: a write lands through a read-only title view and shows at the page's other mapping. A range past a view, or private memory, is not written. - `cargo test -p victory-gpu --lib --locked`: 230 passed. The 2 failures are the known heap-size pair on NVIDIA (`dynamic_slots_are_held_until_released`, `lists_share_a_table_and_copies_keep_their_views`), which fail on `main` too. `cargo test -p victory-hle --lib --locked`: 153 passed. `cargo clippy --workspace --all-targets --locked -- -D warnings` and `cargo xtask policy` are clean. AI-assisted (Claude Code). Runs done on my machine. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
After a submission, each 64 KiB page that shaders marked as written goes back
into title memory: its guards are opened, the kernel copies the page in, and
the page is guarded again. With VICTORY_COPY_BACK_BESIDE=1, a page that a
view of physical pages shows is written through a view of our own of the
whole section instead, which no guard covers: the title's views stay
guarded and show the new bytes (the same pages), with no protection calls,
no first-write faults and no kernel write. The write runs under the mapping
lock, so no view there changes meanwhile. The page and its other mappings
are marked changed in their trackers before (armed ones save their bytes)
and after the write, as around a kernel write. Other memory goes back as
before.

The idea is avatarmanz/VIctory-7900XTX's landing beside the guards (commits
66afb2b, 0d5e4c8, 2c982ee, GPL-3.0-or-later); this is a new, smaller
implementation on main, not their code.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 06:28:02 +00:00
Author
Owner

@⁠Dukestar69 on GitHub · 2026-11-26 06:28 UTC · original · approved these changes

Tested locally merged onto origin/main (63bcca4) on Windows 11 x64 (NVIDIA GeForce RTX 5090):

  • Diff review: Opt-in VICTORY_COPY_BACK_BESIDE writes marked pages back through separate physical-page views without disarming/rearming title write-watch guards, preserving guard protection while still marking trackers.
  • cargo test -p victory-gpu watch::tests -- --test-threads=1: 16 passed, 0 failed (including a_write_beside_keeps_the_guards_and_marks_the_trackers).
  • cargo test -p victory-gpu shadow::tests -- --test-threads=1 (RTX 5090): 9 passed, 0 failed.
  • cargo test -p victory-hle xmem::tests -- --test-threads=1: 13 passed, 0 failed (including writes_beside_land_through_guarded_views).
  • cargo clippy -p victory-gpu -p victory-hle --all-targets -- -D warnings: clean.
  • cargo xtask policy: 191 files ok.

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

**@⁠Dukestar69** on GitHub · 2026-11-26 06:28 UTC · [original](https://github.com/Marvisak/VIctory/pull/3069#pullrequestreview-6069501696) · **approved these changes** Tested locally merged onto `origin/main` (`63bcca4`) on Windows 11 x64 (NVIDIA GeForce RTX 5090): - Diff review: Opt-in `VICTORY_COPY_BACK_BESIDE` writes marked pages back through separate physical-page views without disarming/rearming title write-watch guards, preserving guard protection while still marking trackers. - `cargo test -p victory-gpu watch::tests -- --test-threads=1`: 16 passed, 0 failed (including `a_write_beside_keeps_the_guards_and_marks_the_trackers`). - `cargo test -p victory-gpu shadow::tests -- --test-threads=1` (RTX 5090): 9 passed, 0 failed. - `cargo test -p victory-hle xmem::tests -- --test-threads=1`: 13 passed, 0 failed (including `writes_beside_land_through_guarded_views`). - `cargo clippy -p victory-gpu -p victory-hle --all-targets -- -D warnings`: clean. - `cargo xtask policy`: 191 files ok. Synthetic tests only (no game files used). AI-assisted (Gemini 4 Neon).
Author
Owner

@⁠kotekompotek on GitHub · 2026-11-26 06:35 UTC · original · approved these changes

Verified both halves: Section::whole lazily maps an unguarded full-section view (unmapped on Drop); write_beside resolves at->whole offset under the physical() lock (checked_add bound) so no remap mid-write; write_through marks pages+aliases changed with save before the write and again after (a tracker taking the page mid-way reads it), matching the writing-window semantics without opening any guard; fallback to writing+write_mapped preserved; opt-in env. Both tests use real sections+VirtualProtect and discriminate.

**@⁠kotekompotek** on GitHub · 2026-11-26 06:35 UTC · [original](https://github.com/Marvisak/VIctory/pull/3069#pullrequestreview-6069559374) · **approved these changes** Verified both halves: Section::whole lazily maps an unguarded full-section view (unmapped on Drop); write_beside resolves at->whole offset under the physical() lock (checked_add bound) so no remap mid-write; write_through marks pages+aliases changed with save before the write and again after (a tracker taking the page mid-way reads it), matching the writing-window semantics without opening any guard; fallback to writing+write_mapped preserved; opt-in env. Both tests use real sections+VirtualProtect and discriminate.
Author
Owner

@⁠fuzeofficlal on GitHub · 2026-11-26 08:00 UTC · original · reviewed

Tested locally on Windows 11 x64 (rustc 1.98.1, RTX 5090 + WARP):

  • cargo clippy -p victory-gpu -p victory-hle --all-targets --locked -- -D warnings (default and --all-features): clean.
  • RTX 5090 (default and VICTORY_COPY_BACK_BESIDE=1): victory-gpu watch::tests (16 passed), shadow::tests (9 passed); victory-hle xmem::tests (13 passed).
  • WARP (--features diagnostics, VICTORY_D3D12_WARP=1, VICTORY_COPY_BACK_BESIDE=1): watch::tests (16 passed), shadow::tests (9 passed).
  • cargo xtask policy: 191 files ok.
  • Verified lock ordering (PHYSICAL -> GUARDS matches xmem::map/xmem::unmap, no GUARDS -> PHYSICAL path) and confirmed with a standalone Windows 11 SEC_RESERVE probe that committing a new page via VirtualAlloc(MEM_COMMIT) on a MapViewOfFile3 title view after Section::whole() was already mapped immediately makes that page MEM_COMMIT | PAGE_READWRITE in the cached whole view without faulting.

One concurrency gap in crates/victory-gpu/src/watch.rs (write_through) should be tightened before merge:

  1. Missing g.busy tracking across the unlocked copy_nonoverlapping window (watch.rs:102-111):
    Unlike writing() (watch.rs:806), write_through releases GUARDS after mark_with_aliases(..., true), copies bytes without (first, last) in g.busy, and then re-acquires GUARDS for mark_with_aliases(..., false). Because the pages remain in g.protected and are not marked busy, a concurrent Tracked::take() / take_split() / taken() on the page or an alias (which the doc comment on write_beside anticipates, and which becomes reachable alongside parallel copy-back like #2914) succeeds during the copy:

    • Shadow::update_tracked can read torn mid-copy bytes via copy_mapped while copy_nonoverlapping is writing the same physical page; and
    • watch.rs:517 calls self.changed.taken(page), which drops saved.bytes[page] on an armed tracker—breaking the invariant guarded by watch::tests::a_busy_alias_leaves_the_page_changed_and_keeps_saved_bytes. When write_through then runs mark_with_aliases(..., false), the page is re-marked changed with saved == None, causing a subsequent Shadow::copy_back_marked to drop GPU writes on that page (MarkedKept).

    Pushing (first, last) into g.busy under the first guards() lock and removing it (via a small drop guard) under the second guards() lock closes both races cleanly with minimal overhead.

Minor non-blocking polish:

  • crates/victory-gpu/src/watch.rs:103: handle bytes.is_empty() early and use at.saturating_add(bytes.len()).div_ceil(PAGE) to match writing() (watch.rs:804) and avoid marking a page on an empty unaligned write or overflowing near usize::MAX.
  • crates/victory-gpu/src/shadow.rs:522: written can be Option<&[u8]> ((&*host) / title.as_slice()) directly, avoiding the raw pointer round-trip and extra unsafe from_raw_parts block.
**@⁠fuzeofficlal** on GitHub · 2026-11-26 08:00 UTC · [original](https://github.com/Marvisak/VIctory/pull/3069#pullrequestreview-6070370384) · **reviewed** Tested locally on Windows 11 x64 (`rustc 1.98.1`, RTX 5090 + WARP): - `cargo clippy -p victory-gpu -p victory-hle --all-targets --locked -- -D warnings` (default and `--all-features`): clean. - RTX 5090 (default and `VICTORY_COPY_BACK_BESIDE=1`): `victory-gpu` `watch::tests` (16 passed), `shadow::tests` (9 passed); `victory-hle` `xmem::tests` (13 passed). - WARP (`--features diagnostics`, `VICTORY_D3D12_WARP=1`, `VICTORY_COPY_BACK_BESIDE=1`): `watch::tests` (16 passed), `shadow::tests` (9 passed). - `cargo xtask policy`: 191 files ok. - Verified lock ordering (`PHYSICAL -> GUARDS` matches `xmem::map`/`xmem::unmap`, no `GUARDS -> PHYSICAL` path) and confirmed with a standalone Windows 11 `SEC_RESERVE` probe that committing a new page via `VirtualAlloc(MEM_COMMIT)` on a `MapViewOfFile3` title view after `Section::whole()` was already mapped immediately makes that page `MEM_COMMIT | PAGE_READWRITE` in the cached `whole` view without faulting. One concurrency gap in `crates/victory-gpu/src/watch.rs` (`write_through`) should be tightened before merge: 1. **Missing `g.busy` tracking across the unlocked `copy_nonoverlapping` window (`watch.rs:102-111`)**: Unlike `writing()` (`watch.rs:806`), `write_through` releases `GUARDS` after `mark_with_aliases(..., true)`, copies `bytes` without `(first, last)` in `g.busy`, and then re-acquires `GUARDS` for `mark_with_aliases(..., false)`. Because the pages remain in `g.protected` and are not marked busy, a concurrent `Tracked::take()` / `take_split()` / `taken()` on the page or an alias (which the doc comment on `write_beside` anticipates, and which becomes reachable alongside parallel copy-back like #2914) succeeds during the copy: - `Shadow::update_tracked` can read torn mid-copy bytes via `copy_mapped` while `copy_nonoverlapping` is writing the same physical page; and - `watch.rs:517` calls `self.changed.taken(page)`, which drops `saved.bytes[page]` on an armed tracker—breaking the invariant guarded by `watch::tests::a_busy_alias_leaves_the_page_changed_and_keeps_saved_bytes`. When `write_through` then runs `mark_with_aliases(..., false)`, the page is re-marked changed with `saved == None`, causing a subsequent `Shadow::copy_back_marked` to drop GPU writes on that page (`MarkedKept`). Pushing `(first, last)` into `g.busy` under the first `guards()` lock and removing it (via a small drop guard) under the second `guards()` lock closes both races cleanly with minimal overhead. Minor non-blocking polish: - `crates/victory-gpu/src/watch.rs:103`: handle `bytes.is_empty()` early and use `at.saturating_add(bytes.len()).div_ceil(PAGE)` to match `writing()` (`watch.rs:804`) and avoid marking a page on an empty unaligned write or overflowing near `usize::MAX`. - `crates/victory-gpu/src/shadow.rs:522`: `written` can be `Option<&[u8]>` (`(&*host)` / `title.as_slice()`) directly, avoiding the raw pointer round-trip and extra `unsafe` `from_raw_parts` block.
Author
Owner

@⁠Marvisak on GitHub · 2026-11-26 08:23 UTC · original

Automated triage for the maintainers.

Verdict: merge candidate. With VICTORY_COPY_BACK_BESIDE=1 (off by default), Shadow::copy_back_marked writes marked pages into title memory through a whole-section view of our own (xmem::write_beside via watch::write_beside/write_through). It no longer opens the guards and does a kernel write. Anything else falls back to the old path.

What I checked against main:

  • The address math: physical.views entries are single runs of one section starting at slice.index, so whole + slice.index * PAGE + (at - base) is right. Ranges past a view are refused.
  • Only committed pages are touched: they lie in a title view, and map→commit committed them.
  • Lock order PHYSICAL → GUARDS is the one map/unmap already use. watch doesn't call back into xmem under the guard lock.
  • The tracker marking before and after the write (mark_with_aliases) stands in for the busy window of writing.
  • The whole view is unmapped in Section::drop.

CI: check passing. The source of the idea is credited (same licence, GPL-3.0-or-later), and the PR states this is a new implementation.


Generated by Claude Code

**@⁠Marvisak** on GitHub · 2026-11-26 08:23 UTC · [original](https://github.com/Marvisak/VIctory/pull/3069#issuecomment-7186533395) <!-- victory-triage v1 head=5454d415e8618f8f66f3bb22c62e6765c0e03da3 verdict=merge-candidate at=2026-11-26T08:18:50Z --> Automated triage for the maintainers. **Verdict: merge candidate.** With `VICTORY_COPY_BACK_BESIDE=1` (off by default), `Shadow::copy_back_marked` writes marked pages into title memory through a whole-section view of our own (`xmem::write_beside` via `watch::write_beside`/`write_through`). It no longer opens the guards and does a kernel write. Anything else falls back to the old path. What I checked against main: - The address math: `physical.views` entries are single runs of one section starting at `slice.index`, so `whole + slice.index * PAGE + (at - base)` is right. Ranges past a view are refused. - Only committed pages are touched: they lie in a title view, and `map`→`commit` committed them. - Lock order PHYSICAL → GUARDS is the one `map`/`unmap` already use. `watch` doesn't call back into xmem under the guard lock. - The tracker marking before and after the write (`mark_with_aliases`) stands in for the `busy` window of `writing`. - The whole view is unmapped in `Section::drop`. CI: `check` passing. The source of the idea is credited (same licence, GPL-3.0-or-later), and the PR states this is a new implementation. --- _Generated by [Claude Code](https://claude.ai/code)_
This pull request can be merged automatically.
You are not authorized to merge this pull request.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin '+refs/pull/3069/head:github-bot/gh-3069'
git switch 'github-bot/gh-3069'

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-3069'
git switch 'github-bot/gh-3069'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-3069'
git switch 'github-bot/gh-3069'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-3069'
git switch 'main'
git merge --squash 'github-bot/gh-3069'
git switch 'main'
git merge --ff-only 'github-bot/gh-3069'
git switch 'main'
git merge 'github-bot/gh-3069'
git push origin 'main'
This discussion has been locked. Commenting is limited to contributors.
No reviewers
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.

Dependencies

No dependencies set

Reference
github/Marvisak--VIctory!3069
No description provided.