fix(agc): import GPU-mapped memory on a host stack #4662
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/4662"
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 fdfawef324 on 2026-10-12T18:17:26Z.
What
Demon's Souls (PPSA01342 v1.005), on
mainwith #425, #2904 and #757 applied (it stops earlier without them), aborted at random points during the intro in the previous round of this PR:Frame #2 lies inside the job fiber contexts at 0x268780000.
The abort comes from the title's own stack check (
__stack_chk_fail) in a BPE job fiber. Those fibers run on 16 KiB contexts that the title packs back to back in guest memory, with no guard page. When a fiber maps GPU memory, the GPU-map observer imports the mapping into Vulkan on that same stack, so a fiber that maps memory deep in a job can overrun its context into the next fiber's context.agc_driver_gpu_map_on_fibermeasures the fiber stack used by a whole GPU mapping made on a fiber:Mutation::Add,NoteGpuMapping, the registry walk inGuestAllocationsEnd, the observer, and the fiber's entry and return. On NVIDIA that is 10,616 bytes with the import on the fiber (currentmain) and 2,960 bytes with the import on a host thread, so the import accounts for about 7.5 KiB.The observer now imports inline when the current stack has room for it, and on a host thread it starts and joins otherwise:
GetCurrentThreadStackLimits, which reads the TIB; libSceFiber sets the TIB bounds to the fiber context while a fiber runs, so a fiber shows its own small context. On Linux they come frompthread_getattr_np, cached per thread; a fiber's stack lies outside them.mainand starts no thread. Fibers and threads with less than 256 KiB of stack left import on a host thread.std::system_error), the observer prints[gpu] import of GPU memory at its mapping failed: no host thread to run it on: ...and the mapping carries on, as it does for any other failed import. The observer runs fromGuestAllocationsEnd_nid_postfix, which isnoexcept, so nothing is rethrown from it any more.Left open: when the mapping thread already holds the GPU mutex, the import still runs inline, even on a fiber. When the import cannot finish without the lock, a host thread would wait for that mutex while the mapping thread waits for the host thread.
Unchanged: a failed import is printed to stderr and the mapping carries on, as on
main.Cost per mapping (Add + NoteGpuMapping + import of a 256 KiB block; 60 mappings per case, 3 runs, range of the per-run minimum and median). Every run was made while other jobs kept the CPU at 100%, so the medians mostly measure scheduling. Starting a bare
std::threadcost 0.14-33 ms (minimum) and 1.3-115 ms (median) in the same runs.main, inline everywhereThe offloaded path on a fiber is dominated by starting the thread. In a standalone program on the same machine, handing work to a thread that waits on a condition variable took 41 us (median) while starting a thread took 26-59 ms (median). A persistent worker would therefore be faster, but it would make imports from different fibers wait for each other; it is not part of this PR.
agc_driver_gpu_map_on_fibernow also requires that the mapping on the fiber start exactly one host thread and that the same mapping made from the main thread start none (Driver::GpuMapImportThreadscounts the threads). The test frees its mapped memory.Tested
Windows Server 2025, RTX 3060, GCC 15.2. This round:
agc_driver_gpu_map_on_fiberwith the import always inline (as onmain): fails, the fiber used 10,616 bytes. With a host thread for every mapping (previous revision): fails, the mapping from the main thread started 1 host thread. With this change: passes, 2,960 bytes. The red runs were made onmainat1acfa7f78. The green run was repeated onmainat825620ae5(the base of this commit), which changed neither file.ctest --test-dir build-dev/core/libs/prx/libSceAgcDriver -j 4onmainat3e0e01f35, the previous base (451 tests; the latermainchange in the library,Pipeline.cppfrom #3846, is not covered by a full rerun): 441 passed, 1 skipped (agc_driver_stencil_ref_export), 9 failed. The same 9 also failed withmain'sLifecycle.cpp(checked at682ef0c17andcf4557e5d):agc_driver_flat_loads,agc_driver_flat_loads_x2_x3,agc_driver_flat_loads_x4,agc_driver_global_loads,agc_driver_global_loads_x2_x3,agc_driver_global_loads_x4,agc_driver_flat_apertures_2,agc_driver_flat_apertures_5andagc_driver_pixel_interlock. Most abort after 60 s with "guest allocation ... is still pinned by GPU work or an import".agc_driver_pixel_interlock, and in some runs one of the others, fail withvkDeviceWaitIdle: Vulkan result -4. The heap-corruption exit from the earlier rounds did not occur.python -I tools/check_test_registration.py build-dev: passes.Not tested in this round: Linux (the change has no
#ifdefaround the decision and runs there too; only the stack-bounds query differs, and it was not built or run on Linux), and no title was run. The Demon's Souls runs in the previous round (3 intro runs aborted before the change, 4 runs of 9-11 minutes did not abort after it) used the previous revision, which imported on a host thread for every mapping; the fiber path of this revision imports the same way.Checklist
main; no other open PR implements the same functionsNotImplemented_nid_no_patch); silent stubs are listed under Silent stubs in their library's technical debt file.mdfiles or images added to the repository (attach them to this PR)🤖 Generated with Claude Code
Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-12T19:54:32Z.
Other open pull requests touch the same code. Merging one will break the other, or both implement the same thing. If this one builds on them, list them in Depends on.
main)core/libs/prx/libSceAgcDriver/CMakeLists.txtL1091Imported from boykopovar/AnyPS5. Originally posted by Scramblrrrr on 2026-10-12T20:08:56Z.
Native Windows cross-test on RTX5070Ti/GCC15.2: cherry-picked #4662 ce5eef555d095132a8f13541676a6318287cba0f onto our 007 recovery integration (public head418ecdc86). agc_driver_gpu_map_on_fiber PASS0.78s; reported3120 bytes. Production-only negative restored Lifecycle.cpp from pre-change5b2d691f while leaving fixture/CMake unchanged: FAIL0.41s, reported11080 bytes vs8192 threshold. Restore/rebuild PASS0.42s. No skip/mocks for this case. Integration branch codex/first-light-fiber-import-cross-test; game cross-test next. This does not yet establish a 007 crash fix or FPS gain. AI-assisted: Codex.
Imported from boykopovar/AnyPS5. Originally posted by fdfawef324 on 2026-10-12T20:47:34Z.
Changes in this revision (rebased onto current
main, still one commit):GetCurrentThreadStackLimits, which libSceFiber points at the fiber context; Linux:pthread_getattr_np). It runs on a host thread only for fibers and short stacks. The test now also requires one host thread for the fiber mapping and none for a mapping from the main thread (Driver::GpuMapImportThreads). Cost per mapping before and after is in the body. Every run was made under 100% CPU load from other jobs, so the offloaded path's numbers are mostly the time to start a thread.std::system_errorfrom starting the host thread is now caught and reported as a failed import. Thecatch (...)/std::rethrow_exceptionis gone: the import catchesstd::exceptionitself, so nothing reached it.VirtualAlloc/aligned_allocblock.Unblocks: PPSA01342 #1283.Imported from boykopovar/AnyPS5. Originally posted by Scramblrrrr on 2026-10-12T20:54:06Z.
Cross-test of revised #4662
ef022d53b8on Windows/RTX5070Ti/GCC15.2, integration a22926660 (recovery5b plus revised PR only). Native agc_driver_gpu_map_on_fiber PASS0.68s,3120 bytes; assertions also verify one host thread for fiber mapping and none for main-thread mapping. Production-only always-inline negative FAIL0.40s/11080 bytes vs8192. Production-only always-offload negative FAIL0.43s/main-thread import spawned1 thread instead of0. Fixture/CMake unchanged for both controls. Restored/touched/rebuilt positive PASS0.39s. Clean source/diff-check. Public fork branch codex/first-light-fiber-short-stack-cross-test. Game run next; not a 007 crash-fix or FPS claim. AI-assisted: Codex.Imported from boykopovar/AnyPS5. Originally posted by kr4y4 on 2026-10-12T22:41:12Z.
A run on two more GPUs, and the full suite on the current
main.Setup: Windows 11 (26200), WinLibs GCC 15.2.0 r7, Release, CI configure flags, an RTX 4070 Laptop GPU and an Intel Raptor Lake-S iGPU. The head
ef022d53bmerged intomain584cc4c57.agc_driver_gpu_map_on_fiberpasses on both GPUs with--repeat until-fail:3.ctest --timeout 120 --parallel 4): 1019 of 1019 on both GPUs. That includes the nine that failed on your machine with and without this change:agc_driver_flat_loads(and_x2_x3,_x4),agc_driver_global_loads(and_x2_x3,_x4),agc_driver_flat_apertures_2,agc_driver_flat_apertures_5andagc_driver_pixel_interlock. So here they don't depend on this change, and they pass on both GPUs.Written with AI assistance.