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