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