1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 18:27:35 +00:00

Kernel: Add MAP_NORESERVE support to mmap

Rather than lazily committing regions by default, we now commit
the entire region unless MAP_NORESERVE is specified.

This solves random crashes in low-memory situations where e.g. the
malloc heap allocated memory, but using pages that haven't been
used before triggers a crash when no more physical memory is available.

Use this flag to create large regions without actually committing
the backing memory. madvise() can be used to commit arbitrary areas
of such regions after creating them.
This commit is contained in:
Tom 2020-09-03 15:06:25 -06:00 committed by Andreas Kling
parent bc5d6992a4
commit c3451899bc
7 changed files with 30 additions and 15 deletions

View file

@ -187,13 +187,18 @@ auto Region::set_volatile(VirtualAddress vaddr, size_t size, bool is_volatile, b
// Attempt to remap the page range. We want to make sure we have
// enough memory, if not we need to inform the caller of that
// fact
if (!remap_page_range(first_page_index, last_page_index - first_page_index))
if (!remap_page_range(first_page_index, last_page_index - first_page_index, true))
return SetVolatileError::OutOfMemory;
}
}
return SetVolatileError::Success;
}
bool Region::can_commit() const
{
return vmobject().is_anonymous() || vmobject().is_purgeable();
}
bool Region::commit()
{
ScopedSpinLock lock(s_mm_lock);
@ -339,7 +344,7 @@ bool Region::map_individual_page_impl(size_t page_index)
return true;
}
bool Region::remap_page_range(size_t page_index, size_t page_count)
bool Region::remap_page_range(size_t page_index, size_t page_count, bool do_commit)
{
bool success = true;
ScopedSpinLock lock(s_mm_lock);
@ -347,6 +352,10 @@ bool Region::remap_page_range(size_t page_index, size_t page_count)
ScopedSpinLock page_lock(m_page_directory->get_lock());
size_t index = page_index;
while (index < page_index + page_count) {
if (do_commit && !commit(index)) {
success = false;
break;
}
if (!map_individual_page_impl(index)) {
success = false;
break;