fix(libkernel): unmap released direct memory views before freeing the physical range #7448

Merged
Tamalero merged 3 commits from upstream-pr/7448 into main 2026-10-19 21:27:30 +00:00
Tamalero commented 2026-10-18 07:00:28 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Tamalero on 2026-10-18T07:00:28Z.


What

sceKernelReleaseDirectMemory and sceKernelCheckedReleaseDirectMemory returned the range to the physical allocator (DirectMemoryFree / DirectMemoryCheckedFree) before they unmapped its views (UnmapDirectMemoryViews). The unmap waits in GuestAllocationsRequireUnpinned until GPU leases on the view are gone, and that wait can be long (#7408). Meanwhile another allocation can take the freed pages and map them, while the old view is still mapped and host-imported. GPU writes through the old view then land in the new owner's memory.

LEGO Batman: Legacy of the Dark Knight (PPSA16833) hits this on almost every boot when its files load fast. It releases a slice of its render-target block, then allocates the same physical pages for its command-buffer heap during the wait. A host import of the old render-target block is created again before the unmap ends. The title then stops on its own command buffers, now holding image rows where packets should be: AGC driver: unsupported PM4 packet type, truncated PM4 packet, or sceAgcSetShRegIndirectPatchSetAddress: unexpected packet type, size or flags. #7408 has the memory and import trace of one such boot.

Fix: both calls unmap the views first and free the range afterwards. The checked call still returns SCE_KERNEL_ERROR_ENOENT for a range that is not fully allocated, before it touches any view. For that check, the pool gets a non-destructive DirectMemoryAllocated, which tests the same pages CheckedFree would.

This conflicts textually with #7105, which edits the same lines to return EACCES. Whichever lands second keeps the order: unmap, then free.

Tested

  • CachyOS (Linux 7.2), GCC, Release, RTX 4090.
  • New CheckReleaseKeepsPhysicalUntilViewsUnmapped in guest_memory_tests, for both release calls. A lease holds the mapping while another thread releases the range. While that release still waits, the physical range must not be allocatable again. After the lease is dropped, the release finishes and the same range can be allocated. guest_memory, guest_memory_lifetime and guest_memory_pool_stats pass (guest_memory 0.6 s).
  • Negative check: with Export.cpp restored to main, the new test fails. It stops at the check that the range cannot be allocated again during the wait (GuestMemory.cpp:1589): on main the pages are handed out while the view is still pinned.
  • python3 tools/check_conventions.py --base origin/main: passes.
  • PPSA16833, files on an NVMe SSD, same build otherwise. Without the change, the boot stopped on a corrupted command buffer at the animated main menu in 8 of 9 runs, on builds from 5d9dcc42d to 2626e5ee7. With the change: 2 of 2 runs passed the menu, and one of them went on into gameplay. Blocky rendering corruption remains on parts of the menu scene; I have not established whether it predates this change, and it is not a stop. With the files on an HDD the stop was rarer (0 of 2 runs tonight, none in about 20 earlier runs), which fits a race that depends on how soon the pages are allocated again.
  • Not covered: #7408's long lease wait itself is unchanged. The release now waits on it before freeing, so the title pauses instead of corrupting memory.

Checklist

  • Based on current main; no other open PR implements the same functions (#7105 touches the same lines for a different change)
  • 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
  • Fixes: none (related: #7408)
  • Unblocks: PPSA16833 (boot past the animated main menu with fast storage)
  • GPUs tested: n/a (libkernel change; the title runs were on an RTX 4090, NVIDIA 615.78.08, Linux)
  • AI-assisted: yes

Tested and written by Claude (AI assistant) on a contributor's machine.

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4907115115 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/7448). Originally posted by [Tamalero](https://github.com/Tamalero) on 2026-10-18T07:00:28Z. --- ### What `sceKernelReleaseDirectMemory` and `sceKernelCheckedReleaseDirectMemory` returned the range to the physical allocator (`DirectMemoryFree` / `DirectMemoryCheckedFree`) **before** they unmapped its views (`UnmapDirectMemoryViews`). The unmap waits in `GuestAllocationsRequireUnpinned` until GPU leases on the view are gone, and that wait can be long (#7408). Meanwhile another allocation can take the freed pages and map them, while the old view is still mapped and host-imported. GPU writes through the old view then land in the new owner's memory. LEGO Batman: Legacy of the Dark Knight (PPSA16833) hits this on almost every boot when its files load fast. It releases a slice of its render-target block, then allocates the same physical pages for its command-buffer heap during the wait. A host import of the old render-target block is created again before the unmap ends. The title then stops on its own command buffers, now holding image rows where packets should be: `AGC driver: unsupported PM4 packet type`, `truncated PM4 packet`, or `sceAgcSetShRegIndirectPatchSetAddress: unexpected packet type, size or flags`. #7408 has the memory and import trace of one such boot. **Fix:** both calls unmap the views first and free the range afterwards. The checked call still returns `SCE_KERNEL_ERROR_ENOENT` for a range that is not fully allocated, before it touches any view. For that check, the pool gets a non-destructive `DirectMemoryAllocated`, which tests the same pages `CheckedFree` would. This conflicts textually with #7105, which edits the same lines to return `EACCES`. Whichever lands second keeps the order: unmap, then free. ### Tested - CachyOS (Linux 7.2), GCC, Release, RTX 4090. - New `CheckReleaseKeepsPhysicalUntilViewsUnmapped` in `guest_memory_tests`, for both release calls. A lease holds the mapping while another thread releases the range. While that release still waits, the physical range must not be allocatable again. After the lease is dropped, the release finishes and the same range can be allocated. `guest_memory`, `guest_memory_lifetime` and `guest_memory_pool_stats` pass (`guest_memory` 0.6 s). - **Negative check:** with `Export.cpp` restored to `main`, the new test fails. It stops at the check that the range cannot be allocated again during the wait (`GuestMemory.cpp:1589`): on `main` the pages are handed out while the view is still pinned. - `python3 tools/check_conventions.py --base origin/main`: passes. - PPSA16833, files on an NVMe SSD, same build otherwise. Without the change, the boot stopped on a corrupted command buffer at the animated main menu in 8 of 9 runs, on builds from `5d9dcc42d` to `2626e5ee7`. With the change: 2 of 2 runs passed the menu, and one of them went on into gameplay. Blocky rendering corruption remains on parts of the menu scene; I have not established whether it predates this change, and it is not a stop. With the files on an HDD the stop was rarer (0 of 2 runs tonight, none in about 20 earlier runs), which fits a race that depends on how soon the pages are allocated again. - Not covered: #7408's long lease wait itself is unchanged. The release now waits on it before freeing, so the title pauses instead of corrupting memory. ### Checklist - [x] Based on current `main`; no other open PR implements the same functions (#7105 touches the same lines for a different change) - [x] One topic per PR; follow-ups go in a new PR - [x] No comments in code except technical debt - [x] Unimplemented paths throw (`NotImplemented_nid_no_patch`); silent stubs are listed under Silent stubs in their library's technical 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] Fixes: none (related: #7408) - [x] Unblocks: PPSA16833 (boot past the animated main menu with fast storage) - [x] GPUs tested: n/a (libkernel change; the title runs were on an RTX 4090, NVIDIA 615.78.08, Linux) - [x] AI-assisted: yes *Tested and written by Claude (AI assistant) on a contributor's machine.*
github-actions[bot] commented 2026-10-18 07:14:12 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-18T07:14:12Z.


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
PR 6565 core/libs/tests/GuestMemory.cpp L2894
PR 7471 core/libs/tests/GuestMemory.cpp L1572, L2893-2894

Some comparisons could not be completed because a pull request conflicts with the base branch. Review these shared files manually before concluding that there is no overlap.

Pull request Shared files not compared
PR 4449 core/libs/tests/GuestMemory.cpp
PR 5021 core/libs/prx/libkernel/DirectMemory/Export.cpp
core/libs/tests/GuestMemory.cpp
PR 6044 core/libs/tests/GuestMemory.cpp
PR 6321 core/libs/tests/GuestMemory.cpp
PR 6469 core/libs/tests/GuestMemory.cpp
PR 7105 core/libs/prx/libkernel/DirectMemory/DirectMemory.hpp
core/libs/prx/libkernel/DirectMemory/Export.cpp
core/libs/prx/libkernel/DirectMemory/MemoryPool.cpp
core/libs/tests/GuestMemory.cpp
PR 7149 core/libs/prx/libkernel/DirectMemory/DirectMemory.hpp
PR 7201 core/libs/prx/libkernel/DirectMemory/Export.cpp
PR 7332 core/libs/prx/libkernel/DirectMemory/Export.cpp
core/libs/tests/GuestMemory.cpp
PR 7473 core/libs/tests/GuestMemory.cpp
<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6247023624 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/7448#issuecomment-6247023624). Originally posted by [github-actions[bot]](https://github.com/apps/github-actions) on 2026-10-18T07:14:12Z. --- <!-- 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 | |---|---|---|---| | [PR 6565](https://www.github.com/boykopovar/AnyPS5/pull/6565) | `core/libs/tests/GuestMemory.cpp` [L2894](https://github.com/boykopovar/AnyPS5/blob/e004d03ceb460c5b802b7905a8a88a2a3db26b46/core/libs/tests/GuestMemory.cpp#L2894) | | | | [PR 7471](https://www.github.com/boykopovar/AnyPS5/pull/7471) | `core/libs/tests/GuestMemory.cpp` [L1572](https://github.com/boykopovar/AnyPS5/blob/e004d03ceb460c5b802b7905a8a88a2a3db26b46/core/libs/tests/GuestMemory.cpp#L1572), [L2893-2894](https://github.com/boykopovar/AnyPS5/blob/e004d03ceb460c5b802b7905a8a88a2a3db26b46/core/libs/tests/GuestMemory.cpp#L2893-L2894) | | | Some comparisons could not be completed because a pull request conflicts with the base branch. Review these shared files manually before concluding that there is no overlap. | Pull request | Shared files not compared | |---|---| | [PR 4449](https://www.github.com/boykopovar/AnyPS5/pull/4449) | `core/libs/tests/GuestMemory.cpp` | | [PR 5021](https://www.github.com/boykopovar/AnyPS5/pull/5021) | `core/libs/prx/libkernel/DirectMemory/Export.cpp`<br>`core/libs/tests/GuestMemory.cpp` | | [PR 6044](https://www.github.com/boykopovar/AnyPS5/pull/6044) | `core/libs/tests/GuestMemory.cpp` | | [PR 6321](https://www.github.com/boykopovar/AnyPS5/pull/6321) | `core/libs/tests/GuestMemory.cpp` | | [PR 6469](https://www.github.com/boykopovar/AnyPS5/pull/6469) | `core/libs/tests/GuestMemory.cpp` | | [PR 7105](https://www.github.com/boykopovar/AnyPS5/pull/7105) | `core/libs/prx/libkernel/DirectMemory/DirectMemory.hpp`<br>`core/libs/prx/libkernel/DirectMemory/Export.cpp`<br>`core/libs/prx/libkernel/DirectMemory/MemoryPool.cpp`<br>`core/libs/tests/GuestMemory.cpp` | | [PR 7149](https://www.github.com/boykopovar/AnyPS5/pull/7149) | `core/libs/prx/libkernel/DirectMemory/DirectMemory.hpp` | | [PR 7201](https://www.github.com/boykopovar/AnyPS5/pull/7201) | `core/libs/prx/libkernel/DirectMemory/Export.cpp` | | [PR 7332](https://www.github.com/boykopovar/AnyPS5/pull/7332) | `core/libs/prx/libkernel/DirectMemory/Export.cpp`<br>`core/libs/tests/GuestMemory.cpp` | | [PR 7473](https://www.github.com/boykopovar/AnyPS5/pull/7473) | `core/libs/tests/GuestMemory.cpp` |
Bonjourcestmatt commented 2026-10-18 07:25:59 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Bonjourcestmatt on 2026-10-18T07:25:59Z.


Independent Windows build of this PR (head 268e96df8b224c2a09385e9c2b975e2bfbfdea18) merged onto origin/main 1c523bdb48f724c06d0f526ddfbcd4c3a22bbea0.

Setup: Windows 11, WinLibs GCC 15.2.0 UCRT, Ninja, Release, BUILD_TESTING=ON.

Target guest_memory_tests main main + this PR
build OK fails
ctest guest_memory passes not run (no binary)

The error is in the new test, core/libs/tests/GuestMemory.cpp:2867 and :2868:

invalid conversion from 'int (__attribute__((sysv_abi)) *)(int64_t, std::size_t)'
to 'int (*)(int64_t, std::size_t)'

CheckReleaseKeepsPhysicalUntilViewsUnmapped takes a plain int (*)(std::int64_t, std::size_t), but sceKernelReleaseDirectMemory and sceKernelCheckedReleaseDirectMemory are declared sysv_abi, so the pointer types differ on Windows. Declaring the parameter as int (__attribute__((sysv_abi)) *release)(std::int64_t, std::size_t) (or a small alias for it) should fix it; I did not try that, so I do not know whether the rest of the new test passes on Windows.

Not checked: Linux/macOS, the non-GPU suite, and the behaviour of the fix itself.

Written with AI assistance (Claude Code). I ran the builds and tests above on my machine.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6247119973 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/7448#issuecomment-6247119973). Originally posted by [Bonjourcestmatt](https://github.com/Bonjourcestmatt) on 2026-10-18T07:25:59Z. --- Independent Windows build of this PR (head `268e96df8b224c2a09385e9c2b975e2bfbfdea18`) merged onto `origin/main` `1c523bdb48f724c06d0f526ddfbcd4c3a22bbea0`. Setup: Windows 11, WinLibs GCC 15.2.0 UCRT, Ninja, Release, `BUILD_TESTING=ON`. | Target `guest_memory_tests` | main | main + this PR | |---|---|---| | build | OK | **fails** | | ctest `guest_memory` | passes | not run (no binary) | The error is in the new test, `core/libs/tests/GuestMemory.cpp:2867` and `:2868`: ``` invalid conversion from 'int (__attribute__((sysv_abi)) *)(int64_t, std::size_t)' to 'int (*)(int64_t, std::size_t)' ``` `CheckReleaseKeepsPhysicalUntilViewsUnmapped` takes a plain `int (*)(std::int64_t, std::size_t)`, but `sceKernelReleaseDirectMemory` and `sceKernelCheckedReleaseDirectMemory` are declared `sysv_abi`, so the pointer types differ on Windows. Declaring the parameter as `int (__attribute__((sysv_abi)) *release)(std::int64_t, std::size_t)` (or a small alias for it) should fix it; I did not try that, so I do not know whether the rest of the new test passes on Windows. Not checked: Linux/macOS, the non-GPU suite, and the behaviour of the fix itself. Written with AI assistance (Claude Code). I ran the builds and tests above on my machine.
Tamalero commented 2026-10-18 07:57:03 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Tamalero on 2026-10-18T07:57:03Z.


@Bonjourcestmatt thanks for the Windows build. You're right: the test took a plain function pointer, and the release calls are declared sysv_abi. Pushed a follow-up commit that declares the parameter as decltype(&sceKernelReleaseDirectMemory), so it carries the calling convention on every platform. guest_memory_tests still passes on Linux. I can't build on Windows here, so a rerun on your side would confirm the rest of the test.

Tested and written by Claude (AI assistant) on a contributor's machine.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6247372117 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/7448#issuecomment-6247372117). Originally posted by [Tamalero](https://github.com/Tamalero) on 2026-10-18T07:57:03Z. --- @Bonjourcestmatt thanks for the Windows build. You're right: the test took a plain function pointer, and the release calls are declared `sysv_abi`. Pushed a follow-up commit that declares the parameter as `decltype(&sceKernelReleaseDirectMemory)`, so it carries the calling convention on every platform. `guest_memory_tests` still passes on Linux. I can't build on Windows here, so a rerun on your side would confirm the rest of the test. *Tested and written by Claude (AI assistant) on a contributor's machine.*
MarcZX10 commented 2026-10-18 15:04:53 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-18T15:04:53Z.

Review: APPROVED


Reviewed the release ordering and checked-release prevalidation. Keeping the physical pages allocated while UnmapDirectMemoryViews waits prevents another allocation from reusing the range before the old views are gone; DirectMemoryAllocated also preserves the ENOENT no-side-effect path. The regression test exercises both release APIs while a guest allocation lease is held. I reviewed the patch and CI results but did not rerun the platform/game tests locally.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5563586701 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/7448#pullrequestreview-5563586701). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-18T15:04:53Z. **Review:** APPROVED --- Reviewed the release ordering and checked-release prevalidation. Keeping the physical pages allocated while UnmapDirectMemoryViews waits prevents another allocation from reusing the range before the old views are gone; DirectMemoryAllocated also preserves the ENOENT no-side-effect path. The regression test exercises both release APIs while a guest allocation lease is held. I reviewed the patch and CI results but did not rerun the platform/game tests locally.
MarcZX10 commented 2026-10-18 19:04:03 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-18T19:04:03Z.


I checked the updated calling-convention pattern with the MinGW compiler available in this Linux environment (GCC 12): a minimal Windows-targeted translation unit declaring sceKernelReleaseDirectMemory with APS5_VABI and passing it through decltype(&sceKernelReleaseDirectMemory) compiles successfully.

This only validates the function-pointer type pattern. It is not a build of guest_memory_tests or a runtime test, and this GCC version is older than the repository's supported MinGW GCC 15.2. A full Windows build/test is still needed.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6253895988 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/7448#issuecomment-6253895988). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-18T19:04:03Z. --- I checked the updated calling-convention pattern with the MinGW compiler available in this Linux environment (GCC 12): a minimal Windows-targeted translation unit declaring `sceKernelReleaseDirectMemory` with `APS5_VABI` and passing it through `decltype(&sceKernelReleaseDirectMemory)` compiles successfully. This only validates the function-pointer type pattern. It is not a build of `guest_memory_tests` or a runtime test, and this GCC version is older than the repository's supported MinGW GCC 15.2. A full Windows build/test is still needed.
MarcZX10 commented 2026-10-18 20:01:37 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-18T20:01:37Z.

Review: COMMENTED


One concurrency case to clarify around the new DirectMemoryAllocated precheck: it releases the pool mutex before UnmapDirectMemoryViews waits for leases, so two concurrent sceKernelCheckedReleaseDirectMemory(start, len) calls can both observe the range as allocated and snapshot the same views. After the first unmaps and frees the range, the second still proceeds with its stale view snapshot before DirectMemoryCheckedFree returns ENOENT. Could another thread map a new allocation at one of those virtual addresses in that interval, letting the second call unmap the new view? If concurrent release is excluded by an API/caller invariant, please point to it; otherwise a concurrent checked-release/remap regression would clarify that the second call returns ENOENT without affecting a new mapping.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5564708220 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/7448#pullrequestreview-5564708220). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-18T20:01:37Z. **Review:** COMMENTED --- One concurrency case to clarify around the new `DirectMemoryAllocated` precheck: it releases the pool mutex before `UnmapDirectMemoryViews` waits for leases, so two concurrent `sceKernelCheckedReleaseDirectMemory(start, len)` calls can both observe the range as allocated and snapshot the same views. After the first unmaps and frees the range, the second still proceeds with its stale view snapshot before `DirectMemoryCheckedFree` returns `ENOENT`. Could another thread map a new allocation at one of those virtual addresses in that interval, letting the second call unmap the new view? If concurrent release is excluded by an API/caller invariant, please point to it; otherwise a concurrent checked-release/remap regression would clarify that the second call returns `ENOENT` without affecting a new mapping.
Tamalero commented 2026-10-19 00:16:32 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Tamalero on 2026-10-19T00:16:32Z.


Title check on a longer session: LEGO Batman: Legacy of the Dark Knight (PPSA16833), with the game's files on an NVMe SSD, where the corruption showed up on almost every boot.

  • Build: main fb9a1eb8f + this PR (both commits) + #6182 + the local APS5_PIN_DIAG counters from #7408. fb9a1eb8f predates #6270, which stops this title at boot (#7678).
  • Session: 34 minutes, closed by hand: the animated main menu, the first area, the second map, the Falcone boss fight (where most of the earlier crashes happened) and on into the third map.
  • Result: no command-buffer corruption (unsupported PM4 packet type, truncated PM4 packet, sceAgcSetShRegIndirectPatchSetAddress: unexpected packet type), no Oodle shader-group failure and no RenderThread timeout. Exit code 0.
  • Releases: 11,939 sceKernelReleaseDirectMemory unmaps finished. Their lease waits had a median of 2 ms, a 99th percentile of 123 ms and a maximum of 1.5 s.

For comparison: without this change, the same scene (the animated main menu) stopped on a corrupted command buffer in 8 of 9 SSD boots, on builds from 5d9dcc42d to 2626e5ee7. I didn't rerun a no-PR control on fb9a1eb8f itself, so this session shows the title running cleanly with the change, not a fresh A/B on this base.

Tested and written by Claude (AI assistant) on a contributor's machine.

Examples of INGAME for LEGO BATMAN:

https://youtu.be/C04JePxxcOs

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6256955678 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/7448#issuecomment-6256955678). Originally posted by [Tamalero](https://github.com/Tamalero) on 2026-10-19T00:16:32Z. --- Title check on a longer session: LEGO Batman: Legacy of the Dark Knight (PPSA16833), with the game's files on an NVMe SSD, where the corruption showed up on almost every boot. - **Build:** `main` `fb9a1eb8f` + this PR (both commits) + #6182 + the local `APS5_PIN_DIAG` counters from #7408. `fb9a1eb8f` predates #6270, which stops this title at boot (#7678). - **Session:** 34 minutes, closed by hand: the animated main menu, the first area, the second map, the Falcone boss fight (where most of the earlier crashes happened) and on into the third map. - **Result:** no command-buffer corruption (`unsupported PM4 packet type`, `truncated PM4 packet`, `sceAgcSetShRegIndirectPatchSetAddress: unexpected packet type`), no Oodle shader-group failure and no RenderThread timeout. Exit code 0. - **Releases:** 11,939 `sceKernelReleaseDirectMemory` unmaps finished. Their lease waits had a median of 2 ms, a 99th percentile of 123 ms and a maximum of 1.5 s. For comparison: without this change, the same scene (the animated main menu) stopped on a corrupted command buffer in 8 of 9 SSD boots, on builds from `5d9dcc42d` to `2626e5ee7`. I didn't rerun a no-PR control on `fb9a1eb8f` itself, so this session shows the title running cleanly with the change, not a fresh A/B on this base. *Tested and written by Claude (AI assistant) on a contributor's machine.* Examples of INGAME for LEGO BATMAN: https://youtu.be/C04JePxxcOs
jotitadev commented 2026-10-19 00:26:57 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by jotitadev on 2026-10-19T00:26:57Z.

Review: COMMENTED


Linux run of 9f5efd4f9, with a probe for @MarcZX10's question about two concurrent checked releases. On this head the second release terminates the process. On main it returns ENOENT.

Machine: Linux x86_64, i7-8700, Ubuntu 26.04, GCC 15.2, Release, BUILD_TESTING=ON, full build.
Head 9f5efd4f9 merged onto main 84c1a4d9a (clean merge).

  • Builds. The memory tests (ctest -R guest_memory, 11 tests) pass, and the non-GPU suite (ctest -E "^agc|gpu|vulkan|video_out") is 739/739, same as main.

  • Probe, added to GuestMemory.cpp behind a command-line switch: allocate 4 pages, map them, then two threads call sceKernelCheckedReleaseDirectMemory(phys, 4 pages) on the same range at the same time. In one variant a GuestAllocationsAcquire lease is held for 50 ms, as in your new test. In the other there is no lease.

    main 9f5efd4f9
    lease held, 50 iterations, 3 runs one 0 and one ENOENT in every iteration terminate: unmap address is not registered, first iteration, 3/3
    no lease, 2000 iterations, 3 runs one 0 and one ENOENT in every iteration 1 run clean, 2 runs terminate with the same message

    Both callers pass DirectMemoryAllocated and take the same DirectMemoryViews snapshot. The first one unmaps the views. When the second gets the mutation, it calls UnmapRegistered on views that are already gone, and that throws before it reaches DirectMemoryCheckedFree, which would have returned ENOENT. On main the CheckedFree comes first and is atomic under the pool mutex, so the loser returns before it touches the views. One way to fix it is to claim the range under one lock before the unmap, so a second checked release sees it as already released. Re-reading the views after the mutation is taken would also work.

Written with AI assistance (Claude Code). I ran the builds and tests above on my machine.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5565829046 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/7448#pullrequestreview-5565829046). Originally posted by [jotitadev](https://github.com/jotitadev) on 2026-10-19T00:26:57Z. **Review:** COMMENTED --- Linux run of `9f5efd4f9`, with a probe for @MarcZX10's question about two concurrent checked releases. On this head the second release terminates the process. On `main` it returns `ENOENT`. Machine: Linux x86_64, i7-8700, Ubuntu 26.04, GCC 15.2, Release, `BUILD_TESTING=ON`, full build. Head `9f5efd4f9` merged onto `main` `84c1a4d9a` (clean merge). - Builds. The memory tests (`ctest -R guest_memory`, 11 tests) pass, and the non-GPU suite (`ctest -E "^agc|gpu|vulkan|video_out"`) is 739/739, same as `main`. - Probe, added to `GuestMemory.cpp` behind a command-line switch: allocate 4 pages, map them, then two threads call `sceKernelCheckedReleaseDirectMemory(phys, 4 pages)` on the same range at the same time. In one variant a `GuestAllocationsAcquire` lease is held for 50 ms, as in your new test. In the other there is no lease. | | `main` | `9f5efd4f9` | |---|---|---| | lease held, 50 iterations, 3 runs | one 0 and one `ENOENT` in every iteration | `terminate`: `unmap address is not registered`, first iteration, 3/3 | | no lease, 2000 iterations, 3 runs | one 0 and one `ENOENT` in every iteration | 1 run clean, 2 runs `terminate` with the same message | Both callers pass `DirectMemoryAllocated` and take the same `DirectMemoryViews` snapshot. The first one unmaps the views. When the second gets the mutation, it calls `UnmapRegistered` on views that are already gone, and that throws before it reaches `DirectMemoryCheckedFree`, which would have returned `ENOENT`. On `main` the `CheckedFree` comes first and is atomic under the pool mutex, so the loser returns before it touches the views. One way to fix it is to claim the range under one lock before the unmap, so a second checked release sees it as already released. Re-reading the views after the mutation is taken would also work. Written with AI assistance (Claude Code). I ran the builds and tests above on my machine.
Tamalero commented 2026-10-19 05:41:31 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Tamalero on 2026-10-19T05:41:31Z.


@MarcZX10 @jotitadev thanks, the race is real. The precheck dropped the pool lock before the unmap, and the lease wait inside the unmap also drops the registry lock, so re-reading the views under the mutation would still let two releasers both wait and then both unmap. I went with the first suggestion instead: claim the range in the pool.

Pushed b574f2a51:

  • A release now claims its pages under the pool mutex before it reads the views (DirectMemoryClaimRelease). After the unmap it frees them and drops the claim in one step (DirectMemoryFinishRelease). If the unmap throws, it drops the claim without freeing (DirectMemoryAbandonRelease).
  • A checked release whose pages are unallocated, or already claimed by another release, returns ENOENT before it touches any view. That is what the losing caller got on main. So for the question in the review: the second call never takes a view snapshot, and it can't unmap a mapping created in the meantime.
  • A plain release that overlaps a claimed range waits for that release to finish, then proceeds as before. A second plain release of the same range still ends the way a sequential double release does on main (ForgetDirectMemory rejects pages that are no longer allocated). I left that unchanged since it is outside this PR.
  • Claimed pages stay marked used, so they still can't be reallocated during the lease wait. CheckReleaseKeepsPhysicalUntilViewsUnmapped still covers this for both calls.

New test CheckConcurrentCheckedReleasesUnmapViewsOnce, modelled on jotitadev's probe: two threads check-release the same 4-page mapped range while a lease is held for 50 ms. It requires exactly one 0 and one ENOENT, then reallocates the range.

Linux (GCC, Release):

result
new test on the previous head 9f5efd4f9 (release code restored, new test kept) terminate: unmap address is not registered, 3/3
guest_memory tests on b574f2a51 11/11, five repeats
non-GPU suite (ctest -E "^agc|gpu|vulkan|video_out") 709/709
tools/check_conventions.py --base origin/main passed
merge onto main 4ffb0cd12 clean

Not run on Windows yet.

Tested and written by Claude (AI assistant) on a contributor's machine.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6260525495 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/7448#issuecomment-6260525495). Originally posted by [Tamalero](https://github.com/Tamalero) on 2026-10-19T05:41:31Z. --- @MarcZX10 @jotitadev thanks, the race is real. The precheck dropped the pool lock before the unmap, and the lease wait inside the unmap also drops the registry lock, so re-reading the views under the mutation would still let two releasers both wait and then both unmap. I went with the first suggestion instead: claim the range in the pool. Pushed b574f2a51: - A release now claims its pages under the pool mutex before it reads the views (`DirectMemoryClaimRelease`). After the unmap it frees them and drops the claim in one step (`DirectMemoryFinishRelease`). If the unmap throws, it drops the claim without freeing (`DirectMemoryAbandonRelease`). - A checked release whose pages are unallocated, or already claimed by another release, returns `ENOENT` before it touches any view. That is what the losing caller got on `main`. So for the question in the review: the second call never takes a view snapshot, and it can't unmap a mapping created in the meantime. - A plain release that overlaps a claimed range waits for that release to finish, then proceeds as before. A second plain release of the same range still ends the way a sequential double release does on `main` (`ForgetDirectMemory` rejects pages that are no longer allocated). I left that unchanged since it is outside this PR. - Claimed pages stay marked used, so they still can't be reallocated during the lease wait. `CheckReleaseKeepsPhysicalUntilViewsUnmapped` still covers this for both calls. New test `CheckConcurrentCheckedReleasesUnmapViewsOnce`, modelled on jotitadev's probe: two threads check-release the same 4-page mapped range while a lease is held for 50 ms. It requires exactly one 0 and one `ENOENT`, then reallocates the range. Linux (GCC, Release): | | result | |---|---| | new test on the previous head 9f5efd4f9 (release code restored, new test kept) | `terminate`: `unmap address is not registered`, 3/3 | | `guest_memory` tests on b574f2a51 | 11/11, five repeats | | non-GPU suite (`ctest -E "^agc\|gpu\|vulkan\|video_out"`) | 709/709 | | `tools/check_conventions.py --base origin/main` | passed | | merge onto `main` 4ffb0cd12 | clean | Not run on Windows yet. *Tested and written by Claude (AI assistant) on a contributor's machine.*
jotitadev commented 2026-10-19 08:12:37 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by jotitadev on 2026-10-19T08:12:37Z.

Review: COMMENTED


Linux rerun of b574f2a51 with the same probe as my earlier report on 9f5efd4f9 (two threads call sceKernelCheckedReleaseDirectMemory on the same mapped 4-page range).

Machine: Linux x86_64, i7-8700, Ubuntu 26.04, GCC 15.2, Release, BUILD_TESTING=ON, full build. Head b574f2a51 merged onto main 6b8228b2b (clean merge).

9f5efd4f9 b574f2a51
lease held while both start, 50 iterations, 3 runs terminate: unmap address is not registered in the first iteration, 3 of 3 runs one 0 and one ENOENT in 150 of 150
no lease, 2000 iterations, 3 runs terminate in 2 of 3 runs one 0 and one ENOENT in 6000 of 6000

That matches what main returns. The 11 memory tests pass, including the new CheckConcurrentCheckedReleasesUnmapViewsOnce, and the non-GPU suite is 766/766, same as main.

Written with AI assistance (Claude Code). I ran the builds and tests above on my machine.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5568645398 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/7448#pullrequestreview-5568645398). Originally posted by [jotitadev](https://github.com/jotitadev) on 2026-10-19T08:12:37Z. **Review:** COMMENTED --- Linux rerun of `b574f2a51` with the same probe as my earlier report on `9f5efd4f9` (two threads call `sceKernelCheckedReleaseDirectMemory` on the same mapped 4-page range). Machine: Linux x86_64, i7-8700, Ubuntu 26.04, GCC 15.2, Release, `BUILD_TESTING=ON`, full build. Head `b574f2a51` merged onto `main` `6b8228b2b` (clean merge). | | `9f5efd4f9` | `b574f2a51` | |---|---|---| | lease held while both start, 50 iterations, 3 runs | `terminate: unmap address is not registered` in the first iteration, 3 of 3 runs | one `0` and one `ENOENT` in 150 of 150 | | no lease, 2000 iterations, 3 runs | terminate in 2 of 3 runs | one `0` and one `ENOENT` in 6000 of 6000 | That matches what `main` returns. The 11 memory tests pass, including the new `CheckConcurrentCheckedReleasesUnmapViewsOnce`, and the non-GPU suite is 766/766, same as `main`. Written with AI assistance (Claude Code). I ran the builds and tests above on my machine.
adriadam10 commented 2026-10-19 21:29:18 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-19T21:29:18Z.


Checked the claim/unmap/free order in Export.cpp and MemoryPool.cpp: the claim is taken under the pool mutex before the views are read, overlapping releases wait (unchecked) or get ENOENT (checked), and the claim is dropped on throw. The new tests pin the physical range staying unallocatable until the lease is gone and the two-concurrent-checked-release outcome. Matches the Linux probe by @jotitadev and the review by @MarcZX10; merged.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6277730857 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/7448#issuecomment-6277730857). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-19T21:29:18Z. --- Checked the claim/unmap/free order in `Export.cpp` and `MemoryPool.cpp`: the claim is taken under the pool mutex before the views are read, overlapping releases wait (unchecked) or get `ENOENT` (checked), and the claim is dropped on throw. The new tests pin the physical range staying unallocatable until the lease is gone and the two-concurrent-checked-release outcome. Matches the Linux probe by @jotitadev and the review by @MarcZX10; merged.
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!7448
No description provided.