perf(gpu): reduce watch GUARDS lock contention during holes, protect, and kernel I/O #513

Manually merged
github merged 1 commit from github-bot/gh-513 into main 2026-12-05 17:38:30 +00:00 AGit
Owner

@⁠Not4Eric on GitHub · 2026-12-04 20:24 UTC · original
Not4Eric:perf/watch-reduce-guards-contention → main · +44 −21 · 1 files · 1 commits · merged 2026-12-05 17:29 UTC

What this does

Reduces watch::GUARDS write-lock and per-block claim contention between faulting title threads, refreshes, and kernel I/O:

  1. Uses guards_shared() instead of exclusive guards() when sampling blocks in watch::look_at_holes().
  2. Bounds protect_claimed() to chunks of PROTECT_CHUNK = 32 blocks so faulting title threads wait on at most a small chunk instead of an entire multi-megabyte run.
  3. Materializes pending unprotected lazy pages outside GUARDS (under a per-block Claims::wait_for) before taking guards_timed() in watch::reading() and watch::kernel_writing(), keeping VirtualAlloc/MapViewOfFile syscalls off the global GUARDS write lock while still re-checking under guards_timed() for any protected or racing pages.

Checks

  • N/A: pure lock-contention optimization preserving identical guard/unmap/kernel-I/O ordering semantics.
  • N/A: no switches added, changed, or removed.
  • No unrelated cleanup: diff is limited to look_at_holes, protect_claimed, reading, and kernel_writing in crates/victory-gpu/src/watch.rs.
  • N/A: existing watch::tests directly exercise reading and kernel_writing semantics; no prior behavioural bug existed for a failing-before unit test.
  • Checked open PR queue: independent of #392 (UMA pool locks — um/residency.rs is untouched here) and #428 (many-views clean bit).
  • cargo xtask policy passed (360 files ok); no fork-only files, configs, or build outputs.

Verified

  • cargo xtask policy — 360 files ok
  • PKG=victory-gpu ./build.sh test --lib -- watch::tests::a_refresh_leaves_open_what_the_kernel_is_writing watch::tests::kernel_reads_into_guarded_blocks_after_opening --test-threads=1 (Proton Wine on Linux) — 2 passed; 0 failed, 0 warnings (note: 4 unrelated watch:: tests relying on native Windows MEM_WRITE_WATCH/MEM_RESERVE_PLACEHOLDER behaviour fail under Wine)
  • Also exercised in gameplay on GTA 6 Series S (Ryzen 5 7600X, RX 9070 XT, CachyOS + Proton); not benchmarked in isolation.

Cost

Small extra VirtualProtect + elsewhere walk per 32-block chunk in protect_claimed instead of once per run (traded for much shorter GUARDS write-lock and per-block claim stalls); 0 new warnings.

AI-assisted: yes

**@⁠Not4Eric** on GitHub · 2026-12-04 20:24 UTC · [original](https://github.com/lobomfz/VIctory-xmem/pull/513) `Not4Eric:perf/watch-reduce-guards-contention` → `main` · +44 −21 · 1 files · 1 commits · merged 2026-12-05 17:29 UTC ## What this does Reduces `watch::GUARDS` write-lock and per-block claim contention between faulting title threads, refreshes, and kernel I/O: 1. Uses `guards_shared()` instead of exclusive `guards()` when sampling blocks in `watch::look_at_holes()`. 2. Bounds `protect_claimed()` to chunks of `PROTECT_CHUNK = 32` blocks so faulting title threads wait on at most a small chunk instead of an entire multi-megabyte run. 3. Materializes pending unprotected lazy pages outside `GUARDS` (under a per-block `Claims::wait_for`) before taking `guards_timed()` in `watch::reading()` and `watch::kernel_writing()`, keeping `VirtualAlloc`/`MapViewOfFile` syscalls off the global `GUARDS` write lock while still re-checking under `guards_timed()` for any protected or racing pages. ## Checks - N/A: pure lock-contention optimization preserving identical guard/unmap/kernel-I/O ordering semantics. - N/A: no switches added, changed, or removed. - [x] No unrelated cleanup: diff is limited to `look_at_holes`, `protect_claimed`, `reading`, and `kernel_writing` in `crates/victory-gpu/src/watch.rs`. - N/A: existing `watch::tests` directly exercise `reading` and `kernel_writing` semantics; no prior behavioural bug existed for a failing-before unit test. - [x] Checked open PR queue: independent of #392 (UMA pool locks — `um/residency.rs` is untouched here) and #428 (`many-views` clean bit). - [x] `cargo xtask policy` passed (`360 files ok`); no fork-only files, configs, or build outputs. ## Verified - `cargo xtask policy` — `360 files ok` - `PKG=victory-gpu ./build.sh test --lib -- watch::tests::a_refresh_leaves_open_what_the_kernel_is_writing watch::tests::kernel_reads_into_guarded_blocks_after_opening --test-threads=1` (Proton Wine on Linux) — `2 passed; 0 failed`, 0 warnings *(note: 4 unrelated `watch::` tests relying on native Windows `MEM_WRITE_WATCH`/`MEM_RESERVE_PLACEHOLDER` behaviour fail under Wine)* - Also exercised in gameplay on GTA 6 Series S (Ryzen 5 7600X, RX 9070 XT, CachyOS + Proton); not benchmarked in isolation. ## Cost Small extra `VirtualProtect` + `elsewhere` walk per 32-block chunk in `protect_claimed` instead of once per run (traded for much shorter `GUARDS` write-lock and per-block claim stalls); 0 new warnings. AI-assisted: yes
github-bot locked as GitHub sync and limited conversation to collaborators 2026-12-04 20:27:43 +00:00
Author
Owner

@⁠Kal0s-design on GitHub · 2026-12-04 21:15 UTC · original

Checked aeac5b0 on native Windows (real VirtualProtect/GetWriteWatch, didn't run the game), next to current main.

watch:: tests, 12 runs per side (6 with one test thread, 6 with two). Everything passes every time except the_rest_of_the_one_asking_has_a_number_of_its_own, which failed 11 of 12 here and 11 of 12 on main. Alone it passes 10 of 10 on both, so it's an ordering flake main already has. Full victory-gpu --lib with two threads, three runs per side, shows the same native-only failures main has (the two indirect host_list tests, the two pipeline::host library loads) plus that flake now and then. Clippy adds nothing new in watch.rs, with or without diagnostics.

I also ran a throwaway stress test, not pushed. It uses a 256-block view with 6 threads writing and asking about ranges of up to ~240 blocks, so protect_claimed runs cross several 32-block chunks. Next to them one thread loops kernel_writing() (writing into the range while it's held) and reading(), and another asks about a half-mapped placeholder and calls look_at_holes(), so held isn't empty. 15 runs per side at 4000 iterations per thread gave no lost write, no hang and no stray access violation. With look_at_holes() in a tight loop the writers finished in about 10.7 s here against 16.3 s on main. Without that thread the two are the same (about 3.6 s each). So in this test the gain comes from the shared lock in look_at_holes, which the game only calls once a frame, and the chunking itself didn't change the timing.

The lazy-page pre-pass wasn't exercised. each_pending returns at once unless the UMA lazy pool is active, so in the test reading() returns early and kernel_writing() only runs the old part. That part is from reading only.

On the code

  • look_at_holes (watch.rs:1727) only reads held (atomic bits) and FROM under the shared lock, and the removal still happens under guards() at 1748. A stale snapshot was already possible on main, so this looks fine.
  • protect_claimed (2227) lets go of claims after each chunk, the way guard_shared already does between runs. A block is still set in PROTECTED and made read-only only while its own claim is held (2400/2453 through protect_run, 2361 through cover, always given Some(claims)), and a fault waits for that claim (1551). So a fault in a later chunk finds the block either not guarded yet or fully guarded. The cost is one VirtualProtect and one elsewhere walk per 32 blocks instead of per run, which is small but not quite none.
  • The pre-pass (2608, 2640). BLOCK == PAGE == 64 KiB, so one claim covers one lazy page, and every PROTECTED.set is under the block's claim, so the check and materialize can't race a protect. watch::retire (1612) takes no claim and can land between the pre-pass and guards_timed(). A retired page is KEPT without REAL, which kind() (um/lazy.rs:244) still calls Pending, so the second pass makes it again. An unbind or release racing it goes through the same per-page lazy lock (um/lazy.rs:565, 431), so a late materialize just sees it's no longer bound. title_memory_unmapping (2856) only clears watch state, and either order is fine there.
  • Lock order. The pre-pass holds one claim and no GUARDS, then drops it before guards_timed(). Nobody holding a claim waits for GUARDS alone, and claim, then lazy page lock, then MOVING read is the order the fault path already uses (1551-1553). The fault handler itself isn't touched. The BUSY doc (1174-1176) still says claims are taken holding GUARDS shared, which these two callers no longer do.

Nothing blocking from my side.

AI-assisted: yes

**@⁠Kal0s-design** on GitHub · 2026-12-04 21:15 UTC · [original](https://github.com/lobomfz/VIctory-xmem/pull/513#issuecomment-7424449649) Checked aeac5b0 on native Windows (real VirtualProtect/GetWriteWatch, didn't run the game), next to current main. `watch::` tests, 12 runs per side (6 with one test thread, 6 with two). Everything passes every time except `the_rest_of_the_one_asking_has_a_number_of_its_own`, which failed 11 of 12 here and 11 of 12 on main. Alone it passes 10 of 10 on both, so it's an ordering flake main already has. Full `victory-gpu --lib` with two threads, three runs per side, shows the same native-only failures main has (the two `indirect` host_list tests, the two `pipeline::host` library loads) plus that flake now and then. Clippy adds nothing new in watch.rs, with or without `diagnostics`. I also ran a throwaway stress test, not pushed. It uses a 256-block view with 6 threads writing and asking about ranges of up to ~240 blocks, so `protect_claimed` runs cross several 32-block chunks. Next to them one thread loops `kernel_writing()` (writing into the range while it's held) and `reading()`, and another asks about a half-mapped placeholder and calls `look_at_holes()`, so `held` isn't empty. 15 runs per side at 4000 iterations per thread gave no lost write, no hang and no stray access violation. With `look_at_holes()` in a tight loop the writers finished in about 10.7 s here against 16.3 s on main. Without that thread the two are the same (about 3.6 s each). So in this test the gain comes from the shared lock in `look_at_holes`, which the game only calls once a frame, and the chunking itself didn't change the timing. The lazy-page pre-pass wasn't exercised. `each_pending` returns at once unless the UMA lazy pool is active, so in the test `reading()` returns early and `kernel_writing()` only runs the old part. That part is from reading only. On the code - `look_at_holes` (watch.rs:1727) only reads `held` (atomic bits) and `FROM` under the shared lock, and the removal still happens under `guards()` at 1748. A stale snapshot was already possible on main, so this looks fine. - `protect_claimed` (2227) lets go of claims after each chunk, the way `guard_shared` already does between runs. A block is still set in `PROTECTED` and made read-only only while its own claim is held (2400/2453 through `protect_run`, 2361 through `cover`, always given `Some(claims)`), and a fault waits for that claim (1551). So a fault in a later chunk finds the block either not guarded yet or fully guarded. The cost is one `VirtualProtect` and one `elsewhere` walk per 32 blocks instead of per run, which is small but not quite none. - The pre-pass (2608, 2640). `BLOCK == PAGE == 64 KiB`, so one claim covers one lazy page, and every `PROTECTED.set` is under the block's claim, so the check and `materialize` can't race a protect. `watch::retire` (1612) takes no claim and can land between the pre-pass and `guards_timed()`. A retired page is KEPT without REAL, which `kind()` (um/lazy.rs:244) still calls Pending, so the second pass makes it again. An unbind or release racing it goes through the same per-page lazy lock (um/lazy.rs:565, 431), so a late `materialize` just sees it's no longer bound. `title_memory_unmapping` (2856) only clears watch state, and either order is fine there. - Lock order. The pre-pass holds one claim and no `GUARDS`, then drops it before `guards_timed()`. Nobody holding a claim waits for `GUARDS` alone, and claim, then lazy page lock, then `MOVING` read is the order the fault path already uses (1551-1553). The fault handler itself isn't touched. The `BUSY` doc (1174-1176) still says claims are taken holding `GUARDS` shared, which these two callers no longer do. Nothing blocking from my side. AI-assisted: yes
github-bot force-pushed github-bot/gh-513 from aeac5b0931 to f768c44b12 2026-12-05 00:05:55 +00:00 Compare
Author
Owner

@⁠Not4Eric on GitHub · 2026-12-05 00:00 UTC · original

Thank you so much for helping, I'm not the best at this and I'm heavily relying on AI to even do this

Pushed updates addressing your review:

  • Updated BUSY's doc comment in watch.rs to reflect that reading and kernel_writing also claim blocks via Claims while materializing lazy pages before acquiring GUARDS.
**@⁠Not4Eric** on GitHub · 2026-12-05 00:00 UTC · [original](https://github.com/lobomfz/VIctory-xmem/pull/513#issuecomment-7427751149) Thank you so much for helping, I'm not the best at this and I'm heavily relying on AI to even do this Pushed updates addressing your review: - Updated `BUSY`'s doc comment in `watch.rs` to reflect that `reading` and `kernel_writing` also claim blocks via `Claims` while materializing lazy pages before acquiring `GUARDS`.
Author
Owner

@⁠Kal0s-design on GitHub · 2026-12-05 00:59 UTC · original

Re-checked f768c44 on native Windows (didn't run the game).

The BUSY doc at watch.rs:1174-1177 now covers reading and kernel_writing claiming blocks before GUARDS, which matches the code. Nothing else changed. The full lib run shows only the four failures main already has natively plus the ordering flake in the_rest_of_the_one_asking_has_a_number_of_its_own (it failed 2 of 3 watch:: runs here, and main has the same flake), clippy has nothing new in watch.rs, and it still merges cleanly onto current main.

Two leftovers, neither blocking. The lazy-page pre-pass still isn't covered by any test, since each_pending returns at once without the UMA lazy pool, so that part is reviewed from reading only. And "Cost: none" in the description could mention the extra VirtualProtect per 32 blocks in protect_claimed, small as it is.

AI-assisted: yes

**@⁠Kal0s-design** on GitHub · 2026-12-05 00:59 UTC · [original](https://github.com/lobomfz/VIctory-xmem/pull/513#issuecomment-7428826936) Re-checked f768c44 on native Windows (didn't run the game). The BUSY doc at watch.rs:1174-1177 now covers reading and kernel_writing claiming blocks before GUARDS, which matches the code. Nothing else changed. The full lib run shows only the four failures main already has natively plus the ordering flake in the_rest_of_the_one_asking_has_a_number_of_its_own (it failed 2 of 3 watch:: runs here, and main has the same flake), clippy has nothing new in watch.rs, and it still merges cleanly onto current main. Two leftovers, neither blocking. The lazy-page pre-pass still isn't covered by any test, since each_pending returns at once without the UMA lazy pool, so that part is reviewed from reading only. And "Cost: none" in the description could mention the extra VirtualProtect per 32 blocks in protect_claimed, small as it is. AI-assisted: yes
github manually merged commit f3d856e3b2 into main 2026-12-05 17:38:30 +00:00
This discussion has been locked. Commenting is limited to contributors.
No description provided.