2020-07-30 21:38:15 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
|
|
|
*
|
2021-04-22 08:24:48 +00:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-07-30 21:38:15 +00:00
|
|
|
*/
|
|
|
|
|
2021-08-06 08:45:34 +00:00
|
|
|
#include <Kernel/Memory/AnonymousVMObject.h>
|
|
|
|
#include <Kernel/Memory/InodeVMObject.h>
|
|
|
|
#include <Kernel/Memory/MemoryManager.h>
|
2023-02-24 17:45:37 +00:00
|
|
|
#include <Kernel/Tasks/Process.h>
|
2020-07-30 21:38:15 +00:00
|
|
|
|
|
|
|
namespace Kernel {
|
|
|
|
|
2021-11-07 23:51:39 +00:00
|
|
|
ErrorOr<FlatPtr> Process::sys$purge(int mode)
|
2020-07-30 21:38:15 +00:00
|
|
|
{
|
2022-08-17 20:03:04 +00:00
|
|
|
VERIFY_PROCESS_BIG_LOCK_ACQUIRED(this);
|
2021-12-29 09:11:45 +00:00
|
|
|
TRY(require_no_promises());
|
2022-08-20 22:21:01 +00:00
|
|
|
auto credentials = this->credentials();
|
|
|
|
if (!credentials->is_superuser())
|
2021-03-01 12:49:16 +00:00
|
|
|
return EPERM;
|
2021-07-24 23:46:44 +00:00
|
|
|
size_t purged_page_count = 0;
|
2020-07-30 21:38:15 +00:00
|
|
|
if (mode & PURGE_ALL_VOLATILE) {
|
2023-03-06 16:56:28 +00:00
|
|
|
Vector<NonnullLockRefPtr<Memory::AnonymousVMObject>> vmobjects;
|
2020-07-30 21:38:15 +00:00
|
|
|
{
|
2021-11-07 23:51:39 +00:00
|
|
|
ErrorOr<void> result;
|
2021-10-02 06:45:15 +00:00
|
|
|
Memory::MemoryManager::for_each_vmobject([&](auto& vmobject) {
|
2021-04-29 09:03:39 +00: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 10:55:37 +00: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 09:03:39 +00:00
|
|
|
return IterationDecision::Break;
|
|
|
|
}
|
|
|
|
}
|
2020-07-30 21:38:15 +00:00
|
|
|
return IterationDecision::Continue;
|
|
|
|
});
|
2021-04-29 09:03:39 +00:00
|
|
|
|
|
|
|
if (result.is_error())
|
2021-11-07 23:51:39 +00:00
|
|
|
return result.release_error();
|
2020-07-30 21:38:15 +00:00
|
|
|
}
|
|
|
|
for (auto& vmobject : vmobjects) {
|
2023-03-06 16:56:28 +00:00
|
|
|
purged_page_count += vmobject->purge();
|
2020-07-30 21:38:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (mode & PURGE_ALL_CLEAN_INODE) {
|
2023-03-06 16:56:28 +00:00
|
|
|
Vector<NonnullLockRefPtr<Memory::InodeVMObject>> vmobjects;
|
2020-07-30 21:38:15 +00:00
|
|
|
{
|
2021-11-07 23:51:39 +00:00
|
|
|
ErrorOr<void> result;
|
2021-10-02 06:45:15 +00:00
|
|
|
Memory::MemoryManager::for_each_vmobject([&](auto& vmobject) {
|
2021-04-29 09:03:39 +00: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 10:55:37 +00: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 09:03:39 +00:00
|
|
|
return IterationDecision::Break;
|
|
|
|
}
|
|
|
|
}
|
2020-07-30 21:38:15 +00:00
|
|
|
return IterationDecision::Continue;
|
|
|
|
});
|
2021-04-29 09:03:39 +00:00
|
|
|
|
|
|
|
if (result.is_error())
|
2021-11-07 23:51:39 +00:00
|
|
|
return result.release_error();
|
2020-07-30 21:38:15 +00:00
|
|
|
}
|
|
|
|
for (auto& vmobject : vmobjects) {
|
2023-03-06 16:56:28 +00:00
|
|
|
purged_page_count += vmobject->release_all_clean_pages();
|
2020-07-30 21:38:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return purged_page_count;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|