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