Rework pthread_setschedprio support
v2 cover Rework pthread_setschedprio support
It turned out just in time that the __CURRENT_SCHED-approach is broken and not reasonably fixable. This introduces a separate syscall for implementing that posix function.
Changes in v2:
- account for !CONFIG_XENO_LAZY_SETSCHED in setsched test
- split smokey changes into two patches
- enable lazy-setsched in debian package
Jan
Jan Kiszka (7):
cobalt/posix/thread: Factor out thread_lookup_or_shadow cobalt/posix: Add thread_setschedprio
lib/cobalt: Use new thread_setschedprio syscall Revert "cobalt/kernel: Introduce __SCHED_CURRENT policy to
setschedparam_ex"
testsuite/smokey: Fix setsched /wrt lazy scheduling parameter
application
testsuite/smokey: Add pthread_setschedprio to setsched test case
debian: Enable lazy-setsched in userspace package
debian/rules | 1 + include/cobalt/uapi/sched.h | 3 - include/cobalt/uapi/syscall.h | 1 + .../arch/x86/include/asm/xenomai/syscall32-table.h | 1 + kernel/cobalt/posix/syscall32.c | 9 +++ kernel/cobalt/posix/syscall32.h | 6 ++ kernel/cobalt/posix/thread.c | 85 ++++++++++++++++------ kernel/cobalt/posix/thread.h | 11 +++ kernel/cobalt/trace/cobalt-posix.h | 18 ++++- lib/cobalt/thread.c | 33 ++++++++- testsuite/smokey/setsched/setsched.c | 31 +++++++- 11 files changed, 167 insertions(+), 32 deletions(-)
Signature · 1 line
2.16.4
PATCH v2 1/7 cobalt/posix/thread: Factor out thread_lookup_or_shadow
From: Jan Kiszka <[email protected]>
This pattern will be reused by cobalt_thread_setschedprio.
--- kernel/cobalt/posix/thread.c | 38 ++++++++++++++++++++++++++------------ 1 file changed, 26 insertions(+), 12 deletions(-)
kernel/cobalt/posix/thread.c +26 -12
@@ -463,32 +463,46 @@ static inline int pthread_setmode_np(int clrmask, int setmask, int *mode_r)return 0 ; } -int cobalt_thread_setschedparam_ex (unsigned long pth, -int policy, -const struct sched_param_ex *param_ex, -__u32 __user *u_winoff, -int __user *u_promoted) +static struct cobalt_thread *thread_lookup_or_shadow (unsigned long pth, +__u32 __user *u_winoff, +int *promoted_r) {struct cobalt_local_hkey hkey;struct cobalt_thread *thread; -int ret, promoted =0 ; + + *promoted_r =0 ; hkey.u_pth = pth; hkey.mm = current->mm; -trace_cobalt_pthread_setschedparam (pth, policy, param_ex); thread =thread_lookup (&hkey);if (thread ==NULL ) {if (u_winoff ==NULL ) -return -ESRCH ; +return ERR_PTR (-ESRCH ); thread =cobalt_thread_shadow (&hkey, u_winoff); -if (IS_ERR (thread)) -return PTR_ERR (thread); - - promoted =1 ; +if (!IS_ERR (thread)) + *promoted_r =1 ; } +return thread; +} + +int cobalt_thread_setschedparam_ex (unsigned long pth, +int policy, +const struct sched_param_ex *param_ex, +__u32 __user *u_winoff, +int __user *u_promoted) +{ +struct cobalt_thread *thread; +int ret, promoted; + +trace_cobalt_pthread_setschedparam (pth, policy, param_ex); + + thread =thread_lookup_or_shadow (pth, u_winoff, &promoted); +if (IS_ERR (thread)) +return PTR_ERR (thread); + ret =__cobalt_thread_setschedparam_ex (thread, policy, param_ex);if (ret)return ret;
Signature · 1 line
2.16.4
PATCH v2 2/7 cobalt/posix: Add thread_setschedprio
In reply to Jan KiszkaFrom: Jan Kiszka <[email protected]>
The approach of using thread_setschedparam_ex with __SCHED_CURRENT failed for policies with complex parameter sets. Therefore, introduce a syscall that is fully aware of how to apply only the priority change to the target thread's scheduling settings.
We need a compat path as well because we are passing down pointers.
--- include/cobalt/uapi/syscall.h | 1 + .../arch/x86/include/asm/xenomai/syscall32-table.h | 1 + kernel/cobalt/posix/syscall32.c | 9 ++++++ kernel/cobalt/posix/syscall32.h | 6 ++++ kernel/cobalt/posix/thread.c | 37 ++++++++++++++++++++++ kernel/cobalt/posix/thread.h | 11 +++++++ kernel/cobalt/trace/cobalt-posix.h | 15 +++++++++ 7 files changed, 80 insertions(+)
include/cobalt/uapi/syscall.h +1 -0
@@ -121,6 +121,7 @@#define sc_cobalt_recvmmsg 98#define sc_cobalt_sendmmsg 99#define sc_cobalt_clock_adjtime 100 +#define sc_cobalt_thread_setschedprio 101#define __NR_COBALT_SYSCALLS 128/* Power of 2 */
kernel/cobalt/arch/x86/include/asm/xenomai/syscall32-table.h +1 -0
@@ -27,6 +27,7 @@ __COBALT_CALL32emu_THUNK(thread_create) __COBALT_CALL32emu_THUNK(thread_setschedparam_ex) __COBALT_CALL32emu_THUNK(thread_getschedparam_ex) +__COBALT_CALL32emu_THUNK(thread_setschedprio) __COBALT_CALL32emu_THUNK(sem_open) __COBALT_CALL32x_THUNK(sem_open) __COBALT_CALL32emu_THUNK(sem_timedwait)
kernel/cobalt/posix/syscall32.c +9 -0
@@ -88,6 +88,15 @@ COBALT_SYSCALL32emu(thread_getschedparam_ex, current,return ret ?:sys32_put_param_ex (policy, u_param, ¶m_ex); } +COBALT_SYSCALL32emu (thread_setschedprio, conforming, + (compat_ulong_t pth, + int prio, + __u32__user *u_winoff, + int __user *u_promoted)) +{ +return cobalt_thread_setschedprio (pth, prio, u_winoff, u_promoted); +} +static inline int sys32_fetch_timeout (struct timespec *ts,const void __user *u_ts) {
kernel/cobalt/posix/syscall32.h +6 -0
@@ -45,6 +45,12 @@ COBALT_SYSCALL32emu_DECL(thread_getschedparam_ex,int __user *u_policy, struct compat_sched_param_ex __user *u_param)); +COBALT_SYSCALL32emu_DECL (thread_setschedprio, + (compat_ulong_t pth, + int prio, + __u32__user *u_winoff, + int__user *u_promoted)); +COBALT_SYSCALL32emu_DECL (clock_getres, (clockid_t clock_id, struct compat_timespec __user *u_ts));
kernel/cobalt/posix/thread.c +37 -0
@@ -568,6 +568,43 @@ COBALT_SYSCALL(thread_getschedparam_ex, current,return cobalt_copy_to_user (u_param, ¶m_ex,sizeof (param_ex)); } +int cobalt_thread_setschedprio (unsigned long pth, +int prio, +__u32 __user *u_winoff, +int __user *u_promoted) +{ +struct sched_param_ex param_ex; +struct cobalt_thread *thread; +int ret, policy, promoted; + +trace_cobalt_pthread_setschedprio (pth, prio); + + thread =thread_lookup_or_shadow (pth, u_winoff, &promoted); +if (IS_ERR (thread)) +return PTR_ERR (thread); + + ret =__cobalt_thread_getschedparam_ex (thread, &policy, ¶m_ex); +if (ret) +return ret; + + param_ex.sched_priority = prio; + + ret =__cobalt_thread_setschedparam_ex (thread, policy, ¶m_ex); +if (ret) +return ret; + +return cobalt_copy_to_user (u_promoted, &promoted,sizeof (promoted)); +} + +COBALT_SYSCALL (thread_setschedprio, conforming, + (unsigned long pth, + int prio, + __u32__user *u_winoff, + int __user *u_promoted)) +{ +return cobalt_thread_setschedprio (pth, prio, u_winoff, u_promoted); +} +int __cobalt_thread_create (unsigned long pth,int policy,struct sched_param_ex *param_ex,int xid,__u32 __user *u_winoff)
kernel/cobalt/posix/thread.h +11 -0
@@ -139,6 +139,11 @@ int __cobalt_thread_getschedparam_ex(struct cobalt_thread *thread,int *policy_r, struct sched_param_ex *param_ex); +int cobalt_thread_setschedprio (unsigned long pth, +int prio, +__u32 __user *u_winoff, +int __user *u_promoted); +struct cobalt_thread *cobalt_thread_find (pid_t pid);struct cobalt_thread *cobalt_thread_find_local (pid_t pid);
@@ -181,6 +186,12 @@ COBALT_SYSCALL_DECL(thread_getschedparam_ex,int __user *u_policy, struct sched_param_ex __user *u_param)); +COBALT_SYSCALL_DECL (thread_setschedprio, + (unsigned long pth, + int prio, + __u32__user *u_winoff, + int__user *u_promoted)); +void cobalt_thread_map (struct xnthread *curr);struct xnthread_personality *cobalt_thread_exit (struct xnthread *curr);
kernel/cobalt/trace/cobalt-posix.h +15 -0
@@ -70,6 +70,7 @@__cobalt_symbolic_syscall (thread_kill), \__cobalt_symbolic_syscall (thread_setschedparam_ex), \__cobalt_symbolic_syscall (thread_getschedparam_ex), \ +__cobalt_symbolic_syscall (thread_setschedprio), \__cobalt_symbolic_syscall (thread_getstat), \__cobalt_symbolic_syscall (sem_init), \ __cobalt_symbolic_syscall(sem_destroy), \
@@ -311,6 +312,20 @@ DEFINE_EVENT(cobalt_posix_schedparam, cobalt_pthread_getschedparam,TP_ARGS (pth, policy, param_ex) ); +TRACE_EVENT (cobalt_pthread_setschedprio, + TP_PROTO(unsigned long pth, int prio), +TP_ARGS (pth, prio), +TP_STRUCT__entry ( + __field(unsigned long, pth) +__field (int, prio) + ), +TP_fast_assign ( + __entry->pth = pth; + __entry->prio = prio; + ), +TP_printk ("pth=%p prio=%d" , (void *)__entry->pth, __entry->prio) +); +#define cobalt_print_thread_mode (__mode) \ __print_flags(__mode, "|", \ {PTHREAD_WARNSW, "warnsw"}, \
Signature · 1 line
2.16.4
PATCH v2 3/7 lib/cobalt: Use new thread_setschedprio syscall
In reply to Jan KiszkaFrom: Jan Kiszka <[email protected]>
Switch the implementation of pthread_setschedprio to the new cobalt syscall in order to avoid scheduler setting corruptions for SCHED_QUOTA and similar complex policies.
--- lib/cobalt/thread.c | 33 +++++++++++++++++++++++++++++---- 1 file changed, 29 insertions(+), 4 deletions(-)
lib/cobalt/thread.c +29 -4
@@ -666,11 +666,36 @@ int pthread_setschedparam_ex(pthread_t thread, COBALT_IMPL(int, pthread_setschedprio, (pthread_t thread, int prio)) { -struct sched_param_ex param_ex = { - .sched_priority = prio, - }; +__u32 u_winoff, *u_winoff_ptr; +int ret, promoted; -return pthread_setschedparam_ex (thread, __SCHED_CURRENT, ¶m_ex); +/* See pthread_setschedparam_ex. */ +if (cobalt_eager_setsched ()) { + ret =__STD (pthread_setschedprio (thread, prio)); +if (ret) +return ret; + } + +/* only request promotion when this targets the current thread */ + u_winoff_ptr = thread ==pthread_self () ? &u_winoff :NULL ; + + ret = -XENOMAI_SYSCALL4 (sc_cobalt_thread_setschedprio, + thread, prio, u_winoff_ptr, &promoted); + +/* +* If the kernel has no reference to the target thread. let glibc +* handle the call. +*/ +if (ret ==ESRCH ) +return __STD (pthread_setschedprio (thread, prio)); + +if (ret ==0 && promoted) { +cobalt_sigshadow_install_once (); +cobalt_set_tsd (u_winoff); +cobalt_thread_harden (); + } + +return ret; }/**
Signature · 1 line
2.16.4
PATCH v2 4/7 Revert "cobalt/kernel: Introduce __SCHED_CURRENT policy to setschedparam_ex"
In reply to Jan KiszkaFrom: Jan Kiszka <[email protected]>
This reverts commit b3c70f1a470397c915a6c206d07c7c342c164df4.
Was broken by design and is now no longer needed.
--- include/cobalt/uapi/sched.h | 3 --- kernel/cobalt/posix/thread.c | 10 ---------- kernel/cobalt/trace/cobalt-posix.h | 3 +-- 3 files changed, 1 insertion(+), 15 deletions(-)
include/cobalt/uapi/sched.h +0 -3
@@ -21,9 +21,6 @@#define SCHED_COBALT 42#define SCHED_WEAK 43 -/* for internal use */ -#define __SCHED_CURRENT 44 -#ifndef SCHED_SPORADIC #define SCHED_SPORADIC 10#define sched_ss_low_priority sched_u.ss.__sched_low_priority
kernel/cobalt/posix/thread.c +0 -10
@@ -242,7 +242,6 @@ struct xnthread_personality *cobalt_thread_finalize(struct xnthread *zombie)int __cobalt_thread_setschedparam_ex (struct cobalt_thread *thread,int policy,const struct sched_param_ex *param_ex) { -struct xnthread *base_thread = &thread->threadbase;struct xnsched_class *sched_class;union xnsched_policy_param param;xnticks_t tslice;
@@ -257,15 +256,6 @@ int __cobalt_thread_setschedparam_ex(struct cobalt_thread *thread, int policy, goto out; } -if (policy == __SCHED_CURRENT) { - policy = base_thread->base_class->policy; -if (xnthread_base_priority (base_thread) ==0 ) - policy =SCHED_NORMAL ; -else if (base_thread->base_class == &xnsched_class_rt && -xnthread_test_state (base_thread,XNRRB )) - policy =SCHED_RR ; - } - tslice = thread->threadbase.rrperiod; sched_class =cobalt_sched_policy_param (¶m, policy, param_ex, &tslice);
kernel/cobalt/trace/cobalt-posix.h +1 -2
@@ -202,8 +202,7 @@ DECLARE_EVENT_CLASS(syscall_exit, {SCHED_QUOTA, "quota"}, \ {SCHED_SPORADIC, "sporadic"}, \ {SCHED_COBALT, "cobalt"}, \ - {SCHED_WEAK, "weak"}, \ - {__SCHED_CURRENT, "<current>"}) + {SCHED_WEAK, "weak"})const char *cobalt_trace_parse_sched_params (struct trace_seq *,int ,struct sched_param_ex *);
Signature · 1 line
2.16.4
PATCH v2 5/7 testsuite/smokey: Fix setsched /wrt lazy scheduling parameter application
In reply to Jan KiszkaFrom: Jan Kiszka <[email protected]>
As we didn't migrate to RT, we didn't test properly if lazy parameter application actually works. Fix that, also taking into account the related config switch.
--- testsuite/smokey/setsched/setsched.c | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-)
testsuite/smokey/setsched/setsched.c +9 -1
@@ -68,18 +68,24 @@ static void __check_rt_schedparams(int expected_policy, int expected_prio,static void *thread_body (void *arg) { -struct cobalt_threadstat stats;struct sched_param param; +#ifdef CONFIG_XENO_LAZY_SETSCHED +struct cobalt_threadstat stats;unsigned long long msw; +#endif thread_pid =syscall (SYS_gettid);check_rt_schedparams (SCHED_FIFO ,1 );check_linux_schedparams (SCHED_FIFO ,1 ); +cobalt_thread_harden (); + +#ifdef CONFIG_XENO_LAZY_SETSCHED if (smokey_check_status (cobalt_thread_stat (thread_pid, &stats)))pthread_exit ((void *)(long )-EINVAL ); msw = stats.msw; +#endif param.sched_priority =2 ; if (smokey_check_status(pthread_setschedparam(pthread_self(),
@@ -88,9 +94,11 @@ static void *thread_body(void *arg)check_rt_schedparams (SCHED_FIFO ,2 ); +#ifdef CONFIG_XENO_LAZY_SETSCHED if (smokey_check_status (cobalt_thread_stat (thread_pid, &stats)) || !smokey_assert (stats.msw == msw))pthread_exit ((void *)(long )-EINVAL ); +#endif check_linux_schedparams (SCHED_FIFO ,2 );
Signature · 1 line
2.16.4
PATCH v2 6/7 testsuite/smokey: Add pthread_setschedprio to setsched test case
In reply to Jan KiszkaFrom: Jan Kiszka <[email protected]>
Now that this path goes via an own syscall, better test it.
--- testsuite/smokey/setsched/setsched.c | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+)
testsuite/smokey/setsched/setsched.c +21 -0
@@ -102,6 +102,27 @@ static void *thread_body(void *arg)check_linux_schedparams (SCHED_FIFO ,2 ); +cobalt_thread_harden (); + +#ifdef CONFIG_XENO_LAZY_SETSCHED +if (smokey_check_status (cobalt_thread_stat (thread_pid, &stats))) +pthread_exit ((void *)(long )-EINVAL ); + msw = stats.msw; +#endif + +if (smokey_check_status (pthread_setschedprio (pthread_self (),3 ))) +pthread_exit ((void *)(long )-EINVAL ); + +check_rt_schedparams (SCHED_FIFO ,3 ); + +#ifdef CONFIG_XENO_LAZY_SETSCHED +if (smokey_check_status (cobalt_thread_stat (thread_pid, &stats)) || + !smokey_assert (stats.msw == msw)) +pthread_exit ((void *)(long )-EINVAL ); +#endif + +check_linux_schedparams (SCHED_FIFO ,3 ); +return (void *)0L ; }
Signature · 1 line
2.16.4
PATCH v2 7/7 debian: Enable lazy-setsched in userspace package
In reply to Jan KiszkaFrom: Jan Kiszka <[email protected]>
This feature is useful for applications tuning scheduling parameters from within RT contexts. Primarily a topic for legacy applications. But as this doesn't come with noteworthy downsides, make it available in the generic package configuration.
--- debian/rules | 1 + 1 file changed, 1 insertion(+)
debian/rules +1 -0
@@ -18,6 +18,7 @@ CONFIG_OPTS = --prefix=/usr \ --mandir=/usr/share/man \ --with-testdir=/usr/lib/xenomai/testsuite \ --enable-smp \ + --enable-lazy-setsched \ --enable-debug=symbols ifneq ($(DEB_HOST_GNU_TYPE),$(DEB_BUILD_GNU_TYPE))
Signature · 1 line
2.16.4