GPU, HLE: marked pages go back beside the guards (VICTORY_COPY_BACK_BESIDE, opt-in) #3069
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!3069
Loading…
Reference in a new issue
No description provided.
Delete branch "github-bot/gh-3069"
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?
@moe-miau on GitHub · 2026-11-26 06:17 UTC · original
moe-miau:perf/land→main· +212 −15 · 5 files · 1 commits · openWhat
After a submission, every 64 KiB page shaders marked as written goes back into title memory in
Shadow::copy_back_marked. Each page opens its guards (watch::writing), gets copied in by the kernel (host::write_mapped), then is guarded again (Tracked::taken). On the stack below that is about 2,250 pages a frame, 45 ms of a 60 ms copy-back: two protection changes per page, first-write faults on the newly writable page, and a kernel copy.With
VICTORY_COPY_BACK_BESIDE=1, a page that a view of physical pages shows is written through a view of our own of the whole section instead (xmem::write_beside, whichvictory_gpu::use_page_writerregisters). No guard covers that view. The title's views stay guarded and show the new bytes, since they are the same pages. No protection calls, no first-write faults, no kernel write.PHYSICAL), so no view there is mapped, unmapped or freed while it happens. The lock order PHYSICAL → GUARDS is the onemap/unmapalready use, when they calltitle_memory_remapped/title_memory_aliased.alias_of) are marked changed in every tracker holding them. The first mark comes before the write: armed trackers save their bytes. The second comes after it, for a tracker that took the page in between.changed()now shares that marking throughmark_with_aliases. The copy's own tracker is unarmed first and taken afterwards, as before.The switch is off by default: nothing changes unless it is set.
The idea comes from avatarmanz/VIctory-7900XTX's landing beside the guards (
66afb2b,0d5e4c8,2c982ee; GPL-3.0-or-later). This is a new, smaller implementation onmain, not their code. It has no dword merge or worker pool. It works alongside #2914, which spreads the same loop over threads.Measured
RTX 4070 SUPER, Windows 11. Local stack: main + #2464 + #2098 + #2626 + #2628 + #2711 + #2851 + #2705 + #2925 + #2958, with
VICTORY_CARD_COPIES=1andVICTORY_WAVE32_APPROX=1. Prologue cell, medians of the 5 s reports from 540 to 900 s of a 15-minute run (about 62 reports each):Tests
watch::tests::a_write_beside_keeps_the_guards_and_marks_the_trackers: three views of one section, two of them title mappings with trackers (one armed). A write beside leaves both mappingsPAGE_READONLY, shows in both and marks both trackers. The armed one keeps the bytes from before. A writer that cannot write marks nothing.xmem::tests::writes_beside_land_through_guarded_views: a write lands through a read-only title view and shows at the page's other mapping. A range past a view, or private memory, is not written.cargo test -p victory-gpu --lib --locked: 230 passed. The 2 failures are the known heap-size pair on NVIDIA (dynamic_slots_are_held_until_released,lists_share_a_table_and_copies_keep_their_views), which fail onmaintoo.cargo test -p victory-hle --lib --locked: 153 passed.cargo clippy --workspace --all-targets --locked -- -D warningsandcargo xtask policyare clean.AI-assisted (Claude Code). Runs done on my machine.
🤖 Generated with Claude Code
@Dukestar69 on GitHub · 2026-11-26 06:28 UTC · original · approved these changes
Tested locally merged onto
origin/main(63bcca4) on Windows 11 x64 (NVIDIA GeForce RTX 5090):VICTORY_COPY_BACK_BESIDEwrites marked pages back through separate physical-page views without disarming/rearming title write-watch guards, preserving guard protection while still marking trackers.cargo test -p victory-gpu watch::tests -- --test-threads=1: 16 passed, 0 failed (includinga_write_beside_keeps_the_guards_and_marks_the_trackers).cargo test -p victory-gpu shadow::tests -- --test-threads=1(RTX 5090): 9 passed, 0 failed.cargo test -p victory-hle xmem::tests -- --test-threads=1: 13 passed, 0 failed (includingwrites_beside_land_through_guarded_views).cargo clippy -p victory-gpu -p victory-hle --all-targets -- -D warnings: clean.cargo xtask policy: 191 files ok.Synthetic tests only (no game files used). AI-assisted (Gemini 4 Neon).
@kotekompotek on GitHub · 2026-11-26 06:35 UTC · original · approved these changes
Verified both halves: Section::whole lazily maps an unguarded full-section view (unmapped on Drop); write_beside resolves at->whole offset under the physical() lock (checked_add bound) so no remap mid-write; write_through marks pages+aliases changed with save before the write and again after (a tracker taking the page mid-way reads it), matching the writing-window semantics without opening any guard; fallback to writing+write_mapped preserved; opt-in env. Both tests use real sections+VirtualProtect and discriminate.
@fuzeofficlal on GitHub · 2026-11-26 08:00 UTC · original · reviewed
Tested locally on Windows 11 x64 (
rustc 1.98.1, RTX 5090 + WARP):cargo clippy -p victory-gpu -p victory-hle --all-targets --locked -- -D warnings(default and--all-features): clean.VICTORY_COPY_BACK_BESIDE=1):victory-gpuwatch::tests(16 passed),shadow::tests(9 passed);victory-hlexmem::tests(13 passed).--features diagnostics,VICTORY_D3D12_WARP=1,VICTORY_COPY_BACK_BESIDE=1):watch::tests(16 passed),shadow::tests(9 passed).cargo xtask policy: 191 files ok.PHYSICAL -> GUARDSmatchesxmem::map/xmem::unmap, noGUARDS -> PHYSICALpath) and confirmed with a standalone Windows 11SEC_RESERVEprobe that committing a new page viaVirtualAlloc(MEM_COMMIT)on aMapViewOfFile3title view afterSection::whole()was already mapped immediately makes that pageMEM_COMMIT | PAGE_READWRITEin the cachedwholeview without faulting.One concurrency gap in
crates/victory-gpu/src/watch.rs(write_through) should be tightened before merge:Missing
g.busytracking across the unlockedcopy_nonoverlappingwindow (watch.rs:102-111):Unlike
writing()(watch.rs:806),write_throughreleasesGUARDSaftermark_with_aliases(..., true), copiesbyteswithout(first, last)ing.busy, and then re-acquiresGUARDSformark_with_aliases(..., false). Because the pages remain ing.protectedand are not marked busy, a concurrentTracked::take()/take_split()/taken()on the page or an alias (which the doc comment onwrite_besideanticipates, and which becomes reachable alongside parallel copy-back like #2914) succeeds during the copy:Shadow::update_trackedcan read torn mid-copy bytes viacopy_mappedwhilecopy_nonoverlappingis writing the same physical page; andwatch.rs:517callsself.changed.taken(page), which dropssaved.bytes[page]on an armed tracker—breaking the invariant guarded bywatch::tests::a_busy_alias_leaves_the_page_changed_and_keeps_saved_bytes. Whenwrite_throughthen runsmark_with_aliases(..., false), the page is re-marked changed withsaved == None, causing a subsequentShadow::copy_back_markedto drop GPU writes on that page (MarkedKept).Pushing
(first, last)intog.busyunder the firstguards()lock and removing it (via a small drop guard) under the secondguards()lock closes both races cleanly with minimal overhead.Minor non-blocking polish:
crates/victory-gpu/src/watch.rs:103: handlebytes.is_empty()early and useat.saturating_add(bytes.len()).div_ceil(PAGE)to matchwriting()(watch.rs:804) and avoid marking a page on an empty unaligned write or overflowing nearusize::MAX.crates/victory-gpu/src/shadow.rs:522:writtencan beOption<&[u8]>((&*host)/title.as_slice()) directly, avoiding the raw pointer round-trip and extraunsafefrom_raw_partsblock.@Marvisak on GitHub · 2026-11-26 08:23 UTC · original
Automated triage for the maintainers.
Verdict: merge candidate. With
VICTORY_COPY_BACK_BESIDE=1(off by default),Shadow::copy_back_markedwrites marked pages into title memory through a whole-section view of our own (xmem::write_besideviawatch::write_beside/write_through). It no longer opens the guards and does a kernel write. Anything else falls back to the old path.What I checked against main:
physical.viewsentries are single runs of one section starting atslice.index, sowhole + slice.index * PAGE + (at - base)is right. Ranges past a view are refused.map→commitcommitted them.map/unmapalready use.watchdoesn't call back into xmem under the guard lock.mark_with_aliases) stands in for thebusywindow ofwriting.Section::drop.CI:
checkpassing. The source of the idea is credited (same licence, GPL-3.0-or-later), and the PR states this is a new implementation.Generated by Claude Code
@TheGrayOrange on GitHub · 2026-11-26 20:44 UTC · original
I reproduced fuzeofficlal's concurrency concern at
5454d41using a deterministic CPU-only fixture with a real 64 KiB Windows section.By pausing
write_throughhalfway through a 16-byte write, I confirmed:Tracked::take_splitcan expose eight new bytes and eight old bytes.Both the original mapping and a physical alias exhibited these failures.
Adding the proposed busy-range guard preserved the snapshot and subsequent shader write while keeping both title views read-only. However, partial reads remained possible.
The remaining issue appears to be that
take_splitconstructs its copy ranges from the original dirty-page list, independently of the busy-page acknowledgement filter.Overlapping tracked reads need synchronization from range selection through the actual copy, or a retry mechanism before committing the refreshed host copy and baseline. Filtering busy pages during range selection alone would leave the opposite arrival order exposed.
Both reproduction patches are available if useful.
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.