HLE, GPU: title memory held in place is let go before the title or the runtime changes it #2921
No reviewers
Labels
No labels
accessibility
area: build
area: formats
area: gpu
area: loader
area: runtime
area: shader
bug
documentation
duplicate
enhancement
good first issue
gpu: amd
gpu: intel
gpu: nvidia
help wanted
host: wine
invalid
out of scope
question
request
run report
triage: confirmed
triage: has patch
triage: merge candidate
triage: needs info
triage: needs rebase
triage: needs work
triage: proposed close
wontfix
No milestone
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
github/Marvisak--VIctory!2921
Loading…
Reference in a new issue
No description provided.
Delete branch "github-bot/gh-2921"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
@0xkn0wh3r3 on GitHub · 2026-11-26 00:09 UTC · original
0xkn0wh3r3:gpu/title-memory-changing→main· +693 −3 · 5 files · 2 commits · closed without mergingProblem
A GPU heap opened over title memory (
OpenExistingHeapFromAddress1, as #2112 does towards #880) locks its pages. While the heap lives, these all fail withERROR_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:
VirtualFreestraight to the host,XMemMapPhysicalPagesthrough 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::forgetremoves 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 changes now go through it:
VirtualFree,VirtualFreeEx,VirtualProtect,VirtualProtectEx,UnmapViewOfFile,UnmapViewOfFileEx,UnmapViewOfFile2. A new HLE module,memoryapi, servesapi-ms-win-core-memory-*,kernel32.dllandkernelbase.dllfor these seven only; everything else in those DLLs still resolves to the host. A release, or an unmap, covers the whole allocation, found byVirtualQuery.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 withID3D12Device13; skips otherwise):VirtualProtectis refused while the heap lives.title_memory_changeon one page in the middle releases the holder, and the protect then succeeds.VirtualFreesucceeds.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_keptanda_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_releasedandmemory::tests::lists_share_a_table_and_copies_keep_their_views, fail the same way on clean main on this machine.Related
XMemFreeDefault(write trackers on 32-bit release); a one-line conflict at most.@0xkn0wh3r3 on GitHub · 2026-11-26 00:36 UTC · original
@fuzeofficlal thanks, both notes are addressed in
08261fdc.PINSordering 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.watch.rs: there's no wiring to it in this PR. Thepinned.rsdocs 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 onmainon NVIDIA.victory-hle: 154 passed. Clippy is clean with and withoutdiagnostics.@kotekompotek thanks for the review.
@fuzeofficlal on GitHub · 2026-11-26 00:23 UTC · original · reviewed
Verified head
8ca7c6d2cleanly onorigin/main(63bcca43) on Windows 11 x64 (NVIDIA GeForce RTX 5090 +VICTORY_D3D12_DEBUG=1).Architecture & Concurrency Review
REGISTRYbefore running the removedReleaseclosures prevents lock inversion, single-removal guarantees each closure runs at most once, and theSETTLE = 100 msERROR_INVALID_PARAMETERretry loop cleanly covers the WindowsOpenExistingHeapFromAddress1kernel-unpin latency without clobberingGetLastErroron non-retried failures.PINS.fetch_subordering window (crates/victory-gpu/src/pinned.rs:171):PINSis decremented beforefor release in released { release(); }finishes. If two threads concurrently change the same address range, Thread B can observePINS == 0(orreleased == 0) and immediately callVirtualFree/VirtualProtectbefore Thread A'srelease()finishes dropping the D3D12 heap (gettingERROR_INVALID_PARAMETERwithout retrying). DecrementingPINSafter the release callbacks complete (or tracking in-flight releases so overlapping callers wait for them) closes that window.watch.rsintegration: Note thatwatch::protect_notingruns under theGUARDSmutex andwatch::openruns inside the VEH handler — when wiringwatch.rstotitle_memory_change, pins should be released outsideGUARDSand never inside the VEH handler to avoid deadlocking if aReleasecallback 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 liveOpenExistingHeapFromAddress1testtitle_memory_held_by_a_heap_frees_after_the_call).victory-hle: 154/154 unit tests (includingmemoryapi::tests2/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 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_changereleasing 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.@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, inshadow.rs, and its tests checktitle_memory_changeagainst a held pin. The head is unchanged, and nothing in main callspinyet, so on its own this PR still routes the title'sVirtualFree/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
@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.rspins title memory the host uses in place on unified-memory hosts (crate::pinned::pin), and its tests check thattitle_memory_changereleases 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:
@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.
Pull request closed