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