serenity/Kernel/Syscalls/profiling.cpp
Liav A. dd59fe35c7 Kernel+Userland: Reduce jails to be a simple boolean flag
The whole concept of Jails was far more complicated than I actually want
it to be, so let's reduce the complexity of how it works from now on.
Please note that we always leaked the attach count of a Jail object in
the fork syscall if it failed midway.
Instead, we should have attach to the jail just before registering the
new Process, so we don't need to worry about unsuccessful Process
creation.

The reduction of complexity in regard to jails means that instead of
relying on jails to provide PID isolation, we could simplify the whole
idea of them to be a simple SetOnce, and let the ProcessList (now called
ScopedProcessList) to be responsible for this type of isolation.

Therefore, we apply the following changes to do so:
- We make the Jail concept no longer a class of its own. Instead, we
  simplify the idea of being jailed to a simple ProtectedValues boolean
  flag. This means that we no longer check of matching jail pointers
  anywhere in the Kernel code.
  To set a process as jailed, a new prctl option was added to set a
  Kernel SetOnce boolean flag (so it cannot change ever again).
- We provide Process & Thread methods to iterate over process lists.
  A process can either iterate on the global process list, or if it's
  attached to a scoped process list, then only over that list.
  This essentially replaces the need of checking the Jail pointer of a
  process when iterating over process lists.
2024-07-21 11:44:23 +02:00

153 lines
4.9 KiB
C++

/*
* Copyright (c) 2018-2021, Andreas Kling <kling@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <Kernel/Tasks/Coredump.h>
#include <Kernel/Tasks/PerformanceManager.h>
#include <Kernel/Tasks/Process.h>
#include <Kernel/Tasks/Scheduler.h>
#include <Kernel/Time/TimeManagement.h>
namespace Kernel {
bool g_profiling_all_threads;
PerformanceEventBuffer* g_global_perf_events;
u64 g_profiling_event_mask;
ErrorOr<FlatPtr> Process::sys$profiling_enable(pid_t pid, u64 event_mask)
{
VERIFY_PROCESS_BIG_LOCK_ACQUIRED(this);
TRY(require_no_promises());
return profiling_enable(pid, event_mask);
}
// NOTE: This second entrypoint exists to allow the kernel to invoke the syscall to enable boot profiling.
ErrorOr<FlatPtr> Process::profiling_enable(pid_t pid, u64 event_mask)
{
VERIFY_PROCESS_BIG_LOCK_ACQUIRED(this);
if (pid == -1) {
auto credentials = this->credentials();
if (!credentials->is_superuser())
return EPERM;
ScopedCritical critical;
g_profiling_event_mask = PERF_EVENT_PROCESS_CREATE | PERF_EVENT_THREAD_CREATE | PERF_EVENT_MMAP;
if (g_global_perf_events) {
g_global_perf_events->clear();
} else {
g_global_perf_events = PerformanceEventBuffer::try_create_with_size(32 * MiB).leak_ptr();
if (!g_global_perf_events) {
g_profiling_event_mask = 0;
return ENOMEM;
}
}
SpinlockLocker lock(g_profiling_lock);
if (!TimeManagement::the().enable_profile_timer())
return ENOTSUP;
g_profiling_all_threads = true;
PerformanceManager::add_process_created_event(*Scheduler::colonel());
TRY(Process::for_each_in_same_process_list([](auto& process) -> ErrorOr<void> {
PerformanceManager::add_process_created_event(process);
return {};
}));
g_profiling_event_mask = event_mask;
return 0;
}
auto process = Process::from_pid_in_same_process_list(pid);
if (!process)
return ESRCH;
if (process->is_dead())
return ESRCH;
auto credentials = this->credentials();
auto profile_process_credentials = process->credentials();
if (!credentials->is_superuser() && profile_process_credentials->uid() != credentials->euid())
return EPERM;
SpinlockLocker lock(g_profiling_lock);
g_profiling_event_mask = PERF_EVENT_PROCESS_CREATE | PERF_EVENT_THREAD_CREATE | PERF_EVENT_MMAP;
process->set_profiling(true);
if (!process->create_perf_events_buffer_if_needed()) {
process->set_profiling(false);
return ENOMEM;
}
g_profiling_event_mask = event_mask;
if (!TimeManagement::the().enable_profile_timer()) {
process->set_profiling(false);
return ENOTSUP;
}
return 0;
}
ErrorOr<FlatPtr> Process::sys$profiling_disable(pid_t pid)
{
VERIFY_PROCESS_BIG_LOCK_ACQUIRED(this);
TRY(require_no_promises());
if (pid == -1) {
auto credentials = this->credentials();
if (!credentials->is_superuser())
return EPERM;
ScopedCritical critical;
if (!TimeManagement::the().disable_profile_timer())
return ENOTSUP;
g_profiling_all_threads = false;
return 0;
}
auto process = Process::from_pid_in_same_process_list(pid);
if (!process)
return ESRCH;
auto credentials = this->credentials();
auto profile_process_credentials = process->credentials();
if (!credentials->is_superuser() && profile_process_credentials->uid() != credentials->euid())
return EPERM;
SpinlockLocker lock(g_profiling_lock);
if (!process->is_profiling())
return EINVAL;
// FIXME: If we enabled the profile timer and it's not supported, how do we disable it now?
if (!TimeManagement::the().disable_profile_timer())
return ENOTSUP;
process->set_profiling(false);
return 0;
}
ErrorOr<FlatPtr> Process::sys$profiling_free_buffer(pid_t pid)
{
VERIFY_PROCESS_BIG_LOCK_ACQUIRED(this);
TRY(require_no_promises());
if (pid == -1) {
auto credentials = this->credentials();
if (!credentials->is_superuser())
return EPERM;
OwnPtr<PerformanceEventBuffer> perf_events;
{
ScopedCritical critical;
perf_events = adopt_own_if_nonnull(g_global_perf_events);
g_global_perf_events = nullptr;
}
return 0;
}
auto process = Process::from_pid_in_same_process_list(pid);
if (!process)
return ESRCH;
auto credentials = this->credentials();
auto profile_process_credentials = process->credentials();
if (!credentials->is_superuser() && profile_process_credentials->uid() != credentials->euid())
return EPERM;
SpinlockLocker lock(g_profiling_lock);
if (process->is_profiling())
return EINVAL;
process->delete_perf_events_buffer();
return 0;
}
}