xmem: memory the GPU reads in place must survive protection changes and remaps (no re-import, no 1 s hitch) #1905
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#1905
Loading…
Reference in a new issue
No description provided.
Delete branch "%!s()"
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?
@leonardocfirmino on GitHub · 2026-11-25 00:31 UTC · original
What
#407 (and #880) would have shaders read title memory where it already is, instead of through a host copy. That turns every title remap from a copy-problem into an import-lifetime problem, and this issue is that part: the memory the GPU reads in place has to survive the protections and remaps the title applies to it.
It has no owner yet and it decides whether #407 is workable at all, so it is worth separating from the memory model itself.
What the title does
From the same runs as #880 and the probes in #407:
XMemVirtualAllocwithMEM_RESERVE_PLACEHOLDER, then repeated protection changes: a 16 KiB guard pagemprotectinside a live range is normal (a stack guard), and it lands inside memory a shader may be reading.XMemMapPhysicalPages,XMemMapPhysicalPagesScatterand the GPU'sCopyPageMappingsBatchXmove pages around in place. On the world's 174 MB view one runtime refresh is once per submission, about 20 times a frame.XMemFreePhysicalPagesand unmaps underneath live views.Why it breaks with an arena
xmemkeeps its addresses by mapping over placeholders (MEM_RELEASE | MEM_PRESERVE_PLACEHOLDER, thenMapViewOfFile3(..., MEM_REPLACE_PLACEHOLDER)), so the guest VA survives a remap today — the address is not the problem.With the arena the backing is imported host memory, an import covers one contiguous committed run, and the host refuses overlap. So a protection change or remap that splits a live imported run forces:
OpenExistingHeapFromAddress/FromFileMapping(and a new placed resource) per piece,victory_gpu::title_memory_remapped, which drops the guards, so anything per-submission repeats at that rate.What that costs when it re-imports instead of remapping, measured on the project that already does in-place reads (AnyPS5, Astro Bot, RX 9070 XT, PCIe 3.0 x16): a single 16 KiB guard page
mprotectinside a live import split it, and rebuilding the pieces re-imported ~1 GB with the GPU idle, ~1 s of hitches. At 20 refreshes of one 174 MB view per frame, a per-submission re-import is not survivable.Also unanswered: whether an imported heap tolerates
MEM_RESERVE_PLACEHOLDER/MapViewOfFile3at all when the address is already inside one, and whether tile mappings can be moved between imports of the same memory cheaply (UpdateTileMappingsfrom the old heap to the new one instead of recreating the resource).What a test would look like
Synthetic,
victory-gpuplus the xmem module, no title:XMemVirtualProtecta 16 KiB guard in the middle, then back,Skips without a host GPU; the count and the timings run on the same adapters #407's probe uses.
Proposal, roughly
title_memory_remappedinvalidates only the pages it names.PAGE_READWRITEfor the GPU and the guest-side guard is emulated bywatch.rs's fault handler, which already takes the first write to a block and gives access back.Open questions
MEM_REPLACE_PLACEHOLDERdancexmemrelies on, or must the mapping be created before the import?UpdateTileMappingsbetween two imports of the same memory cheaper than recreating the placed resource, and by how much?XMemVirtualQueryprotection to decide what it can touch?Related: #407, #880, #76, #167.
github-bot referenced this issue2026-11-26 04:49:39 +00:00
@fiestorra on GitHub · 2026-11-25 01:32 UTC · original
Taking one concrete part of this, and naming its limits so nobody expects the whole thing
The issue is right that this decides whether #407 is workable, and it says it has no owner. I am taking one part of it, scoped to what can be measured and tested in this repository today:
Concretely, the first piece is a synthetic test in
victory-gputhat walks the two cases the title performs, with memory the host has mapped for the GPU:mprotectin the middle of a live range — the stack guard the issue describes — and a read of the range on either side of it;MEM_RESERVE_PLACEHOLDERfreed and re-reserved under a live view, checking what the GPU sees afterwards and whether the import still names the same memory.What that gives, either way, is the thing the issue is missing: a number. Today the answer is "it depends on whether the copy survives", and after the test it is either "a live range survives a protection change, here is the test" or "it does not, here is the exact step that loses it" — and only then is it worth talking about import lifetimes, host copies and the 1 s hitch.
What I am not taking, and will not claim:
If someone is already further along on the import-lifetime side, say so and I will hand this test over rather than duplicate it — the test is useful either way and does not depend on which way the memory model goes.
No game data is involved: page sizes, protections and counts.
Written with an AI assistant.
@fiestorra on GitHub · 2026-11-25 01:35 UTC · original
The guard page half holds; the remap half does not, and that is the finding
I wrote the test I claimed, against the model
xmem.rsalready keeps (physical pages,XMemVirtualAllocreservations of 64 KiB pages,XMemMapPhysicalPages), and it answers both halves of the question rather than one.A 16 KiB
PAGE_NOACCESSchange inside a live range is fine. The change is accepted on a range that physical pages are mapped into, the pages on both sides keep their bytes across it and across the change back, and the run model still sees the range as one run. So a stack guard appearing in the middle of memory the GPU is reading is not, by itself, a problem for the direction #407 wants to take.A remap of half a live range is refused. Concretely:
A range that physical pages are mapped into is no longer a placeholder, so the title's "free half of it, reserve it again, map new pages there" step fails before any GPU read happens. Nothing moves, the range still reads as one run of four pages, and the bytes on both sides are the ones that were there — which is why nothing in the runtime notices: the bytes are fine, and the remap simply did not happen.
That matters for this issue in a way I did not expect when I claimed it: if #407 has shaders read title memory in place, a refused remap is not a copy problem or an import-lifetime problem — it is the title's own address-space operation being silently dropped, and the memory it "moved" keeps reading as before. Whether that is harmless (a title that remaps and reads the same bytes) or wrong (one that expects the new pages) is the next question, and it needs the title, not a synthetic test.
What I am handing over, since I claimed only this part:
Verification:
cargo test -p victory-hle --locked --lib xmem11 passed;cargo clippy --workspace --all-targetswith and without--all-featuresclean under-D warnings;rustfmt --checkon the file at the same four pre-existing hunks asmain, none of them mine. The fullvictory-hlesuite has one failure,lifecycle::tests::showing_a_title_window_shows_its_stand_in, which fails the same way onmainin a session with no desktop.Pull request: #1947
No game data is involved: page sizes, protections and return values.
Written with an AI assistant.
@nathanbzrr on GitHub · 2026-11-25 08:40 UTC · original
### Empirical Windows 11 / AMD RX 7600 + WARP Findings & Zero-Reimport Prototype
Tested on Windows 11 x64 (
10.0.26340), AMD Radeon RX 7600 (driver32.0.32029.2010,ID3D12Device13),VICTORY_D3D12_DEBUG=1, andVICTORY_D3D12_WARP=1(rustc 1.98.1, prototype on fork branchproto/issue-1905-inplace-remap, commit593f02b, based on940d32f).1. What Win32 + WDDM + D3D12 actually do
0(ERROR_INVALID_PARAMETER = 87): CallingVirtualFree(MEM_RELEASE | MEM_PRESERVE_PLACEHOLDER)directly on a mapped view fails witherr=87because mapped views are no longer placeholders. Remapping sub-ranges requiresUnmapViewOfFile2(MEM_PRESERVE_PLACEHOLDER)-> split/coalesce viaVirtualFree(MEM_RELEASE | MEM_PRESERVE_PLACEHOLDER)/MEM_COALESCE_PLACEHOLDERS->MapViewOfFile3(MEM_REPLACE_PLACEHOLDER)(prototyped inxmem::prepare_placeholder).OpenExistingHeapFromAddresslocks CPU VA protections and placeholder unmaps while live: On both RX 7600 and WARP, while anOpenExistingHeapFromAddressheap is alive over eitherMEM_WRITE_WATCHor plainVirtualAllocmemory,VirtualProtect(PAGE_GUARD,PAGE_READONLY,PAGE_NOACCESS) andUnmapViewOfFile2(MEM_PRESERVE_PLACEHOLDER)fail witherr=87until theID3D12Heapis dropped (ok=1immediately after drop).ID3D12Device13::OpenExistingHeapFromAddress1also returnsE_INVALIDARG(0x80070057) across split section views ("cannot open a region that spans multiple VirtualAlloc allocations").OpenExistingHeapFromFileMappingrequiresSEC_COMMIT: Succeeds onSEC_COMMITsections (4 KiB..174 MiB,Type=4 CPUPage=3 Pool=1 Flags=0x421D3D12_HEAP_FLAG_SHARED_CROSS_ADAPTER), and rejectsSEC_RESERVEwith0x80070057even when 100% committed.UpdateTileMappingscannot bind imported section heaps: BecauseOpenExistingHeapFromFileMappingreturns aSHARED_CROSS_ADAPTERheap,ID3D12CommandQueue::UpdateTileMappingstriggers device removal (DXGI_ERROR_INVALID_CALL = 0x887A0001; Debug Layer:"UpdateTileMappings does not work with cross-adapter shared heaps").2. Working Zero-Reimport Architecture (
InplaceArenainvictory-gpu)SEC_COMMITsection once viaOpenExistingHeapFromFileMapping->CreatePlacedResource(ALLOW_CROSS_ADAPTER | ALLOW_UNORDERED_ACCESS)-> one persistent raw(SRV, UAV)descriptor pair.2^40in a persistently mapped 2-level GPU page table matchingmemory.rs'sviews[e.x].Load(a - e.y)indirection.VirtualProtect(PAGE_GUARD)/PAGE_NOACCESS+ restore, half-range remaps, split/coalesce (MEM_COALESCE_PLACEHOLDERS), GPU UAV writes visible across aliases, and unmap->zero-read with 0 re-imports, 0 new resources, and 0 D3D12 debug errors:cargo test -p victory-hle xmem: 14/14 passedcargo test -p victory-gpu --lib inplace -- --test-threads=1: 5/5 passed on RX 7600, RX 7600 +VICTORY_D3D12_DEBUG=1, andVICTORY_D3D12_WARP=1+DEBUG=1(2 ignored: benchmark + child-processUpdateTileMappingsremoval probe)cargo test -p victory-gpu --lib -- --test-threads=1: 148/148 passed (RX 7600)cargo clippy --workspace --all-targets -- -D warnings&cargo xtask policy: clean3. RX 7600
--releaseRemap Benchmark (bench_inplace_remap_vs_reimport)FileMapping+Placed(p50)UpdateTileMappings(DEFAULT)+ fence (p50)FromAddressAPI / +GPU read (p50)(Constraint: guest memory must use
SEC_COMMITfile mappings or fall back to copy tracking when backed bySEC_RESERVEor privateVirtualAlloc.)AI-assisted; synthetic tests only, no game files, dumps, or captures used.
@Marvisak on GitHub · 2026-11-25 12:52 UTC · original
Automated triage for the maintainers.
A design request that fits the project's scope: it concerns how the in-place title memory reads proposed in #407/#880 would survive the title's protection changes and remaps. It is kept open as the place for that question.
Where it stands:
xmemtest. It shows a 16 KiB protection change inside a mapped range is accepted, and aMEM_RELEASE | MEM_PRESERVE_PLACEHOLDERon half of a live mapped range is refused on the current model.OpenExistingHeapFromAddressheap blocksVirtualProtectand placeholder unmaps, andUpdateTileMappingscannot bind cross-adapter shared heaps. It also has a zero-reimport prototype (oneSEC_COMMITimport plus a GPU page table), on a fork branch only.Pull requests are open to everyone. Please open the prototype as a PR with its tests, so it can be reviewed here next to #1947 and #407.
Generated by Claude Code