perf(libkernel): back flexible memory with a memfd on Linux #23613
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/23613"
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 JJenkx on 2026-11-19T10:26:05Z.
What
On Linux, flexible memory (
sceKernelMapFlexibleMemoryand the named forms, through_mapFlexible) was anonymous private memory. The shared-backing resolvers (SharedBacking,SharedBackingRuns) describe only direct memory, so when the GPU driver imported a flexible range it fell back to a host-pointer import. amdgpu walks the pages of a host-pointer (userptr) buffer object at every command submission, so the cost of everyvkQueueSubmitgrows with the size of the imported flexible range.Each flexible map now gets its own sealed memfd:
_mapFlexiblemaps throughDoMapFlexibleAnon, which marks the call so thatMapAnonymous, andRemapFixedIntoRegisteredfor a fixed map into a reserved range, map a memfd shared over the range they placed and register its write watch again;MADV_HUGEPAGEasks for transparent huge pages, which shared memory gets only on request whereshmem_enabledisadvise;SharedBackingandSharedBackingRunsresolve a flexible range to its file and offset, so the driver imports it through udmabuf as it does direct memory;Plain anonymous
mmapmemory is unchanged. Windows and macOS are unchanged.Performance
Matched runs on one machine (AMD Radeon RX 6650 XT, RADV Mesa 26.2.7, Linux 7.2.14, Release):
mainb8be4dewith and without this change, both with the #21747 deadline line; save data restored before every run, order main, change, change, main.PPSA01341 imports about 359 MiB of flexible memory for the GPU. Without this change that range is a host-pointer import, and
vkQueueSubmitaverages 2.7 to 7.1 ms over a run in five of sixmainruns (0.42 ms in the sixth, a 180 s run that was still in the intro when it ended); with this change it averages 0.14 to 0.18 ms in all six runs. Because a slow submit also slows the game's own progress, a fixed time window shows different scenes in the two arms, so the frame rates below compare the same content, identified from screenshots taken every 30 s over 360 s runs:vkQueueSubmitmean over the runEach row comes from screenshots taken every 30 s in four 360 s runs (two per arm); the change runs took different menu routes because the scripted button presses landed on different screens.
Shorter 180 s runs of PPSA09809 and PPSA05595, which map little flexible memory (PPSA05595 maps under 1 MiB), show no difference beyond their run-to-run spread:
PPSA09809's menu frame rate depends on which entry the save leaves selected, and PPSA05595's falls steadily through each run (about 40 to 2 FPS), so neither resolves small changes.
Tested
guest_memory(with the newCheckFlexibleMemorySharedBacking, andCheckDirectMemorySharedBackingnow expecting a flexible range to resolve to its own file) and the eightguest_flexible_memory_*budget tests pass on Linux.tools/check_conventions.pyandtools/debt_fold.py --checkpass.mainee85d6e56982; the files it changes are identical there and at the testedb8be4de.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 repository (attach them to this PR)Imported from boykopovar/AnyPS5. Originally posted by PrivacyOnion on 2026-11-19T10:28:57Z.
Review: APPROVED
Verified on Linux (Ubuntu 24.04, GCC 13.3, Ninja, Release) against
9c6afd988c:tools/check_conventions.pypassed.guest_memory_testsbuilt cleanly.guest_memory,guest_mmap_fixed_anonymous,guest_mmap_executable):core/libs/prx/libkernel/DirectMemory/DirectMemory.cpp,core/libs/prx/libkernel/DirectMemory/DirectMemory.hpp,core/libs/prx/libkernel/DirectMemory/Export.cppreverted tomainand the new tests kept, the suite fails: Restoring the change makes it pass again.Looks Good To Me
Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-19T18:21:30Z.
Review: COMMENTED
Reviewed — no blocking findings
The reviewed head is
ec955c40b624122c6004c2ea689d3f441ce89c32; the inspected implementation and tests support the flexible-memory backing change, with one changed GPU-import file not fully reviewed.What I ran
Results
Findings
Blocking
None verified in the code inspected.
Non-blocking
None.
Not verified
core/libs/prx/libSceAgcDriver/Graphics/src/GuestBufferMemory.cppis too large for the review folder; only its changed hunk and nearby caller logic were inspected. Its full file and remaining changed context were not reviewed. The other changed files were read at the added-line ranges, with targeted surrounding context inDirectMemory.cpp,Export.cpp, andPosixMemory.cpp. No test output, build result, performance capture, physical-console behavior, or CI result was independently confirmed.To get this to a good state
Nothing is required.
Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-19T20:27:07Z.
Review: CHANGES_REQUESTED
Reviewed — changes requested
Changes are required before merge: the backing fallback path can fail to preserve the host-pointer import after a partial GPU import failure.
What I ran
1cec0a3837261238590788e8292ea34b8696805fusing the prepared diff andrepo_read.pyat that head. No tests or builds were run.core/libs/prx/libc/include/GuestArena.hpp,core/libs/prx/libc/src/GuestArena.cpp,core/libs/prx/libkernel/DirectMemory/DirectMemory.cpp,core/libs/prx/libkernel/DirectMemory/DirectMemory.hpp,core/libs/prx/libkernel/DirectMemory/Export.cpp,core/libs/prx/libkernel/DirectMemory/PosixMemory.cpp,core/libs/prx/libSceAgcDriver/Graphics/src/GuestBufferMemory.cpp,core/libs/tests/GuestMemory.cpp, and both added flexible-memory debt notes.GuestBufferMemory.cppwas read by range through the reader because it was not included in the review folder.Results
Findings
Blocking
entry. The fallback is triggered aftercreateSharedBackingImportreturns an error (core/libs/prx/libSceAgcDriver/Graphics/src/GuestBufferMemory.cpp:2560), but unlike the existing Windows settle path, it does not destroy/reset the failed import before callingcreateHostPointerImport(core/libs/prx/libSceAgcDriver/Graphics/src/GuestBufferMemory.cpp:2563). If the failed import partially initializedentry, retrying into the same object can leak resources or leave stale handles. Clear the failed import and reset the entry before retrying, and cover a partial-import failure in a test.Non-blocking
None.
Not verified
GuestBufferMemory.cppfull file was not read; only the changed hunk and the directly relevant helper and import paths were read.To get this to a good state
Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-19T22:35:14Z.
Review: APPROVED
Reviewed — looks good
Nothing blocks this change; the checks I could run passed, and the prepared diff and inspected source support merging.
What I ran
Reviewed head
cf3a90f06583aaf2d21bf566dd32d44b90471cda. No build or tests were run in this review; the test results in the PR description are author-reported.Results
Findings
Blocking
None.
Non-blocking
None.
Not verified
The full text of
core/libs/prx/libSceAgcDriver/Graphics/src/GuestBufferMemory.cppwas excluded by the review bundle size rule; I read its changed regions and relevant import helpers throughrepo_read.py. I did not independently run builds, tests, performance captures, or console behavior. The PR description's reported checks and measurements were not independently verified.To get this to a good state
Nothing is required.
Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-20T07:59:02Z.
Review: COMMENTED
Reviewed — no blocking findings
The reviewed diff introduces Linux memfd backing for flexible memory and retries failed flexible dma-buf imports with a host-pointer import; I found no verified blocking defect in the inspected code.
What I ran
No commands or tests were run. I read the prepared diff and targeted source ranges through
repo_read.pyat head4d15b34fa601b7458fc1acecd9119f33d4f86596; no merged-preview check was performed.Results
Findings
Blocking
None verified in the inspected code.
Non-blocking
None.
Not verified
The review folder’s manifest lists 8 changed files, 7 included and 1 excluded because it was too large:
core/libs/prx/libSceAgcDriver/Graphics/src/GuestBufferMemory.cpp. I read targeted ranges in that excluded file throughrepo_read.py, but not its full text. I read targeted ranges inDirectMemory.cpp,GuestMemory.cpp, and the new flexible dma-buf test; the remaining included changed files were not read. Build, tests, runtime performance results, and console behavior were not independently verified. The performance and test results in the PR description are author-reported.To get this to a good state
Nothing is required.