Kernel: Make scheduler control syscalls more generic

The syscalls are renamed as they no longer reflect the exact POSIX
functionality. They can now handle setting/getting scheduler parameters
for both threads and processes.
This commit is contained in:
kleines Filmröllchen 2022-07-24 16:00:51 +02:00 committed by Linus Groh
parent 7bb34279cd
commit b8567d7a9d
Notes: sideshowbarker 2024-07-17 05:04:24 +09:00
6 changed files with 95 additions and 42 deletions

View file

@ -8,6 +8,7 @@
#include <AK/Types.h> #include <AK/Types.h>
#include <AK/Userspace.h> #include <AK/Userspace.h>
#include <Kernel/API/POSIX/sched.h>
constexpr int syscall_vector = 0x82; constexpr int syscall_vector = 0x82;
@ -145,8 +146,8 @@ enum class NeedsBigProcessLock {
S(recvmsg, NeedsBigProcessLock::Yes) \ S(recvmsg, NeedsBigProcessLock::Yes) \
S(rename, NeedsBigProcessLock::No) \ S(rename, NeedsBigProcessLock::No) \
S(rmdir, NeedsBigProcessLock::No) \ S(rmdir, NeedsBigProcessLock::No) \
S(sched_getparam, NeedsBigProcessLock::No) \ S(scheduler_get_parameters, NeedsBigProcessLock::No) \
S(sched_setparam, NeedsBigProcessLock::No) \ S(scheduler_set_parameters, NeedsBigProcessLock::No) \
S(sendfd, NeedsBigProcessLock::No) \ S(sendfd, NeedsBigProcessLock::No) \
S(sendmsg, NeedsBigProcessLock::Yes) \ S(sendmsg, NeedsBigProcessLock::Yes) \
S(set_coredump_metadata, NeedsBigProcessLock::No) \ S(set_coredump_metadata, NeedsBigProcessLock::No) \
@ -480,6 +481,17 @@ struct SC_chmod_params {
int follow_symlinks; int follow_symlinks;
}; };
enum class SchedulerParametersMode : bool {
Process,
Thread,
};
struct SC_scheduler_parameters_params {
pid_t pid_or_tid;
SchedulerParametersMode mode;
struct sched_param parameters;
};
void initialize(); void initialize();
int sync(); int sync();

View file

@ -400,8 +400,8 @@ public:
ErrorOr<FlatPtr> sys$getsockname(Userspace<Syscall::SC_getsockname_params const*>); ErrorOr<FlatPtr> sys$getsockname(Userspace<Syscall::SC_getsockname_params const*>);
ErrorOr<FlatPtr> sys$getpeername(Userspace<Syscall::SC_getpeername_params const*>); ErrorOr<FlatPtr> sys$getpeername(Userspace<Syscall::SC_getpeername_params const*>);
ErrorOr<FlatPtr> sys$socketpair(Userspace<Syscall::SC_socketpair_params const*>); ErrorOr<FlatPtr> sys$socketpair(Userspace<Syscall::SC_socketpair_params const*>);
ErrorOr<FlatPtr> sys$sched_setparam(pid_t pid, Userspace<const struct sched_param*>); ErrorOr<FlatPtr> sys$scheduler_set_parameters(Userspace<Syscall::SC_scheduler_parameters_params const*>);
ErrorOr<FlatPtr> sys$sched_getparam(pid_t pid, Userspace<struct sched_param*>); ErrorOr<FlatPtr> sys$scheduler_get_parameters(Userspace<Syscall::SC_scheduler_parameters_params*>);
ErrorOr<FlatPtr> sys$create_thread(void* (*)(void*), Userspace<Syscall::SC_create_thread_params const*>); ErrorOr<FlatPtr> sys$create_thread(void* (*)(void*), Userspace<Syscall::SC_create_thread_params const*>);
[[noreturn]] void sys$exit_thread(Userspace<void*>, Userspace<void*>, size_t); [[noreturn]] void sys$exit_thread(Userspace<void*>, Userspace<void*>, size_t);
ErrorOr<FlatPtr> sys$join_thread(pid_t tid, Userspace<void**> exit_value); ErrorOr<FlatPtr> sys$join_thread(pid_t tid, Userspace<void**> exit_value);
@ -840,6 +840,8 @@ private:
SpinlockProtected<Thread::ListInProcess>& thread_list() { return m_thread_list; } SpinlockProtected<Thread::ListInProcess>& thread_list() { return m_thread_list; }
SpinlockProtected<Thread::ListInProcess> const& thread_list() const { return m_thread_list; } SpinlockProtected<Thread::ListInProcess> const& thread_list() const { return m_thread_list; }
ErrorOr<NonnullRefPtr<Thread>> get_thread_from_pid_or_tid(pid_t pid_or_tid, Syscall::SchedulerParametersMode mode);
SpinlockProtected<Thread::ListInProcess> m_thread_list { LockRank::None }; SpinlockProtected<Thread::ListInProcess> m_thread_list { LockRank::None };
MutexProtected<OpenFileDescriptions> m_fds; MutexProtected<OpenFileDescriptions> m_fds;

View file

@ -1,9 +1,11 @@
/* /*
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org> * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
* Copyright (c) 2022, kleines Filmröllchen <filmroellchen@serenityos.org>
* *
* SPDX-License-Identifier: BSD-2-Clause * SPDX-License-Identifier: BSD-2-Clause
*/ */
#include <Kernel/API/Syscall.h>
#include <Kernel/Process.h> #include <Kernel/Process.h>
#include <Kernel/Scheduler.h> #include <Kernel/Scheduler.h>
@ -17,48 +19,75 @@ ErrorOr<FlatPtr> Process::sys$yield()
return 0; return 0;
} }
ErrorOr<FlatPtr> Process::sys$sched_setparam(int pid, Userspace<const struct sched_param*> user_param) ErrorOr<NonnullRefPtr<Thread>> Process::get_thread_from_pid_or_tid(pid_t pid_or_tid, Syscall::SchedulerParametersMode mode)
{
VERIFY(g_scheduler_lock.is_locked_by_current_processor());
Thread* peer;
switch (mode) {
case Syscall::SchedulerParametersMode::Thread: {
peer = Thread::current();
if (pid_or_tid != 0)
peer = Thread::from_tid(pid_or_tid);
break;
}
case Syscall::SchedulerParametersMode::Process: {
auto* searched_process = this;
if (pid_or_tid != 0)
searched_process = Process::from_pid(pid_or_tid);
if (searched_process == nullptr)
return ESRCH;
auto pid = searched_process->pid().value();
// Main thread has tid == pid
this->thread_list().for_each([&](auto& thread) {
if (thread.tid().value() == pid)
peer = &thread;
});
break;
}
default:
VERIFY_NOT_REACHED();
}
if (!peer)
return ESRCH;
return NonnullRefPtr<Thread> { *peer };
}
ErrorOr<FlatPtr> Process::sys$scheduler_set_parameters(Userspace<Syscall::SC_scheduler_parameters_params const*> user_param)
{ {
VERIFY_NO_PROCESS_BIG_LOCK(this); VERIFY_NO_PROCESS_BIG_LOCK(this);
TRY(require_promise(Pledge::proc)); TRY(require_promise(Pledge::proc));
auto param = TRY(copy_typed_from_user(user_param)); auto parameters = TRY(copy_typed_from_user(user_param));
if (param.sched_priority < THREAD_PRIORITY_MIN || param.sched_priority > THREAD_PRIORITY_MAX) if (parameters.parameters.sched_priority < THREAD_PRIORITY_MIN || parameters.parameters.sched_priority > THREAD_PRIORITY_MAX)
return EINVAL; return EINVAL;
auto* peer = Thread::current();
SpinlockLocker lock(g_scheduler_lock); SpinlockLocker lock(g_scheduler_lock);
if (pid != 0) auto peer = TRY(get_thread_from_pid_or_tid(parameters.pid_or_tid, parameters.mode));
peer = Thread::from_tid(pid);
if (!peer)
return ESRCH;
auto credentials = this->credentials(); auto credentials = this->credentials();
auto peer_credentials = peer->process().credentials(); auto peer_credentials = peer->process().credentials();
if (!credentials->is_superuser() && credentials->euid() != peer_credentials->uid() && credentials->uid() != peer_credentials->uid()) if (!credentials->is_superuser() && credentials->euid() != peer_credentials->uid() && credentials->uid() != peer_credentials->uid())
return EPERM; return EPERM;
peer->set_priority((u32)param.sched_priority); // FIXME: Only sets priority for main thread of process if mode == PROCESS
peer->set_priority((u32)parameters.parameters.sched_priority);
return 0; return 0;
} }
ErrorOr<FlatPtr> Process::sys$sched_getparam(pid_t pid, Userspace<struct sched_param*> user_param) ErrorOr<FlatPtr> Process::sys$scheduler_get_parameters(Userspace<Syscall::SC_scheduler_parameters_params*> user_param)
{ {
VERIFY_NO_PROCESS_BIG_LOCK(this); VERIFY_NO_PROCESS_BIG_LOCK(this);
TRY(require_promise(Pledge::proc)); TRY(require_promise(Pledge::proc));
Syscall::SC_scheduler_parameters_params parameters;
TRY(copy_from_user(&parameters, user_param));
int priority; int priority;
{ {
auto* peer = Thread::current();
SpinlockLocker lock(g_scheduler_lock); SpinlockLocker lock(g_scheduler_lock);
if (pid != 0) { auto peer = TRY(get_thread_from_pid_or_tid(parameters.pid_or_tid, parameters.mode));
// FIXME: PID/TID BUG
// The entire process is supposed to be affected.
peer = Thread::from_tid(pid);
}
if (!peer)
return ESRCH;
auto credentials = this->credentials(); auto credentials = this->credentials();
auto peer_credentials = peer->process().credentials(); auto peer_credentials = peer->process().credentials();
@ -68,11 +97,9 @@ ErrorOr<FlatPtr> Process::sys$sched_getparam(pid_t pid, Userspace<struct sched_p
priority = (int)peer->priority(); priority = (int)peer->priority();
} }
struct sched_param param { parameters.parameters.sched_priority = priority;
priority
};
TRY(copy_to_user(user_param, &param)); TRY(copy_to_user(user_param, &parameters));
return 0; return 0;
} }

View file

@ -222,8 +222,8 @@ private:
int virt$recvmsg(int sockfd, FlatPtr msg_addr, int flags); int virt$recvmsg(int sockfd, FlatPtr msg_addr, int flags);
int virt$rename(FlatPtr address); int virt$rename(FlatPtr address);
int virt$rmdir(FlatPtr path, size_t path_length); int virt$rmdir(FlatPtr path, size_t path_length);
int virt$sched_getparam(pid_t, FlatPtr); int virt$scheduler_get_parameters(FlatPtr);
int virt$sched_setparam(int, FlatPtr); int virt$scheduler_set_parameters(FlatPtr);
int virt$sendfd(int, int); int virt$sendfd(int, int);
int virt$sendmsg(int sockfd, FlatPtr msg_addr, int flags); int virt$sendmsg(int sockfd, FlatPtr msg_addr, int flags);
int virt$set_coredump_metadata(FlatPtr address); int virt$set_coredump_metadata(FlatPtr address);

View file

@ -201,10 +201,10 @@ u32 Emulator::virt_syscall(u32 function, u32 arg1, u32 arg2, u32 arg3)
return virt$rename(arg1); return virt$rename(arg1);
case SC_rmdir: case SC_rmdir:
return virt$rmdir(arg1, arg2); return virt$rmdir(arg1, arg2);
case SC_sched_getparam: case SC_scheduler_get_parameters:
return virt$sched_getparam(arg1, arg2); return virt$scheduler_get_parameters(arg1);
case SC_sched_setparam: case SC_scheduler_set_parameters:
return virt$sched_setparam(arg1, arg2); return virt$scheduler_set_parameters(arg1);
case SC_sendfd: case SC_sendfd:
return virt$sendfd(arg1, arg2); return virt$sendfd(arg1, arg2);
case SC_sendmsg: case SC_sendmsg:
@ -1504,20 +1504,20 @@ int Emulator::virt$dup2(int old_fd, int new_fd)
return syscall(SC_dup2, old_fd, new_fd); return syscall(SC_dup2, old_fd, new_fd);
} }
int Emulator::virt$sched_getparam(pid_t pid, FlatPtr user_addr) int Emulator::virt$scheduler_get_parameters(FlatPtr user_addr)
{ {
sched_param user_param; Syscall::SC_scheduler_parameters_params user_param;
mmu().copy_from_vm(&user_param, user_addr, sizeof(user_param)); mmu().copy_from_vm(&user_param, user_addr, sizeof(user_param));
auto rc = syscall(SC_sched_getparam, pid, &user_param); auto rc = syscall(SC_scheduler_get_parameters, &user_param);
mmu().copy_to_vm(user_addr, &user_param, sizeof(user_param)); mmu().copy_to_vm(user_addr, &user_param, sizeof(user_param));
return rc; return rc;
} }
int Emulator::virt$sched_setparam(int pid, FlatPtr user_addr) int Emulator::virt$scheduler_set_parameters(FlatPtr user_addr)
{ {
sched_param user_param; Syscall::SC_scheduler_parameters_params user_param;
mmu().copy_from_vm(&user_param, user_addr, sizeof(user_param)); mmu().copy_from_vm(&user_param, user_addr, sizeof(user_param));
return syscall(SC_sched_setparam, pid, &user_param); return syscall(SC_scheduler_set_parameters, &user_param);
} }
int Emulator::virt$set_thread_name(pid_t pid, FlatPtr name_addr, size_t name_length) int Emulator::virt$set_thread_name(pid_t pid, FlatPtr name_addr, size_t name_length)

View file

@ -32,14 +32,26 @@ int sched_get_priority_max([[maybe_unused]] int policy)
// https://pubs.opengroup.org/onlinepubs/9699919799/functions/sched_setparam.html // https://pubs.opengroup.org/onlinepubs/9699919799/functions/sched_setparam.html
int sched_setparam(pid_t pid, const struct sched_param* param) int sched_setparam(pid_t pid, const struct sched_param* param)
{ {
int rc = syscall(SC_sched_setparam, pid, param); Syscall::SC_scheduler_parameters_params parameters {
.pid_or_tid = pid,
.mode = Syscall::SchedulerParametersMode::Process,
.parameters = *param,
};
int rc = syscall(SC_scheduler_set_parameters, &parameters);
__RETURN_WITH_ERRNO(rc, rc, -1); __RETURN_WITH_ERRNO(rc, rc, -1);
} }
// https://pubs.opengroup.org/onlinepubs/9699919799/functions/sched_getparam.html // https://pubs.opengroup.org/onlinepubs/9699919799/functions/sched_getparam.html
int sched_getparam(pid_t pid, struct sched_param* param) int sched_getparam(pid_t pid, struct sched_param* param)
{ {
int rc = syscall(SC_sched_getparam, pid, param); Syscall::SC_scheduler_parameters_params parameters {
.pid_or_tid = pid,
.mode = Syscall::SchedulerParametersMode::Process,
.parameters = {},
};
int rc = syscall(SC_scheduler_get_parameters, &parameters);
if (rc == 0)
*param = parameters.parameters;
__RETURN_WITH_ERRNO(rc, rc, -1); __RETURN_WITH_ERRNO(rc, rc, -1);
} }
} }