shadow: lock-free AtomicU64 landing bitmap and word-scanned take_landed #3753

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

@⁠ScymicX on GitHub · 2026-11-30 05:59 UTC · original
ScymicX:perf/landed-atomic-bitmap → main · +136 −1 · 1 files · 2 commits · open

What it changes

Once a shadow buffer receives any GPU->CPU landing (self.landing == true), every subsequent Shadow::take of known pages goes through Shadow::take_landed. Previously this acquired self.landings: Mutex<Vec>, walked the range 4 KiB page by 4 KiB page, and split even unlanded runs into <= 64 KiB (PAGE) pieces.

This PR adds a lock-free landings_atomic: OnceLock<Box<[AtomicU64]>> bitmap (enabled by default, VICTORY_LANDED_FAST=0 restores the legacy Mutex<Vec> path):

  • Shadow::note_landing: lazily allocates the AtomicU64 word slice once and sets all pages in the landing range word-by-word via etch_or(..., Ordering::Release).
  • Shadow::take_landed:
    • Fast-path exits directly to self.copy(from, to) when landings_atomic is uninitialized or when a lock-free word scan over w_start..w_end finds no set bits in the range (zero lock, zero Vec allocation, zero per-page splitting).
    • For mixed ranges, skips word == 0 chunks as single coalesced unlanded runs (still capping
      oted runs at PAGE so TITLE scratch memory stays bounded) and clears only inspected full KERNEL_PAGE bits atomically with etch_and(!clear_mask, Ordering::AcqRel), preserving partial-page bits and concurrent
      ote_landing updates in the same word.
  • Adds unit test shadow::landing_tests::take_landed_fast_path_and_multi_word_runs covering multi-word unlanded coalescing, cross-word landing boundaries, and partial-page bit retention.

How it was tested

  • GPU and profile: NVIDIA GeForce RTX 5070 Ti, Xbox Series X, 1440p, DLSS Balanced, frame generation off ( 2026.11.28.01 baseline with this optimization).
  • Before: On the 174 MiB main title buffer (1@0x...), Take landed processed 17.45 MB/frame through the per-page landing path.
  • After: Take landed on the same 174 MiB buffer dropped to 1.79 MB/frame (-90%) because unlanded regions fast-path straight to self.copy without locking or splitting.
  • cargo test passed for the crates it touches: yes
    • cargo test --locked -p victory-gpu --lib landing_tests: 3 passed, 0 failed (both default and VICTORY_LANDED_FAST=0).
    • cargo test --locked -p victory-gpu --lib: 922 passed, 60 ignored, 1 pre-existing unrelated failure on main (shader_cache::tests::every_switch_of_the_translator_is_in_the_key).
    • cargo clippy -p victory-gpu --no-deps --all-targets --locked and --all-features: 0 warnings in shadow.rs changes.
    • cargo xtask policy --staged: passed.

Checked for overlap

  • I searched the open pull requests for the same issue and for the files this touches. Related: #3268, #3502 (both touch shadow.rs, neither modifies Shadow::landings /
    ote_landing / ake_landed).
**@⁠ScymicX** on GitHub · 2026-11-30 05:59 UTC · [original](https://github.com/trustno1-coder/VIctory-NVIDIA/pull/3753) `ScymicX:perf/landed-atomic-bitmap` → `main` · +136 −1 · 1 files · 2 commits · open ## What it changes Once a shadow buffer receives any GPU->CPU landing (self.landing == true), every subsequent Shadow::take of known pages goes through Shadow::take_landed. Previously this acquired self.landings: Mutex<Vec<u64>>, walked the range 4 KiB page by 4 KiB page, and split even unlanded runs into <= 64 KiB (PAGE) pieces. This PR adds a lock-free landings_atomic: OnceLock<Box<[AtomicU64]>> bitmap (enabled by default, VICTORY_LANDED_FAST=0 restores the legacy Mutex<Vec<u64>> path): - **Shadow::note_landing**: lazily allocates the AtomicU64 word slice once and sets all pages in the landing range word-by-word via etch_or(..., Ordering::Release). - **Shadow::take_landed**: - Fast-path exits directly to self.copy(from, to) when landings_atomic is uninitialized or when a lock-free word scan over w_start..w_end finds no set bits in the range (zero lock, zero Vec allocation, zero per-page splitting). - For mixed ranges, skips word == 0 chunks as single coalesced unlanded runs (still capping oted runs at PAGE so TITLE scratch memory stays bounded) and clears only inspected full KERNEL_PAGE bits atomically with etch_and(!clear_mask, Ordering::AcqRel), preserving partial-page bits and concurrent ote_landing updates in the same word. - Adds unit test shadow::landing_tests::take_landed_fast_path_and_multi_word_runs covering multi-word unlanded coalescing, cross-word landing boundaries, and partial-page bit retention. ## How it was tested - **GPU and profile**: NVIDIA GeForce RTX 5070 Ti, Xbox Series X, 1440p, DLSS Balanced, frame generation off ( 2026.11.28.01 baseline with this optimization). - **Before**: On the 174 MiB main title buffer (1@0x...), Take landed processed **17.45 MB/frame** through the per-page landing path. - **After**: Take landed on the same 174 MiB buffer dropped to **1.79 MB/frame (-90%)** because unlanded regions fast-path straight to self.copy without locking or splitting. - cargo test passed for the crates it touches: **yes** - cargo test --locked -p victory-gpu --lib landing_tests: 3 passed, 0 failed (both default and VICTORY_LANDED_FAST=0). - cargo test --locked -p victory-gpu --lib: 922 passed, 60 ignored, 1 pre-existing unrelated failure on main (shader_cache::tests::every_switch_of_the_translator_is_in_the_key). - cargo clippy -p victory-gpu --no-deps --all-targets --locked and --all-features: 0 warnings in shadow.rs changes. - cargo xtask policy --staged: passed. ## Checked for overlap - [x] I searched the open pull requests for the same issue and for the files this touches. Related: #3268, #3502 (both touch shadow.rs, neither modifies Shadow::landings / ote_landing / ake_landed).
Replace the Mutex<Vec<u64>> landings lookup/clear in Shadow::note_landing
and Shadow::take_landed with a lazily initialized lock-free Box<[AtomicU64]>
bitmap (landings_atomic), controlled by VICTORY_LANDED_FAST (default on,
=0 restores the legacy mutex path).

- note_landing sets page bits word-by-word with fetch_or(Release).
- take_landed scans 64-page words lock-free; when no bits are set in the
  range it calls self.copy(from, to) directly without allocating or
  splitting into 64 KiB chunks.
- Mixed ranges fast-path whole zero words into coalesced unlanded runs
  and atomically clear inspected whole-page bits via fetch_and(AcqRel).
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-30 06:02:49 +00:00
Author
Owner

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

Tested locally merged onto main (41adb551).

  • Merge: Clean merge in crates/victory-gpu/src/shadow.rs.
  • Tests (RTX 5090): cargo test -p victory-gpu --lib shadow:: passed (73 passed; 0 failed; 5 ignored, including landing_tests::take_landed_fast_path_and_multi_word_runs and take_batches_dirty_runs_for_whole_and_diff_paths) with 0 new warnings.

Clean lock-free AtomicU64 landing bitmap and word-scanned take_landed. LGTM.

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

**@⁠Dukestar69** on GitHub · 2026-11-30 06:09 UTC · [original](https://github.com/trustno1-coder/VIctory-NVIDIA/pull/3753#pullrequestreview-6107596299) · **approved these changes** Tested locally merged onto `main` (`41adb551`). - **Merge**: Clean merge in `crates/victory-gpu/src/shadow.rs`. - **Tests (RTX 5090)**: `cargo test -p victory-gpu --lib shadow::` passed (`73 passed; 0 failed; 5 ignored`, including `landing_tests::take_landed_fast_path_and_multi_word_runs` and `take_batches_dirty_runs_for_whole_and_diff_paths`) with 0 new warnings. Clean lock-free `AtomicU64` landing bitmap and word-scanned `take_landed`. LGTM. Synthetic tests only (no game files used). AI-assisted (Gemini 4 Neon).
Author
Owner

@⁠mstrmoos on GitHub · 2026-11-30 19:22 UTC · original

Picked onto our branch with #3753, #3756, #3760, #3881, #3832, #3858 and four that have since merged (#3818, #3850, #3874, #3871). #3760 needed a hand merge on top of the others.

All of victory-gpu's and victory-shader's tests passed. On the RTX 5080 16 GB, Ryzen 7 9800X3D, Windows 11, Series X profile, same save and 90 s bench route each run, the frame median went from 40.4 to 39.1 ms for the whole set. I didn't measure the PRs one by one, so this is evidence of no regression rather than this PR's own gain. Screenshots looked clean.

**@⁠mstrmoos** on GitHub · 2026-11-30 19:22 UTC · [original](https://github.com/trustno1-coder/VIctory-NVIDIA/pull/3753#issuecomment-7289288838) Picked onto our branch with #3753, #3756, #3760, #3881, #3832, #3858 and four that have since merged (#3818, #3850, #3874, #3871). #3760 needed a hand merge on top of the others. All of victory-gpu's and victory-shader's tests passed. On the RTX 5080 16 GB, Ryzen 7 9800X3D, Windows 11, Series X profile, same save and 90 s bench route each run, the frame median went from 40.4 to 39.1 ms for the whole set. I didn't measure the PRs one by one, so this is evidence of no regression rather than this PR's own gain. Screenshots looked clean.
Author
Owner

@⁠upipibi on GitHub · 2026-12-01 00:04 UTC · original

Race review of #3753 (head 858497ad): the lock-free bitmap holds. I found no interleaving where it skips an upload that the mutex path would send. One small note about A/B builds is at the end.

Why it holds (line numbers on the head):

  • take_landed builds clear_mask per word only from whole pages inside its own [from, to), then calls fetch_and(!clear_mask) (shadow.rs:7681-7697). A 64-page word covers 256 KiB and a refresh part is 64 KiB, so parts on different threads do share words. Each part clears only its own bits.
  • The mutex used to make "read a bit, clear it" atomic. Without it, one interleaving is new: a note_landing of a page whose bit is already set, landing between the load and the fetch_and. That note is cleared. land_card calls note_landing only after land::piece has put the bytes in (shadow.rs:8336-8341). This take reads title memory after its fetch_and (copy_mapped, :7727), so it still sees those bytes. The only effect is that the next take of that page uses self.copy, the same path as with VICTORY_LAND_ECHO off: an extra upload, never a missed one. In every interleaving, the bits set here are a subset of what the mutex version would hold.
  • fetch_or(Release) in note_landing pairs with load(Acquire) in take_landed, so a take that sees a bit also sees the bytes landed before it.

Test: a stress test with two threads on one word. One thread notes and takes pages 0..32 eight times a round; the other notes pages 32..64 one by one. After each round, all bits 32..64 must still be set. That's 2,000 rounds per run.

the test (added to landing_tests)
#[test]
fn take_landed_keeps_notes_of_the_other_half_of_a_word() {
    const PAGES: usize = 64;
    let room = vec![7u8; (PAGES + 1) * KERNEL_PAGE];
    let title = (room.as_ptr() as usize).next_multiple_of(KERNEL_PAGE);
    let mut side = vec![7u8; PAGES * KERNEL_PAGE];
    let copy = Shadow::unbacked(title as u64, side.as_mut_ptr(), side.len() as u64, true);
    copy.note_landing(0, KERNEL_PAGE);
    let word = || copy.landings_atomic.get().expect("bitmap")[0].load(std::sync::atomic::Ordering::Acquire);
    for round in 0..2000 {
        std::thread::scope(|s| {
            s.spawn(|| {
                for _ in 0..8 {
                    copy.note_landing(0, 32 * KERNEL_PAGE);
                    copy.take_landed(title, title + 32 * KERNEL_PAGE);
                }
            });
            s.spawn(|| {
                for p in 32..64 {
                    copy.note_landing(p * KERNEL_PAGE, (p + 1) * KERNEL_PAGE);
                }
            });
        });
        assert_eq!(word() >> 32, u64::from(u32::MAX), "round {round}: a note of pages 32..64 was lost");
        copy.take_landed(title + 32 * KERNEL_PAGE, title + 64 * KERNEL_PAGE);
        let _ = copy.take_dirty();
    }
    drop(room);
}
run result
this PR merged onto main 7d93ee07, shadow::landing_tests 4 passed, 0 failed
the stress test, 5 runs 5 passed
negative control: fetch_and(!clear_mask) replaced by store(word & !clear_mask) (a read-modify-write that isn't atomic), 3 runs 2 failed (round 0: a note of pages 32..64 was lost), 1 passed

The negative control shows the test does catch a lost note. Setup: Ryzen 9 5950X, Windows 11, CPU-only tests. The head alone doesn't build the lib tests (E0061 at host.rs:20302, which isn't in this PR's diff), so I tested it merged onto current main, which merges cleanly.

A/B note: landed_fast() uses toggled!. In diagnostics or ab builds with VICTORY_AB naming VICTORY_LANDED_FAST, it can flip while the title runs (switch.rs:24). Notes then go to one bitmap while takes read the other. After an on → off → on cycle, the atomic bitmap still holds bits from the first "on" stretch that the mutex path never cleared, so a page can be compared as landed when it was last landed long ago. This affects only an A/B of this switch. Latching it once with a OnceLock<bool>, as #3756 did for its switch, avoids that.

Tested with the help of Claude Code (Claude Opus 5.5).

**@⁠upipibi** on GitHub · 2026-12-01 00:04 UTC · [original](https://github.com/trustno1-coder/VIctory-NVIDIA/pull/3753#issuecomment-7295889219) Race review of #3753 (head 858497ad): **the lock-free bitmap holds.** I found no interleaving where it skips an upload that the mutex path would send. One small note about A/B builds is at the end. **Why it holds** (line numbers on the head): - `take_landed` builds `clear_mask` per word only from whole pages inside its own `[from, to)`, then calls `fetch_and(!clear_mask)` (shadow.rs:7681-7697). A 64-page word covers 256 KiB and a refresh part is 64 KiB, so parts on different threads do share words. Each part clears only its own bits. - The mutex used to make "read a bit, clear it" atomic. Without it, one interleaving is new: a `note_landing` of a page whose bit is already set, landing between the `load` and the `fetch_and`. That note is cleared. `land_card` calls `note_landing` only after `land::piece` has put the bytes in (shadow.rs:8336-8341). This take reads title memory after its `fetch_and` (`copy_mapped`, :7727), so it still sees those bytes. The only effect is that the next take of that page uses `self.copy`, the same path as with `VICTORY_LAND_ECHO` off: an extra upload, never a missed one. In every interleaving, the bits set here are a subset of what the mutex version would hold. - `fetch_or(Release)` in `note_landing` pairs with `load(Acquire)` in `take_landed`, so a take that sees a bit also sees the bytes landed before it. **Test:** a stress test with two threads on one word. One thread notes and takes pages 0..32 eight times a round; the other notes pages 32..64 one by one. After each round, all bits 32..64 must still be set. That's 2,000 rounds per run. <details><summary>the test (added to <code>landing_tests</code>)</summary> ```rust #[test] fn take_landed_keeps_notes_of_the_other_half_of_a_word() { const PAGES: usize = 64; let room = vec![7u8; (PAGES + 1) * KERNEL_PAGE]; let title = (room.as_ptr() as usize).next_multiple_of(KERNEL_PAGE); let mut side = vec![7u8; PAGES * KERNEL_PAGE]; let copy = Shadow::unbacked(title as u64, side.as_mut_ptr(), side.len() as u64, true); copy.note_landing(0, KERNEL_PAGE); let word = || copy.landings_atomic.get().expect("bitmap")[0].load(std::sync::atomic::Ordering::Acquire); for round in 0..2000 { std::thread::scope(|s| { s.spawn(|| { for _ in 0..8 { copy.note_landing(0, 32 * KERNEL_PAGE); copy.take_landed(title, title + 32 * KERNEL_PAGE); } }); s.spawn(|| { for p in 32..64 { copy.note_landing(p * KERNEL_PAGE, (p + 1) * KERNEL_PAGE); } }); }); assert_eq!(word() >> 32, u64::from(u32::MAX), "round {round}: a note of pages 32..64 was lost"); copy.take_landed(title + 32 * KERNEL_PAGE, title + 64 * KERNEL_PAGE); let _ = copy.take_dirty(); } drop(room); } ``` </details> | run | result | |---|---| | this PR merged onto main 7d93ee07, `shadow::landing_tests` | 4 passed, 0 failed | | the stress test, 5 runs | 5 passed | | negative control: `fetch_and(!clear_mask)` replaced by `store(word & !clear_mask)` (a read-modify-write that isn't atomic), 3 runs | 2 failed (`round 0: a note of pages 32..64 was lost`), 1 passed | The negative control shows the test does catch a lost note. Setup: Ryzen 9 5950X, Windows 11, CPU-only tests. The head alone doesn't build the lib tests (E0061 at host.rs:20302, which isn't in this PR's diff), so I tested it merged onto current main, which merges cleanly. **A/B note:** `landed_fast()` uses `toggled!`. In diagnostics or `ab` builds with `VICTORY_AB` naming `VICTORY_LANDED_FAST`, it can flip while the title runs (switch.rs:24). Notes then go to one bitmap while takes read the other. After an on → off → on cycle, the atomic bitmap still holds bits from the first "on" stretch that the mutex path never cleared, so a page can be compared as landed when it was last landed long ago. This affects only an A/B of this switch. Latching it once with a `OnceLock<bool>`, as #3756 did for its switch, avoids that. *Tested with the help of Claude Code (Claude Opus 5.5).*
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/3753/head:github-bot/gh-3753'
git switch 'github-bot/gh-3753'

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-3753'
git switch 'github-bot/gh-3753'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-3753'
git switch 'github-bot/gh-3753'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-3753'
git switch 'main'
git merge --squash 'github-bot/gh-3753'
git switch 'main'
git merge --ff-only 'github-bot/gh-3753'
git switch 'main'
git merge 'github-bot/gh-3753'
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/trustno1-coder--VIctory-NVIDIA!3753
No description provided.