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: Oleg Nesterov <oleg@redhat.com> Cc: Christoph Hellwig <hch@lst.de> Cc: "Eric W. Biederman" <ebiederm@xmission.com> Cc: Ingo Molnar <mingo@elte.hu> Cc: Pavel Emelyanov <xemul@openvz.org> Cc: Rusty Russell <rusty@rustcorp.com.au> Cc: Vitaliy Gusev <vgusev@openvz.org Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
This commit is contained in:
parent
cdd140bdd6
commit
63706172f3
1 changed files with 29 additions and 42 deletions
|
@ -35,17 +35,13 @@ struct kthread_create_info
|
||||||
struct list_head list;
|
struct list_head list;
|
||||||
};
|
};
|
||||||
|
|
||||||
struct kthread_stop_info
|
struct kthread {
|
||||||
{
|
int should_stop;
|
||||||
struct task_struct *k;
|
struct completion exited;
|
||||||
int err;
|
|
||||||
struct completion done;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
/* Thread stopping is done by setthing this var: lock serializes
|
#define to_kthread(tsk) \
|
||||||
* multiple kthread_stop calls. */
|
container_of((tsk)->vfork_done, struct kthread, exited)
|
||||||
static DEFINE_MUTEX(kthread_stop_lock);
|
|
||||||
static struct kthread_stop_info kthread_stop_info;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* kthread_should_stop - should this kthread return now?
|
* 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)
|
int kthread_should_stop(void)
|
||||||
{
|
{
|
||||||
return (kthread_stop_info.k == current);
|
return to_kthread(current)->should_stop;
|
||||||
}
|
}
|
||||||
EXPORT_SYMBOL(kthread_should_stop);
|
EXPORT_SYMBOL(kthread_should_stop);
|
||||||
|
|
||||||
static int kthread(void *_create)
|
static int kthread(void *_create)
|
||||||
{
|
{
|
||||||
struct kthread_create_info *create = _create;
|
|
||||||
int (*threadfn)(void *data);
|
|
||||||
void *data;
|
|
||||||
int ret = -EINTR;
|
|
||||||
|
|
||||||
/* Copy data: it's on kthread's stack */
|
/* Copy data: it's on kthread's stack */
|
||||||
threadfn = create->threadfn;
|
struct kthread_create_info *create = _create;
|
||||||
data = create->data;
|
int (*threadfn)(void *data) = create->threadfn;
|
||||||
|
void *data = create->data;
|
||||||
|
struct kthread self;
|
||||||
|
int ret;
|
||||||
|
|
||||||
|
self.should_stop = 0;
|
||||||
|
init_completion(&self.exited);
|
||||||
|
current->vfork_done = &self.exited;
|
||||||
|
|
||||||
/* OK, tell user we're spawned, wait for stop or wakeup */
|
/* OK, tell user we're spawned, wait for stop or wakeup */
|
||||||
__set_current_state(TASK_UNINTERRUPTIBLE);
|
__set_current_state(TASK_UNINTERRUPTIBLE);
|
||||||
|
@ -77,15 +75,12 @@ static int kthread(void *_create)
|
||||||
complete(&create->done);
|
complete(&create->done);
|
||||||
schedule();
|
schedule();
|
||||||
|
|
||||||
if (!kthread_should_stop())
|
ret = -EINTR;
|
||||||
|
if (!self.should_stop)
|
||||||
ret = threadfn(data);
|
ret = threadfn(data);
|
||||||
|
|
||||||
/* It might have exited on its own, w/o kthread_stop. Check. */
|
/* we can't just return, we must preserve "self" on stack */
|
||||||
if (kthread_should_stop()) {
|
do_exit(ret);
|
||||||
kthread_stop_info.err = ret;
|
|
||||||
complete(&kthread_stop_info.done);
|
|
||||||
}
|
|
||||||
return 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static void create_kthread(struct kthread_create_info *create)
|
static void create_kthread(struct kthread_create_info *create)
|
||||||
|
@ -195,30 +190,22 @@ EXPORT_SYMBOL(kthread_bind);
|
||||||
*/
|
*/
|
||||||
int kthread_stop(struct task_struct *k)
|
int kthread_stop(struct task_struct *k)
|
||||||
{
|
{
|
||||||
|
struct kthread *kthread;
|
||||||
int ret;
|
int ret;
|
||||||
|
|
||||||
mutex_lock(&kthread_stop_lock);
|
trace_sched_kthread_stop(k);
|
||||||
|
|
||||||
/* It could exit after stop_info.k set, but before wake_up_process. */
|
|
||||||
get_task_struct(k);
|
get_task_struct(k);
|
||||||
|
|
||||||
trace_sched_kthread_stop(k);
|
kthread = to_kthread(k);
|
||||||
|
barrier(); /* it might have exited */
|
||||||
|
if (k->vfork_done != NULL) {
|
||||||
|
kthread->should_stop = 1;
|
||||||
|
wake_up_process(k);
|
||||||
|
wait_for_completion(&kthread->exited);
|
||||||
|
}
|
||||||
|
ret = k->exit_code;
|
||||||
|
|
||||||
/* Must init completion *before* thread sees kthread_stop_info.k */
|
|
||||||
init_completion(&kthread_stop_info.done);
|
|
||||||
smp_wmb();
|
|
||||||
|
|
||||||
/* Now set kthread_should_stop() to true, and wake it up. */
|
|
||||||
kthread_stop_info.k = k;
|
|
||||||
wake_up_process(k);
|
|
||||||
put_task_struct(k);
|
put_task_struct(k);
|
||||||
|
|
||||||
/* Once it dies, reset stop ptr, gather result and we're done. */
|
|
||||||
wait_for_completion(&kthread_stop_info.done);
|
|
||||||
kthread_stop_info.k = NULL;
|
|
||||||
ret = kthread_stop_info.err;
|
|
||||||
mutex_unlock(&kthread_stop_lock);
|
|
||||||
|
|
||||||
trace_sched_kthread_stop_ret(ret);
|
trace_sched_kthread_stop_ret(ret);
|
||||||
|
|
||||||
return ret;
|
return ret;
|
||||||
|
|
Loading…
Reference in a new issue