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

Kernel: Implement lazy committed page allocation

By designating a committed page pool we can guarantee to have physical
pages available for lazy allocation in mappings. However, when forking
we will overcommit. The assumption is that worst-case it's better for
the fork to die due to insufficient physical memory on COW access than
the parent that created the region. If a fork wants to ensure that all
memory is available (trigger a commit) then it can use madvise.

This also means that fork now can gracefully fail if we don't have
enough physical pages available.
This commit is contained in:
Tom 2020-09-04 21:12:25 -06:00 committed by Andreas Kling
parent e21cc4cff6
commit b2a52f6208
20 changed files with 329 additions and 67 deletions

View file

@ -88,7 +88,14 @@ pid_t Process::sys$fork(RegisterState& regs)
#ifdef FORK_DEBUG
dbg() << "fork: cloning Region{" << &region << "} '" << region.name() << "' @ " << region.vaddr();
#endif
auto& child_region = child->add_region(region.clone());
auto region_clone = region.clone();
if (!region_clone) {
dbg() << "fork: Cannot clone region, insufficient memory";
// TODO: tear down new process?
return -ENOMEM;
}
auto& child_region = child->add_region(region_clone.release_nonnull());
child_region.map(child->page_directory());
if (&region == m_master_tls_region.unsafe_ptr())

View file

@ -52,10 +52,14 @@ int Process::sys$shbuf_create(int size, void** buffer)
return -EINVAL;
size = PAGE_ROUND_UP(size);
auto vmobject = PurgeableVMObject::create_with_size(size);
if (!vmobject)
return -ENOMEM;
LOCKER(shared_buffers().lock());
static int s_next_shbuf_id;
int shbuf_id = ++s_next_shbuf_id;
auto shared_buffer = make<SharedBuffer>(shbuf_id, size);
auto shared_buffer = make<SharedBuffer>(shbuf_id, vmobject.release_nonnull());
shared_buffer->share_with(m_pid);
void* address = shared_buffer->ref_for_process_and_get_address(*this);