workqueue: implement unbound workqueue
[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>
e22bee78
TH
37
38#include "workqueue_sched.h"
1da177e4 39
c8e55f36 40enum {
db7bccf4 41 /* global_cwq flags */
e22bee78
TH
42 GCWQ_MANAGE_WORKERS = 1 << 0, /* need to manage workers */
43 GCWQ_MANAGING_WORKERS = 1 << 1, /* managing workers */
44 GCWQ_DISASSOCIATED = 1 << 2, /* cpu can't serve workers */
db7bccf4 45 GCWQ_FREEZING = 1 << 3, /* freeze in progress */
649027d7 46 GCWQ_HIGHPRI_PENDING = 1 << 4, /* highpri works on queue */
db7bccf4 47
c8e55f36
TH
48 /* worker flags */
49 WORKER_STARTED = 1 << 0, /* started */
50 WORKER_DIE = 1 << 1, /* die die die */
51 WORKER_IDLE = 1 << 2, /* is idle */
e22bee78 52 WORKER_PREP = 1 << 3, /* preparing to run works */
db7bccf4 53 WORKER_ROGUE = 1 << 4, /* not bound to any cpu */
e22bee78 54 WORKER_REBIND = 1 << 5, /* mom is home, come back */
fb0e7beb 55 WORKER_CPU_INTENSIVE = 1 << 6, /* cpu intensive */
f3421797 56 WORKER_UNBOUND = 1 << 7, /* worker is unbound */
e22bee78 57
fb0e7beb 58 WORKER_NOT_RUNNING = WORKER_PREP | WORKER_ROGUE | WORKER_REBIND |
f3421797 59 WORKER_CPU_INTENSIVE | WORKER_UNBOUND,
db7bccf4
TH
60
61 /* gcwq->trustee_state */
62 TRUSTEE_START = 0, /* start */
63 TRUSTEE_IN_CHARGE = 1, /* trustee in charge of gcwq */
64 TRUSTEE_BUTCHER = 2, /* butcher workers */
65 TRUSTEE_RELEASE = 3, /* release workers */
66 TRUSTEE_DONE = 4, /* trustee is done */
c8e55f36
TH
67
68 BUSY_WORKER_HASH_ORDER = 6, /* 64 pointers */
69 BUSY_WORKER_HASH_SIZE = 1 << BUSY_WORKER_HASH_ORDER,
70 BUSY_WORKER_HASH_MASK = BUSY_WORKER_HASH_SIZE - 1,
db7bccf4 71
e22bee78
TH
72 MAX_IDLE_WORKERS_RATIO = 4, /* 1/4 of busy can be idle */
73 IDLE_WORKER_TIMEOUT = 300 * HZ, /* keep idle ones for 5 mins */
74
75 MAYDAY_INITIAL_TIMEOUT = HZ / 100, /* call for help after 10ms */
76 MAYDAY_INTERVAL = HZ / 10, /* and then every 100ms */
77 CREATE_COOLDOWN = HZ, /* time to breath after fail */
db7bccf4 78 TRUSTEE_COOLDOWN = HZ / 10, /* for trustee draining */
e22bee78
TH
79
80 /*
81 * Rescue workers are used only on emergencies and shared by
82 * all cpus. Give -20.
83 */
84 RESCUER_NICE_LEVEL = -20,
c8e55f36
TH
85};
86
4690c4ab
TH
87/*
88 * Structure fields follow one of the following exclusion rules.
89 *
90 * I: Set during initialization and read-only afterwards.
91 *
e22bee78
TH
92 * P: Preemption protected. Disabling preemption is enough and should
93 * only be modified and accessed from the local cpu.
94 *
8b03ae3c 95 * L: gcwq->lock protected. Access with gcwq->lock held.
4690c4ab 96 *
e22bee78
TH
97 * X: During normal operation, modification requires gcwq->lock and
98 * should be done only from local cpu. Either disabling preemption
99 * on local cpu or grabbing gcwq->lock is enough for read access.
f3421797 100 * If GCWQ_DISASSOCIATED is set, it's identical to L.
e22bee78 101 *
73f53c4a
TH
102 * F: wq->flush_mutex protected.
103 *
4690c4ab
TH
104 * W: workqueue_lock protected.
105 */
106
8b03ae3c 107struct global_cwq;
c34056a3 108
e22bee78
TH
109/*
110 * The poor guys doing the actual heavy lifting. All on-duty workers
111 * are either serving the manager role, on idle list or on busy hash.
112 */
c34056a3 113struct worker {
c8e55f36
TH
114 /* on idle list while idle, on busy hash table while busy */
115 union {
116 struct list_head entry; /* L: while idle */
117 struct hlist_node hentry; /* L: while busy */
118 };
119
c34056a3 120 struct work_struct *current_work; /* L: work being processed */
8cca0eea 121 struct cpu_workqueue_struct *current_cwq; /* L: current_work's cwq */
affee4b2 122 struct list_head scheduled; /* L: scheduled works */
c34056a3 123 struct task_struct *task; /* I: worker task */
8b03ae3c 124 struct global_cwq *gcwq; /* I: the associated gcwq */
e22bee78
TH
125 /* 64 bytes boundary on 64bit, 32 on 32bit */
126 unsigned long last_active; /* L: last active timestamp */
127 unsigned int flags; /* X: flags */
c34056a3 128 int id; /* I: worker id */
e22bee78 129 struct work_struct rebind_work; /* L: rebind worker to cpu */
c34056a3
TH
130};
131
8b03ae3c 132/*
e22bee78
TH
133 * Global per-cpu workqueue. There's one and only one for each cpu
134 * and all works are queued and processed here regardless of their
135 * target workqueues.
8b03ae3c
TH
136 */
137struct global_cwq {
138 spinlock_t lock; /* the gcwq lock */
7e11629d 139 struct list_head worklist; /* L: list of pending works */
8b03ae3c 140 unsigned int cpu; /* I: the associated cpu */
db7bccf4 141 unsigned int flags; /* L: GCWQ_* flags */
c8e55f36
TH
142
143 int nr_workers; /* L: total number of workers */
144 int nr_idle; /* L: currently idle ones */
145
146 /* workers are chained either in the idle_list or busy_hash */
e22bee78 147 struct list_head idle_list; /* X: list of idle workers */
c8e55f36
TH
148 struct hlist_head busy_hash[BUSY_WORKER_HASH_SIZE];
149 /* L: hash of busy workers */
150
e22bee78
TH
151 struct timer_list idle_timer; /* L: worker idle timeout */
152 struct timer_list mayday_timer; /* L: SOS timer for dworkers */
153
8b03ae3c 154 struct ida worker_ida; /* L: for worker IDs */
db7bccf4
TH
155
156 struct task_struct *trustee; /* L: for gcwq shutdown */
157 unsigned int trustee_state; /* L: trustee state */
158 wait_queue_head_t trustee_wait; /* trustee wait */
e22bee78 159 struct worker *first_idle; /* L: first idle worker */
8b03ae3c
TH
160} ____cacheline_aligned_in_smp;
161
1da177e4 162/*
502ca9d8 163 * The per-CPU workqueue. The lower WORK_STRUCT_FLAG_BITS of
0f900049
TH
164 * work_struct->data are used for flags and thus cwqs need to be
165 * aligned at two's power of the number of flag bits.
1da177e4
LT
166 */
167struct cpu_workqueue_struct {
8b03ae3c 168 struct global_cwq *gcwq; /* I: the associated gcwq */
4690c4ab 169 struct workqueue_struct *wq; /* I: the owning workqueue */
73f53c4a
TH
170 int work_color; /* L: current color */
171 int flush_color; /* L: flushing color */
172 int nr_in_flight[WORK_NR_COLORS];
173 /* L: nr of in_flight works */
1e19ffc6 174 int nr_active; /* L: nr of active works */
a0a1a5fd 175 int max_active; /* L: max active works */
1e19ffc6 176 struct list_head delayed_works; /* L: delayed works */
0f900049 177};
1da177e4 178
73f53c4a
TH
179/*
180 * Structure used to wait for workqueue flush.
181 */
182struct wq_flusher {
183 struct list_head list; /* F: list of flushers */
184 int flush_color; /* F: flush color waiting for */
185 struct completion done; /* flush completion */
186};
187
1da177e4
LT
188/*
189 * The externally visible workqueue abstraction is an array of
190 * per-CPU workqueues:
191 */
192struct workqueue_struct {
97e37d7b 193 unsigned int flags; /* I: WQ_* flags */
bdbc5dd7
TH
194 union {
195 struct cpu_workqueue_struct __percpu *pcpu;
196 struct cpu_workqueue_struct *single;
197 unsigned long v;
198 } cpu_wq; /* I: cwq's */
4690c4ab 199 struct list_head list; /* W: list of all workqueues */
73f53c4a
TH
200
201 struct mutex flush_mutex; /* protects wq flushing */
202 int work_color; /* F: current work color */
203 int flush_color; /* F: current flush color */
204 atomic_t nr_cwqs_to_flush; /* flush in progress */
205 struct wq_flusher *first_flusher; /* F: first flusher */
206 struct list_head flusher_queue; /* F: flush waiters */
207 struct list_head flusher_overflow; /* F: flush overflow list */
208
502ca9d8
TH
209 unsigned long single_cpu; /* cpu for single cpu wq */
210
e22bee78
TH
211 cpumask_var_t mayday_mask; /* cpus requesting rescue */
212 struct worker *rescuer; /* I: rescue worker */
213
dcd989cb 214 int saved_max_active; /* W: saved cwq max_active */
4690c4ab 215 const char *name; /* I: workqueue name */
4e6045f1 216#ifdef CONFIG_LOCKDEP
4690c4ab 217 struct lockdep_map lockdep_map;
4e6045f1 218#endif
1da177e4
LT
219};
220
d320c038
TH
221struct workqueue_struct *system_wq __read_mostly;
222struct workqueue_struct *system_long_wq __read_mostly;
223struct workqueue_struct *system_nrt_wq __read_mostly;
f3421797 224struct workqueue_struct *system_unbound_wq __read_mostly;
d320c038
TH
225EXPORT_SYMBOL_GPL(system_wq);
226EXPORT_SYMBOL_GPL(system_long_wq);
227EXPORT_SYMBOL_GPL(system_nrt_wq);
f3421797 228EXPORT_SYMBOL_GPL(system_unbound_wq);
d320c038 229
db7bccf4
TH
230#define for_each_busy_worker(worker, i, pos, gcwq) \
231 for (i = 0; i < BUSY_WORKER_HASH_SIZE; i++) \
232 hlist_for_each_entry(worker, pos, &gcwq->busy_hash[i], hentry)
233
f3421797
TH
234static inline int __next_gcwq_cpu(int cpu, const struct cpumask *mask,
235 unsigned int sw)
236{
237 if (cpu < nr_cpu_ids) {
238 if (sw & 1) {
239 cpu = cpumask_next(cpu, mask);
240 if (cpu < nr_cpu_ids)
241 return cpu;
242 }
243 if (sw & 2)
244 return WORK_CPU_UNBOUND;
245 }
246 return WORK_CPU_NONE;
247}
248
249static inline int __next_wq_cpu(int cpu, const struct cpumask *mask,
250 struct workqueue_struct *wq)
251{
252 return __next_gcwq_cpu(cpu, mask, !(wq->flags & WQ_UNBOUND) ? 1 : 2);
253}
254
255#define for_each_gcwq_cpu(cpu) \
256 for ((cpu) = __next_gcwq_cpu(-1, cpu_possible_mask, 3); \
257 (cpu) < WORK_CPU_NONE; \
258 (cpu) = __next_gcwq_cpu((cpu), cpu_possible_mask, 3))
259
260#define for_each_online_gcwq_cpu(cpu) \
261 for ((cpu) = __next_gcwq_cpu(-1, cpu_online_mask, 3); \
262 (cpu) < WORK_CPU_NONE; \
263 (cpu) = __next_gcwq_cpu((cpu), cpu_online_mask, 3))
264
265#define for_each_cwq_cpu(cpu, wq) \
266 for ((cpu) = __next_wq_cpu(-1, cpu_possible_mask, (wq)); \
267 (cpu) < WORK_CPU_NONE; \
268 (cpu) = __next_wq_cpu((cpu), cpu_possible_mask, (wq)))
269
dc186ad7
TG
270#ifdef CONFIG_DEBUG_OBJECTS_WORK
271
272static struct debug_obj_descr work_debug_descr;
273
274/*
275 * fixup_init is called when:
276 * - an active object is initialized
277 */
278static int work_fixup_init(void *addr, enum debug_obj_state state)
279{
280 struct work_struct *work = addr;
281
282 switch (state) {
283 case ODEBUG_STATE_ACTIVE:
284 cancel_work_sync(work);
285 debug_object_init(work, &work_debug_descr);
286 return 1;
287 default:
288 return 0;
289 }
290}
291
292/*
293 * fixup_activate is called when:
294 * - an active object is activated
295 * - an unknown object is activated (might be a statically initialized object)
296 */
297static int work_fixup_activate(void *addr, enum debug_obj_state state)
298{
299 struct work_struct *work = addr;
300
301 switch (state) {
302
303 case ODEBUG_STATE_NOTAVAILABLE:
304 /*
305 * This is not really a fixup. The work struct was
306 * statically initialized. We just make sure that it
307 * is tracked in the object tracker.
308 */
22df02bb 309 if (test_bit(WORK_STRUCT_STATIC_BIT, work_data_bits(work))) {
dc186ad7
TG
310 debug_object_init(work, &work_debug_descr);
311 debug_object_activate(work, &work_debug_descr);
312 return 0;
313 }
314 WARN_ON_ONCE(1);
315 return 0;
316
317 case ODEBUG_STATE_ACTIVE:
318 WARN_ON(1);
319
320 default:
321 return 0;
322 }
323}
324
325/*
326 * fixup_free is called when:
327 * - an active object is freed
328 */
329static int work_fixup_free(void *addr, enum debug_obj_state state)
330{
331 struct work_struct *work = addr;
332
333 switch (state) {
334 case ODEBUG_STATE_ACTIVE:
335 cancel_work_sync(work);
336 debug_object_free(work, &work_debug_descr);
337 return 1;
338 default:
339 return 0;
340 }
341}
342
343static struct debug_obj_descr work_debug_descr = {
344 .name = "work_struct",
345 .fixup_init = work_fixup_init,
346 .fixup_activate = work_fixup_activate,
347 .fixup_free = work_fixup_free,
348};
349
350static inline void debug_work_activate(struct work_struct *work)
351{
352 debug_object_activate(work, &work_debug_descr);
353}
354
355static inline void debug_work_deactivate(struct work_struct *work)
356{
357 debug_object_deactivate(work, &work_debug_descr);
358}
359
360void __init_work(struct work_struct *work, int onstack)
361{
362 if (onstack)
363 debug_object_init_on_stack(work, &work_debug_descr);
364 else
365 debug_object_init(work, &work_debug_descr);
366}
367EXPORT_SYMBOL_GPL(__init_work);
368
369void destroy_work_on_stack(struct work_struct *work)
370{
371 debug_object_free(work, &work_debug_descr);
372}
373EXPORT_SYMBOL_GPL(destroy_work_on_stack);
374
375#else
376static inline void debug_work_activate(struct work_struct *work) { }
377static inline void debug_work_deactivate(struct work_struct *work) { }
378#endif
379
95402b38
GS
380/* Serializes the accesses to the list of workqueues. */
381static DEFINE_SPINLOCK(workqueue_lock);
1da177e4 382static LIST_HEAD(workqueues);
a0a1a5fd 383static bool workqueue_freezing; /* W: have wqs started freezing? */
c34056a3 384
e22bee78
TH
385/*
386 * The almighty global cpu workqueues. nr_running is the only field
387 * which is expected to be used frequently by other cpus via
388 * try_to_wake_up(). Put it in a separate cacheline.
389 */
8b03ae3c 390static DEFINE_PER_CPU(struct global_cwq, global_cwq);
e22bee78 391static DEFINE_PER_CPU_SHARED_ALIGNED(atomic_t, gcwq_nr_running);
8b03ae3c 392
f3421797
TH
393/*
394 * Global cpu workqueue and nr_running counter for unbound gcwq. The
395 * gcwq is always online, has GCWQ_DISASSOCIATED set, and all its
396 * workers have WORKER_UNBOUND set.
397 */
398static struct global_cwq unbound_global_cwq;
399static atomic_t unbound_gcwq_nr_running = ATOMIC_INIT(0); /* always 0 */
400
c34056a3 401static int worker_thread(void *__worker);
1da177e4 402
8b03ae3c
TH
403static struct global_cwq *get_gcwq(unsigned int cpu)
404{
f3421797
TH
405 if (cpu != WORK_CPU_UNBOUND)
406 return &per_cpu(global_cwq, cpu);
407 else
408 return &unbound_global_cwq;
8b03ae3c
TH
409}
410
e22bee78
TH
411static atomic_t *get_gcwq_nr_running(unsigned int cpu)
412{
f3421797
TH
413 if (cpu != WORK_CPU_UNBOUND)
414 return &per_cpu(gcwq_nr_running, cpu);
415 else
416 return &unbound_gcwq_nr_running;
e22bee78
TH
417}
418
1537663f
TH
419static struct cpu_workqueue_struct *get_cwq(unsigned int cpu,
420 struct workqueue_struct *wq)
b1f4ec17 421{
f3421797
TH
422 if (!(wq->flags & WQ_UNBOUND)) {
423 if (likely(cpu < nr_cpu_ids)) {
424#ifdef CONFIG_SMP
425 return per_cpu_ptr(wq->cpu_wq.pcpu, cpu);
bdbc5dd7 426#else
f3421797 427 return wq->cpu_wq.single;
bdbc5dd7 428#endif
f3421797
TH
429 }
430 } else if (likely(cpu == WORK_CPU_UNBOUND))
431 return wq->cpu_wq.single;
432 return NULL;
b1f4ec17
ON
433}
434
73f53c4a
TH
435static unsigned int work_color_to_flags(int color)
436{
437 return color << WORK_STRUCT_COLOR_SHIFT;
438}
439
440static int get_work_color(struct work_struct *work)
441{
442 return (*work_data_bits(work) >> WORK_STRUCT_COLOR_SHIFT) &
443 ((1 << WORK_STRUCT_COLOR_BITS) - 1);
444}
445
446static int work_next_color(int color)
447{
448 return (color + 1) % WORK_NR_COLORS;
449}
450
4594bf15 451/*
7a22ad75
TH
452 * Work data points to the cwq while a work is on queue. Once
453 * execution starts, it points to the cpu the work was last on. This
454 * can be distinguished by comparing the data value against
455 * PAGE_OFFSET.
456 *
457 * set_work_{cwq|cpu}() and clear_work_data() can be used to set the
458 * cwq, cpu or clear work->data. These functions should only be
459 * called while the work is owned - ie. while the PENDING bit is set.
460 *
461 * get_work_[g]cwq() can be used to obtain the gcwq or cwq
462 * corresponding to a work. gcwq is available once the work has been
463 * queued anywhere after initialization. cwq is available only from
464 * queueing until execution starts.
4594bf15 465 */
7a22ad75
TH
466static inline void set_work_data(struct work_struct *work, unsigned long data,
467 unsigned long flags)
365970a1 468{
4594bf15 469 BUG_ON(!work_pending(work));
7a22ad75
TH
470 atomic_long_set(&work->data, data | flags | work_static(work));
471}
365970a1 472
7a22ad75
TH
473static void set_work_cwq(struct work_struct *work,
474 struct cpu_workqueue_struct *cwq,
475 unsigned long extra_flags)
476{
477 set_work_data(work, (unsigned long)cwq,
478 WORK_STRUCT_PENDING | extra_flags);
365970a1
DH
479}
480
7a22ad75
TH
481static void set_work_cpu(struct work_struct *work, unsigned int cpu)
482{
483 set_work_data(work, cpu << WORK_STRUCT_FLAG_BITS, WORK_STRUCT_PENDING);
484}
485
486static void clear_work_data(struct work_struct *work)
487{
488 set_work_data(work, WORK_STRUCT_NO_CPU, 0);
489}
490
491static inline unsigned long get_work_data(struct work_struct *work)
492{
493 return atomic_long_read(&work->data) & WORK_STRUCT_WQ_DATA_MASK;
494}
495
496static struct cpu_workqueue_struct *get_work_cwq(struct work_struct *work)
4d707b9f 497{
7a22ad75
TH
498 unsigned long data = get_work_data(work);
499
500 return data >= PAGE_OFFSET ? (void *)data : NULL;
4d707b9f
ON
501}
502
7a22ad75 503static struct global_cwq *get_work_gcwq(struct work_struct *work)
365970a1 504{
7a22ad75
TH
505 unsigned long data = get_work_data(work);
506 unsigned int cpu;
507
508 if (data >= PAGE_OFFSET)
509 return ((struct cpu_workqueue_struct *)data)->gcwq;
510
511 cpu = data >> WORK_STRUCT_FLAG_BITS;
bdbc5dd7 512 if (cpu == WORK_CPU_NONE)
7a22ad75
TH
513 return NULL;
514
f3421797 515 BUG_ON(cpu >= nr_cpu_ids && cpu != WORK_CPU_UNBOUND);
7a22ad75 516 return get_gcwq(cpu);
365970a1
DH
517}
518
e22bee78
TH
519/*
520 * Policy functions. These define the policies on how the global
521 * worker pool is managed. Unless noted otherwise, these functions
522 * assume that they're being called with gcwq->lock held.
523 */
524
649027d7
TH
525static bool __need_more_worker(struct global_cwq *gcwq)
526{
527 return !atomic_read(get_gcwq_nr_running(gcwq->cpu)) ||
528 gcwq->flags & GCWQ_HIGHPRI_PENDING;
529}
530
e22bee78
TH
531/*
532 * Need to wake up a worker? Called from anything but currently
533 * running workers.
534 */
535static bool need_more_worker(struct global_cwq *gcwq)
536{
649027d7 537 return !list_empty(&gcwq->worklist) && __need_more_worker(gcwq);
e22bee78
TH
538}
539
540/* Can I start working? Called from busy but !running workers. */
541static bool may_start_working(struct global_cwq *gcwq)
542{
543 return gcwq->nr_idle;
544}
545
546/* Do I need to keep working? Called from currently running workers. */
547static bool keep_working(struct global_cwq *gcwq)
548{
549 atomic_t *nr_running = get_gcwq_nr_running(gcwq->cpu);
550
551 return !list_empty(&gcwq->worklist) && atomic_read(nr_running) <= 1;
552}
553
554/* Do we need a new worker? Called from manager. */
555static bool need_to_create_worker(struct global_cwq *gcwq)
556{
557 return need_more_worker(gcwq) && !may_start_working(gcwq);
558}
559
560/* Do I need to be the manager? */
561static bool need_to_manage_workers(struct global_cwq *gcwq)
562{
563 return need_to_create_worker(gcwq) || gcwq->flags & GCWQ_MANAGE_WORKERS;
564}
565
566/* Do we have too many workers and should some go away? */
567static bool too_many_workers(struct global_cwq *gcwq)
568{
569 bool managing = gcwq->flags & GCWQ_MANAGING_WORKERS;
570 int nr_idle = gcwq->nr_idle + managing; /* manager is considered idle */
571 int nr_busy = gcwq->nr_workers - nr_idle;
572
573 return nr_idle > 2 && (nr_idle - 2) * MAX_IDLE_WORKERS_RATIO >= nr_busy;
574}
575
576/*
577 * Wake up functions.
578 */
579
7e11629d
TH
580/* Return the first worker. Safe with preemption disabled */
581static struct worker *first_worker(struct global_cwq *gcwq)
582{
583 if (unlikely(list_empty(&gcwq->idle_list)))
584 return NULL;
585
586 return list_first_entry(&gcwq->idle_list, struct worker, entry);
587}
588
589/**
590 * wake_up_worker - wake up an idle worker
591 * @gcwq: gcwq to wake worker for
592 *
593 * Wake up the first idle worker of @gcwq.
594 *
595 * CONTEXT:
596 * spin_lock_irq(gcwq->lock).
597 */
598static void wake_up_worker(struct global_cwq *gcwq)
599{
600 struct worker *worker = first_worker(gcwq);
601
602 if (likely(worker))
603 wake_up_process(worker->task);
604}
605
d302f017 606/**
e22bee78
TH
607 * wq_worker_waking_up - a worker is waking up
608 * @task: task waking up
609 * @cpu: CPU @task is waking up to
610 *
611 * This function is called during try_to_wake_up() when a worker is
612 * being awoken.
613 *
614 * CONTEXT:
615 * spin_lock_irq(rq->lock)
616 */
617void wq_worker_waking_up(struct task_struct *task, unsigned int cpu)
618{
619 struct worker *worker = kthread_data(task);
620
621 if (likely(!(worker->flags & WORKER_NOT_RUNNING)))
622 atomic_inc(get_gcwq_nr_running(cpu));
623}
624
625/**
626 * wq_worker_sleeping - a worker is going to sleep
627 * @task: task going to sleep
628 * @cpu: CPU in question, must be the current CPU number
629 *
630 * This function is called during schedule() when a busy worker is
631 * going to sleep. Worker on the same cpu can be woken up by
632 * returning pointer to its task.
633 *
634 * CONTEXT:
635 * spin_lock_irq(rq->lock)
636 *
637 * RETURNS:
638 * Worker task on @cpu to wake up, %NULL if none.
639 */
640struct task_struct *wq_worker_sleeping(struct task_struct *task,
641 unsigned int cpu)
642{
643 struct worker *worker = kthread_data(task), *to_wakeup = NULL;
644 struct global_cwq *gcwq = get_gcwq(cpu);
645 atomic_t *nr_running = get_gcwq_nr_running(cpu);
646
647 if (unlikely(worker->flags & WORKER_NOT_RUNNING))
648 return NULL;
649
650 /* this can only happen on the local cpu */
651 BUG_ON(cpu != raw_smp_processor_id());
652
653 /*
654 * The counterpart of the following dec_and_test, implied mb,
655 * worklist not empty test sequence is in insert_work().
656 * Please read comment there.
657 *
658 * NOT_RUNNING is clear. This means that trustee is not in
659 * charge and we're running on the local cpu w/ rq lock held
660 * and preemption disabled, which in turn means that none else
661 * could be manipulating idle_list, so dereferencing idle_list
662 * without gcwq lock is safe.
663 */
664 if (atomic_dec_and_test(nr_running) && !list_empty(&gcwq->worklist))
665 to_wakeup = first_worker(gcwq);
666 return to_wakeup ? to_wakeup->task : NULL;
667}
668
669/**
670 * worker_set_flags - set worker flags and adjust nr_running accordingly
cb444766 671 * @worker: self
d302f017
TH
672 * @flags: flags to set
673 * @wakeup: wakeup an idle worker if necessary
674 *
e22bee78
TH
675 * Set @flags in @worker->flags and adjust nr_running accordingly. If
676 * nr_running becomes zero and @wakeup is %true, an idle worker is
677 * woken up.
d302f017 678 *
cb444766
TH
679 * CONTEXT:
680 * spin_lock_irq(gcwq->lock)
d302f017
TH
681 */
682static inline void worker_set_flags(struct worker *worker, unsigned int flags,
683 bool wakeup)
684{
e22bee78
TH
685 struct global_cwq *gcwq = worker->gcwq;
686
cb444766
TH
687 WARN_ON_ONCE(worker->task != current);
688
e22bee78
TH
689 /*
690 * If transitioning into NOT_RUNNING, adjust nr_running and
691 * wake up an idle worker as necessary if requested by
692 * @wakeup.
693 */
694 if ((flags & WORKER_NOT_RUNNING) &&
695 !(worker->flags & WORKER_NOT_RUNNING)) {
696 atomic_t *nr_running = get_gcwq_nr_running(gcwq->cpu);
697
698 if (wakeup) {
699 if (atomic_dec_and_test(nr_running) &&
700 !list_empty(&gcwq->worklist))
701 wake_up_worker(gcwq);
702 } else
703 atomic_dec(nr_running);
704 }
705
d302f017
TH
706 worker->flags |= flags;
707}
708
709/**
e22bee78 710 * worker_clr_flags - clear worker flags and adjust nr_running accordingly
cb444766 711 * @worker: self
d302f017
TH
712 * @flags: flags to clear
713 *
e22bee78 714 * Clear @flags in @worker->flags and adjust nr_running accordingly.
d302f017 715 *
cb444766
TH
716 * CONTEXT:
717 * spin_lock_irq(gcwq->lock)
d302f017
TH
718 */
719static inline void worker_clr_flags(struct worker *worker, unsigned int flags)
720{
e22bee78
TH
721 struct global_cwq *gcwq = worker->gcwq;
722 unsigned int oflags = worker->flags;
723
cb444766
TH
724 WARN_ON_ONCE(worker->task != current);
725
d302f017 726 worker->flags &= ~flags;
e22bee78
TH
727
728 /* if transitioning out of NOT_RUNNING, increment nr_running */
729 if ((flags & WORKER_NOT_RUNNING) && (oflags & WORKER_NOT_RUNNING))
730 if (!(worker->flags & WORKER_NOT_RUNNING))
731 atomic_inc(get_gcwq_nr_running(gcwq->cpu));
d302f017
TH
732}
733
c8e55f36
TH
734/**
735 * busy_worker_head - return the busy hash head for a work
736 * @gcwq: gcwq of interest
737 * @work: work to be hashed
738 *
739 * Return hash head of @gcwq for @work.
740 *
741 * CONTEXT:
742 * spin_lock_irq(gcwq->lock).
743 *
744 * RETURNS:
745 * Pointer to the hash head.
746 */
747static struct hlist_head *busy_worker_head(struct global_cwq *gcwq,
748 struct work_struct *work)
749{
750 const int base_shift = ilog2(sizeof(struct work_struct));
751 unsigned long v = (unsigned long)work;
752
753 /* simple shift and fold hash, do we need something better? */
754 v >>= base_shift;
755 v += v >> BUSY_WORKER_HASH_ORDER;
756 v &= BUSY_WORKER_HASH_MASK;
757
758 return &gcwq->busy_hash[v];
759}
760
8cca0eea
TH
761/**
762 * __find_worker_executing_work - find worker which is executing a work
763 * @gcwq: gcwq of interest
764 * @bwh: hash head as returned by busy_worker_head()
765 * @work: work to find worker for
766 *
767 * Find a worker which is executing @work on @gcwq. @bwh should be
768 * the hash head obtained by calling busy_worker_head() with the same
769 * work.
770 *
771 * CONTEXT:
772 * spin_lock_irq(gcwq->lock).
773 *
774 * RETURNS:
775 * Pointer to worker which is executing @work if found, NULL
776 * otherwise.
777 */
778static struct worker *__find_worker_executing_work(struct global_cwq *gcwq,
779 struct hlist_head *bwh,
780 struct work_struct *work)
781{
782 struct worker *worker;
783 struct hlist_node *tmp;
784
785 hlist_for_each_entry(worker, tmp, bwh, hentry)
786 if (worker->current_work == work)
787 return worker;
788 return NULL;
789}
790
791/**
792 * find_worker_executing_work - find worker which is executing a work
793 * @gcwq: gcwq of interest
794 * @work: work to find worker for
795 *
796 * Find a worker which is executing @work on @gcwq. This function is
797 * identical to __find_worker_executing_work() except that this
798 * function calculates @bwh itself.
799 *
800 * CONTEXT:
801 * spin_lock_irq(gcwq->lock).
802 *
803 * RETURNS:
804 * Pointer to worker which is executing @work if found, NULL
805 * otherwise.
806 */
807static struct worker *find_worker_executing_work(struct global_cwq *gcwq,
808 struct work_struct *work)
809{
810 return __find_worker_executing_work(gcwq, busy_worker_head(gcwq, work),
811 work);
812}
813
649027d7
TH
814/**
815 * gcwq_determine_ins_pos - find insertion position
816 * @gcwq: gcwq of interest
817 * @cwq: cwq a work is being queued for
818 *
819 * A work for @cwq is about to be queued on @gcwq, determine insertion
820 * position for the work. If @cwq is for HIGHPRI wq, the work is
821 * queued at the head of the queue but in FIFO order with respect to
822 * other HIGHPRI works; otherwise, at the end of the queue. This
823 * function also sets GCWQ_HIGHPRI_PENDING flag to hint @gcwq that
824 * there are HIGHPRI works pending.
825 *
826 * CONTEXT:
827 * spin_lock_irq(gcwq->lock).
828 *
829 * RETURNS:
830 * Pointer to inserstion position.
831 */
832static inline struct list_head *gcwq_determine_ins_pos(struct global_cwq *gcwq,
833 struct cpu_workqueue_struct *cwq)
834{
835 struct work_struct *twork;
836
837 if (likely(!(cwq->wq->flags & WQ_HIGHPRI)))
838 return &gcwq->worklist;
839
840 list_for_each_entry(twork, &gcwq->worklist, entry) {
841 struct cpu_workqueue_struct *tcwq = get_work_cwq(twork);
842
843 if (!(tcwq->wq->flags & WQ_HIGHPRI))
844 break;
845 }
846
847 gcwq->flags |= GCWQ_HIGHPRI_PENDING;
848 return &twork->entry;
849}
850
4690c4ab 851/**
7e11629d 852 * insert_work - insert a work into gcwq
4690c4ab
TH
853 * @cwq: cwq @work belongs to
854 * @work: work to insert
855 * @head: insertion point
856 * @extra_flags: extra WORK_STRUCT_* flags to set
857 *
7e11629d
TH
858 * Insert @work which belongs to @cwq into @gcwq after @head.
859 * @extra_flags is or'd to work_struct flags.
4690c4ab
TH
860 *
861 * CONTEXT:
8b03ae3c 862 * spin_lock_irq(gcwq->lock).
4690c4ab 863 */
b89deed3 864static void insert_work(struct cpu_workqueue_struct *cwq,
4690c4ab
TH
865 struct work_struct *work, struct list_head *head,
866 unsigned int extra_flags)
b89deed3 867{
e22bee78
TH
868 struct global_cwq *gcwq = cwq->gcwq;
869
4690c4ab 870 /* we own @work, set data and link */
7a22ad75 871 set_work_cwq(work, cwq, extra_flags);
4690c4ab 872
6e84d644
ON
873 /*
874 * Ensure that we get the right work->data if we see the
875 * result of list_add() below, see try_to_grab_pending().
876 */
877 smp_wmb();
4690c4ab 878
1a4d9b0a 879 list_add_tail(&work->entry, head);
e22bee78
TH
880
881 /*
882 * Ensure either worker_sched_deactivated() sees the above
883 * list_add_tail() or we see zero nr_running to avoid workers
884 * lying around lazily while there are works to be processed.
885 */
886 smp_mb();
887
649027d7 888 if (__need_more_worker(gcwq))
e22bee78 889 wake_up_worker(gcwq);
b89deed3
ON
890}
891
502ca9d8
TH
892/**
893 * cwq_unbind_single_cpu - unbind cwq from single cpu workqueue processing
894 * @cwq: cwq to unbind
895 *
896 * Try to unbind @cwq from single cpu workqueue processing. If
897 * @cwq->wq is frozen, unbind is delayed till the workqueue is thawed.
898 *
899 * CONTEXT:
900 * spin_lock_irq(gcwq->lock).
901 */
902static void cwq_unbind_single_cpu(struct cpu_workqueue_struct *cwq)
903{
904 struct workqueue_struct *wq = cwq->wq;
905 struct global_cwq *gcwq = cwq->gcwq;
906
907 BUG_ON(wq->single_cpu != gcwq->cpu);
908 /*
909 * Unbind from workqueue if @cwq is not frozen. If frozen,
910 * thaw_workqueues() will either restart processing on this
911 * cpu or unbind if empty. This keeps works queued while
912 * frozen fully ordered and flushable.
913 */
914 if (likely(!(gcwq->flags & GCWQ_FREEZING))) {
915 smp_wmb(); /* paired with cmpxchg() in __queue_work() */
bdbc5dd7 916 wq->single_cpu = WORK_CPU_NONE;
502ca9d8
TH
917 }
918}
919
4690c4ab 920static void __queue_work(unsigned int cpu, struct workqueue_struct *wq,
1da177e4
LT
921 struct work_struct *work)
922{
502ca9d8
TH
923 struct global_cwq *gcwq;
924 struct cpu_workqueue_struct *cwq;
1e19ffc6 925 struct list_head *worklist;
1da177e4 926 unsigned long flags;
502ca9d8 927 bool arbitrate;
1da177e4 928
dc186ad7 929 debug_work_activate(work);
1e19ffc6 930
f3421797
TH
931 if (unlikely(cpu == WORK_CPU_UNBOUND))
932 cpu = raw_smp_processor_id();
933
18aa9eff
TH
934 /*
935 * Determine gcwq to use. SINGLE_CPU is inherently
936 * NON_REENTRANT, so test it first.
937 */
f3421797 938 if (!(wq->flags & (WQ_SINGLE_CPU | WQ_UNBOUND))) {
18aa9eff
TH
939 struct global_cwq *last_gcwq;
940
941 /*
942 * It's multi cpu. If @wq is non-reentrant and @work
943 * was previously on a different cpu, it might still
944 * be running there, in which case the work needs to
945 * be queued on that cpu to guarantee non-reentrance.
946 */
502ca9d8 947 gcwq = get_gcwq(cpu);
18aa9eff
TH
948 if (wq->flags & WQ_NON_REENTRANT &&
949 (last_gcwq = get_work_gcwq(work)) && last_gcwq != gcwq) {
950 struct worker *worker;
951
952 spin_lock_irqsave(&last_gcwq->lock, flags);
953
954 worker = find_worker_executing_work(last_gcwq, work);
955
956 if (worker && worker->current_cwq->wq == wq)
957 gcwq = last_gcwq;
958 else {
959 /* meh... not running there, queue here */
960 spin_unlock_irqrestore(&last_gcwq->lock, flags);
961 spin_lock_irqsave(&gcwq->lock, flags);
962 }
963 } else
964 spin_lock_irqsave(&gcwq->lock, flags);
f3421797 965 } else if (!(wq->flags & WQ_UNBOUND)) {
502ca9d8
TH
966 unsigned int req_cpu = cpu;
967
968 /*
969 * It's a bit more complex for single cpu workqueues.
970 * We first need to determine which cpu is going to be
971 * used. If no cpu is currently serving this
972 * workqueue, arbitrate using atomic accesses to
973 * wq->single_cpu; otherwise, use the current one.
974 */
975 retry:
976 cpu = wq->single_cpu;
bdbc5dd7 977 arbitrate = cpu == WORK_CPU_NONE;
502ca9d8
TH
978 if (arbitrate)
979 cpu = req_cpu;
980
981 gcwq = get_gcwq(cpu);
982 spin_lock_irqsave(&gcwq->lock, flags);
983
984 /*
985 * The following cmpxchg() is a full barrier paired
986 * with smp_wmb() in cwq_unbind_single_cpu() and
987 * guarantees that all changes to wq->st_* fields are
988 * visible on the new cpu after this point.
989 */
990 if (arbitrate)
bdbc5dd7 991 cmpxchg(&wq->single_cpu, WORK_CPU_NONE, cpu);
502ca9d8
TH
992
993 if (unlikely(wq->single_cpu != cpu)) {
994 spin_unlock_irqrestore(&gcwq->lock, flags);
995 goto retry;
996 }
f3421797
TH
997 } else {
998 gcwq = get_gcwq(WORK_CPU_UNBOUND);
999 spin_lock_irqsave(&gcwq->lock, flags);
502ca9d8
TH
1000 }
1001
1002 /* gcwq determined, get cwq and queue */
1003 cwq = get_cwq(gcwq->cpu, wq);
1004
4690c4ab 1005 BUG_ON(!list_empty(&work->entry));
1e19ffc6 1006
73f53c4a 1007 cwq->nr_in_flight[cwq->work_color]++;
1e19ffc6
TH
1008
1009 if (likely(cwq->nr_active < cwq->max_active)) {
1010 cwq->nr_active++;
649027d7 1011 worklist = gcwq_determine_ins_pos(gcwq, cwq);
1e19ffc6
TH
1012 } else
1013 worklist = &cwq->delayed_works;
1014
1015 insert_work(cwq, work, worklist, work_color_to_flags(cwq->work_color));
1016
8b03ae3c 1017 spin_unlock_irqrestore(&gcwq->lock, flags);
1da177e4
LT
1018}
1019
0fcb78c2
REB
1020/**
1021 * queue_work - queue work on a workqueue
1022 * @wq: workqueue to use
1023 * @work: work to queue
1024 *
057647fc 1025 * Returns 0 if @work was already on a queue, non-zero otherwise.
1da177e4 1026 *
00dfcaf7
ON
1027 * We queue the work to the CPU on which it was submitted, but if the CPU dies
1028 * it can be processed by another CPU.
1da177e4 1029 */
7ad5b3a5 1030int queue_work(struct workqueue_struct *wq, struct work_struct *work)
1da177e4 1031{
ef1ca236
ON
1032 int ret;
1033
1034 ret = queue_work_on(get_cpu(), wq, work);
1035 put_cpu();
1036
1da177e4
LT
1037 return ret;
1038}
ae90dd5d 1039EXPORT_SYMBOL_GPL(queue_work);
1da177e4 1040
c1a220e7
ZR
1041/**
1042 * queue_work_on - queue work on specific cpu
1043 * @cpu: CPU number to execute work on
1044 * @wq: workqueue to use
1045 * @work: work to queue
1046 *
1047 * Returns 0 if @work was already on a queue, non-zero otherwise.
1048 *
1049 * We queue the work to a specific CPU, the caller must ensure it
1050 * can't go away.
1051 */
1052int
1053queue_work_on(int cpu, struct workqueue_struct *wq, struct work_struct *work)
1054{
1055 int ret = 0;
1056
22df02bb 1057 if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))) {
4690c4ab 1058 __queue_work(cpu, wq, work);
c1a220e7
ZR
1059 ret = 1;
1060 }
1061 return ret;
1062}
1063EXPORT_SYMBOL_GPL(queue_work_on);
1064
6d141c3f 1065static void delayed_work_timer_fn(unsigned long __data)
1da177e4 1066{
52bad64d 1067 struct delayed_work *dwork = (struct delayed_work *)__data;
7a22ad75 1068 struct cpu_workqueue_struct *cwq = get_work_cwq(&dwork->work);
1da177e4 1069
4690c4ab 1070 __queue_work(smp_processor_id(), cwq->wq, &dwork->work);
1da177e4
LT
1071}
1072
0fcb78c2
REB
1073/**
1074 * queue_delayed_work - queue work on a workqueue after delay
1075 * @wq: workqueue to use
af9997e4 1076 * @dwork: delayable work to queue
0fcb78c2
REB
1077 * @delay: number of jiffies to wait before queueing
1078 *
057647fc 1079 * Returns 0 if @work was already on a queue, non-zero otherwise.
0fcb78c2 1080 */
7ad5b3a5 1081int queue_delayed_work(struct workqueue_struct *wq,
52bad64d 1082 struct delayed_work *dwork, unsigned long delay)
1da177e4 1083{
52bad64d 1084 if (delay == 0)
63bc0362 1085 return queue_work(wq, &dwork->work);
1da177e4 1086
63bc0362 1087 return queue_delayed_work_on(-1, wq, dwork, delay);
1da177e4 1088}
ae90dd5d 1089EXPORT_SYMBOL_GPL(queue_delayed_work);
1da177e4 1090
0fcb78c2
REB
1091/**
1092 * queue_delayed_work_on - queue work on specific CPU after delay
1093 * @cpu: CPU number to execute work on
1094 * @wq: workqueue to use
af9997e4 1095 * @dwork: work to queue
0fcb78c2
REB
1096 * @delay: number of jiffies to wait before queueing
1097 *
057647fc 1098 * Returns 0 if @work was already on a queue, non-zero otherwise.
0fcb78c2 1099 */
7a6bc1cd 1100int queue_delayed_work_on(int cpu, struct workqueue_struct *wq,
52bad64d 1101 struct delayed_work *dwork, unsigned long delay)
7a6bc1cd
VP
1102{
1103 int ret = 0;
52bad64d
DH
1104 struct timer_list *timer = &dwork->timer;
1105 struct work_struct *work = &dwork->work;
7a6bc1cd 1106
22df02bb 1107 if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))) {
7a22ad75
TH
1108 struct global_cwq *gcwq = get_work_gcwq(work);
1109 unsigned int lcpu = gcwq ? gcwq->cpu : raw_smp_processor_id();
1110
7a6bc1cd
VP
1111 BUG_ON(timer_pending(timer));
1112 BUG_ON(!list_empty(&work->entry));
1113
8a3e77cc 1114 timer_stats_timer_set_start_info(&dwork->timer);
7a22ad75
TH
1115 /*
1116 * This stores cwq for the moment, for the timer_fn.
1117 * Note that the work's gcwq is preserved to allow
1118 * reentrance detection for delayed works.
1119 */
1120 set_work_cwq(work, get_cwq(lcpu, wq), 0);
7a6bc1cd 1121 timer->expires = jiffies + delay;
52bad64d 1122 timer->data = (unsigned long)dwork;
7a6bc1cd 1123 timer->function = delayed_work_timer_fn;
63bc0362
ON
1124
1125 if (unlikely(cpu >= 0))
1126 add_timer_on(timer, cpu);
1127 else
1128 add_timer(timer);
7a6bc1cd
VP
1129 ret = 1;
1130 }
1131 return ret;
1132}
ae90dd5d 1133EXPORT_SYMBOL_GPL(queue_delayed_work_on);
1da177e4 1134
c8e55f36
TH
1135/**
1136 * worker_enter_idle - enter idle state
1137 * @worker: worker which is entering idle state
1138 *
1139 * @worker is entering idle state. Update stats and idle timer if
1140 * necessary.
1141 *
1142 * LOCKING:
1143 * spin_lock_irq(gcwq->lock).
1144 */
1145static void worker_enter_idle(struct worker *worker)
1146{
1147 struct global_cwq *gcwq = worker->gcwq;
1148
1149 BUG_ON(worker->flags & WORKER_IDLE);
1150 BUG_ON(!list_empty(&worker->entry) &&
1151 (worker->hentry.next || worker->hentry.pprev));
1152
cb444766
TH
1153 /* can't use worker_set_flags(), also called from start_worker() */
1154 worker->flags |= WORKER_IDLE;
c8e55f36 1155 gcwq->nr_idle++;
e22bee78 1156 worker->last_active = jiffies;
c8e55f36
TH
1157
1158 /* idle_list is LIFO */
1159 list_add(&worker->entry, &gcwq->idle_list);
db7bccf4 1160
e22bee78
TH
1161 if (likely(!(worker->flags & WORKER_ROGUE))) {
1162 if (too_many_workers(gcwq) && !timer_pending(&gcwq->idle_timer))
1163 mod_timer(&gcwq->idle_timer,
1164 jiffies + IDLE_WORKER_TIMEOUT);
1165 } else
db7bccf4 1166 wake_up_all(&gcwq->trustee_wait);
cb444766
TH
1167
1168 /* sanity check nr_running */
1169 WARN_ON_ONCE(gcwq->nr_workers == gcwq->nr_idle &&
1170 atomic_read(get_gcwq_nr_running(gcwq->cpu)));
c8e55f36
TH
1171}
1172
1173/**
1174 * worker_leave_idle - leave idle state
1175 * @worker: worker which is leaving idle state
1176 *
1177 * @worker is leaving idle state. Update stats.
1178 *
1179 * LOCKING:
1180 * spin_lock_irq(gcwq->lock).
1181 */
1182static void worker_leave_idle(struct worker *worker)
1183{
1184 struct global_cwq *gcwq = worker->gcwq;
1185
1186 BUG_ON(!(worker->flags & WORKER_IDLE));
d302f017 1187 worker_clr_flags(worker, WORKER_IDLE);
c8e55f36
TH
1188 gcwq->nr_idle--;
1189 list_del_init(&worker->entry);
1190}
1191
e22bee78
TH
1192/**
1193 * worker_maybe_bind_and_lock - bind worker to its cpu if possible and lock gcwq
1194 * @worker: self
1195 *
1196 * Works which are scheduled while the cpu is online must at least be
1197 * scheduled to a worker which is bound to the cpu so that if they are
1198 * flushed from cpu callbacks while cpu is going down, they are
1199 * guaranteed to execute on the cpu.
1200 *
1201 * This function is to be used by rogue workers and rescuers to bind
1202 * themselves to the target cpu and may race with cpu going down or
1203 * coming online. kthread_bind() can't be used because it may put the
1204 * worker to already dead cpu and set_cpus_allowed_ptr() can't be used
1205 * verbatim as it's best effort and blocking and gcwq may be
1206 * [dis]associated in the meantime.
1207 *
1208 * This function tries set_cpus_allowed() and locks gcwq and verifies
1209 * the binding against GCWQ_DISASSOCIATED which is set during
1210 * CPU_DYING and cleared during CPU_ONLINE, so if the worker enters
1211 * idle state or fetches works without dropping lock, it can guarantee
1212 * the scheduling requirement described in the first paragraph.
1213 *
1214 * CONTEXT:
1215 * Might sleep. Called without any lock but returns with gcwq->lock
1216 * held.
1217 *
1218 * RETURNS:
1219 * %true if the associated gcwq is online (@worker is successfully
1220 * bound), %false if offline.
1221 */
1222static bool worker_maybe_bind_and_lock(struct worker *worker)
1223{
1224 struct global_cwq *gcwq = worker->gcwq;
1225 struct task_struct *task = worker->task;
1226
1227 while (true) {
1228 /*
1229 * The following call may fail, succeed or succeed
1230 * without actually migrating the task to the cpu if
1231 * it races with cpu hotunplug operation. Verify
1232 * against GCWQ_DISASSOCIATED.
1233 */
f3421797
TH
1234 if (!(gcwq->flags & GCWQ_DISASSOCIATED))
1235 set_cpus_allowed_ptr(task, get_cpu_mask(gcwq->cpu));
e22bee78
TH
1236
1237 spin_lock_irq(&gcwq->lock);
1238 if (gcwq->flags & GCWQ_DISASSOCIATED)
1239 return false;
1240 if (task_cpu(task) == gcwq->cpu &&
1241 cpumask_equal(&current->cpus_allowed,
1242 get_cpu_mask(gcwq->cpu)))
1243 return true;
1244 spin_unlock_irq(&gcwq->lock);
1245
1246 /* CPU has come up inbetween, retry migration */
1247 cpu_relax();
1248 }
1249}
1250
1251/*
1252 * Function for worker->rebind_work used to rebind rogue busy workers
1253 * to the associated cpu which is coming back online. This is
1254 * scheduled by cpu up but can race with other cpu hotplug operations
1255 * and may be executed twice without intervening cpu down.
1256 */
1257static void worker_rebind_fn(struct work_struct *work)
1258{
1259 struct worker *worker = container_of(work, struct worker, rebind_work);
1260 struct global_cwq *gcwq = worker->gcwq;
1261
1262 if (worker_maybe_bind_and_lock(worker))
1263 worker_clr_flags(worker, WORKER_REBIND);
1264
1265 spin_unlock_irq(&gcwq->lock);
1266}
1267
c34056a3
TH
1268static struct worker *alloc_worker(void)
1269{
1270 struct worker *worker;
1271
1272 worker = kzalloc(sizeof(*worker), GFP_KERNEL);
c8e55f36
TH
1273 if (worker) {
1274 INIT_LIST_HEAD(&worker->entry);
affee4b2 1275 INIT_LIST_HEAD(&worker->scheduled);
e22bee78
TH
1276 INIT_WORK(&worker->rebind_work, worker_rebind_fn);
1277 /* on creation a worker is in !idle && prep state */
1278 worker->flags = WORKER_PREP;
c8e55f36 1279 }
c34056a3
TH
1280 return worker;
1281}
1282
1283/**
1284 * create_worker - create a new workqueue worker
7e11629d 1285 * @gcwq: gcwq the new worker will belong to
c34056a3
TH
1286 * @bind: whether to set affinity to @cpu or not
1287 *
7e11629d 1288 * Create a new worker which is bound to @gcwq. The returned worker
c34056a3
TH
1289 * can be started by calling start_worker() or destroyed using
1290 * destroy_worker().
1291 *
1292 * CONTEXT:
1293 * Might sleep. Does GFP_KERNEL allocations.
1294 *
1295 * RETURNS:
1296 * Pointer to the newly created worker.
1297 */
7e11629d 1298static struct worker *create_worker(struct global_cwq *gcwq, bool bind)
c34056a3 1299{
f3421797 1300 bool on_unbound_cpu = gcwq->cpu == WORK_CPU_UNBOUND;
c34056a3 1301 struct worker *worker = NULL;
f3421797 1302 int id = -1;
c34056a3 1303
8b03ae3c
TH
1304 spin_lock_irq(&gcwq->lock);
1305 while (ida_get_new(&gcwq->worker_ida, &id)) {
1306 spin_unlock_irq(&gcwq->lock);
1307 if (!ida_pre_get(&gcwq->worker_ida, GFP_KERNEL))
c34056a3 1308 goto fail;
8b03ae3c 1309 spin_lock_irq(&gcwq->lock);
c34056a3 1310 }
8b03ae3c 1311 spin_unlock_irq(&gcwq->lock);
c34056a3
TH
1312
1313 worker = alloc_worker();
1314 if (!worker)
1315 goto fail;
1316
8b03ae3c 1317 worker->gcwq = gcwq;
c34056a3
TH
1318 worker->id = id;
1319
f3421797
TH
1320 if (!on_unbound_cpu)
1321 worker->task = kthread_create(worker_thread, worker,
1322 "kworker/%u:%d", gcwq->cpu, id);
1323 else
1324 worker->task = kthread_create(worker_thread, worker,
1325 "kworker/u:%d", id);
c34056a3
TH
1326 if (IS_ERR(worker->task))
1327 goto fail;
1328
db7bccf4
TH
1329 /*
1330 * A rogue worker will become a regular one if CPU comes
1331 * online later on. Make sure every worker has
1332 * PF_THREAD_BOUND set.
1333 */
f3421797 1334 if (bind && !on_unbound_cpu)
8b03ae3c 1335 kthread_bind(worker->task, gcwq->cpu);
f3421797 1336 else {
db7bccf4 1337 worker->task->flags |= PF_THREAD_BOUND;
f3421797
TH
1338 if (on_unbound_cpu)
1339 worker->flags |= WORKER_UNBOUND;
1340 }
c34056a3
TH
1341
1342 return worker;
1343fail:
1344 if (id >= 0) {
8b03ae3c
TH
1345 spin_lock_irq(&gcwq->lock);
1346 ida_remove(&gcwq->worker_ida, id);
1347 spin_unlock_irq(&gcwq->lock);
c34056a3
TH
1348 }
1349 kfree(worker);
1350 return NULL;
1351}
1352
1353/**
1354 * start_worker - start a newly created worker
1355 * @worker: worker to start
1356 *
c8e55f36 1357 * Make the gcwq aware of @worker and start it.
c34056a3
TH
1358 *
1359 * CONTEXT:
8b03ae3c 1360 * spin_lock_irq(gcwq->lock).
c34056a3
TH
1361 */
1362static void start_worker(struct worker *worker)
1363{
cb444766 1364 worker->flags |= WORKER_STARTED;
c8e55f36
TH
1365 worker->gcwq->nr_workers++;
1366 worker_enter_idle(worker);
c34056a3
TH
1367 wake_up_process(worker->task);
1368}
1369
1370/**
1371 * destroy_worker - destroy a workqueue worker
1372 * @worker: worker to be destroyed
1373 *
c8e55f36
TH
1374 * Destroy @worker and adjust @gcwq stats accordingly.
1375 *
1376 * CONTEXT:
1377 * spin_lock_irq(gcwq->lock) which is released and regrabbed.
c34056a3
TH
1378 */
1379static void destroy_worker(struct worker *worker)
1380{
8b03ae3c 1381 struct global_cwq *gcwq = worker->gcwq;
c34056a3
TH
1382 int id = worker->id;
1383
1384 /* sanity check frenzy */
1385 BUG_ON(worker->current_work);
affee4b2 1386 BUG_ON(!list_empty(&worker->scheduled));
c34056a3 1387
c8e55f36
TH
1388 if (worker->flags & WORKER_STARTED)
1389 gcwq->nr_workers--;
1390 if (worker->flags & WORKER_IDLE)
1391 gcwq->nr_idle--;
1392
1393 list_del_init(&worker->entry);
cb444766 1394 worker->flags |= WORKER_DIE;
c8e55f36
TH
1395
1396 spin_unlock_irq(&gcwq->lock);
1397
c34056a3
TH
1398 kthread_stop(worker->task);
1399 kfree(worker);
1400
8b03ae3c
TH
1401 spin_lock_irq(&gcwq->lock);
1402 ida_remove(&gcwq->worker_ida, id);
c34056a3
TH
1403}
1404
e22bee78
TH
1405static void idle_worker_timeout(unsigned long __gcwq)
1406{
1407 struct global_cwq *gcwq = (void *)__gcwq;
1408
1409 spin_lock_irq(&gcwq->lock);
1410
1411 if (too_many_workers(gcwq)) {
1412 struct worker *worker;
1413 unsigned long expires;
1414
1415 /* idle_list is kept in LIFO order, check the last one */
1416 worker = list_entry(gcwq->idle_list.prev, struct worker, entry);
1417 expires = worker->last_active + IDLE_WORKER_TIMEOUT;
1418
1419 if (time_before(jiffies, expires))
1420 mod_timer(&gcwq->idle_timer, expires);
1421 else {
1422 /* it's been idle for too long, wake up manager */
1423 gcwq->flags |= GCWQ_MANAGE_WORKERS;
1424 wake_up_worker(gcwq);
1425 }
1426 }
1427
1428 spin_unlock_irq(&gcwq->lock);
1429}
1430
1431static bool send_mayday(struct work_struct *work)
1432{
1433 struct cpu_workqueue_struct *cwq = get_work_cwq(work);
1434 struct workqueue_struct *wq = cwq->wq;
f3421797 1435 unsigned int cpu;
e22bee78
TH
1436
1437 if (!(wq->flags & WQ_RESCUER))
1438 return false;
1439
1440 /* mayday mayday mayday */
f3421797
TH
1441 cpu = cwq->gcwq->cpu;
1442 /* WORK_CPU_UNBOUND can't be set in cpumask, use cpu 0 instead */
1443 if (cpu == WORK_CPU_UNBOUND)
1444 cpu = 0;
1445 if (!cpumask_test_and_set_cpu(cpu, wq->mayday_mask))
e22bee78
TH
1446 wake_up_process(wq->rescuer->task);
1447 return true;
1448}
1449
1450static void gcwq_mayday_timeout(unsigned long __gcwq)
1451{
1452 struct global_cwq *gcwq = (void *)__gcwq;
1453 struct work_struct *work;
1454
1455 spin_lock_irq(&gcwq->lock);
1456
1457 if (need_to_create_worker(gcwq)) {
1458 /*
1459 * We've been trying to create a new worker but
1460 * haven't been successful. We might be hitting an
1461 * allocation deadlock. Send distress signals to
1462 * rescuers.
1463 */
1464 list_for_each_entry(work, &gcwq->worklist, entry)
1465 send_mayday(work);
1466 }
1467
1468 spin_unlock_irq(&gcwq->lock);
1469
1470 mod_timer(&gcwq->mayday_timer, jiffies + MAYDAY_INTERVAL);
1471}
1472
1473/**
1474 * maybe_create_worker - create a new worker if necessary
1475 * @gcwq: gcwq to create a new worker for
1476 *
1477 * Create a new worker for @gcwq if necessary. @gcwq is guaranteed to
1478 * have at least one idle worker on return from this function. If
1479 * creating a new worker takes longer than MAYDAY_INTERVAL, mayday is
1480 * sent to all rescuers with works scheduled on @gcwq to resolve
1481 * possible allocation deadlock.
1482 *
1483 * On return, need_to_create_worker() is guaranteed to be false and
1484 * may_start_working() true.
1485 *
1486 * LOCKING:
1487 * spin_lock_irq(gcwq->lock) which may be released and regrabbed
1488 * multiple times. Does GFP_KERNEL allocations. Called only from
1489 * manager.
1490 *
1491 * RETURNS:
1492 * false if no action was taken and gcwq->lock stayed locked, true
1493 * otherwise.
1494 */
1495static bool maybe_create_worker(struct global_cwq *gcwq)
1496{
1497 if (!need_to_create_worker(gcwq))
1498 return false;
1499restart:
1500 /* if we don't make progress in MAYDAY_INITIAL_TIMEOUT, call for help */
1501 mod_timer(&gcwq->mayday_timer, jiffies + MAYDAY_INITIAL_TIMEOUT);
1502
1503 while (true) {
1504 struct worker *worker;
1505
1506 spin_unlock_irq(&gcwq->lock);
1507
1508 worker = create_worker(gcwq, true);
1509 if (worker) {
1510 del_timer_sync(&gcwq->mayday_timer);
1511 spin_lock_irq(&gcwq->lock);
1512 start_worker(worker);
1513 BUG_ON(need_to_create_worker(gcwq));
1514 return true;
1515 }
1516
1517 if (!need_to_create_worker(gcwq))
1518 break;
1519
1520 spin_unlock_irq(&gcwq->lock);
1521 __set_current_state(TASK_INTERRUPTIBLE);
1522 schedule_timeout(CREATE_COOLDOWN);
1523 spin_lock_irq(&gcwq->lock);
1524 if (!need_to_create_worker(gcwq))
1525 break;
1526 }
1527
1528 spin_unlock_irq(&gcwq->lock);
1529 del_timer_sync(&gcwq->mayday_timer);
1530 spin_lock_irq(&gcwq->lock);
1531 if (need_to_create_worker(gcwq))
1532 goto restart;
1533 return true;
1534}
1535
1536/**
1537 * maybe_destroy_worker - destroy workers which have been idle for a while
1538 * @gcwq: gcwq to destroy workers for
1539 *
1540 * Destroy @gcwq workers which have been idle for longer than
1541 * IDLE_WORKER_TIMEOUT.
1542 *
1543 * LOCKING:
1544 * spin_lock_irq(gcwq->lock) which may be released and regrabbed
1545 * multiple times. Called only from manager.
1546 *
1547 * RETURNS:
1548 * false if no action was taken and gcwq->lock stayed locked, true
1549 * otherwise.
1550 */
1551static bool maybe_destroy_workers(struct global_cwq *gcwq)
1552{
1553 bool ret = false;
1554
1555 while (too_many_workers(gcwq)) {
1556 struct worker *worker;
1557 unsigned long expires;
1558
1559 worker = list_entry(gcwq->idle_list.prev, struct worker, entry);
1560 expires = worker->last_active + IDLE_WORKER_TIMEOUT;
1561
1562 if (time_before(jiffies, expires)) {
1563 mod_timer(&gcwq->idle_timer, expires);
1564 break;
1565 }
1566
1567 destroy_worker(worker);
1568 ret = true;
1569 }
1570
1571 return ret;
1572}
1573
1574/**
1575 * manage_workers - manage worker pool
1576 * @worker: self
1577 *
1578 * Assume the manager role and manage gcwq worker pool @worker belongs
1579 * to. At any given time, there can be only zero or one manager per
1580 * gcwq. The exclusion is handled automatically by this function.
1581 *
1582 * The caller can safely start processing works on false return. On
1583 * true return, it's guaranteed that need_to_create_worker() is false
1584 * and may_start_working() is true.
1585 *
1586 * CONTEXT:
1587 * spin_lock_irq(gcwq->lock) which may be released and regrabbed
1588 * multiple times. Does GFP_KERNEL allocations.
1589 *
1590 * RETURNS:
1591 * false if no action was taken and gcwq->lock stayed locked, true if
1592 * some action was taken.
1593 */
1594static bool manage_workers(struct worker *worker)
1595{
1596 struct global_cwq *gcwq = worker->gcwq;
1597 bool ret = false;
1598
1599 if (gcwq->flags & GCWQ_MANAGING_WORKERS)
1600 return ret;
1601
1602 gcwq->flags &= ~GCWQ_MANAGE_WORKERS;
1603 gcwq->flags |= GCWQ_MANAGING_WORKERS;
1604
1605 /*
1606 * Destroy and then create so that may_start_working() is true
1607 * on return.
1608 */
1609 ret |= maybe_destroy_workers(gcwq);
1610 ret |= maybe_create_worker(gcwq);
1611
1612 gcwq->flags &= ~GCWQ_MANAGING_WORKERS;
1613
1614 /*
1615 * The trustee might be waiting to take over the manager
1616 * position, tell it we're done.
1617 */
1618 if (unlikely(gcwq->trustee))
1619 wake_up_all(&gcwq->trustee_wait);
1620
1621 return ret;
1622}
1623
affee4b2
TH
1624/**
1625 * move_linked_works - move linked works to a list
1626 * @work: start of series of works to be scheduled
1627 * @head: target list to append @work to
1628 * @nextp: out paramter for nested worklist walking
1629 *
1630 * Schedule linked works starting from @work to @head. Work series to
1631 * be scheduled starts at @work and includes any consecutive work with
1632 * WORK_STRUCT_LINKED set in its predecessor.
1633 *
1634 * If @nextp is not NULL, it's updated to point to the next work of
1635 * the last scheduled work. This allows move_linked_works() to be
1636 * nested inside outer list_for_each_entry_safe().
1637 *
1638 * CONTEXT:
8b03ae3c 1639 * spin_lock_irq(gcwq->lock).
affee4b2
TH
1640 */
1641static void move_linked_works(struct work_struct *work, struct list_head *head,
1642 struct work_struct **nextp)
1643{
1644 struct work_struct *n;
1645
1646 /*
1647 * Linked worklist will always end before the end of the list,
1648 * use NULL for list head.
1649 */
1650 list_for_each_entry_safe_from(work, n, NULL, entry) {
1651 list_move_tail(&work->entry, head);
1652 if (!(*work_data_bits(work) & WORK_STRUCT_LINKED))
1653 break;
1654 }
1655
1656 /*
1657 * If we're already inside safe list traversal and have moved
1658 * multiple works to the scheduled queue, the next position
1659 * needs to be updated.
1660 */
1661 if (nextp)
1662 *nextp = n;
1663}
1664
1e19ffc6
TH
1665static void cwq_activate_first_delayed(struct cpu_workqueue_struct *cwq)
1666{
1667 struct work_struct *work = list_first_entry(&cwq->delayed_works,
1668 struct work_struct, entry);
649027d7 1669 struct list_head *pos = gcwq_determine_ins_pos(cwq->gcwq, cwq);
1e19ffc6 1670
649027d7 1671 move_linked_works(work, pos, NULL);
1e19ffc6
TH
1672 cwq->nr_active++;
1673}
1674
73f53c4a
TH
1675/**
1676 * cwq_dec_nr_in_flight - decrement cwq's nr_in_flight
1677 * @cwq: cwq of interest
1678 * @color: color of work which left the queue
1679 *
1680 * A work either has completed or is removed from pending queue,
1681 * decrement nr_in_flight of its cwq and handle workqueue flushing.
1682 *
1683 * CONTEXT:
8b03ae3c 1684 * spin_lock_irq(gcwq->lock).
73f53c4a
TH
1685 */
1686static void cwq_dec_nr_in_flight(struct cpu_workqueue_struct *cwq, int color)
1687{
1688 /* ignore uncolored works */
1689 if (color == WORK_NO_COLOR)
1690 return;
1691
1692 cwq->nr_in_flight[color]--;
1e19ffc6
TH
1693 cwq->nr_active--;
1694
502ca9d8
TH
1695 if (!list_empty(&cwq->delayed_works)) {
1696 /* one down, submit a delayed one */
1697 if (cwq->nr_active < cwq->max_active)
1698 cwq_activate_first_delayed(cwq);
1699 } else if (!cwq->nr_active && cwq->wq->flags & WQ_SINGLE_CPU) {
1700 /* this was the last work, unbind from single cpu */
1701 cwq_unbind_single_cpu(cwq);
1702 }
73f53c4a
TH
1703
1704 /* is flush in progress and are we at the flushing tip? */
1705 if (likely(cwq->flush_color != color))
1706 return;
1707
1708 /* are there still in-flight works? */
1709 if (cwq->nr_in_flight[color])
1710 return;
1711
1712 /* this cwq is done, clear flush_color */
1713 cwq->flush_color = -1;
1714
1715 /*
1716 * If this was the last cwq, wake up the first flusher. It
1717 * will handle the rest.
1718 */
1719 if (atomic_dec_and_test(&cwq->wq->nr_cwqs_to_flush))
1720 complete(&cwq->wq->first_flusher->done);
1721}
1722
a62428c0
TH
1723/**
1724 * process_one_work - process single work
c34056a3 1725 * @worker: self
a62428c0
TH
1726 * @work: work to process
1727 *
1728 * Process @work. This function contains all the logics necessary to
1729 * process a single work including synchronization against and
1730 * interaction with other workers on the same cpu, queueing and
1731 * flushing. As long as context requirement is met, any worker can
1732 * call this function to process a work.
1733 *
1734 * CONTEXT:
8b03ae3c 1735 * spin_lock_irq(gcwq->lock) which is released and regrabbed.
a62428c0 1736 */
c34056a3 1737static void process_one_work(struct worker *worker, struct work_struct *work)
a62428c0 1738{
7e11629d 1739 struct cpu_workqueue_struct *cwq = get_work_cwq(work);
8b03ae3c 1740 struct global_cwq *gcwq = cwq->gcwq;
c8e55f36 1741 struct hlist_head *bwh = busy_worker_head(gcwq, work);
fb0e7beb 1742 bool cpu_intensive = cwq->wq->flags & WQ_CPU_INTENSIVE;
a62428c0 1743 work_func_t f = work->func;
73f53c4a 1744 int work_color;
7e11629d 1745 struct worker *collision;
a62428c0
TH
1746#ifdef CONFIG_LOCKDEP
1747 /*
1748 * It is permissible to free the struct work_struct from
1749 * inside the function that is called from it, this we need to
1750 * take into account for lockdep too. To avoid bogus "held
1751 * lock freed" warnings as well as problems when looking into
1752 * work->lockdep_map, make a copy and use that here.
1753 */
1754 struct lockdep_map lockdep_map = work->lockdep_map;
1755#endif
7e11629d
TH
1756 /*
1757 * A single work shouldn't be executed concurrently by
1758 * multiple workers on a single cpu. Check whether anyone is
1759 * already processing the work. If so, defer the work to the
1760 * currently executing one.
1761 */
1762 collision = __find_worker_executing_work(gcwq, bwh, work);
1763 if (unlikely(collision)) {
1764 move_linked_works(work, &collision->scheduled, NULL);
1765 return;
1766 }
1767
a62428c0 1768 /* claim and process */
a62428c0 1769 debug_work_deactivate(work);
c8e55f36 1770 hlist_add_head(&worker->hentry, bwh);
c34056a3 1771 worker->current_work = work;
8cca0eea 1772 worker->current_cwq = cwq;
73f53c4a 1773 work_color = get_work_color(work);
7a22ad75 1774
7a22ad75
TH
1775 /* record the current cpu number in the work data and dequeue */
1776 set_work_cpu(work, gcwq->cpu);
a62428c0
TH
1777 list_del_init(&work->entry);
1778
649027d7
TH
1779 /*
1780 * If HIGHPRI_PENDING, check the next work, and, if HIGHPRI,
1781 * wake up another worker; otherwise, clear HIGHPRI_PENDING.
1782 */
1783 if (unlikely(gcwq->flags & GCWQ_HIGHPRI_PENDING)) {
1784 struct work_struct *nwork = list_first_entry(&gcwq->worklist,
1785 struct work_struct, entry);
1786
1787 if (!list_empty(&gcwq->worklist) &&
1788 get_work_cwq(nwork)->wq->flags & WQ_HIGHPRI)
1789 wake_up_worker(gcwq);
1790 else
1791 gcwq->flags &= ~GCWQ_HIGHPRI_PENDING;
1792 }
1793
fb0e7beb
TH
1794 /*
1795 * CPU intensive works don't participate in concurrency
1796 * management. They're the scheduler's responsibility.
1797 */
1798 if (unlikely(cpu_intensive))
1799 worker_set_flags(worker, WORKER_CPU_INTENSIVE, true);
1800
8b03ae3c 1801 spin_unlock_irq(&gcwq->lock);
a62428c0 1802
a62428c0
TH
1803 work_clear_pending(work);
1804 lock_map_acquire(&cwq->wq->lockdep_map);
1805 lock_map_acquire(&lockdep_map);
1806 f(work);
1807 lock_map_release(&lockdep_map);
1808 lock_map_release(&cwq->wq->lockdep_map);
1809
1810 if (unlikely(in_atomic() || lockdep_depth(current) > 0)) {
1811 printk(KERN_ERR "BUG: workqueue leaked lock or atomic: "
1812 "%s/0x%08x/%d\n",
1813 current->comm, preempt_count(), task_pid_nr(current));
1814 printk(KERN_ERR " last function: ");
1815 print_symbol("%s\n", (unsigned long)f);
1816 debug_show_held_locks(current);
1817 dump_stack();
1818 }
1819
8b03ae3c 1820 spin_lock_irq(&gcwq->lock);
a62428c0 1821
fb0e7beb
TH
1822 /* clear cpu intensive status */
1823 if (unlikely(cpu_intensive))
1824 worker_clr_flags(worker, WORKER_CPU_INTENSIVE);
1825
a62428c0 1826 /* we're done with it, release */
c8e55f36 1827 hlist_del_init(&worker->hentry);
c34056a3 1828 worker->current_work = NULL;
8cca0eea 1829 worker->current_cwq = NULL;
73f53c4a 1830 cwq_dec_nr_in_flight(cwq, work_color);
a62428c0
TH
1831}
1832
affee4b2
TH
1833/**
1834 * process_scheduled_works - process scheduled works
1835 * @worker: self
1836 *
1837 * Process all scheduled works. Please note that the scheduled list
1838 * may change while processing a work, so this function repeatedly
1839 * fetches a work from the top and executes it.
1840 *
1841 * CONTEXT:
8b03ae3c 1842 * spin_lock_irq(gcwq->lock) which may be released and regrabbed
affee4b2
TH
1843 * multiple times.
1844 */
1845static void process_scheduled_works(struct worker *worker)
1da177e4 1846{
affee4b2
TH
1847 while (!list_empty(&worker->scheduled)) {
1848 struct work_struct *work = list_first_entry(&worker->scheduled,
1da177e4 1849 struct work_struct, entry);
c34056a3 1850 process_one_work(worker, work);
1da177e4 1851 }
1da177e4
LT
1852}
1853
4690c4ab
TH
1854/**
1855 * worker_thread - the worker thread function
c34056a3 1856 * @__worker: self
4690c4ab 1857 *
e22bee78
TH
1858 * The gcwq worker thread function. There's a single dynamic pool of
1859 * these per each cpu. These workers process all works regardless of
1860 * their specific target workqueue. The only exception is works which
1861 * belong to workqueues with a rescuer which will be explained in
1862 * rescuer_thread().
4690c4ab 1863 */
c34056a3 1864static int worker_thread(void *__worker)
1da177e4 1865{
c34056a3 1866 struct worker *worker = __worker;
8b03ae3c 1867 struct global_cwq *gcwq = worker->gcwq;
1da177e4 1868
e22bee78
TH
1869 /* tell the scheduler that this is a workqueue worker */
1870 worker->task->flags |= PF_WQ_WORKER;
c8e55f36 1871woke_up:
c8e55f36 1872 spin_lock_irq(&gcwq->lock);
1da177e4 1873
c8e55f36
TH
1874 /* DIE can be set only while we're idle, checking here is enough */
1875 if (worker->flags & WORKER_DIE) {
1876 spin_unlock_irq(&gcwq->lock);
e22bee78 1877 worker->task->flags &= ~PF_WQ_WORKER;
c8e55f36
TH
1878 return 0;
1879 }
affee4b2 1880
c8e55f36 1881 worker_leave_idle(worker);
db7bccf4 1882recheck:
e22bee78
TH
1883 /* no more worker necessary? */
1884 if (!need_more_worker(gcwq))
1885 goto sleep;
1886
1887 /* do we need to manage? */
1888 if (unlikely(!may_start_working(gcwq)) && manage_workers(worker))
1889 goto recheck;
1890
c8e55f36
TH
1891 /*
1892 * ->scheduled list can only be filled while a worker is
1893 * preparing to process a work or actually processing it.
1894 * Make sure nobody diddled with it while I was sleeping.
1895 */
1896 BUG_ON(!list_empty(&worker->scheduled));
1897
e22bee78
TH
1898 /*
1899 * When control reaches this point, we're guaranteed to have
1900 * at least one idle worker or that someone else has already
1901 * assumed the manager role.
1902 */
1903 worker_clr_flags(worker, WORKER_PREP);
1904
1905 do {
c8e55f36 1906 struct work_struct *work =
7e11629d 1907 list_first_entry(&gcwq->worklist,
c8e55f36
TH
1908 struct work_struct, entry);
1909
1910 if (likely(!(*work_data_bits(work) & WORK_STRUCT_LINKED))) {
1911 /* optimization path, not strictly necessary */
1912 process_one_work(worker, work);
1913 if (unlikely(!list_empty(&worker->scheduled)))
affee4b2 1914 process_scheduled_works(worker);
c8e55f36
TH
1915 } else {
1916 move_linked_works(work, &worker->scheduled, NULL);
1917 process_scheduled_works(worker);
affee4b2 1918 }
e22bee78
TH
1919 } while (keep_working(gcwq));
1920
1921 worker_set_flags(worker, WORKER_PREP, false);
d313dd85 1922sleep:
e22bee78
TH
1923 if (unlikely(need_to_manage_workers(gcwq)) && manage_workers(worker))
1924 goto recheck;
d313dd85 1925
c8e55f36 1926 /*
e22bee78
TH
1927 * gcwq->lock is held and there's no work to process and no
1928 * need to manage, sleep. Workers are woken up only while
1929 * holding gcwq->lock or from local cpu, so setting the
1930 * current state before releasing gcwq->lock is enough to
1931 * prevent losing any event.
c8e55f36
TH
1932 */
1933 worker_enter_idle(worker);
1934 __set_current_state(TASK_INTERRUPTIBLE);
1935 spin_unlock_irq(&gcwq->lock);
1936 schedule();
1937 goto woke_up;
1da177e4
LT
1938}
1939
e22bee78
TH
1940/**
1941 * rescuer_thread - the rescuer thread function
1942 * @__wq: the associated workqueue
1943 *
1944 * Workqueue rescuer thread function. There's one rescuer for each
1945 * workqueue which has WQ_RESCUER set.
1946 *
1947 * Regular work processing on a gcwq may block trying to create a new
1948 * worker which uses GFP_KERNEL allocation which has slight chance of
1949 * developing into deadlock if some works currently on the same queue
1950 * need to be processed to satisfy the GFP_KERNEL allocation. This is
1951 * the problem rescuer solves.
1952 *
1953 * When such condition is possible, the gcwq summons rescuers of all
1954 * workqueues which have works queued on the gcwq and let them process
1955 * those works so that forward progress can be guaranteed.
1956 *
1957 * This should happen rarely.
1958 */
1959static int rescuer_thread(void *__wq)
1960{
1961 struct workqueue_struct *wq = __wq;
1962 struct worker *rescuer = wq->rescuer;
1963 struct list_head *scheduled = &rescuer->scheduled;
f3421797 1964 bool is_unbound = wq->flags & WQ_UNBOUND;
e22bee78
TH
1965 unsigned int cpu;
1966
1967 set_user_nice(current, RESCUER_NICE_LEVEL);
1968repeat:
1969 set_current_state(TASK_INTERRUPTIBLE);
1970
1971 if (kthread_should_stop())
1972 return 0;
1973
f3421797
TH
1974 /*
1975 * See whether any cpu is asking for help. Unbounded
1976 * workqueues use cpu 0 in mayday_mask for CPU_UNBOUND.
1977 */
e22bee78 1978 for_each_cpu(cpu, wq->mayday_mask) {
f3421797
TH
1979 unsigned int tcpu = is_unbound ? WORK_CPU_UNBOUND : cpu;
1980 struct cpu_workqueue_struct *cwq = get_cwq(tcpu, wq);
e22bee78
TH
1981 struct global_cwq *gcwq = cwq->gcwq;
1982 struct work_struct *work, *n;
1983
1984 __set_current_state(TASK_RUNNING);
1985 cpumask_clear_cpu(cpu, wq->mayday_mask);
1986
1987 /* migrate to the target cpu if possible */
1988 rescuer->gcwq = gcwq;
1989 worker_maybe_bind_and_lock(rescuer);
1990
1991 /*
1992 * Slurp in all works issued via this workqueue and
1993 * process'em.
1994 */
1995 BUG_ON(!list_empty(&rescuer->scheduled));
1996 list_for_each_entry_safe(work, n, &gcwq->worklist, entry)
1997 if (get_work_cwq(work) == cwq)
1998 move_linked_works(work, scheduled, &n);
1999
2000 process_scheduled_works(rescuer);
2001 spin_unlock_irq(&gcwq->lock);
2002 }
2003
2004 schedule();
2005 goto repeat;
2006}
2007
fc2e4d70
ON
2008struct wq_barrier {
2009 struct work_struct work;
2010 struct completion done;
2011};
2012
2013static void wq_barrier_func(struct work_struct *work)
2014{
2015 struct wq_barrier *barr = container_of(work, struct wq_barrier, work);
2016 complete(&barr->done);
2017}
2018
4690c4ab
TH
2019/**
2020 * insert_wq_barrier - insert a barrier work
2021 * @cwq: cwq to insert barrier into
2022 * @barr: wq_barrier to insert
affee4b2
TH
2023 * @target: target work to attach @barr to
2024 * @worker: worker currently executing @target, NULL if @target is not executing
4690c4ab 2025 *
affee4b2
TH
2026 * @barr is linked to @target such that @barr is completed only after
2027 * @target finishes execution. Please note that the ordering
2028 * guarantee is observed only with respect to @target and on the local
2029 * cpu.
2030 *
2031 * Currently, a queued barrier can't be canceled. This is because
2032 * try_to_grab_pending() can't determine whether the work to be
2033 * grabbed is at the head of the queue and thus can't clear LINKED
2034 * flag of the previous work while there must be a valid next work
2035 * after a work with LINKED flag set.
2036 *
2037 * Note that when @worker is non-NULL, @target may be modified
2038 * underneath us, so we can't reliably determine cwq from @target.
4690c4ab
TH
2039 *
2040 * CONTEXT:
8b03ae3c 2041 * spin_lock_irq(gcwq->lock).
4690c4ab 2042 */
83c22520 2043static void insert_wq_barrier(struct cpu_workqueue_struct *cwq,
affee4b2
TH
2044 struct wq_barrier *barr,
2045 struct work_struct *target, struct worker *worker)
fc2e4d70 2046{
affee4b2
TH
2047 struct list_head *head;
2048 unsigned int linked = 0;
2049
dc186ad7 2050 /*
8b03ae3c 2051 * debugobject calls are safe here even with gcwq->lock locked
dc186ad7
TG
2052 * as we know for sure that this will not trigger any of the
2053 * checks and call back into the fixup functions where we
2054 * might deadlock.
2055 */
2056 INIT_WORK_ON_STACK(&barr->work, wq_barrier_func);
22df02bb 2057 __set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(&barr->work));
fc2e4d70 2058 init_completion(&barr->done);
83c22520 2059
affee4b2
TH
2060 /*
2061 * If @target is currently being executed, schedule the
2062 * barrier to the worker; otherwise, put it after @target.
2063 */
2064 if (worker)
2065 head = worker->scheduled.next;
2066 else {
2067 unsigned long *bits = work_data_bits(target);
2068
2069 head = target->entry.next;
2070 /* there can already be other linked works, inherit and set */
2071 linked = *bits & WORK_STRUCT_LINKED;
2072 __set_bit(WORK_STRUCT_LINKED_BIT, bits);
2073 }
2074
dc186ad7 2075 debug_work_activate(&barr->work);
affee4b2
TH
2076 insert_work(cwq, &barr->work, head,
2077 work_color_to_flags(WORK_NO_COLOR) | linked);
fc2e4d70
ON
2078}
2079
73f53c4a
TH
2080/**
2081 * flush_workqueue_prep_cwqs - prepare cwqs for workqueue flushing
2082 * @wq: workqueue being flushed
2083 * @flush_color: new flush color, < 0 for no-op
2084 * @work_color: new work color, < 0 for no-op
2085 *
2086 * Prepare cwqs for workqueue flushing.
2087 *
2088 * If @flush_color is non-negative, flush_color on all cwqs should be
2089 * -1. If no cwq has in-flight commands at the specified color, all
2090 * cwq->flush_color's stay at -1 and %false is returned. If any cwq
2091 * has in flight commands, its cwq->flush_color is set to
2092 * @flush_color, @wq->nr_cwqs_to_flush is updated accordingly, cwq
2093 * wakeup logic is armed and %true is returned.
2094 *
2095 * The caller should have initialized @wq->first_flusher prior to
2096 * calling this function with non-negative @flush_color. If
2097 * @flush_color is negative, no flush color update is done and %false
2098 * is returned.
2099 *
2100 * If @work_color is non-negative, all cwqs should have the same
2101 * work_color which is previous to @work_color and all will be
2102 * advanced to @work_color.
2103 *
2104 * CONTEXT:
2105 * mutex_lock(wq->flush_mutex).
2106 *
2107 * RETURNS:
2108 * %true if @flush_color >= 0 and there's something to flush. %false
2109 * otherwise.
2110 */
2111static bool flush_workqueue_prep_cwqs(struct workqueue_struct *wq,
2112 int flush_color, int work_color)
1da177e4 2113{
73f53c4a
TH
2114 bool wait = false;
2115 unsigned int cpu;
1da177e4 2116
73f53c4a
TH
2117 if (flush_color >= 0) {
2118 BUG_ON(atomic_read(&wq->nr_cwqs_to_flush));
2119 atomic_set(&wq->nr_cwqs_to_flush, 1);
1da177e4 2120 }
2355b70f 2121
f3421797 2122 for_each_cwq_cpu(cpu, wq) {
73f53c4a 2123 struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
8b03ae3c 2124 struct global_cwq *gcwq = cwq->gcwq;
73f53c4a 2125
8b03ae3c 2126 spin_lock_irq(&gcwq->lock);
73f53c4a
TH
2127
2128 if (flush_color >= 0) {
2129 BUG_ON(cwq->flush_color != -1);
2130
2131 if (cwq->nr_in_flight[flush_color]) {
2132 cwq->flush_color = flush_color;
2133 atomic_inc(&wq->nr_cwqs_to_flush);
2134 wait = true;
2135 }
2136 }
2137
2138 if (work_color >= 0) {
2139 BUG_ON(work_color != work_next_color(cwq->work_color));
2140 cwq->work_color = work_color;
2141 }
2142
8b03ae3c 2143 spin_unlock_irq(&gcwq->lock);
dc186ad7 2144 }
14441960 2145
73f53c4a
TH
2146 if (flush_color >= 0 && atomic_dec_and_test(&wq->nr_cwqs_to_flush))
2147 complete(&wq->first_flusher->done);
2148
2149 return wait;
1da177e4
LT
2150}
2151
0fcb78c2 2152/**
1da177e4 2153 * flush_workqueue - ensure that any scheduled work has run to completion.
0fcb78c2 2154 * @wq: workqueue to flush
1da177e4
LT
2155 *
2156 * Forces execution of the workqueue and blocks until its completion.
2157 * This is typically used in driver shutdown handlers.
2158 *
fc2e4d70
ON
2159 * We sleep until all works which were queued on entry have been handled,
2160 * but we are not livelocked by new incoming ones.
1da177e4 2161 */
7ad5b3a5 2162void flush_workqueue(struct workqueue_struct *wq)
1da177e4 2163{
73f53c4a
TH
2164 struct wq_flusher this_flusher = {
2165 .list = LIST_HEAD_INIT(this_flusher.list),
2166 .flush_color = -1,
2167 .done = COMPLETION_INITIALIZER_ONSTACK(this_flusher.done),
2168 };
2169 int next_color;
1da177e4 2170
3295f0ef
IM
2171 lock_map_acquire(&wq->lockdep_map);
2172 lock_map_release(&wq->lockdep_map);
73f53c4a
TH
2173
2174 mutex_lock(&wq->flush_mutex);
2175
2176 /*
2177 * Start-to-wait phase
2178 */
2179 next_color = work_next_color(wq->work_color);
2180
2181 if (next_color != wq->flush_color) {
2182 /*
2183 * Color space is not full. The current work_color
2184 * becomes our flush_color and work_color is advanced
2185 * by one.
2186 */
2187 BUG_ON(!list_empty(&wq->flusher_overflow));
2188 this_flusher.flush_color = wq->work_color;
2189 wq->work_color = next_color;
2190
2191 if (!wq->first_flusher) {
2192 /* no flush in progress, become the first flusher */
2193 BUG_ON(wq->flush_color != this_flusher.flush_color);
2194
2195 wq->first_flusher = &this_flusher;
2196
2197 if (!flush_workqueue_prep_cwqs(wq, wq->flush_color,
2198 wq->work_color)) {
2199 /* nothing to flush, done */
2200 wq->flush_color = next_color;
2201 wq->first_flusher = NULL;
2202 goto out_unlock;
2203 }
2204 } else {
2205 /* wait in queue */
2206 BUG_ON(wq->flush_color == this_flusher.flush_color);
2207 list_add_tail(&this_flusher.list, &wq->flusher_queue);
2208 flush_workqueue_prep_cwqs(wq, -1, wq->work_color);
2209 }
2210 } else {
2211 /*
2212 * Oops, color space is full, wait on overflow queue.
2213 * The next flush completion will assign us
2214 * flush_color and transfer to flusher_queue.
2215 */
2216 list_add_tail(&this_flusher.list, &wq->flusher_overflow);
2217 }
2218
2219 mutex_unlock(&wq->flush_mutex);
2220
2221 wait_for_completion(&this_flusher.done);
2222
2223 /*
2224 * Wake-up-and-cascade phase
2225 *
2226 * First flushers are responsible for cascading flushes and
2227 * handling overflow. Non-first flushers can simply return.
2228 */
2229 if (wq->first_flusher != &this_flusher)
2230 return;
2231
2232 mutex_lock(&wq->flush_mutex);
2233
4ce48b37
TH
2234 /* we might have raced, check again with mutex held */
2235 if (wq->first_flusher != &this_flusher)
2236 goto out_unlock;
2237
73f53c4a
TH
2238 wq->first_flusher = NULL;
2239
2240 BUG_ON(!list_empty(&this_flusher.list));
2241 BUG_ON(wq->flush_color != this_flusher.flush_color);
2242
2243 while (true) {
2244 struct wq_flusher *next, *tmp;
2245
2246 /* complete all the flushers sharing the current flush color */
2247 list_for_each_entry_safe(next, tmp, &wq->flusher_queue, list) {
2248 if (next->flush_color != wq->flush_color)
2249 break;
2250 list_del_init(&next->list);
2251 complete(&next->done);
2252 }
2253
2254 BUG_ON(!list_empty(&wq->flusher_overflow) &&
2255 wq->flush_color != work_next_color(wq->work_color));
2256
2257 /* this flush_color is finished, advance by one */
2258 wq->flush_color = work_next_color(wq->flush_color);
2259
2260 /* one color has been freed, handle overflow queue */
2261 if (!list_empty(&wq->flusher_overflow)) {
2262 /*
2263 * Assign the same color to all overflowed
2264 * flushers, advance work_color and append to
2265 * flusher_queue. This is the start-to-wait
2266 * phase for these overflowed flushers.
2267 */
2268 list_for_each_entry(tmp, &wq->flusher_overflow, list)
2269 tmp->flush_color = wq->work_color;
2270
2271 wq->work_color = work_next_color(wq->work_color);
2272
2273 list_splice_tail_init(&wq->flusher_overflow,
2274 &wq->flusher_queue);
2275 flush_workqueue_prep_cwqs(wq, -1, wq->work_color);
2276 }
2277
2278 if (list_empty(&wq->flusher_queue)) {
2279 BUG_ON(wq->flush_color != wq->work_color);
2280 break;
2281 }
2282
2283 /*
2284 * Need to flush more colors. Make the next flusher
2285 * the new first flusher and arm cwqs.
2286 */
2287 BUG_ON(wq->flush_color == wq->work_color);
2288 BUG_ON(wq->flush_color != next->flush_color);
2289
2290 list_del_init(&next->list);
2291 wq->first_flusher = next;
2292
2293 if (flush_workqueue_prep_cwqs(wq, wq->flush_color, -1))
2294 break;
2295
2296 /*
2297 * Meh... this color is already done, clear first
2298 * flusher and repeat cascading.
2299 */
2300 wq->first_flusher = NULL;
2301 }
2302
2303out_unlock:
2304 mutex_unlock(&wq->flush_mutex);
1da177e4 2305}
ae90dd5d 2306EXPORT_SYMBOL_GPL(flush_workqueue);
1da177e4 2307
db700897
ON
2308/**
2309 * flush_work - block until a work_struct's callback has terminated
2310 * @work: the work which is to be flushed
2311 *
a67da70d
ON
2312 * Returns false if @work has already terminated.
2313 *
db700897
ON
2314 * It is expected that, prior to calling flush_work(), the caller has
2315 * arranged for the work to not be requeued, otherwise it doesn't make
2316 * sense to use this function.
2317 */
2318int flush_work(struct work_struct *work)
2319{
affee4b2 2320 struct worker *worker = NULL;
8b03ae3c 2321 struct global_cwq *gcwq;
7a22ad75 2322 struct cpu_workqueue_struct *cwq;
db700897
ON
2323 struct wq_barrier barr;
2324
2325 might_sleep();
7a22ad75
TH
2326 gcwq = get_work_gcwq(work);
2327 if (!gcwq)
db700897 2328 return 0;
a67da70d 2329
8b03ae3c 2330 spin_lock_irq(&gcwq->lock);
db700897
ON
2331 if (!list_empty(&work->entry)) {
2332 /*
2333 * See the comment near try_to_grab_pending()->smp_rmb().
7a22ad75
TH
2334 * If it was re-queued to a different gcwq under us, we
2335 * are not going to wait.
db700897
ON
2336 */
2337 smp_rmb();
7a22ad75
TH
2338 cwq = get_work_cwq(work);
2339 if (unlikely(!cwq || gcwq != cwq->gcwq))
4690c4ab 2340 goto already_gone;
db700897 2341 } else {
7a22ad75 2342 worker = find_worker_executing_work(gcwq, work);
affee4b2 2343 if (!worker)
4690c4ab 2344 goto already_gone;
7a22ad75 2345 cwq = worker->current_cwq;
db700897 2346 }
db700897 2347
affee4b2 2348 insert_wq_barrier(cwq, &barr, work, worker);
8b03ae3c 2349 spin_unlock_irq(&gcwq->lock);
7a22ad75
TH
2350
2351 lock_map_acquire(&cwq->wq->lockdep_map);
2352 lock_map_release(&cwq->wq->lockdep_map);
2353
db700897 2354 wait_for_completion(&barr.done);
dc186ad7 2355 destroy_work_on_stack(&barr.work);
db700897 2356 return 1;
4690c4ab 2357already_gone:
8b03ae3c 2358 spin_unlock_irq(&gcwq->lock);
4690c4ab 2359 return 0;
db700897
ON
2360}
2361EXPORT_SYMBOL_GPL(flush_work);
2362
6e84d644 2363/*
1f1f642e 2364 * Upon a successful return (>= 0), the caller "owns" WORK_STRUCT_PENDING bit,
6e84d644
ON
2365 * so this work can't be re-armed in any way.
2366 */
2367static int try_to_grab_pending(struct work_struct *work)
2368{
8b03ae3c 2369 struct global_cwq *gcwq;
1f1f642e 2370 int ret = -1;
6e84d644 2371
22df02bb 2372 if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work)))
1f1f642e 2373 return 0;
6e84d644
ON
2374
2375 /*
2376 * The queueing is in progress, or it is already queued. Try to
2377 * steal it from ->worklist without clearing WORK_STRUCT_PENDING.
2378 */
7a22ad75
TH
2379 gcwq = get_work_gcwq(work);
2380 if (!gcwq)
6e84d644
ON
2381 return ret;
2382
8b03ae3c 2383 spin_lock_irq(&gcwq->lock);
6e84d644
ON
2384 if (!list_empty(&work->entry)) {
2385 /*
7a22ad75 2386 * This work is queued, but perhaps we locked the wrong gcwq.
6e84d644
ON
2387 * In that case we must see the new value after rmb(), see
2388 * insert_work()->wmb().
2389 */
2390 smp_rmb();
7a22ad75 2391 if (gcwq == get_work_gcwq(work)) {
dc186ad7 2392 debug_work_deactivate(work);
6e84d644 2393 list_del_init(&work->entry);
7a22ad75
TH
2394 cwq_dec_nr_in_flight(get_work_cwq(work),
2395 get_work_color(work));
6e84d644
ON
2396 ret = 1;
2397 }
2398 }
8b03ae3c 2399 spin_unlock_irq(&gcwq->lock);
6e84d644
ON
2400
2401 return ret;
2402}
2403
7a22ad75 2404static void wait_on_cpu_work(struct global_cwq *gcwq, struct work_struct *work)
b89deed3
ON
2405{
2406 struct wq_barrier barr;
affee4b2 2407 struct worker *worker;
b89deed3 2408
8b03ae3c 2409 spin_lock_irq(&gcwq->lock);
affee4b2 2410
7a22ad75
TH
2411 worker = find_worker_executing_work(gcwq, work);
2412 if (unlikely(worker))
2413 insert_wq_barrier(worker->current_cwq, &barr, work, worker);
affee4b2 2414
8b03ae3c 2415 spin_unlock_irq(&gcwq->lock);
b89deed3 2416
affee4b2 2417 if (unlikely(worker)) {
b89deed3 2418 wait_for_completion(&barr.done);
dc186ad7
TG
2419 destroy_work_on_stack(&barr.work);
2420 }
b89deed3
ON
2421}
2422
6e84d644 2423static void wait_on_work(struct work_struct *work)
b89deed3 2424{
b1f4ec17 2425 int cpu;
b89deed3 2426
f293ea92
ON
2427 might_sleep();
2428
3295f0ef
IM
2429 lock_map_acquire(&work->lockdep_map);
2430 lock_map_release(&work->lockdep_map);
4e6045f1 2431
f3421797 2432 for_each_gcwq_cpu(cpu)
7a22ad75 2433 wait_on_cpu_work(get_gcwq(cpu), work);
6e84d644
ON
2434}
2435
1f1f642e
ON
2436static int __cancel_work_timer(struct work_struct *work,
2437 struct timer_list* timer)
2438{
2439 int ret;
2440
2441 do {
2442 ret = (timer && likely(del_timer(timer)));
2443 if (!ret)
2444 ret = try_to_grab_pending(work);
2445 wait_on_work(work);
2446 } while (unlikely(ret < 0));
2447
7a22ad75 2448 clear_work_data(work);
1f1f642e
ON
2449 return ret;
2450}
2451
6e84d644
ON
2452/**
2453 * cancel_work_sync - block until a work_struct's callback has terminated
2454 * @work: the work which is to be flushed
2455 *
1f1f642e
ON
2456 * Returns true if @work was pending.
2457 *
6e84d644
ON
2458 * cancel_work_sync() will cancel the work if it is queued. If the work's
2459 * callback appears to be running, cancel_work_sync() will block until it
2460 * has completed.
2461 *
2462 * It is possible to use this function if the work re-queues itself. It can
2463 * cancel the work even if it migrates to another workqueue, however in that
2464 * case it only guarantees that work->func() has completed on the last queued
2465 * workqueue.
2466 *
2467 * cancel_work_sync(&delayed_work->work) should be used only if ->timer is not
2468 * pending, otherwise it goes into a busy-wait loop until the timer expires.
2469 *
2470 * The caller must ensure that workqueue_struct on which this work was last
2471 * queued can't be destroyed before this function returns.
2472 */
1f1f642e 2473int cancel_work_sync(struct work_struct *work)
6e84d644 2474{
1f1f642e 2475 return __cancel_work_timer(work, NULL);
b89deed3 2476}
28e53bdd 2477EXPORT_SYMBOL_GPL(cancel_work_sync);
b89deed3 2478
6e84d644 2479/**
f5a421a4 2480 * cancel_delayed_work_sync - reliably kill off a delayed work.
6e84d644
ON
2481 * @dwork: the delayed work struct
2482 *
1f1f642e
ON
2483 * Returns true if @dwork was pending.
2484 *
6e84d644
ON
2485 * It is possible to use this function if @dwork rearms itself via queue_work()
2486 * or queue_delayed_work(). See also the comment for cancel_work_sync().
2487 */
1f1f642e 2488int cancel_delayed_work_sync(struct delayed_work *dwork)
6e84d644 2489{
1f1f642e 2490 return __cancel_work_timer(&dwork->work, &dwork->timer);
6e84d644 2491}
f5a421a4 2492EXPORT_SYMBOL(cancel_delayed_work_sync);
1da177e4 2493
0fcb78c2
REB
2494/**
2495 * schedule_work - put work task in global workqueue
2496 * @work: job to be done
2497 *
5b0f437d
BVA
2498 * Returns zero if @work was already on the kernel-global workqueue and
2499 * non-zero otherwise.
2500 *
2501 * This puts a job in the kernel-global workqueue if it was not already
2502 * queued and leaves it in the same position on the kernel-global
2503 * workqueue otherwise.
0fcb78c2 2504 */
7ad5b3a5 2505int schedule_work(struct work_struct *work)
1da177e4 2506{
d320c038 2507 return queue_work(system_wq, work);
1da177e4 2508}
ae90dd5d 2509EXPORT_SYMBOL(schedule_work);
1da177e4 2510
c1a220e7
ZR
2511/*
2512 * schedule_work_on - put work task on a specific cpu
2513 * @cpu: cpu to put the work task on
2514 * @work: job to be done
2515 *
2516 * This puts a job on a specific cpu
2517 */
2518int schedule_work_on(int cpu, struct work_struct *work)
2519{
d320c038 2520 return queue_work_on(cpu, system_wq, work);
c1a220e7
ZR
2521}
2522EXPORT_SYMBOL(schedule_work_on);
2523
0fcb78c2
REB
2524/**
2525 * schedule_delayed_work - put work task in global workqueue after delay
52bad64d
DH
2526 * @dwork: job to be done
2527 * @delay: number of jiffies to wait or 0 for immediate execution
0fcb78c2
REB
2528 *
2529 * After waiting for a given time this puts a job in the kernel-global
2530 * workqueue.
2531 */
7ad5b3a5 2532int schedule_delayed_work(struct delayed_work *dwork,
82f67cd9 2533 unsigned long delay)
1da177e4 2534{
d320c038 2535 return queue_delayed_work(system_wq, dwork, delay);
1da177e4 2536}
ae90dd5d 2537EXPORT_SYMBOL(schedule_delayed_work);
1da177e4 2538
8c53e463
LT
2539/**
2540 * flush_delayed_work - block until a dwork_struct's callback has terminated
2541 * @dwork: the delayed work which is to be flushed
2542 *
2543 * Any timeout is cancelled, and any pending work is run immediately.
2544 */
2545void flush_delayed_work(struct delayed_work *dwork)
2546{
2547 if (del_timer_sync(&dwork->timer)) {
7a22ad75 2548 __queue_work(get_cpu(), get_work_cwq(&dwork->work)->wq,
4690c4ab 2549 &dwork->work);
8c53e463
LT
2550 put_cpu();
2551 }
2552 flush_work(&dwork->work);
2553}
2554EXPORT_SYMBOL(flush_delayed_work);
2555
0fcb78c2
REB
2556/**
2557 * schedule_delayed_work_on - queue work in global workqueue on CPU after delay
2558 * @cpu: cpu to use
52bad64d 2559 * @dwork: job to be done
0fcb78c2
REB
2560 * @delay: number of jiffies to wait
2561 *
2562 * After waiting for a given time this puts a job in the kernel-global
2563 * workqueue on the specified CPU.
2564 */
1da177e4 2565int schedule_delayed_work_on(int cpu,
52bad64d 2566 struct delayed_work *dwork, unsigned long delay)
1da177e4 2567{
d320c038 2568 return queue_delayed_work_on(cpu, system_wq, dwork, delay);
1da177e4 2569}
ae90dd5d 2570EXPORT_SYMBOL(schedule_delayed_work_on);
1da177e4 2571
b6136773
AM
2572/**
2573 * schedule_on_each_cpu - call a function on each online CPU from keventd
2574 * @func: the function to call
b6136773
AM
2575 *
2576 * Returns zero on success.
2577 * Returns -ve errno on failure.
2578 *
b6136773
AM
2579 * schedule_on_each_cpu() is very slow.
2580 */
65f27f38 2581int schedule_on_each_cpu(work_func_t func)
15316ba8
CL
2582{
2583 int cpu;
b6136773 2584 struct work_struct *works;
15316ba8 2585
b6136773
AM
2586 works = alloc_percpu(struct work_struct);
2587 if (!works)
15316ba8 2588 return -ENOMEM;
b6136773 2589
93981800
TH
2590 get_online_cpus();
2591
15316ba8 2592 for_each_online_cpu(cpu) {
9bfb1839
IM
2593 struct work_struct *work = per_cpu_ptr(works, cpu);
2594
2595 INIT_WORK(work, func);
b71ab8c2 2596 schedule_work_on(cpu, work);
65a64464 2597 }
93981800
TH
2598
2599 for_each_online_cpu(cpu)
2600 flush_work(per_cpu_ptr(works, cpu));
2601
95402b38 2602 put_online_cpus();
b6136773 2603 free_percpu(works);
15316ba8
CL
2604 return 0;
2605}
2606
eef6a7d5
AS
2607/**
2608 * flush_scheduled_work - ensure that any scheduled work has run to completion.
2609 *
2610 * Forces execution of the kernel-global workqueue and blocks until its
2611 * completion.
2612 *
2613 * Think twice before calling this function! It's very easy to get into
2614 * trouble if you don't take great care. Either of the following situations
2615 * will lead to deadlock:
2616 *
2617 * One of the work items currently on the workqueue needs to acquire
2618 * a lock held by your code or its caller.
2619 *
2620 * Your code is running in the context of a work routine.
2621 *
2622 * They will be detected by lockdep when they occur, but the first might not
2623 * occur very often. It depends on what work items are on the workqueue and
2624 * what locks they need, which you have no control over.
2625 *
2626 * In most situations flushing the entire workqueue is overkill; you merely
2627 * need to know that a particular work item isn't queued and isn't running.
2628 * In such cases you should use cancel_delayed_work_sync() or
2629 * cancel_work_sync() instead.
2630 */
1da177e4
LT
2631void flush_scheduled_work(void)
2632{
d320c038 2633 flush_workqueue(system_wq);
1da177e4 2634}
ae90dd5d 2635EXPORT_SYMBOL(flush_scheduled_work);
1da177e4 2636
1fa44eca
JB
2637/**
2638 * execute_in_process_context - reliably execute the routine with user context
2639 * @fn: the function to execute
1fa44eca
JB
2640 * @ew: guaranteed storage for the execute work structure (must
2641 * be available when the work executes)
2642 *
2643 * Executes the function immediately if process context is available,
2644 * otherwise schedules the function for delayed execution.
2645 *
2646 * Returns: 0 - function was executed
2647 * 1 - function was scheduled for execution
2648 */
65f27f38 2649int execute_in_process_context(work_func_t fn, struct execute_work *ew)
1fa44eca
JB
2650{
2651 if (!in_interrupt()) {
65f27f38 2652 fn(&ew->work);
1fa44eca
JB
2653 return 0;
2654 }
2655
65f27f38 2656 INIT_WORK(&ew->work, fn);
1fa44eca
JB
2657 schedule_work(&ew->work);
2658
2659 return 1;
2660}
2661EXPORT_SYMBOL_GPL(execute_in_process_context);
2662
1da177e4
LT
2663int keventd_up(void)
2664{
d320c038 2665 return system_wq != NULL;
1da177e4
LT
2666}
2667
bdbc5dd7 2668static int alloc_cwqs(struct workqueue_struct *wq)
0f900049
TH
2669{
2670 /*
2671 * cwqs are forced aligned according to WORK_STRUCT_FLAG_BITS.
2672 * Make sure that the alignment isn't lower than that of
2673 * unsigned long long.
2674 */
2675 const size_t size = sizeof(struct cpu_workqueue_struct);
2676 const size_t align = max_t(size_t, 1 << WORK_STRUCT_FLAG_BITS,
2677 __alignof__(unsigned long long));
0f900049 2678
f3421797
TH
2679 if (CONFIG_SMP && !(wq->flags & WQ_UNBOUND)) {
2680 /* on SMP, percpu allocator can align itself */
2681 wq->cpu_wq.pcpu = __alloc_percpu(size, align);
2682 } else {
2683 void *ptr;
2684
2685 /*
2686 * Allocate enough room to align cwq and put an extra
2687 * pointer at the end pointing back to the originally
2688 * allocated pointer which will be used for free.
2689 */
2690 ptr = kzalloc(size + align + sizeof(void *), GFP_KERNEL);
2691 if (ptr) {
2692 wq->cpu_wq.single = PTR_ALIGN(ptr, align);
2693 *(void **)(wq->cpu_wq.single + 1) = ptr;
2694 }
bdbc5dd7 2695 }
f3421797 2696
0f900049 2697 /* just in case, make sure it's actually aligned */
bdbc5dd7
TH
2698 BUG_ON(!IS_ALIGNED(wq->cpu_wq.v, align));
2699 return wq->cpu_wq.v ? 0 : -ENOMEM;
0f900049
TH
2700}
2701
bdbc5dd7 2702static void free_cwqs(struct workqueue_struct *wq)
0f900049 2703{
f3421797
TH
2704 if (CONFIG_SMP && !(wq->flags & WQ_UNBOUND))
2705 free_percpu(wq->cpu_wq.pcpu);
2706 else if (wq->cpu_wq.single) {
2707 /* the pointer to free is stored right after the cwq */
bdbc5dd7 2708 kfree(*(void **)(wq->cpu_wq.single + 1));
f3421797 2709 }
0f900049
TH
2710}
2711
f3421797
TH
2712static int wq_clamp_max_active(int max_active, unsigned int flags,
2713 const char *name)
b71ab8c2 2714{
f3421797
TH
2715 int lim = flags & WQ_UNBOUND ? WQ_UNBOUND_MAX_ACTIVE : WQ_MAX_ACTIVE;
2716
2717 if (max_active < 1 || max_active > lim)
b71ab8c2
TH
2718 printk(KERN_WARNING "workqueue: max_active %d requested for %s "
2719 "is out of range, clamping between %d and %d\n",
f3421797 2720 max_active, name, 1, lim);
b71ab8c2 2721
f3421797 2722 return clamp_val(max_active, 1, lim);
b71ab8c2
TH
2723}
2724
d320c038
TH
2725struct workqueue_struct *__alloc_workqueue_key(const char *name,
2726 unsigned int flags,
2727 int max_active,
2728 struct lock_class_key *key,
2729 const char *lock_name)
1da177e4 2730{
1da177e4 2731 struct workqueue_struct *wq;
c34056a3 2732 unsigned int cpu;
1da177e4 2733
f3421797
TH
2734 /*
2735 * Unbound workqueues aren't concurrency managed and should be
2736 * dispatched to workers immediately.
2737 */
2738 if (flags & WQ_UNBOUND)
2739 flags |= WQ_HIGHPRI;
2740
d320c038 2741 max_active = max_active ?: WQ_DFL_ACTIVE;
f3421797 2742 max_active = wq_clamp_max_active(max_active, flags, name);
1e19ffc6 2743
3af24433
ON
2744 wq = kzalloc(sizeof(*wq), GFP_KERNEL);
2745 if (!wq)
4690c4ab 2746 goto err;
3af24433 2747
97e37d7b 2748 wq->flags = flags;
a0a1a5fd 2749 wq->saved_max_active = max_active;
73f53c4a
TH
2750 mutex_init(&wq->flush_mutex);
2751 atomic_set(&wq->nr_cwqs_to_flush, 0);
2752 INIT_LIST_HEAD(&wq->flusher_queue);
2753 INIT_LIST_HEAD(&wq->flusher_overflow);
bdbc5dd7 2754 wq->single_cpu = WORK_CPU_NONE;
502ca9d8 2755
3af24433 2756 wq->name = name;
eb13ba87 2757 lockdep_init_map(&wq->lockdep_map, lock_name, key, 0);
cce1a165 2758 INIT_LIST_HEAD(&wq->list);
3af24433 2759
bdbc5dd7
TH
2760 if (alloc_cwqs(wq) < 0)
2761 goto err;
2762
f3421797 2763 for_each_cwq_cpu(cpu, wq) {
1537663f 2764 struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
8b03ae3c 2765 struct global_cwq *gcwq = get_gcwq(cpu);
1537663f 2766
0f900049 2767 BUG_ON((unsigned long)cwq & WORK_STRUCT_FLAG_MASK);
8b03ae3c 2768 cwq->gcwq = gcwq;
c34056a3 2769 cwq->wq = wq;
73f53c4a 2770 cwq->flush_color = -1;
1e19ffc6 2771 cwq->max_active = max_active;
1e19ffc6 2772 INIT_LIST_HEAD(&cwq->delayed_works);
e22bee78 2773 }
1537663f 2774
e22bee78
TH
2775 if (flags & WQ_RESCUER) {
2776 struct worker *rescuer;
2777
2778 if (!alloc_cpumask_var(&wq->mayday_mask, GFP_KERNEL))
2779 goto err;
2780
2781 wq->rescuer = rescuer = alloc_worker();
2782 if (!rescuer)
2783 goto err;
2784
2785 rescuer->task = kthread_create(rescuer_thread, wq, "%s", name);
2786 if (IS_ERR(rescuer->task))
2787 goto err;
2788
2789 wq->rescuer = rescuer;
2790 rescuer->task->flags |= PF_THREAD_BOUND;
2791 wake_up_process(rescuer->task);
3af24433
ON
2792 }
2793
a0a1a5fd
TH
2794 /*
2795 * workqueue_lock protects global freeze state and workqueues
2796 * list. Grab it, set max_active accordingly and add the new
2797 * workqueue to workqueues list.
2798 */
1537663f 2799 spin_lock(&workqueue_lock);
a0a1a5fd
TH
2800
2801 if (workqueue_freezing && wq->flags & WQ_FREEZEABLE)
f3421797 2802 for_each_cwq_cpu(cpu, wq)
a0a1a5fd
TH
2803 get_cwq(cpu, wq)->max_active = 0;
2804
1537663f 2805 list_add(&wq->list, &workqueues);
a0a1a5fd 2806
1537663f
TH
2807 spin_unlock(&workqueue_lock);
2808
3af24433 2809 return wq;
4690c4ab
TH
2810err:
2811 if (wq) {
bdbc5dd7 2812 free_cwqs(wq);
e22bee78
TH
2813 free_cpumask_var(wq->mayday_mask);
2814 kfree(wq->rescuer);
4690c4ab
TH
2815 kfree(wq);
2816 }
2817 return NULL;
3af24433 2818}
d320c038 2819EXPORT_SYMBOL_GPL(__alloc_workqueue_key);
1da177e4 2820
3af24433
ON
2821/**
2822 * destroy_workqueue - safely terminate a workqueue
2823 * @wq: target workqueue
2824 *
2825 * Safely destroy a workqueue. All work currently pending will be done first.
2826 */
2827void destroy_workqueue(struct workqueue_struct *wq)
2828{
c8e55f36 2829 unsigned int cpu;
3af24433 2830
a0a1a5fd
TH
2831 flush_workqueue(wq);
2832
2833 /*
2834 * wq list is used to freeze wq, remove from list after
2835 * flushing is complete in case freeze races us.
2836 */
95402b38 2837 spin_lock(&workqueue_lock);
b1f4ec17 2838 list_del(&wq->list);
95402b38 2839 spin_unlock(&workqueue_lock);
3af24433 2840
e22bee78 2841 /* sanity check */
f3421797 2842 for_each_cwq_cpu(cpu, wq) {
73f53c4a
TH
2843 struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
2844 int i;
2845
73f53c4a
TH
2846 for (i = 0; i < WORK_NR_COLORS; i++)
2847 BUG_ON(cwq->nr_in_flight[i]);
1e19ffc6
TH
2848 BUG_ON(cwq->nr_active);
2849 BUG_ON(!list_empty(&cwq->delayed_works));
73f53c4a 2850 }
9b41ea72 2851
e22bee78
TH
2852 if (wq->flags & WQ_RESCUER) {
2853 kthread_stop(wq->rescuer->task);
2854 free_cpumask_var(wq->mayday_mask);
2855 }
2856
bdbc5dd7 2857 free_cwqs(wq);
3af24433
ON
2858 kfree(wq);
2859}
2860EXPORT_SYMBOL_GPL(destroy_workqueue);
2861
dcd989cb
TH
2862/**
2863 * workqueue_set_max_active - adjust max_active of a workqueue
2864 * @wq: target workqueue
2865 * @max_active: new max_active value.
2866 *
2867 * Set max_active of @wq to @max_active.
2868 *
2869 * CONTEXT:
2870 * Don't call from IRQ context.
2871 */
2872void workqueue_set_max_active(struct workqueue_struct *wq, int max_active)
2873{
2874 unsigned int cpu;
2875
f3421797 2876 max_active = wq_clamp_max_active(max_active, wq->flags, wq->name);
dcd989cb
TH
2877
2878 spin_lock(&workqueue_lock);
2879
2880 wq->saved_max_active = max_active;
2881
f3421797 2882 for_each_cwq_cpu(cpu, wq) {
dcd989cb
TH
2883 struct global_cwq *gcwq = get_gcwq(cpu);
2884
2885 spin_lock_irq(&gcwq->lock);
2886
2887 if (!(wq->flags & WQ_FREEZEABLE) ||
2888 !(gcwq->flags & GCWQ_FREEZING))
2889 get_cwq(gcwq->cpu, wq)->max_active = max_active;
2890
2891 spin_unlock_irq(&gcwq->lock);
2892 }
2893
2894 spin_unlock(&workqueue_lock);
2895}
2896EXPORT_SYMBOL_GPL(workqueue_set_max_active);
2897
2898/**
2899 * workqueue_congested - test whether a workqueue is congested
2900 * @cpu: CPU in question
2901 * @wq: target workqueue
2902 *
2903 * Test whether @wq's cpu workqueue for @cpu is congested. There is
2904 * no synchronization around this function and the test result is
2905 * unreliable and only useful as advisory hints or for debugging.
2906 *
2907 * RETURNS:
2908 * %true if congested, %false otherwise.
2909 */
2910bool workqueue_congested(unsigned int cpu, struct workqueue_struct *wq)
2911{
2912 struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
2913
2914 return !list_empty(&cwq->delayed_works);
2915}
2916EXPORT_SYMBOL_GPL(workqueue_congested);
2917
2918/**
2919 * work_cpu - return the last known associated cpu for @work
2920 * @work: the work of interest
2921 *
2922 * RETURNS:
bdbc5dd7 2923 * CPU number if @work was ever queued. WORK_CPU_NONE otherwise.
dcd989cb
TH
2924 */
2925unsigned int work_cpu(struct work_struct *work)
2926{
2927 struct global_cwq *gcwq = get_work_gcwq(work);
2928
bdbc5dd7 2929 return gcwq ? gcwq->cpu : WORK_CPU_NONE;
dcd989cb
TH
2930}
2931EXPORT_SYMBOL_GPL(work_cpu);
2932
2933/**
2934 * work_busy - test whether a work is currently pending or running
2935 * @work: the work to be tested
2936 *
2937 * Test whether @work is currently pending or running. There is no
2938 * synchronization around this function and the test result is
2939 * unreliable and only useful as advisory hints or for debugging.
2940 * Especially for reentrant wqs, the pending state might hide the
2941 * running state.
2942 *
2943 * RETURNS:
2944 * OR'd bitmask of WORK_BUSY_* bits.
2945 */
2946unsigned int work_busy(struct work_struct *work)
2947{
2948 struct global_cwq *gcwq = get_work_gcwq(work);
2949 unsigned long flags;
2950 unsigned int ret = 0;
2951
2952 if (!gcwq)
2953 return false;
2954
2955 spin_lock_irqsave(&gcwq->lock, flags);
2956
2957 if (work_pending(work))
2958 ret |= WORK_BUSY_PENDING;
2959 if (find_worker_executing_work(gcwq, work))
2960 ret |= WORK_BUSY_RUNNING;
2961
2962 spin_unlock_irqrestore(&gcwq->lock, flags);
2963
2964 return ret;
2965}
2966EXPORT_SYMBOL_GPL(work_busy);
2967
db7bccf4
TH
2968/*
2969 * CPU hotplug.
2970 *
e22bee78
TH
2971 * There are two challenges in supporting CPU hotplug. Firstly, there
2972 * are a lot of assumptions on strong associations among work, cwq and
2973 * gcwq which make migrating pending and scheduled works very
2974 * difficult to implement without impacting hot paths. Secondly,
2975 * gcwqs serve mix of short, long and very long running works making
2976 * blocked draining impractical.
2977 *
2978 * This is solved by allowing a gcwq to be detached from CPU, running
2979 * it with unbound (rogue) workers and allowing it to be reattached
2980 * later if the cpu comes back online. A separate thread is created
2981 * to govern a gcwq in such state and is called the trustee of the
2982 * gcwq.
db7bccf4
TH
2983 *
2984 * Trustee states and their descriptions.
2985 *
2986 * START Command state used on startup. On CPU_DOWN_PREPARE, a
2987 * new trustee is started with this state.
2988 *
2989 * IN_CHARGE Once started, trustee will enter this state after
e22bee78
TH
2990 * assuming the manager role and making all existing
2991 * workers rogue. DOWN_PREPARE waits for trustee to
2992 * enter this state. After reaching IN_CHARGE, trustee
2993 * tries to execute the pending worklist until it's empty
2994 * and the state is set to BUTCHER, or the state is set
2995 * to RELEASE.
db7bccf4
TH
2996 *
2997 * BUTCHER Command state which is set by the cpu callback after
2998 * the cpu has went down. Once this state is set trustee
2999 * knows that there will be no new works on the worklist
3000 * and once the worklist is empty it can proceed to
3001 * killing idle workers.
3002 *
3003 * RELEASE Command state which is set by the cpu callback if the
3004 * cpu down has been canceled or it has come online
3005 * again. After recognizing this state, trustee stops
e22bee78
TH
3006 * trying to drain or butcher and clears ROGUE, rebinds
3007 * all remaining workers back to the cpu and releases
3008 * manager role.
db7bccf4
TH
3009 *
3010 * DONE Trustee will enter this state after BUTCHER or RELEASE
3011 * is complete.
3012 *
3013 * trustee CPU draining
3014 * took over down complete
3015 * START -----------> IN_CHARGE -----------> BUTCHER -----------> DONE
3016 * | | ^
3017 * | CPU is back online v return workers |
3018 * ----------------> RELEASE --------------
3019 */
3020
3021/**
3022 * trustee_wait_event_timeout - timed event wait for trustee
3023 * @cond: condition to wait for
3024 * @timeout: timeout in jiffies
3025 *
3026 * wait_event_timeout() for trustee to use. Handles locking and
3027 * checks for RELEASE request.
3028 *
3029 * CONTEXT:
3030 * spin_lock_irq(gcwq->lock) which may be released and regrabbed
3031 * multiple times. To be used by trustee.
3032 *
3033 * RETURNS:
3034 * Positive indicating left time if @cond is satisfied, 0 if timed
3035 * out, -1 if canceled.
3036 */
3037#define trustee_wait_event_timeout(cond, timeout) ({ \
3038 long __ret = (timeout); \
3039 while (!((cond) || (gcwq->trustee_state == TRUSTEE_RELEASE)) && \
3040 __ret) { \
3041 spin_unlock_irq(&gcwq->lock); \
3042 __wait_event_timeout(gcwq->trustee_wait, (cond) || \
3043 (gcwq->trustee_state == TRUSTEE_RELEASE), \
3044 __ret); \
3045 spin_lock_irq(&gcwq->lock); \
3046 } \
3047 gcwq->trustee_state == TRUSTEE_RELEASE ? -1 : (__ret); \
3048})
3049
3050/**
3051 * trustee_wait_event - event wait for trustee
3052 * @cond: condition to wait for
3053 *
3054 * wait_event() for trustee to use. Automatically handles locking and
3055 * checks for CANCEL request.
3056 *
3057 * CONTEXT:
3058 * spin_lock_irq(gcwq->lock) which may be released and regrabbed
3059 * multiple times. To be used by trustee.
3060 *
3061 * RETURNS:
3062 * 0 if @cond is satisfied, -1 if canceled.
3063 */
3064#define trustee_wait_event(cond) ({ \
3065 long __ret1; \
3066 __ret1 = trustee_wait_event_timeout(cond, MAX_SCHEDULE_TIMEOUT);\
3067 __ret1 < 0 ? -1 : 0; \
3068})
3069
3070static int __cpuinit trustee_thread(void *__gcwq)
3071{
3072 struct global_cwq *gcwq = __gcwq;
3073 struct worker *worker;
e22bee78 3074 struct work_struct *work;
db7bccf4 3075 struct hlist_node *pos;
e22bee78 3076 long rc;
db7bccf4
TH
3077 int i;
3078
3079 BUG_ON(gcwq->cpu != smp_processor_id());
3080
3081 spin_lock_irq(&gcwq->lock);
3082 /*
e22bee78
TH
3083 * Claim the manager position and make all workers rogue.
3084 * Trustee must be bound to the target cpu and can't be
3085 * cancelled.
db7bccf4
TH
3086 */
3087 BUG_ON(gcwq->cpu != smp_processor_id());
e22bee78
TH
3088 rc = trustee_wait_event(!(gcwq->flags & GCWQ_MANAGING_WORKERS));
3089 BUG_ON(rc < 0);
3090
3091 gcwq->flags |= GCWQ_MANAGING_WORKERS;
db7bccf4
TH
3092
3093 list_for_each_entry(worker, &gcwq->idle_list, entry)
cb444766 3094 worker->flags |= WORKER_ROGUE;
db7bccf4
TH
3095
3096 for_each_busy_worker(worker, i, pos, gcwq)
cb444766 3097 worker->flags |= WORKER_ROGUE;
db7bccf4 3098
e22bee78
TH
3099 /*
3100 * Call schedule() so that we cross rq->lock and thus can
3101 * guarantee sched callbacks see the rogue flag. This is
3102 * necessary as scheduler callbacks may be invoked from other
3103 * cpus.
3104 */
3105 spin_unlock_irq(&gcwq->lock);
3106 schedule();
3107 spin_lock_irq(&gcwq->lock);
3108
3109 /*
cb444766
TH
3110 * Sched callbacks are disabled now. Zap nr_running. After
3111 * this, nr_running stays zero and need_more_worker() and
3112 * keep_working() are always true as long as the worklist is
3113 * not empty.
e22bee78 3114 */
cb444766 3115 atomic_set(get_gcwq_nr_running(gcwq->cpu), 0);
e22bee78
TH
3116
3117 spin_unlock_irq(&gcwq->lock);
3118 del_timer_sync(&gcwq->idle_timer);
3119 spin_lock_irq(&gcwq->lock);
3120
db7bccf4
TH
3121 /*
3122 * We're now in charge. Notify and proceed to drain. We need
3123 * to keep the gcwq running during the whole CPU down
3124 * procedure as other cpu hotunplug callbacks may need to
3125 * flush currently running tasks.
3126 */
3127 gcwq->trustee_state = TRUSTEE_IN_CHARGE;
3128 wake_up_all(&gcwq->trustee_wait);
3129
3130 /*
3131 * The original cpu is in the process of dying and may go away
3132 * anytime now. When that happens, we and all workers would
e22bee78
TH
3133 * be migrated to other cpus. Try draining any left work. We
3134 * want to get it over with ASAP - spam rescuers, wake up as
3135 * many idlers as necessary and create new ones till the
3136 * worklist is empty. Note that if the gcwq is frozen, there
3137 * may be frozen works in freezeable cwqs. Don't declare
3138 * completion while frozen.
db7bccf4
TH
3139 */
3140 while (gcwq->nr_workers != gcwq->nr_idle ||
3141 gcwq->flags & GCWQ_FREEZING ||
3142 gcwq->trustee_state == TRUSTEE_IN_CHARGE) {
e22bee78
TH
3143 int nr_works = 0;
3144
3145 list_for_each_entry(work, &gcwq->worklist, entry) {
3146 send_mayday(work);
3147 nr_works++;
3148 }
3149
3150 list_for_each_entry(worker, &gcwq->idle_list, entry) {
3151 if (!nr_works--)
3152 break;
3153 wake_up_process(worker->task);
3154 }
3155
3156 if (need_to_create_worker(gcwq)) {
3157 spin_unlock_irq(&gcwq->lock);
3158 worker = create_worker(gcwq, false);
3159 spin_lock_irq(&gcwq->lock);
3160 if (worker) {
cb444766 3161 worker->flags |= WORKER_ROGUE;
e22bee78
TH
3162 start_worker(worker);
3163 }
3164 }
3165
db7bccf4
TH
3166 /* give a breather */
3167 if (trustee_wait_event_timeout(false, TRUSTEE_COOLDOWN) < 0)
3168 break;
3169 }
3170
e22bee78
TH
3171 /*
3172 * Either all works have been scheduled and cpu is down, or
3173 * cpu down has already been canceled. Wait for and butcher
3174 * all workers till we're canceled.
3175 */
3176 do {
3177 rc = trustee_wait_event(!list_empty(&gcwq->idle_list));
3178 while (!list_empty(&gcwq->idle_list))
3179 destroy_worker(list_first_entry(&gcwq->idle_list,
3180 struct worker, entry));
3181 } while (gcwq->nr_workers && rc >= 0);
3182
3183 /*
3184 * At this point, either draining has completed and no worker
3185 * is left, or cpu down has been canceled or the cpu is being
3186 * brought back up. There shouldn't be any idle one left.
3187 * Tell the remaining busy ones to rebind once it finishes the
3188 * currently scheduled works by scheduling the rebind_work.
3189 */
3190 WARN_ON(!list_empty(&gcwq->idle_list));
3191
3192 for_each_busy_worker(worker, i, pos, gcwq) {
3193 struct work_struct *rebind_work = &worker->rebind_work;
3194
3195 /*
3196 * Rebind_work may race with future cpu hotplug
3197 * operations. Use a separate flag to mark that
3198 * rebinding is scheduled.
3199 */
cb444766
TH
3200 worker->flags |= WORKER_REBIND;
3201 worker->flags &= ~WORKER_ROGUE;
e22bee78
TH
3202
3203 /* queue rebind_work, wq doesn't matter, use the default one */
3204 if (test_and_set_bit(WORK_STRUCT_PENDING_BIT,
3205 work_data_bits(rebind_work)))
3206 continue;
3207
3208 debug_work_activate(rebind_work);
d320c038 3209 insert_work(get_cwq(gcwq->cpu, system_wq), rebind_work,
e22bee78
TH
3210 worker->scheduled.next,
3211 work_color_to_flags(WORK_NO_COLOR));
3212 }
3213
3214 /* relinquish manager role */
3215 gcwq->flags &= ~GCWQ_MANAGING_WORKERS;
3216
db7bccf4
TH
3217 /* notify completion */
3218 gcwq->trustee = NULL;
3219 gcwq->trustee_state = TRUSTEE_DONE;
3220 wake_up_all(&gcwq->trustee_wait);
3221 spin_unlock_irq(&gcwq->lock);
3222 return 0;
3223}
3224
3225/**
3226 * wait_trustee_state - wait for trustee to enter the specified state
3227 * @gcwq: gcwq the trustee of interest belongs to
3228 * @state: target state to wait for
3229 *
3230 * Wait for the trustee to reach @state. DONE is already matched.
3231 *
3232 * CONTEXT:
3233 * spin_lock_irq(gcwq->lock) which may be released and regrabbed
3234 * multiple times. To be used by cpu_callback.
3235 */
3236static void __cpuinit wait_trustee_state(struct global_cwq *gcwq, int state)
3237{
3238 if (!(gcwq->trustee_state == state ||
3239 gcwq->trustee_state == TRUSTEE_DONE)) {
3240 spin_unlock_irq(&gcwq->lock);
3241 __wait_event(gcwq->trustee_wait,
3242 gcwq->trustee_state == state ||
3243 gcwq->trustee_state == TRUSTEE_DONE);
3244 spin_lock_irq(&gcwq->lock);
3245 }
3246}
3247
3af24433
ON
3248static int __devinit workqueue_cpu_callback(struct notifier_block *nfb,
3249 unsigned long action,
3250 void *hcpu)
3251{
3252 unsigned int cpu = (unsigned long)hcpu;
db7bccf4
TH
3253 struct global_cwq *gcwq = get_gcwq(cpu);
3254 struct task_struct *new_trustee = NULL;
e22bee78 3255 struct worker *uninitialized_var(new_worker);
db7bccf4 3256 unsigned long flags;
3af24433 3257
8bb78442
RW
3258 action &= ~CPU_TASKS_FROZEN;
3259
db7bccf4
TH
3260 switch (action) {
3261 case CPU_DOWN_PREPARE:
3262 new_trustee = kthread_create(trustee_thread, gcwq,
3263 "workqueue_trustee/%d\n", cpu);
3264 if (IS_ERR(new_trustee))
3265 return notifier_from_errno(PTR_ERR(new_trustee));
3266 kthread_bind(new_trustee, cpu);
e22bee78
TH
3267 /* fall through */
3268 case CPU_UP_PREPARE:
3269 BUG_ON(gcwq->first_idle);
3270 new_worker = create_worker(gcwq, false);
3271 if (!new_worker) {
3272 if (new_trustee)
3273 kthread_stop(new_trustee);
3274 return NOTIFY_BAD;
3275 }
db7bccf4 3276 }
3af24433 3277
db7bccf4
TH
3278 /* some are called w/ irq disabled, don't disturb irq status */
3279 spin_lock_irqsave(&gcwq->lock, flags);
3af24433 3280
db7bccf4
TH
3281 switch (action) {
3282 case CPU_DOWN_PREPARE:
3283 /* initialize trustee and tell it to acquire the gcwq */
3284 BUG_ON(gcwq->trustee || gcwq->trustee_state != TRUSTEE_DONE);
3285 gcwq->trustee = new_trustee;
3286 gcwq->trustee_state = TRUSTEE_START;
3287 wake_up_process(gcwq->trustee);
3288 wait_trustee_state(gcwq, TRUSTEE_IN_CHARGE);
e22bee78
TH
3289 /* fall through */
3290 case CPU_UP_PREPARE:
3291 BUG_ON(gcwq->first_idle);
3292 gcwq->first_idle = new_worker;
3293 break;
3294
3295 case CPU_DYING:
3296 /*
3297 * Before this, the trustee and all workers except for
3298 * the ones which are still executing works from
3299 * before the last CPU down must be on the cpu. After
3300 * this, they'll all be diasporas.
3301 */
3302 gcwq->flags |= GCWQ_DISASSOCIATED;
db7bccf4
TH
3303 break;
3304
3305 case CPU_POST_DEAD:
3306 gcwq->trustee_state = TRUSTEE_BUTCHER;
e22bee78
TH
3307 /* fall through */
3308 case CPU_UP_CANCELED:
3309 destroy_worker(gcwq->first_idle);
3310 gcwq->first_idle = NULL;
db7bccf4
TH
3311 break;
3312
3313 case CPU_DOWN_FAILED:
3314 case CPU_ONLINE:
e22bee78 3315 gcwq->flags &= ~GCWQ_DISASSOCIATED;
db7bccf4
TH
3316 if (gcwq->trustee_state != TRUSTEE_DONE) {
3317 gcwq->trustee_state = TRUSTEE_RELEASE;
3318 wake_up_process(gcwq->trustee);
3319 wait_trustee_state(gcwq, TRUSTEE_DONE);
3af24433 3320 }
db7bccf4 3321
e22bee78
TH
3322 /*
3323 * Trustee is done and there might be no worker left.
3324 * Put the first_idle in and request a real manager to
3325 * take a look.
3326 */
3327 spin_unlock_irq(&gcwq->lock);
3328 kthread_bind(gcwq->first_idle->task, cpu);
3329 spin_lock_irq(&gcwq->lock);
3330 gcwq->flags |= GCWQ_MANAGE_WORKERS;
3331 start_worker(gcwq->first_idle);
3332 gcwq->first_idle = NULL;
db7bccf4 3333 break;
1da177e4
LT
3334 }
3335
db7bccf4
TH
3336 spin_unlock_irqrestore(&gcwq->lock, flags);
3337
1537663f 3338 return notifier_from_errno(0);
1da177e4 3339}
1da177e4 3340
2d3854a3 3341#ifdef CONFIG_SMP
8ccad40d 3342
2d3854a3 3343struct work_for_cpu {
6b44003e 3344 struct completion completion;
2d3854a3
RR
3345 long (*fn)(void *);
3346 void *arg;
3347 long ret;
3348};
3349
6b44003e 3350static int do_work_for_cpu(void *_wfc)
2d3854a3 3351{
6b44003e 3352 struct work_for_cpu *wfc = _wfc;
2d3854a3 3353 wfc->ret = wfc->fn(wfc->arg);
6b44003e
AM
3354 complete(&wfc->completion);
3355 return 0;
2d3854a3
RR
3356}
3357
3358/**
3359 * work_on_cpu - run a function in user context on a particular cpu
3360 * @cpu: the cpu to run on
3361 * @fn: the function to run
3362 * @arg: the function arg
3363 *
31ad9081
RR
3364 * This will return the value @fn returns.
3365 * It is up to the caller to ensure that the cpu doesn't go offline.
6b44003e 3366 * The caller must not hold any locks which would prevent @fn from completing.
2d3854a3
RR
3367 */
3368long work_on_cpu(unsigned int cpu, long (*fn)(void *), void *arg)
3369{
6b44003e
AM
3370 struct task_struct *sub_thread;
3371 struct work_for_cpu wfc = {
3372 .completion = COMPLETION_INITIALIZER_ONSTACK(wfc.completion),
3373 .fn = fn,
3374 .arg = arg,
3375 };
3376
3377 sub_thread = kthread_create(do_work_for_cpu, &wfc, "work_for_cpu");
3378 if (IS_ERR(sub_thread))
3379 return PTR_ERR(sub_thread);
3380 kthread_bind(sub_thread, cpu);
3381 wake_up_process(sub_thread);
3382 wait_for_completion(&wfc.completion);
2d3854a3
RR
3383 return wfc.ret;
3384}
3385EXPORT_SYMBOL_GPL(work_on_cpu);
3386#endif /* CONFIG_SMP */
3387
a0a1a5fd
TH
3388#ifdef CONFIG_FREEZER
3389
3390/**
3391 * freeze_workqueues_begin - begin freezing workqueues
3392 *
3393 * Start freezing workqueues. After this function returns, all
3394 * freezeable workqueues will queue new works to their frozen_works
7e11629d 3395 * list instead of gcwq->worklist.
a0a1a5fd
TH
3396 *
3397 * CONTEXT:
8b03ae3c 3398 * Grabs and releases workqueue_lock and gcwq->lock's.
a0a1a5fd
TH
3399 */
3400void freeze_workqueues_begin(void)
3401{
a0a1a5fd
TH
3402 unsigned int cpu;
3403
3404 spin_lock(&workqueue_lock);
3405
3406 BUG_ON(workqueue_freezing);
3407 workqueue_freezing = true;
3408
f3421797 3409 for_each_gcwq_cpu(cpu) {
8b03ae3c 3410 struct global_cwq *gcwq = get_gcwq(cpu);
bdbc5dd7 3411 struct workqueue_struct *wq;
8b03ae3c
TH
3412
3413 spin_lock_irq(&gcwq->lock);
3414
db7bccf4
TH
3415 BUG_ON(gcwq->flags & GCWQ_FREEZING);
3416 gcwq->flags |= GCWQ_FREEZING;
3417
a0a1a5fd
TH
3418 list_for_each_entry(wq, &workqueues, list) {
3419 struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
3420
f3421797 3421 if (cwq && wq->flags & WQ_FREEZEABLE)
a0a1a5fd 3422 cwq->max_active = 0;
a0a1a5fd 3423 }
8b03ae3c
TH
3424
3425 spin_unlock_irq(&gcwq->lock);
a0a1a5fd
TH
3426 }
3427
3428 spin_unlock(&workqueue_lock);
3429}
3430
3431/**
3432 * freeze_workqueues_busy - are freezeable workqueues still busy?
3433 *
3434 * Check whether freezing is complete. This function must be called
3435 * between freeze_workqueues_begin() and thaw_workqueues().
3436 *
3437 * CONTEXT:
3438 * Grabs and releases workqueue_lock.
3439 *
3440 * RETURNS:
3441 * %true if some freezeable workqueues are still busy. %false if
3442 * freezing is complete.
3443 */
3444bool freeze_workqueues_busy(void)
3445{
a0a1a5fd
TH
3446 unsigned int cpu;
3447 bool busy = false;
3448
3449 spin_lock(&workqueue_lock);
3450
3451 BUG_ON(!workqueue_freezing);
3452
f3421797 3453 for_each_gcwq_cpu(cpu) {
bdbc5dd7 3454 struct workqueue_struct *wq;
a0a1a5fd
TH
3455 /*
3456 * nr_active is monotonically decreasing. It's safe
3457 * to peek without lock.
3458 */
3459 list_for_each_entry(wq, &workqueues, list) {
3460 struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
3461
f3421797 3462 if (!cwq || !(wq->flags & WQ_FREEZEABLE))
a0a1a5fd
TH
3463 continue;
3464
3465 BUG_ON(cwq->nr_active < 0);
3466 if (cwq->nr_active) {
3467 busy = true;
3468 goto out_unlock;
3469 }
3470 }
3471 }
3472out_unlock:
3473 spin_unlock(&workqueue_lock);
3474 return busy;
3475}
3476
3477/**
3478 * thaw_workqueues - thaw workqueues
3479 *
3480 * Thaw workqueues. Normal queueing is restored and all collected
7e11629d 3481 * frozen works are transferred to their respective gcwq worklists.
a0a1a5fd
TH
3482 *
3483 * CONTEXT:
8b03ae3c 3484 * Grabs and releases workqueue_lock and gcwq->lock's.
a0a1a5fd
TH
3485 */
3486void thaw_workqueues(void)
3487{
a0a1a5fd
TH
3488 unsigned int cpu;
3489
3490 spin_lock(&workqueue_lock);
3491
3492 if (!workqueue_freezing)
3493 goto out_unlock;
3494
f3421797 3495 for_each_gcwq_cpu(cpu) {
8b03ae3c 3496 struct global_cwq *gcwq = get_gcwq(cpu);
bdbc5dd7 3497 struct workqueue_struct *wq;
8b03ae3c
TH
3498
3499 spin_lock_irq(&gcwq->lock);
3500
db7bccf4
TH
3501 BUG_ON(!(gcwq->flags & GCWQ_FREEZING));
3502 gcwq->flags &= ~GCWQ_FREEZING;
3503
a0a1a5fd
TH
3504 list_for_each_entry(wq, &workqueues, list) {
3505 struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq);
3506
f3421797 3507 if (!cwq || !(wq->flags & WQ_FREEZEABLE))
a0a1a5fd
TH
3508 continue;
3509
a0a1a5fd
TH
3510 /* restore max_active and repopulate worklist */
3511 cwq->max_active = wq->saved_max_active;
3512
3513 while (!list_empty(&cwq->delayed_works) &&
3514 cwq->nr_active < cwq->max_active)
3515 cwq_activate_first_delayed(cwq);
3516
502ca9d8
TH
3517 /* perform delayed unbind from single cpu if empty */
3518 if (wq->single_cpu == gcwq->cpu &&
3519 !cwq->nr_active && list_empty(&cwq->delayed_works))
3520 cwq_unbind_single_cpu(cwq);
a0a1a5fd 3521 }
8b03ae3c 3522
e22bee78
TH
3523 wake_up_worker(gcwq);
3524
8b03ae3c 3525 spin_unlock_irq(&gcwq->lock);
a0a1a5fd
TH
3526 }
3527
3528 workqueue_freezing = false;
3529out_unlock:
3530 spin_unlock(&workqueue_lock);
3531}
3532#endif /* CONFIG_FREEZER */
3533
c12920d1 3534void __init init_workqueues(void)
1da177e4 3535{
c34056a3 3536 unsigned int cpu;
c8e55f36 3537 int i;
c34056a3 3538
7a22ad75
TH
3539 /*
3540 * The pointer part of work->data is either pointing to the
3541 * cwq or contains the cpu number the work ran last on. Make
3542 * sure cpu number won't overflow into kernel pointer area so
3543 * that they can be distinguished.
3544 */
bdbc5dd7 3545 BUILD_BUG_ON(WORK_CPU_LAST << WORK_STRUCT_FLAG_BITS >= PAGE_OFFSET);
7a22ad75 3546
db7bccf4 3547 hotcpu_notifier(workqueue_cpu_callback, CPU_PRI_WORKQUEUE);
8b03ae3c
TH
3548
3549 /* initialize gcwqs */
f3421797 3550 for_each_gcwq_cpu(cpu) {
8b03ae3c
TH
3551 struct global_cwq *gcwq = get_gcwq(cpu);
3552
3553 spin_lock_init(&gcwq->lock);
7e11629d 3554 INIT_LIST_HEAD(&gcwq->worklist);
8b03ae3c 3555 gcwq->cpu = cpu;
f3421797
TH
3556 if (cpu == WORK_CPU_UNBOUND)
3557 gcwq->flags |= GCWQ_DISASSOCIATED;
8b03ae3c 3558
c8e55f36
TH
3559 INIT_LIST_HEAD(&gcwq->idle_list);
3560 for (i = 0; i < BUSY_WORKER_HASH_SIZE; i++)
3561 INIT_HLIST_HEAD(&gcwq->busy_hash[i]);
3562
e22bee78
TH
3563 init_timer_deferrable(&gcwq->idle_timer);
3564 gcwq->idle_timer.function = idle_worker_timeout;
3565 gcwq->idle_timer.data = (unsigned long)gcwq;
3566
3567 setup_timer(&gcwq->mayday_timer, gcwq_mayday_timeout,
3568 (unsigned long)gcwq);
3569
8b03ae3c 3570 ida_init(&gcwq->worker_ida);
db7bccf4
TH
3571
3572 gcwq->trustee_state = TRUSTEE_DONE;
3573 init_waitqueue_head(&gcwq->trustee_wait);
8b03ae3c
TH
3574 }
3575
e22bee78 3576 /* create the initial worker */
f3421797 3577 for_each_online_gcwq_cpu(cpu) {
e22bee78
TH
3578 struct global_cwq *gcwq = get_gcwq(cpu);
3579 struct worker *worker;
3580
3581 worker = create_worker(gcwq, true);
3582 BUG_ON(!worker);
3583 spin_lock_irq(&gcwq->lock);
3584 start_worker(worker);
3585 spin_unlock_irq(&gcwq->lock);
3586 }
3587
d320c038
TH
3588 system_wq = alloc_workqueue("events", 0, 0);
3589 system_long_wq = alloc_workqueue("events_long", 0, 0);
3590 system_nrt_wq = alloc_workqueue("events_nrt", WQ_NON_REENTRANT, 0);
f3421797
TH
3591 system_unbound_wq = alloc_workqueue("events_unbound", WQ_UNBOUND,
3592 WQ_UNBOUND_MAX_ACTIVE);
d320c038 3593 BUG_ON(!system_wq || !system_long_wq || !system_nrt_wq);
1da177e4 3594}
This page took 0.66799 seconds and 5 git commands to generate.