[RFC PATCH 07/20] kthread: Add kthread_kill_stop()

Previous message: [thread] [date] [author]
Next message: [thread] [date] [author]
From: Mathieu Desnoyers
Date: Tuesday, August 17, 2010 - 4:16 pm

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-08-17 16:28:25.000000000 -0400
+++ linux.trees.git/include/linux/kthread.h	2010-08-17 16:32:36.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);
 void *kthread_data(struct task_struct *k);
 
Index: linux.trees.git/kernel/kthread.c
===================================================================
--- linux.trees.git.orig/kernel/kthread.c	2010-08-17 16:28:26.000000000 -0400
+++ linux.trees.git/kernel/kthread.c	2010-08-17 16:32:36.000000000 -0400
@@ -228,6 +228,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;

--
Previous message: [thread] [date] [author]
Next message: [thread] [date] [author]

Messages in current thread:
[RFC PATCH 00/20] Generic Ring Buffer Library (v2), Mathieu Desnoyers, (Tue Aug 17, 4:16 pm)
[RFC PATCH 01/20] Create generic alignment API (v8), Mathieu Desnoyers, (Tue Aug 17, 4:16 pm)
[RFC PATCH 02/20] Notifier atomic call chain notrace, Mathieu Desnoyers, (Tue Aug 17, 4:16 pm)
[RFC PATCH 05/20] Poll : add poll_wait_set_exclusive, Mathieu Desnoyers, (Tue Aug 17, 4:16 pm)
[RFC PATCH 07/20] kthread: Add kthread_kill_stop(), Mathieu Desnoyers, (Tue Aug 17, 4:16 pm)
[RFC PATCH 09/20] x86: inline memcpy, Mathieu Desnoyers, (Tue Aug 17, 4:16 pm)
[RFC PATCH 10/20] Trace clock - build standalone, Mathieu Desnoyers, (Tue Aug 17, 4:16 pm)
[RFC PATCH 11/20] Ftrace ring buffer renaming (v2), Mathieu Desnoyers, (Tue Aug 17, 4:16 pm)
[RFC PATCH 12/20] Ring buffer backend, Mathieu Desnoyers, (Tue Aug 17, 4:16 pm)
[RFC PATCH 13/20] Ring buffer frontend, Mathieu Desnoyers, (Tue Aug 17, 4:16 pm)
[RFC PATCH 14/20] Ring buffer library - documentation (v2), Mathieu Desnoyers, (Tue Aug 17, 4:16 pm)
[RFC PATCH 15/20] Ring buffer library - VFS operations (v2), Mathieu Desnoyers, (Tue Aug 17, 4:16 pm)
[RFC PATCH 16/20] Ring buffer library - client sample, Mathieu Desnoyers, (Tue Aug 17, 4:16 pm)
[RFC PATCH 17/20] Ring buffer benchmark library, Mathieu Desnoyers, (Tue Aug 17, 4:16 pm)
[RFC PATCH 18/20] Ring buffer record iterator, Mathieu Desnoyers, (Tue Aug 17, 4:16 pm)
[RFC PATCH 19/20] Ring buffer library: basic API (v2), Mathieu Desnoyers, (Tue Aug 17, 4:16 pm)
[RFC PATCH 20/20] Ring buffer: benchmark simple API (v2), Mathieu Desnoyers, (Tue Aug 17, 4:16 pm)
Re: [RFC PATCH 01/20] Create generic alignment API (v8), Kirill A. Shutemov, (Tue Aug 17, 5:00 pm)
Re: [RFC PATCH 01/20] Create generic alignment API (v8), Mathieu Desnoyers, (Tue Aug 17, 6:05 pm)
Re: [RFC PATCH 01/20] Create generic alignment API (v8), Steven Rostedt, (Tue Aug 17, 6:25 pm)
Re: [RFC PATCH 01/20] Create generic alignment API (v8), Mathieu Desnoyers, (Tue Aug 17, 7:09 pm)