fix(libkernel): back direct memory with a memfd on Linux #203

Merged
oneandonlydean merged 1 commit from upstream-pr/203 into main 2026-10-01 08:31:44 +00:00
oneandonlydean commented 2026-10-01 03:12:03 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-01T03:12:03Z.


What

Follows abb9c852.

On Linux, PhysicalBacking creates the shared direct memory backing with std::tmpfile(), which glibc places in /tmp. A title's direct memory therefore counts against that filesystem's size: with /tmp on a tmpfs (half of RAM by default) a title with several GiB of direct memory can fill it, and touching a backing page past that raises SIGBUS. With /tmp on disk it costs disk space instead.

memfd_create gives the same shared, ftruncate-sized, MAP_SHARED-mappable memory without a filesystem or its size limit. The handle is now a file descriptor (MFD_CLOEXEC) instead of a FILE*; Windows is unchanged.

In PPSA21564 the 4.5 GiB backing file in /tmp filled it, and the title died with SIGBUS reading a direct memory page in GuestBufferMemory::AcquireRegistered.

Tested

  • Linux (CachyOS, RTX 4090): new CheckDirectMemoryBackingNeedsNoFilesystem in guest_memory_tests maps direct memory and reads its backing from /proc/self/maps. Without the fix it fails and prints direct memory backing: /tmp/#889275 (deleted); with it the backing is /memfd:direct memory (deleted) and the test passes. The existing direct memory tests (aliases, protections, release and reallocation, write watch) pass unchanged.
  • Exercised in PPSA21564 on our integration branch: no more SIGBUS once direct memory grew past the free space in /tmp.
  • ctest --test-dir build: 79/79 pass on current main with this branch.

Checklist

  • Based on current main; no other open PR implements the same functions (#198 also touches DirectMemory.cpp, in DoReserveVirtual; the two merge cleanly)
  • 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 in TechnicalDebt
  • 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
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4698747522 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/203). Originally posted by [oneandonlydean](https://github.com/oneandonlydean) on 2026-10-01T03:12:03Z. --- ### What Follows abb9c852. On Linux, `PhysicalBacking` creates the shared direct memory backing with `std::tmpfile()`, which glibc places in `/tmp`. A title's direct memory therefore counts against that filesystem's size: with `/tmp` on a tmpfs (half of RAM by default) a title with several GiB of direct memory can fill it, and touching a backing page past that raises SIGBUS. With `/tmp` on disk it costs disk space instead. `memfd_create` gives the same shared, `ftruncate`-sized, `MAP_SHARED`-mappable memory without a filesystem or its size limit. The handle is now a file descriptor (`MFD_CLOEXEC`) instead of a `FILE*`; Windows is unchanged. In PPSA21564 the 4.5 GiB backing file in `/tmp` filled it, and the title died with SIGBUS reading a direct memory page in `GuestBufferMemory::AcquireRegistered`. ### Tested - Linux (CachyOS, RTX 4090): new `CheckDirectMemoryBackingNeedsNoFilesystem` in `guest_memory_tests` maps direct memory and reads its backing from `/proc/self/maps`. Without the fix it fails and prints `direct memory backing: /tmp/#889275 (deleted)`; with it the backing is `/memfd:direct memory (deleted)` and the test passes. The existing direct memory tests (aliases, protections, release and reallocation, write watch) pass unchanged. - Exercised in PPSA21564 on our integration branch: no more SIGBUS once direct memory grew past the free space in `/tmp`. - `ctest --test-dir build`: 79/79 pass on current main with this branch. ### Checklist - [x] Based on current `main`; no other open PR implements the same functions (#198 also touches `DirectMemory.cpp`, in `DoReserveVirtual`; the two merge cleanly) - [x] One topic per PR; follow-ups go in a new PR - [x] No comments in code except technical debt - [x] Unimplemented paths throw (`NotImplemented_nid_no_patch`); silent stubs are listed in TechnicalDebt - [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] AI-assisted: yes
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!203
No description provided.