GPU: give back exactly the pages a placement locked, and place nothing under late waits #358

Open
HailLucifer666 wants to merge 2 commits from upstream-pr/358 into main
HailLucifer666 commented 2026-11-24 23:51:19 +00:00 (Migrated from github.com)

Imported from fxceless/victory-fork. Originally posted by HailLucifer666 on 2026-11-24T23:51:19Z.


Problem

Three defects in the VICTORY_INPLACE zero-copy path, all reachable from the title's own addresses and switches.

  • place_part computed the pages a span covers as (start + len.max(1)).next_multiple_of(K64). The add is unchecked, so a span whose end leaves the 64-bit address space wrapped, and the rounding after it panicked rather than declining. A span near the top of the space is not memory the title can hold, and it must not be able to take the loader down.
  • place_for gated the budget on STATS[1] + STATS[3] + len > most(): two counters read and compared, then the placement made later, so two threads could each see room for their own. It also compared the title's len, while what the host locks is that length rounded out to 64 KiB pages — up to one page more per placement — and recorded the same short figure, so VICTORY_INPLACE_MB was overrun by the rounding alone, every time, not only under a race.
  • The module's own documentation said "Do not combine 2 with late wait", because stage 2 assumes nothing reads title memory that a submission not yet run writes through a placed buffer, and VICTORY_LATE_WAIT breaks that. Nothing enforced it, and setting both produced silent stale reads.

Fix

  • aligned_span(start, len) is now the one place a span is rounded, returning None instead of wrapping or panicking, and it is what place_part, place_for and gone all go through.
  • The budget became one counter with one compare-and-exchange (try_reserve_at / release_at, held in RESERVED), reserving the rounded bytes, released exactly once on every failure path and in gone. release_at subtracts with checked_sub in every build: a wrapping subtraction would take the counter to about 2^64, after which every reservation is refused and the feature is silently off for the rest of the run.
  • Nothing is placed while VICTORY_LATE_WAIT or the relay is on: buffers and windows are copied instead, counted in STATS[8], and warned about once.

The window exemption was withdrawn during review, and that is the part worth reading. The first version exempted windows outside buffers, justified by "it is a copy, so the flights know its writes". That is false about this code: a placed window is a Shadow with place set, and Shadow::gpu_writes, Shadow::writing and Shadow::copy_back_with all return early on that, so nothing declares a placed window's writes to the flights either. The exemption kept the exact hazard it was meant to refuse. Whether a shader ever writes one is not something I can settle here, so both stages are refused together rather than keeping an unproven exemption.

Verification

  • cargo test -p victory-gpu inplace:: --locked -- --test-threads=1 — 10 passed
  • cargo clippy --workspace --all-targets --locked -- -D warnings — clean
  • cargo clippy --workspace --all-targets --all-features --locked -- -D warnings — clean
  • cargo test -p xtask --locked — 4/4 passed
  • Mutation probes, all run 2026-11-25 and restored, each verified to have applied: unchecked aligned_span panics "attempt to add with overflow"; a two-step check-then-claim admits 8 of 8 racing threads instead of 4; refuses_in_place returning false fails the rule assertion; release_at back to a wrapping fetch_sub leaves the counter at 18446744073709486080 where 0 was expected.

Known residual, and a pre-existing flake

One gap I could not close. The release-on-failure branches have no test of their own. I wrote one (place_for_with against uncommitted memory, asserting RESERVED unchanged) and removed it after measuring that it was the cause of a different failure: it initialises the process-global GPU device, and with it in the suite warp_a_title_buffer_in_place_shows_its_bytes_at_its_offset failed to decommit a page about one run in three (6 of 6 clean with it isolated). A test that makes CI flaky is worse than the gap it closes. What remains tested is release_at's pairing and its underflow guard, plus place_for and gone deriving their bytes from the same pure aligned_span.

cargo test -p victory-gpu --lib crashes before this change too. Under parallel execution the test binary intermittently dies with STATUS_ACCESS_VIOLATION naming no test. Measured on victory-fork/main itself: 4 failures in 20 runs (20%). This branch: 5 in 30 (17%) — the same rate, so it is not introduced here. My earlier 5-in-5 and 10-in-10 clean base samples were luck. Related: PR #151 is an open fix for the decommit retry in the same test, which suggests the instability around these WARP tests is already known. I did not attempt to fix it; it is out of scope for this change.

Review notes

  • Deliberately not fixed: gone's early return on an unrounded span is dead by construction and kept rather than an expect, which would unwind out of a Drop; the comment names both consequences.
  • A race test cannot be certain. The barrier makes the overlap likely, not guaranteed; the test says so.
  • Merge adjacency: 11 open PRs touch inplace.rs/shadow.rs, most near let_go_idle. This touches place_part/place_for/gone and the one gone call site in shadow.rs; the diff is small and additive.
<!-- greyfinch-sync:fxceless/victory-fork:issue:6593823761 --> Imported from [fxceless/victory-fork](https://github.com/fxceless/victory-fork/pull/358). Originally posted by [HailLucifer666](https://github.com/HailLucifer666) on 2026-11-24T23:51:19Z. --- ### Problem Three defects in the `VICTORY_INPLACE` zero-copy path, all reachable from the title's own addresses and switches. - `place_part` computed the pages a span covers as `(start + len.max(1)).next_multiple_of(K64)`. The add is unchecked, so a span whose end leaves the 64-bit address space wrapped, and the rounding after it panicked rather than declining. A span near the top of the space is not memory the title can hold, and it must not be able to take the loader down. - `place_for` gated the budget on `STATS[1] + STATS[3] + len > most()`: two counters read and compared, then the placement made later, so two threads could each see room for their own. It also compared the title's `len`, while what the host locks is that length rounded out to 64 KiB pages — up to one page more per placement — and recorded the same short figure, so `VICTORY_INPLACE_MB` was overrun by the rounding alone, every time, not only under a race. - The module's own documentation said "Do not combine 2 with late wait", because stage 2 assumes nothing reads title memory that a submission not yet run writes through a placed buffer, and `VICTORY_LATE_WAIT` breaks that. Nothing enforced it, and setting both produced silent stale reads. ### Fix - `aligned_span(start, len)` is now the one place a span is rounded, returning `None` instead of wrapping or panicking, and it is what `place_part`, `place_for` and `gone` all go through. - The budget became one counter with one compare-and-exchange (`try_reserve_at` / `release_at`, held in `RESERVED`), reserving the **rounded** bytes, released exactly once on every failure path and in `gone`. `release_at` subtracts with `checked_sub` in every build: a wrapping subtraction would take the counter to about 2^64, after which every reservation is refused and the feature is silently off for the rest of the run. - Nothing is placed while `VICTORY_LATE_WAIT` or the relay is on: buffers and windows are copied instead, counted in `STATS[8]`, and warned about once. **The window exemption was withdrawn during review, and that is the part worth reading.** The first version exempted windows outside buffers, justified by "it *is* a copy, so the flights know its writes". That is false about this code: a placed window is a `Shadow` with `place` set, and `Shadow::gpu_writes`, `Shadow::writing` and `Shadow::copy_back_with` all return early on that, so nothing declares a placed window's writes to the flights either. The exemption kept the exact hazard it was meant to refuse. Whether a shader ever writes one is not something I can settle here, so both stages are refused together rather than keeping an unproven exemption. ### Verification - `cargo test -p victory-gpu inplace:: --locked -- --test-threads=1` — 10 passed - `cargo clippy --workspace --all-targets --locked -- -D warnings` — clean - `cargo clippy --workspace --all-targets --all-features --locked -- -D warnings` — clean - `cargo test -p xtask --locked` — 4/4 passed - Mutation probes, all run 2026-11-25 and restored, each verified to have applied: unchecked `aligned_span` panics "attempt to add with overflow"; a two-step check-then-claim admits 8 of 8 racing threads instead of 4; `refuses_in_place` returning false fails the rule assertion; `release_at` back to a wrapping `fetch_sub` leaves the counter at 18446744073709486080 where 0 was expected. ### Known residual, and a pre-existing flake **One gap I could not close.** The release-on-failure branches have no test of their own. I wrote one (`place_for_with` against uncommitted memory, asserting `RESERVED` unchanged) and removed it after measuring that it was the cause of a different failure: it initialises the process-global GPU device, and with it in the suite `warp_a_title_buffer_in_place_shows_its_bytes_at_its_offset` failed to decommit a page about one run in three (6 of 6 clean with it isolated). A test that makes CI flaky is worse than the gap it closes. What remains tested is `release_at`'s pairing and its underflow guard, plus `place_for` and `gone` deriving their bytes from the same pure `aligned_span`. **`cargo test -p victory-gpu --lib` crashes before this change too.** Under parallel execution the test binary intermittently dies with `STATUS_ACCESS_VIOLATION` naming no test. Measured on `victory-fork/main` itself: **4 failures in 20 runs (20%)**. This branch: **5 in 30 (17%)** — the same rate, so it is not introduced here. My earlier 5-in-5 and 10-in-10 clean base samples were luck. Related: PR #151 is an open fix for the decommit retry in the same test, which suggests the instability around these WARP tests is already known. I did not attempt to fix it; it is out of scope for this change. ### Review notes - Deliberately not fixed: `gone`'s early return on an unrounded span is dead by construction and kept rather than an `expect`, which would unwind out of a `Drop`; the comment names both consequences. - A race test cannot be certain. The barrier makes the overlap likely, not guaranteed; the test says so. - Merge adjacency: 11 open PRs touch `inplace.rs`/`shadow.rs`, most near `let_go_idle`. This touches `place_part`/`place_for`/`gone` and the one `gone` call site in `shadow.rs`; the diff is small and additive.
HailLucifer666 commented 2026-11-25 14:10:52 +00:00 (Migrated from github.com)

Imported from fxceless/victory-fork. Originally posted by HailLucifer666 on 2026-11-25T13:55:19Z.


Rebased onto current main and each of the three fixes probed separately, because a test that passes is not evidence until breaking the code makes it fail.

fix probe (the code put back the way it was) result
aligned_span overflow wrapping_add + next_multiple_of FAILED — panics on the rounding
try_reserve_at atomicity load, check, then store FAILED — the limit is exceeded under threads
late warns refuse every placement refuses_in_place() && window FAILED — a title buffer places anyway

All three restored byte-identical afterwards (inplace.rs sha256 ffe62947919e60d1). cargo test -p victory-gpu --lib inplace::: 10 passed, 0 failed.

What each one is actually protecting, since they are three different failure shapes:

  • The span. start + len past the top of the address space wrapped to a small number, and the placement then described memory that is not the memory asked for. Now the ask comes to nothing before the host is asked, counted where the cover refusal is counted.
  • The budget. Reading the counter, deciding, and claiming was three steps, so two threads could both see the last room and both take it. compare_exchange_weak makes the decision and the claim one operation. This is the one a test can only catch under threads, which is why that test runs threads.
  • The guard. Late waits (VICTORY_LATE_WAIT, and the relay, which counts as stage 3) know about shader writes only from the ranges copies declare. A placed buffer has no copy to declare them from, so the assumption "nothing reads title memory that a submission not yet run writes" is false there. The branch refuses it for windows only; this refuses it for every placement.

The part this does not cover: the refusal means VICTORY_INPLACE does nothing whenever late waits are on, and it says so once. That is a behaviour change for anyone who had both switches set and was, unknowingly, racing. Their frame cost goes up; their corruption risk goes to zero. I think that is the right trade, but it is a trade, not a free fix.

<!-- greyfinch-sync:fxceless/victory-fork:comment:7164485049 --> Imported from [fxceless/victory-fork](https://github.com/fxceless/victory-fork/pull/358#issuecomment-7164485049). Originally posted by [HailLucifer666](https://github.com/HailLucifer666) on 2026-11-25T13:55:19Z. --- Rebased onto current `main` and each of the three fixes probed separately, because a test that passes is not evidence until breaking the code makes it fail. | fix | probe (the code put back the way it was) | result | |---|---|---| | `aligned_span` overflow | `wrapping_add` + `next_multiple_of` | **FAILED** — panics on the rounding | | `try_reserve_at` atomicity | load, check, then `store` | **FAILED** — the limit is exceeded under threads | | late warns refuse every placement | `refuses_in_place() && window` | **FAILED** — a title buffer places anyway | All three restored byte-identical afterwards (`inplace.rs` sha256 `ffe62947919e60d1`). `cargo test -p victory-gpu --lib inplace::`: 10 passed, 0 failed. What each one is actually protecting, since they are three different failure shapes: - **The span.** `start + len` past the top of the address space wrapped to a small number, and the placement then described memory that is not the memory asked for. Now the ask comes to nothing before the host is asked, counted where the cover refusal is counted. - **The budget.** Reading the counter, deciding, and claiming was three steps, so two threads could both see the last room and both take it. `compare_exchange_weak` makes the decision and the claim one operation. This is the one a test can only catch under threads, which is why that test runs threads. - **The guard.** Late waits (`VICTORY_LATE_WAIT`, and the relay, which counts as stage 3) know about shader writes only from the ranges copies declare. A placed buffer has no copy to declare them from, so the assumption "nothing reads title memory that a submission not yet run writes" is false there. The branch refuses it for windows only; this refuses it for every placement. The part this does **not** cover: the refusal means `VICTORY_INPLACE` does nothing whenever late waits are on, and it says so once. That is a behaviour change for anyone who had both switches set and was, unknowingly, racing. Their frame cost goes up; their corruption risk goes to zero. I think that is the right trade, but it is a trade, not a free fix.
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/358:upstream-pr/358'
git switch 'upstream-pr/358'

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