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