Write watch: kernel writes note their range without the guards' lock #933
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/933"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
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
ReadFileandfreadof its calledkernel_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 atlight()). Nearly all of these reads land in memory that nothing guards, so they waited for nothing.What
KERNEL, 64 ranges, eachfirst << 32 | lastin one word) instead ofGuards::kernel. Dropping theKernelWritefrees the slot without the lock.PROTECTED), guarded together with other views of its page (ALIASED), or known to be absent (ABSENT, a new bit mirror ofGuards::absent, which also coversheld). Those cases go the locked way as before.Guards::kernel's (left_alone). The race is closed in both directions:PROTECTEDbit, then looks at the slots, and leaves out any block it finds there (protect_here,protect_alias);All of these are
SeqCst, so one side always sees the other.ABSENTbefore itsPROTECTEDbit is cleared, so a write that finds the latter cleared finds the former set.Guards::kernelunder the lock, as before.VICTORY_KERNEL_UNLOCKED=0turns it off (every write under the lock, as before). It usestoggled!, soVICTORY_ABcan 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, andkernel_will_writeonce 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:VICTORY_KERNEL_UNLOCKED=0)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 itsPROTECTEDbit) 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).a_refresh_leaves_open_what_the_kernel_is_writingandwhat_is_left_alone_is_no_known_hole.cargo test --workspace --lockedpasses with no failures; clippy with-D warningsreports nothing inwatch.rsin either build (it reports one finding inshadow.rs, and with--all-featuresone indescriptor.rs, both already onmain).Imported from trustno1-coder/VIctory-NVIDIA. Originally posted by Dukestar69 on 2026-11-28T09:29:40Z.
Review: APPROVED
Tested locally merged onto
main(63b902f).cargo xtask policypassed (294 files ok).cargo test -p victory-gpu --lib watch -- --test-threads=1passed (51 passed; 0 failed; 1 ignored, including all 4 newwatch::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, andkernel_writes_racing_refreshes_land_and_are_seen) with 0 compiler warnings. (Note: parallel execution ofwatch::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).
View command line instructions
Checkout
From your project repository, check out a new branch and test the changes.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.