mirror of
https://github.com/RGBCube/serenity
synced 2025-07-25 23:47:45 +00:00
Kernel: Make purgeable memory a VMObject level concept (again)
This patch changes the semantics of purgeable memory. - AnonymousVMObject now has a "purgeable" flag. It can only be set when constructing the object. (Previously, all anonymous memory was effectively purgeable.) - AnonymousVMObject now has a "volatile" flag. It covers the entire range of physical pages. (Previously, we tracked ranges of volatile pages, effectively making it a page-level concept.) - Non-volatile objects maintain a physical page reservation via the committed pages mechanism, to ensure full coverage for page faults. - When an object is made volatile, it relinquishes any unused committed pages immediately. If later made non-volatile again, we then attempt to make a new committed pages reservation. If this fails, we return ENOMEM to userspace. mmap() now creates purgeable objects if passed the MAP_PURGEABLE option together with MAP_ANONYMOUS. anon_create() memory is always purgeable.
This commit is contained in:
parent
6bb53d6a80
commit
2d1a651e0a
17 changed files with 189 additions and 1004 deletions
|
@ -29,7 +29,7 @@ KResultOr<FlatPtr> Process::sys$anon_create(size_t size, int options)
|
|||
if (new_fd < 0)
|
||||
return new_fd;
|
||||
|
||||
auto vmobject = AnonymousVMObject::try_create_with_size(size, AllocationStrategy::Reserve);
|
||||
auto vmobject = AnonymousVMObject::try_create_purgeable_with_size(size, AllocationStrategy::Reserve);
|
||||
if (!vmobject)
|
||||
return ENOMEM;
|
||||
|
||||
|
|
|
@ -12,6 +12,7 @@
|
|||
#include <Kernel/PerformanceEventBuffer.h>
|
||||
#include <Kernel/PerformanceManager.h>
|
||||
#include <Kernel/Process.h>
|
||||
#include <Kernel/VM/AnonymousVMObject.h>
|
||||
#include <Kernel/VM/MemoryManager.h>
|
||||
#include <Kernel/VM/PageDirectory.h>
|
||||
#include <Kernel/VM/PrivateInodeVMObject.h>
|
||||
|
@ -217,7 +218,14 @@ KResultOr<FlatPtr> Process::sys$mmap(Userspace<const Syscall::SC_mmap_params*> u
|
|||
|
||||
if (map_anonymous) {
|
||||
auto strategy = map_noreserve ? AllocationStrategy::None : AllocationStrategy::Reserve;
|
||||
auto region_or_error = space().allocate_region(range.value(), {}, prot, strategy);
|
||||
RefPtr<AnonymousVMObject> vmobject;
|
||||
if (flags & MAP_PURGEABLE)
|
||||
vmobject = AnonymousVMObject::try_create_purgeable_with_size(page_round_up(size), strategy);
|
||||
else
|
||||
vmobject = AnonymousVMObject::try_create_with_size(page_round_up(size), strategy);
|
||||
if (!vmobject)
|
||||
return ENOMEM;
|
||||
auto region_or_error = space().allocate_region_with_vmobject(range.value(), vmobject.release_nonnull(), 0, {}, prot, map_shared);
|
||||
if (region_or_error.is_error())
|
||||
return region_or_error.error().error();
|
||||
region = region_or_error.value();
|
||||
|
@ -465,23 +473,17 @@ KResultOr<FlatPtr> Process::sys$madvise(Userspace<void*> address, size_t size, i
|
|||
if (set_volatile || set_nonvolatile) {
|
||||
if (!region->vmobject().is_anonymous())
|
||||
return EPERM;
|
||||
auto& vmobject = static_cast<AnonymousVMObject&>(region->vmobject());
|
||||
bool was_purged = false;
|
||||
switch (region->set_volatile(VirtualAddress(address), size, set_volatile, was_purged)) {
|
||||
case Region::SetVolatileError::Success:
|
||||
break;
|
||||
case Region::SetVolatileError::NotPurgeable:
|
||||
return EPERM;
|
||||
case Region::SetVolatileError::OutOfMemory:
|
||||
return ENOMEM;
|
||||
}
|
||||
if (set_nonvolatile)
|
||||
return was_purged ? 1 : 0;
|
||||
return 0;
|
||||
auto result = vmobject.set_volatile(set_volatile, was_purged);
|
||||
if (result.is_error())
|
||||
return result.error();
|
||||
return was_purged ? 1 : 0;
|
||||
}
|
||||
if (advice & MADV_GET_VOLATILE) {
|
||||
if (!region->vmobject().is_anonymous())
|
||||
return EPERM;
|
||||
return region->is_volatile(VirtualAddress(address), size) ? 0 : 1;
|
||||
return static_cast<AnonymousVMObject&>(region->vmobject()).is_volatile() ? 0 : 1;
|
||||
}
|
||||
return EINVAL;
|
||||
}
|
||||
|
@ -668,5 +670,4 @@ KResultOr<FlatPtr> Process::sys$msyscall(Userspace<void*> address)
|
|||
region->set_syscall_region(true);
|
||||
return 0;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -19,7 +19,7 @@ KResultOr<FlatPtr> Process::sys$purge(int mode)
|
|||
REQUIRE_NO_PROMISES;
|
||||
if (!is_superuser())
|
||||
return EPERM;
|
||||
int purged_page_count = 0;
|
||||
size_t purged_page_count = 0;
|
||||
if (mode & PURGE_ALL_VOLATILE) {
|
||||
NonnullRefPtrVector<AnonymousVMObject> vmobjects;
|
||||
{
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue