HLE, GPU: title memory held in place is let go before the title or the runtime changes it #2921

Closed
github-bot wants to merge 2 commits from github-bot/gh-2921 into main AGit
Owner

@⁠0xkn0wh3r3 on GitHub · 2026-11-26 00:09 UTC · original
0xkn0wh3r3:gpu/title-memory-changing → main · +693 −3 · 5 files · 2 commits · closed without merging

Problem

A GPU heap opened over title memory (OpenExistingHeapFromAddress1, as #2112 does towards #880) locks its pages. While the heap lives, these all fail with ERROR_INVALID_PARAMETER (87):

  • VirtualProtect;
  • UnmapViewOfFile2;
  • VirtualFree (MEM_DECOMMIT / MEM_RELEASE).

This was measured on an RTX 5070 Ti, the Ryzen 7900X's integrated Radeon and WARP alike, so it is the system's rule, not a driver's. Without a heap, all three work. nathanbzrr saw the same in #2112's review.

The title frees and remaps exactly this memory: VirtualFree straight to the host, XMemMapPhysicalPages through xmem's unmapping. On the console these always work, because its GPU reads title memory in place. Any in-place design therefore needs to let go of a range before it changes. Today the GPU side hears about remaps only afterwards (title_memory_remapped).

What this adds

victory_gpu::pinned, a registry of title memory held in place:

  • pin(at, len, release) registers a range and how to let it go; Pin::forget removes it when the holder drops it by itself.
  • title_memory_change(at, len, change) releases every holder overlapping the range, then runs the host call.
  • The system sometimes unlocks released pages late. On the 5070 Ti, 32 of 40 releases were unlocked within 0.01 ms and the slowest after 5.9 ms. So after a release, a call refused with error 87 is tried again for up to 100 ms. The call itself is the probe: nothing else touches protections, which could race with the write guards.

The changes now go through it:

  • the title's VirtualFree, VirtualFreeEx, VirtualProtect, VirtualProtectEx, UnmapViewOfFile, UnmapViewOfFileEx, UnmapViewOfFile2. A new HLE module, memoryapi, serves api-ms-win-core-memory-*, kernel32.dll and kernelbase.dll for these seven only; everything else in those DLLs still resolves to the host. A release, or an unmap, covers the whole allocation, found by VirtualQuery.
  • xmem's unmapping of physical pages (unmap);
  • XMemFreeDefault's release of 32-bit memory.

Nothing registers a pin on main, so the only cost is one atomic load per call. A holder (#2112, #2734) registers when it opens a heap; its release drops the heap and its resources once no submission uses them.

Tests

  • pinned::tests::title_memory_held_by_a_heap_frees_after_the_call (host GPU with ID3D12Device13; skips otherwise):

    1. A write-watched 4 MiB block is opened as a heap with a placed buffer and pinned.
    2. VirtualProtect is refused while the heap lives.
    3. title_memory_change on one page in the middle releases the holder, and the protect then succeeds.
    4. VirtualFree succeeds.

    25 of 25 runs passed. Before the retry, about 1 run in 6 failed on the late unlock.

  • overlapping_pins_are_released_once_and_others_kept and a_long_pin_starting_far_before_is_found: the range lookup.

  • a_change_is_tried_again_only_after_a_release: the retry happens only after a release, and only for error 87.

  • memoryapi::tests::the_rest_of_an_allocation_spans_its_regions: allocation extents across regions of different protection, and the wrappers behaving like the host's functions.

  • memoryapi::tests::the_title_s_memory_functions_are_served_here_and_the_rest_by_the_host.

  • Workspace clippy (both configurations) is clean. victory-hle lib tests: 154 passed.

  • victory-gpu lib tests: 233 passed, 2 failed. The two, descriptor::host::tests::dynamic_slots_are_held_until_released and memory::tests::lists_share_a_table_and_copies_keep_their_views, fail the same way on clean main on this machine.

  • #2112 / #2734 (in place): they can register their heaps here instead of failing the title's free or remap.
  • #2912: detects unified memory and runs the in-place self-test. Independent of this PR.
  • #2824: also edits XMemFreeDefault (write trackers on 32-bit release); a one-line conflict at most.
  • #2494: RFC prototype over SEC_COMMIT sections.
**@⁠0xkn0wh3r3** on GitHub · 2026-11-26 00:09 UTC · [original](https://github.com/Marvisak/VIctory/pull/2921) `0xkn0wh3r3:gpu/title-memory-changing` → `main` · +693 −3 · 5 files · 2 commits · closed without merging ### Problem A GPU heap opened over title memory (`OpenExistingHeapFromAddress1`, as #2112 does towards #880) locks its pages. While the heap lives, these all fail with `ERROR_INVALID_PARAMETER` (87): - `VirtualProtect`; - `UnmapViewOfFile2`; - `VirtualFree` (`MEM_DECOMMIT` / `MEM_RELEASE`). This was measured on an RTX 5070 Ti, the Ryzen 7900X's integrated Radeon and WARP alike, so it is the system's rule, not a driver's. Without a heap, all three work. nathanbzrr saw the same in #2112's review. The title frees and remaps exactly this memory: `VirtualFree` straight to the host, `XMemMapPhysicalPages` through xmem's unmapping. On the console these always work, because its GPU reads title memory in place. Any in-place design therefore needs to let go of a range *before* it changes. Today the GPU side hears about remaps only afterwards (`title_memory_remapped`). ### What this adds **`victory_gpu::pinned`**, a registry of title memory held in place: - `pin(at, len, release)` registers a range and how to let it go; `Pin::forget` removes it when the holder drops it by itself. - `title_memory_change(at, len, change)` releases every holder overlapping the range, then runs the host call. - The system sometimes unlocks released pages late. On the 5070 Ti, 32 of 40 releases were unlocked within 0.01 ms and the slowest after 5.9 ms. So after a release, a call refused with error 87 is tried again for up to 100 ms. The call itself is the probe: nothing else touches protections, which could race with the write guards. **The changes now go through it:** - the title's `VirtualFree`, `VirtualFreeEx`, `VirtualProtect`, `VirtualProtectEx`, `UnmapViewOfFile`, `UnmapViewOfFileEx`, `UnmapViewOfFile2`. A new HLE module, `memoryapi`, serves `api-ms-win-core-memory-*`, `kernel32.dll` and `kernelbase.dll` for these seven only; everything else in those DLLs still resolves to the host. A release, or an unmap, covers the whole allocation, found by `VirtualQuery`. - xmem's unmapping of physical pages (`unmap`); - `XMemFreeDefault`'s release of 32-bit memory. Nothing registers a pin on main, so the only cost is one atomic load per call. A holder (#2112, #2734) registers when it opens a heap; its release drops the heap and its resources once no submission uses them. ### Tests - `pinned::tests::title_memory_held_by_a_heap_frees_after_the_call` (host GPU with `ID3D12Device13`; skips otherwise): 1. A write-watched 4 MiB block is opened as a heap with a placed buffer and pinned. 2. `VirtualProtect` is refused while the heap lives. 3. `title_memory_change` on one page in the middle releases the holder, and the protect then succeeds. 4. `VirtualFree` succeeds. 25 of 25 runs passed. Before the retry, about 1 run in 6 failed on the late unlock. - `overlapping_pins_are_released_once_and_others_kept` and `a_long_pin_starting_far_before_is_found`: the range lookup. - `a_change_is_tried_again_only_after_a_release`: the retry happens only after a release, and only for error 87. - `memoryapi::tests::the_rest_of_an_allocation_spans_its_regions`: allocation extents across regions of different protection, and the wrappers behaving like the host's functions. - `memoryapi::tests::the_title_s_memory_functions_are_served_here_and_the_rest_by_the_host`. - Workspace clippy (both configurations) is clean. victory-hle lib tests: 154 passed. - victory-gpu lib tests: 233 passed, 2 failed. The two, `descriptor::host::tests::dynamic_slots_are_held_until_released` and `memory::tests::lists_share_a_table_and_copies_keep_their_views`, fail the same way on clean main on this machine. ### Related - **#2112 / #2734 (in place):** they can register their heaps here instead of failing the title's free or remap. - **#2912:** detects unified memory and runs the in-place self-test. Independent of this PR. - **#2824:** also edits `XMemFreeDefault` (write trackers on 32-bit release); a one-line conflict at most. - **#2494:** RFC prototype over SEC_COMMIT sections.
A GPU heap opened over title memory (OpenExistingHeapFromAddress1, as
#2112 does) locks its pages. While it lives, VirtualProtect,
UnmapViewOfFile2 and VirtualFree fail with ERROR_INVALID_PARAMETER, on
NVIDIA, AMD and WARP alike. The title frees and remaps such memory.

victory_gpu::pinned keeps a registry of held ranges, each with how to let
it go. title_memory_change(at, len, change) releases every holder
overlapping the range, then runs the host call. The system sometimes
unlocks released pages late (RTX 5070 Ti: 32 of 40 within 0.01 ms, the
slowest 5.9 ms), so after a release a call refused with that error is
tried again for up to 100 ms.

The changes go through it:
- the title's VirtualFree, VirtualFreeEx, VirtualProtect, VirtualProtectEx,
  UnmapViewOfFile, UnmapViewOfFileEx and UnmapViewOfFile2 (new HLE module
  memoryapi; everything else in those DLLs stays the host's);
- xmem's unmapping of physical pages;
- XMemFreeDefault's release of 32-bit memory.

Nothing registers a pin on main yet, so the cost is one atomic load per
call. Holders (#2112, #2734) register when they open a heap.
Review (fuzeofficlal): the pin count dropped before the release callbacks
ran. A second thread changing an overlapping range could then see nothing
held, call VirtualFree or VirtualProtect while the first was still dropping
the heap, and get ERROR_INVALID_PARAMETER without a retry.

Released pins now stay counted, as releasing, until their callback returns.
A change on another thread that overlaps one waits on a condition variable
for it, then gets the late-unlock retry. A release running on the caller's
own thread is not waited for, so a callback that changes memory itself
does not deadlock. The docs also say never to change title memory through
here from the guard handler or under watch's lock: a release may wait for
a submission.

Test: a change on a second thread waits for a 50 ms release running on the
first.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 05:17:33 +00:00
Author
Owner

@⁠0xkn0wh3r3 on GitHub · 2026-11-26 00:36 UTC · original

@⁠fuzeofficlal thanks, both notes are addressed in 08261fdc.

  1. PINS ordering window: a released pin now stays counted as releasing until its callback returns. A change on another thread that overlaps it waits on a condvar, then takes the late-unlock retry. The releasing thread itself isn't waited for, so a callback that changes memory itself doesn't deadlock. New test: a second thread's change waits for a 50 ms release.
  2. watch.rs: there's no wiring to it in this PR. The pinned.rs docs now say never to change title memory through here from the guard handler or while holding watch's lock.

victory-gpu: 234 passed, with only the two heap-cap failures that also happen on main on NVIDIA. victory-hle: 154 passed. Clippy is clean with and without diagnostics.

@⁠kotekompotek thanks for the review.

**@⁠0xkn0wh3r3** on GitHub · 2026-11-26 00:36 UTC · [original](https://github.com/Marvisak/VIctory/pull/2921#issuecomment-7178935773) @⁠fuzeofficlal thanks, both notes are addressed in 08261fdc. 1. **`PINS` ordering window:** a released pin now stays counted as releasing until its callback returns. A change on another thread that overlaps it waits on a condvar, then takes the late-unlock retry. The releasing thread itself isn't waited for, so a callback that changes memory itself doesn't deadlock. New test: a second thread's change waits for a 50 ms release. 2. **`watch.rs`:** there's no wiring to it in this PR. The `pinned.rs` docs now say never to change title memory through here from the guard handler or while holding watch's lock. `victory-gpu`: 234 passed, with only the two heap-cap failures that also happen on `main` on NVIDIA. `victory-hle`: 154 passed. Clippy is clean with and without `diagnostics`. @⁠kotekompotek thanks for the review.
github-bot closed this pull request 2026-11-26 08:07:36 +00:00
Author
Owner

@⁠fuzeofficlal on GitHub · 2026-11-26 00:23 UTC · original · reviewed

Verified head 8ca7c6d2 cleanly on origin/main (63bcca43) on Windows 11 x64 (NVIDIA GeForce RTX 5090 + VICTORY_D3D12_DEBUG=1).

Architecture & Concurrency Review

  • Releasing REGISTRY before running the removed Release closures prevents lock inversion, single-removal guarantees each closure runs at most once, and the SETTLE = 100 ms ERROR_INVALID_PARAMETER retry loop cleanly covers the Windows OpenExistingHeapFromAddress1 kernel-unpin latency without clobbering GetLastError on non-retried failures.
  • Two concurrency notes (non-blocking for this plumbing PR, worth fixing before wiring live pins):
    1. PINS.fetch_sub ordering window (crates/victory-gpu/src/pinned.rs:171): PINS is decremented before for release in released { release(); } finishes. If two threads concurrently change the same address range, Thread B can observe PINS == 0 (or released == 0) and immediately call VirtualFree/VirtualProtect before Thread A's release() finishes dropping the D3D12 heap (getting ERROR_INVALID_PARAMETER without retrying). Decrementing PINS after the release callbacks complete (or tracking in-flight releases so overlapping callers wait for them) closes that window.
    2. Future watch.rs integration: Note that watch::protect_noting runs under the GUARDS mutex and watch::open runs inside the VEH handler — when wiring watch.rs to title_memory_change, pins should be released outside GUARDS and never inside the VEH handler to avoid deadlocking if a Release callback waits on GPU submission completion.

Local Verification (VICTORY_D3D12_DEBUG=1)

  • victory-gpu (--features diagnostics, RTX 5090): pinned::tests + shadow::tests + draw::tests + host::tests (non-descriptor) 45/45 passed (including live OpenExistingHeapFromAddress1 test title_memory_held_by_a_heap_frees_after_the_call).
  • victory-hle: 154/154 unit tests (including memoryapi::tests 2/2) + fidelityfx (1/1) + no_dll_planting (1/1) passed.
  • cargo clippy --locked -p victory-gpu -p victory-hle --tests -- -D warnings (default and --all-features) & cargo xtask policy (193 files ok): clean.
**@⁠fuzeofficlal** on GitHub · 2026-11-26 00:23 UTC · [original](https://github.com/Marvisak/VIctory/pull/2921#pullrequestreview-6066877021) · **reviewed** Verified head `8ca7c6d2` cleanly on `origin/main` (`63bcca43`) on Windows 11 x64 (NVIDIA GeForce RTX 5090 + `VICTORY_D3D12_DEBUG=1`). ### Architecture & Concurrency Review - Releasing `REGISTRY` before running the removed `Release` closures prevents lock inversion, single-removal guarantees each closure runs at most once, and the `SETTLE = 100 ms` `ERROR_INVALID_PARAMETER` retry loop cleanly covers the Windows `OpenExistingHeapFromAddress1` kernel-unpin latency without clobbering `GetLastError` on non-retried failures. - **Two concurrency notes (non-blocking for this plumbing PR, worth fixing before wiring live pins)**: 1. **`PINS.fetch_sub` ordering window (`crates/victory-gpu/src/pinned.rs:171`)**: `PINS` is decremented *before* `for release in released { release(); }` finishes. If two threads concurrently change the same address range, Thread B can observe `PINS == 0` (or `released == 0`) and immediately call `VirtualFree`/`VirtualProtect` before Thread A's `release()` finishes dropping the D3D12 heap (getting `ERROR_INVALID_PARAMETER` without retrying). Decrementing `PINS` *after* the release callbacks complete (or tracking in-flight releases so overlapping callers wait for them) closes that window. 2. **Future `watch.rs` integration**: Note that `watch::protect_noting` runs under the `GUARDS` mutex and `watch::open` runs inside the VEH handler — when wiring `watch.rs` to `title_memory_change`, pins should be released *outside* `GUARDS` and never inside the VEH handler to avoid deadlocking if a `Release` callback waits on GPU submission completion. ### Local Verification (`VICTORY_D3D12_DEBUG=1`) - `victory-gpu` (`--features diagnostics`, RTX 5090): `pinned::tests` + `shadow::tests` + `draw::tests` + `host::tests` (non-descriptor) **45/45 passed** (including live `OpenExistingHeapFromAddress1` test `title_memory_held_by_a_heap_frees_after_the_call`). - `victory-hle`: **154/154** unit tests (including `memoryapi::tests` 2/2) + `fidelityfx` (1/1) + `no_dll_planting` (1/1) passed. - `cargo clippy --locked -p victory-gpu -p victory-hle --tests -- -D warnings` (default and `--all-features`) & `cargo xtask policy` (`193 files ok`): clean.
Author
Owner

@⁠kotekompotek on GitHub · 2026-11-26 00:27 UTC · original · reviewed

Scoped review: the pin-release design is the right shape for in-place title memory — Windows locks heap-opened pages, so title_memory_change releasing every overlapping pin before unmap/protect/free is required for correctness, and making release the holder's job (drop heap+resources once no in-flight submission uses them) avoids use-after-free on the GPU side. The wait-for-submission choice over the console's live-race semantics is the safer one to start with.

**@⁠kotekompotek** on GitHub · 2026-11-26 00:27 UTC · [original](https://github.com/Marvisak/VIctory/pull/2921#pullrequestreview-6066905234) · **reviewed** Scoped review: the pin-release design is the right shape for in-place title memory — Windows locks heap-opened pages, so `title_memory_change` releasing every overlapping pin before unmap/protect/free is required for correctness, and making release the holder's job (drop heap+resources once no in-flight submission uses them) avoids use-after-free on the GPU side. The wait-for-submission choice over the console's live-race semantics is the safer one to start with.
Author
Owner

@⁠Marvisak on GitHub · 2026-11-26 04:30 UTC · original · edited 2026-11-26 07:23 UTC

Automated triage for the maintainers.

Needs work: needs the maintainer's decision. You are right, and our earlier comment named the wrong PRs. Neither #2112 nor #2734 calls pinned::pin. #2945 does, in shadow.rs, and its tests check title_memory_change against a held pin. The head is unchanged, and nothing in main calls pin yet, so on its own this PR still routes the title's VirtualFree/VirtualProtect/UnmapViewOfFile* through code that has no effect. Whether to review it as the stacked base of #2112 -> #2921 -> #2945 or only through #2945 is the maintainer's call. Either way, it should land no earlier than #2945.

CI: passing.


Generated by Claude Code

**@⁠Marvisak** on GitHub · 2026-11-26 04:30 UTC · [original](https://github.com/Marvisak/VIctory/pull/2921#issuecomment-7182650137) · edited 2026-11-26 07:23 UTC <!-- victory-triage v1 head=08261fdcb0e10d4c27cc37b071f66852a4da11d6 verdict=needs-work at=2026-11-26T07:23:44Z --> Automated triage for the maintainers. **Needs work: needs the maintainer's decision.** You are right, and our earlier comment named the wrong PRs. Neither #2112 nor #2734 calls `pinned::pin`. #2945 does, in `shadow.rs`, and its tests check `title_memory_change` against a held pin. The head is unchanged, and nothing in main calls `pin` yet, so on its own this PR still routes the title's `VirtualFree`/`VirtualProtect`/`UnmapViewOfFile*` through code that has no effect. Whether to review it as the stacked base of #2112 -> #2921 -> #2945 or only through #2945 is the maintainer's call. Either way, it should land no earlier than #2945. CI: passing. --- _Generated by [Claude Code](https://claude.ai/code)_
Author
Owner

@⁠0xkn0wh3r3 on GitHub · 2026-11-26 06:41 UTC · original

The first caller is #2945, not #2112 or #2734 (neither of those calls pin). In #2945, shadow.rs pins title memory the host uses in place on unified-memory hosts (crate::pinned::pin), and its tests check that title_memory_change releases a held range before the title frees it. #2945 is stacked on this branch and registers the heaps #2112 opens, so the order is #2112, #2921, #2945.

Two ways to land it, whichever you prefer:

  1. Close this PR and review the interception together with #2945, which already carries these commits.
  2. Keep it as a stacked base, to land just before #2945.
**@⁠0xkn0wh3r3** on GitHub · 2026-11-26 06:41 UTC · [original](https://github.com/Marvisak/VIctory/pull/2921#issuecomment-7184685476) The first caller is #2945, not #2112 or #2734 (neither of those calls `pin`). In #2945, `shadow.rs` pins title memory the host uses in place on unified-memory hosts (`crate::pinned::pin`), and its tests check that `title_memory_change` releases a held range before the title frees it. #2945 is stacked on this branch and registers the heaps #2112 opens, so the order is #2112, #2921, #2945. Two ways to land it, whichever you prefer: 1. Close this PR and review the interception together with #2945, which already carries these commits. 2. Keep it as a stacked base, to land just before #2945.
Author
Owner

@⁠0xkn0wh3r3 on GitHub · 2026-11-26 08:05 UTC · original

Replaced by #3102, which combines this with the rest of the host-memory placement work in one PR, with measurements on a discrete and an integrated GPU. Closing here.

**@⁠0xkn0wh3r3** on GitHub · 2026-11-26 08:05 UTC · [original](https://github.com/Marvisak/VIctory/pull/2921#issuecomment-7186178668) Replaced by #3102, which combines this with the rest of the host-memory placement work in one PR, with measurements on a discrete and an integrated GPU. Closing here.

Pull request closed

This discussion has been locked. Commenting is limited to contributors.
No reviewers
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.

Dependencies

No dependencies set

Reference
github/Marvisak--VIctory!2921
No description provided.