fix(agc): import GPU-mapped memory on a host stack #4662

Merged
fdfawef324 merged 1 commit from upstream-pr/4662 into main 2026-10-13 04:50:13 +00:00
fdfawef324 commented 2026-10-12 18:17:26 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by fdfawef324 on 2026-10-12T18:17:26Z.


What

Demon's Souls (PPSA01342 v1.005), on main with #425, #2904 and #757 applied (it stops earlier without them), aborted at random points during the intro in the previous round of this PR:

FATAL: abort() (SIGABRT) on thread 42400
    #0 0x00007ffa2f99a9b8 libc.prx+0xa9b8
    #1 0x00007ffa2fa2f4c1 libc.prx+0x9f4c1
    #2 0x0000000268783c80

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_fiber measures the fiber stack used by a whole GPU mapping made on a fiber: Mutation::Add, NoteGpuMapping, the registry walk in GuestAllocationsEnd, the observer, and the fiber's entry and return. On NVIDIA that is 10,616 bytes with the import on the fiber (current main) 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:

  • Inline when the stack pointer lies inside the thread's stack bounds with at least 256 KiB below it. On Windows the bounds come from 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 from pthread_getattr_np, cached per thread; a fiber's stack lies outside them.
  • An ordinary host or guest thread with a large stack therefore imports inline as on main and starts no thread. Fibers and threads with less than 256 KiB of stack left import on a host thread.
  • If that host thread cannot be started (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 from GuestAllocationsEnd_nid_postfix, which is noexcept, 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::thread cost 0.14-33 ms (minimum) and 1.3-115 ms (median) in the same runs.

ordinary thread, min ordinary thread, median fiber, min fiber, median
main, inline everywhere 102-187 us 229-429 us 91-106 us 257-464 us
previous revision, a host thread for every mapping 0.55-13.4 ms 0.78-87 ms 1.2-5.8 ms 62-112 ms
this revision 96-119 us 124-279 us 0.77-5.7 ms 17-97 ms

The 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_fiber now 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::GpuMapImportThreads counts the threads). The test frees its mapped memory.

Tested

Windows Server 2025, RTX 3060, GCC 15.2. This round:

  • agc_driver_gpu_map_on_fiber with the import always inline (as on main): 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 on main at 1acfa7f78. The green run was repeated on main at 825620ae5 (the base of this commit), which changed neither file.
  • ctest --test-dir build-dev/core/libs/prx/libSceAgcDriver -j 4 on main at 3e0e01f35, the previous base (451 tests; the later main change in the library, Pipeline.cpp from #3846, is not covered by a full rerun): 441 passed, 1 skipped (agc_driver_stencil_ref_export), 9 failed. The same 9 also failed with main's Lifecycle.cpp (checked at 682ef0c17 and cf4557e5d): 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_5 and agc_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 with vkDeviceWaitIdle: 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 #ifdef around 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

  • Based on current main; no other open PR implements the same functions
  • One topic per PR; follow-ups go in a new PR
  • No comments in code except technical debt
  • Unimplemented paths throw (NotImplemented_nid_no_patch); silent stubs are listed under Silent stubs in their library's technical debt file
  • No notes, investigation .md files or images added to the repository (attach them to this PR)
  • New third-party code is a submodule built from source
  • Generic behaviour, not specific to one title
  • Depends on: none
  • Unblocks: PPSA01342 #1283
  • AI-assisted: yes

🤖 Generated with Claude Code

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4833332022 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4662). Originally posted by [fdfawef324](https://github.com/fdfawef324) on 2026-10-12T18:17:26Z. --- ### What Demon's Souls (PPSA01342 v1.005), on `main` with #425, #2904 and #757 applied (it stops earlier without them), aborted at random points during the intro in the previous round of this PR: ``` FATAL: abort() (SIGABRT) on thread 42400 #0 0x00007ffa2f99a9b8 libc.prx+0xa9b8 #1 0x00007ffa2fa2f4c1 libc.prx+0x9f4c1 #2 0x0000000268783c80 ``` 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_fiber` measures the fiber stack used by a whole GPU mapping made on a fiber: `Mutation::Add`, `NoteGpuMapping`, the registry walk in `GuestAllocationsEnd`, the observer, and the fiber's entry and return. On NVIDIA that is 10,616 bytes with the import on the fiber (current `main`) 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: - Inline when the stack pointer lies inside the thread's stack bounds with at least 256 KiB below it. On Windows the bounds come from `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 from `pthread_getattr_np`, cached per thread; a fiber's stack lies outside them. - An ordinary host or guest thread with a large stack therefore imports inline as on `main` and starts no thread. Fibers and threads with less than 256 KiB of stack left import on a host thread. - If that host thread cannot be started (`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 from `GuestAllocationsEnd_nid_postfix`, which is `noexcept`, 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::thread` cost 0.14-33 ms (minimum) and 1.3-115 ms (median) in the same runs. | | ordinary thread, min | ordinary thread, median | fiber, min | fiber, median | |---|---|---|---|---| | `main`, inline everywhere | 102-187 us | 229-429 us | 91-106 us | 257-464 us | | previous revision, a host thread for every mapping | 0.55-13.4 ms | 0.78-87 ms | 1.2-5.8 ms | 62-112 ms | | this revision | 96-119 us | 124-279 us | 0.77-5.7 ms | 17-97 ms | The 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_fiber` now 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::GpuMapImportThreads` counts the threads). The test frees its mapped memory. ### Tested Windows Server 2025, RTX 3060, GCC 15.2. This round: - `agc_driver_gpu_map_on_fiber` with the import always inline (as on `main`): 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 on `main` at 1acfa7f78. The green run was repeated on `main` at 825620ae5 (the base of this commit), which changed neither file. - `ctest --test-dir build-dev/core/libs/prx/libSceAgcDriver -j 4` on `main` at 3e0e01f35, the previous base (451 tests; the later `main` change in the library, `Pipeline.cpp` from #3846, is not covered by a full rerun): 441 passed, 1 skipped (`agc_driver_stencil_ref_export`), 9 failed. The same 9 also failed with `main`'s `Lifecycle.cpp` (checked at 682ef0c17 and cf4557e5d): `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_5` and `agc_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 with `vkDeviceWaitIdle: 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 `#ifdef` around 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 - [x] Based on current `main`; no other open PR implements the same functions - [x] One topic per PR; follow-ups go in a new PR - [x] No comments in code except [technical debt](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/CONVENTIONS.md) - [x] Unimplemented paths throw (`NotImplemented_nid_no_patch`); silent stubs are listed under Silent stubs in their library's [technical debt](https://github.com/boykopovar/AnyPS5/tree/main/docs/dev/debt) file - [x] No notes, investigation `.md` files or images added to the repository (attach them to this PR) - [x] New third-party code is a submodule built from source - [x] Generic behaviour, not specific to one title - [x] Depends on: none - [x] Unblocks: PPSA01342 #1283 - [x] AI-assisted: yes 🤖 Generated with [Claude Code](https://claude.com/claude-code)
github-actions[bot] commented 2026-10-12 19:54:32 +00:00 (Migrated from github.com)

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.

Pull request Conflicting files (lines on main) Same exports Same new files
#4724 core/libs/prx/libSceAgcDriver/CMakeLists.txt L1091
<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6131562345 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4662#issuecomment-6131562345). Originally posted by [github-actions[bot]](https://github.com/apps/github-actions) on 2026-10-12T19:54:32Z. --- <!-- pr-overlap --> 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**. | Pull request | Conflicting files (lines on `main`) | Same exports | Same new files | |---|---|---|---| | #4724 | `core/libs/prx/libSceAgcDriver/CMakeLists.txt` [L1091](https://github.com/boykopovar/AnyPS5/blob/61e4b5506ce7bb5ce6fdd1c63a5396e589206143/core/libs/prx/libSceAgcDriver/CMakeLists.txt#L1091) | | |
Scramblrrrr commented 2026-10-12 20:08:56 +00:00 (Migrated from github.com)

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

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6131775414 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4662#issuecomment-6131775414). Originally posted by [Scramblrrrr](https://github.com/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.
fdfawef324 commented 2026-10-12 20:47:34 +00:00 (Migrated from github.com)

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):

  • A: the import now runs inline when the stack pointer is inside the thread's stack bounds with at least 256 KiB left (Windows: TIB via 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.
  • B: a std::system_error from starting the host thread is now caught and reported as a failed import. The catch (...) / std::rethrow_exception is gone: the import catches std::exception itself, so nothing reached it.
  • C: left open and stated in the body. With the GPU mutex held, the import stays inline, because a host thread could wait for that mutex forever.
  • D: the test frees its VirtualAlloc / aligned_alloc block.
  • E: the body now says the 10,616 bytes (10,520 before the rebase) cover the whole mapping scope on the fiber, with about 7.5 KiB for the import. The heap-corruption counts are removed. It says Linux was neither built nor run, and adds Unblocks: PPSA01342 #1283.
  • F: unchanged, and the body says so.
  • Red/green: always inline fails (10,616 bytes on the fiber), a host thread for every mapping fails (1 thread from the main thread), and this revision passes (2,960 bytes).
  • No findings were refuted.
<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6132340467 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4662#issuecomment-6132340467). Originally posted by [fdfawef324](https://github.com/fdfawef324) on 2026-10-12T20:47:34Z. --- Changes in this revision (rebased onto current `main`, still one commit): - A: the import now runs inline when the stack pointer is inside the thread's stack bounds with at least 256 KiB left (Windows: TIB via `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. - B: a `std::system_error` from starting the host thread is now caught and reported as a failed import. The `catch (...)` / `std::rethrow_exception` is gone: the import catches `std::exception` itself, so nothing reached it. - C: left open and stated in the body. With the GPU mutex held, the import stays inline, because a host thread could wait for that mutex forever. - D: the test frees its `VirtualAlloc` / `aligned_alloc` block. - E: the body now says the 10,616 bytes (10,520 before the rebase) cover the whole mapping scope on the fiber, with about 7.5 KiB for the import. The heap-corruption counts are removed. It says Linux was neither built nor run, and adds `Unblocks: PPSA01342 #1283`. - F: unchanged, and the body says so. - Red/green: always inline fails (10,616 bytes on the fiber), a host thread for every mapping fails (1 thread from the main thread), and this revision passes (2,960 bytes). - No findings were refuted.
Scramblrrrr commented 2026-10-12 20:54:06 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Scramblrrrr on 2026-10-12T20:54:06Z.


Cross-test of revised #4662 ef022d53b8 on 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.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6132434942 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4662#issuecomment-6132434942). Originally posted by [Scramblrrrr](https://github.com/Scramblrrrr) on 2026-10-12T20:54:06Z. --- Cross-test of revised #4662 ef022d53b8f04bb7b2a675efffa9c433b2495f98 on 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.
kr4y4 commented 2026-10-12 22:41:12 +00:00 (Migrated from github.com)

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 ef022d53b merged into main 584cc4c57.

  • agc_driver_gpu_map_on_fiber passes on both GPUs with --repeat until-fail:3.
  • Full suite (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_5 and agc_driver_pixel_interlock. So here they don't depend on this change, and they pass on both GPUs.

Written with AI assistance.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6134003961 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4662#issuecomment-6134003961). Originally posted by [kr4y4](https://github.com/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 ef022d53b merged into `main` 584cc4c57. - `agc_driver_gpu_map_on_fiber` passes on both GPUs with `--repeat until-fail:3`. - Full suite (`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_5` and `agc_driver_pixel_interlock`. So here they don't depend on this change, and they pass on both GPUs. Written with AI assistance.
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
boykopovar/AnyPS5!4662
No description provided.