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