Resolve kernel thread shutdown race, simplify host PID handling
cover Resolve kernel thread shutdown race, simplify host PID handling
Triggered by Richard's analysis of a race we have between (RTDM) kernel xnthreads terminating and xnthread_join waiting for them, this reworks how we retrieve the PID of an xnthread host part, resolving that race (hopefully) and simplifying/optimizing the code along that.
Patch 1 is a stable backport candidate.
Jan
Jan Kiszka (3):
cobalt/thread: Keep host task PID in xnthread struct as well cobalt/thread: Simplify wait_for_rcu_grace_period
cobalt: Eliminate xnthread_host_pid wrapper
include/cobalt/kernel/thread.h | 4 +-- kernel/cobalt/debug.c | 2 +- kernel/cobalt/dovetail/kevents.c | 4 +-- kernel/cobalt/dovetail/sched.c | 2 ++ kernel/cobalt/posix/event.c | 2 +- kernel/cobalt/posix/process.c | 5 ++-- kernel/cobalt/posix/sem.c | 2 +- kernel/cobalt/posix/thread.c | 2 +- kernel/cobalt/posix/timer.c | 2 +- kernel/cobalt/sched-quota.c | 2 +- kernel/cobalt/sched-rt.c | 2 +- kernel/cobalt/sched-sporadic.c | 2 +- kernel/cobalt/sched-tp.c | 2 +- kernel/cobalt/sched-weak.c | 2 +- kernel/cobalt/sched.c | 4 +-- kernel/cobalt/thread.c | 48 +++++++------------------------ kernel/cobalt/trace/cobalt-core.h | 18 ++++++------ 17 files changed, 40 insertions(+), 65 deletions(-)
Signature · 1 line
2.51.0
PATCH 1/3 cobalt/thread: Keep host task PID in xnthread struct as well
From: Jan Kiszka <[email protected]>
This side-steps the logic of current xnthread_host_pid to check for corner cases. Furthermore, it avoids the race on kernel thread shutdown between the thread leaving its loop and freeing its task_struct on the one side and xnthread_join trying to dereference the host task in order to retrieve the PID (see also
https://lore.kernel.org/xenomai/[email protected]).
--- include/cobalt/kernel/thread.h | 7 ++++++- kernel/cobalt/dovetail/sched.c | 2 ++ kernel/cobalt/thread.c | 11 +---------- 3 files changed, 9 insertions(+), 11 deletions(-)
include/cobalt/kernel/thread.h +6 -1
@@ -204,6 +204,8 @@ struct xnthread {void (*entry)(void *cookie);/* Thread entry routine */ void *cookie;/* Cookie to pass to the entry routine */ +pid_t host_pid;/* PID of host task (if any), or -1 */ +/** * Thread data visible from userland through a window on the * global heap.
@@ -453,7 +455,10 @@ void xnthread_deregister(struct xnthread *thread);char *xnthread_format_status (unsigned long status,char *buf,int size); -pid_t xnthread_host_pid (struct xnthread *thread); +static inline pid_t xnthread_host_pid (struct xnthread *thread) +{ +return thread->host_pid; +}int xnthread_set_clock (struct xnthread *thread,struct xnclock *newclock);
kernel/cobalt/dovetail/sched.c +2 -0
@@ -37,6 +37,7 @@ void pipeline_init_shadow_tcb(struct xnthread *thread)* Initialize the alternate scheduling control block. */ dovetail_init_altsched (&xnthread_archtcb (thread)->altsched); + thread->host_pid =task_pid_nr (xnthread_host_task (thread));trace_cobalt_shadow_map (thread); }
@@ -47,6 +48,7 @@ void pipeline_init_root_tcb(struct xnthread *thread)* Initialize the alternate scheduling control block. */ dovetail_init_altsched (&xnthread_archtcb (thread)->altsched); + thread->host_pid =0 ; }int pipeline_leave_inband (void )
kernel/cobalt/thread.c +1 -10
@@ -284,6 +284,7 @@ int __xnthread_init(struct xnthread *thread,/* These will be filled by xnthread_start() */ thread->entry =NULL ; thread->cookie =NULL ; + thread->host_pid =-1 ;init_completion (&thread->exited);memset (xnthread_archtcb (thread),0 ,sizeof (struct xnarchtcb ));memset (thread->sigarray,0 ,sizeof (thread->sigarray));
@@ -377,16 +378,6 @@ char *xnthread_format_status(unsigned long status, char *buf, int size)return buf; } -pid_t xnthread_host_pid (struct xnthread *thread) -{ -if (xnthread_test_state (thread,XNROOT )) -return 0 ; -if (!xnthread_host_task (thread)) -return -1 ; - -return task_pid_nr (xnthread_host_task (thread)); -} -int xnthread_set_clock (struct xnthread *thread,struct xnclock *newclock) {spl_t s;
Signature · 1 line
2.51.0
PATCH 2/3 cobalt/thread: Simplify wait_for_rcu_grace_period
In reply to Jan KiszkaFrom: Jan Kiszka <[email protected]>
Now that xnthread struct holds a stable host_pid, we can simplify its interface, just passing the thread. As we are no longer holder some reference the pid object, additionally match the host_pid's current task against the xnthread's host task reference to avoid being misguided by a potential pid reuse.
--- kernel/cobalt/thread.c | 28 ++++++---------------------- 1 file changed, 6 insertions(+), 22 deletions(-)
kernel/cobalt/thread.c +6 -22
@@ -1588,7 +1588,7 @@ static void grace_elapsed(struct rcu_head *head)complete (&wgs->done); } -static void wait_for_rcu_grace_period (struct pid *pid) +static void wait_for_rcu_grace_period (struct xnthread *thread) {struct wait_grace_struct wait = { .done =COMPLETION_INITIALIZER_ONSTACK (wait.done),
@@ -1600,12 +1600,12 @@ static void wait_for_rcu_grace_period(struct pid *pid)for (;;) {call_rcu (&wait.rcu, grace_elapsed);wait_for_completion (&wait.done); -if (pid ==NULL ) +if (thread ==NULL )break ;rcu_read_lock (); - p =pid_task (pid,PIDTYPE_PID ); + p =find_task_by_pid_ns (thread->host_pid, &init_pid_ns);rcu_read_unlock (); -if (p ==NULL ) +if (p !=xnthread_host_task (thread))break ;reinit_completion (&wait.done); }
@@ -1648,8 +1648,6 @@ int xnthread_join(struct xnthread *thread, bool uninterruptible) {struct xnthread *curr =xnthread_current ();int ret =0 , switched =0 ; -struct pid *pid; -pid_t tpid;spl_t s;XENO_BUG_ON (COBALT ,xnthread_test_state (thread,XNROOT ));
@@ -1670,7 +1668,6 @@ int xnthread_join(struct xnthread *thread, bool uninterruptible)trace_cobalt_thread_join (thread);xnthread_set_state (thread,XNJOINED ); - tpid =xnthread_host_pid (thread);if (curr && !xnthread_test_state (curr,XNRELAX )) {xnlock_put_irqrestore (&nklock, s);
@@ -1679,15 +1676,6 @@ int xnthread_join(struct xnthread *thread, bool uninterruptible) }else xnlock_put_irqrestore (&nklock, s); -/* -* Since in theory, we might be sleeping there for a long -* time, we get a reference on the pid struct holding our -* target, then we check for its existence upon wake up. -*/ - pid =find_get_pid (tpid); -if (pid ==NULL ) - goto done; -/* * We have a tricky issue to deal with, which involves code * relying on the assumption that a destroyed thread will have
@@ -1726,17 +1714,13 @@ int xnthread_join(struct xnthread *thread, bool uninterruptible)wait_for_completion (&thread->exited); else { ret =wait_for_completion_interruptible (&thread->exited); -if (ret <0 ) { -put_pid (pid); +if (ret <0 )return -EINTR ; - } }/* Make sure the joinee has scheduled away ultimately. */ -wait_for_rcu_grace_period (pid); +wait_for_rcu_grace_period (thread); -put_pid (pid); -done: ret =0 ;if (switched) ret =xnthread_harden ();
Signature · 1 line
2.51.0
PATCH 3/3 cobalt: Eliminate xnthread_host_pid wrapper
In reply to Jan KiszkaFrom: Jan Kiszka <[email protected]>
There is no value in this wrapper anymore, just access the new field directly.
--- include/cobalt/kernel/thread.h | 5 ----- kernel/cobalt/debug.c | 2 +- kernel/cobalt/dovetail/kevents.c | 4 ++-- kernel/cobalt/posix/event.c | 2 +- kernel/cobalt/posix/process.c | 5 ++--- kernel/cobalt/posix/sem.c | 2 +- kernel/cobalt/posix/thread.c | 2 +- kernel/cobalt/posix/timer.c | 2 +- kernel/cobalt/sched-quota.c | 2 +- kernel/cobalt/sched-rt.c | 2 +- kernel/cobalt/sched-sporadic.c | 2 +- kernel/cobalt/sched-tp.c | 2 +- kernel/cobalt/sched-weak.c | 2 +- kernel/cobalt/sched.c | 4 ++-- kernel/cobalt/thread.c | 9 ++++----- kernel/cobalt/trace/cobalt-core.h | 18 +++++++++--------- 16 files changed, 29 insertions(+), 36 deletions(-)
include/cobalt/kernel/thread.h +0 -5
@@ -455,11 +455,6 @@ void xnthread_deregister(struct xnthread *thread);char *xnthread_format_status (unsigned long status,char *buf,int size); -static inline pid_t xnthread_host_pid (struct xnthread *thread) -{ -return thread->host_pid; -} -int xnthread_set_clock (struct xnthread *thread,struct xnclock *newclock);
kernel/cobalt/debug.c +1 -1
@@ -296,7 +296,7 @@ void xndebug_trace_relax(int nr, unsigned long *backtrace,*/ spot.depth = depth; spot.proghash = thread->proghash; - spot.pid =xnthread_host_pid (thread); + spot.pid = thread->host_pid; spot.reason = reason;strcpy (spot.thread, thread->name); hash =jhash2 ((u32 *)&spot,sizeof (spot) /sizeof (u32),0 );
kernel/cobalt/dovetail/kevents.c +2 -2
@@ -70,14 +70,14 @@ void handle_oob_trap_entry(unsigned int trapnr, struct pt_regs *regs)"kernel-space at %pS (pid %d)\n" , thread->name, trapnr, (void *)xnarch_fault_pc (regs), - xnthread_host_pid(thread)); + thread->host_pid); }else if (xnarch_fault_notify (trapnr))/* Don't report debug traps */ printk (XENO_WARNING "switching %s to secondary mode after exception #%u from " "user-space at 0x%lx (pid %d)\n" , thread->name, trapnr,xnarch_fault_pc (regs), -xnthread_host_pid (thread)); + thread->host_pid);#endif if (xnarch_fault_pf_p (trapnr))
kernel/cobalt/posix/event.c +1 -1
@@ -372,7 +372,7 @@ COBALT_SYSCALL(event_inquire, current,xnsynch_for_each_sleeper (thread, &event->synch) {if (nrwait >= nrpids)break ; - t[nrwait++] =xnthread_host_pid (thread); + t[nrwait++] = thread->host_pid; } }
kernel/cobalt/posix/process.c +2 -3
@@ -656,8 +656,7 @@ int cobalt_map_user(struct xnthread *thread, __u32 __user *u_winoff)xnthread_sync_window (thread); -xntrace_pid (xnthread_host_pid (thread), -xnthread_current_priority (thread)); +xntrace_pid (thread->host_pid,xnthread_current_priority (thread));return 0 ; }
@@ -809,7 +808,7 @@ void cobalt_adjust_affinity(struct task_struct *task) /* nklocked, IRQs off */* Whine and cancel that thread. */ printk (XENO_WARNING "thread %s[%d] switched to non-rt CPU%d, aborted.\n" , - thread->name,xnthread_host_pid (thread), cpu); + thread->name, thread->host_pid, cpu);/* * Can't call xnthread_cancel() from a migration * point, that would break. Since we are on the wakeup
kernel/cobalt/posix/sem.c +1 -1
@@ -629,7 +629,7 @@ COBALT_SYSCALL(sem_inquire, current,xnsynch_for_each_sleeper (thread, &sem->synchbase) {if (nrwait >= nrpids)break ; - t[nrwait++] =xnthread_host_pid (thread); + t[nrwait++] = thread->host_pid; } }
kernel/cobalt/posix/thread.c +1 -1
@@ -820,7 +820,7 @@ COBALT_SYSCALL(thread_getpid, current, (unsigned long pth))if (thread ==NULL ) pid = -ESRCH ;else - pid =xnthread_host_pid (&thread->threadbase); + pid = thread->threadbase.host_pid;xnlock_put_irqrestore (&nklock, s);
kernel/cobalt/posix/timer.c +1 -1
@@ -184,7 +184,7 @@ static int cobalt_timer_create_internal(clockid_t clockid, goto fail; } - timer->target =xnthread_host_pid (&target->threadbase); + timer->target = target->threadbase.host_pid; cc->timers[timer_id] = timer;xnlock_put_irqrestore (&nklock, s);
kernel/cobalt/sched-quota.c +1 -1
@@ -749,7 +749,7 @@ static int vfile_sched_quota_next(struct xnvfile_snapshot_iterator *it,return VFILE_SEQ_SKIP ; p->cpu =xnsched_cpu (thread->sched); - p->pid =xnthread_host_pid (thread); + p->pid = thread->host_pid;memcpy (p->name, thread->name,sizeof (p->name)); p->tgid = thread->quota->tgid; p->prio = thread->cprio;
kernel/cobalt/sched-rt.c +1 -1
@@ -173,7 +173,7 @@ static int vfile_sched_rt_next(struct xnvfile_snapshot_iterator *it,return VFILE_SEQ_SKIP ; p->cpu =xnsched_cpu (thread->sched); - p->pid =xnthread_host_pid (thread); + p->pid = thread->host_pid;memcpy (p->name, thread->name,sizeof (p->name)); p->cprio = thread->cprio; p->period =xnthread_get_period (thread);
kernel/cobalt/sched-sporadic.c +1 -1
@@ -458,7 +458,7 @@ static int vfile_sched_sporadic_next(struct xnvfile_snapshot_iterator *it,return VFILE_SEQ_SKIP ; p->cpu =xnsched_cpu (thread->sched); - p->pid =xnthread_host_pid (thread); + p->pid = thread->host_pid;memcpy (p->name, thread->name,sizeof (p->name)); p->current_prio = thread->cprio; p->low_prio = thread->pss->param.low_prio;
kernel/cobalt/sched-tp.c +1 -1
@@ -382,7 +382,7 @@ static int vfile_sched_tp_next(struct xnvfile_snapshot_iterator *it,return VFILE_SEQ_SKIP ; p->cpu =xnsched_cpu (thread->sched); - p->pid =xnthread_host_pid (thread); + p->pid = thread->host_pid;memcpy (p->name, thread->name,sizeof (p->name)); p->ptid = thread->tps - thread->sched->tp.partitions; p->prio = thread->cprio;
kernel/cobalt/sched-weak.c +1 -1
@@ -143,7 +143,7 @@ static int vfile_sched_weak_next(struct xnvfile_snapshot_iterator *it,return VFILE_SEQ_SKIP ; p->cpu =xnsched_cpu (thread->sched); - p->pid =xnthread_host_pid (thread); + p->pid = thread->host_pid;memcpy (p->name, thread->name,sizeof (p->name)); p->cprio = thread->cprio;
kernel/cobalt/sched.c +2 -2
@@ -1037,7 +1037,7 @@ static int vfile_schedlist_next(struct xnvfile_snapshot_iterator *it, priv->curr =list_next_entry (thread, glink); p->cpu =xnsched_cpu (thread->sched); - p->pid =xnthread_host_pid (thread); + p->pid = thread->host_pid;memcpy (p->name, thread->name,sizeof (p->name)); p->cprio = thread->cprio; p->state =xnthread_get_state (thread);
@@ -1197,7 +1197,7 @@ static int vfile_schedstat_next(struct xnvfile_snapshot_iterator *it, sched = thread->sched; p->cpu =xnsched_cpu (sched); - p->pid =xnthread_host_pid (thread); + p->pid = thread->host_pid;memcpy (p->name, thread->name,sizeof (p->name)); p->state =xnthread_get_state (thread);if (thread->lock_count >0 )
kernel/cobalt/thread.c +4 -5
@@ -167,8 +167,7 @@ static int map_kthread(struct xnthread *thread, struct kthread_arg *ka)xnthread_test_cancel (); -xntrace_pid (xnthread_host_pid (thread), -xnthread_current_priority (thread)); +xntrace_pid (thread->host_pid,xnthread_current_priority (thread));return ret; }
@@ -1092,7 +1091,7 @@ void xnthread_resume(struct xnthread *thread, int mask)trace_cobalt_thread_resume (thread, mask); -xntrace_pid (xnthread_host_pid (thread),xnthread_current_priority (thread)); +xntrace_pid (thread->host_pid,xnthread_current_priority (thread)); sched = thread->sched; oldstate = thread->state;
@@ -2066,7 +2065,7 @@ void xnthread_relax(int notify, int reason)*/ XENO_WARN (COBALT ,is_primary_domain (),"xnthread_relax() failed for thread %s[%d]" , - thread->name,xnthread_host_pid (thread)); + thread->name, thread->host_pid);pipeline_leave_oob_finish ();
@@ -2447,7 +2446,7 @@ int xnthread_killall(int grace, int mask)if (XENO_DEBUG (COBALT ))printk (XENO_INFO "terminating %s[%d]\n" , - t->name,xnthread_host_pid (t)); + t->name, t->host_pid); nrkilled++;xnthread_cancel (t); }
kernel/cobalt/trace/cobalt-core.h +9 -9
@@ -49,7 +49,7 @@ DECLARE_EVENT_CLASS(thread_event,TP_fast_assign ( __entry->state = thread->state; __entry->info = thread->info; - __entry->pid =xnthread_host_pid (thread); + __entry->pid = thread->host_pid; ), TP_printk("pid=%d state=0x%lx info=0x%lx",
@@ -216,12 +216,12 @@ TRACE_EVENT(cobalt_switch_context,TP_fast_assign ( __entry->prev = prev;__wrap_assign_str (prev_name, prev->name); - __entry->prev_pid =xnthread_host_pid (prev); + __entry->prev_pid = prev->host_pid; __entry->prev_prio =xnthread_current_priority (prev); __entry->prev_state = prev->state; __entry->next = next;__wrap_assign_str (next_name, next->name); - __entry->next_pid =xnthread_host_pid (next); + __entry->next_pid = next->host_pid; __entry->next_prio =xnthread_current_priority (next); ),
@@ -310,7 +310,7 @@ DECLARE_EVENT_CLASS(schedquota_thread_event,TP_fast_assign ( __entry->tgid = tg->tgid; __entry->thread = thread; - __entry->pid =xnthread_host_pid (thread); + __entry->pid = thread->host_pid; ), TP_printk("tgid=%d thread=%p pid=%d",
@@ -372,7 +372,7 @@ TRACE_EVENT(cobalt_thread_suspend, ), TP_fast_assign( - __entry->pid = xnthread_host_pid(thread); + __entry->pid = thread->host_pid; __entry->mask = mask; __entry->timeout = timeout; __entry->timeout_mode = timeout_mode;
@@ -396,7 +396,7 @@ TRACE_EVENT(cobalt_thread_resume,TP_fast_assign ( __wrap_assign_str(name, thread->name); - __entry->pid =xnthread_host_pid (thread); + __entry->pid = thread->host_pid; __entry->mask = mask; ),
@@ -434,7 +434,7 @@ TRACE_EVENT(cobalt_thread_set_current_prio,TP_fast_assign ( __entry->thread = thread; - __entry->pid =xnthread_host_pid (thread); + __entry->pid = thread->host_pid; __entry->cprio =xnthread_current_priority (thread); ),
@@ -504,7 +504,7 @@ TRACE_EVENT(cobalt_thread_migrate_passive,TP_fast_assign ( __entry->thread = thread; - __entry->pid =xnthread_host_pid (thread); + __entry->pid = thread->host_pid; __entry->cpu = cpu; ),
@@ -571,7 +571,7 @@ TRACE_EVENT(cobalt_shadow_map,TP_fast_assign ( __entry->thread = thread; - __entry->pid =xnthread_host_pid (thread); + __entry->pid = thread->host_pid; __entry->prio =xnthread_base_priority (thread); ),
Signature · 1 line
2.51.0