Write watch: kernel writes note their range without the guards' lock #933

Open
0xkn0wh3r3 wants to merge 1 commit from upstream-pr/933 into main
0xkn0wh3r3 commented 2026-11-28 09:24:30 +00:00 (Migrated from github.com)

Imported from trustno1-coder/VIctory-NVIDIA. Originally posted by 0xkn0wh3r3 on 2026-11-28T09:24:30Z.


Based directly on main; independent of #697 and of #934 (DirectStorage queues), which goes with it.

Why

Every raw DirectStorage read of the title's (to a plain address) and every ReadFile and fread of its called kernel_writing, which took the write-watch guards' single lock twice. It took it once to open anything guarded in the destination and note the range the kernel was about to write (Guards::kernel), and once more when the read was done, to take the note back. That is the lock every refresh of host copies holds while it guards blocks, and it is known to be contended (the 94 ms a frame of waiting noted at light()). Nearly all of these reads land in memory that nothing guards, so they waited for nothing.

What

  • The range the kernel writes is noted in a small table of atomic slots (KERNEL, 64 ranges, each first << 32 | last in one word) instead of Guards::kernel. Dropping the KernelWrite frees the slot without the lock.
  • The guards' lock is taken only if a block of the range is guarded or about to be (PROTECTED), guarded together with other views of its page (ALIASED), or known to be absent (ABSENT, a new bit mirror of Guards::absent, which also covers held). Those cases go the locked way as before.
  • Refreshes leave the slots' blocks alone, as they left Guards::kernel's (left_alone). The race is closed in both directions:
    • a refresh that decided to guard a block before a write noted it sets the block's PROTECTED bit, then looks at the slots, and leaves out any block it finds there (protect_here, protect_alias);
    • a kernel write notes its range, then looks at those bits, and takes the lock if it finds one set.
      All of these are SeqCst, so one side always sees the other.
  • A block is noted in ABSENT before its PROTECTED bit is cleared, so a write that finds the latter cleared finds the former set.
  • More than 64 concurrent writes fall back to Guards::kernel under the lock, as before.
  • VICTORY_KERNEL_UNLOCKED=0 turns it off (every write under the lock, as before). It uses toggled!, so VICTORY_AB can flip it in diagnostics builds.

Nothing changes in which bytes the GPU side copies: a block the kernel is writing is still never guarded mid-write, still reported as written by every refresh while it is being written, and reported again by the first refresh after.

Before / after

cargo test --release -p victory-gpu --lib kernel_writes_beside_refreshes -- --ignored --nocapture (added here). Four threads do what a raw DirectStorage read does in the guards' notes (kernel_writing, its drop, and kernel_will_write once the bytes are in) on 64 KiB of plain memory, beside two threads that write a few blocks of a 64-block view and refresh it, which guards those blocks again under the lock. Runs with the switch on and off take turns, 7 of each, 0.7 s each; medians of the runs, on this branch:

per read (median) 99th percentile reads/s refreshes done meanwhile
before (VICTORY_KERNEL_UNLOCKED=0) 0.9 us 121.0 us 243,223 22,653
this branch 0.1 us 1.3 us 3,130,400 24,707

The time per read is the bookkeeping alone; the copy standing in for the read is left out.

Correctness

  • a_kernel_write_to_unguarded_memory_takes_no_lock: with another thread holding the guards' lock, a write to unguarded memory finishes; with the switch off it waits; a write to a guarded block waits and opens the block.
  • a_refresh_leaves_alone_what_the_kernel_writes_unlocked: while a write noted without the lock is under way, refreshes leave its block writable and report it every time; after it, the next refresh reports it once more and guards it.
  • a_guard_leaves_out_what_the_kernel_began_to_write_meanwhile: a guard of three blocks with a write noted on the middle one guards the other two and leaves that one (and its PROTECTED bit) alone.
  • kernel_writes_racing_refreshes_land_and_are_seen: one thread has the kernel write a block again and again with new bytes while another refreshes the view as fast as it can (thousands of each, many of the writes taking the unlocked path). No read fails, every read's bytes land, and a refresh that ends after each read reports the block. With the refresh's look at the slots taken out, this test fails (a read finds its destination guarded).
  • The existing watch tests pass unchanged, including a_refresh_leaves_open_what_the_kernel_is_writing and what_is_left_alone_is_no_known_hole.
  • Locally on this branch: the watch tests pass in three runs out of three; cargo test --workspace --locked passes with no failures; clippy with -D warnings reports nothing in watch.rs in either build (it reports one finding in shadow.rs, and with --all-features one in descriptor.rs, both already on main).
  • Not yet measured in play.
<!-- greyfinch-sync:trustno1-coder/VIctory-NVIDIA:issue:5538703798 --> Imported from [trustno1-coder/VIctory-NVIDIA](https://github.com/trustno1-coder/VIctory-NVIDIA/pull/933). Originally posted by [0xkn0wh3r3](https://github.com/0xkn0wh3r3) on 2026-11-28T09:24:30Z. --- Based directly on `main`; independent of #697 and of #934 (DirectStorage queues), which goes with it. ## Why Every raw DirectStorage read of the title's (to a plain address) and every `ReadFile` and `fread` of its called `kernel_writing`, which took the write-watch guards' single lock twice. It took it once to open anything guarded in the destination and note the range the kernel was about to write (`Guards::kernel`), and once more when the read was done, to take the note back. That is the lock every refresh of host copies holds while it guards blocks, and it is known to be contended (the 94 ms a frame of waiting noted at `light()`). Nearly all of these reads land in memory that nothing guards, so they waited for nothing. ## What - The range the kernel writes is noted in a small table of atomic slots (`KERNEL`, 64 ranges, each `first << 32 | last` in one word) instead of `Guards::kernel`. Dropping the `KernelWrite` frees the slot without the lock. - The guards' lock is taken only if a block of the range is guarded or about to be (`PROTECTED`), guarded together with other views of its page (`ALIASED`), or known to be absent (`ABSENT`, a new bit mirror of `Guards::absent`, which also covers `held`). Those cases go the locked way as before. - Refreshes leave the slots' blocks alone, as they left `Guards::kernel`'s (`left_alone`). The race is closed in both directions: - a refresh that decided to guard a block before a write noted it sets the block's `PROTECTED` bit, then looks at the slots, and leaves out any block it finds there (`protect_here`, `protect_alias`); - a kernel write notes its range, then looks at those bits, and takes the lock if it finds one set. All of these are `SeqCst`, so one side always sees the other. - A block is noted in `ABSENT` before its `PROTECTED` bit is cleared, so a write that finds the latter cleared finds the former set. - More than 64 concurrent writes fall back to `Guards::kernel` under the lock, as before. - `VICTORY_KERNEL_UNLOCKED=0` turns it off (every write under the lock, as before). It uses `toggled!`, so `VICTORY_AB` can flip it in diagnostics builds. Nothing changes in which bytes the GPU side copies: a block the kernel is writing is still never guarded mid-write, still reported as written by every refresh while it is being written, and reported again by the first refresh after. ## Before / after `cargo test --release -p victory-gpu --lib kernel_writes_beside_refreshes -- --ignored --nocapture` (added here). Four threads do what a raw DirectStorage read does in the guards' notes (`kernel_writing`, its drop, and `kernel_will_write` once the bytes are in) on 64 KiB of plain memory, beside two threads that write a few blocks of a 64-block view and refresh it, which guards those blocks again under the lock. Runs with the switch on and off take turns, 7 of each, 0.7 s each; medians of the runs, on this branch: | | per read (median) | 99th percentile | reads/s | refreshes done meanwhile | |---|---|---|---|---| | before (`VICTORY_KERNEL_UNLOCKED=0`) | 0.9 us | 121.0 us | 243,223 | 22,653 | | this branch | 0.1 us | 1.3 us | 3,130,400 | 24,707 | The time per read is the bookkeeping alone; the copy standing in for the read is left out. ## Correctness - `a_kernel_write_to_unguarded_memory_takes_no_lock`: with another thread holding the guards' lock, a write to unguarded memory finishes; with the switch off it waits; a write to a guarded block waits and opens the block. - `a_refresh_leaves_alone_what_the_kernel_writes_unlocked`: while a write noted without the lock is under way, refreshes leave its block writable and report it every time; after it, the next refresh reports it once more and guards it. - `a_guard_leaves_out_what_the_kernel_began_to_write_meanwhile`: a guard of three blocks with a write noted on the middle one guards the other two and leaves that one (and its `PROTECTED` bit) alone. - `kernel_writes_racing_refreshes_land_and_are_seen`: one thread has the kernel write a block again and again with new bytes while another refreshes the view as fast as it can (thousands of each, many of the writes taking the unlocked path). No read fails, every read's bytes land, and a refresh that ends after each read reports the block. With the refresh's look at the slots taken out, this test fails (a read finds its destination guarded). - The existing watch tests pass unchanged, including `a_refresh_leaves_open_what_the_kernel_is_writing` and `what_is_left_alone_is_no_known_hole`. - Locally on this branch: the watch tests pass in three runs out of three; `cargo test --workspace --locked` passes with no failures; clippy with `-D warnings` reports nothing in `watch.rs` in either build (it reports one finding in `shadow.rs`, and with `--all-features` one in `descriptor.rs`, both already on `main`). - Not yet measured in play.
Dukestar69 commented 2026-11-28 09:29:40 +00:00 (Migrated from github.com)

Imported from trustno1-coder/VIctory-NVIDIA. Originally posted by Dukestar69 on 2026-11-28T09:29:40Z.

Review: APPROVED


Tested locally merged onto main (63b902f).

  • Merge & Policy: Clean merge; cargo xtask policy passed (294 files ok).
  • Tests: cargo test -p victory-gpu --lib watch -- --test-threads=1 passed (51 passed; 0 failed; 1 ignored, including all 4 new watch::kernel_unlocked_tests::* tests: a_guard_leaves_out_what_the_kernel_began_to_write_meanwhile, a_kernel_write_to_unguarded_memory_takes_no_lock, a_refresh_leaves_alone_what_the_kernel_writes_unlocked, and kernel_writes_racing_refreshes_land_and_are_seen) with 0 compiler warnings. (Note: parallel execution of watch:: tests hits pre-existing cross-test interference on the process-global watch/fault state; serial execution is completely green.)

Clean lock-free kernel-write fast path avoiding guard-lock contention. LGTM.

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

<!-- greyfinch-sync:trustno1-coder/VIctory-NVIDIA:review:6096970093 --> Imported from [trustno1-coder/VIctory-NVIDIA](https://github.com/trustno1-coder/VIctory-NVIDIA/pull/933#pullrequestreview-6096970093). Originally posted by [Dukestar69](https://github.com/Dukestar69) on 2026-11-28T09:29:40Z. **Review:** APPROVED --- Tested locally merged onto `main` (`63b902f`). - **Merge & Policy**: Clean merge; `cargo xtask policy` passed (`294 files ok`). - **Tests**: `cargo test -p victory-gpu --lib watch -- --test-threads=1` passed (`51 passed; 0 failed; 1 ignored`, including all 4 new `watch::kernel_unlocked_tests::*` tests: `a_guard_leaves_out_what_the_kernel_began_to_write_meanwhile`, `a_kernel_write_to_unguarded_memory_takes_no_lock`, `a_refresh_leaves_alone_what_the_kernel_writes_unlocked`, and `kernel_writes_racing_refreshes_land_and_are_seen`) with 0 compiler warnings. (Note: parallel execution of `watch::` tests hits pre-existing cross-test interference on the process-global watch/fault state; serial execution is completely green.) Clean lock-free kernel-write fast path avoiding guard-lock contention. LGTM. Synthetic tests only (no game files used). AI-assisted (Gemini 4 Neon).
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/933:upstream-pr/933'
git switch 'upstream-pr/933'

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