libkernel: POSIX munmap of flexible memory does not return it to the flexible budget; fix branch ready #5260

Closed
opened 2026-10-13 15:54:06 +00:00 by husseinkad · 2 comments
husseinkad commented 2026-10-13 15:54:06 +00:00 (Migrated from github.com)
Contributor

Imported from boykopovar/AnyPS5. Originally posted by husseinkad on 2026-10-13T15:54:06Z.


The POSIX munmap does not give flexible memory back to the flexible-memory budget. After sceKernelMapFlexibleMemory(1 MiB) followed by munmap of that range, sceKernelAvailableFlexibleMemorySize stays 1 MiB lower; only sceKernelMunmap (and sceKernelReleaseFlexibleMemory) release it. A guest that maps flexible memory and unmaps it with munmap slowly exhausts the budget and later maps fail.

Fix branch: https://github.com/husseinkad/AnyPS5/tree/fix/kernel-munmap-flexible-release (one commit on a recent main: DirectMemory/PosixMemory.cpp and a new CheckPosixMunmapReleasesFlexibleMemory in tests/GuestMemory.cpp). munmap_nid_postfix now unmaps through sceKernelMunmap, the same wrapper that already releases the budget, so no new behaviour is assumed: sceKernelMmap and sceKernelReleaseFlexibleMemory already reuse that path.

Checks:

  • The new check (including unmapping the range in two halves) fails on main and passes with the change; guest_memory, guest_msync and guest_mlock pass.
  • A fuzz of 40 seeds of 300 steps each, mixing munmap, sceKernelMunmap, sceKernelReleaseFlexibleMemory, fixed flexible maps into holes and direct-memory maps, with sceKernelAvailableFlexibleMemorySize checked after every step: 0 model mismatches, the budget never went negative or above its initial value and returned to the start at the end. A double release did not inflate the budget; partial, middle-hole and spanning unmaps released exactly the flexible pages they covered; munmap of an anonymous or direct mapping changed nothing (only ranges tracked as flexible are released). On main every fuzz seed ended with a nonzero leak (about 500 KB in the 4-thread test).
  • Two independent reviews found no defect in the change. check_conventions.py is clean and pr_overlap.py finds no overlap.

Not covered, and unchanged from main: DoMunmap and the budget release are not atomic, so if another thread reuses the address in between, its charge can be erased (about 30% of iterations in a 4-thread test undercounted on both main's sceKernelMunmap and the branch; the final budget was still correct); a fixed flexible map over an existing flexible range overwrites the tracked entry and under-counts the charge. Only Windows was built and run.

AI-assisted: yes

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5833463808 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/issues/5260). Originally posted by [husseinkad](https://github.com/husseinkad) on 2026-10-13T15:54:06Z. --- The POSIX `munmap` does not give flexible memory back to the flexible-memory budget. After `sceKernelMapFlexibleMemory(1 MiB)` followed by `munmap` of that range, `sceKernelAvailableFlexibleMemorySize` stays 1 MiB lower; only `sceKernelMunmap` (and `sceKernelReleaseFlexibleMemory`) release it. A guest that maps flexible memory and unmaps it with `munmap` slowly exhausts the budget and later maps fail. Fix branch: https://github.com/husseinkad/AnyPS5/tree/fix/kernel-munmap-flexible-release (one commit on a recent main: `DirectMemory/PosixMemory.cpp` and a new `CheckPosixMunmapReleasesFlexibleMemory` in `tests/GuestMemory.cpp`). `munmap_nid_postfix` now unmaps through `sceKernelMunmap`, the same wrapper that already releases the budget, so no new behaviour is assumed: `sceKernelMmap` and `sceKernelReleaseFlexibleMemory` already reuse that path. Checks: - The new check (including unmapping the range in two halves) fails on main and passes with the change; `guest_memory`, `guest_msync` and `guest_mlock` pass. - A fuzz of 40 seeds of 300 steps each, mixing `munmap`, `sceKernelMunmap`, `sceKernelReleaseFlexibleMemory`, fixed flexible maps into holes and direct-memory maps, with `sceKernelAvailableFlexibleMemorySize` checked after every step: 0 model mismatches, the budget never went negative or above its initial value and returned to the start at the end. A double release did not inflate the budget; partial, middle-hole and spanning unmaps released exactly the flexible pages they covered; `munmap` of an anonymous or direct mapping changed nothing (only ranges tracked as flexible are released). On main every fuzz seed ended with a nonzero leak (about 500 KB in the 4-thread test). - Two independent reviews found no defect in the change. `check_conventions.py` is clean and `pr_overlap.py` finds no overlap. Not covered, and unchanged from main: `DoMunmap` and the budget release are not atomic, so if another thread reuses the address in between, its charge can be erased (about 30% of iterations in a 4-thread test undercounted on both main's `sceKernelMunmap` and the branch; the final budget was still correct); a fixed flexible map over an existing flexible range overwrites the tracked entry and under-counts the charge. Only Windows was built and run. AI-assisted: yes
milescoviello commented 2026-10-13 16:00:58 +00:00 (Migrated from github.com)
Author
Contributor

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


@husseinkad I've opened your branch as #5267, with your commit and authorship kept. It cherry-picks onto current main without changes.

I added a Linux run there: Ubuntu 24.04, GCC 13.3, Release. With main's PosixMemory.cpp, the new check fails at the budget check after the first munmap; with your change it passes. guest_memory, guest_msync and guest_mlock pass 20/20, and all 341 guest_* tests pass in two runs. I'll keep it up to date with main and answer review there.

AI-assisted: yes

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6149707804 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/issues/5260#issuecomment-6149707804). Originally posted by [milescoviello](https://github.com/milescoviello) on 2026-10-13T16:00:58Z. --- @husseinkad I've opened your branch as #5267, with your commit and authorship kept. It cherry-picks onto current `main` without changes. I added a Linux run there: Ubuntu 24.04, GCC 13.3, Release. With `main`'s `PosixMemory.cpp`, the new check fails at the budget check after the first `munmap`; with your change it passes. `guest_memory`, `guest_msync` and `guest_mlock` pass 20/20, and all 341 `guest_*` tests pass in two runs. I'll keep it up to date with `main` and answer review there. AI-assisted: yes
oneandonlydean commented 2026-10-14 06:54:40 +00:00 (Migrated from github.com)
Author
Contributor

Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-14T06:54:40Z.


Closing: no title is known to reach this, and functions are only changed when a title needs them. #5267 is closed for the same reason. If a title stops on or misbehaves because of it on current main, comment here with the title ID and the log line.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6162715968 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/issues/5260#issuecomment-6162715968). Originally posted by [oneandonlydean](https://github.com/oneandonlydean) on 2026-10-14T06:54:40Z. --- Closing: no title is known to reach this, and functions are only changed when a title needs them. #5267 is closed for the same reason. If a title stops on or misbehaves because of it on current main, comment here with the title ID and the log line.
Sign in to join this conversation.
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#5260
No description provided.