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