[patch 07/20] kthread_kill_stop()

Mathieu Desnoyers
2010-07-09T23:39:40Z

[-- Attachment #1: kthread_kill_stop.patch --]
[-- Type: text/plain, Size: 2513 bytes --]

Allow use of "interruptible" functions in kernel threads by creating this
kthread_stop_kill() variant. Instead of just waking up the thread, it also sends
a signal after setting the "must exit" variable to 1.

Signed-off-by: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
---
include/linux/kthread.h | 1 +
kernel/kthread.c | 40 ++++++++++++++++++++++++++++++++++++++++
2 files changed, 41 insertions(+)

Index: linux.trees.git/include/linux/kthread.h
===================================================================
--- linux.trees.git.orig/include/linux/kthread.h 2010-06-18 18:37:11.000000000 -0400
+++ linux.trees.git/include/linux/kthread.h 2010-06-18 18:37:27.000000000 -0400
@@ -29,6 +29,7 @@ struct task_struct *kthread_create(int (

void kthread_bind(struct task_struct *k, unsigned int cpu);
int kthread_stop(struct task_struct *k);
+int kthread_kill_stop(struct task_struct *k, int signo);
int kthread_should_stop(void);

int kthreadd(void *unused);
Index: linux.trees.git/kernel/kthread.c
===================================================================
--- linux.trees.git.orig/kernel/kthread.c 2010-06-18 18:32:21.000000000 -0400
+++ linux.trees.git/kernel/kthread.c 2010-06-18 18:37:57.000000000 -0400
@@ -211,6 +211,46 @@ int kthread_stop(struct task_struct *k)
}
EXPORT_SYMBOL(kthread_stop);

+/**
+ * kthread_kill_stop - kill and stop a thread created by kthread_create().
+ * @k: thread created by kthread_create().
+ * @signo: signal number to send.
+ *
+ * Sets kthread_should_stop() for @k to return true, sends a signal, and
+ * waits for it to exit. This can also be called after kthread_create()
+ * instead of calling wake_up_process(): the thread will exit without
+ * calling threadfn().
+ *
+ * If threadfn() may call do_exit() itself, the caller must ensure
+ * task_struct can't go away.
+ *
+ * Returns the result of threadfn(), or %-EINTR if wake_up_process()
+ * was never called.
+ */
+int kthread_kill_stop(struct task_struct *k, int signo)
+{
+ 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;
+ force_sig(signo, k);
+ wait_for_completion(&kthread->exited);
+ }
+ ret = k->exit_code;
+
+ put_task_struct(k);
+ trace_sched_kthread_stop_ret(ret);
+
+ return ret;
+}
+EXPORT_SYMBOL(kthread_kill_stop);
+
int kthreadd(void *unused)
{
struct task_struct *tsk = current;