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
|
|
|
*/
|
|
|
|
|
2022-08-19 21:53:40 +03:00
|
|
|
#include <Kernel/Library/NonnullLockRefPtrVector.h>
|
2021-08-06 11:45:34 +03:00
|
|
|
#include <Kernel/Memory/AnonymousVMObject.h>
|
|
|
|
#include <Kernel/Memory/InodeVMObject.h>
|
|
|
|
#include <Kernel/Memory/MemoryManager.h>
|
2020-07-31 00:38:15 +03:00
|
|
|
#include <Kernel/Process.h>
|
|
|
|
|
|
|
|
namespace Kernel {
|
|
|
|
|
2021-11-08 02:51:39 +03:00
|
|
|
ErrorOr<FlatPtr> Process::sys$purge(int mode)
|
2020-07-31 00:38:15 +03:00
|
|
|
{
|
2022-08-17 23:03:04 +03:00
|
|
|
VERIFY_PROCESS_BIG_LOCK_ACQUIRED(this);
|
2021-12-29 12:11:45 +03:00
|
|
|
TRY(require_no_promises());
|
2022-08-21 01:21:01 +03:00
|
|
|
auto credentials = this->credentials();
|
|
|
|
if (!credentials->is_superuser())
|
2021-03-01 15:49:16 +03:00
|
|
|
return EPERM;
|
2021-07-25 02:46:44 +03:00
|
|
|
size_t purged_page_count = 0;
|
2020-07-31 00:38:15 +03:00
|
|
|
if (mode & PURGE_ALL_VOLATILE) {
|
2022-08-19 21:53:40 +03:00
|
|
|
NonnullLockRefPtrVector<Memory::AnonymousVMObject> vmobjects;
|
2020-07-31 00:38:15 +03:00
|
|
|
{
|
2021-11-08 02:51:39 +03:00
|
|
|
ErrorOr<void> result;
|
2021-10-02 09:45:15 +03:00
|
|
|
Memory::MemoryManager::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.
|
2021-11-10 13:55:37 +03:00
|
|
|
if (auto append_result = vmobjects.try_append(static_cast<Memory::AnonymousVMObject&>(vmobject)); append_result.is_error() && vmobjects.is_empty()) {
|
|
|
|
result = append_result.release_error();
|
2021-04-29 12:03:39 +03:00
|
|
|
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())
|
2021-11-08 02:51:39 +03:00
|
|
|
return result.release_error();
|
2020-07-31 00:38:15 +03:00
|
|
|
}
|
|
|
|
for (auto& vmobject : vmobjects) {
|
|
|
|
purged_page_count += vmobject.purge();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (mode & PURGE_ALL_CLEAN_INODE) {
|
2022-08-19 21:53:40 +03:00
|
|
|
NonnullLockRefPtrVector<Memory::InodeVMObject> vmobjects;
|
2020-07-31 00:38:15 +03:00
|
|
|
{
|
2021-11-08 02:51:39 +03:00
|
|
|
ErrorOr<void> result;
|
2021-10-02 09:45:15 +03:00
|
|
|
Memory::MemoryManager::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.
|
2021-11-10 13:55:37 +03:00
|
|
|
if (auto append_result = vmobjects.try_append(static_cast<Memory::InodeVMObject&>(vmobject)); append_result.is_error() && vmobjects.is_empty()) {
|
|
|
|
result = append_result.release_error();
|
2021-04-29 12:03:39 +03:00
|
|
|
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())
|
2021-11-08 02:51:39 +03:00
|
|
|
return result.release_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;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|