GPU: batch watch GUARDS locks, VirtualProtect runs, and shadow copy-back #2169
No reviewers
Labels
No labels
accessibility
area: build
area: formats
area: gpu
area: loader
area: runtime
area: shader
bug
documentation
duplicate
enhancement
good first issue
gpu: amd
gpu: intel
gpu: nvidia
help wanted
host: wine
invalid
out of scope
question
request
run report
triage: confirmed
triage: has patch
triage: merge candidate
triage: needs info
triage: needs rebase
triage: needs work
triage: proposed close
wontfix
No milestone
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
github/Marvisak--VIctory!2169
Loading…
Reference in a new issue
No description provided.
Delete branch "gh-pr/2169/head"
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?
@RVZO6 on GitHub · 2026-11-25 06:50 UTC · original
RVZO6:rvzo/watch-batch→main· +517 −130 · 5 files · 2 commits · closed without mergingOn CPU-heavy frames and physical/tiled memory mappings, write-watch maintenance and marked-page copy-back incur avoidable global lock churn, per-page
VirtualProtectsyscalls, and per-page 64 KiB scratch allocations:xmem::mapcallstitle_memory_aliasedin a loop per 64 KiB page — locking globalGUARDSeven whenshownis empty (the common unaliased case) and callingprotect(&mut g, &[page])one page at a time.Tracked::drop,changed, andchanged_herecallVirtualProtect(..., PAGE, PAGE_READWRITE, ...)one page at a time instead of coalescing contiguous runs.Shadow::copy_back_markedacquiresTracked's mutexes three times per page (unarm,saved,changed), allocates a freshvec per page, and locksGUARDS+ callsVirtualProtectper page viat.taken(&[page / PAGE]).The change
crates/victory-gpu/src/watch.rs&crates/victory-gpu/src/lib.rs:title_memory_aliased_batch(with zero-lock early return when no entry has aliases;title_memory_aliaseddelegates when!others.is_empty()), linking aliases under oneGUARDSlock and callingprotect(&mut g, &to_protect)once (sorting & deduping inprotect).open()withopen_runs(), coalescing consecutive pages into oneVirtualProtect(PAGE_READWRITE)per run with per-page fallback across unmapped holes or distinct reservations.Changed's two mutexes into oneMutex<ChangedState>, addingTracked::begin_copy_back(page)(single lock for disarm + take saved bytes + changed check) andChanged::mark(page, save).crates/victory-hle/src/modules/xmem.rs: calltitle_memory_aliased_batchonce permap()instead of per page.crates/victory-gpu/src/shadow.rs&crates/victory-gpu/src/memory.rs:page_scratch) acrosscopy_back_marksandmemory::bind'scopy_back_pageloop.t.taken(&batch)per contiguousShadowrun incopy_back_marks, queuing only written or previously opened pages so unchanged guarded pages keep their guard and never clear a concurrent CPU write arriving afterbegin_copy_back.VirtualProtect/ twin-section tests:watch::tests::title_memory_aliased_empty_and_batch_propagate_faults,watch::tests::open_runs_coalesces_and_falls_back_across_unmapped_holes, andshadow::tests::batched_copy_back_reuses_scratch_batches_taken_and_rearms.Not a duplicate of
#2147changesSaved::armedfrom aBTreeSet<usize>to page runs (Tracked::arm/unarm); it does not batchtitle_memory_aliased, coalesceVirtualProtectinopen_runs, unifybegin_copy_back, reuse copy-back scratch buffers, or batchTracked::taken. If#2147merges first, this rebases cleanly onto itsarmedruns representation.#2136skips requesting unchanged host copies at submission and is orthogonal to marked-page copy-back and watch batching.Tested
windows-latestMSVC + Windows WARP SM 6.6 CI on fork integration stack (Run 45562558052):cargo clippy --workspace --all-targets --locked -- -D warnings: clean.cargo clippy --workspace --all-targets --all-features --locked -- -D warnings: clean.cargo test --workspace --locked: 712 passed, 0 failed, 10 ignored, includingvictory-gpu152 passed, 0 failed, 2 ignored,victory-hle143 passed, 0 failed (all 10modules::xmem::tests::*), andxtask(6 passed, 0 failed,repository_passes_policy).c5a9547:cargo clippy(default &--all-features)-D warningsclean; Wine-10.0 passedwatch/shadow(13 passed, 0 failed) andxmem(10 passed, 0 failed).Not run: the title; any hardware GPU (CI ran on Windows WARP only). The title was not used in these tests.
AI-assisted: T3 Code (Antigravity, gemini-4-neon)
@nathanbzrr on GitHub · 2026-11-25 08:40 UTC · original
Audited head
c5a95470f95e158354f3304eb36f68d3034b9dd5onmain(940d32f69b) on Windows 11 x64 / AMD Radeon RX 7600.Baseline tests pass (
watch::tests8/8,shadow::tests5/5), but empirical repros confirm a silent CPU data-loss regression and a broken public alias API:Live Repro — Deferred
t.taken(&batch)loses concurrent CPU writes (shadow.rs:480-484, 770-802):In
Shadow::copy_back_marked_page, after copying GPU bytes back to pageP0,_writingdrops immediately (leavingP0asPAGE_READWRITE) whileP0 / PAGEis only queued intobatch. Whilecopy_back_marksprocesses remaining pagesP1..PNof thatShadowcopy, any concurrent CPU write toP0succeeds without faulting, and the finalt.taken(&batch)removesP0fromchanged.pages(watch.rs:428) and setsPAGE_READONLY.Repro across a 64-page pool (
repro_batched_taken_loses_concurrent_cpu_write_on_written_page):Live Repro —
pub fn title_memory_aliased_batchmisses chained aliases (watch.rs:545-583):Because
protect(&mut g, &to_protect)is deferred until after the loop, chained pairs liketitle_memory_aliased_batch([(a, &[b][..]), (c, &[a][..])])(wherebis protected) checkg.protected.contains(&a) == falseand skip protectingc:Source-level — Widened pre-write snapshot race & VEH allocation (
watch.rs:498-501):When
save == true(inside VEHon_write/writing),changed()callsopen_runs(g, &others)before looping overothersto snapshot pre-write bytes (c.mark(page, true)), widening the window where another thread writing through an alias corruptsSaved.bytes(and allocatingto_openinside VEH wheneverothers.len() >= 2).(Proposed / uncompiled fix direction:) re-guard each written page immediately in one
GUARDSlock at_writingend (finish_writing), leave previously opened unchanged pages inchanged.pages, propagate protections across chained aliases via worklist/BFS intitle_memory_aliased_batch, and snapshot armed holders beforeopen_runswhensave == true.AI-assisted; synthetic tests only, no game files, dumps, or captures used.
@MamaLeVrai on GitHub · 2026-11-25 08:52 UTC · original
Test report on real hardware: AMD RX 6950 XT (RDNA2), Windows 11, Rust 1.98.1, with a GPU present, so the hardware-only shader checks run. I merged this PR's head
c5a95470intomain940d32fin a throwaway tree (no conflict).cargo clippy --workspace --all-targets --locked -- -D warnings: clean; the same with--all-features: clean.cargo test --workspace --locked: 704 passed, 0 failed, 10 ignored, two runs (mainalone on the same machine: 701 passed, 0 failed, 10 ignored). The title was not run, so this says nothing about in-game effect. No game data.AI-assisted (Claude Code); no game data.
@RVZO6 on GitHub · 2026-11-25 09:19 UTC · original
Thanks @nathanbzrr for the thorough RX 7600 repros — all three findings are fixed in
8b8b076:watch.rs,shadow.rs):t.taken(&batch)inShadow::copy_back_markswithWriting::finish_taken, which removes the busy range, notifies other holders/aliases, re-guards the written full page and its aliases, and clears it fromtracked.changedunder oneGUARDSlock immediately afterwrite_mapped(also used incopy_back_page).saved.is_some() && written.is_none()) remain inchanged.pagesso concurrent CPU writes afterread_intoare never lost.title_memory_aliased_batch(watch.rs):g.alias_of(covering both forward[(a, &[b]), (c, &[a])]and reverse[(c, &[a]), (a, &[b])]chains in oneGUARDSlock).watch.rs):watch::changednow marks/snapshots armed holders before removing read-only guards on[first, last)or aliases, andopen_runs,open_range, andchangeduse a 16-entry stack buffer to avoid heap allocation on the VEH fault path.shadow::tests::batched_copy_back_reuses_scratch_batches_taken_and_rearmsto assert a concurrent CPU write immediately after page 0 copy-back faults and stays markedchanged, and addedwatch::tests::title_memory_aliased_batch_propagates_chained_aliases.Verified on fork
windows-latestMSVC + Windows WARP SM 6.6 CI (Run45581807630): bothclippy -D warningsbuilds clean,716 passed; 0 failed; 10 ignored,xtask6/6.AI-assisted: T3 Code (Antigravity, gemini-4-neon)
@Marvisak on GitHub · 2026-11-25 13:27 UTC · original · edited 2026-11-25 16:00 UTC
Automated triage for the maintainers.
This pull request no longer merges cleanly into
main(conflicts incrates/victory-gpu/src/watch.rs). It will be reviewed once it is rebased onto the currentmain. The earlier verdict (needs-work) was for an older state ofmain.Generated by Claude Code
@RVZO6 on GitHub · 2026-11-25 17:13 UTC · original
Closing as superseded by
main's change-tracking rework (cc0e153dense dirty bitmap,280b150region-by-region tracking,36ca299guard-free retake). Thanks to @nathanbzrr, @DenRakEiw, and @MamaLeVrai for reviewing!Pull request closed