fix(libkernel): release flexible memory budget through munmap #6311
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/6311"
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 Ondra97 on 2026-10-15T10:53:37Z.
What
The POSIX
munmappath removes a flexible-memory mapping but leaves its bytes charged to the guest budget. A subsequent allocation can therefore returnENOMEMafter the memory has been freed. In my public-API probe with a 16 MiB configured budget, unmapping 512 KiB succeeds andsceKernelVirtualQueryconfirms 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
e49ec591dc93c9e4dfaad8b6dbc0b995dd7830a0and final head0cbec9f8f427a904297e1896b06c7f1c91abb42f, based on main8856f180ca0b5605574842666ffbc8b0bb69cc4d. 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.ENOMEM(0x8002000c)guest_flexible_memory_limitedregression 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.declared,limited,none,missing) additionally passed ten executions each: 40 runs, 0.01-0.06 seconds each. The invalid-metadata case remains covered separately.I checked complete pinned-head file inventories of 582 open PRs and manually compared the three other
PosixMemory.cppchanges: #6222 only enables macOS file-backed paths, #5835 refuses archive-backedmmap, and #5663 addsmunlockall. 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 inDirectMemory/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
main; no other open PR implements the same functionsNotImplemented_nid_no_patch); silent stubs are listed under Silent stubs in their library's technical debt file.mdfiles or images added to the repositoryImported 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.
core/libs/tests/GuestFlexibleMemoryBudget.cppcore/libs/prx/libkernel/DirectMemory/PosixMemory.cppImported 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. Head0cbec9f8fmerged onto main03c4cb5e7.PosixMemory.cppfrom main):guest_flexible_memory_declaredandguest_flexible_memory_limitedfail at the first partial unmap. The budget is not released:guest_flexible_memory_*tests pass.file_writer_binaryandfile_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.
Imported from boykopovar/AnyPS5. Originally posted by screamm on 2026-10-17T12:07:31Z.
Windows run of head
0cbec9f8f, merged ontomain673f7406ctogether with #6302 (no shared file; both merges clean). It complements the Linux run above.libspass, with no warnings inPosixMemory.cpporGuestFlexibleMemoryBudget.cpp.--repeat until-fail:3:guest_flexible_memory_declared,_limited,_none,_missingand_invalidpass, 15/15 runs.PosixMemory.cppfrommainand the test kept,declared,limited,noneandmissingabort with 0xc0000409 on all three runs, andinvalidpasses. The unmap succeeds and 32 KiB stay charged:declaredprintsrounded partial unmap: available=301301760 expected=301334528,limitedprintsavailable=16384000 expected=16416768(the figure in the description),noneprintsavailable=469368832 expected=469401600. With the source restored all five pass.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 (sixconventions_function_bodies*,hardware_oracle_compare,guest_none_relocations,tls_function_coverage_register_address_loadsandwindows_diagnostic_fixture_isolationtimed out, andguest_selectaborted 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 plainmain.tools/check_conventions.py --base 673f7406candgit diff --checkpass. There are no new exports.Titles. The description names none. In the relinked titles I have,
munmapis imported by 46,sceKernelReleaseFlexibleMemoryby 14 andsceKernelMapFlexibleMemoryby 7. I ran six of them for 45 s with a local diagnosticlibkernelthat logs POSIXmunmapcalls with the available flexible size before and after. PPSA99109, PPSA99920 and PPSA99997 callmunmaptwice 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:main2236 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 onmainand two on this stack: no stop line on any run (onemainrun lost the Vulkan device at 34 s while the host was busy).Notes on the diff, none blocking:
Export.cppand the same test file. Both merge cleanly with this PR.docs/dev/debt/libkernel.md. I did not build that combination, so the unmap of file views throughsceKernelMunmapis 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).