From c5d57d5216e9c8f00317b45725e7c76fc402e2e6 Mon Sep 17 00:00:00 2001 From: js2xxx Date: Wed, 18 Jun 2025 11:48:11 +0000 Subject: [PATCH] Fix mapping between `sched_attr` and `SchedPolicy` --- kernel/src/syscall/sched_getattr.rs | 23 ++++++++++++++++----- test/apps/sched/sched_attr_idle.c | 31 +++++++++++++++++++++++++++++ test/apps/scripts/process.sh | 1 + 3 files changed, 50 insertions(+), 5 deletions(-) create mode 100644 test/apps/sched/sched_attr_idle.c diff --git a/kernel/src/syscall/sched_getattr.rs b/kernel/src/syscall/sched_getattr.rs index ad53e041c..232c4ac5b 100644 --- a/kernel/src/syscall/sched_getattr.rs +++ b/kernel/src/syscall/sched_getattr.rs @@ -69,16 +69,26 @@ impl TryFrom for LinuxSchedAttr { ..Default::default() }, + // The SCHED_IDLE policy is mapped to the highest nice value of + // `SchedPolicy::Fair` instead of `SchedPolicy::Idle`. Tasks of the + // latter policy are invisible to the user API. + SchedPolicy::Fair(Nice::MAX) => LinuxSchedAttr { + sched_policy: SCHED_IDLE, + ..Default::default() + }, + SchedPolicy::Fair(nice) => LinuxSchedAttr { sched_policy: SCHED_NORMAL, sched_nice: nice.value().get().into(), ..Default::default() }, - SchedPolicy::Idle => LinuxSchedAttr { - sched_policy: SCHED_IDLE, - ..Default::default() - }, + SchedPolicy::Idle => { + return Err(Error::with_message( + Errno::EACCES, + "attr for idle tasks are not accessible", + )) + } }) } } @@ -112,7 +122,10 @@ impl TryFrom for SchedPolicy { .map_err(|msg| Error::with_message(Errno::EINVAL, msg))?, )), - SCHED_IDLE => SchedPolicy::Idle, + // The SCHED_IDLE policy is mapped to the highest nice value of + // `SchedPolicy::Fair` instead of `SchedPolicy::Idle`. Tasks of the + // latter policy are invisible to the user API. + SCHED_IDLE => SchedPolicy::Fair(Nice::MAX), _ => { return Err(Error::with_message( diff --git a/test/apps/sched/sched_attr_idle.c b/test/apps/sched/sched_attr_idle.c new file mode 100644 index 000000000..cd20d6da2 --- /dev/null +++ b/test/apps/sched/sched_attr_idle.c @@ -0,0 +1,31 @@ +// SPDX-License-Identifier: MPL-2.0 + +#define _GNU_SOURCE +#include +#include +#include +#include +#include +#include + +#define SCHED_IDLE 5 + +void *test(void *__arg) +{ + struct sched_param param = { .sched_priority = 0 }; + assert(sched_setscheduler(0, SCHED_IDLE, ¶m) == 0); + sleep(1); + return NULL; +} + +int main() +{ + pthread_t thread; + assert(pthread_create(&thread, NULL, test, NULL) == 0); + test(NULL); + + pthread_join(thread, NULL); + printf("Test completed\n"); + + return 0; +} \ No newline at end of file diff --git a/test/apps/scripts/process.sh b/test/apps/scripts/process.sh index 58534c41c..606cea4bf 100755 --- a/test/apps/scripts/process.sh +++ b/test/apps/scripts/process.sh @@ -39,6 +39,7 @@ pthread/pthread_test pty/open_pty pty/pty_blocking sched/sched_attr +sched/sched_attr_idle shm/posix_shm signal_c/parent_death_signal signal_c/signal_test