aboutsummaryrefslogtreecommitdiffstats
path: root/kernel/workqueue.c
diff options
context:
space:
mode:
Diffstat (limited to 'kernel/workqueue.c')
-rw-r--r--kernel/workqueue.c2964
1 files changed, 2083 insertions, 881 deletions
diff --git a/kernel/workqueue.c b/kernel/workqueue.c
index 55fac5b991b7..ee8e29a2320c 100644
--- a/kernel/workqueue.c
+++ b/kernel/workqueue.c
@@ -41,7 +41,12 @@
41#include <linux/debug_locks.h> 41#include <linux/debug_locks.h>
42#include <linux/lockdep.h> 42#include <linux/lockdep.h>
43#include <linux/idr.h> 43#include <linux/idr.h>
44#include <linux/jhash.h>
44#include <linux/hashtable.h> 45#include <linux/hashtable.h>
46#include <linux/rculist.h>
47#include <linux/nodemask.h>
48#include <linux/moduleparam.h>
49#include <linux/uaccess.h>
45 50
46#include "workqueue_internal.h" 51#include "workqueue_internal.h"
47 52
@@ -58,12 +63,11 @@ enum {
58 * %WORKER_UNBOUND set and concurrency management disabled, and may 63 * %WORKER_UNBOUND set and concurrency management disabled, and may
59 * be executing on any CPU. The pool behaves as an unbound one. 64 * be executing on any CPU. The pool behaves as an unbound one.
60 * 65 *
61 * Note that DISASSOCIATED can be flipped only while holding 66 * Note that DISASSOCIATED should be flipped only while holding
62 * assoc_mutex to avoid changing binding state while 67 * manager_mutex to avoid changing binding state while
63 * create_worker() is in progress. 68 * create_worker() is in progress.
64 */ 69 */
65 POOL_MANAGE_WORKERS = 1 << 0, /* need to manage workers */ 70 POOL_MANAGE_WORKERS = 1 << 0, /* need to manage workers */
66 POOL_MANAGING_WORKERS = 1 << 1, /* managing workers */
67 POOL_DISASSOCIATED = 1 << 2, /* cpu can't serve workers */ 71 POOL_DISASSOCIATED = 1 << 2, /* cpu can't serve workers */
68 POOL_FREEZING = 1 << 3, /* freeze in progress */ 72 POOL_FREEZING = 1 << 3, /* freeze in progress */
69 73
@@ -74,12 +78,14 @@ enum {
74 WORKER_PREP = 1 << 3, /* preparing to run works */ 78 WORKER_PREP = 1 << 3, /* preparing to run works */
75 WORKER_CPU_INTENSIVE = 1 << 6, /* cpu intensive */ 79 WORKER_CPU_INTENSIVE = 1 << 6, /* cpu intensive */
76 WORKER_UNBOUND = 1 << 7, /* worker is unbound */ 80 WORKER_UNBOUND = 1 << 7, /* worker is unbound */
81 WORKER_REBOUND = 1 << 8, /* worker was rebound */
77 82
78 WORKER_NOT_RUNNING = WORKER_PREP | WORKER_UNBOUND | 83 WORKER_NOT_RUNNING = WORKER_PREP | WORKER_CPU_INTENSIVE |
79 WORKER_CPU_INTENSIVE, 84 WORKER_UNBOUND | WORKER_REBOUND,
80 85
81 NR_STD_WORKER_POOLS = 2, /* # standard pools per cpu */ 86 NR_STD_WORKER_POOLS = 2, /* # standard pools per cpu */
82 87
88 UNBOUND_POOL_HASH_ORDER = 6, /* hashed by pool->attrs */
83 BUSY_WORKER_HASH_ORDER = 6, /* 64 pointers */ 89 BUSY_WORKER_HASH_ORDER = 6, /* 64 pointers */
84 90
85 MAX_IDLE_WORKERS_RATIO = 4, /* 1/4 of busy can be idle */ 91 MAX_IDLE_WORKERS_RATIO = 4, /* 1/4 of busy can be idle */
@@ -97,6 +103,8 @@ enum {
97 */ 103 */
98 RESCUER_NICE_LEVEL = -20, 104 RESCUER_NICE_LEVEL = -20,
99 HIGHPRI_NICE_LEVEL = -20, 105 HIGHPRI_NICE_LEVEL = -20,
106
107 WQ_NAME_LEN = 24,
100}; 108};
101 109
102/* 110/*
@@ -115,16 +123,26 @@ enum {
115 * cpu or grabbing pool->lock is enough for read access. If 123 * cpu or grabbing pool->lock is enough for read access. If
116 * POOL_DISASSOCIATED is set, it's identical to L. 124 * POOL_DISASSOCIATED is set, it's identical to L.
117 * 125 *
118 * F: wq->flush_mutex protected. 126 * MG: pool->manager_mutex and pool->lock protected. Writes require both
127 * locks. Reads can happen under either lock.
128 *
129 * PL: wq_pool_mutex protected.
130 *
131 * PR: wq_pool_mutex protected for writes. Sched-RCU protected for reads.
132 *
133 * WQ: wq->mutex protected.
119 * 134 *
120 * W: workqueue_lock protected. 135 * WR: wq->mutex protected for writes. Sched-RCU protected for reads.
136 *
137 * MD: wq_mayday_lock protected.
121 */ 138 */
122 139
123/* struct worker is defined in workqueue_internal.h */ 140/* struct worker is defined in workqueue_internal.h */
124 141
125struct worker_pool { 142struct worker_pool {
126 spinlock_t lock; /* the pool lock */ 143 spinlock_t lock; /* the pool lock */
127 unsigned int cpu; /* I: the associated cpu */ 144 int cpu; /* I: the associated cpu */
145 int node; /* I: the associated node ID */
128 int id; /* I: pool ID */ 146 int id; /* I: pool ID */
129 unsigned int flags; /* X: flags */ 147 unsigned int flags; /* X: flags */
130 148
@@ -138,12 +156,18 @@ struct worker_pool {
138 struct timer_list idle_timer; /* L: worker idle timeout */ 156 struct timer_list idle_timer; /* L: worker idle timeout */
139 struct timer_list mayday_timer; /* L: SOS timer for workers */ 157 struct timer_list mayday_timer; /* L: SOS timer for workers */
140 158
141 /* workers are chained either in busy_hash or idle_list */ 159 /* a workers is either on busy_hash or idle_list, or the manager */
142 DECLARE_HASHTABLE(busy_hash, BUSY_WORKER_HASH_ORDER); 160 DECLARE_HASHTABLE(busy_hash, BUSY_WORKER_HASH_ORDER);
143 /* L: hash of busy workers */ 161 /* L: hash of busy workers */
144 162
145 struct mutex assoc_mutex; /* protect POOL_DISASSOCIATED */ 163 /* see manage_workers() for details on the two manager mutexes */
146 struct ida worker_ida; /* L: for worker IDs */ 164 struct mutex manager_arb; /* manager arbitration */
165 struct mutex manager_mutex; /* manager exclusion */
166 struct idr worker_idr; /* MG: worker IDs and iteration */
167
168 struct workqueue_attrs *attrs; /* I: worker attributes */
169 struct hlist_node hash_node; /* PL: unbound_pool_hash node */
170 int refcnt; /* PL: refcnt for unbound pools */
147 171
148 /* 172 /*
149 * The current concurrency level. As it's likely to be accessed 173 * The current concurrency level. As it's likely to be accessed
@@ -151,6 +175,12 @@ struct worker_pool {
151 * cacheline. 175 * cacheline.
152 */ 176 */
153 atomic_t nr_running ____cacheline_aligned_in_smp; 177 atomic_t nr_running ____cacheline_aligned_in_smp;
178
179 /*
180 * Destruction of pool is sched-RCU protected to allow dereferences
181 * from get_work_pool().
182 */
183 struct rcu_head rcu;
154} ____cacheline_aligned_in_smp; 184} ____cacheline_aligned_in_smp;
155 185
156/* 186/*
@@ -164,77 +194,109 @@ struct pool_workqueue {
164 struct workqueue_struct *wq; /* I: the owning workqueue */ 194 struct workqueue_struct *wq; /* I: the owning workqueue */
165 int work_color; /* L: current color */ 195 int work_color; /* L: current color */
166 int flush_color; /* L: flushing color */ 196 int flush_color; /* L: flushing color */
197 int refcnt; /* L: reference count */
167 int nr_in_flight[WORK_NR_COLORS]; 198 int nr_in_flight[WORK_NR_COLORS];
168 /* L: nr of in_flight works */ 199 /* L: nr of in_flight works */
169 int nr_active; /* L: nr of active works */ 200 int nr_active; /* L: nr of active works */
170 int max_active; /* L: max active works */ 201 int max_active; /* L: max active works */
171 struct list_head delayed_works; /* L: delayed works */ 202 struct list_head delayed_works; /* L: delayed works */
172}; 203 struct list_head pwqs_node; /* WR: node on wq->pwqs */
204 struct list_head mayday_node; /* MD: node on wq->maydays */
205
206 /*
207 * Release of unbound pwq is punted to system_wq. See put_pwq()
208 * and pwq_unbound_release_workfn() for details. pool_workqueue
209 * itself is also sched-RCU protected so that the first pwq can be
210 * determined without grabbing wq->mutex.
211 */
212 struct work_struct unbound_release_work;
213 struct rcu_head rcu;
214} __aligned(1 << WORK_STRUCT_FLAG_BITS);
173 215
174/* 216/*
175 * Structure used to wait for workqueue flush. 217 * Structure used to wait for workqueue flush.
176 */ 218 */
177struct wq_flusher { 219struct wq_flusher {
178 struct list_head list; /* F: list of flushers */ 220 struct list_head list; /* WQ: list of flushers */
179 int flush_color; /* F: flush color waiting for */ 221 int flush_color; /* WQ: flush color waiting for */
180 struct completion done; /* flush completion */ 222 struct completion done; /* flush completion */
181}; 223};
182 224
183/* 225struct wq_device;
184 * All cpumasks are assumed to be always set on UP and thus can't be
185 * used to determine whether there's something to be done.
186 */
187#ifdef CONFIG_SMP
188typedef cpumask_var_t mayday_mask_t;
189#define mayday_test_and_set_cpu(cpu, mask) \
190 cpumask_test_and_set_cpu((cpu), (mask))
191#define mayday_clear_cpu(cpu, mask) cpumask_clear_cpu((cpu), (mask))
192#define for_each_mayday_cpu(cpu, mask) for_each_cpu((cpu), (mask))
193#define alloc_mayday_mask(maskp, gfp) zalloc_cpumask_var((maskp), (gfp))
194#define free_mayday_mask(mask) free_cpumask_var((mask))
195#else
196typedef unsigned long mayday_mask_t;
197#define mayday_test_and_set_cpu(cpu, mask) test_and_set_bit(0, &(mask))
198#define mayday_clear_cpu(cpu, mask) clear_bit(0, &(mask))
199#define for_each_mayday_cpu(cpu, mask) if ((cpu) = 0, (mask))
200#define alloc_mayday_mask(maskp, gfp) true
201#define free_mayday_mask(mask) do { } while (0)
202#endif
203 226
204/* 227/*
205 * The externally visible workqueue abstraction is an array of 228 * The externally visible workqueue. It relays the issued work items to
206 * per-CPU workqueues: 229 * the appropriate worker_pool through its pool_workqueues.
207 */ 230 */
208struct workqueue_struct { 231struct workqueue_struct {
209 unsigned int flags; /* W: WQ_* flags */ 232 struct list_head pwqs; /* WR: all pwqs of this wq */
210 union { 233 struct list_head list; /* PL: list of all workqueues */
211 struct pool_workqueue __percpu *pcpu; 234
212 struct pool_workqueue *single; 235 struct mutex mutex; /* protects this wq */
213 unsigned long v; 236 int work_color; /* WQ: current work color */
214 } pool_wq; /* I: pwq's */ 237 int flush_color; /* WQ: current flush color */
215 struct list_head list; /* W: list of all workqueues */
216
217 struct mutex flush_mutex; /* protects wq flushing */
218 int work_color; /* F: current work color */
219 int flush_color; /* F: current flush color */
220 atomic_t nr_pwqs_to_flush; /* flush in progress */ 238 atomic_t nr_pwqs_to_flush; /* flush in progress */
221 struct wq_flusher *first_flusher; /* F: first flusher */ 239 struct wq_flusher *first_flusher; /* WQ: first flusher */
222 struct list_head flusher_queue; /* F: flush waiters */ 240 struct list_head flusher_queue; /* WQ: flush waiters */
223 struct list_head flusher_overflow; /* F: flush overflow list */ 241 struct list_head flusher_overflow; /* WQ: flush overflow list */
224 242
225 mayday_mask_t mayday_mask; /* cpus requesting rescue */ 243 struct list_head maydays; /* MD: pwqs requesting rescue */
226 struct worker *rescuer; /* I: rescue worker */ 244 struct worker *rescuer; /* I: rescue worker */
227 245
228 int nr_drainers; /* W: drain in progress */ 246 int nr_drainers; /* WQ: drain in progress */
229 int saved_max_active; /* W: saved pwq max_active */ 247 int saved_max_active; /* WQ: saved pwq max_active */
248
249 struct workqueue_attrs *unbound_attrs; /* WQ: only for unbound wqs */
250 struct pool_workqueue *dfl_pwq; /* WQ: only for unbound wqs */
251
252#ifdef CONFIG_SYSFS
253 struct wq_device *wq_dev; /* I: for sysfs interface */
254#endif
230#ifdef CONFIG_LOCKDEP 255#ifdef CONFIG_LOCKDEP
231 struct lockdep_map lockdep_map; 256 struct lockdep_map lockdep_map;
232#endif 257#endif
233 char name[]; /* I: workqueue name */ 258 char name[WQ_NAME_LEN]; /* I: workqueue name */
259
260 /* hot fields used during command issue, aligned to cacheline */
261 unsigned int flags ____cacheline_aligned; /* WQ: WQ_* flags */
262 struct pool_workqueue __percpu *cpu_pwqs; /* I: per-cpu pwqs */
263 struct pool_workqueue __rcu *numa_pwq_tbl[]; /* FR: unbound pwqs indexed by node */
234}; 264};
235 265
266static struct kmem_cache *pwq_cache;
267
268static int wq_numa_tbl_len; /* highest possible NUMA node id + 1 */
269static cpumask_var_t *wq_numa_possible_cpumask;
270 /* possible CPUs of each node */
271
272static bool wq_disable_numa;
273module_param_named(disable_numa, wq_disable_numa, bool, 0444);
274
275static bool wq_numa_enabled; /* unbound NUMA affinity enabled */
276
277/* buf for wq_update_unbound_numa_attrs(), protected by CPU hotplug exclusion */
278static struct workqueue_attrs *wq_update_unbound_numa_attrs_buf;
279
280static DEFINE_MUTEX(wq_pool_mutex); /* protects pools and workqueues list */
281static DEFINE_SPINLOCK(wq_mayday_lock); /* protects wq->maydays list */
282
283static LIST_HEAD(workqueues); /* PL: list of all workqueues */
284static bool workqueue_freezing; /* PL: have wqs started freezing? */
285
286/* the per-cpu worker pools */
287static DEFINE_PER_CPU_SHARED_ALIGNED(struct worker_pool [NR_STD_WORKER_POOLS],
288 cpu_worker_pools);
289
290static DEFINE_IDR(worker_pool_idr); /* PR: idr of all pools */
291
292/* PL: hash of all unbound pools keyed by pool->attrs */
293static DEFINE_HASHTABLE(unbound_pool_hash, UNBOUND_POOL_HASH_ORDER);
294
295/* I: attributes used when instantiating standard unbound pools on demand */
296static struct workqueue_attrs *unbound_std_wq_attrs[NR_STD_WORKER_POOLS];
297
236struct workqueue_struct *system_wq __read_mostly; 298struct workqueue_struct *system_wq __read_mostly;
237EXPORT_SYMBOL_GPL(system_wq); 299EXPORT_SYMBOL(system_wq);
238struct workqueue_struct *system_highpri_wq __read_mostly; 300struct workqueue_struct *system_highpri_wq __read_mostly;
239EXPORT_SYMBOL_GPL(system_highpri_wq); 301EXPORT_SYMBOL_GPL(system_highpri_wq);
240struct workqueue_struct *system_long_wq __read_mostly; 302struct workqueue_struct *system_long_wq __read_mostly;
@@ -244,64 +306,87 @@ EXPORT_SYMBOL_GPL(system_unbound_wq);
244struct workqueue_struct *system_freezable_wq __read_mostly; 306struct workqueue_struct *system_freezable_wq __read_mostly;
245EXPORT_SYMBOL_GPL(system_freezable_wq); 307EXPORT_SYMBOL_GPL(system_freezable_wq);
246 308
309static int worker_thread(void *__worker);
310static void copy_workqueue_attrs(struct workqueue_attrs *to,
311 const struct workqueue_attrs *from);
312
247#define CREATE_TRACE_POINTS 313#define CREATE_TRACE_POINTS
248#include <trace/events/workqueue.h> 314#include <trace/events/workqueue.h>
249 315
250#define for_each_std_worker_pool(pool, cpu) \ 316#define assert_rcu_or_pool_mutex() \
251 for ((pool) = &std_worker_pools(cpu)[0]; \ 317 rcu_lockdep_assert(rcu_read_lock_sched_held() || \
252 (pool) < &std_worker_pools(cpu)[NR_STD_WORKER_POOLS]; (pool)++) 318 lockdep_is_held(&wq_pool_mutex), \
319 "sched RCU or wq_pool_mutex should be held")
253 320
254#define for_each_busy_worker(worker, i, pool) \ 321#define assert_rcu_or_wq_mutex(wq) \
255 hash_for_each(pool->busy_hash, i, worker, hentry) 322 rcu_lockdep_assert(rcu_read_lock_sched_held() || \
323 lockdep_is_held(&wq->mutex), \
324 "sched RCU or wq->mutex should be held")
256 325
257static inline int __next_wq_cpu(int cpu, const struct cpumask *mask, 326#ifdef CONFIG_LOCKDEP
258 unsigned int sw) 327#define assert_manager_or_pool_lock(pool) \
259{ 328 WARN_ONCE(debug_locks && \
260 if (cpu < nr_cpu_ids) { 329 !lockdep_is_held(&(pool)->manager_mutex) && \
261 if (sw & 1) { 330 !lockdep_is_held(&(pool)->lock), \
262 cpu = cpumask_next(cpu, mask); 331 "pool->manager_mutex or ->lock should be held")
263 if (cpu < nr_cpu_ids) 332#else
264 return cpu; 333#define assert_manager_or_pool_lock(pool) do { } while (0)
265 } 334#endif
266 if (sw & 2)
267 return WORK_CPU_UNBOUND;
268 }
269 return WORK_CPU_END;
270}
271 335
272static inline int __next_pwq_cpu(int cpu, const struct cpumask *mask, 336#define for_each_cpu_worker_pool(pool, cpu) \
273 struct workqueue_struct *wq) 337 for ((pool) = &per_cpu(cpu_worker_pools, cpu)[0]; \
274{ 338 (pool) < &per_cpu(cpu_worker_pools, cpu)[NR_STD_WORKER_POOLS]; \
275 return __next_wq_cpu(cpu, mask, !(wq->flags & WQ_UNBOUND) ? 1 : 2); 339 (pool)++)
276}
277 340
278/* 341/**
279 * CPU iterators 342 * for_each_pool - iterate through all worker_pools in the system
343 * @pool: iteration cursor
344 * @pi: integer used for iteration
280 * 345 *
281 * An extra cpu number is defined using an invalid cpu number 346 * This must be called either with wq_pool_mutex held or sched RCU read
282 * (WORK_CPU_UNBOUND) to host workqueues which are not bound to any 347 * locked. If the pool needs to be used beyond the locking in effect, the
283 * specific CPU. The following iterators are similar to for_each_*_cpu() 348 * caller is responsible for guaranteeing that the pool stays online.
284 * iterators but also considers the unbound CPU.
285 * 349 *
286 * for_each_wq_cpu() : possible CPUs + WORK_CPU_UNBOUND 350 * The if/else clause exists only for the lockdep assertion and can be
287 * for_each_online_wq_cpu() : online CPUs + WORK_CPU_UNBOUND 351 * ignored.
288 * for_each_pwq_cpu() : possible CPUs for bound workqueues,
289 * WORK_CPU_UNBOUND for unbound workqueues
290 */ 352 */
291#define for_each_wq_cpu(cpu) \ 353#define for_each_pool(pool, pi) \
292 for ((cpu) = __next_wq_cpu(-1, cpu_possible_mask, 3); \ 354 idr_for_each_entry(&worker_pool_idr, pool, pi) \
293 (cpu) < WORK_CPU_END; \ 355 if (({ assert_rcu_or_pool_mutex(); false; })) { } \
294 (cpu) = __next_wq_cpu((cpu), cpu_possible_mask, 3)) 356 else
295 357
296#define for_each_online_wq_cpu(cpu) \ 358/**
297 for ((cpu) = __next_wq_cpu(-1, cpu_online_mask, 3); \ 359 * for_each_pool_worker - iterate through all workers of a worker_pool
298 (cpu) < WORK_CPU_END; \ 360 * @worker: iteration cursor
299 (cpu) = __next_wq_cpu((cpu), cpu_online_mask, 3)) 361 * @wi: integer used for iteration
362 * @pool: worker_pool to iterate workers of
363 *
364 * This must be called with either @pool->manager_mutex or ->lock held.
365 *
366 * The if/else clause exists only for the lockdep assertion and can be
367 * ignored.
368 */
369#define for_each_pool_worker(worker, wi, pool) \
370 idr_for_each_entry(&(pool)->worker_idr, (worker), (wi)) \
371 if (({ assert_manager_or_pool_lock((pool)); false; })) { } \
372 else
300 373
301#define for_each_pwq_cpu(cpu, wq) \ 374/**
302 for ((cpu) = __next_pwq_cpu(-1, cpu_possible_mask, (wq)); \ 375 * for_each_pwq - iterate through all pool_workqueues of the specified workqueue
303 (cpu) < WORK_CPU_END; \ 376 * @pwq: iteration cursor
304 (cpu) = __next_pwq_cpu((cpu), cpu_possible_mask, (wq))) 377 * @wq: the target workqueue
378 *
379 * This must be called either with wq->mutex held or sched RCU read locked.
380 * If the pwq needs to be used beyond the locking in effect, the caller is
381 * responsible for guaranteeing that the pwq stays online.
382 *
383 * The if/else clause exists only for the lockdep assertion and can be
384 * ignored.
385 */
386#define for_each_pwq(pwq, wq) \
387 list_for_each_entry_rcu((pwq), &(wq)->pwqs, pwqs_node) \
388 if (({ assert_rcu_or_wq_mutex(wq); false; })) { } \
389 else
305 390
306#ifdef CONFIG_DEBUG_OBJECTS_WORK 391#ifdef CONFIG_DEBUG_OBJECTS_WORK
307 392
@@ -419,77 +504,35 @@ static inline void debug_work_activate(struct work_struct *work) { }
419static inline void debug_work_deactivate(struct work_struct *work) { } 504static inline void debug_work_deactivate(struct work_struct *work) { }
420#endif 505#endif
421 506
422/* Serializes the accesses to the list of workqueues. */
423static DEFINE_SPINLOCK(workqueue_lock);
424static LIST_HEAD(workqueues);
425static bool workqueue_freezing; /* W: have wqs started freezing? */
426
427/*
428 * The CPU and unbound standard worker pools. The unbound ones have
429 * POOL_DISASSOCIATED set, and their workers have WORKER_UNBOUND set.
430 */
431static DEFINE_PER_CPU_SHARED_ALIGNED(struct worker_pool [NR_STD_WORKER_POOLS],
432 cpu_std_worker_pools);
433static struct worker_pool unbound_std_worker_pools[NR_STD_WORKER_POOLS];
434
435/* idr of all pools */
436static DEFINE_MUTEX(worker_pool_idr_mutex);
437static DEFINE_IDR(worker_pool_idr);
438
439static int worker_thread(void *__worker);
440
441static struct worker_pool *std_worker_pools(int cpu)
442{
443 if (cpu != WORK_CPU_UNBOUND)
444 return per_cpu(cpu_std_worker_pools, cpu);
445 else
446 return unbound_std_worker_pools;
447}
448
449static int std_worker_pool_pri(struct worker_pool *pool)
450{
451 return pool - std_worker_pools(pool->cpu);
452}
453
454/* allocate ID and assign it to @pool */ 507/* allocate ID and assign it to @pool */
455static int worker_pool_assign_id(struct worker_pool *pool) 508static int worker_pool_assign_id(struct worker_pool *pool)
456{ 509{
457 int ret; 510 int ret;
458 511
459 mutex_lock(&worker_pool_idr_mutex); 512 lockdep_assert_held(&wq_pool_mutex);
513
460 ret = idr_alloc(&worker_pool_idr, pool, 0, 0, GFP_KERNEL); 514 ret = idr_alloc(&worker_pool_idr, pool, 0, 0, GFP_KERNEL);
461 if (ret >= 0) 515 if (ret >= 0) {
462 pool->id = ret; 516 pool->id = ret;
463 mutex_unlock(&worker_pool_idr_mutex); 517 return 0;
464 518 }
465 return ret < 0 ? ret : 0; 519 return ret;
466} 520}
467 521
468/* 522/**
469 * Lookup worker_pool by id. The idr currently is built during boot and 523 * unbound_pwq_by_node - return the unbound pool_workqueue for the given node
470 * never modified. Don't worry about locking for now. 524 * @wq: the target workqueue
525 * @node: the node ID
526 *
527 * This must be called either with pwq_lock held or sched RCU read locked.
528 * If the pwq needs to be used beyond the locking in effect, the caller is
529 * responsible for guaranteeing that the pwq stays online.
471 */ 530 */
472static struct worker_pool *worker_pool_by_id(int pool_id) 531static struct pool_workqueue *unbound_pwq_by_node(struct workqueue_struct *wq,
532 int node)
473{ 533{
474 return idr_find(&worker_pool_idr, pool_id); 534 assert_rcu_or_wq_mutex(wq);
475} 535 return rcu_dereference_raw(wq->numa_pwq_tbl[node]);
476
477static struct worker_pool *get_std_worker_pool(int cpu, bool highpri)
478{
479 struct worker_pool *pools = std_worker_pools(cpu);
480
481 return &pools[highpri];
482}
483
484static struct pool_workqueue *get_pwq(unsigned int cpu,
485 struct workqueue_struct *wq)
486{
487 if (!(wq->flags & WQ_UNBOUND)) {
488 if (likely(cpu < nr_cpu_ids))
489 return per_cpu_ptr(wq->pool_wq.pcpu, cpu);
490 } else if (likely(cpu == WORK_CPU_UNBOUND))
491 return wq->pool_wq.single;
492 return NULL;
493} 536}
494 537
495static unsigned int work_color_to_flags(int color) 538static unsigned int work_color_to_flags(int color)
@@ -531,7 +574,7 @@ static int work_next_color(int color)
531static inline void set_work_data(struct work_struct *work, unsigned long data, 574static inline void set_work_data(struct work_struct *work, unsigned long data,
532 unsigned long flags) 575 unsigned long flags)
533{ 576{
534 BUG_ON(!work_pending(work)); 577 WARN_ON_ONCE(!work_pending(work));
535 atomic_long_set(&work->data, data | flags | work_static(work)); 578 atomic_long_set(&work->data, data | flags | work_static(work));
536} 579}
537 580
@@ -583,13 +626,23 @@ static struct pool_workqueue *get_work_pwq(struct work_struct *work)
583 * @work: the work item of interest 626 * @work: the work item of interest
584 * 627 *
585 * Return the worker_pool @work was last associated with. %NULL if none. 628 * Return the worker_pool @work was last associated with. %NULL if none.
629 *
630 * Pools are created and destroyed under wq_pool_mutex, and allows read
631 * access under sched-RCU read lock. As such, this function should be
632 * called under wq_pool_mutex or with preemption disabled.
633 *
634 * All fields of the returned pool are accessible as long as the above
635 * mentioned locking is in effect. If the returned pool needs to be used
636 * beyond the critical section, the caller is responsible for ensuring the
637 * returned pool is and stays online.
586 */ 638 */
587static struct worker_pool *get_work_pool(struct work_struct *work) 639static struct worker_pool *get_work_pool(struct work_struct *work)
588{ 640{
589 unsigned long data = atomic_long_read(&work->data); 641 unsigned long data = atomic_long_read(&work->data);
590 struct worker_pool *pool;
591 int pool_id; 642 int pool_id;
592 643
644 assert_rcu_or_pool_mutex();
645
593 if (data & WORK_STRUCT_PWQ) 646 if (data & WORK_STRUCT_PWQ)
594 return ((struct pool_workqueue *) 647 return ((struct pool_workqueue *)
595 (data & WORK_STRUCT_WQ_DATA_MASK))->pool; 648 (data & WORK_STRUCT_WQ_DATA_MASK))->pool;
@@ -598,9 +651,7 @@ static struct worker_pool *get_work_pool(struct work_struct *work)
598 if (pool_id == WORK_OFFQ_POOL_NONE) 651 if (pool_id == WORK_OFFQ_POOL_NONE)
599 return NULL; 652 return NULL;
600 653
601 pool = worker_pool_by_id(pool_id); 654 return idr_find(&worker_pool_idr, pool_id);
602 WARN_ON_ONCE(!pool);
603 return pool;
604} 655}
605 656
606/** 657/**
@@ -689,7 +740,7 @@ static bool need_to_manage_workers(struct worker_pool *pool)
689/* Do we have too many workers and should some go away? */ 740/* Do we have too many workers and should some go away? */
690static bool too_many_workers(struct worker_pool *pool) 741static bool too_many_workers(struct worker_pool *pool)
691{ 742{
692 bool managing = pool->flags & POOL_MANAGING_WORKERS; 743 bool managing = mutex_is_locked(&pool->manager_arb);
693 int nr_idle = pool->nr_idle + managing; /* manager is considered idle */ 744 int nr_idle = pool->nr_idle + managing; /* manager is considered idle */
694 int nr_busy = pool->nr_workers - nr_idle; 745 int nr_busy = pool->nr_workers - nr_idle;
695 746
@@ -744,7 +795,7 @@ static void wake_up_worker(struct worker_pool *pool)
744 * CONTEXT: 795 * CONTEXT:
745 * spin_lock_irq(rq->lock) 796 * spin_lock_irq(rq->lock)
746 */ 797 */
747void wq_worker_waking_up(struct task_struct *task, unsigned int cpu) 798void wq_worker_waking_up(struct task_struct *task, int cpu)
748{ 799{
749 struct worker *worker = kthread_data(task); 800 struct worker *worker = kthread_data(task);
750 801
@@ -769,8 +820,7 @@ void wq_worker_waking_up(struct task_struct *task, unsigned int cpu)
769 * RETURNS: 820 * RETURNS:
770 * Worker task on @cpu to wake up, %NULL if none. 821 * Worker task on @cpu to wake up, %NULL if none.
771 */ 822 */
772struct task_struct *wq_worker_sleeping(struct task_struct *task, 823struct task_struct *wq_worker_sleeping(struct task_struct *task, int cpu)
773 unsigned int cpu)
774{ 824{
775 struct worker *worker = kthread_data(task), *to_wakeup = NULL; 825 struct worker *worker = kthread_data(task), *to_wakeup = NULL;
776 struct worker_pool *pool; 826 struct worker_pool *pool;
@@ -786,7 +836,8 @@ struct task_struct *wq_worker_sleeping(struct task_struct *task,
786 pool = worker->pool; 836 pool = worker->pool;
787 837
788 /* this can only happen on the local cpu */ 838 /* this can only happen on the local cpu */
789 BUG_ON(cpu != raw_smp_processor_id()); 839 if (WARN_ON_ONCE(cpu != raw_smp_processor_id()))
840 return NULL;
790 841
791 /* 842 /*
792 * The counterpart of the following dec_and_test, implied mb, 843 * The counterpart of the following dec_and_test, implied mb,
@@ -891,13 +942,12 @@ static inline void worker_clr_flags(struct worker *worker, unsigned int flags)
891 * recycled work item as currently executing and make it wait until the 942 * recycled work item as currently executing and make it wait until the
892 * current execution finishes, introducing an unwanted dependency. 943 * current execution finishes, introducing an unwanted dependency.
893 * 944 *
894 * This function checks the work item address, work function and workqueue 945 * This function checks the work item address and work function to avoid
895 * to avoid false positives. Note that this isn't complete as one may 946 * false positives. Note that this isn't complete as one may construct a
896 * construct a work function which can introduce dependency onto itself 947 * work function which can introduce dependency onto itself through a
897 * through a recycled work item. Well, if somebody wants to shoot oneself 948 * recycled work item. Well, if somebody wants to shoot oneself in the
898 * in the foot that badly, there's only so much we can do, and if such 949 * foot that badly, there's only so much we can do, and if such deadlock
899 * deadlock actually occurs, it should be easy to locate the culprit work 950 * actually occurs, it should be easy to locate the culprit work function.
900 * function.
901 * 951 *
902 * CONTEXT: 952 * CONTEXT:
903 * spin_lock_irq(pool->lock). 953 * spin_lock_irq(pool->lock).
@@ -961,6 +1011,64 @@ static void move_linked_works(struct work_struct *work, struct list_head *head,
961 *nextp = n; 1011 *nextp = n;
962} 1012}
963 1013
1014/**
1015 * get_pwq - get an extra reference on the specified pool_workqueue
1016 * @pwq: pool_workqueue to get
1017 *
1018 * Obtain an extra reference on @pwq. The caller should guarantee that
1019 * @pwq has positive refcnt and be holding the matching pool->lock.
1020 */
1021static void get_pwq(struct pool_workqueue *pwq)
1022{
1023 lockdep_assert_held(&pwq->pool->lock);
1024 WARN_ON_ONCE(pwq->refcnt <= 0);
1025 pwq->refcnt++;
1026}
1027
1028/**
1029 * put_pwq - put a pool_workqueue reference
1030 * @pwq: pool_workqueue to put
1031 *
1032 * Drop a reference of @pwq. If its refcnt reaches zero, schedule its
1033 * destruction. The caller should be holding the matching pool->lock.
1034 */
1035static void put_pwq(struct pool_workqueue *pwq)
1036{
1037 lockdep_assert_held(&pwq->pool->lock);
1038 if (likely(--pwq->refcnt))
1039 return;
1040 if (WARN_ON_ONCE(!(pwq->wq->flags & WQ_UNBOUND)))
1041 return;
1042 /*
1043 * @pwq can't be released under pool->lock, bounce to
1044 * pwq_unbound_release_workfn(). This never recurses on the same
1045 * pool->lock as this path is taken only for unbound workqueues and
1046 * the release work item is scheduled on a per-cpu workqueue. To
1047 * avoid lockdep warning, unbound pool->locks are given lockdep
1048 * subclass of 1 in get_unbound_pool().
1049 */
1050 schedule_work(&pwq->unbound_release_work);
1051}
1052
1053/**
1054 * put_pwq_unlocked - put_pwq() with surrounding pool lock/unlock
1055 * @pwq: pool_workqueue to put (can be %NULL)
1056 *
1057 * put_pwq() with locking. This function also allows %NULL @pwq.
1058 */
1059static void put_pwq_unlocked(struct pool_workqueue *pwq)
1060{
1061 if (pwq) {
1062 /*
1063 * As both pwqs and pools are sched-RCU protected, the
1064 * following lock operations are safe.
1065 */
1066 spin_lock_irq(&pwq->pool->lock);
1067 put_pwq(pwq);
1068 spin_unlock_irq(&pwq->pool->lock);
1069 }
1070}
1071
964static void pwq_activate_delayed_work(struct work_struct *work) 1072static void pwq_activate_delayed_work(struct work_struct *work)
965{ 1073{
966 struct pool_workqueue *pwq = get_work_pwq(work); 1074 struct pool_workqueue *pwq = get_work_pwq(work);
@@ -992,9 +1100,9 @@ static void pwq_activate_first_delayed(struct pool_workqueue *pwq)
992 */ 1100 */
993static void pwq_dec_nr_in_flight(struct pool_workqueue *pwq, int color) 1101static void pwq_dec_nr_in_flight(struct pool_workqueue *pwq, int color)
994{ 1102{
995 /* ignore uncolored works */ 1103 /* uncolored work items don't participate in flushing or nr_active */
996 if (color == WORK_NO_COLOR) 1104 if (color == WORK_NO_COLOR)
997 return; 1105 goto out_put;
998 1106
999 pwq->nr_in_flight[color]--; 1107 pwq->nr_in_flight[color]--;
1000 1108
@@ -1007,11 +1115,11 @@ static void pwq_dec_nr_in_flight(struct pool_workqueue *pwq, int color)
1007 1115
1008 /* is flush in progress and are we at the flushing tip? */ 1116 /* is flush in progress and are we at the flushing tip? */
1009 if (likely(pwq->flush_color != color)) 1117 if (likely(pwq->flush_color != color))
1010 return; 1118 goto out_put;
1011 1119
1012 /* are there still in-flight works? */ 1120 /* are there still in-flight works? */
1013 if (pwq->nr_in_flight[color]) 1121 if (pwq->nr_in_flight[color])
1014 return; 1122 goto out_put;
1015 1123
1016 /* this pwq is done, clear flush_color */ 1124 /* this pwq is done, clear flush_color */
1017 pwq->flush_color = -1; 1125 pwq->flush_color = -1;
@@ -1022,6 +1130,8 @@ static void pwq_dec_nr_in_flight(struct pool_workqueue *pwq, int color)
1022 */ 1130 */
1023 if (atomic_dec_and_test(&pwq->wq->nr_pwqs_to_flush)) 1131 if (atomic_dec_and_test(&pwq->wq->nr_pwqs_to_flush))
1024 complete(&pwq->wq->first_flusher->done); 1132 complete(&pwq->wq->first_flusher->done);
1133out_put:
1134 put_pwq(pwq);
1025} 1135}
1026 1136
1027/** 1137/**
@@ -1144,11 +1254,12 @@ static void insert_work(struct pool_workqueue *pwq, struct work_struct *work,
1144 /* we own @work, set data and link */ 1254 /* we own @work, set data and link */
1145 set_work_pwq(work, pwq, extra_flags); 1255 set_work_pwq(work, pwq, extra_flags);
1146 list_add_tail(&work->entry, head); 1256 list_add_tail(&work->entry, head);
1257 get_pwq(pwq);
1147 1258
1148 /* 1259 /*
1149 * Ensure either worker_sched_deactivated() sees the above 1260 * Ensure either wq_worker_sleeping() sees the above
1150 * list_add_tail() or we see zero nr_running to avoid workers 1261 * list_add_tail() or we see zero nr_running to avoid workers lying
1151 * lying around lazily while there are works to be processed. 1262 * around lazily while there are works to be processed.
1152 */ 1263 */
1153 smp_mb(); 1264 smp_mb();
1154 1265
@@ -1172,10 +1283,11 @@ static bool is_chained_work(struct workqueue_struct *wq)
1172 return worker && worker->current_pwq->wq == wq; 1283 return worker && worker->current_pwq->wq == wq;
1173} 1284}
1174 1285
1175static void __queue_work(unsigned int cpu, struct workqueue_struct *wq, 1286static void __queue_work(int cpu, struct workqueue_struct *wq,
1176 struct work_struct *work) 1287 struct work_struct *work)
1177{ 1288{
1178 struct pool_workqueue *pwq; 1289 struct pool_workqueue *pwq;
1290 struct worker_pool *last_pool;
1179 struct list_head *worklist; 1291 struct list_head *worklist;
1180 unsigned int work_flags; 1292 unsigned int work_flags;
1181 unsigned int req_cpu = cpu; 1293 unsigned int req_cpu = cpu;
@@ -1191,48 +1303,62 @@ static void __queue_work(unsigned int cpu, struct workqueue_struct *wq,
1191 debug_work_activate(work); 1303 debug_work_activate(work);
1192 1304
1193 /* if dying, only works from the same workqueue are allowed */ 1305 /* if dying, only works from the same workqueue are allowed */
1194 if (unlikely(wq->flags & WQ_DRAINING) && 1306 if (unlikely(wq->flags & __WQ_DRAINING) &&
1195 WARN_ON_ONCE(!is_chained_work(wq))) 1307 WARN_ON_ONCE(!is_chained_work(wq)))
1196 return; 1308 return;
1309retry:
1310 if (req_cpu == WORK_CPU_UNBOUND)
1311 cpu = raw_smp_processor_id();
1197 1312
1198 /* determine the pwq to use */ 1313 /* pwq which will be used unless @work is executing elsewhere */
1199 if (!(wq->flags & WQ_UNBOUND)) { 1314 if (!(wq->flags & WQ_UNBOUND))
1200 struct worker_pool *last_pool; 1315 pwq = per_cpu_ptr(wq->cpu_pwqs, cpu);
1201 1316 else
1202 if (cpu == WORK_CPU_UNBOUND) 1317 pwq = unbound_pwq_by_node(wq, cpu_to_node(cpu));
1203 cpu = raw_smp_processor_id();
1204
1205 /*
1206 * It's multi cpu. If @work was previously on a different
1207 * cpu, it might still be running there, in which case the
1208 * work needs to be queued on that cpu to guarantee
1209 * non-reentrancy.
1210 */
1211 pwq = get_pwq(cpu, wq);
1212 last_pool = get_work_pool(work);
1213 1318
1214 if (last_pool && last_pool != pwq->pool) { 1319 /*
1215 struct worker *worker; 1320 * If @work was previously on a different pool, it might still be
1321 * running there, in which case the work needs to be queued on that
1322 * pool to guarantee non-reentrancy.
1323 */
1324 last_pool = get_work_pool(work);
1325 if (last_pool && last_pool != pwq->pool) {
1326 struct worker *worker;
1216 1327
1217 spin_lock(&last_pool->lock); 1328 spin_lock(&last_pool->lock);
1218 1329
1219 worker = find_worker_executing_work(last_pool, work); 1330 worker = find_worker_executing_work(last_pool, work);
1220 1331
1221 if (worker && worker->current_pwq->wq == wq) { 1332 if (worker && worker->current_pwq->wq == wq) {
1222 pwq = get_pwq(last_pool->cpu, wq); 1333 pwq = worker->current_pwq;
1223 } else {
1224 /* meh... not running there, queue here */
1225 spin_unlock(&last_pool->lock);
1226 spin_lock(&pwq->pool->lock);
1227 }
1228 } else { 1334 } else {
1335 /* meh... not running there, queue here */
1336 spin_unlock(&last_pool->lock);
1229 spin_lock(&pwq->pool->lock); 1337 spin_lock(&pwq->pool->lock);
1230 } 1338 }
1231 } else { 1339 } else {
1232 pwq = get_pwq(WORK_CPU_UNBOUND, wq);
1233 spin_lock(&pwq->pool->lock); 1340 spin_lock(&pwq->pool->lock);
1234 } 1341 }
1235 1342
1343 /*
1344 * pwq is determined and locked. For unbound pools, we could have
1345 * raced with pwq release and it could already be dead. If its
1346 * refcnt is zero, repeat pwq selection. Note that pwqs never die
1347 * without another pwq replacing it in the numa_pwq_tbl or while
1348 * work items are executing on it, so the retrying is guaranteed to
1349 * make forward-progress.
1350 */
1351 if (unlikely(!pwq->refcnt)) {
1352 if (wq->flags & WQ_UNBOUND) {
1353 spin_unlock(&pwq->pool->lock);
1354 cpu_relax();
1355 goto retry;
1356 }
1357 /* oops */
1358 WARN_ONCE(true, "workqueue: per-cpu pwq for %s on cpu%d has 0 refcnt",
1359 wq->name, cpu);
1360 }
1361
1236 /* pwq determined, queue */ 1362 /* pwq determined, queue */
1237 trace_workqueue_queue_work(req_cpu, pwq, work); 1363 trace_workqueue_queue_work(req_cpu, pwq, work);
1238 1364
@@ -1285,23 +1411,7 @@ bool queue_work_on(int cpu, struct workqueue_struct *wq,
1285 local_irq_restore(flags); 1411 local_irq_restore(flags);
1286 return ret; 1412 return ret;
1287} 1413}
1288EXPORT_SYMBOL_GPL(queue_work_on); 1414EXPORT_SYMBOL(queue_work_on);
1289
1290/**
1291 * queue_work - queue work on a workqueue
1292 * @wq: workqueue to use
1293 * @work: work to queue
1294 *
1295 * Returns %false if @work was already on a queue, %true otherwise.
1296 *
1297 * We queue the work to the CPU on which it was submitted, but if the CPU dies
1298 * it can be processed by another CPU.
1299 */
1300bool queue_work(struct workqueue_struct *wq, struct work_struct *work)
1301{
1302 return queue_work_on(WORK_CPU_UNBOUND, wq, work);
1303}
1304EXPORT_SYMBOL_GPL(queue_work);
1305 1415
1306void delayed_work_timer_fn(unsigned long __data) 1416void delayed_work_timer_fn(unsigned long __data)
1307{ 1417{
@@ -1375,22 +1485,7 @@ bool queue_delayed_work_on(int cpu, struct workqueue_struct *wq,
1375 local_irq_restore(flags); 1485 local_irq_restore(flags);
1376 return ret; 1486 return ret;
1377} 1487}
1378EXPORT_SYMBOL_GPL(queue_delayed_work_on); 1488EXPORT_SYMBOL(queue_delayed_work_on);
1379
1380/**
1381 * queue_delayed_work - queue work on a workqueue after delay
1382 * @wq: workqueue to use
1383 * @dwork: delayable work to queue
1384 * @delay: number of jiffies to wait before queueing
1385 *
1386 * Equivalent to queue_delayed_work_on() but tries to use the local CPU.
1387 */
1388bool queue_delayed_work(struct workqueue_struct *wq,
1389 struct delayed_work *dwork, unsigned long delay)
1390{
1391 return queue_delayed_work_on(WORK_CPU_UNBOUND, wq, dwork, delay);
1392}
1393EXPORT_SYMBOL_GPL(queue_delayed_work);
1394 1489
1395/** 1490/**
1396 * mod_delayed_work_on - modify delay of or queue a delayed work on specific CPU 1491 * mod_delayed_work_on - modify delay of or queue a delayed work on specific CPU
@@ -1431,21 +1526,6 @@ bool mod_delayed_work_on(int cpu, struct workqueue_struct *wq,
1431EXPORT_SYMBOL_GPL(mod_delayed_work_on); 1526EXPORT_SYMBOL_GPL(mod_delayed_work_on);
1432 1527
1433/** 1528/**
1434 * mod_delayed_work - modify delay of or queue a delayed work
1435 * @wq: workqueue to use
1436 * @dwork: work to queue
1437 * @delay: number of jiffies to wait before queueing
1438 *
1439 * mod_delayed_work_on() on local CPU.
1440 */
1441bool mod_delayed_work(struct workqueue_struct *wq, struct delayed_work *dwork,
1442 unsigned long delay)
1443{
1444 return mod_delayed_work_on(WORK_CPU_UNBOUND, wq, dwork, delay);
1445}
1446EXPORT_SYMBOL_GPL(mod_delayed_work);
1447
1448/**
1449 * worker_enter_idle - enter idle state 1529 * worker_enter_idle - enter idle state
1450 * @worker: worker which is entering idle state 1530 * @worker: worker which is entering idle state
1451 * 1531 *
@@ -1459,9 +1539,10 @@ static void worker_enter_idle(struct worker *worker)
1459{ 1539{
1460 struct worker_pool *pool = worker->pool; 1540 struct worker_pool *pool = worker->pool;
1461 1541
1462 BUG_ON(worker->flags & WORKER_IDLE); 1542 if (WARN_ON_ONCE(worker->flags & WORKER_IDLE) ||
1463 BUG_ON(!list_empty(&worker->entry) && 1543 WARN_ON_ONCE(!list_empty(&worker->entry) &&
1464 (worker->hentry.next || worker->hentry.pprev)); 1544 (worker->hentry.next || worker->hentry.pprev)))
1545 return;
1465 1546
1466 /* can't use worker_set_flags(), also called from start_worker() */ 1547 /* can't use worker_set_flags(), also called from start_worker() */
1467 worker->flags |= WORKER_IDLE; 1548 worker->flags |= WORKER_IDLE;
@@ -1498,22 +1579,25 @@ static void worker_leave_idle(struct worker *worker)
1498{ 1579{
1499 struct worker_pool *pool = worker->pool; 1580 struct worker_pool *pool = worker->pool;
1500 1581
1501 BUG_ON(!(worker->flags & WORKER_IDLE)); 1582 if (WARN_ON_ONCE(!(worker->flags & WORKER_IDLE)))
1583 return;
1502 worker_clr_flags(worker, WORKER_IDLE); 1584 worker_clr_flags(worker, WORKER_IDLE);
1503 pool->nr_idle--; 1585 pool->nr_idle--;
1504 list_del_init(&worker->entry); 1586 list_del_init(&worker->entry);
1505} 1587}
1506 1588
1507/** 1589/**
1508 * worker_maybe_bind_and_lock - bind worker to its cpu if possible and lock pool 1590 * worker_maybe_bind_and_lock - try to bind %current to worker_pool and lock it
1509 * @worker: self 1591 * @pool: target worker_pool
1592 *
1593 * Bind %current to the cpu of @pool if it is associated and lock @pool.
1510 * 1594 *
1511 * Works which are scheduled while the cpu is online must at least be 1595 * Works which are scheduled while the cpu is online must at least be
1512 * scheduled to a worker which is bound to the cpu so that if they are 1596 * scheduled to a worker which is bound to the cpu so that if they are
1513 * flushed from cpu callbacks while cpu is going down, they are 1597 * flushed from cpu callbacks while cpu is going down, they are
1514 * guaranteed to execute on the cpu. 1598 * guaranteed to execute on the cpu.
1515 * 1599 *
1516 * This function is to be used by rogue workers and rescuers to bind 1600 * This function is to be used by unbound workers and rescuers to bind
1517 * themselves to the target cpu and may race with cpu going down or 1601 * themselves to the target cpu and may race with cpu going down or
1518 * coming online. kthread_bind() can't be used because it may put the 1602 * coming online. kthread_bind() can't be used because it may put the
1519 * worker to already dead cpu and set_cpus_allowed_ptr() can't be used 1603 * worker to already dead cpu and set_cpus_allowed_ptr() can't be used
@@ -1534,12 +1618,9 @@ static void worker_leave_idle(struct worker *worker)
1534 * %true if the associated pool is online (@worker is successfully 1618 * %true if the associated pool is online (@worker is successfully
1535 * bound), %false if offline. 1619 * bound), %false if offline.
1536 */ 1620 */
1537static bool worker_maybe_bind_and_lock(struct worker *worker) 1621static bool worker_maybe_bind_and_lock(struct worker_pool *pool)
1538__acquires(&pool->lock) 1622__acquires(&pool->lock)
1539{ 1623{
1540 struct worker_pool *pool = worker->pool;
1541 struct task_struct *task = worker->task;
1542
1543 while (true) { 1624 while (true) {
1544 /* 1625 /*
1545 * The following call may fail, succeed or succeed 1626 * The following call may fail, succeed or succeed
@@ -1548,14 +1629,13 @@ __acquires(&pool->lock)
1548 * against POOL_DISASSOCIATED. 1629 * against POOL_DISASSOCIATED.
1549 */ 1630 */
1550 if (!(pool->flags & POOL_DISASSOCIATED)) 1631 if (!(pool->flags & POOL_DISASSOCIATED))
1551 set_cpus_allowed_ptr(task, get_cpu_mask(pool->cpu)); 1632 set_cpus_allowed_ptr(current, pool->attrs->cpumask);
1552 1633
1553 spin_lock_irq(&pool->lock); 1634 spin_lock_irq(&pool->lock);
1554 if (pool->flags & POOL_DISASSOCIATED) 1635 if (pool->flags & POOL_DISASSOCIATED)
1555 return false; 1636 return false;
1556 if (task_cpu(task) == pool->cpu && 1637 if (task_cpu(current) == pool->cpu &&
1557 cpumask_equal(&current->cpus_allowed, 1638 cpumask_equal(&current->cpus_allowed, pool->attrs->cpumask))
1558 get_cpu_mask(pool->cpu)))
1559 return true; 1639 return true;
1560 spin_unlock_irq(&pool->lock); 1640 spin_unlock_irq(&pool->lock);
1561 1641
@@ -1570,108 +1650,6 @@ __acquires(&pool->lock)
1570 } 1650 }
1571} 1651}
1572 1652
1573/*
1574 * Rebind an idle @worker to its CPU. worker_thread() will test
1575 * list_empty(@worker->entry) before leaving idle and call this function.
1576 */
1577static void idle_worker_rebind(struct worker *worker)
1578{
1579 /* CPU may go down again inbetween, clear UNBOUND only on success */
1580 if (worker_maybe_bind_and_lock(worker))
1581 worker_clr_flags(worker, WORKER_UNBOUND);
1582
1583 /* rebind complete, become available again */
1584 list_add(&worker->entry, &worker->pool->idle_list);
1585 spin_unlock_irq(&worker->pool->lock);
1586}
1587
1588/*
1589 * Function for @worker->rebind.work used to rebind unbound busy workers to
1590 * the associated cpu which is coming back online. This is scheduled by
1591 * cpu up but can race with other cpu hotplug operations and may be
1592 * executed twice without intervening cpu down.
1593 */
1594static void busy_worker_rebind_fn(struct work_struct *work)
1595{
1596 struct worker *worker = container_of(work, struct worker, rebind_work);
1597
1598 if (worker_maybe_bind_and_lock(worker))
1599 worker_clr_flags(worker, WORKER_UNBOUND);
1600
1601 spin_unlock_irq(&worker->pool->lock);
1602}
1603
1604/**
1605 * rebind_workers - rebind all workers of a pool to the associated CPU
1606 * @pool: pool of interest
1607 *
1608 * @pool->cpu is coming online. Rebind all workers to the CPU. Rebinding
1609 * is different for idle and busy ones.
1610 *
1611 * Idle ones will be removed from the idle_list and woken up. They will
1612 * add themselves back after completing rebind. This ensures that the
1613 * idle_list doesn't contain any unbound workers when re-bound busy workers
1614 * try to perform local wake-ups for concurrency management.
1615 *
1616 * Busy workers can rebind after they finish their current work items.
1617 * Queueing the rebind work item at the head of the scheduled list is
1618 * enough. Note that nr_running will be properly bumped as busy workers
1619 * rebind.
1620 *
1621 * On return, all non-manager workers are scheduled for rebind - see
1622 * manage_workers() for the manager special case. Any idle worker
1623 * including the manager will not appear on @idle_list until rebind is
1624 * complete, making local wake-ups safe.
1625 */
1626static void rebind_workers(struct worker_pool *pool)
1627{
1628 struct worker *worker, *n;
1629 int i;
1630
1631 lockdep_assert_held(&pool->assoc_mutex);
1632 lockdep_assert_held(&pool->lock);
1633
1634 /* dequeue and kick idle ones */
1635 list_for_each_entry_safe(worker, n, &pool->idle_list, entry) {
1636 /*
1637 * idle workers should be off @pool->idle_list until rebind
1638 * is complete to avoid receiving premature local wake-ups.
1639 */
1640 list_del_init(&worker->entry);
1641
1642 /*
1643 * worker_thread() will see the above dequeuing and call
1644 * idle_worker_rebind().
1645 */
1646 wake_up_process(worker->task);
1647 }
1648
1649 /* rebind busy workers */
1650 for_each_busy_worker(worker, i, pool) {
1651 struct work_struct *rebind_work = &worker->rebind_work;
1652 struct workqueue_struct *wq;
1653
1654 if (test_and_set_bit(WORK_STRUCT_PENDING_BIT,
1655 work_data_bits(rebind_work)))
1656 continue;
1657
1658 debug_work_activate(rebind_work);
1659
1660 /*
1661 * wq doesn't really matter but let's keep @worker->pool
1662 * and @pwq->pool consistent for sanity.
1663 */
1664 if (std_worker_pool_pri(worker->pool))
1665 wq = system_highpri_wq;
1666 else
1667 wq = system_wq;
1668
1669 insert_work(get_pwq(pool->cpu, wq), rebind_work,
1670 worker->scheduled.next,
1671 work_color_to_flags(WORK_NO_COLOR));
1672 }
1673}
1674
1675static struct worker *alloc_worker(void) 1653static struct worker *alloc_worker(void)
1676{ 1654{
1677 struct worker *worker; 1655 struct worker *worker;
@@ -1680,7 +1658,6 @@ static struct worker *alloc_worker(void)
1680 if (worker) { 1658 if (worker) {
1681 INIT_LIST_HEAD(&worker->entry); 1659 INIT_LIST_HEAD(&worker->entry);
1682 INIT_LIST_HEAD(&worker->scheduled); 1660 INIT_LIST_HEAD(&worker->scheduled);
1683 INIT_WORK(&worker->rebind_work, busy_worker_rebind_fn);
1684 /* on creation a worker is in !idle && prep state */ 1661 /* on creation a worker is in !idle && prep state */
1685 worker->flags = WORKER_PREP; 1662 worker->flags = WORKER_PREP;
1686 } 1663 }
@@ -1703,18 +1680,25 @@ static struct worker *alloc_worker(void)
1703 */ 1680 */
1704static struct worker *create_worker(struct worker_pool *pool) 1681static struct worker *create_worker(struct worker_pool *pool)
1705{ 1682{
1706 const char *pri = std_worker_pool_pri(pool) ? "H" : "";
1707 struct worker *worker = NULL; 1683 struct worker *worker = NULL;
1708 int id = -1; 1684 int id = -1;
1685 char id_buf[16];
1686
1687 lockdep_assert_held(&pool->manager_mutex);
1709 1688
1689 /*
1690 * ID is needed to determine kthread name. Allocate ID first
1691 * without installing the pointer.
1692 */
1693 idr_preload(GFP_KERNEL);
1710 spin_lock_irq(&pool->lock); 1694 spin_lock_irq(&pool->lock);
1711 while (ida_get_new(&pool->worker_ida, &id)) { 1695
1712 spin_unlock_irq(&pool->lock); 1696 id = idr_alloc(&pool->worker_idr, NULL, 0, 0, GFP_NOWAIT);
1713 if (!ida_pre_get(&pool->worker_ida, GFP_KERNEL)) 1697
1714 goto fail;
1715 spin_lock_irq(&pool->lock);
1716 }
1717 spin_unlock_irq(&pool->lock); 1698 spin_unlock_irq(&pool->lock);
1699 idr_preload_end();
1700 if (id < 0)
1701 goto fail;
1718 1702
1719 worker = alloc_worker(); 1703 worker = alloc_worker();
1720 if (!worker) 1704 if (!worker)
@@ -1723,40 +1707,46 @@ static struct worker *create_worker(struct worker_pool *pool)
1723 worker->pool = pool; 1707 worker->pool = pool;
1724 worker->id = id; 1708 worker->id = id;
1725 1709
1726 if (pool->cpu != WORK_CPU_UNBOUND) 1710 if (pool->cpu >= 0)
1727 worker->task = kthread_create_on_node(worker_thread, 1711 snprintf(id_buf, sizeof(id_buf), "%d:%d%s", pool->cpu, id,
1728 worker, cpu_to_node(pool->cpu), 1712 pool->attrs->nice < 0 ? "H" : "");
1729 "kworker/%u:%d%s", pool->cpu, id, pri);
1730 else 1713 else
1731 worker->task = kthread_create(worker_thread, worker, 1714 snprintf(id_buf, sizeof(id_buf), "u%d:%d", pool->id, id);
1732 "kworker/u:%d%s", id, pri); 1715
1716 worker->task = kthread_create_on_node(worker_thread, worker, pool->node,
1717 "kworker/%s", id_buf);
1733 if (IS_ERR(worker->task)) 1718 if (IS_ERR(worker->task))
1734 goto fail; 1719 goto fail;
1735 1720
1736 if (std_worker_pool_pri(pool)) 1721 /*
1737 set_user_nice(worker->task, HIGHPRI_NICE_LEVEL); 1722 * set_cpus_allowed_ptr() will fail if the cpumask doesn't have any
1723 * online CPUs. It'll be re-applied when any of the CPUs come up.
1724 */
1725 set_user_nice(worker->task, pool->attrs->nice);
1726 set_cpus_allowed_ptr(worker->task, pool->attrs->cpumask);
1727
1728 /* prevent userland from meddling with cpumask of workqueue workers */
1729 worker->task->flags |= PF_NO_SETAFFINITY;
1738 1730
1739 /* 1731 /*
1740 * Determine CPU binding of the new worker depending on 1732 * The caller is responsible for ensuring %POOL_DISASSOCIATED
1741 * %POOL_DISASSOCIATED. The caller is responsible for ensuring the 1733 * remains stable across this function. See the comments above the
1742 * flag remains stable across this function. See the comments 1734 * flag definition for details.
1743 * above the flag definition for details.
1744 *
1745 * As an unbound worker may later become a regular one if CPU comes
1746 * online, make sure every worker has %PF_THREAD_BOUND set.
1747 */ 1735 */
1748 if (!(pool->flags & POOL_DISASSOCIATED)) { 1736 if (pool->flags & POOL_DISASSOCIATED)
1749 kthread_bind(worker->task, pool->cpu);
1750 } else {
1751 worker->task->flags |= PF_THREAD_BOUND;
1752 worker->flags |= WORKER_UNBOUND; 1737 worker->flags |= WORKER_UNBOUND;
1753 } 1738
1739 /* successful, commit the pointer to idr */
1740 spin_lock_irq(&pool->lock);
1741 idr_replace(&pool->worker_idr, worker, worker->id);
1742 spin_unlock_irq(&pool->lock);
1754 1743
1755 return worker; 1744 return worker;
1745
1756fail: 1746fail:
1757 if (id >= 0) { 1747 if (id >= 0) {
1758 spin_lock_irq(&pool->lock); 1748 spin_lock_irq(&pool->lock);
1759 ida_remove(&pool->worker_ida, id); 1749 idr_remove(&pool->worker_idr, id);
1760 spin_unlock_irq(&pool->lock); 1750 spin_unlock_irq(&pool->lock);
1761 } 1751 }
1762 kfree(worker); 1752 kfree(worker);
@@ -1781,6 +1771,30 @@ static void start_worker(struct worker *worker)
1781} 1771}
1782 1772
1783/** 1773/**
1774 * create_and_start_worker - create and start a worker for a pool
1775 * @pool: the target pool
1776 *
1777 * Grab the managership of @pool and create and start a new worker for it.
1778 */
1779static int create_and_start_worker(struct worker_pool *pool)
1780{
1781 struct worker *worker;
1782
1783 mutex_lock(&pool->manager_mutex);
1784
1785 worker = create_worker(pool);
1786 if (worker) {
1787 spin_lock_irq(&pool->lock);
1788 start_worker(worker);
1789 spin_unlock_irq(&pool->lock);
1790 }
1791
1792 mutex_unlock(&pool->manager_mutex);
1793
1794 return worker ? 0 : -ENOMEM;
1795}
1796
1797/**
1784 * destroy_worker - destroy a workqueue worker 1798 * destroy_worker - destroy a workqueue worker
1785 * @worker: worker to be destroyed 1799 * @worker: worker to be destroyed
1786 * 1800 *
@@ -1792,11 +1806,14 @@ static void start_worker(struct worker *worker)
1792static void destroy_worker(struct worker *worker) 1806static void destroy_worker(struct worker *worker)
1793{ 1807{
1794 struct worker_pool *pool = worker->pool; 1808 struct worker_pool *pool = worker->pool;
1795 int id = worker->id; 1809
1810 lockdep_assert_held(&pool->manager_mutex);
1811 lockdep_assert_held(&pool->lock);
1796 1812
1797 /* sanity check frenzy */ 1813 /* sanity check frenzy */
1798 BUG_ON(worker->current_work); 1814 if (WARN_ON(worker->current_work) ||
1799 BUG_ON(!list_empty(&worker->scheduled)); 1815 WARN_ON(!list_empty(&worker->scheduled)))
1816 return;
1800 1817
1801 if (worker->flags & WORKER_STARTED) 1818 if (worker->flags & WORKER_STARTED)
1802 pool->nr_workers--; 1819 pool->nr_workers--;
@@ -1806,13 +1823,14 @@ static void destroy_worker(struct worker *worker)
1806 list_del_init(&worker->entry); 1823 list_del_init(&worker->entry);
1807 worker->flags |= WORKER_DIE; 1824 worker->flags |= WORKER_DIE;
1808 1825
1826 idr_remove(&pool->worker_idr, worker->id);
1827
1809 spin_unlock_irq(&pool->lock); 1828 spin_unlock_irq(&pool->lock);
1810 1829
1811 kthread_stop(worker->task); 1830 kthread_stop(worker->task);
1812 kfree(worker); 1831 kfree(worker);
1813 1832
1814 spin_lock_irq(&pool->lock); 1833 spin_lock_irq(&pool->lock);
1815 ida_remove(&pool->worker_ida, id);
1816} 1834}
1817 1835
1818static void idle_worker_timeout(unsigned long __pool) 1836static void idle_worker_timeout(unsigned long __pool)
@@ -1841,23 +1859,21 @@ static void idle_worker_timeout(unsigned long __pool)
1841 spin_unlock_irq(&pool->lock); 1859 spin_unlock_irq(&pool->lock);
1842} 1860}
1843 1861
1844static bool send_mayday(struct work_struct *work) 1862static void send_mayday(struct work_struct *work)
1845{ 1863{
1846 struct pool_workqueue *pwq = get_work_pwq(work); 1864 struct pool_workqueue *pwq = get_work_pwq(work);
1847 struct workqueue_struct *wq = pwq->wq; 1865 struct workqueue_struct *wq = pwq->wq;
1848 unsigned int cpu;
1849 1866
1850 if (!(wq->flags & WQ_RESCUER)) 1867 lockdep_assert_held(&wq_mayday_lock);
1851 return false; 1868
1869 if (!wq->rescuer)
1870 return;
1852 1871
1853 /* mayday mayday mayday */ 1872 /* mayday mayday mayday */
1854 cpu = pwq->pool->cpu; 1873 if (list_empty(&pwq->mayday_node)) {
1855 /* WORK_CPU_UNBOUND can't be set in cpumask, use cpu 0 instead */ 1874 list_add_tail(&pwq->mayday_node, &wq->maydays);
1856 if (cpu == WORK_CPU_UNBOUND)
1857 cpu = 0;
1858 if (!mayday_test_and_set_cpu(cpu, wq->mayday_mask))
1859 wake_up_process(wq->rescuer->task); 1875 wake_up_process(wq->rescuer->task);
1860 return true; 1876 }
1861} 1877}
1862 1878
1863static void pool_mayday_timeout(unsigned long __pool) 1879static void pool_mayday_timeout(unsigned long __pool)
@@ -1865,7 +1881,8 @@ static void pool_mayday_timeout(unsigned long __pool)
1865 struct worker_pool *pool = (void *)__pool; 1881 struct worker_pool *pool = (void *)__pool;
1866 struct work_struct *work; 1882 struct work_struct *work;
1867 1883
1868 spin_lock_irq(&pool->lock); 1884 spin_lock_irq(&wq_mayday_lock); /* for wq->maydays */
1885 spin_lock(&pool->lock);
1869 1886
1870 if (need_to_create_worker(pool)) { 1887 if (need_to_create_worker(pool)) {
1871 /* 1888 /*
@@ -1878,7 +1895,8 @@ static void pool_mayday_timeout(unsigned long __pool)
1878 send_mayday(work); 1895 send_mayday(work);
1879 } 1896 }
1880 1897
1881 spin_unlock_irq(&pool->lock); 1898 spin_unlock(&pool->lock);
1899 spin_unlock_irq(&wq_mayday_lock);
1882 1900
1883 mod_timer(&pool->mayday_timer, jiffies + MAYDAY_INTERVAL); 1901 mod_timer(&pool->mayday_timer, jiffies + MAYDAY_INTERVAL);
1884} 1902}
@@ -1893,8 +1911,8 @@ static void pool_mayday_timeout(unsigned long __pool)
1893 * sent to all rescuers with works scheduled on @pool to resolve 1911 * sent to all rescuers with works scheduled on @pool to resolve
1894 * possible allocation deadlock. 1912 * possible allocation deadlock.
1895 * 1913 *
1896 * On return, need_to_create_worker() is guaranteed to be false and 1914 * On return, need_to_create_worker() is guaranteed to be %false and
1897 * may_start_working() true. 1915 * may_start_working() %true.
1898 * 1916 *
1899 * LOCKING: 1917 * LOCKING:
1900 * spin_lock_irq(pool->lock) which may be released and regrabbed 1918 * spin_lock_irq(pool->lock) which may be released and regrabbed
@@ -1902,7 +1920,7 @@ static void pool_mayday_timeout(unsigned long __pool)
1902 * manager. 1920 * manager.
1903 * 1921 *
1904 * RETURNS: 1922 * RETURNS:
1905 * false if no action was taken and pool->lock stayed locked, true 1923 * %false if no action was taken and pool->lock stayed locked, %true
1906 * otherwise. 1924 * otherwise.
1907 */ 1925 */
1908static bool maybe_create_worker(struct worker_pool *pool) 1926static bool maybe_create_worker(struct worker_pool *pool)
@@ -1925,7 +1943,8 @@ restart:
1925 del_timer_sync(&pool->mayday_timer); 1943 del_timer_sync(&pool->mayday_timer);
1926 spin_lock_irq(&pool->lock); 1944 spin_lock_irq(&pool->lock);
1927 start_worker(worker); 1945 start_worker(worker);
1928 BUG_ON(need_to_create_worker(pool)); 1946 if (WARN_ON_ONCE(need_to_create_worker(pool)))
1947 goto restart;
1929 return true; 1948 return true;
1930 } 1949 }
1931 1950
@@ -1958,7 +1977,7 @@ restart:
1958 * multiple times. Called only from manager. 1977 * multiple times. Called only from manager.
1959 * 1978 *
1960 * RETURNS: 1979 * RETURNS:
1961 * false if no action was taken and pool->lock stayed locked, true 1980 * %false if no action was taken and pool->lock stayed locked, %true
1962 * otherwise. 1981 * otherwise.
1963 */ 1982 */
1964static bool maybe_destroy_workers(struct worker_pool *pool) 1983static bool maybe_destroy_workers(struct worker_pool *pool)
@@ -2009,42 +2028,38 @@ static bool manage_workers(struct worker *worker)
2009 struct worker_pool *pool = worker->pool; 2028 struct worker_pool *pool = worker->pool;
2010 bool ret = false; 2029 bool ret = false;
2011 2030
2012 if (pool->flags & POOL_MANAGING_WORKERS) 2031 /*
2032 * Managership is governed by two mutexes - manager_arb and
2033 * manager_mutex. manager_arb handles arbitration of manager role.
2034 * Anyone who successfully grabs manager_arb wins the arbitration
2035 * and becomes the manager. mutex_trylock() on pool->manager_arb
2036 * failure while holding pool->lock reliably indicates that someone
2037 * else is managing the pool and the worker which failed trylock
2038 * can proceed to executing work items. This means that anyone
2039 * grabbing manager_arb is responsible for actually performing
2040 * manager duties. If manager_arb is grabbed and released without
2041 * actual management, the pool may stall indefinitely.
2042 *
2043 * manager_mutex is used for exclusion of actual management
2044 * operations. The holder of manager_mutex can be sure that none
2045 * of management operations, including creation and destruction of
2046 * workers, won't take place until the mutex is released. Because
2047 * manager_mutex doesn't interfere with manager role arbitration,
2048 * it is guaranteed that the pool's management, while may be
2049 * delayed, won't be disturbed by someone else grabbing
2050 * manager_mutex.
2051 */
2052 if (!mutex_trylock(&pool->manager_arb))
2013 return ret; 2053 return ret;
2014 2054
2015 pool->flags |= POOL_MANAGING_WORKERS;
2016
2017 /* 2055 /*
2018 * To simplify both worker management and CPU hotplug, hold off 2056 * With manager arbitration won, manager_mutex would be free in
2019 * management while hotplug is in progress. CPU hotplug path can't 2057 * most cases. trylock first without dropping @pool->lock.
2020 * grab %POOL_MANAGING_WORKERS to achieve this because that can
2021 * lead to idle worker depletion (all become busy thinking someone
2022 * else is managing) which in turn can result in deadlock under
2023 * extreme circumstances. Use @pool->assoc_mutex to synchronize
2024 * manager against CPU hotplug.
2025 *
2026 * assoc_mutex would always be free unless CPU hotplug is in
2027 * progress. trylock first without dropping @pool->lock.
2028 */ 2058 */
2029 if (unlikely(!mutex_trylock(&pool->assoc_mutex))) { 2059 if (unlikely(!mutex_trylock(&pool->manager_mutex))) {
2030 spin_unlock_irq(&pool->lock); 2060 spin_unlock_irq(&pool->lock);
2031 mutex_lock(&pool->assoc_mutex); 2061 mutex_lock(&pool->manager_mutex);
2032 /* 2062 spin_lock_irq(&pool->lock);
2033 * CPU hotplug could have happened while we were waiting
2034 * for assoc_mutex. Hotplug itself can't handle us
2035 * because manager isn't either on idle or busy list, and
2036 * @pool's state and ours could have deviated.
2037 *
2038 * As hotplug is now excluded via assoc_mutex, we can
2039 * simply try to bind. It will succeed or fail depending
2040 * on @pool's current state. Try it and adjust
2041 * %WORKER_UNBOUND accordingly.
2042 */
2043 if (worker_maybe_bind_and_lock(worker))
2044 worker->flags &= ~WORKER_UNBOUND;
2045 else
2046 worker->flags |= WORKER_UNBOUND;
2047
2048 ret = true; 2063 ret = true;
2049 } 2064 }
2050 2065
@@ -2057,8 +2072,8 @@ static bool manage_workers(struct worker *worker)
2057 ret |= maybe_destroy_workers(pool); 2072 ret |= maybe_destroy_workers(pool);
2058 ret |= maybe_create_worker(pool); 2073 ret |= maybe_create_worker(pool);
2059 2074
2060 pool->flags &= ~POOL_MANAGING_WORKERS; 2075 mutex_unlock(&pool->manager_mutex);
2061 mutex_unlock(&pool->assoc_mutex); 2076 mutex_unlock(&pool->manager_arb);
2062 return ret; 2077 return ret;
2063} 2078}
2064 2079
@@ -2184,6 +2199,7 @@ __acquires(&pool->lock)
2184 worker->current_work = NULL; 2199 worker->current_work = NULL;
2185 worker->current_func = NULL; 2200 worker->current_func = NULL;
2186 worker->current_pwq = NULL; 2201 worker->current_pwq = NULL;
2202 worker->desc_valid = false;
2187 pwq_dec_nr_in_flight(pwq, work_color); 2203 pwq_dec_nr_in_flight(pwq, work_color);
2188} 2204}
2189 2205
@@ -2212,11 +2228,11 @@ static void process_scheduled_works(struct worker *worker)
2212 * worker_thread - the worker thread function 2228 * worker_thread - the worker thread function
2213 * @__worker: self 2229 * @__worker: self
2214 * 2230 *
2215 * The worker thread function. There are NR_CPU_WORKER_POOLS dynamic pools 2231 * The worker thread function. All workers belong to a worker_pool -
2216 * of these per each cpu. These workers process all works regardless of 2232 * either a per-cpu one or dynamic unbound one. These workers process all
2217 * their specific target workqueue. The only exception is works which 2233 * work items regardless of their specific target workqueue. The only
2218 * belong to workqueues with a rescuer which will be explained in 2234 * exception is work items which belong to workqueues with a rescuer which
2219 * rescuer_thread(). 2235 * will be explained in rescuer_thread().
2220 */ 2236 */
2221static int worker_thread(void *__worker) 2237static int worker_thread(void *__worker)
2222{ 2238{
@@ -2228,19 +2244,12 @@ static int worker_thread(void *__worker)
2228woke_up: 2244woke_up:
2229 spin_lock_irq(&pool->lock); 2245 spin_lock_irq(&pool->lock);
2230 2246
2231 /* we are off idle list if destruction or rebind is requested */ 2247 /* am I supposed to die? */
2232 if (unlikely(list_empty(&worker->entry))) { 2248 if (unlikely(worker->flags & WORKER_DIE)) {
2233 spin_unlock_irq(&pool->lock); 2249 spin_unlock_irq(&pool->lock);
2234 2250 WARN_ON_ONCE(!list_empty(&worker->entry));
2235 /* if DIE is set, destruction is requested */ 2251 worker->task->flags &= ~PF_WQ_WORKER;
2236 if (worker->flags & WORKER_DIE) { 2252 return 0;
2237 worker->task->flags &= ~PF_WQ_WORKER;
2238 return 0;
2239 }
2240
2241 /* otherwise, rebind */
2242 idle_worker_rebind(worker);
2243 goto woke_up;
2244 } 2253 }
2245 2254
2246 worker_leave_idle(worker); 2255 worker_leave_idle(worker);
@@ -2258,14 +2267,16 @@ recheck:
2258 * preparing to process a work or actually processing it. 2267 * preparing to process a work or actually processing it.
2259 * Make sure nobody diddled with it while I was sleeping. 2268 * Make sure nobody diddled with it while I was sleeping.
2260 */ 2269 */
2261 BUG_ON(!list_empty(&worker->scheduled)); 2270 WARN_ON_ONCE(!list_empty(&worker->scheduled));
2262 2271
2263 /* 2272 /*
2264 * When control reaches this point, we're guaranteed to have 2273 * Finish PREP stage. We're guaranteed to have at least one idle
2265 * at least one idle worker or that someone else has already 2274 * worker or that someone else has already assumed the manager
2266 * assumed the manager role. 2275 * role. This is where @worker starts participating in concurrency
2276 * management if applicable and concurrency management is restored
2277 * after being rebound. See rebind_workers() for details.
2267 */ 2278 */
2268 worker_clr_flags(worker, WORKER_PREP); 2279 worker_clr_flags(worker, WORKER_PREP | WORKER_REBOUND);
2269 2280
2270 do { 2281 do {
2271 struct work_struct *work = 2282 struct work_struct *work =
@@ -2307,7 +2318,7 @@ sleep:
2307 * @__rescuer: self 2318 * @__rescuer: self
2308 * 2319 *
2309 * Workqueue rescuer thread function. There's one rescuer for each 2320 * Workqueue rescuer thread function. There's one rescuer for each
2310 * workqueue which has WQ_RESCUER set. 2321 * workqueue which has WQ_MEM_RECLAIM set.
2311 * 2322 *
2312 * Regular work processing on a pool may block trying to create a new 2323 * Regular work processing on a pool may block trying to create a new
2313 * worker which uses GFP_KERNEL allocation which has slight chance of 2324 * worker which uses GFP_KERNEL allocation which has slight chance of
@@ -2326,8 +2337,6 @@ static int rescuer_thread(void *__rescuer)
2326 struct worker *rescuer = __rescuer; 2337 struct worker *rescuer = __rescuer;
2327 struct workqueue_struct *wq = rescuer->rescue_wq; 2338 struct workqueue_struct *wq = rescuer->rescue_wq;
2328 struct list_head *scheduled = &rescuer->scheduled; 2339 struct list_head *scheduled = &rescuer->scheduled;
2329 bool is_unbound = wq->flags & WQ_UNBOUND;
2330 unsigned int cpu;
2331 2340
2332 set_user_nice(current, RESCUER_NICE_LEVEL); 2341 set_user_nice(current, RESCUER_NICE_LEVEL);
2333 2342
@@ -2345,28 +2354,29 @@ repeat:
2345 return 0; 2354 return 0;
2346 } 2355 }
2347 2356
2348 /* 2357 /* see whether any pwq is asking for help */
2349 * See whether any cpu is asking for help. Unbounded 2358 spin_lock_irq(&wq_mayday_lock);
2350 * workqueues use cpu 0 in mayday_mask for CPU_UNBOUND. 2359
2351 */ 2360 while (!list_empty(&wq->maydays)) {
2352 for_each_mayday_cpu(cpu, wq->mayday_mask) { 2361 struct pool_workqueue *pwq = list_first_entry(&wq->maydays,
2353 unsigned int tcpu = is_unbound ? WORK_CPU_UNBOUND : cpu; 2362 struct pool_workqueue, mayday_node);
2354 struct pool_workqueue *pwq = get_pwq(tcpu, wq);
2355 struct worker_pool *pool = pwq->pool; 2363 struct worker_pool *pool = pwq->pool;
2356 struct work_struct *work, *n; 2364 struct work_struct *work, *n;
2357 2365
2358 __set_current_state(TASK_RUNNING); 2366 __set_current_state(TASK_RUNNING);
2359 mayday_clear_cpu(cpu, wq->mayday_mask); 2367 list_del_init(&pwq->mayday_node);
2368
2369 spin_unlock_irq(&wq_mayday_lock);
2360 2370
2361 /* migrate to the target cpu if possible */ 2371 /* migrate to the target cpu if possible */
2372 worker_maybe_bind_and_lock(pool);
2362 rescuer->pool = pool; 2373 rescuer->pool = pool;
2363 worker_maybe_bind_and_lock(rescuer);
2364 2374
2365 /* 2375 /*
2366 * Slurp in all works issued via this workqueue and 2376 * Slurp in all works issued via this workqueue and
2367 * process'em. 2377 * process'em.
2368 */ 2378 */
2369 BUG_ON(!list_empty(&rescuer->scheduled)); 2379 WARN_ON_ONCE(!list_empty(&rescuer->scheduled));
2370 list_for_each_entry_safe(work, n, &pool->worklist, entry) 2380 list_for_each_entry_safe(work, n, &pool->worklist, entry)
2371 if (get_work_pwq(work) == pwq) 2381 if (get_work_pwq(work) == pwq)
2372 move_linked_works(work, scheduled, &n); 2382 move_linked_works(work, scheduled, &n);
@@ -2381,9 +2391,13 @@ repeat:
2381 if (keep_working(pool)) 2391 if (keep_working(pool))
2382 wake_up_worker(pool); 2392 wake_up_worker(pool);
2383 2393
2384 spin_unlock_irq(&pool->lock); 2394 rescuer->pool = NULL;
2395 spin_unlock(&pool->lock);
2396 spin_lock(&wq_mayday_lock);
2385 } 2397 }
2386 2398
2399 spin_unlock_irq(&wq_mayday_lock);
2400
2387 /* rescuers should never participate in concurrency management */ 2401 /* rescuers should never participate in concurrency management */
2388 WARN_ON_ONCE(!(rescuer->flags & WORKER_NOT_RUNNING)); 2402 WARN_ON_ONCE(!(rescuer->flags & WORKER_NOT_RUNNING));
2389 schedule(); 2403 schedule();
@@ -2487,7 +2501,7 @@ static void insert_wq_barrier(struct pool_workqueue *pwq,
2487 * advanced to @work_color. 2501 * advanced to @work_color.
2488 * 2502 *
2489 * CONTEXT: 2503 * CONTEXT:
2490 * mutex_lock(wq->flush_mutex). 2504 * mutex_lock(wq->mutex).
2491 * 2505 *
2492 * RETURNS: 2506 * RETURNS:
2493 * %true if @flush_color >= 0 and there's something to flush. %false 2507 * %true if @flush_color >= 0 and there's something to flush. %false
@@ -2497,21 +2511,20 @@ static bool flush_workqueue_prep_pwqs(struct workqueue_struct *wq,
2497 int flush_color, int work_color) 2511 int flush_color, int work_color)
2498{ 2512{
2499 bool wait = false; 2513 bool wait = false;
2500 unsigned int cpu; 2514 struct pool_workqueue *pwq;
2501 2515
2502 if (flush_color >= 0) { 2516 if (flush_color >= 0) {
2503 BUG_ON(atomic_read(&wq->nr_pwqs_to_flush)); 2517 WARN_ON_ONCE(atomic_read(&wq->nr_pwqs_to_flush));
2504 atomic_set(&wq->nr_pwqs_to_flush, 1); 2518 atomic_set(&wq->nr_pwqs_to_flush, 1);
2505 } 2519 }
2506 2520
2507 for_each_pwq_cpu(cpu, wq) { 2521 for_each_pwq(pwq, wq) {
2508 struct pool_workqueue *pwq = get_pwq(cpu, wq);
2509 struct worker_pool *pool = pwq->pool; 2522 struct worker_pool *pool = pwq->pool;
2510 2523
2511 spin_lock_irq(&pool->lock); 2524 spin_lock_irq(&pool->lock);
2512 2525
2513 if (flush_color >= 0) { 2526 if (flush_color >= 0) {
2514 BUG_ON(pwq->flush_color != -1); 2527 WARN_ON_ONCE(pwq->flush_color != -1);
2515 2528
2516 if (pwq->nr_in_flight[flush_color]) { 2529 if (pwq->nr_in_flight[flush_color]) {
2517 pwq->flush_color = flush_color; 2530 pwq->flush_color = flush_color;
@@ -2521,7 +2534,7 @@ static bool flush_workqueue_prep_pwqs(struct workqueue_struct *wq,
2521 } 2534 }
2522 2535
2523 if (work_color >= 0) { 2536 if (work_color >= 0) {
2524 BUG_ON(work_color != work_next_color(pwq->work_color)); 2537 WARN_ON_ONCE(work_color != work_next_color(pwq->work_color));
2525 pwq->work_color = work_color; 2538 pwq->work_color = work_color;
2526 } 2539 }
2527 2540
@@ -2538,11 +2551,8 @@ static bool flush_workqueue_prep_pwqs(struct workqueue_struct *wq,
2538 * flush_workqueue - ensure that any scheduled work has run to completion. 2551 * flush_workqueue - ensure that any scheduled work has run to completion.
2539 * @wq: workqueue to flush 2552 * @wq: workqueue to flush
2540 * 2553 *
2541 * Forces execution of the workqueue and blocks until its completion. 2554 * This function sleeps until all work items which were queued on entry
2542 * This is typically used in driver shutdown handlers. 2555 * have finished execution, but it is not livelocked by new incoming ones.
2543 *
2544 * We sleep until all works which were queued on entry have been handled,
2545 * but we are not livelocked by new incoming ones.
2546 */ 2556 */
2547void flush_workqueue(struct workqueue_struct *wq) 2557void flush_workqueue(struct workqueue_struct *wq)
2548{ 2558{
@@ -2556,7 +2566,7 @@ void flush_workqueue(struct workqueue_struct *wq)
2556 lock_map_acquire(&wq->lockdep_map); 2566 lock_map_acquire(&wq->lockdep_map);
2557 lock_map_release(&wq->lockdep_map); 2567 lock_map_release(&wq->lockdep_map);
2558 2568
2559 mutex_lock(&wq->flush_mutex); 2569 mutex_lock(&wq->mutex);
2560 2570
2561 /* 2571 /*
2562 * Start-to-wait phase 2572 * Start-to-wait phase
@@ -2569,13 +2579,13 @@ void flush_workqueue(struct workqueue_struct *wq)
2569 * becomes our flush_color and work_color is advanced 2579 * becomes our flush_color and work_color is advanced
2570 * by one. 2580 * by one.
2571 */ 2581 */
2572 BUG_ON(!list_empty(&wq->flusher_overflow)); 2582 WARN_ON_ONCE(!list_empty(&wq->flusher_overflow));
2573 this_flusher.flush_color = wq->work_color; 2583 this_flusher.flush_color = wq->work_color;
2574 wq->work_color = next_color; 2584 wq->work_color = next_color;
2575 2585
2576 if (!wq->first_flusher) { 2586 if (!wq->first_flusher) {
2577 /* no flush in progress, become the first flusher */ 2587 /* no flush in progress, become the first flusher */
2578 BUG_ON(wq->flush_color != this_flusher.flush_color); 2588 WARN_ON_ONCE(wq->flush_color != this_flusher.flush_color);
2579 2589
2580 wq->first_flusher = &this_flusher; 2590 wq->first_flusher = &this_flusher;
2581 2591
@@ -2588,7 +2598,7 @@ void flush_workqueue(struct workqueue_struct *wq)
2588 } 2598 }
2589 } else { 2599 } else {
2590 /* wait in queue */ 2600 /* wait in queue */
2591 BUG_ON(wq->flush_color == this_flusher.flush_color); 2601 WARN_ON_ONCE(wq->flush_color == this_flusher.flush_color);
2592 list_add_tail(&this_flusher.list, &wq->flusher_queue); 2602 list_add_tail(&this_flusher.list, &wq->flusher_queue);
2593 flush_workqueue_prep_pwqs(wq, -1, wq->work_color); 2603 flush_workqueue_prep_pwqs(wq, -1, wq->work_color);
2594 } 2604 }
@@ -2601,7 +2611,7 @@ void flush_workqueue(struct workqueue_struct *wq)
2601 list_add_tail(&this_flusher.list, &wq->flusher_overflow); 2611 list_add_tail(&this_flusher.list, &wq->flusher_overflow);
2602 } 2612 }
2603 2613
2604 mutex_unlock(&wq->flush_mutex); 2614 mutex_unlock(&wq->mutex);
2605 2615
2606 wait_for_completion(&this_flusher.done); 2616 wait_for_completion(&this_flusher.done);
2607 2617
@@ -2614,7 +2624,7 @@ void flush_workqueue(struct workqueue_struct *wq)
2614 if (wq->first_flusher != &this_flusher) 2624 if (wq->first_flusher != &this_flusher)
2615 return; 2625 return;
2616 2626
2617 mutex_lock(&wq->flush_mutex); 2627 mutex_lock(&wq->mutex);
2618 2628
2619 /* we might have raced, check again with mutex held */ 2629 /* we might have raced, check again with mutex held */
2620 if (wq->first_flusher != &this_flusher) 2630 if (wq->first_flusher != &this_flusher)
@@ -2622,8 +2632,8 @@ void flush_workqueue(struct workqueue_struct *wq)
2622 2632
2623 wq->first_flusher = NULL; 2633 wq->first_flusher = NULL;
2624 2634
2625 BUG_ON(!list_empty(&this_flusher.list)); 2635 WARN_ON_ONCE(!list_empty(&this_flusher.list));
2626 BUG_ON(wq->flush_color != this_flusher.flush_color); 2636 WARN_ON_ONCE(wq->flush_color != this_flusher.flush_color);
2627 2637
2628 while (true) { 2638 while (true) {
2629 struct wq_flusher *next, *tmp; 2639 struct wq_flusher *next, *tmp;
@@ -2636,8 +2646,8 @@ void flush_workqueue(struct workqueue_struct *wq)
2636 complete(&next->done); 2646 complete(&next->done);
2637 } 2647 }
2638 2648
2639 BUG_ON(!list_empty(&wq->flusher_overflow) && 2649 WARN_ON_ONCE(!list_empty(&wq->flusher_overflow) &&
2640 wq->flush_color != work_next_color(wq->work_color)); 2650 wq->flush_color != work_next_color(wq->work_color));
2641 2651
2642 /* this flush_color is finished, advance by one */ 2652 /* this flush_color is finished, advance by one */
2643 wq->flush_color = work_next_color(wq->flush_color); 2653 wq->flush_color = work_next_color(wq->flush_color);
@@ -2661,7 +2671,7 @@ void flush_workqueue(struct workqueue_struct *wq)
2661 } 2671 }
2662 2672
2663 if (list_empty(&wq->flusher_queue)) { 2673 if (list_empty(&wq->flusher_queue)) {
2664 BUG_ON(wq->flush_color != wq->work_color); 2674 WARN_ON_ONCE(wq->flush_color != wq->work_color);
2665 break; 2675 break;
2666 } 2676 }
2667 2677
@@ -2669,8 +2679,8 @@ void flush_workqueue(struct workqueue_struct *wq)
2669 * Need to flush more colors. Make the next flusher 2679 * Need to flush more colors. Make the next flusher
2670 * the new first flusher and arm pwqs. 2680 * the new first flusher and arm pwqs.
2671 */ 2681 */
2672 BUG_ON(wq->flush_color == wq->work_color); 2682 WARN_ON_ONCE(wq->flush_color == wq->work_color);
2673 BUG_ON(wq->flush_color != next->flush_color); 2683 WARN_ON_ONCE(wq->flush_color != next->flush_color);
2674 2684
2675 list_del_init(&next->list); 2685 list_del_init(&next->list);
2676 wq->first_flusher = next; 2686 wq->first_flusher = next;
@@ -2686,7 +2696,7 @@ void flush_workqueue(struct workqueue_struct *wq)
2686 } 2696 }
2687 2697
2688out_unlock: 2698out_unlock:
2689 mutex_unlock(&wq->flush_mutex); 2699 mutex_unlock(&wq->mutex);
2690} 2700}
2691EXPORT_SYMBOL_GPL(flush_workqueue); 2701EXPORT_SYMBOL_GPL(flush_workqueue);
2692 2702
@@ -2704,22 +2714,23 @@ EXPORT_SYMBOL_GPL(flush_workqueue);
2704void drain_workqueue(struct workqueue_struct *wq) 2714void drain_workqueue(struct workqueue_struct *wq)
2705{ 2715{
2706 unsigned int flush_cnt = 0; 2716 unsigned int flush_cnt = 0;
2707 unsigned int cpu; 2717 struct pool_workqueue *pwq;
2708 2718
2709 /* 2719 /*
2710 * __queue_work() needs to test whether there are drainers, is much 2720 * __queue_work() needs to test whether there are drainers, is much
2711 * hotter than drain_workqueue() and already looks at @wq->flags. 2721 * hotter than drain_workqueue() and already looks at @wq->flags.
2712 * Use WQ_DRAINING so that queue doesn't have to check nr_drainers. 2722 * Use __WQ_DRAINING so that queue doesn't have to check nr_drainers.
2713 */ 2723 */
2714 spin_lock(&workqueue_lock); 2724 mutex_lock(&wq->mutex);
2715 if (!wq->nr_drainers++) 2725 if (!wq->nr_drainers++)
2716 wq->flags |= WQ_DRAINING; 2726 wq->flags |= __WQ_DRAINING;
2717 spin_unlock(&workqueue_lock); 2727 mutex_unlock(&wq->mutex);
2718reflush: 2728reflush:
2719 flush_workqueue(wq); 2729 flush_workqueue(wq);
2720 2730
2721 for_each_pwq_cpu(cpu, wq) { 2731 mutex_lock(&wq->mutex);
2722 struct pool_workqueue *pwq = get_pwq(cpu, wq); 2732
2733 for_each_pwq(pwq, wq) {
2723 bool drained; 2734 bool drained;
2724 2735
2725 spin_lock_irq(&pwq->pool->lock); 2736 spin_lock_irq(&pwq->pool->lock);
@@ -2731,15 +2742,16 @@ reflush:
2731 2742
2732 if (++flush_cnt == 10 || 2743 if (++flush_cnt == 10 ||
2733 (flush_cnt % 100 == 0 && flush_cnt <= 1000)) 2744 (flush_cnt % 100 == 0 && flush_cnt <= 1000))
2734 pr_warn("workqueue %s: flush on destruction isn't complete after %u tries\n", 2745 pr_warn("workqueue %s: drain_workqueue() isn't complete after %u tries\n",
2735 wq->name, flush_cnt); 2746 wq->name, flush_cnt);
2747
2748 mutex_unlock(&wq->mutex);
2736 goto reflush; 2749 goto reflush;
2737 } 2750 }
2738 2751
2739 spin_lock(&workqueue_lock);
2740 if (!--wq->nr_drainers) 2752 if (!--wq->nr_drainers)
2741 wq->flags &= ~WQ_DRAINING; 2753 wq->flags &= ~__WQ_DRAINING;
2742 spin_unlock(&workqueue_lock); 2754 mutex_unlock(&wq->mutex);
2743} 2755}
2744EXPORT_SYMBOL_GPL(drain_workqueue); 2756EXPORT_SYMBOL_GPL(drain_workqueue);
2745 2757
@@ -2750,11 +2762,15 @@ static bool start_flush_work(struct work_struct *work, struct wq_barrier *barr)
2750 struct pool_workqueue *pwq; 2762 struct pool_workqueue *pwq;
2751 2763
2752 might_sleep(); 2764 might_sleep();
2765
2766 local_irq_disable();
2753 pool = get_work_pool(work); 2767 pool = get_work_pool(work);
2754 if (!pool) 2768 if (!pool) {
2769 local_irq_enable();
2755 return false; 2770 return false;
2771 }
2756 2772
2757 spin_lock_irq(&pool->lock); 2773 spin_lock(&pool->lock);
2758 /* see the comment in try_to_grab_pending() with the same code */ 2774 /* see the comment in try_to_grab_pending() with the same code */
2759 pwq = get_work_pwq(work); 2775 pwq = get_work_pwq(work);
2760 if (pwq) { 2776 if (pwq) {
@@ -2776,7 +2792,7 @@ static bool start_flush_work(struct work_struct *work, struct wq_barrier *barr)
2776 * flusher is not running on the same workqueue by verifying write 2792 * flusher is not running on the same workqueue by verifying write
2777 * access. 2793 * access.
2778 */ 2794 */
2779 if (pwq->wq->saved_max_active == 1 || pwq->wq->flags & WQ_RESCUER) 2795 if (pwq->wq->saved_max_active == 1 || pwq->wq->rescuer)
2780 lock_map_acquire(&pwq->wq->lockdep_map); 2796 lock_map_acquire(&pwq->wq->lockdep_map);
2781 else 2797 else
2782 lock_map_acquire_read(&pwq->wq->lockdep_map); 2798 lock_map_acquire_read(&pwq->wq->lockdep_map);
@@ -2933,66 +2949,6 @@ bool cancel_delayed_work_sync(struct delayed_work *dwork)
2933EXPORT_SYMBOL(cancel_delayed_work_sync); 2949EXPORT_SYMBOL(cancel_delayed_work_sync);
2934 2950
2935/** 2951/**
2936 * schedule_work_on - put work task on a specific cpu
2937 * @cpu: cpu to put the work task on
2938 * @work: job to be done
2939 *
2940 * This puts a job on a specific cpu
2941 */
2942bool schedule_work_on(int cpu, struct work_struct *work)
2943{
2944 return queue_work_on(cpu, system_wq, work);
2945}
2946EXPORT_SYMBOL(schedule_work_on);
2947
2948/**
2949 * schedule_work - put work task in global workqueue
2950 * @work: job to be done
2951 *
2952 * Returns %false if @work was already on the kernel-global workqueue and
2953 * %true otherwise.
2954 *
2955 * This puts a job in the kernel-global workqueue if it was not already
2956 * queued and leaves it in the same position on the kernel-global
2957 * workqueue otherwise.
2958 */
2959bool schedule_work(struct work_struct *work)
2960{
2961 return queue_work(system_wq, work);
2962}
2963EXPORT_SYMBOL(schedule_work);
2964
2965/**
2966 * schedule_delayed_work_on - queue work in global workqueue on CPU after delay
2967 * @cpu: cpu to use
2968 * @dwork: job to be done
2969 * @delay: number of jiffies to wait
2970 *
2971 * After waiting for a given time this puts a job in the kernel-global
2972 * workqueue on the specified CPU.
2973 */
2974bool schedule_delayed_work_on(int cpu, struct delayed_work *dwork,
2975 unsigned long delay)
2976{
2977 return queue_delayed_work_on(cpu, system_wq, dwork, delay);
2978}
2979EXPORT_SYMBOL(schedule_delayed_work_on);
2980
2981/**
2982 * schedule_delayed_work - put work task in global workqueue after delay
2983 * @dwork: job to be done
2984 * @delay: number of jiffies to wait or 0 for immediate execution
2985 *
2986 * After waiting for a given time this puts a job in the kernel-global
2987 * workqueue.
2988 */
2989bool schedule_delayed_work(struct delayed_work *dwork, unsigned long delay)
2990{
2991 return queue_delayed_work(system_wq, dwork, delay);
2992}
2993EXPORT_SYMBOL(schedule_delayed_work);
2994
2995/**
2996 * schedule_on_each_cpu - execute a function synchronously on each online CPU 2952 * schedule_on_each_cpu - execute a function synchronously on each online CPU
2997 * @func: the function to call 2953 * @func: the function to call
2998 * 2954 *
@@ -3085,51 +3041,1025 @@ int execute_in_process_context(work_func_t fn, struct execute_work *ew)
3085} 3041}
3086EXPORT_SYMBOL_GPL(execute_in_process_context); 3042EXPORT_SYMBOL_GPL(execute_in_process_context);
3087 3043
3088int keventd_up(void) 3044#ifdef CONFIG_SYSFS
3045/*
3046 * Workqueues with WQ_SYSFS flag set is visible to userland via
3047 * /sys/bus/workqueue/devices/WQ_NAME. All visible workqueues have the
3048 * following attributes.
3049 *
3050 * per_cpu RO bool : whether the workqueue is per-cpu or unbound
3051 * max_active RW int : maximum number of in-flight work items
3052 *
3053 * Unbound workqueues have the following extra attributes.
3054 *
3055 * id RO int : the associated pool ID
3056 * nice RW int : nice value of the workers
3057 * cpumask RW mask : bitmask of allowed CPUs for the workers
3058 */
3059struct wq_device {
3060 struct workqueue_struct *wq;
3061 struct device dev;
3062};
3063
3064static struct workqueue_struct *dev_to_wq(struct device *dev)
3065{
3066 struct wq_device *wq_dev = container_of(dev, struct wq_device, dev);
3067
3068 return wq_dev->wq;
3069}
3070
3071static ssize_t wq_per_cpu_show(struct device *dev,
3072 struct device_attribute *attr, char *buf)
3073{
3074 struct workqueue_struct *wq = dev_to_wq(dev);
3075
3076 return scnprintf(buf, PAGE_SIZE, "%d\n", (bool)!(wq->flags & WQ_UNBOUND));
3077}
3078
3079static ssize_t wq_max_active_show(struct device *dev,
3080 struct device_attribute *attr, char *buf)
3089{ 3081{
3090 return system_wq != NULL; 3082 struct workqueue_struct *wq = dev_to_wq(dev);
3083
3084 return scnprintf(buf, PAGE_SIZE, "%d\n", wq->saved_max_active);
3091} 3085}
3092 3086
3093static int alloc_pwqs(struct workqueue_struct *wq) 3087static ssize_t wq_max_active_store(struct device *dev,
3088 struct device_attribute *attr,
3089 const char *buf, size_t count)
3094{ 3090{
3091 struct workqueue_struct *wq = dev_to_wq(dev);
3092 int val;
3093
3094 if (sscanf(buf, "%d", &val) != 1 || val <= 0)
3095 return -EINVAL;
3096
3097 workqueue_set_max_active(wq, val);
3098 return count;
3099}
3100
3101static struct device_attribute wq_sysfs_attrs[] = {
3102 __ATTR(per_cpu, 0444, wq_per_cpu_show, NULL),
3103 __ATTR(max_active, 0644, wq_max_active_show, wq_max_active_store),
3104 __ATTR_NULL,
3105};
3106
3107static ssize_t wq_pool_ids_show(struct device *dev,
3108 struct device_attribute *attr, char *buf)
3109{
3110 struct workqueue_struct *wq = dev_to_wq(dev);
3111 const char *delim = "";
3112 int node, written = 0;
3113
3114 rcu_read_lock_sched();
3115 for_each_node(node) {
3116 written += scnprintf(buf + written, PAGE_SIZE - written,
3117 "%s%d:%d", delim, node,
3118 unbound_pwq_by_node(wq, node)->pool->id);
3119 delim = " ";
3120 }
3121 written += scnprintf(buf + written, PAGE_SIZE - written, "\n");
3122 rcu_read_unlock_sched();
3123
3124 return written;
3125}
3126
3127static ssize_t wq_nice_show(struct device *dev, struct device_attribute *attr,
3128 char *buf)
3129{
3130 struct workqueue_struct *wq = dev_to_wq(dev);
3131 int written;
3132
3133 mutex_lock(&wq->mutex);
3134 written = scnprintf(buf, PAGE_SIZE, "%d\n", wq->unbound_attrs->nice);
3135 mutex_unlock(&wq->mutex);
3136
3137 return written;
3138}
3139
3140/* prepare workqueue_attrs for sysfs store operations */
3141static struct workqueue_attrs *wq_sysfs_prep_attrs(struct workqueue_struct *wq)
3142{
3143 struct workqueue_attrs *attrs;
3144
3145 attrs = alloc_workqueue_attrs(GFP_KERNEL);
3146 if (!attrs)
3147 return NULL;
3148
3149 mutex_lock(&wq->mutex);
3150 copy_workqueue_attrs(attrs, wq->unbound_attrs);
3151 mutex_unlock(&wq->mutex);
3152 return attrs;
3153}
3154
3155static ssize_t wq_nice_store(struct device *dev, struct device_attribute *attr,
3156 const char *buf, size_t count)
3157{
3158 struct workqueue_struct *wq = dev_to_wq(dev);
3159 struct workqueue_attrs *attrs;
3160 int ret;
3161
3162 attrs = wq_sysfs_prep_attrs(wq);
3163 if (!attrs)
3164 return -ENOMEM;
3165
3166 if (sscanf(buf, "%d", &attrs->nice) == 1 &&
3167 attrs->nice >= -20 && attrs->nice <= 19)
3168 ret = apply_workqueue_attrs(wq, attrs);
3169 else
3170 ret = -EINVAL;
3171
3172 free_workqueue_attrs(attrs);
3173 return ret ?: count;
3174}
3175
3176static ssize_t wq_cpumask_show(struct device *dev,
3177 struct device_attribute *attr, char *buf)
3178{
3179 struct workqueue_struct *wq = dev_to_wq(dev);
3180 int written;
3181
3182 mutex_lock(&wq->mutex);
3183 written = cpumask_scnprintf(buf, PAGE_SIZE, wq->unbound_attrs->cpumask);
3184 mutex_unlock(&wq->mutex);
3185
3186 written += scnprintf(buf + written, PAGE_SIZE - written, "\n");
3187 return written;
3188}
3189
3190static ssize_t wq_cpumask_store(struct device *dev,
3191 struct device_attribute *attr,
3192 const char *buf, size_t count)
3193{
3194 struct workqueue_struct *wq = dev_to_wq(dev);
3195 struct workqueue_attrs *attrs;
3196 int ret;
3197
3198 attrs = wq_sysfs_prep_attrs(wq);
3199 if (!attrs)
3200 return -ENOMEM;
3201
3202 ret = cpumask_parse(buf, attrs->cpumask);
3203 if (!ret)
3204 ret = apply_workqueue_attrs(wq, attrs);
3205
3206 free_workqueue_attrs(attrs);
3207 return ret ?: count;
3208}
3209
3210static ssize_t wq_numa_show(struct device *dev, struct device_attribute *attr,
3211 char *buf)
3212{
3213 struct workqueue_struct *wq = dev_to_wq(dev);
3214 int written;
3215
3216 mutex_lock(&wq->mutex);
3217 written = scnprintf(buf, PAGE_SIZE, "%d\n",
3218 !wq->unbound_attrs->no_numa);
3219 mutex_unlock(&wq->mutex);
3220
3221 return written;
3222}
3223
3224static ssize_t wq_numa_store(struct device *dev, struct device_attribute *attr,
3225 const char *buf, size_t count)
3226{
3227 struct workqueue_struct *wq = dev_to_wq(dev);
3228 struct workqueue_attrs *attrs;
3229 int v, ret;
3230
3231 attrs = wq_sysfs_prep_attrs(wq);
3232 if (!attrs)
3233 return -ENOMEM;
3234
3235 ret = -EINVAL;
3236 if (sscanf(buf, "%d", &v) == 1) {
3237 attrs->no_numa = !v;
3238 ret = apply_workqueue_attrs(wq, attrs);
3239 }
3240
3241 free_workqueue_attrs(attrs);
3242 return ret ?: count;
3243}
3244
3245static struct device_attribute wq_sysfs_unbound_attrs[] = {
3246 __ATTR(pool_ids, 0444, wq_pool_ids_show, NULL),
3247 __ATTR(nice, 0644, wq_nice_show, wq_nice_store),
3248 __ATTR(cpumask, 0644, wq_cpumask_show, wq_cpumask_store),
3249 __ATTR(numa, 0644, wq_numa_show, wq_numa_store),
3250 __ATTR_NULL,
3251};
3252
3253static struct bus_type wq_subsys = {
3254 .name = "workqueue",
3255 .dev_attrs = wq_sysfs_attrs,
3256};
3257
3258static int __init wq_sysfs_init(void)
3259{
3260 return subsys_virtual_register(&wq_subsys, NULL);
3261}
3262core_initcall(wq_sysfs_init);
3263
3264static void wq_device_release(struct device *dev)
3265{
3266 struct wq_device *wq_dev = container_of(dev, struct wq_device, dev);
3267
3268 kfree(wq_dev);
3269}
3270
3271/**
3272 * workqueue_sysfs_register - make a workqueue visible in sysfs
3273 * @wq: the workqueue to register
3274 *
3275 * Expose @wq in sysfs under /sys/bus/workqueue/devices.
3276 * alloc_workqueue*() automatically calls this function if WQ_SYSFS is set
3277 * which is the preferred method.
3278 *
3279 * Workqueue user should use this function directly iff it wants to apply
3280 * workqueue_attrs before making the workqueue visible in sysfs; otherwise,
3281 * apply_workqueue_attrs() may race against userland updating the
3282 * attributes.
3283 *
3284 * Returns 0 on success, -errno on failure.
3285 */
3286int workqueue_sysfs_register(struct workqueue_struct *wq)
3287{
3288 struct wq_device *wq_dev;
3289 int ret;
3290
3095 /* 3291 /*
3096 * pwqs are forced aligned according to WORK_STRUCT_FLAG_BITS. 3292 * Adjusting max_active or creating new pwqs by applyting
3097 * Make sure that the alignment isn't lower than that of 3293 * attributes breaks ordering guarantee. Disallow exposing ordered
3098 * unsigned long long. 3294 * workqueues.
3099 */ 3295 */
3100 const size_t size = sizeof(struct pool_workqueue); 3296 if (WARN_ON(wq->flags & __WQ_ORDERED))
3101 const size_t align = max_t(size_t, 1 << WORK_STRUCT_FLAG_BITS, 3297 return -EINVAL;
3102 __alignof__(unsigned long long));
3103 3298
3104 if (!(wq->flags & WQ_UNBOUND)) 3299 wq->wq_dev = wq_dev = kzalloc(sizeof(*wq_dev), GFP_KERNEL);
3105 wq->pool_wq.pcpu = __alloc_percpu(size, align); 3300 if (!wq_dev)
3106 else { 3301 return -ENOMEM;
3107 void *ptr; 3302
3303 wq_dev->wq = wq;
3304 wq_dev->dev.bus = &wq_subsys;
3305 wq_dev->dev.init_name = wq->name;
3306 wq_dev->dev.release = wq_device_release;
3307
3308 /*
3309 * unbound_attrs are created separately. Suppress uevent until
3310 * everything is ready.
3311 */
3312 dev_set_uevent_suppress(&wq_dev->dev, true);
3313
3314 ret = device_register(&wq_dev->dev);
3315 if (ret) {
3316 kfree(wq_dev);
3317 wq->wq_dev = NULL;
3318 return ret;
3319 }
3320
3321 if (wq->flags & WQ_UNBOUND) {
3322 struct device_attribute *attr;
3323
3324 for (attr = wq_sysfs_unbound_attrs; attr->attr.name; attr++) {
3325 ret = device_create_file(&wq_dev->dev, attr);
3326 if (ret) {
3327 device_unregister(&wq_dev->dev);
3328 wq->wq_dev = NULL;
3329 return ret;
3330 }
3331 }
3332 }
3333
3334 kobject_uevent(&wq_dev->dev.kobj, KOBJ_ADD);
3335 return 0;
3336}
3337
3338/**
3339 * workqueue_sysfs_unregister - undo workqueue_sysfs_register()
3340 * @wq: the workqueue to unregister
3341 *
3342 * If @wq is registered to sysfs by workqueue_sysfs_register(), unregister.
3343 */
3344static void workqueue_sysfs_unregister(struct workqueue_struct *wq)
3345{
3346 struct wq_device *wq_dev = wq->wq_dev;
3347
3348 if (!wq->wq_dev)
3349 return;
3350
3351 wq->wq_dev = NULL;
3352 device_unregister(&wq_dev->dev);
3353}
3354#else /* CONFIG_SYSFS */
3355static void workqueue_sysfs_unregister(struct workqueue_struct *wq) { }
3356#endif /* CONFIG_SYSFS */
3357
3358/**
3359 * free_workqueue_attrs - free a workqueue_attrs
3360 * @attrs: workqueue_attrs to free
3361 *
3362 * Undo alloc_workqueue_attrs().
3363 */
3364void free_workqueue_attrs(struct workqueue_attrs *attrs)
3365{
3366 if (attrs) {
3367 free_cpumask_var(attrs->cpumask);
3368 kfree(attrs);
3369 }
3370}
3371
3372/**
3373 * alloc_workqueue_attrs - allocate a workqueue_attrs
3374 * @gfp_mask: allocation mask to use
3375 *
3376 * Allocate a new workqueue_attrs, initialize with default settings and
3377 * return it. Returns NULL on failure.
3378 */
3379struct workqueue_attrs *alloc_workqueue_attrs(gfp_t gfp_mask)
3380{
3381 struct workqueue_attrs *attrs;
3382
3383 attrs = kzalloc(sizeof(*attrs), gfp_mask);
3384 if (!attrs)
3385 goto fail;
3386 if (!alloc_cpumask_var(&attrs->cpumask, gfp_mask))
3387 goto fail;
3388
3389 cpumask_copy(attrs->cpumask, cpu_possible_mask);
3390 return attrs;
3391fail:
3392 free_workqueue_attrs(attrs);
3393 return NULL;
3394}
3395
3396static void copy_workqueue_attrs(struct workqueue_attrs *to,
3397 const struct workqueue_attrs *from)
3398{
3399 to->nice = from->nice;
3400 cpumask_copy(to->cpumask, from->cpumask);
3401}
3402
3403/* hash value of the content of @attr */
3404static u32 wqattrs_hash(const struct workqueue_attrs *attrs)
3405{
3406 u32 hash = 0;
3407
3408 hash = jhash_1word(attrs->nice, hash);
3409 hash = jhash(cpumask_bits(attrs->cpumask),
3410 BITS_TO_LONGS(nr_cpumask_bits) * sizeof(long), hash);
3411 return hash;
3412}
3413
3414/* content equality test */
3415static bool wqattrs_equal(const struct workqueue_attrs *a,
3416 const struct workqueue_attrs *b)
3417{
3418 if (a->nice != b->nice)
3419 return false;
3420 if (!cpumask_equal(a->cpumask, b->cpumask))
3421 return false;
3422 return true;
3423}
3424
3425/**
3426 * init_worker_pool - initialize a newly zalloc'd worker_pool
3427 * @pool: worker_pool to initialize
3428 *
3429 * Initiailize a newly zalloc'd @pool. It also allocates @pool->attrs.
3430 * Returns 0 on success, -errno on failure. Even on failure, all fields
3431 * inside @pool proper are initialized and put_unbound_pool() can be called
3432 * on @pool safely to release it.
3433 */
3434static int init_worker_pool(struct worker_pool *pool)
3435{
3436 spin_lock_init(&pool->lock);
3437 pool->id = -1;
3438 pool->cpu = -1;
3439 pool->node = NUMA_NO_NODE;
3440 pool->flags |= POOL_DISASSOCIATED;
3441 INIT_LIST_HEAD(&pool->worklist);
3442 INIT_LIST_HEAD(&pool->idle_list);
3443 hash_init(pool->busy_hash);
3444
3445 init_timer_deferrable(&pool->idle_timer);
3446 pool->idle_timer.function = idle_worker_timeout;
3447 pool->idle_timer.data = (unsigned long)pool;
3448
3449 setup_timer(&pool->mayday_timer, pool_mayday_timeout,
3450 (unsigned long)pool);
3451
3452 mutex_init(&pool->manager_arb);
3453 mutex_init(&pool->manager_mutex);
3454 idr_init(&pool->worker_idr);
3455
3456 INIT_HLIST_NODE(&pool->hash_node);
3457 pool->refcnt = 1;
3458
3459 /* shouldn't fail above this point */
3460 pool->attrs = alloc_workqueue_attrs(GFP_KERNEL);
3461 if (!pool->attrs)
3462 return -ENOMEM;
3463 return 0;
3464}
3465
3466static void rcu_free_pool(struct rcu_head *rcu)
3467{
3468 struct worker_pool *pool = container_of(rcu, struct worker_pool, rcu);
3469
3470 idr_destroy(&pool->worker_idr);
3471 free_workqueue_attrs(pool->attrs);
3472 kfree(pool);
3473}
3474
3475/**
3476 * put_unbound_pool - put a worker_pool
3477 * @pool: worker_pool to put
3478 *
3479 * Put @pool. If its refcnt reaches zero, it gets destroyed in sched-RCU
3480 * safe manner. get_unbound_pool() calls this function on its failure path
3481 * and this function should be able to release pools which went through,
3482 * successfully or not, init_worker_pool().
3483 *
3484 * Should be called with wq_pool_mutex held.
3485 */
3486static void put_unbound_pool(struct worker_pool *pool)
3487{
3488 struct worker *worker;
3489
3490 lockdep_assert_held(&wq_pool_mutex);
3491
3492 if (--pool->refcnt)
3493 return;
3494
3495 /* sanity checks */
3496 if (WARN_ON(!(pool->flags & POOL_DISASSOCIATED)) ||
3497 WARN_ON(!list_empty(&pool->worklist)))
3498 return;
3499
3500 /* release id and unhash */
3501 if (pool->id >= 0)
3502 idr_remove(&worker_pool_idr, pool->id);
3503 hash_del(&pool->hash_node);
3504
3505 /*
3506 * Become the manager and destroy all workers. Grabbing
3507 * manager_arb prevents @pool's workers from blocking on
3508 * manager_mutex.
3509 */
3510 mutex_lock(&pool->manager_arb);
3511 mutex_lock(&pool->manager_mutex);
3512 spin_lock_irq(&pool->lock);
3513
3514 while ((worker = first_worker(pool)))
3515 destroy_worker(worker);
3516 WARN_ON(pool->nr_workers || pool->nr_idle);
3517
3518 spin_unlock_irq(&pool->lock);
3519 mutex_unlock(&pool->manager_mutex);
3520 mutex_unlock(&pool->manager_arb);
3521
3522 /* shut down the timers */
3523 del_timer_sync(&pool->idle_timer);
3524 del_timer_sync(&pool->mayday_timer);
3525
3526 /* sched-RCU protected to allow dereferences from get_work_pool() */
3527 call_rcu_sched(&pool->rcu, rcu_free_pool);
3528}
3529
3530/**
3531 * get_unbound_pool - get a worker_pool with the specified attributes
3532 * @attrs: the attributes of the worker_pool to get
3533 *
3534 * Obtain a worker_pool which has the same attributes as @attrs, bump the
3535 * reference count and return it. If there already is a matching
3536 * worker_pool, it will be used; otherwise, this function attempts to
3537 * create a new one. On failure, returns NULL.
3538 *
3539 * Should be called with wq_pool_mutex held.
3540 */
3541static struct worker_pool *get_unbound_pool(const struct workqueue_attrs *attrs)
3542{
3543 u32 hash = wqattrs_hash(attrs);
3544 struct worker_pool *pool;
3545 int node;
3546
3547 lockdep_assert_held(&wq_pool_mutex);
3548
3549 /* do we already have a matching pool? */
3550 hash_for_each_possible(unbound_pool_hash, pool, hash_node, hash) {
3551 if (wqattrs_equal(pool->attrs, attrs)) {
3552 pool->refcnt++;
3553 goto out_unlock;
3554 }
3555 }
3556
3557 /* nope, create a new one */
3558 pool = kzalloc(sizeof(*pool), GFP_KERNEL);
3559 if (!pool || init_worker_pool(pool) < 0)
3560 goto fail;
3561
3562 if (workqueue_freezing)
3563 pool->flags |= POOL_FREEZING;
3564
3565 lockdep_set_subclass(&pool->lock, 1); /* see put_pwq() */
3566 copy_workqueue_attrs(pool->attrs, attrs);
3567
3568 /* if cpumask is contained inside a NUMA node, we belong to that node */
3569 if (wq_numa_enabled) {
3570 for_each_node(node) {
3571 if (cpumask_subset(pool->attrs->cpumask,
3572 wq_numa_possible_cpumask[node])) {
3573 pool->node = node;
3574 break;
3575 }
3576 }
3577 }
3578
3579 if (worker_pool_assign_id(pool) < 0)
3580 goto fail;
3581
3582 /* create and start the initial worker */
3583 if (create_and_start_worker(pool) < 0)
3584 goto fail;
3585
3586 /* install */
3587 hash_add(unbound_pool_hash, &pool->hash_node, hash);
3588out_unlock:
3589 return pool;
3590fail:
3591 if (pool)
3592 put_unbound_pool(pool);
3593 return NULL;
3594}
3595
3596static void rcu_free_pwq(struct rcu_head *rcu)
3597{
3598 kmem_cache_free(pwq_cache,
3599 container_of(rcu, struct pool_workqueue, rcu));
3600}
3601
3602/*
3603 * Scheduled on system_wq by put_pwq() when an unbound pwq hits zero refcnt
3604 * and needs to be destroyed.
3605 */
3606static void pwq_unbound_release_workfn(struct work_struct *work)
3607{
3608 struct pool_workqueue *pwq = container_of(work, struct pool_workqueue,
3609 unbound_release_work);
3610 struct workqueue_struct *wq = pwq->wq;
3611 struct worker_pool *pool = pwq->pool;
3612 bool is_last;
3613
3614 if (WARN_ON_ONCE(!(wq->flags & WQ_UNBOUND)))
3615 return;
3616
3617 /*
3618 * Unlink @pwq. Synchronization against wq->mutex isn't strictly
3619 * necessary on release but do it anyway. It's easier to verify
3620 * and consistent with the linking path.
3621 */
3622 mutex_lock(&wq->mutex);
3623 list_del_rcu(&pwq->pwqs_node);
3624 is_last = list_empty(&wq->pwqs);
3625 mutex_unlock(&wq->mutex);
3626
3627 mutex_lock(&wq_pool_mutex);
3628 put_unbound_pool(pool);
3629 mutex_unlock(&wq_pool_mutex);
3630
3631 call_rcu_sched(&pwq->rcu, rcu_free_pwq);
3632
3633 /*
3634 * If we're the last pwq going away, @wq is already dead and no one
3635 * is gonna access it anymore. Free it.
3636 */
3637 if (is_last) {
3638 free_workqueue_attrs(wq->unbound_attrs);
3639 kfree(wq);
3640 }
3641}
3642
3643/**
3644 * pwq_adjust_max_active - update a pwq's max_active to the current setting
3645 * @pwq: target pool_workqueue
3646 *
3647 * If @pwq isn't freezing, set @pwq->max_active to the associated
3648 * workqueue's saved_max_active and activate delayed work items
3649 * accordingly. If @pwq is freezing, clear @pwq->max_active to zero.
3650 */
3651static void pwq_adjust_max_active(struct pool_workqueue *pwq)
3652{
3653 struct workqueue_struct *wq = pwq->wq;
3654 bool freezable = wq->flags & WQ_FREEZABLE;
3655
3656 /* for @wq->saved_max_active */
3657 lockdep_assert_held(&wq->mutex);
3658
3659 /* fast exit for non-freezable wqs */
3660 if (!freezable && pwq->max_active == wq->saved_max_active)
3661 return;
3662
3663 spin_lock_irq(&pwq->pool->lock);
3664
3665 if (!freezable || !(pwq->pool->flags & POOL_FREEZING)) {
3666 pwq->max_active = wq->saved_max_active;
3667
3668 while (!list_empty(&pwq->delayed_works) &&
3669 pwq->nr_active < pwq->max_active)
3670 pwq_activate_first_delayed(pwq);
3108 3671
3109 /* 3672 /*
3110 * Allocate enough room to align pwq and put an extra 3673 * Need to kick a worker after thawed or an unbound wq's
3111 * pointer at the end pointing back to the originally 3674 * max_active is bumped. It's a slow path. Do it always.
3112 * allocated pointer which will be used for free.
3113 */ 3675 */
3114 ptr = kzalloc(size + align + sizeof(void *), GFP_KERNEL); 3676 wake_up_worker(pwq->pool);
3115 if (ptr) { 3677 } else {
3116 wq->pool_wq.single = PTR_ALIGN(ptr, align); 3678 pwq->max_active = 0;
3117 *(void **)(wq->pool_wq.single + 1) = ptr; 3679 }
3680
3681 spin_unlock_irq(&pwq->pool->lock);
3682}
3683
3684/* initialize newly alloced @pwq which is associated with @wq and @pool */
3685static void init_pwq(struct pool_workqueue *pwq, struct workqueue_struct *wq,
3686 struct worker_pool *pool)
3687{
3688 BUG_ON((unsigned long)pwq & WORK_STRUCT_FLAG_MASK);
3689
3690 memset(pwq, 0, sizeof(*pwq));
3691
3692 pwq->pool = pool;
3693 pwq->wq = wq;
3694 pwq->flush_color = -1;
3695 pwq->refcnt = 1;
3696 INIT_LIST_HEAD(&pwq->delayed_works);
3697 INIT_LIST_HEAD(&pwq->pwqs_node);
3698 INIT_LIST_HEAD(&pwq->mayday_node);
3699 INIT_WORK(&pwq->unbound_release_work, pwq_unbound_release_workfn);
3700}
3701
3702/* sync @pwq with the current state of its associated wq and link it */
3703static void link_pwq(struct pool_workqueue *pwq)
3704{
3705 struct workqueue_struct *wq = pwq->wq;
3706
3707 lockdep_assert_held(&wq->mutex);
3708
3709 /* may be called multiple times, ignore if already linked */
3710 if (!list_empty(&pwq->pwqs_node))
3711 return;
3712
3713 /*
3714 * Set the matching work_color. This is synchronized with
3715 * wq->mutex to avoid confusing flush_workqueue().
3716 */
3717 pwq->work_color = wq->work_color;
3718
3719 /* sync max_active to the current setting */
3720 pwq_adjust_max_active(pwq);
3721
3722 /* link in @pwq */
3723 list_add_rcu(&pwq->pwqs_node, &wq->pwqs);
3724}
3725
3726/* obtain a pool matching @attr and create a pwq associating the pool and @wq */
3727static struct pool_workqueue *alloc_unbound_pwq(struct workqueue_struct *wq,
3728 const struct workqueue_attrs *attrs)
3729{
3730 struct worker_pool *pool;
3731 struct pool_workqueue *pwq;
3732
3733 lockdep_assert_held(&wq_pool_mutex);
3734
3735 pool = get_unbound_pool(attrs);
3736 if (!pool)
3737 return NULL;
3738
3739 pwq = kmem_cache_alloc_node(pwq_cache, GFP_KERNEL, pool->node);
3740 if (!pwq) {
3741 put_unbound_pool(pool);
3742 return NULL;
3743 }
3744
3745 init_pwq(pwq, wq, pool);
3746 return pwq;
3747}
3748
3749/* undo alloc_unbound_pwq(), used only in the error path */
3750static void free_unbound_pwq(struct pool_workqueue *pwq)
3751{
3752 lockdep_assert_held(&wq_pool_mutex);
3753
3754 if (pwq) {
3755 put_unbound_pool(pwq->pool);
3756 kmem_cache_free(pwq_cache, pwq);
3757 }
3758}
3759
3760/**
3761 * wq_calc_node_mask - calculate a wq_attrs' cpumask for the specified node
3762 * @attrs: the wq_attrs of interest
3763 * @node: the target NUMA node
3764 * @cpu_going_down: if >= 0, the CPU to consider as offline
3765 * @cpumask: outarg, the resulting cpumask
3766 *
3767 * Calculate the cpumask a workqueue with @attrs should use on @node. If
3768 * @cpu_going_down is >= 0, that cpu is considered offline during
3769 * calculation. The result is stored in @cpumask. This function returns
3770 * %true if the resulting @cpumask is different from @attrs->cpumask,
3771 * %false if equal.
3772 *
3773 * If NUMA affinity is not enabled, @attrs->cpumask is always used. If
3774 * enabled and @node has online CPUs requested by @attrs, the returned
3775 * cpumask is the intersection of the possible CPUs of @node and
3776 * @attrs->cpumask.
3777 *
3778 * The caller is responsible for ensuring that the cpumask of @node stays
3779 * stable.
3780 */
3781static bool wq_calc_node_cpumask(const struct workqueue_attrs *attrs, int node,
3782 int cpu_going_down, cpumask_t *cpumask)
3783{
3784 if (!wq_numa_enabled || attrs->no_numa)
3785 goto use_dfl;
3786
3787 /* does @node have any online CPUs @attrs wants? */
3788 cpumask_and(cpumask, cpumask_of_node(node), attrs->cpumask);
3789 if (cpu_going_down >= 0)
3790 cpumask_clear_cpu(cpu_going_down, cpumask);
3791
3792 if (cpumask_empty(cpumask))
3793 goto use_dfl;
3794
3795 /* yeap, return possible CPUs in @node that @attrs wants */
3796 cpumask_and(cpumask, attrs->cpumask, wq_numa_possible_cpumask[node]);
3797 return !cpumask_equal(cpumask, attrs->cpumask);
3798
3799use_dfl:
3800 cpumask_copy(cpumask, attrs->cpumask);
3801 return false;
3802}
3803
3804/* install @pwq into @wq's numa_pwq_tbl[] for @node and return the old pwq */
3805static struct pool_workqueue *numa_pwq_tbl_install(struct workqueue_struct *wq,
3806 int node,
3807 struct pool_workqueue *pwq)
3808{
3809 struct pool_workqueue *old_pwq;
3810
3811 lockdep_assert_held(&wq->mutex);
3812
3813 /* link_pwq() can handle duplicate calls */
3814 link_pwq(pwq);
3815
3816 old_pwq = rcu_access_pointer(wq->numa_pwq_tbl[node]);
3817 rcu_assign_pointer(wq->numa_pwq_tbl[node], pwq);
3818 return old_pwq;
3819}
3820
3821/**
3822 * apply_workqueue_attrs - apply new workqueue_attrs to an unbound workqueue
3823 * @wq: the target workqueue
3824 * @attrs: the workqueue_attrs to apply, allocated with alloc_workqueue_attrs()
3825 *
3826 * Apply @attrs to an unbound workqueue @wq. Unless disabled, on NUMA
3827 * machines, this function maps a separate pwq to each NUMA node with
3828 * possibles CPUs in @attrs->cpumask so that work items are affine to the
3829 * NUMA node it was issued on. Older pwqs are released as in-flight work
3830 * items finish. Note that a work item which repeatedly requeues itself
3831 * back-to-back will stay on its current pwq.
3832 *
3833 * Performs GFP_KERNEL allocations. Returns 0 on success and -errno on
3834 * failure.
3835 */
3836int apply_workqueue_attrs(struct workqueue_struct *wq,
3837 const struct workqueue_attrs *attrs)
3838{
3839 struct workqueue_attrs *new_attrs, *tmp_attrs;
3840 struct pool_workqueue **pwq_tbl, *dfl_pwq;
3841 int node, ret;
3842
3843 /* only unbound workqueues can change attributes */
3844 if (WARN_ON(!(wq->flags & WQ_UNBOUND)))
3845 return -EINVAL;
3846
3847 /* creating multiple pwqs breaks ordering guarantee */
3848 if (WARN_ON((wq->flags & __WQ_ORDERED) && !list_empty(&wq->pwqs)))
3849 return -EINVAL;
3850
3851 pwq_tbl = kzalloc(wq_numa_tbl_len * sizeof(pwq_tbl[0]), GFP_KERNEL);
3852 new_attrs = alloc_workqueue_attrs(GFP_KERNEL);
3853 tmp_attrs = alloc_workqueue_attrs(GFP_KERNEL);
3854 if (!pwq_tbl || !new_attrs || !tmp_attrs)
3855 goto enomem;
3856
3857 /* make a copy of @attrs and sanitize it */
3858 copy_workqueue_attrs(new_attrs, attrs);
3859 cpumask_and(new_attrs->cpumask, new_attrs->cpumask, cpu_possible_mask);
3860
3861 /*
3862 * We may create multiple pwqs with differing cpumasks. Make a
3863 * copy of @new_attrs which will be modified and used to obtain
3864 * pools.
3865 */
3866 copy_workqueue_attrs(tmp_attrs, new_attrs);
3867
3868 /*
3869 * CPUs should stay stable across pwq creations and installations.
3870 * Pin CPUs, determine the target cpumask for each node and create
3871 * pwqs accordingly.
3872 */
3873 get_online_cpus();
3874
3875 mutex_lock(&wq_pool_mutex);
3876
3877 /*
3878 * If something goes wrong during CPU up/down, we'll fall back to
3879 * the default pwq covering whole @attrs->cpumask. Always create
3880 * it even if we don't use it immediately.
3881 */
3882 dfl_pwq = alloc_unbound_pwq(wq, new_attrs);
3883 if (!dfl_pwq)
3884 goto enomem_pwq;
3885
3886 for_each_node(node) {
3887 if (wq_calc_node_cpumask(attrs, node, -1, tmp_attrs->cpumask)) {
3888 pwq_tbl[node] = alloc_unbound_pwq(wq, tmp_attrs);
3889 if (!pwq_tbl[node])
3890 goto enomem_pwq;
3891 } else {
3892 dfl_pwq->refcnt++;
3893 pwq_tbl[node] = dfl_pwq;
3118 } 3894 }
3119 } 3895 }
3120 3896
3121 /* just in case, make sure it's actually aligned */ 3897 mutex_unlock(&wq_pool_mutex);
3122 BUG_ON(!IS_ALIGNED(wq->pool_wq.v, align)); 3898
3123 return wq->pool_wq.v ? 0 : -ENOMEM; 3899 /* all pwqs have been created successfully, let's install'em */
3900 mutex_lock(&wq->mutex);
3901
3902 copy_workqueue_attrs(wq->unbound_attrs, new_attrs);
3903
3904 /* save the previous pwq and install the new one */
3905 for_each_node(node)
3906 pwq_tbl[node] = numa_pwq_tbl_install(wq, node, pwq_tbl[node]);
3907
3908 /* @dfl_pwq might not have been used, ensure it's linked */
3909 link_pwq(dfl_pwq);
3910 swap(wq->dfl_pwq, dfl_pwq);
3911
3912 mutex_unlock(&wq->mutex);
3913
3914 /* put the old pwqs */
3915 for_each_node(node)
3916 put_pwq_unlocked(pwq_tbl[node]);
3917 put_pwq_unlocked(dfl_pwq);
3918
3919 put_online_cpus();
3920 ret = 0;
3921 /* fall through */
3922out_free:
3923 free_workqueue_attrs(tmp_attrs);
3924 free_workqueue_attrs(new_attrs);
3925 kfree(pwq_tbl);
3926 return ret;
3927
3928enomem_pwq:
3929 free_unbound_pwq(dfl_pwq);
3930 for_each_node(node)
3931 if (pwq_tbl && pwq_tbl[node] != dfl_pwq)
3932 free_unbound_pwq(pwq_tbl[node]);
3933 mutex_unlock(&wq_pool_mutex);
3934 put_online_cpus();
3935enomem:
3936 ret = -ENOMEM;
3937 goto out_free;
3124} 3938}
3125 3939
3126static void free_pwqs(struct workqueue_struct *wq) 3940/**
3941 * wq_update_unbound_numa - update NUMA affinity of a wq for CPU hot[un]plug
3942 * @wq: the target workqueue
3943 * @cpu: the CPU coming up or going down
3944 * @online: whether @cpu is coming up or going down
3945 *
3946 * This function is to be called from %CPU_DOWN_PREPARE, %CPU_ONLINE and
3947 * %CPU_DOWN_FAILED. @cpu is being hot[un]plugged, update NUMA affinity of
3948 * @wq accordingly.
3949 *
3950 * If NUMA affinity can't be adjusted due to memory allocation failure, it
3951 * falls back to @wq->dfl_pwq which may not be optimal but is always
3952 * correct.
3953 *
3954 * Note that when the last allowed CPU of a NUMA node goes offline for a
3955 * workqueue with a cpumask spanning multiple nodes, the workers which were
3956 * already executing the work items for the workqueue will lose their CPU
3957 * affinity and may execute on any CPU. This is similar to how per-cpu
3958 * workqueues behave on CPU_DOWN. If a workqueue user wants strict
3959 * affinity, it's the user's responsibility to flush the work item from
3960 * CPU_DOWN_PREPARE.
3961 */
3962static void wq_update_unbound_numa(struct workqueue_struct *wq, int cpu,
3963 bool online)
3127{ 3964{
3128 if (!(wq->flags & WQ_UNBOUND)) 3965 int node = cpu_to_node(cpu);
3129 free_percpu(wq->pool_wq.pcpu); 3966 int cpu_off = online ? -1 : cpu;
3130 else if (wq->pool_wq.single) { 3967 struct pool_workqueue *old_pwq = NULL, *pwq;
3131 /* the pointer to free is stored right after the pwq */ 3968 struct workqueue_attrs *target_attrs;
3132 kfree(*(void **)(wq->pool_wq.single + 1)); 3969 cpumask_t *cpumask;
3970
3971 lockdep_assert_held(&wq_pool_mutex);
3972
3973 if (!wq_numa_enabled || !(wq->flags & WQ_UNBOUND))
3974 return;
3975
3976 /*
3977 * We don't wanna alloc/free wq_attrs for each wq for each CPU.
3978 * Let's use a preallocated one. The following buf is protected by
3979 * CPU hotplug exclusion.
3980 */
3981 target_attrs = wq_update_unbound_numa_attrs_buf;
3982 cpumask = target_attrs->cpumask;
3983
3984 mutex_lock(&wq->mutex);
3985 if (wq->unbound_attrs->no_numa)
3986 goto out_unlock;
3987
3988 copy_workqueue_attrs(target_attrs, wq->unbound_attrs);
3989 pwq = unbound_pwq_by_node(wq, node);
3990
3991 /*
3992 * Let's determine what needs to be done. If the target cpumask is
3993 * different from wq's, we need to compare it to @pwq's and create
3994 * a new one if they don't match. If the target cpumask equals
3995 * wq's, the default pwq should be used. If @pwq is already the
3996 * default one, nothing to do; otherwise, install the default one.
3997 */
3998 if (wq_calc_node_cpumask(wq->unbound_attrs, node, cpu_off, cpumask)) {
3999 if (cpumask_equal(cpumask, pwq->pool->attrs->cpumask))
4000 goto out_unlock;
4001 } else {
4002 if (pwq == wq->dfl_pwq)
4003 goto out_unlock;
4004 else
4005 goto use_dfl_pwq;
4006 }
4007
4008 mutex_unlock(&wq->mutex);
4009
4010 /* create a new pwq */
4011 pwq = alloc_unbound_pwq(wq, target_attrs);
4012 if (!pwq) {
4013 pr_warning("workqueue: allocation failed while updating NUMA affinity of \"%s\"\n",
4014 wq->name);
4015 goto out_unlock;
4016 }
4017
4018 /*
4019 * Install the new pwq. As this function is called only from CPU
4020 * hotplug callbacks and applying a new attrs is wrapped with
4021 * get/put_online_cpus(), @wq->unbound_attrs couldn't have changed
4022 * inbetween.
4023 */
4024 mutex_lock(&wq->mutex);
4025 old_pwq = numa_pwq_tbl_install(wq, node, pwq);
4026 goto out_unlock;
4027
4028use_dfl_pwq:
4029 spin_lock_irq(&wq->dfl_pwq->pool->lock);
4030 get_pwq(wq->dfl_pwq);
4031 spin_unlock_irq(&wq->dfl_pwq->pool->lock);
4032 old_pwq = numa_pwq_tbl_install(wq, node, wq->dfl_pwq);
4033out_unlock:
4034 mutex_unlock(&wq->mutex);
4035 put_pwq_unlocked(old_pwq);
4036}
4037
4038static int alloc_and_link_pwqs(struct workqueue_struct *wq)
4039{
4040 bool highpri = wq->flags & WQ_HIGHPRI;
4041 int cpu;
4042
4043 if (!(wq->flags & WQ_UNBOUND)) {
4044 wq->cpu_pwqs = alloc_percpu(struct pool_workqueue);
4045 if (!wq->cpu_pwqs)
4046 return -ENOMEM;
4047
4048 for_each_possible_cpu(cpu) {
4049 struct pool_workqueue *pwq =
4050 per_cpu_ptr(wq->cpu_pwqs, cpu);
4051 struct worker_pool *cpu_pools =
4052 per_cpu(cpu_worker_pools, cpu);
4053
4054 init_pwq(pwq, wq, &cpu_pools[highpri]);
4055
4056 mutex_lock(&wq->mutex);
4057 link_pwq(pwq);
4058 mutex_unlock(&wq->mutex);
4059 }
4060 return 0;
4061 } else {
4062 return apply_workqueue_attrs(wq, unbound_std_wq_attrs[highpri]);
3133 } 4063 }
3134} 4064}
3135 4065
@@ -3151,30 +4081,28 @@ struct workqueue_struct *__alloc_workqueue_key(const char *fmt,
3151 struct lock_class_key *key, 4081 struct lock_class_key *key,
3152 const char *lock_name, ...) 4082 const char *lock_name, ...)
3153{ 4083{
3154 va_list args, args1; 4084 size_t tbl_size = 0;
4085 va_list args;
3155 struct workqueue_struct *wq; 4086 struct workqueue_struct *wq;
3156 unsigned int cpu; 4087 struct pool_workqueue *pwq;
3157 size_t namelen;
3158 4088
3159 /* determine namelen, allocate wq and format name */ 4089 /* allocate wq and format name */
3160 va_start(args, lock_name); 4090 if (flags & WQ_UNBOUND)
3161 va_copy(args1, args); 4091 tbl_size = wq_numa_tbl_len * sizeof(wq->numa_pwq_tbl[0]);
3162 namelen = vsnprintf(NULL, 0, fmt, args) + 1;
3163 4092
3164 wq = kzalloc(sizeof(*wq) + namelen, GFP_KERNEL); 4093 wq = kzalloc(sizeof(*wq) + tbl_size, GFP_KERNEL);
3165 if (!wq) 4094 if (!wq)
3166 goto err; 4095 return NULL;
3167 4096
3168 vsnprintf(wq->name, namelen, fmt, args1); 4097 if (flags & WQ_UNBOUND) {
3169 va_end(args); 4098 wq->unbound_attrs = alloc_workqueue_attrs(GFP_KERNEL);
3170 va_end(args1); 4099 if (!wq->unbound_attrs)
4100 goto err_free_wq;
4101 }
3171 4102
3172 /* 4103 va_start(args, lock_name);
3173 * Workqueues which may be used during memory reclaim should 4104 vsnprintf(wq->name, sizeof(wq->name), fmt, args);
3174 * have a rescuer to guarantee forward progress. 4105 va_end(args);
3175 */
3176 if (flags & WQ_MEM_RECLAIM)
3177 flags |= WQ_RESCUER;
3178 4106
3179 max_active = max_active ?: WQ_DFL_ACTIVE; 4107 max_active = max_active ?: WQ_DFL_ACTIVE;
3180 max_active = wq_clamp_max_active(max_active, flags, wq->name); 4108 max_active = wq_clamp_max_active(max_active, flags, wq->name);
@@ -3182,71 +4110,70 @@ struct workqueue_struct *__alloc_workqueue_key(const char *fmt,
3182 /* init wq */ 4110 /* init wq */
3183 wq->flags = flags; 4111 wq->flags = flags;
3184 wq->saved_max_active = max_active; 4112 wq->saved_max_active = max_active;
3185 mutex_init(&wq->flush_mutex); 4113 mutex_init(&wq->mutex);
3186 atomic_set(&wq->nr_pwqs_to_flush, 0); 4114 atomic_set(&wq->nr_pwqs_to_flush, 0);
4115 INIT_LIST_HEAD(&wq->pwqs);
3187 INIT_LIST_HEAD(&wq->flusher_queue); 4116 INIT_LIST_HEAD(&wq->flusher_queue);
3188 INIT_LIST_HEAD(&wq->flusher_overflow); 4117 INIT_LIST_HEAD(&wq->flusher_overflow);
4118 INIT_LIST_HEAD(&wq->maydays);
3189 4119
3190 lockdep_init_map(&wq->lockdep_map, lock_name, key, 0); 4120 lockdep_init_map(&wq->lockdep_map, lock_name, key, 0);
3191 INIT_LIST_HEAD(&wq->list); 4121 INIT_LIST_HEAD(&wq->list);
3192 4122
3193 if (alloc_pwqs(wq) < 0) 4123 if (alloc_and_link_pwqs(wq) < 0)
3194 goto err; 4124 goto err_free_wq;
3195
3196 for_each_pwq_cpu(cpu, wq) {
3197 struct pool_workqueue *pwq = get_pwq(cpu, wq);
3198
3199 BUG_ON((unsigned long)pwq & WORK_STRUCT_FLAG_MASK);
3200 pwq->pool = get_std_worker_pool(cpu, flags & WQ_HIGHPRI);
3201 pwq->wq = wq;
3202 pwq->flush_color = -1;
3203 pwq->max_active = max_active;
3204 INIT_LIST_HEAD(&pwq->delayed_works);
3205 }
3206 4125
3207 if (flags & WQ_RESCUER) { 4126 /*
4127 * Workqueues which may be used during memory reclaim should
4128 * have a rescuer to guarantee forward progress.
4129 */
4130 if (flags & WQ_MEM_RECLAIM) {
3208 struct worker *rescuer; 4131 struct worker *rescuer;
3209 4132
3210 if (!alloc_mayday_mask(&wq->mayday_mask, GFP_KERNEL)) 4133 rescuer = alloc_worker();
3211 goto err;
3212
3213 wq->rescuer = rescuer = alloc_worker();
3214 if (!rescuer) 4134 if (!rescuer)
3215 goto err; 4135 goto err_destroy;
3216 4136
3217 rescuer->rescue_wq = wq; 4137 rescuer->rescue_wq = wq;
3218 rescuer->task = kthread_create(rescuer_thread, rescuer, "%s", 4138 rescuer->task = kthread_create(rescuer_thread, rescuer, "%s",
3219 wq->name); 4139 wq->name);
3220 if (IS_ERR(rescuer->task)) 4140 if (IS_ERR(rescuer->task)) {
3221 goto err; 4141 kfree(rescuer);
4142 goto err_destroy;
4143 }
3222 4144
3223 rescuer->task->flags |= PF_THREAD_BOUND; 4145 wq->rescuer = rescuer;
4146 rescuer->task->flags |= PF_NO_SETAFFINITY;
3224 wake_up_process(rescuer->task); 4147 wake_up_process(rescuer->task);
3225 } 4148 }
3226 4149
4150 if ((wq->flags & WQ_SYSFS) && workqueue_sysfs_register(wq))
4151 goto err_destroy;
4152
3227 /* 4153 /*
3228 * workqueue_lock protects global freeze state and workqueues 4154 * wq_pool_mutex protects global freeze state and workqueues list.
3229 * list. Grab it, set max_active accordingly and add the new 4155 * Grab it, adjust max_active and add the new @wq to workqueues
3230 * workqueue to workqueues list. 4156 * list.
3231 */ 4157 */
3232 spin_lock(&workqueue_lock); 4158 mutex_lock(&wq_pool_mutex);
3233 4159
3234 if (workqueue_freezing && wq->flags & WQ_FREEZABLE) 4160 mutex_lock(&wq->mutex);
3235 for_each_pwq_cpu(cpu, wq) 4161 for_each_pwq(pwq, wq)
3236 get_pwq(cpu, wq)->max_active = 0; 4162 pwq_adjust_max_active(pwq);
4163 mutex_unlock(&wq->mutex);
3237 4164
3238 list_add(&wq->list, &workqueues); 4165 list_add(&wq->list, &workqueues);
3239 4166
3240 spin_unlock(&workqueue_lock); 4167 mutex_unlock(&wq_pool_mutex);
3241 4168
3242 return wq; 4169 return wq;
3243err: 4170
3244 if (wq) { 4171err_free_wq:
3245 free_pwqs(wq); 4172 free_workqueue_attrs(wq->unbound_attrs);
3246 free_mayday_mask(wq->mayday_mask); 4173 kfree(wq);
3247 kfree(wq->rescuer); 4174 return NULL;
3248 kfree(wq); 4175err_destroy:
3249 } 4176 destroy_workqueue(wq);
3250 return NULL; 4177 return NULL;
3251} 4178}
3252EXPORT_SYMBOL_GPL(__alloc_workqueue_key); 4179EXPORT_SYMBOL_GPL(__alloc_workqueue_key);
@@ -3259,60 +4186,78 @@ EXPORT_SYMBOL_GPL(__alloc_workqueue_key);
3259 */ 4186 */
3260void destroy_workqueue(struct workqueue_struct *wq) 4187void destroy_workqueue(struct workqueue_struct *wq)
3261{ 4188{
3262 unsigned int cpu; 4189 struct pool_workqueue *pwq;
4190 int node;
3263 4191
3264 /* drain it before proceeding with destruction */ 4192 /* drain it before proceeding with destruction */
3265 drain_workqueue(wq); 4193 drain_workqueue(wq);
3266 4194
4195 /* sanity checks */
4196 mutex_lock(&wq->mutex);
4197 for_each_pwq(pwq, wq) {
4198 int i;
4199
4200 for (i = 0; i < WORK_NR_COLORS; i++) {
4201 if (WARN_ON(pwq->nr_in_flight[i])) {
4202 mutex_unlock(&wq->mutex);
4203 return;
4204 }
4205 }
4206
4207 if (WARN_ON((pwq != wq->dfl_pwq) && (pwq->refcnt > 1)) ||
4208 WARN_ON(pwq->nr_active) ||
4209 WARN_ON(!list_empty(&pwq->delayed_works))) {
4210 mutex_unlock(&wq->mutex);
4211 return;
4212 }
4213 }
4214 mutex_unlock(&wq->mutex);
4215
3267 /* 4216 /*
3268 * wq list is used to freeze wq, remove from list after 4217 * wq list is used to freeze wq, remove from list after
3269 * flushing is complete in case freeze races us. 4218 * flushing is complete in case freeze races us.
3270 */ 4219 */
3271 spin_lock(&workqueue_lock); 4220 mutex_lock(&wq_pool_mutex);
3272 list_del(&wq->list); 4221 list_del_init(&wq->list);
3273 spin_unlock(&workqueue_lock); 4222 mutex_unlock(&wq_pool_mutex);
3274 4223
3275 /* sanity check */ 4224 workqueue_sysfs_unregister(wq);
3276 for_each_pwq_cpu(cpu, wq) {
3277 struct pool_workqueue *pwq = get_pwq(cpu, wq);
3278 int i;
3279 4225
3280 for (i = 0; i < WORK_NR_COLORS; i++) 4226 if (wq->rescuer) {
3281 BUG_ON(pwq->nr_in_flight[i]);
3282 BUG_ON(pwq->nr_active);
3283 BUG_ON(!list_empty(&pwq->delayed_works));
3284 }
3285
3286 if (wq->flags & WQ_RESCUER) {
3287 kthread_stop(wq->rescuer->task); 4227 kthread_stop(wq->rescuer->task);
3288 free_mayday_mask(wq->mayday_mask);
3289 kfree(wq->rescuer); 4228 kfree(wq->rescuer);
4229 wq->rescuer = NULL;
3290 } 4230 }
3291 4231
3292 free_pwqs(wq); 4232 if (!(wq->flags & WQ_UNBOUND)) {
3293 kfree(wq); 4233 /*
3294} 4234 * The base ref is never dropped on per-cpu pwqs. Directly
3295EXPORT_SYMBOL_GPL(destroy_workqueue); 4235 * free the pwqs and wq.
3296 4236 */
3297/** 4237 free_percpu(wq->cpu_pwqs);
3298 * pwq_set_max_active - adjust max_active of a pwq 4238 kfree(wq);
3299 * @pwq: target pool_workqueue 4239 } else {
3300 * @max_active: new max_active value. 4240 /*
3301 * 4241 * We're the sole accessor of @wq at this point. Directly
3302 * Set @pwq->max_active to @max_active and activate delayed works if 4242 * access numa_pwq_tbl[] and dfl_pwq to put the base refs.
3303 * increased. 4243 * @wq will be freed when the last pwq is released.
3304 * 4244 */
3305 * CONTEXT: 4245 for_each_node(node) {
3306 * spin_lock_irq(pool->lock). 4246 pwq = rcu_access_pointer(wq->numa_pwq_tbl[node]);
3307 */ 4247 RCU_INIT_POINTER(wq->numa_pwq_tbl[node], NULL);
3308static void pwq_set_max_active(struct pool_workqueue *pwq, int max_active) 4248 put_pwq_unlocked(pwq);
3309{ 4249 }
3310 pwq->max_active = max_active;
3311 4250
3312 while (!list_empty(&pwq->delayed_works) && 4251 /*
3313 pwq->nr_active < pwq->max_active) 4252 * Put dfl_pwq. @wq may be freed any time after dfl_pwq is
3314 pwq_activate_first_delayed(pwq); 4253 * put. Don't access it afterwards.
4254 */
4255 pwq = wq->dfl_pwq;
4256 wq->dfl_pwq = NULL;
4257 put_pwq_unlocked(pwq);
4258 }
3315} 4259}
4260EXPORT_SYMBOL_GPL(destroy_workqueue);
3316 4261
3317/** 4262/**
3318 * workqueue_set_max_active - adjust max_active of a workqueue 4263 * workqueue_set_max_active - adjust max_active of a workqueue
@@ -3326,30 +4271,37 @@ static void pwq_set_max_active(struct pool_workqueue *pwq, int max_active)
3326 */ 4271 */
3327void workqueue_set_max_active(struct workqueue_struct *wq, int max_active) 4272void workqueue_set_max_active(struct workqueue_struct *wq, int max_active)
3328{ 4273{
3329 unsigned int cpu; 4274 struct pool_workqueue *pwq;
4275
4276 /* disallow meddling with max_active for ordered workqueues */
4277 if (WARN_ON(wq->flags & __WQ_ORDERED))
4278 return;
3330 4279
3331 max_active = wq_clamp_max_active(max_active, wq->flags, wq->name); 4280 max_active = wq_clamp_max_active(max_active, wq->flags, wq->name);
3332 4281
3333 spin_lock(&workqueue_lock); 4282 mutex_lock(&wq->mutex);
3334 4283
3335 wq->saved_max_active = max_active; 4284 wq->saved_max_active = max_active;
3336 4285
3337 for_each_pwq_cpu(cpu, wq) { 4286 for_each_pwq(pwq, wq)
3338 struct pool_workqueue *pwq = get_pwq(cpu, wq); 4287 pwq_adjust_max_active(pwq);
3339 struct worker_pool *pool = pwq->pool;
3340
3341 spin_lock_irq(&pool->lock);
3342 4288
3343 if (!(wq->flags & WQ_FREEZABLE) || 4289 mutex_unlock(&wq->mutex);
3344 !(pool->flags & POOL_FREEZING)) 4290}
3345 pwq_set_max_active(pwq, max_active); 4291EXPORT_SYMBOL_GPL(workqueue_set_max_active);
3346 4292
3347 spin_unlock_irq(&pool->lock); 4293/**
3348 } 4294 * current_is_workqueue_rescuer - is %current workqueue rescuer?
4295 *
4296 * Determine whether %current is a workqueue rescuer. Can be used from
4297 * work functions to determine whether it's being run off the rescuer task.
4298 */
4299bool current_is_workqueue_rescuer(void)
4300{
4301 struct worker *worker = current_wq_worker();
3349 4302
3350 spin_unlock(&workqueue_lock); 4303 return worker && worker->rescue_wq;
3351} 4304}
3352EXPORT_SYMBOL_GPL(workqueue_set_max_active);
3353 4305
3354/** 4306/**
3355 * workqueue_congested - test whether a workqueue is congested 4307 * workqueue_congested - test whether a workqueue is congested
@@ -3360,14 +4312,34 @@ EXPORT_SYMBOL_GPL(workqueue_set_max_active);
3360 * no synchronization around this function and the test result is 4312 * no synchronization around this function and the test result is
3361 * unreliable and only useful as advisory hints or for debugging. 4313 * unreliable and only useful as advisory hints or for debugging.
3362 * 4314 *
4315 * If @cpu is WORK_CPU_UNBOUND, the test is performed on the local CPU.
4316 * Note that both per-cpu and unbound workqueues may be associated with
4317 * multiple pool_workqueues which have separate congested states. A
4318 * workqueue being congested on one CPU doesn't mean the workqueue is also
4319 * contested on other CPUs / NUMA nodes.
4320 *
3363 * RETURNS: 4321 * RETURNS:
3364 * %true if congested, %false otherwise. 4322 * %true if congested, %false otherwise.
3365 */ 4323 */
3366bool workqueue_congested(unsigned int cpu, struct workqueue_struct *wq) 4324bool workqueue_congested(int cpu, struct workqueue_struct *wq)
3367{ 4325{
3368 struct pool_workqueue *pwq = get_pwq(cpu, wq); 4326 struct pool_workqueue *pwq;
4327 bool ret;
4328
4329 rcu_read_lock_sched();
4330
4331 if (cpu == WORK_CPU_UNBOUND)
4332 cpu = smp_processor_id();
4333
4334 if (!(wq->flags & WQ_UNBOUND))
4335 pwq = per_cpu_ptr(wq->cpu_pwqs, cpu);
4336 else
4337 pwq = unbound_pwq_by_node(wq, cpu_to_node(cpu));
3369 4338
3370 return !list_empty(&pwq->delayed_works); 4339 ret = !list_empty(&pwq->delayed_works);
4340 rcu_read_unlock_sched();
4341
4342 return ret;
3371} 4343}
3372EXPORT_SYMBOL_GPL(workqueue_congested); 4344EXPORT_SYMBOL_GPL(workqueue_congested);
3373 4345
@@ -3384,24 +4356,104 @@ EXPORT_SYMBOL_GPL(workqueue_congested);
3384 */ 4356 */
3385unsigned int work_busy(struct work_struct *work) 4357unsigned int work_busy(struct work_struct *work)
3386{ 4358{
3387 struct worker_pool *pool = get_work_pool(work); 4359 struct worker_pool *pool;
3388 unsigned long flags; 4360 unsigned long flags;
3389 unsigned int ret = 0; 4361 unsigned int ret = 0;
3390 4362
3391 if (work_pending(work)) 4363 if (work_pending(work))
3392 ret |= WORK_BUSY_PENDING; 4364 ret |= WORK_BUSY_PENDING;
3393 4365
4366 local_irq_save(flags);
4367 pool = get_work_pool(work);
3394 if (pool) { 4368 if (pool) {
3395 spin_lock_irqsave(&pool->lock, flags); 4369 spin_lock(&pool->lock);
3396 if (find_worker_executing_work(pool, work)) 4370 if (find_worker_executing_work(pool, work))
3397 ret |= WORK_BUSY_RUNNING; 4371 ret |= WORK_BUSY_RUNNING;
3398 spin_unlock_irqrestore(&pool->lock, flags); 4372 spin_unlock(&pool->lock);
3399 } 4373 }
4374 local_irq_restore(flags);
3400 4375
3401 return ret; 4376 return ret;
3402} 4377}
3403EXPORT_SYMBOL_GPL(work_busy); 4378EXPORT_SYMBOL_GPL(work_busy);
3404 4379
4380/**
4381 * set_worker_desc - set description for the current work item
4382 * @fmt: printf-style format string
4383 * @...: arguments for the format string
4384 *
4385 * This function can be called by a running work function to describe what
4386 * the work item is about. If the worker task gets dumped, this
4387 * information will be printed out together to help debugging. The
4388 * description can be at most WORKER_DESC_LEN including the trailing '\0'.
4389 */
4390void set_worker_desc(const char *fmt, ...)
4391{
4392 struct worker *worker = current_wq_worker();
4393 va_list args;
4394
4395 if (worker) {
4396 va_start(args, fmt);
4397 vsnprintf(worker->desc, sizeof(worker->desc), fmt, args);
4398 va_end(args);
4399 worker->desc_valid = true;
4400 }
4401}
4402
4403/**
4404 * print_worker_info - print out worker information and description
4405 * @log_lvl: the log level to use when printing
4406 * @task: target task
4407 *
4408 * If @task is a worker and currently executing a work item, print out the
4409 * name of the workqueue being serviced and worker description set with
4410 * set_worker_desc() by the currently executing work item.
4411 *
4412 * This function can be safely called on any task as long as the
4413 * task_struct itself is accessible. While safe, this function isn't
4414 * synchronized and may print out mixups or garbages of limited length.
4415 */
4416void print_worker_info(const char *log_lvl, struct task_struct *task)
4417{
4418 work_func_t *fn = NULL;
4419 char name[WQ_NAME_LEN] = { };
4420 char desc[WORKER_DESC_LEN] = { };
4421 struct pool_workqueue *pwq = NULL;
4422 struct workqueue_struct *wq = NULL;
4423 bool desc_valid = false;
4424 struct worker *worker;
4425
4426 if (!(task->flags & PF_WQ_WORKER))
4427 return;
4428
4429 /*
4430 * This function is called without any synchronization and @task
4431 * could be in any state. Be careful with dereferences.
4432 */
4433 worker = probe_kthread_data(task);
4434
4435 /*
4436 * Carefully copy the associated workqueue's workfn and name. Keep
4437 * the original last '\0' in case the original contains garbage.
4438 */
4439 probe_kernel_read(&fn, &worker->current_func, sizeof(fn));
4440 probe_kernel_read(&pwq, &worker->current_pwq, sizeof(pwq));
4441 probe_kernel_read(&wq, &pwq->wq, sizeof(wq));
4442 probe_kernel_read(name, wq->name, sizeof(name) - 1);
4443
4444 /* copy worker description */
4445 probe_kernel_read(&desc_valid, &worker->desc_valid, sizeof(desc_valid));
4446 if (desc_valid)
4447 probe_kernel_read(desc, worker->desc, sizeof(desc) - 1);
4448
4449 if (fn || name[0] || desc[0]) {
4450 printk("%sWorkqueue: %s %pf", log_lvl, name, fn);
4451 if (desc[0])
4452 pr_cont(" (%s)", desc);
4453 pr_cont("\n");
4454 }
4455}
4456
3405/* 4457/*
3406 * CPU hotplug. 4458 * CPU hotplug.
3407 * 4459 *
@@ -3422,53 +4474,153 @@ static void wq_unbind_fn(struct work_struct *work)
3422 int cpu = smp_processor_id(); 4474 int cpu = smp_processor_id();
3423 struct worker_pool *pool; 4475 struct worker_pool *pool;
3424 struct worker *worker; 4476 struct worker *worker;
3425 int i; 4477 int wi;
3426 4478
3427 for_each_std_worker_pool(pool, cpu) { 4479 for_each_cpu_worker_pool(pool, cpu) {
3428 BUG_ON(cpu != smp_processor_id()); 4480 WARN_ON_ONCE(cpu != smp_processor_id());
3429 4481
3430 mutex_lock(&pool->assoc_mutex); 4482 mutex_lock(&pool->manager_mutex);
3431 spin_lock_irq(&pool->lock); 4483 spin_lock_irq(&pool->lock);
3432 4484
3433 /* 4485 /*
3434 * We've claimed all manager positions. Make all workers 4486 * We've blocked all manager operations. Make all workers
3435 * unbound and set DISASSOCIATED. Before this, all workers 4487 * unbound and set DISASSOCIATED. Before this, all workers
3436 * except for the ones which are still executing works from 4488 * except for the ones which are still executing works from
3437 * before the last CPU down must be on the cpu. After 4489 * before the last CPU down must be on the cpu. After
3438 * this, they may become diasporas. 4490 * this, they may become diasporas.
3439 */ 4491 */
3440 list_for_each_entry(worker, &pool->idle_list, entry) 4492 for_each_pool_worker(worker, wi, pool)
3441 worker->flags |= WORKER_UNBOUND;
3442
3443 for_each_busy_worker(worker, i, pool)
3444 worker->flags |= WORKER_UNBOUND; 4493 worker->flags |= WORKER_UNBOUND;
3445 4494
3446 pool->flags |= POOL_DISASSOCIATED; 4495 pool->flags |= POOL_DISASSOCIATED;
3447 4496
3448 spin_unlock_irq(&pool->lock); 4497 spin_unlock_irq(&pool->lock);
3449 mutex_unlock(&pool->assoc_mutex); 4498 mutex_unlock(&pool->manager_mutex);
4499
4500 /*
4501 * Call schedule() so that we cross rq->lock and thus can
4502 * guarantee sched callbacks see the %WORKER_UNBOUND flag.
4503 * This is necessary as scheduler callbacks may be invoked
4504 * from other cpus.
4505 */
4506 schedule();
4507
4508 /*
4509 * Sched callbacks are disabled now. Zap nr_running.
4510 * After this, nr_running stays zero and need_more_worker()
4511 * and keep_working() are always true as long as the
4512 * worklist is not empty. This pool now behaves as an
4513 * unbound (in terms of concurrency management) pool which
4514 * are served by workers tied to the pool.
4515 */
4516 atomic_set(&pool->nr_running, 0);
4517
4518 /*
4519 * With concurrency management just turned off, a busy
4520 * worker blocking could lead to lengthy stalls. Kick off
4521 * unbound chain execution of currently pending work items.
4522 */
4523 spin_lock_irq(&pool->lock);
4524 wake_up_worker(pool);
4525 spin_unlock_irq(&pool->lock);
3450 } 4526 }
4527}
3451 4528
3452 /* 4529/**
3453 * Call schedule() so that we cross rq->lock and thus can guarantee 4530 * rebind_workers - rebind all workers of a pool to the associated CPU
3454 * sched callbacks see the %WORKER_UNBOUND flag. This is necessary 4531 * @pool: pool of interest
3455 * as scheduler callbacks may be invoked from other cpus. 4532 *
3456 */ 4533 * @pool->cpu is coming online. Rebind all workers to the CPU.
3457 schedule(); 4534 */
4535static void rebind_workers(struct worker_pool *pool)
4536{
4537 struct worker *worker;
4538 int wi;
4539
4540 lockdep_assert_held(&pool->manager_mutex);
3458 4541
3459 /* 4542 /*
3460 * Sched callbacks are disabled now. Zap nr_running. After this, 4543 * Restore CPU affinity of all workers. As all idle workers should
3461 * nr_running stays zero and need_more_worker() and keep_working() 4544 * be on the run-queue of the associated CPU before any local
3462 * are always true as long as the worklist is not empty. Pools on 4545 * wake-ups for concurrency management happen, restore CPU affinty
3463 * @cpu now behave as unbound (in terms of concurrency management) 4546 * of all workers first and then clear UNBOUND. As we're called
3464 * pools which are served by workers tied to the CPU. 4547 * from CPU_ONLINE, the following shouldn't fail.
3465 *
3466 * On return from this function, the current worker would trigger
3467 * unbound chain execution of pending work items if other workers
3468 * didn't already.
3469 */ 4548 */
3470 for_each_std_worker_pool(pool, cpu) 4549 for_each_pool_worker(worker, wi, pool)
3471 atomic_set(&pool->nr_running, 0); 4550 WARN_ON_ONCE(set_cpus_allowed_ptr(worker->task,
4551 pool->attrs->cpumask) < 0);
4552
4553 spin_lock_irq(&pool->lock);
4554
4555 for_each_pool_worker(worker, wi, pool) {
4556 unsigned int worker_flags = worker->flags;
4557
4558 /*
4559 * A bound idle worker should actually be on the runqueue
4560 * of the associated CPU for local wake-ups targeting it to
4561 * work. Kick all idle workers so that they migrate to the
4562 * associated CPU. Doing this in the same loop as
4563 * replacing UNBOUND with REBOUND is safe as no worker will
4564 * be bound before @pool->lock is released.
4565 */
4566 if (worker_flags & WORKER_IDLE)
4567 wake_up_process(worker->task);
4568
4569 /*
4570 * We want to clear UNBOUND but can't directly call
4571 * worker_clr_flags() or adjust nr_running. Atomically
4572 * replace UNBOUND with another NOT_RUNNING flag REBOUND.
4573 * @worker will clear REBOUND using worker_clr_flags() when
4574 * it initiates the next execution cycle thus restoring
4575 * concurrency management. Note that when or whether
4576 * @worker clears REBOUND doesn't affect correctness.
4577 *
4578 * ACCESS_ONCE() is necessary because @worker->flags may be
4579 * tested without holding any lock in
4580 * wq_worker_waking_up(). Without it, NOT_RUNNING test may
4581 * fail incorrectly leading to premature concurrency
4582 * management operations.
4583 */
4584 WARN_ON_ONCE(!(worker_flags & WORKER_UNBOUND));
4585 worker_flags |= WORKER_REBOUND;
4586 worker_flags &= ~WORKER_UNBOUND;
4587 ACCESS_ONCE(worker->flags) = worker_flags;
4588 }
4589
4590 spin_unlock_irq(&pool->lock);
4591}
4592
4593/**
4594 * restore_unbound_workers_cpumask - restore cpumask of unbound workers
4595 * @pool: unbound pool of interest
4596 * @cpu: the CPU which is coming up
4597 *
4598 * An unbound pool may end up with a cpumask which doesn't have any online
4599 * CPUs. When a worker of such pool get scheduled, the scheduler resets
4600 * its cpus_allowed. If @cpu is in @pool's cpumask which didn't have any
4601 * online CPU before, cpus_allowed of all its workers should be restored.
4602 */
4603static void restore_unbound_workers_cpumask(struct worker_pool *pool, int cpu)
4604{
4605 static cpumask_t cpumask;
4606 struct worker *worker;
4607 int wi;
4608
4609 lockdep_assert_held(&pool->manager_mutex);
4610
4611 /* is @cpu allowed for @pool? */
4612 if (!cpumask_test_cpu(cpu, pool->attrs->cpumask))
4613 return;
4614
4615 /* is @cpu the only online CPU? */
4616 cpumask_and(&cpumask, pool->attrs->cpumask, cpu_online_mask);
4617 if (cpumask_weight(&cpumask) != 1)
4618 return;
4619
4620 /* as we're called from CPU_ONLINE, the following shouldn't fail */
4621 for_each_pool_worker(worker, wi, pool)
4622 WARN_ON_ONCE(set_cpus_allowed_ptr(worker->task,
4623 pool->attrs->cpumask) < 0);
3472} 4624}
3473 4625
3474/* 4626/*
@@ -3479,39 +4631,46 @@ static int __cpuinit workqueue_cpu_up_callback(struct notifier_block *nfb,
3479 unsigned long action, 4631 unsigned long action,
3480 void *hcpu) 4632 void *hcpu)
3481{ 4633{
3482 unsigned int cpu = (unsigned long)hcpu; 4634 int cpu = (unsigned long)hcpu;
3483 struct worker_pool *pool; 4635 struct worker_pool *pool;
4636 struct workqueue_struct *wq;
4637 int pi;
3484 4638
3485 switch (action & ~CPU_TASKS_FROZEN) { 4639 switch (action & ~CPU_TASKS_FROZEN) {
3486 case CPU_UP_PREPARE: 4640 case CPU_UP_PREPARE:
3487 for_each_std_worker_pool(pool, cpu) { 4641 for_each_cpu_worker_pool(pool, cpu) {
3488 struct worker *worker;
3489
3490 if (pool->nr_workers) 4642 if (pool->nr_workers)
3491 continue; 4643 continue;
3492 4644 if (create_and_start_worker(pool) < 0)
3493 worker = create_worker(pool);
3494 if (!worker)
3495 return NOTIFY_BAD; 4645 return NOTIFY_BAD;
3496
3497 spin_lock_irq(&pool->lock);
3498 start_worker(worker);
3499 spin_unlock_irq(&pool->lock);
3500 } 4646 }
3501 break; 4647 break;
3502 4648
3503 case CPU_DOWN_FAILED: 4649 case CPU_DOWN_FAILED:
3504 case CPU_ONLINE: 4650 case CPU_ONLINE:
3505 for_each_std_worker_pool(pool, cpu) { 4651 mutex_lock(&wq_pool_mutex);
3506 mutex_lock(&pool->assoc_mutex);
3507 spin_lock_irq(&pool->lock);
3508 4652
3509 pool->flags &= ~POOL_DISASSOCIATED; 4653 for_each_pool(pool, pi) {
3510 rebind_workers(pool); 4654 mutex_lock(&pool->manager_mutex);
3511 4655
3512 spin_unlock_irq(&pool->lock); 4656 if (pool->cpu == cpu) {
3513 mutex_unlock(&pool->assoc_mutex); 4657 spin_lock_irq(&pool->lock);
4658 pool->flags &= ~POOL_DISASSOCIATED;
4659 spin_unlock_irq(&pool->lock);
4660
4661 rebind_workers(pool);
4662 } else if (pool->cpu < 0) {
4663 restore_unbound_workers_cpumask(pool, cpu);
4664 }
4665
4666 mutex_unlock(&pool->manager_mutex);
3514 } 4667 }
4668
4669 /* update NUMA affinity of unbound workqueues */
4670 list_for_each_entry(wq, &workqueues, list)
4671 wq_update_unbound_numa(wq, cpu, true);
4672
4673 mutex_unlock(&wq_pool_mutex);
3515 break; 4674 break;
3516 } 4675 }
3517 return NOTIFY_OK; 4676 return NOTIFY_OK;
@@ -3525,14 +4684,23 @@ static int __cpuinit workqueue_cpu_down_callback(struct notifier_block *nfb,
3525 unsigned long action, 4684 unsigned long action,
3526 void *hcpu) 4685 void *hcpu)
3527{ 4686{
3528 unsigned int cpu = (unsigned long)hcpu; 4687 int cpu = (unsigned long)hcpu;
3529 struct work_struct unbind_work; 4688 struct work_struct unbind_work;
4689 struct workqueue_struct *wq;
3530 4690
3531 switch (action & ~CPU_TASKS_FROZEN) { 4691 switch (action & ~CPU_TASKS_FROZEN) {
3532 case CPU_DOWN_PREPARE: 4692 case CPU_DOWN_PREPARE:
3533 /* unbinding should happen on the local CPU */ 4693 /* unbinding per-cpu workers should happen on the local CPU */
3534 INIT_WORK_ONSTACK(&unbind_work, wq_unbind_fn); 4694 INIT_WORK_ONSTACK(&unbind_work, wq_unbind_fn);
3535 queue_work_on(cpu, system_highpri_wq, &unbind_work); 4695 queue_work_on(cpu, system_highpri_wq, &unbind_work);
4696
4697 /* update NUMA affinity of unbound workqueues */
4698 mutex_lock(&wq_pool_mutex);
4699 list_for_each_entry(wq, &workqueues, list)
4700 wq_update_unbound_numa(wq, cpu, false);
4701 mutex_unlock(&wq_pool_mutex);
4702
4703 /* wait for per-cpu unbinding to finish */
3536 flush_work(&unbind_work); 4704 flush_work(&unbind_work);
3537 break; 4705 break;
3538 } 4706 }
@@ -3565,7 +4733,7 @@ static void work_for_cpu_fn(struct work_struct *work)
3565 * It is up to the caller to ensure that the cpu doesn't go offline. 4733 * It is up to the caller to ensure that the cpu doesn't go offline.
3566 * The caller must not hold any locks which would prevent @fn from completing. 4734 * The caller must not hold any locks which would prevent @fn from completing.
3567 */ 4735 */
3568long work_on_cpu(unsigned int cpu, long (*fn)(void *), void *arg) 4736long work_on_cpu(int cpu, long (*fn)(void *), void *arg)
3569{ 4737{
3570 struct work_for_cpu wfc = { .fn = fn, .arg = arg }; 4738 struct work_for_cpu wfc = { .fn = fn, .arg = arg };
3571 4739
@@ -3583,44 +4751,40 @@ EXPORT_SYMBOL_GPL(work_on_cpu);
3583 * freeze_workqueues_begin - begin freezing workqueues 4751 * freeze_workqueues_begin - begin freezing workqueues
3584 * 4752 *
3585 * Start freezing workqueues. After this function returns, all freezable 4753 * Start freezing workqueues. After this function returns, all freezable
3586 * workqueues will queue new works to their frozen_works list instead of 4754 * workqueues will queue new works to their delayed_works list instead of
3587 * pool->worklist. 4755 * pool->worklist.
3588 * 4756 *
3589 * CONTEXT: 4757 * CONTEXT:
3590 * Grabs and releases workqueue_lock and pool->lock's. 4758 * Grabs and releases wq_pool_mutex, wq->mutex and pool->lock's.
3591 */ 4759 */
3592void freeze_workqueues_begin(void) 4760void freeze_workqueues_begin(void)
3593{ 4761{
3594 unsigned int cpu; 4762 struct worker_pool *pool;
4763 struct workqueue_struct *wq;
4764 struct pool_workqueue *pwq;
4765 int pi;
3595 4766
3596 spin_lock(&workqueue_lock); 4767 mutex_lock(&wq_pool_mutex);
3597 4768
3598 BUG_ON(workqueue_freezing); 4769 WARN_ON_ONCE(workqueue_freezing);
3599 workqueue_freezing = true; 4770 workqueue_freezing = true;
3600 4771
3601 for_each_wq_cpu(cpu) { 4772 /* set FREEZING */
3602 struct worker_pool *pool; 4773 for_each_pool(pool, pi) {
3603 struct workqueue_struct *wq; 4774 spin_lock_irq(&pool->lock);
3604 4775 WARN_ON_ONCE(pool->flags & POOL_FREEZING);
3605 for_each_std_worker_pool(pool, cpu) { 4776 pool->flags |= POOL_FREEZING;
3606 spin_lock_irq(&pool->lock); 4777 spin_unlock_irq(&pool->lock);
3607 4778 }
3608 WARN_ON_ONCE(pool->flags & POOL_FREEZING);
3609 pool->flags |= POOL_FREEZING;
3610
3611 list_for_each_entry(wq, &workqueues, list) {
3612 struct pool_workqueue *pwq = get_pwq(cpu, wq);
3613
3614 if (pwq && pwq->pool == pool &&
3615 (wq->flags & WQ_FREEZABLE))
3616 pwq->max_active = 0;
3617 }
3618 4779
3619 spin_unlock_irq(&pool->lock); 4780 list_for_each_entry(wq, &workqueues, list) {
3620 } 4781 mutex_lock(&wq->mutex);
4782 for_each_pwq(pwq, wq)
4783 pwq_adjust_max_active(pwq);
4784 mutex_unlock(&wq->mutex);
3621 } 4785 }
3622 4786
3623 spin_unlock(&workqueue_lock); 4787 mutex_unlock(&wq_pool_mutex);
3624} 4788}
3625 4789
3626/** 4790/**
@@ -3630,7 +4794,7 @@ void freeze_workqueues_begin(void)
3630 * between freeze_workqueues_begin() and thaw_workqueues(). 4794 * between freeze_workqueues_begin() and thaw_workqueues().
3631 * 4795 *
3632 * CONTEXT: 4796 * CONTEXT:
3633 * Grabs and releases workqueue_lock. 4797 * Grabs and releases wq_pool_mutex.
3634 * 4798 *
3635 * RETURNS: 4799 * RETURNS:
3636 * %true if some freezable workqueues are still busy. %false if freezing 4800 * %true if some freezable workqueues are still busy. %false if freezing
@@ -3638,34 +4802,34 @@ void freeze_workqueues_begin(void)
3638 */ 4802 */
3639bool freeze_workqueues_busy(void) 4803bool freeze_workqueues_busy(void)
3640{ 4804{
3641 unsigned int cpu;
3642 bool busy = false; 4805 bool busy = false;
4806 struct workqueue_struct *wq;
4807 struct pool_workqueue *pwq;
3643 4808
3644 spin_lock(&workqueue_lock); 4809 mutex_lock(&wq_pool_mutex);
3645 4810
3646 BUG_ON(!workqueue_freezing); 4811 WARN_ON_ONCE(!workqueue_freezing);
3647 4812
3648 for_each_wq_cpu(cpu) { 4813 list_for_each_entry(wq, &workqueues, list) {
3649 struct workqueue_struct *wq; 4814 if (!(wq->flags & WQ_FREEZABLE))
4815 continue;
3650 /* 4816 /*
3651 * nr_active is monotonically decreasing. It's safe 4817 * nr_active is monotonically decreasing. It's safe
3652 * to peek without lock. 4818 * to peek without lock.
3653 */ 4819 */
3654 list_for_each_entry(wq, &workqueues, list) { 4820 rcu_read_lock_sched();
3655 struct pool_workqueue *pwq = get_pwq(cpu, wq); 4821 for_each_pwq(pwq, wq) {
3656 4822 WARN_ON_ONCE(pwq->nr_active < 0);
3657 if (!pwq || !(wq->flags & WQ_FREEZABLE))
3658 continue;
3659
3660 BUG_ON(pwq->nr_active < 0);
3661 if (pwq->nr_active) { 4823 if (pwq->nr_active) {
3662 busy = true; 4824 busy = true;
4825 rcu_read_unlock_sched();
3663 goto out_unlock; 4826 goto out_unlock;
3664 } 4827 }
3665 } 4828 }
4829 rcu_read_unlock_sched();
3666 } 4830 }
3667out_unlock: 4831out_unlock:
3668 spin_unlock(&workqueue_lock); 4832 mutex_unlock(&wq_pool_mutex);
3669 return busy; 4833 return busy;
3670} 4834}
3671 4835
@@ -3676,104 +4840,142 @@ out_unlock:
3676 * frozen works are transferred to their respective pool worklists. 4840 * frozen works are transferred to their respective pool worklists.
3677 * 4841 *
3678 * CONTEXT: 4842 * CONTEXT:
3679 * Grabs and releases workqueue_lock and pool->lock's. 4843 * Grabs and releases wq_pool_mutex, wq->mutex and pool->lock's.
3680 */ 4844 */
3681void thaw_workqueues(void) 4845void thaw_workqueues(void)
3682{ 4846{
3683 unsigned int cpu; 4847 struct workqueue_struct *wq;
4848 struct pool_workqueue *pwq;
4849 struct worker_pool *pool;
4850 int pi;
3684 4851
3685 spin_lock(&workqueue_lock); 4852 mutex_lock(&wq_pool_mutex);
3686 4853
3687 if (!workqueue_freezing) 4854 if (!workqueue_freezing)
3688 goto out_unlock; 4855 goto out_unlock;
3689 4856
3690 for_each_wq_cpu(cpu) { 4857 /* clear FREEZING */
3691 struct worker_pool *pool; 4858 for_each_pool(pool, pi) {
3692 struct workqueue_struct *wq; 4859 spin_lock_irq(&pool->lock);
4860 WARN_ON_ONCE(!(pool->flags & POOL_FREEZING));
4861 pool->flags &= ~POOL_FREEZING;
4862 spin_unlock_irq(&pool->lock);
4863 }
3693 4864
3694 for_each_std_worker_pool(pool, cpu) { 4865 /* restore max_active and repopulate worklist */
3695 spin_lock_irq(&pool->lock); 4866 list_for_each_entry(wq, &workqueues, list) {
4867 mutex_lock(&wq->mutex);
4868 for_each_pwq(pwq, wq)
4869 pwq_adjust_max_active(pwq);
4870 mutex_unlock(&wq->mutex);
4871 }
3696 4872
3697 WARN_ON_ONCE(!(pool->flags & POOL_FREEZING)); 4873 workqueue_freezing = false;
3698 pool->flags &= ~POOL_FREEZING; 4874out_unlock:
4875 mutex_unlock(&wq_pool_mutex);
4876}
4877#endif /* CONFIG_FREEZER */
3699 4878
3700 list_for_each_entry(wq, &workqueues, list) { 4879static void __init wq_numa_init(void)
3701 struct pool_workqueue *pwq = get_pwq(cpu, wq); 4880{
4881 cpumask_var_t *tbl;
4882 int node, cpu;
3702 4883
3703 if (!pwq || pwq->pool != pool || 4884 /* determine NUMA pwq table len - highest node id + 1 */
3704 !(wq->flags & WQ_FREEZABLE)) 4885 for_each_node(node)
3705 continue; 4886 wq_numa_tbl_len = max(wq_numa_tbl_len, node + 1);
3706 4887
3707 /* restore max_active and repopulate worklist */ 4888 if (num_possible_nodes() <= 1)
3708 pwq_set_max_active(pwq, wq->saved_max_active); 4889 return;
3709 }
3710 4890
3711 wake_up_worker(pool); 4891 if (wq_disable_numa) {
4892 pr_info("workqueue: NUMA affinity support disabled\n");
4893 return;
4894 }
3712 4895
3713 spin_unlock_irq(&pool->lock); 4896 wq_update_unbound_numa_attrs_buf = alloc_workqueue_attrs(GFP_KERNEL);
4897 BUG_ON(!wq_update_unbound_numa_attrs_buf);
4898
4899 /*
4900 * We want masks of possible CPUs of each node which isn't readily
4901 * available. Build one from cpu_to_node() which should have been
4902 * fully initialized by now.
4903 */
4904 tbl = kzalloc(wq_numa_tbl_len * sizeof(tbl[0]), GFP_KERNEL);
4905 BUG_ON(!tbl);
4906
4907 for_each_node(node)
4908 BUG_ON(!alloc_cpumask_var_node(&tbl[node], GFP_KERNEL,
4909 node_online(node) ? node : NUMA_NO_NODE));
4910
4911 for_each_possible_cpu(cpu) {
4912 node = cpu_to_node(cpu);
4913 if (WARN_ON(node == NUMA_NO_NODE)) {
4914 pr_warn("workqueue: NUMA node mapping not available for cpu%d, disabling NUMA support\n", cpu);
4915 /* happens iff arch is bonkers, let's just proceed */
4916 return;
3714 } 4917 }
4918 cpumask_set_cpu(cpu, tbl[node]);
3715 } 4919 }
3716 4920
3717 workqueue_freezing = false; 4921 wq_numa_possible_cpumask = tbl;
3718out_unlock: 4922 wq_numa_enabled = true;
3719 spin_unlock(&workqueue_lock);
3720} 4923}
3721#endif /* CONFIG_FREEZER */
3722 4924
3723static int __init init_workqueues(void) 4925static int __init init_workqueues(void)
3724{ 4926{
3725 unsigned int cpu; 4927 int std_nice[NR_STD_WORKER_POOLS] = { 0, HIGHPRI_NICE_LEVEL };
4928 int i, cpu;
3726 4929
3727 /* make sure we have enough bits for OFFQ pool ID */ 4930 /* make sure we have enough bits for OFFQ pool ID */
3728 BUILD_BUG_ON((1LU << (BITS_PER_LONG - WORK_OFFQ_POOL_SHIFT)) < 4931 BUILD_BUG_ON((1LU << (BITS_PER_LONG - WORK_OFFQ_POOL_SHIFT)) <
3729 WORK_CPU_END * NR_STD_WORKER_POOLS); 4932 WORK_CPU_END * NR_STD_WORKER_POOLS);
3730 4933
4934 WARN_ON(__alignof__(struct pool_workqueue) < __alignof__(long long));
4935
4936 pwq_cache = KMEM_CACHE(pool_workqueue, SLAB_PANIC);
4937
3731 cpu_notifier(workqueue_cpu_up_callback, CPU_PRI_WORKQUEUE_UP); 4938 cpu_notifier(workqueue_cpu_up_callback, CPU_PRI_WORKQUEUE_UP);
3732 hotcpu_notifier(workqueue_cpu_down_callback, CPU_PRI_WORKQUEUE_DOWN); 4939 hotcpu_notifier(workqueue_cpu_down_callback, CPU_PRI_WORKQUEUE_DOWN);
3733 4940
4941 wq_numa_init();
4942
3734 /* initialize CPU pools */ 4943 /* initialize CPU pools */
3735 for_each_wq_cpu(cpu) { 4944 for_each_possible_cpu(cpu) {
3736 struct worker_pool *pool; 4945 struct worker_pool *pool;
3737 4946
3738 for_each_std_worker_pool(pool, cpu) { 4947 i = 0;
3739 spin_lock_init(&pool->lock); 4948 for_each_cpu_worker_pool(pool, cpu) {
4949 BUG_ON(init_worker_pool(pool));
3740 pool->cpu = cpu; 4950 pool->cpu = cpu;
3741 pool->flags |= POOL_DISASSOCIATED; 4951 cpumask_copy(pool->attrs->cpumask, cpumask_of(cpu));
3742 INIT_LIST_HEAD(&pool->worklist); 4952 pool->attrs->nice = std_nice[i++];
3743 INIT_LIST_HEAD(&pool->idle_list); 4953 pool->node = cpu_to_node(cpu);
3744 hash_init(pool->busy_hash);
3745
3746 init_timer_deferrable(&pool->idle_timer);
3747 pool->idle_timer.function = idle_worker_timeout;
3748 pool->idle_timer.data = (unsigned long)pool;
3749
3750 setup_timer(&pool->mayday_timer, pool_mayday_timeout,
3751 (unsigned long)pool);
3752
3753 mutex_init(&pool->assoc_mutex);
3754 ida_init(&pool->worker_ida);
3755 4954
3756 /* alloc pool ID */ 4955 /* alloc pool ID */
4956 mutex_lock(&wq_pool_mutex);
3757 BUG_ON(worker_pool_assign_id(pool)); 4957 BUG_ON(worker_pool_assign_id(pool));
4958 mutex_unlock(&wq_pool_mutex);
3758 } 4959 }
3759 } 4960 }
3760 4961
3761 /* create the initial worker */ 4962 /* create the initial worker */
3762 for_each_online_wq_cpu(cpu) { 4963 for_each_online_cpu(cpu) {
3763 struct worker_pool *pool; 4964 struct worker_pool *pool;
3764 4965
3765 for_each_std_worker_pool(pool, cpu) { 4966 for_each_cpu_worker_pool(pool, cpu) {
3766 struct worker *worker; 4967 pool->flags &= ~POOL_DISASSOCIATED;
4968 BUG_ON(create_and_start_worker(pool) < 0);
4969 }
4970 }
3767 4971
3768 if (cpu != WORK_CPU_UNBOUND) 4972 /* create default unbound wq attrs */
3769 pool->flags &= ~POOL_DISASSOCIATED; 4973 for (i = 0; i < NR_STD_WORKER_POOLS; i++) {
4974 struct workqueue_attrs *attrs;
3770 4975
3771 worker = create_worker(pool); 4976 BUG_ON(!(attrs = alloc_workqueue_attrs(GFP_KERNEL)));
3772 BUG_ON(!worker); 4977 attrs->nice = std_nice[i];
3773 spin_lock_irq(&pool->lock); 4978 unbound_std_wq_attrs[i] = attrs;
3774 start_worker(worker);
3775 spin_unlock_irq(&pool->lock);
3776 }
3777 } 4979 }
3778 4980
3779 system_wq = alloc_workqueue("events", 0, 0); 4981 system_wq = alloc_workqueue("events", 0, 0);