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