pm: introduce new interfaces schedule_work_on() and queue_work_on()
[GitHub/mt8127/android_kernel_alcatel_ttab.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 15 *
cde53535 16 * Made to use alloc_percpu by Christoph Lameter.
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>
4e6045f1 35#include <linux/lockdep.h>
1da177e4
LT
36
37/*
f756d5e2
NL
38 * The per-CPU workqueue (if single thread, we always use the first
39 * possible cpu).
1da177e4
LT
40 */
41struct cpu_workqueue_struct {
42
43 spinlock_t lock;
44
1da177e4
LT
45 struct list_head worklist;
46 wait_queue_head_t more_work;
3af24433 47 struct work_struct *current_work;
1da177e4
LT
48
49 struct workqueue_struct *wq;
36c8b586 50 struct task_struct *thread;
1da177e4
LT
51
52 int run_depth; /* Detect run_workqueue() recursion depth */
53} ____cacheline_aligned;
54
55/*
56 * The externally visible workqueue abstraction is an array of
57 * per-CPU workqueues:
58 */
59struct workqueue_struct {
89ada679 60 struct cpu_workqueue_struct *cpu_wq;
cce1a165 61 struct list_head list;
1da177e4 62 const char *name;
cce1a165 63 int singlethread;
319c2a98 64 int freezeable; /* Freeze threads during suspend */
4e6045f1
JB
65#ifdef CONFIG_LOCKDEP
66 struct lockdep_map lockdep_map;
67#endif
1da177e4
LT
68};
69
95402b38
GS
70/* Serializes the accesses to the list of workqueues. */
71static DEFINE_SPINLOCK(workqueue_lock);
1da177e4
LT
72static LIST_HEAD(workqueues);
73
3af24433 74static int singlethread_cpu __read_mostly;
b1f4ec17 75static cpumask_t cpu_singlethread_map __read_mostly;
14441960
ON
76/*
77 * _cpu_down() first removes CPU from cpu_online_map, then CPU_DEAD
78 * flushes cwq->worklist. This means that flush_workqueue/wait_on_work
79 * which comes in between can't use for_each_online_cpu(). We could
80 * use cpu_possible_map, the cpumask below is more a documentation
81 * than optimization.
82 */
3af24433 83static cpumask_t cpu_populated_map __read_mostly;
f756d5e2 84
1da177e4
LT
85/* If it's single threaded, it isn't in the list of workqueues. */
86static inline int is_single_threaded(struct workqueue_struct *wq)
87{
cce1a165 88 return wq->singlethread;
1da177e4
LT
89}
90
b1f4ec17
ON
91static const cpumask_t *wq_cpu_map(struct workqueue_struct *wq)
92{
93 return is_single_threaded(wq)
94 ? &cpu_singlethread_map : &cpu_populated_map;
95}
96
a848e3b6
ON
97static
98struct cpu_workqueue_struct *wq_per_cpu(struct workqueue_struct *wq, int cpu)
99{
100 if (unlikely(is_single_threaded(wq)))
101 cpu = singlethread_cpu;
102 return per_cpu_ptr(wq->cpu_wq, cpu);
103}
104
4594bf15
DH
105/*
106 * Set the workqueue on which a work item is to be run
107 * - Must *only* be called if the pending flag is set
108 */
ed7c0fee
ON
109static inline void set_wq_data(struct work_struct *work,
110 struct cpu_workqueue_struct *cwq)
365970a1 111{
4594bf15
DH
112 unsigned long new;
113
114 BUG_ON(!work_pending(work));
365970a1 115
ed7c0fee 116 new = (unsigned long) cwq | (1UL << WORK_STRUCT_PENDING);
a08727ba
LT
117 new |= WORK_STRUCT_FLAG_MASK & *work_data_bits(work);
118 atomic_long_set(&work->data, new);
365970a1
DH
119}
120
ed7c0fee
ON
121static inline
122struct cpu_workqueue_struct *get_wq_data(struct work_struct *work)
365970a1 123{
a08727ba 124 return (void *) (atomic_long_read(&work->data) & WORK_STRUCT_WQ_DATA_MASK);
365970a1
DH
125}
126
b89deed3
ON
127static void insert_work(struct cpu_workqueue_struct *cwq,
128 struct work_struct *work, int tail)
129{
130 set_wq_data(work, cwq);
6e84d644
ON
131 /*
132 * Ensure that we get the right work->data if we see the
133 * result of list_add() below, see try_to_grab_pending().
134 */
135 smp_wmb();
b89deed3
ON
136 if (tail)
137 list_add_tail(&work->entry, &cwq->worklist);
138 else
139 list_add(&work->entry, &cwq->worklist);
140 wake_up(&cwq->more_work);
141}
142
1da177e4
LT
143static void __queue_work(struct cpu_workqueue_struct *cwq,
144 struct work_struct *work)
145{
146 unsigned long flags;
147
148 spin_lock_irqsave(&cwq->lock, flags);
b89deed3 149 insert_work(cwq, work, 1);
1da177e4
LT
150 spin_unlock_irqrestore(&cwq->lock, flags);
151}
152
0fcb78c2
REB
153/**
154 * queue_work - queue work on a workqueue
155 * @wq: workqueue to use
156 * @work: work to queue
157 *
057647fc 158 * Returns 0 if @work was already on a queue, non-zero otherwise.
1da177e4 159 *
00dfcaf7
ON
160 * We queue the work to the CPU on which it was submitted, but if the CPU dies
161 * it can be processed by another CPU.
1da177e4 162 */
7ad5b3a5 163int queue_work(struct workqueue_struct *wq, struct work_struct *work)
1da177e4 164{
a848e3b6 165 int ret = 0;
1da177e4 166
a08727ba 167 if (!test_and_set_bit(WORK_STRUCT_PENDING, work_data_bits(work))) {
1da177e4 168 BUG_ON(!list_empty(&work->entry));
a848e3b6
ON
169 __queue_work(wq_per_cpu(wq, get_cpu()), work);
170 put_cpu();
1da177e4
LT
171 ret = 1;
172 }
1da177e4
LT
173 return ret;
174}
ae90dd5d 175EXPORT_SYMBOL_GPL(queue_work);
1da177e4 176
c1a220e7
ZR
177/**
178 * queue_work_on - queue work on specific cpu
179 * @cpu: CPU number to execute work on
180 * @wq: workqueue to use
181 * @work: work to queue
182 *
183 * Returns 0 if @work was already on a queue, non-zero otherwise.
184 *
185 * We queue the work to a specific CPU, the caller must ensure it
186 * can't go away.
187 */
188int
189queue_work_on(int cpu, struct workqueue_struct *wq, struct work_struct *work)
190{
191 int ret = 0;
192
193 if (!test_and_set_bit(WORK_STRUCT_PENDING, work_data_bits(work))) {
194 BUG_ON(!list_empty(&work->entry));
195 __queue_work(wq_per_cpu(wq, cpu), work);
196 ret = 1;
197 }
198 return ret;
199}
200EXPORT_SYMBOL_GPL(queue_work_on);
201
6d141c3f 202static void delayed_work_timer_fn(unsigned long __data)
1da177e4 203{
52bad64d 204 struct delayed_work *dwork = (struct delayed_work *)__data;
ed7c0fee
ON
205 struct cpu_workqueue_struct *cwq = get_wq_data(&dwork->work);
206 struct workqueue_struct *wq = cwq->wq;
1da177e4 207
a848e3b6 208 __queue_work(wq_per_cpu(wq, smp_processor_id()), &dwork->work);
1da177e4
LT
209}
210
0fcb78c2
REB
211/**
212 * queue_delayed_work - queue work on a workqueue after delay
213 * @wq: workqueue to use
af9997e4 214 * @dwork: delayable work to queue
0fcb78c2
REB
215 * @delay: number of jiffies to wait before queueing
216 *
057647fc 217 * Returns 0 if @work was already on a queue, non-zero otherwise.
0fcb78c2 218 */
7ad5b3a5 219int queue_delayed_work(struct workqueue_struct *wq,
52bad64d 220 struct delayed_work *dwork, unsigned long delay)
1da177e4 221{
52bad64d 222 if (delay == 0)
63bc0362 223 return queue_work(wq, &dwork->work);
1da177e4 224
63bc0362 225 return queue_delayed_work_on(-1, wq, dwork, delay);
1da177e4 226}
ae90dd5d 227EXPORT_SYMBOL_GPL(queue_delayed_work);
1da177e4 228
0fcb78c2
REB
229/**
230 * queue_delayed_work_on - queue work on specific CPU after delay
231 * @cpu: CPU number to execute work on
232 * @wq: workqueue to use
af9997e4 233 * @dwork: work to queue
0fcb78c2
REB
234 * @delay: number of jiffies to wait before queueing
235 *
057647fc 236 * Returns 0 if @work was already on a queue, non-zero otherwise.
0fcb78c2 237 */
7a6bc1cd 238int queue_delayed_work_on(int cpu, struct workqueue_struct *wq,
52bad64d 239 struct delayed_work *dwork, unsigned long delay)
7a6bc1cd
VP
240{
241 int ret = 0;
52bad64d
DH
242 struct timer_list *timer = &dwork->timer;
243 struct work_struct *work = &dwork->work;
7a6bc1cd 244
a08727ba 245 if (!test_and_set_bit(WORK_STRUCT_PENDING, work_data_bits(work))) {
7a6bc1cd
VP
246 BUG_ON(timer_pending(timer));
247 BUG_ON(!list_empty(&work->entry));
248
8a3e77cc
AL
249 timer_stats_timer_set_start_info(&dwork->timer);
250
ed7c0fee 251 /* This stores cwq for the moment, for the timer_fn */
a848e3b6 252 set_wq_data(work, wq_per_cpu(wq, raw_smp_processor_id()));
7a6bc1cd 253 timer->expires = jiffies + delay;
52bad64d 254 timer->data = (unsigned long)dwork;
7a6bc1cd 255 timer->function = delayed_work_timer_fn;
63bc0362
ON
256
257 if (unlikely(cpu >= 0))
258 add_timer_on(timer, cpu);
259 else
260 add_timer(timer);
7a6bc1cd
VP
261 ret = 1;
262 }
263 return ret;
264}
ae90dd5d 265EXPORT_SYMBOL_GPL(queue_delayed_work_on);
1da177e4 266
858119e1 267static void run_workqueue(struct cpu_workqueue_struct *cwq)
1da177e4 268{
f293ea92 269 spin_lock_irq(&cwq->lock);
1da177e4
LT
270 cwq->run_depth++;
271 if (cwq->run_depth > 3) {
272 /* morton gets to eat his hat */
273 printk("%s: recursion depth exceeded: %d\n",
af1f16d0 274 __func__, cwq->run_depth);
1da177e4
LT
275 dump_stack();
276 }
277 while (!list_empty(&cwq->worklist)) {
278 struct work_struct *work = list_entry(cwq->worklist.next,
279 struct work_struct, entry);
6bb49e59 280 work_func_t f = work->func;
4e6045f1
JB
281#ifdef CONFIG_LOCKDEP
282 /*
283 * It is permissible to free the struct work_struct
284 * from inside the function that is called from it,
285 * this we need to take into account for lockdep too.
286 * To avoid bogus "held lock freed" warnings as well
287 * as problems when looking into work->lockdep_map,
288 * make a copy and use that here.
289 */
290 struct lockdep_map lockdep_map = work->lockdep_map;
291#endif
1da177e4 292
b89deed3 293 cwq->current_work = work;
1da177e4 294 list_del_init(cwq->worklist.next);
f293ea92 295 spin_unlock_irq(&cwq->lock);
1da177e4 296
365970a1 297 BUG_ON(get_wq_data(work) != cwq);
23b2e599 298 work_clear_pending(work);
4e6045f1
JB
299 lock_acquire(&cwq->wq->lockdep_map, 0, 0, 0, 2, _THIS_IP_);
300 lock_acquire(&lockdep_map, 0, 0, 0, 2, _THIS_IP_);
65f27f38 301 f(work);
4e6045f1
JB
302 lock_release(&lockdep_map, 1, _THIS_IP_);
303 lock_release(&cwq->wq->lockdep_map, 1, _THIS_IP_);
1da177e4 304
d5abe669
PZ
305 if (unlikely(in_atomic() || lockdep_depth(current) > 0)) {
306 printk(KERN_ERR "BUG: workqueue leaked lock or atomic: "
307 "%s/0x%08x/%d\n",
308 current->comm, preempt_count(),
ba25f9dc 309 task_pid_nr(current));
d5abe669
PZ
310 printk(KERN_ERR " last function: ");
311 print_symbol("%s\n", (unsigned long)f);
312 debug_show_held_locks(current);
313 dump_stack();
314 }
315
f293ea92 316 spin_lock_irq(&cwq->lock);
b89deed3 317 cwq->current_work = NULL;
1da177e4
LT
318 }
319 cwq->run_depth--;
f293ea92 320 spin_unlock_irq(&cwq->lock);
1da177e4
LT
321}
322
323static int worker_thread(void *__cwq)
324{
325 struct cpu_workqueue_struct *cwq = __cwq;
3af24433 326 DEFINE_WAIT(wait);
1da177e4 327
83144186
RW
328 if (cwq->wq->freezeable)
329 set_freezable();
1da177e4
LT
330
331 set_user_nice(current, -5);
1da177e4 332
3af24433 333 for (;;) {
3af24433 334 prepare_to_wait(&cwq->more_work, &wait, TASK_INTERRUPTIBLE);
14441960
ON
335 if (!freezing(current) &&
336 !kthread_should_stop() &&
337 list_empty(&cwq->worklist))
1da177e4 338 schedule();
3af24433
ON
339 finish_wait(&cwq->more_work, &wait);
340
85f4186a
ON
341 try_to_freeze();
342
14441960 343 if (kthread_should_stop())
3af24433 344 break;
1da177e4 345
3af24433 346 run_workqueue(cwq);
1da177e4 347 }
3af24433 348
1da177e4
LT
349 return 0;
350}
351
fc2e4d70
ON
352struct wq_barrier {
353 struct work_struct work;
354 struct completion done;
355};
356
357static void wq_barrier_func(struct work_struct *work)
358{
359 struct wq_barrier *barr = container_of(work, struct wq_barrier, work);
360 complete(&barr->done);
361}
362
83c22520
ON
363static void insert_wq_barrier(struct cpu_workqueue_struct *cwq,
364 struct wq_barrier *barr, int tail)
fc2e4d70
ON
365{
366 INIT_WORK(&barr->work, wq_barrier_func);
367 __set_bit(WORK_STRUCT_PENDING, work_data_bits(&barr->work));
368
369 init_completion(&barr->done);
83c22520
ON
370
371 insert_work(cwq, &barr->work, tail);
fc2e4d70
ON
372}
373
14441960 374static int flush_cpu_workqueue(struct cpu_workqueue_struct *cwq)
1da177e4 375{
14441960
ON
376 int active;
377
1da177e4
LT
378 if (cwq->thread == current) {
379 /*
380 * Probably keventd trying to flush its own queue. So simply run
381 * it by hand rather than deadlocking.
382 */
383 run_workqueue(cwq);
14441960 384 active = 1;
1da177e4 385 } else {
fc2e4d70 386 struct wq_barrier barr;
1da177e4 387
14441960 388 active = 0;
83c22520
ON
389 spin_lock_irq(&cwq->lock);
390 if (!list_empty(&cwq->worklist) || cwq->current_work != NULL) {
391 insert_wq_barrier(cwq, &barr, 1);
392 active = 1;
393 }
394 spin_unlock_irq(&cwq->lock);
1da177e4 395
d721304d 396 if (active)
83c22520 397 wait_for_completion(&barr.done);
1da177e4 398 }
14441960
ON
399
400 return active;
1da177e4
LT
401}
402
0fcb78c2 403/**
1da177e4 404 * flush_workqueue - ensure that any scheduled work has run to completion.
0fcb78c2 405 * @wq: workqueue to flush
1da177e4
LT
406 *
407 * Forces execution of the workqueue and blocks until its completion.
408 * This is typically used in driver shutdown handlers.
409 *
fc2e4d70
ON
410 * We sleep until all works which were queued on entry have been handled,
411 * but we are not livelocked by new incoming ones.
1da177e4
LT
412 *
413 * This function used to run the workqueues itself. Now we just wait for the
414 * helper threads to do it.
415 */
7ad5b3a5 416void flush_workqueue(struct workqueue_struct *wq)
1da177e4 417{
b1f4ec17 418 const cpumask_t *cpu_map = wq_cpu_map(wq);
cce1a165 419 int cpu;
1da177e4 420
b1f4ec17 421 might_sleep();
4e6045f1
JB
422 lock_acquire(&wq->lockdep_map, 0, 0, 0, 2, _THIS_IP_);
423 lock_release(&wq->lockdep_map, 1, _THIS_IP_);
363ab6f1 424 for_each_cpu_mask_nr(cpu, *cpu_map)
b1f4ec17 425 flush_cpu_workqueue(per_cpu_ptr(wq->cpu_wq, cpu));
1da177e4 426}
ae90dd5d 427EXPORT_SYMBOL_GPL(flush_workqueue);
1da177e4 428
6e84d644 429/*
1f1f642e 430 * Upon a successful return (>= 0), the caller "owns" WORK_STRUCT_PENDING bit,
6e84d644
ON
431 * so this work can't be re-armed in any way.
432 */
433static int try_to_grab_pending(struct work_struct *work)
434{
435 struct cpu_workqueue_struct *cwq;
1f1f642e 436 int ret = -1;
6e84d644
ON
437
438 if (!test_and_set_bit(WORK_STRUCT_PENDING, work_data_bits(work)))
1f1f642e 439 return 0;
6e84d644
ON
440
441 /*
442 * The queueing is in progress, or it is already queued. Try to
443 * steal it from ->worklist without clearing WORK_STRUCT_PENDING.
444 */
445
446 cwq = get_wq_data(work);
447 if (!cwq)
448 return ret;
449
450 spin_lock_irq(&cwq->lock);
451 if (!list_empty(&work->entry)) {
452 /*
453 * This work is queued, but perhaps we locked the wrong cwq.
454 * In that case we must see the new value after rmb(), see
455 * insert_work()->wmb().
456 */
457 smp_rmb();
458 if (cwq == get_wq_data(work)) {
459 list_del_init(&work->entry);
460 ret = 1;
461 }
462 }
463 spin_unlock_irq(&cwq->lock);
464
465 return ret;
466}
467
468static void wait_on_cpu_work(struct cpu_workqueue_struct *cwq,
b89deed3
ON
469 struct work_struct *work)
470{
471 struct wq_barrier barr;
472 int running = 0;
473
474 spin_lock_irq(&cwq->lock);
475 if (unlikely(cwq->current_work == work)) {
83c22520 476 insert_wq_barrier(cwq, &barr, 0);
b89deed3
ON
477 running = 1;
478 }
479 spin_unlock_irq(&cwq->lock);
480
3af24433 481 if (unlikely(running))
b89deed3 482 wait_for_completion(&barr.done);
b89deed3
ON
483}
484
6e84d644 485static void wait_on_work(struct work_struct *work)
b89deed3
ON
486{
487 struct cpu_workqueue_struct *cwq;
28e53bdd
ON
488 struct workqueue_struct *wq;
489 const cpumask_t *cpu_map;
b1f4ec17 490 int cpu;
b89deed3 491
f293ea92
ON
492 might_sleep();
493
4e6045f1
JB
494 lock_acquire(&work->lockdep_map, 0, 0, 0, 2, _THIS_IP_);
495 lock_release(&work->lockdep_map, 1, _THIS_IP_);
496
b89deed3 497 cwq = get_wq_data(work);
b89deed3 498 if (!cwq)
3af24433 499 return;
b89deed3 500
28e53bdd
ON
501 wq = cwq->wq;
502 cpu_map = wq_cpu_map(wq);
503
363ab6f1 504 for_each_cpu_mask_nr(cpu, *cpu_map)
6e84d644
ON
505 wait_on_cpu_work(per_cpu_ptr(wq->cpu_wq, cpu), work);
506}
507
1f1f642e
ON
508static int __cancel_work_timer(struct work_struct *work,
509 struct timer_list* timer)
510{
511 int ret;
512
513 do {
514 ret = (timer && likely(del_timer(timer)));
515 if (!ret)
516 ret = try_to_grab_pending(work);
517 wait_on_work(work);
518 } while (unlikely(ret < 0));
519
520 work_clear_pending(work);
521 return ret;
522}
523
6e84d644
ON
524/**
525 * cancel_work_sync - block until a work_struct's callback has terminated
526 * @work: the work which is to be flushed
527 *
1f1f642e
ON
528 * Returns true if @work was pending.
529 *
6e84d644
ON
530 * cancel_work_sync() will cancel the work if it is queued. If the work's
531 * callback appears to be running, cancel_work_sync() will block until it
532 * has completed.
533 *
534 * It is possible to use this function if the work re-queues itself. It can
535 * cancel the work even if it migrates to another workqueue, however in that
536 * case it only guarantees that work->func() has completed on the last queued
537 * workqueue.
538 *
539 * cancel_work_sync(&delayed_work->work) should be used only if ->timer is not
540 * pending, otherwise it goes into a busy-wait loop until the timer expires.
541 *
542 * The caller must ensure that workqueue_struct on which this work was last
543 * queued can't be destroyed before this function returns.
544 */
1f1f642e 545int cancel_work_sync(struct work_struct *work)
6e84d644 546{
1f1f642e 547 return __cancel_work_timer(work, NULL);
b89deed3 548}
28e53bdd 549EXPORT_SYMBOL_GPL(cancel_work_sync);
b89deed3 550
6e84d644 551/**
f5a421a4 552 * cancel_delayed_work_sync - reliably kill off a delayed work.
6e84d644
ON
553 * @dwork: the delayed work struct
554 *
1f1f642e
ON
555 * Returns true if @dwork was pending.
556 *
6e84d644
ON
557 * It is possible to use this function if @dwork rearms itself via queue_work()
558 * or queue_delayed_work(). See also the comment for cancel_work_sync().
559 */
1f1f642e 560int cancel_delayed_work_sync(struct delayed_work *dwork)
6e84d644 561{
1f1f642e 562 return __cancel_work_timer(&dwork->work, &dwork->timer);
6e84d644 563}
f5a421a4 564EXPORT_SYMBOL(cancel_delayed_work_sync);
1da177e4 565
6e84d644 566static struct workqueue_struct *keventd_wq __read_mostly;
1da177e4 567
0fcb78c2
REB
568/**
569 * schedule_work - put work task in global workqueue
570 * @work: job to be done
571 *
572 * This puts a job in the kernel-global workqueue.
573 */
7ad5b3a5 574int schedule_work(struct work_struct *work)
1da177e4
LT
575{
576 return queue_work(keventd_wq, work);
577}
ae90dd5d 578EXPORT_SYMBOL(schedule_work);
1da177e4 579
c1a220e7
ZR
580/*
581 * schedule_work_on - put work task on a specific cpu
582 * @cpu: cpu to put the work task on
583 * @work: job to be done
584 *
585 * This puts a job on a specific cpu
586 */
587int schedule_work_on(int cpu, struct work_struct *work)
588{
589 return queue_work_on(cpu, keventd_wq, work);
590}
591EXPORT_SYMBOL(schedule_work_on);
592
0fcb78c2
REB
593/**
594 * schedule_delayed_work - put work task in global workqueue after delay
52bad64d
DH
595 * @dwork: job to be done
596 * @delay: number of jiffies to wait or 0 for immediate execution
0fcb78c2
REB
597 *
598 * After waiting for a given time this puts a job in the kernel-global
599 * workqueue.
600 */
7ad5b3a5 601int schedule_delayed_work(struct delayed_work *dwork,
82f67cd9 602 unsigned long delay)
1da177e4 603{
52bad64d 604 return queue_delayed_work(keventd_wq, dwork, delay);
1da177e4 605}
ae90dd5d 606EXPORT_SYMBOL(schedule_delayed_work);
1da177e4 607
0fcb78c2
REB
608/**
609 * schedule_delayed_work_on - queue work in global workqueue on CPU after delay
610 * @cpu: cpu to use
52bad64d 611 * @dwork: job to be done
0fcb78c2
REB
612 * @delay: number of jiffies to wait
613 *
614 * After waiting for a given time this puts a job in the kernel-global
615 * workqueue on the specified CPU.
616 */
1da177e4 617int schedule_delayed_work_on(int cpu,
52bad64d 618 struct delayed_work *dwork, unsigned long delay)
1da177e4 619{
52bad64d 620 return queue_delayed_work_on(cpu, keventd_wq, dwork, delay);
1da177e4 621}
ae90dd5d 622EXPORT_SYMBOL(schedule_delayed_work_on);
1da177e4 623
b6136773
AM
624/**
625 * schedule_on_each_cpu - call a function on each online CPU from keventd
626 * @func: the function to call
b6136773
AM
627 *
628 * Returns zero on success.
629 * Returns -ve errno on failure.
630 *
b6136773
AM
631 * schedule_on_each_cpu() is very slow.
632 */
65f27f38 633int schedule_on_each_cpu(work_func_t func)
15316ba8
CL
634{
635 int cpu;
b6136773 636 struct work_struct *works;
15316ba8 637
b6136773
AM
638 works = alloc_percpu(struct work_struct);
639 if (!works)
15316ba8 640 return -ENOMEM;
b6136773 641
95402b38 642 get_online_cpus();
15316ba8 643 for_each_online_cpu(cpu) {
9bfb1839
IM
644 struct work_struct *work = per_cpu_ptr(works, cpu);
645
646 INIT_WORK(work, func);
647 set_bit(WORK_STRUCT_PENDING, work_data_bits(work));
648 __queue_work(per_cpu_ptr(keventd_wq->cpu_wq, cpu), work);
15316ba8
CL
649 }
650 flush_workqueue(keventd_wq);
95402b38 651 put_online_cpus();
b6136773 652 free_percpu(works);
15316ba8
CL
653 return 0;
654}
655
1da177e4
LT
656void flush_scheduled_work(void)
657{
658 flush_workqueue(keventd_wq);
659}
ae90dd5d 660EXPORT_SYMBOL(flush_scheduled_work);
1da177e4 661
1fa44eca
JB
662/**
663 * execute_in_process_context - reliably execute the routine with user context
664 * @fn: the function to execute
1fa44eca
JB
665 * @ew: guaranteed storage for the execute work structure (must
666 * be available when the work executes)
667 *
668 * Executes the function immediately if process context is available,
669 * otherwise schedules the function for delayed execution.
670 *
671 * Returns: 0 - function was executed
672 * 1 - function was scheduled for execution
673 */
65f27f38 674int execute_in_process_context(work_func_t fn, struct execute_work *ew)
1fa44eca
JB
675{
676 if (!in_interrupt()) {
65f27f38 677 fn(&ew->work);
1fa44eca
JB
678 return 0;
679 }
680
65f27f38 681 INIT_WORK(&ew->work, fn);
1fa44eca
JB
682 schedule_work(&ew->work);
683
684 return 1;
685}
686EXPORT_SYMBOL_GPL(execute_in_process_context);
687
1da177e4
LT
688int keventd_up(void)
689{
690 return keventd_wq != NULL;
691}
692
693int current_is_keventd(void)
694{
695 struct cpu_workqueue_struct *cwq;
d243769d 696 int cpu = raw_smp_processor_id(); /* preempt-safe: keventd is per-cpu */
1da177e4
LT
697 int ret = 0;
698
699 BUG_ON(!keventd_wq);
700
89ada679 701 cwq = per_cpu_ptr(keventd_wq->cpu_wq, cpu);
1da177e4
LT
702 if (current == cwq->thread)
703 ret = 1;
704
705 return ret;
706
707}
708
3af24433
ON
709static struct cpu_workqueue_struct *
710init_cpu_workqueue(struct workqueue_struct *wq, int cpu)
1da177e4 711{
89ada679 712 struct cpu_workqueue_struct *cwq = per_cpu_ptr(wq->cpu_wq, cpu);
1da177e4 713
3af24433
ON
714 cwq->wq = wq;
715 spin_lock_init(&cwq->lock);
716 INIT_LIST_HEAD(&cwq->worklist);
717 init_waitqueue_head(&cwq->more_work);
718
719 return cwq;
1da177e4
LT
720}
721
3af24433
ON
722static int create_workqueue_thread(struct cpu_workqueue_struct *cwq, int cpu)
723{
724 struct workqueue_struct *wq = cwq->wq;
725 const char *fmt = is_single_threaded(wq) ? "%s" : "%s/%d";
726 struct task_struct *p;
727
728 p = kthread_create(worker_thread, cwq, fmt, wq->name, cpu);
729 /*
730 * Nobody can add the work_struct to this cwq,
731 * if (caller is __create_workqueue)
732 * nobody should see this wq
733 * else // caller is CPU_UP_PREPARE
734 * cpu is not on cpu_online_map
735 * so we can abort safely.
736 */
737 if (IS_ERR(p))
738 return PTR_ERR(p);
739
740 cwq->thread = p;
3af24433
ON
741
742 return 0;
743}
744
06ba38a9
ON
745static void start_workqueue_thread(struct cpu_workqueue_struct *cwq, int cpu)
746{
747 struct task_struct *p = cwq->thread;
748
749 if (p != NULL) {
750 if (cpu >= 0)
751 kthread_bind(p, cpu);
752 wake_up_process(p);
753 }
754}
755
4e6045f1
JB
756struct workqueue_struct *__create_workqueue_key(const char *name,
757 int singlethread,
758 int freezeable,
eb13ba87
JB
759 struct lock_class_key *key,
760 const char *lock_name)
1da177e4 761{
1da177e4 762 struct workqueue_struct *wq;
3af24433
ON
763 struct cpu_workqueue_struct *cwq;
764 int err = 0, cpu;
1da177e4 765
3af24433
ON
766 wq = kzalloc(sizeof(*wq), GFP_KERNEL);
767 if (!wq)
768 return NULL;
769
770 wq->cpu_wq = alloc_percpu(struct cpu_workqueue_struct);
771 if (!wq->cpu_wq) {
772 kfree(wq);
773 return NULL;
774 }
775
776 wq->name = name;
eb13ba87 777 lockdep_init_map(&wq->lockdep_map, lock_name, key, 0);
cce1a165 778 wq->singlethread = singlethread;
3af24433 779 wq->freezeable = freezeable;
cce1a165 780 INIT_LIST_HEAD(&wq->list);
3af24433
ON
781
782 if (singlethread) {
3af24433
ON
783 cwq = init_cpu_workqueue(wq, singlethread_cpu);
784 err = create_workqueue_thread(cwq, singlethread_cpu);
06ba38a9 785 start_workqueue_thread(cwq, -1);
3af24433 786 } else {
95402b38
GS
787 get_online_cpus();
788 spin_lock(&workqueue_lock);
3af24433 789 list_add(&wq->list, &workqueues);
95402b38 790 spin_unlock(&workqueue_lock);
3af24433
ON
791
792 for_each_possible_cpu(cpu) {
793 cwq = init_cpu_workqueue(wq, cpu);
794 if (err || !cpu_online(cpu))
795 continue;
796 err = create_workqueue_thread(cwq, cpu);
06ba38a9 797 start_workqueue_thread(cwq, cpu);
1da177e4 798 }
95402b38 799 put_online_cpus();
3af24433
ON
800 }
801
802 if (err) {
803 destroy_workqueue(wq);
804 wq = NULL;
805 }
806 return wq;
807}
4e6045f1 808EXPORT_SYMBOL_GPL(__create_workqueue_key);
1da177e4 809
1e35eaa2 810static void cleanup_workqueue_thread(struct cpu_workqueue_struct *cwq)
3af24433 811{
14441960
ON
812 /*
813 * Our caller is either destroy_workqueue() or CPU_DEAD,
95402b38 814 * get_online_cpus() protects cwq->thread.
14441960
ON
815 */
816 if (cwq->thread == NULL)
817 return;
3af24433 818
4e6045f1
JB
819 lock_acquire(&cwq->wq->lockdep_map, 0, 0, 0, 2, _THIS_IP_);
820 lock_release(&cwq->wq->lockdep_map, 1, _THIS_IP_);
821
13c22168 822 flush_cpu_workqueue(cwq);
14441960 823 /*
13c22168
ON
824 * If the caller is CPU_DEAD and cwq->worklist was not empty,
825 * a concurrent flush_workqueue() can insert a barrier after us.
826 * However, in that case run_workqueue() won't return and check
827 * kthread_should_stop() until it flushes all work_struct's.
14441960
ON
828 * When ->worklist becomes empty it is safe to exit because no
829 * more work_structs can be queued on this cwq: flush_workqueue
830 * checks list_empty(), and a "normal" queue_work() can't use
831 * a dead CPU.
832 */
14441960
ON
833 kthread_stop(cwq->thread);
834 cwq->thread = NULL;
3af24433
ON
835}
836
837/**
838 * destroy_workqueue - safely terminate a workqueue
839 * @wq: target workqueue
840 *
841 * Safely destroy a workqueue. All work currently pending will be done first.
842 */
843void destroy_workqueue(struct workqueue_struct *wq)
844{
b1f4ec17 845 const cpumask_t *cpu_map = wq_cpu_map(wq);
b1f4ec17 846 int cpu;
3af24433 847
95402b38
GS
848 get_online_cpus();
849 spin_lock(&workqueue_lock);
b1f4ec17 850 list_del(&wq->list);
95402b38 851 spin_unlock(&workqueue_lock);
3af24433 852
363ab6f1 853 for_each_cpu_mask_nr(cpu, *cpu_map)
1e35eaa2 854 cleanup_workqueue_thread(per_cpu_ptr(wq->cpu_wq, cpu));
00dfcaf7 855 put_online_cpus();
9b41ea72 856
3af24433
ON
857 free_percpu(wq->cpu_wq);
858 kfree(wq);
859}
860EXPORT_SYMBOL_GPL(destroy_workqueue);
861
862static int __devinit workqueue_cpu_callback(struct notifier_block *nfb,
863 unsigned long action,
864 void *hcpu)
865{
866 unsigned int cpu = (unsigned long)hcpu;
867 struct cpu_workqueue_struct *cwq;
868 struct workqueue_struct *wq;
869
8bb78442
RW
870 action &= ~CPU_TASKS_FROZEN;
871
3af24433 872 switch (action) {
3af24433
ON
873 case CPU_UP_PREPARE:
874 cpu_set(cpu, cpu_populated_map);
875 }
876
877 list_for_each_entry(wq, &workqueues, list) {
878 cwq = per_cpu_ptr(wq->cpu_wq, cpu);
879
880 switch (action) {
881 case CPU_UP_PREPARE:
882 if (!create_workqueue_thread(cwq, cpu))
883 break;
95402b38
GS
884 printk(KERN_ERR "workqueue [%s] for %i failed\n",
885 wq->name, cpu);
3af24433
ON
886 return NOTIFY_BAD;
887
888 case CPU_ONLINE:
06ba38a9 889 start_workqueue_thread(cwq, cpu);
3af24433
ON
890 break;
891
892 case CPU_UP_CANCELED:
06ba38a9 893 start_workqueue_thread(cwq, -1);
3af24433 894 case CPU_DEAD:
1e35eaa2 895 cleanup_workqueue_thread(cwq);
3af24433
ON
896 break;
897 }
1da177e4
LT
898 }
899
00dfcaf7
ON
900 switch (action) {
901 case CPU_UP_CANCELED:
902 case CPU_DEAD:
903 cpu_clear(cpu, cpu_populated_map);
904 }
905
1da177e4
LT
906 return NOTIFY_OK;
907}
1da177e4 908
c12920d1 909void __init init_workqueues(void)
1da177e4 910{
3af24433 911 cpu_populated_map = cpu_online_map;
f756d5e2 912 singlethread_cpu = first_cpu(cpu_possible_map);
b1f4ec17 913 cpu_singlethread_map = cpumask_of_cpu(singlethread_cpu);
1da177e4
LT
914 hotcpu_notifier(workqueue_cpu_callback, 0);
915 keventd_wq = create_workqueue("events");
916 BUG_ON(!keventd_wq);
917}