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