Re: Module/kthread/printk question/problem

From: Eric Dumazet
Date: Thu Feb 02 2012 - 02:45:20 EST


Le jeudi 02 fÃvrier 2012 Ã 10:13 +0400, Dmitry Antipov a Ãcrit :
> On 02/01/2012 09:16 PM, Eric Dumazet wrote:
>
> >> I realize this, but there was a second part of the question: what's the
> >> better way to ensure that all test/X threads are really gone at some point of
> >> testmod_exit()?
> >>
> >
> > You could use kthread_stop()
> >
> > This way you can control all your kernel threads really exited before
> > module cleanup.
>
> Hm... if I try something like:
>
> static void __exit testmod_exit(void)
> {
> int i;
>
> wait_for_completion(&done);
> for (i = 0; i < nrthreads; i++)
> kthread_stop(threads[i]);
> kfree(threads);
> }
>
> typical result is:


> I suppose that __put_task_struct() was called for the thread when is 'partially dead'
> (because it's somewhere in do_exit() called by kthread() after returning from thread's
> function), but not 'dead enough' to finalize it with free_task().
>
> So, the question is still open.
>

Really you need to better understand how all this works.

Remove the wait_for_completion(), this brings nothing at all, as you
already discovered.

Then you need cooperation from worker threads : they must wait for
kthread_should_stop(), or else your kthread_stop(arg) pass an already
freed "arg" memory block.

Take the time to read kernel/kthread.c and function kthread_stop()



--
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/