]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - kernel/workqueue.c
workqueue: don't clear cwq->thread until it exits
[mirror_ubuntu-artful-kernel.git] / kernel / workqueue.c
CommitLineData
1da177e4
LT
1/*
2 * linux/kernel/workqueue.c
3 *
4 * Generic mechanism for defining kernel helper threads for running
5 * arbitrary tasks in process context.
6 *
7 * Started by Ingo Molnar, Copyright (C) 2002
8 *
9 * Derived from the taskqueue/keventd code by:
10 *
11 * David Woodhouse <dwmw2@infradead.org>
12 * Andrew Morton <andrewm@uow.edu.au>
13 * Kai Petzke <wpp@marie.physik.tu-berlin.de>
14 * Theodore Ts'o <tytso@mit.edu>
89ada679
CL
15 *
16 * Made to use alloc_percpu by Christoph Lameter <clameter@sgi.com>.
1da177e4
LT
17 */
18
19#include <linux/module.h>
20#include <linux/kernel.h>
21#include <linux/sched.h>
22#include <linux/init.h>
23#include <linux/signal.h>
24#include <linux/completion.h>
25#include <linux/workqueue.h>
26#include <linux/slab.h>
27#include <linux/cpu.h>
28#include <linux/notifier.h>
29#include <linux/kthread.h>
1fa44eca 30#include <linux/hardirq.h>
46934023 31#include <linux/mempolicy.h>
341a5958 32#include <linux/freezer.h>
d5abe669
PZ
33#include <linux/kallsyms.h>
34#include <linux/debug_locks.h>
1da177e4
LT
35
36/*
f756d5e2
NL
37 * The per-CPU workqueue (if single thread, we always use the first
38 * possible cpu).
1da177e4
LT
39 */
40struct cpu_workqueue_struct {
41
42 spinlock_t lock;
43
1da177e4
LT
44 struct list_head worklist;
45 wait_queue_head_t more_work;
1da177e4
LT
46
47 struct workqueue_struct *wq;
36c8b586 48 struct task_struct *thread;
b89deed3 49 struct work_struct *current_work;
1da177e4
LT
50
51 int run_depth; /* Detect run_workqueue() recursion depth */
52} ____cacheline_aligned;
53
54/*
55 * The externally visible workqueue abstraction is an array of
56 * per-CPU workqueues:
57 */
58struct workqueue_struct {
89ada679 59 struct cpu_workqueue_struct *cpu_wq;
1da177e4
LT
60 const char *name;
61 struct list_head list; /* Empty if single thread */
319c2a98 62 int freezeable; /* Freeze threads during suspend */
1da177e4
LT
63};
64
65/* All the per-cpu workqueues on the system, for hotplug cpu to add/remove
66 threads to each one as cpus come/go. */
d721304d 67static long migrate_sequence __read_mostly;
9b41ea72 68static DEFINE_MUTEX(workqueue_mutex);
1da177e4
LT
69static LIST_HEAD(workqueues);
70
f756d5e2
NL
71static int singlethread_cpu;
72
1da177e4
LT
73/* If it's single threaded, it isn't in the list of workqueues. */
74static inline int is_single_threaded(struct workqueue_struct *wq)
75{
76 return list_empty(&wq->list);
77}
78
4594bf15
DH
79/*
80 * Set the workqueue on which a work item is to be run
81 * - Must *only* be called if the pending flag is set
82 */
365970a1
DH
83static inline void set_wq_data(struct work_struct *work, void *wq)
84{
4594bf15
DH
85 unsigned long new;
86
87 BUG_ON(!work_pending(work));
365970a1 88
365970a1 89 new = (unsigned long) wq | (1UL << WORK_STRUCT_PENDING);
a08727ba
LT
90 new |= WORK_STRUCT_FLAG_MASK & *work_data_bits(work);
91 atomic_long_set(&work->data, new);
365970a1
DH
92}
93
94static inline void *get_wq_data(struct work_struct *work)
95{
a08727ba 96 return (void *) (atomic_long_read(&work->data) & WORK_STRUCT_WQ_DATA_MASK);
365970a1
DH
97}
98
68380b58
LT
99static int __run_work(struct cpu_workqueue_struct *cwq, struct work_struct *work)
100{
101 int ret = 0;
102 unsigned long flags;
103
104 spin_lock_irqsave(&cwq->lock, flags);
105 /*
106 * We need to re-validate the work info after we've gotten
107 * the cpu_workqueue lock. We can run the work now iff:
108 *
109 * - the wq_data still matches the cpu_workqueue_struct
110 * - AND the work is still marked pending
111 * - AND the work is still on a list (which will be this
112 * workqueue_struct list)
113 *
114 * All these conditions are important, because we
115 * need to protect against the work being run right
116 * now on another CPU (all but the last one might be
117 * true if it's currently running and has not been
118 * released yet, for example).
119 */
120 if (get_wq_data(work) == cwq
121 && work_pending(work)
122 && !list_empty(&work->entry)) {
123 work_func_t f = work->func;
b89deed3 124 cwq->current_work = work;
68380b58
LT
125 list_del_init(&work->entry);
126 spin_unlock_irqrestore(&cwq->lock, flags);
127
a08727ba 128 if (!test_bit(WORK_STRUCT_NOAUTOREL, work_data_bits(work)))
68380b58
LT
129 work_release(work);
130 f(work);
131
132 spin_lock_irqsave(&cwq->lock, flags);
b89deed3 133 cwq->current_work = NULL;
68380b58
LT
134 ret = 1;
135 }
136 spin_unlock_irqrestore(&cwq->lock, flags);
137 return ret;
138}
139
140/**
141 * run_scheduled_work - run scheduled work synchronously
142 * @work: work to run
143 *
144 * This checks if the work was pending, and runs it
145 * synchronously if so. It returns a boolean to indicate
146 * whether it had any scheduled work to run or not.
147 *
148 * NOTE! This _only_ works for normal work_structs. You
149 * CANNOT use this for delayed work, because the wq data
150 * for delayed work will not point properly to the per-
151 * CPU workqueue struct, but will change!
152 */
153int fastcall run_scheduled_work(struct work_struct *work)
154{
155 for (;;) {
156 struct cpu_workqueue_struct *cwq;
157
158 if (!work_pending(work))
159 return 0;
160 if (list_empty(&work->entry))
161 return 0;
162 /* NOTE! This depends intimately on __queue_work! */
163 cwq = get_wq_data(work);
164 if (!cwq)
165 return 0;
166 if (__run_work(cwq, work))
167 return 1;
168 }
169}
170EXPORT_SYMBOL(run_scheduled_work);
171
b89deed3
ON
172static void insert_work(struct cpu_workqueue_struct *cwq,
173 struct work_struct *work, int tail)
174{
175 set_wq_data(work, cwq);
176 if (tail)
177 list_add_tail(&work->entry, &cwq->worklist);
178 else
179 list_add(&work->entry, &cwq->worklist);
180 wake_up(&cwq->more_work);
181}
182
1da177e4
LT
183/* Preempt must be disabled. */
184static void __queue_work(struct cpu_workqueue_struct *cwq,
185 struct work_struct *work)
186{
187 unsigned long flags;
188
189 spin_lock_irqsave(&cwq->lock, flags);
b89deed3 190 insert_work(cwq, work, 1);
1da177e4
LT
191 spin_unlock_irqrestore(&cwq->lock, flags);
192}
193
0fcb78c2
REB
194/**
195 * queue_work - queue work on a workqueue
196 * @wq: workqueue to use
197 * @work: work to queue
198 *
057647fc 199 * Returns 0 if @work was already on a queue, non-zero otherwise.
1da177e4
LT
200 *
201 * We queue the work to the CPU it was submitted, but there is no
202 * guarantee that it will be processed by that CPU.
203 */
204int fastcall queue_work(struct workqueue_struct *wq, struct work_struct *work)
205{
206 int ret = 0, cpu = get_cpu();
207
a08727ba 208 if (!test_and_set_bit(WORK_STRUCT_PENDING, work_data_bits(work))) {
1da177e4 209 if (unlikely(is_single_threaded(wq)))
f756d5e2 210 cpu = singlethread_cpu;
1da177e4 211 BUG_ON(!list_empty(&work->entry));
89ada679 212 __queue_work(per_cpu_ptr(wq->cpu_wq, cpu), work);
1da177e4
LT
213 ret = 1;
214 }
215 put_cpu();
216 return ret;
217}
ae90dd5d 218EXPORT_SYMBOL_GPL(queue_work);
1da177e4 219
82f67cd9 220void delayed_work_timer_fn(unsigned long __data)
1da177e4 221{
52bad64d 222 struct delayed_work *dwork = (struct delayed_work *)__data;
365970a1 223 struct workqueue_struct *wq = get_wq_data(&dwork->work);
1da177e4
LT
224 int cpu = smp_processor_id();
225
226 if (unlikely(is_single_threaded(wq)))
f756d5e2 227 cpu = singlethread_cpu;
1da177e4 228
52bad64d 229 __queue_work(per_cpu_ptr(wq->cpu_wq, cpu), &dwork->work);
1da177e4
LT
230}
231
0fcb78c2
REB
232/**
233 * queue_delayed_work - queue work on a workqueue after delay
234 * @wq: workqueue to use
af9997e4 235 * @dwork: delayable work to queue
0fcb78c2
REB
236 * @delay: number of jiffies to wait before queueing
237 *
057647fc 238 * Returns 0 if @work was already on a queue, non-zero otherwise.
0fcb78c2 239 */
1da177e4 240int fastcall queue_delayed_work(struct workqueue_struct *wq,
52bad64d 241 struct delayed_work *dwork, unsigned long delay)
1da177e4
LT
242{
243 int ret = 0;
52bad64d
DH
244 struct timer_list *timer = &dwork->timer;
245 struct work_struct *work = &dwork->work;
246
82f67cd9 247 timer_stats_timer_set_start_info(timer);
52bad64d
DH
248 if (delay == 0)
249 return queue_work(wq, work);
1da177e4 250
a08727ba 251 if (!test_and_set_bit(WORK_STRUCT_PENDING, work_data_bits(work))) {
1da177e4
LT
252 BUG_ON(timer_pending(timer));
253 BUG_ON(!list_empty(&work->entry));
254
255 /* This stores wq for the moment, for the timer_fn */
365970a1 256 set_wq_data(work, wq);
1da177e4 257 timer->expires = jiffies + delay;
52bad64d 258 timer->data = (unsigned long)dwork;
1da177e4
LT
259 timer->function = delayed_work_timer_fn;
260 add_timer(timer);
261 ret = 1;
262 }
263 return ret;
264}
ae90dd5d 265EXPORT_SYMBOL_GPL(queue_delayed_work);
1da177e4 266
0fcb78c2
REB
267/**
268 * queue_delayed_work_on - queue work on specific CPU after delay
269 * @cpu: CPU number to execute work on
270 * @wq: workqueue to use
af9997e4 271 * @dwork: work to queue
0fcb78c2
REB
272 * @delay: number of jiffies to wait before queueing
273 *
057647fc 274 * Returns 0 if @work was already on a queue, non-zero otherwise.
0fcb78c2 275 */
7a6bc1cd 276int queue_delayed_work_on(int cpu, struct workqueue_struct *wq,
52bad64d 277 struct delayed_work *dwork, unsigned long delay)
7a6bc1cd
VP
278{
279 int ret = 0;
52bad64d
DH
280 struct timer_list *timer = &dwork->timer;
281 struct work_struct *work = &dwork->work;
7a6bc1cd 282
a08727ba 283 if (!test_and_set_bit(WORK_STRUCT_PENDING, work_data_bits(work))) {
7a6bc1cd
VP
284 BUG_ON(timer_pending(timer));
285 BUG_ON(!list_empty(&work->entry));
286
287 /* This stores wq for the moment, for the timer_fn */
365970a1 288 set_wq_data(work, wq);
7a6bc1cd 289 timer->expires = jiffies + delay;
52bad64d 290 timer->data = (unsigned long)dwork;
7a6bc1cd
VP
291 timer->function = delayed_work_timer_fn;
292 add_timer_on(timer, cpu);
293 ret = 1;
294 }
295 return ret;
296}
ae90dd5d 297EXPORT_SYMBOL_GPL(queue_delayed_work_on);
1da177e4 298
858119e1 299static void run_workqueue(struct cpu_workqueue_struct *cwq)
1da177e4
LT
300{
301 unsigned long flags;
302
303 /*
304 * Keep taking off work from the queue until
305 * done.
306 */
307 spin_lock_irqsave(&cwq->lock, flags);
308 cwq->run_depth++;
309 if (cwq->run_depth > 3) {
310 /* morton gets to eat his hat */
311 printk("%s: recursion depth exceeded: %d\n",
312 __FUNCTION__, cwq->run_depth);
313 dump_stack();
314 }
315 while (!list_empty(&cwq->worklist)) {
316 struct work_struct *work = list_entry(cwq->worklist.next,
317 struct work_struct, entry);
6bb49e59 318 work_func_t f = work->func;
1da177e4 319
b89deed3 320 cwq->current_work = work;
1da177e4
LT
321 list_del_init(cwq->worklist.next);
322 spin_unlock_irqrestore(&cwq->lock, flags);
323
365970a1 324 BUG_ON(get_wq_data(work) != cwq);
a08727ba 325 if (!test_bit(WORK_STRUCT_NOAUTOREL, work_data_bits(work)))
65f27f38
DH
326 work_release(work);
327 f(work);
1da177e4 328
d5abe669
PZ
329 if (unlikely(in_atomic() || lockdep_depth(current) > 0)) {
330 printk(KERN_ERR "BUG: workqueue leaked lock or atomic: "
331 "%s/0x%08x/%d\n",
332 current->comm, preempt_count(),
333 current->pid);
334 printk(KERN_ERR " last function: ");
335 print_symbol("%s\n", (unsigned long)f);
336 debug_show_held_locks(current);
337 dump_stack();
338 }
339
1da177e4 340 spin_lock_irqsave(&cwq->lock, flags);
b89deed3 341 cwq->current_work = NULL;
1da177e4
LT
342 }
343 cwq->run_depth--;
344 spin_unlock_irqrestore(&cwq->lock, flags);
345}
346
347static int worker_thread(void *__cwq)
348{
349 struct cpu_workqueue_struct *cwq = __cwq;
350 DECLARE_WAITQUEUE(wait, current);
351 struct k_sigaction sa;
352 sigset_t blocked;
353
319c2a98 354 if (!cwq->wq->freezeable)
341a5958 355 current->flags |= PF_NOFREEZE;
1da177e4
LT
356
357 set_user_nice(current, -5);
358
359 /* Block and flush all signals */
360 sigfillset(&blocked);
361 sigprocmask(SIG_BLOCK, &blocked, NULL);
362 flush_signals(current);
363
46934023
CL
364 /*
365 * We inherited MPOL_INTERLEAVE from the booting kernel.
366 * Set MPOL_DEFAULT to insure node local allocations.
367 */
368 numa_default_policy();
369
1da177e4
LT
370 /* SIG_IGN makes children autoreap: see do_notify_parent(). */
371 sa.sa.sa_handler = SIG_IGN;
372 sa.sa.sa_flags = 0;
373 siginitset(&sa.sa.sa_mask, sigmask(SIGCHLD));
374 do_sigaction(SIGCHLD, &sa, (struct k_sigaction *)0);
375
376 set_current_state(TASK_INTERRUPTIBLE);
377 while (!kthread_should_stop()) {
319c2a98 378 if (cwq->wq->freezeable)
341a5958
RW
379 try_to_freeze();
380
1da177e4
LT
381 add_wait_queue(&cwq->more_work, &wait);
382 if (list_empty(&cwq->worklist))
383 schedule();
384 else
385 __set_current_state(TASK_RUNNING);
386 remove_wait_queue(&cwq->more_work, &wait);
387
388 if (!list_empty(&cwq->worklist))
389 run_workqueue(cwq);
390 set_current_state(TASK_INTERRUPTIBLE);
391 }
392 __set_current_state(TASK_RUNNING);
393 return 0;
394}
395
fc2e4d70
ON
396struct wq_barrier {
397 struct work_struct work;
398 struct completion done;
399};
400
401static void wq_barrier_func(struct work_struct *work)
402{
403 struct wq_barrier *barr = container_of(work, struct wq_barrier, work);
404 complete(&barr->done);
405}
406
83c22520
ON
407static void insert_wq_barrier(struct cpu_workqueue_struct *cwq,
408 struct wq_barrier *barr, int tail)
fc2e4d70
ON
409{
410 INIT_WORK(&barr->work, wq_barrier_func);
411 __set_bit(WORK_STRUCT_PENDING, work_data_bits(&barr->work));
412
413 init_completion(&barr->done);
83c22520
ON
414
415 insert_work(cwq, &barr->work, tail);
fc2e4d70
ON
416}
417
1da177e4
LT
418static void flush_cpu_workqueue(struct cpu_workqueue_struct *cwq)
419{
420 if (cwq->thread == current) {
421 /*
422 * Probably keventd trying to flush its own queue. So simply run
423 * it by hand rather than deadlocking.
424 */
425 run_workqueue(cwq);
426 } else {
fc2e4d70 427 struct wq_barrier barr;
83c22520 428 int active = 0;
1da177e4 429
83c22520
ON
430 spin_lock_irq(&cwq->lock);
431 if (!list_empty(&cwq->worklist) || cwq->current_work != NULL) {
432 insert_wq_barrier(cwq, &barr, 1);
433 active = 1;
434 }
435 spin_unlock_irq(&cwq->lock);
1da177e4 436
d721304d 437 if (active)
83c22520 438 wait_for_completion(&barr.done);
1da177e4
LT
439 }
440}
441
0fcb78c2 442/**
1da177e4 443 * flush_workqueue - ensure that any scheduled work has run to completion.
0fcb78c2 444 * @wq: workqueue to flush
1da177e4
LT
445 *
446 * Forces execution of the workqueue and blocks until its completion.
447 * This is typically used in driver shutdown handlers.
448 *
fc2e4d70
ON
449 * We sleep until all works which were queued on entry have been handled,
450 * but we are not livelocked by new incoming ones.
1da177e4
LT
451 *
452 * This function used to run the workqueues itself. Now we just wait for the
453 * helper threads to do it.
454 */
455void fastcall flush_workqueue(struct workqueue_struct *wq)
456{
1da177e4 457 if (is_single_threaded(wq)) {
bce61dd4 458 /* Always use first cpu's area. */
f756d5e2 459 flush_cpu_workqueue(per_cpu_ptr(wq->cpu_wq, singlethread_cpu));
1da177e4 460 } else {
d721304d 461 long sequence;
1da177e4 462 int cpu;
d721304d
ON
463again:
464 sequence = migrate_sequence;
1da177e4 465
d721304d 466 for_each_possible_cpu(cpu)
89ada679 467 flush_cpu_workqueue(per_cpu_ptr(wq->cpu_wq, cpu));
d721304d
ON
468
469 if (unlikely(sequence != migrate_sequence))
470 goto again;
1da177e4
LT
471 }
472}
ae90dd5d 473EXPORT_SYMBOL_GPL(flush_workqueue);
1da177e4 474
b89deed3
ON
475static void wait_on_work(struct cpu_workqueue_struct *cwq,
476 struct work_struct *work)
477{
478 struct wq_barrier barr;
479 int running = 0;
480
481 spin_lock_irq(&cwq->lock);
482 if (unlikely(cwq->current_work == work)) {
83c22520 483 insert_wq_barrier(cwq, &barr, 0);
b89deed3
ON
484 running = 1;
485 }
486 spin_unlock_irq(&cwq->lock);
487
488 if (unlikely(running)) {
489 mutex_unlock(&workqueue_mutex);
490 wait_for_completion(&barr.done);
491 mutex_lock(&workqueue_mutex);
492 }
493}
494
495/**
496 * flush_work - block until a work_struct's callback has terminated
497 * @wq: the workqueue on which the work is queued
498 * @work: the work which is to be flushed
499 *
500 * flush_work() will attempt to cancel the work if it is queued. If the work's
501 * callback appears to be running, flush_work() will block until it has
502 * completed.
503 *
504 * flush_work() is designed to be used when the caller is tearing down data
505 * structures which the callback function operates upon. It is expected that,
506 * prior to calling flush_work(), the caller has arranged for the work to not
507 * be requeued.
508 */
509void flush_work(struct workqueue_struct *wq, struct work_struct *work)
510{
511 struct cpu_workqueue_struct *cwq;
512
513 mutex_lock(&workqueue_mutex);
514 cwq = get_wq_data(work);
515 /* Was it ever queued ? */
516 if (!cwq)
517 goto out;
518
519 /*
520 * This work can't be re-queued, and the lock above protects us
521 * from take_over_work(), no need to re-check that get_wq_data()
522 * is still the same when we take cwq->lock.
523 */
524 spin_lock_irq(&cwq->lock);
525 list_del_init(&work->entry);
526 work_release(work);
527 spin_unlock_irq(&cwq->lock);
528
529 if (is_single_threaded(wq)) {
530 /* Always use first cpu's area. */
531 wait_on_work(per_cpu_ptr(wq->cpu_wq, singlethread_cpu), work);
532 } else {
533 int cpu;
534
535 for_each_online_cpu(cpu)
536 wait_on_work(per_cpu_ptr(wq->cpu_wq, cpu), work);
537 }
538out:
539 mutex_unlock(&workqueue_mutex);
540}
541EXPORT_SYMBOL_GPL(flush_work);
542
d721304d 543static void init_cpu_workqueue(struct workqueue_struct *wq, int cpu)
1da177e4 544{
89ada679 545 struct cpu_workqueue_struct *cwq = per_cpu_ptr(wq->cpu_wq, cpu);
1da177e4 546
1da177e4 547 cwq->wq = wq;
d721304d 548 spin_lock_init(&cwq->lock);
1da177e4
LT
549 INIT_LIST_HEAD(&cwq->worklist);
550 init_waitqueue_head(&cwq->more_work);
d721304d
ON
551}
552
553static struct task_struct *create_workqueue_thread(struct workqueue_struct *wq,
554 int cpu)
555{
556 struct cpu_workqueue_struct *cwq = per_cpu_ptr(wq->cpu_wq, cpu);
557 struct task_struct *p;
1da177e4
LT
558
559 if (is_single_threaded(wq))
560 p = kthread_create(worker_thread, cwq, "%s", wq->name);
561 else
562 p = kthread_create(worker_thread, cwq, "%s/%d", wq->name, cpu);
563 if (IS_ERR(p))
564 return NULL;
565 cwq->thread = p;
566 return p;
567}
568
569struct workqueue_struct *__create_workqueue(const char *name,
341a5958 570 int singlethread, int freezeable)
1da177e4
LT
571{
572 int cpu, destroy = 0;
573 struct workqueue_struct *wq;
574 struct task_struct *p;
575
dd392710 576 wq = kzalloc(sizeof(*wq), GFP_KERNEL);
1da177e4
LT
577 if (!wq)
578 return NULL;
1da177e4 579
89ada679 580 wq->cpu_wq = alloc_percpu(struct cpu_workqueue_struct);
676121fc
BC
581 if (!wq->cpu_wq) {
582 kfree(wq);
583 return NULL;
584 }
585
1da177e4 586 wq->name = name;
319c2a98
ON
587 wq->freezeable = freezeable;
588
9b41ea72 589 mutex_lock(&workqueue_mutex);
1da177e4
LT
590 if (singlethread) {
591 INIT_LIST_HEAD(&wq->list);
d721304d 592 init_cpu_workqueue(wq, singlethread_cpu);
319c2a98 593 p = create_workqueue_thread(wq, singlethread_cpu);
1da177e4
LT
594 if (!p)
595 destroy = 1;
596 else
597 wake_up_process(p);
598 } else {
1da177e4 599 list_add(&wq->list, &workqueues);
d721304d
ON
600 for_each_possible_cpu(cpu) {
601 init_cpu_workqueue(wq, cpu);
602 if (!cpu_online(cpu))
603 continue;
604
319c2a98 605 p = create_workqueue_thread(wq, cpu);
1da177e4
LT
606 if (p) {
607 kthread_bind(p, cpu);
608 wake_up_process(p);
609 } else
610 destroy = 1;
611 }
612 }
9b41ea72 613 mutex_unlock(&workqueue_mutex);
1da177e4
LT
614
615 /*
616 * Was there any error during startup? If yes then clean up:
617 */
618 if (destroy) {
619 destroy_workqueue(wq);
620 wq = NULL;
621 }
622 return wq;
623}
ae90dd5d 624EXPORT_SYMBOL_GPL(__create_workqueue);
1da177e4
LT
625
626static void cleanup_workqueue_thread(struct workqueue_struct *wq, int cpu)
627{
36aa9dfc 628 struct cpu_workqueue_struct *cwq = per_cpu_ptr(wq->cpu_wq, cpu);
1da177e4 629
36aa9dfc
ON
630 if (cwq->thread) {
631 kthread_stop(cwq->thread);
632 cwq->thread = NULL;
633 }
1da177e4
LT
634}
635
0fcb78c2
REB
636/**
637 * destroy_workqueue - safely terminate a workqueue
638 * @wq: target workqueue
639 *
640 * Safely destroy a workqueue. All work currently pending will be done first.
641 */
1da177e4
LT
642void destroy_workqueue(struct workqueue_struct *wq)
643{
644 int cpu;
645
646 flush_workqueue(wq);
647
648 /* We don't need the distraction of CPUs appearing and vanishing. */
9b41ea72 649 mutex_lock(&workqueue_mutex);
1da177e4 650 if (is_single_threaded(wq))
f756d5e2 651 cleanup_workqueue_thread(wq, singlethread_cpu);
1da177e4
LT
652 else {
653 for_each_online_cpu(cpu)
654 cleanup_workqueue_thread(wq, cpu);
1da177e4 655 list_del(&wq->list);
1da177e4 656 }
9b41ea72 657 mutex_unlock(&workqueue_mutex);
89ada679 658 free_percpu(wq->cpu_wq);
1da177e4
LT
659 kfree(wq);
660}
ae90dd5d 661EXPORT_SYMBOL_GPL(destroy_workqueue);
1da177e4
LT
662
663static struct workqueue_struct *keventd_wq;
664
0fcb78c2
REB
665/**
666 * schedule_work - put work task in global workqueue
667 * @work: job to be done
668 *
669 * This puts a job in the kernel-global workqueue.
670 */
1da177e4
LT
671int fastcall schedule_work(struct work_struct *work)
672{
673 return queue_work(keventd_wq, work);
674}
ae90dd5d 675EXPORT_SYMBOL(schedule_work);
1da177e4 676
0fcb78c2
REB
677/**
678 * schedule_delayed_work - put work task in global workqueue after delay
52bad64d
DH
679 * @dwork: job to be done
680 * @delay: number of jiffies to wait or 0 for immediate execution
0fcb78c2
REB
681 *
682 * After waiting for a given time this puts a job in the kernel-global
683 * workqueue.
684 */
82f67cd9
IM
685int fastcall schedule_delayed_work(struct delayed_work *dwork,
686 unsigned long delay)
1da177e4 687{
82f67cd9 688 timer_stats_timer_set_start_info(&dwork->timer);
52bad64d 689 return queue_delayed_work(keventd_wq, dwork, delay);
1da177e4 690}
ae90dd5d 691EXPORT_SYMBOL(schedule_delayed_work);
1da177e4 692
0fcb78c2
REB
693/**
694 * schedule_delayed_work_on - queue work in global workqueue on CPU after delay
695 * @cpu: cpu to use
52bad64d 696 * @dwork: job to be done
0fcb78c2
REB
697 * @delay: number of jiffies to wait
698 *
699 * After waiting for a given time this puts a job in the kernel-global
700 * workqueue on the specified CPU.
701 */
1da177e4 702int schedule_delayed_work_on(int cpu,
52bad64d 703 struct delayed_work *dwork, unsigned long delay)
1da177e4 704{
52bad64d 705 return queue_delayed_work_on(cpu, keventd_wq, dwork, delay);
1da177e4 706}
ae90dd5d 707EXPORT_SYMBOL(schedule_delayed_work_on);
1da177e4 708
b6136773
AM
709/**
710 * schedule_on_each_cpu - call a function on each online CPU from keventd
711 * @func: the function to call
b6136773
AM
712 *
713 * Returns zero on success.
714 * Returns -ve errno on failure.
715 *
716 * Appears to be racy against CPU hotplug.
717 *
718 * schedule_on_each_cpu() is very slow.
719 */
65f27f38 720int schedule_on_each_cpu(work_func_t func)
15316ba8
CL
721{
722 int cpu;
b6136773 723 struct work_struct *works;
15316ba8 724
b6136773
AM
725 works = alloc_percpu(struct work_struct);
726 if (!works)
15316ba8 727 return -ENOMEM;
b6136773 728
e18f3ffb 729 preempt_disable(); /* CPU hotplug */
15316ba8 730 for_each_online_cpu(cpu) {
9bfb1839
IM
731 struct work_struct *work = per_cpu_ptr(works, cpu);
732
733 INIT_WORK(work, func);
734 set_bit(WORK_STRUCT_PENDING, work_data_bits(work));
735 __queue_work(per_cpu_ptr(keventd_wq->cpu_wq, cpu), work);
15316ba8 736 }
e18f3ffb 737 preempt_enable();
15316ba8 738 flush_workqueue(keventd_wq);
b6136773 739 free_percpu(works);
15316ba8
CL
740 return 0;
741}
742
1da177e4
LT
743void flush_scheduled_work(void)
744{
745 flush_workqueue(keventd_wq);
746}
ae90dd5d 747EXPORT_SYMBOL(flush_scheduled_work);
1da177e4 748
b89deed3
ON
749void flush_work_keventd(struct work_struct *work)
750{
751 flush_work(keventd_wq, work);
752}
753EXPORT_SYMBOL(flush_work_keventd);
754
1da177e4 755/**
72fd4a35 756 * cancel_rearming_delayed_workqueue - reliably kill off a delayed work whose handler rearms the delayed work.
1da177e4 757 * @wq: the controlling workqueue structure
52bad64d 758 * @dwork: the delayed work struct
1da177e4 759 */
81ddef77 760void cancel_rearming_delayed_workqueue(struct workqueue_struct *wq,
52bad64d 761 struct delayed_work *dwork)
1da177e4 762{
52bad64d 763 while (!cancel_delayed_work(dwork))
1da177e4
LT
764 flush_workqueue(wq);
765}
81ddef77 766EXPORT_SYMBOL(cancel_rearming_delayed_workqueue);
1da177e4
LT
767
768/**
72fd4a35 769 * cancel_rearming_delayed_work - reliably kill off a delayed keventd work whose handler rearms the delayed work.
52bad64d 770 * @dwork: the delayed work struct
1da177e4 771 */
52bad64d 772void cancel_rearming_delayed_work(struct delayed_work *dwork)
1da177e4 773{
52bad64d 774 cancel_rearming_delayed_workqueue(keventd_wq, dwork);
1da177e4
LT
775}
776EXPORT_SYMBOL(cancel_rearming_delayed_work);
777
1fa44eca
JB
778/**
779 * execute_in_process_context - reliably execute the routine with user context
780 * @fn: the function to execute
1fa44eca
JB
781 * @ew: guaranteed storage for the execute work structure (must
782 * be available when the work executes)
783 *
784 * Executes the function immediately if process context is available,
785 * otherwise schedules the function for delayed execution.
786 *
787 * Returns: 0 - function was executed
788 * 1 - function was scheduled for execution
789 */
65f27f38 790int execute_in_process_context(work_func_t fn, struct execute_work *ew)
1fa44eca
JB
791{
792 if (!in_interrupt()) {
65f27f38 793 fn(&ew->work);
1fa44eca
JB
794 return 0;
795 }
796
65f27f38 797 INIT_WORK(&ew->work, fn);
1fa44eca
JB
798 schedule_work(&ew->work);
799
800 return 1;
801}
802EXPORT_SYMBOL_GPL(execute_in_process_context);
803
1da177e4
LT
804int keventd_up(void)
805{
806 return keventd_wq != NULL;
807}
808
809int current_is_keventd(void)
810{
811 struct cpu_workqueue_struct *cwq;
812 int cpu = smp_processor_id(); /* preempt-safe: keventd is per-cpu */
813 int ret = 0;
814
815 BUG_ON(!keventd_wq);
816
89ada679 817 cwq = per_cpu_ptr(keventd_wq->cpu_wq, cpu);
1da177e4
LT
818 if (current == cwq->thread)
819 ret = 1;
820
821 return ret;
822
823}
824
1da177e4
LT
825/* Take the work from this (downed) CPU. */
826static void take_over_work(struct workqueue_struct *wq, unsigned int cpu)
827{
89ada679 828 struct cpu_workqueue_struct *cwq = per_cpu_ptr(wq->cpu_wq, cpu);
626ab0e6 829 struct list_head list;
1da177e4
LT
830 struct work_struct *work;
831
832 spin_lock_irq(&cwq->lock);
626ab0e6 833 list_replace_init(&cwq->worklist, &list);
d721304d 834 migrate_sequence++;
1da177e4
LT
835
836 while (!list_empty(&list)) {
837 printk("Taking work for %s\n", wq->name);
838 work = list_entry(list.next,struct work_struct,entry);
839 list_del(&work->entry);
89ada679 840 __queue_work(per_cpu_ptr(wq->cpu_wq, smp_processor_id()), work);
1da177e4
LT
841 }
842 spin_unlock_irq(&cwq->lock);
843}
844
845/* We're holding the cpucontrol mutex here */
9c7b216d 846static int __devinit workqueue_cpu_callback(struct notifier_block *nfb,
1da177e4
LT
847 unsigned long action,
848 void *hcpu)
849{
850 unsigned int hotcpu = (unsigned long)hcpu;
851 struct workqueue_struct *wq;
852
853 switch (action) {
854 case CPU_UP_PREPARE:
9b41ea72 855 mutex_lock(&workqueue_mutex);
1da177e4
LT
856 /* Create a new workqueue thread for it. */
857 list_for_each_entry(wq, &workqueues, list) {
319c2a98 858 if (!create_workqueue_thread(wq, hotcpu)) {
1da177e4
LT
859 printk("workqueue for %i failed\n", hotcpu);
860 return NOTIFY_BAD;
861 }
862 }
863 break;
864
865 case CPU_ONLINE:
866 /* Kick off worker threads. */
867 list_for_each_entry(wq, &workqueues, list) {
89ada679
CL
868 struct cpu_workqueue_struct *cwq;
869
870 cwq = per_cpu_ptr(wq->cpu_wq, hotcpu);
871 kthread_bind(cwq->thread, hotcpu);
872 wake_up_process(cwq->thread);
1da177e4 873 }
9b41ea72 874 mutex_unlock(&workqueue_mutex);
1da177e4
LT
875 break;
876
877 case CPU_UP_CANCELED:
878 list_for_each_entry(wq, &workqueues, list) {
fc75cdfa
HC
879 if (!per_cpu_ptr(wq->cpu_wq, hotcpu)->thread)
880 continue;
1da177e4 881 /* Unbind so it can run. */
89ada679 882 kthread_bind(per_cpu_ptr(wq->cpu_wq, hotcpu)->thread,
a4c4af7c 883 any_online_cpu(cpu_online_map));
1da177e4
LT
884 cleanup_workqueue_thread(wq, hotcpu);
885 }
9b41ea72
AM
886 mutex_unlock(&workqueue_mutex);
887 break;
888
889 case CPU_DOWN_PREPARE:
890 mutex_lock(&workqueue_mutex);
891 break;
892
893 case CPU_DOWN_FAILED:
894 mutex_unlock(&workqueue_mutex);
1da177e4
LT
895 break;
896
897 case CPU_DEAD:
898 list_for_each_entry(wq, &workqueues, list)
899 cleanup_workqueue_thread(wq, hotcpu);
900 list_for_each_entry(wq, &workqueues, list)
901 take_over_work(wq, hotcpu);
9b41ea72 902 mutex_unlock(&workqueue_mutex);
1da177e4
LT
903 break;
904 }
905
906 return NOTIFY_OK;
907}
1da177e4
LT
908
909void init_workqueues(void)
910{
f756d5e2 911 singlethread_cpu = first_cpu(cpu_possible_map);
1da177e4
LT
912 hotcpu_notifier(workqueue_cpu_callback, 0);
913 keventd_wq = create_workqueue("events");
914 BUG_ON(!keventd_wq);
915}
916