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

Open
github-bot wants to merge 2 commits from github-bot/gh-358 into main AGit
Owner

@⁠HailLucifer666 on GitHub · 2026-11-24 23:51 UTC · original
HailLucifer666:fix/inplace-budget-overflow-and-late-wait-guard → main · +292 −21 · 2 files · 2 commits · open

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** on GitHub · 2026-11-24 23:51 UTC · [original](https://github.com/fxceless/victory-fork/pull/358) `HailLucifer666:fix/inplace-budget-overflow-and-late-wait-guard` → `main` · +292 −21 · 2 files · 2 commits · open ### 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.
THE BUG.
Three defects in the `VICTORY_INPLACE` zero-copy path, all reachable from
the title's own addresses and switches.

One, `place_part` computed the pages a title span covers as
`(start + len.max(1)).next_multiple_of(K64)`. The add is unchecked: a span
whose end left the 64-bit address space wrapped, and the rounding after it
then 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.

Two, `place_for` gated the budget on
`STATS[1].load() + STATS[3].load() + len > most()`: two counters read and
compared, then the placement made later, so two threads could each see room
for their own and both take it. 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 silently overrun by the rounding alone, every time,
not only under a race.

Three, this module's own documentation says "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: the flights know shader writes only by the ranges copies declare.
Nothing enforced it. Setting both produced silent stale reads, which is the
one failure mode this feature must not have.

WHY NOT HYPOTHETICAL.
`place_for` runs on every title buffer and every window the GPU side copies,
from `shadow.rs` on the frame path, and the layers copy from many threads --
that is why the counters are atomics at all. The budget is the only thing
standing between this feature and locking the PC's memory down, and it was
undercounting by design: a run at the 4096 MiB default could lock 4096 MiB
of pages plus one page per placement. The late-wait combination is a switch
an operator sets by hand to chase a stutter, which is exactly when nobody is
watching for subtle wrong pixels.

THE 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, so the pages reserved and the pages
released cannot drift apart.

The budget became one counter with one compare-and-exchange:
`try_reserve_at` checks and claims in the same operation, `release_at` gives
it back, and `RESERVED` holds it so no reader has to sum two atomics. The
rounded bytes are what is reserved, what `STATS` records, and what `gone`
releases. Every failure path in `place_for_with` releases exactly once: a
refused host, a memory that is not committed whole, and a missing GPU device.

The late-wait rule became code: `Switches::refuses_late_wait` answers it, and
`place_for` refuses a title buffer while allowing a window outside buffers,
which is safe because a window *is* a copy and the flights already account
for its writes. The refusal is counted in a new `STATS[8]` and reported on
the module's ten-second line, and warned about once.

`Switches` is the seam the tests need: `level()` and `host::late()` are each
read once per process from the environment, so a test cannot set them.

SCOPE.
`inplace.rs`, plus the one `gone` call site in `shadow.rs` that now passes
the placement's `start`. Windows outside buffers keep their behaviour in
every combination, including late waits. Untouched: how a heap is opened, the
view-memory refusal, `title_memory_freeing`, and the census. `STATS` grew from
8 to 9 counters for the new refusal; the existing eight keep their meaning.

Verified: cargo test -p victory-gpu inplace:: --locked -- --test-threads=1
10 passed (3 new, all executing); cargo clippy --workspace --all-targets
--locked -- -D warnings clean; the same with --all-features clean; cargo test
-p xtask --locked 4 passed.

Mutation probes, all run 2026-11-25 and restored. (a) `aligned_span` with `+`
and `next_multiple_of` panics "attempt to add with overflow" on the three
end-of-space cases and nothing else. (b) The compare-exchange replaced by a
load and a store admits all 8 racing threads instead of 4 -- but only once the
test's threads wait on a barrier: without it the probe passed 3 runs in a row,
so the test was strengthened before it was trusted. (c) `refuses_late_wait`
losing its `!window` fails with "a window is a copy". (d) The guard deleted
from `place_for_with` leaves the refusal uncounted, which the counter
assertion catches where the `None` return alone would not.

One thing this does not cover, said plainly: a race test cannot be certain.
The barrier makes the overlap likely, not guaranteed, and a schedule that
never overlaps would pass with the bug present.

Co-Authored-By: DeepSeek V4.1 Flash <[email protected]>
Review of the commit before this one found that its safety argument was
wrong, and that no test could see the paths most likely to leak.

THE BUG.
The guard exempted windows outside buffers on the stated reason that "a window
outside buffers is safe either way: it *is* a copy, so the flights know its
writes". That reason 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 when `place.is_some()`, so nothing
ever declares a placed window's writes to the flights either. The exemption
therefore kept the exact hazard the guard exists to refuse, while explaining
it away.

Second, no test touched `RESERVED`. Deleting `release(locked)` from any of the
three failure branches failed no test, and a leak there shrinks
`VICTORY_INPLACE_MB` for the rest of the run with nothing to notice it. That
is the class the previous commit introduced.

WHY NOT HYPOTHETICAL.
`place_part` gives a placed buffer `D3D12_RESOURCE_FLAG_ALLOW_UNORDERED_ACCESS`,
and the root-UAV path in `draw` takes any buffer carrying that flag that
`shadow::find` returns, which can be a placed window. Whether the title binds
memory outside one of its own buffer objects as a UAV is not something this
tree can settle -- and that is the point: the argument for exempting windows
was a claim about the title that nobody had checked, inside a guard whose only
job is to be right without the title's cooperation.

THE FIX.
`Switches::refuses_in_place` answers with `late` alone, so nothing is placed
while `VICTORY_LATE_WAIT` or the relay is on -- buffers and windows alike are
copied. `VICTORY_INPLACE` and late waits are now mutually exclusive rather
than one of them being silently wrong. The refusal is counted in `STATS[8]`
and the warning names the variables and the reason.

`release_at` no longer relies on a `debug_assert!`. It subtracts with
`checked_sub` through `fetch_update` in every build; releasing more than was
reserved leaves the counter alone and warns. The old shape wrapped the counter
to about 2^64, after which `checked_add` refused every reservation and the
feature was off for the rest of the run while still counting refusals.

A span the address space cannot hold is now counted where the cover refusal is
counted, instead of returning silently.

The module documentation now describes the enforcement, names the relay, and
records that the stage-1 exemption was withdrawn and on what evidence.

SCOPE, AND ONE RESIDUAL SAID PLAINLY.
inplace.rs only. The release-on-failure paths have no test of their own. One
was written -- `place_for_with` against an address that is not committed
memory, asserting `RESERVED` unchanged -- and removed after it was measured to
be the cause of a flake: 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; the base branch was 5 of 5 clean). A test that makes CI flaky is
worse than the gap it closes. What remains is the release helper's pairing and
its underflow guard, both tested, plus the fact that `place_for` and `gone`
derive their bytes from the same pure `aligned_span`.

Verified: cargo test -p victory-gpu inplace:: --locked -- --test-threads=1
10 passed; both clippy commands clean with and without --all-features;
cargo test -p xtask 4 passed; cargo test -p victory-gpu --lib 114 passed
across repeated runs.

Mutation runs 2026-11-25, all restored and each verified to have applied:
`refuses_in_place` returning false fails the rule assertion; `release_at` back
to a wrapping `fetch_sub` fails with the counter at 18446744073709486080 where
0 was expected; and the earlier ones still behave as recorded -- unchecked
`aligned_span` panics on overflow, a two-step check-then-claim admits 8 of 8
threads behind a barrier, and deleting the guard leaves the refusal uncounted.

Co-Authored-By: DeepSeek V4.1 Flash <[email protected]>
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 03:41:35 +00:00
Author
Owner

@⁠HailLucifer666 on GitHub · 2026-11-25 13:55 UTC · original

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.

**@⁠HailLucifer666** on GitHub · 2026-11-25 13:55 UTC · [original](https://github.com/fxceless/victory-fork/pull/358#issuecomment-7164485049) 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 can be merged automatically.
This branch is out-of-date with the base branch
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/358/head:github-bot/gh-358'
git switch 'github-bot/gh-358'

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-358'
git switch 'github-bot/gh-358'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-358'
git switch 'github-bot/gh-358'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-358'
git switch 'main'
git merge --squash 'github-bot/gh-358'
git switch 'main'
git merge --ff-only 'github-bot/gh-358'
git switch 'main'
git merge 'github-bot/gh-358'
git push origin 'main'
This discussion has been locked. Commenting is limited to contributors.
No description provided.