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