blob: fbb6222fe7e04d6dd863ea7975e70c4f3f78636d [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* Kernel thread helper functions.
2 * Copyright (C) 2004 IBM Corporation, Rusty Russell.
3 *
Eric W. Biederman73c27992007-05-09 02:34:32 -07004 * Creation is done via kthreadd, so that we get a clean environment
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 * even if we're invoked from userspace (think modprobe, hotplug cpu,
6 * etc.).
7 */
8#include <linux/sched.h>
9#include <linux/kthread.h>
10#include <linux/completion.h>
11#include <linux/err.h>
Miao Xie58568d22009-06-16 15:31:49 -070012#include <linux/cpuset.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include <linux/unistd.h>
14#include <linux/file.h>
15#include <linux/module.h>
Arjan van de Ven97d1f152006-03-23 03:00:24 -080016#include <linux/mutex.h>
Steven Rostedtad8d75f2009-04-14 19:39:12 -040017#include <trace/events/sched.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070018
Eric W. Biederman73c27992007-05-09 02:34:32 -070019static DEFINE_SPINLOCK(kthread_create_lock);
20static LIST_HEAD(kthread_create_list);
21struct task_struct *kthreadd_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -070022
23struct kthread_create_info
24{
Eric W. Biederman73c27992007-05-09 02:34:32 -070025 /* Information passed to kthread() from kthreadd. */
Linus Torvalds1da177e2005-04-16 15:20:36 -070026 int (*threadfn)(void *data);
27 void *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -070028
Eric W. Biederman73c27992007-05-09 02:34:32 -070029 /* Result passed back to kthread_create() from kthreadd. */
Linus Torvalds1da177e2005-04-16 15:20:36 -070030 struct task_struct *result;
31 struct completion done;
David Howells65f27f32006-11-22 14:55:48 +000032
Eric W. Biederman73c27992007-05-09 02:34:32 -070033 struct list_head list;
Linus Torvalds1da177e2005-04-16 15:20:36 -070034};
35
Oleg Nesterov63706172009-06-17 16:27:45 -070036struct kthread {
37 int should_stop;
38 struct completion exited;
Linus Torvalds1da177e2005-04-16 15:20:36 -070039};
40
Oleg Nesterov63706172009-06-17 16:27:45 -070041#define to_kthread(tsk) \
42 container_of((tsk)->vfork_done, struct kthread, exited)
Linus Torvalds1da177e2005-04-16 15:20:36 -070043
Randy Dunlap9e37bd32006-06-25 05:49:19 -070044/**
45 * kthread_should_stop - should this kthread return now?
46 *
Robert P. J. Day72fd4a32007-02-10 01:45:59 -080047 * When someone calls kthread_stop() on your kthread, it will be woken
Randy Dunlap9e37bd32006-06-25 05:49:19 -070048 * and this will return true. You should then return, and your return
49 * value will be passed through to kthread_stop().
50 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070051int kthread_should_stop(void)
52{
Oleg Nesterov63706172009-06-17 16:27:45 -070053 return to_kthread(current)->should_stop;
Linus Torvalds1da177e2005-04-16 15:20:36 -070054}
55EXPORT_SYMBOL(kthread_should_stop);
56
Linus Torvalds1da177e2005-04-16 15:20:36 -070057static int kthread(void *_create)
58{
Eric W. Biederman73c27992007-05-09 02:34:32 -070059 /* Copy data: it's on kthread's stack */
Oleg Nesterov63706172009-06-17 16:27:45 -070060 struct kthread_create_info *create = _create;
61 int (*threadfn)(void *data) = create->threadfn;
62 void *data = create->data;
63 struct kthread self;
64 int ret;
65
66 self.should_stop = 0;
67 init_completion(&self.exited);
68 current->vfork_done = &self.exited;
Linus Torvalds1da177e2005-04-16 15:20:36 -070069
Linus Torvalds1da177e2005-04-16 15:20:36 -070070 /* OK, tell user we're spawned, wait for stop or wakeup */
Oleg Nesterova076e4b2007-05-23 13:57:27 -070071 __set_current_state(TASK_UNINTERRUPTIBLE);
Vitaliy Gusev3217ab92009-04-09 09:50:35 -060072 create->result = current;
Oleg Nesterovcdd140b2009-06-17 16:27:43 -070073 complete(&create->done);
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 schedule();
75
Oleg Nesterov63706172009-06-17 16:27:45 -070076 ret = -EINTR;
77 if (!self.should_stop)
Linus Torvalds1da177e2005-04-16 15:20:36 -070078 ret = threadfn(data);
79
Oleg Nesterov63706172009-06-17 16:27:45 -070080 /* we can't just return, we must preserve "self" on stack */
81 do_exit(ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -070082}
83
Eric W. Biederman73c27992007-05-09 02:34:32 -070084static void create_kthread(struct kthread_create_info *create)
Linus Torvalds1da177e2005-04-16 15:20:36 -070085{
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 int pid;
87
88 /* We want our own signal handler (we take no signals by default). */
89 pid = kernel_thread(kthread, create, CLONE_FS | CLONE_FILES | SIGCHLD);
Oleg Nesterovcdd140b2009-06-17 16:27:43 -070090 if (pid < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 create->result = ERR_PTR(pid);
Oleg Nesterovcdd140b2009-06-17 16:27:43 -070092 complete(&create->done);
93 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070094}
95
Randy Dunlap9e37bd32006-06-25 05:49:19 -070096/**
97 * kthread_create - create a kthread.
98 * @threadfn: the function to run until signal_pending(current).
99 * @data: data ptr for @threadfn.
100 * @namefmt: printf-style name for the thread.
101 *
102 * Description: This helper function creates and names a kernel
103 * thread. The thread will be stopped: use wake_up_process() to start
104 * it. See also kthread_run(), kthread_create_on_cpu().
105 *
106 * When woken, the thread will run @threadfn() with @data as its
Robert P. J. Day72fd4a32007-02-10 01:45:59 -0800107 * argument. @threadfn() can either call do_exit() directly if it is a
Randy Dunlap9e37bd32006-06-25 05:49:19 -0700108 * standalone thread for which noone will call kthread_stop(), or
109 * return when 'kthread_should_stop()' is true (which means
110 * kthread_stop() has been called). The return value should be zero
111 * or a negative error number; it will be passed to kthread_stop().
112 *
113 * Returns a task_struct or ERR_PTR(-ENOMEM).
114 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115struct task_struct *kthread_create(int (*threadfn)(void *data),
116 void *data,
117 const char namefmt[],
118 ...)
119{
120 struct kthread_create_info create;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121
122 create.threadfn = threadfn;
123 create.data = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 init_completion(&create.done);
125
Eric W. Biederman73c27992007-05-09 02:34:32 -0700126 spin_lock(&kthread_create_lock);
127 list_add_tail(&create.list, &kthread_create_list);
Eric W. Biederman73c27992007-05-09 02:34:32 -0700128 spin_unlock(&kthread_create_lock);
129
Dmitry Adamushkocbd9b672008-04-29 00:59:23 -0700130 wake_up_process(kthreadd_task);
Eric W. Biederman73c27992007-05-09 02:34:32 -0700131 wait_for_completion(&create.done);
132
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 if (!IS_ERR(create.result)) {
Oleg Nesterov1c993152009-04-09 09:50:36 -0600134 struct sched_param param = { .sched_priority = 0 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 va_list args;
Oleg Nesterov1c993152009-04-09 09:50:36 -0600136
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 va_start(args, namefmt);
138 vsnprintf(create.result->comm, sizeof(create.result->comm),
139 namefmt, args);
140 va_end(args);
Oleg Nesterov1c993152009-04-09 09:50:36 -0600141 /*
142 * root may have changed our (kthreadd's) priority or CPU mask.
143 * The kernel thread should not inherit these properties.
144 */
145 sched_setscheduler_nocheck(create.result, SCHED_NORMAL, &param);
Oleg Nesterov1c993152009-04-09 09:50:36 -0600146 set_cpus_allowed_ptr(create.result, cpu_all_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 return create.result;
149}
150EXPORT_SYMBOL(kthread_create);
151
Randy Dunlap9e37bd32006-06-25 05:49:19 -0700152/**
Peter Zijlstra881232b2009-12-16 18:04:39 +0100153 * kthread_bind - bind a just-created kthread to a cpu.
154 * @p: thread created by kthread_create().
155 * @cpu: cpu (might not be online, must be possible) for @k to run on.
156 *
157 * Description: This function is equivalent to set_cpus_allowed(),
158 * except that @cpu doesn't need to be online, and the thread must be
159 * stopped (i.e., just returned from kthread_create()).
160 */
161void kthread_bind(struct task_struct *p, unsigned int cpu)
162{
163 /* Must have done schedule() in kthread() before we set_task_cpu */
164 if (!wait_task_inactive(p, TASK_UNINTERRUPTIBLE)) {
165 WARN_ON(1);
166 return;
167 }
168
169 p->cpus_allowed = cpumask_of_cpu(cpu);
170 p->rt.nr_cpus_allowed = 1;
171 p->flags |= PF_THREAD_BOUND;
172}
173EXPORT_SYMBOL(kthread_bind);
174
175/**
Randy Dunlap9e37bd32006-06-25 05:49:19 -0700176 * kthread_stop - stop a thread created by kthread_create().
177 * @k: thread created by kthread_create().
178 *
179 * Sets kthread_should_stop() for @k to return true, wakes it, and
Oleg Nesterov9ae26022009-06-19 02:51:13 +0200180 * waits for it to exit. This can also be called after kthread_create()
181 * instead of calling wake_up_process(): the thread will exit without
182 * calling threadfn().
183 *
184 * If threadfn() may call do_exit() itself, the caller must ensure
185 * task_struct can't go away.
Randy Dunlap9e37bd32006-06-25 05:49:19 -0700186 *
187 * Returns the result of threadfn(), or %-EINTR if wake_up_process()
188 * was never called.
189 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190int kthread_stop(struct task_struct *k)
191{
Oleg Nesterov63706172009-06-17 16:27:45 -0700192 struct kthread *kthread;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 int ret;
194
Oleg Nesterov63706172009-06-17 16:27:45 -0700195 trace_sched_kthread_stop(k);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 get_task_struct(k);
197
Oleg Nesterov63706172009-06-17 16:27:45 -0700198 kthread = to_kthread(k);
199 barrier(); /* it might have exited */
200 if (k->vfork_done != NULL) {
201 kthread->should_stop = 1;
202 wake_up_process(k);
203 wait_for_completion(&kthread->exited);
204 }
205 ret = k->exit_code;
Mathieu Desnoyers0a16b602008-07-18 12:16:17 -0400206
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 put_task_struct(k);
Mathieu Desnoyers0a16b602008-07-18 12:16:17 -0400208 trace_sched_kthread_stop_ret(ret);
209
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 return ret;
211}
Adrian Bunk52e92e52006-07-14 00:24:05 -0700212EXPORT_SYMBOL(kthread_stop);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213
Satyam Sharmae804a4a2007-07-31 00:39:16 -0700214int kthreadd(void *unused)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215{
Eric W. Biederman73c27992007-05-09 02:34:32 -0700216 struct task_struct *tsk = current;
Eric W. Biederman73c27992007-05-09 02:34:32 -0700217
Satyam Sharmae804a4a2007-07-31 00:39:16 -0700218 /* Setup a clean context for our children to inherit. */
Eric W. Biederman73c27992007-05-09 02:34:32 -0700219 set_task_comm(tsk, "kthreadd");
Oleg Nesterov10ab8252007-05-09 02:34:37 -0700220 ignore_signals(tsk);
Rusty Russell1a2142a2009-03-30 22:05:10 -0600221 set_cpus_allowed_ptr(tsk, cpu_all_mask);
Miao Xie58568d22009-06-16 15:31:49 -0700222 set_mems_allowed(node_possible_map);
Eric W. Biederman73c27992007-05-09 02:34:32 -0700223
Rafael J. Wysockiebb12db2008-06-11 22:04:29 +0200224 current->flags |= PF_NOFREEZE | PF_FREEZER_NOSIG;
Eric W. Biederman73c27992007-05-09 02:34:32 -0700225
226 for (;;) {
227 set_current_state(TASK_INTERRUPTIBLE);
228 if (list_empty(&kthread_create_list))
229 schedule();
230 __set_current_state(TASK_RUNNING);
231
232 spin_lock(&kthread_create_lock);
233 while (!list_empty(&kthread_create_list)) {
234 struct kthread_create_info *create;
235
236 create = list_entry(kthread_create_list.next,
237 struct kthread_create_info, list);
238 list_del_init(&create->list);
239 spin_unlock(&kthread_create_lock);
240
241 create_kthread(create);
242
243 spin_lock(&kthread_create_lock);
244 }
245 spin_unlock(&kthread_create_lock);
246 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247
248 return 0;
249}