Re: [PATCH 3/4] kthreads: rework kthread_stop()

From: Oleg Nesterov
Date: Fri Jan 30 2009 - 07:53:38 EST


On 01/30, Oleg Nesterov wrote:
>
> 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.

To simplify the review, please see the code with the patch applied.

Oleg.

struct kthread {
int should_stop;
struct completion exited;
};

#define to_kthread(tsk) \
container_of((tsk)->vfork_done, struct kthread, exited)

int kthread_should_stop(void)
{
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) = 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 */
__set_current_state(TASK_UNINTERRUPTIBLE);
create->result = current;
complete(&create->done);
schedule();

ret = -EINTR;
if (!self.should_stop)
ret = threadfn(data);

/* we can't just return, we must preserve "self" on stack */
do_exit(ret);
}

int kthread_stop(struct task_struct *k)
{
struct kthread *kthread;
int ret;

trace_sched_kthread_stop(k);
get_task_struct(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;

put_task_struct(k);
trace_sched_kthread_stop_ret(ret);

return ret;
}

--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/