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