fix(libkernel): release flexible memory budget through munmap #6311

Merged
Ondra97 merged 1 commit from upstream-pr/6311 into main 2026-10-18 22:06:26 +00:00
Ondra97 commented 2026-10-15 10:53:37 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Ondra97 on 2026-10-15T10:53:37Z.


What

The POSIX munmap path removes a flexible-memory mapping but leaves its bytes charged to the guest budget. A subsequent allocation can therefore return ENOMEM after the memory has been freed. In my public-API probe with a 16 MiB configured budget, unmapping 512 KiB succeeds and sceKernelVirtualQuery confirms the mapping is gone, yet the next 16,465,920-byte allocation fails. Releasing the same allocation through the existing kernel API restores the budget and allows that request.

Route POSIX unmaps through sceKernelMunmap, which already performs the native unmap and then releases flexible-memory accounting. Keep the existing argument checks, guest-page rounding and error translation. This uses the same cleanup path for POSIX and kernel callers without adding accounting state or a new lock.

Extend the existing registered flexible-memory budget tests with rounded partial/full POSIX unmaps, retained contents of neighboring live pages, mixed POSIX/kernel releases and repeated unmaps. The 16 MiB case also verifies that the recovered budget can be allocated again. Whole-page removal follows the existing guest model and the POSIX munmap contract; no new console behavior is claimed.

Tested

I tested on Windows 11 Pro 10.0.26300, WinLibs MinGW-w64 GCC 15.2.0 r7, Release/Ninja, with BUILD_TESTING=ON, outside the sandbox.

Hardware: AMD Ryzen 9 7900X, 32 GB RAM, NVIDIA GeForce RTX 4070 Ti SUPER.

These are my captured runs of the same standalone public-API probe against main e49ec591dc93c9e4dfaad8b6dbc0b995dd7830a0 and final head 0cbec9f8f427a904297e1896b06c7f1c91abb42f, based on main 8856f180ca0b5605574842666ffbc8b0bb69cc4d. All libkernel/libc and regression Git blobs are identical to the initially measured fixed tree after rebase. The image's existing charge is already excluded from the initial available budget. This is a correctness comparison, not a performance measurement.

Measurement Main This change
Available bytes before allocation 16,465,920 16,465,920
Available bytes after successful POSIX unmap 15,941,632 16,465,920
Bytes still charged after their mapping is gone 524,288 0
Next allocation of 16,465,920 bytes ENOMEM (0x8002000c) Success
  • The expanded registered guest_flexible_memory_limited regression fails on main after the two middle pages have been unmapped: available 16,384,000, expected 16,416,768. With the fix it passes, including the final full-budget allocation.
  • Full default Windows build passed on the final head, including patched libraries.
  • All 20 related CPU memory, heap, stack, shared/JIT-memory, fixed-anonymous-mapping, write-watch and memory-pool CTest cases passed again on the final head in 2.26 seconds; no CTest skips or untested-branch notices were reported.
  • The four flexible-memory regression modes (declared, limited, none, missing) additionally passed ten executions each: 40 runs, 0.01-0.06 seconds each. The invalid-metadata case remains covered separately.
  • Test registration passed: 879 executables, none built without a recorded CTest invocation. Conventions and whitespace checks passed.

I checked complete pinned-head file inventories of 582 open PRs and manually compared the three other PosixMemory.cpp changes: #6222 only enables macOS file-backed paths, #5835 refuses archive-backed mmap, and #5663 adds munlockall. Its existing extension of the kernel unmap cleanup will also be reached through the shared path after integration. The older AOT branches change platform query/locking paths in DirectMemory/Export.cpp; none restores the POSIX unmap budget. #5865 changes the meaning of a zero configured budget and is independent. No other open PR changes the regression file.

File-level three-way merge previews with the exact heads of #6222, #5835 and #5663 are clean and preserve both sides. These are static integration checks; no combined-feature runtime run is claimed.

Limits: no retail title, Linux runtime, console or GPU execution was tested. The Windows run does not cover Linux-only module-lifetime targets. Full hosted Windows/Linux PR builds require collaborator dispatch under CONTRIBUTING.md; skipped jobs are not validation passes.

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
  • New third-party code is a submodule built from source; none added
  • Generic behaviour, not specific to one title
  • Depends on: none
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes - Codex. I am responsible for the change.
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4872107810 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/6311). Originally posted by [Ondra97](https://github.com/Ondra97) on 2026-10-15T10:53:37Z. --- ### What The POSIX `munmap` path removes a flexible-memory mapping but leaves its bytes charged to the guest budget. A subsequent allocation can therefore return `ENOMEM` after the memory has been freed. In my public-API probe with a 16 MiB configured budget, unmapping 512 KiB succeeds and `sceKernelVirtualQuery` confirms the mapping is gone, yet the next 16,465,920-byte allocation fails. Releasing the same allocation through the existing kernel API restores the budget and allows that request. Route POSIX unmaps through `sceKernelMunmap`, which already performs the native unmap and then releases flexible-memory accounting. Keep the existing argument checks, guest-page rounding and error translation. This uses the same cleanup path for POSIX and kernel callers without adding accounting state or a new lock. Extend the existing registered flexible-memory budget tests with rounded partial/full POSIX unmaps, retained contents of neighboring live pages, mixed POSIX/kernel releases and repeated unmaps. The 16 MiB case also verifies that the recovered budget can be allocated again. Whole-page removal follows the existing guest model and the [POSIX munmap contract](https://pubs.opengroup.org/onlinepubs/9799919799/functions/munmap.html); no new console behavior is claimed. ### Tested I tested on Windows 11 Pro 10.0.26300, WinLibs MinGW-w64 GCC 15.2.0 r7, Release/Ninja, with `BUILD_TESTING=ON`, outside the sandbox. Hardware: AMD Ryzen 9 7900X, 32 GB RAM, NVIDIA GeForce RTX 4070 Ti SUPER. These are my captured runs of the same standalone public-API probe against main `e49ec591dc93c9e4dfaad8b6dbc0b995dd7830a0` and final head `0cbec9f8f427a904297e1896b06c7f1c91abb42f`, based on main `8856f180ca0b5605574842666ffbc8b0bb69cc4d`. All libkernel/libc and regression Git blobs are identical to the initially measured fixed tree after rebase. The image's existing charge is already excluded from the initial available budget. This is a correctness comparison, not a performance measurement. | Measurement | Main | This change | |---|---:|---:| | Available bytes before allocation | 16,465,920 | 16,465,920 | | Available bytes after successful POSIX unmap | 15,941,632 | 16,465,920 | | Bytes still charged after their mapping is gone | 524,288 | 0 | | Next allocation of 16,465,920 bytes | `ENOMEM` (`0x8002000c`) | Success | - The expanded registered `guest_flexible_memory_limited` regression fails on main after the two middle pages have been unmapped: available 16,384,000, expected 16,416,768. With the fix it passes, including the final full-budget allocation. - Full default Windows build passed on the final head, including patched libraries. - All 20 related CPU memory, heap, stack, shared/JIT-memory, fixed-anonymous-mapping, write-watch and memory-pool CTest cases passed again on the final head in 2.26 seconds; no CTest skips or untested-branch notices were reported. - The four flexible-memory regression modes (`declared`, `limited`, `none`, `missing`) additionally passed ten executions each: 40 runs, 0.01-0.06 seconds each. The invalid-metadata case remains covered separately. - Test registration passed: 879 executables, none built without a recorded CTest invocation. Conventions and whitespace checks passed. I checked complete pinned-head file inventories of 582 open PRs and manually compared the three other `PosixMemory.cpp` changes: #6222 only enables macOS file-backed paths, #5835 refuses archive-backed `mmap`, and #5663 adds `munlockall`. Its existing extension of the kernel unmap cleanup will also be reached through the shared path after integration. The older AOT branches change platform query/locking paths in `DirectMemory/Export.cpp`; none restores the POSIX unmap budget. #5865 changes the meaning of a zero configured budget and is independent. No other open PR changes the regression file. File-level three-way merge previews with the exact heads of #6222, #5835 and #5663 are clean and preserve both sides. These are static integration checks; no combined-feature runtime run is claimed. Limits: no retail title, Linux runtime, console or GPU execution was tested. The Windows run does not cover Linux-only module-lifetime targets. Full hosted Windows/Linux PR builds require collaborator dispatch under `CONTRIBUTING.md`; skipped jobs are not validation passes. ### Checklist - [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 - [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] Unblocks: none - [x] GPUs tested: n/a - [x] AI-assisted: yes - Codex. I am responsible for the change.
github-actions[bot] commented 2026-10-15 12:18:27 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-15T12:18:27Z.


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 6044 core/libs/tests/GuestFlexibleMemoryBudget.cpp
PR 6534 core/libs/prx/libkernel/DirectMemory/PosixMemory.cpp
<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6191865119 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/6311#issuecomment-6191865119). Originally posted by [github-actions[bot]](https://github.com/apps/github-actions) on 2026-10-15T12:18:27Z. --- <!-- pr-overlap --> 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 6044](https://www.github.com/boykopovar/AnyPS5/pull/6044) | `core/libs/tests/GuestFlexibleMemoryBudget.cpp` | | [PR 6534](https://www.github.com/boykopovar/AnyPS5/pull/6534) | `core/libs/prx/libkernel/DirectMemory/PosixMemory.cpp` |
jotitadev commented 2026-10-15 18:15:37 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by jotitadev on 2026-10-15T18:15:37Z.

Review: COMMENTED


Linux red/green, full build. The description covers only Windows.

Machine: Ubuntu 26.04, Intel i7-8700, GCC 15.2, Release, full build (-DBUILD_TESTING=ON), non-GPU tests. Head 0cbec9f8f merged onto main 03c4cb5e7.

  • Test only (PosixMemory.cpp from main): guest_flexible_memory_declared and guest_flexible_memory_limited fail at the first partial unmap. The budget is not released:
    POSIX flexible release rounded partial unmap: available=300728320 expected=300761088
    
  • With the fix, all five guest_flexible_memory_* tests pass.
  • Full non-GPU suite: 668/670. The two failures, file_writer_binary and file_writer_text, also fail on plain main on this machine.

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

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5537875794 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/6311#pullrequestreview-5537875794). Originally posted by [jotitadev](https://github.com/jotitadev) on 2026-10-15T18:15:37Z. **Review:** COMMENTED --- **Linux red/green, full build. The description covers only Windows.** Machine: Ubuntu 26.04, Intel i7-8700, GCC 15.2, Release, full build (`-DBUILD_TESTING=ON`), non-GPU tests. Head `0cbec9f8f` merged onto main `03c4cb5e7`. - Test only (`PosixMemory.cpp` from main): `guest_flexible_memory_declared` and `guest_flexible_memory_limited` fail at the first partial unmap. The budget is not released: ``` POSIX flexible release rounded partial unmap: available=300728320 expected=300761088 ``` - With the fix, all five `guest_flexible_memory_*` tests pass. - Full non-GPU suite: 668/670. The two failures, `file_writer_binary` and `file_writer_text`, also fail on plain main on this machine. Written with AI assistance (Claude Code). I ran the builds and tests above on my machine.
screamm commented 2026-10-17 12:07:31 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by screamm on 2026-10-17T12:07:31Z.


Windows run of head 0cbec9f8f, merged onto main 673f7406c together with #6302 (no shared file; both merges clean). It complements the Linux run above.

  • Host: Windows 11 Pro 26220, Ryzen 7 5700X, RTX 4070 (driver 596.21), WinLibs MinGW-w64 GCC 15.2.0 r7, Ninja, Release.
  • Build: full build and libs pass, with no warnings in PosixMemory.cpp or GuestFlexibleMemoryBudget.cpp.
  • --repeat until-fail:3: guest_flexible_memory_declared, _limited, _none, _missing and _invalid pass, 15/15 runs.
  • Negative control: with PosixMemory.cpp from main and the test kept, declared, limited, none and missing abort with 0xc0000409 on all three runs, and invalid passes. The unmap succeeds and 32 KiB stay charged: declared prints rounded partial unmap: available=301301760 expected=301334528, limited prints available=16384000 expected=16416768 (the figure in the description), none prints available=469368832 expected=469401600. With the source restored all five pass.
  • Non-GPU suite (ctest -j8 --timeout 120 -E '^agc_driver_|^video_out_flip'): 865 of 878 pass and 2 are skipped. Eleven tests failed in the first pass (six conventions_function_bodies*, hardware_oracle_compare, guest_none_relocations, tls_function_coverage_register_address_loads and windows_diagnostic_fixture_isolation timed out, and guest_select aborted with 0xc0000409 while the host was loaded). All eleven pass on serial reruns, and none is related to memory. I did not run the suite on plain main.
  • tools/check_conventions.py --base 673f7406c and git diff --check pass. There are no new exports.

Titles. The description names none. In the relinked titles I have, munmap is imported by 46, sceKernelReleaseFlexibleMemory by 14 and sceKernelMapFlexibleMemory by 7. I ran six of them for 45 s with a local diagnostic libkernel that logs POSIX munmap calls with the available flexible size before and after. PPSA99109, PPSA99920 and PPSA99997 call munmap twice each, and PPSA99169, PPSA85300 and PPSA99039 never. In all six calls the available size did not change, because the ranges (0x4000 to 0x170000 bytes) were not flexible mappings, so no local title reaches the budget release in that window. PPSA99997 Nuvio for 60 s, one run per side: main 2236 frames and this PR 2069 frames, both about 60 FPS at the end and killed at the time limit, with no stop line. PPSA99169 RetroArch for 60 s, three runs on main and two on this stack: no stop line on any run (one main run lost the Vulkan device at 34 s while the host was busy).

Notes on the diff, none blocking:

  • #6515 and #6491 also change the flexible accounting in Export.cpp and the same test file. Both merge cleanly with this PR.
  • #6542 merges with this PR with one conflict, in docs/dev/debt/libkernel.md. I did not build that combination, so the unmap of file views through sceKernelMunmap is untested.

Not checked: Linux (covered above), macOS, and any title that frees flexible memory through munmap.

Written with Claude Code assistance (validation by a subagent, reviewed in this session).

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6233697459 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/6311#issuecomment-6233697459). Originally posted by [screamm](https://github.com/screamm) on 2026-10-17T12:07:31Z. --- Windows run of head `0cbec9f8f`, merged onto `main` `673f7406c` together with #6302 (no shared file; both merges clean). It complements the Linux run above. - Host: Windows 11 Pro 26220, Ryzen 7 5700X, RTX 4070 (driver 596.21), WinLibs MinGW-w64 GCC 15.2.0 r7, Ninja, Release. - Build: full build and `libs` pass, with no warnings in `PosixMemory.cpp` or `GuestFlexibleMemoryBudget.cpp`. - `--repeat until-fail:3`: `guest_flexible_memory_declared`, `_limited`, `_none`, `_missing` and `_invalid` pass, 15/15 runs. - Negative control: with `PosixMemory.cpp` from `main` and the test kept, `declared`, `limited`, `none` and `missing` abort with 0xc0000409 on all three runs, and `invalid` passes. The unmap succeeds and 32 KiB stay charged: `declared` prints `rounded partial unmap: available=301301760 expected=301334528`, `limited` prints `available=16384000 expected=16416768` (the figure in the description), `none` prints `available=469368832 expected=469401600`. With the source restored all five pass. - Non-GPU suite (`ctest -j8 --timeout 120 -E '^agc_driver_|^video_out_flip'`): 865 of 878 pass and 2 are skipped. Eleven tests failed in the first pass (six `conventions_function_bodies*`, `hardware_oracle_compare`, `guest_none_relocations`, `tls_function_coverage_register_address_loads` and `windows_diagnostic_fixture_isolation` timed out, and `guest_select` aborted with 0xc0000409 while the host was loaded). All eleven pass on serial reruns, and none is related to memory. I did not run the suite on plain `main`. - `tools/check_conventions.py --base 673f7406c` and `git diff --check` pass. There are no new exports. Titles. The description names none. In the relinked titles I have, `munmap` is imported by 46, `sceKernelReleaseFlexibleMemory` by 14 and `sceKernelMapFlexibleMemory` by 7. I ran six of them for 45 s with a local diagnostic `libkernel` that logs POSIX `munmap` calls with the available flexible size before and after. PPSA99109, PPSA99920 and PPSA99997 call `munmap` twice each, and PPSA99169, PPSA85300 and PPSA99039 never. In all six calls the available size did not change, because the ranges (0x4000 to 0x170000 bytes) were not flexible mappings, so no local title reaches the budget release in that window. PPSA99997 Nuvio for 60 s, one run per side: `main` 2236 frames and this PR 2069 frames, both about 60 FPS at the end and killed at the time limit, with no stop line. PPSA99169 RetroArch for 60 s, three runs on `main` and two on this stack: no stop line on any run (one `main` run lost the Vulkan device at 34 s while the host was busy). Notes on the diff, none blocking: - #6515 and #6491 also change the flexible accounting in `Export.cpp` and the same test file. Both merge cleanly with this PR. - #6542 merges with this PR with one conflict, in `docs/dev/debt/libkernel.md`. I did not build that combination, so the unmap of file views through `sceKernelMunmap` is untested. Not checked: Linux (covered above), macOS, and any title that frees flexible memory through `munmap`. Written with Claude Code assistance (validation by a subagent, reviewed in this session).
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!6311
No description provided.