]> git.proxmox.com Git - mirror_ubuntu-zesty-kernel.git/blame - kernel/kthread.c
tick/broadcast: Prevent NULL pointer dereference
[mirror_ubuntu-zesty-kernel.git] / kernel / kthread.c
CommitLineData
1da177e4
LT
1/* Kernel thread helper functions.
2 * Copyright (C) 2004 IBM Corporation, Rusty Russell.
3 *
73c27992 4 * Creation is done via kthreadd, so that we get a clean environment
1da177e4
LT
5 * 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>
58568d2a 12#include <linux/cpuset.h>
1da177e4
LT
13#include <linux/unistd.h>
14#include <linux/file.h>
9984de1a 15#include <linux/export.h>
97d1f15b 16#include <linux/mutex.h>
b56c0d89
TH
17#include <linux/slab.h>
18#include <linux/freezer.h>
a74fb73c 19#include <linux/ptrace.h>
cd42d559 20#include <linux/uaccess.h>
ad8d75ff 21#include <trace/events/sched.h>
1da177e4 22
73c27992
EB
23static DEFINE_SPINLOCK(kthread_create_lock);
24static LIST_HEAD(kthread_create_list);
25struct task_struct *kthreadd_task;
1da177e4
LT
26
27struct kthread_create_info
28{
73c27992 29 /* Information passed to kthread() from kthreadd. */
1da177e4
LT
30 int (*threadfn)(void *data);
31 void *data;
207205a2 32 int node;
1da177e4 33
73c27992 34 /* Result passed back to kthread_create() from kthreadd. */
1da177e4 35 struct task_struct *result;
786235ee 36 struct completion *done;
65f27f38 37
73c27992 38 struct list_head list;
1da177e4
LT
39};
40
63706172 41struct kthread {
2a1d4460
TG
42 unsigned long flags;
43 unsigned int cpu;
82805ab7 44 void *data;
2a1d4460 45 struct completion parked;
63706172 46 struct completion exited;
1da177e4
LT
47};
48
2a1d4460
TG
49enum KTHREAD_BITS {
50 KTHREAD_IS_PER_CPU = 0,
51 KTHREAD_SHOULD_STOP,
52 KTHREAD_SHOULD_PARK,
53 KTHREAD_IS_PARKED,
54};
55
4ecdafc8
ON
56#define __to_kthread(vfork) \
57 container_of(vfork, struct kthread, exited)
58
59static inline struct kthread *to_kthread(struct task_struct *k)
60{
61 return __to_kthread(k->vfork_done);
62}
63
64static struct kthread *to_live_kthread(struct task_struct *k)
65{
66 struct completion *vfork = ACCESS_ONCE(k->vfork_done);
67 if (likely(vfork))
68 return __to_kthread(vfork);
69 return NULL;
70}
1da177e4 71
9e37bd30
RD
72/**
73 * kthread_should_stop - should this kthread return now?
74 *
72fd4a35 75 * When someone calls kthread_stop() on your kthread, it will be woken
9e37bd30
RD
76 * and this will return true. You should then return, and your return
77 * value will be passed through to kthread_stop().
78 */
2a1d4460 79bool kthread_should_stop(void)
1da177e4 80{
2a1d4460 81 return test_bit(KTHREAD_SHOULD_STOP, &to_kthread(current)->flags);
1da177e4
LT
82}
83EXPORT_SYMBOL(kthread_should_stop);
84
2a1d4460
TG
85/**
86 * kthread_should_park - should this kthread park now?
87 *
88 * When someone calls kthread_park() on your kthread, it will be woken
89 * and this will return true. You should then do the necessary
90 * cleanup and call kthread_parkme()
91 *
92 * Similar to kthread_should_stop(), but this keeps the thread alive
93 * and in a park position. kthread_unpark() "restarts" the thread and
94 * calls the thread function again.
95 */
96bool kthread_should_park(void)
97{
98 return test_bit(KTHREAD_SHOULD_PARK, &to_kthread(current)->flags);
99}
18896451 100EXPORT_SYMBOL_GPL(kthread_should_park);
2a1d4460 101
8a32c441
TH
102/**
103 * kthread_freezable_should_stop - should this freezable kthread return now?
104 * @was_frozen: optional out parameter, indicates whether %current was frozen
105 *
106 * kthread_should_stop() for freezable kthreads, which will enter
107 * refrigerator if necessary. This function is safe from kthread_stop() /
108 * freezer deadlock and freezable kthreads should use this function instead
109 * of calling try_to_freeze() directly.
110 */
111bool kthread_freezable_should_stop(bool *was_frozen)
112{
113 bool frozen = false;
114
115 might_sleep();
116
117 if (unlikely(freezing(current)))
118 frozen = __refrigerator(true);
119
120 if (was_frozen)
121 *was_frozen = frozen;
122
123 return kthread_should_stop();
124}
125EXPORT_SYMBOL_GPL(kthread_freezable_should_stop);
126
82805ab7
TH
127/**
128 * kthread_data - return data value specified on kthread creation
129 * @task: kthread task in question
130 *
131 * Return the data value specified when kthread @task was created.
132 * The caller is responsible for ensuring the validity of @task when
133 * calling this function.
134 */
135void *kthread_data(struct task_struct *task)
136{
137 return to_kthread(task)->data;
138}
139
cd42d559
TH
140/**
141 * probe_kthread_data - speculative version of kthread_data()
142 * @task: possible kthread task in question
143 *
144 * @task could be a kthread task. Return the data value specified when it
145 * was created if accessible. If @task isn't a kthread task or its data is
146 * inaccessible for any reason, %NULL is returned. This function requires
147 * that @task itself is safe to dereference.
148 */
149void *probe_kthread_data(struct task_struct *task)
150{
151 struct kthread *kthread = to_kthread(task);
152 void *data = NULL;
153
154 probe_kernel_read(&data, &kthread->data, sizeof(data));
155 return data;
156}
157
2a1d4460
TG
158static void __kthread_parkme(struct kthread *self)
159{
f2530dc7 160 __set_current_state(TASK_PARKED);
2a1d4460
TG
161 while (test_bit(KTHREAD_SHOULD_PARK, &self->flags)) {
162 if (!test_and_set_bit(KTHREAD_IS_PARKED, &self->flags))
163 complete(&self->parked);
164 schedule();
f2530dc7 165 __set_current_state(TASK_PARKED);
2a1d4460
TG
166 }
167 clear_bit(KTHREAD_IS_PARKED, &self->flags);
168 __set_current_state(TASK_RUNNING);
169}
170
171void kthread_parkme(void)
172{
173 __kthread_parkme(to_kthread(current));
174}
18896451 175EXPORT_SYMBOL_GPL(kthread_parkme);
2a1d4460 176
1da177e4
LT
177static int kthread(void *_create)
178{
63706172 179 /* Copy data: it's on kthread's stack */
1da177e4 180 struct kthread_create_info *create = _create;
63706172
ON
181 int (*threadfn)(void *data) = create->threadfn;
182 void *data = create->data;
786235ee 183 struct completion *done;
63706172
ON
184 struct kthread self;
185 int ret;
1da177e4 186
2a1d4460 187 self.flags = 0;
82805ab7 188 self.data = data;
63706172 189 init_completion(&self.exited);
2a1d4460 190 init_completion(&self.parked);
63706172 191 current->vfork_done = &self.exited;
1da177e4 192
786235ee
TH
193 /* If user was SIGKILLed, I release the structure. */
194 done = xchg(&create->done, NULL);
195 if (!done) {
196 kfree(create);
197 do_exit(-EINTR);
198 }
1da177e4 199 /* OK, tell user we're spawned, wait for stop or wakeup */
a076e4bc 200 __set_current_state(TASK_UNINTERRUPTIBLE);
3217ab97 201 create->result = current;
786235ee 202 complete(done);
1da177e4
LT
203 schedule();
204
63706172 205 ret = -EINTR;
1da177e4 206
2a1d4460
TG
207 if (!test_bit(KTHREAD_SHOULD_STOP, &self.flags)) {
208 __kthread_parkme(&self);
209 ret = threadfn(data);
210 }
63706172
ON
211 /* we can't just return, we must preserve "self" on stack */
212 do_exit(ret);
1da177e4
LT
213}
214
207205a2
ED
215/* called from do_fork() to get node information for about to be created task */
216int tsk_fork_get_node(struct task_struct *tsk)
217{
218#ifdef CONFIG_NUMA
219 if (tsk == kthreadd_task)
220 return tsk->pref_node_fork;
221#endif
81c98869 222 return NUMA_NO_NODE;
207205a2
ED
223}
224
73c27992 225static void create_kthread(struct kthread_create_info *create)
1da177e4 226{
1da177e4
LT
227 int pid;
228
207205a2
ED
229#ifdef CONFIG_NUMA
230 current->pref_node_fork = create->node;
231#endif
1da177e4
LT
232 /* We want our own signal handler (we take no signals by default). */
233 pid = kernel_thread(kthread, create, CLONE_FS | CLONE_FILES | SIGCHLD);
cdd140bd 234 if (pid < 0) {
786235ee
TH
235 /* If user was SIGKILLed, I release the structure. */
236 struct completion *done = xchg(&create->done, NULL);
237
238 if (!done) {
239 kfree(create);
240 return;
241 }
1da177e4 242 create->result = ERR_PTR(pid);
786235ee 243 complete(done);
cdd140bd 244 }
1da177e4
LT
245}
246
9e37bd30 247/**
207205a2 248 * kthread_create_on_node - create a kthread.
9e37bd30
RD
249 * @threadfn: the function to run until signal_pending(current).
250 * @data: data ptr for @threadfn.
e9f06986 251 * @node: task and thread structures for the thread are allocated on this node
9e37bd30
RD
252 * @namefmt: printf-style name for the thread.
253 *
254 * Description: This helper function creates and names a kernel
255 * thread. The thread will be stopped: use wake_up_process() to start
e9f06986
AM
256 * it. See also kthread_run(). The new thread has SCHED_NORMAL policy and
257 * is affine to all CPUs.
9e37bd30 258 *
207205a2 259 * If thread is going to be bound on a particular cpu, give its node
e9f06986 260 * in @node, to get NUMA affinity for kthread stack, or else give NUMA_NO_NODE.
9e37bd30 261 * When woken, the thread will run @threadfn() with @data as its
72fd4a35 262 * argument. @threadfn() can either call do_exit() directly if it is a
25985edc 263 * standalone thread for which no one will call kthread_stop(), or
9e37bd30
RD
264 * return when 'kthread_should_stop()' is true (which means
265 * kthread_stop() has been called). The return value should be zero
266 * or a negative error number; it will be passed to kthread_stop().
267 *
8fe6929c 268 * Returns a task_struct or ERR_PTR(-ENOMEM) or ERR_PTR(-EINTR).
9e37bd30 269 */
207205a2 270struct task_struct *kthread_create_on_node(int (*threadfn)(void *data),
2a1d4460 271 void *data, int node,
207205a2
ED
272 const char namefmt[],
273 ...)
1da177e4 274{
786235ee
TH
275 DECLARE_COMPLETION_ONSTACK(done);
276 struct task_struct *task;
277 struct kthread_create_info *create = kmalloc(sizeof(*create),
278 GFP_KERNEL);
279
280 if (!create)
281 return ERR_PTR(-ENOMEM);
282 create->threadfn = threadfn;
283 create->data = data;
284 create->node = node;
285 create->done = &done;
73c27992
EB
286
287 spin_lock(&kthread_create_lock);
786235ee 288 list_add_tail(&create->list, &kthread_create_list);
73c27992
EB
289 spin_unlock(&kthread_create_lock);
290
cbd9b67b 291 wake_up_process(kthreadd_task);
786235ee
TH
292 /*
293 * Wait for completion in killable state, for I might be chosen by
294 * the OOM killer while kthreadd is trying to allocate memory for
295 * new kernel thread.
296 */
297 if (unlikely(wait_for_completion_killable(&done))) {
81769990
TH
298 int i = 0;
299
300 /*
301 * I got SIGKILL, but wait for 10 more seconds for completion
302 * unless chosen by the OOM killer. This delay is there as a
303 * workaround for boot failure caused by SIGKILL upon device
304 * driver initialization timeout.
305 */
306 while (i++ < 10 && !test_tsk_thread_flag(current, TIF_MEMDIE))
307 if (wait_for_completion_timeout(&done, HZ))
308 goto ready;
786235ee
TH
309 /*
310 * If I was SIGKILLed before kthreadd (or new kernel thread)
311 * calls complete(), leave the cleanup of this structure to
312 * that thread.
313 */
314 if (xchg(&create->done, NULL))
8fe6929c 315 return ERR_PTR(-EINTR);
786235ee
TH
316 /*
317 * kthreadd (or new kernel thread) will call complete()
318 * shortly.
319 */
320 wait_for_completion(&done);
321 }
81769990 322ready:
786235ee
TH
323 task = create->result;
324 if (!IS_ERR(task)) {
c9b5f501 325 static const struct sched_param param = { .sched_priority = 0 };
1da177e4 326 va_list args;
1c99315b 327
1da177e4 328 va_start(args, namefmt);
786235ee 329 vsnprintf(task->comm, sizeof(task->comm), namefmt, args);
1da177e4 330 va_end(args);
1c99315b
ON
331 /*
332 * root may have changed our (kthreadd's) priority or CPU mask.
333 * The kernel thread should not inherit these properties.
334 */
786235ee
TH
335 sched_setscheduler_nocheck(task, SCHED_NORMAL, &param);
336 set_cpus_allowed_ptr(task, cpu_all_mask);
1da177e4 337 }
786235ee
TH
338 kfree(create);
339 return task;
1da177e4 340}
207205a2 341EXPORT_SYMBOL(kthread_create_on_node);
1da177e4 342
25834c73 343static void __kthread_bind_mask(struct task_struct *p, const struct cpumask *mask, long state)
2a1d4460 344{
25834c73
PZ
345 unsigned long flags;
346
f2530dc7
TG
347 if (!wait_task_inactive(p, state)) {
348 WARN_ON(1);
349 return;
350 }
25834c73 351
2a1d4460 352 /* It's safe because the task is inactive. */
25834c73
PZ
353 raw_spin_lock_irqsave(&p->pi_lock, flags);
354 do_set_cpus_allowed(p, mask);
14a40ffc 355 p->flags |= PF_NO_SETAFFINITY;
25834c73
PZ
356 raw_spin_unlock_irqrestore(&p->pi_lock, flags);
357}
358
359static void __kthread_bind(struct task_struct *p, unsigned int cpu, long state)
360{
361 __kthread_bind_mask(p, cpumask_of(cpu), state);
362}
363
364void kthread_bind_mask(struct task_struct *p, const struct cpumask *mask)
365{
366 __kthread_bind_mask(p, mask, TASK_UNINTERRUPTIBLE);
2a1d4460
TG
367}
368
881232b7
PZ
369/**
370 * kthread_bind - bind a just-created kthread to a cpu.
371 * @p: thread created by kthread_create().
372 * @cpu: cpu (might not be online, must be possible) for @k to run on.
373 *
374 * Description: This function is equivalent to set_cpus_allowed(),
375 * except that @cpu doesn't need to be online, and the thread must be
376 * stopped (i.e., just returned from kthread_create()).
377 */
378void kthread_bind(struct task_struct *p, unsigned int cpu)
379{
f2530dc7 380 __kthread_bind(p, cpu, TASK_UNINTERRUPTIBLE);
881232b7
PZ
381}
382EXPORT_SYMBOL(kthread_bind);
383
2a1d4460
TG
384/**
385 * kthread_create_on_cpu - Create a cpu bound kthread
386 * @threadfn: the function to run until signal_pending(current).
387 * @data: data ptr for @threadfn.
388 * @cpu: The cpu on which the thread should be bound,
389 * @namefmt: printf-style name for the thread. Format is restricted
390 * to "name.*%u". Code fills in cpu number.
391 *
392 * Description: This helper function creates and names a kernel thread
393 * The thread will be woken and put into park mode.
394 */
395struct task_struct *kthread_create_on_cpu(int (*threadfn)(void *data),
396 void *data, unsigned int cpu,
397 const char *namefmt)
398{
399 struct task_struct *p;
400
10922838 401 p = kthread_create_on_node(threadfn, data, cpu_to_node(cpu), namefmt,
2a1d4460
TG
402 cpu);
403 if (IS_ERR(p))
404 return p;
405 set_bit(KTHREAD_IS_PER_CPU, &to_kthread(p)->flags);
406 to_kthread(p)->cpu = cpu;
407 /* Park the thread to get it out of TASK_UNINTERRUPTIBLE state */
408 kthread_park(p);
409 return p;
410}
411
f2530dc7
TG
412static void __kthread_unpark(struct task_struct *k, struct kthread *kthread)
413{
414 clear_bit(KTHREAD_SHOULD_PARK, &kthread->flags);
415 /*
416 * We clear the IS_PARKED bit here as we don't wait
417 * until the task has left the park code. So if we'd
418 * park before that happens we'd see the IS_PARKED bit
419 * which might be about to be cleared.
420 */
421 if (test_and_clear_bit(KTHREAD_IS_PARKED, &kthread->flags)) {
422 if (test_bit(KTHREAD_IS_PER_CPU, &kthread->flags))
423 __kthread_bind(k, kthread->cpu, TASK_PARKED);
424 wake_up_state(k, TASK_PARKED);
425 }
426}
427
2a1d4460
TG
428/**
429 * kthread_unpark - unpark a thread created by kthread_create().
430 * @k: thread created by kthread_create().
431 *
432 * Sets kthread_should_park() for @k to return false, wakes it, and
433 * waits for it to return. If the thread is marked percpu then its
434 * bound to the cpu again.
435 */
436void kthread_unpark(struct task_struct *k)
437{
b5c5442b 438 struct kthread *kthread = to_live_kthread(k);
2a1d4460 439
f2530dc7
TG
440 if (kthread)
441 __kthread_unpark(k, kthread);
2a1d4460 442}
18896451 443EXPORT_SYMBOL_GPL(kthread_unpark);
2a1d4460
TG
444
445/**
446 * kthread_park - park a thread created by kthread_create().
447 * @k: thread created by kthread_create().
448 *
449 * Sets kthread_should_park() for @k to return true, wakes it, and
450 * waits for it to return. This can also be called after kthread_create()
451 * instead of calling wake_up_process(): the thread will park without
452 * calling threadfn().
453 *
454 * Returns 0 if the thread is parked, -ENOSYS if the thread exited.
455 * If called by the kthread itself just the park bit is set.
456 */
457int kthread_park(struct task_struct *k)
458{
b5c5442b 459 struct kthread *kthread = to_live_kthread(k);
2a1d4460
TG
460 int ret = -ENOSYS;
461
462 if (kthread) {
463 if (!test_bit(KTHREAD_IS_PARKED, &kthread->flags)) {
464 set_bit(KTHREAD_SHOULD_PARK, &kthread->flags);
465 if (k != current) {
466 wake_up_process(k);
467 wait_for_completion(&kthread->parked);
468 }
469 }
470 ret = 0;
471 }
2a1d4460
TG
472 return ret;
473}
18896451 474EXPORT_SYMBOL_GPL(kthread_park);
2a1d4460 475
9e37bd30
RD
476/**
477 * kthread_stop - stop a thread created by kthread_create().
478 * @k: thread created by kthread_create().
479 *
480 * Sets kthread_should_stop() for @k to return true, wakes it, and
9ae26027
ON
481 * waits for it to exit. This can also be called after kthread_create()
482 * instead of calling wake_up_process(): the thread will exit without
483 * calling threadfn().
484 *
485 * If threadfn() may call do_exit() itself, the caller must ensure
486 * task_struct can't go away.
9e37bd30
RD
487 *
488 * Returns the result of threadfn(), or %-EINTR if wake_up_process()
489 * was never called.
490 */
1da177e4
LT
491int kthread_stop(struct task_struct *k)
492{
b5c5442b 493 struct kthread *kthread;
1da177e4
LT
494 int ret;
495
0a16b607 496 trace_sched_kthread_stop(k);
b5c5442b
ON
497
498 get_task_struct(k);
499 kthread = to_live_kthread(k);
2a1d4460
TG
500 if (kthread) {
501 set_bit(KTHREAD_SHOULD_STOP, &kthread->flags);
f2530dc7 502 __kthread_unpark(k, kthread);
63706172
ON
503 wake_up_process(k);
504 wait_for_completion(&kthread->exited);
505 }
506 ret = k->exit_code;
1da177e4 507 put_task_struct(k);
0a16b607 508
b5c5442b 509 trace_sched_kthread_stop_ret(ret);
1da177e4
LT
510 return ret;
511}
52e92e57 512EXPORT_SYMBOL(kthread_stop);
1da177e4 513
e804a4a4 514int kthreadd(void *unused)
1da177e4 515{
73c27992 516 struct task_struct *tsk = current;
1da177e4 517
e804a4a4 518 /* Setup a clean context for our children to inherit. */
73c27992 519 set_task_comm(tsk, "kthreadd");
10ab825b 520 ignore_signals(tsk);
1a2142af 521 set_cpus_allowed_ptr(tsk, cpu_all_mask);
aee4faa4 522 set_mems_allowed(node_states[N_MEMORY]);
73c27992 523
34b087e4 524 current->flags |= PF_NOFREEZE;
73c27992
EB
525
526 for (;;) {
527 set_current_state(TASK_INTERRUPTIBLE);
528 if (list_empty(&kthread_create_list))
529 schedule();
530 __set_current_state(TASK_RUNNING);
531
532 spin_lock(&kthread_create_lock);
533 while (!list_empty(&kthread_create_list)) {
534 struct kthread_create_info *create;
535
536 create = list_entry(kthread_create_list.next,
537 struct kthread_create_info, list);
538 list_del_init(&create->list);
539 spin_unlock(&kthread_create_lock);
540
541 create_kthread(create);
542
543 spin_lock(&kthread_create_lock);
544 }
545 spin_unlock(&kthread_create_lock);
546 }
547
548 return 0;
549}
b56c0d89 550
4f32e9b1
YZ
551void __init_kthread_worker(struct kthread_worker *worker,
552 const char *name,
553 struct lock_class_key *key)
554{
555 spin_lock_init(&worker->lock);
556 lockdep_set_class_and_name(&worker->lock, key, name);
557 INIT_LIST_HEAD(&worker->work_list);
558 worker->task = NULL;
559}
560EXPORT_SYMBOL_GPL(__init_kthread_worker);
561
b56c0d89
TH
562/**
563 * kthread_worker_fn - kthread function to process kthread_worker
564 * @worker_ptr: pointer to initialized kthread_worker
565 *
566 * This function can be used as @threadfn to kthread_create() or
567 * kthread_run() with @worker_ptr argument pointing to an initialized
568 * kthread_worker. The started kthread will process work_list until
569 * the it is stopped with kthread_stop(). A kthread can also call
570 * this function directly after extra initialization.
571 *
572 * Different kthreads can be used for the same kthread_worker as long
573 * as there's only one kthread attached to it at any given time. A
574 * kthread_worker without an attached kthread simply collects queued
575 * kthread_works.
576 */
577int kthread_worker_fn(void *worker_ptr)
578{
579 struct kthread_worker *worker = worker_ptr;
580 struct kthread_work *work;
581
582 WARN_ON(worker->task);
583 worker->task = current;
584repeat:
585 set_current_state(TASK_INTERRUPTIBLE); /* mb paired w/ kthread_stop */
586
587 if (kthread_should_stop()) {
588 __set_current_state(TASK_RUNNING);
589 spin_lock_irq(&worker->lock);
590 worker->task = NULL;
591 spin_unlock_irq(&worker->lock);
592 return 0;
593 }
594
595 work = NULL;
596 spin_lock_irq(&worker->lock);
597 if (!list_empty(&worker->work_list)) {
598 work = list_first_entry(&worker->work_list,
599 struct kthread_work, node);
600 list_del_init(&work->node);
601 }
46f3d976 602 worker->current_work = work;
b56c0d89
TH
603 spin_unlock_irq(&worker->lock);
604
605 if (work) {
606 __set_current_state(TASK_RUNNING);
607 work->func(work);
b56c0d89
TH
608 } else if (!freezing(current))
609 schedule();
610
611 try_to_freeze();
612 goto repeat;
613}
614EXPORT_SYMBOL_GPL(kthread_worker_fn);
615
9a2e03d8
TH
616/* insert @work before @pos in @worker */
617static void insert_kthread_work(struct kthread_worker *worker,
618 struct kthread_work *work,
619 struct list_head *pos)
620{
621 lockdep_assert_held(&worker->lock);
622
623 list_add_tail(&work->node, pos);
46f3d976 624 work->worker = worker;
ed1403ec 625 if (!worker->current_work && likely(worker->task))
9a2e03d8
TH
626 wake_up_process(worker->task);
627}
628
b56c0d89
TH
629/**
630 * queue_kthread_work - queue a kthread_work
631 * @worker: target kthread_worker
632 * @work: kthread_work to queue
633 *
634 * Queue @work to work processor @task for async execution. @task
635 * must have been created with kthread_worker_create(). Returns %true
636 * if @work was successfully queued, %false if it was already pending.
637 */
638bool queue_kthread_work(struct kthread_worker *worker,
639 struct kthread_work *work)
640{
641 bool ret = false;
642 unsigned long flags;
643
644 spin_lock_irqsave(&worker->lock, flags);
645 if (list_empty(&work->node)) {
9a2e03d8 646 insert_kthread_work(worker, work, &worker->work_list);
b56c0d89
TH
647 ret = true;
648 }
649 spin_unlock_irqrestore(&worker->lock, flags);
650 return ret;
651}
652EXPORT_SYMBOL_GPL(queue_kthread_work);
653
9a2e03d8
TH
654struct kthread_flush_work {
655 struct kthread_work work;
656 struct completion done;
657};
658
659static void kthread_flush_work_fn(struct kthread_work *work)
660{
661 struct kthread_flush_work *fwork =
662 container_of(work, struct kthread_flush_work, work);
663 complete(&fwork->done);
664}
665
b56c0d89
TH
666/**
667 * flush_kthread_work - flush a kthread_work
668 * @work: work to flush
669 *
670 * If @work is queued or executing, wait for it to finish execution.
671 */
672void flush_kthread_work(struct kthread_work *work)
673{
46f3d976
TH
674 struct kthread_flush_work fwork = {
675 KTHREAD_WORK_INIT(fwork.work, kthread_flush_work_fn),
676 COMPLETION_INITIALIZER_ONSTACK(fwork.done),
677 };
678 struct kthread_worker *worker;
679 bool noop = false;
680
681retry:
682 worker = work->worker;
683 if (!worker)
684 return;
b56c0d89 685
46f3d976
TH
686 spin_lock_irq(&worker->lock);
687 if (work->worker != worker) {
688 spin_unlock_irq(&worker->lock);
689 goto retry;
690 }
b56c0d89 691
46f3d976
TH
692 if (!list_empty(&work->node))
693 insert_kthread_work(worker, &fwork.work, work->node.next);
694 else if (worker->current_work == work)
695 insert_kthread_work(worker, &fwork.work, worker->work_list.next);
696 else
697 noop = true;
b56c0d89 698
46f3d976 699 spin_unlock_irq(&worker->lock);
b56c0d89 700
46f3d976
TH
701 if (!noop)
702 wait_for_completion(&fwork.done);
b56c0d89
TH
703}
704EXPORT_SYMBOL_GPL(flush_kthread_work);
705
b56c0d89
TH
706/**
707 * flush_kthread_worker - flush all current works on a kthread_worker
708 * @worker: worker to flush
709 *
710 * Wait until all currently executing or pending works on @worker are
711 * finished.
712 */
713void flush_kthread_worker(struct kthread_worker *worker)
714{
715 struct kthread_flush_work fwork = {
716 KTHREAD_WORK_INIT(fwork.work, kthread_flush_work_fn),
717 COMPLETION_INITIALIZER_ONSTACK(fwork.done),
718 };
719
720 queue_kthread_work(worker, &fwork.work);
721 wait_for_completion(&fwork.done);
722}
723EXPORT_SYMBOL_GPL(flush_kthread_worker);