blob: 3b72b80dd0f9a9a749937679716a832eb662c5fb [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>
Steven Rostedtad8d75f2009-04-14 19:39:12 -040019#include <trace/events/sched.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020
Eric W. Biederman73c27992007-05-09 02:34:32 -070021static DEFINE_SPINLOCK(kthread_create_lock);
22static LIST_HEAD(kthread_create_list);
23struct task_struct *kthreadd_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -070024
25struct kthread_create_info
26{
Eric W. Biederman73c27992007-05-09 02:34:32 -070027 /* Information passed to kthread() from kthreadd. */
Linus Torvalds1da177e2005-04-16 15:20:36 -070028 int (*threadfn)(void *data);
29 void *data;
Eric Dumazet207205a2011-03-22 16:30:44 -070030 int node;
Linus Torvalds1da177e2005-04-16 15:20:36 -070031
Eric W. Biederman73c27992007-05-09 02:34:32 -070032 /* Result passed back to kthread_create() from kthreadd. */
Linus Torvalds1da177e2005-04-16 15:20:36 -070033 struct task_struct *result;
34 struct completion done;
David Howells65f27f32006-11-22 14:55:48 +000035
Eric W. Biederman73c27992007-05-09 02:34:32 -070036 struct list_head list;
Linus Torvalds1da177e2005-04-16 15:20:36 -070037};
38
Oleg Nesterov63706172009-06-17 16:27:45 -070039struct kthread {
Thomas Gleixner20bdb6e2012-07-16 10:42:36 +000040 unsigned long flags;
41 unsigned int cpu;
Tejun Heo82805ab2010-06-29 10:07:09 +020042 void *data;
Thomas Gleixner20bdb6e2012-07-16 10:42:36 +000043 struct completion parked;
Oleg Nesterov63706172009-06-17 16:27:45 -070044 struct completion exited;
Linus Torvalds1da177e2005-04-16 15:20:36 -070045};
46
Thomas Gleixner20bdb6e2012-07-16 10:42:36 +000047enum KTHREAD_BITS {
48 KTHREAD_IS_PER_CPU = 0,
49 KTHREAD_SHOULD_STOP,
50 KTHREAD_SHOULD_PARK,
51 KTHREAD_IS_PARKED,
52};
53
Oleg Nesterov63706172009-06-17 16:27:45 -070054#define to_kthread(tsk) \
55 container_of((tsk)->vfork_done, struct kthread, exited)
Linus Torvalds1da177e2005-04-16 15:20:36 -070056
Randy Dunlap9e37bd32006-06-25 05:49:19 -070057/**
58 * kthread_should_stop - should this kthread return now?
59 *
Robert P. J. Day72fd4a32007-02-10 01:45:59 -080060 * When someone calls kthread_stop() on your kthread, it will be woken
Randy Dunlap9e37bd32006-06-25 05:49:19 -070061 * and this will return true. You should then return, and your return
62 * value will be passed through to kthread_stop().
63 */
Thomas Gleixner20bdb6e2012-07-16 10:42:36 +000064bool kthread_should_stop(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070065{
Thomas Gleixner20bdb6e2012-07-16 10:42:36 +000066 return test_bit(KTHREAD_SHOULD_STOP, &to_kthread(current)->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -070067}
68EXPORT_SYMBOL(kthread_should_stop);
69
Tejun Heo82805ab2010-06-29 10:07:09 +020070/**
Thomas Gleixner20bdb6e2012-07-16 10:42:36 +000071 * kthread_should_park - should this kthread park now?
72 *
73 * When someone calls kthread_park() on your kthread, it will be woken
74 * and this will return true. You should then do the necessary
75 * cleanup and call kthread_parkme()
76 *
77 * Similar to kthread_should_stop(), but this keeps the thread alive
78 * and in a park position. kthread_unpark() "restarts" the thread and
79 * calls the thread function again.
80 */
81bool kthread_should_park(void)
82{
83 return test_bit(KTHREAD_SHOULD_PARK, &to_kthread(current)->flags);
84}
85
86/**
Tejun Heo8a32c442011-11-21 12:32:23 -080087 * kthread_freezable_should_stop - should this freezable kthread return now?
88 * @was_frozen: optional out parameter, indicates whether %current was frozen
89 *
90 * kthread_should_stop() for freezable kthreads, which will enter
91 * refrigerator if necessary. This function is safe from kthread_stop() /
92 * freezer deadlock and freezable kthreads should use this function instead
93 * of calling try_to_freeze() directly.
94 */
95bool kthread_freezable_should_stop(bool *was_frozen)
96{
97 bool frozen = false;
98
99 might_sleep();
100
101 if (unlikely(freezing(current)))
102 frozen = __refrigerator(true);
103
104 if (was_frozen)
105 *was_frozen = frozen;
106
107 return kthread_should_stop();
108}
109EXPORT_SYMBOL_GPL(kthread_freezable_should_stop);
110
111/**
Tejun Heo82805ab2010-06-29 10:07:09 +0200112 * kthread_data - return data value specified on kthread creation
113 * @task: kthread task in question
114 *
115 * Return the data value specified when kthread @task was created.
116 * The caller is responsible for ensuring the validity of @task when
117 * calling this function.
118 */
119void *kthread_data(struct task_struct *task)
120{
121 return to_kthread(task)->data;
122}
123
Thomas Gleixner20bdb6e2012-07-16 10:42:36 +0000124static void __kthread_parkme(struct kthread *self)
125{
Thomas Gleixnerbb32d262013-04-09 09:33:34 +0200126 __set_current_state(TASK_PARKED);
Thomas Gleixner20bdb6e2012-07-16 10:42:36 +0000127 while (test_bit(KTHREAD_SHOULD_PARK, &self->flags)) {
128 if (!test_and_set_bit(KTHREAD_IS_PARKED, &self->flags))
129 complete(&self->parked);
130 schedule();
Thomas Gleixnerbb32d262013-04-09 09:33:34 +0200131 __set_current_state(TASK_PARKED);
Thomas Gleixner20bdb6e2012-07-16 10:42:36 +0000132 }
133 clear_bit(KTHREAD_IS_PARKED, &self->flags);
134 __set_current_state(TASK_RUNNING);
135}
136
137void kthread_parkme(void)
138{
139 __kthread_parkme(to_kthread(current));
140}
141
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142static int kthread(void *_create)
143{
Eric W. Biederman73c27992007-05-09 02:34:32 -0700144 /* Copy data: it's on kthread's stack */
Oleg Nesterov63706172009-06-17 16:27:45 -0700145 struct kthread_create_info *create = _create;
146 int (*threadfn)(void *data) = create->threadfn;
147 void *data = create->data;
148 struct kthread self;
149 int ret;
150
Thomas Gleixner20bdb6e2012-07-16 10:42:36 +0000151 self.flags = 0;
Tejun Heo82805ab2010-06-29 10:07:09 +0200152 self.data = data;
Oleg Nesterov63706172009-06-17 16:27:45 -0700153 init_completion(&self.exited);
Thomas Gleixner20bdb6e2012-07-16 10:42:36 +0000154 init_completion(&self.parked);
Oleg Nesterov63706172009-06-17 16:27:45 -0700155 current->vfork_done = &self.exited;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 /* OK, tell user we're spawned, wait for stop or wakeup */
Oleg Nesterova076e4b2007-05-23 13:57:27 -0700158 __set_current_state(TASK_UNINTERRUPTIBLE);
Vitaliy Gusev3217ab92009-04-09 09:50:35 -0600159 create->result = current;
Oleg Nesterovcdd140b2009-06-17 16:27:43 -0700160 complete(&create->done);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 schedule();
162
Oleg Nesterov63706172009-06-17 16:27:45 -0700163 ret = -EINTR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164
Thomas Gleixner20bdb6e2012-07-16 10:42:36 +0000165 if (!test_bit(KTHREAD_SHOULD_STOP, &self.flags)) {
166 __kthread_parkme(&self);
167 ret = threadfn(data);
168 }
Oleg Nesterov63706172009-06-17 16:27:45 -0700169 /* we can't just return, we must preserve "self" on stack */
170 do_exit(ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171}
172
Eric Dumazet207205a2011-03-22 16:30:44 -0700173/* called from do_fork() to get node information for about to be created task */
174int tsk_fork_get_node(struct task_struct *tsk)
175{
176#ifdef CONFIG_NUMA
177 if (tsk == kthreadd_task)
178 return tsk->pref_node_fork;
179#endif
180 return numa_node_id();
181}
182
Eric W. Biederman73c27992007-05-09 02:34:32 -0700183static void create_kthread(struct kthread_create_info *create)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 int pid;
186
Eric Dumazet207205a2011-03-22 16:30:44 -0700187#ifdef CONFIG_NUMA
188 current->pref_node_fork = create->node;
189#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 /* We want our own signal handler (we take no signals by default). */
191 pid = kernel_thread(kthread, create, CLONE_FS | CLONE_FILES | SIGCHLD);
Oleg Nesterovcdd140b2009-06-17 16:27:43 -0700192 if (pid < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 create->result = ERR_PTR(pid);
Oleg Nesterovcdd140b2009-06-17 16:27:43 -0700194 complete(&create->done);
195 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196}
197
Randy Dunlap9e37bd32006-06-25 05:49:19 -0700198/**
Eric Dumazet207205a2011-03-22 16:30:44 -0700199 * kthread_create_on_node - create a kthread.
Randy Dunlap9e37bd32006-06-25 05:49:19 -0700200 * @threadfn: the function to run until signal_pending(current).
201 * @data: data ptr for @threadfn.
Eric Dumazet207205a2011-03-22 16:30:44 -0700202 * @node: memory node number.
Randy Dunlap9e37bd32006-06-25 05:49:19 -0700203 * @namefmt: printf-style name for the thread.
204 *
205 * Description: This helper function creates and names a kernel
206 * thread. The thread will be stopped: use wake_up_process() to start
Anton Blanchard301ba042010-02-09 15:07:40 +1100207 * it. See also kthread_run().
Randy Dunlap9e37bd32006-06-25 05:49:19 -0700208 *
Eric Dumazet207205a2011-03-22 16:30:44 -0700209 * If thread is going to be bound on a particular cpu, give its node
210 * in @node, to get NUMA affinity for kthread stack, or else give -1.
Randy Dunlap9e37bd32006-06-25 05:49:19 -0700211 * When woken, the thread will run @threadfn() with @data as its
Robert P. J. Day72fd4a32007-02-10 01:45:59 -0800212 * argument. @threadfn() can either call do_exit() directly if it is a
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300213 * standalone thread for which no one will call kthread_stop(), or
Randy Dunlap9e37bd32006-06-25 05:49:19 -0700214 * return when 'kthread_should_stop()' is true (which means
215 * kthread_stop() has been called). The return value should be zero
216 * or a negative error number; it will be passed to kthread_stop().
217 *
218 * Returns a task_struct or ERR_PTR(-ENOMEM).
219 */
Eric Dumazet207205a2011-03-22 16:30:44 -0700220struct task_struct *kthread_create_on_node(int (*threadfn)(void *data),
Thomas Gleixner20bdb6e2012-07-16 10:42:36 +0000221 void *data, int node,
Eric Dumazet207205a2011-03-22 16:30:44 -0700222 const char namefmt[],
223 ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224{
225 struct kthread_create_info create;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226
227 create.threadfn = threadfn;
228 create.data = data;
Eric Dumazet207205a2011-03-22 16:30:44 -0700229 create.node = node;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 init_completion(&create.done);
231
Eric W. Biederman73c27992007-05-09 02:34:32 -0700232 spin_lock(&kthread_create_lock);
233 list_add_tail(&create.list, &kthread_create_list);
Eric W. Biederman73c27992007-05-09 02:34:32 -0700234 spin_unlock(&kthread_create_lock);
235
Dmitry Adamushkocbd9b672008-04-29 00:59:23 -0700236 wake_up_process(kthreadd_task);
Eric W. Biederman73c27992007-05-09 02:34:32 -0700237 wait_for_completion(&create.done);
238
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 if (!IS_ERR(create.result)) {
Peter Zijlstrac9b5f502011-01-07 13:41:40 +0100240 static const struct sched_param param = { .sched_priority = 0 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 va_list args;
Oleg Nesterov1c993152009-04-09 09:50:36 -0600242
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 va_start(args, namefmt);
244 vsnprintf(create.result->comm, sizeof(create.result->comm),
245 namefmt, args);
246 va_end(args);
Oleg Nesterov1c993152009-04-09 09:50:36 -0600247 /*
248 * root may have changed our (kthreadd's) priority or CPU mask.
249 * The kernel thread should not inherit these properties.
250 */
251 sched_setscheduler_nocheck(create.result, SCHED_NORMAL, &param);
Oleg Nesterov1c993152009-04-09 09:50:36 -0600252 set_cpus_allowed_ptr(create.result, cpu_all_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 return create.result;
255}
Eric Dumazet207205a2011-03-22 16:30:44 -0700256EXPORT_SYMBOL(kthread_create_on_node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257
Thomas Gleixnerbb32d262013-04-09 09:33:34 +0200258static void __kthread_bind(struct task_struct *p, unsigned int cpu, long state)
Thomas Gleixner20bdb6e2012-07-16 10:42:36 +0000259{
Thomas Gleixnerbb32d262013-04-09 09:33:34 +0200260 /* Must have done schedule() in kthread() before we set_task_cpu */
261 if (!wait_task_inactive(p, state)) {
262 WARN_ON(1);
263 return;
264 }
Thomas Gleixner20bdb6e2012-07-16 10:42:36 +0000265 /* It's safe because the task is inactive. */
266 do_set_cpus_allowed(p, cpumask_of(cpu));
267 p->flags |= PF_THREAD_BOUND;
268}
269
Randy Dunlap9e37bd32006-06-25 05:49:19 -0700270/**
Peter Zijlstra881232b2009-12-16 18:04:39 +0100271 * kthread_bind - bind a just-created kthread to a cpu.
272 * @p: thread created by kthread_create().
273 * @cpu: cpu (might not be online, must be possible) for @k to run on.
274 *
275 * Description: This function is equivalent to set_cpus_allowed(),
276 * except that @cpu doesn't need to be online, and the thread must be
277 * stopped (i.e., just returned from kthread_create()).
278 */
279void kthread_bind(struct task_struct *p, unsigned int cpu)
280{
Thomas Gleixnerbb32d262013-04-09 09:33:34 +0200281 __kthread_bind(p, cpu, TASK_UNINTERRUPTIBLE);
Peter Zijlstra881232b2009-12-16 18:04:39 +0100282}
283EXPORT_SYMBOL(kthread_bind);
284
285/**
Thomas Gleixner20bdb6e2012-07-16 10:42:36 +0000286 * kthread_create_on_cpu - Create a cpu bound kthread
287 * @threadfn: the function to run until signal_pending(current).
288 * @data: data ptr for @threadfn.
289 * @cpu: The cpu on which the thread should be bound,
290 * @namefmt: printf-style name for the thread. Format is restricted
291 * to "name.*%u". Code fills in cpu number.
292 *
293 * Description: This helper function creates and names a kernel thread
294 * The thread will be woken and put into park mode.
295 */
296struct task_struct *kthread_create_on_cpu(int (*threadfn)(void *data),
297 void *data, unsigned int cpu,
298 const char *namefmt)
299{
300 struct task_struct *p;
301
302 p = kthread_create_on_node(threadfn, data, cpu_to_node(cpu), namefmt,
303 cpu);
304 if (IS_ERR(p))
305 return p;
306 set_bit(KTHREAD_IS_PER_CPU, &to_kthread(p)->flags);
307 to_kthread(p)->cpu = cpu;
308 /* Park the thread to get it out of TASK_UNINTERRUPTIBLE state */
309 kthread_park(p);
310 return p;
311}
312
313static struct kthread *task_get_live_kthread(struct task_struct *k)
314{
315 struct kthread *kthread;
316
317 get_task_struct(k);
318 kthread = to_kthread(k);
319 /* It might have exited */
320 barrier();
321 if (k->vfork_done != NULL)
322 return kthread;
323 return NULL;
324}
325
Thomas Gleixnerbb32d262013-04-09 09:33:34 +0200326static void __kthread_unpark(struct task_struct *k, struct kthread *kthread)
327{
328 clear_bit(KTHREAD_SHOULD_PARK, &kthread->flags);
329 /*
330 * We clear the IS_PARKED bit here as we don't wait
331 * until the task has left the park code. So if we'd
332 * park before that happens we'd see the IS_PARKED bit
333 * which might be about to be cleared.
334 */
335 if (test_and_clear_bit(KTHREAD_IS_PARKED, &kthread->flags)) {
336 if (test_bit(KTHREAD_IS_PER_CPU, &kthread->flags))
337 __kthread_bind(k, kthread->cpu, TASK_PARKED);
338 wake_up_state(k, TASK_PARKED);
339 }
340}
341
Thomas Gleixner20bdb6e2012-07-16 10:42:36 +0000342/**
343 * kthread_unpark - unpark a thread created by kthread_create().
344 * @k: thread created by kthread_create().
345 *
346 * Sets kthread_should_park() for @k to return false, wakes it, and
347 * waits for it to return. If the thread is marked percpu then its
348 * bound to the cpu again.
349 */
350void kthread_unpark(struct task_struct *k)
351{
352 struct kthread *kthread = task_get_live_kthread(k);
353
Thomas Gleixnerbb32d262013-04-09 09:33:34 +0200354 if (kthread)
355 __kthread_unpark(k, kthread);
Thomas Gleixner20bdb6e2012-07-16 10:42:36 +0000356 put_task_struct(k);
357}
358
359/**
360 * kthread_park - park a thread created by kthread_create().
361 * @k: thread created by kthread_create().
362 *
363 * Sets kthread_should_park() for @k to return true, wakes it, and
364 * waits for it to return. This can also be called after kthread_create()
365 * instead of calling wake_up_process(): the thread will park without
366 * calling threadfn().
367 *
368 * Returns 0 if the thread is parked, -ENOSYS if the thread exited.
369 * If called by the kthread itself just the park bit is set.
370 */
371int kthread_park(struct task_struct *k)
372{
373 struct kthread *kthread = task_get_live_kthread(k);
374 int ret = -ENOSYS;
375
376 if (kthread) {
377 if (!test_bit(KTHREAD_IS_PARKED, &kthread->flags)) {
378 set_bit(KTHREAD_SHOULD_PARK, &kthread->flags);
379 if (k != current) {
380 wake_up_process(k);
381 wait_for_completion(&kthread->parked);
382 }
383 }
384 ret = 0;
385 }
386 put_task_struct(k);
387 return ret;
388}
389
390/**
Randy Dunlap9e37bd32006-06-25 05:49:19 -0700391 * kthread_stop - stop a thread created by kthread_create().
392 * @k: thread created by kthread_create().
393 *
394 * Sets kthread_should_stop() for @k to return true, wakes it, and
Oleg Nesterov9ae26022009-06-19 02:51:13 +0200395 * waits for it to exit. This can also be called after kthread_create()
396 * instead of calling wake_up_process(): the thread will exit without
397 * calling threadfn().
398 *
399 * If threadfn() may call do_exit() itself, the caller must ensure
400 * task_struct can't go away.
Randy Dunlap9e37bd32006-06-25 05:49:19 -0700401 *
402 * Returns the result of threadfn(), or %-EINTR if wake_up_process()
403 * was never called.
404 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405int kthread_stop(struct task_struct *k)
406{
Thomas Gleixner20bdb6e2012-07-16 10:42:36 +0000407 struct kthread *kthread = task_get_live_kthread(k);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 int ret;
409
Oleg Nesterov63706172009-06-17 16:27:45 -0700410 trace_sched_kthread_stop(k);
Thomas Gleixner20bdb6e2012-07-16 10:42:36 +0000411 if (kthread) {
412 set_bit(KTHREAD_SHOULD_STOP, &kthread->flags);
Thomas Gleixnerbb32d262013-04-09 09:33:34 +0200413 __kthread_unpark(k, kthread);
Oleg Nesterov63706172009-06-17 16:27:45 -0700414 wake_up_process(k);
415 wait_for_completion(&kthread->exited);
416 }
417 ret = k->exit_code;
Mathieu Desnoyers0a16b602008-07-18 12:16:17 -0400418
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 put_task_struct(k);
Mathieu Desnoyers0a16b602008-07-18 12:16:17 -0400420 trace_sched_kthread_stop_ret(ret);
421
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 return ret;
423}
Adrian Bunk52e92e52006-07-14 00:24:05 -0700424EXPORT_SYMBOL(kthread_stop);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425
Satyam Sharmae804a4a2007-07-31 00:39:16 -0700426int kthreadd(void *unused)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427{
Eric W. Biederman73c27992007-05-09 02:34:32 -0700428 struct task_struct *tsk = current;
Eric W. Biederman73c27992007-05-09 02:34:32 -0700429
Satyam Sharmae804a4a2007-07-31 00:39:16 -0700430 /* Setup a clean context for our children to inherit. */
Eric W. Biederman73c27992007-05-09 02:34:32 -0700431 set_task_comm(tsk, "kthreadd");
Oleg Nesterov10ab8252007-05-09 02:34:37 -0700432 ignore_signals(tsk);
Rusty Russell1a2142a2009-03-30 22:05:10 -0600433 set_cpus_allowed_ptr(tsk, cpu_all_mask);
Miao Xie5ab116c2010-03-23 13:35:34 -0700434 set_mems_allowed(node_states[N_HIGH_MEMORY]);
Eric W. Biederman73c27992007-05-09 02:34:32 -0700435
Tejun Heo34b087e2011-11-23 09:28:17 -0800436 current->flags |= PF_NOFREEZE;
Eric W. Biederman73c27992007-05-09 02:34:32 -0700437
438 for (;;) {
439 set_current_state(TASK_INTERRUPTIBLE);
440 if (list_empty(&kthread_create_list))
441 schedule();
442 __set_current_state(TASK_RUNNING);
443
444 spin_lock(&kthread_create_lock);
445 while (!list_empty(&kthread_create_list)) {
446 struct kthread_create_info *create;
447
448 create = list_entry(kthread_create_list.next,
449 struct kthread_create_info, list);
450 list_del_init(&create->list);
451 spin_unlock(&kthread_create_lock);
452
453 create_kthread(create);
454
455 spin_lock(&kthread_create_lock);
456 }
457 spin_unlock(&kthread_create_lock);
458 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459
460 return 0;
461}
Tejun Heob56c0d82010-06-29 10:07:09 +0200462
Yong Zhang4f32e9b2010-12-22 10:27:53 +0100463void __init_kthread_worker(struct kthread_worker *worker,
464 const char *name,
465 struct lock_class_key *key)
466{
467 spin_lock_init(&worker->lock);
468 lockdep_set_class_and_name(&worker->lock, key, name);
469 INIT_LIST_HEAD(&worker->work_list);
470 worker->task = NULL;
471}
472EXPORT_SYMBOL_GPL(__init_kthread_worker);
473
Tejun Heob56c0d82010-06-29 10:07:09 +0200474/**
475 * kthread_worker_fn - kthread function to process kthread_worker
476 * @worker_ptr: pointer to initialized kthread_worker
477 *
478 * This function can be used as @threadfn to kthread_create() or
479 * kthread_run() with @worker_ptr argument pointing to an initialized
480 * kthread_worker. The started kthread will process work_list until
481 * the it is stopped with kthread_stop(). A kthread can also call
482 * this function directly after extra initialization.
483 *
484 * Different kthreads can be used for the same kthread_worker as long
485 * as there's only one kthread attached to it at any given time. A
486 * kthread_worker without an attached kthread simply collects queued
487 * kthread_works.
488 */
489int kthread_worker_fn(void *worker_ptr)
490{
491 struct kthread_worker *worker = worker_ptr;
492 struct kthread_work *work;
493
494 WARN_ON(worker->task);
495 worker->task = current;
496repeat:
497 set_current_state(TASK_INTERRUPTIBLE); /* mb paired w/ kthread_stop */
498
499 if (kthread_should_stop()) {
500 __set_current_state(TASK_RUNNING);
501 spin_lock_irq(&worker->lock);
502 worker->task = NULL;
503 spin_unlock_irq(&worker->lock);
504 return 0;
505 }
506
507 work = NULL;
508 spin_lock_irq(&worker->lock);
509 if (!list_empty(&worker->work_list)) {
510 work = list_first_entry(&worker->work_list,
511 struct kthread_work, node);
512 list_del_init(&work->node);
513 }
Tejun Heo97ed5372012-07-19 13:52:53 -0700514 worker->current_work = work;
Tejun Heob56c0d82010-06-29 10:07:09 +0200515 spin_unlock_irq(&worker->lock);
516
517 if (work) {
518 __set_current_state(TASK_RUNNING);
519 work->func(work);
Tejun Heob56c0d82010-06-29 10:07:09 +0200520 } else if (!freezing(current))
521 schedule();
522
523 try_to_freeze();
524 goto repeat;
525}
526EXPORT_SYMBOL_GPL(kthread_worker_fn);
527
Tejun Heo34b65672012-07-19 13:52:53 -0700528/* insert @work before @pos in @worker */
529static void insert_kthread_work(struct kthread_worker *worker,
530 struct kthread_work *work,
531 struct list_head *pos)
532{
533 lockdep_assert_held(&worker->lock);
534
535 list_add_tail(&work->node, pos);
Tejun Heo97ed5372012-07-19 13:52:53 -0700536 work->worker = worker;
Tejun Heo34b65672012-07-19 13:52:53 -0700537 if (likely(worker->task))
538 wake_up_process(worker->task);
539}
540
Tejun Heob56c0d82010-06-29 10:07:09 +0200541/**
542 * queue_kthread_work - queue a kthread_work
543 * @worker: target kthread_worker
544 * @work: kthread_work to queue
545 *
546 * Queue @work to work processor @task for async execution. @task
547 * must have been created with kthread_worker_create(). Returns %true
548 * if @work was successfully queued, %false if it was already pending.
549 */
550bool queue_kthread_work(struct kthread_worker *worker,
551 struct kthread_work *work)
552{
553 bool ret = false;
554 unsigned long flags;
555
556 spin_lock_irqsave(&worker->lock, flags);
557 if (list_empty(&work->node)) {
Tejun Heo34b65672012-07-19 13:52:53 -0700558 insert_kthread_work(worker, work, &worker->work_list);
Tejun Heob56c0d82010-06-29 10:07:09 +0200559 ret = true;
560 }
561 spin_unlock_irqrestore(&worker->lock, flags);
562 return ret;
563}
564EXPORT_SYMBOL_GPL(queue_kthread_work);
565
Tejun Heo34b65672012-07-19 13:52:53 -0700566struct kthread_flush_work {
567 struct kthread_work work;
568 struct completion done;
569};
570
571static void kthread_flush_work_fn(struct kthread_work *work)
572{
573 struct kthread_flush_work *fwork =
574 container_of(work, struct kthread_flush_work, work);
575 complete(&fwork->done);
576}
577
Tejun Heob56c0d82010-06-29 10:07:09 +0200578/**
579 * flush_kthread_work - flush a kthread_work
580 * @work: work to flush
581 *
582 * If @work is queued or executing, wait for it to finish execution.
583 */
584void flush_kthread_work(struct kthread_work *work)
585{
Tejun Heo97ed5372012-07-19 13:52:53 -0700586 struct kthread_flush_work fwork = {
587 KTHREAD_WORK_INIT(fwork.work, kthread_flush_work_fn),
588 COMPLETION_INITIALIZER_ONSTACK(fwork.done),
589 };
590 struct kthread_worker *worker;
591 bool noop = false;
Tejun Heob56c0d82010-06-29 10:07:09 +0200592
Tejun Heo97ed5372012-07-19 13:52:53 -0700593retry:
594 worker = work->worker;
595 if (!worker)
596 return;
Tejun Heob56c0d82010-06-29 10:07:09 +0200597
Tejun Heo97ed5372012-07-19 13:52:53 -0700598 spin_lock_irq(&worker->lock);
599 if (work->worker != worker) {
600 spin_unlock_irq(&worker->lock);
601 goto retry;
602 }
Tejun Heob56c0d82010-06-29 10:07:09 +0200603
Tejun Heo97ed5372012-07-19 13:52:53 -0700604 if (!list_empty(&work->node))
605 insert_kthread_work(worker, &fwork.work, work->node.next);
606 else if (worker->current_work == work)
607 insert_kthread_work(worker, &fwork.work, worker->work_list.next);
608 else
609 noop = true;
Tejun Heob56c0d82010-06-29 10:07:09 +0200610
Tejun Heo97ed5372012-07-19 13:52:53 -0700611 spin_unlock_irq(&worker->lock);
612
613 if (!noop)
614 wait_for_completion(&fwork.done);
Tejun Heob56c0d82010-06-29 10:07:09 +0200615}
616EXPORT_SYMBOL_GPL(flush_kthread_work);
617
Tejun Heob56c0d82010-06-29 10:07:09 +0200618/**
619 * flush_kthread_worker - flush all current works on a kthread_worker
620 * @worker: worker to flush
621 *
622 * Wait until all currently executing or pending works on @worker are
623 * finished.
624 */
625void flush_kthread_worker(struct kthread_worker *worker)
626{
627 struct kthread_flush_work fwork = {
628 KTHREAD_WORK_INIT(fwork.work, kthread_flush_work_fn),
629 COMPLETION_INITIALIZER_ONSTACK(fwork.done),
630 };
631
632 queue_kthread_work(worker, &fwork.work);
633 wait_for_completion(&fwork.done);
634}
635EXPORT_SYMBOL_GPL(flush_kthread_worker);