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