From 5e425e4e9ecbe28ae88472b311767f3b3fd9c984 Mon Sep 17 00:00:00 2001 From: lizzie Date: Sun, 26 Apr 2026 09:39:11 +0000 Subject: [PATCH] [ports, freebsd] use shm_create_largepage(3) with a DEFAULT allocation to exploit defragmented RAM situations Signed-off-by: lizzie --- src/common/host_memory.cpp | 33 +++++++++++++++++++++++++++++++-- 1 file changed, 31 insertions(+), 2 deletions(-) diff --git a/src/common/host_memory.cpp b/src/common/host_memory.cpp index 75a9da80e9..975b5c407b 100644 --- a/src/common/host_memory.cpp +++ b/src/common/host_memory.cpp @@ -22,6 +22,7 @@ #include #include #include "common/scope_exit.h" +#include "common/literals.h" #if defined(__linux__) #include @@ -526,7 +527,30 @@ public: #elif defined(__OpenBSD__) fd = shm_open_anon(O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW, 0600); #elif defined(__FreeBSD__) - fd = shm_open(SHM_ANON, O_RDWR, 0600); + int n_page_sizes = getpagesizes(nullptr, 0); + if (n_page_sizes > 0) { + std::vector page_sizes(n_page_sizes); + if (getpagesizes(page_sizes.data(), n_page_sizes) > 0) { + size_t max_size = page_sizes[0]; + size_t max_index = 0; + for (size_t i = 0; i < page_sizes.size(); ++i) { + using namespace Common::Literals; + if (page_sizes[i] <= 4_GiB) { + max_size = (std::max)(max_size, page_sizes[i]); + max_index = i; + } + } + LOG_WARNING(Common_Memory, "using largepage of size {} #{}", max_size, max_index); + // Do not use SHM_LARGEPAGE_ALLOC_HARD, yknow what will happen when you do? + // the entire Eden process will hang for eternity! that's what will happen + // Want to fuck around and find out? Go ahead, I tempt you change the "default" to "hard" + fd = shm_create_largepage(SHM_ANON, O_RDWR, max_index, SHM_LARGEPAGE_ALLOC_DEFAULT, 0600); + } + } + if (fd < 0) { + LOG_WARNING(Common_Memory, "unable to force largepage: {}", strerror(errno)); + fd = shm_open(SHM_ANON, O_RDWR, 0600); + } #elif defined(__APPLE__) || defined(__managarm__) // macOS doesn't have memfd_create, use anonymous temporary file char template_path[] = "/tmp/eden_mem_XXXXXX"; @@ -558,7 +582,12 @@ public: close(fd); } } else { - backing_base = static_cast(mmap(nullptr, backing_size, PROT_READ | PROT_WRITE, MAP_SHARED | MAP_NOCORE, fd, 0)); + int map_flags =MAP_SHARED | MAP_NOCORE; +#ifdef __FreeBSD__ + // Prevent dirty tracking of memfd + map_flags |= MAP_NOSYNC; +#endif + backing_base = static_cast(mmap(nullptr, backing_size, PROT_READ | PROT_WRITE, map_flags, fd, 0)); } if (backing_base == MAP_FAILED) { LOG_CRITICAL(HW_Memory, "mmap failed: {}", strerror(errno));