fix(libkernel): unmap direct memory views when the memory is released #2236

Merged
mrk1987 merged 1 commit from upstream-pr/2236 into main 2026-10-10 04:45:32 +00:00
mrk1987 commented 2026-10-09 12:43:24 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by mrk1987 on 2026-10-09T12:43:24Z.


What

sceKernelReleaseDirectMemory and sceKernelCheckedReleaseDirectMemory free the physical pages and forget their direct mappings, but the virtual ranges that mapped them stay registered as guest allocations. A later sceKernelVirtualQuery then reports those ranges as committed flexible memory at offset 0.

A title that frees GPU memory by walking a virtual range with FIND_NEXT and releasing offset + (start - info.start) for each region it finds then frees physical offset 0. That stops with direct memory range 0x0+0x200000 references unallocated physical page 0x0.

With this change, releasing direct memory also unmaps every virtual view of the released pages, aliases included, so the range is free afterwards. The views are collected before the pages are freed and unmapped through the existing UnmapRegistered path. The checked variant unmaps only when the release succeeds.

This follows shadPS4's MemoryManager::Free, which releases "any dmem mappings that reference this physical block": https://github.com/shadps4-emu/shadPS4/blob/main/src/core/memory.cpp

CheckReleaseDirectMemoryClearsMappings asserted the old behaviour, where a fully released mapping still answered queries. It now checks that both the mapping and a second alias stop answering queries, and that FIND_NEXT from them does not return them. It no longer depends on how a hole inside an allocation is reported, which #1731 changes.

Tested

Windows 11, MinGW-w64 GCC (project toolchain), Release/Ninja on upstream main d70b8998.

  • guest_memory passes with the change. On unchanged production code the updated test fails at the released-mapping query (GuestMemory.cpp:288).
  • tools/check_conventions.py and git diff --check pass.
  • A retail Unreal Engine title previously stopped on the release of physical page 0 described above. With this change (plus unrelated local work) it reaches its first presented frames. Gameplay is not verified.

Checklist

  • Based on current main; no other open PR implements the same functions (#1731 changes sceKernelVirtualQuery range selection and explicitly excludes memory release; the two are independent)
  • One topic per PR; follow-ups go in a new PR
  • No comments in code except technical debt
  • Unimplemented paths throw; no silent stubs added
  • No notes, investigation files or images added to the repository
  • New third-party code is a submodule built from source (none added)
  • Generic behaviour, not specific to one title
  • Depends on: none
  • AI-assisted: yes. The investigation, code, tests and this description were written by Claude (Claude Code), reviewed and run on my machine.

🤖 Generated with Claude Code

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4800563748 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/2236). Originally posted by [mrk1987](https://github.com/mrk1987) on 2026-10-09T12:43:24Z. --- ### What `sceKernelReleaseDirectMemory` and `sceKernelCheckedReleaseDirectMemory` free the physical pages and forget their direct mappings, but the virtual ranges that mapped them stay registered as guest allocations. A later `sceKernelVirtualQuery` then reports those ranges as committed flexible memory at offset 0. A title that frees GPU memory by walking a virtual range with `FIND_NEXT` and releasing `offset + (start - info.start)` for each region it finds then frees physical offset 0. That stops with `direct memory range 0x0+0x200000 references unallocated physical page 0x0`. With this change, releasing direct memory also unmaps every virtual view of the released pages, aliases included, so the range is free afterwards. The views are collected before the pages are freed and unmapped through the existing `UnmapRegistered` path. The checked variant unmaps only when the release succeeds. This follows shadPS4's `MemoryManager::Free`, which releases "any dmem mappings that reference this physical block": https://github.com/shadps4-emu/shadPS4/blob/main/src/core/memory.cpp `CheckReleaseDirectMemoryClearsMappings` asserted the old behaviour, where a fully released mapping still answered queries. It now checks that both the mapping and a second alias stop answering queries, and that `FIND_NEXT` from them does not return them. It no longer depends on how a hole inside an allocation is reported, which #1731 changes. ### Tested Windows 11, MinGW-w64 GCC (project toolchain), Release/Ninja on upstream main `d70b8998`. - `guest_memory` passes with the change. On unchanged production code the updated test fails at the released-mapping query (`GuestMemory.cpp:288`). - `tools/check_conventions.py` and `git diff --check` pass. - A retail Unreal Engine title previously stopped on the release of physical page 0 described above. With this change (plus unrelated local work) it reaches its first presented frames. Gameplay is not verified. ### Checklist - [x] Based on current `main`; no other open PR implements the same functions (#1731 changes `sceKernelVirtualQuery` range selection and explicitly excludes memory release; the two are independent) - [x] One topic per PR; follow-ups go in a new PR - [x] No comments in code except technical debt - [x] Unimplemented paths throw; no silent stubs added - [x] No notes, investigation files or images added to the repository - [x] New third-party code is a submodule built from source (none added) - [x] Generic behaviour, not specific to one title - [x] Depends on: none - [x] AI-assisted: yes. The investigation, code, tests and this description were written by Claude (Claude Code), reviewed and run on my machine. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
Kikoarl commented 2026-10-09 14:04:29 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Kikoarl on 2026-10-09T14:04:29Z.


Linux run: Linux 7.2 (x86_64), GCC 16.2.1, Release, -DBUILD_TESTING=ON, #2236 (24474e3d) tested on current main (d70b8998).

  • tests/guest_memory_tests (ctest -R "^guest_memory"): passes cleanly.
  • tools/check_conventions.py: passes cleanly.
  • Confirmed the mutation on main: on unpatched main the updated test fails at GuestMemory.cpp:288 (released-mapping query); with #2236 applied, it passes.
  • No regressions observed in guest_allocations / guest_addresses.
<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6082512079 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/2236#issuecomment-6082512079). Originally posted by [Kikoarl](https://github.com/Kikoarl) on 2026-10-09T14:04:29Z. --- Linux run: Linux 7.2 (x86_64), GCC 16.2.1, Release, `-DBUILD_TESTING=ON`, #2236 (`24474e3d`) tested on current `main` (`d70b8998`). - `tests/guest_memory_tests` (`ctest -R "^guest_memory"`): passes cleanly. - `tools/check_conventions.py`: passes cleanly. - Confirmed the mutation on `main`: on unpatched `main` the updated test fails at `GuestMemory.cpp:288` (released-mapping query); with #2236 applied, it passes. - No regressions observed in `guest_allocations` / `guest_addresses`.
github-actions[bot] commented 2026-10-09 19:06:18 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-09T19:06:18Z.


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
#2421 core/libs/prx/libkernel/DirectMemory/Export.cpp L218-219
<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6087457326 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/2236#issuecomment-6087457326). Originally posted by [github-actions[bot]](https://github.com/apps/github-actions) on 2026-10-09T19:06:18Z. --- <!-- 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 | |---|---|---|---| | #2421 | `core/libs/prx/libkernel/DirectMemory/Export.cpp` [L218-219](https://github.com/boykopovar/AnyPS5/blob/80d539abcbd0f90a7b6e64f7ab4868556671f241/core/libs/prx/libkernel/DirectMemory/Export.cpp#L218-L219) | | |
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!2236
No description provided.