Write watch: out-of-GUARDS 64-stripe VirtualProtect (VICTORY_GUARD_SHARDED, off) #434

Open
Pototoooo wants to merge 1 commit from upstream-pr/434 into main
Pototoooo commented 2026-12-02 09:35:22 +00:00 (Migrated from github.com)

Imported from readmet3xt-lab/foxhound. Originally posted by Pototoooo on 2026-12-02T09:35:22Z.


Summary

Ports out-of-GUARDS striped VirtualProtect guarding from RFC #413 (patches 01, 05 and 06, MIT-0) onto foxhound's watch.rs, behind an opt-in switch (VICTORY_GUARD_SHARDED=1, off by default):

  • Adds 64 cache-line-aligned native Windows SRWLOCK stripes (VP_STRIPE, 4 MiB / 64 blocks per stripe) strictly outside GUARDS in the lock hierarchy (VP_STRIPE[i < j] -> GUARDS). VpStripes(u64) carries only an 8-byte bitmask on the stack (zero heap allocation, safe in the vectored exception handler).
  • Splits on_write's write-fault handling into a #[inline(never)] helper so VEH entry keeps a minimal stack footprint for non-write exceptions (e.g. stack growth guard pages).
  • When VICTORY_GUARD_SHARDED=1, guard_locked drops GUARDS before calling soft_runs, VirtualProtect(PAGE_READONLY), VirtualQuery, and elsewhere, holding only the target 4 MiB stripe(s) in protect_here_striped and cover_striped and taking GUARDS only for brief metadata checks and commits.
  • Hardens concurrent races with:
    • ARMING bitmap set under GUARDS before Phase 2 VirtualProtect and cleared only after committing into g.protected or completing reopen_striped_block (so title_memory_written's VICTORY_LOCK_TRIM fast path, writing, views_mapped, open_page, put_beside, and the VEH handler never observe a half-armed block).
    • Per-slot monotonic invalidation epochs (Guards::epochs, 4096 slots) bumped on every open (including open_one_view), unguard, settle, views_mapped, title_memory_splitting, title_memory_unmapping, title_memory_changed, and title_memory_freed, preventing ABA races between out-of-lock VirtualProtect/elsewhere phases and Phase-3 commit / cover_striped clean marking.
    • Live-view verification (g.aliased.contains(ob) && !inplace::pages_of(ob * BLOCK).is_empty()) under VP_STRIPE[ob] + GUARDS before alias VirtualProtect in cover_striped, serializing against title_memory_unmapping so an unmapped view whose VA was reused by the allocator can never be made read-only.
    • reopen_striped_block under VP_STRIPE (outside GUARDS) when a concurrent open, fault, partial-page open, or remap invalidates a Phase-3 commit, preventing orphaned read-only pages.
    • Full compatibility with foxhound's VICTORY_GUARD_PAGES (PARTLY / close_pages / forget_pages), VICTORY_FAULT_UNLOCKED (OPENING / settle clearing PROTECTED unconditionally), VICTORY_SOFT_VIEWS, and VICTORY_GUARD_BATCH (guard_ahead).
  • When VICTORY_GUARD_SHARDED is unset (the default), guard_sharded() is false, lock_vp_range/lock_vp_all are no-ops, and guard_locked runs the existing path unchanged.

Verification

  • New unit tests in watch::guard_striped_tests:
    • striped_protect_reopens_block_when_open_races_phase2 (forced interleaving via pre-VirtualProtect hook confirming ARMING is set, stale epoch is rejected in Phase 3, PAGE_READWRITE is restored via VirtualQuery + direct write, and PROTECTED/ARMING are cleared)
    • striped_guard_tracks_writes_and_aliases (two mapped views of one section guarded with striped mode, alias write detection, title_memory_written, and unmapping cleanup)
  • All 61 watch:: tests pass under:
    • VICTORY_GUARD_SHARDED=0 (default)
    • VICTORY_GUARD_SHARDED=1
    • VICTORY_GUARD_SHARDED=1 VICTORY_GUARD_PAGES=1
  • Concurrency stress testing under VICTORY_GUARD_SHARDED=1:
    • no_write_to_views_is_lost_with_batched_guards: 850+ consecutive runs passed (0 failures, 0 deadlocks, 0 stack overflows)
    • no_write_is_lost_when_faults_open_one_page: 50/50 consecutive runs passed
  • cargo clippy --workspace --all-targets --locked -- -D warnings: clean
  • cargo clippy --workspace --all-targets --all-features --locked -- -D warnings: clean
  • cargo test -p victory-gpu -p victory-hle -p victory-core -p victory-pipeline -p victory-loader --locked --lib: all passing

After procotos-art's RFC #413 patches 01, 05 and 06 (MIT-0), adapted to foxhound's PARTLY / guard_ahead / VICTORY_LOCK_TRIM watch code.

<!-- greyfinch-sync:readmet3xt-lab/foxhound:issue:5603727607 --> Imported from [readmet3xt-lab/foxhound](https://github.com/readmet3xt-lab/foxhound/pull/434). Originally posted by [Pototoooo](https://github.com/Pototoooo) on 2026-12-02T09:35:22Z. --- ## Summary Ports out-of-`GUARDS` striped `VirtualProtect` guarding from RFC #413 (patches 01, 05 and 06, MIT-0) onto foxhound's `watch.rs`, behind an opt-in switch (`VICTORY_GUARD_SHARDED=1`, **off by default**): - Adds 64 cache-line-aligned native Windows `SRWLOCK` stripes (`VP_STRIPE`, 4 MiB / 64 blocks per stripe) strictly outside `GUARDS` in the lock hierarchy (`VP_STRIPE[i < j]` -> `GUARDS`). `VpStripes(u64)` carries only an 8-byte bitmask on the stack (zero heap allocation, safe in the vectored exception handler). - Splits `on_write`'s write-fault handling into a `#[inline(never)]` helper so VEH entry keeps a minimal stack footprint for non-write exceptions (e.g. stack growth guard pages). - When `VICTORY_GUARD_SHARDED=1`, `guard_locked` drops `GUARDS` before calling `soft_runs`, `VirtualProtect(PAGE_READONLY)`, `VirtualQuery`, and `elsewhere`, holding only the target 4 MiB stripe(s) in `protect_here_striped` and `cover_striped` and taking `GUARDS` only for brief metadata checks and commits. - Hardens concurrent races with: - `ARMING` bitmap set under `GUARDS` before Phase 2 `VirtualProtect` and cleared only after committing into `g.protected` or completing `reopen_striped_block` (so `title_memory_written`'s `VICTORY_LOCK_TRIM` fast path, `writing`, `views_mapped`, `open_page`, `put_beside`, and the VEH handler never observe a half-armed block). - Per-slot monotonic invalidation epochs (`Guards::epochs`, 4096 slots) bumped on every `open` (including `open_one_view`), `unguard`, `settle`, `views_mapped`, `title_memory_splitting`, `title_memory_unmapping`, `title_memory_changed`, and `title_memory_freed`, preventing ABA races between out-of-lock `VirtualProtect`/`elsewhere` phases and Phase-3 commit / `cover_striped` clean marking. - Live-view verification (`g.aliased.contains(ob) && !inplace::pages_of(ob * BLOCK).is_empty()`) under `VP_STRIPE[ob]` + `GUARDS` before alias `VirtualProtect` in `cover_striped`, serializing against `title_memory_unmapping` so an unmapped view whose VA was reused by the allocator can never be made read-only. - `reopen_striped_block` under `VP_STRIPE` (outside `GUARDS`) when a concurrent open, fault, partial-page open, or remap invalidates a Phase-3 commit, preventing orphaned read-only pages. - Full compatibility with foxhound's `VICTORY_GUARD_PAGES` (`PARTLY` / `close_pages` / `forget_pages`), `VICTORY_FAULT_UNLOCKED` (`OPENING` / `settle` clearing `PROTECTED` unconditionally), `VICTORY_SOFT_VIEWS`, and `VICTORY_GUARD_BATCH` (`guard_ahead`). - When `VICTORY_GUARD_SHARDED` is unset (the default), `guard_sharded()` is `false`, `lock_vp_range`/`lock_vp_all` are no-ops, and `guard_locked` runs the existing path unchanged. ## Verification - New unit tests in `watch::guard_striped_tests`: - `striped_protect_reopens_block_when_open_races_phase2` (forced interleaving via pre-`VirtualProtect` hook confirming `ARMING` is set, stale epoch is rejected in Phase 3, `PAGE_READWRITE` is restored via `VirtualQuery` + direct write, and `PROTECTED`/`ARMING` are cleared) - `striped_guard_tracks_writes_and_aliases` (two mapped views of one section guarded with striped mode, alias write detection, `title_memory_written`, and unmapping cleanup) - All 61 `watch::` tests pass under: - `VICTORY_GUARD_SHARDED=0` (default) - `VICTORY_GUARD_SHARDED=1` - `VICTORY_GUARD_SHARDED=1 VICTORY_GUARD_PAGES=1` - Concurrency stress testing under `VICTORY_GUARD_SHARDED=1`: - `no_write_to_views_is_lost_with_batched_guards`: 850+ consecutive runs passed (0 failures, 0 deadlocks, 0 stack overflows) - `no_write_is_lost_when_faults_open_one_page`: 50/50 consecutive runs passed - `cargo clippy --workspace --all-targets --locked -- -D warnings`: clean - `cargo clippy --workspace --all-targets --all-features --locked -- -D warnings`: clean - `cargo test -p victory-gpu -p victory-hle -p victory-core -p victory-pipeline -p victory-loader --locked --lib`: all passing After procotos-art's RFC #413 patches 01, 05 and 06 (MIT-0), adapted to foxhound's `PARTLY` / `guard_ahead` / `VICTORY_LOCK_TRIM` watch code.
GValdezN commented 2026-12-02 16:51:02 +00:00 (Migrated from github.com)

Imported from readmet3xt-lab/foxhound. Originally posted by GValdezN on 2026-12-02T16:51:02Z.


Impressive scope — the stripe lock design moving VirtualProtect outside GUARDS should significantly reduce contention on the guard path. The epoch-based skip in bury is a nice touch.

Interaction with our work: Our #478 batches contiguous blocks in unguard() into single VirtualProtect calls. Both PRs touch unguard() — ours reduces syscall count, yours reduces lock hold time. They're complementary but will need merge reconciliation. Our journal sharding (16 RwLock shards in JOURNAL) operates on a completely different lock and doesn't conflict with the VP_STRIPE lock order.

Questions for the author:

  1. When a batched VirtualProtect range spans two stripes, which stripe lock is held? Or does the batching only happen within a stripe?
  2. The reopen_striped_block recovery path — is this the same race that #457 fixes, or a different window?
<!-- greyfinch-sync:readmet3xt-lab/foxhound:comment:7352545766 --> Imported from [readmet3xt-lab/foxhound](https://github.com/readmet3xt-lab/foxhound/pull/434#issuecomment-7352545766). Originally posted by [GValdezN](https://github.com/GValdezN) on 2026-12-02T16:51:02Z. --- Impressive scope — the stripe lock design moving VirtualProtect outside GUARDS should significantly reduce contention on the guard path. The epoch-based skip in bury is a nice touch. **Interaction with our work:** Our #478 batches contiguous blocks in `unguard()` into single VirtualProtect calls. Both PRs touch `unguard()` — ours reduces syscall count, yours reduces lock hold time. They're complementary but will need merge reconciliation. Our journal sharding (16 RwLock shards in JOURNAL) operates on a completely different lock and doesn't conflict with the VP_STRIPE lock order. Questions for the author: 1. When a batched VirtualProtect range spans two stripes, which stripe lock is held? Or does the batching only happen within a stripe? 2. The `reopen_striped_block` recovery path — is this the same race that #457 fixes, or a different window?
tillo88 commented 2026-12-02 21:05:55 +00:00 (Migrated from github.com)

Imported from readmet3xt-lab/foxhound. Originally posted by tillo88 on 2026-12-02T21:05:55Z.


Replay-bench measurement: no measurable difference on v0.8.3.3

Measured: PR head c0ab945 merged onto release v0.8.3.3 (same code as main 7dbcdcb), with VICTORY_GUARD_SHARDED=1, 2 measured runs (after a warm-up run that is not counted), against 3 runs of v0.8.3.3 without the PR, same route (~6.5 min).

run fps CPU prep ms median ms p95 ms p99 ms frames > 100 ms
release run 1 36.54 19.08 29.00 50.42 60.01 32
release run 2 36.45 20.15 28.10 38.59 45.48 13
release run 3 36.15 19.30 29.35 38.74 46.08 10
PR #434 + VICTORY_GUARD_SHARDED=1 run 1 35.84 20.38 28.90 39.61 46.89 14
PR #434 + VICTORY_GUARD_SHARDED=1 run 2 36.07 20.08 28.80 39.61 47.38 21

Mean fps: release 36.38, PR 35.95 (-1.2%). That is below the 2-3% we treat as a real change, so: no measurable difference on this route. Only 3 release runs so far, so the release's own spread is not established yet.

Crashes: none in the PR runs and none in the release runs (no host GPU failure, no device lost).

Note: two earlier release runs the same evening, with identical switches, were much slower for most of the route (6-9 fps average); the cause is not known yet and they are left out of the comparison above. No PR run fell into that state.

Config: RTX 5070 Ti 16 GB, Ryzen 7 9800X3D, 32 GB, Windows 11; default play-foxhound.ps1 settings (Series X profile, 1440p, DLSS balanced, no frame generation), fullscreen, no frame cap.

Measured unattended on a replay bench (same save, recorded controller input); numbers and text prepared with Claude.

<!-- greyfinch-sync:readmet3xt-lab/foxhound:comment:7358786988 --> Imported from [readmet3xt-lab/foxhound](https://github.com/readmet3xt-lab/foxhound/pull/434#issuecomment-7358786988). Originally posted by [tillo88](https://github.com/tillo88) on 2026-12-02T21:05:55Z. --- **Replay-bench measurement: no measurable difference on v0.8.3.3** Measured: PR head `c0ab945` merged onto release **v0.8.3.3** (same code as main `7dbcdcb`), with `VICTORY_GUARD_SHARDED=1`, 2 measured runs (after a warm-up run that is not counted), against 3 runs of v0.8.3.3 without the PR, same route (~6.5 min). | run | fps | CPU prep ms | median ms | p95 ms | p99 ms | frames > 100 ms | |---|---|---|---|---|---|---| | release run 1 | 36.54 | 19.08 | 29.00 | 50.42 | 60.01 | 32 | | release run 2 | 36.45 | 20.15 | 28.10 | 38.59 | 45.48 | 13 | | release run 3 | 36.15 | 19.30 | 29.35 | 38.74 | 46.08 | 10 | | PR #434 + VICTORY_GUARD_SHARDED=1 run 1 | 35.84 | 20.38 | 28.90 | 39.61 | 46.89 | 14 | | PR #434 + VICTORY_GUARD_SHARDED=1 run 2 | 36.07 | 20.08 | 28.80 | 39.61 | 47.38 | 21 | Mean fps: release 36.38, PR 35.95 (-1.2%). That is below the 2-3% we treat as a real change, so: **no measurable difference** on this route. Only 3 release runs so far, so the release's own spread is not established yet. Crashes: none in the PR runs and none in the release runs (no host GPU failure, no device lost). Note: two earlier release runs the same evening, with identical switches, were much slower for most of the route (6-9 fps average); the cause is not known yet and they are left out of the comparison above. No PR run fell into that state. Config: RTX 5070 Ti 16 GB, Ryzen 7 9800X3D, 32 GB, Windows 11; default `play-foxhound.ps1` settings (Series X profile, 1440p, DLSS balanced, no frame generation), fullscreen, no frame cap. Measured unattended on a replay bench (same save, recorded controller input); numbers and text prepared with Claude.
Pototoooo commented 2026-12-03 03:09:36 +00:00 (Migrated from github.com)

Imported from readmet3xt-lab/foxhound. Originally posted by Pototoooo on 2026-12-03T03:09:36Z.


Thanks for the review and the notes on #478 / #457, @GValdezN!

  1. Cross-stripe batched VirtualProtect: Batching is not restricted to a single stripe. protect_here_striped() calls lock_vp_range(from, to) up front for the entire contiguous run [from, to), computes the 64-bit stripe bitmask (4 MiB per stripe), and acquires all spanned VP_STRIPE[idx] locks in strict ascending index order (i < j). So a batched VirtualProtect in Phase 2 holds every stripe lock covering that range. As for reconciling with #478, this PR only adds g.bump_epoch(block) inside unguard(), so your contiguous VirtualProtect batching there should compose cleanly.
  2. reopen_striped_block vs #457: On the VICTORY_GUARD_SHARDED=1 path, yes — it covers the same protect vs open_unlocked (VICTORY_FAULT_UNLOCKED=1) race window as #457, plus the wider windows opened by releasing GUARDS during Phase 2 (such as concurrent open_page/PARTLY, settle(), or epoch-bumping opens/remaps). If Phase 3 (commit_striped_block) detects any of those under GUARDS, it rejects commit and reopen_striped_block() restores PAGE_READWRITE and clears PROTECTED and ARMING while still holding the stripe lock(s). The default VICTORY_GUARD_SHARDED=0 path (protect_here/protect_alias/open_unlocked) is untouched here, so #457 remains complementary for the default path (and for not pushing to OPENED when open_unlocked's VirtualProtect fails).

@tillo88 Thanks for running the replay bench and confirming zero crashes on v0.8.3.3! That's very useful data — on this route/CPU, GUARDS contention during VirtualProtect doesn't look like the primary bottleneck, and the conservative lock_vp_all() currently taken on open() paths (because open() can also unguard aliases at arbitrary addresses) adds a small fixed cost under low contention. Good confirmation that keeping VICTORY_GUARD_SHARDED opt-in (off by default) is the right call unless we narrow the non-aliased open() locking or see a workload where GUARDS wait time dominates.

<!-- greyfinch-sync:readmet3xt-lab/foxhound:comment:7366264978 --> Imported from [readmet3xt-lab/foxhound](https://github.com/readmet3xt-lab/foxhound/pull/434#issuecomment-7366264978). Originally posted by [Pototoooo](https://github.com/Pototoooo) on 2026-12-03T03:09:36Z. --- Thanks for the review and the notes on #478 / #457, @GValdezN! 1. **Cross-stripe batched `VirtualProtect`:** Batching is not restricted to a single stripe. `protect_here_striped()` calls `lock_vp_range(from, to)` up front for the entire contiguous run `[from, to)`, computes the 64-bit stripe bitmask (4 MiB per stripe), and acquires all spanned `VP_STRIPE[idx]` locks in strict ascending index order (`i < j`). So a batched `VirtualProtect` in Phase 2 holds every stripe lock covering that range. As for reconciling with #478, this PR only adds `g.bump_epoch(block)` inside `unguard()`, so your contiguous `VirtualProtect` batching there should compose cleanly. 2. **`reopen_striped_block` vs #457:** On the `VICTORY_GUARD_SHARDED=1` path, yes — it covers the same `protect` vs `open_unlocked` (`VICTORY_FAULT_UNLOCKED=1`) race window as #457, plus the wider windows opened by releasing `GUARDS` during Phase 2 (such as concurrent `open_page`/`PARTLY`, `settle()`, or epoch-bumping opens/remaps). If Phase 3 (`commit_striped_block`) detects any of those under `GUARDS`, it rejects commit and `reopen_striped_block()` restores `PAGE_READWRITE` and clears `PROTECTED` and `ARMING` while still holding the stripe lock(s). The default `VICTORY_GUARD_SHARDED=0` path (`protect_here`/`protect_alias`/`open_unlocked`) is untouched here, so #457 remains complementary for the default path (and for not pushing to `OPENED` when `open_unlocked`'s `VirtualProtect` fails). --- @tillo88 Thanks for running the replay bench and confirming zero crashes on v0.8.3.3! That's very useful data — on this route/CPU, `GUARDS` contention during `VirtualProtect` doesn't look like the primary bottleneck, and the conservative `lock_vp_all()` currently taken on `open()` paths (because `open()` can also unguard aliases at arbitrary addresses) adds a small fixed cost under low contention. Good confirmation that keeping `VICTORY_GUARD_SHARDED` opt-in (off by default) is the right call unless we narrow the non-aliased `open()` locking or see a workload where `GUARDS` wait time dominates.
This pull request cannot be merged automatically due to conflicts.
Merge manually to resolve the conflicts.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin 'upstream-pr/434:upstream-pr/434'
git switch 'upstream-pr/434'

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.

git switch 'main'
git merge --no-ff 'upstream-pr/434'
git switch 'upstream-pr/434'
git rebase 'main'
git switch 'main'
git merge --ff-only 'upstream-pr/434'
git switch 'upstream-pr/434'
git rebase 'main'
git switch 'main'
git merge --no-ff 'upstream-pr/434'
git switch 'main'
git merge --squash 'upstream-pr/434'
git switch 'main'
git merge --ff-only 'upstream-pr/434'
git switch 'main'
git merge 'upstream-pr/434'
git push origin 'main'
Sign in to join this conversation.
No reviewers
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
readmet3xt-lab/foxhound!434
No description provided.