Repository navigation
GPU: open guarded pages in runs, not one VirtualProtect at a time - #2849
Therecalloftheonion wants to merge 2 commits into
Conversation
A bulk title write and a dropped tracker both make guarded pages writable again: changed_here opened each protected page of a range with its own VirtualProtect, and Tracked::drop opened each freed page the same way. The pages are sorted in both callers, so open_pages now groups consecutive guarded pages and open_range makes one VirtualProtect call for the run. The guard state, the pages opened and the changed marks are the same; only runs that are wholly guarded are opened, and pages not guarded are skipped as before. Benchmark (512 pages, 20 rounds, release, RX 6700 XT): one call per page 10,240 VirtualProtect calls in 4.27 ms; one call per run 20 calls in 1.79 ms, a 2.4x reduction in the open step. This is not a game-FPS measurement. Tests: a_bulk_write_opens_guarded_pages_in_one_call (8-page run, one call, every page read-write again); the watch suite passes (16 tests); the GPU suite passes apart from two unrelated upstream failures on this machine. A test-only switch runs the old per-page path in benchmark_guard_open_runs for the before/after numbers.
|
Tested Bug:
|
kotekompotek
left a comment
There was a problem hiding this comment.
Correct batching: the debug_assert that a run is fully guarded is exactly the safety property needed — one VirtualProtect over a partial run would silently change protection of pages that aren't ours or that another tracker watches. Collecting to_open first keeps the same semantics as the per-page loop. LGTM.
|
Thanks @fuzeofficlal. Confirmed and accepted: I also see #2848 item 6 implements the same open-runs optimization. This PR is a duplicate of that item, so I am closing it rather than carrying a second copy. The failure and the fallback belong on #2848; I have left the detail there. No credit claimed here. |
VirtualProtect returns 0 with ERROR_INVALID_ADDRESS (487) when a run crosses two separate VirtualAlloc regions or an uncommitted page, even when every page is guarded and ours. open_range removed the pages from the guard set first and ignored the BOOL, so on failure they stayed read-only but unguarded; the next write faulted and could access-violate. Check the batch result and, when it fails, restore the run one page at a time as the old path did. The pages were already taken out of the guard set; the fallback opens each of them, and a page that is gone fails harmlessly as before. Adds a regression test with two adjacent pages allocated separately: both are guarded, the batch is attempted, and both must end up PAGE_READWRITE with the guard set empty. The benchmark is unchanged when the batch succeeds (4.13 ms against 1.61 ms over 20 rounds of 512 pages).
|
Reopened with the fix for the
Added Benchmark unchanged when the batch succeeds: 512 pages, 20 rounds, release: 4.13 ms (10,240 calls) vs 1.61 ms (20 calls). The fix adds no work to the success path. Still overlaps #2848 item 6 ( |
|
Hardware result at head 7be4f20, cherry-picked onto a fork stack that opens a changed page's aliases too, RTX 3080 Ti, driver 621.24.
Ran with AI assistance (Claude Code). |
|
Measured on a Ryzen 7 5700G (16 threads) + RTX 3060, Windows 11, release, against their base
With writers pausing 50 µs between writes the picture is the same. Under contention the time is waiting for the guard lock, not the open calls, so batching them does not show here. Even with one writer, an 8-page AI use: Claude Code ran the builds and benchmarks on this machine and drafted this comment. |
After a submission, each 64 KiB page shaders marked as written through views was compared and, if it differed, written back on its own: one hold of the guard lock, the page opened, written and guarded again. In the prison scene that is about 1,950 pages a frame and 61-62 ms, 45-48 ms of it holding the guard lock that title threads' write faults wait for. The pages come in order, so `copy_back_marks` hands each copy its runs of consecutive pages, and `Shadow::copy_back_marked` compares them page by page as before but writes the whole pages that differ, next to each other, in one go: the copy holds the bytes of all of them (what goes back is its bytes, or the merge `merge_writeback` leaves in both). `write_back_pages` does for a run what `write_back_page` did for a page, under one hold of the lock, opening (upstream PR Marvisak#2849) and guarding in runs. A page the title wrote unarmed, one equal to title memory or a part page ends a run and is handled as before. Measured against the same build without it (two runs each): about 185 writes a frame instead of about 1,940, 44-47 ms instead of 61-62, and the lock held 33 ms instead of 45-48 ms there; frame times within the noise. Tests: a run with a page equal to title memory between written ones, two armed pages merged and written as one run, and a two-page write-back taken here and changed for another tracker.
… in runs) and the view tree Rows for the three GPU commits and the diagnostics with what they measured; the views section notes that our Proton build's wineserver finds views in a tree.
|
Automated triage for the maintainers. Verdict: needs work (small). The batching in
For the maintainers: #2848 (item 6) makes the same batching change in CI: passing ( Generated by Claude Code |
protect_here and protect_alias made PAGE_READONLY over a whole 64 KiB block,
or over a whole range, after looking at the first page's old protection
only. A PAGE_GUARD page the title set further on in that block lost its guard
with no fault and no write seen. Now a change of protection covers only
blocks that Windows says are alike (committed, one region, read-only or
read-write throughout). A block that is not gets no call and stays as it is,
with VICTORY_GUARD_RUNS on or off.
Runs (VICTORY_GUARD_RUNS, on; =0 keeps one call per block where a range is
not one alike region):
- unguard: blocks that follow one another in one view are opened with one
call.
- protect_here: one call per run of alike blocks within each view the runtime
told of. A call across two views is refused, so runs are cut there.
- cover: the other views of a run of guarded pages are made read-only with
one call per run (protect_alias_runs); protect_alias takes what is left.
With =0 a range is asked for whole only when it is one alike region, so the
call across views that was refused is no longer made.
Counter on victory_gpu::host (diagnostics builds), on the 5 s lines: calls and
blocks per frame for giving write access back and for making read-only, and
the refused ones ("the guards' changes of protection per frame").
Tests (watch::tests): a_write_over_guarded_blocks_opens_them_with_one_call,
a_write_inside_guarded_blocks_opens_its_own_and_no_others,
runs_of_guarded_blocks_are_cut_where_views_meet,
other_memory_inside_a_run_keeps_its_protection,
the_other_views_of_a_guarded_run_are_guarded_with_one_call,
a_page_the_title_guards_inside_a_range_keeps_its_guard,
a_page_the_title_guards_in_another_view_keeps_its_guard.
Credits: Marvisak/VIctory PR Marvisak#2849 (Therecalloftheonion) for the idea of one
change of protection per run of guarded pages; PR Marvisak#2848 item 6 (pes7) for
giving guarded pages back in runs. As we read them, their runs ignore view
boundaries and the call's result, so their code does not fit this tree; this
is a new implementation for it.
(cherry picked from commit 8b8da9b06fe3a252184876d6e84f7f3d8238a93f)
…BVH hot-path & XDXR BOX16 fix - XMA2: atomic CAS in XmaContextView::set_field (Marvisak/VIctory#1537, GPL-3.0), PacketSkipCount==0xFF EOS (Zettrox/ztrx#152, GPL-3.0), static precomputed IMDCT twiddle LUTs + stack buffers (Belobug/VIctory-Belobug-AMD#35, GPL-3.0) - BCrypt: accept BCRYPT_RNG_ALG_HANDLE (0x81) pseudo-handle in gen_random and reject in close_algorithm_provider (ubiquitarianism/leonida#2132, GPL-3.0) - XMem: batch contiguous VirtualProtect guard-page runs (Marvisak/VIctory#2849, GPL-3.0), cache/reuse placeholder prefix bounds (JMAN730/project-flamingo#777, GPL-3.0), guard write-tracking against MEM_FREE/MEM_IMAGE - Xbox BVH builder: ScopedWaitLabel stall attribution (xbox_bvh_build/xbox_bvh_rehydrate), ReadableRegionCache + per-call BLAS header cache for TLAS, single-pass active partition, bulk array VA resolution - XDXR: fix BOX16 sign-bit extraction in xdxr_build.comp (ubiquitarianism/leonida#1210, GPL-3.0) and recompile xdxr_build.inc via glslc + spirv-val
|
Verified Tests & Microbenchmark
Live Title Verification & Integration Note
Hardware runs were performed by me on my 7950X / RX 9070 XT; comment drafted with Antigravity. |
|
On "the gain in the title is unproven": here is an in-game A/B from Linux, where every
No crash or change in behaviour in those runs, and the crate's tests pass under Wine, the fallback test included AI-assisted (Claude Code). No game data attached. |
After a submission, each 64 KiB page shaders marked as written through views was compared and, if it differed, written back on its own: one hold of the guard lock, the page opened, written and guarded again. In the prison scene that is about 1,950 pages a frame and 61-62 ms, 45-48 ms of it holding the guard lock that title threads' write faults wait for. The pages come in order, so `copy_back_marks` hands each copy its runs of consecutive pages, and `Shadow::copy_back_marked` compares them page by page as before but writes the whole pages that differ, next to each other, in one go: the copy holds the bytes of all of them (what goes back is its bytes, or the merge `merge_writeback` leaves in both). `write_back_pages` does for a run what `write_back_page` did for a page, under one hold of the lock, opening (upstream PR Marvisak#2849) and guarding in runs. A page the title wrote unarmed, one equal to title memory or a part page ends a run and is handled as before. Measured against the same build without it (two runs each): about 185 writes a frame instead of about 1,940, 44-47 ms instead of 61-62, and the lock held 33 ms instead of 45-48 ms there; frame times within the noise. Tests: a run with a page equal to title memory between written ones, two armed pages merged and written as one run, and a two-page write-back taken here and changed for another tracker.
… in runs) and the view tree Rows for the three GPU commits and the diagnostics with what they measured; the views section notes that our Proton build's wineserver finds views in a tree.
Problem
Issue #400 puts about 600 ms a frame in the guard lock on an RTX 2080, with ~3,800
VirtualProtectcalls a frame. Two hot paths opened guarded pages one call at a time:changed_here(a title write over a range, e.g. amemsetor a file read) collected the protected pages of the range and calledVirtualProtectper page.Tracked::dropdid the same for every page freed when the last tracker let go of it.The pages are already in sorted order in both callers.
Change
open_pagesgroups consecutive guarded pages andopen_rangemakes oneVirtualProtectcall per run. Only runs where every page is guarded are opened; unguarded pages are skipped exactly as before, and the guard set, changed marks and saved snapshots are unchanged.This is not #1884: that sorts/deduplicates alias pages and replaces active-writer scans; this batches the write-access restore calls themselves. It does not touch the copy path or the listed copy PRs.
Benchmark
cargo test --release -p victory-gpu benchmark_guard_open_runs -- --ignored --test-threads=1 --nocapture, 512 pages, 20 rounds, RX 6700 XT, release:The test-only switch runs both paths on the same protected pages, so this is a direct before/after. It measures the write-access restore step only, not game FPS; no title data was available here.
Tests
watch::tests::a_bulk_write_opens_guarded_pages_in_one_call: an 8-page write opens the run with one call and every page is read-write again.cargo test -p victory-gpu --lib: 227 passed, 2 ignored. Two unrelated upstream tests are filtered because they fail onmainon this machine (the 2^21 view-heap assertion and the copy-pair placement test).cargo clippy -p victory-gpu --all-targets -- -D warnings: clean.rustfmt --checkon the file reports pre-existing differences (first at line 100, unrelated); the new code follows the surrounding style.Regressions
open_pagesrelies on the sorted page lists both callers already provide; a future unsorted caller would lose batching but not correctness (unguarded pages are skipped).All Windows D3D12 vendors benefit equally; it is host CPU/kernel work, not GPU-specific.