2020-07-30 23:38:15 +02:00
|
|
|
/*
|
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
|
|
|
|
*
|
2021-04-22 01:24:48 -07:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-07-30 23:38:15 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include <AK/NonnullRefPtrVector.h>
|
2021-06-21 17:34:09 +02:00
|
|
|
#include <Kernel/Arch/x86/InterruptDisabler.h>
|
2020-07-30 23:38:15 +02:00
|
|
|
#include <Kernel/Process.h>
|
2020-09-05 15:52:14 -06:00
|
|
|
#include <Kernel/VM/AnonymousVMObject.h>
|
2020-07-30 23:38:15 +02:00
|
|
|
#include <Kernel/VM/InodeVMObject.h>
|
|
|
|
|
#include <Kernel/VM/MemoryManager.h>
|
|
|
|
|
|
|
|
|
|
namespace Kernel {
|
|
|
|
|
|
2021-03-01 13:49:16 +01:00
|
|
|
KResultOr<int> Process::sys$purge(int mode)
|
2020-07-30 23:38:15 +02:00
|
|
|
{
|
|
|
|
|
REQUIRE_NO_PROMISES;
|
|
|
|
|
if (!is_superuser())
|
2021-03-01 13:49:16 +01:00
|
|
|
return EPERM;
|
2020-07-30 23:38:15 +02:00
|
|
|
int purged_page_count = 0;
|
|
|
|
|
if (mode & PURGE_ALL_VOLATILE) {
|
2020-09-05 15:52:14 -06:00
|
|
|
NonnullRefPtrVector<AnonymousVMObject> vmobjects;
|
2020-07-30 23:38:15 +02:00
|
|
|
{
|
2021-04-29 02:03:39 -07:00
|
|
|
KResult result(KSuccess);
|
2020-07-30 23:38:15 +02:00
|
|
|
InterruptDisabler disabler;
|
2021-01-01 15:32:06 +01:00
|
|
|
MM.for_each_vmobject([&](auto& vmobject) {
|
2021-04-29 02:03:39 -07:00
|
|
|
if (vmobject.is_anonymous()) {
|
|
|
|
|
// In the event that the append fails, only attempt to continue
|
|
|
|
|
// the purge if we have already appended something successfully.
|
|
|
|
|
if (!vmobjects.try_append(vmobject) && vmobjects.is_empty()) {
|
|
|
|
|
result = ENOMEM;
|
|
|
|
|
return IterationDecision::Break;
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-07-30 23:38:15 +02:00
|
|
|
return IterationDecision::Continue;
|
|
|
|
|
});
|
2021-04-29 02:03:39 -07:00
|
|
|
|
|
|
|
|
if (result.is_error())
|
|
|
|
|
return result.error();
|
2020-07-30 23:38:15 +02:00
|
|
|
}
|
|
|
|
|
for (auto& vmobject : vmobjects) {
|
|
|
|
|
purged_page_count += vmobject.purge();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (mode & PURGE_ALL_CLEAN_INODE) {
|
|
|
|
|
NonnullRefPtrVector<InodeVMObject> vmobjects;
|
|
|
|
|
{
|
2021-04-29 02:03:39 -07:00
|
|
|
KResult result(KSuccess);
|
2020-07-30 23:38:15 +02:00
|
|
|
InterruptDisabler disabler;
|
2021-01-01 15:32:06 +01:00
|
|
|
MM.for_each_vmobject([&](auto& vmobject) {
|
2021-04-29 02:03:39 -07:00
|
|
|
if (vmobject.is_inode()) {
|
|
|
|
|
// In the event that the append fails, only attempt to continue
|
|
|
|
|
// the purge if we have already appended something successfully.
|
|
|
|
|
if (!vmobjects.try_append(static_cast<InodeVMObject&>(vmobject)) && vmobjects.is_empty()) {
|
|
|
|
|
result = ENOMEM;
|
|
|
|
|
return IterationDecision::Break;
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-07-30 23:38:15 +02:00
|
|
|
return IterationDecision::Continue;
|
|
|
|
|
});
|
2021-04-29 02:03:39 -07:00
|
|
|
|
|
|
|
|
if (result.is_error())
|
|
|
|
|
return result.error();
|
2020-07-30 23:38:15 +02:00
|
|
|
}
|
|
|
|
|
for (auto& vmobject : vmobjects) {
|
|
|
|
|
purged_page_count += vmobject.release_all_clean_pages();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return purged_page_count;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|