GPU: remember which title memory is view memory until it is remapped #1277

Closed
TG0000 wants to merge 1 commit from upstream-pr/1277 into main
TG0000 commented 2026-11-24 17:02:43 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by TG0000 on 2026-11-24T17:02:43Z.


Why

The write guards ask VirtualQuery whether a range is view memory at each check, about 900 times in a world frame. In the title's big heap each call walks the page tables.

What

  • Blocks known to be view memory are remembered until they are remapped. Memory that is not a view is asked about each time, as before.
  • The query runs without the guards' lock, which title threads take at their writes to guarded blocks. An answer from before a remapping is not remembered.
  • The runtime reported an unmapping before doing it, so a check in between would have remembered a view that was going away (and left its block guarded). It now reports it after.

Measured

Time of a looped world frame's submissions (preparation, GPU, copy-back), all ways in one launch, RX 6800 on Windows; 8 frames, each repeated with every way. This is not frame time, and nothing here is an fps claim. One launch, one area, in a build of main with measuring tools and other local changes, the same in every row.

submissions, ms per frame (about 285 of it GPU in every row)
this change and four other copy changes of mine (#1267 and others, not all open) 551
the same four without this change +98 (62 to 171) against the row above

Medians of the paired differences with the range over the 8 frames; a control (same switches, another order) differed by 9 ms. A build that also asked each time found no wrong answer in 23,140 checks in the world.

Tests

  • remapped_memory_is_asked_about_again, a_check_during_the_unmapping_leaves_nothing_behind (fails when the report comes before the unmapping). cargo test --workspace --locked (344 passed), clippy with -D warnings with and without --all-features on Windows, clippy on macOS: pass at 6739366.
  • One recorded frame (24 submissions with shader writes) replayed with this and without: title memory and the copies are byte for byte equal. This exercises the copies, not remapping during a frame.

Overlap and conflicts

  • #1025 changes kernel_will_write and watch.rs, #1122 the guards of all views of physical pages and the unmapping in xmem.rs: both conflict here. If either goes in first I rebase.
  • #943, #991, #1010, #1024, #1124, #1215 and #1267 merge cleanly.

Not verified

  • NVIDIA and Intel GPUs; Linux and Proton.
  • Frame time and fps in the game.
  • A remapping while the world streams, beyond the tests above and the 23,140 checks.

AI-assisted (Claude Code).

🤖 Generated with Claude Code

<!-- greyfinch-sync:Marvisak/VIctory:issue:6587145333 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/1277). Originally posted by [TG0000](https://github.com/TG0000) on 2026-11-24T17:02:43Z. --- ## Why The write guards ask `VirtualQuery` whether a range is view memory at each check, about 900 times in a world frame. In the title's big heap each call walks the page tables. ## What - Blocks known to be view memory are remembered until they are remapped. Memory that is not a view is asked about each time, as before. - The query runs without the guards' lock, which title threads take at their writes to guarded blocks. An answer from before a remapping is not remembered. - The runtime reported an unmapping before doing it, so a check in between would have remembered a view that was going away (and left its block guarded). It now reports it after. ## Measured Time of a looped world frame's submissions (preparation, GPU, copy-back), all ways in one launch, RX 6800 on Windows; 8 frames, each repeated with every way. **This is not frame time, and nothing here is an fps claim.** One launch, one area, in a build of `main` with measuring tools and other local changes, the same in every row. | | submissions, ms per frame (about 285 of it GPU in every row) | |---|---| | this change and four other copy changes of mine (#1267 and others, not all open) | 551 | | the same four without this change | +98 (62 to 171) against the row above | Medians of the paired differences with the range over the 8 frames; a control (same switches, another order) differed by 9 ms. A build that also asked each time found no wrong answer in 23,140 checks in the world. ## Tests - `remapped_memory_is_asked_about_again`, `a_check_during_the_unmapping_leaves_nothing_behind` (fails when the report comes before the unmapping). `cargo test --workspace --locked` (344 passed), clippy with `-D warnings` with and without `--all-features` on Windows, clippy on macOS: pass at 6739366. - One recorded frame (24 submissions with shader writes) replayed with this and without: title memory and the copies are byte for byte equal. This exercises the copies, not remapping during a frame. ## Overlap and conflicts - #1025 changes `kernel_will_write` and `watch.rs`, #1122 the guards of all views of physical pages and the unmapping in `xmem.rs`: both conflict here. If either goes in first I rebase. - #943, #991, #1010, #1024, #1124, #1215 and #1267 merge cleanly. ## Not verified - NVIDIA and Intel GPUs; Linux and Proton. - Frame time and fps in the game. - A remapping while the world streams, beyond the tests above and the 23,140 checks. AI-assisted (Claude Code). 🤖 Generated with [Claude Code](https://claude.com/claude-code)
adamdotpy commented 2026-11-24 21:03:22 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by adamdotpy on 2026-11-24T20:09:57Z.


Reviewed at head 6739366 (worktree on the PR head, isolated target dir, Windows 11 / Rust 1.98.1). Findings, ranked:

1. The caching is correct about invalidation. guard() now remembers, per block, that the memory is a mapped view (Guards.views), so the world heap's ~900 checks per frame skip the VirtualQuery (the 60–130 ms you measured). The cache is dropped for a range on title_memory_remapped, and a remaps counter handles the check-in-flight race: guard() drops the lock for the query, re-locks, and only records the answer if remaps is unchanged — so a remap that happened during the query discards the stale answer instead of caching it for good.

2. The unmap path fixes a real ordering bug. xmem::unmap previously called title_memory_remapped before UnmapViewOfFile2, so a guard() racing inside that window would query the still-mapped view, guard the blocks, and remember them — for good, since the later clearing had already run. The new unmap_title_view (exported from victory-gpu) unmaps first and then forgets via title_memory_remapped, and the map failure path uses it too. The new a_check_during_the_unmapping_leaves_nothing_behind test pins exactly that interleaving (guard during unmap → after, views/protected/clean are empty for the range, the next guard returns None, and fresh private memory at the same addresses is not guarded). remapped_memory_is_asked_about_again pins the cache-and-invalidate behavior. Both pass here, plus the pre-existing watch tests: cargo test -p victory-gpu --lib watch 5 passed.

3. Full checks on 6739366: cargo test -p victory-gpu 52 passed / 0 failed / 1 ignored; cargo test -p victory-hle 94 passed + 1 integration suite, 0 failed; cargo clippy -p victory-gpu -p victory-hle --all-targets --all-features --locked -- -D warnings clean; cargo build --release -p victory builds. rustfmt --check: xmem.rs clean, watch.rs has 8 hunks vs main's 2 pre-existing — about 6 are in the new code (cosmetic; CI has no fmt job).

4. One residual race, noted not blocking: if a remap lands between guard()'s query and its re-lock, the query answer is discarded for the views cache but the block loop still guards based on the stale (pre-remap) MEM_MAPPED answer. The fault handler re-queries at write time and unguards non-view memory, so it self-heals on first touch; making guard() re-query when remaps changed would close it fully if you want zero window.

Nothing found that would block this.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7143132185 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/1277#issuecomment-7143132185). Originally posted by [adamdotpy](https://github.com/adamdotpy) on 2026-11-24T20:09:57Z. --- Reviewed at head `6739366` (worktree on the PR head, isolated target dir, Windows 11 / Rust 1.98.1). Findings, ranked: **1. The caching is correct about invalidation.** `guard()` now remembers, per block, that the memory is a mapped view (`Guards.views`), so the world heap's ~900 checks per frame skip the `VirtualQuery` (the 60–130 ms you measured). The cache is dropped for a range on `title_memory_remapped`, and a `remaps` counter handles the check-in-flight race: `guard()` drops the lock for the query, re-locks, and only records the answer if `remaps` is unchanged — so a remap that happened during the query discards the stale answer instead of caching it for good. **2. The unmap path fixes a real ordering bug.** `xmem::unmap` previously called `title_memory_remapped` *before* `UnmapViewOfFile2`, so a `guard()` racing inside that window would query the still-mapped view, guard the blocks, and remember them — for good, since the later clearing had already run. The new `unmap_title_view` (exported from `victory-gpu`) unmaps first and then forgets via `title_memory_remapped`, and the `map` failure path uses it too. The new `a_check_during_the_unmapping_leaves_nothing_behind` test pins exactly that interleaving (guard during unmap → after, `views`/`protected`/`clean` are empty for the range, the next `guard` returns `None`, and fresh private memory at the same addresses is not guarded). `remapped_memory_is_asked_about_again` pins the cache-and-invalidate behavior. Both pass here, plus the pre-existing watch tests: `cargo test -p victory-gpu --lib watch` 5 passed. **3. Full checks on `6739366`:** `cargo test -p victory-gpu` 52 passed / 0 failed / 1 ignored; `cargo test -p victory-hle` 94 passed + 1 integration suite, 0 failed; `cargo clippy -p victory-gpu -p victory-hle --all-targets --all-features --locked -- -D warnings` clean; `cargo build --release -p victory` builds. `rustfmt --check`: `xmem.rs` clean, `watch.rs` has 8 hunks vs main's 2 pre-existing — about 6 are in the new code (cosmetic; CI has no fmt job). **4. One residual race, noted not blocking:** if a remap lands *between* `guard()`'s query and its re-lock, the query answer is discarded for the `views` cache but the block loop still guards based on the stale (pre-remap) `MEM_MAPPED` answer. The fault handler re-queries at write time and unguards non-view memory, so it self-heals on first touch; making `guard()` re-query when `remaps` changed would close it fully if you want zero window. Nothing found that would block this.
upstream-sync closed this pull request 2026-11-25 01:06:07 +00:00
Marvisak commented 2026-11-25 01:08:07 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Marvisak on 2026-11-25T00:36:31Z.


Automated triage for the maintainers. Closing because this change is already in main, or main has since replaced the code it patches.

Caches the per-check view query of the old guard; main has no per-check query (trackers ask once when made) and remapping is reported by the runtime.

If this is mistaken, reply here with what was missed (or push a change and say so) and it will be looked at again. Thank you for the contribution.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7148689617 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/1277#issuecomment-7148689617). Originally posted by [Marvisak](https://github.com/Marvisak) on 2026-11-25T00:36:31Z. --- Automated triage for the maintainers. Closing because this change is already in `main`, or `main` has since replaced the code it patches. Caches the per-check view query of the old guard; main has no per-check query (trackers ask once when made) and remapping is reported by the runtime. If this is mistaken, reply here with what was missed (or push a change and say so) and it will be looked at again. Thank you for the contribution.

Pull request closed

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
Marvisak/VIctory!1277
No description provided.