workqueue: reimplement work flushing using linked works
[linux-2.6-block.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>
c34056a3 36#include <linux/idr.h>
1da177e4 37
4690c4ab
TH
38/*
39 * Structure fields follow one of the following exclusion rules.
40 *
41 * I: Set during initialization and read-only afterwards.
42 *
43 * L: cwq->lock protected. Access with cwq->lock held.
44 *
73f53c4a
TH
45 * F: wq->flush_mutex protected.
46 *
4690c4ab
TH
47 * W: workqueue_lock protected.
48 */
49
c34056a3
TH
50struct cpu_workqueue_struct;
51
52struct worker {
53 struct work_struct *current_work; /* L: work being processed */
affee4b2 54 struct list_head scheduled; /* L: scheduled works */
c34056a3
TH
55 struct task_struct *task; /* I: worker task */
56 struct cpu_workqueue_struct *cwq; /* I: the associated cwq */
57 int id; /* I: worker id */
58};
59
1da177e4 60/*
f756d5e2 61 * The per-CPU workqueue (if single thread, we always use the first
0f900049
TH
62 * possible cpu). The lower WORK_STRUCT_FLAG_BITS of
63 * work_struct->data are used for flags and thus cwqs need to be
64 * aligned at two's power of the number of flag bits.
1da177e4
LT
65 */
66struct cpu_workqueue_struct {
67
68 spinlock_t lock;
69
1da177e4
LT
70 struct list_head worklist;
71 wait_queue_head_t more_work;
1537663f 72 unsigned int cpu;
c34056a3 73 struct worker *worker;
1da177e4 74
4690c4ab 75 struct workqueue_struct *wq; /* I: the owning workqueue */
73f53c4a
TH
76 int work_color; /* L: current color */
77 int flush_color; /* L: flushing color */
78 int nr_in_flight[WORK_NR_COLORS];
79 /* L: nr of in_flight works */
0f900049 80};
1da177e4 81
73f53c4a
TH
82/*
83 * Structure used to wait for workqueue flush.
84 */
85struct wq_flusher {
86 struct list_head list; /* F: list of flushers */
87 int flush_color; /* F: flush color waiting for */
88 struct completion done; /* flush completion */
89};
90
1da177e4
LT
91/*
92 * The externally visible workqueue abstraction is an array of
93 * per-CPU workqueues:
94 */
95struct workqueue_struct {
97e37d7b 96 unsigned int flags; /* I: WQ_* flags */
4690c4ab
TH
97 struct cpu_workqueue_struct *cpu_wq; /* I: cwq's */
98 struct list_head list; /* W: list of all workqueues */
73f53c4a
TH
99
100 struct mutex flush_mutex; /* protects wq flushing */
101 int work_color; /* F: current work color */
102 int flush_color; /* F: current flush color */
103 atomic_t nr_cwqs_to_flush; /* flush in progress */
104 struct wq_flusher *first_flusher; /* F: first flusher */
105 struct list_head flusher_queue; /* F: flush waiters */
106 struct list_head flusher_overflow; /* F: flush overflow list */
107
4690c4ab 108 const char *name; /* I: workqueue name */
4e6045f1 109#ifdef CONFIG_LOCKDEP
4690c4ab 110 struct lockdep_map lockdep_map;
4e6045f1 111#endif
1da177e4
LT
112};
113
dc186ad7
TG
114#ifdef CONFIG_DEBUG_OBJECTS_WORK
115
116static struct debug_obj_descr work_debug_descr;
117
118/*
119 * fixup_init is called when:
120 * - an active object is initialized
121 */
122static int work_fixup_init(void *addr, enum debug_obj_state state)
123{
124 struct work_struct *work = addr;
125
126 switch (state) {
127 case ODEBUG_STATE_ACTIVE:
128 cancel_work_sync(work);
129 debug_object_init(work, &work_debug_descr);
130 return 1;
131 default:
132 return 0;
133 }
134}
135
136/*
137 * fixup_activate is called when:
138 * - an active object is activated
139 * - an unknown object is activated (might be a statically initialized object)
140 */
141static int work_fixup_activate(void *addr, enum debug_obj_state state)
142{
143 struct work_struct *work = addr;
144
145 switch (state) {
146
147 case ODEBUG_STATE_NOTAVAILABLE:
148 /*
149 * This is not really a fixup. The work struct was
150 * statically initialized. We just make sure that it
151 * is tracked in the object tracker.
152 */
22df02bb 153 if (test_bit(WORK_STRUCT_STATIC_BIT, work_data_bits(work))) {
dc186ad7
TG
154 debug_object_init(work, &work_debug_descr);
155 debug_object_activate(work, &work_debug_descr);
156 return 0;
157 }
158 WARN_ON_ONCE(1);
159 return 0;
160
161 case ODEBUG_STATE_ACTIVE:
162 WARN_ON(1);
163
164 default:
165 return 0;
166 }
167}
168
169/*
170 * fixup_free is called when:
171 * - an active object is freed
172 */
173static int work_fixup_free(void *addr, enum debug_obj_state state)
174{
175 struct work_struct *work = addr;
176
177 switch (state) {
178 case ODEBUG_STATE_ACTIVE:
179 cancel_work_sync(work);
180 debug_object_free(work, &work_debug_descr);
181 return 1;
182 default:
183 return 0;
184 }
185}
186
187static struct debug_obj_descr work_debug_descr = {
188 .name = "work_struct",
189 .fixup_init = work_fixup_init,
190 .fixup_activate = work_fixup_activate,
191 .fixup_free = work_fixup_free,
192};
193
194static inline void debug_work_activate(struct work_struct *work)
195{
196 debug_object_activate(work, &work_debug_descr);
197}
198
199static inline void debug_work_deactivate(struct work_struct *work)
200{
201 debug_object_deactivate(work, &work_debug_descr);
202}
203
204void __init_work(struct work_struct *work, int onstack)
205{
206 if (onstack)
207 debug_object_init_on_stack(work, &work_debug_descr);
208 else
209 debug_object_init(work, &work_debug_descr);
210}
211EXPORT_SYMBOL_GPL(__init_work);
212
213void destroy_work_on_stack(struct work_struct *work)
214{
215 debug_object_free(work, &work_debug_descr);
216}
217EXPORT_SYMBOL_GPL(destroy_work_on_stack);
218
219#else
220static inline void debug_work_activate(struct work_struct *work) { }
221static inline void debug_work_deactivate(struct work_struct *work) { }
222#endif
223
95402b38
GS
224/* Serializes the accesses to the list of workqueues. */
225static DEFINE_SPINLOCK(workqueue_lock);
1da177e4 226static LIST_HEAD(workqueues);
c34056a3
TH
227static DEFINE_PER_CPU(struct ida, worker_ida);
228
229static int worker_thread(void *__worker);
1da177e4 230
3af24433 231static int singlethread_cpu __read_mostly;
1da177e4 232
1537663f
TH
233static struct cpu_workqueue_struct *get_cwq(unsigned int cpu,
234 struct workqueue_struct *wq)
b1f4ec17 235{
1537663f 236 return per_cpu_ptr(wq->cpu_wq, cpu);
b1f4ec17
ON
237}
238
1537663f
TH
239static struct cpu_workqueue_struct *target_cwq(unsigned int cpu,
240 struct workqueue_struct *wq)
a848e3b6 241{
1537663f 242 if (unlikely(wq->flags & WQ_SINGLE_THREAD))
a848e3b6 243 cpu = singlethread_cpu;
1537663f 244 return get_cwq(cpu, wq);
a848e3b6
ON
245}
246
73f53c4a
TH
247static unsigned int work_color_to_flags(int color)
248{
249 return color << WORK_STRUCT_COLOR_SHIFT;
250}
251
252static int get_work_color(struct work_struct *work)
253{
254 return (*work_data_bits(work) >> WORK_STRUCT_COLOR_SHIFT) &
255 ((1 << WORK_STRUCT_COLOR_BITS) - 1);
256}
257
258static int work_next_color(int color)
259{
260 return (color + 1) % WORK_NR_COLORS;
261}
262
4594bf15
DH
263/*
264 * Set the workqueue on which a work item is to be run
265 * - Must *only* be called if the pending flag is set
266 */
ed7c0fee 267static inline void set_wq_data(struct work_struct *work,
4690c4ab
TH
268 struct cpu_workqueue_struct *cwq,
269 unsigned long extra_flags)
365970a1 270{
4594bf15 271 BUG_ON(!work_pending(work));
365970a1 272
4690c4ab 273 atomic_long_set(&work->data, (unsigned long)cwq | work_static(work) |
22df02bb 274 WORK_STRUCT_PENDING | extra_flags);
365970a1
DH
275}
276
4d707b9f
ON
277/*
278 * Clear WORK_STRUCT_PENDING and the workqueue on which it was queued.
279 */
280static inline void clear_wq_data(struct work_struct *work)
281{
4690c4ab 282 atomic_long_set(&work->data, work_static(work));
4d707b9f
ON
283}
284
64166699 285static inline struct cpu_workqueue_struct *get_wq_data(struct work_struct *work)
365970a1 286{
64166699
TH
287 return (void *)(atomic_long_read(&work->data) &
288 WORK_STRUCT_WQ_DATA_MASK);
365970a1
DH
289}
290
4690c4ab
TH
291/**
292 * insert_work - insert a work into cwq
293 * @cwq: cwq @work belongs to
294 * @work: work to insert
295 * @head: insertion point
296 * @extra_flags: extra WORK_STRUCT_* flags to set
297 *
298 * Insert @work into @cwq after @head.
299 *
300 * CONTEXT:
301 * spin_lock_irq(cwq->lock).
302 */
b89deed3 303static void insert_work(struct cpu_workqueue_struct *cwq,
4690c4ab
TH
304 struct work_struct *work, struct list_head *head,
305 unsigned int extra_flags)
b89deed3 306{
4690c4ab
TH
307 /* we own @work, set data and link */
308 set_wq_data(work, cwq, extra_flags);
309
6e84d644
ON
310 /*
311 * Ensure that we get the right work->data if we see the
312 * result of list_add() below, see try_to_grab_pending().
313 */
314 smp_wmb();
4690c4ab 315
1a4d9b0a 316 list_add_tail(&work->entry, head);
b89deed3
ON
317 wake_up(&cwq->more_work);
318}
319
4690c4ab 320static void __queue_work(unsigned int cpu, struct workqueue_struct *wq,
1da177e4
LT
321 struct work_struct *work)
322{
1537663f 323 struct cpu_workqueue_struct *cwq = target_cwq(cpu, wq);
1da177e4
LT
324 unsigned long flags;
325
dc186ad7 326 debug_work_activate(work);
1da177e4 327 spin_lock_irqsave(&cwq->lock, flags);
4690c4ab 328 BUG_ON(!list_empty(&work->entry));
73f53c4a
TH
329 cwq->nr_in_flight[cwq->work_color]++;
330 insert_work(cwq, work, &cwq->worklist,
331 work_color_to_flags(cwq->work_color));
1da177e4
LT
332 spin_unlock_irqrestore(&cwq->lock, flags);
333}
334
0fcb78c2
REB
335/**
336 * queue_work - queue work on a workqueue
337 * @wq: workqueue to use
338 * @work: work to queue
339 *
057647fc 340 * Returns 0 if @work was already on a queue, non-zero otherwise.
1da177e4 341 *
00dfcaf7
ON
342 * We queue the work to the CPU on which it was submitted, but if the CPU dies
343 * it can be processed by another CPU.
1da177e4 344 */
7ad5b3a5 345int queue_work(struct workqueue_struct *wq, struct work_struct *work)
1da177e4 346{
ef1ca236
ON
347 int ret;
348
349 ret = queue_work_on(get_cpu(), wq, work);
350 put_cpu();
351
1da177e4
LT
352 return ret;
353}
ae90dd5d 354EXPORT_SYMBOL_GPL(queue_work);
1da177e4 355
c1a220e7
ZR
356/**
357 * queue_work_on - queue work on specific cpu
358 * @cpu: CPU number to execute work on
359 * @wq: workqueue to use
360 * @work: work to queue
361 *
362 * Returns 0 if @work was already on a queue, non-zero otherwise.
363 *
364 * We queue the work to a specific CPU, the caller must ensure it
365 * can't go away.
366 */
367int
368queue_work_on(int cpu, struct workqueue_struct *wq, struct work_struct *work)
369{
370 int ret = 0;
371
22df02bb 372 if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))) {
4690c4ab 373 __queue_work(cpu, wq, work);
c1a220e7
ZR
374 ret = 1;
375 }
376 return ret;
377}
378EXPORT_SYMBOL_GPL(queue_work_on);
379
6d141c3f 380static void delayed_work_timer_fn(unsigned long __data)
1da177e4 381{
52bad64d 382 struct delayed_work *dwork = (struct delayed_work *)__data;
ed7c0fee 383 struct cpu_workqueue_struct *cwq = get_wq_data(&dwork->work);
1da177e4 384
4690c4ab 385 __queue_work(smp_processor_id(), cwq->wq, &dwork->work);
1da177e4
LT
386}
387
0fcb78c2
REB
388/**
389 * queue_delayed_work - queue work on a workqueue after delay
390 * @wq: workqueue to use
af9997e4 391 * @dwork: delayable work to queue
0fcb78c2
REB
392 * @delay: number of jiffies to wait before queueing
393 *
057647fc 394 * Returns 0 if @work was already on a queue, non-zero otherwise.
0fcb78c2 395 */
7ad5b3a5 396int queue_delayed_work(struct workqueue_struct *wq,
52bad64d 397 struct delayed_work *dwork, unsigned long delay)
1da177e4 398{
52bad64d 399 if (delay == 0)
63bc0362 400 return queue_work(wq, &dwork->work);
1da177e4 401
63bc0362 402 return queue_delayed_work_on(-1, wq, dwork, delay);
1da177e4 403}
ae90dd5d 404EXPORT_SYMBOL_GPL(queue_delayed_work);
1da177e4 405
0fcb78c2
REB
406/**
407 * queue_delayed_work_on - queue work on specific CPU after delay
408 * @cpu: CPU number to execute work on
409 * @wq: workqueue to use
af9997e4 410 * @dwork: work to queue
0fcb78c2
REB
411 * @delay: number of jiffies to wait before queueing
412 *
057647fc 413 * Returns 0 if @work was already on a queue, non-zero otherwise.
0fcb78c2 414 */
7a6bc1cd 415int queue_delayed_work_on(int cpu, struct workqueue_struct *wq,
52bad64d 416 struct delayed_work *dwork, unsigned long delay)
7a6bc1cd
VP
417{
418 int ret = 0;
52bad64d
DH
419 struct timer_list *timer = &dwork->timer;
420 struct work_struct *work = &dwork->work;
7a6bc1cd 421
22df02bb 422 if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))) {
7a6bc1cd
VP
423 BUG_ON(timer_pending(timer));
424 BUG_ON(!list_empty(&work->entry));
425
8a3e77cc
AL
426 timer_stats_timer_set_start_info(&dwork->timer);
427
ed7c0fee 428 /* This stores cwq for the moment, for the timer_fn */
1537663f 429 set_wq_data(work, target_cwq(raw_smp_processor_id(), wq), 0);
7a6bc1cd 430 timer->expires = jiffies + delay;
52bad64d 431 timer->data = (unsigned long)dwork;
7a6bc1cd 432 timer->function = delayed_work_timer_fn;
63bc0362
ON
433
434 if (unlikely(cpu >= 0))
435 add_timer_on(timer, cpu);
436 else
437 add_timer(timer);
7a6bc1cd
VP
438 ret = 1;
439 }
440 return ret;
441}
ae90dd5d 442EXPORT_SYMBOL_GPL(queue_delayed_work_on);
1da177e4 443
c34056a3
TH
444static struct worker *alloc_worker(void)
445{
446 struct worker *worker;
447
448 worker = kzalloc(sizeof(*worker), GFP_KERNEL);
affee4b2
TH
449 if (worker)
450 INIT_LIST_HEAD(&worker->scheduled);
c34056a3
TH
451 return worker;
452}
453
454/**
455 * create_worker - create a new workqueue worker
456 * @cwq: cwq the new worker will belong to
457 * @bind: whether to set affinity to @cpu or not
458 *
459 * Create a new worker which is bound to @cwq. The returned worker
460 * can be started by calling start_worker() or destroyed using
461 * destroy_worker().
462 *
463 * CONTEXT:
464 * Might sleep. Does GFP_KERNEL allocations.
465 *
466 * RETURNS:
467 * Pointer to the newly created worker.
468 */
469static struct worker *create_worker(struct cpu_workqueue_struct *cwq, bool bind)
470{
471 int id = -1;
472 struct worker *worker = NULL;
473
474 spin_lock(&workqueue_lock);
475 while (ida_get_new(&per_cpu(worker_ida, cwq->cpu), &id)) {
476 spin_unlock(&workqueue_lock);
477 if (!ida_pre_get(&per_cpu(worker_ida, cwq->cpu), GFP_KERNEL))
478 goto fail;
479 spin_lock(&workqueue_lock);
480 }
481 spin_unlock(&workqueue_lock);
482
483 worker = alloc_worker();
484 if (!worker)
485 goto fail;
486
487 worker->cwq = cwq;
488 worker->id = id;
489
490 worker->task = kthread_create(worker_thread, worker, "kworker/%u:%d",
491 cwq->cpu, id);
492 if (IS_ERR(worker->task))
493 goto fail;
494
495 if (bind)
496 kthread_bind(worker->task, cwq->cpu);
497
498 return worker;
499fail:
500 if (id >= 0) {
501 spin_lock(&workqueue_lock);
502 ida_remove(&per_cpu(worker_ida, cwq->cpu), id);
503 spin_unlock(&workqueue_lock);
504 }
505 kfree(worker);
506 return NULL;
507}
508
509/**
510 * start_worker - start a newly created worker
511 * @worker: worker to start
512 *
513 * Start @worker.
514 *
515 * CONTEXT:
516 * spin_lock_irq(cwq->lock).
517 */
518static void start_worker(struct worker *worker)
519{
520 wake_up_process(worker->task);
521}
522
523/**
524 * destroy_worker - destroy a workqueue worker
525 * @worker: worker to be destroyed
526 *
527 * Destroy @worker.
528 */
529static void destroy_worker(struct worker *worker)
530{
531 int cpu = worker->cwq->cpu;
532 int id = worker->id;
533
534 /* sanity check frenzy */
535 BUG_ON(worker->current_work);
affee4b2 536 BUG_ON(!list_empty(&worker->scheduled));
c34056a3
TH
537
538 kthread_stop(worker->task);
539 kfree(worker);
540
541 spin_lock(&workqueue_lock);
542 ida_remove(&per_cpu(worker_ida, cpu), id);
543 spin_unlock(&workqueue_lock);
544}
545
affee4b2
TH
546/**
547 * move_linked_works - move linked works to a list
548 * @work: start of series of works to be scheduled
549 * @head: target list to append @work to
550 * @nextp: out paramter for nested worklist walking
551 *
552 * Schedule linked works starting from @work to @head. Work series to
553 * be scheduled starts at @work and includes any consecutive work with
554 * WORK_STRUCT_LINKED set in its predecessor.
555 *
556 * If @nextp is not NULL, it's updated to point to the next work of
557 * the last scheduled work. This allows move_linked_works() to be
558 * nested inside outer list_for_each_entry_safe().
559 *
560 * CONTEXT:
561 * spin_lock_irq(cwq->lock).
562 */
563static void move_linked_works(struct work_struct *work, struct list_head *head,
564 struct work_struct **nextp)
565{
566 struct work_struct *n;
567
568 /*
569 * Linked worklist will always end before the end of the list,
570 * use NULL for list head.
571 */
572 list_for_each_entry_safe_from(work, n, NULL, entry) {
573 list_move_tail(&work->entry, head);
574 if (!(*work_data_bits(work) & WORK_STRUCT_LINKED))
575 break;
576 }
577
578 /*
579 * If we're already inside safe list traversal and have moved
580 * multiple works to the scheduled queue, the next position
581 * needs to be updated.
582 */
583 if (nextp)
584 *nextp = n;
585}
586
73f53c4a
TH
587/**
588 * cwq_dec_nr_in_flight - decrement cwq's nr_in_flight
589 * @cwq: cwq of interest
590 * @color: color of work which left the queue
591 *
592 * A work either has completed or is removed from pending queue,
593 * decrement nr_in_flight of its cwq and handle workqueue flushing.
594 *
595 * CONTEXT:
596 * spin_lock_irq(cwq->lock).
597 */
598static void cwq_dec_nr_in_flight(struct cpu_workqueue_struct *cwq, int color)
599{
600 /* ignore uncolored works */
601 if (color == WORK_NO_COLOR)
602 return;
603
604 cwq->nr_in_flight[color]--;
605
606 /* is flush in progress and are we at the flushing tip? */
607 if (likely(cwq->flush_color != color))
608 return;
609
610 /* are there still in-flight works? */
611 if (cwq->nr_in_flight[color])
612 return;
613
614 /* this cwq is done, clear flush_color */
615 cwq->flush_color = -1;
616
617 /*
618 * If this was the last cwq, wake up the first flusher. It
619 * will handle the rest.
620 */
621 if (atomic_dec_and_test(&cwq->wq->nr_cwqs_to_flush))
622 complete(&cwq->wq->first_flusher->done);
623}
624
a62428c0
TH
625/**
626 * process_one_work - process single work
c34056a3 627 * @worker: self
a62428c0
TH
628 * @work: work to process
629 *
630 * Process @work. This function contains all the logics necessary to
631 * process a single work including synchronization against and
632 * interaction with other workers on the same cpu, queueing and
633 * flushing. As long as context requirement is met, any worker can
634 * call this function to process a work.
635 *
636 * CONTEXT:
637 * spin_lock_irq(cwq->lock) which is released and regrabbed.
638 */
c34056a3 639static void process_one_work(struct worker *worker, struct work_struct *work)
a62428c0 640{
c34056a3 641 struct cpu_workqueue_struct *cwq = worker->cwq;
a62428c0 642 work_func_t f = work->func;
73f53c4a 643 int work_color;
a62428c0
TH
644#ifdef CONFIG_LOCKDEP
645 /*
646 * It is permissible to free the struct work_struct from
647 * inside the function that is called from it, this we need to
648 * take into account for lockdep too. To avoid bogus "held
649 * lock freed" warnings as well as problems when looking into
650 * work->lockdep_map, make a copy and use that here.
651 */
652 struct lockdep_map lockdep_map = work->lockdep_map;
653#endif
654 /* claim and process */
a62428c0 655 debug_work_deactivate(work);
c34056a3 656 worker->current_work = work;
73f53c4a 657 work_color = get_work_color(work);
a62428c0
TH
658 list_del_init(&work->entry);
659
660 spin_unlock_irq(&cwq->lock);
661
662 BUG_ON(get_wq_data(work) != cwq);
663 work_clear_pending(work);
664 lock_map_acquire(&cwq->wq->lockdep_map);
665 lock_map_acquire(&lockdep_map);
666 f(work);
667 lock_map_release(&lockdep_map);
668 lock_map_release(&cwq->wq->lockdep_map);
669
670 if (unlikely(in_atomic() || lockdep_depth(current) > 0)) {
671 printk(KERN_ERR "BUG: workqueue leaked lock or atomic: "
672 "%s/0x%08x/%d\n",
673 current->comm, preempt_count(), task_pid_nr(current));
674 printk(KERN_ERR " last function: ");
675 print_symbol("%s\n", (unsigned long)f);
676 debug_show_held_locks(current);
677 dump_stack();
678 }
679
680 spin_lock_irq(&cwq->lock);
681
682 /* we're done with it, release */
c34056a3 683 worker->current_work = NULL;
73f53c4a 684 cwq_dec_nr_in_flight(cwq, work_color);
a62428c0
TH
685}
686
affee4b2
TH
687/**
688 * process_scheduled_works - process scheduled works
689 * @worker: self
690 *
691 * Process all scheduled works. Please note that the scheduled list
692 * may change while processing a work, so this function repeatedly
693 * fetches a work from the top and executes it.
694 *
695 * CONTEXT:
696 * spin_lock_irq(cwq->lock) which may be released and regrabbed
697 * multiple times.
698 */
699static void process_scheduled_works(struct worker *worker)
1da177e4 700{
affee4b2
TH
701 while (!list_empty(&worker->scheduled)) {
702 struct work_struct *work = list_first_entry(&worker->scheduled,
1da177e4 703 struct work_struct, entry);
c34056a3 704 process_one_work(worker, work);
1da177e4 705 }
1da177e4
LT
706}
707
4690c4ab
TH
708/**
709 * worker_thread - the worker thread function
c34056a3 710 * @__worker: self
4690c4ab
TH
711 *
712 * The cwq worker thread function.
713 */
c34056a3 714static int worker_thread(void *__worker)
1da177e4 715{
c34056a3
TH
716 struct worker *worker = __worker;
717 struct cpu_workqueue_struct *cwq = worker->cwq;
3af24433 718 DEFINE_WAIT(wait);
1da177e4 719
97e37d7b 720 if (cwq->wq->flags & WQ_FREEZEABLE)
83144186 721 set_freezable();
1da177e4 722
3af24433 723 for (;;) {
3af24433 724 prepare_to_wait(&cwq->more_work, &wait, TASK_INTERRUPTIBLE);
14441960
ON
725 if (!freezing(current) &&
726 !kthread_should_stop() &&
727 list_empty(&cwq->worklist))
1da177e4 728 schedule();
3af24433
ON
729 finish_wait(&cwq->more_work, &wait);
730
85f4186a
ON
731 try_to_freeze();
732
14441960 733 if (kthread_should_stop())
3af24433 734 break;
1da177e4 735
c34056a3 736 if (unlikely(!cpumask_equal(&worker->task->cpus_allowed,
1537663f 737 get_cpu_mask(cwq->cpu))))
c34056a3 738 set_cpus_allowed_ptr(worker->task,
1537663f 739 get_cpu_mask(cwq->cpu));
affee4b2
TH
740
741 spin_lock_irq(&cwq->lock);
742
743 while (!list_empty(&cwq->worklist)) {
744 struct work_struct *work =
745 list_first_entry(&cwq->worklist,
746 struct work_struct, entry);
747
748 if (likely(!(*work_data_bits(work) &
749 WORK_STRUCT_LINKED))) {
750 /* optimization path, not strictly necessary */
751 process_one_work(worker, work);
752 if (unlikely(!list_empty(&worker->scheduled)))
753 process_scheduled_works(worker);
754 } else {
755 move_linked_works(work, &worker->scheduled,
756 NULL);
757 process_scheduled_works(worker);
758 }
759 }
760
761 spin_unlock_irq(&cwq->lock);
1da177e4 762 }
3af24433 763
1da177e4
LT
764 return 0;
765}
766
fc2e4d70
ON
767struct wq_barrier {
768 struct work_struct work;
769 struct completion done;
770};
771
772static void wq_barrier_func(struct work_struct *work)
773{
774 struct wq_barrier *barr = container_of(work, struct wq_barrier, work);
775 complete(&barr->done);
776}
777
4690c4ab
TH
778/**
779 * insert_wq_barrier - insert a barrier work
780 * @cwq: cwq to insert barrier into
781 * @barr: wq_barrier to insert
affee4b2
TH
782 * @target: target work to attach @barr to
783 * @worker: worker currently executing @target, NULL if @target is not executing
4690c4ab 784 *
affee4b2
TH
785 * @barr is linked to @target such that @barr is completed only after
786 * @target finishes execution. Please note that the ordering
787 * guarantee is observed only with respect to @target and on the local
788 * cpu.
789 *
790 * Currently, a queued barrier can't be canceled. This is because
791 * try_to_grab_pending() can't determine whether the work to be
792 * grabbed is at the head of the queue and thus can't clear LINKED
793 * flag of the previous work while there must be a valid next work
794 * after a work with LINKED flag set.
795 *
796 * Note that when @worker is non-NULL, @target may be modified
797 * underneath us, so we can't reliably determine cwq from @target.
4690c4ab
TH
798 *
799 * CONTEXT:
800 * spin_lock_irq(cwq->lock).
801 */
83c22520 802static void insert_wq_barrier(struct cpu_workqueue_struct *cwq,
affee4b2
TH
803 struct wq_barrier *barr,
804 struct work_struct *target, struct worker *worker)
fc2e4d70 805{
affee4b2
TH
806 struct list_head *head;
807 unsigned int linked = 0;
808
dc186ad7
TG
809 /*
810 * debugobject calls are safe here even with cwq->lock locked
811 * as we know for sure that this will not trigger any of the
812 * checks and call back into the fixup functions where we
813 * might deadlock.
814 */
815 INIT_WORK_ON_STACK(&barr->work, wq_barrier_func);
22df02bb 816 __set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(&barr->work));
fc2e4d70 817 init_completion(&barr->done);
83c22520 818
affee4b2
TH
819 /*
820 * If @target is currently being executed, schedule the
821 * barrier to the worker; otherwise, put it after @target.
822 */
823 if (worker)
824 head = worker->scheduled.next;
825 else {
826 unsigned long *bits = work_data_bits(target);
827
828 head = target->entry.next;
829 /* there can already be other linked works, inherit and set */
830 linked = *bits & WORK_STRUCT_LINKED;
831 __set_bit(WORK_STRUCT_LINKED_BIT, bits);
832 }
833
dc186ad7 834 debug_work_activate(&barr->work);
affee4b2
TH
835 insert_work(cwq, &barr->work, head,
836 work_color_to_flags(WORK_NO_COLOR) | linked);
fc2e4d70
ON
837}
838
73f53c4a
TH
839/**
840 * flush_workqueue_prep_cwqs - prepare cwqs for workqueue flushing
841 * @wq: workqueue being flushed
842 * @flush_color: new flush color, < 0 for no-op
843 * @work_color: new work color, < 0 for no-op
844 *
845 * Prepare cwqs for workqueue flushing.
846 *
847 * If @flush_color is non-negative, flush_color on all cwqs should be
848 * -1. If no cwq has in-flight commands at the specified color, all
849 * cwq->flush_color's stay at -1 and %false is returned. If any cwq
850 * has in flight commands, its cwq->flush_color is set to
851 * @flush_color, @wq->nr_cwqs_to_flush is updated accordingly, cwq
852 * wakeup logic is armed and %true is returned.
853 *
854 * The caller should have initialized @wq->first_flusher prior to
855 * calling this function with non-negative @flush_color. If
856 * @flush_color is negative, no flush color update is done and %false
857 * is returned.
858 *
859 * If @work_color is non-negative, all cwqs should have the same
860 * work_color which is previous to @work_color and all will be
861 * advanced to @work_color.
862 *
863 * CONTEXT:
864 * mutex_lock(wq->flush_mutex).
865 *
866 * RETURNS:
867 * %true if @flush_color >= 0 and there's something to flush. %false
868 * otherwise.
869 */
870static bool flush_workqueue_prep_cwqs(struct workqueue_struct *wq,
871 int flush_color, int work_color)
1da177e4 872{
73f53c4a
TH
873 bool wait = false;
874 unsigned int cpu;
1da177e4 875
73f53c4a
TH
876 if (flush_color >= 0) {
877 BUG_ON(atomic_read(&wq->nr_cwqs_to_flush));
878 atomic_set(&wq->nr_cwqs_to_flush, 1);
1da177e4 879 }
2355b70f 880
73f53c4a
TH
881 for_each_possible_cpu(cpu) {
882 struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
883
884 spin_lock_irq(&cwq->lock);
885
886 if (flush_color >= 0) {
887 BUG_ON(cwq->flush_color != -1);
888
889 if (cwq->nr_in_flight[flush_color]) {
890 cwq->flush_color = flush_color;
891 atomic_inc(&wq->nr_cwqs_to_flush);
892 wait = true;
893 }
894 }
895
896 if (work_color >= 0) {
897 BUG_ON(work_color != work_next_color(cwq->work_color));
898 cwq->work_color = work_color;
899 }
900
901 spin_unlock_irq(&cwq->lock);
dc186ad7 902 }
14441960 903
73f53c4a
TH
904 if (flush_color >= 0 && atomic_dec_and_test(&wq->nr_cwqs_to_flush))
905 complete(&wq->first_flusher->done);
906
907 return wait;
1da177e4
LT
908}
909
0fcb78c2 910/**
1da177e4 911 * flush_workqueue - ensure that any scheduled work has run to completion.
0fcb78c2 912 * @wq: workqueue to flush
1da177e4
LT
913 *
914 * Forces execution of the workqueue and blocks until its completion.
915 * This is typically used in driver shutdown handlers.
916 *
fc2e4d70
ON
917 * We sleep until all works which were queued on entry have been handled,
918 * but we are not livelocked by new incoming ones.
1da177e4 919 */
7ad5b3a5 920void flush_workqueue(struct workqueue_struct *wq)
1da177e4 921{
73f53c4a
TH
922 struct wq_flusher this_flusher = {
923 .list = LIST_HEAD_INIT(this_flusher.list),
924 .flush_color = -1,
925 .done = COMPLETION_INITIALIZER_ONSTACK(this_flusher.done),
926 };
927 int next_color;
1da177e4 928
3295f0ef
IM
929 lock_map_acquire(&wq->lockdep_map);
930 lock_map_release(&wq->lockdep_map);
73f53c4a
TH
931
932 mutex_lock(&wq->flush_mutex);
933
934 /*
935 * Start-to-wait phase
936 */
937 next_color = work_next_color(wq->work_color);
938
939 if (next_color != wq->flush_color) {
940 /*
941 * Color space is not full. The current work_color
942 * becomes our flush_color and work_color is advanced
943 * by one.
944 */
945 BUG_ON(!list_empty(&wq->flusher_overflow));
946 this_flusher.flush_color = wq->work_color;
947 wq->work_color = next_color;
948
949 if (!wq->first_flusher) {
950 /* no flush in progress, become the first flusher */
951 BUG_ON(wq->flush_color != this_flusher.flush_color);
952
953 wq->first_flusher = &this_flusher;
954
955 if (!flush_workqueue_prep_cwqs(wq, wq->flush_color,
956 wq->work_color)) {
957 /* nothing to flush, done */
958 wq->flush_color = next_color;
959 wq->first_flusher = NULL;
960 goto out_unlock;
961 }
962 } else {
963 /* wait in queue */
964 BUG_ON(wq->flush_color == this_flusher.flush_color);
965 list_add_tail(&this_flusher.list, &wq->flusher_queue);
966 flush_workqueue_prep_cwqs(wq, -1, wq->work_color);
967 }
968 } else {
969 /*
970 * Oops, color space is full, wait on overflow queue.
971 * The next flush completion will assign us
972 * flush_color and transfer to flusher_queue.
973 */
974 list_add_tail(&this_flusher.list, &wq->flusher_overflow);
975 }
976
977 mutex_unlock(&wq->flush_mutex);
978
979 wait_for_completion(&this_flusher.done);
980
981 /*
982 * Wake-up-and-cascade phase
983 *
984 * First flushers are responsible for cascading flushes and
985 * handling overflow. Non-first flushers can simply return.
986 */
987 if (wq->first_flusher != &this_flusher)
988 return;
989
990 mutex_lock(&wq->flush_mutex);
991
992 wq->first_flusher = NULL;
993
994 BUG_ON(!list_empty(&this_flusher.list));
995 BUG_ON(wq->flush_color != this_flusher.flush_color);
996
997 while (true) {
998 struct wq_flusher *next, *tmp;
999
1000 /* complete all the flushers sharing the current flush color */
1001 list_for_each_entry_safe(next, tmp, &wq->flusher_queue, list) {
1002 if (next->flush_color != wq->flush_color)
1003 break;
1004 list_del_init(&next->list);
1005 complete(&next->done);
1006 }
1007
1008 BUG_ON(!list_empty(&wq->flusher_overflow) &&
1009 wq->flush_color != work_next_color(wq->work_color));
1010
1011 /* this flush_color is finished, advance by one */
1012 wq->flush_color = work_next_color(wq->flush_color);
1013
1014 /* one color has been freed, handle overflow queue */
1015 if (!list_empty(&wq->flusher_overflow)) {
1016 /*
1017 * Assign the same color to all overflowed
1018 * flushers, advance work_color and append to
1019 * flusher_queue. This is the start-to-wait
1020 * phase for these overflowed flushers.
1021 */
1022 list_for_each_entry(tmp, &wq->flusher_overflow, list)
1023 tmp->flush_color = wq->work_color;
1024
1025 wq->work_color = work_next_color(wq->work_color);
1026
1027 list_splice_tail_init(&wq->flusher_overflow,
1028 &wq->flusher_queue);
1029 flush_workqueue_prep_cwqs(wq, -1, wq->work_color);
1030 }
1031
1032 if (list_empty(&wq->flusher_queue)) {
1033 BUG_ON(wq->flush_color != wq->work_color);
1034 break;
1035 }
1036
1037 /*
1038 * Need to flush more colors. Make the next flusher
1039 * the new first flusher and arm cwqs.
1040 */
1041 BUG_ON(wq->flush_color == wq->work_color);
1042 BUG_ON(wq->flush_color != next->flush_color);
1043
1044 list_del_init(&next->list);
1045 wq->first_flusher = next;
1046
1047 if (flush_workqueue_prep_cwqs(wq, wq->flush_color, -1))
1048 break;
1049
1050 /*
1051 * Meh... this color is already done, clear first
1052 * flusher and repeat cascading.
1053 */
1054 wq->first_flusher = NULL;
1055 }
1056
1057out_unlock:
1058 mutex_unlock(&wq->flush_mutex);
1da177e4 1059}
ae90dd5d 1060EXPORT_SYMBOL_GPL(flush_workqueue);
1da177e4 1061
db700897
ON
1062/**
1063 * flush_work - block until a work_struct's callback has terminated
1064 * @work: the work which is to be flushed
1065 *
a67da70d
ON
1066 * Returns false if @work has already terminated.
1067 *
db700897
ON
1068 * It is expected that, prior to calling flush_work(), the caller has
1069 * arranged for the work to not be requeued, otherwise it doesn't make
1070 * sense to use this function.
1071 */
1072int flush_work(struct work_struct *work)
1073{
affee4b2 1074 struct worker *worker = NULL;
db700897 1075 struct cpu_workqueue_struct *cwq;
db700897
ON
1076 struct wq_barrier barr;
1077
1078 might_sleep();
1079 cwq = get_wq_data(work);
1080 if (!cwq)
1081 return 0;
1082
3295f0ef
IM
1083 lock_map_acquire(&cwq->wq->lockdep_map);
1084 lock_map_release(&cwq->wq->lockdep_map);
a67da70d 1085
db700897
ON
1086 spin_lock_irq(&cwq->lock);
1087 if (!list_empty(&work->entry)) {
1088 /*
1089 * See the comment near try_to_grab_pending()->smp_rmb().
1090 * If it was re-queued under us we are not going to wait.
1091 */
1092 smp_rmb();
1093 if (unlikely(cwq != get_wq_data(work)))
4690c4ab 1094 goto already_gone;
db700897 1095 } else {
affee4b2
TH
1096 if (cwq->worker && cwq->worker->current_work == work)
1097 worker = cwq->worker;
1098 if (!worker)
4690c4ab 1099 goto already_gone;
db700897 1100 }
db700897 1101
affee4b2 1102 insert_wq_barrier(cwq, &barr, work, worker);
4690c4ab 1103 spin_unlock_irq(&cwq->lock);
db700897 1104 wait_for_completion(&barr.done);
dc186ad7 1105 destroy_work_on_stack(&barr.work);
db700897 1106 return 1;
4690c4ab
TH
1107already_gone:
1108 spin_unlock_irq(&cwq->lock);
1109 return 0;
db700897
ON
1110}
1111EXPORT_SYMBOL_GPL(flush_work);
1112
6e84d644 1113/*
1f1f642e 1114 * Upon a successful return (>= 0), the caller "owns" WORK_STRUCT_PENDING bit,
6e84d644
ON
1115 * so this work can't be re-armed in any way.
1116 */
1117static int try_to_grab_pending(struct work_struct *work)
1118{
1119 struct cpu_workqueue_struct *cwq;
1f1f642e 1120 int ret = -1;
6e84d644 1121
22df02bb 1122 if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work)))
1f1f642e 1123 return 0;
6e84d644
ON
1124
1125 /*
1126 * The queueing is in progress, or it is already queued. Try to
1127 * steal it from ->worklist without clearing WORK_STRUCT_PENDING.
1128 */
1129
1130 cwq = get_wq_data(work);
1131 if (!cwq)
1132 return ret;
1133
1134 spin_lock_irq(&cwq->lock);
1135 if (!list_empty(&work->entry)) {
1136 /*
1137 * This work is queued, but perhaps we locked the wrong cwq.
1138 * In that case we must see the new value after rmb(), see
1139 * insert_work()->wmb().
1140 */
1141 smp_rmb();
1142 if (cwq == get_wq_data(work)) {
dc186ad7 1143 debug_work_deactivate(work);
6e84d644 1144 list_del_init(&work->entry);
73f53c4a 1145 cwq_dec_nr_in_flight(cwq, get_work_color(work));
6e84d644
ON
1146 ret = 1;
1147 }
1148 }
1149 spin_unlock_irq(&cwq->lock);
1150
1151 return ret;
1152}
1153
1154static void wait_on_cpu_work(struct cpu_workqueue_struct *cwq,
b89deed3
ON
1155 struct work_struct *work)
1156{
1157 struct wq_barrier barr;
affee4b2 1158 struct worker *worker;
b89deed3
ON
1159
1160 spin_lock_irq(&cwq->lock);
affee4b2
TH
1161
1162 worker = NULL;
c34056a3 1163 if (unlikely(cwq->worker && cwq->worker->current_work == work)) {
affee4b2
TH
1164 worker = cwq->worker;
1165 insert_wq_barrier(cwq, &barr, work, worker);
b89deed3 1166 }
affee4b2 1167
b89deed3
ON
1168 spin_unlock_irq(&cwq->lock);
1169
affee4b2 1170 if (unlikely(worker)) {
b89deed3 1171 wait_for_completion(&barr.done);
dc186ad7
TG
1172 destroy_work_on_stack(&barr.work);
1173 }
b89deed3
ON
1174}
1175
6e84d644 1176static void wait_on_work(struct work_struct *work)
b89deed3
ON
1177{
1178 struct cpu_workqueue_struct *cwq;
28e53bdd 1179 struct workqueue_struct *wq;
b1f4ec17 1180 int cpu;
b89deed3 1181
f293ea92
ON
1182 might_sleep();
1183
3295f0ef
IM
1184 lock_map_acquire(&work->lockdep_map);
1185 lock_map_release(&work->lockdep_map);
4e6045f1 1186
b89deed3 1187 cwq = get_wq_data(work);
b89deed3 1188 if (!cwq)
3af24433 1189 return;
b89deed3 1190
28e53bdd 1191 wq = cwq->wq;
28e53bdd 1192
1537663f 1193 for_each_possible_cpu(cpu)
4690c4ab 1194 wait_on_cpu_work(get_cwq(cpu, wq), work);
6e84d644
ON
1195}
1196
1f1f642e
ON
1197static int __cancel_work_timer(struct work_struct *work,
1198 struct timer_list* timer)
1199{
1200 int ret;
1201
1202 do {
1203 ret = (timer && likely(del_timer(timer)));
1204 if (!ret)
1205 ret = try_to_grab_pending(work);
1206 wait_on_work(work);
1207 } while (unlikely(ret < 0));
1208
4d707b9f 1209 clear_wq_data(work);
1f1f642e
ON
1210 return ret;
1211}
1212
6e84d644
ON
1213/**
1214 * cancel_work_sync - block until a work_struct's callback has terminated
1215 * @work: the work which is to be flushed
1216 *
1f1f642e
ON
1217 * Returns true if @work was pending.
1218 *
6e84d644
ON
1219 * cancel_work_sync() will cancel the work if it is queued. If the work's
1220 * callback appears to be running, cancel_work_sync() will block until it
1221 * has completed.
1222 *
1223 * It is possible to use this function if the work re-queues itself. It can
1224 * cancel the work even if it migrates to another workqueue, however in that
1225 * case it only guarantees that work->func() has completed on the last queued
1226 * workqueue.
1227 *
1228 * cancel_work_sync(&delayed_work->work) should be used only if ->timer is not
1229 * pending, otherwise it goes into a busy-wait loop until the timer expires.
1230 *
1231 * The caller must ensure that workqueue_struct on which this work was last
1232 * queued can't be destroyed before this function returns.
1233 */
1f1f642e 1234int cancel_work_sync(struct work_struct *work)
6e84d644 1235{
1f1f642e 1236 return __cancel_work_timer(work, NULL);
b89deed3 1237}
28e53bdd 1238EXPORT_SYMBOL_GPL(cancel_work_sync);
b89deed3 1239
6e84d644 1240/**
f5a421a4 1241 * cancel_delayed_work_sync - reliably kill off a delayed work.
6e84d644
ON
1242 * @dwork: the delayed work struct
1243 *
1f1f642e
ON
1244 * Returns true if @dwork was pending.
1245 *
6e84d644
ON
1246 * It is possible to use this function if @dwork rearms itself via queue_work()
1247 * or queue_delayed_work(). See also the comment for cancel_work_sync().
1248 */
1f1f642e 1249int cancel_delayed_work_sync(struct delayed_work *dwork)
6e84d644 1250{
1f1f642e 1251 return __cancel_work_timer(&dwork->work, &dwork->timer);
6e84d644 1252}
f5a421a4 1253EXPORT_SYMBOL(cancel_delayed_work_sync);
1da177e4 1254
6e84d644 1255static struct workqueue_struct *keventd_wq __read_mostly;
1da177e4 1256
0fcb78c2
REB
1257/**
1258 * schedule_work - put work task in global workqueue
1259 * @work: job to be done
1260 *
5b0f437d
BVA
1261 * Returns zero if @work was already on the kernel-global workqueue and
1262 * non-zero otherwise.
1263 *
1264 * This puts a job in the kernel-global workqueue if it was not already
1265 * queued and leaves it in the same position on the kernel-global
1266 * workqueue otherwise.
0fcb78c2 1267 */
7ad5b3a5 1268int schedule_work(struct work_struct *work)
1da177e4
LT
1269{
1270 return queue_work(keventd_wq, work);
1271}
ae90dd5d 1272EXPORT_SYMBOL(schedule_work);
1da177e4 1273
c1a220e7
ZR
1274/*
1275 * schedule_work_on - put work task on a specific cpu
1276 * @cpu: cpu to put the work task on
1277 * @work: job to be done
1278 *
1279 * This puts a job on a specific cpu
1280 */
1281int schedule_work_on(int cpu, struct work_struct *work)
1282{
1283 return queue_work_on(cpu, keventd_wq, work);
1284}
1285EXPORT_SYMBOL(schedule_work_on);
1286
0fcb78c2
REB
1287/**
1288 * schedule_delayed_work - put work task in global workqueue after delay
52bad64d
DH
1289 * @dwork: job to be done
1290 * @delay: number of jiffies to wait or 0 for immediate execution
0fcb78c2
REB
1291 *
1292 * After waiting for a given time this puts a job in the kernel-global
1293 * workqueue.
1294 */
7ad5b3a5 1295int schedule_delayed_work(struct delayed_work *dwork,
82f67cd9 1296 unsigned long delay)
1da177e4 1297{
52bad64d 1298 return queue_delayed_work(keventd_wq, dwork, delay);
1da177e4 1299}
ae90dd5d 1300EXPORT_SYMBOL(schedule_delayed_work);
1da177e4 1301
8c53e463
LT
1302/**
1303 * flush_delayed_work - block until a dwork_struct's callback has terminated
1304 * @dwork: the delayed work which is to be flushed
1305 *
1306 * Any timeout is cancelled, and any pending work is run immediately.
1307 */
1308void flush_delayed_work(struct delayed_work *dwork)
1309{
1310 if (del_timer_sync(&dwork->timer)) {
4690c4ab
TH
1311 __queue_work(get_cpu(), get_wq_data(&dwork->work)->wq,
1312 &dwork->work);
8c53e463
LT
1313 put_cpu();
1314 }
1315 flush_work(&dwork->work);
1316}
1317EXPORT_SYMBOL(flush_delayed_work);
1318
0fcb78c2
REB
1319/**
1320 * schedule_delayed_work_on - queue work in global workqueue on CPU after delay
1321 * @cpu: cpu to use
52bad64d 1322 * @dwork: job to be done
0fcb78c2
REB
1323 * @delay: number of jiffies to wait
1324 *
1325 * After waiting for a given time this puts a job in the kernel-global
1326 * workqueue on the specified CPU.
1327 */
1da177e4 1328int schedule_delayed_work_on(int cpu,
52bad64d 1329 struct delayed_work *dwork, unsigned long delay)
1da177e4 1330{
52bad64d 1331 return queue_delayed_work_on(cpu, keventd_wq, dwork, delay);
1da177e4 1332}
ae90dd5d 1333EXPORT_SYMBOL(schedule_delayed_work_on);
1da177e4 1334
b6136773
AM
1335/**
1336 * schedule_on_each_cpu - call a function on each online CPU from keventd
1337 * @func: the function to call
b6136773
AM
1338 *
1339 * Returns zero on success.
1340 * Returns -ve errno on failure.
1341 *
b6136773
AM
1342 * schedule_on_each_cpu() is very slow.
1343 */
65f27f38 1344int schedule_on_each_cpu(work_func_t func)
15316ba8
CL
1345{
1346 int cpu;
65a64464 1347 int orig = -1;
b6136773 1348 struct work_struct *works;
15316ba8 1349
b6136773
AM
1350 works = alloc_percpu(struct work_struct);
1351 if (!works)
15316ba8 1352 return -ENOMEM;
b6136773 1353
93981800
TH
1354 get_online_cpus();
1355
65a64464 1356 /*
93981800
TH
1357 * When running in keventd don't schedule a work item on
1358 * itself. Can just call directly because the work queue is
1359 * already bound. This also is faster.
65a64464 1360 */
93981800 1361 if (current_is_keventd())
65a64464 1362 orig = raw_smp_processor_id();
65a64464 1363
15316ba8 1364 for_each_online_cpu(cpu) {
9bfb1839
IM
1365 struct work_struct *work = per_cpu_ptr(works, cpu);
1366
1367 INIT_WORK(work, func);
65a64464 1368 if (cpu != orig)
93981800 1369 schedule_work_on(cpu, work);
65a64464 1370 }
93981800
TH
1371 if (orig >= 0)
1372 func(per_cpu_ptr(works, orig));
1373
1374 for_each_online_cpu(cpu)
1375 flush_work(per_cpu_ptr(works, cpu));
1376
95402b38 1377 put_online_cpus();
b6136773 1378 free_percpu(works);
15316ba8
CL
1379 return 0;
1380}
1381
eef6a7d5
AS
1382/**
1383 * flush_scheduled_work - ensure that any scheduled work has run to completion.
1384 *
1385 * Forces execution of the kernel-global workqueue and blocks until its
1386 * completion.
1387 *
1388 * Think twice before calling this function! It's very easy to get into
1389 * trouble if you don't take great care. Either of the following situations
1390 * will lead to deadlock:
1391 *
1392 * One of the work items currently on the workqueue needs to acquire
1393 * a lock held by your code or its caller.
1394 *
1395 * Your code is running in the context of a work routine.
1396 *
1397 * They will be detected by lockdep when they occur, but the first might not
1398 * occur very often. It depends on what work items are on the workqueue and
1399 * what locks they need, which you have no control over.
1400 *
1401 * In most situations flushing the entire workqueue is overkill; you merely
1402 * need to know that a particular work item isn't queued and isn't running.
1403 * In such cases you should use cancel_delayed_work_sync() or
1404 * cancel_work_sync() instead.
1405 */
1da177e4
LT
1406void flush_scheduled_work(void)
1407{
1408 flush_workqueue(keventd_wq);
1409}
ae90dd5d 1410EXPORT_SYMBOL(flush_scheduled_work);
1da177e4 1411
1fa44eca
JB
1412/**
1413 * execute_in_process_context - reliably execute the routine with user context
1414 * @fn: the function to execute
1fa44eca
JB
1415 * @ew: guaranteed storage for the execute work structure (must
1416 * be available when the work executes)
1417 *
1418 * Executes the function immediately if process context is available,
1419 * otherwise schedules the function for delayed execution.
1420 *
1421 * Returns: 0 - function was executed
1422 * 1 - function was scheduled for execution
1423 */
65f27f38 1424int execute_in_process_context(work_func_t fn, struct execute_work *ew)
1fa44eca
JB
1425{
1426 if (!in_interrupt()) {
65f27f38 1427 fn(&ew->work);
1fa44eca
JB
1428 return 0;
1429 }
1430
65f27f38 1431 INIT_WORK(&ew->work, fn);
1fa44eca
JB
1432 schedule_work(&ew->work);
1433
1434 return 1;
1435}
1436EXPORT_SYMBOL_GPL(execute_in_process_context);
1437
1da177e4
LT
1438int keventd_up(void)
1439{
1440 return keventd_wq != NULL;
1441}
1442
1443int current_is_keventd(void)
1444{
1445 struct cpu_workqueue_struct *cwq;
d243769d 1446 int cpu = raw_smp_processor_id(); /* preempt-safe: keventd is per-cpu */
1da177e4
LT
1447 int ret = 0;
1448
1449 BUG_ON(!keventd_wq);
1450
1537663f 1451 cwq = get_cwq(cpu, keventd_wq);
c34056a3 1452 if (current == cwq->worker->task)
1da177e4
LT
1453 ret = 1;
1454
1455 return ret;
1456
1457}
1458
0f900049
TH
1459static struct cpu_workqueue_struct *alloc_cwqs(void)
1460{
1461 /*
1462 * cwqs are forced aligned according to WORK_STRUCT_FLAG_BITS.
1463 * Make sure that the alignment isn't lower than that of
1464 * unsigned long long.
1465 */
1466 const size_t size = sizeof(struct cpu_workqueue_struct);
1467 const size_t align = max_t(size_t, 1 << WORK_STRUCT_FLAG_BITS,
1468 __alignof__(unsigned long long));
1469 struct cpu_workqueue_struct *cwqs;
1470#ifndef CONFIG_SMP
1471 void *ptr;
1472
1473 /*
1474 * On UP, percpu allocator doesn't honor alignment parameter
1475 * and simply uses arch-dependent default. Allocate enough
1476 * room to align cwq and put an extra pointer at the end
1477 * pointing back to the originally allocated pointer which
1478 * will be used for free.
1479 *
1480 * FIXME: This really belongs to UP percpu code. Update UP
1481 * percpu code to honor alignment and remove this ugliness.
1482 */
1483 ptr = __alloc_percpu(size + align + sizeof(void *), 1);
1484 cwqs = PTR_ALIGN(ptr, align);
1485 *(void **)per_cpu_ptr(cwqs + 1, 0) = ptr;
1486#else
1487 /* On SMP, percpu allocator can do it itself */
1488 cwqs = __alloc_percpu(size, align);
1489#endif
1490 /* just in case, make sure it's actually aligned */
1491 BUG_ON(!IS_ALIGNED((unsigned long)cwqs, align));
1492 return cwqs;
1493}
1494
1495static void free_cwqs(struct cpu_workqueue_struct *cwqs)
1496{
1497#ifndef CONFIG_SMP
1498 /* on UP, the pointer to free is stored right after the cwq */
1499 if (cwqs)
1500 free_percpu(*(void **)per_cpu_ptr(cwqs + 1, 0));
1501#else
1502 free_percpu(cwqs);
1503#endif
1504}
1505
4e6045f1 1506struct workqueue_struct *__create_workqueue_key(const char *name,
97e37d7b 1507 unsigned int flags,
eb13ba87
JB
1508 struct lock_class_key *key,
1509 const char *lock_name)
1da177e4 1510{
1537663f 1511 bool singlethread = flags & WQ_SINGLE_THREAD;
1da177e4 1512 struct workqueue_struct *wq;
c34056a3
TH
1513 bool failed = false;
1514 unsigned int cpu;
1da177e4 1515
3af24433
ON
1516 wq = kzalloc(sizeof(*wq), GFP_KERNEL);
1517 if (!wq)
4690c4ab 1518 goto err;
3af24433 1519
0f900049 1520 wq->cpu_wq = alloc_cwqs();
4690c4ab
TH
1521 if (!wq->cpu_wq)
1522 goto err;
3af24433 1523
97e37d7b 1524 wq->flags = flags;
73f53c4a
TH
1525 mutex_init(&wq->flush_mutex);
1526 atomic_set(&wq->nr_cwqs_to_flush, 0);
1527 INIT_LIST_HEAD(&wq->flusher_queue);
1528 INIT_LIST_HEAD(&wq->flusher_overflow);
3af24433 1529 wq->name = name;
eb13ba87 1530 lockdep_init_map(&wq->lockdep_map, lock_name, key, 0);
cce1a165 1531 INIT_LIST_HEAD(&wq->list);
3af24433 1532
1537663f
TH
1533 cpu_maps_update_begin();
1534 /*
1535 * We must initialize cwqs for each possible cpu even if we
1536 * are going to call destroy_workqueue() finally. Otherwise
1537 * cpu_up() can hit the uninitialized cwq once we drop the
1538 * lock.
1539 */
1540 for_each_possible_cpu(cpu) {
1541 struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
1542
0f900049 1543 BUG_ON((unsigned long)cwq & WORK_STRUCT_FLAG_MASK);
1537663f 1544 cwq->cpu = cpu;
c34056a3 1545 cwq->wq = wq;
73f53c4a 1546 cwq->flush_color = -1;
1537663f
TH
1547 spin_lock_init(&cwq->lock);
1548 INIT_LIST_HEAD(&cwq->worklist);
1549 init_waitqueue_head(&cwq->more_work);
1550
c34056a3 1551 if (failed)
1537663f 1552 continue;
c34056a3
TH
1553 cwq->worker = create_worker(cwq,
1554 cpu_online(cpu) && !singlethread);
1555 if (cwq->worker)
1556 start_worker(cwq->worker);
1537663f 1557 else
c34056a3 1558 failed = true;
3af24433
ON
1559 }
1560
1537663f
TH
1561 spin_lock(&workqueue_lock);
1562 list_add(&wq->list, &workqueues);
1563 spin_unlock(&workqueue_lock);
1564
1565 cpu_maps_update_done();
1566
c34056a3 1567 if (failed) {
3af24433
ON
1568 destroy_workqueue(wq);
1569 wq = NULL;
1570 }
1571 return wq;
4690c4ab
TH
1572err:
1573 if (wq) {
0f900049 1574 free_cwqs(wq->cpu_wq);
4690c4ab
TH
1575 kfree(wq);
1576 }
1577 return NULL;
3af24433 1578}
4e6045f1 1579EXPORT_SYMBOL_GPL(__create_workqueue_key);
1da177e4 1580
3af24433
ON
1581/**
1582 * destroy_workqueue - safely terminate a workqueue
1583 * @wq: target workqueue
1584 *
1585 * Safely destroy a workqueue. All work currently pending will be done first.
1586 */
1587void destroy_workqueue(struct workqueue_struct *wq)
1588{
b1f4ec17 1589 int cpu;
3af24433 1590
3da1c84c 1591 cpu_maps_update_begin();
95402b38 1592 spin_lock(&workqueue_lock);
b1f4ec17 1593 list_del(&wq->list);
95402b38 1594 spin_unlock(&workqueue_lock);
1537663f 1595 cpu_maps_update_done();
3af24433 1596
73f53c4a
TH
1597 flush_workqueue(wq);
1598
1599 for_each_possible_cpu(cpu) {
1600 struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
1601 int i;
1602
c34056a3
TH
1603 if (cwq->worker) {
1604 destroy_worker(cwq->worker);
1605 cwq->worker = NULL;
73f53c4a
TH
1606 }
1607
1608 for (i = 0; i < WORK_NR_COLORS; i++)
1609 BUG_ON(cwq->nr_in_flight[i]);
1610 }
9b41ea72 1611
0f900049 1612 free_cwqs(wq->cpu_wq);
3af24433
ON
1613 kfree(wq);
1614}
1615EXPORT_SYMBOL_GPL(destroy_workqueue);
1616
1617static int __devinit workqueue_cpu_callback(struct notifier_block *nfb,
1618 unsigned long action,
1619 void *hcpu)
1620{
1621 unsigned int cpu = (unsigned long)hcpu;
1622 struct cpu_workqueue_struct *cwq;
1623 struct workqueue_struct *wq;
1624
8bb78442
RW
1625 action &= ~CPU_TASKS_FROZEN;
1626
3af24433 1627 list_for_each_entry(wq, &workqueues, list) {
1537663f
TH
1628 if (wq->flags & WQ_SINGLE_THREAD)
1629 continue;
3af24433 1630
1537663f 1631 cwq = get_cwq(cpu, wq);
3af24433 1632
1537663f 1633 switch (action) {
3da1c84c 1634 case CPU_POST_DEAD:
73f53c4a 1635 flush_workqueue(wq);
3af24433
ON
1636 break;
1637 }
1da177e4
LT
1638 }
1639
1537663f 1640 return notifier_from_errno(0);
1da177e4 1641}
1da177e4 1642
2d3854a3 1643#ifdef CONFIG_SMP
8ccad40d 1644
2d3854a3 1645struct work_for_cpu {
6b44003e 1646 struct completion completion;
2d3854a3
RR
1647 long (*fn)(void *);
1648 void *arg;
1649 long ret;
1650};
1651
6b44003e 1652static int do_work_for_cpu(void *_wfc)
2d3854a3 1653{
6b44003e 1654 struct work_for_cpu *wfc = _wfc;
2d3854a3 1655 wfc->ret = wfc->fn(wfc->arg);
6b44003e
AM
1656 complete(&wfc->completion);
1657 return 0;
2d3854a3
RR
1658}
1659
1660/**
1661 * work_on_cpu - run a function in user context on a particular cpu
1662 * @cpu: the cpu to run on
1663 * @fn: the function to run
1664 * @arg: the function arg
1665 *
31ad9081
RR
1666 * This will return the value @fn returns.
1667 * It is up to the caller to ensure that the cpu doesn't go offline.
6b44003e 1668 * The caller must not hold any locks which would prevent @fn from completing.
2d3854a3
RR
1669 */
1670long work_on_cpu(unsigned int cpu, long (*fn)(void *), void *arg)
1671{
6b44003e
AM
1672 struct task_struct *sub_thread;
1673 struct work_for_cpu wfc = {
1674 .completion = COMPLETION_INITIALIZER_ONSTACK(wfc.completion),
1675 .fn = fn,
1676 .arg = arg,
1677 };
1678
1679 sub_thread = kthread_create(do_work_for_cpu, &wfc, "work_for_cpu");
1680 if (IS_ERR(sub_thread))
1681 return PTR_ERR(sub_thread);
1682 kthread_bind(sub_thread, cpu);
1683 wake_up_process(sub_thread);
1684 wait_for_completion(&wfc.completion);
2d3854a3
RR
1685 return wfc.ret;
1686}
1687EXPORT_SYMBOL_GPL(work_on_cpu);
1688#endif /* CONFIG_SMP */
1689
c12920d1 1690void __init init_workqueues(void)
1da177e4 1691{
c34056a3
TH
1692 unsigned int cpu;
1693
1694 for_each_possible_cpu(cpu)
1695 ida_init(&per_cpu(worker_ida, cpu));
1696
e7577c50 1697 singlethread_cpu = cpumask_first(cpu_possible_mask);
1da177e4
LT
1698 hotcpu_notifier(workqueue_cpu_callback, 0);
1699 keventd_wq = create_workqueue("events");
1700 BUG_ON(!keventd_wq);
1701}