From d544596283234237489f0e6bd53c29d8d6389fd8 Mon Sep 17 00:00:00 2001 From: Dean Galvin Date: Thu, 1 Oct 2026 16:06:42 +1300 Subject: [PATCH] fix(libkernel): back direct memory with a memfd on Linux std::tmpfile() places the shared direct memory backing in /tmp, so a title's direct memory counts against that filesystem's size (a tmpfs is half of RAM by default): once /tmp fills, touching a backing page raises SIGBUS. memfd_create gives the same shared memory pages without a filesystem or its size limit. --- .../libkernel/DirectMemory/DirectMemory.cpp | 14 +++++----- core/libs/tests/GuestMemory.cpp | 27 +++++++++++++++++++ 2 files changed, 34 insertions(+), 7 deletions(-) diff --git a/core/libs/prx/libkernel/DirectMemory/DirectMemory.cpp b/core/libs/prx/libkernel/DirectMemory/DirectMemory.cpp index 5a1919508..65bfb31cd 100644 --- a/core/libs/prx/libkernel/DirectMemory/DirectMemory.cpp +++ b/core/libs/prx/libkernel/DirectMemory/DirectMemory.cpp @@ -228,11 +228,11 @@ public: section = CreateFileMappingW(INVALID_HANDLE_VALUE, nullptr, PAGE_EXECUTE_READWRITE, static_cast(size >> 32), static_cast(size), nullptr); if (!section) throw std::system_error(static_cast(GetLastError()), std::system_category(), "create direct memory backing"); #else - file = std::tmpfile(); - if (!file) throw std::system_error(errno, std::generic_category(), "create direct memory backing"); - if (ftruncate(fileno(file), static_cast(bytes)) != 0) { + file = memfd_create("direct memory", MFD_CLOEXEC); + if (file < 0) throw std::system_error(errno, std::generic_category(), "create direct memory backing"); + if (ftruncate(file, static_cast(bytes)) != 0) { const int error = errno; - std::fclose(file); + ::close(file); throw std::system_error(error, std::generic_category(), "size direct memory backing"); } #endif @@ -244,7 +244,7 @@ public: #ifdef _WIN32 CloseHandle(section); #else - std::fclose(file); + ::close(file); #endif } @@ -255,7 +255,7 @@ public: #ifdef _WIN32 GuestArena::GuestArenaMap_nid_postfix(reinterpret_cast(address), bytes, section, offset, WinProtFromPosix(protection)); #else - if (::mmap(reinterpret_cast(address), bytes, protection, MAP_SHARED | MAP_FIXED, fileno(file), static_cast(offset)) == MAP_FAILED) throw std::system_error(errno, std::generic_category(), "map direct memory backing"); + if (::mmap(reinterpret_cast(address), bytes, protection, MAP_SHARED | MAP_FIXED, file, static_cast(offset)) == MAP_FAILED) throw std::system_error(errno, std::generic_category(), "map direct memory backing"); #endif } @@ -264,7 +264,7 @@ private: #ifdef _WIN32 HANDLE section = nullptr; #else - std::FILE* file = nullptr; + int file = -1; #endif }; diff --git a/core/libs/tests/GuestMemory.cpp b/core/libs/tests/GuestMemory.cpp index 7449b08bd..0aaaef735 100644 --- a/core/libs/tests/GuestMemory.cpp +++ b/core/libs/tests/GuestMemory.cpp @@ -16,6 +16,8 @@ #include #include #if defined(__linux__) +#include +#include #include #include #endif @@ -356,6 +358,30 @@ static void CheckWriteWatch() { Require(munmap(raw, spanned + tableSpan) == 0); } +static std::string BackingOf(const void* address) { + std::ifstream maps("/proc/self/maps"); + std::string line; + while (std::getline(maps, line)) { + if (std::strtoull(line.c_str(), nullptr, 16) != reinterpret_cast(address)) continue; + const auto path = line.find('/'); + return path == std::string::npos ? std::string() : line.substr(path); + } + return {}; +} + +static void CheckDirectMemoryBackingNeedsNoFilesystem() { + constexpr std::size_t page = 0x4000; + std::int64_t phys = 0; + Require(sceKernelAllocateDirectMemory(0, 0x7fffffffffll, page, 0, 0, &phys) == 0); + void* mapped = nullptr; + Require(sceKernelMapDirectMemory(&mapped, page, 3, 0, phys, 0) == 0); + const auto backing = BackingOf(mapped); + if (backing.rfind("/memfd:", 0) != 0) std::fprintf(stderr, "direct memory backing: %s\n", backing.c_str()); + Require(backing.rfind("/memfd:", 0) == 0); + Require(sceKernelMunmap(mapped, page) == 0); + Require(sceKernelReleaseDirectMemory(phys, page) == 0); +} + static void CheckDirectMemoryWriteWatch() { if (!GuestWriteWatch::GuestWriteWatchAvailable_nid_postfix()) { std::puts("write watch unavailable: direct memory not tested"); @@ -416,6 +442,7 @@ int main() { #if defined(__linux__) CheckWriteWatch(); CheckDirectMemoryWriteWatch(); + CheckDirectMemoryBackingNeedsNoFilesystem(); #endif constexpr std::size_t page = 0x4000; const auto failed = reinterpret_cast(static_cast(-1));