GPU: track hot MEM_WRITE_WATCH buffer pages via batched GetWriteWatch instead of PAGE_READONLY guard faults #3615
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/3615"
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 Marvisak/VIctory. Originally posted by Texbirds on 2026-11-27T21:38:53Z.
Problem
On every submission,
watch::Tracked::take_splitre-guards modified 64 KiB pages withVirtualProtect(PAGE_READONLY)(triggering cross-core TLB shootdowns). For hot CPU-written buffer pages (ring buffers, per-frame constants, instance/command data written every frame), the title's very next CPU write immediately faults intoon_write(hardware#PF→ kernel exception dispatch → user-mode VEH → globalGUARDSmutex →VirtualProtect(PAGE_READWRITE)TLB shootdown → replay).victory-hlealready allocates runtime pools withMEM_WRITE_WATCH, andwatch.rsalready queriesGetWriteWatchto classify them — but never used hardware write-watch to track writes on hot pages.Approach
Keep
PAGE_READONLYguards for cold pages (preserving zero-syscallTracked::clean()viaDIRTYbits), section/aliased views, texture stagings, descriptor heaps, and armed GPU-write ranges, while adaptively promoting hotMEM_WRITE_WATCHbuffer-copy pages to batched hardwareGetWriteWatch:HOT_THRESHOLD = 3): After 3 consecutive guard faults on a page with no physical aliases, where every holder is an unarmed host buffer copy (copy == true),protect_notingcallsResetWriteWatch(which atomically verifies the page is aMEM_WRITE_WATCHallocation and clears its dirty bits) and moves the page intoGuards::watchedwithout callingVirtualProtect(PAGE_READONLY).WATCH_BATCH_PAGES = 64, up to 4 MiB per syscall):Tracked::take_splitgroups contiguous watched pages into runs of up to 64 pages and polls/resets them in a singleGetWriteWatch(WRITE_WATCH_FLAG_RESET, ...)call using a stack buffer sized for all64 × 16 = 1,024hardware 4 KiB subpages, propagating dirty bits to any other buffer copies sharing those pages.COOL_TAKES = 2): If a watched page sees 2 consecutive quiet takes with 0 CPU writes, it is demoted back toPAGE_READONLYso cold trackers return to zero-syscallTracked::clean().Tracked::armacquiresGUARDSbeforeSaved(matching global lock order) and atomically demotes any watched page in the armed range back toPAGE_READONLY(polling pending writes first) soon_writealways capturesChanged::savepre-write bytes before concurrent CPU writes land.Tracked::changedperforms a non-resettingGetWriteWatch(0, ...)peek on watched pages soShadow::copy_back_marked/Shadow::copy_back_pagenever falsely drop GPU writes on untouched hot pages.Tracked::taken,track(..., copy=false),title_memory_remapped,title_memory_aliased, andDrop for Trackedall cleanly demote/clear watched state with zero lost writes.Benchmarks (
--release, Windows 11 x64, Ryzen 9 5900X + NVIDIA GeForce RTX 3080 Ti)1. End-to-end 8-thread CPU writers + real D3D12
Shadow::updateon RTX 3080 Ti (512hot pages = 32 MiB × 100 frames,cargo test --release --locked -p victory-gpu -- watch::tests::bench_multithreaded_shadow_refresh_on_host_gpu --ignored --nocapture):PAGE_READONLY+ VEH)MEM_WRITE_WATCH)Shadow::update(take + 32 MiB copy)2. Single-threaded tracking-only A/B (
256hot pages = 16 MiB × 100 frames,cargo test --release --locked -p victory-gpu -- watch::tests::bench_hot_write_watch_vs_guards --ignored --nocapture):PAGE_READONLYguards + VEH:88.45 ms(0.88 ms/frame)MEM_WRITE_WATCH:19.42 ms(0.19 ms/frame) — 4.6× fasterVerification (Windows 11 x64, RTX 3080 Ti + WARP, Rust 1.98.1)
cargo run --locked -p xtask -- policy:191 files okcargo clippy --locked -p victory-gpu --all-targets -- -D warnings: 0 warningscargo clippy --locked -p victory-gpu --all-targets --all-features -- -D warnings: 0 warningscargo test --locked -p victory-gpu -- watch::: 21/21 passed (2 benchmarks ignored)VICTORY_D3D12_WARP=1 cargo test --locked -p victory-gpu --features diagnostics -- watch:: shadow:: bindless:: descriptor::: 56/56 passedwatch:: shadow:: bindless:: descriptor::: 55/55 passed (excluding the pre-existing NVIDIA 1M view-heap testdynamic_slots_are_held_until_released)Imported from Marvisak/VIctory. Originally posted by Desamodeth on 2026-11-27T22:20:46Z.
Confirmed a missed write on
222edf70on Windows. After a hot 64 KiB page gets two quiet 4-byte refreshes, writing its last word produces an empty refresh. The same sequence passes onmain(63bcca43). A second reproducer uses two buffer copies sharing a page while a hostWritingguard is open.Cherry-pickable fix and both regression tests. Hardware polling stays active until the read-only guard takes over. All 23 active watch tests passed. A separate local test injected a write between the two polls and verified delivery to both holders.
CPU writes + tracking, median µs/update across six interleaved runs per build (main / PR / fix, 128 pages): all pages written 296.0 / 57.6 / 57.6, every eighth page 43.1 / 18.6 / 18.6, four dense updates followed by two quiet updates 199.5 / 173.8 / 174.2. These timings cover the CPU tracker only. AI-assisted with Codex Astra 6.2 High.
Imported from Marvisak/VIctory. Originally posted by Texbirds on 2026-11-27T22:32:19Z.
Awesome catch @Desamodeth — thank you for the reproducers, the clean fix, and the independent benchmark confirmation (
296.0 µs → 57.6 µson 128 pages)!Cherry-picked
d2d23a3eonto the PR branch as5b8f14e(preserving your authorship) and re-verified locally on Windows 11 x64 (RTX 3080 Ti):cargo run --locked -p xtask -- policy:191 files okcargo clippy --locked -p victory-gpu --all-targets -- -D warnings(default &--all-features): 0 warningscargo test --locked -p victory-gpu -- watch:: shadow::: 33/33 passed (including both new regression tests)Imported from Marvisak/VIctory. Originally posted by upipibi on 2026-11-27T22:55:58Z.
An interaction with #3585 (open, same
watch.rscode), read from both heads and not run: each PR is consistent on its own, but merged together, writes to watched pages would be missed on the refresh path.Guards::watchedis left writable, and its writes are found only by theGetWriteWatchpoll insideTracked::take_split. That poll runs even when the changed set is empty (watch.rs:718-719at5b8f14e,has_watched).af08b8b) moves a submission's refreshes offtake_split.Batch::flushcallsshadow::plan(shadow.rs:698), which takes the pages fromTracked::written(shadow.rs:720→watch.rs:490). That reads only the changed set, and a copy with nothing in it returnsPlan::Done(0)(shadow.rs:729-733).take_written(watch.rs:550) then only guards the pages it was given.With both in, a watched page the title wrote without a fault is not in the changed set. The batch skips it, and the host copy keeps its old bytes.
take_splititself still polls, but in #3585 it is only the one-tracker path.Whichever lands second needs the watched-page poll in
Tracked::written, or inplanbefore its early return, plus a test that refreshes a watched page throughBatch::flush. The two PRs also overlap textually over most oftake_splitandprotect_noting.AI-assisted (Claude Code, Claude Opus 5.5 read both heads and checked the lines).
Imported from Marvisak/VIctory. Originally posted by Texbirds on 2026-11-27T22:59:17Z.
Good catch @upipibi — verified against #3585 (
af08b8b), your read is spot on.Since both PRs target
main(63bcca4) and are self-contained on their own, no change is needed here right now. If #3585 lands first, I'll rebase #3615 sowatch::take_writtenpolls watched pages inside the single batchedGUARDSlock round (and add aBatch::flushwatched-page regression test). Both optimizations should compose cleanly once combined.Imported from Marvisak/VIctory. Originally posted by pickles976 on 2026-11-28T05:55:17Z.
Exact head
5b8f14e7b88a468c8a4a84e74e43d612f2a59b7b: deterministic lost pre-write snapshot when the first hot page is promoted concurrently withTracked::arm.Linux/Wine 11, 16GiB host. CPU Win32/tracking only, no GPU dispatch. Fresh intended-tree build8.12s. Your existing sequential
arming_a_hot_write_watch_page_demotes_it_and_saves_pre_write_bytespasses; the new interleaving below fails withsaved=None, expected first word0xAAAA5555. Production behavior unchanged; only a cfg(test) scheduling hook and regression added.Interleaving:
WATCHED_PAGES == 0, pauses before acquiringSaved.takeafter the third write fault:can_watchstill sees an unarmed holder; production promotion succeeds and leaves the page writable.Savedarmed and returns without demotion.0xDEADBEEF; no fault occurs and the required old bytes are absent.This is the path the current sequential test cannot exercise. A global zero count is not synchronization with registration of the first watched page. Conservative correction: serialize arming with promotion under
GUARDSeven when the count was zero (maintaining GUARDS→Saved lock order), or provide an equivalent proven handshake. A second unsynchronized counter check alone leaves the eligibility-check/publication race. Please retain a concurrent before-image regression before enabling this path in title runs. No title corruption/performance attribution is claimed here.Evidence
.compat-runtime/logs/verify-write-watch-arming-race-20261128T055339Z/: parent 1passed/1failed, isolated child fails the semantic before-image assertion; no timeout. No assets involved.Test-only hook beside
WATCHED_PAGES:At the start of the
if WATCHED_PAGES.load(Ordering::Acquire) == 0branch, beforeSaved::arm:The subprocess makes this isolated from other tests' global watched-page state; both rendezvous waits have ten-second deadlines:
Imported from Marvisak/VIctory. Originally posted by Texbirds on 2026-11-28T08:39:01Z.
Great catch @pickles976 — that
WATCHED_PAGES == 0skip was indeed an unsynchronized race against first-time promotion inprotect_noting.Fixed in
3de7653:WATCHED_PAGESand serializedTracked::armunconditionally underGUARDS(maintainingGUARDS -> Savedlock order), checking!g.watched.is_empty()before scanningg.watched.range(first..last).watch::tests::arming_during_hot_promotion_keeps_before_image.Re-verified locally on Windows 11 x64 (RTX 3080 Ti):
cargo run --locked -p xtask -- policy:191 files okcargo clippy --locked -p victory-gpu --all-targets -- -D warnings(default &--all-features): 0 warningscargo test --locked -p victory-gpu -- watch:: shadow::: 34/34 passedImported from Marvisak/VIctory. Originally posted by futurepro62 on 2026-11-29T18:13:17Z.
Full-suite run on NVIDIA, since this PR has no test report yet. Tested exact head
3de7653(on its base63bcca4).Tested
dxil.dllfrom Windows SDK 10.0.26100 (the loader's default), debug.cargo build --workspace --locked, thencargo test --workspace --locked --no-fail-fast -- --test-threads=4 --skip emit::memory_tests::heap_direct_texture_reads_mark_their_heap_index --skip descriptor::host::tests::dynamic_slots_are_held_until_released --skip memory::tests::lists_share_a_table_and_copies_keep_their_views --skip emit::stride_tests::a_heap_buffer_read_by_element_takes_the_views_stride --skip emit::stride_tests::a_heap_buffer_read_where_no_buffer_view_is_reads_zeros --skip emit::synthetic_tests::gathers_at_a_level_match_the_hardware: 875 passed, 0 failed, 12 ignored.hot_write_watch_pages_promote_and_cool_without_faulting,hot_write_watch_page_shared_by_two_copies_notifies_both,arming_a_hot_write_watch_page_demotes_it_and_saves_pre_write_bytes,changed_on_hot_write_watch_page_distinguishes_written_from_untouched,non_copy_taken_and_remap_demote_hot_write_watch_pages,hot_write_watch_concurrent_churn_loses_no_writes,quiet_partial_refresh_preserves_the_next_write,hot_page_stays_visible_to_both_holders_while_a_writer_is_open,arming_during_hot_promotion_keeps_before_image.--skipleaves out tests that fail on main63bcca4on this card too, not because of this PR:heap_direct_texture_reads_mark_their_heap_indexends thevictory-shadertest process with0xc0000005(#2571), so without the skip the rest of that crate never reports;dynamic_slots_are_held_until_released,lists_share_a_table_and_copies_keep_their_viewsand the twostride_testshit the 1,000,000-view heap cap of NVIDIA drivers (#1835, #2317);gathers_at_a_level_match_the_hardwarefails on lane 0 (#1744).Not tested
AI use
Imported from Marvisak/VIctory. Originally posted by Texbirds on 2026-11-29T18:24:52Z.
Thank you @futurepro62 for running the full workspace suite on Blackwell (
RTX 5070 Ti+9800X3D,875 passed, 0 failedat3de7653, including all 9 newwatchregression tests)!With that hardware pass alongside @Desamodeth's Windows benchmark confirmation, @pickles976's concurrent arming race fix, and green CI,
3de7653is clean and ready for merge.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.