Commit 63706172 authored by Oleg Nesterov's avatar Oleg Nesterov Committed by Linus Torvalds
Browse files

kthreads: rework kthread_stop()

Based on Eric's patch which in turn was based on my patch.

kthread_stop() has the nasty problems:

- it runs unpredictably long with the global semaphore held.

- it deadlocks if kthread itself does kthread_stop() before it obeys
  the kthread_should_stop() request.

- it is not useable if kthread exits on its own, see for example the
  ugly "wait_to_die:" hack in migration_thread()

- it is not possible to just tell kthread it should stop, we must always
  wait for its exit.

With this patch kthread() allocates all neccesary data (struct kthread) on
its own stack, globals kthread_stop_xxx are deleted.  ->vfork_done is used
as a pointer into "struct kthread", this means kthread_stop() can easily
wait for kthread's exit.
Signed-off-by: default avatarOleg Nesterov <>
Cc: Christoph Hellwig <>
Cc: "Eric W. Biederman" <>
Cc: Ingo Molnar <>
Cc: Pavel Emelyanov <>
Cc: Rusty Russell <>
Cc: Vitaliy Gusev <
Signed-off-by: default avatarAndrew Morton <>
Signed-off-by: default avatarLinus Torvalds <>
parent cdd140bd
......@@ -35,17 +35,13 @@ struct kthread_create_info
struct list_head list;
struct kthread_stop_info
struct task_struct *k;
int err;
struct completion done;
struct kthread {
int should_stop;
struct completion exited;
/* Thread stopping is done by setthing this var: lock serializes
* multiple kthread_stop calls. */
static DEFINE_MUTEX(kthread_stop_lock);
static struct kthread_stop_info kthread_stop_info;
#define to_kthread(tsk) \
container_of((tsk)->vfork_done, struct kthread, exited)
* kthread_should_stop - should this kthread return now?
......@@ -56,20 +52,22 @@ static struct kthread_stop_info kthread_stop_info;
int kthread_should_stop(void)
return (kthread_stop_info.k == current);
return to_kthread(current)->should_stop;
static int kthread(void *_create)
/* Copy data: it's on kthread's stack */
struct kthread_create_info *create = _create;
int (*threadfn)(void *data);
void *data;
int ret = -EINTR;
int (*threadfn)(void *data) = create->threadfn;
void *data = create->data;
struct kthread self;
int ret;
/* Copy data: it's on kthread's stack */
threadfn = create->threadfn;
data = create->data;
self.should_stop = 0;
current->vfork_done = &self.exited;
/* OK, tell user we're spawned, wait for stop or wakeup */
......@@ -77,15 +75,12 @@ static int kthread(void *_create)
if (!kthread_should_stop())
ret = -EINTR;
if (!self.should_stop)
ret = threadfn(data);
/* It might have exited on its own, w/o kthread_stop. Check. */
if (kthread_should_stop()) {
kthread_stop_info.err = ret;
return 0;
/* we can't just return, we must preserve "self" on stack */
static void create_kthread(struct kthread_create_info *create)
......@@ -195,30 +190,22 @@ EXPORT_SYMBOL(kthread_bind);
int kthread_stop(struct task_struct *k)
struct kthread *kthread;
int ret;
/* It could exit after stop_info.k set, but before wake_up_process. */
/* Must init completion *before* thread sees kthread_stop_info.k */
kthread = to_kthread(k);
barrier(); /* it might have exited */
if (k->vfork_done != NULL) {
kthread->should_stop = 1;
ret = k->exit_code;
/* Now set kthread_should_stop() to true, and wake it up. */
kthread_stop_info.k = k;
/* Once it dies, reset stop ptr, gather result and we're done. */
kthread_stop_info.k = NULL;
ret = kthread_stop_info.err;
return ret;
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment