GPU: copy-back can mark a page up to date after the title wrote it, so the GPU keeps reading old data #722

Open
opened 2026-11-26 04:13:53 +00:00 by github-bot · 5 comments
Owner

@⁠Dufftwt on GitHub · 2026-11-24 08:26 UTC · original

What's wrong

At the end of Shadow::copy_back (crates/victory-gpu/src/shadow.rs:237-247), every page it synced is marked up to date in the host copy (copied[page] = now). The now comes from one write-watch query (watch::writes(.., reset)) made after all pieces are done.

That query also clears any title write that happened after a page was read and before the query. The kernel can't tell our own write_mapped from a title write, so both are reset together. The page is then marked current, later refreshes skip it, and the GPU keeps reading the old bytes until the title happens to write that page again.

copy_stale does this in the safe order (ask first, then copy). copy_back reads first and asks afterwards.

Example

  • A large buffer with GPU writes all over it, so copy_back takes milliseconds.
  • Right after page 0 is synced, a title thread writes new constants into page 0.
  • The final query resets the write-watch bit and page 0 is marked current.
  • The next refresh copies 0 bytes, and draws read the old constants.

Evidence

The test a_title_write_during_copy_back_reaches_the_copy replays it deterministically:

  1. Over 64 write-watched pages, a thread waits until copy_back has written page 1, so page 0 is fully done.
  2. It then writes 7 into page 0 while copy_back still has 62 pages to go.
  3. After copy_back, a refresh of page 0 should bring the 7 into the host copy.

On main (dd54de2), on GitHub's windows-latest:

assertion `left == right` failed: in the copy
  left: 0
 right: 7

The write reached title memory but never reached the copy. The test's code is in the race_tests module posted in #721.

Suggested fix

After the final query, mark a synced page current only if title memory, read again with read_into, still matches the host copy:

  • A title write before the query shows up as a difference, so the page stays stale and the next refresh copies it.
  • A title write after the query is reported by the next query, as usual.
  • #693 adds a check that title memory matched at the time each page was read, which doesn't cover writes after that read.
  • #721 is about the same function: the whole-piece write-back.
**@⁠Dufftwt** on GitHub · 2026-11-24 08:26 UTC · [original](https://github.com/Marvisak/VIctory/issues/722) ## What's wrong At the end of `Shadow::copy_back` (`crates/victory-gpu/src/shadow.rs:237-247`), every page it synced is marked up to date in the host copy (`copied[page] = now`). The `now` comes from **one** write-watch query (`watch::writes(.., reset)`) made after all pieces are done. That query also clears any title write that happened after a page was read and before the query. The kernel can't tell our own `write_mapped` from a title write, so both are reset together. The page is then marked current, later refreshes skip it, and the GPU keeps reading the old bytes until the title happens to write that page again. `copy_stale` does this in the safe order (ask first, then copy). `copy_back` reads first and asks afterwards. ## Example - A large buffer with GPU writes all over it, so `copy_back` takes milliseconds. - Right after page 0 is synced, a title thread writes new constants into page 0. - The final query resets the write-watch bit and page 0 is marked current. - The next refresh copies 0 bytes, and draws read the old constants. ## Evidence The test `a_title_write_during_copy_back_reaches_the_copy` replays it deterministically: 1. Over 64 write-watched pages, a thread waits until `copy_back` has written page 1, so page 0 is fully done. 2. It then writes 7 into page 0 while `copy_back` still has 62 pages to go. 3. After `copy_back`, a refresh of page 0 should bring the 7 into the host copy. On `main` (dd54de2), on GitHub's `windows-latest`: ``` assertion `left == right` failed: in the copy left: 0 right: 7 ``` The write reached title memory but never reached the copy. The test's code is in the `race_tests` module posted in #721. ## Suggested fix After the final query, mark a synced page current only if title memory, read again with `read_into`, still matches the host copy: - A title write before the query shows up as a difference, so the page stays stale and the next refresh copies it. - A title write after the query is reported by the next query, as usual. ## Related - #693 adds a check that title memory matched at the time each page was read, which doesn't cover writes after that read. - #721 is about the same function: the whole-piece write-back.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 04:13:53 +00:00
Author
Owner

@⁠Yorivel on GitHub · 2026-11-24 09:25 UTC · original

A fix as suggested here, with the test from #721's race_tests module (credited in the code): https://github.com/Yorivel/VIctory/tree/fix/copy-back-current-pages (on 8e79c80).

After the final asking, copy_back reads each synced page again and marks it current only if it still matches the copy. A title write cleared by that asking leaves the page as written at now, after its last copy, so the next refresh copies it. A write after the asking shows at the next one as before. Cost: one 64 KiB read per synced page.

Tested (Windows 11 26300, MSVC, Rust 1.98.1, --test-threads=1):

  • a_title_write_during_copy_back_reaches_the_copy on unchanged main: 3 of 3 runs fail (left: 0, right: 7, "in the copy"); with the fix: 3 of 3 pass.
  • cargo test -p victory-gpu --lib: 51 passed, 1 ignored. Clippy (-D warnings) clean, cargo xtask policy ok.

The #721 test is not included, as this branch does not change the whole-piece write-back; the fix doesn't touch the lines xstraok's #721 branch changes, but I haven't tested the two together.

Written with Claude Code.

**@⁠Yorivel** on GitHub · 2026-11-24 09:25 UTC · [original](https://github.com/Marvisak/VIctory/issues/722#issuecomment-7127390345) A fix as suggested here, with the test from #721's `race_tests` module (credited in the code): https://github.com/Yorivel/VIctory/tree/fix/copy-back-current-pages (on `8e79c80`). After the final asking, `copy_back` reads each synced page again and marks it current only if it still matches the copy. A title write cleared by that asking leaves the page as written at `now`, after its last copy, so the next refresh copies it. A write after the asking shows at the next one as before. Cost: one 64 KiB read per synced page. **Tested** (Windows 11 26300, MSVC, Rust 1.98.1, `--test-threads=1`): - `a_title_write_during_copy_back_reaches_the_copy` on unchanged `main`: 3 of 3 runs fail (`left: 0, right: 7`, "in the copy"); with the fix: 3 of 3 pass. - `cargo test -p victory-gpu --lib`: 51 passed, 1 ignored. Clippy (`-D warnings`) clean, `cargo xtask policy` ok. The #721 test is not included, as this branch does not change the whole-piece write-back; the fix doesn't touch the lines xstraok's #721 branch changes, but I haven't tested the two together. Written with Claude Code.
Author
Owner

@⁠flaxdmin199 on GitHub · 2026-11-24 09:32 UTC · original

Implemented and verified along the suggested fix:

In \crates/victory-gpu/src/shadow.rs, \Shadow::copy_back\ now checks whether title memory still matches the host copy before marking \copied[page - base] = now. If a concurrent title write happened between its piece merge and the write-watch reset, title memory differs, so the page remains stale and the subsequent \update\ refreshes it into the host copy.

Verification

  • \cargo test -p victory-gpu --lib shadow::tests::a_title_write_during_copy_back_reaches_the_copy\ (fails on \main\ before the fix with \ssertion left == right failed: in the copy (left: 0, right: 7), passes with the fix).
  • \cargo test -p victory-gpu --lib shadow::tests::copy_back_marks_synced_pages_current_when_title_is_unchanged\ (control test, passes).
  • \cargo test -p victory-gpu\ (52 passed, 0 failed, 1 ignored benchmark).
  • \cargo test -p victory-gpu --features diagnostics\ (55 passed, 0 failed, 1 ignored).
  • \VICTORY_D3D12_WARP=1 cargo test -p victory-gpu --features diagnostics\ (55 passed, 0 failed, 1 ignored).
  • \cargo clippy -p victory-gpu --all-targets\ (0 warnings).
  • \cargo xtask policy\ (153 files ok).
  • \cargo test --workspace\ (all workspace crates pass).
  • \cargo build --release -p victory\ and \cargo build --profile diag -p victory --features diagnostics\ (clean builds).

Tested on Windows 11 (MSVC, Rust 1.98.1), synthetic tests only, no game data. External PR creation is collaborator-only, so the branch is available on the fork for cherry-picking:

**@⁠flaxdmin199** on GitHub · 2026-11-24 09:32 UTC · [original](https://github.com/Marvisak/VIctory/issues/722#issuecomment-7127546983) Implemented and verified along the suggested fix: In \crates/victory-gpu/src/shadow.rs\, \Shadow::copy_back\ now checks whether title memory still matches the host copy before marking \copied[page - base] = now\. If a concurrent title write happened between its piece merge and the write-watch reset, title memory differs, so the page remains stale and the subsequent \update\ refreshes it into the host copy. ### Verification - \cargo test -p victory-gpu --lib shadow::tests::a_title_write_during_copy_back_reaches_the_copy\ (fails on \main\ before the fix with \ssertion left == right failed: in the copy (left: 0, right: 7)\, passes with the fix). - \cargo test -p victory-gpu --lib shadow::tests::copy_back_marks_synced_pages_current_when_title_is_unchanged\ (control test, passes). - \cargo test -p victory-gpu\ (52 passed, 0 failed, 1 ignored benchmark). - \cargo test -p victory-gpu --features diagnostics\ (55 passed, 0 failed, 1 ignored). - \VICTORY_D3D12_WARP=1 cargo test -p victory-gpu --features diagnostics\ (55 passed, 0 failed, 1 ignored). - \cargo clippy -p victory-gpu --all-targets\ (0 warnings). - \cargo xtask policy\ (153 files ok). - \cargo test --workspace\ (all workspace crates pass). - \cargo build --release -p victory\ and \cargo build --profile diag -p victory --features diagnostics\ (clean builds). Tested on Windows 11 (MSVC, Rust 1.98.1), synthetic tests only, no game data. External PR creation is collaborator-only, so the branch is available on the fork for cherry-picking: - **Branch:** https://github.com/flaxdmin199/VIctory/tree/fix/copy-back-stale-page - **Commit:** https://github.com/flaxdmin199/VIctory/commit/b17490a6e3559f0f9b37c093aee86ff68962ceb3
Author
Owner

@⁠Machad3x on GitHub · 2026-11-24 14:12 UTC · original

Fix, with your test (failed 3/3 before, passes 3/3 after): github.com/Machad3x/VIctory@3800387a5c. The watch is now asked (watch::writes, reset) before the first piece is read, and synced pages are marked current with that asking instead of one taken after the whole copy-back, so a title write made while pieces are copied back is newer than the mark and shows at the next refresh. Our own write-backs come after the asking, so the next refresh copies those bytes once more (equal bytes, only some cost). Fork branch claude/gta6-shader-translation-crashes-705550.

**@⁠Machad3x** on GitHub · 2026-11-24 14:12 UTC · [original](https://github.com/Marvisak/VIctory/issues/722#issuecomment-7134004849) Fix, with your test (failed 3/3 before, passes 3/3 after): https://github.com/Machad3x/VIctory/commit/3800387a5cce9c030c2dbaeefcde104253e7e3e6. The watch is now asked (watch::writes, reset) before the first piece is read, and synced pages are marked current with that asking instead of one taken after the whole copy-back, so a title write made while pieces are copied back is newer than the mark and shows at the next refresh. Our own write-backs come after the asking, so the next refresh copies those bytes once more (equal bytes, only some cost). Fork branch claude/gta6-shader-translation-crashes-705550.
Author
Owner

@⁠Dufftwt on GitHub · 2026-11-24 15:10 UTC · original

Fixed in #1024, together with #721: a synced page is marked current only if title memory, read again after the final write-watch query, still matches the copy.

**@⁠Dufftwt** on GitHub · 2026-11-24 15:10 UTC · [original](https://github.com/Marvisak/VIctory/issues/722#issuecomment-7135570588) Fixed in #1024, together with #721: a synced page is marked current only if title memory, read again after the final write-watch query, still matches the copy.
Author
Owner

@⁠Marvisak on GitHub · 2026-11-25 01:48 UTC · original

Automated triage for the maintainers.

The mechanism the report names is gone from main. There's no write-watch query or copied[page] = now any more: since c75fdba, changes are tracked per 64 KiB page with read-only guards in crates/victory-gpu/src/watch.rs. The defect itself is still there in the new form:

  • Shadow::copy_back_or_not (crates/victory-gpu/src/shadow.rs) collects the pieces it synced and calls Tracked::taken on them once, after all pieces are done. taken re-guards those pages and clears their "changed" mark without looking at title memory again.
  • A piece that was written back went through watch::writing, and dropping that left its pages open (unguarded). A title write to such a page while later pieces are still processed doesn't fault, so nothing records it. taken then marks the page current, and later refreshes skip it, so the host copy keeps the older bytes.
  • copy_back_marked and copy_back_page, the per-page paths (the first added in cd94d35), call taken right after their own write. That leaves a shorter window of the same kind.

The two newest commits (cd94d35, 940d32f) don't close this. They change which pages are compared and stop the marked path's own write from saving the page, but they don't re-check title memory before taken.

The fix is in an open pull request: #1024 (also fixes #721). It re-reads title memory and marks a page current only if it still matches the copy. It was written against the write-watch version (ffe310e) and needs a rebase onto the guard-based taken. The fork branches in the comments (Yorivel, flaxdmin199, Machad3x) target the old code too.


Generated by Claude Code

**@⁠Marvisak** on GitHub · 2026-11-25 01:48 UTC · [original](https://github.com/Marvisak/VIctory/issues/722#issuecomment-7149972977) <!-- victory-issue-triage v1 verdict=confirmed seen=2026-11-24T15:10:07Z at=2026-11-25T01:51:15Z --> Automated triage for the maintainers. The mechanism the report names is gone from `main`. There's no write-watch query or `copied[page] = now` any more: since `c75fdba`, changes are tracked per 64 KiB page with read-only guards in `crates/victory-gpu/src/watch.rs`. The defect itself is still there in the new form: - `Shadow::copy_back_or_not` (`crates/victory-gpu/src/shadow.rs`) collects the pieces it synced and calls `Tracked::taken` on them once, **after all pieces are done**. `taken` re-guards those pages and clears their "changed" mark without looking at title memory again. - A piece that was written back went through `watch::writing`, and dropping that left its pages open (unguarded). A title write to such a page while later pieces are still processed doesn't fault, so nothing records it. `taken` then marks the page current, and later refreshes skip it, so the host copy keeps the older bytes. - `copy_back_marked` and `copy_back_page`, the per-page paths (the first added in `cd94d35`), call `taken` right after their own write. That leaves a shorter window of the same kind. The two newest commits (`cd94d35`, `940d32f`) don't close this. They change which pages are compared and stop the marked path's own write from saving the page, but they don't re-check title memory before `taken`. The fix is in an open pull request: #1024 (also fixes #721). It re-reads title memory and marks a page current only if it still matches the copy. It was written against the write-watch version (`ffe310e`) and needs a rebase onto the guard-based `taken`. The fork branches in the comments (Yorivel, flaxdmin199, Machad3x) target the old code too. --- _Generated by [Claude Code](https://claude.ai/code)_
This discussion has been locked. Commenting is limited to contributors.
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#722
No description provided.