From 9e9924115f68c0cf7c24697510ed07b726529983 Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Sun, 14 Aug 2022 21:46:08 +0200 Subject: [PATCH] Kernel: Release some clean file-backed memory when starved for pages Until now, our only backup plan when running out of physical pages was to try and purge volatile memory. If that didn't work out, we just hung userspace out to dry with an ENOMEM. This patch improves the situation by also considering clean, file-backed pages (that we could page back in from disk). This could be better in many ways, but it already allows us to boot to WindowServer with 256 MiB of RAM. :^) --- Kernel/Memory/MemoryManager.cpp | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/Kernel/Memory/MemoryManager.cpp b/Kernel/Memory/MemoryManager.cpp index fda5530b02..0b312fa4a6 100644 --- a/Kernel/Memory/MemoryManager.cpp +++ b/Kernel/Memory/MemoryManager.cpp @@ -929,6 +929,20 @@ ErrorOr> MemoryManager::allocate_physical_page(Shoul } return IterationDecision::Continue; }); + // Second, we look for a file-backed VMObject with clean pages. + for_each_vmobject([&](auto& vmobject) { + if (!vmobject.is_inode()) + return IterationDecision::Continue; + auto& inode_vmobject = static_cast(vmobject); + // FIXME: It seems excessive to release *all* clean pages from the inode when we only need one. + if (auto released_page_count = inode_vmobject.release_all_clean_pages()) { + dbgln("MM: Clean inode release saved the day! Released {} pages from InodeVMObject", released_page_count); + page = find_free_physical_page(false); + VERIFY(page); + return IterationDecision::Break; + } + return IterationDecision::Continue; + }); if (!page) { dmesgln("MM: no physical pages available"); return ENOMEM;