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