blob: 146a6fa968254fe00b4aa6a4a423113f083b3e89 [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 Gleixner2a1d4462012-07-16 10:42:36 +000040 unsigned long flags;
41 unsigned int cpu;
Tejun Heo82805ab2010-06-29 10:07:09 +020042 void *data;
Thomas Gleixner2a1d4462012-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 Gleixner2a1d4462012-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 Gleixner2a1d4462012-07-16 10:42:36 +000064bool kthread_should_stop(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070065{
Thomas Gleixner2a1d4462012-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 Gleixner2a1d4462012-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 Gleixner2a1d4462012-07-16 10:42:36 +0000124static void __kthread_parkme(struct kthread *self)
125{
126 __set_current_state(TASK_INTERRUPTIBLE);
127 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();
131 __set_current_state(TASK_INTERRUPTIBLE);
132 }
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 Gleixner2a1d4462012-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 Gleixner2a1d4462012-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 Gleixner2a1d4462012-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 Gleixner2a1d4462012-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 Gleixner2a1d4462012-07-16 10:42:36 +0000258static void __kthread_bind(struct task_struct *p, unsigned int cpu)
259{
260 /* It's safe because the task is inactive. */
261 do_set_cpus_allowed(p, cpumask_of(cpu));
262 p->flags |= PF_THREAD_BOUND;
263}
264
Randy Dunlap9e37bd32006-06-25 05:49:19 -0700265/**
Peter Zijlstra881232b2009-12-16 18:04:39 +0100266 * kthread_bind - bind a just-created kthread to a cpu.
267 * @p: thread created by kthread_create().
268 * @cpu: cpu (might not be online, must be possible) for @k to run on.
269 *
270 * Description: This function is equivalent to set_cpus_allowed(),
271 * except that @cpu doesn't need to be online, and the thread must be
272 * stopped (i.e., just returned from kthread_create()).
273 */
274void kthread_bind(struct task_struct *p, unsigned int cpu)
275{
276 /* Must have done schedule() in kthread() before we set_task_cpu */
277 if (!wait_task_inactive(p, TASK_UNINTERRUPTIBLE)) {
278 WARN_ON(1);
279 return;
280 }
Thomas Gleixner2a1d4462012-07-16 10:42:36 +0000281 __kthread_bind(p, cpu);
Peter Zijlstra881232b2009-12-16 18:04:39 +0100282}
283EXPORT_SYMBOL(kthread_bind);
284
285/**
Thomas Gleixner2a1d4462012-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
326/**
327 * kthread_unpark - unpark a thread created by kthread_create().
328 * @k: thread created by kthread_create().
329 *
330 * Sets kthread_should_park() for @k to return false, wakes it, and
331 * waits for it to return. If the thread is marked percpu then its
332 * bound to the cpu again.
333 */
334void kthread_unpark(struct task_struct *k)
335{
336 struct kthread *kthread = task_get_live_kthread(k);
337
338 if (kthread) {
339 clear_bit(KTHREAD_SHOULD_PARK, &kthread->flags);
340 /*
341 * We clear the IS_PARKED bit here as we don't wait
342 * until the task has left the park code. So if we'd
343 * park before that happens we'd see the IS_PARKED bit
344 * which might be about to be cleared.
345 */
346 if (test_and_clear_bit(KTHREAD_IS_PARKED, &kthread->flags)) {
347 if (test_bit(KTHREAD_IS_PER_CPU, &kthread->flags))
348 __kthread_bind(k, kthread->cpu);
349 wake_up_process(k);
350 }
351 }
352 put_task_struct(k);
353}
354
355/**
356 * kthread_park - park a thread created by kthread_create().
357 * @k: thread created by kthread_create().
358 *
359 * Sets kthread_should_park() for @k to return true, wakes it, and
360 * waits for it to return. This can also be called after kthread_create()
361 * instead of calling wake_up_process(): the thread will park without
362 * calling threadfn().
363 *
364 * Returns 0 if the thread is parked, -ENOSYS if the thread exited.
365 * If called by the kthread itself just the park bit is set.
366 */
367int kthread_park(struct task_struct *k)
368{
369 struct kthread *kthread = task_get_live_kthread(k);
370 int ret = -ENOSYS;
371
372 if (kthread) {
373 if (!test_bit(KTHREAD_IS_PARKED, &kthread->flags)) {
374 set_bit(KTHREAD_SHOULD_PARK, &kthread->flags);
375 if (k != current) {
376 wake_up_process(k);
377 wait_for_completion(&kthread->parked);
378 }
379 }
380 ret = 0;
381 }
382 put_task_struct(k);
383 return ret;
384}
385
386/**
Randy Dunlap9e37bd32006-06-25 05:49:19 -0700387 * kthread_stop - stop a thread created by kthread_create().
388 * @k: thread created by kthread_create().
389 *
390 * Sets kthread_should_stop() for @k to return true, wakes it, and
Oleg Nesterov9ae26022009-06-19 02:51:13 +0200391 * waits for it to exit. This can also be called after kthread_create()
392 * instead of calling wake_up_process(): the thread will exit without
393 * calling threadfn().
394 *
395 * If threadfn() may call do_exit() itself, the caller must ensure
396 * task_struct can't go away.
Randy Dunlap9e37bd32006-06-25 05:49:19 -0700397 *
398 * Returns the result of threadfn(), or %-EINTR if wake_up_process()
399 * was never called.
400 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401int kthread_stop(struct task_struct *k)
402{
Thomas Gleixner2a1d4462012-07-16 10:42:36 +0000403 struct kthread *kthread = task_get_live_kthread(k);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 int ret;
405
Oleg Nesterov63706172009-06-17 16:27:45 -0700406 trace_sched_kthread_stop(k);
Thomas Gleixner2a1d4462012-07-16 10:42:36 +0000407 if (kthread) {
408 set_bit(KTHREAD_SHOULD_STOP, &kthread->flags);
409 clear_bit(KTHREAD_SHOULD_PARK, &kthread->flags);
Oleg Nesterov63706172009-06-17 16:27:45 -0700410 wake_up_process(k);
411 wait_for_completion(&kthread->exited);
412 }
413 ret = k->exit_code;
Mathieu Desnoyers0a16b602008-07-18 12:16:17 -0400414
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 put_task_struct(k);
Mathieu Desnoyers0a16b602008-07-18 12:16:17 -0400416 trace_sched_kthread_stop_ret(ret);
417
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 return ret;
419}
Adrian Bunk52e92e52006-07-14 00:24:05 -0700420EXPORT_SYMBOL(kthread_stop);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421
Satyam Sharmae804a4a2007-07-31 00:39:16 -0700422int kthreadd(void *unused)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423{
Eric W. Biederman73c27992007-05-09 02:34:32 -0700424 struct task_struct *tsk = current;
Eric W. Biederman73c27992007-05-09 02:34:32 -0700425
Satyam Sharmae804a4a2007-07-31 00:39:16 -0700426 /* Setup a clean context for our children to inherit. */
Eric W. Biederman73c27992007-05-09 02:34:32 -0700427 set_task_comm(tsk, "kthreadd");
Oleg Nesterov10ab8252007-05-09 02:34:37 -0700428 ignore_signals(tsk);
Rusty Russell1a2142a2009-03-30 22:05:10 -0600429 set_cpus_allowed_ptr(tsk, cpu_all_mask);
Miao Xie5ab116c2010-03-23 13:35:34 -0700430 set_mems_allowed(node_states[N_HIGH_MEMORY]);
Eric W. Biederman73c27992007-05-09 02:34:32 -0700431
Tejun Heo34b087e2011-11-23 09:28:17 -0800432 current->flags |= PF_NOFREEZE;
Eric W. Biederman73c27992007-05-09 02:34:32 -0700433
434 for (;;) {
435 set_current_state(TASK_INTERRUPTIBLE);
436 if (list_empty(&kthread_create_list))
437 schedule();
438 __set_current_state(TASK_RUNNING);
439
440 spin_lock(&kthread_create_lock);
441 while (!list_empty(&kthread_create_list)) {
442 struct kthread_create_info *create;
443
444 create = list_entry(kthread_create_list.next,
445 struct kthread_create_info, list);
446 list_del_init(&create->list);
447 spin_unlock(&kthread_create_lock);
448
449 create_kthread(create);
450
451 spin_lock(&kthread_create_lock);
452 }
453 spin_unlock(&kthread_create_lock);
454 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455
456 return 0;
457}
Tejun Heob56c0d82010-06-29 10:07:09 +0200458
Yong Zhang4f32e9b2010-12-22 10:27:53 +0100459void __init_kthread_worker(struct kthread_worker *worker,
460 const char *name,
461 struct lock_class_key *key)
462{
463 spin_lock_init(&worker->lock);
464 lockdep_set_class_and_name(&worker->lock, key, name);
465 INIT_LIST_HEAD(&worker->work_list);
466 worker->task = NULL;
467}
468EXPORT_SYMBOL_GPL(__init_kthread_worker);
469
Tejun Heob56c0d82010-06-29 10:07:09 +0200470/**
471 * kthread_worker_fn - kthread function to process kthread_worker
472 * @worker_ptr: pointer to initialized kthread_worker
473 *
474 * This function can be used as @threadfn to kthread_create() or
475 * kthread_run() with @worker_ptr argument pointing to an initialized
476 * kthread_worker. The started kthread will process work_list until
477 * the it is stopped with kthread_stop(). A kthread can also call
478 * this function directly after extra initialization.
479 *
480 * Different kthreads can be used for the same kthread_worker as long
481 * as there's only one kthread attached to it at any given time. A
482 * kthread_worker without an attached kthread simply collects queued
483 * kthread_works.
484 */
485int kthread_worker_fn(void *worker_ptr)
486{
487 struct kthread_worker *worker = worker_ptr;
488 struct kthread_work *work;
489
490 WARN_ON(worker->task);
491 worker->task = current;
492repeat:
493 set_current_state(TASK_INTERRUPTIBLE); /* mb paired w/ kthread_stop */
494
495 if (kthread_should_stop()) {
496 __set_current_state(TASK_RUNNING);
497 spin_lock_irq(&worker->lock);
498 worker->task = NULL;
499 spin_unlock_irq(&worker->lock);
500 return 0;
501 }
502
503 work = NULL;
504 spin_lock_irq(&worker->lock);
505 if (!list_empty(&worker->work_list)) {
506 work = list_first_entry(&worker->work_list,
507 struct kthread_work, node);
508 list_del_init(&work->node);
509 }
Tejun Heo46f3d972012-07-19 13:52:53 -0700510 worker->current_work = work;
Tejun Heob56c0d82010-06-29 10:07:09 +0200511 spin_unlock_irq(&worker->lock);
512
513 if (work) {
514 __set_current_state(TASK_RUNNING);
515 work->func(work);
Tejun Heob56c0d82010-06-29 10:07:09 +0200516 } else if (!freezing(current))
517 schedule();
518
519 try_to_freeze();
520 goto repeat;
521}
522EXPORT_SYMBOL_GPL(kthread_worker_fn);
523
Tejun Heo9a2e03d2012-07-19 13:52:53 -0700524/* insert @work before @pos in @worker */
525static void insert_kthread_work(struct kthread_worker *worker,
526 struct kthread_work *work,
527 struct list_head *pos)
528{
529 lockdep_assert_held(&worker->lock);
530
531 list_add_tail(&work->node, pos);
Tejun Heo46f3d972012-07-19 13:52:53 -0700532 work->worker = worker;
Tejun Heo9a2e03d2012-07-19 13:52:53 -0700533 if (likely(worker->task))
534 wake_up_process(worker->task);
535}
536
Tejun Heob56c0d82010-06-29 10:07:09 +0200537/**
538 * queue_kthread_work - queue a kthread_work
539 * @worker: target kthread_worker
540 * @work: kthread_work to queue
541 *
542 * Queue @work to work processor @task for async execution. @task
543 * must have been created with kthread_worker_create(). Returns %true
544 * if @work was successfully queued, %false if it was already pending.
545 */
546bool queue_kthread_work(struct kthread_worker *worker,
547 struct kthread_work *work)
548{
549 bool ret = false;
550 unsigned long flags;
551
552 spin_lock_irqsave(&worker->lock, flags);
553 if (list_empty(&work->node)) {
Tejun Heo9a2e03d2012-07-19 13:52:53 -0700554 insert_kthread_work(worker, work, &worker->work_list);
Tejun Heob56c0d82010-06-29 10:07:09 +0200555 ret = true;
556 }
557 spin_unlock_irqrestore(&worker->lock, flags);
558 return ret;
559}
560EXPORT_SYMBOL_GPL(queue_kthread_work);
561
Tejun Heo9a2e03d2012-07-19 13:52:53 -0700562struct kthread_flush_work {
563 struct kthread_work work;
564 struct completion done;
565};
566
567static void kthread_flush_work_fn(struct kthread_work *work)
568{
569 struct kthread_flush_work *fwork =
570 container_of(work, struct kthread_flush_work, work);
571 complete(&fwork->done);
572}
573
Tejun Heob56c0d82010-06-29 10:07:09 +0200574/**
575 * flush_kthread_work - flush a kthread_work
576 * @work: work to flush
577 *
578 * If @work is queued or executing, wait for it to finish execution.
579 */
580void flush_kthread_work(struct kthread_work *work)
581{
Tejun Heo46f3d972012-07-19 13:52:53 -0700582 struct kthread_flush_work fwork = {
583 KTHREAD_WORK_INIT(fwork.work, kthread_flush_work_fn),
584 COMPLETION_INITIALIZER_ONSTACK(fwork.done),
585 };
586 struct kthread_worker *worker;
587 bool noop = false;
Tejun Heob56c0d82010-06-29 10:07:09 +0200588
Tejun Heo46f3d972012-07-19 13:52:53 -0700589retry:
590 worker = work->worker;
591 if (!worker)
592 return;
Tejun Heob56c0d82010-06-29 10:07:09 +0200593
Tejun Heo46f3d972012-07-19 13:52:53 -0700594 spin_lock_irq(&worker->lock);
595 if (work->worker != worker) {
596 spin_unlock_irq(&worker->lock);
597 goto retry;
598 }
Tejun Heob56c0d82010-06-29 10:07:09 +0200599
Tejun Heo46f3d972012-07-19 13:52:53 -0700600 if (!list_empty(&work->node))
601 insert_kthread_work(worker, &fwork.work, work->node.next);
602 else if (worker->current_work == work)
603 insert_kthread_work(worker, &fwork.work, worker->work_list.next);
604 else
605 noop = true;
Tejun Heob56c0d82010-06-29 10:07:09 +0200606
Tejun Heo46f3d972012-07-19 13:52:53 -0700607 spin_unlock_irq(&worker->lock);
608
609 if (!noop)
610 wait_for_completion(&fwork.done);
Tejun Heob56c0d82010-06-29 10:07:09 +0200611}
612EXPORT_SYMBOL_GPL(flush_kthread_work);
613
Tejun Heob56c0d82010-06-29 10:07:09 +0200614/**
615 * flush_kthread_worker - flush all current works on a kthread_worker
616 * @worker: worker to flush
617 *
618 * Wait until all currently executing or pending works on @worker are
619 * finished.
620 */
621void flush_kthread_worker(struct kthread_worker *worker)
622{
623 struct kthread_flush_work fwork = {
624 KTHREAD_WORK_INIT(fwork.work, kthread_flush_work_fn),
625 COMPLETION_INITIALIZER_ONSTACK(fwork.done),
626 };
627
628 queue_kthread_work(worker, &fwork.work);
629 wait_for_completion(&fwork.done);
630}
631EXPORT_SYMBOL_GPL(flush_kthread_worker);