I've seen bits of scattered information all around, but I can't seem to get to one final answer. How do you clean up a zombie thread in kernel?
Just to make sure, and produce a final correct way of handling threads in kernel, I would like to ask this question more broadly. How do you create, terminate and clean up a thread in the Linux kernel?
What I have so far is this:
thread_func:
exited = 0;
while (!must_exit)
do stuff
exited = 1;
do_exit(0)
init_module:
must_exit = 0;
exited = 1;
kthread_run(thread_func, ...) /* creates and runs the thread */
cleanup_module:
must_exit = 1;
while (!exited)
set_current_state(TASK_INTERRUPTIBLE);
msleep(1);
/* How do I cleanup? */
The closest thing I have found to the cleanup solution is release_task, but I didn't find anywhere talking about it. I imagined since the thread functions are kthread_create
, kthread_run
etc, there should be a kthread_join
or kthread_wait
, but there wasn't. do_wait
also seemed likely, but it doesn't take a struct task_struct *
.
Furthermore, I am not sure if do_exit
is a good idea, or if at all necessary either. Can someone please come up with the minimum sketch of how a kthread should be created, terminated and cleaned up?
do_exit
) or pollkthread_should_stop
until someone (cleanup_module
) callskthread_stop
. I didn't find anywhere saying whetherkthread_stop
also cleans up the thread or not. What makes me wonder is that, if people (on the internet) suggest using eitherdo_exit
or whatever, shouldn't there be a way to cleanup the thread afterdo_exit
? – Aqualung