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