fix(libkernel): unmap direct memory views when the memory is released #2236
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/2236"
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 mrk1987 on 2026-10-09T12:43:24Z.
What
sceKernelReleaseDirectMemoryandsceKernelCheckedReleaseDirectMemoryfree the physical pages and forget their direct mappings, but the virtual ranges that mapped them stay registered as guest allocations. A latersceKernelVirtualQuerythen reports those ranges as committed flexible memory at offset 0.A title that frees GPU memory by walking a virtual range with
FIND_NEXTand releasingoffset + (start - info.start)for each region it finds then frees physical offset 0. That stops withdirect 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
UnmapRegisteredpath. 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.cppCheckReleaseDirectMemoryClearsMappingsasserted 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 thatFIND_NEXTfrom 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_memorypasses with the change. On unchanged production code the updated test fails at the released-mapping query (GuestMemory.cpp:288).tools/check_conventions.pyandgit diff --checkpass.Checklist
main; no other open PR implements the same functions (#1731 changessceKernelVirtualQueryrange selection and explicitly excludes memory release; the two are independent)🤖 Generated with Claude Code
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 currentmain(d70b8998).tests/guest_memory_tests(ctest -R "^guest_memory"): passes cleanly.tools/check_conventions.py: passes cleanly.main: on unpatchedmainthe updated test fails atGuestMemory.cpp:288(released-mapping query); with #2236 applied, it passes.guest_allocations/guest_addresses.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.
main)core/libs/prx/libkernel/DirectMemory/Export.cppL218-219