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