From 4b209db82cf21d26b55a4764dd0a023037b732a5 Mon Sep 17 00:00:00 2001 From: CamilleLaVey Date: Sat, 1 Aug 2026 23:20:41 -0400 Subject: [PATCH] Request maximum map counts --- src/common/host_memory.cpp | 12 ++++++++++-- src/common/memory_detect.cpp | 17 +++++++++++++++++ src/common/memory_detect.h | 2 ++ 3 files changed, 29 insertions(+), 2 deletions(-) diff --git a/src/common/host_memory.cpp b/src/common/host_memory.cpp index bae706e6ca..cb7ec1ffa7 100644 --- a/src/common/host_memory.cpp +++ b/src/common/host_memory.cpp @@ -683,7 +683,15 @@ public: total_physical >> 20, MinimumTotalPhysical >> 20); return 0; } + const u64 max_map_count = Common::GetMaxMapCount(); + constexpr u64 ReservedMaps = 24576; + if (max_map_count == 0 || max_map_count <= ReservedMaps) { + LOG_WARNING(HW_Memory, + "Skipping hardware buffer backing, vm.max_map_count is unknown or too low"); + return 0; + } u64 budget = total_physical / 6; + budget = (std::min)(budget, (max_map_count - ReservedMaps) * PageAlignment); const u64 available = Common::GetAvailablePhysicalMemory(); if (available != 0) { constexpr u64 Headroom = 2ULL << 30; @@ -695,8 +703,8 @@ public: if (budget < MinimumBudget) { LOG_INFO(HW_Memory, "Skipping hardware buffer backing, only {} MiB could be committed on a {} MiB " - "system with {} MiB available", - budget >> 20, total_physical >> 20, available >> 20); + "system with {} MiB available and vm.max_map_count {}", + budget >> 20, total_physical >> 20, available >> 20, max_map_count); return 0; } return static_cast(budget); diff --git a/src/common/memory_detect.cpp b/src/common/memory_detect.cpp index d7d8a0741d..d0f9518bf5 100644 --- a/src/common/memory_detect.cpp +++ b/src/common/memory_detect.cpp @@ -107,4 +107,21 @@ u64 GetAvailablePhysicalMemory() { #endif } +u64 GetMaxMapCount() { +#ifdef __linux__ + if (std::FILE* const file = std::fopen("/proc/sys/vm/max_map_count", "re")) { + char line[32]; + u64 count = 0; + if (std::fgets(line, sizeof(line), file) != nullptr) { + count = std::strtoull(line, nullptr, 10); + } + std::fclose(file); + return count; + } + return 0; +#else + return 0; +#endif +} + } // namespace Common diff --git a/src/common/memory_detect.h b/src/common/memory_detect.h index 7141df1172..3bf013b79a 100644 --- a/src/common/memory_detect.h +++ b/src/common/memory_detect.h @@ -20,4 +20,6 @@ struct MemoryInfo { [[nodiscard]] u64 GetAvailablePhysicalMemory(); +[[nodiscard]] u64 GetMaxMapCount(); + } // namespace Common