ladybird/Kernel/VM/PurgeableVMObject.cpp
Andreas Kling c1f8291ce4 Kernel: When physical page allocation fails, try to purge something
Instead of panicking right away when we run out of physical pages,
we now try to find a PurgeableVMObject with some volatile pages in it.
If we find one, we purge that entire object and steal one of its pages.

This makes it possible for the kernel to keep going instead of dying.
Very cool. :^)
2019-12-26 11:45:36 +01:00

61 lines
1.3 KiB
C++

#include <Kernel/VM/MemoryManager.h>
#include <Kernel/VM/PhysicalPage.h>
#include <Kernel/VM/PurgeableVMObject.h>
NonnullRefPtr<PurgeableVMObject> PurgeableVMObject::create_with_size(size_t size)
{
return adopt(*new PurgeableVMObject(size));
}
PurgeableVMObject::PurgeableVMObject(size_t size)
: AnonymousVMObject(size)
{
}
PurgeableVMObject::PurgeableVMObject(const PurgeableVMObject& other)
: AnonymousVMObject(other)
{
}
PurgeableVMObject::~PurgeableVMObject()
{
}
NonnullRefPtr<VMObject> PurgeableVMObject::clone()
{
return adopt(*new PurgeableVMObject(*this));
}
int PurgeableVMObject::purge()
{
LOCKER(m_paging_lock);
return purge_impl();
}
int PurgeableVMObject::purge_with_interrupts_disabled(Badge<MemoryManager>)
{
ASSERT_INTERRUPTS_DISABLED();
if (m_paging_lock.is_locked())
return 0;
return purge_impl();
}
int PurgeableVMObject::purge_impl()
{
if (!m_volatile)
return 0;
int purged_page_count = 0;
for (size_t i = 0; i < m_physical_pages.size(); ++i) {
if (m_physical_pages[i])
++purged_page_count;
m_physical_pages[i] = nullptr;
}
m_was_purged = true;
for_each_region([&](auto& region) {
region.remap();
});
return purged_page_count;
}