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