GPU: whole pages a copy's shaders wrote stay in the copy until a touch (VICTORY_OWNER, opt-in) #3677

Open
neonbyte1 wants to merge 1 commit from upstream-pr/3677 into main
neonbyte1 commented 2026-11-28 05:42:41 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by neonbyte1 on 2026-11-28T05:42:41Z.


What's wrong

At the end of a submission, every 64 KiB page shaders marked as written goes
back into title memory (crate::shadow::copy_back_marked): read the page
through the kernel, compare it with the copy's bytes from before the
submission, write back what differs. Most of that work is thrown away. A
census of the pages written back on our integration tree (the switch that
names who first touches each page, VICTORY_OWNER_CENSUS) found that
87 % of them are overwritten by the GPU before anything reads them; of
the rest, the first toucher is mostly our own copy-back of another copy.

The change

VICTORY_OWNER=1 (off by default) leaves a whole page the title did not
write in its copy instead of writing it back:

  • Title memory keeps its older bytes behind PAGE_NOACCESS, and the copy
    keeps the bytes that go back.
  • The first touch gives the page the copy's bytes through the runtime's own
    writable view of the page's section
    (Section::whole, reported to the GPU
    side with set_writable_alias), while every title mapping still has no
    access: no reader ever sees the old bytes.
  • Who counts as a touch, and how each gets the page's bytes first: in-process
    reads and writes (they fault into the existing vectored handler, which
    resolves the page before the access retries; a read fault on a page that is
    readable again — another thread resolved it, or a mapping replaced it —
    continues); our kernel reads (host::read_into/copy_mapped serve the
    bytes from the copy without writing title memory); kernel writes
    (watch::writing); DirectStorage's writes into physical pages
    (xmem::will_write, before the bytes land, then report_written after);
    the runtime mapping or unmapping memory; the copy going (Shadow's drop);
    and a withholding (Shadow::withhold).
  • The count of held pages is raised before a page loses its access and
    lowered only after it is readable again, and a page that cannot be guarded
    again is left readable: a count of zero never covers a page with no access,
    so the handler's lock-free early out and serve either take the lock and
    find the page readable, or serve a page that is still held.
  • A page its shaders mark again before any touch is never written in between:
    the copy already holds the newest bytes (owning/again), the other
    holders are marked changed with the page's bytes kept first, and this
    copy's
    armed held pages keep their bytes at arming (arm_keeping) so its
    own copy-back merges against them.
  • A page that cannot stay in the copy (the title wrote it during the
    submission, it is not guarded, it is shown at another address, or its
    section has no writable view) is written back through that same view, and
    every other holder of the page and of its other mappings is marked changed
    with its bytes kept first: the title's mapping is never opened, so a title
    write during the copy faults, waits for the guard lock, and lands after
    ours. A page kept in the copy reports no bytes written.
  • Withholdings act on the copy that held the page: a held page the
    submission's shaders wrote goes back by the bytes kept at arming (what
    title memory would have held) and the withheld ones are dropped; a held
    page they did not write goes back by its older, valid bytes. Another copy's
    held page is left to that copy's own copy-back or withholding.
  • A page whose mapping was replaced without a report, or whose own view is
    gone, is not written: title memory keeps its bytes and its copy reads them
    again. No new page is kept while a later submission may be writing its
    copy (VICTORY_GPU_OVERLAP), and a released title buffer gives its held
    pages back at shadow::unregister.

With the switch off nothing is kept: the added costs in the default path are
one relaxed atomic load per read_into/changed, and for a read access
violation one relaxed load (the switch) before the handler gives up.

Measured

These numbers are from our integration tree (the fork's main: upstream
plus 320 merged PRs and our measured changes), with this mechanism plus the
lazy-view base it was built on there — not from this branch, which is the
port. Setup: prison yard, Series X profile, 2560x1440, ray tracing on, 420 s
runs, warm cache, median of the last 30 victory_gpu::host: ... ms/frame
lines; RTX 5090 (32-lane waves), Wine 11.22 + vkd3d-proton (GE-Proton 11-14),
VICTORY_GPU_OVERLAP=1 VICTORY_CLAMP_WINDOW=1 VICTORY_EARLY_RELEASE=1 VICTORY_EARLY_BARRIERS=1. Two adjacent on/off pairs (on first) are thin evidence. (Edited 2026-11-29: this said "A-B-A-B pairs".)

  • frame: -9 ms.
  • copy-back: -10 ms per frame.
  • census: 87 % of the pages written back are overwritten by the GPU before
    anything reads them; of the remainder the first toucher is mostly our own
    copy-back of another copy.

What the switch needs to do anything

Pages must be section-backed (the runtime's physical-page views) so a
writable view of the page exists. Title memory from the runtime's
write-watched pools is not: it goes back as it does today.

Known limits

  • A copy armed over a page another copy holds can lose its own shader writes
    when both write that page in one submission (it is marked changed, keeps no
    bytes to merge against, and its copy-back then keeps title memory). Our
    fork's main avoids this with mark_others/arm_keeping for every holder;
    that is not done here, to keep the memory arm_keeping allocates bounded.
  • Withholding through copy_back_marks' held-page loop and the
    address-driven path (memory.rs) is untested; the tested path is
    keep_writes → Shadow::withhold. A copy that did not arm a page it wrote
    leaks the withheld bytes there (its held page is resolved instead of
    dropped).
  • arm_keeping's cost (64 KiB per armed held page of the copy) is
    unmeasured on this branch.
  • Pages already held are still served and resolved from their copy while a
    later submission may be writing it; only new pages are refused under
    overlap.
  • owner() reads the environment on its first call, which can happen inside
    the fault handler (once per process).
  • The device tests skip silently when there is no host GPU.

Tests

crates/victory-gpu/src/watch.rs (CPU), on a section's page, the runtime's
own writable view of it and a copy. The title's reads and writes are real
loads and stores into a PAGE_NOACCESS page, so they prove the handler
resolves the page and the access retries.

The mechanism: a_lazy_page_leaves_title_memory_alone_until_a_touch,
a_title_read_of_a_lazy_page_gets_its_copys_bytes,
a_title_write_of_a_lazy_page_keeps_the_copys_other_bytes,
kernel_reads_are_served_a_lazy_pages_copys_bytes,
a_kernel_write_gives_a_lazy_page_its_copys_bytes_first,
a_lazy_page_written_again_is_never_written_in_between,
put_back_writes_through_our_own_mapping,
a_page_written_again_and_again_goes_back_once,
pages_that_cannot_stay_lazy_go_back,
a_copy_going_gives_its_lazy_pages_back,
the_count_of_held_pages_covers_the_pages_with_no_access,
random_touches_lose_no_title_visible_byte (400 random touches of one page).

crates/victory-gpu/src/shadow.rs (device, gated):
marked_pages_stay_in_the_copy_and_merge_a_title_write,
a_held_page_written_again_marks_the_other_copy,
a_withheld_held_page_gives_back_the_bytes_kept_at_arming.

The review's findings. "Fails with the defect back" means the defect was put
back with the APIs kept and the test run.

Finding Test Defect back
B1 (round 3: count lowered before the page was readable) the_count_of_held_pages_covers_the_pages_with_no_access pins the count at both ends; the order inside lazify/resolve is not observable from one thread not testable deterministically
B2 (round 1: again never reached) shadow::tests::a_held_page_written_again_marks_the_other_copy — a second copy over the page boundary, no title touch between the two copy-backs fails with the served compare back
B3 (round 1: holders not marked) put_back_marks_the_other_holders_of_the_page fails
B4 (round 1: copy's page, not the merge) put_back_writes_the_bytes_it_is_given, and the wiring test's second half fails (both)
B5 (round 1/2: withheld pages) a_withheld_page_gives_back_the_bytes_kept_at_arming (CPU) and shadow::tests::a_withheld_held_page_gives_back_the_bytes_kept_at_arming through keep_writes → Shadow::withhold fails (writes the withheld bytes)
B6 (round 1/2/3: crash on a resolved page) the handler's decision function (a_read_fault_after_the_last_resolve_continues) and a two-thread stress test the decision test fails when the early out gives up; the race itself is not staged deterministically
S1 (round 3: another copy's pages) held_pages takes the tracker; no dedicated test not covered by a test
S2 (round 3: arm_keeping cost) reverted to this copy's own held pages; no test asserts the filter not covered by a test
S2 (round 2: page's own view not writable) lazify_refuses_a_page_whose_own_view_is_not_writable fails
S3 (round 3: put_kept on a replaced mapping) shares resolve's code path; no test stages a replaced mapping not covered by a test
S3 (round 2: a later submission) lazify_refuses_while_a_later_submission_runs — no new page is kept; held ones are not re-checked fails
S4 (round 3: readable bits, switch-off cost) structural n/a

Checks at 9ef6bdb9f, Linux + Wine 11.22 (vkd3d-proton GE-Proton 11-14,
RTX 5090): clippy --workspace --all-targets and --all-features clean;
cargo xtask policy 191 files ok; rustfmt on the changed lines only, no
formatting-only hunks against the base; CPU tests headless — 36/36 watch::
with the switch off and on; gated device tests at ea9b65bfe (the commit
before the counter order, which cannot change device behaviour) — 57 of 60
pass with the switch off and on, the same 3 fail as the base binary
63bcca43c (34 pass there): D3DCompile rejects unbounded resource arrays
×2 and this host caps its view heaps below HEAP_VIEWS.

Not tested

  • No game run with the switch on: the timings above are from our integration
    tree. A frame-time run of this branch and a picture comparison are still to
    do; the arm_keeping cost is unmeasured here too.
  • Not run on Windows; the tree was built and tested on Linux + Wine.
  • The counter order rests on the code, not on a test: the race cannot be
    staged deterministically, and the two-thread test is a stress check.
  • The known limits above list what has no test.

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

<!-- greyfinch-sync:Marvisak/VIctory:issue:5537159729 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/3677). Originally posted by [neonbyte1](https://github.com/neonbyte1) on 2026-11-28T05:42:41Z. --- ## What's wrong At the end of a submission, every 64 KiB page shaders marked as written goes back into title memory (`crate::shadow::copy_back_marked`): read the page through the kernel, compare it with the copy's bytes from before the submission, write back what differs. Most of that work is thrown away. A census of the pages written back on our integration tree (the switch that names who first touches each page, `VICTORY_OWNER_CENSUS`) found that **87 % of them are overwritten by the GPU before anything reads them**; of the rest, the first toucher is mostly our own copy-back of another copy. ## The change `VICTORY_OWNER=1` (off by default) leaves a whole page the title did not write in its copy instead of writing it back: - Title memory keeps its older bytes behind `PAGE_NOACCESS`, and the copy keeps the bytes that go back. - The first touch gives the page the copy's bytes *through the runtime's own writable view of the page's section* (`Section::whole`, reported to the GPU side with `set_writable_alias`), while every title mapping still has no access: no reader ever sees the old bytes. - Who counts as a touch, and how each gets the page's bytes first: in-process reads and writes (they fault into the existing vectored handler, which resolves the page before the access retries; a read fault on a page that is readable again — another thread resolved it, or a mapping replaced it — continues); our kernel reads (`host::read_into`/`copy_mapped` serve the bytes from the copy without writing title memory); kernel writes (`watch::writing`); DirectStorage's writes into physical pages (`xmem::will_write`, *before* the bytes land, then `report_written` after); the runtime mapping or unmapping memory; the copy going (`Shadow`'s drop); and a withholding (`Shadow::withhold`). - The count of held pages is raised before a page loses its access and lowered only after it is readable again, and a page that cannot be guarded again is left readable: a count of zero never covers a page with no access, so the handler's lock-free early out and `serve` either take the lock and find the page readable, or serve a page that is still held. - A page its shaders mark again before any touch is never written in between: the copy already holds the newest bytes (`owning`/`again`), the other holders are marked changed with the page's bytes kept first, and *this copy's* armed held pages keep their bytes at arming (`arm_keeping`) so its own copy-back merges against them. - A page that cannot stay in the copy (the title wrote it during the submission, it is not guarded, it is shown at another address, or its section has no writable view) is written back through that same view, and every other holder of the page and of its other mappings is marked changed with its bytes kept first: the title's mapping is never opened, so a title write during the copy faults, waits for the guard lock, and lands after ours. A page kept in the copy reports no bytes written. - Withholdings act on the copy that held the page: a held page the submission's shaders wrote goes back by the bytes kept at arming (what title memory would have held) and the withheld ones are dropped; a held page they did not write goes back by its older, valid bytes. Another copy's held page is left to that copy's own copy-back or withholding. - A page whose mapping was replaced without a report, or whose own view is gone, is not written: title memory keeps its bytes and its copy reads them again. No *new* page is kept while a later submission may be writing its copy (`VICTORY_GPU_OVERLAP`), and a released title buffer gives its held pages back at `shadow::unregister`. With the switch off nothing is kept: the added costs in the default path are one relaxed atomic load per `read_into`/`changed`, and for a read access violation one relaxed load (the switch) before the handler gives up. ## Measured These numbers are **from our integration tree** (the fork's `main`: upstream plus 320 merged PRs and our measured changes), with this mechanism plus the lazy-view base it was built on there — not from this branch, which is the port. Setup: prison yard, Series X profile, 2560x1440, ray tracing on, 420 s runs, warm cache, median of the last 30 `victory_gpu::host: ... ms/frame` lines; RTX 5090 (32-lane waves), Wine 11.22 + vkd3d-proton (GE-Proton 11-14), `VICTORY_GPU_OVERLAP=1 VICTORY_CLAMP_WINDOW=1 VICTORY_EARLY_RELEASE=1 VICTORY_EARLY_BARRIERS=1`. Two adjacent on/off pairs (on first) are thin evidence. (Edited 2026-11-29: this said "A-B-A-B pairs".) - frame: **-9 ms**. - copy-back: **-10 ms** per frame. - census: 87 % of the pages written back are overwritten by the GPU before anything reads them; of the remainder the first toucher is mostly our own copy-back of another copy. ## What the switch needs to do anything Pages must be section-backed (the runtime's physical-page views) so a writable view of the page exists. Title memory from the runtime's write-watched pools is not: it goes back as it does today. ## Known limits - A copy armed over a page another copy holds can lose its own shader writes when both write that page in one submission (it is marked changed, keeps no bytes to merge against, and its copy-back then keeps title memory). Our fork's `main` avoids this with `mark_others`/`arm_keeping` for every holder; that is not done here, to keep the memory `arm_keeping` allocates bounded. - Withholding through `copy_back_marks`' held-page loop and the address-driven path (`memory.rs`) is untested; the tested path is `keep_writes` → `Shadow::withhold`. A copy that did not arm a page it wrote leaks the withheld bytes there (its held page is resolved instead of dropped). - `arm_keeping`'s cost (64 KiB per armed held page of the copy) is unmeasured on this branch. - Pages already held are still served and resolved from their copy while a later submission may be writing it; only *new* pages are refused under overlap. - `owner()` reads the environment on its first call, which can happen inside the fault handler (once per process). - The device tests skip silently when there is no host GPU. ## Tests `crates/victory-gpu/src/watch.rs` (CPU), on a section's page, the runtime's own writable view of it and a copy. The title's reads and writes are real loads and stores into a `PAGE_NOACCESS` page, so they prove the handler resolves the page and the access retries. The mechanism: `a_lazy_page_leaves_title_memory_alone_until_a_touch`, `a_title_read_of_a_lazy_page_gets_its_copys_bytes`, `a_title_write_of_a_lazy_page_keeps_the_copys_other_bytes`, `kernel_reads_are_served_a_lazy_pages_copys_bytes`, `a_kernel_write_gives_a_lazy_page_its_copys_bytes_first`, `a_lazy_page_written_again_is_never_written_in_between`, `put_back_writes_through_our_own_mapping`, `a_page_written_again_and_again_goes_back_once`, `pages_that_cannot_stay_lazy_go_back`, `a_copy_going_gives_its_lazy_pages_back`, `the_count_of_held_pages_covers_the_pages_with_no_access`, `random_touches_lose_no_title_visible_byte` (400 random touches of one page). `crates/victory-gpu/src/shadow.rs` (device, gated): `marked_pages_stay_in_the_copy_and_merge_a_title_write`, `a_held_page_written_again_marks_the_other_copy`, `a_withheld_held_page_gives_back_the_bytes_kept_at_arming`. The review's findings. "Fails with the defect back" means the defect was put back with the APIs kept and the test run. | Finding | Test | Defect back | |---|---|---| | B1 (round 3: count lowered before the page was readable) | `the_count_of_held_pages_covers_the_pages_with_no_access` pins the count at both ends; the order inside `lazify`/`resolve` is not observable from one thread | not testable deterministically | | B2 (round 1: `again` never reached) | `shadow::tests::a_held_page_written_again_marks_the_other_copy` — a second copy over the page boundary, no title touch between the two copy-backs | **fails** with the served compare back | | B3 (round 1: holders not marked) | `put_back_marks_the_other_holders_of_the_page` | fails | | B4 (round 1: copy's page, not the merge) | `put_back_writes_the_bytes_it_is_given`, and the wiring test's second half | fails (both) | | B5 (round 1/2: withheld pages) | `a_withheld_page_gives_back_the_bytes_kept_at_arming` (CPU) and `shadow::tests::a_withheld_held_page_gives_back_the_bytes_kept_at_arming` through `keep_writes` → `Shadow::withhold` | fails (writes the withheld bytes) | | B6 (round 1/2/3: crash on a resolved page) | the handler's decision function (`a_read_fault_after_the_last_resolve_continues`) and a two-thread stress test | the decision test fails when the early out gives up; the race itself is not staged deterministically | | S1 (round 3: another copy's pages) | `held_pages` takes the tracker; no dedicated test | not covered by a test | | S2 (round 3: `arm_keeping` cost) | reverted to this copy's own held pages; no test asserts the filter | not covered by a test | | S2 (round 2: page's own view not writable) | `lazify_refuses_a_page_whose_own_view_is_not_writable` | fails | | S3 (round 3: `put_kept` on a replaced mapping) | shares `resolve`'s code path; no test stages a replaced mapping | not covered by a test | | S3 (round 2: a later submission) | `lazify_refuses_while_a_later_submission_runs` — no *new* page is kept; held ones are not re-checked | fails | | S4 (round 3: `readable` bits, switch-off cost) | structural | n/a | Checks at `9ef6bdb9f`, Linux + Wine 11.22 (vkd3d-proton GE-Proton 11-14, RTX 5090): clippy `--workspace --all-targets` and `--all-features` clean; `cargo xtask policy` 191 files ok; rustfmt on the changed lines only, no formatting-only hunks against the base; CPU tests headless — 36/36 `watch::` with the switch off and on; gated device tests at `ea9b65bfe` (the commit before the counter order, which cannot change device behaviour) — 57 of 60 pass with the switch off and on, the same 3 fail as the base binary `63bcca43c` (34 pass there): `D3DCompile` rejects unbounded resource arrays ×2 and this host caps its view heaps below `HEAP_VIEWS`. ## Not tested - No game run with the switch on: the timings above are from our integration tree. A frame-time run of this branch and a picture comparison are still to do; the `arm_keeping` cost is unmeasured here too. - Not run on Windows; the tree was built and tested on Linux + Wine. - The counter order rests on the code, not on a test: the race cannot be staged deterministically, and the two-thread test is a stress check. - The known limits above list what has no test. _AI-assisted (Claude); measured on my machine, no game data shared._
neonbyte1 commented 2026-11-29 08:00:37 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by neonbyte1 on 2026-11-29T08:00:37Z.


Where the measurement lives, and two things the body did not say.

  • Fork issue with the evidence table: https://github.com/neonbyte1/VIctory/issues/178 (RESULT: https://github.com/neonbyte1/VIctory/issues/178#issuecomment-7252778852).
  • The numbers in this body are two adjacent on/off pairs of VICTORY_OWNER (on first; the body said "A-B-A-B pairs", edited) on one exe of our integration stack: RTX 5090, Linux/Wine 11.22, vkd3d-proton of GE-Proton11-14, warm pipeline library. Frame -9.0 and -9.1 ms, copy-back -9.3 and -10.0 ms, preparing unchanged.
  • An earlier build of the same mechanism was 5.7 ms slower in the frame (202.1 against 196.4 ms): copy-back fell 9.9 ms and preparing rose 17.9 ms. That is why the later build exists; it belongs next to the gain.
  • Still unmeasured: a game run of this branch (it is the port, not the code that was timed), the arm_keeping cost, and a pair on our current integration tree, where the mechanism is nearly idle (0 pages offered and 955 lazy pages a frame, against about 12,000 lazy pages in the measured runs).

We now hold our own PRs to a rule on the fork: a hardware test with an adjacent control of the PR's own tree before it leaves draft. This one does not meet it yet and stays a draft.

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

<!-- greyfinch-sync:Marvisak/VIctory:comment:7252826746 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/3677#issuecomment-7252826746). Originally posted by [neonbyte1](https://github.com/neonbyte1) on 2026-11-29T08:00:37Z. --- Where the measurement lives, and two things the body did not say. - Fork issue with the evidence table: https://github.com/neonbyte1/VIctory/issues/178 (`RESULT`: https://github.com/neonbyte1/VIctory/issues/178#issuecomment-7252778852). - The numbers in this body are two adjacent on/off pairs of `VICTORY_OWNER` (on first; the body said "A-B-A-B pairs", edited) on one exe of our integration stack: RTX 5090, Linux/Wine 11.22, vkd3d-proton of GE-Proton11-14, warm pipeline library. Frame -9.0 and -9.1 ms, copy-back -9.3 and -10.0 ms, preparing unchanged. - **An earlier build of the same mechanism was 5.7 ms slower in the frame** (202.1 against 196.4 ms): copy-back fell 9.9 ms and preparing rose 17.9 ms. That is why the later build exists; it belongs next to the gain. - **Still unmeasured:** a game run of this branch (it is the port, not the code that was timed), the `arm_keeping` cost, and a pair on our current integration tree, where the mechanism is nearly idle (0 pages offered and 955 lazy pages a frame, against about 12,000 lazy pages in the measured runs). We now hold our own PRs to a rule on the fork: a hardware test with an adjacent control of the PR's own tree before it leaves draft. This one does not meet it yet and stays a draft. _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/3677:upstream-pr/3677'
git switch 'upstream-pr/3677'

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