aboutsummaryrefslogtreecommitdiffstats
path: root/kernel/workqueue.c
diff options
context:
space:
mode:
Diffstat (limited to 'kernel/workqueue.c')
-rw-r--r--kernel/workqueue.c2947
1 files changed, 2069 insertions, 878 deletions
diff --git a/kernel/workqueue.c b/kernel/workqueue.c
index 55fac5b991b7..4aa9f5bc6b2d 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,75 +194,107 @@ 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_GPL(system_wq);
238struct workqueue_struct *system_highpri_wq __read_mostly; 300struct workqueue_struct *system_highpri_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,
473{ 532 int node)
474 return idr_find(&worker_pool_idr, pool_id);
475}
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{ 533{
487 if (!(wq->flags & WQ_UNBOUND)) { 534 assert_rcu_or_wq_mutex(wq);
488 if (likely(cpu < nr_cpu_ids)) 535 return rcu_dereference_raw(wq->numa_pwq_tbl[node]);
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
@@ -1287,22 +1413,6 @@ bool queue_work_on(int cpu, struct workqueue_struct *wq,
1287} 1413}
1288EXPORT_SYMBOL_GPL(queue_work_on); 1414EXPORT_SYMBOL_GPL(queue_work_on);
1289 1415
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
1306void delayed_work_timer_fn(unsigned long __data) 1416void delayed_work_timer_fn(unsigned long __data)
1307{ 1417{
1308 struct delayed_work *dwork = (struct delayed_work *)__data; 1418 struct delayed_work *dwork = (struct delayed_work *)__data;
@@ -1378,21 +1488,6 @@ bool queue_delayed_work_on(int cpu, struct workqueue_struct *wq,
1378EXPORT_SYMBOL_GPL(queue_delayed_work_on); 1488EXPORT_SYMBOL_GPL(queue_delayed_work_on);
1379 1489
1380/** 1490/**
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
1395/**
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
1397 * @cpu: CPU number to execute work on 1492 * @cpu: CPU number to execute work on
1398 * @wq: workqueue to use 1493 * @wq: workqueue to use
@@ -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,37 @@ 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 /*
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; 2062 ret = true;
2049 } 2063 }
2050 2064
@@ -2057,8 +2071,8 @@ static bool manage_workers(struct worker *worker)
2057 ret |= maybe_destroy_workers(pool); 2071 ret |= maybe_destroy_workers(pool);
2058 ret |= maybe_create_worker(pool); 2072 ret |= maybe_create_worker(pool);
2059 2073
2060 pool->flags &= ~POOL_MANAGING_WORKERS; 2074 mutex_unlock(&pool->manager_mutex);
2061 mutex_unlock(&pool->assoc_mutex); 2075 mutex_unlock(&pool->manager_arb);
2062 return ret; 2076 return ret;
2063} 2077}
2064 2078
@@ -2184,6 +2198,7 @@ __acquires(&pool->lock)
2184 worker->current_work = NULL; 2198 worker->current_work = NULL;
2185 worker->current_func = NULL; 2199 worker->current_func = NULL;
2186 worker->current_pwq = NULL; 2200 worker->current_pwq = NULL;
2201 worker->desc_valid = false;
2187 pwq_dec_nr_in_flight(pwq, work_color); 2202 pwq_dec_nr_in_flight(pwq, work_color);
2188} 2203}
2189 2204
@@ -2212,11 +2227,11 @@ static void process_scheduled_works(struct worker *worker)
2212 * worker_thread - the worker thread function 2227 * worker_thread - the worker thread function
2213 * @__worker: self 2228 * @__worker: self
2214 * 2229 *
2215 * The worker thread function. There are NR_CPU_WORKER_POOLS dynamic pools 2230 * The worker thread function. All workers belong to a worker_pool -
2216 * of these per each cpu. These workers process all works regardless of 2231 * either a per-cpu one or dynamic unbound one. These workers process all
2217 * their specific target workqueue. The only exception is works which 2232 * work items regardless of their specific target workqueue. The only
2218 * belong to workqueues with a rescuer which will be explained in 2233 * exception is work items which belong to workqueues with a rescuer which
2219 * rescuer_thread(). 2234 * will be explained in rescuer_thread().
2220 */ 2235 */
2221static int worker_thread(void *__worker) 2236static int worker_thread(void *__worker)
2222{ 2237{
@@ -2228,19 +2243,12 @@ static int worker_thread(void *__worker)
2228woke_up: 2243woke_up:
2229 spin_lock_irq(&pool->lock); 2244 spin_lock_irq(&pool->lock);
2230 2245
2231 /* we are off idle list if destruction or rebind is requested */ 2246 /* am I supposed to die? */
2232 if (unlikely(list_empty(&worker->entry))) { 2247 if (unlikely(worker->flags & WORKER_DIE)) {
2233 spin_unlock_irq(&pool->lock); 2248 spin_unlock_irq(&pool->lock);
2234 2249 WARN_ON_ONCE(!list_empty(&worker->entry));
2235 /* if DIE is set, destruction is requested */ 2250 worker->task->flags &= ~PF_WQ_WORKER;
2236 if (worker->flags & WORKER_DIE) { 2251 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 } 2252 }
2245 2253
2246 worker_leave_idle(worker); 2254 worker_leave_idle(worker);
@@ -2258,14 +2266,16 @@ recheck:
2258 * preparing to process a work or actually processing it. 2266 * preparing to process a work or actually processing it.
2259 * Make sure nobody diddled with it while I was sleeping. 2267 * Make sure nobody diddled with it while I was sleeping.
2260 */ 2268 */
2261 BUG_ON(!list_empty(&worker->scheduled)); 2269 WARN_ON_ONCE(!list_empty(&worker->scheduled));
2262 2270
2263 /* 2271 /*
2264 * When control reaches this point, we're guaranteed to have 2272 * Finish PREP stage. We're guaranteed to have at least one idle
2265 * at least one idle worker or that someone else has already 2273 * worker or that someone else has already assumed the manager
2266 * assumed the manager role. 2274 * role. This is where @worker starts participating in concurrency
2275 * management if applicable and concurrency management is restored
2276 * after being rebound. See rebind_workers() for details.
2267 */ 2277 */
2268 worker_clr_flags(worker, WORKER_PREP); 2278 worker_clr_flags(worker, WORKER_PREP | WORKER_REBOUND);
2269 2279
2270 do { 2280 do {
2271 struct work_struct *work = 2281 struct work_struct *work =
@@ -2307,7 +2317,7 @@ sleep:
2307 * @__rescuer: self 2317 * @__rescuer: self
2308 * 2318 *
2309 * Workqueue rescuer thread function. There's one rescuer for each 2319 * Workqueue rescuer thread function. There's one rescuer for each
2310 * workqueue which has WQ_RESCUER set. 2320 * workqueue which has WQ_MEM_RECLAIM set.
2311 * 2321 *
2312 * Regular work processing on a pool may block trying to create a new 2322 * 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 2323 * worker which uses GFP_KERNEL allocation which has slight chance of
@@ -2326,8 +2336,6 @@ static int rescuer_thread(void *__rescuer)
2326 struct worker *rescuer = __rescuer; 2336 struct worker *rescuer = __rescuer;
2327 struct workqueue_struct *wq = rescuer->rescue_wq; 2337 struct workqueue_struct *wq = rescuer->rescue_wq;
2328 struct list_head *scheduled = &rescuer->scheduled; 2338 struct list_head *scheduled = &rescuer->scheduled;
2329 bool is_unbound = wq->flags & WQ_UNBOUND;
2330 unsigned int cpu;
2331 2339
2332 set_user_nice(current, RESCUER_NICE_LEVEL); 2340 set_user_nice(current, RESCUER_NICE_LEVEL);
2333 2341
@@ -2345,28 +2353,29 @@ repeat:
2345 return 0; 2353 return 0;
2346 } 2354 }
2347 2355
2348 /* 2356 /* see whether any pwq is asking for help */
2349 * See whether any cpu is asking for help. Unbounded 2357 spin_lock_irq(&wq_mayday_lock);
2350 * workqueues use cpu 0 in mayday_mask for CPU_UNBOUND. 2358
2351 */ 2359 while (!list_empty(&wq->maydays)) {
2352 for_each_mayday_cpu(cpu, wq->mayday_mask) { 2360 struct pool_workqueue *pwq = list_first_entry(&wq->maydays,
2353 unsigned int tcpu = is_unbound ? WORK_CPU_UNBOUND : cpu; 2361 struct pool_workqueue, mayday_node);
2354 struct pool_workqueue *pwq = get_pwq(tcpu, wq);
2355 struct worker_pool *pool = pwq->pool; 2362 struct worker_pool *pool = pwq->pool;
2356 struct work_struct *work, *n; 2363 struct work_struct *work, *n;
2357 2364
2358 __set_current_state(TASK_RUNNING); 2365 __set_current_state(TASK_RUNNING);
2359 mayday_clear_cpu(cpu, wq->mayday_mask); 2366 list_del_init(&pwq->mayday_node);
2367
2368 spin_unlock_irq(&wq_mayday_lock);
2360 2369
2361 /* migrate to the target cpu if possible */ 2370 /* migrate to the target cpu if possible */
2371 worker_maybe_bind_and_lock(pool);
2362 rescuer->pool = pool; 2372 rescuer->pool = pool;
2363 worker_maybe_bind_and_lock(rescuer);
2364 2373
2365 /* 2374 /*
2366 * Slurp in all works issued via this workqueue and 2375 * Slurp in all works issued via this workqueue and
2367 * process'em. 2376 * process'em.
2368 */ 2377 */
2369 BUG_ON(!list_empty(&rescuer->scheduled)); 2378 WARN_ON_ONCE(!list_empty(&rescuer->scheduled));
2370 list_for_each_entry_safe(work, n, &pool->worklist, entry) 2379 list_for_each_entry_safe(work, n, &pool->worklist, entry)
2371 if (get_work_pwq(work) == pwq) 2380 if (get_work_pwq(work) == pwq)
2372 move_linked_works(work, scheduled, &n); 2381 move_linked_works(work, scheduled, &n);
@@ -2381,9 +2390,13 @@ repeat:
2381 if (keep_working(pool)) 2390 if (keep_working(pool))
2382 wake_up_worker(pool); 2391 wake_up_worker(pool);
2383 2392
2384 spin_unlock_irq(&pool->lock); 2393 rescuer->pool = NULL;
2394 spin_unlock(&pool->lock);
2395 spin_lock(&wq_mayday_lock);
2385 } 2396 }
2386 2397
2398 spin_unlock_irq(&wq_mayday_lock);
2399
2387 /* rescuers should never participate in concurrency management */ 2400 /* rescuers should never participate in concurrency management */
2388 WARN_ON_ONCE(!(rescuer->flags & WORKER_NOT_RUNNING)); 2401 WARN_ON_ONCE(!(rescuer->flags & WORKER_NOT_RUNNING));
2389 schedule(); 2402 schedule();
@@ -2487,7 +2500,7 @@ static void insert_wq_barrier(struct pool_workqueue *pwq,
2487 * advanced to @work_color. 2500 * advanced to @work_color.
2488 * 2501 *
2489 * CONTEXT: 2502 * CONTEXT:
2490 * mutex_lock(wq->flush_mutex). 2503 * mutex_lock(wq->mutex).
2491 * 2504 *
2492 * RETURNS: 2505 * RETURNS:
2493 * %true if @flush_color >= 0 and there's something to flush. %false 2506 * %true if @flush_color >= 0 and there's something to flush. %false
@@ -2497,21 +2510,20 @@ static bool flush_workqueue_prep_pwqs(struct workqueue_struct *wq,
2497 int flush_color, int work_color) 2510 int flush_color, int work_color)
2498{ 2511{
2499 bool wait = false; 2512 bool wait = false;
2500 unsigned int cpu; 2513 struct pool_workqueue *pwq;
2501 2514
2502 if (flush_color >= 0) { 2515 if (flush_color >= 0) {
2503 BUG_ON(atomic_read(&wq->nr_pwqs_to_flush)); 2516 WARN_ON_ONCE(atomic_read(&wq->nr_pwqs_to_flush));
2504 atomic_set(&wq->nr_pwqs_to_flush, 1); 2517 atomic_set(&wq->nr_pwqs_to_flush, 1);
2505 } 2518 }
2506 2519
2507 for_each_pwq_cpu(cpu, wq) { 2520 for_each_pwq(pwq, wq) {
2508 struct pool_workqueue *pwq = get_pwq(cpu, wq);
2509 struct worker_pool *pool = pwq->pool; 2521 struct worker_pool *pool = pwq->pool;
2510 2522
2511 spin_lock_irq(&pool->lock); 2523 spin_lock_irq(&pool->lock);
2512 2524
2513 if (flush_color >= 0) { 2525 if (flush_color >= 0) {
2514 BUG_ON(pwq->flush_color != -1); 2526 WARN_ON_ONCE(pwq->flush_color != -1);
2515 2527
2516 if (pwq->nr_in_flight[flush_color]) { 2528 if (pwq->nr_in_flight[flush_color]) {
2517 pwq->flush_color = flush_color; 2529 pwq->flush_color = flush_color;
@@ -2521,7 +2533,7 @@ static bool flush_workqueue_prep_pwqs(struct workqueue_struct *wq,
2521 } 2533 }
2522 2534
2523 if (work_color >= 0) { 2535 if (work_color >= 0) {
2524 BUG_ON(work_color != work_next_color(pwq->work_color)); 2536 WARN_ON_ONCE(work_color != work_next_color(pwq->work_color));
2525 pwq->work_color = work_color; 2537 pwq->work_color = work_color;
2526 } 2538 }
2527 2539
@@ -2538,11 +2550,8 @@ static bool flush_workqueue_prep_pwqs(struct workqueue_struct *wq,
2538 * flush_workqueue - ensure that any scheduled work has run to completion. 2550 * flush_workqueue - ensure that any scheduled work has run to completion.
2539 * @wq: workqueue to flush 2551 * @wq: workqueue to flush
2540 * 2552 *
2541 * Forces execution of the workqueue and blocks until its completion. 2553 * This function sleeps until all work items which were queued on entry
2542 * This is typically used in driver shutdown handlers. 2554 * 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 */ 2555 */
2547void flush_workqueue(struct workqueue_struct *wq) 2556void flush_workqueue(struct workqueue_struct *wq)
2548{ 2557{
@@ -2556,7 +2565,7 @@ void flush_workqueue(struct workqueue_struct *wq)
2556 lock_map_acquire(&wq->lockdep_map); 2565 lock_map_acquire(&wq->lockdep_map);
2557 lock_map_release(&wq->lockdep_map); 2566 lock_map_release(&wq->lockdep_map);
2558 2567
2559 mutex_lock(&wq->flush_mutex); 2568 mutex_lock(&wq->mutex);
2560 2569
2561 /* 2570 /*
2562 * Start-to-wait phase 2571 * Start-to-wait phase
@@ -2569,13 +2578,13 @@ void flush_workqueue(struct workqueue_struct *wq)
2569 * becomes our flush_color and work_color is advanced 2578 * becomes our flush_color and work_color is advanced
2570 * by one. 2579 * by one.
2571 */ 2580 */
2572 BUG_ON(!list_empty(&wq->flusher_overflow)); 2581 WARN_ON_ONCE(!list_empty(&wq->flusher_overflow));
2573 this_flusher.flush_color = wq->work_color; 2582 this_flusher.flush_color = wq->work_color;
2574 wq->work_color = next_color; 2583 wq->work_color = next_color;
2575 2584
2576 if (!wq->first_flusher) { 2585 if (!wq->first_flusher) {
2577 /* no flush in progress, become the first flusher */ 2586 /* no flush in progress, become the first flusher */
2578 BUG_ON(wq->flush_color != this_flusher.flush_color); 2587 WARN_ON_ONCE(wq->flush_color != this_flusher.flush_color);
2579 2588
2580 wq->first_flusher = &this_flusher; 2589 wq->first_flusher = &this_flusher;
2581 2590
@@ -2588,7 +2597,7 @@ void flush_workqueue(struct workqueue_struct *wq)
2588 } 2597 }
2589 } else { 2598 } else {
2590 /* wait in queue */ 2599 /* wait in queue */
2591 BUG_ON(wq->flush_color == this_flusher.flush_color); 2600 WARN_ON_ONCE(wq->flush_color == this_flusher.flush_color);
2592 list_add_tail(&this_flusher.list, &wq->flusher_queue); 2601 list_add_tail(&this_flusher.list, &wq->flusher_queue);
2593 flush_workqueue_prep_pwqs(wq, -1, wq->work_color); 2602 flush_workqueue_prep_pwqs(wq, -1, wq->work_color);
2594 } 2603 }
@@ -2601,7 +2610,7 @@ void flush_workqueue(struct workqueue_struct *wq)
2601 list_add_tail(&this_flusher.list, &wq->flusher_overflow); 2610 list_add_tail(&this_flusher.list, &wq->flusher_overflow);
2602 } 2611 }
2603 2612
2604 mutex_unlock(&wq->flush_mutex); 2613 mutex_unlock(&wq->mutex);
2605 2614
2606 wait_for_completion(&this_flusher.done); 2615 wait_for_completion(&this_flusher.done);
2607 2616
@@ -2614,7 +2623,7 @@ void flush_workqueue(struct workqueue_struct *wq)
2614 if (wq->first_flusher != &this_flusher) 2623 if (wq->first_flusher != &this_flusher)
2615 return; 2624 return;
2616 2625
2617 mutex_lock(&wq->flush_mutex); 2626 mutex_lock(&wq->mutex);
2618 2627
2619 /* we might have raced, check again with mutex held */ 2628 /* we might have raced, check again with mutex held */
2620 if (wq->first_flusher != &this_flusher) 2629 if (wq->first_flusher != &this_flusher)
@@ -2622,8 +2631,8 @@ void flush_workqueue(struct workqueue_struct *wq)
2622 2631
2623 wq->first_flusher = NULL; 2632 wq->first_flusher = NULL;
2624 2633
2625 BUG_ON(!list_empty(&this_flusher.list)); 2634 WARN_ON_ONCE(!list_empty(&this_flusher.list));
2626 BUG_ON(wq->flush_color != this_flusher.flush_color); 2635 WARN_ON_ONCE(wq->flush_color != this_flusher.flush_color);
2627 2636
2628 while (true) { 2637 while (true) {
2629 struct wq_flusher *next, *tmp; 2638 struct wq_flusher *next, *tmp;
@@ -2636,8 +2645,8 @@ void flush_workqueue(struct workqueue_struct *wq)
2636 complete(&next->done); 2645 complete(&next->done);
2637 } 2646 }
2638 2647
2639 BUG_ON(!list_empty(&wq->flusher_overflow) && 2648 WARN_ON_ONCE(!list_empty(&wq->flusher_overflow) &&
2640 wq->flush_color != work_next_color(wq->work_color)); 2649 wq->flush_color != work_next_color(wq->work_color));
2641 2650
2642 /* this flush_color is finished, advance by one */ 2651 /* this flush_color is finished, advance by one */
2643 wq->flush_color = work_next_color(wq->flush_color); 2652 wq->flush_color = work_next_color(wq->flush_color);
@@ -2661,7 +2670,7 @@ void flush_workqueue(struct workqueue_struct *wq)
2661 } 2670 }
2662 2671
2663 if (list_empty(&wq->flusher_queue)) { 2672 if (list_empty(&wq->flusher_queue)) {
2664 BUG_ON(wq->flush_color != wq->work_color); 2673 WARN_ON_ONCE(wq->flush_color != wq->work_color);
2665 break; 2674 break;
2666 } 2675 }
2667 2676
@@ -2669,8 +2678,8 @@ void flush_workqueue(struct workqueue_struct *wq)
2669 * Need to flush more colors. Make the next flusher 2678 * Need to flush more colors. Make the next flusher
2670 * the new first flusher and arm pwqs. 2679 * the new first flusher and arm pwqs.
2671 */ 2680 */
2672 BUG_ON(wq->flush_color == wq->work_color); 2681 WARN_ON_ONCE(wq->flush_color == wq->work_color);
2673 BUG_ON(wq->flush_color != next->flush_color); 2682 WARN_ON_ONCE(wq->flush_color != next->flush_color);
2674 2683
2675 list_del_init(&next->list); 2684 list_del_init(&next->list);
2676 wq->first_flusher = next; 2685 wq->first_flusher = next;
@@ -2686,7 +2695,7 @@ void flush_workqueue(struct workqueue_struct *wq)
2686 } 2695 }
2687 2696
2688out_unlock: 2697out_unlock:
2689 mutex_unlock(&wq->flush_mutex); 2698 mutex_unlock(&wq->mutex);
2690} 2699}
2691EXPORT_SYMBOL_GPL(flush_workqueue); 2700EXPORT_SYMBOL_GPL(flush_workqueue);
2692 2701
@@ -2704,22 +2713,23 @@ EXPORT_SYMBOL_GPL(flush_workqueue);
2704void drain_workqueue(struct workqueue_struct *wq) 2713void drain_workqueue(struct workqueue_struct *wq)
2705{ 2714{
2706 unsigned int flush_cnt = 0; 2715 unsigned int flush_cnt = 0;
2707 unsigned int cpu; 2716 struct pool_workqueue *pwq;
2708 2717
2709 /* 2718 /*
2710 * __queue_work() needs to test whether there are drainers, is much 2719 * __queue_work() needs to test whether there are drainers, is much
2711 * hotter than drain_workqueue() and already looks at @wq->flags. 2720 * hotter than drain_workqueue() and already looks at @wq->flags.
2712 * Use WQ_DRAINING so that queue doesn't have to check nr_drainers. 2721 * Use __WQ_DRAINING so that queue doesn't have to check nr_drainers.
2713 */ 2722 */
2714 spin_lock(&workqueue_lock); 2723 mutex_lock(&wq->mutex);
2715 if (!wq->nr_drainers++) 2724 if (!wq->nr_drainers++)
2716 wq->flags |= WQ_DRAINING; 2725 wq->flags |= __WQ_DRAINING;
2717 spin_unlock(&workqueue_lock); 2726 mutex_unlock(&wq->mutex);
2718reflush: 2727reflush:
2719 flush_workqueue(wq); 2728 flush_workqueue(wq);
2720 2729
2721 for_each_pwq_cpu(cpu, wq) { 2730 mutex_lock(&wq->mutex);
2722 struct pool_workqueue *pwq = get_pwq(cpu, wq); 2731
2732 for_each_pwq(pwq, wq) {
2723 bool drained; 2733 bool drained;
2724 2734
2725 spin_lock_irq(&pwq->pool->lock); 2735 spin_lock_irq(&pwq->pool->lock);
@@ -2731,15 +2741,16 @@ reflush:
2731 2741
2732 if (++flush_cnt == 10 || 2742 if (++flush_cnt == 10 ||
2733 (flush_cnt % 100 == 0 && flush_cnt <= 1000)) 2743 (flush_cnt % 100 == 0 && flush_cnt <= 1000))
2734 pr_warn("workqueue %s: flush on destruction isn't complete after %u tries\n", 2744 pr_warn("workqueue %s: drain_workqueue() isn't complete after %u tries\n",
2735 wq->name, flush_cnt); 2745 wq->name, flush_cnt);
2746
2747 mutex_unlock(&wq->mutex);
2736 goto reflush; 2748 goto reflush;
2737 } 2749 }
2738 2750
2739 spin_lock(&workqueue_lock);
2740 if (!--wq->nr_drainers) 2751 if (!--wq->nr_drainers)
2741 wq->flags &= ~WQ_DRAINING; 2752 wq->flags &= ~__WQ_DRAINING;
2742 spin_unlock(&workqueue_lock); 2753 mutex_unlock(&wq->mutex);
2743} 2754}
2744EXPORT_SYMBOL_GPL(drain_workqueue); 2755EXPORT_SYMBOL_GPL(drain_workqueue);
2745 2756
@@ -2750,11 +2761,15 @@ static bool start_flush_work(struct work_struct *work, struct wq_barrier *barr)
2750 struct pool_workqueue *pwq; 2761 struct pool_workqueue *pwq;
2751 2762
2752 might_sleep(); 2763 might_sleep();
2764
2765 local_irq_disable();
2753 pool = get_work_pool(work); 2766 pool = get_work_pool(work);
2754 if (!pool) 2767 if (!pool) {
2768 local_irq_enable();
2755 return false; 2769 return false;
2770 }
2756 2771
2757 spin_lock_irq(&pool->lock); 2772 spin_lock(&pool->lock);
2758 /* see the comment in try_to_grab_pending() with the same code */ 2773 /* see the comment in try_to_grab_pending() with the same code */
2759 pwq = get_work_pwq(work); 2774 pwq = get_work_pwq(work);
2760 if (pwq) { 2775 if (pwq) {
@@ -2776,7 +2791,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 2791 * flusher is not running on the same workqueue by verifying write
2777 * access. 2792 * access.
2778 */ 2793 */
2779 if (pwq->wq->saved_max_active == 1 || pwq->wq->flags & WQ_RESCUER) 2794 if (pwq->wq->saved_max_active == 1 || pwq->wq->rescuer)
2780 lock_map_acquire(&pwq->wq->lockdep_map); 2795 lock_map_acquire(&pwq->wq->lockdep_map);
2781 else 2796 else
2782 lock_map_acquire_read(&pwq->wq->lockdep_map); 2797 lock_map_acquire_read(&pwq->wq->lockdep_map);
@@ -2933,66 +2948,6 @@ bool cancel_delayed_work_sync(struct delayed_work *dwork)
2933EXPORT_SYMBOL(cancel_delayed_work_sync); 2948EXPORT_SYMBOL(cancel_delayed_work_sync);
2934 2949
2935/** 2950/**
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 2951 * schedule_on_each_cpu - execute a function synchronously on each online CPU
2997 * @func: the function to call 2952 * @func: the function to call
2998 * 2953 *
@@ -3085,51 +3040,1025 @@ int execute_in_process_context(work_func_t fn, struct execute_work *ew)
3085} 3040}
3086EXPORT_SYMBOL_GPL(execute_in_process_context); 3041EXPORT_SYMBOL_GPL(execute_in_process_context);
3087 3042
3088int keventd_up(void) 3043#ifdef CONFIG_SYSFS
3044/*
3045 * Workqueues with WQ_SYSFS flag set is visible to userland via
3046 * /sys/bus/workqueue/devices/WQ_NAME. All visible workqueues have the
3047 * following attributes.
3048 *
3049 * per_cpu RO bool : whether the workqueue is per-cpu or unbound
3050 * max_active RW int : maximum number of in-flight work items
3051 *
3052 * Unbound workqueues have the following extra attributes.
3053 *
3054 * id RO int : the associated pool ID
3055 * nice RW int : nice value of the workers
3056 * cpumask RW mask : bitmask of allowed CPUs for the workers
3057 */
3058struct wq_device {
3059 struct workqueue_struct *wq;
3060 struct device dev;
3061};
3062
3063static struct workqueue_struct *dev_to_wq(struct device *dev)
3064{
3065 struct wq_device *wq_dev = container_of(dev, struct wq_device, dev);
3066
3067 return wq_dev->wq;
3068}
3069
3070static ssize_t wq_per_cpu_show(struct device *dev,
3071 struct device_attribute *attr, char *buf)
3072{
3073 struct workqueue_struct *wq = dev_to_wq(dev);
3074
3075 return scnprintf(buf, PAGE_SIZE, "%d\n", (bool)!(wq->flags & WQ_UNBOUND));
3076}
3077
3078static ssize_t wq_max_active_show(struct device *dev,
3079 struct device_attribute *attr, char *buf)
3089{ 3080{
3090 return system_wq != NULL; 3081 struct workqueue_struct *wq = dev_to_wq(dev);
3082
3083 return scnprintf(buf, PAGE_SIZE, "%d\n", wq->saved_max_active);
3091} 3084}
3092 3085
3093static int alloc_pwqs(struct workqueue_struct *wq) 3086static ssize_t wq_max_active_store(struct device *dev,
3087 struct device_attribute *attr,
3088 const char *buf, size_t count)
3094{ 3089{
3090 struct workqueue_struct *wq = dev_to_wq(dev);
3091 int val;
3092
3093 if (sscanf(buf, "%d", &val) != 1 || val <= 0)
3094 return -EINVAL;
3095
3096 workqueue_set_max_active(wq, val);
3097 return count;
3098}
3099
3100static struct device_attribute wq_sysfs_attrs[] = {
3101 __ATTR(per_cpu, 0444, wq_per_cpu_show, NULL),
3102 __ATTR(max_active, 0644, wq_max_active_show, wq_max_active_store),
3103 __ATTR_NULL,
3104};
3105
3106static ssize_t wq_pool_ids_show(struct device *dev,
3107 struct device_attribute *attr, char *buf)
3108{
3109 struct workqueue_struct *wq = dev_to_wq(dev);
3110 const char *delim = "";
3111 int node, written = 0;
3112
3113 rcu_read_lock_sched();
3114 for_each_node(node) {
3115 written += scnprintf(buf + written, PAGE_SIZE - written,
3116 "%s%d:%d", delim, node,
3117 unbound_pwq_by_node(wq, node)->pool->id);
3118 delim = " ";
3119 }
3120 written += scnprintf(buf + written, PAGE_SIZE - written, "\n");
3121 rcu_read_unlock_sched();
3122
3123 return written;
3124}
3125
3126static ssize_t wq_nice_show(struct device *dev, struct device_attribute *attr,
3127 char *buf)
3128{
3129 struct workqueue_struct *wq = dev_to_wq(dev);
3130 int written;
3131
3132 mutex_lock(&wq->mutex);
3133 written = scnprintf(buf, PAGE_SIZE, "%d\n", wq->unbound_attrs->nice);
3134 mutex_unlock(&wq->mutex);
3135
3136 return written;
3137}
3138
3139/* prepare workqueue_attrs for sysfs store operations */
3140static struct workqueue_attrs *wq_sysfs_prep_attrs(struct workqueue_struct *wq)
3141{
3142 struct workqueue_attrs *attrs;
3143
3144 attrs = alloc_workqueue_attrs(GFP_KERNEL);
3145 if (!attrs)
3146 return NULL;
3147
3148 mutex_lock(&wq->mutex);
3149 copy_workqueue_attrs(attrs, wq->unbound_attrs);
3150 mutex_unlock(&wq->mutex);
3151 return attrs;
3152}
3153
3154static ssize_t wq_nice_store(struct device *dev, struct device_attribute *attr,
3155 const char *buf, size_t count)
3156{
3157 struct workqueue_struct *wq = dev_to_wq(dev);
3158 struct workqueue_attrs *attrs;
3159 int ret;
3160
3161 attrs = wq_sysfs_prep_attrs(wq);
3162 if (!attrs)
3163 return -ENOMEM;
3164
3165 if (sscanf(buf, "%d", &attrs->nice) == 1 &&
3166 attrs->nice >= -20 && attrs->nice <= 19)
3167 ret = apply_workqueue_attrs(wq, attrs);
3168 else
3169 ret = -EINVAL;
3170
3171 free_workqueue_attrs(attrs);
3172 return ret ?: count;
3173}
3174
3175static ssize_t wq_cpumask_show(struct device *dev,
3176 struct device_attribute *attr, char *buf)
3177{
3178 struct workqueue_struct *wq = dev_to_wq(dev);
3179 int written;
3180
3181 mutex_lock(&wq->mutex);
3182 written = cpumask_scnprintf(buf, PAGE_SIZE, wq->unbound_attrs->cpumask);
3183 mutex_unlock(&wq->mutex);
3184
3185 written += scnprintf(buf + written, PAGE_SIZE - written, "\n");
3186 return written;
3187}
3188
3189static ssize_t wq_cpumask_store(struct device *dev,
3190 struct device_attribute *attr,
3191 const char *buf, size_t count)
3192{
3193 struct workqueue_struct *wq = dev_to_wq(dev);
3194 struct workqueue_attrs *attrs;
3195 int ret;
3196
3197 attrs = wq_sysfs_prep_attrs(wq);
3198 if (!attrs)
3199 return -ENOMEM;
3200
3201 ret = cpumask_parse(buf, attrs->cpumask);
3202 if (!ret)
3203 ret = apply_workqueue_attrs(wq, attrs);
3204
3205 free_workqueue_attrs(attrs);
3206 return ret ?: count;
3207}
3208
3209static ssize_t wq_numa_show(struct device *dev, struct device_attribute *attr,
3210 char *buf)
3211{
3212 struct workqueue_struct *wq = dev_to_wq(dev);
3213 int written;
3214
3215 mutex_lock(&wq->mutex);
3216 written = scnprintf(buf, PAGE_SIZE, "%d\n",
3217 !wq->unbound_attrs->no_numa);
3218 mutex_unlock(&wq->mutex);
3219
3220 return written;
3221}
3222
3223static ssize_t wq_numa_store(struct device *dev, struct device_attribute *attr,
3224 const char *buf, size_t count)
3225{
3226 struct workqueue_struct *wq = dev_to_wq(dev);
3227 struct workqueue_attrs *attrs;
3228 int v, ret;
3229
3230 attrs = wq_sysfs_prep_attrs(wq);
3231 if (!attrs)
3232 return -ENOMEM;
3233
3234 ret = -EINVAL;
3235 if (sscanf(buf, "%d", &v) == 1) {
3236 attrs->no_numa = !v;
3237 ret = apply_workqueue_attrs(wq, attrs);
3238 }
3239
3240 free_workqueue_attrs(attrs);
3241 return ret ?: count;
3242}
3243
3244static struct device_attribute wq_sysfs_unbound_attrs[] = {
3245 __ATTR(pool_ids, 0444, wq_pool_ids_show, NULL),
3246 __ATTR(nice, 0644, wq_nice_show, wq_nice_store),
3247 __ATTR(cpumask, 0644, wq_cpumask_show, wq_cpumask_store),
3248 __ATTR(numa, 0644, wq_numa_show, wq_numa_store),
3249 __ATTR_NULL,
3250};
3251
3252static struct bus_type wq_subsys = {
3253 .name = "workqueue",
3254 .dev_attrs = wq_sysfs_attrs,
3255};
3256
3257static int __init wq_sysfs_init(void)
3258{
3259 return subsys_virtual_register(&wq_subsys, NULL);
3260}
3261core_initcall(wq_sysfs_init);
3262
3263static void wq_device_release(struct device *dev)
3264{
3265 struct wq_device *wq_dev = container_of(dev, struct wq_device, dev);
3266
3267 kfree(wq_dev);
3268}
3269
3270/**
3271 * workqueue_sysfs_register - make a workqueue visible in sysfs
3272 * @wq: the workqueue to register
3273 *
3274 * Expose @wq in sysfs under /sys/bus/workqueue/devices.
3275 * alloc_workqueue*() automatically calls this function if WQ_SYSFS is set
3276 * which is the preferred method.
3277 *
3278 * Workqueue user should use this function directly iff it wants to apply
3279 * workqueue_attrs before making the workqueue visible in sysfs; otherwise,
3280 * apply_workqueue_attrs() may race against userland updating the
3281 * attributes.
3282 *
3283 * Returns 0 on success, -errno on failure.
3284 */
3285int workqueue_sysfs_register(struct workqueue_struct *wq)
3286{
3287 struct wq_device *wq_dev;
3288 int ret;
3289
3095 /* 3290 /*
3096 * pwqs are forced aligned according to WORK_STRUCT_FLAG_BITS. 3291 * Adjusting max_active or creating new pwqs by applyting
3097 * Make sure that the alignment isn't lower than that of 3292 * attributes breaks ordering guarantee. Disallow exposing ordered
3098 * unsigned long long. 3293 * workqueues.
3099 */ 3294 */
3100 const size_t size = sizeof(struct pool_workqueue); 3295 if (WARN_ON(wq->flags & __WQ_ORDERED))
3101 const size_t align = max_t(size_t, 1 << WORK_STRUCT_FLAG_BITS, 3296 return -EINVAL;
3102 __alignof__(unsigned long long));
3103 3297
3104 if (!(wq->flags & WQ_UNBOUND)) 3298 wq->wq_dev = wq_dev = kzalloc(sizeof(*wq_dev), GFP_KERNEL);
3105 wq->pool_wq.pcpu = __alloc_percpu(size, align); 3299 if (!wq_dev)
3106 else { 3300 return -ENOMEM;
3107 void *ptr; 3301
3302 wq_dev->wq = wq;
3303 wq_dev->dev.bus = &wq_subsys;
3304 wq_dev->dev.init_name = wq->name;
3305 wq_dev->dev.release = wq_device_release;
3306
3307 /*
3308 * unbound_attrs are created separately. Suppress uevent until
3309 * everything is ready.
3310 */
3311 dev_set_uevent_suppress(&wq_dev->dev, true);
3312
3313 ret = device_register(&wq_dev->dev);
3314 if (ret) {
3315 kfree(wq_dev);
3316 wq->wq_dev = NULL;
3317 return ret;
3318 }
3319
3320 if (wq->flags & WQ_UNBOUND) {
3321 struct device_attribute *attr;
3322
3323 for (attr = wq_sysfs_unbound_attrs; attr->attr.name; attr++) {
3324 ret = device_create_file(&wq_dev->dev, attr);
3325 if (ret) {
3326 device_unregister(&wq_dev->dev);
3327 wq->wq_dev = NULL;
3328 return ret;
3329 }
3330 }
3331 }
3332
3333 kobject_uevent(&wq_dev->dev.kobj, KOBJ_ADD);
3334 return 0;
3335}
3336
3337/**
3338 * workqueue_sysfs_unregister - undo workqueue_sysfs_register()
3339 * @wq: the workqueue to unregister
3340 *
3341 * If @wq is registered to sysfs by workqueue_sysfs_register(), unregister.
3342 */
3343static void workqueue_sysfs_unregister(struct workqueue_struct *wq)
3344{
3345 struct wq_device *wq_dev = wq->wq_dev;
3346
3347 if (!wq->wq_dev)
3348 return;
3349
3350 wq->wq_dev = NULL;
3351 device_unregister(&wq_dev->dev);
3352}
3353#else /* CONFIG_SYSFS */
3354static void workqueue_sysfs_unregister(struct workqueue_struct *wq) { }
3355#endif /* CONFIG_SYSFS */
3356
3357/**
3358 * free_workqueue_attrs - free a workqueue_attrs
3359 * @attrs: workqueue_attrs to free
3360 *
3361 * Undo alloc_workqueue_attrs().
3362 */
3363void free_workqueue_attrs(struct workqueue_attrs *attrs)
3364{
3365 if (attrs) {
3366 free_cpumask_var(attrs->cpumask);
3367 kfree(attrs);
3368 }
3369}
3370
3371/**
3372 * alloc_workqueue_attrs - allocate a workqueue_attrs
3373 * @gfp_mask: allocation mask to use
3374 *
3375 * Allocate a new workqueue_attrs, initialize with default settings and
3376 * return it. Returns NULL on failure.
3377 */
3378struct workqueue_attrs *alloc_workqueue_attrs(gfp_t gfp_mask)
3379{
3380 struct workqueue_attrs *attrs;
3381
3382 attrs = kzalloc(sizeof(*attrs), gfp_mask);
3383 if (!attrs)
3384 goto fail;
3385 if (!alloc_cpumask_var(&attrs->cpumask, gfp_mask))
3386 goto fail;
3387
3388 cpumask_copy(attrs->cpumask, cpu_possible_mask);
3389 return attrs;
3390fail:
3391 free_workqueue_attrs(attrs);
3392 return NULL;
3393}
3394
3395static void copy_workqueue_attrs(struct workqueue_attrs *to,
3396 const struct workqueue_attrs *from)
3397{
3398 to->nice = from->nice;
3399 cpumask_copy(to->cpumask, from->cpumask);
3400}
3401
3402/* hash value of the content of @attr */
3403static u32 wqattrs_hash(const struct workqueue_attrs *attrs)
3404{
3405 u32 hash = 0;
3406
3407 hash = jhash_1word(attrs->nice, hash);
3408 hash = jhash(cpumask_bits(attrs->cpumask),
3409 BITS_TO_LONGS(nr_cpumask_bits) * sizeof(long), hash);
3410 return hash;
3411}
3412
3413/* content equality test */
3414static bool wqattrs_equal(const struct workqueue_attrs *a,
3415 const struct workqueue_attrs *b)
3416{
3417 if (a->nice != b->nice)
3418 return false;
3419 if (!cpumask_equal(a->cpumask, b->cpumask))
3420 return false;
3421 return true;
3422}
3423
3424/**
3425 * init_worker_pool - initialize a newly zalloc'd worker_pool
3426 * @pool: worker_pool to initialize
3427 *
3428 * Initiailize a newly zalloc'd @pool. It also allocates @pool->attrs.
3429 * Returns 0 on success, -errno on failure. Even on failure, all fields
3430 * inside @pool proper are initialized and put_unbound_pool() can be called
3431 * on @pool safely to release it.
3432 */
3433static int init_worker_pool(struct worker_pool *pool)
3434{
3435 spin_lock_init(&pool->lock);
3436 pool->id = -1;
3437 pool->cpu = -1;
3438 pool->node = NUMA_NO_NODE;
3439 pool->flags |= POOL_DISASSOCIATED;
3440 INIT_LIST_HEAD(&pool->worklist);
3441 INIT_LIST_HEAD(&pool->idle_list);
3442 hash_init(pool->busy_hash);
3443
3444 init_timer_deferrable(&pool->idle_timer);
3445 pool->idle_timer.function = idle_worker_timeout;
3446 pool->idle_timer.data = (unsigned long)pool;
3447
3448 setup_timer(&pool->mayday_timer, pool_mayday_timeout,
3449 (unsigned long)pool);
3450
3451 mutex_init(&pool->manager_arb);
3452 mutex_init(&pool->manager_mutex);
3453 idr_init(&pool->worker_idr);
3454
3455 INIT_HLIST_NODE(&pool->hash_node);
3456 pool->refcnt = 1;
3457
3458 /* shouldn't fail above this point */
3459 pool->attrs = alloc_workqueue_attrs(GFP_KERNEL);
3460 if (!pool->attrs)
3461 return -ENOMEM;
3462 return 0;
3463}
3464
3465static void rcu_free_pool(struct rcu_head *rcu)
3466{
3467 struct worker_pool *pool = container_of(rcu, struct worker_pool, rcu);
3468
3469 idr_destroy(&pool->worker_idr);
3470 free_workqueue_attrs(pool->attrs);
3471 kfree(pool);
3472}
3473
3474/**
3475 * put_unbound_pool - put a worker_pool
3476 * @pool: worker_pool to put
3477 *
3478 * Put @pool. If its refcnt reaches zero, it gets destroyed in sched-RCU
3479 * safe manner. get_unbound_pool() calls this function on its failure path
3480 * and this function should be able to release pools which went through,
3481 * successfully or not, init_worker_pool().
3482 *
3483 * Should be called with wq_pool_mutex held.
3484 */
3485static void put_unbound_pool(struct worker_pool *pool)
3486{
3487 struct worker *worker;
3488
3489 lockdep_assert_held(&wq_pool_mutex);
3490
3491 if (--pool->refcnt)
3492 return;
3493
3494 /* sanity checks */
3495 if (WARN_ON(!(pool->flags & POOL_DISASSOCIATED)) ||
3496 WARN_ON(!list_empty(&pool->worklist)))
3497 return;
3498
3499 /* release id and unhash */
3500 if (pool->id >= 0)
3501 idr_remove(&worker_pool_idr, pool->id);
3502 hash_del(&pool->hash_node);
3503
3504 /*
3505 * Become the manager and destroy all workers. Grabbing
3506 * manager_arb prevents @pool's workers from blocking on
3507 * manager_mutex.
3508 */
3509 mutex_lock(&pool->manager_arb);
3510 mutex_lock(&pool->manager_mutex);
3511 spin_lock_irq(&pool->lock);
3512
3513 while ((worker = first_worker(pool)))
3514 destroy_worker(worker);
3515 WARN_ON(pool->nr_workers || pool->nr_idle);
3516
3517 spin_unlock_irq(&pool->lock);
3518 mutex_unlock(&pool->manager_mutex);
3519 mutex_unlock(&pool->manager_arb);
3520
3521 /* shut down the timers */
3522 del_timer_sync(&pool->idle_timer);
3523 del_timer_sync(&pool->mayday_timer);
3524
3525 /* sched-RCU protected to allow dereferences from get_work_pool() */
3526 call_rcu_sched(&pool->rcu, rcu_free_pool);
3527}
3528
3529/**
3530 * get_unbound_pool - get a worker_pool with the specified attributes
3531 * @attrs: the attributes of the worker_pool to get
3532 *
3533 * Obtain a worker_pool which has the same attributes as @attrs, bump the
3534 * reference count and return it. If there already is a matching
3535 * worker_pool, it will be used; otherwise, this function attempts to
3536 * create a new one. On failure, returns NULL.
3537 *
3538 * Should be called with wq_pool_mutex held.
3539 */
3540static struct worker_pool *get_unbound_pool(const struct workqueue_attrs *attrs)
3541{
3542 u32 hash = wqattrs_hash(attrs);
3543 struct worker_pool *pool;
3544 int node;
3545
3546 lockdep_assert_held(&wq_pool_mutex);
3547
3548 /* do we already have a matching pool? */
3549 hash_for_each_possible(unbound_pool_hash, pool, hash_node, hash) {
3550 if (wqattrs_equal(pool->attrs, attrs)) {
3551 pool->refcnt++;
3552 goto out_unlock;
3553 }
3554 }
3555
3556 /* nope, create a new one */
3557 pool = kzalloc(sizeof(*pool), GFP_KERNEL);
3558 if (!pool || init_worker_pool(pool) < 0)
3559 goto fail;
3560
3561 if (workqueue_freezing)
3562 pool->flags |= POOL_FREEZING;
3563
3564 lockdep_set_subclass(&pool->lock, 1); /* see put_pwq() */
3565 copy_workqueue_attrs(pool->attrs, attrs);
3566
3567 /* if cpumask is contained inside a NUMA node, we belong to that node */
3568 if (wq_numa_enabled) {
3569 for_each_node(node) {
3570 if (cpumask_subset(pool->attrs->cpumask,
3571 wq_numa_possible_cpumask[node])) {
3572 pool->node = node;
3573 break;
3574 }
3575 }
3576 }
3577
3578 if (worker_pool_assign_id(pool) < 0)
3579 goto fail;
3580
3581 /* create and start the initial worker */
3582 if (create_and_start_worker(pool) < 0)
3583 goto fail;
3584
3585 /* install */
3586 hash_add(unbound_pool_hash, &pool->hash_node, hash);
3587out_unlock:
3588 return pool;
3589fail:
3590 if (pool)
3591 put_unbound_pool(pool);
3592 return NULL;
3593}
3594
3595static void rcu_free_pwq(struct rcu_head *rcu)
3596{
3597 kmem_cache_free(pwq_cache,
3598 container_of(rcu, struct pool_workqueue, rcu));
3599}
3600
3601/*
3602 * Scheduled on system_wq by put_pwq() when an unbound pwq hits zero refcnt
3603 * and needs to be destroyed.
3604 */
3605static void pwq_unbound_release_workfn(struct work_struct *work)
3606{
3607 struct pool_workqueue *pwq = container_of(work, struct pool_workqueue,
3608 unbound_release_work);
3609 struct workqueue_struct *wq = pwq->wq;
3610 struct worker_pool *pool = pwq->pool;
3611 bool is_last;
3612
3613 if (WARN_ON_ONCE(!(wq->flags & WQ_UNBOUND)))
3614 return;
3615
3616 /*
3617 * Unlink @pwq. Synchronization against wq->mutex isn't strictly
3618 * necessary on release but do it anyway. It's easier to verify
3619 * and consistent with the linking path.
3620 */
3621 mutex_lock(&wq->mutex);
3622 list_del_rcu(&pwq->pwqs_node);
3623 is_last = list_empty(&wq->pwqs);
3624 mutex_unlock(&wq->mutex);
3625
3626 mutex_lock(&wq_pool_mutex);
3627 put_unbound_pool(pool);
3628 mutex_unlock(&wq_pool_mutex);
3629
3630 call_rcu_sched(&pwq->rcu, rcu_free_pwq);
3631
3632 /*
3633 * If we're the last pwq going away, @wq is already dead and no one
3634 * is gonna access it anymore. Free it.
3635 */
3636 if (is_last) {
3637 free_workqueue_attrs(wq->unbound_attrs);
3638 kfree(wq);
3639 }
3640}
3641
3642/**
3643 * pwq_adjust_max_active - update a pwq's max_active to the current setting
3644 * @pwq: target pool_workqueue
3645 *
3646 * If @pwq isn't freezing, set @pwq->max_active to the associated
3647 * workqueue's saved_max_active and activate delayed work items
3648 * accordingly. If @pwq is freezing, clear @pwq->max_active to zero.
3649 */
3650static void pwq_adjust_max_active(struct pool_workqueue *pwq)
3651{
3652 struct workqueue_struct *wq = pwq->wq;
3653 bool freezable = wq->flags & WQ_FREEZABLE;
3654
3655 /* for @wq->saved_max_active */
3656 lockdep_assert_held(&wq->mutex);
3657
3658 /* fast exit for non-freezable wqs */
3659 if (!freezable && pwq->max_active == wq->saved_max_active)
3660 return;
3661
3662 spin_lock_irq(&pwq->pool->lock);
3663
3664 if (!freezable || !(pwq->pool->flags & POOL_FREEZING)) {
3665 pwq->max_active = wq->saved_max_active;
3666
3667 while (!list_empty(&pwq->delayed_works) &&
3668 pwq->nr_active < pwq->max_active)
3669 pwq_activate_first_delayed(pwq);
3108 3670
3109 /* 3671 /*
3110 * Allocate enough room to align pwq and put an extra 3672 * Need to kick a worker after thawed or an unbound wq's
3111 * pointer at the end pointing back to the originally 3673 * max_active is bumped. It's a slow path. Do it always.
3112 * allocated pointer which will be used for free.
3113 */ 3674 */
3114 ptr = kzalloc(size + align + sizeof(void *), GFP_KERNEL); 3675 wake_up_worker(pwq->pool);
3115 if (ptr) { 3676 } else {
3116 wq->pool_wq.single = PTR_ALIGN(ptr, align); 3677 pwq->max_active = 0;
3117 *(void **)(wq->pool_wq.single + 1) = ptr; 3678 }
3679
3680 spin_unlock_irq(&pwq->pool->lock);
3681}
3682
3683/* initialize newly alloced @pwq which is associated with @wq and @pool */
3684static void init_pwq(struct pool_workqueue *pwq, struct workqueue_struct *wq,
3685 struct worker_pool *pool)
3686{
3687 BUG_ON((unsigned long)pwq & WORK_STRUCT_FLAG_MASK);
3688
3689 memset(pwq, 0, sizeof(*pwq));
3690
3691 pwq->pool = pool;
3692 pwq->wq = wq;
3693 pwq->flush_color = -1;
3694 pwq->refcnt = 1;
3695 INIT_LIST_HEAD(&pwq->delayed_works);
3696 INIT_LIST_HEAD(&pwq->pwqs_node);
3697 INIT_LIST_HEAD(&pwq->mayday_node);
3698 INIT_WORK(&pwq->unbound_release_work, pwq_unbound_release_workfn);
3699}
3700
3701/* sync @pwq with the current state of its associated wq and link it */
3702static void link_pwq(struct pool_workqueue *pwq)
3703{
3704 struct workqueue_struct *wq = pwq->wq;
3705
3706 lockdep_assert_held(&wq->mutex);
3707
3708 /* may be called multiple times, ignore if already linked */
3709 if (!list_empty(&pwq->pwqs_node))
3710 return;
3711
3712 /*
3713 * Set the matching work_color. This is synchronized with
3714 * wq->mutex to avoid confusing flush_workqueue().
3715 */
3716 pwq->work_color = wq->work_color;
3717
3718 /* sync max_active to the current setting */
3719 pwq_adjust_max_active(pwq);
3720
3721 /* link in @pwq */
3722 list_add_rcu(&pwq->pwqs_node, &wq->pwqs);
3723}
3724
3725/* obtain a pool matching @attr and create a pwq associating the pool and @wq */
3726static struct pool_workqueue *alloc_unbound_pwq(struct workqueue_struct *wq,
3727 const struct workqueue_attrs *attrs)
3728{
3729 struct worker_pool *pool;
3730 struct pool_workqueue *pwq;
3731
3732 lockdep_assert_held(&wq_pool_mutex);
3733
3734 pool = get_unbound_pool(attrs);
3735 if (!pool)
3736 return NULL;
3737
3738 pwq = kmem_cache_alloc_node(pwq_cache, GFP_KERNEL, pool->node);
3739 if (!pwq) {
3740 put_unbound_pool(pool);
3741 return NULL;
3742 }
3743
3744 init_pwq(pwq, wq, pool);
3745 return pwq;
3746}
3747
3748/* undo alloc_unbound_pwq(), used only in the error path */
3749static void free_unbound_pwq(struct pool_workqueue *pwq)
3750{
3751 lockdep_assert_held(&wq_pool_mutex);
3752
3753 if (pwq) {
3754 put_unbound_pool(pwq->pool);
3755 kmem_cache_free(pwq_cache, pwq);
3756 }
3757}
3758
3759/**
3760 * wq_calc_node_mask - calculate a wq_attrs' cpumask for the specified node
3761 * @attrs: the wq_attrs of interest
3762 * @node: the target NUMA node
3763 * @cpu_going_down: if >= 0, the CPU to consider as offline
3764 * @cpumask: outarg, the resulting cpumask
3765 *
3766 * Calculate the cpumask a workqueue with @attrs should use on @node. If
3767 * @cpu_going_down is >= 0, that cpu is considered offline during
3768 * calculation. The result is stored in @cpumask. This function returns
3769 * %true if the resulting @cpumask is different from @attrs->cpumask,
3770 * %false if equal.
3771 *
3772 * If NUMA affinity is not enabled, @attrs->cpumask is always used. If
3773 * enabled and @node has online CPUs requested by @attrs, the returned
3774 * cpumask is the intersection of the possible CPUs of @node and
3775 * @attrs->cpumask.
3776 *
3777 * The caller is responsible for ensuring that the cpumask of @node stays
3778 * stable.
3779 */
3780static bool wq_calc_node_cpumask(const struct workqueue_attrs *attrs, int node,
3781 int cpu_going_down, cpumask_t *cpumask)
3782{
3783 if (!wq_numa_enabled || attrs->no_numa)
3784 goto use_dfl;
3785
3786 /* does @node have any online CPUs @attrs wants? */
3787 cpumask_and(cpumask, cpumask_of_node(node), attrs->cpumask);
3788 if (cpu_going_down >= 0)
3789 cpumask_clear_cpu(cpu_going_down, cpumask);
3790
3791 if (cpumask_empty(cpumask))
3792 goto use_dfl;
3793
3794 /* yeap, return possible CPUs in @node that @attrs wants */
3795 cpumask_and(cpumask, attrs->cpumask, wq_numa_possible_cpumask[node]);
3796 return !cpumask_equal(cpumask, attrs->cpumask);
3797
3798use_dfl:
3799 cpumask_copy(cpumask, attrs->cpumask);
3800 return false;
3801}
3802
3803/* install @pwq into @wq's numa_pwq_tbl[] for @node and return the old pwq */
3804static struct pool_workqueue *numa_pwq_tbl_install(struct workqueue_struct *wq,
3805 int node,
3806 struct pool_workqueue *pwq)
3807{
3808 struct pool_workqueue *old_pwq;
3809
3810 lockdep_assert_held(&wq->mutex);
3811
3812 /* link_pwq() can handle duplicate calls */
3813 link_pwq(pwq);
3814
3815 old_pwq = rcu_access_pointer(wq->numa_pwq_tbl[node]);
3816 rcu_assign_pointer(wq->numa_pwq_tbl[node], pwq);
3817 return old_pwq;
3818}
3819
3820/**
3821 * apply_workqueue_attrs - apply new workqueue_attrs to an unbound workqueue
3822 * @wq: the target workqueue
3823 * @attrs: the workqueue_attrs to apply, allocated with alloc_workqueue_attrs()
3824 *
3825 * Apply @attrs to an unbound workqueue @wq. Unless disabled, on NUMA
3826 * machines, this function maps a separate pwq to each NUMA node with
3827 * possibles CPUs in @attrs->cpumask so that work items are affine to the
3828 * NUMA node it was issued on. Older pwqs are released as in-flight work
3829 * items finish. Note that a work item which repeatedly requeues itself
3830 * back-to-back will stay on its current pwq.
3831 *
3832 * Performs GFP_KERNEL allocations. Returns 0 on success and -errno on
3833 * failure.
3834 */
3835int apply_workqueue_attrs(struct workqueue_struct *wq,
3836 const struct workqueue_attrs *attrs)
3837{
3838 struct workqueue_attrs *new_attrs, *tmp_attrs;
3839 struct pool_workqueue **pwq_tbl, *dfl_pwq;
3840 int node, ret;
3841
3842 /* only unbound workqueues can change attributes */
3843 if (WARN_ON(!(wq->flags & WQ_UNBOUND)))
3844 return -EINVAL;
3845
3846 /* creating multiple pwqs breaks ordering guarantee */
3847 if (WARN_ON((wq->flags & __WQ_ORDERED) && !list_empty(&wq->pwqs)))
3848 return -EINVAL;
3849
3850 pwq_tbl = kzalloc(wq_numa_tbl_len * sizeof(pwq_tbl[0]), GFP_KERNEL);
3851 new_attrs = alloc_workqueue_attrs(GFP_KERNEL);
3852 tmp_attrs = alloc_workqueue_attrs(GFP_KERNEL);
3853 if (!pwq_tbl || !new_attrs || !tmp_attrs)
3854 goto enomem;
3855
3856 /* make a copy of @attrs and sanitize it */
3857 copy_workqueue_attrs(new_attrs, attrs);
3858 cpumask_and(new_attrs->cpumask, new_attrs->cpumask, cpu_possible_mask);
3859
3860 /*
3861 * We may create multiple pwqs with differing cpumasks. Make a
3862 * copy of @new_attrs which will be modified and used to obtain
3863 * pools.
3864 */
3865 copy_workqueue_attrs(tmp_attrs, new_attrs);
3866
3867 /*
3868 * CPUs should stay stable across pwq creations and installations.
3869 * Pin CPUs, determine the target cpumask for each node and create
3870 * pwqs accordingly.
3871 */
3872 get_online_cpus();
3873
3874 mutex_lock(&wq_pool_mutex);
3875
3876 /*
3877 * If something goes wrong during CPU up/down, we'll fall back to
3878 * the default pwq covering whole @attrs->cpumask. Always create
3879 * it even if we don't use it immediately.
3880 */
3881 dfl_pwq = alloc_unbound_pwq(wq, new_attrs);
3882 if (!dfl_pwq)
3883 goto enomem_pwq;
3884
3885 for_each_node(node) {
3886 if (wq_calc_node_cpumask(attrs, node, -1, tmp_attrs->cpumask)) {
3887 pwq_tbl[node] = alloc_unbound_pwq(wq, tmp_attrs);
3888 if (!pwq_tbl[node])
3889 goto enomem_pwq;
3890 } else {
3891 dfl_pwq->refcnt++;
3892 pwq_tbl[node] = dfl_pwq;
3118 } 3893 }
3119 } 3894 }
3120 3895
3121 /* just in case, make sure it's actually aligned */ 3896 mutex_unlock(&wq_pool_mutex);
3122 BUG_ON(!IS_ALIGNED(wq->pool_wq.v, align)); 3897
3123 return wq->pool_wq.v ? 0 : -ENOMEM; 3898 /* all pwqs have been created successfully, let's install'em */
3899 mutex_lock(&wq->mutex);
3900
3901 copy_workqueue_attrs(wq->unbound_attrs, new_attrs);
3902
3903 /* save the previous pwq and install the new one */
3904 for_each_node(node)
3905 pwq_tbl[node] = numa_pwq_tbl_install(wq, node, pwq_tbl[node]);
3906
3907 /* @dfl_pwq might not have been used, ensure it's linked */
3908 link_pwq(dfl_pwq);
3909 swap(wq->dfl_pwq, dfl_pwq);
3910
3911 mutex_unlock(&wq->mutex);
3912
3913 /* put the old pwqs */
3914 for_each_node(node)
3915 put_pwq_unlocked(pwq_tbl[node]);
3916 put_pwq_unlocked(dfl_pwq);
3917
3918 put_online_cpus();
3919 ret = 0;
3920 /* fall through */
3921out_free:
3922 free_workqueue_attrs(tmp_attrs);
3923 free_workqueue_attrs(new_attrs);
3924 kfree(pwq_tbl);
3925 return ret;
3926
3927enomem_pwq:
3928 free_unbound_pwq(dfl_pwq);
3929 for_each_node(node)
3930 if (pwq_tbl && pwq_tbl[node] != dfl_pwq)
3931 free_unbound_pwq(pwq_tbl[node]);
3932 mutex_unlock(&wq_pool_mutex);
3933 put_online_cpus();
3934enomem:
3935 ret = -ENOMEM;
3936 goto out_free;
3124} 3937}
3125 3938
3126static void free_pwqs(struct workqueue_struct *wq) 3939/**
3940 * wq_update_unbound_numa - update NUMA affinity of a wq for CPU hot[un]plug
3941 * @wq: the target workqueue
3942 * @cpu: the CPU coming up or going down
3943 * @online: whether @cpu is coming up or going down
3944 *
3945 * This function is to be called from %CPU_DOWN_PREPARE, %CPU_ONLINE and
3946 * %CPU_DOWN_FAILED. @cpu is being hot[un]plugged, update NUMA affinity of
3947 * @wq accordingly.
3948 *
3949 * If NUMA affinity can't be adjusted due to memory allocation failure, it
3950 * falls back to @wq->dfl_pwq which may not be optimal but is always
3951 * correct.
3952 *
3953 * Note that when the last allowed CPU of a NUMA node goes offline for a
3954 * workqueue with a cpumask spanning multiple nodes, the workers which were
3955 * already executing the work items for the workqueue will lose their CPU
3956 * affinity and may execute on any CPU. This is similar to how per-cpu
3957 * workqueues behave on CPU_DOWN. If a workqueue user wants strict
3958 * affinity, it's the user's responsibility to flush the work item from
3959 * CPU_DOWN_PREPARE.
3960 */
3961static void wq_update_unbound_numa(struct workqueue_struct *wq, int cpu,
3962 bool online)
3127{ 3963{
3128 if (!(wq->flags & WQ_UNBOUND)) 3964 int node = cpu_to_node(cpu);
3129 free_percpu(wq->pool_wq.pcpu); 3965 int cpu_off = online ? -1 : cpu;
3130 else if (wq->pool_wq.single) { 3966 struct pool_workqueue *old_pwq = NULL, *pwq;
3131 /* the pointer to free is stored right after the pwq */ 3967 struct workqueue_attrs *target_attrs;
3132 kfree(*(void **)(wq->pool_wq.single + 1)); 3968 cpumask_t *cpumask;
3969
3970 lockdep_assert_held(&wq_pool_mutex);
3971
3972 if (!wq_numa_enabled || !(wq->flags & WQ_UNBOUND))
3973 return;
3974
3975 /*
3976 * We don't wanna alloc/free wq_attrs for each wq for each CPU.
3977 * Let's use a preallocated one. The following buf is protected by
3978 * CPU hotplug exclusion.
3979 */
3980 target_attrs = wq_update_unbound_numa_attrs_buf;
3981 cpumask = target_attrs->cpumask;
3982
3983 mutex_lock(&wq->mutex);
3984 if (wq->unbound_attrs->no_numa)
3985 goto out_unlock;
3986
3987 copy_workqueue_attrs(target_attrs, wq->unbound_attrs);
3988 pwq = unbound_pwq_by_node(wq, node);
3989
3990 /*
3991 * Let's determine what needs to be done. If the target cpumask is
3992 * different from wq's, we need to compare it to @pwq's and create
3993 * a new one if they don't match. If the target cpumask equals
3994 * wq's, the default pwq should be used. If @pwq is already the
3995 * default one, nothing to do; otherwise, install the default one.
3996 */
3997 if (wq_calc_node_cpumask(wq->unbound_attrs, node, cpu_off, cpumask)) {
3998 if (cpumask_equal(cpumask, pwq->pool->attrs->cpumask))
3999 goto out_unlock;
4000 } else {
4001 if (pwq == wq->dfl_pwq)
4002 goto out_unlock;
4003 else
4004 goto use_dfl_pwq;
4005 }
4006
4007 mutex_unlock(&wq->mutex);
4008
4009 /* create a new pwq */
4010 pwq = alloc_unbound_pwq(wq, target_attrs);
4011 if (!pwq) {
4012 pr_warning("workqueue: allocation failed while updating NUMA affinity of \"%s\"\n",
4013 wq->name);
4014 goto out_unlock;
4015 }
4016
4017 /*
4018 * Install the new pwq. As this function is called only from CPU
4019 * hotplug callbacks and applying a new attrs is wrapped with
4020 * get/put_online_cpus(), @wq->unbound_attrs couldn't have changed
4021 * inbetween.
4022 */
4023 mutex_lock(&wq->mutex);
4024 old_pwq = numa_pwq_tbl_install(wq, node, pwq);
4025 goto out_unlock;
4026
4027use_dfl_pwq:
4028 spin_lock_irq(&wq->dfl_pwq->pool->lock);
4029 get_pwq(wq->dfl_pwq);
4030 spin_unlock_irq(&wq->dfl_pwq->pool->lock);
4031 old_pwq = numa_pwq_tbl_install(wq, node, wq->dfl_pwq);
4032out_unlock:
4033 mutex_unlock(&wq->mutex);
4034 put_pwq_unlocked(old_pwq);
4035}
4036
4037static int alloc_and_link_pwqs(struct workqueue_struct *wq)
4038{
4039 bool highpri = wq->flags & WQ_HIGHPRI;
4040 int cpu;
4041
4042 if (!(wq->flags & WQ_UNBOUND)) {
4043 wq->cpu_pwqs = alloc_percpu(struct pool_workqueue);
4044 if (!wq->cpu_pwqs)
4045 return -ENOMEM;
4046
4047 for_each_possible_cpu(cpu) {
4048 struct pool_workqueue *pwq =
4049 per_cpu_ptr(wq->cpu_pwqs, cpu);
4050 struct worker_pool *cpu_pools =
4051 per_cpu(cpu_worker_pools, cpu);
4052
4053 init_pwq(pwq, wq, &cpu_pools[highpri]);
4054
4055 mutex_lock(&wq->mutex);
4056 link_pwq(pwq);
4057 mutex_unlock(&wq->mutex);
4058 }
4059 return 0;
4060 } else {
4061 return apply_workqueue_attrs(wq, unbound_std_wq_attrs[highpri]);
3133 } 4062 }
3134} 4063}
3135 4064
@@ -3151,30 +4080,28 @@ struct workqueue_struct *__alloc_workqueue_key(const char *fmt,
3151 struct lock_class_key *key, 4080 struct lock_class_key *key,
3152 const char *lock_name, ...) 4081 const char *lock_name, ...)
3153{ 4082{
3154 va_list args, args1; 4083 size_t tbl_size = 0;
4084 va_list args;
3155 struct workqueue_struct *wq; 4085 struct workqueue_struct *wq;
3156 unsigned int cpu; 4086 struct pool_workqueue *pwq;
3157 size_t namelen;
3158 4087
3159 /* determine namelen, allocate wq and format name */ 4088 /* allocate wq and format name */
3160 va_start(args, lock_name); 4089 if (flags & WQ_UNBOUND)
3161 va_copy(args1, args); 4090 tbl_size = wq_numa_tbl_len * sizeof(wq->numa_pwq_tbl[0]);
3162 namelen = vsnprintf(NULL, 0, fmt, args) + 1;
3163 4091
3164 wq = kzalloc(sizeof(*wq) + namelen, GFP_KERNEL); 4092 wq = kzalloc(sizeof(*wq) + tbl_size, GFP_KERNEL);
3165 if (!wq) 4093 if (!wq)
3166 goto err; 4094 return NULL;
3167 4095
3168 vsnprintf(wq->name, namelen, fmt, args1); 4096 if (flags & WQ_UNBOUND) {
3169 va_end(args); 4097 wq->unbound_attrs = alloc_workqueue_attrs(GFP_KERNEL);
3170 va_end(args1); 4098 if (!wq->unbound_attrs)
4099 goto err_free_wq;
4100 }
3171 4101
3172 /* 4102 va_start(args, lock_name);
3173 * Workqueues which may be used during memory reclaim should 4103 vsnprintf(wq->name, sizeof(wq->name), fmt, args);
3174 * have a rescuer to guarantee forward progress. 4104 va_end(args);
3175 */
3176 if (flags & WQ_MEM_RECLAIM)
3177 flags |= WQ_RESCUER;
3178 4105
3179 max_active = max_active ?: WQ_DFL_ACTIVE; 4106 max_active = max_active ?: WQ_DFL_ACTIVE;
3180 max_active = wq_clamp_max_active(max_active, flags, wq->name); 4107 max_active = wq_clamp_max_active(max_active, flags, wq->name);
@@ -3182,71 +4109,70 @@ struct workqueue_struct *__alloc_workqueue_key(const char *fmt,
3182 /* init wq */ 4109 /* init wq */
3183 wq->flags = flags; 4110 wq->flags = flags;
3184 wq->saved_max_active = max_active; 4111 wq->saved_max_active = max_active;
3185 mutex_init(&wq->flush_mutex); 4112 mutex_init(&wq->mutex);
3186 atomic_set(&wq->nr_pwqs_to_flush, 0); 4113 atomic_set(&wq->nr_pwqs_to_flush, 0);
4114 INIT_LIST_HEAD(&wq->pwqs);
3187 INIT_LIST_HEAD(&wq->flusher_queue); 4115 INIT_LIST_HEAD(&wq->flusher_queue);
3188 INIT_LIST_HEAD(&wq->flusher_overflow); 4116 INIT_LIST_HEAD(&wq->flusher_overflow);
4117 INIT_LIST_HEAD(&wq->maydays);
3189 4118
3190 lockdep_init_map(&wq->lockdep_map, lock_name, key, 0); 4119 lockdep_init_map(&wq->lockdep_map, lock_name, key, 0);
3191 INIT_LIST_HEAD(&wq->list); 4120 INIT_LIST_HEAD(&wq->list);
3192 4121
3193 if (alloc_pwqs(wq) < 0) 4122 if (alloc_and_link_pwqs(wq) < 0)
3194 goto err; 4123 goto err_free_wq;
3195
3196 for_each_pwq_cpu(cpu, wq) {
3197 struct pool_workqueue *pwq = get_pwq(cpu, wq);
3198 4124
3199 BUG_ON((unsigned long)pwq & WORK_STRUCT_FLAG_MASK); 4125 /*
3200 pwq->pool = get_std_worker_pool(cpu, flags & WQ_HIGHPRI); 4126 * Workqueues which may be used during memory reclaim should
3201 pwq->wq = wq; 4127 * have a rescuer to guarantee forward progress.
3202 pwq->flush_color = -1; 4128 */
3203 pwq->max_active = max_active; 4129 if (flags & WQ_MEM_RECLAIM) {
3204 INIT_LIST_HEAD(&pwq->delayed_works);
3205 }
3206
3207 if (flags & WQ_RESCUER) {
3208 struct worker *rescuer; 4130 struct worker *rescuer;
3209 4131
3210 if (!alloc_mayday_mask(&wq->mayday_mask, GFP_KERNEL)) 4132 rescuer = alloc_worker();
3211 goto err;
3212
3213 wq->rescuer = rescuer = alloc_worker();
3214 if (!rescuer) 4133 if (!rescuer)
3215 goto err; 4134 goto err_destroy;
3216 4135
3217 rescuer->rescue_wq = wq; 4136 rescuer->rescue_wq = wq;
3218 rescuer->task = kthread_create(rescuer_thread, rescuer, "%s", 4137 rescuer->task = kthread_create(rescuer_thread, rescuer, "%s",
3219 wq->name); 4138 wq->name);
3220 if (IS_ERR(rescuer->task)) 4139 if (IS_ERR(rescuer->task)) {
3221 goto err; 4140 kfree(rescuer);
4141 goto err_destroy;
4142 }
3222 4143
3223 rescuer->task->flags |= PF_THREAD_BOUND; 4144 wq->rescuer = rescuer;
4145 rescuer->task->flags |= PF_NO_SETAFFINITY;
3224 wake_up_process(rescuer->task); 4146 wake_up_process(rescuer->task);
3225 } 4147 }
3226 4148
4149 if ((wq->flags & WQ_SYSFS) && workqueue_sysfs_register(wq))
4150 goto err_destroy;
4151
3227 /* 4152 /*
3228 * workqueue_lock protects global freeze state and workqueues 4153 * wq_pool_mutex protects global freeze state and workqueues list.
3229 * list. Grab it, set max_active accordingly and add the new 4154 * Grab it, adjust max_active and add the new @wq to workqueues
3230 * workqueue to workqueues list. 4155 * list.
3231 */ 4156 */
3232 spin_lock(&workqueue_lock); 4157 mutex_lock(&wq_pool_mutex);
3233 4158
3234 if (workqueue_freezing && wq->flags & WQ_FREEZABLE) 4159 mutex_lock(&wq->mutex);
3235 for_each_pwq_cpu(cpu, wq) 4160 for_each_pwq(pwq, wq)
3236 get_pwq(cpu, wq)->max_active = 0; 4161 pwq_adjust_max_active(pwq);
4162 mutex_unlock(&wq->mutex);
3237 4163
3238 list_add(&wq->list, &workqueues); 4164 list_add(&wq->list, &workqueues);
3239 4165
3240 spin_unlock(&workqueue_lock); 4166 mutex_unlock(&wq_pool_mutex);
3241 4167
3242 return wq; 4168 return wq;
3243err: 4169
3244 if (wq) { 4170err_free_wq:
3245 free_pwqs(wq); 4171 free_workqueue_attrs(wq->unbound_attrs);
3246 free_mayday_mask(wq->mayday_mask); 4172 kfree(wq);
3247 kfree(wq->rescuer); 4173 return NULL;
3248 kfree(wq); 4174err_destroy:
3249 } 4175 destroy_workqueue(wq);
3250 return NULL; 4176 return NULL;
3251} 4177}
3252EXPORT_SYMBOL_GPL(__alloc_workqueue_key); 4178EXPORT_SYMBOL_GPL(__alloc_workqueue_key);
@@ -3259,60 +4185,78 @@ EXPORT_SYMBOL_GPL(__alloc_workqueue_key);
3259 */ 4185 */
3260void destroy_workqueue(struct workqueue_struct *wq) 4186void destroy_workqueue(struct workqueue_struct *wq)
3261{ 4187{
3262 unsigned int cpu; 4188 struct pool_workqueue *pwq;
4189 int node;
3263 4190
3264 /* drain it before proceeding with destruction */ 4191 /* drain it before proceeding with destruction */
3265 drain_workqueue(wq); 4192 drain_workqueue(wq);
3266 4193
4194 /* sanity checks */
4195 mutex_lock(&wq->mutex);
4196 for_each_pwq(pwq, wq) {
4197 int i;
4198
4199 for (i = 0; i < WORK_NR_COLORS; i++) {
4200 if (WARN_ON(pwq->nr_in_flight[i])) {
4201 mutex_unlock(&wq->mutex);
4202 return;
4203 }
4204 }
4205
4206 if (WARN_ON((pwq != wq->dfl_pwq) && (pwq->refcnt > 1)) ||
4207 WARN_ON(pwq->nr_active) ||
4208 WARN_ON(!list_empty(&pwq->delayed_works))) {
4209 mutex_unlock(&wq->mutex);
4210 return;
4211 }
4212 }
4213 mutex_unlock(&wq->mutex);
4214
3267 /* 4215 /*
3268 * wq list is used to freeze wq, remove from list after 4216 * wq list is used to freeze wq, remove from list after
3269 * flushing is complete in case freeze races us. 4217 * flushing is complete in case freeze races us.
3270 */ 4218 */
3271 spin_lock(&workqueue_lock); 4219 mutex_lock(&wq_pool_mutex);
3272 list_del(&wq->list); 4220 list_del_init(&wq->list);
3273 spin_unlock(&workqueue_lock); 4221 mutex_unlock(&wq_pool_mutex);
3274
3275 /* sanity check */
3276 for_each_pwq_cpu(cpu, wq) {
3277 struct pool_workqueue *pwq = get_pwq(cpu, wq);
3278 int i;
3279 4222
3280 for (i = 0; i < WORK_NR_COLORS; i++) 4223 workqueue_sysfs_unregister(wq);
3281 BUG_ON(pwq->nr_in_flight[i]);
3282 BUG_ON(pwq->nr_active);
3283 BUG_ON(!list_empty(&pwq->delayed_works));
3284 }
3285 4224
3286 if (wq->flags & WQ_RESCUER) { 4225 if (wq->rescuer) {
3287 kthread_stop(wq->rescuer->task); 4226 kthread_stop(wq->rescuer->task);
3288 free_mayday_mask(wq->mayday_mask);
3289 kfree(wq->rescuer); 4227 kfree(wq->rescuer);
4228 wq->rescuer = NULL;
3290 } 4229 }
3291 4230
3292 free_pwqs(wq); 4231 if (!(wq->flags & WQ_UNBOUND)) {
3293 kfree(wq); 4232 /*
3294} 4233 * The base ref is never dropped on per-cpu pwqs. Directly
3295EXPORT_SYMBOL_GPL(destroy_workqueue); 4234 * free the pwqs and wq.
3296 4235 */
3297/** 4236 free_percpu(wq->cpu_pwqs);
3298 * pwq_set_max_active - adjust max_active of a pwq 4237 kfree(wq);
3299 * @pwq: target pool_workqueue 4238 } else {
3300 * @max_active: new max_active value. 4239 /*
3301 * 4240 * We're the sole accessor of @wq at this point. Directly
3302 * Set @pwq->max_active to @max_active and activate delayed works if 4241 * access numa_pwq_tbl[] and dfl_pwq to put the base refs.
3303 * increased. 4242 * @wq will be freed when the last pwq is released.
3304 * 4243 */
3305 * CONTEXT: 4244 for_each_node(node) {
3306 * spin_lock_irq(pool->lock). 4245 pwq = rcu_access_pointer(wq->numa_pwq_tbl[node]);
3307 */ 4246 RCU_INIT_POINTER(wq->numa_pwq_tbl[node], NULL);
3308static void pwq_set_max_active(struct pool_workqueue *pwq, int max_active) 4247 put_pwq_unlocked(pwq);
3309{ 4248 }
3310 pwq->max_active = max_active;
3311 4249
3312 while (!list_empty(&pwq->delayed_works) && 4250 /*
3313 pwq->nr_active < pwq->max_active) 4251 * Put dfl_pwq. @wq may be freed any time after dfl_pwq is
3314 pwq_activate_first_delayed(pwq); 4252 * put. Don't access it afterwards.
4253 */
4254 pwq = wq->dfl_pwq;
4255 wq->dfl_pwq = NULL;
4256 put_pwq_unlocked(pwq);
4257 }
3315} 4258}
4259EXPORT_SYMBOL_GPL(destroy_workqueue);
3316 4260
3317/** 4261/**
3318 * workqueue_set_max_active - adjust max_active of a workqueue 4262 * workqueue_set_max_active - adjust max_active of a workqueue
@@ -3326,30 +4270,37 @@ static void pwq_set_max_active(struct pool_workqueue *pwq, int max_active)
3326 */ 4270 */
3327void workqueue_set_max_active(struct workqueue_struct *wq, int max_active) 4271void workqueue_set_max_active(struct workqueue_struct *wq, int max_active)
3328{ 4272{
3329 unsigned int cpu; 4273 struct pool_workqueue *pwq;
4274
4275 /* disallow meddling with max_active for ordered workqueues */
4276 if (WARN_ON(wq->flags & __WQ_ORDERED))
4277 return;
3330 4278
3331 max_active = wq_clamp_max_active(max_active, wq->flags, wq->name); 4279 max_active = wq_clamp_max_active(max_active, wq->flags, wq->name);
3332 4280
3333 spin_lock(&workqueue_lock); 4281 mutex_lock(&wq->mutex);
3334 4282
3335 wq->saved_max_active = max_active; 4283 wq->saved_max_active = max_active;
3336 4284
3337 for_each_pwq_cpu(cpu, wq) { 4285 for_each_pwq(pwq, wq)
3338 struct pool_workqueue *pwq = get_pwq(cpu, wq); 4286 pwq_adjust_max_active(pwq);
3339 struct worker_pool *pool = pwq->pool;
3340
3341 spin_lock_irq(&pool->lock);
3342 4287
3343 if (!(wq->flags & WQ_FREEZABLE) || 4288 mutex_unlock(&wq->mutex);
3344 !(pool->flags & POOL_FREEZING)) 4289}
3345 pwq_set_max_active(pwq, max_active); 4290EXPORT_SYMBOL_GPL(workqueue_set_max_active);
3346 4291
3347 spin_unlock_irq(&pool->lock); 4292/**
3348 } 4293 * current_is_workqueue_rescuer - is %current workqueue rescuer?
4294 *
4295 * Determine whether %current is a workqueue rescuer. Can be used from
4296 * work functions to determine whether it's being run off the rescuer task.
4297 */
4298bool current_is_workqueue_rescuer(void)
4299{
4300 struct worker *worker = current_wq_worker();
3349 4301
3350 spin_unlock(&workqueue_lock); 4302 return worker && worker->rescue_wq;
3351} 4303}
3352EXPORT_SYMBOL_GPL(workqueue_set_max_active);
3353 4304
3354/** 4305/**
3355 * workqueue_congested - test whether a workqueue is congested 4306 * workqueue_congested - test whether a workqueue is congested
@@ -3363,11 +4314,22 @@ EXPORT_SYMBOL_GPL(workqueue_set_max_active);
3363 * RETURNS: 4314 * RETURNS:
3364 * %true if congested, %false otherwise. 4315 * %true if congested, %false otherwise.
3365 */ 4316 */
3366bool workqueue_congested(unsigned int cpu, struct workqueue_struct *wq) 4317bool workqueue_congested(int cpu, struct workqueue_struct *wq)
3367{ 4318{
3368 struct pool_workqueue *pwq = get_pwq(cpu, wq); 4319 struct pool_workqueue *pwq;
4320 bool ret;
4321
4322 rcu_read_lock_sched();
4323
4324 if (!(wq->flags & WQ_UNBOUND))
4325 pwq = per_cpu_ptr(wq->cpu_pwqs, cpu);
4326 else
4327 pwq = unbound_pwq_by_node(wq, cpu_to_node(cpu));
4328
4329 ret = !list_empty(&pwq->delayed_works);
4330 rcu_read_unlock_sched();
3369 4331
3370 return !list_empty(&pwq->delayed_works); 4332 return ret;
3371} 4333}
3372EXPORT_SYMBOL_GPL(workqueue_congested); 4334EXPORT_SYMBOL_GPL(workqueue_congested);
3373 4335
@@ -3384,24 +4346,104 @@ EXPORT_SYMBOL_GPL(workqueue_congested);
3384 */ 4346 */
3385unsigned int work_busy(struct work_struct *work) 4347unsigned int work_busy(struct work_struct *work)
3386{ 4348{
3387 struct worker_pool *pool = get_work_pool(work); 4349 struct worker_pool *pool;
3388 unsigned long flags; 4350 unsigned long flags;
3389 unsigned int ret = 0; 4351 unsigned int ret = 0;
3390 4352
3391 if (work_pending(work)) 4353 if (work_pending(work))
3392 ret |= WORK_BUSY_PENDING; 4354 ret |= WORK_BUSY_PENDING;
3393 4355
4356 local_irq_save(flags);
4357 pool = get_work_pool(work);
3394 if (pool) { 4358 if (pool) {
3395 spin_lock_irqsave(&pool->lock, flags); 4359 spin_lock(&pool->lock);
3396 if (find_worker_executing_work(pool, work)) 4360 if (find_worker_executing_work(pool, work))
3397 ret |= WORK_BUSY_RUNNING; 4361 ret |= WORK_BUSY_RUNNING;
3398 spin_unlock_irqrestore(&pool->lock, flags); 4362 spin_unlock(&pool->lock);
3399 } 4363 }
4364 local_irq_restore(flags);
3400 4365
3401 return ret; 4366 return ret;
3402} 4367}
3403EXPORT_SYMBOL_GPL(work_busy); 4368EXPORT_SYMBOL_GPL(work_busy);
3404 4369
4370/**
4371 * set_worker_desc - set description for the current work item
4372 * @fmt: printf-style format string
4373 * @...: arguments for the format string
4374 *
4375 * This function can be called by a running work function to describe what
4376 * the work item is about. If the worker task gets dumped, this
4377 * information will be printed out together to help debugging. The
4378 * description can be at most WORKER_DESC_LEN including the trailing '\0'.
4379 */
4380void set_worker_desc(const char *fmt, ...)
4381{
4382 struct worker *worker = current_wq_worker();
4383 va_list args;
4384
4385 if (worker) {
4386 va_start(args, fmt);
4387 vsnprintf(worker->desc, sizeof(worker->desc), fmt, args);
4388 va_end(args);
4389 worker->desc_valid = true;
4390 }
4391}
4392
4393/**
4394 * print_worker_info - print out worker information and description
4395 * @log_lvl: the log level to use when printing
4396 * @task: target task
4397 *
4398 * If @task is a worker and currently executing a work item, print out the
4399 * name of the workqueue being serviced and worker description set with
4400 * set_worker_desc() by the currently executing work item.
4401 *
4402 * This function can be safely called on any task as long as the
4403 * task_struct itself is accessible. While safe, this function isn't
4404 * synchronized and may print out mixups or garbages of limited length.
4405 */
4406void print_worker_info(const char *log_lvl, struct task_struct *task)
4407{
4408 work_func_t *fn = NULL;
4409 char name[WQ_NAME_LEN] = { };
4410 char desc[WORKER_DESC_LEN] = { };
4411 struct pool_workqueue *pwq = NULL;
4412 struct workqueue_struct *wq = NULL;
4413 bool desc_valid = false;
4414 struct worker *worker;
4415
4416 if (!(task->flags & PF_WQ_WORKER))
4417 return;
4418
4419 /*
4420 * This function is called without any synchronization and @task
4421 * could be in any state. Be careful with dereferences.
4422 */
4423 worker = probe_kthread_data(task);
4424
4425 /*
4426 * Carefully copy the associated workqueue's workfn and name. Keep
4427 * the original last '\0' in case the original contains garbage.
4428 */
4429 probe_kernel_read(&fn, &worker->current_func, sizeof(fn));
4430 probe_kernel_read(&pwq, &worker->current_pwq, sizeof(pwq));
4431 probe_kernel_read(&wq, &pwq->wq, sizeof(wq));
4432 probe_kernel_read(name, wq->name, sizeof(name) - 1);
4433
4434 /* copy worker description */
4435 probe_kernel_read(&desc_valid, &worker->desc_valid, sizeof(desc_valid));
4436 if (desc_valid)
4437 probe_kernel_read(desc, worker->desc, sizeof(desc) - 1);
4438
4439 if (fn || name[0] || desc[0]) {
4440 printk("%sWorkqueue: %s %pf", log_lvl, name, fn);
4441 if (desc[0])
4442 pr_cont(" (%s)", desc);
4443 pr_cont("\n");
4444 }
4445}
4446
3405/* 4447/*
3406 * CPU hotplug. 4448 * CPU hotplug.
3407 * 4449 *
@@ -3422,53 +4464,153 @@ static void wq_unbind_fn(struct work_struct *work)
3422 int cpu = smp_processor_id(); 4464 int cpu = smp_processor_id();
3423 struct worker_pool *pool; 4465 struct worker_pool *pool;
3424 struct worker *worker; 4466 struct worker *worker;
3425 int i; 4467 int wi;
3426 4468
3427 for_each_std_worker_pool(pool, cpu) { 4469 for_each_cpu_worker_pool(pool, cpu) {
3428 BUG_ON(cpu != smp_processor_id()); 4470 WARN_ON_ONCE(cpu != smp_processor_id());
3429 4471
3430 mutex_lock(&pool->assoc_mutex); 4472 mutex_lock(&pool->manager_mutex);
3431 spin_lock_irq(&pool->lock); 4473 spin_lock_irq(&pool->lock);
3432 4474
3433 /* 4475 /*
3434 * We've claimed all manager positions. Make all workers 4476 * We've blocked all manager operations. Make all workers
3435 * unbound and set DISASSOCIATED. Before this, all workers 4477 * unbound and set DISASSOCIATED. Before this, all workers
3436 * except for the ones which are still executing works from 4478 * except for the ones which are still executing works from
3437 * before the last CPU down must be on the cpu. After 4479 * before the last CPU down must be on the cpu. After
3438 * this, they may become diasporas. 4480 * this, they may become diasporas.
3439 */ 4481 */
3440 list_for_each_entry(worker, &pool->idle_list, entry) 4482 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; 4483 worker->flags |= WORKER_UNBOUND;
3445 4484
3446 pool->flags |= POOL_DISASSOCIATED; 4485 pool->flags |= POOL_DISASSOCIATED;
3447 4486
3448 spin_unlock_irq(&pool->lock); 4487 spin_unlock_irq(&pool->lock);
3449 mutex_unlock(&pool->assoc_mutex); 4488 mutex_unlock(&pool->manager_mutex);
4489
4490 /*
4491 * Call schedule() so that we cross rq->lock and thus can
4492 * guarantee sched callbacks see the %WORKER_UNBOUND flag.
4493 * This is necessary as scheduler callbacks may be invoked
4494 * from other cpus.
4495 */
4496 schedule();
4497
4498 /*
4499 * Sched callbacks are disabled now. Zap nr_running.
4500 * After this, nr_running stays zero and need_more_worker()
4501 * and keep_working() are always true as long as the
4502 * worklist is not empty. This pool now behaves as an
4503 * unbound (in terms of concurrency management) pool which
4504 * are served by workers tied to the pool.
4505 */
4506 atomic_set(&pool->nr_running, 0);
4507
4508 /*
4509 * With concurrency management just turned off, a busy
4510 * worker blocking could lead to lengthy stalls. Kick off
4511 * unbound chain execution of currently pending work items.
4512 */
4513 spin_lock_irq(&pool->lock);
4514 wake_up_worker(pool);
4515 spin_unlock_irq(&pool->lock);
3450 } 4516 }
4517}
3451 4518
3452 /* 4519/**
3453 * Call schedule() so that we cross rq->lock and thus can guarantee 4520 * rebind_workers - rebind all workers of a pool to the associated CPU
3454 * sched callbacks see the %WORKER_UNBOUND flag. This is necessary 4521 * @pool: pool of interest
3455 * as scheduler callbacks may be invoked from other cpus. 4522 *
3456 */ 4523 * @pool->cpu is coming online. Rebind all workers to the CPU.
3457 schedule(); 4524 */
4525static void rebind_workers(struct worker_pool *pool)
4526{
4527 struct worker *worker;
4528 int wi;
4529
4530 lockdep_assert_held(&pool->manager_mutex);
3458 4531
3459 /* 4532 /*
3460 * Sched callbacks are disabled now. Zap nr_running. After this, 4533 * Restore CPU affinity of all workers. As all idle workers should
3461 * nr_running stays zero and need_more_worker() and keep_working() 4534 * 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 4535 * wake-ups for concurrency management happen, restore CPU affinty
3463 * @cpu now behave as unbound (in terms of concurrency management) 4536 * of all workers first and then clear UNBOUND. As we're called
3464 * pools which are served by workers tied to the CPU. 4537 * 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 */ 4538 */
3470 for_each_std_worker_pool(pool, cpu) 4539 for_each_pool_worker(worker, wi, pool)
3471 atomic_set(&pool->nr_running, 0); 4540 WARN_ON_ONCE(set_cpus_allowed_ptr(worker->task,
4541 pool->attrs->cpumask) < 0);
4542
4543 spin_lock_irq(&pool->lock);
4544
4545 for_each_pool_worker(worker, wi, pool) {
4546 unsigned int worker_flags = worker->flags;
4547
4548 /*
4549 * A bound idle worker should actually be on the runqueue
4550 * of the associated CPU for local wake-ups targeting it to
4551 * work. Kick all idle workers so that they migrate to the
4552 * associated CPU. Doing this in the same loop as
4553 * replacing UNBOUND with REBOUND is safe as no worker will
4554 * be bound before @pool->lock is released.
4555 */
4556 if (worker_flags & WORKER_IDLE)
4557 wake_up_process(worker->task);
4558
4559 /*
4560 * We want to clear UNBOUND but can't directly call
4561 * worker_clr_flags() or adjust nr_running. Atomically
4562 * replace UNBOUND with another NOT_RUNNING flag REBOUND.
4563 * @worker will clear REBOUND using worker_clr_flags() when
4564 * it initiates the next execution cycle thus restoring
4565 * concurrency management. Note that when or whether
4566 * @worker clears REBOUND doesn't affect correctness.
4567 *
4568 * ACCESS_ONCE() is necessary because @worker->flags may be
4569 * tested without holding any lock in
4570 * wq_worker_waking_up(). Without it, NOT_RUNNING test may
4571 * fail incorrectly leading to premature concurrency
4572 * management operations.
4573 */
4574 WARN_ON_ONCE(!(worker_flags & WORKER_UNBOUND));
4575 worker_flags |= WORKER_REBOUND;
4576 worker_flags &= ~WORKER_UNBOUND;
4577 ACCESS_ONCE(worker->flags) = worker_flags;
4578 }
4579
4580 spin_unlock_irq(&pool->lock);
4581}
4582
4583/**
4584 * restore_unbound_workers_cpumask - restore cpumask of unbound workers
4585 * @pool: unbound pool of interest
4586 * @cpu: the CPU which is coming up
4587 *
4588 * An unbound pool may end up with a cpumask which doesn't have any online
4589 * CPUs. When a worker of such pool get scheduled, the scheduler resets
4590 * its cpus_allowed. If @cpu is in @pool's cpumask which didn't have any
4591 * online CPU before, cpus_allowed of all its workers should be restored.
4592 */
4593static void restore_unbound_workers_cpumask(struct worker_pool *pool, int cpu)
4594{
4595 static cpumask_t cpumask;
4596 struct worker *worker;
4597 int wi;
4598
4599 lockdep_assert_held(&pool->manager_mutex);
4600
4601 /* is @cpu allowed for @pool? */
4602 if (!cpumask_test_cpu(cpu, pool->attrs->cpumask))
4603 return;
4604
4605 /* is @cpu the only online CPU? */
4606 cpumask_and(&cpumask, pool->attrs->cpumask, cpu_online_mask);
4607 if (cpumask_weight(&cpumask) != 1)
4608 return;
4609
4610 /* as we're called from CPU_ONLINE, the following shouldn't fail */
4611 for_each_pool_worker(worker, wi, pool)
4612 WARN_ON_ONCE(set_cpus_allowed_ptr(worker->task,
4613 pool->attrs->cpumask) < 0);
3472} 4614}
3473 4615
3474/* 4616/*
@@ -3479,39 +4621,46 @@ static int __cpuinit workqueue_cpu_up_callback(struct notifier_block *nfb,
3479 unsigned long action, 4621 unsigned long action,
3480 void *hcpu) 4622 void *hcpu)
3481{ 4623{
3482 unsigned int cpu = (unsigned long)hcpu; 4624 int cpu = (unsigned long)hcpu;
3483 struct worker_pool *pool; 4625 struct worker_pool *pool;
4626 struct workqueue_struct *wq;
4627 int pi;
3484 4628
3485 switch (action & ~CPU_TASKS_FROZEN) { 4629 switch (action & ~CPU_TASKS_FROZEN) {
3486 case CPU_UP_PREPARE: 4630 case CPU_UP_PREPARE:
3487 for_each_std_worker_pool(pool, cpu) { 4631 for_each_cpu_worker_pool(pool, cpu) {
3488 struct worker *worker;
3489
3490 if (pool->nr_workers) 4632 if (pool->nr_workers)
3491 continue; 4633 continue;
3492 4634 if (create_and_start_worker(pool) < 0)
3493 worker = create_worker(pool);
3494 if (!worker)
3495 return NOTIFY_BAD; 4635 return NOTIFY_BAD;
3496
3497 spin_lock_irq(&pool->lock);
3498 start_worker(worker);
3499 spin_unlock_irq(&pool->lock);
3500 } 4636 }
3501 break; 4637 break;
3502 4638
3503 case CPU_DOWN_FAILED: 4639 case CPU_DOWN_FAILED:
3504 case CPU_ONLINE: 4640 case CPU_ONLINE:
3505 for_each_std_worker_pool(pool, cpu) { 4641 mutex_lock(&wq_pool_mutex);
3506 mutex_lock(&pool->assoc_mutex);
3507 spin_lock_irq(&pool->lock);
3508 4642
3509 pool->flags &= ~POOL_DISASSOCIATED; 4643 for_each_pool(pool, pi) {
3510 rebind_workers(pool); 4644 mutex_lock(&pool->manager_mutex);
4645
4646 if (pool->cpu == cpu) {
4647 spin_lock_irq(&pool->lock);
4648 pool->flags &= ~POOL_DISASSOCIATED;
4649 spin_unlock_irq(&pool->lock);
3511 4650
3512 spin_unlock_irq(&pool->lock); 4651 rebind_workers(pool);
3513 mutex_unlock(&pool->assoc_mutex); 4652 } else if (pool->cpu < 0) {
4653 restore_unbound_workers_cpumask(pool, cpu);
4654 }
4655
4656 mutex_unlock(&pool->manager_mutex);
3514 } 4657 }
4658
4659 /* update NUMA affinity of unbound workqueues */
4660 list_for_each_entry(wq, &workqueues, list)
4661 wq_update_unbound_numa(wq, cpu, true);
4662
4663 mutex_unlock(&wq_pool_mutex);
3515 break; 4664 break;
3516 } 4665 }
3517 return NOTIFY_OK; 4666 return NOTIFY_OK;
@@ -3525,14 +4674,23 @@ static int __cpuinit workqueue_cpu_down_callback(struct notifier_block *nfb,
3525 unsigned long action, 4674 unsigned long action,
3526 void *hcpu) 4675 void *hcpu)
3527{ 4676{
3528 unsigned int cpu = (unsigned long)hcpu; 4677 int cpu = (unsigned long)hcpu;
3529 struct work_struct unbind_work; 4678 struct work_struct unbind_work;
4679 struct workqueue_struct *wq;
3530 4680
3531 switch (action & ~CPU_TASKS_FROZEN) { 4681 switch (action & ~CPU_TASKS_FROZEN) {
3532 case CPU_DOWN_PREPARE: 4682 case CPU_DOWN_PREPARE:
3533 /* unbinding should happen on the local CPU */ 4683 /* unbinding per-cpu workers should happen on the local CPU */
3534 INIT_WORK_ONSTACK(&unbind_work, wq_unbind_fn); 4684 INIT_WORK_ONSTACK(&unbind_work, wq_unbind_fn);
3535 queue_work_on(cpu, system_highpri_wq, &unbind_work); 4685 queue_work_on(cpu, system_highpri_wq, &unbind_work);
4686
4687 /* update NUMA affinity of unbound workqueues */
4688 mutex_lock(&wq_pool_mutex);
4689 list_for_each_entry(wq, &workqueues, list)
4690 wq_update_unbound_numa(wq, cpu, false);
4691 mutex_unlock(&wq_pool_mutex);
4692
4693 /* wait for per-cpu unbinding to finish */
3536 flush_work(&unbind_work); 4694 flush_work(&unbind_work);
3537 break; 4695 break;
3538 } 4696 }
@@ -3565,7 +4723,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. 4723 * 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. 4724 * The caller must not hold any locks which would prevent @fn from completing.
3567 */ 4725 */
3568long work_on_cpu(unsigned int cpu, long (*fn)(void *), void *arg) 4726long work_on_cpu(int cpu, long (*fn)(void *), void *arg)
3569{ 4727{
3570 struct work_for_cpu wfc = { .fn = fn, .arg = arg }; 4728 struct work_for_cpu wfc = { .fn = fn, .arg = arg };
3571 4729
@@ -3583,44 +4741,40 @@ EXPORT_SYMBOL_GPL(work_on_cpu);
3583 * freeze_workqueues_begin - begin freezing workqueues 4741 * freeze_workqueues_begin - begin freezing workqueues
3584 * 4742 *
3585 * Start freezing workqueues. After this function returns, all freezable 4743 * Start freezing workqueues. After this function returns, all freezable
3586 * workqueues will queue new works to their frozen_works list instead of 4744 * workqueues will queue new works to their delayed_works list instead of
3587 * pool->worklist. 4745 * pool->worklist.
3588 * 4746 *
3589 * CONTEXT: 4747 * CONTEXT:
3590 * Grabs and releases workqueue_lock and pool->lock's. 4748 * Grabs and releases wq_pool_mutex, wq->mutex and pool->lock's.
3591 */ 4749 */
3592void freeze_workqueues_begin(void) 4750void freeze_workqueues_begin(void)
3593{ 4751{
3594 unsigned int cpu; 4752 struct worker_pool *pool;
4753 struct workqueue_struct *wq;
4754 struct pool_workqueue *pwq;
4755 int pi;
3595 4756
3596 spin_lock(&workqueue_lock); 4757 mutex_lock(&wq_pool_mutex);
3597 4758
3598 BUG_ON(workqueue_freezing); 4759 WARN_ON_ONCE(workqueue_freezing);
3599 workqueue_freezing = true; 4760 workqueue_freezing = true;
3600 4761
3601 for_each_wq_cpu(cpu) { 4762 /* set FREEZING */
3602 struct worker_pool *pool; 4763 for_each_pool(pool, pi) {
3603 struct workqueue_struct *wq; 4764 spin_lock_irq(&pool->lock);
3604 4765 WARN_ON_ONCE(pool->flags & POOL_FREEZING);
3605 for_each_std_worker_pool(pool, cpu) { 4766 pool->flags |= POOL_FREEZING;
3606 spin_lock_irq(&pool->lock); 4767 spin_unlock_irq(&pool->lock);
3607 4768 }
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 4769
3619 spin_unlock_irq(&pool->lock); 4770 list_for_each_entry(wq, &workqueues, list) {
3620 } 4771 mutex_lock(&wq->mutex);
4772 for_each_pwq(pwq, wq)
4773 pwq_adjust_max_active(pwq);
4774 mutex_unlock(&wq->mutex);
3621 } 4775 }
3622 4776
3623 spin_unlock(&workqueue_lock); 4777 mutex_unlock(&wq_pool_mutex);
3624} 4778}
3625 4779
3626/** 4780/**
@@ -3630,7 +4784,7 @@ void freeze_workqueues_begin(void)
3630 * between freeze_workqueues_begin() and thaw_workqueues(). 4784 * between freeze_workqueues_begin() and thaw_workqueues().
3631 * 4785 *
3632 * CONTEXT: 4786 * CONTEXT:
3633 * Grabs and releases workqueue_lock. 4787 * Grabs and releases wq_pool_mutex.
3634 * 4788 *
3635 * RETURNS: 4789 * RETURNS:
3636 * %true if some freezable workqueues are still busy. %false if freezing 4790 * %true if some freezable workqueues are still busy. %false if freezing
@@ -3638,34 +4792,34 @@ void freeze_workqueues_begin(void)
3638 */ 4792 */
3639bool freeze_workqueues_busy(void) 4793bool freeze_workqueues_busy(void)
3640{ 4794{
3641 unsigned int cpu;
3642 bool busy = false; 4795 bool busy = false;
4796 struct workqueue_struct *wq;
4797 struct pool_workqueue *pwq;
3643 4798
3644 spin_lock(&workqueue_lock); 4799 mutex_lock(&wq_pool_mutex);
3645 4800
3646 BUG_ON(!workqueue_freezing); 4801 WARN_ON_ONCE(!workqueue_freezing);
3647 4802
3648 for_each_wq_cpu(cpu) { 4803 list_for_each_entry(wq, &workqueues, list) {
3649 struct workqueue_struct *wq; 4804 if (!(wq->flags & WQ_FREEZABLE))
4805 continue;
3650 /* 4806 /*
3651 * nr_active is monotonically decreasing. It's safe 4807 * nr_active is monotonically decreasing. It's safe
3652 * to peek without lock. 4808 * to peek without lock.
3653 */ 4809 */
3654 list_for_each_entry(wq, &workqueues, list) { 4810 rcu_read_lock_sched();
3655 struct pool_workqueue *pwq = get_pwq(cpu, wq); 4811 for_each_pwq(pwq, wq) {
3656 4812 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) { 4813 if (pwq->nr_active) {
3662 busy = true; 4814 busy = true;
4815 rcu_read_unlock_sched();
3663 goto out_unlock; 4816 goto out_unlock;
3664 } 4817 }
3665 } 4818 }
4819 rcu_read_unlock_sched();
3666 } 4820 }
3667out_unlock: 4821out_unlock:
3668 spin_unlock(&workqueue_lock); 4822 mutex_unlock(&wq_pool_mutex);
3669 return busy; 4823 return busy;
3670} 4824}
3671 4825
@@ -3676,104 +4830,141 @@ out_unlock:
3676 * frozen works are transferred to their respective pool worklists. 4830 * frozen works are transferred to their respective pool worklists.
3677 * 4831 *
3678 * CONTEXT: 4832 * CONTEXT:
3679 * Grabs and releases workqueue_lock and pool->lock's. 4833 * Grabs and releases wq_pool_mutex, wq->mutex and pool->lock's.
3680 */ 4834 */
3681void thaw_workqueues(void) 4835void thaw_workqueues(void)
3682{ 4836{
3683 unsigned int cpu; 4837 struct workqueue_struct *wq;
4838 struct pool_workqueue *pwq;
4839 struct worker_pool *pool;
4840 int pi;
3684 4841
3685 spin_lock(&workqueue_lock); 4842 mutex_lock(&wq_pool_mutex);
3686 4843
3687 if (!workqueue_freezing) 4844 if (!workqueue_freezing)
3688 goto out_unlock; 4845 goto out_unlock;
3689 4846
3690 for_each_wq_cpu(cpu) { 4847 /* clear FREEZING */
3691 struct worker_pool *pool; 4848 for_each_pool(pool, pi) {
3692 struct workqueue_struct *wq; 4849 spin_lock_irq(&pool->lock);
4850 WARN_ON_ONCE(!(pool->flags & POOL_FREEZING));
4851 pool->flags &= ~POOL_FREEZING;
4852 spin_unlock_irq(&pool->lock);
4853 }
3693 4854
3694 for_each_std_worker_pool(pool, cpu) { 4855 /* restore max_active and repopulate worklist */
3695 spin_lock_irq(&pool->lock); 4856 list_for_each_entry(wq, &workqueues, list) {
4857 mutex_lock(&wq->mutex);
4858 for_each_pwq(pwq, wq)
4859 pwq_adjust_max_active(pwq);
4860 mutex_unlock(&wq->mutex);
4861 }
3696 4862
3697 WARN_ON_ONCE(!(pool->flags & POOL_FREEZING)); 4863 workqueue_freezing = false;
3698 pool->flags &= ~POOL_FREEZING; 4864out_unlock:
4865 mutex_unlock(&wq_pool_mutex);
4866}
4867#endif /* CONFIG_FREEZER */
3699 4868
3700 list_for_each_entry(wq, &workqueues, list) { 4869static void __init wq_numa_init(void)
3701 struct pool_workqueue *pwq = get_pwq(cpu, wq); 4870{
4871 cpumask_var_t *tbl;
4872 int node, cpu;
3702 4873
3703 if (!pwq || pwq->pool != pool || 4874 /* determine NUMA pwq table len - highest node id + 1 */
3704 !(wq->flags & WQ_FREEZABLE)) 4875 for_each_node(node)
3705 continue; 4876 wq_numa_tbl_len = max(wq_numa_tbl_len, node + 1);
3706 4877
3707 /* restore max_active and repopulate worklist */ 4878 if (num_possible_nodes() <= 1)
3708 pwq_set_max_active(pwq, wq->saved_max_active); 4879 return;
3709 }
3710 4880
3711 wake_up_worker(pool); 4881 if (wq_disable_numa) {
4882 pr_info("workqueue: NUMA affinity support disabled\n");
4883 return;
4884 }
4885
4886 wq_update_unbound_numa_attrs_buf = alloc_workqueue_attrs(GFP_KERNEL);
4887 BUG_ON(!wq_update_unbound_numa_attrs_buf);
3712 4888
3713 spin_unlock_irq(&pool->lock); 4889 /*
4890 * We want masks of possible CPUs of each node which isn't readily
4891 * available. Build one from cpu_to_node() which should have been
4892 * fully initialized by now.
4893 */
4894 tbl = kzalloc(wq_numa_tbl_len * sizeof(tbl[0]), GFP_KERNEL);
4895 BUG_ON(!tbl);
4896
4897 for_each_node(node)
4898 BUG_ON(!alloc_cpumask_var_node(&tbl[node], GFP_KERNEL, node));
4899
4900 for_each_possible_cpu(cpu) {
4901 node = cpu_to_node(cpu);
4902 if (WARN_ON(node == NUMA_NO_NODE)) {
4903 pr_warn("workqueue: NUMA node mapping not available for cpu%d, disabling NUMA support\n", cpu);
4904 /* happens iff arch is bonkers, let's just proceed */
4905 return;
3714 } 4906 }
4907 cpumask_set_cpu(cpu, tbl[node]);
3715 } 4908 }
3716 4909
3717 workqueue_freezing = false; 4910 wq_numa_possible_cpumask = tbl;
3718out_unlock: 4911 wq_numa_enabled = true;
3719 spin_unlock(&workqueue_lock);
3720} 4912}
3721#endif /* CONFIG_FREEZER */
3722 4913
3723static int __init init_workqueues(void) 4914static int __init init_workqueues(void)
3724{ 4915{
3725 unsigned int cpu; 4916 int std_nice[NR_STD_WORKER_POOLS] = { 0, HIGHPRI_NICE_LEVEL };
4917 int i, cpu;
3726 4918
3727 /* make sure we have enough bits for OFFQ pool ID */ 4919 /* make sure we have enough bits for OFFQ pool ID */
3728 BUILD_BUG_ON((1LU << (BITS_PER_LONG - WORK_OFFQ_POOL_SHIFT)) < 4920 BUILD_BUG_ON((1LU << (BITS_PER_LONG - WORK_OFFQ_POOL_SHIFT)) <
3729 WORK_CPU_END * NR_STD_WORKER_POOLS); 4921 WORK_CPU_END * NR_STD_WORKER_POOLS);
3730 4922
4923 WARN_ON(__alignof__(struct pool_workqueue) < __alignof__(long long));
4924
4925 pwq_cache = KMEM_CACHE(pool_workqueue, SLAB_PANIC);
4926
3731 cpu_notifier(workqueue_cpu_up_callback, CPU_PRI_WORKQUEUE_UP); 4927 cpu_notifier(workqueue_cpu_up_callback, CPU_PRI_WORKQUEUE_UP);
3732 hotcpu_notifier(workqueue_cpu_down_callback, CPU_PRI_WORKQUEUE_DOWN); 4928 hotcpu_notifier(workqueue_cpu_down_callback, CPU_PRI_WORKQUEUE_DOWN);
3733 4929
4930 wq_numa_init();
4931
3734 /* initialize CPU pools */ 4932 /* initialize CPU pools */
3735 for_each_wq_cpu(cpu) { 4933 for_each_possible_cpu(cpu) {
3736 struct worker_pool *pool; 4934 struct worker_pool *pool;
3737 4935
3738 for_each_std_worker_pool(pool, cpu) { 4936 i = 0;
3739 spin_lock_init(&pool->lock); 4937 for_each_cpu_worker_pool(pool, cpu) {
4938 BUG_ON(init_worker_pool(pool));
3740 pool->cpu = cpu; 4939 pool->cpu = cpu;
3741 pool->flags |= POOL_DISASSOCIATED; 4940 cpumask_copy(pool->attrs->cpumask, cpumask_of(cpu));
3742 INIT_LIST_HEAD(&pool->worklist); 4941 pool->attrs->nice = std_nice[i++];
3743 INIT_LIST_HEAD(&pool->idle_list); 4942 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 4943
3756 /* alloc pool ID */ 4944 /* alloc pool ID */
4945 mutex_lock(&wq_pool_mutex);
3757 BUG_ON(worker_pool_assign_id(pool)); 4946 BUG_ON(worker_pool_assign_id(pool));
4947 mutex_unlock(&wq_pool_mutex);
3758 } 4948 }
3759 } 4949 }
3760 4950
3761 /* create the initial worker */ 4951 /* create the initial worker */
3762 for_each_online_wq_cpu(cpu) { 4952 for_each_online_cpu(cpu) {
3763 struct worker_pool *pool; 4953 struct worker_pool *pool;
3764 4954
3765 for_each_std_worker_pool(pool, cpu) { 4955 for_each_cpu_worker_pool(pool, cpu) {
3766 struct worker *worker; 4956 pool->flags &= ~POOL_DISASSOCIATED;
4957 BUG_ON(create_and_start_worker(pool) < 0);
4958 }
4959 }
3767 4960
3768 if (cpu != WORK_CPU_UNBOUND) 4961 /* create default unbound wq attrs */
3769 pool->flags &= ~POOL_DISASSOCIATED; 4962 for (i = 0; i < NR_STD_WORKER_POOLS; i++) {
4963 struct workqueue_attrs *attrs;
3770 4964
3771 worker = create_worker(pool); 4965 BUG_ON(!(attrs = alloc_workqueue_attrs(GFP_KERNEL)));
3772 BUG_ON(!worker); 4966 attrs->nice = std_nice[i];
3773 spin_lock_irq(&pool->lock); 4967 unbound_std_wq_attrs[i] = attrs;
3774 start_worker(worker);
3775 spin_unlock_irq(&pool->lock);
3776 }
3777 } 4968 }
3778 4969
3779 system_wq = alloc_workqueue("events", 0, 0); 4970 system_wq = alloc_workqueue("events", 0, 0);