GPU, HLE: marked pages go back beside the guards (VICTORY_COPY_BACK_BESIDE, opt-in) #3067
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/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?
Imported from Marvisak/VIctory. Originally posted by moe-miau on 2026-11-26T06:17:33Z.
What
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
Imported from Marvisak/VIctory. Originally posted by Dukestar69 on 2026-11-26T06:28:23Z.
Review: APPROVED
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).
Imported from Marvisak/VIctory. Originally posted by kotekompotek on 2026-11-26T06:35:17Z.
Review: APPROVED
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.
Imported from Marvisak/VIctory. Originally posted by fuzeofficlal on 2026-11-26T08:00:06Z.
Review: COMMENTED
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.Imported from Marvisak/VIctory. Originally posted by Marvisak on 2026-11-26T08:23:19Z.
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
Imported from Marvisak/VIctory. Originally posted by TheGrayOrange on 2026-11-26T20:44:36Z.
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.
Imported from Marvisak/VIctory. Originally posted by moe-miau on 2026-11-27T02:34:55Z.
@TheGrayOrange thanks, confirmed. Fixed in
a18f50b(force-pushed: the first push of it carried an unrelated reformat by mistake): from the first mark to the second,write_throughputs its pages and their aliases inbusy, the same aswritingdoes. A tracker that takes them meanwhile leaves them changed, keeps its saved bytes and doesn't guard them again. New testa_take_during_a_write_beside_leaves_the_pages_changed: half-writes, takes on both the mapping and its alias, then checks both stay changed and the saved bytes are the ones from before the write. It fails without the guard and passes with it.You're right that
take_splitstill copies a busy page mid-write. The oldwriting+ kernel-write path does the same, because its pages are open while the kernel writes. In both, the page stays changed and is copied again on the next take, but one copy can still be torn. Closing that needs the take held from range selection through the copy (or a retry) for both paths, so I'd do it as a separate PR rather than grow this one. Happy to look at your reproduction patches for that.Imported from Marvisak/VIctory. Originally posted by TheGrayOrange on 2026-11-28T00:33:04Z.
Independently verified "
a18f50b" using our original synthetic regression fixtures.All four focused CPU-only checks passed. The saved snapshot survived through both the original mapping and its alias, and subsequent copy-back preserved the beside-written bytes and delivered the simulated shader write.
The inherited partial-read limitation remains reproducible, as expected, but it no longer causes the snapshot loss identified in our original finding.
Both reproduction patches have been checked against their documented revisions and are attached for reference.
Thanks for addressing this.
Imported from Marvisak/VIctory. Originally posted by akimfomin on 2026-11-28T00:56:20Z.
PortBridge combined
a18f50bwith #3246c2e4507at common base63bcca4. Git produced tree2f379ab7b466cf425c913a525610c70cbab453bcwith no conflicts. The recent four CPU checks cover #3069 by itself. An extra correctness check would run #3246’s partial-page-after-whole-page fixture on the pair, also enabling the beside-guards route if that fixture reaches it. Continued #2517 adds a complementary range control: a bridge must preserve its prefix and must not infer an uncovered byte was written. Pinned test request for the two heads.Exact comparisons and controls. If this workflow is useful, your team or agent can send the next public PR URL, target SHA, and behavior to preserve via PortBridge #98. No install is needed.
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.