Kernel thread removal
How do I remove a kernel thread in kernel mode ?
I write a C-function in one of the Linux source files and create a kernel thread by invoking kernel_thread() to run the function, like:
"kernel_thread(a1, NULL, CLONE_FS | CLONE_FILES | CLONE_SIGNAL);" Function a1 simply invokes printk() to output some message on console. I invoke do_exit(0); at the end of a1, but a1's task_struct still exists in in task_struct list after its execution.
How do I remove it a1's task_struct upon its completion? I thought explicitly invoke do_exit() ensures the removal of task_struct?
Quoted 10 lines · James Yu
How do I remove a kernel thread in kernel mode ?
I write a C-function in one of the Linux source files and create a kernel thread by invokingkernel_thread()to run the function, like:
"kernel_thread(a1, NULL, CLONE_FS | CLONE_FILES | CLONE_SIGNAL);" Function a1 simply invokesprintk()to output some message on console. I invokedo_exit(0); at the end of a1, but a1'stask_structstill exists in intask_structlist after its execution.
How do I remove it a1'stask_structupon its completion? I thought explicitly invokedo_exit()ensures the removal oftask_struct? -
This is becoming a FAQ. In the main loop that your kernel thread executes, you do:
if(signal_pending(current))
complete_and_exit(&struct_completion, status_value);
In the module exit code, or wherever you want to shut down the kernel thread, you do:
kill_proc(thread_pid, some_unblocked_signal, 1); wait_for_completion(&struct_completion);
Remember to do init_completion(&struct_completion) in the startup code, and to unblock the signal the kernel thread is supposed to receive, SIGTERM is a good one.
The secret of success is that the kernel thread needs to exit in the context of the kernel thread, and somebody needs to pick up its status. Re-parenting to init will not always work for reaping child status because init needs to get the CPU at the time that you, in the module-remove routine, may have the CPU. This might deadlock. The solution is above.
A final word, both complete_and_exit() and wait_for_completion() need to be free of any spin-locks.
Cheers,
Dick Johnson
Penguin : Linux version 2.6.15.4 on an i686 machine (5589.54 BogoMips). Warning : 98.36% of all statistics are fiction.
_
****************************************************************
The information transmitted in this message is confidential and may be privileged. Any review, retransmission, dissemination, or other use of this information by persons or entities other than the intended recipient is prohibited. If you are not the intended recipient, please notify Analogic Corporation immediately - by replying to this message or by sending an email to [email protected] - and destroy all copies of this information, including any attachments, without reading or disclosing them.
Thank you.
Quoted 9 lines · James Yu
How do I remove a kernel thread in kernel mode ?
I write a C-function in one of the Linux source files and create a kernel thread by invokingkernel_thread()to run the function, like:
"kernel_thread(a1, NULL, CLONE_FS | CLONE_FILES | CLONE_SIGNAL);" Function a1 simply invokesprintk()to output some message on console. I invokedo_exit(0); at the end of a1, but a1'stask_structstill exists in intask_structlist after its execution.
How do I remove it a1'stask_structupon its completion? I thought explicitly invokedo_exit()ensures the removal oftask_struct?
Please use the kthread_ api. See include/linux/kthread.h and kernel/kthread.c for details, or grep for kthread_ to find users.