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