fix(libkernel): release flexible memory when POSIX munmap unmaps it #5267

Closed
milescoviello wants to merge 1 commit from upstream-pr/5267 into main
milescoviello commented 2026-10-13 16:00:49 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by milescoviello on 2026-10-13T16:00:49Z.


What

Fixes #5260. Carries @husseinkad's fix branch from that issue (husseinkad:fix/kernel-munmap-flexible-release, 7815f5ae8, one commit), keeping the commit and its authorship. It cherry-picks onto current main without changes.

The POSIX munmap did not give flexible memory back to the flexible-memory budget. After sceKernelMapFlexibleMemory and a munmap of that range, sceKernelAvailableFlexibleMemorySize stayed lower by the unmapped size. Only sceKernelMunmap and sceKernelReleaseFlexibleMemory released it. A title that maps flexible memory and frees it with munmap therefore leaks budget on every cycle, and later flexible maps fail once the budget runs out.

munmap_nid_postfix called DoMunmap directly. It now calls sceKernelMunmap, which is DoMunmap followed by the release of the flexible pages in the range, the same path sceKernelReleaseFlexibleMemory already takes. Only ranges tracked as flexible are released, so munmap of anonymous or direct mappings changes nothing. The argument checks and the EINVAL for a failed unmap are as before.

guest_memory gains CheckPosixMunmapReleasesFlexibleMemory. It maps 64 KiB of flexible memory, unmaps the upper half and then the lower half with munmap, and checks the available size after each step.

Tested

  • From the issue, on Windows: the new check fails on main and passes with the change, and guest_memory, guest_msync and guest_mlock pass. A fuzz of 40 seeds of 300 steps each mixes munmap, sceKernelMunmap, sceKernelReleaseFlexibleMemory, fixed flexible maps and direct maps, and checks the budget after every step. It found no mismatch with the model, and the budget returned to its start. On main every seed ended with a leak.
  • Linux, added for this carry: Ubuntu 24.04, GCC 13.3 (CI's compiler), Release with CI's configure flags, on main 9d0a41d5a:
    • The build is clean.
    • With main's PosixMemory.cpp and the new test, guest_memory fails at GuestMemory.cpp:128, the budget check after the first munmap; with the change it passes.
    • guest_memory, guest_msync and guest_mlock pass 20/20.
    • All 341 guest_* tests pass in two runs.
  • tools/check_conventions.py --base upstream/main passes.

Not changed: as the issue notes, DoMunmap and the budget release in sceKernelMunmap are not one atomic step, which was already the case for sceKernelMunmap on main.

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: none
  • AI-assisted: yes

🤖 Generated with Claude Code

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4845966332 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/5267). Originally posted by [milescoviello](https://github.com/milescoviello) on 2026-10-13T16:00:49Z. --- ### What Fixes #5260. Carries @husseinkad's fix branch from that issue (`husseinkad:fix/kernel-munmap-flexible-release`, `7815f5ae8`, one commit), keeping the commit and its authorship. It cherry-picks onto current `main` without changes. The POSIX `munmap` did not give flexible memory back to the flexible-memory budget. After `sceKernelMapFlexibleMemory` and a `munmap` of that range, `sceKernelAvailableFlexibleMemorySize` stayed lower by the unmapped size. Only `sceKernelMunmap` and `sceKernelReleaseFlexibleMemory` released it. A title that maps flexible memory and frees it with `munmap` therefore leaks budget on every cycle, and later flexible maps fail once the budget runs out. `munmap_nid_postfix` called `DoMunmap` directly. It now calls `sceKernelMunmap`, which is `DoMunmap` followed by the release of the flexible pages in the range, the same path `sceKernelReleaseFlexibleMemory` already takes. Only ranges tracked as flexible are released, so `munmap` of anonymous or direct mappings changes nothing. The argument checks and the `EINVAL` for a failed unmap are as before. `guest_memory` gains `CheckPosixMunmapReleasesFlexibleMemory`. It maps 64 KiB of flexible memory, unmaps the upper half and then the lower half with `munmap`, and checks the available size after each step. ### Tested - From the issue, on Windows: the new check fails on `main` and passes with the change, and `guest_memory`, `guest_msync` and `guest_mlock` pass. A fuzz of 40 seeds of 300 steps each mixes `munmap`, `sceKernelMunmap`, `sceKernelReleaseFlexibleMemory`, fixed flexible maps and direct maps, and checks the budget after every step. It found no mismatch with the model, and the budget returned to its start. On `main` every seed ended with a leak. - Linux, added for this carry: Ubuntu 24.04, GCC 13.3 (CI's compiler), Release with CI's configure flags, on `main` 9d0a41d5a: - The build is clean. - With `main`'s `PosixMemory.cpp` and the new test, `guest_memory` fails at `GuestMemory.cpp:128`, the budget check after the first `munmap`; with the change it passes. - `guest_memory`, `guest_msync` and `guest_mlock` pass 20/20. - All 341 `guest_*` tests pass in two runs. - `tools/check_conventions.py --base upstream/main` passes. Not changed: as the issue notes, `DoMunmap` and the budget release in `sceKernelMunmap` are not one atomic step, which was already the case for `sceKernelMunmap` on `main`. ### Checklist <!-- Rules behind each item: https://github.com/boykopovar/AnyPS5/blob/main/CONTRIBUTING.md --> - [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: none - [x] AI-assisted: yes 🤖 Generated with [Claude Code](https://claude.com/claude-code)
oneandonlydean commented 2026-10-14 02:30:22 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-14T02:30:22Z.


Closing: no title is known to reach this. With the queue at 700+ open PRs, please hold off on new PRs that don't name a title needing the change until it's down. If a title stops on this on current main, reopen with the title ID and the log line.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6159557590 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/5267#issuecomment-6159557590). Originally posted by [oneandonlydean](https://github.com/oneandonlydean) on 2026-10-14T02:30:22Z. --- Closing: no title is known to reach this. With the queue at 700+ open PRs, please hold off on new PRs that don't name a title needing the change until it's down. If a title stops on this on current main, reopen with the title ID and the log line.

Pull request closed

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!5267
No description provided.