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

Closed
0xkn0wh3r3 wants to merge 2 commits from upstream-pr/2921 into main
0xkn0wh3r3 commented 2026-11-26 00:09:49 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by 0xkn0wh3r3 on 2026-11-26T00:09:49Z.


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.
<!-- greyfinch-sync:Marvisak/VIctory:issue:6616435519 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2921). Originally posted by [0xkn0wh3r3](https://github.com/0xkn0wh3r3) on 2026-11-26T00:09:49Z. --- ### 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.
fuzeofficlal commented 2026-11-26 01:03:46 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by fuzeofficlal on 2026-11-26T00:23:38Z.

Review: COMMENTED


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.
<!-- greyfinch-sync:Marvisak/VIctory:review:6066877021 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2921#pullrequestreview-6066877021). Originally posted by [fuzeofficlal](https://github.com/fuzeofficlal) on 2026-11-26T00:23:38Z. **Review:** COMMENTED --- 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.
kotekompotek commented 2026-11-26 01:03:46 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by kotekompotek on 2026-11-26T00:27:36Z.

Review: COMMENTED


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.

<!-- greyfinch-sync:Marvisak/VIctory:review:6066905234 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2921#pullrequestreview-6066905234). Originally posted by [kotekompotek](https://github.com/kotekompotek) on 2026-11-26T00:27:36Z. **Review:** COMMENTED --- 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.
0xkn0wh3r3 commented 2026-11-26 01:04:09 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by 0xkn0wh3r3 on 2026-11-26T00:36:44Z.


@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.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7178935773 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2921#issuecomment-7178935773). Originally posted by [0xkn0wh3r3](https://github.com/0xkn0wh3r3) on 2026-11-26T00:36:44Z. --- @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.
Marvisak commented 2026-11-26 05:04:12 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Marvisak on 2026-11-26T04:30:09Z.


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

<!-- greyfinch-sync:Marvisak/VIctory:comment:7182650137 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2921#issuecomment-7182650137). Originally posted by [Marvisak](https://github.com/Marvisak) on 2026-11-26T04:30:09Z. --- <!-- 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)_
0xkn0wh3r3 commented 2026-11-26 07:01:52 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by 0xkn0wh3r3 on 2026-11-26T06:41:31Z.


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.
<!-- greyfinch-sync:Marvisak/VIctory:comment:7184685476 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2921#issuecomment-7184685476). Originally posted by [0xkn0wh3r3](https://github.com/0xkn0wh3r3) on 2026-11-26T06:41:31Z. --- 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.
upstream-sync closed this pull request 2026-11-26 09:05:27 +00:00
0xkn0wh3r3 commented 2026-11-26 09:06:03 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by 0xkn0wh3r3 on 2026-11-26T08:05:42Z.


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.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7186178668 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2921#issuecomment-7186178668). Originally posted by [0xkn0wh3r3](https://github.com/0xkn0wh3r3) on 2026-11-26T08:05:42Z. --- 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

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!2919
No description provided.