fix(agc): import GPU-mapped guest memory when it is mapped #1961
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/1961"
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?
Imported from boykopovar/AnyPS5. Originally posted by Raghav314 on 2026-10-09T02:32:35Z.
What
An address-based build imports every registered allocation with GPU access that it leases, whole, the first time it builds with it. Pinning a large allocation takes about 0.3 s per GiB here (the kernel faults in and zeroes each page), and it runs on the queue that first uses the allocation, under the device lock.
GuestAllocationsSetGpuMapObserver_nid_postfix) on the mapping thread, after the registry lock is released, with the ranges a kernel mapping gave GPU access: direct and flexible maps with it (Mutation::NoteGpuMapping), and fixed maps with it over a registered range such as a reservation. The mutation looks the ranges up while it still holds the lock and hands them over as weak pointers, with the registry generation it ended at, so the observer never keeps a range alive (a concurrent unmap does not wait for it). AnyPS5's own libc heap is registered with GPU access but is not reported.ImportMappedRanges), the same whole-range import an address-based build makes, without reconciling the other imports with the registry, so the work per mapping does not depend on how many imports there are. It skips a range that is already gone, and every range when the imports were reconciled with a newer registry since; the next lookup imports those as before. It installs the pin waiter as the other import paths do, so unmapping a range imported at map retires its import (#2575); without it, unmapping a range imported at map before any lookup waited forAPS5_PIN_WAIT_MS(60 s) and failed. The live import bytes are a running total instead of a sum over every import. With no device yet, nothing happens and the first use imports as before. A failed import is logged per range and handled as a failed import is today.sceKernelMprotectis still imported on first use.Sources:
src/core/memory.cppcallsrasterizer->MapMemorywhen a mapping with GPU access is made, and KytyPS5src/kernel/memory.cppMapGpuRangecallsRenderContext::MapMemoryat map. Both only record the range, because their buffer caches upload on use. AnyPS5's address-based builds bind host memory in place, so the matching work at map is the import.PPSA36082
While loading it maps 512 MiB, 6 GiB and 128 MiB of direct memory with
0x32. Its 512 MiB upload pool is freed only after a GPU fence. Without this change, its first address-based build pins those allocations on the queue the loader's fence waits on, and the pool overflows at about 2.0 s, before any import finishes (0/3). With it, the imports run on the mapping thread (done by about 0.9 s, 2.8 s and 2.8 s) and nothing is imported on the fence path. Together with #1959 (formatted buffer accesses) and #1960 (imports leave pages unprotected), it reaches the main menu in 10/10 runs (40 s each, pool peak 340-391 MiB of 512); without this change, 0/3 overflow at about 2.0 s. Three of the ten later stopped at "prepared shader artifact is missing for the requested static ABI", a separate issue after the menu.PPSA36082 also needs an import budget of at least 6784 MiB, which this change does not need: with main's 6 GiB, the 6 GiB allocation is refused at map (and logged) because 512 MiB is already imported. #290 sets the budget to max(6 GiB, RAM/2); these runs used
APS5_HOST_IMPORT_MIB=8192in its place. With #290's import windows, a request that covers a whole registered range, as this one does, still imports the whole range, which is what an address-based build imports. The runs were on mainf1451396with #861, #1367, #1249, #1905, #1959 and #1960.Tested
Linux (CachyOS), x86-64, GCC, 7800X3D / RTX 4090.
guest_memory: a libc heap allocation is not reported; a direct map with3is not reported; one with0x32is reported once, with its address and size, and the observer can take a registry lease; an mprotect to0x13is not reported. Over a reservation, a fixed map with3is not reported and one with0x32is reported once with its address and size. Clearing the observer while a call runs returns only after it finished. Never reporting, reporting maps without GPU access, reporting the heap, not reporting direct maps, reporting with the registry still locked (it deadlocks; the test times out), not reporting fixed GPU maps, reporting every fixed map, and clearing without waiting each fail it.agc_driver_host_import_at_map(new): a mapping imported before any lookup is retired when it is unmapped; a mapped allocation is imported on that device, also after another mapping changed and when a range is mapped again; a mapping unregistered before its import, and an import older than the last reconcile, are not imported; with the device lock held by another thread, a mapping on a device that already imports is imported. Not installing the pin waiter, dropping the generation check, and always taking the device lock each fail it.Checklist
main; no other open PR implements the same functionsNotImplemented_nid_no_patch); silent stubs are listed in TechnicalDebt.mdfiles or images added to the repository (attach them to this PR)Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-09T02:44:22Z.
Review: CHANGES_REQUESTED
The new observer misses an important map path.
DoMapDirect/DoMapAnonfirst callRemapFixedIntoRegisteredfor a fixed mapping over an existing reservation; that path callsMutation::Protect(..., gpu, ...)rather thanMutation::Add.GuestAllocationsProtect_nid_postfixupdates the range but never appends it toMutationState::gpuMapped, so the observer does not import a reserved range when it is mapped with GPU access. The new test only coversAdd, so it does not exercise this path. Please notify the observer for GPU-enabled fixed remaps (while preserving the documented deferred behavior for plainsceKernelMprotect, if intended) and add a reserve-then-fixed-map regression test. I will re-review the updated head.Imported from boykopovar/AnyPS5. Originally posted by Raghav314 on 2026-10-09T02:52:31Z.
Thanks, A fixed map over a reservation goes through RemapFixedIntoRegistered, which uses Protect instead of Add, so the observer never saw it. Fixed remaps with GPU access now get reported too (Mutation::NoteGpuMapping). Plain sceKernelMprotect still isn't reported, so that stays deferred to first use like the description says.
I added the regression test you suggested for in guest_memory, it reserves two pages and maps them fixed and checks that only the GPU-mapped one is reported
Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-09T03:00:58Z.
Review: APPROVED
Re-reviewed the updated head and the follow-up against the earlier request. GPU-enabled fixed remaps over reservations now call
NoteGpuMappingafter the registry mutation, while ordinarysceKernelMprotectremains deferred. The regression test covers reserve → fixed CPU map → fixed GPU map, verifies only the GPU range is observed, and also checks the observer can lease the registry after the lock is released. Linux and Windows CI and Conventions pass. This change depends on #1905 as documented; no remaining blocker found.Imported from boykopovar/AnyPS5. Originally posted by popcoder565 on 2026-10-09T09:20:13Z.
Native Windows run, since the description only covers Windows under Wine.
Setup: Windows 10 (19045), RTX 3060 Laptop (NVIDIA 616.92), MinGW-w64 GCC 15.2.0 (WinLibs r7), Release. Head
a050a7c6merged ontomaind9484c71together with #1905 (cf6ae853), which it depends on.agc_driver_host_import_at_map,guest_memoryandagc_driver_graphics: 5/5 each.No title was run.
🤖 Addressed by Claude Code
Imported from boykopovar/AnyPS5. Originally posted by Celegans12 on 2026-10-09T15:32:27Z.
I tried this with Ghost of Yōtei (PPSA26344) on Windows with an RTX 3080 Laptop GPU. It was a backport onto our older pre-#639 test tree, not current main, so treat the numbers as indicative. The change does what it says: the largest early-world address rebuild went from 19.9 s to 25 ms. But the run as a whole got a lot slower. The world showed up at 549 s instead of 278 s, and the first 23 world frames averaged 15.9 s against 7.2 s on the same build with the change switched off.
The cost is per mapping. Yōtei makes about 43.7k GPU mappings of 128 KiB each, and every
ImportGuestMemorycall takes the GPU mutex and goes throughHostImportFor. Because each new mapping bumps the registry generation, every call takes a fresh lease of the whole registry and runsrefreshImportsover all ~40k imports before importing the one range. That came to about 6 ms per mapping. The mapping thread held the GPU mutex for 272 s over the run and waited another 260 s for it, and once streaming ran into the frames the render queue paid the same reload there. Current main'sHostImportForhas the same stale-check → lease →refreshImportspath, so I'd expect the same there.Importing the range the hook is handed directly, or reconciling once per batch of mappings rather than on every call, would probably keep the gain without the cost. I'm happy to rerun a revised version.
Numbers (same build, switch on/off)
On its own, #1905 is neutral for speed here: only 182 of about 40.5k ranges lack GPU access. It does lower the import peak by about 711 MiB, which is nice since we sit right at the host-import ceiling. No new skips or errors in any of the runs.
Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-09T20:14:54Z.
Celegans12's run above shows the cost of importing at map: each GPU mapping takes a fresh registry lease and runs
refreshImportsover every import, about 6 ms per 128 KiB map, and PPSA26344 reached its world at 549 s instead of 278 s. Please make the per-mapping work independent of the number of imports (import the range the observer is handed, or reconcile once per batch) and rebase onto main (conflict intests/BdaResources.cpp).Imported from boykopovar/AnyPS5. Originally posted by Raghav314 on 2026-10-10T02:48:48Z.
Thanks for the numbers @Celegans12, the cause was what you described. Reworked, and rebased on main together with #1905 (@oneandonlydean):
The observer doesn't take a lease of the whole registry anymore: the mapping hands it just the ranges it mapped (weakly, with the registry generation it ended at).
The import at map doesn't reconcile the other imports, so its work doesn't depend on how many imports there are. A range that's gone, overlaps an existing import, or comes from before the last reconcile is left to the next lookup, like on main. The live import bytes are a running total now instead of a sum over every import.
Once the device has its import table, the import at map takes only that table's lock, not the device lock, so the mapping thread doesn't wait for the render thread anymore. The first import on a device and a device that was just replaced still go through the device lock.
AnyPS5's own libc heap doesn't trigger it anymore, only kernel GPU mappings do.
Per mapping, on top of N existing 64 KiB imports here (RTX 4090, Linux, average of 50 maps):
What's left is mostly the Vulkan import itself. The draw-time reconcile after a registry change is main's and still walks every import.
If you can rerun Yotei with this I'd like to see how it compares.
Imported from boykopovar/AnyPS5. Originally posted by SleglJan on 2026-10-10T11:08:20Z.
007 First Light (PPSA11386 v01.018.000), start-up only, on the reworked head 11563171. Linux (CachyOS, kernel 7.2.9, GCC 16.2.1, Ryzen 9 7950X3D, RTX 3080 with NVIDIA 615.71.09), Release with
ANYPS5_ENABLE_SPIRV_TOOLS=OFF.Setup:
maina29ef592. #1905 is already in thatmain, so the two builds differ only by 11563171.ShaderInfo::MaxImages64 → 128 experiment but not its sampled-heap one (mainhas 64 since #2347). In every run of both builds, 32 shaders fail preparation withshader image heap capacity exceeded; this is logged and not fatal.APS5_TRACE_DRAWS=1). It says nothing about in-world frame times, which is what the PPSA26344 run above measured on the earlier head.main+ stack run filled the cache, and 9 warm runs (the first with this PR) reused it (0 cache writes, 32 misses each).main+ stackmain+ stackguest memory changed while GPU results were pending; keeping the CPU's blocksonce for a storage image. With it, none does.AI-assisted: yes (Claude Code)
Imported from boykopovar/AnyPS5. Originally posted by klosixgaming-arch on 2026-10-10T14:14:35Z.
Independent validation on real hardware
Built and ran the PR head
11563171on my own box:Release,BUILD_TESTING=ON.Results:
agc_driver_host_import_at_map(the new dedicated test) — PASSES (exit 0) on the 5090.guest_memoryadditions all pass on my box:CheckDirectMemoryGpuProtBits(prot-bit reporting),CheckGpuAccessFollowsProtection,CheckGpuMapsAreObserved, andCheckClearingTheObserverWaitsForItsCallsall run and pass — the test proceeds well past them.One failure I tracked down (not a regression from this PR): the full
guest_memorybinary aborts on my Windows box insideCheckReadsIntoSharedWriteTrackingat theRequire(refused)check (asceKernelPreadinto a page justmprotected toPROT_READis expected to throw). I confirmed this is pre-existing on my box, not caused by #1961: I checked out the PR's base3d1a7d90(which lacks this PR'sCheckGpuMapsAreObserved/CheckGpuAccessFollowsProtection/CheckClearingTheObserverWaitsForItsCalls) and the same check also fails there (GuestMemory.cpp:992, the identicalRequire(refused)pread assertion). So on my Windows/MinGW setup that shared-write-tracking/AIO pread-refusal check does not hold, with or without this PR. Worth a maintainer look, but I don't believe it's this PR's doing.Honest limits of what I verified:
APS5_HOST_IMPORT_MIBbudget interaction are unverified on my end.Written with AI assistance.
Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-10T22:30:28Z.
Rebased onto current
main; the only conflict was incore/libs/tests/GuestMemory.cpp(the new observer tests next toCheckHintedVirtualReservation), resolved by keeping both. Scoped build and ctest of libs, libc, libkernel and AgcDriver are green;agc_driver_host_import_at_mapandguest_memorypass, and both fail without the change. Merging once CI is green.