Fix issues around setsched on remote threads
cover Fix issues around setsched on remote threads
This addresses http://xenomai.org/pipermail/xenomai/2018-July/039267.html.
The last patch of the series may not be a stable candidate as it changes some return code of the kernel. The rest should go, though.
Jan
Jan Kiszka (6):
cobalt/posix: Harden cobalt_thread_shadow against invalid requests cobalt/posix: Avoid mapping remote threads in
cobalt_sched_setscheduler_ex
cobalt/posix: Remove task parameter from cobalt_thread_shadow lib/cobalt: Avoid requesting shadowing for remote threads lib/cobalt: Pass setsched requests for unknown threads to glibc
cobalt: Fix error code of cobalt_thread_setschedparam_ex
kernel/cobalt/posix/sched.c | 4 ++-- kernel/cobalt/posix/thread.c | 14 ++++++++------ kernel/cobalt/posix/thread.h | 3 +-- lib/cobalt/sched.c | 10 ++++++++++ lib/cobalt/thread.c | 18 ++++++++++++++++-- 5 files changed, 37 insertions(+), 12 deletions(-)
Signature · 1 line
2.16.4
PATCH 1/6 cobalt/posix: Harden cobalt_thread_shadow against invalid requests
From: Jan Kiszka <[email protected]>
If the caller has already a Xenomai shadow, we will crash rather than failing gracefully. The reason is that pthread_discard expects secondary mode - which may not be the case if the caller is already able to migrate. So check early and avoid that we can run into this trap.
--- kernel/cobalt/posix/thread.c | 3 +++ 1 file changed, 3 insertions(+)
kernel/cobalt/posix/thread.c +3 -0
@@ -625,6 +625,9 @@ cobalt_thread_shadow(struct task_struct *p,struct sched_param_ex param_ex;int ret; +if (xnthread_current ()) +return ERR_PTR (-EBUSY ); + param_ex.sched_priority =0 ;trace_cobalt_pthread_create (hkey->u_pth,SCHED_NORMAL , ¶m_ex); ret =pthread_create (&thread,SCHED_NORMAL , ¶m_ex, p);
Signature · 1 line
2.16.4
PATCH 2/6 cobalt/posix: Avoid mapping remote threads in cobalt_sched_setscheduler_ex
In reply to Jan KiszkaFrom: Jan Kiszka <[email protected]>
Detect if the caller passed the PID of a remote thread that we do not know yet. In that case, do not try to shadow anything, that will only end up in a mess.
--- kernel/cobalt/posix/sched.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
kernel/cobalt/posix/sched.c +1 -1
@@ -752,7 +752,7 @@ int cobalt_sched_setscheduler_ex(pid_t pid, thread =cobalt_current_thread ();if (thread ==NULL ) { -if (u_winoff ==NULL ) +if (u_winoff ==NULL || pid !=task_pid_vnr (current))return -ESRCH ; thread =cobalt_thread_shadow (current, &hkey, u_winoff);
Signature · 1 line
2.16.4
PATCH 3/6 cobalt/posix: Remove task parameter from cobalt_thread_shadow
In reply to Jan KiszkaFrom: Jan Kiszka <[email protected]>
It's always current, and it can only be current.
--- kernel/cobalt/posix/sched.c | 2 +- kernel/cobalt/posix/thread.c | 9 ++++----- kernel/cobalt/posix/thread.h | 3 +-- 3 files changed, 6 insertions(+), 8 deletions(-)
kernel/cobalt/posix/sched.c +1 -1
@@ -755,7 +755,7 @@ int cobalt_sched_setscheduler_ex(pid_t pid,if (u_winoff ==NULL || pid !=task_pid_vnr (current))return -ESRCH ; - thread =cobalt_thread_shadow (current, &hkey, u_winoff); + thread =cobalt_thread_shadow (&hkey, u_winoff);if (IS_ERR (thread))return PTR_ERR (thread);
kernel/cobalt/posix/thread.c +4 -5
@@ -480,7 +480,7 @@ int cobalt_thread_setschedparam_ex(unsigned long pth,if (u_winoff ==NULL )return -EINVAL ; - thread =cobalt_thread_shadow (current, &hkey, u_winoff); + thread =cobalt_thread_shadow (&hkey, u_winoff);if (IS_ERR (thread))return PTR_ERR (thread);
@@ -617,8 +617,7 @@ COBALT_SYSCALL(thread_create, init, }struct cobalt_thread * -cobalt_thread_shadow (struct task_struct *p, -struct cobalt_local_hkey *hkey, +cobalt_thread_shadow (struct cobalt_local_hkey *hkey,__u32 __user *u_winoff) {struct cobalt_thread *thread =NULL ;
@@ -630,7 +629,7 @@ cobalt_thread_shadow(struct task_struct *p, param_ex.sched_priority =0 ;trace_cobalt_pthread_create (hkey->u_pth,SCHED_NORMAL , ¶m_ex); - ret =pthread_create (&thread,SCHED_NORMAL , ¶m_ex, p); + ret =pthread_create (&thread,SCHED_NORMAL , ¶m_ex, current);if (ret)return ERR_PTR (ret);
@@ -640,7 +639,7 @@ cobalt_thread_shadow(struct task_struct *p,return ERR_PTR (ret); } -if (!thread_hash (hkey, thread,task_pid_vnr (p))) { +if (!thread_hash (hkey, thread,task_pid_vnr (current))) { ret = -EAGAIN ; goto fail; }
kernel/cobalt/posix/thread.h +1 -2
@@ -151,8 +151,7 @@ COBALT_SYSCALL_DECL(thread_create, int xid, __u32 __user *u_winoff));struct cobalt_thread * -cobalt_thread_shadow (struct task_struct *p, -struct cobalt_local_hkey *lhkey, +cobalt_thread_shadow (struct cobalt_local_hkey *lhkey,__u32 __user *u_winoff); COBALT_SYSCALL_DECL(thread_setmode,
Signature · 1 line
2.16.4
PATCH 4/6 lib/cobalt: Avoid requesting shadowing for remote threads
In reply to Jan KiszkaFrom: Jan Kiszka <[email protected]>
Fix pthread_setschedparam_ex that it does not suggests the core to map the current thread if the target thread of the request is actually not pthread_self(). The core will reject such requests now, but we should avoid them in the first place so that we get a clear error code.
--- lib/cobalt/thread.c | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-)
lib/cobalt/thread.c +5 -2
@@ -607,7 +607,7 @@ int pthread_setschedparam_ex(pthread_t thread, {int ret, promoted, std_policy;struct sched_param std_param; -__u32 u_winoff; +__u32 u_winoff, *u_winoff_ptr;/* * If we enter this call over a relaxed context, take the
@@ -637,9 +637,12 @@ int pthread_setschedparam_ex(pthread_t thread,return ret; } +/* only request promotion when this targets the current thread */ + u_winoff_ptr = thread ==pthread_self () ? &u_winoff :NULL ; + ret = -XENOMAI_SYSCALL5 (sc_cobalt_thread_setschedparam_ex, thread, policy, param_ex, - &u_winoff, &promoted); + u_winoff_ptr, &promoted);if (ret ==0 && promoted) {cobalt_sigshadow_install_once ();
Signature · 1 line
2.16.4
PATCH 5/6 lib/cobalt: Pass setsched requests for unknown threads to glibc
In reply to Jan KiszkaFrom: Jan Kiszka <[email protected]>
This aligns the implementation with the documentation: If we get an error from the kernel that suggests the thread is unknown to it, let glibc deal with it.
Note that pthread_setschedparam_ex currently returns an invalid error code, EINVAL rather than ESRCH, so that we try to detect the remote thread case with some heuristic.
--- lib/cobalt/sched.c | 10 ++++++++++ lib/cobalt/thread.c | 11 +++++++++++ 2 files changed, 21 insertions(+)
lib/cobalt/sched.c +10 -0
@@ -308,6 +308,16 @@ int sched_setscheduler_ex(pid_t pid, pid, policy, param_ex, &u_winoff, &promoted); +/* +* If the kernel has no reference to the target thread, let glibc +* handle the call. +*/ +if (ret ==ESRCH ) { + std_policy =cobalt_xlate_schedparam (policy, param_ex, + &std_param); +return __STD (sched_setscheduler (pid, std_policy, &std_param)); + } +if (ret ==0 && promoted) {cobalt_sigshadow_install_once ();cobalt_set_tsd (u_winoff);
lib/cobalt/thread.c +11 -0
@@ -644,6 +644,17 @@ int pthread_setschedparam_ex(pthread_t thread, thread, policy, param_ex, u_winoff_ptr, &promoted); +/* +* If the kernel has no reference to the target thread. let glibc +* handle the call. +*/ +if (!u_winoff_ptr && ret ==EINVAL ) { + std_policy =cobalt_xlate_schedparam (policy, param_ex, + &std_param); +return __STD (pthread_setschedparam (thread, std_policy, + &std_param)); + } +if (ret ==0 && promoted) {cobalt_sigshadow_install_once ();cobalt_set_tsd (u_winoff);
Signature · 1 line
2.16.4
PATCH 6/6 cobalt: Fix error code of cobalt_thread_setschedparam_ex
In reply to Jan KiszkaFrom: Jan Kiszka <[email protected]>
This aligns the return code with cobalt_sched_setschedparam_ex: If there is no u_winoff and no thread, we should return ESRCH so that userspace can easily detect that it should call the standard libc service instead. Fix the core and remove the related workaround from the userspace lib.
--- kernel/cobalt/posix/thread.c | 2 +- lib/cobalt/thread.c | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-)
kernel/cobalt/posix/thread.c +1 -1
@@ -478,7 +478,7 @@ int cobalt_thread_setschedparam_ex(unsigned long pth, thread =thread_lookup (&hkey);if (thread ==NULL ) {if (u_winoff ==NULL ) -return -EINVAL ; +return -ESRCH ; thread =cobalt_thread_shadow (&hkey, u_winoff);if (IS_ERR (thread))
lib/cobalt/thread.c +1 -1
@@ -648,7 +648,7 @@ int pthread_setschedparam_ex(pthread_t thread,* If the kernel has no reference to the target thread. let glibc * handle the call. */ -if (!u_winoff_ptr && ret ==EINVAL ) { +if (ret ==ESRCH ) { std_policy =cobalt_xlate_schedparam (policy, param_ex, &std_param); return __STD(pthread_setschedparam(thread, std_policy,
Signature · 1 line
2.16.4