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