2020-07-31 00:38:15 +03:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
|
|
|
*
|
2021-04-22 11:24:48 +03:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-07-31 00:38:15 +03:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <AK/NonnullRefPtrVector.h>
|
|
|
|
#include <Kernel/Process.h>
|
2020-09-06 00:52:14 +03:00
|
|
|
#include <Kernel/VM/AnonymousVMObject.h>
|
2020-07-31 00:38:15 +03:00
|
|
|
#include <Kernel/VM/InodeVMObject.h>
|
|
|
|
#include <Kernel/VM/MemoryManager.h>
|
|
|
|
|
|
|
|
namespace Kernel {
|
|
|
|
|
2021-03-01 15:49:16 +03:00
|
|
|
KResultOr<int> Process::sys$purge(int mode)
|
2020-07-31 00:38:15 +03:00
|
|
|
{
|
|
|
|
REQUIRE_NO_PROMISES;
|
|
|
|
if (!is_superuser())
|
2021-03-01 15:49:16 +03:00
|
|
|
return EPERM;
|
2020-07-31 00:38:15 +03:00
|
|
|
int purged_page_count = 0;
|
|
|
|
if (mode & PURGE_ALL_VOLATILE) {
|
2020-09-06 00:52:14 +03:00
|
|
|
NonnullRefPtrVector<AnonymousVMObject> vmobjects;
|
2020-07-31 00:38:15 +03:00
|
|
|
{
|
2021-04-29 12:03:39 +03:00
|
|
|
KResult result(KSuccess);
|
2020-07-31 00:38:15 +03:00
|
|
|
InterruptDisabler disabler;
|
2021-01-01 17:32:06 +03:00
|
|
|
MM.for_each_vmobject([&](auto& vmobject) {
|
2021-04-29 12:03:39 +03: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-31 00:38:15 +03:00
|
|
|
return IterationDecision::Continue;
|
|
|
|
});
|
2021-04-29 12:03:39 +03:00
|
|
|
|
|
|
|
if (result.is_error())
|
|
|
|
return result.error();
|
2020-07-31 00:38:15 +03:00
|
|
|
}
|
|
|
|
for (auto& vmobject : vmobjects) {
|
|
|
|
purged_page_count += vmobject.purge();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (mode & PURGE_ALL_CLEAN_INODE) {
|
|
|
|
NonnullRefPtrVector<InodeVMObject> vmobjects;
|
|
|
|
{
|
2021-04-29 12:03:39 +03:00
|
|
|
KResult result(KSuccess);
|
2020-07-31 00:38:15 +03:00
|
|
|
InterruptDisabler disabler;
|
2021-01-01 17:32:06 +03:00
|
|
|
MM.for_each_vmobject([&](auto& vmobject) {
|
2021-04-29 12:03:39 +03: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-31 00:38:15 +03:00
|
|
|
return IterationDecision::Continue;
|
|
|
|
});
|
2021-04-29 12:03:39 +03:00
|
|
|
|
|
|
|
if (result.is_error())
|
|
|
|
return result.error();
|
2020-07-31 00:38:15 +03:00
|
|
|
}
|
|
|
|
for (auto& vmobject : vmobjects) {
|
|
|
|
purged_page_count += vmobject.release_all_clean_pages();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return purged_page_count;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|