blob: 9eb7fed0bbaa9895973a14e551c76de31fffe533 [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>
Paul Gortmaker9984de12011-05-23 14:51:41 -040015#include <linux/export.h>
Arjan van de Ven97d1f152006-03-23 03:00:24 -080016#include <linux/mutex.h>
Tejun Heob56c0d82010-06-29 10:07:09 +020017#include <linux/slab.h>
18#include <linux/freezer.h>
Al Viroa74fb732012-10-10 21:28:25 -040019#include <linux/ptrace.h>
Steven Rostedtad8d75f2009-04-14 19:39:12 -040020#include <trace/events/sched.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021
Eric W. Biederman73c27992007-05-09 02:34:32 -070022static DEFINE_SPINLOCK(kthread_create_lock);
23static LIST_HEAD(kthread_create_list);
24struct task_struct *kthreadd_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -070025
26struct kthread_create_info
27{
Eric W. Biederman73c27992007-05-09 02:34:32 -070028 /* Information passed to kthread() from kthreadd. */
Linus Torvalds1da177e2005-04-16 15:20:36 -070029 int (*threadfn)(void *data);
30 void *data;
Eric Dumazet207205a2011-03-22 16:30:44 -070031 int node;
Linus Torvalds1da177e2005-04-16 15:20:36 -070032
Eric W. Biederman73c27992007-05-09 02:34:32 -070033 /* Result passed back to kthread_create() from kthreadd. */
Linus Torvalds1da177e2005-04-16 15:20:36 -070034 struct task_struct *result;
35 struct completion done;
David Howells65f27f32006-11-22 14:55:48 +000036
Eric W. Biederman73c27992007-05-09 02:34:32 -070037 struct list_head list;
Linus Torvalds1da177e2005-04-16 15:20:36 -070038};
39
Oleg Nesterov63706172009-06-17 16:27:45 -070040struct kthread {
Thomas Gleixner2a1d4462012-07-16 10:42:36 +000041 unsigned long flags;
42 unsigned int cpu;
Tejun Heo82805ab2010-06-29 10:07:09 +020043 void *data;
Thomas Gleixner2a1d4462012-07-16 10:42:36 +000044 struct completion parked;
Oleg Nesterov63706172009-06-17 16:27:45 -070045 struct completion exited;
Linus Torvalds1da177e2005-04-16 15:20:36 -070046};
47
Thomas Gleixner2a1d4462012-07-16 10:42:36 +000048enum KTHREAD_BITS {
49 KTHREAD_IS_PER_CPU = 0,
50 KTHREAD_SHOULD_STOP,
51 KTHREAD_SHOULD_PARK,
52 KTHREAD_IS_PARKED,
53};
54
Oleg Nesterov63706172009-06-17 16:27:45 -070055#define to_kthread(tsk) \
56 container_of((tsk)->vfork_done, struct kthread, exited)
Linus Torvalds1da177e2005-04-16 15:20:36 -070057
Randy Dunlap9e37bd32006-06-25 05:49:19 -070058/**
59 * kthread_should_stop - should this kthread return now?
60 *
Robert P. J. Day72fd4a32007-02-10 01:45:59 -080061 * When someone calls kthread_stop() on your kthread, it will be woken
Randy Dunlap9e37bd32006-06-25 05:49:19 -070062 * and this will return true. You should then return, and your return
63 * value will be passed through to kthread_stop().
64 */
Thomas Gleixner2a1d4462012-07-16 10:42:36 +000065bool kthread_should_stop(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070066{
Thomas Gleixner2a1d4462012-07-16 10:42:36 +000067 return test_bit(KTHREAD_SHOULD_STOP, &to_kthread(current)->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -070068}
69EXPORT_SYMBOL(kthread_should_stop);
70
Tejun Heo82805ab2010-06-29 10:07:09 +020071/**
Thomas Gleixner2a1d4462012-07-16 10:42:36 +000072 * kthread_should_park - should this kthread park now?
73 *
74 * When someone calls kthread_park() on your kthread, it will be woken
75 * and this will return true. You should then do the necessary
76 * cleanup and call kthread_parkme()
77 *
78 * Similar to kthread_should_stop(), but this keeps the thread alive
79 * and in a park position. kthread_unpark() "restarts" the thread and
80 * calls the thread function again.
81 */
82bool kthread_should_park(void)
83{
84 return test_bit(KTHREAD_SHOULD_PARK, &to_kthread(current)->flags);
85}
86
87/**
Tejun Heo8a32c442011-11-21 12:32:23 -080088 * kthread_freezable_should_stop - should this freezable kthread return now?
89 * @was_frozen: optional out parameter, indicates whether %current was frozen
90 *
91 * kthread_should_stop() for freezable kthreads, which will enter
92 * refrigerator if necessary. This function is safe from kthread_stop() /
93 * freezer deadlock and freezable kthreads should use this function instead
94 * of calling try_to_freeze() directly.
95 */
96bool kthread_freezable_should_stop(bool *was_frozen)
97{
98 bool frozen = false;
99
100 might_sleep();
101
102 if (unlikely(freezing(current)))
103 frozen = __refrigerator(true);
104
105 if (was_frozen)
106 *was_frozen = frozen;
107
108 return kthread_should_stop();
109}
110EXPORT_SYMBOL_GPL(kthread_freezable_should_stop);
111
112/**
Tejun Heo82805ab2010-06-29 10:07:09 +0200113 * kthread_data - return data value specified on kthread creation
114 * @task: kthread task in question
115 *
116 * Return the data value specified when kthread @task was created.
117 * The caller is responsible for ensuring the validity of @task when
118 * calling this function.
119 */
120void *kthread_data(struct task_struct *task)
121{
122 return to_kthread(task)->data;
123}
124
Thomas Gleixner2a1d4462012-07-16 10:42:36 +0000125static void __kthread_parkme(struct kthread *self)
126{
Thomas Gleixnerf2530dc2013-04-09 09:33:34 +0200127 __set_current_state(TASK_PARKED);
Thomas Gleixner2a1d4462012-07-16 10:42:36 +0000128 while (test_bit(KTHREAD_SHOULD_PARK, &self->flags)) {
129 if (!test_and_set_bit(KTHREAD_IS_PARKED, &self->flags))
130 complete(&self->parked);
131 schedule();
Thomas Gleixnerf2530dc2013-04-09 09:33:34 +0200132 __set_current_state(TASK_PARKED);
Thomas Gleixner2a1d4462012-07-16 10:42:36 +0000133 }
134 clear_bit(KTHREAD_IS_PARKED, &self->flags);
135 __set_current_state(TASK_RUNNING);
136}
137
138void kthread_parkme(void)
139{
140 __kthread_parkme(to_kthread(current));
141}
142
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143static int kthread(void *_create)
144{
Eric W. Biederman73c27992007-05-09 02:34:32 -0700145 /* Copy data: it's on kthread's stack */
Oleg Nesterov63706172009-06-17 16:27:45 -0700146 struct kthread_create_info *create = _create;
147 int (*threadfn)(void *data) = create->threadfn;
148 void *data = create->data;
149 struct kthread self;
150 int ret;
151
Thomas Gleixner2a1d4462012-07-16 10:42:36 +0000152 self.flags = 0;
Tejun Heo82805ab2010-06-29 10:07:09 +0200153 self.data = data;
Oleg Nesterov63706172009-06-17 16:27:45 -0700154 init_completion(&self.exited);
Thomas Gleixner2a1d4462012-07-16 10:42:36 +0000155 init_completion(&self.parked);
Oleg Nesterov63706172009-06-17 16:27:45 -0700156 current->vfork_done = &self.exited;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 /* OK, tell user we're spawned, wait for stop or wakeup */
Oleg Nesterova076e4b2007-05-23 13:57:27 -0700159 __set_current_state(TASK_UNINTERRUPTIBLE);
Vitaliy Gusev3217ab92009-04-09 09:50:35 -0600160 create->result = current;
Oleg Nesterovcdd140b2009-06-17 16:27:43 -0700161 complete(&create->done);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 schedule();
163
Oleg Nesterov63706172009-06-17 16:27:45 -0700164 ret = -EINTR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165
Thomas Gleixner2a1d4462012-07-16 10:42:36 +0000166 if (!test_bit(KTHREAD_SHOULD_STOP, &self.flags)) {
167 __kthread_parkme(&self);
168 ret = threadfn(data);
169 }
Oleg Nesterov63706172009-06-17 16:27:45 -0700170 /* we can't just return, we must preserve "self" on stack */
171 do_exit(ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172}
173
Eric Dumazet207205a2011-03-22 16:30:44 -0700174/* called from do_fork() to get node information for about to be created task */
175int tsk_fork_get_node(struct task_struct *tsk)
176{
177#ifdef CONFIG_NUMA
178 if (tsk == kthreadd_task)
179 return tsk->pref_node_fork;
180#endif
181 return numa_node_id();
182}
183
Eric W. Biederman73c27992007-05-09 02:34:32 -0700184static void create_kthread(struct kthread_create_info *create)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 int pid;
187
Eric Dumazet207205a2011-03-22 16:30:44 -0700188#ifdef CONFIG_NUMA
189 current->pref_node_fork = create->node;
190#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 /* We want our own signal handler (we take no signals by default). */
192 pid = kernel_thread(kthread, create, CLONE_FS | CLONE_FILES | SIGCHLD);
Oleg Nesterovcdd140b2009-06-17 16:27:43 -0700193 if (pid < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 create->result = ERR_PTR(pid);
Oleg Nesterovcdd140b2009-06-17 16:27:43 -0700195 complete(&create->done);
196 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197}
198
Randy Dunlap9e37bd32006-06-25 05:49:19 -0700199/**
Eric Dumazet207205a2011-03-22 16:30:44 -0700200 * kthread_create_on_node - create a kthread.
Randy Dunlap9e37bd32006-06-25 05:49:19 -0700201 * @threadfn: the function to run until signal_pending(current).
202 * @data: data ptr for @threadfn.
Eric Dumazet207205a2011-03-22 16:30:44 -0700203 * @node: memory node number.
Randy Dunlap9e37bd32006-06-25 05:49:19 -0700204 * @namefmt: printf-style name for the thread.
205 *
206 * Description: This helper function creates and names a kernel
207 * thread. The thread will be stopped: use wake_up_process() to start
Anton Blanchard301ba042010-02-09 15:07:40 +1100208 * it. See also kthread_run().
Randy Dunlap9e37bd32006-06-25 05:49:19 -0700209 *
Eric Dumazet207205a2011-03-22 16:30:44 -0700210 * If thread is going to be bound on a particular cpu, give its node
211 * in @node, to get NUMA affinity for kthread stack, or else give -1.
Randy Dunlap9e37bd32006-06-25 05:49:19 -0700212 * When woken, the thread will run @threadfn() with @data as its
Robert P. J. Day72fd4a32007-02-10 01:45:59 -0800213 * argument. @threadfn() can either call do_exit() directly if it is a
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300214 * standalone thread for which no one will call kthread_stop(), or
Randy Dunlap9e37bd32006-06-25 05:49:19 -0700215 * return when 'kthread_should_stop()' is true (which means
216 * kthread_stop() has been called). The return value should be zero
217 * or a negative error number; it will be passed to kthread_stop().
218 *
219 * Returns a task_struct or ERR_PTR(-ENOMEM).
220 */
Eric Dumazet207205a2011-03-22 16:30:44 -0700221struct task_struct *kthread_create_on_node(int (*threadfn)(void *data),
Thomas Gleixner2a1d4462012-07-16 10:42:36 +0000222 void *data, int node,
Eric Dumazet207205a2011-03-22 16:30:44 -0700223 const char namefmt[],
224 ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225{
226 struct kthread_create_info create;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227
228 create.threadfn = threadfn;
229 create.data = data;
Eric Dumazet207205a2011-03-22 16:30:44 -0700230 create.node = node;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 init_completion(&create.done);
232
Eric W. Biederman73c27992007-05-09 02:34:32 -0700233 spin_lock(&kthread_create_lock);
234 list_add_tail(&create.list, &kthread_create_list);
Eric W. Biederman73c27992007-05-09 02:34:32 -0700235 spin_unlock(&kthread_create_lock);
236
Dmitry Adamushkocbd9b672008-04-29 00:59:23 -0700237 wake_up_process(kthreadd_task);
Eric W. Biederman73c27992007-05-09 02:34:32 -0700238 wait_for_completion(&create.done);
239
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 if (!IS_ERR(create.result)) {
Peter Zijlstrac9b5f502011-01-07 13:41:40 +0100241 static const struct sched_param param = { .sched_priority = 0 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 va_list args;
Oleg Nesterov1c993152009-04-09 09:50:36 -0600243
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 va_start(args, namefmt);
245 vsnprintf(create.result->comm, sizeof(create.result->comm),
246 namefmt, args);
247 va_end(args);
Oleg Nesterov1c993152009-04-09 09:50:36 -0600248 /*
249 * root may have changed our (kthreadd's) priority or CPU mask.
250 * The kernel thread should not inherit these properties.
251 */
252 sched_setscheduler_nocheck(create.result, SCHED_NORMAL, &param);
Oleg Nesterov1c993152009-04-09 09:50:36 -0600253 set_cpus_allowed_ptr(create.result, cpu_all_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 return create.result;
256}
Eric Dumazet207205a2011-03-22 16:30:44 -0700257EXPORT_SYMBOL(kthread_create_on_node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258
Thomas Gleixnerf2530dc2013-04-09 09:33:34 +0200259static void __kthread_bind(struct task_struct *p, unsigned int cpu, long state)
Thomas Gleixner2a1d4462012-07-16 10:42:36 +0000260{
Thomas Gleixnerf2530dc2013-04-09 09:33:34 +0200261 /* Must have done schedule() in kthread() before we set_task_cpu */
262 if (!wait_task_inactive(p, state)) {
263 WARN_ON(1);
264 return;
265 }
Thomas Gleixner2a1d4462012-07-16 10:42:36 +0000266 /* It's safe because the task is inactive. */
267 do_set_cpus_allowed(p, cpumask_of(cpu));
268 p->flags |= PF_THREAD_BOUND;
269}
270
Randy Dunlap9e37bd32006-06-25 05:49:19 -0700271/**
Peter Zijlstra881232b2009-12-16 18:04:39 +0100272 * kthread_bind - bind a just-created kthread to a cpu.
273 * @p: thread created by kthread_create().
274 * @cpu: cpu (might not be online, must be possible) for @k to run on.
275 *
276 * Description: This function is equivalent to set_cpus_allowed(),
277 * except that @cpu doesn't need to be online, and the thread must be
278 * stopped (i.e., just returned from kthread_create()).
279 */
280void kthread_bind(struct task_struct *p, unsigned int cpu)
281{
Thomas Gleixnerf2530dc2013-04-09 09:33:34 +0200282 __kthread_bind(p, cpu, TASK_UNINTERRUPTIBLE);
Peter Zijlstra881232b2009-12-16 18:04:39 +0100283}
284EXPORT_SYMBOL(kthread_bind);
285
286/**
Thomas Gleixner2a1d4462012-07-16 10:42:36 +0000287 * kthread_create_on_cpu - Create a cpu bound kthread
288 * @threadfn: the function to run until signal_pending(current).
289 * @data: data ptr for @threadfn.
290 * @cpu: The cpu on which the thread should be bound,
291 * @namefmt: printf-style name for the thread. Format is restricted
292 * to "name.*%u". Code fills in cpu number.
293 *
294 * Description: This helper function creates and names a kernel thread
295 * The thread will be woken and put into park mode.
296 */
297struct task_struct *kthread_create_on_cpu(int (*threadfn)(void *data),
298 void *data, unsigned int cpu,
299 const char *namefmt)
300{
301 struct task_struct *p;
302
303 p = kthread_create_on_node(threadfn, data, cpu_to_node(cpu), namefmt,
304 cpu);
305 if (IS_ERR(p))
306 return p;
307 set_bit(KTHREAD_IS_PER_CPU, &to_kthread(p)->flags);
308 to_kthread(p)->cpu = cpu;
309 /* Park the thread to get it out of TASK_UNINTERRUPTIBLE state */
310 kthread_park(p);
311 return p;
312}
313
314static struct kthread *task_get_live_kthread(struct task_struct *k)
315{
316 struct kthread *kthread;
317
318 get_task_struct(k);
319 kthread = to_kthread(k);
320 /* It might have exited */
321 barrier();
322 if (k->vfork_done != NULL)
323 return kthread;
324 return NULL;
325}
326
Thomas Gleixnerf2530dc2013-04-09 09:33:34 +0200327static void __kthread_unpark(struct task_struct *k, struct kthread *kthread)
328{
329 clear_bit(KTHREAD_SHOULD_PARK, &kthread->flags);
330 /*
331 * We clear the IS_PARKED bit here as we don't wait
332 * until the task has left the park code. So if we'd
333 * park before that happens we'd see the IS_PARKED bit
334 * which might be about to be cleared.
335 */
336 if (test_and_clear_bit(KTHREAD_IS_PARKED, &kthread->flags)) {
337 if (test_bit(KTHREAD_IS_PER_CPU, &kthread->flags))
338 __kthread_bind(k, kthread->cpu, TASK_PARKED);
339 wake_up_state(k, TASK_PARKED);
340 }
341}
342
Thomas Gleixner2a1d4462012-07-16 10:42:36 +0000343/**
344 * kthread_unpark - unpark a thread created by kthread_create().
345 * @k: thread created by kthread_create().
346 *
347 * Sets kthread_should_park() for @k to return false, wakes it, and
348 * waits for it to return. If the thread is marked percpu then its
349 * bound to the cpu again.
350 */
351void kthread_unpark(struct task_struct *k)
352{
353 struct kthread *kthread = task_get_live_kthread(k);
354
Thomas Gleixnerf2530dc2013-04-09 09:33:34 +0200355 if (kthread)
356 __kthread_unpark(k, kthread);
Thomas Gleixner2a1d4462012-07-16 10:42:36 +0000357 put_task_struct(k);
358}
359
360/**
361 * kthread_park - park a thread created by kthread_create().
362 * @k: thread created by kthread_create().
363 *
364 * Sets kthread_should_park() for @k to return true, wakes it, and
365 * waits for it to return. This can also be called after kthread_create()
366 * instead of calling wake_up_process(): the thread will park without
367 * calling threadfn().
368 *
369 * Returns 0 if the thread is parked, -ENOSYS if the thread exited.
370 * If called by the kthread itself just the park bit is set.
371 */
372int kthread_park(struct task_struct *k)
373{
374 struct kthread *kthread = task_get_live_kthread(k);
375 int ret = -ENOSYS;
376
377 if (kthread) {
378 if (!test_bit(KTHREAD_IS_PARKED, &kthread->flags)) {
379 set_bit(KTHREAD_SHOULD_PARK, &kthread->flags);
380 if (k != current) {
381 wake_up_process(k);
382 wait_for_completion(&kthread->parked);
383 }
384 }
385 ret = 0;
386 }
387 put_task_struct(k);
388 return ret;
389}
390
391/**
Randy Dunlap9e37bd32006-06-25 05:49:19 -0700392 * kthread_stop - stop a thread created by kthread_create().
393 * @k: thread created by kthread_create().
394 *
395 * Sets kthread_should_stop() for @k to return true, wakes it, and
Oleg Nesterov9ae26022009-06-19 02:51:13 +0200396 * waits for it to exit. This can also be called after kthread_create()
397 * instead of calling wake_up_process(): the thread will exit without
398 * calling threadfn().
399 *
400 * If threadfn() may call do_exit() itself, the caller must ensure
401 * task_struct can't go away.
Randy Dunlap9e37bd32006-06-25 05:49:19 -0700402 *
403 * Returns the result of threadfn(), or %-EINTR if wake_up_process()
404 * was never called.
405 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406int kthread_stop(struct task_struct *k)
407{
Thomas Gleixner2a1d4462012-07-16 10:42:36 +0000408 struct kthread *kthread = task_get_live_kthread(k);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 int ret;
410
Oleg Nesterov63706172009-06-17 16:27:45 -0700411 trace_sched_kthread_stop(k);
Thomas Gleixner2a1d4462012-07-16 10:42:36 +0000412 if (kthread) {
413 set_bit(KTHREAD_SHOULD_STOP, &kthread->flags);
Thomas Gleixnerf2530dc2013-04-09 09:33:34 +0200414 __kthread_unpark(k, kthread);
Oleg Nesterov63706172009-06-17 16:27:45 -0700415 wake_up_process(k);
416 wait_for_completion(&kthread->exited);
417 }
418 ret = k->exit_code;
Mathieu Desnoyers0a16b602008-07-18 12:16:17 -0400419
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 put_task_struct(k);
Mathieu Desnoyers0a16b602008-07-18 12:16:17 -0400421 trace_sched_kthread_stop_ret(ret);
422
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 return ret;
424}
Adrian Bunk52e92e52006-07-14 00:24:05 -0700425EXPORT_SYMBOL(kthread_stop);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426
Satyam Sharmae804a4a2007-07-31 00:39:16 -0700427int kthreadd(void *unused)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428{
Eric W. Biederman73c27992007-05-09 02:34:32 -0700429 struct task_struct *tsk = current;
Eric W. Biederman73c27992007-05-09 02:34:32 -0700430
Satyam Sharmae804a4a2007-07-31 00:39:16 -0700431 /* Setup a clean context for our children to inherit. */
Eric W. Biederman73c27992007-05-09 02:34:32 -0700432 set_task_comm(tsk, "kthreadd");
Oleg Nesterov10ab8252007-05-09 02:34:37 -0700433 ignore_signals(tsk);
Rusty Russell1a2142a2009-03-30 22:05:10 -0600434 set_cpus_allowed_ptr(tsk, cpu_all_mask);
Lai Jiangshanaee4faa2012-12-12 13:51:39 -0800435 set_mems_allowed(node_states[N_MEMORY]);
Eric W. Biederman73c27992007-05-09 02:34:32 -0700436
Tejun Heo34b087e2011-11-23 09:28:17 -0800437 current->flags |= PF_NOFREEZE;
Eric W. Biederman73c27992007-05-09 02:34:32 -0700438
439 for (;;) {
440 set_current_state(TASK_INTERRUPTIBLE);
441 if (list_empty(&kthread_create_list))
442 schedule();
443 __set_current_state(TASK_RUNNING);
444
445 spin_lock(&kthread_create_lock);
446 while (!list_empty(&kthread_create_list)) {
447 struct kthread_create_info *create;
448
449 create = list_entry(kthread_create_list.next,
450 struct kthread_create_info, list);
451 list_del_init(&create->list);
452 spin_unlock(&kthread_create_lock);
453
454 create_kthread(create);
455
456 spin_lock(&kthread_create_lock);
457 }
458 spin_unlock(&kthread_create_lock);
459 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460
461 return 0;
462}
Tejun Heob56c0d82010-06-29 10:07:09 +0200463
Yong Zhang4f32e9b2010-12-22 10:27:53 +0100464void __init_kthread_worker(struct kthread_worker *worker,
465 const char *name,
466 struct lock_class_key *key)
467{
468 spin_lock_init(&worker->lock);
469 lockdep_set_class_and_name(&worker->lock, key, name);
470 INIT_LIST_HEAD(&worker->work_list);
471 worker->task = NULL;
472}
473EXPORT_SYMBOL_GPL(__init_kthread_worker);
474
Tejun Heob56c0d82010-06-29 10:07:09 +0200475/**
476 * kthread_worker_fn - kthread function to process kthread_worker
477 * @worker_ptr: pointer to initialized kthread_worker
478 *
479 * This function can be used as @threadfn to kthread_create() or
480 * kthread_run() with @worker_ptr argument pointing to an initialized
481 * kthread_worker. The started kthread will process work_list until
482 * the it is stopped with kthread_stop(). A kthread can also call
483 * this function directly after extra initialization.
484 *
485 * Different kthreads can be used for the same kthread_worker as long
486 * as there's only one kthread attached to it at any given time. A
487 * kthread_worker without an attached kthread simply collects queued
488 * kthread_works.
489 */
490int kthread_worker_fn(void *worker_ptr)
491{
492 struct kthread_worker *worker = worker_ptr;
493 struct kthread_work *work;
494
495 WARN_ON(worker->task);
496 worker->task = current;
497repeat:
498 set_current_state(TASK_INTERRUPTIBLE); /* mb paired w/ kthread_stop */
499
500 if (kthread_should_stop()) {
501 __set_current_state(TASK_RUNNING);
502 spin_lock_irq(&worker->lock);
503 worker->task = NULL;
504 spin_unlock_irq(&worker->lock);
505 return 0;
506 }
507
508 work = NULL;
509 spin_lock_irq(&worker->lock);
510 if (!list_empty(&worker->work_list)) {
511 work = list_first_entry(&worker->work_list,
512 struct kthread_work, node);
513 list_del_init(&work->node);
514 }
Tejun Heo46f3d972012-07-19 13:52:53 -0700515 worker->current_work = work;
Tejun Heob56c0d82010-06-29 10:07:09 +0200516 spin_unlock_irq(&worker->lock);
517
518 if (work) {
519 __set_current_state(TASK_RUNNING);
520 work->func(work);
Tejun Heob56c0d82010-06-29 10:07:09 +0200521 } else if (!freezing(current))
522 schedule();
523
524 try_to_freeze();
525 goto repeat;
526}
527EXPORT_SYMBOL_GPL(kthread_worker_fn);
528
Tejun Heo9a2e03d2012-07-19 13:52:53 -0700529/* insert @work before @pos in @worker */
530static void insert_kthread_work(struct kthread_worker *worker,
531 struct kthread_work *work,
532 struct list_head *pos)
533{
534 lockdep_assert_held(&worker->lock);
535
536 list_add_tail(&work->node, pos);
Tejun Heo46f3d972012-07-19 13:52:53 -0700537 work->worker = worker;
Tejun Heo9a2e03d2012-07-19 13:52:53 -0700538 if (likely(worker->task))
539 wake_up_process(worker->task);
540}
541
Tejun Heob56c0d82010-06-29 10:07:09 +0200542/**
543 * queue_kthread_work - queue a kthread_work
544 * @worker: target kthread_worker
545 * @work: kthread_work to queue
546 *
547 * Queue @work to work processor @task for async execution. @task
548 * must have been created with kthread_worker_create(). Returns %true
549 * if @work was successfully queued, %false if it was already pending.
550 */
551bool queue_kthread_work(struct kthread_worker *worker,
552 struct kthread_work *work)
553{
554 bool ret = false;
555 unsigned long flags;
556
557 spin_lock_irqsave(&worker->lock, flags);
558 if (list_empty(&work->node)) {
Tejun Heo9a2e03d2012-07-19 13:52:53 -0700559 insert_kthread_work(worker, work, &worker->work_list);
Tejun Heob56c0d82010-06-29 10:07:09 +0200560 ret = true;
561 }
562 spin_unlock_irqrestore(&worker->lock, flags);
563 return ret;
564}
565EXPORT_SYMBOL_GPL(queue_kthread_work);
566
Tejun Heo9a2e03d2012-07-19 13:52:53 -0700567struct kthread_flush_work {
568 struct kthread_work work;
569 struct completion done;
570};
571
572static void kthread_flush_work_fn(struct kthread_work *work)
573{
574 struct kthread_flush_work *fwork =
575 container_of(work, struct kthread_flush_work, work);
576 complete(&fwork->done);
577}
578
Tejun Heob56c0d82010-06-29 10:07:09 +0200579/**
580 * flush_kthread_work - flush a kthread_work
581 * @work: work to flush
582 *
583 * If @work is queued or executing, wait for it to finish execution.
584 */
585void flush_kthread_work(struct kthread_work *work)
586{
Tejun Heo46f3d972012-07-19 13:52:53 -0700587 struct kthread_flush_work fwork = {
588 KTHREAD_WORK_INIT(fwork.work, kthread_flush_work_fn),
589 COMPLETION_INITIALIZER_ONSTACK(fwork.done),
590 };
591 struct kthread_worker *worker;
592 bool noop = false;
Tejun Heob56c0d82010-06-29 10:07:09 +0200593
Tejun Heo46f3d972012-07-19 13:52:53 -0700594retry:
595 worker = work->worker;
596 if (!worker)
597 return;
Tejun Heob56c0d82010-06-29 10:07:09 +0200598
Tejun Heo46f3d972012-07-19 13:52:53 -0700599 spin_lock_irq(&worker->lock);
600 if (work->worker != worker) {
601 spin_unlock_irq(&worker->lock);
602 goto retry;
603 }
Tejun Heob56c0d82010-06-29 10:07:09 +0200604
Tejun Heo46f3d972012-07-19 13:52:53 -0700605 if (!list_empty(&work->node))
606 insert_kthread_work(worker, &fwork.work, work->node.next);
607 else if (worker->current_work == work)
608 insert_kthread_work(worker, &fwork.work, worker->work_list.next);
609 else
610 noop = true;
Tejun Heob56c0d82010-06-29 10:07:09 +0200611
Tejun Heo46f3d972012-07-19 13:52:53 -0700612 spin_unlock_irq(&worker->lock);
613
614 if (!noop)
615 wait_for_completion(&fwork.done);
Tejun Heob56c0d82010-06-29 10:07:09 +0200616}
617EXPORT_SYMBOL_GPL(flush_kthread_work);
618
Tejun Heob56c0d82010-06-29 10:07:09 +0200619/**
620 * flush_kthread_worker - flush all current works on a kthread_worker
621 * @worker: worker to flush
622 *
623 * Wait until all currently executing or pending works on @worker are
624 * finished.
625 */
626void flush_kthread_worker(struct kthread_worker *worker)
627{
628 struct kthread_flush_work fwork = {
629 KTHREAD_WORK_INIT(fwork.work, kthread_flush_work_fn),
630 COMPLETION_INITIALIZER_ONSTACK(fwork.done),
631 };
632
633 queue_kthread_work(worker, &fwork.work);
634 wait_for_completion(&fwork.done);
635}
636EXPORT_SYMBOL_GPL(flush_kthread_worker);