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 TH |
62 | * |
63 | * Note that DISASSOCIATED can be flipped only while holding | |
24647570 TH |
64 | * assoc_mutex to avoid changing binding state while |
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 | * | |
4690c4ab | 122 | * W: workqueue_lock protected. |
76af4d93 TH |
123 | * |
124 | * R: workqueue_lock protected for writes. Sched-RCU protected for reads. | |
75ccf595 TH |
125 | * |
126 | * FR: wq->flush_mutex and workqueue_lock protected for writes. Sched-RCU | |
127 | * protected for reads. | |
1da177e4 | 128 | */ |
1da177e4 | 129 | |
2eaebdb3 | 130 | /* struct worker is defined in workqueue_internal.h */ |
c34056a3 | 131 | |
bd7bdd43 | 132 | struct worker_pool { |
d565ed63 | 133 | spinlock_t lock; /* the pool lock */ |
d84ff051 | 134 | int cpu; /* I: the associated cpu */ |
9daf9e67 | 135 | int id; /* I: pool ID */ |
11ebea50 | 136 | unsigned int flags; /* X: flags */ |
bd7bdd43 TH |
137 | |
138 | struct list_head worklist; /* L: list of pending works */ | |
139 | int nr_workers; /* L: total number of workers */ | |
ea1abd61 LJ |
140 | |
141 | /* nr_idle includes the ones off idle_list for rebinding */ | |
bd7bdd43 TH |
142 | int nr_idle; /* L: currently idle ones */ |
143 | ||
144 | struct list_head idle_list; /* X: list of idle workers */ | |
145 | struct timer_list idle_timer; /* L: worker idle timeout */ | |
146 | struct timer_list mayday_timer; /* L: SOS timer for workers */ | |
147 | ||
c5aa87bb | 148 | /* a workers is either on busy_hash or idle_list, or the manager */ |
c9e7cf27 TH |
149 | DECLARE_HASHTABLE(busy_hash, BUSY_WORKER_HASH_ORDER); |
150 | /* L: hash of busy workers */ | |
151 | ||
34a06bd6 | 152 | struct mutex manager_arb; /* manager arbitration */ |
24647570 | 153 | struct mutex assoc_mutex; /* protect POOL_DISASSOCIATED */ |
bd7bdd43 | 154 | struct ida worker_ida; /* L: for worker IDs */ |
e19e397a | 155 | |
7a4e344c | 156 | struct workqueue_attrs *attrs; /* I: worker attributes */ |
c5aa87bb TH |
157 | struct hlist_node hash_node; /* W: unbound_pool_hash node */ |
158 | int refcnt; /* W: refcnt for unbound pools */ | |
7a4e344c | 159 | |
e19e397a TH |
160 | /* |
161 | * The current concurrency level. As it's likely to be accessed | |
162 | * from other CPUs during try_to_wake_up(), put it in a separate | |
163 | * cacheline. | |
164 | */ | |
165 | atomic_t nr_running ____cacheline_aligned_in_smp; | |
29c91e99 TH |
166 | |
167 | /* | |
168 | * Destruction of pool is sched-RCU protected to allow dereferences | |
169 | * from get_work_pool(). | |
170 | */ | |
171 | struct rcu_head rcu; | |
8b03ae3c TH |
172 | } ____cacheline_aligned_in_smp; |
173 | ||
1da177e4 | 174 | /* |
112202d9 TH |
175 | * The per-pool workqueue. While queued, the lower WORK_STRUCT_FLAG_BITS |
176 | * of work_struct->data are used for flags and the remaining high bits | |
177 | * point to the pwq; thus, pwqs need to be aligned at two's power of the | |
178 | * number of flag bits. | |
1da177e4 | 179 | */ |
112202d9 | 180 | struct pool_workqueue { |
bd7bdd43 | 181 | struct worker_pool *pool; /* I: the associated pool */ |
4690c4ab | 182 | struct workqueue_struct *wq; /* I: the owning workqueue */ |
73f53c4a TH |
183 | int work_color; /* L: current color */ |
184 | int flush_color; /* L: flushing color */ | |
8864b4e5 | 185 | int refcnt; /* L: reference count */ |
73f53c4a TH |
186 | int nr_in_flight[WORK_NR_COLORS]; |
187 | /* L: nr of in_flight works */ | |
1e19ffc6 | 188 | int nr_active; /* L: nr of active works */ |
a0a1a5fd | 189 | int max_active; /* L: max active works */ |
1e19ffc6 | 190 | struct list_head delayed_works; /* L: delayed works */ |
75ccf595 | 191 | struct list_head pwqs_node; /* FR: node on wq->pwqs */ |
493a1724 | 192 | struct list_head mayday_node; /* W: node on wq->maydays */ |
8864b4e5 TH |
193 | |
194 | /* | |
195 | * Release of unbound pwq is punted to system_wq. See put_pwq() | |
196 | * and pwq_unbound_release_workfn() for details. pool_workqueue | |
197 | * itself is also sched-RCU protected so that the first pwq can be | |
198 | * determined without grabbing workqueue_lock. | |
199 | */ | |
200 | struct work_struct unbound_release_work; | |
201 | struct rcu_head rcu; | |
e904e6c2 | 202 | } __aligned(1 << WORK_STRUCT_FLAG_BITS); |
1da177e4 | 203 | |
73f53c4a TH |
204 | /* |
205 | * Structure used to wait for workqueue flush. | |
206 | */ | |
207 | struct wq_flusher { | |
208 | struct list_head list; /* F: list of flushers */ | |
209 | int flush_color; /* F: flush color waiting for */ | |
210 | struct completion done; /* flush completion */ | |
211 | }; | |
212 | ||
226223ab TH |
213 | struct wq_device; |
214 | ||
1da177e4 | 215 | /* |
c5aa87bb TH |
216 | * The externally visible workqueue. It relays the issued work items to |
217 | * the appropriate worker_pool through its pool_workqueues. | |
1da177e4 LT |
218 | */ |
219 | struct workqueue_struct { | |
9c5a2ba7 | 220 | unsigned int flags; /* W: WQ_* flags */ |
420c0ddb | 221 | struct pool_workqueue __percpu *cpu_pwqs; /* I: per-cpu pwq's */ |
75ccf595 | 222 | struct list_head pwqs; /* FR: all pwqs of this wq */ |
4690c4ab | 223 | struct list_head list; /* W: list of all workqueues */ |
73f53c4a TH |
224 | |
225 | struct mutex flush_mutex; /* protects wq flushing */ | |
226 | int work_color; /* F: current work color */ | |
227 | int flush_color; /* F: current flush color */ | |
112202d9 | 228 | atomic_t nr_pwqs_to_flush; /* flush in progress */ |
73f53c4a TH |
229 | struct wq_flusher *first_flusher; /* F: first flusher */ |
230 | struct list_head flusher_queue; /* F: flush waiters */ | |
231 | struct list_head flusher_overflow; /* F: flush overflow list */ | |
232 | ||
493a1724 | 233 | struct list_head maydays; /* W: pwqs requesting rescue */ |
e22bee78 TH |
234 | struct worker *rescuer; /* I: rescue worker */ |
235 | ||
9c5a2ba7 | 236 | int nr_drainers; /* W: drain in progress */ |
112202d9 | 237 | int saved_max_active; /* W: saved pwq max_active */ |
226223ab TH |
238 | |
239 | #ifdef CONFIG_SYSFS | |
240 | struct wq_device *wq_dev; /* I: for sysfs interface */ | |
241 | #endif | |
4e6045f1 | 242 | #ifdef CONFIG_LOCKDEP |
4690c4ab | 243 | struct lockdep_map lockdep_map; |
4e6045f1 | 244 | #endif |
b196be89 | 245 | char name[]; /* I: workqueue name */ |
1da177e4 LT |
246 | }; |
247 | ||
e904e6c2 TH |
248 | static struct kmem_cache *pwq_cache; |
249 | ||
c5aa87bb | 250 | /* W: hash of all unbound pools keyed by pool->attrs */ |
29c91e99 TH |
251 | static DEFINE_HASHTABLE(unbound_pool_hash, UNBOUND_POOL_HASH_ORDER); |
252 | ||
c5aa87bb | 253 | /* I: attributes used when instantiating standard unbound pools on demand */ |
29c91e99 TH |
254 | static struct workqueue_attrs *unbound_std_wq_attrs[NR_STD_WORKER_POOLS]; |
255 | ||
d320c038 | 256 | struct workqueue_struct *system_wq __read_mostly; |
d320c038 | 257 | EXPORT_SYMBOL_GPL(system_wq); |
044c782c | 258 | struct workqueue_struct *system_highpri_wq __read_mostly; |
1aabe902 | 259 | EXPORT_SYMBOL_GPL(system_highpri_wq); |
044c782c | 260 | struct workqueue_struct *system_long_wq __read_mostly; |
d320c038 | 261 | EXPORT_SYMBOL_GPL(system_long_wq); |
044c782c | 262 | struct workqueue_struct *system_unbound_wq __read_mostly; |
f3421797 | 263 | EXPORT_SYMBOL_GPL(system_unbound_wq); |
044c782c | 264 | struct workqueue_struct *system_freezable_wq __read_mostly; |
24d51add | 265 | EXPORT_SYMBOL_GPL(system_freezable_wq); |
d320c038 | 266 | |
97bd2347 TH |
267 | #define CREATE_TRACE_POINTS |
268 | #include <trace/events/workqueue.h> | |
269 | ||
76af4d93 TH |
270 | #define assert_rcu_or_wq_lock() \ |
271 | rcu_lockdep_assert(rcu_read_lock_sched_held() || \ | |
272 | lockdep_is_held(&workqueue_lock), \ | |
273 | "sched RCU or workqueue lock should be held") | |
274 | ||
f02ae73a TH |
275 | #define for_each_cpu_worker_pool(pool, cpu) \ |
276 | for ((pool) = &per_cpu(cpu_worker_pools, cpu)[0]; \ | |
277 | (pool) < &per_cpu(cpu_worker_pools, cpu)[NR_STD_WORKER_POOLS]; \ | |
7a62c2c8 | 278 | (pool)++) |
4ce62e9e | 279 | |
b67bfe0d SL |
280 | #define for_each_busy_worker(worker, i, pool) \ |
281 | hash_for_each(pool->busy_hash, i, worker, hentry) | |
db7bccf4 | 282 | |
17116969 TH |
283 | /** |
284 | * for_each_pool - iterate through all worker_pools in the system | |
285 | * @pool: iteration cursor | |
286 | * @id: integer used for iteration | |
fa1b54e6 TH |
287 | * |
288 | * This must be called either with workqueue_lock held or sched RCU read | |
289 | * locked. If the pool needs to be used beyond the locking in effect, the | |
290 | * caller is responsible for guaranteeing that the pool stays online. | |
291 | * | |
292 | * The if/else clause exists only for the lockdep assertion and can be | |
293 | * ignored. | |
17116969 TH |
294 | */ |
295 | #define for_each_pool(pool, id) \ | |
fa1b54e6 TH |
296 | idr_for_each_entry(&worker_pool_idr, pool, id) \ |
297 | if (({ assert_rcu_or_wq_lock(); false; })) { } \ | |
298 | else | |
17116969 | 299 | |
49e3cf44 TH |
300 | /** |
301 | * for_each_pwq - iterate through all pool_workqueues of the specified workqueue | |
302 | * @pwq: iteration cursor | |
303 | * @wq: the target workqueue | |
76af4d93 TH |
304 | * |
305 | * This must be called either with workqueue_lock held or sched RCU read | |
306 | * locked. If the pwq needs to be used beyond the locking in effect, the | |
307 | * caller is responsible for guaranteeing that the pwq stays online. | |
308 | * | |
309 | * The if/else clause exists only for the lockdep assertion and can be | |
310 | * ignored. | |
49e3cf44 TH |
311 | */ |
312 | #define for_each_pwq(pwq, wq) \ | |
76af4d93 TH |
313 | list_for_each_entry_rcu((pwq), &(wq)->pwqs, pwqs_node) \ |
314 | if (({ assert_rcu_or_wq_lock(); false; })) { } \ | |
315 | else | |
f3421797 | 316 | |
dc186ad7 TG |
317 | #ifdef CONFIG_DEBUG_OBJECTS_WORK |
318 | ||
319 | static struct debug_obj_descr work_debug_descr; | |
320 | ||
99777288 SG |
321 | static void *work_debug_hint(void *addr) |
322 | { | |
323 | return ((struct work_struct *) addr)->func; | |
324 | } | |
325 | ||
dc186ad7 TG |
326 | /* |
327 | * fixup_init is called when: | |
328 | * - an active object is initialized | |
329 | */ | |
330 | static int work_fixup_init(void *addr, enum debug_obj_state state) | |
331 | { | |
332 | struct work_struct *work = addr; | |
333 | ||
334 | switch (state) { | |
335 | case ODEBUG_STATE_ACTIVE: | |
336 | cancel_work_sync(work); | |
337 | debug_object_init(work, &work_debug_descr); | |
338 | return 1; | |
339 | default: | |
340 | return 0; | |
341 | } | |
342 | } | |
343 | ||
344 | /* | |
345 | * fixup_activate is called when: | |
346 | * - an active object is activated | |
347 | * - an unknown object is activated (might be a statically initialized object) | |
348 | */ | |
349 | static int work_fixup_activate(void *addr, enum debug_obj_state state) | |
350 | { | |
351 | struct work_struct *work = addr; | |
352 | ||
353 | switch (state) { | |
354 | ||
355 | case ODEBUG_STATE_NOTAVAILABLE: | |
356 | /* | |
357 | * This is not really a fixup. The work struct was | |
358 | * statically initialized. We just make sure that it | |
359 | * is tracked in the object tracker. | |
360 | */ | |
22df02bb | 361 | if (test_bit(WORK_STRUCT_STATIC_BIT, work_data_bits(work))) { |
dc186ad7 TG |
362 | debug_object_init(work, &work_debug_descr); |
363 | debug_object_activate(work, &work_debug_descr); | |
364 | return 0; | |
365 | } | |
366 | WARN_ON_ONCE(1); | |
367 | return 0; | |
368 | ||
369 | case ODEBUG_STATE_ACTIVE: | |
370 | WARN_ON(1); | |
371 | ||
372 | default: | |
373 | return 0; | |
374 | } | |
375 | } | |
376 | ||
377 | /* | |
378 | * fixup_free is called when: | |
379 | * - an active object is freed | |
380 | */ | |
381 | static int work_fixup_free(void *addr, enum debug_obj_state state) | |
382 | { | |
383 | struct work_struct *work = addr; | |
384 | ||
385 | switch (state) { | |
386 | case ODEBUG_STATE_ACTIVE: | |
387 | cancel_work_sync(work); | |
388 | debug_object_free(work, &work_debug_descr); | |
389 | return 1; | |
390 | default: | |
391 | return 0; | |
392 | } | |
393 | } | |
394 | ||
395 | static struct debug_obj_descr work_debug_descr = { | |
396 | .name = "work_struct", | |
99777288 | 397 | .debug_hint = work_debug_hint, |
dc186ad7 TG |
398 | .fixup_init = work_fixup_init, |
399 | .fixup_activate = work_fixup_activate, | |
400 | .fixup_free = work_fixup_free, | |
401 | }; | |
402 | ||
403 | static inline void debug_work_activate(struct work_struct *work) | |
404 | { | |
405 | debug_object_activate(work, &work_debug_descr); | |
406 | } | |
407 | ||
408 | static inline void debug_work_deactivate(struct work_struct *work) | |
409 | { | |
410 | debug_object_deactivate(work, &work_debug_descr); | |
411 | } | |
412 | ||
413 | void __init_work(struct work_struct *work, int onstack) | |
414 | { | |
415 | if (onstack) | |
416 | debug_object_init_on_stack(work, &work_debug_descr); | |
417 | else | |
418 | debug_object_init(work, &work_debug_descr); | |
419 | } | |
420 | EXPORT_SYMBOL_GPL(__init_work); | |
421 | ||
422 | void destroy_work_on_stack(struct work_struct *work) | |
423 | { | |
424 | debug_object_free(work, &work_debug_descr); | |
425 | } | |
426 | EXPORT_SYMBOL_GPL(destroy_work_on_stack); | |
427 | ||
428 | #else | |
429 | static inline void debug_work_activate(struct work_struct *work) { } | |
430 | static inline void debug_work_deactivate(struct work_struct *work) { } | |
431 | #endif | |
432 | ||
95402b38 GS |
433 | /* Serializes the accesses to the list of workqueues. */ |
434 | static DEFINE_SPINLOCK(workqueue_lock); | |
1da177e4 | 435 | static LIST_HEAD(workqueues); |
a0a1a5fd | 436 | static bool workqueue_freezing; /* W: have wqs started freezing? */ |
c34056a3 | 437 | |
c5aa87bb | 438 | /* the per-cpu worker pools */ |
e19e397a | 439 | static DEFINE_PER_CPU_SHARED_ALIGNED(struct worker_pool [NR_STD_WORKER_POOLS], |
f02ae73a | 440 | cpu_worker_pools); |
f3421797 | 441 | |
fa1b54e6 | 442 | /* |
c5aa87bb TH |
443 | * R: idr of all pools. Modifications are protected by workqueue_lock. |
444 | * Read accesses are protected by sched-RCU protected. | |
fa1b54e6 | 445 | */ |
9daf9e67 TH |
446 | static DEFINE_IDR(worker_pool_idr); |
447 | ||
c34056a3 | 448 | static int worker_thread(void *__worker); |
226223ab TH |
449 | static void copy_workqueue_attrs(struct workqueue_attrs *to, |
450 | const struct workqueue_attrs *from); | |
1da177e4 | 451 | |
9daf9e67 TH |
452 | /* allocate ID and assign it to @pool */ |
453 | static int worker_pool_assign_id(struct worker_pool *pool) | |
454 | { | |
455 | int ret; | |
456 | ||
fa1b54e6 TH |
457 | do { |
458 | if (!idr_pre_get(&worker_pool_idr, GFP_KERNEL)) | |
459 | return -ENOMEM; | |
9daf9e67 | 460 | |
fa1b54e6 TH |
461 | spin_lock_irq(&workqueue_lock); |
462 | ret = idr_get_new(&worker_pool_idr, pool, &pool->id); | |
463 | spin_unlock_irq(&workqueue_lock); | |
464 | } while (ret == -EAGAIN); | |
9daf9e67 | 465 | |
fa1b54e6 | 466 | return ret; |
7c3eed5c TH |
467 | } |
468 | ||
76af4d93 TH |
469 | /** |
470 | * first_pwq - return the first pool_workqueue of the specified workqueue | |
471 | * @wq: the target workqueue | |
472 | * | |
473 | * This must be called either with workqueue_lock held or sched RCU read | |
474 | * locked. If the pwq needs to be used beyond the locking in effect, the | |
475 | * caller is responsible for guaranteeing that the pwq stays online. | |
476 | */ | |
7fb98ea7 | 477 | static struct pool_workqueue *first_pwq(struct workqueue_struct *wq) |
b1f4ec17 | 478 | { |
76af4d93 TH |
479 | assert_rcu_or_wq_lock(); |
480 | return list_first_or_null_rcu(&wq->pwqs, struct pool_workqueue, | |
481 | pwqs_node); | |
b1f4ec17 ON |
482 | } |
483 | ||
73f53c4a TH |
484 | static unsigned int work_color_to_flags(int color) |
485 | { | |
486 | return color << WORK_STRUCT_COLOR_SHIFT; | |
487 | } | |
488 | ||
489 | static int get_work_color(struct work_struct *work) | |
490 | { | |
491 | return (*work_data_bits(work) >> WORK_STRUCT_COLOR_SHIFT) & | |
492 | ((1 << WORK_STRUCT_COLOR_BITS) - 1); | |
493 | } | |
494 | ||
495 | static int work_next_color(int color) | |
496 | { | |
497 | return (color + 1) % WORK_NR_COLORS; | |
498 | } | |
1da177e4 | 499 | |
14441960 | 500 | /* |
112202d9 TH |
501 | * While queued, %WORK_STRUCT_PWQ is set and non flag bits of a work's data |
502 | * contain the pointer to the queued pwq. Once execution starts, the flag | |
7c3eed5c | 503 | * is cleared and the high bits contain OFFQ flags and pool ID. |
7a22ad75 | 504 | * |
112202d9 TH |
505 | * set_work_pwq(), set_work_pool_and_clear_pending(), mark_work_canceling() |
506 | * and clear_work_data() can be used to set the pwq, pool or clear | |
bbb68dfa TH |
507 | * work->data. These functions should only be called while the work is |
508 | * owned - ie. while the PENDING bit is set. | |
7a22ad75 | 509 | * |
112202d9 | 510 | * get_work_pool() and get_work_pwq() can be used to obtain the pool or pwq |
7c3eed5c | 511 | * corresponding to a work. Pool is available once the work has been |
112202d9 | 512 | * queued anywhere after initialization until it is sync canceled. pwq is |
7c3eed5c | 513 | * available only while the work item is queued. |
7a22ad75 | 514 | * |
bbb68dfa TH |
515 | * %WORK_OFFQ_CANCELING is used to mark a work item which is being |
516 | * canceled. While being canceled, a work item may have its PENDING set | |
517 | * but stay off timer and worklist for arbitrarily long and nobody should | |
518 | * try to steal the PENDING bit. | |
14441960 | 519 | */ |
7a22ad75 TH |
520 | static inline void set_work_data(struct work_struct *work, unsigned long data, |
521 | unsigned long flags) | |
365970a1 | 522 | { |
6183c009 | 523 | WARN_ON_ONCE(!work_pending(work)); |
7a22ad75 TH |
524 | atomic_long_set(&work->data, data | flags | work_static(work)); |
525 | } | |
365970a1 | 526 | |
112202d9 | 527 | static void set_work_pwq(struct work_struct *work, struct pool_workqueue *pwq, |
7a22ad75 TH |
528 | unsigned long extra_flags) |
529 | { | |
112202d9 TH |
530 | set_work_data(work, (unsigned long)pwq, |
531 | WORK_STRUCT_PENDING | WORK_STRUCT_PWQ | extra_flags); | |
365970a1 DH |
532 | } |
533 | ||
4468a00f LJ |
534 | static void set_work_pool_and_keep_pending(struct work_struct *work, |
535 | int pool_id) | |
536 | { | |
537 | set_work_data(work, (unsigned long)pool_id << WORK_OFFQ_POOL_SHIFT, | |
538 | WORK_STRUCT_PENDING); | |
539 | } | |
540 | ||
7c3eed5c TH |
541 | static void set_work_pool_and_clear_pending(struct work_struct *work, |
542 | int pool_id) | |
7a22ad75 | 543 | { |
23657bb1 TH |
544 | /* |
545 | * The following wmb is paired with the implied mb in | |
546 | * test_and_set_bit(PENDING) and ensures all updates to @work made | |
547 | * here are visible to and precede any updates by the next PENDING | |
548 | * owner. | |
549 | */ | |
550 | smp_wmb(); | |
7c3eed5c | 551 | set_work_data(work, (unsigned long)pool_id << WORK_OFFQ_POOL_SHIFT, 0); |
7a22ad75 | 552 | } |
f756d5e2 | 553 | |
7a22ad75 | 554 | static void clear_work_data(struct work_struct *work) |
1da177e4 | 555 | { |
7c3eed5c TH |
556 | smp_wmb(); /* see set_work_pool_and_clear_pending() */ |
557 | set_work_data(work, WORK_STRUCT_NO_POOL, 0); | |
1da177e4 LT |
558 | } |
559 | ||
112202d9 | 560 | static struct pool_workqueue *get_work_pwq(struct work_struct *work) |
b1f4ec17 | 561 | { |
e120153d | 562 | unsigned long data = atomic_long_read(&work->data); |
7a22ad75 | 563 | |
112202d9 | 564 | if (data & WORK_STRUCT_PWQ) |
e120153d TH |
565 | return (void *)(data & WORK_STRUCT_WQ_DATA_MASK); |
566 | else | |
567 | return NULL; | |
4d707b9f ON |
568 | } |
569 | ||
7c3eed5c TH |
570 | /** |
571 | * get_work_pool - return the worker_pool a given work was associated with | |
572 | * @work: the work item of interest | |
573 | * | |
574 | * Return the worker_pool @work was last associated with. %NULL if none. | |
fa1b54e6 TH |
575 | * |
576 | * Pools are created and destroyed under workqueue_lock, and allows read | |
577 | * access under sched-RCU read lock. As such, this function should be | |
578 | * called under workqueue_lock or with preemption disabled. | |
579 | * | |
580 | * All fields of the returned pool are accessible as long as the above | |
581 | * mentioned locking is in effect. If the returned pool needs to be used | |
582 | * beyond the critical section, the caller is responsible for ensuring the | |
583 | * returned pool is and stays online. | |
7c3eed5c TH |
584 | */ |
585 | static struct worker_pool *get_work_pool(struct work_struct *work) | |
365970a1 | 586 | { |
e120153d | 587 | unsigned long data = atomic_long_read(&work->data); |
7c3eed5c | 588 | int pool_id; |
7a22ad75 | 589 | |
fa1b54e6 TH |
590 | assert_rcu_or_wq_lock(); |
591 | ||
112202d9 TH |
592 | if (data & WORK_STRUCT_PWQ) |
593 | return ((struct pool_workqueue *) | |
7c3eed5c | 594 | (data & WORK_STRUCT_WQ_DATA_MASK))->pool; |
7a22ad75 | 595 | |
7c3eed5c TH |
596 | pool_id = data >> WORK_OFFQ_POOL_SHIFT; |
597 | if (pool_id == WORK_OFFQ_POOL_NONE) | |
7a22ad75 TH |
598 | return NULL; |
599 | ||
fa1b54e6 | 600 | return idr_find(&worker_pool_idr, pool_id); |
7c3eed5c TH |
601 | } |
602 | ||
603 | /** | |
604 | * get_work_pool_id - return the worker pool ID a given work is associated with | |
605 | * @work: the work item of interest | |
606 | * | |
607 | * Return the worker_pool ID @work was last associated with. | |
608 | * %WORK_OFFQ_POOL_NONE if none. | |
609 | */ | |
610 | static int get_work_pool_id(struct work_struct *work) | |
611 | { | |
54d5b7d0 LJ |
612 | unsigned long data = atomic_long_read(&work->data); |
613 | ||
112202d9 TH |
614 | if (data & WORK_STRUCT_PWQ) |
615 | return ((struct pool_workqueue *) | |
54d5b7d0 | 616 | (data & WORK_STRUCT_WQ_DATA_MASK))->pool->id; |
7c3eed5c | 617 | |
54d5b7d0 | 618 | return data >> WORK_OFFQ_POOL_SHIFT; |
7c3eed5c TH |
619 | } |
620 | ||
bbb68dfa TH |
621 | static void mark_work_canceling(struct work_struct *work) |
622 | { | |
7c3eed5c | 623 | unsigned long pool_id = get_work_pool_id(work); |
bbb68dfa | 624 | |
7c3eed5c TH |
625 | pool_id <<= WORK_OFFQ_POOL_SHIFT; |
626 | set_work_data(work, pool_id | WORK_OFFQ_CANCELING, WORK_STRUCT_PENDING); | |
bbb68dfa TH |
627 | } |
628 | ||
629 | static bool work_is_canceling(struct work_struct *work) | |
630 | { | |
631 | unsigned long data = atomic_long_read(&work->data); | |
632 | ||
112202d9 | 633 | return !(data & WORK_STRUCT_PWQ) && (data & WORK_OFFQ_CANCELING); |
bbb68dfa TH |
634 | } |
635 | ||
e22bee78 | 636 | /* |
3270476a TH |
637 | * Policy functions. These define the policies on how the global worker |
638 | * pools are managed. Unless noted otherwise, these functions assume that | |
d565ed63 | 639 | * they're being called with pool->lock held. |
e22bee78 TH |
640 | */ |
641 | ||
63d95a91 | 642 | static bool __need_more_worker(struct worker_pool *pool) |
a848e3b6 | 643 | { |
e19e397a | 644 | return !atomic_read(&pool->nr_running); |
a848e3b6 ON |
645 | } |
646 | ||
4594bf15 | 647 | /* |
e22bee78 TH |
648 | * Need to wake up a worker? Called from anything but currently |
649 | * running workers. | |
974271c4 TH |
650 | * |
651 | * Note that, because unbound workers never contribute to nr_running, this | |
706026c2 | 652 | * function will always return %true for unbound pools as long as the |
974271c4 | 653 | * worklist isn't empty. |
4594bf15 | 654 | */ |
63d95a91 | 655 | static bool need_more_worker(struct worker_pool *pool) |
365970a1 | 656 | { |
63d95a91 | 657 | return !list_empty(&pool->worklist) && __need_more_worker(pool); |
e22bee78 | 658 | } |
4594bf15 | 659 | |
e22bee78 | 660 | /* Can I start working? Called from busy but !running workers. */ |
63d95a91 | 661 | static bool may_start_working(struct worker_pool *pool) |
e22bee78 | 662 | { |
63d95a91 | 663 | return pool->nr_idle; |
e22bee78 TH |
664 | } |
665 | ||
666 | /* Do I need to keep working? Called from currently running workers. */ | |
63d95a91 | 667 | static bool keep_working(struct worker_pool *pool) |
e22bee78 | 668 | { |
e19e397a TH |
669 | return !list_empty(&pool->worklist) && |
670 | atomic_read(&pool->nr_running) <= 1; | |
e22bee78 TH |
671 | } |
672 | ||
673 | /* Do we need a new worker? Called from manager. */ | |
63d95a91 | 674 | static bool need_to_create_worker(struct worker_pool *pool) |
e22bee78 | 675 | { |
63d95a91 | 676 | return need_more_worker(pool) && !may_start_working(pool); |
e22bee78 | 677 | } |
365970a1 | 678 | |
e22bee78 | 679 | /* Do I need to be the manager? */ |
63d95a91 | 680 | static bool need_to_manage_workers(struct worker_pool *pool) |
e22bee78 | 681 | { |
63d95a91 | 682 | return need_to_create_worker(pool) || |
11ebea50 | 683 | (pool->flags & POOL_MANAGE_WORKERS); |
e22bee78 TH |
684 | } |
685 | ||
686 | /* Do we have too many workers and should some go away? */ | |
63d95a91 | 687 | static bool too_many_workers(struct worker_pool *pool) |
e22bee78 | 688 | { |
34a06bd6 | 689 | bool managing = mutex_is_locked(&pool->manager_arb); |
63d95a91 TH |
690 | int nr_idle = pool->nr_idle + managing; /* manager is considered idle */ |
691 | int nr_busy = pool->nr_workers - nr_idle; | |
e22bee78 | 692 | |
ea1abd61 LJ |
693 | /* |
694 | * nr_idle and idle_list may disagree if idle rebinding is in | |
695 | * progress. Never return %true if idle_list is empty. | |
696 | */ | |
697 | if (list_empty(&pool->idle_list)) | |
698 | return false; | |
699 | ||
e22bee78 | 700 | return nr_idle > 2 && (nr_idle - 2) * MAX_IDLE_WORKERS_RATIO >= nr_busy; |
365970a1 DH |
701 | } |
702 | ||
4d707b9f | 703 | /* |
e22bee78 TH |
704 | * Wake up functions. |
705 | */ | |
706 | ||
7e11629d | 707 | /* Return the first worker. Safe with preemption disabled */ |
63d95a91 | 708 | static struct worker *first_worker(struct worker_pool *pool) |
7e11629d | 709 | { |
63d95a91 | 710 | if (unlikely(list_empty(&pool->idle_list))) |
7e11629d TH |
711 | return NULL; |
712 | ||
63d95a91 | 713 | return list_first_entry(&pool->idle_list, struct worker, entry); |
7e11629d TH |
714 | } |
715 | ||
716 | /** | |
717 | * wake_up_worker - wake up an idle worker | |
63d95a91 | 718 | * @pool: worker pool to wake worker from |
7e11629d | 719 | * |
63d95a91 | 720 | * Wake up the first idle worker of @pool. |
7e11629d TH |
721 | * |
722 | * CONTEXT: | |
d565ed63 | 723 | * spin_lock_irq(pool->lock). |
7e11629d | 724 | */ |
63d95a91 | 725 | static void wake_up_worker(struct worker_pool *pool) |
7e11629d | 726 | { |
63d95a91 | 727 | struct worker *worker = first_worker(pool); |
7e11629d TH |
728 | |
729 | if (likely(worker)) | |
730 | wake_up_process(worker->task); | |
731 | } | |
732 | ||
d302f017 | 733 | /** |
e22bee78 TH |
734 | * wq_worker_waking_up - a worker is waking up |
735 | * @task: task waking up | |
736 | * @cpu: CPU @task is waking up to | |
737 | * | |
738 | * This function is called during try_to_wake_up() when a worker is | |
739 | * being awoken. | |
740 | * | |
741 | * CONTEXT: | |
742 | * spin_lock_irq(rq->lock) | |
743 | */ | |
d84ff051 | 744 | void wq_worker_waking_up(struct task_struct *task, int cpu) |
e22bee78 TH |
745 | { |
746 | struct worker *worker = kthread_data(task); | |
747 | ||
36576000 | 748 | if (!(worker->flags & WORKER_NOT_RUNNING)) { |
ec22ca5e | 749 | WARN_ON_ONCE(worker->pool->cpu != cpu); |
e19e397a | 750 | atomic_inc(&worker->pool->nr_running); |
36576000 | 751 | } |
e22bee78 TH |
752 | } |
753 | ||
754 | /** | |
755 | * wq_worker_sleeping - a worker is going to sleep | |
756 | * @task: task going to sleep | |
757 | * @cpu: CPU in question, must be the current CPU number | |
758 | * | |
759 | * This function is called during schedule() when a busy worker is | |
760 | * going to sleep. Worker on the same cpu can be woken up by | |
761 | * returning pointer to its task. | |
762 | * | |
763 | * CONTEXT: | |
764 | * spin_lock_irq(rq->lock) | |
765 | * | |
766 | * RETURNS: | |
767 | * Worker task on @cpu to wake up, %NULL if none. | |
768 | */ | |
d84ff051 | 769 | struct task_struct *wq_worker_sleeping(struct task_struct *task, int cpu) |
e22bee78 TH |
770 | { |
771 | struct worker *worker = kthread_data(task), *to_wakeup = NULL; | |
111c225a | 772 | struct worker_pool *pool; |
e22bee78 | 773 | |
111c225a TH |
774 | /* |
775 | * Rescuers, which may not have all the fields set up like normal | |
776 | * workers, also reach here, let's not access anything before | |
777 | * checking NOT_RUNNING. | |
778 | */ | |
2d64672e | 779 | if (worker->flags & WORKER_NOT_RUNNING) |
e22bee78 TH |
780 | return NULL; |
781 | ||
111c225a | 782 | pool = worker->pool; |
111c225a | 783 | |
e22bee78 | 784 | /* this can only happen on the local cpu */ |
6183c009 TH |
785 | if (WARN_ON_ONCE(cpu != raw_smp_processor_id())) |
786 | return NULL; | |
e22bee78 TH |
787 | |
788 | /* | |
789 | * The counterpart of the following dec_and_test, implied mb, | |
790 | * worklist not empty test sequence is in insert_work(). | |
791 | * Please read comment there. | |
792 | * | |
628c78e7 TH |
793 | * NOT_RUNNING is clear. This means that we're bound to and |
794 | * running on the local cpu w/ rq lock held and preemption | |
795 | * disabled, which in turn means that none else could be | |
d565ed63 | 796 | * manipulating idle_list, so dereferencing idle_list without pool |
628c78e7 | 797 | * lock is safe. |
e22bee78 | 798 | */ |
e19e397a TH |
799 | if (atomic_dec_and_test(&pool->nr_running) && |
800 | !list_empty(&pool->worklist)) | |
63d95a91 | 801 | to_wakeup = first_worker(pool); |
e22bee78 TH |
802 | return to_wakeup ? to_wakeup->task : NULL; |
803 | } | |
804 | ||
805 | /** | |
806 | * worker_set_flags - set worker flags and adjust nr_running accordingly | |
cb444766 | 807 | * @worker: self |
d302f017 TH |
808 | * @flags: flags to set |
809 | * @wakeup: wakeup an idle worker if necessary | |
810 | * | |
e22bee78 TH |
811 | * Set @flags in @worker->flags and adjust nr_running accordingly. If |
812 | * nr_running becomes zero and @wakeup is %true, an idle worker is | |
813 | * woken up. | |
d302f017 | 814 | * |
cb444766 | 815 | * CONTEXT: |
d565ed63 | 816 | * spin_lock_irq(pool->lock) |
d302f017 TH |
817 | */ |
818 | static inline void worker_set_flags(struct worker *worker, unsigned int flags, | |
819 | bool wakeup) | |
820 | { | |
bd7bdd43 | 821 | struct worker_pool *pool = worker->pool; |
e22bee78 | 822 | |
cb444766 TH |
823 | WARN_ON_ONCE(worker->task != current); |
824 | ||
e22bee78 TH |
825 | /* |
826 | * If transitioning into NOT_RUNNING, adjust nr_running and | |
827 | * wake up an idle worker as necessary if requested by | |
828 | * @wakeup. | |
829 | */ | |
830 | if ((flags & WORKER_NOT_RUNNING) && | |
831 | !(worker->flags & WORKER_NOT_RUNNING)) { | |
e22bee78 | 832 | if (wakeup) { |
e19e397a | 833 | if (atomic_dec_and_test(&pool->nr_running) && |
bd7bdd43 | 834 | !list_empty(&pool->worklist)) |
63d95a91 | 835 | wake_up_worker(pool); |
e22bee78 | 836 | } else |
e19e397a | 837 | atomic_dec(&pool->nr_running); |
e22bee78 TH |
838 | } |
839 | ||
d302f017 TH |
840 | worker->flags |= flags; |
841 | } | |
842 | ||
843 | /** | |
e22bee78 | 844 | * worker_clr_flags - clear worker flags and adjust nr_running accordingly |
cb444766 | 845 | * @worker: self |
d302f017 TH |
846 | * @flags: flags to clear |
847 | * | |
e22bee78 | 848 | * Clear @flags in @worker->flags and adjust nr_running accordingly. |
d302f017 | 849 | * |
cb444766 | 850 | * CONTEXT: |
d565ed63 | 851 | * spin_lock_irq(pool->lock) |
d302f017 TH |
852 | */ |
853 | static inline void worker_clr_flags(struct worker *worker, unsigned int flags) | |
854 | { | |
63d95a91 | 855 | struct worker_pool *pool = worker->pool; |
e22bee78 TH |
856 | unsigned int oflags = worker->flags; |
857 | ||
cb444766 TH |
858 | WARN_ON_ONCE(worker->task != current); |
859 | ||
d302f017 | 860 | worker->flags &= ~flags; |
e22bee78 | 861 | |
42c025f3 TH |
862 | /* |
863 | * If transitioning out of NOT_RUNNING, increment nr_running. Note | |
864 | * that the nested NOT_RUNNING is not a noop. NOT_RUNNING is mask | |
865 | * of multiple flags, not a single flag. | |
866 | */ | |
e22bee78 TH |
867 | if ((flags & WORKER_NOT_RUNNING) && (oflags & WORKER_NOT_RUNNING)) |
868 | if (!(worker->flags & WORKER_NOT_RUNNING)) | |
e19e397a | 869 | atomic_inc(&pool->nr_running); |
d302f017 TH |
870 | } |
871 | ||
8cca0eea TH |
872 | /** |
873 | * find_worker_executing_work - find worker which is executing a work | |
c9e7cf27 | 874 | * @pool: pool of interest |
8cca0eea TH |
875 | * @work: work to find worker for |
876 | * | |
c9e7cf27 TH |
877 | * Find a worker which is executing @work on @pool by searching |
878 | * @pool->busy_hash which is keyed by the address of @work. For a worker | |
a2c1c57b TH |
879 | * to match, its current execution should match the address of @work and |
880 | * its work function. This is to avoid unwanted dependency between | |
881 | * unrelated work executions through a work item being recycled while still | |
882 | * being executed. | |
883 | * | |
884 | * This is a bit tricky. A work item may be freed once its execution | |
885 | * starts and nothing prevents the freed area from being recycled for | |
886 | * another work item. If the same work item address ends up being reused | |
887 | * before the original execution finishes, workqueue will identify the | |
888 | * recycled work item as currently executing and make it wait until the | |
889 | * current execution finishes, introducing an unwanted dependency. | |
890 | * | |
c5aa87bb TH |
891 | * This function checks the work item address and work function to avoid |
892 | * false positives. Note that this isn't complete as one may construct a | |
893 | * work function which can introduce dependency onto itself through a | |
894 | * recycled work item. Well, if somebody wants to shoot oneself in the | |
895 | * foot that badly, there's only so much we can do, and if such deadlock | |
896 | * actually occurs, it should be easy to locate the culprit work function. | |
8cca0eea TH |
897 | * |
898 | * CONTEXT: | |
d565ed63 | 899 | * spin_lock_irq(pool->lock). |
8cca0eea TH |
900 | * |
901 | * RETURNS: | |
902 | * Pointer to worker which is executing @work if found, NULL | |
903 | * otherwise. | |
4d707b9f | 904 | */ |
c9e7cf27 | 905 | static struct worker *find_worker_executing_work(struct worker_pool *pool, |
8cca0eea | 906 | struct work_struct *work) |
4d707b9f | 907 | { |
42f8570f | 908 | struct worker *worker; |
42f8570f | 909 | |
b67bfe0d | 910 | hash_for_each_possible(pool->busy_hash, worker, hentry, |
a2c1c57b TH |
911 | (unsigned long)work) |
912 | if (worker->current_work == work && | |
913 | worker->current_func == work->func) | |
42f8570f SL |
914 | return worker; |
915 | ||
916 | return NULL; | |
4d707b9f ON |
917 | } |
918 | ||
bf4ede01 TH |
919 | /** |
920 | * move_linked_works - move linked works to a list | |
921 | * @work: start of series of works to be scheduled | |
922 | * @head: target list to append @work to | |
923 | * @nextp: out paramter for nested worklist walking | |
924 | * | |
925 | * Schedule linked works starting from @work to @head. Work series to | |
926 | * be scheduled starts at @work and includes any consecutive work with | |
927 | * WORK_STRUCT_LINKED set in its predecessor. | |
928 | * | |
929 | * If @nextp is not NULL, it's updated to point to the next work of | |
930 | * the last scheduled work. This allows move_linked_works() to be | |
931 | * nested inside outer list_for_each_entry_safe(). | |
932 | * | |
933 | * CONTEXT: | |
d565ed63 | 934 | * spin_lock_irq(pool->lock). |
bf4ede01 TH |
935 | */ |
936 | static void move_linked_works(struct work_struct *work, struct list_head *head, | |
937 | struct work_struct **nextp) | |
938 | { | |
939 | struct work_struct *n; | |
940 | ||
941 | /* | |
942 | * Linked worklist will always end before the end of the list, | |
943 | * use NULL for list head. | |
944 | */ | |
945 | list_for_each_entry_safe_from(work, n, NULL, entry) { | |
946 | list_move_tail(&work->entry, head); | |
947 | if (!(*work_data_bits(work) & WORK_STRUCT_LINKED)) | |
948 | break; | |
949 | } | |
950 | ||
951 | /* | |
952 | * If we're already inside safe list traversal and have moved | |
953 | * multiple works to the scheduled queue, the next position | |
954 | * needs to be updated. | |
955 | */ | |
956 | if (nextp) | |
957 | *nextp = n; | |
958 | } | |
959 | ||
8864b4e5 TH |
960 | /** |
961 | * get_pwq - get an extra reference on the specified pool_workqueue | |
962 | * @pwq: pool_workqueue to get | |
963 | * | |
964 | * Obtain an extra reference on @pwq. The caller should guarantee that | |
965 | * @pwq has positive refcnt and be holding the matching pool->lock. | |
966 | */ | |
967 | static void get_pwq(struct pool_workqueue *pwq) | |
968 | { | |
969 | lockdep_assert_held(&pwq->pool->lock); | |
970 | WARN_ON_ONCE(pwq->refcnt <= 0); | |
971 | pwq->refcnt++; | |
972 | } | |
973 | ||
974 | /** | |
975 | * put_pwq - put a pool_workqueue reference | |
976 | * @pwq: pool_workqueue to put | |
977 | * | |
978 | * Drop a reference of @pwq. If its refcnt reaches zero, schedule its | |
979 | * destruction. The caller should be holding the matching pool->lock. | |
980 | */ | |
981 | static void put_pwq(struct pool_workqueue *pwq) | |
982 | { | |
983 | lockdep_assert_held(&pwq->pool->lock); | |
984 | if (likely(--pwq->refcnt)) | |
985 | return; | |
986 | if (WARN_ON_ONCE(!(pwq->wq->flags & WQ_UNBOUND))) | |
987 | return; | |
988 | /* | |
989 | * @pwq can't be released under pool->lock, bounce to | |
990 | * pwq_unbound_release_workfn(). This never recurses on the same | |
991 | * pool->lock as this path is taken only for unbound workqueues and | |
992 | * the release work item is scheduled on a per-cpu workqueue. To | |
993 | * avoid lockdep warning, unbound pool->locks are given lockdep | |
994 | * subclass of 1 in get_unbound_pool(). | |
995 | */ | |
996 | schedule_work(&pwq->unbound_release_work); | |
997 | } | |
998 | ||
112202d9 | 999 | static void pwq_activate_delayed_work(struct work_struct *work) |
bf4ede01 | 1000 | { |
112202d9 | 1001 | struct pool_workqueue *pwq = get_work_pwq(work); |
bf4ede01 TH |
1002 | |
1003 | trace_workqueue_activate_work(work); | |
112202d9 | 1004 | move_linked_works(work, &pwq->pool->worklist, NULL); |
bf4ede01 | 1005 | __clear_bit(WORK_STRUCT_DELAYED_BIT, work_data_bits(work)); |
112202d9 | 1006 | pwq->nr_active++; |
bf4ede01 TH |
1007 | } |
1008 | ||
112202d9 | 1009 | static void pwq_activate_first_delayed(struct pool_workqueue *pwq) |
3aa62497 | 1010 | { |
112202d9 | 1011 | struct work_struct *work = list_first_entry(&pwq->delayed_works, |
3aa62497 LJ |
1012 | struct work_struct, entry); |
1013 | ||
112202d9 | 1014 | pwq_activate_delayed_work(work); |
3aa62497 LJ |
1015 | } |
1016 | ||
bf4ede01 | 1017 | /** |
112202d9 TH |
1018 | * pwq_dec_nr_in_flight - decrement pwq's nr_in_flight |
1019 | * @pwq: pwq of interest | |
bf4ede01 | 1020 | * @color: color of work which left the queue |
bf4ede01 TH |
1021 | * |
1022 | * A work either has completed or is removed from pending queue, | |
112202d9 | 1023 | * decrement nr_in_flight of its pwq and handle workqueue flushing. |
bf4ede01 TH |
1024 | * |
1025 | * CONTEXT: | |
d565ed63 | 1026 | * spin_lock_irq(pool->lock). |
bf4ede01 | 1027 | */ |
112202d9 | 1028 | static void pwq_dec_nr_in_flight(struct pool_workqueue *pwq, int color) |
bf4ede01 | 1029 | { |
8864b4e5 | 1030 | /* uncolored work items don't participate in flushing or nr_active */ |
bf4ede01 | 1031 | if (color == WORK_NO_COLOR) |
8864b4e5 | 1032 | goto out_put; |
bf4ede01 | 1033 | |
112202d9 | 1034 | pwq->nr_in_flight[color]--; |
bf4ede01 | 1035 | |
112202d9 TH |
1036 | pwq->nr_active--; |
1037 | if (!list_empty(&pwq->delayed_works)) { | |
b3f9f405 | 1038 | /* one down, submit a delayed one */ |
112202d9 TH |
1039 | if (pwq->nr_active < pwq->max_active) |
1040 | pwq_activate_first_delayed(pwq); | |
bf4ede01 TH |
1041 | } |
1042 | ||
1043 | /* is flush in progress and are we at the flushing tip? */ | |
112202d9 | 1044 | if (likely(pwq->flush_color != color)) |
8864b4e5 | 1045 | goto out_put; |
bf4ede01 TH |
1046 | |
1047 | /* are there still in-flight works? */ | |
112202d9 | 1048 | if (pwq->nr_in_flight[color]) |
8864b4e5 | 1049 | goto out_put; |
bf4ede01 | 1050 | |
112202d9 TH |
1051 | /* this pwq is done, clear flush_color */ |
1052 | pwq->flush_color = -1; | |
bf4ede01 TH |
1053 | |
1054 | /* | |
112202d9 | 1055 | * If this was the last pwq, wake up the first flusher. It |
bf4ede01 TH |
1056 | * will handle the rest. |
1057 | */ | |
112202d9 TH |
1058 | if (atomic_dec_and_test(&pwq->wq->nr_pwqs_to_flush)) |
1059 | complete(&pwq->wq->first_flusher->done); | |
8864b4e5 TH |
1060 | out_put: |
1061 | put_pwq(pwq); | |
bf4ede01 TH |
1062 | } |
1063 | ||
36e227d2 | 1064 | /** |
bbb68dfa | 1065 | * try_to_grab_pending - steal work item from worklist and disable irq |
36e227d2 TH |
1066 | * @work: work item to steal |
1067 | * @is_dwork: @work is a delayed_work | |
bbb68dfa | 1068 | * @flags: place to store irq state |
36e227d2 TH |
1069 | * |
1070 | * Try to grab PENDING bit of @work. This function can handle @work in any | |
1071 | * stable state - idle, on timer or on worklist. Return values are | |
1072 | * | |
1073 | * 1 if @work was pending and we successfully stole PENDING | |
1074 | * 0 if @work was idle and we claimed PENDING | |
1075 | * -EAGAIN if PENDING couldn't be grabbed at the moment, safe to busy-retry | |
bbb68dfa TH |
1076 | * -ENOENT if someone else is canceling @work, this state may persist |
1077 | * for arbitrarily long | |
36e227d2 | 1078 | * |
bbb68dfa | 1079 | * On >= 0 return, the caller owns @work's PENDING bit. To avoid getting |
e0aecdd8 TH |
1080 | * interrupted while holding PENDING and @work off queue, irq must be |
1081 | * disabled on entry. This, combined with delayed_work->timer being | |
1082 | * irqsafe, ensures that we return -EAGAIN for finite short period of time. | |
bbb68dfa TH |
1083 | * |
1084 | * On successful return, >= 0, irq is disabled and the caller is | |
1085 | * responsible for releasing it using local_irq_restore(*@flags). | |
1086 | * | |
e0aecdd8 | 1087 | * This function is safe to call from any context including IRQ handler. |
bf4ede01 | 1088 | */ |
bbb68dfa TH |
1089 | static int try_to_grab_pending(struct work_struct *work, bool is_dwork, |
1090 | unsigned long *flags) | |
bf4ede01 | 1091 | { |
d565ed63 | 1092 | struct worker_pool *pool; |
112202d9 | 1093 | struct pool_workqueue *pwq; |
bf4ede01 | 1094 | |
bbb68dfa TH |
1095 | local_irq_save(*flags); |
1096 | ||
36e227d2 TH |
1097 | /* try to steal the timer if it exists */ |
1098 | if (is_dwork) { | |
1099 | struct delayed_work *dwork = to_delayed_work(work); | |
1100 | ||
e0aecdd8 TH |
1101 | /* |
1102 | * dwork->timer is irqsafe. If del_timer() fails, it's | |
1103 | * guaranteed that the timer is not queued anywhere and not | |
1104 | * running on the local CPU. | |
1105 | */ | |
36e227d2 TH |
1106 | if (likely(del_timer(&dwork->timer))) |
1107 | return 1; | |
1108 | } | |
1109 | ||
1110 | /* try to claim PENDING the normal way */ | |
bf4ede01 TH |
1111 | if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))) |
1112 | return 0; | |
1113 | ||
1114 | /* | |
1115 | * The queueing is in progress, or it is already queued. Try to | |
1116 | * steal it from ->worklist without clearing WORK_STRUCT_PENDING. | |
1117 | */ | |
d565ed63 TH |
1118 | pool = get_work_pool(work); |
1119 | if (!pool) | |
bbb68dfa | 1120 | goto fail; |
bf4ede01 | 1121 | |
d565ed63 | 1122 | spin_lock(&pool->lock); |
0b3dae68 | 1123 | /* |
112202d9 TH |
1124 | * work->data is guaranteed to point to pwq only while the work |
1125 | * item is queued on pwq->wq, and both updating work->data to point | |
1126 | * to pwq on queueing and to pool on dequeueing are done under | |
1127 | * pwq->pool->lock. This in turn guarantees that, if work->data | |
1128 | * points to pwq which is associated with a locked pool, the work | |
0b3dae68 LJ |
1129 | * item is currently queued on that pool. |
1130 | */ | |
112202d9 TH |
1131 | pwq = get_work_pwq(work); |
1132 | if (pwq && pwq->pool == pool) { | |
16062836 TH |
1133 | debug_work_deactivate(work); |
1134 | ||
1135 | /* | |
1136 | * A delayed work item cannot be grabbed directly because | |
1137 | * it might have linked NO_COLOR work items which, if left | |
112202d9 | 1138 | * on the delayed_list, will confuse pwq->nr_active |
16062836 TH |
1139 | * management later on and cause stall. Make sure the work |
1140 | * item is activated before grabbing. | |
1141 | */ | |
1142 | if (*work_data_bits(work) & WORK_STRUCT_DELAYED) | |
112202d9 | 1143 | pwq_activate_delayed_work(work); |
16062836 TH |
1144 | |
1145 | list_del_init(&work->entry); | |
112202d9 | 1146 | pwq_dec_nr_in_flight(get_work_pwq(work), get_work_color(work)); |
16062836 | 1147 | |
112202d9 | 1148 | /* work->data points to pwq iff queued, point to pool */ |
16062836 TH |
1149 | set_work_pool_and_keep_pending(work, pool->id); |
1150 | ||
1151 | spin_unlock(&pool->lock); | |
1152 | return 1; | |
bf4ede01 | 1153 | } |
d565ed63 | 1154 | spin_unlock(&pool->lock); |
bbb68dfa TH |
1155 | fail: |
1156 | local_irq_restore(*flags); | |
1157 | if (work_is_canceling(work)) | |
1158 | return -ENOENT; | |
1159 | cpu_relax(); | |
36e227d2 | 1160 | return -EAGAIN; |
bf4ede01 TH |
1161 | } |
1162 | ||
4690c4ab | 1163 | /** |
706026c2 | 1164 | * insert_work - insert a work into a pool |
112202d9 | 1165 | * @pwq: pwq @work belongs to |
4690c4ab TH |
1166 | * @work: work to insert |
1167 | * @head: insertion point | |
1168 | * @extra_flags: extra WORK_STRUCT_* flags to set | |
1169 | * | |
112202d9 | 1170 | * Insert @work which belongs to @pwq after @head. @extra_flags is or'd to |
706026c2 | 1171 | * work_struct flags. |
4690c4ab TH |
1172 | * |
1173 | * CONTEXT: | |
d565ed63 | 1174 | * spin_lock_irq(pool->lock). |
4690c4ab | 1175 | */ |
112202d9 TH |
1176 | static void insert_work(struct pool_workqueue *pwq, struct work_struct *work, |
1177 | struct list_head *head, unsigned int extra_flags) | |
b89deed3 | 1178 | { |
112202d9 | 1179 | struct worker_pool *pool = pwq->pool; |
e22bee78 | 1180 | |
4690c4ab | 1181 | /* we own @work, set data and link */ |
112202d9 | 1182 | set_work_pwq(work, pwq, extra_flags); |
1a4d9b0a | 1183 | list_add_tail(&work->entry, head); |
8864b4e5 | 1184 | get_pwq(pwq); |
e22bee78 TH |
1185 | |
1186 | /* | |
c5aa87bb TH |
1187 | * Ensure either wq_worker_sleeping() sees the above |
1188 | * list_add_tail() or we see zero nr_running to avoid workers lying | |
1189 | * around lazily while there are works to be processed. | |
e22bee78 TH |
1190 | */ |
1191 | smp_mb(); | |
1192 | ||
63d95a91 TH |
1193 | if (__need_more_worker(pool)) |
1194 | wake_up_worker(pool); | |
b89deed3 ON |
1195 | } |
1196 | ||
c8efcc25 TH |
1197 | /* |
1198 | * Test whether @work is being queued from another work executing on the | |
8d03ecfe | 1199 | * same workqueue. |
c8efcc25 TH |
1200 | */ |
1201 | static bool is_chained_work(struct workqueue_struct *wq) | |
1202 | { | |
8d03ecfe TH |
1203 | struct worker *worker; |
1204 | ||
1205 | worker = current_wq_worker(); | |
1206 | /* | |
1207 | * Return %true iff I'm a worker execuing a work item on @wq. If | |
1208 | * I'm @worker, it's safe to dereference it without locking. | |
1209 | */ | |
112202d9 | 1210 | return worker && worker->current_pwq->wq == wq; |
c8efcc25 TH |
1211 | } |
1212 | ||
d84ff051 | 1213 | static void __queue_work(int cpu, struct workqueue_struct *wq, |
1da177e4 LT |
1214 | struct work_struct *work) |
1215 | { | |
112202d9 | 1216 | struct pool_workqueue *pwq; |
c9178087 | 1217 | struct worker_pool *last_pool; |
1e19ffc6 | 1218 | struct list_head *worklist; |
8a2e8e5d | 1219 | unsigned int work_flags; |
b75cac93 | 1220 | unsigned int req_cpu = cpu; |
8930caba TH |
1221 | |
1222 | /* | |
1223 | * While a work item is PENDING && off queue, a task trying to | |
1224 | * steal the PENDING will busy-loop waiting for it to either get | |
1225 | * queued or lose PENDING. Grabbing PENDING and queueing should | |
1226 | * happen with IRQ disabled. | |
1227 | */ | |
1228 | WARN_ON_ONCE(!irqs_disabled()); | |
1da177e4 | 1229 | |
dc186ad7 | 1230 | debug_work_activate(work); |
1e19ffc6 | 1231 | |
c8efcc25 | 1232 | /* if dying, only works from the same workqueue are allowed */ |
618b01eb | 1233 | if (unlikely(wq->flags & __WQ_DRAINING) && |
c8efcc25 | 1234 | WARN_ON_ONCE(!is_chained_work(wq))) |
e41e704b | 1235 | return; |
9e8cd2f5 | 1236 | retry: |
c9178087 | 1237 | /* pwq which will be used unless @work is executing elsewhere */ |
c7fc77f7 | 1238 | if (!(wq->flags & WQ_UNBOUND)) { |
57469821 | 1239 | if (cpu == WORK_CPU_UNBOUND) |
c7fc77f7 | 1240 | cpu = raw_smp_processor_id(); |
7fb98ea7 | 1241 | pwq = per_cpu_ptr(wq->cpu_pwqs, cpu); |
c9178087 TH |
1242 | } else { |
1243 | pwq = first_pwq(wq); | |
1244 | } | |
dbf2576e | 1245 | |
c9178087 TH |
1246 | /* |
1247 | * If @work was previously on a different pool, it might still be | |
1248 | * running there, in which case the work needs to be queued on that | |
1249 | * pool to guarantee non-reentrancy. | |
1250 | */ | |
1251 | last_pool = get_work_pool(work); | |
1252 | if (last_pool && last_pool != pwq->pool) { | |
1253 | struct worker *worker; | |
18aa9eff | 1254 | |
c9178087 | 1255 | spin_lock(&last_pool->lock); |
18aa9eff | 1256 | |
c9178087 | 1257 | worker = find_worker_executing_work(last_pool, work); |
18aa9eff | 1258 | |
c9178087 TH |
1259 | if (worker && worker->current_pwq->wq == wq) { |
1260 | pwq = worker->current_pwq; | |
8930caba | 1261 | } else { |
c9178087 TH |
1262 | /* meh... not running there, queue here */ |
1263 | spin_unlock(&last_pool->lock); | |
112202d9 | 1264 | spin_lock(&pwq->pool->lock); |
8930caba | 1265 | } |
f3421797 | 1266 | } else { |
112202d9 | 1267 | spin_lock(&pwq->pool->lock); |
502ca9d8 TH |
1268 | } |
1269 | ||
9e8cd2f5 TH |
1270 | /* |
1271 | * pwq is determined and locked. For unbound pools, we could have | |
1272 | * raced with pwq release and it could already be dead. If its | |
1273 | * refcnt is zero, repeat pwq selection. Note that pwqs never die | |
1274 | * without another pwq replacing it as the first pwq or while a | |
1275 | * work item is executing on it, so the retying is guaranteed to | |
1276 | * make forward-progress. | |
1277 | */ | |
1278 | if (unlikely(!pwq->refcnt)) { | |
1279 | if (wq->flags & WQ_UNBOUND) { | |
1280 | spin_unlock(&pwq->pool->lock); | |
1281 | cpu_relax(); | |
1282 | goto retry; | |
1283 | } | |
1284 | /* oops */ | |
1285 | WARN_ONCE(true, "workqueue: per-cpu pwq for %s on cpu%d has 0 refcnt", | |
1286 | wq->name, cpu); | |
1287 | } | |
1288 | ||
112202d9 TH |
1289 | /* pwq determined, queue */ |
1290 | trace_workqueue_queue_work(req_cpu, pwq, work); | |
502ca9d8 | 1291 | |
f5b2552b | 1292 | if (WARN_ON(!list_empty(&work->entry))) { |
112202d9 | 1293 | spin_unlock(&pwq->pool->lock); |
f5b2552b DC |
1294 | return; |
1295 | } | |
1e19ffc6 | 1296 | |
112202d9 TH |
1297 | pwq->nr_in_flight[pwq->work_color]++; |
1298 | work_flags = work_color_to_flags(pwq->work_color); | |
1e19ffc6 | 1299 | |
112202d9 | 1300 | if (likely(pwq->nr_active < pwq->max_active)) { |
cdadf009 | 1301 | trace_workqueue_activate_work(work); |
112202d9 TH |
1302 | pwq->nr_active++; |
1303 | worklist = &pwq->pool->worklist; | |
8a2e8e5d TH |
1304 | } else { |
1305 | work_flags |= WORK_STRUCT_DELAYED; | |
112202d9 | 1306 | worklist = &pwq->delayed_works; |
8a2e8e5d | 1307 | } |
1e19ffc6 | 1308 | |
112202d9 | 1309 | insert_work(pwq, work, worklist, work_flags); |
1e19ffc6 | 1310 | |
112202d9 | 1311 | spin_unlock(&pwq->pool->lock); |
1da177e4 LT |
1312 | } |
1313 | ||
0fcb78c2 | 1314 | /** |
c1a220e7 ZR |
1315 | * queue_work_on - queue work on specific cpu |
1316 | * @cpu: CPU number to execute work on | |
0fcb78c2 REB |
1317 | * @wq: workqueue to use |
1318 | * @work: work to queue | |
1319 | * | |
d4283e93 | 1320 | * Returns %false if @work was already on a queue, %true otherwise. |
1da177e4 | 1321 | * |
c1a220e7 ZR |
1322 | * We queue the work to a specific CPU, the caller must ensure it |
1323 | * can't go away. | |
1da177e4 | 1324 | */ |
d4283e93 TH |
1325 | bool queue_work_on(int cpu, struct workqueue_struct *wq, |
1326 | struct work_struct *work) | |
1da177e4 | 1327 | { |
d4283e93 | 1328 | bool ret = false; |
8930caba | 1329 | unsigned long flags; |
ef1ca236 | 1330 | |
8930caba | 1331 | local_irq_save(flags); |
c1a220e7 | 1332 | |
22df02bb | 1333 | if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))) { |
4690c4ab | 1334 | __queue_work(cpu, wq, work); |
d4283e93 | 1335 | ret = true; |
c1a220e7 | 1336 | } |
ef1ca236 | 1337 | |
8930caba | 1338 | local_irq_restore(flags); |
1da177e4 LT |
1339 | return ret; |
1340 | } | |
c1a220e7 | 1341 | EXPORT_SYMBOL_GPL(queue_work_on); |
1da177e4 | 1342 | |
c1a220e7 | 1343 | /** |
0a13c00e | 1344 | * queue_work - queue work on a workqueue |
c1a220e7 ZR |
1345 | * @wq: workqueue to use |
1346 | * @work: work to queue | |
1347 | * | |
d4283e93 | 1348 | * Returns %false if @work was already on a queue, %true otherwise. |
c1a220e7 | 1349 | * |
0a13c00e TH |
1350 | * We queue the work to the CPU on which it was submitted, but if the CPU dies |
1351 | * it can be processed by another CPU. | |
c1a220e7 | 1352 | */ |
d4283e93 | 1353 | bool queue_work(struct workqueue_struct *wq, struct work_struct *work) |
c1a220e7 | 1354 | { |
57469821 | 1355 | return queue_work_on(WORK_CPU_UNBOUND, wq, work); |
c1a220e7 | 1356 | } |
0a13c00e | 1357 | EXPORT_SYMBOL_GPL(queue_work); |
c1a220e7 | 1358 | |
d8e794df | 1359 | void delayed_work_timer_fn(unsigned long __data) |
1da177e4 | 1360 | { |
52bad64d | 1361 | struct delayed_work *dwork = (struct delayed_work *)__data; |
1da177e4 | 1362 | |
e0aecdd8 | 1363 | /* should have been called from irqsafe timer with irq already off */ |
60c057bc | 1364 | __queue_work(dwork->cpu, dwork->wq, &dwork->work); |
1da177e4 | 1365 | } |
1438ade5 | 1366 | EXPORT_SYMBOL(delayed_work_timer_fn); |
1da177e4 | 1367 | |
7beb2edf TH |
1368 | static void __queue_delayed_work(int cpu, struct workqueue_struct *wq, |
1369 | struct delayed_work *dwork, unsigned long delay) | |
1da177e4 | 1370 | { |
7beb2edf TH |
1371 | struct timer_list *timer = &dwork->timer; |
1372 | struct work_struct *work = &dwork->work; | |
7beb2edf TH |
1373 | |
1374 | WARN_ON_ONCE(timer->function != delayed_work_timer_fn || | |
1375 | timer->data != (unsigned long)dwork); | |
fc4b514f TH |
1376 | WARN_ON_ONCE(timer_pending(timer)); |
1377 | WARN_ON_ONCE(!list_empty(&work->entry)); | |
7beb2edf | 1378 | |
8852aac2 TH |
1379 | /* |
1380 | * If @delay is 0, queue @dwork->work immediately. This is for | |
1381 | * both optimization and correctness. The earliest @timer can | |
1382 | * expire is on the closest next tick and delayed_work users depend | |
1383 | * on that there's no such delay when @delay is 0. | |
1384 | */ | |
1385 | if (!delay) { | |
1386 | __queue_work(cpu, wq, &dwork->work); | |
1387 | return; | |
1388 | } | |
1389 | ||
7beb2edf | 1390 | timer_stats_timer_set_start_info(&dwork->timer); |
1da177e4 | 1391 | |
60c057bc | 1392 | dwork->wq = wq; |
1265057f | 1393 | dwork->cpu = cpu; |
7beb2edf TH |
1394 | timer->expires = jiffies + delay; |
1395 | ||
1396 | if (unlikely(cpu != WORK_CPU_UNBOUND)) | |
1397 | add_timer_on(timer, cpu); | |
1398 | else | |
1399 | add_timer(timer); | |
1da177e4 LT |
1400 | } |
1401 | ||
0fcb78c2 REB |
1402 | /** |
1403 | * queue_delayed_work_on - queue work on specific CPU after delay | |
1404 | * @cpu: CPU number to execute work on | |
1405 | * @wq: workqueue to use | |
af9997e4 | 1406 | * @dwork: work to queue |
0fcb78c2 REB |
1407 | * @delay: number of jiffies to wait before queueing |
1408 | * | |
715f1300 TH |
1409 | * Returns %false if @work was already on a queue, %true otherwise. If |
1410 | * @delay is zero and @dwork is idle, it will be scheduled for immediate | |
1411 | * execution. | |
0fcb78c2 | 1412 | */ |
d4283e93 TH |
1413 | bool queue_delayed_work_on(int cpu, struct workqueue_struct *wq, |
1414 | struct delayed_work *dwork, unsigned long delay) | |
7a6bc1cd | 1415 | { |
52bad64d | 1416 | struct work_struct *work = &dwork->work; |
d4283e93 | 1417 | bool ret = false; |
8930caba | 1418 | unsigned long flags; |
7a6bc1cd | 1419 | |
8930caba TH |
1420 | /* read the comment in __queue_work() */ |
1421 | local_irq_save(flags); | |
7a6bc1cd | 1422 | |
22df02bb | 1423 | if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))) { |
7beb2edf | 1424 | __queue_delayed_work(cpu, wq, dwork, delay); |
d4283e93 | 1425 | ret = true; |
7a6bc1cd | 1426 | } |
8a3e77cc | 1427 | |
8930caba | 1428 | local_irq_restore(flags); |
7a6bc1cd VP |
1429 | return ret; |
1430 | } | |
ae90dd5d | 1431 | EXPORT_SYMBOL_GPL(queue_delayed_work_on); |
c7fc77f7 | 1432 | |
0a13c00e TH |
1433 | /** |
1434 | * queue_delayed_work - queue work on a workqueue after delay | |
1435 | * @wq: workqueue to use | |
1436 | * @dwork: delayable work to queue | |
1437 | * @delay: number of jiffies to wait before queueing | |
1438 | * | |
715f1300 | 1439 | * Equivalent to queue_delayed_work_on() but tries to use the local CPU. |
0a13c00e | 1440 | */ |
d4283e93 | 1441 | bool queue_delayed_work(struct workqueue_struct *wq, |
0a13c00e TH |
1442 | struct delayed_work *dwork, unsigned long delay) |
1443 | { | |
57469821 | 1444 | return queue_delayed_work_on(WORK_CPU_UNBOUND, wq, dwork, delay); |
0a13c00e TH |
1445 | } |
1446 | EXPORT_SYMBOL_GPL(queue_delayed_work); | |
c7fc77f7 | 1447 | |
8376fe22 TH |
1448 | /** |
1449 | * mod_delayed_work_on - modify delay of or queue a delayed work on specific CPU | |
1450 | * @cpu: CPU number to execute work on | |
1451 | * @wq: workqueue to use | |
1452 | * @dwork: work to queue | |
1453 | * @delay: number of jiffies to wait before queueing | |
1454 | * | |
1455 | * If @dwork is idle, equivalent to queue_delayed_work_on(); otherwise, | |
1456 | * modify @dwork's timer so that it expires after @delay. If @delay is | |
1457 | * zero, @work is guaranteed to be scheduled immediately regardless of its | |
1458 | * current state. | |
1459 | * | |
1460 | * Returns %false if @dwork was idle and queued, %true if @dwork was | |
1461 | * pending and its timer was modified. | |
1462 | * | |
e0aecdd8 | 1463 | * This function is safe to call from any context including IRQ handler. |
8376fe22 TH |
1464 | * See try_to_grab_pending() for details. |
1465 | */ | |
1466 | bool mod_delayed_work_on(int cpu, struct workqueue_struct *wq, | |
1467 | struct delayed_work *dwork, unsigned long delay) | |
1468 | { | |
1469 | unsigned long flags; | |
1470 | int ret; | |
c7fc77f7 | 1471 | |
8376fe22 TH |
1472 | do { |
1473 | ret = try_to_grab_pending(&dwork->work, true, &flags); | |
1474 | } while (unlikely(ret == -EAGAIN)); | |
63bc0362 | 1475 | |
8376fe22 TH |
1476 | if (likely(ret >= 0)) { |
1477 | __queue_delayed_work(cpu, wq, dwork, delay); | |
1478 | local_irq_restore(flags); | |
7a6bc1cd | 1479 | } |
8376fe22 TH |
1480 | |
1481 | /* -ENOENT from try_to_grab_pending() becomes %true */ | |
7a6bc1cd VP |
1482 | return ret; |
1483 | } | |
8376fe22 TH |
1484 | EXPORT_SYMBOL_GPL(mod_delayed_work_on); |
1485 | ||
1486 | /** | |
1487 | * mod_delayed_work - modify delay of or queue a delayed work | |
1488 | * @wq: workqueue to use | |
1489 | * @dwork: work to queue | |
1490 | * @delay: number of jiffies to wait before queueing | |
1491 | * | |
1492 | * mod_delayed_work_on() on local CPU. | |
1493 | */ | |
1494 | bool mod_delayed_work(struct workqueue_struct *wq, struct delayed_work *dwork, | |
1495 | unsigned long delay) | |
1496 | { | |
1497 | return mod_delayed_work_on(WORK_CPU_UNBOUND, wq, dwork, delay); | |
1498 | } | |
1499 | EXPORT_SYMBOL_GPL(mod_delayed_work); | |
1da177e4 | 1500 | |
c8e55f36 TH |
1501 | /** |
1502 | * worker_enter_idle - enter idle state | |
1503 | * @worker: worker which is entering idle state | |
1504 | * | |
1505 | * @worker is entering idle state. Update stats and idle timer if | |
1506 | * necessary. | |
1507 | * | |
1508 | * LOCKING: | |
d565ed63 | 1509 | * spin_lock_irq(pool->lock). |
c8e55f36 TH |
1510 | */ |
1511 | static void worker_enter_idle(struct worker *worker) | |
1da177e4 | 1512 | { |
bd7bdd43 | 1513 | struct worker_pool *pool = worker->pool; |
c8e55f36 | 1514 | |
6183c009 TH |
1515 | if (WARN_ON_ONCE(worker->flags & WORKER_IDLE) || |
1516 | WARN_ON_ONCE(!list_empty(&worker->entry) && | |
1517 | (worker->hentry.next || worker->hentry.pprev))) | |
1518 | return; | |
c8e55f36 | 1519 | |
cb444766 TH |
1520 | /* can't use worker_set_flags(), also called from start_worker() */ |
1521 | worker->flags |= WORKER_IDLE; | |
bd7bdd43 | 1522 | pool->nr_idle++; |
e22bee78 | 1523 | worker->last_active = jiffies; |
c8e55f36 TH |
1524 | |
1525 | /* idle_list is LIFO */ | |
bd7bdd43 | 1526 | list_add(&worker->entry, &pool->idle_list); |
db7bccf4 | 1527 | |
628c78e7 TH |
1528 | if (too_many_workers(pool) && !timer_pending(&pool->idle_timer)) |
1529 | mod_timer(&pool->idle_timer, jiffies + IDLE_WORKER_TIMEOUT); | |
cb444766 | 1530 | |
544ecf31 | 1531 | /* |
706026c2 | 1532 | * Sanity check nr_running. Because wq_unbind_fn() releases |
d565ed63 | 1533 | * pool->lock between setting %WORKER_UNBOUND and zapping |
628c78e7 TH |
1534 | * nr_running, the warning may trigger spuriously. Check iff |
1535 | * unbind is not in progress. | |
544ecf31 | 1536 | */ |
24647570 | 1537 | WARN_ON_ONCE(!(pool->flags & POOL_DISASSOCIATED) && |
bd7bdd43 | 1538 | pool->nr_workers == pool->nr_idle && |
e19e397a | 1539 | atomic_read(&pool->nr_running)); |
c8e55f36 TH |
1540 | } |
1541 | ||
1542 | /** | |
1543 | * worker_leave_idle - leave idle state | |
1544 | * @worker: worker which is leaving idle state | |
1545 | * | |
1546 | * @worker is leaving idle state. Update stats. | |
1547 | * | |
1548 | * LOCKING: | |
d565ed63 | 1549 | * spin_lock_irq(pool->lock). |
c8e55f36 TH |
1550 | */ |
1551 | static void worker_leave_idle(struct worker *worker) | |
1552 | { | |
bd7bdd43 | 1553 | struct worker_pool *pool = worker->pool; |
c8e55f36 | 1554 | |
6183c009 TH |
1555 | if (WARN_ON_ONCE(!(worker->flags & WORKER_IDLE))) |
1556 | return; | |
d302f017 | 1557 | worker_clr_flags(worker, WORKER_IDLE); |
bd7bdd43 | 1558 | pool->nr_idle--; |
c8e55f36 TH |
1559 | list_del_init(&worker->entry); |
1560 | } | |
1561 | ||
e22bee78 | 1562 | /** |
f36dc67b LJ |
1563 | * worker_maybe_bind_and_lock - try to bind %current to worker_pool and lock it |
1564 | * @pool: target worker_pool | |
1565 | * | |
1566 | * Bind %current to the cpu of @pool if it is associated and lock @pool. | |
e22bee78 TH |
1567 | * |
1568 | * Works which are scheduled while the cpu is online must at least be | |
1569 | * scheduled to a worker which is bound to the cpu so that if they are | |
1570 | * flushed from cpu callbacks while cpu is going down, they are | |
1571 | * guaranteed to execute on the cpu. | |
1572 | * | |
f5faa077 | 1573 | * This function is to be used by unbound workers and rescuers to bind |
e22bee78 TH |
1574 | * themselves to the target cpu and may race with cpu going down or |
1575 | * coming online. kthread_bind() can't be used because it may put the | |
1576 | * worker to already dead cpu and set_cpus_allowed_ptr() can't be used | |
706026c2 | 1577 | * verbatim as it's best effort and blocking and pool may be |
e22bee78 TH |
1578 | * [dis]associated in the meantime. |
1579 | * | |
706026c2 | 1580 | * This function tries set_cpus_allowed() and locks pool and verifies the |
24647570 | 1581 | * binding against %POOL_DISASSOCIATED which is set during |
f2d5a0ee TH |
1582 | * %CPU_DOWN_PREPARE and cleared during %CPU_ONLINE, so if the worker |
1583 | * enters idle state or fetches works without dropping lock, it can | |
1584 | * guarantee the scheduling requirement described in the first paragraph. | |
e22bee78 TH |
1585 | * |
1586 | * CONTEXT: | |
d565ed63 | 1587 | * Might sleep. Called without any lock but returns with pool->lock |
e22bee78 TH |
1588 | * held. |
1589 | * | |
1590 | * RETURNS: | |
706026c2 | 1591 | * %true if the associated pool is online (@worker is successfully |
e22bee78 TH |
1592 | * bound), %false if offline. |
1593 | */ | |
f36dc67b | 1594 | static bool worker_maybe_bind_and_lock(struct worker_pool *pool) |
d565ed63 | 1595 | __acquires(&pool->lock) |
e22bee78 | 1596 | { |
e22bee78 | 1597 | while (true) { |
4e6045f1 | 1598 | /* |
e22bee78 TH |
1599 | * The following call may fail, succeed or succeed |
1600 | * without actually migrating the task to the cpu if | |
1601 | * it races with cpu hotunplug operation. Verify | |
24647570 | 1602 | * against POOL_DISASSOCIATED. |
4e6045f1 | 1603 | */ |
24647570 | 1604 | if (!(pool->flags & POOL_DISASSOCIATED)) |
7a4e344c | 1605 | set_cpus_allowed_ptr(current, pool->attrs->cpumask); |
e22bee78 | 1606 | |
d565ed63 | 1607 | spin_lock_irq(&pool->lock); |
24647570 | 1608 | if (pool->flags & POOL_DISASSOCIATED) |
e22bee78 | 1609 | return false; |
f5faa077 | 1610 | if (task_cpu(current) == pool->cpu && |
7a4e344c | 1611 | cpumask_equal(¤t->cpus_allowed, pool->attrs->cpumask)) |
e22bee78 | 1612 | return true; |
d565ed63 | 1613 | spin_unlock_irq(&pool->lock); |
e22bee78 | 1614 | |
5035b20f TH |
1615 | /* |
1616 | * We've raced with CPU hot[un]plug. Give it a breather | |
1617 | * and retry migration. cond_resched() is required here; | |
1618 | * otherwise, we might deadlock against cpu_stop trying to | |
1619 | * bring down the CPU on non-preemptive kernel. | |
1620 | */ | |
e22bee78 | 1621 | cpu_relax(); |
5035b20f | 1622 | cond_resched(); |
e22bee78 TH |
1623 | } |
1624 | } | |
1625 | ||
25511a47 | 1626 | /* |
ea1abd61 | 1627 | * Rebind an idle @worker to its CPU. worker_thread() will test |
5f7dabfd | 1628 | * list_empty(@worker->entry) before leaving idle and call this function. |
25511a47 TH |
1629 | */ |
1630 | static void idle_worker_rebind(struct worker *worker) | |
1631 | { | |
5f7dabfd | 1632 | /* CPU may go down again inbetween, clear UNBOUND only on success */ |
f36dc67b | 1633 | if (worker_maybe_bind_and_lock(worker->pool)) |
5f7dabfd | 1634 | worker_clr_flags(worker, WORKER_UNBOUND); |
25511a47 | 1635 | |
ea1abd61 LJ |
1636 | /* rebind complete, become available again */ |
1637 | list_add(&worker->entry, &worker->pool->idle_list); | |
d565ed63 | 1638 | spin_unlock_irq(&worker->pool->lock); |
25511a47 TH |
1639 | } |
1640 | ||
e22bee78 | 1641 | /* |
25511a47 | 1642 | * Function for @worker->rebind.work used to rebind unbound busy workers to |
403c821d TH |
1643 | * the associated cpu which is coming back online. This is scheduled by |
1644 | * cpu up but can race with other cpu hotplug operations and may be | |
1645 | * executed twice without intervening cpu down. | |
e22bee78 | 1646 | */ |
25511a47 | 1647 | static void busy_worker_rebind_fn(struct work_struct *work) |
e22bee78 TH |
1648 | { |
1649 | struct worker *worker = container_of(work, struct worker, rebind_work); | |
e22bee78 | 1650 | |
f36dc67b | 1651 | if (worker_maybe_bind_and_lock(worker->pool)) |
eab6d828 | 1652 | worker_clr_flags(worker, WORKER_UNBOUND); |
e22bee78 | 1653 | |
d565ed63 | 1654 | spin_unlock_irq(&worker->pool->lock); |
e22bee78 TH |
1655 | } |
1656 | ||
25511a47 | 1657 | /** |
94cf58bb TH |
1658 | * rebind_workers - rebind all workers of a pool to the associated CPU |
1659 | * @pool: pool of interest | |
25511a47 | 1660 | * |
94cf58bb | 1661 | * @pool->cpu is coming online. Rebind all workers to the CPU. Rebinding |
25511a47 TH |
1662 | * is different for idle and busy ones. |
1663 | * | |
ea1abd61 LJ |
1664 | * Idle ones will be removed from the idle_list and woken up. They will |
1665 | * add themselves back after completing rebind. This ensures that the | |
1666 | * idle_list doesn't contain any unbound workers when re-bound busy workers | |
1667 | * try to perform local wake-ups for concurrency management. | |
25511a47 | 1668 | * |
ea1abd61 LJ |
1669 | * Busy workers can rebind after they finish their current work items. |
1670 | * Queueing the rebind work item at the head of the scheduled list is | |
1671 | * enough. Note that nr_running will be properly bumped as busy workers | |
1672 | * rebind. | |
25511a47 | 1673 | * |
ea1abd61 LJ |
1674 | * On return, all non-manager workers are scheduled for rebind - see |
1675 | * manage_workers() for the manager special case. Any idle worker | |
1676 | * including the manager will not appear on @idle_list until rebind is | |
1677 | * complete, making local wake-ups safe. | |
25511a47 | 1678 | */ |
94cf58bb | 1679 | static void rebind_workers(struct worker_pool *pool) |
25511a47 | 1680 | { |
ea1abd61 | 1681 | struct worker *worker, *n; |
25511a47 TH |
1682 | int i; |
1683 | ||
94cf58bb TH |
1684 | lockdep_assert_held(&pool->assoc_mutex); |
1685 | lockdep_assert_held(&pool->lock); | |
25511a47 | 1686 | |
5f7dabfd | 1687 | /* dequeue and kick idle ones */ |
94cf58bb TH |
1688 | list_for_each_entry_safe(worker, n, &pool->idle_list, entry) { |
1689 | /* | |
1690 | * idle workers should be off @pool->idle_list until rebind | |
1691 | * is complete to avoid receiving premature local wake-ups. | |
1692 | */ | |
1693 | list_del_init(&worker->entry); | |
25511a47 | 1694 | |
94cf58bb TH |
1695 | /* |
1696 | * worker_thread() will see the above dequeuing and call | |
1697 | * idle_worker_rebind(). | |
1698 | */ | |
1699 | wake_up_process(worker->task); | |
1700 | } | |
25511a47 | 1701 | |
94cf58bb | 1702 | /* rebind busy workers */ |
b67bfe0d | 1703 | for_each_busy_worker(worker, i, pool) { |
94cf58bb TH |
1704 | struct work_struct *rebind_work = &worker->rebind_work; |
1705 | struct workqueue_struct *wq; | |
25511a47 | 1706 | |
94cf58bb TH |
1707 | if (test_and_set_bit(WORK_STRUCT_PENDING_BIT, |
1708 | work_data_bits(rebind_work))) | |
1709 | continue; | |
25511a47 | 1710 | |
94cf58bb | 1711 | debug_work_activate(rebind_work); |
90beca5d | 1712 | |
94cf58bb TH |
1713 | /* |
1714 | * wq doesn't really matter but let's keep @worker->pool | |
112202d9 | 1715 | * and @pwq->pool consistent for sanity. |
94cf58bb | 1716 | */ |
7a4e344c | 1717 | if (worker->pool->attrs->nice < 0) |
94cf58bb TH |
1718 | wq = system_highpri_wq; |
1719 | else | |
1720 | wq = system_wq; | |
1721 | ||
7fb98ea7 | 1722 | insert_work(per_cpu_ptr(wq->cpu_pwqs, pool->cpu), rebind_work, |
94cf58bb TH |
1723 | worker->scheduled.next, |
1724 | work_color_to_flags(WORK_NO_COLOR)); | |
ec58815a | 1725 | } |
25511a47 TH |
1726 | } |
1727 | ||
c34056a3 TH |
1728 | static struct worker *alloc_worker(void) |
1729 | { | |
1730 | struct worker *worker; | |
1731 | ||
1732 | worker = kzalloc(sizeof(*worker), GFP_KERNEL); | |
c8e55f36 TH |
1733 | if (worker) { |
1734 | INIT_LIST_HEAD(&worker->entry); | |
affee4b2 | 1735 | INIT_LIST_HEAD(&worker->scheduled); |
25511a47 | 1736 | INIT_WORK(&worker->rebind_work, busy_worker_rebind_fn); |
e22bee78 TH |
1737 | /* on creation a worker is in !idle && prep state */ |
1738 | worker->flags = WORKER_PREP; | |
c8e55f36 | 1739 | } |
c34056a3 TH |
1740 | return worker; |
1741 | } | |
1742 | ||
1743 | /** | |
1744 | * create_worker - create a new workqueue worker | |
63d95a91 | 1745 | * @pool: pool the new worker will belong to |
c34056a3 | 1746 | * |
63d95a91 | 1747 | * Create a new worker which is bound to @pool. The returned worker |
c34056a3 TH |
1748 | * can be started by calling start_worker() or destroyed using |
1749 | * destroy_worker(). | |
1750 | * | |
1751 | * CONTEXT: | |
1752 | * Might sleep. Does GFP_KERNEL allocations. | |
1753 | * | |
1754 | * RETURNS: | |
1755 | * Pointer to the newly created worker. | |
1756 | */ | |
bc2ae0f5 | 1757 | static struct worker *create_worker(struct worker_pool *pool) |
c34056a3 | 1758 | { |
7a4e344c | 1759 | const char *pri = pool->attrs->nice < 0 ? "H" : ""; |
c34056a3 | 1760 | struct worker *worker = NULL; |
f3421797 | 1761 | int id = -1; |
c34056a3 | 1762 | |
d565ed63 | 1763 | spin_lock_irq(&pool->lock); |
bd7bdd43 | 1764 | while (ida_get_new(&pool->worker_ida, &id)) { |
d565ed63 | 1765 | spin_unlock_irq(&pool->lock); |
bd7bdd43 | 1766 | if (!ida_pre_get(&pool->worker_ida, GFP_KERNEL)) |
c34056a3 | 1767 | goto fail; |
d565ed63 | 1768 | spin_lock_irq(&pool->lock); |
c34056a3 | 1769 | } |
d565ed63 | 1770 | spin_unlock_irq(&pool->lock); |
c34056a3 TH |
1771 | |
1772 | worker = alloc_worker(); | |
1773 | if (!worker) | |
1774 | goto fail; | |
1775 | ||
bd7bdd43 | 1776 | worker->pool = pool; |
c34056a3 TH |
1777 | worker->id = id; |
1778 | ||
29c91e99 | 1779 | if (pool->cpu >= 0) |
94dcf29a | 1780 | worker->task = kthread_create_on_node(worker_thread, |
ec22ca5e | 1781 | worker, cpu_to_node(pool->cpu), |
d84ff051 | 1782 | "kworker/%d:%d%s", pool->cpu, id, pri); |
f3421797 TH |
1783 | else |
1784 | worker->task = kthread_create(worker_thread, worker, | |
ac6104cd TH |
1785 | "kworker/u%d:%d%s", |
1786 | pool->id, id, pri); | |
c34056a3 TH |
1787 | if (IS_ERR(worker->task)) |
1788 | goto fail; | |
1789 | ||
c5aa87bb TH |
1790 | /* |
1791 | * set_cpus_allowed_ptr() will fail if the cpumask doesn't have any | |
1792 | * online CPUs. It'll be re-applied when any of the CPUs come up. | |
1793 | */ | |
7a4e344c TH |
1794 | set_user_nice(worker->task, pool->attrs->nice); |
1795 | set_cpus_allowed_ptr(worker->task, pool->attrs->cpumask); | |
3270476a | 1796 | |
db7bccf4 | 1797 | /* |
7a4e344c TH |
1798 | * %PF_THREAD_BOUND is used to prevent userland from meddling with |
1799 | * cpumask of workqueue workers. This is an abuse. We need | |
1800 | * %PF_NO_SETAFFINITY. | |
db7bccf4 | 1801 | */ |
7a4e344c TH |
1802 | worker->task->flags |= PF_THREAD_BOUND; |
1803 | ||
1804 | /* | |
1805 | * The caller is responsible for ensuring %POOL_DISASSOCIATED | |
1806 | * remains stable across this function. See the comments above the | |
1807 | * flag definition for details. | |
1808 | */ | |
1809 | if (pool->flags & POOL_DISASSOCIATED) | |
bc2ae0f5 | 1810 | worker->flags |= WORKER_UNBOUND; |
c34056a3 TH |
1811 | |
1812 | return worker; | |
1813 | fail: | |
1814 | if (id >= 0) { | |
d565ed63 | 1815 | spin_lock_irq(&pool->lock); |
bd7bdd43 | 1816 | ida_remove(&pool->worker_ida, id); |
d565ed63 | 1817 | spin_unlock_irq(&pool->lock); |
c34056a3 TH |
1818 | } |
1819 | kfree(worker); | |
1820 | return NULL; | |
1821 | } | |
1822 | ||
1823 | /** | |
1824 | * start_worker - start a newly created worker | |
1825 | * @worker: worker to start | |
1826 | * | |
706026c2 | 1827 | * Make the pool aware of @worker and start it. |
c34056a3 TH |
1828 | * |
1829 | * CONTEXT: | |
d565ed63 | 1830 | * spin_lock_irq(pool->lock). |
c34056a3 TH |
1831 | */ |
1832 | static void start_worker(struct worker *worker) | |
1833 | { | |
cb444766 | 1834 | worker->flags |= WORKER_STARTED; |
bd7bdd43 | 1835 | worker->pool->nr_workers++; |
c8e55f36 | 1836 | worker_enter_idle(worker); |
c34056a3 TH |
1837 | wake_up_process(worker->task); |
1838 | } | |
1839 | ||
1840 | /** | |
1841 | * destroy_worker - destroy a workqueue worker | |
1842 | * @worker: worker to be destroyed | |
1843 | * | |
706026c2 | 1844 | * Destroy @worker and adjust @pool stats accordingly. |
c8e55f36 TH |
1845 | * |
1846 | * CONTEXT: | |
d565ed63 | 1847 | * spin_lock_irq(pool->lock) which is released and regrabbed. |
c34056a3 TH |
1848 | */ |
1849 | static void destroy_worker(struct worker *worker) | |
1850 | { | |
bd7bdd43 | 1851 | struct worker_pool *pool = worker->pool; |
c34056a3 TH |
1852 | int id = worker->id; |
1853 | ||
1854 | /* sanity check frenzy */ | |
6183c009 TH |
1855 | if (WARN_ON(worker->current_work) || |
1856 | WARN_ON(!list_empty(&worker->scheduled))) | |
1857 | return; | |
c34056a3 | 1858 | |
c8e55f36 | 1859 | if (worker->flags & WORKER_STARTED) |
bd7bdd43 | 1860 | pool->nr_workers--; |
c8e55f36 | 1861 | if (worker->flags & WORKER_IDLE) |
bd7bdd43 | 1862 | pool->nr_idle--; |
c8e55f36 TH |
1863 | |
1864 | list_del_init(&worker->entry); | |
cb444766 | 1865 | worker->flags |= WORKER_DIE; |
c8e55f36 | 1866 | |
d565ed63 | 1867 | spin_unlock_irq(&pool->lock); |
c8e55f36 | 1868 | |
c34056a3 TH |
1869 | kthread_stop(worker->task); |
1870 | kfree(worker); | |
1871 | ||
d565ed63 | 1872 | spin_lock_irq(&pool->lock); |
bd7bdd43 | 1873 | ida_remove(&pool->worker_ida, id); |
c34056a3 TH |
1874 | } |
1875 | ||
63d95a91 | 1876 | static void idle_worker_timeout(unsigned long __pool) |
e22bee78 | 1877 | { |
63d95a91 | 1878 | struct worker_pool *pool = (void *)__pool; |
e22bee78 | 1879 | |
d565ed63 | 1880 | spin_lock_irq(&pool->lock); |
e22bee78 | 1881 | |
63d95a91 | 1882 | if (too_many_workers(pool)) { |
e22bee78 TH |
1883 | struct worker *worker; |
1884 | unsigned long expires; | |
1885 | ||
1886 | /* idle_list is kept in LIFO order, check the last one */ | |
63d95a91 | 1887 | worker = list_entry(pool->idle_list.prev, struct worker, entry); |
e22bee78 TH |
1888 | expires = worker->last_active + IDLE_WORKER_TIMEOUT; |
1889 | ||
1890 | if (time_before(jiffies, expires)) | |
63d95a91 | 1891 | mod_timer(&pool->idle_timer, expires); |
e22bee78 TH |
1892 | else { |
1893 | /* it's been idle for too long, wake up manager */ | |
11ebea50 | 1894 | pool->flags |= POOL_MANAGE_WORKERS; |
63d95a91 | 1895 | wake_up_worker(pool); |
d5abe669 | 1896 | } |
e22bee78 TH |
1897 | } |
1898 | ||
d565ed63 | 1899 | spin_unlock_irq(&pool->lock); |
e22bee78 | 1900 | } |
d5abe669 | 1901 | |
493a1724 | 1902 | static void send_mayday(struct work_struct *work) |
e22bee78 | 1903 | { |
112202d9 TH |
1904 | struct pool_workqueue *pwq = get_work_pwq(work); |
1905 | struct workqueue_struct *wq = pwq->wq; | |
493a1724 TH |
1906 | |
1907 | lockdep_assert_held(&workqueue_lock); | |
e22bee78 | 1908 | |
493008a8 | 1909 | if (!wq->rescuer) |
493a1724 | 1910 | return; |
e22bee78 TH |
1911 | |
1912 | /* mayday mayday mayday */ | |
493a1724 TH |
1913 | if (list_empty(&pwq->mayday_node)) { |
1914 | list_add_tail(&pwq->mayday_node, &wq->maydays); | |
e22bee78 | 1915 | wake_up_process(wq->rescuer->task); |
493a1724 | 1916 | } |
e22bee78 TH |
1917 | } |
1918 | ||
706026c2 | 1919 | static void pool_mayday_timeout(unsigned long __pool) |
e22bee78 | 1920 | { |
63d95a91 | 1921 | struct worker_pool *pool = (void *)__pool; |
e22bee78 TH |
1922 | struct work_struct *work; |
1923 | ||
493a1724 TH |
1924 | spin_lock_irq(&workqueue_lock); /* for wq->maydays */ |
1925 | spin_lock(&pool->lock); | |
e22bee78 | 1926 | |
63d95a91 | 1927 | if (need_to_create_worker(pool)) { |
e22bee78 TH |
1928 | /* |
1929 | * We've been trying to create a new worker but | |
1930 | * haven't been successful. We might be hitting an | |
1931 | * allocation deadlock. Send distress signals to | |
1932 | * rescuers. | |
1933 | */ | |
63d95a91 | 1934 | list_for_each_entry(work, &pool->worklist, entry) |
e22bee78 | 1935 | send_mayday(work); |
1da177e4 | 1936 | } |
e22bee78 | 1937 | |
493a1724 TH |
1938 | spin_unlock(&pool->lock); |
1939 | spin_unlock_irq(&workqueue_lock); | |
e22bee78 | 1940 | |
63d95a91 | 1941 | mod_timer(&pool->mayday_timer, jiffies + MAYDAY_INTERVAL); |
1da177e4 LT |
1942 | } |
1943 | ||
e22bee78 TH |
1944 | /** |
1945 | * maybe_create_worker - create a new worker if necessary | |
63d95a91 | 1946 | * @pool: pool to create a new worker for |
e22bee78 | 1947 | * |
63d95a91 | 1948 | * Create a new worker for @pool if necessary. @pool is guaranteed to |
e22bee78 TH |
1949 | * have at least one idle worker on return from this function. If |
1950 | * creating a new worker takes longer than MAYDAY_INTERVAL, mayday is | |
63d95a91 | 1951 | * sent to all rescuers with works scheduled on @pool to resolve |
e22bee78 TH |
1952 | * possible allocation deadlock. |
1953 | * | |
c5aa87bb TH |
1954 | * On return, need_to_create_worker() is guaranteed to be %false and |
1955 | * may_start_working() %true. | |
e22bee78 TH |
1956 | * |
1957 | * LOCKING: | |
d565ed63 | 1958 | * spin_lock_irq(pool->lock) which may be released and regrabbed |
e22bee78 TH |
1959 | * multiple times. Does GFP_KERNEL allocations. Called only from |
1960 | * manager. | |
1961 | * | |
1962 | * RETURNS: | |
c5aa87bb | 1963 | * %false if no action was taken and pool->lock stayed locked, %true |
e22bee78 TH |
1964 | * otherwise. |
1965 | */ | |
63d95a91 | 1966 | static bool maybe_create_worker(struct worker_pool *pool) |
d565ed63 TH |
1967 | __releases(&pool->lock) |
1968 | __acquires(&pool->lock) | |
1da177e4 | 1969 | { |
63d95a91 | 1970 | if (!need_to_create_worker(pool)) |
e22bee78 TH |
1971 | return false; |
1972 | restart: | |
d565ed63 | 1973 | spin_unlock_irq(&pool->lock); |
9f9c2364 | 1974 | |
e22bee78 | 1975 | /* if we don't make progress in MAYDAY_INITIAL_TIMEOUT, call for help */ |
63d95a91 | 1976 | mod_timer(&pool->mayday_timer, jiffies + MAYDAY_INITIAL_TIMEOUT); |
e22bee78 TH |
1977 | |
1978 | while (true) { | |
1979 | struct worker *worker; | |
1980 | ||
bc2ae0f5 | 1981 | worker = create_worker(pool); |
e22bee78 | 1982 | if (worker) { |
63d95a91 | 1983 | del_timer_sync(&pool->mayday_timer); |
d565ed63 | 1984 | spin_lock_irq(&pool->lock); |
e22bee78 | 1985 | start_worker(worker); |
6183c009 TH |
1986 | if (WARN_ON_ONCE(need_to_create_worker(pool))) |
1987 | goto restart; | |
e22bee78 TH |
1988 | return true; |
1989 | } | |
1990 | ||
63d95a91 | 1991 | if (!need_to_create_worker(pool)) |
e22bee78 | 1992 | break; |
1da177e4 | 1993 | |
e22bee78 TH |
1994 | __set_current_state(TASK_INTERRUPTIBLE); |
1995 | schedule_timeout(CREATE_COOLDOWN); | |
9f9c2364 | 1996 | |
63d95a91 | 1997 | if (!need_to_create_worker(pool)) |
e22bee78 TH |
1998 | break; |
1999 | } | |
2000 | ||
63d95a91 | 2001 | del_timer_sync(&pool->mayday_timer); |
d565ed63 | 2002 | spin_lock_irq(&pool->lock); |
63d95a91 | 2003 | if (need_to_create_worker(pool)) |
e22bee78 TH |
2004 | goto restart; |
2005 | return true; | |
2006 | } | |
2007 | ||
2008 | /** | |
2009 | * maybe_destroy_worker - destroy workers which have been idle for a while | |
63d95a91 | 2010 | * @pool: pool to destroy workers for |
e22bee78 | 2011 | * |
63d95a91 | 2012 | * Destroy @pool workers which have been idle for longer than |
e22bee78 TH |
2013 | * IDLE_WORKER_TIMEOUT. |
2014 | * | |
2015 | * LOCKING: | |
d565ed63 | 2016 | * spin_lock_irq(pool->lock) which may be released and regrabbed |
e22bee78 TH |
2017 | * multiple times. Called only from manager. |
2018 | * | |
2019 | * RETURNS: | |
c5aa87bb | 2020 | * %false if no action was taken and pool->lock stayed locked, %true |
e22bee78 TH |
2021 | * otherwise. |
2022 | */ | |
63d95a91 | 2023 | static bool maybe_destroy_workers(struct worker_pool *pool) |
e22bee78 TH |
2024 | { |
2025 | bool ret = false; | |
1da177e4 | 2026 | |
63d95a91 | 2027 | while (too_many_workers(pool)) { |
e22bee78 TH |
2028 | struct worker *worker; |
2029 | unsigned long expires; | |
3af24433 | 2030 | |
63d95a91 | 2031 | worker = list_entry(pool->idle_list.prev, struct worker, entry); |
e22bee78 | 2032 | expires = worker->last_active + IDLE_WORKER_TIMEOUT; |
85f4186a | 2033 | |
e22bee78 | 2034 | if (time_before(jiffies, expires)) { |
63d95a91 | 2035 | mod_timer(&pool->idle_timer, expires); |
3af24433 | 2036 | break; |
e22bee78 | 2037 | } |
1da177e4 | 2038 | |
e22bee78 TH |
2039 | destroy_worker(worker); |
2040 | ret = true; | |
1da177e4 | 2041 | } |
1e19ffc6 | 2042 | |
e22bee78 | 2043 | return ret; |
1e19ffc6 TH |
2044 | } |
2045 | ||
73f53c4a | 2046 | /** |
e22bee78 TH |
2047 | * manage_workers - manage worker pool |
2048 | * @worker: self | |
73f53c4a | 2049 | * |
706026c2 | 2050 | * Assume the manager role and manage the worker pool @worker belongs |
e22bee78 | 2051 | * to. At any given time, there can be only zero or one manager per |
706026c2 | 2052 | * pool. The exclusion is handled automatically by this function. |
e22bee78 TH |
2053 | * |
2054 | * The caller can safely start processing works on false return. On | |
2055 | * true return, it's guaranteed that need_to_create_worker() is false | |
2056 | * and may_start_working() is true. | |
73f53c4a TH |
2057 | * |
2058 | * CONTEXT: | |
d565ed63 | 2059 | * spin_lock_irq(pool->lock) which may be released and regrabbed |
e22bee78 TH |
2060 | * multiple times. Does GFP_KERNEL allocations. |
2061 | * | |
2062 | * RETURNS: | |
d565ed63 TH |
2063 | * spin_lock_irq(pool->lock) which may be released and regrabbed |
2064 | * multiple times. Does GFP_KERNEL allocations. | |
73f53c4a | 2065 | */ |
e22bee78 | 2066 | static bool manage_workers(struct worker *worker) |
73f53c4a | 2067 | { |
63d95a91 | 2068 | struct worker_pool *pool = worker->pool; |
e22bee78 | 2069 | bool ret = false; |
73f53c4a | 2070 | |
34a06bd6 | 2071 | if (!mutex_trylock(&pool->manager_arb)) |
e22bee78 | 2072 | return ret; |
1e19ffc6 | 2073 | |
ee378aa4 LJ |
2074 | /* |
2075 | * To simplify both worker management and CPU hotplug, hold off | |
2076 | * management while hotplug is in progress. CPU hotplug path can't | |
34a06bd6 TH |
2077 | * grab @pool->manager_arb to achieve this because that can lead to |
2078 | * idle worker depletion (all become busy thinking someone else is | |
2079 | * managing) which in turn can result in deadlock under extreme | |
2080 | * circumstances. Use @pool->assoc_mutex to synchronize manager | |
2081 | * against CPU hotplug. | |
ee378aa4 | 2082 | * |
b2eb83d1 | 2083 | * assoc_mutex would always be free unless CPU hotplug is in |
d565ed63 | 2084 | * progress. trylock first without dropping @pool->lock. |
ee378aa4 | 2085 | */ |
b2eb83d1 | 2086 | if (unlikely(!mutex_trylock(&pool->assoc_mutex))) { |
d565ed63 | 2087 | spin_unlock_irq(&pool->lock); |
b2eb83d1 | 2088 | mutex_lock(&pool->assoc_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 | * |
b2eb83d1 | 2095 | * As hotplug is now excluded via assoc_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 | |
b2eb83d1 | 2117 | mutex_unlock(&pool->assoc_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 | */ |
e98d5b16 | 2774 | spin_lock_irq(&workqueue_lock); |
9c5a2ba7 | 2775 | if (!wq->nr_drainers++) |
618b01eb | 2776 | wq->flags |= __WQ_DRAINING; |
e98d5b16 | 2777 | spin_unlock_irq(&workqueue_lock); |
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 | ||
76af4d93 | 2802 | spin_lock(&workqueue_lock); |
9c5a2ba7 | 2803 | if (!--wq->nr_drainers) |
618b01eb | 2804 | wq->flags &= ~__WQ_DRAINING; |
76af4d93 TH |
2805 | spin_unlock(&workqueue_lock); |
2806 | ||
2807 | local_irq_enable(); | |
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 | |
0fcb78c2 | 3004 | /** |
c1a220e7 ZR |
3005 | * schedule_work_on - put work task on a specific cpu |
3006 | * @cpu: cpu to put the work task on | |
3007 | * @work: job to be done | |
3008 | * | |
3009 | * This puts a job on a specific cpu | |
3010 | */ | |
d4283e93 | 3011 | bool schedule_work_on(int cpu, struct work_struct *work) |
c1a220e7 | 3012 | { |
d320c038 | 3013 | return queue_work_on(cpu, system_wq, work); |
c1a220e7 ZR |
3014 | } |
3015 | EXPORT_SYMBOL(schedule_work_on); | |
3016 | ||
0fcb78c2 | 3017 | /** |
0fcb78c2 REB |
3018 | * schedule_work - put work task in global workqueue |
3019 | * @work: job to be done | |
0fcb78c2 | 3020 | * |
d4283e93 TH |
3021 | * Returns %false if @work was already on the kernel-global workqueue and |
3022 | * %true otherwise. | |
5b0f437d BVA |
3023 | * |
3024 | * This puts a job in the kernel-global workqueue if it was not already | |
3025 | * queued and leaves it in the same position on the kernel-global | |
3026 | * workqueue otherwise. | |
0fcb78c2 | 3027 | */ |
d4283e93 | 3028 | bool schedule_work(struct work_struct *work) |
1da177e4 | 3029 | { |
d320c038 | 3030 | return queue_work(system_wq, work); |
1da177e4 | 3031 | } |
ae90dd5d | 3032 | EXPORT_SYMBOL(schedule_work); |
1da177e4 | 3033 | |
0fcb78c2 REB |
3034 | /** |
3035 | * schedule_delayed_work_on - queue work in global workqueue on CPU after delay | |
3036 | * @cpu: cpu to use | |
52bad64d | 3037 | * @dwork: job to be done |
0fcb78c2 REB |
3038 | * @delay: number of jiffies to wait |
3039 | * | |
3040 | * After waiting for a given time this puts a job in the kernel-global | |
3041 | * workqueue on the specified CPU. | |
3042 | */ | |
d4283e93 TH |
3043 | bool schedule_delayed_work_on(int cpu, struct delayed_work *dwork, |
3044 | unsigned long delay) | |
1da177e4 | 3045 | { |
d320c038 | 3046 | return queue_delayed_work_on(cpu, system_wq, dwork, delay); |
1da177e4 | 3047 | } |
ae90dd5d | 3048 | EXPORT_SYMBOL(schedule_delayed_work_on); |
1da177e4 | 3049 | |
0fcb78c2 REB |
3050 | /** |
3051 | * schedule_delayed_work - put work task in global workqueue after delay | |
52bad64d DH |
3052 | * @dwork: job to be done |
3053 | * @delay: number of jiffies to wait or 0 for immediate execution | |
0fcb78c2 REB |
3054 | * |
3055 | * After waiting for a given time this puts a job in the kernel-global | |
3056 | * workqueue. | |
3057 | */ | |
d4283e93 | 3058 | bool schedule_delayed_work(struct delayed_work *dwork, unsigned long delay) |
1da177e4 | 3059 | { |
d320c038 | 3060 | return queue_delayed_work(system_wq, dwork, delay); |
1da177e4 | 3061 | } |
ae90dd5d | 3062 | EXPORT_SYMBOL(schedule_delayed_work); |
1da177e4 | 3063 | |
b6136773 | 3064 | /** |
31ddd871 | 3065 | * schedule_on_each_cpu - execute a function synchronously on each online CPU |
b6136773 | 3066 | * @func: the function to call |
b6136773 | 3067 | * |
31ddd871 TH |
3068 | * schedule_on_each_cpu() executes @func on each online CPU using the |
3069 | * system workqueue and blocks until all CPUs have completed. | |
b6136773 | 3070 | * schedule_on_each_cpu() is very slow. |
31ddd871 TH |
3071 | * |
3072 | * RETURNS: | |
3073 | * 0 on success, -errno on failure. | |
b6136773 | 3074 | */ |
65f27f38 | 3075 | int schedule_on_each_cpu(work_func_t func) |
15316ba8 CL |
3076 | { |
3077 | int cpu; | |
38f51568 | 3078 | struct work_struct __percpu *works; |
15316ba8 | 3079 | |
b6136773 AM |
3080 | works = alloc_percpu(struct work_struct); |
3081 | if (!works) | |
15316ba8 | 3082 | return -ENOMEM; |
b6136773 | 3083 | |
93981800 TH |
3084 | get_online_cpus(); |
3085 | ||
15316ba8 | 3086 | for_each_online_cpu(cpu) { |
9bfb1839 IM |
3087 | struct work_struct *work = per_cpu_ptr(works, cpu); |
3088 | ||
3089 | INIT_WORK(work, func); | |
b71ab8c2 | 3090 | schedule_work_on(cpu, work); |
65a64464 | 3091 | } |
93981800 TH |
3092 | |
3093 | for_each_online_cpu(cpu) | |
3094 | flush_work(per_cpu_ptr(works, cpu)); | |
3095 | ||
95402b38 | 3096 | put_online_cpus(); |
b6136773 | 3097 | free_percpu(works); |
15316ba8 CL |
3098 | return 0; |
3099 | } | |
3100 | ||
eef6a7d5 AS |
3101 | /** |
3102 | * flush_scheduled_work - ensure that any scheduled work has run to completion. | |
3103 | * | |
3104 | * Forces execution of the kernel-global workqueue and blocks until its | |
3105 | * completion. | |
3106 | * | |
3107 | * Think twice before calling this function! It's very easy to get into | |
3108 | * trouble if you don't take great care. Either of the following situations | |
3109 | * will lead to deadlock: | |
3110 | * | |
3111 | * One of the work items currently on the workqueue needs to acquire | |
3112 | * a lock held by your code or its caller. | |
3113 | * | |
3114 | * Your code is running in the context of a work routine. | |
3115 | * | |
3116 | * They will be detected by lockdep when they occur, but the first might not | |
3117 | * occur very often. It depends on what work items are on the workqueue and | |
3118 | * what locks they need, which you have no control over. | |
3119 | * | |
3120 | * In most situations flushing the entire workqueue is overkill; you merely | |
3121 | * need to know that a particular work item isn't queued and isn't running. | |
3122 | * In such cases you should use cancel_delayed_work_sync() or | |
3123 | * cancel_work_sync() instead. | |
3124 | */ | |
1da177e4 LT |
3125 | void flush_scheduled_work(void) |
3126 | { | |
d320c038 | 3127 | flush_workqueue(system_wq); |
1da177e4 | 3128 | } |
ae90dd5d | 3129 | EXPORT_SYMBOL(flush_scheduled_work); |
1da177e4 | 3130 | |
1fa44eca JB |
3131 | /** |
3132 | * execute_in_process_context - reliably execute the routine with user context | |
3133 | * @fn: the function to execute | |
1fa44eca JB |
3134 | * @ew: guaranteed storage for the execute work structure (must |
3135 | * be available when the work executes) | |
3136 | * | |
3137 | * Executes the function immediately if process context is available, | |
3138 | * otherwise schedules the function for delayed execution. | |
3139 | * | |
3140 | * Returns: 0 - function was executed | |
3141 | * 1 - function was scheduled for execution | |
3142 | */ | |
65f27f38 | 3143 | int execute_in_process_context(work_func_t fn, struct execute_work *ew) |
1fa44eca JB |
3144 | { |
3145 | if (!in_interrupt()) { | |
65f27f38 | 3146 | fn(&ew->work); |
1fa44eca JB |
3147 | return 0; |
3148 | } | |
3149 | ||
65f27f38 | 3150 | INIT_WORK(&ew->work, fn); |
1fa44eca JB |
3151 | schedule_work(&ew->work); |
3152 | ||
3153 | return 1; | |
3154 | } | |
3155 | EXPORT_SYMBOL_GPL(execute_in_process_context); | |
3156 | ||
1da177e4 LT |
3157 | int keventd_up(void) |
3158 | { | |
d320c038 | 3159 | return system_wq != NULL; |
1da177e4 LT |
3160 | } |
3161 | ||
226223ab TH |
3162 | #ifdef CONFIG_SYSFS |
3163 | /* | |
3164 | * Workqueues with WQ_SYSFS flag set is visible to userland via | |
3165 | * /sys/bus/workqueue/devices/WQ_NAME. All visible workqueues have the | |
3166 | * following attributes. | |
3167 | * | |
3168 | * per_cpu RO bool : whether the workqueue is per-cpu or unbound | |
3169 | * max_active RW int : maximum number of in-flight work items | |
3170 | * | |
3171 | * Unbound workqueues have the following extra attributes. | |
3172 | * | |
3173 | * id RO int : the associated pool ID | |
3174 | * nice RW int : nice value of the workers | |
3175 | * cpumask RW mask : bitmask of allowed CPUs for the workers | |
3176 | */ | |
3177 | struct wq_device { | |
3178 | struct workqueue_struct *wq; | |
3179 | struct device dev; | |
3180 | }; | |
3181 | ||
3182 | static struct workqueue_struct *dev_to_wq(struct device *dev) | |
3183 | { | |
3184 | struct wq_device *wq_dev = container_of(dev, struct wq_device, dev); | |
3185 | ||
3186 | return wq_dev->wq; | |
3187 | } | |
3188 | ||
3189 | static ssize_t wq_per_cpu_show(struct device *dev, | |
3190 | struct device_attribute *attr, char *buf) | |
3191 | { | |
3192 | struct workqueue_struct *wq = dev_to_wq(dev); | |
3193 | ||
3194 | return scnprintf(buf, PAGE_SIZE, "%d\n", (bool)!(wq->flags & WQ_UNBOUND)); | |
3195 | } | |
3196 | ||
3197 | static ssize_t wq_max_active_show(struct device *dev, | |
3198 | struct device_attribute *attr, char *buf) | |
3199 | { | |
3200 | struct workqueue_struct *wq = dev_to_wq(dev); | |
3201 | ||
3202 | return scnprintf(buf, PAGE_SIZE, "%d\n", wq->saved_max_active); | |
3203 | } | |
3204 | ||
3205 | static ssize_t wq_max_active_store(struct device *dev, | |
3206 | struct device_attribute *attr, | |
3207 | const char *buf, size_t count) | |
3208 | { | |
3209 | struct workqueue_struct *wq = dev_to_wq(dev); | |
3210 | int val; | |
3211 | ||
3212 | if (sscanf(buf, "%d", &val) != 1 || val <= 0) | |
3213 | return -EINVAL; | |
3214 | ||
3215 | workqueue_set_max_active(wq, val); | |
3216 | return count; | |
3217 | } | |
3218 | ||
3219 | static struct device_attribute wq_sysfs_attrs[] = { | |
3220 | __ATTR(per_cpu, 0444, wq_per_cpu_show, NULL), | |
3221 | __ATTR(max_active, 0644, wq_max_active_show, wq_max_active_store), | |
3222 | __ATTR_NULL, | |
3223 | }; | |
3224 | ||
3225 | static ssize_t wq_pool_id_show(struct device *dev, | |
3226 | struct device_attribute *attr, char *buf) | |
3227 | { | |
3228 | struct workqueue_struct *wq = dev_to_wq(dev); | |
3229 | struct worker_pool *pool; | |
3230 | int written; | |
3231 | ||
3232 | rcu_read_lock_sched(); | |
3233 | pool = first_pwq(wq)->pool; | |
3234 | written = scnprintf(buf, PAGE_SIZE, "%d\n", pool->id); | |
3235 | rcu_read_unlock_sched(); | |
3236 | ||
3237 | return written; | |
3238 | } | |
3239 | ||
3240 | static ssize_t wq_nice_show(struct device *dev, struct device_attribute *attr, | |
3241 | char *buf) | |
3242 | { | |
3243 | struct workqueue_struct *wq = dev_to_wq(dev); | |
3244 | int written; | |
3245 | ||
3246 | rcu_read_lock_sched(); | |
3247 | written = scnprintf(buf, PAGE_SIZE, "%d\n", | |
3248 | first_pwq(wq)->pool->attrs->nice); | |
3249 | rcu_read_unlock_sched(); | |
3250 | ||
3251 | return written; | |
3252 | } | |
3253 | ||
3254 | /* prepare workqueue_attrs for sysfs store operations */ | |
3255 | static struct workqueue_attrs *wq_sysfs_prep_attrs(struct workqueue_struct *wq) | |
3256 | { | |
3257 | struct workqueue_attrs *attrs; | |
3258 | ||
3259 | attrs = alloc_workqueue_attrs(GFP_KERNEL); | |
3260 | if (!attrs) | |
3261 | return NULL; | |
3262 | ||
3263 | rcu_read_lock_sched(); | |
3264 | copy_workqueue_attrs(attrs, first_pwq(wq)->pool->attrs); | |
3265 | rcu_read_unlock_sched(); | |
3266 | return attrs; | |
3267 | } | |
3268 | ||
3269 | static ssize_t wq_nice_store(struct device *dev, struct device_attribute *attr, | |
3270 | const char *buf, size_t count) | |
3271 | { | |
3272 | struct workqueue_struct *wq = dev_to_wq(dev); | |
3273 | struct workqueue_attrs *attrs; | |
3274 | int ret; | |
3275 | ||
3276 | attrs = wq_sysfs_prep_attrs(wq); | |
3277 | if (!attrs) | |
3278 | return -ENOMEM; | |
3279 | ||
3280 | if (sscanf(buf, "%d", &attrs->nice) == 1 && | |
3281 | attrs->nice >= -20 && attrs->nice <= 19) | |
3282 | ret = apply_workqueue_attrs(wq, attrs); | |
3283 | else | |
3284 | ret = -EINVAL; | |
3285 | ||
3286 | free_workqueue_attrs(attrs); | |
3287 | return ret ?: count; | |
3288 | } | |
3289 | ||
3290 | static ssize_t wq_cpumask_show(struct device *dev, | |
3291 | struct device_attribute *attr, char *buf) | |
3292 | { | |
3293 | struct workqueue_struct *wq = dev_to_wq(dev); | |
3294 | int written; | |
3295 | ||
3296 | rcu_read_lock_sched(); | |
3297 | written = cpumask_scnprintf(buf, PAGE_SIZE, | |
3298 | first_pwq(wq)->pool->attrs->cpumask); | |
3299 | rcu_read_unlock_sched(); | |
3300 | ||
3301 | written += scnprintf(buf + written, PAGE_SIZE - written, "\n"); | |
3302 | return written; | |
3303 | } | |
3304 | ||
3305 | static ssize_t wq_cpumask_store(struct device *dev, | |
3306 | struct device_attribute *attr, | |
3307 | const char *buf, size_t count) | |
3308 | { | |
3309 | struct workqueue_struct *wq = dev_to_wq(dev); | |
3310 | struct workqueue_attrs *attrs; | |
3311 | int ret; | |
3312 | ||
3313 | attrs = wq_sysfs_prep_attrs(wq); | |
3314 | if (!attrs) | |
3315 | return -ENOMEM; | |
3316 | ||
3317 | ret = cpumask_parse(buf, attrs->cpumask); | |
3318 | if (!ret) | |
3319 | ret = apply_workqueue_attrs(wq, attrs); | |
3320 | ||
3321 | free_workqueue_attrs(attrs); | |
3322 | return ret ?: count; | |
3323 | } | |
3324 | ||
3325 | static struct device_attribute wq_sysfs_unbound_attrs[] = { | |
3326 | __ATTR(pool_id, 0444, wq_pool_id_show, NULL), | |
3327 | __ATTR(nice, 0644, wq_nice_show, wq_nice_store), | |
3328 | __ATTR(cpumask, 0644, wq_cpumask_show, wq_cpumask_store), | |
3329 | __ATTR_NULL, | |
3330 | }; | |
3331 | ||
3332 | static struct bus_type wq_subsys = { | |
3333 | .name = "workqueue", | |
3334 | .dev_attrs = wq_sysfs_attrs, | |
3335 | }; | |
3336 | ||
3337 | static int __init wq_sysfs_init(void) | |
3338 | { | |
3339 | return subsys_virtual_register(&wq_subsys, NULL); | |
3340 | } | |
3341 | core_initcall(wq_sysfs_init); | |
3342 | ||
3343 | static void wq_device_release(struct device *dev) | |
3344 | { | |
3345 | struct wq_device *wq_dev = container_of(dev, struct wq_device, dev); | |
3346 | ||
3347 | kfree(wq_dev); | |
3348 | } | |
3349 | ||
3350 | /** | |
3351 | * workqueue_sysfs_register - make a workqueue visible in sysfs | |
3352 | * @wq: the workqueue to register | |
3353 | * | |
3354 | * Expose @wq in sysfs under /sys/bus/workqueue/devices. | |
3355 | * alloc_workqueue*() automatically calls this function if WQ_SYSFS is set | |
3356 | * which is the preferred method. | |
3357 | * | |
3358 | * Workqueue user should use this function directly iff it wants to apply | |
3359 | * workqueue_attrs before making the workqueue visible in sysfs; otherwise, | |
3360 | * apply_workqueue_attrs() may race against userland updating the | |
3361 | * attributes. | |
3362 | * | |
3363 | * Returns 0 on success, -errno on failure. | |
3364 | */ | |
3365 | int workqueue_sysfs_register(struct workqueue_struct *wq) | |
3366 | { | |
3367 | struct wq_device *wq_dev; | |
3368 | int ret; | |
3369 | ||
3370 | /* | |
3371 | * Adjusting max_active or creating new pwqs by applyting | |
3372 | * attributes breaks ordering guarantee. Disallow exposing ordered | |
3373 | * workqueues. | |
3374 | */ | |
3375 | if (WARN_ON(wq->flags & __WQ_ORDERED)) | |
3376 | return -EINVAL; | |
3377 | ||
3378 | wq->wq_dev = wq_dev = kzalloc(sizeof(*wq_dev), GFP_KERNEL); | |
3379 | if (!wq_dev) | |
3380 | return -ENOMEM; | |
3381 | ||
3382 | wq_dev->wq = wq; | |
3383 | wq_dev->dev.bus = &wq_subsys; | |
3384 | wq_dev->dev.init_name = wq->name; | |
3385 | wq_dev->dev.release = wq_device_release; | |
3386 | ||
3387 | /* | |
3388 | * unbound_attrs are created separately. Suppress uevent until | |
3389 | * everything is ready. | |
3390 | */ | |
3391 | dev_set_uevent_suppress(&wq_dev->dev, true); | |
3392 | ||
3393 | ret = device_register(&wq_dev->dev); | |
3394 | if (ret) { | |
3395 | kfree(wq_dev); | |
3396 | wq->wq_dev = NULL; | |
3397 | return ret; | |
3398 | } | |
3399 | ||
3400 | if (wq->flags & WQ_UNBOUND) { | |
3401 | struct device_attribute *attr; | |
3402 | ||
3403 | for (attr = wq_sysfs_unbound_attrs; attr->attr.name; attr++) { | |
3404 | ret = device_create_file(&wq_dev->dev, attr); | |
3405 | if (ret) { | |
3406 | device_unregister(&wq_dev->dev); | |
3407 | wq->wq_dev = NULL; | |
3408 | return ret; | |
3409 | } | |
3410 | } | |
3411 | } | |
3412 | ||
3413 | kobject_uevent(&wq_dev->dev.kobj, KOBJ_ADD); | |
3414 | return 0; | |
3415 | } | |
3416 | ||
3417 | /** | |
3418 | * workqueue_sysfs_unregister - undo workqueue_sysfs_register() | |
3419 | * @wq: the workqueue to unregister | |
3420 | * | |
3421 | * If @wq is registered to sysfs by workqueue_sysfs_register(), unregister. | |
3422 | */ | |
3423 | static void workqueue_sysfs_unregister(struct workqueue_struct *wq) | |
3424 | { | |
3425 | struct wq_device *wq_dev = wq->wq_dev; | |
3426 | ||
3427 | if (!wq->wq_dev) | |
3428 | return; | |
3429 | ||
3430 | wq->wq_dev = NULL; | |
3431 | device_unregister(&wq_dev->dev); | |
3432 | } | |
3433 | #else /* CONFIG_SYSFS */ | |
3434 | static void workqueue_sysfs_unregister(struct workqueue_struct *wq) { } | |
3435 | #endif /* CONFIG_SYSFS */ | |
3436 | ||
7a4e344c TH |
3437 | /** |
3438 | * free_workqueue_attrs - free a workqueue_attrs | |
3439 | * @attrs: workqueue_attrs to free | |
3440 | * | |
3441 | * Undo alloc_workqueue_attrs(). | |
3442 | */ | |
3443 | void free_workqueue_attrs(struct workqueue_attrs *attrs) | |
3444 | { | |
3445 | if (attrs) { | |
3446 | free_cpumask_var(attrs->cpumask); | |
3447 | kfree(attrs); | |
3448 | } | |
3449 | } | |
3450 | ||
3451 | /** | |
3452 | * alloc_workqueue_attrs - allocate a workqueue_attrs | |
3453 | * @gfp_mask: allocation mask to use | |
3454 | * | |
3455 | * Allocate a new workqueue_attrs, initialize with default settings and | |
3456 | * return it. Returns NULL on failure. | |
3457 | */ | |
3458 | struct workqueue_attrs *alloc_workqueue_attrs(gfp_t gfp_mask) | |
3459 | { | |
3460 | struct workqueue_attrs *attrs; | |
3461 | ||
3462 | attrs = kzalloc(sizeof(*attrs), gfp_mask); | |
3463 | if (!attrs) | |
3464 | goto fail; | |
3465 | if (!alloc_cpumask_var(&attrs->cpumask, gfp_mask)) | |
3466 | goto fail; | |
3467 | ||
3468 | cpumask_setall(attrs->cpumask); | |
3469 | return attrs; | |
3470 | fail: | |
3471 | free_workqueue_attrs(attrs); | |
3472 | return NULL; | |
3473 | } | |
3474 | ||
29c91e99 TH |
3475 | static void copy_workqueue_attrs(struct workqueue_attrs *to, |
3476 | const struct workqueue_attrs *from) | |
3477 | { | |
3478 | to->nice = from->nice; | |
3479 | cpumask_copy(to->cpumask, from->cpumask); | |
3480 | } | |
3481 | ||
3482 | /* | |
3483 | * Hacky implementation of jhash of bitmaps which only considers the | |
3484 | * specified number of bits. We probably want a proper implementation in | |
3485 | * include/linux/jhash.h. | |
3486 | */ | |
3487 | static u32 jhash_bitmap(const unsigned long *bitmap, int bits, u32 hash) | |
3488 | { | |
3489 | int nr_longs = bits / BITS_PER_LONG; | |
3490 | int nr_leftover = bits % BITS_PER_LONG; | |
3491 | unsigned long leftover = 0; | |
3492 | ||
3493 | if (nr_longs) | |
3494 | hash = jhash(bitmap, nr_longs * sizeof(long), hash); | |
3495 | if (nr_leftover) { | |
3496 | bitmap_copy(&leftover, bitmap + nr_longs, nr_leftover); | |
3497 | hash = jhash(&leftover, sizeof(long), hash); | |
3498 | } | |
3499 | return hash; | |
3500 | } | |
3501 | ||
3502 | /* hash value of the content of @attr */ | |
3503 | static u32 wqattrs_hash(const struct workqueue_attrs *attrs) | |
3504 | { | |
3505 | u32 hash = 0; | |
3506 | ||
3507 | hash = jhash_1word(attrs->nice, hash); | |
3508 | hash = jhash_bitmap(cpumask_bits(attrs->cpumask), nr_cpu_ids, hash); | |
3509 | return hash; | |
3510 | } | |
3511 | ||
3512 | /* content equality test */ | |
3513 | static bool wqattrs_equal(const struct workqueue_attrs *a, | |
3514 | const struct workqueue_attrs *b) | |
3515 | { | |
3516 | if (a->nice != b->nice) | |
3517 | return false; | |
3518 | if (!cpumask_equal(a->cpumask, b->cpumask)) | |
3519 | return false; | |
3520 | return true; | |
3521 | } | |
3522 | ||
7a4e344c TH |
3523 | /** |
3524 | * init_worker_pool - initialize a newly zalloc'd worker_pool | |
3525 | * @pool: worker_pool to initialize | |
3526 | * | |
3527 | * Initiailize a newly zalloc'd @pool. It also allocates @pool->attrs. | |
29c91e99 TH |
3528 | * Returns 0 on success, -errno on failure. Even on failure, all fields |
3529 | * inside @pool proper are initialized and put_unbound_pool() can be called | |
3530 | * on @pool safely to release it. | |
7a4e344c TH |
3531 | */ |
3532 | static int init_worker_pool(struct worker_pool *pool) | |
4e1a1f9a TH |
3533 | { |
3534 | spin_lock_init(&pool->lock); | |
29c91e99 TH |
3535 | pool->id = -1; |
3536 | pool->cpu = -1; | |
4e1a1f9a TH |
3537 | pool->flags |= POOL_DISASSOCIATED; |
3538 | INIT_LIST_HEAD(&pool->worklist); | |
3539 | INIT_LIST_HEAD(&pool->idle_list); | |
3540 | hash_init(pool->busy_hash); | |
3541 | ||
3542 | init_timer_deferrable(&pool->idle_timer); | |
3543 | pool->idle_timer.function = idle_worker_timeout; | |
3544 | pool->idle_timer.data = (unsigned long)pool; | |
3545 | ||
3546 | setup_timer(&pool->mayday_timer, pool_mayday_timeout, | |
3547 | (unsigned long)pool); | |
3548 | ||
3549 | mutex_init(&pool->manager_arb); | |
3550 | mutex_init(&pool->assoc_mutex); | |
3551 | ida_init(&pool->worker_ida); | |
7a4e344c | 3552 | |
29c91e99 TH |
3553 | INIT_HLIST_NODE(&pool->hash_node); |
3554 | pool->refcnt = 1; | |
3555 | ||
3556 | /* shouldn't fail above this point */ | |
7a4e344c TH |
3557 | pool->attrs = alloc_workqueue_attrs(GFP_KERNEL); |
3558 | if (!pool->attrs) | |
3559 | return -ENOMEM; | |
3560 | return 0; | |
4e1a1f9a TH |
3561 | } |
3562 | ||
29c91e99 TH |
3563 | static void rcu_free_pool(struct rcu_head *rcu) |
3564 | { | |
3565 | struct worker_pool *pool = container_of(rcu, struct worker_pool, rcu); | |
3566 | ||
3567 | ida_destroy(&pool->worker_ida); | |
3568 | free_workqueue_attrs(pool->attrs); | |
3569 | kfree(pool); | |
3570 | } | |
3571 | ||
3572 | /** | |
3573 | * put_unbound_pool - put a worker_pool | |
3574 | * @pool: worker_pool to put | |
3575 | * | |
3576 | * Put @pool. If its refcnt reaches zero, it gets destroyed in sched-RCU | |
c5aa87bb TH |
3577 | * safe manner. get_unbound_pool() calls this function on its failure path |
3578 | * and this function should be able to release pools which went through, | |
3579 | * successfully or not, init_worker_pool(). | |
29c91e99 TH |
3580 | */ |
3581 | static void put_unbound_pool(struct worker_pool *pool) | |
3582 | { | |
3583 | struct worker *worker; | |
3584 | ||
3585 | spin_lock_irq(&workqueue_lock); | |
3586 | if (--pool->refcnt) { | |
3587 | spin_unlock_irq(&workqueue_lock); | |
3588 | return; | |
3589 | } | |
3590 | ||
3591 | /* sanity checks */ | |
3592 | if (WARN_ON(!(pool->flags & POOL_DISASSOCIATED)) || | |
3593 | WARN_ON(!list_empty(&pool->worklist))) { | |
3594 | spin_unlock_irq(&workqueue_lock); | |
3595 | return; | |
3596 | } | |
3597 | ||
3598 | /* release id and unhash */ | |
3599 | if (pool->id >= 0) | |
3600 | idr_remove(&worker_pool_idr, pool->id); | |
3601 | hash_del(&pool->hash_node); | |
3602 | ||
3603 | spin_unlock_irq(&workqueue_lock); | |
3604 | ||
c5aa87bb TH |
3605 | /* |
3606 | * Become the manager and destroy all workers. Grabbing | |
3607 | * manager_arb prevents @pool's workers from blocking on | |
3608 | * manager_mutex. | |
3609 | */ | |
29c91e99 TH |
3610 | mutex_lock(&pool->manager_arb); |
3611 | spin_lock_irq(&pool->lock); | |
3612 | ||
3613 | while ((worker = first_worker(pool))) | |
3614 | destroy_worker(worker); | |
3615 | WARN_ON(pool->nr_workers || pool->nr_idle); | |
3616 | ||
3617 | spin_unlock_irq(&pool->lock); | |
3618 | mutex_unlock(&pool->manager_arb); | |
3619 | ||
3620 | /* shut down the timers */ | |
3621 | del_timer_sync(&pool->idle_timer); | |
3622 | del_timer_sync(&pool->mayday_timer); | |
3623 | ||
3624 | /* sched-RCU protected to allow dereferences from get_work_pool() */ | |
3625 | call_rcu_sched(&pool->rcu, rcu_free_pool); | |
3626 | } | |
3627 | ||
3628 | /** | |
3629 | * get_unbound_pool - get a worker_pool with the specified attributes | |
3630 | * @attrs: the attributes of the worker_pool to get | |
3631 | * | |
3632 | * Obtain a worker_pool which has the same attributes as @attrs, bump the | |
3633 | * reference count and return it. If there already is a matching | |
3634 | * worker_pool, it will be used; otherwise, this function attempts to | |
3635 | * create a new one. On failure, returns NULL. | |
3636 | */ | |
3637 | static struct worker_pool *get_unbound_pool(const struct workqueue_attrs *attrs) | |
3638 | { | |
3639 | static DEFINE_MUTEX(create_mutex); | |
3640 | u32 hash = wqattrs_hash(attrs); | |
3641 | struct worker_pool *pool; | |
3642 | struct worker *worker; | |
3643 | ||
3644 | mutex_lock(&create_mutex); | |
3645 | ||
3646 | /* do we already have a matching pool? */ | |
3647 | spin_lock_irq(&workqueue_lock); | |
3648 | hash_for_each_possible(unbound_pool_hash, pool, hash_node, hash) { | |
3649 | if (wqattrs_equal(pool->attrs, attrs)) { | |
3650 | pool->refcnt++; | |
3651 | goto out_unlock; | |
3652 | } | |
3653 | } | |
3654 | spin_unlock_irq(&workqueue_lock); | |
3655 | ||
3656 | /* nope, create a new one */ | |
3657 | pool = kzalloc(sizeof(*pool), GFP_KERNEL); | |
3658 | if (!pool || init_worker_pool(pool) < 0) | |
3659 | goto fail; | |
3660 | ||
8864b4e5 | 3661 | lockdep_set_subclass(&pool->lock, 1); /* see put_pwq() */ |
29c91e99 TH |
3662 | copy_workqueue_attrs(pool->attrs, attrs); |
3663 | ||
3664 | if (worker_pool_assign_id(pool) < 0) | |
3665 | goto fail; | |
3666 | ||
3667 | /* create and start the initial worker */ | |
3668 | worker = create_worker(pool); | |
3669 | if (!worker) | |
3670 | goto fail; | |
3671 | ||
3672 | spin_lock_irq(&pool->lock); | |
3673 | start_worker(worker); | |
3674 | spin_unlock_irq(&pool->lock); | |
3675 | ||
3676 | /* install */ | |
3677 | spin_lock_irq(&workqueue_lock); | |
3678 | hash_add(unbound_pool_hash, &pool->hash_node, hash); | |
3679 | out_unlock: | |
3680 | spin_unlock_irq(&workqueue_lock); | |
3681 | mutex_unlock(&create_mutex); | |
3682 | return pool; | |
3683 | fail: | |
3684 | mutex_unlock(&create_mutex); | |
3685 | if (pool) | |
3686 | put_unbound_pool(pool); | |
3687 | return NULL; | |
3688 | } | |
3689 | ||
8864b4e5 TH |
3690 | static void rcu_free_pwq(struct rcu_head *rcu) |
3691 | { | |
3692 | kmem_cache_free(pwq_cache, | |
3693 | container_of(rcu, struct pool_workqueue, rcu)); | |
3694 | } | |
3695 | ||
3696 | /* | |
3697 | * Scheduled on system_wq by put_pwq() when an unbound pwq hits zero refcnt | |
3698 | * and needs to be destroyed. | |
3699 | */ | |
3700 | static void pwq_unbound_release_workfn(struct work_struct *work) | |
3701 | { | |
3702 | struct pool_workqueue *pwq = container_of(work, struct pool_workqueue, | |
3703 | unbound_release_work); | |
3704 | struct workqueue_struct *wq = pwq->wq; | |
3705 | struct worker_pool *pool = pwq->pool; | |
3706 | ||
3707 | if (WARN_ON_ONCE(!(wq->flags & WQ_UNBOUND))) | |
3708 | return; | |
3709 | ||
75ccf595 TH |
3710 | /* |
3711 | * Unlink @pwq. Synchronization against flush_mutex isn't strictly | |
3712 | * necessary on release but do it anyway. It's easier to verify | |
3713 | * and consistent with the linking path. | |
3714 | */ | |
3715 | mutex_lock(&wq->flush_mutex); | |
8864b4e5 TH |
3716 | spin_lock_irq(&workqueue_lock); |
3717 | list_del_rcu(&pwq->pwqs_node); | |
3718 | spin_unlock_irq(&workqueue_lock); | |
75ccf595 | 3719 | mutex_unlock(&wq->flush_mutex); |
8864b4e5 TH |
3720 | |
3721 | put_unbound_pool(pool); | |
3722 | call_rcu_sched(&pwq->rcu, rcu_free_pwq); | |
3723 | ||
3724 | /* | |
3725 | * If we're the last pwq going away, @wq is already dead and no one | |
3726 | * is gonna access it anymore. Free it. | |
3727 | */ | |
3728 | if (list_empty(&wq->pwqs)) | |
3729 | kfree(wq); | |
3730 | } | |
3731 | ||
0fbd95aa | 3732 | /** |
699ce097 | 3733 | * pwq_adjust_max_active - update a pwq's max_active to the current setting |
0fbd95aa | 3734 | * @pwq: target pool_workqueue |
0fbd95aa | 3735 | * |
699ce097 TH |
3736 | * If @pwq isn't freezing, set @pwq->max_active to the associated |
3737 | * workqueue's saved_max_active and activate delayed work items | |
3738 | * accordingly. If @pwq is freezing, clear @pwq->max_active to zero. | |
0fbd95aa | 3739 | */ |
699ce097 | 3740 | static void pwq_adjust_max_active(struct pool_workqueue *pwq) |
0fbd95aa | 3741 | { |
699ce097 TH |
3742 | struct workqueue_struct *wq = pwq->wq; |
3743 | bool freezable = wq->flags & WQ_FREEZABLE; | |
3744 | ||
3745 | /* for @wq->saved_max_active */ | |
3746 | lockdep_assert_held(&workqueue_lock); | |
3747 | ||
3748 | /* fast exit for non-freezable wqs */ | |
3749 | if (!freezable && pwq->max_active == wq->saved_max_active) | |
3750 | return; | |
3751 | ||
3752 | spin_lock(&pwq->pool->lock); | |
3753 | ||
3754 | if (!freezable || !(pwq->pool->flags & POOL_FREEZING)) { | |
3755 | pwq->max_active = wq->saved_max_active; | |
0fbd95aa | 3756 | |
699ce097 TH |
3757 | while (!list_empty(&pwq->delayed_works) && |
3758 | pwq->nr_active < pwq->max_active) | |
3759 | pwq_activate_first_delayed(pwq); | |
3760 | } else { | |
3761 | pwq->max_active = 0; | |
3762 | } | |
3763 | ||
3764 | spin_unlock(&pwq->pool->lock); | |
0fbd95aa TH |
3765 | } |
3766 | ||
d2c1d404 TH |
3767 | static void init_and_link_pwq(struct pool_workqueue *pwq, |
3768 | struct workqueue_struct *wq, | |
9e8cd2f5 TH |
3769 | struct worker_pool *pool, |
3770 | struct pool_workqueue **p_last_pwq) | |
d2c1d404 TH |
3771 | { |
3772 | BUG_ON((unsigned long)pwq & WORK_STRUCT_FLAG_MASK); | |
3773 | ||
3774 | pwq->pool = pool; | |
3775 | pwq->wq = wq; | |
3776 | pwq->flush_color = -1; | |
8864b4e5 | 3777 | pwq->refcnt = 1; |
d2c1d404 TH |
3778 | INIT_LIST_HEAD(&pwq->delayed_works); |
3779 | INIT_LIST_HEAD(&pwq->mayday_node); | |
8864b4e5 | 3780 | INIT_WORK(&pwq->unbound_release_work, pwq_unbound_release_workfn); |
d2c1d404 | 3781 | |
75ccf595 TH |
3782 | mutex_lock(&wq->flush_mutex); |
3783 | spin_lock_irq(&workqueue_lock); | |
3784 | ||
983ca25e TH |
3785 | /* |
3786 | * Set the matching work_color. This is synchronized with | |
3787 | * flush_mutex to avoid confusing flush_workqueue(). | |
3788 | */ | |
9e8cd2f5 TH |
3789 | if (p_last_pwq) |
3790 | *p_last_pwq = first_pwq(wq); | |
75ccf595 | 3791 | pwq->work_color = wq->work_color; |
983ca25e TH |
3792 | |
3793 | /* sync max_active to the current setting */ | |
3794 | pwq_adjust_max_active(pwq); | |
3795 | ||
3796 | /* link in @pwq */ | |
9e8cd2f5 | 3797 | list_add_rcu(&pwq->pwqs_node, &wq->pwqs); |
75ccf595 TH |
3798 | |
3799 | spin_unlock_irq(&workqueue_lock); | |
3800 | mutex_unlock(&wq->flush_mutex); | |
d2c1d404 TH |
3801 | } |
3802 | ||
9e8cd2f5 TH |
3803 | /** |
3804 | * apply_workqueue_attrs - apply new workqueue_attrs to an unbound workqueue | |
3805 | * @wq: the target workqueue | |
3806 | * @attrs: the workqueue_attrs to apply, allocated with alloc_workqueue_attrs() | |
3807 | * | |
3808 | * Apply @attrs to an unbound workqueue @wq. If @attrs doesn't match the | |
3809 | * current attributes, a new pwq is created and made the first pwq which | |
3810 | * will serve all new work items. Older pwqs are released as in-flight | |
3811 | * work items finish. Note that a work item which repeatedly requeues | |
3812 | * itself back-to-back will stay on its current pwq. | |
3813 | * | |
3814 | * Performs GFP_KERNEL allocations. Returns 0 on success and -errno on | |
3815 | * failure. | |
3816 | */ | |
3817 | int apply_workqueue_attrs(struct workqueue_struct *wq, | |
3818 | const struct workqueue_attrs *attrs) | |
3819 | { | |
3820 | struct pool_workqueue *pwq, *last_pwq; | |
3821 | struct worker_pool *pool; | |
3822 | ||
8719dcea | 3823 | /* only unbound workqueues can change attributes */ |
9e8cd2f5 TH |
3824 | if (WARN_ON(!(wq->flags & WQ_UNBOUND))) |
3825 | return -EINVAL; | |
3826 | ||
8719dcea TH |
3827 | /* creating multiple pwqs breaks ordering guarantee */ |
3828 | if (WARN_ON((wq->flags & __WQ_ORDERED) && !list_empty(&wq->pwqs))) | |
3829 | return -EINVAL; | |
3830 | ||
9e8cd2f5 TH |
3831 | pwq = kmem_cache_zalloc(pwq_cache, GFP_KERNEL); |
3832 | if (!pwq) | |
3833 | return -ENOMEM; | |
3834 | ||
3835 | pool = get_unbound_pool(attrs); | |
3836 | if (!pool) { | |
3837 | kmem_cache_free(pwq_cache, pwq); | |
3838 | return -ENOMEM; | |
3839 | } | |
3840 | ||
3841 | init_and_link_pwq(pwq, wq, pool, &last_pwq); | |
3842 | if (last_pwq) { | |
3843 | spin_lock_irq(&last_pwq->pool->lock); | |
3844 | put_pwq(last_pwq); | |
3845 | spin_unlock_irq(&last_pwq->pool->lock); | |
3846 | } | |
3847 | ||
3848 | return 0; | |
3849 | } | |
3850 | ||
30cdf249 | 3851 | static int alloc_and_link_pwqs(struct workqueue_struct *wq) |
0f900049 | 3852 | { |
49e3cf44 | 3853 | bool highpri = wq->flags & WQ_HIGHPRI; |
30cdf249 TH |
3854 | int cpu; |
3855 | ||
3856 | if (!(wq->flags & WQ_UNBOUND)) { | |
420c0ddb TH |
3857 | wq->cpu_pwqs = alloc_percpu(struct pool_workqueue); |
3858 | if (!wq->cpu_pwqs) | |
30cdf249 TH |
3859 | return -ENOMEM; |
3860 | ||
3861 | for_each_possible_cpu(cpu) { | |
7fb98ea7 TH |
3862 | struct pool_workqueue *pwq = |
3863 | per_cpu_ptr(wq->cpu_pwqs, cpu); | |
7a62c2c8 | 3864 | struct worker_pool *cpu_pools = |
f02ae73a | 3865 | per_cpu(cpu_worker_pools, cpu); |
f3421797 | 3866 | |
9e8cd2f5 | 3867 | init_and_link_pwq(pwq, wq, &cpu_pools[highpri], NULL); |
30cdf249 | 3868 | } |
9e8cd2f5 | 3869 | return 0; |
30cdf249 | 3870 | } else { |
9e8cd2f5 | 3871 | return apply_workqueue_attrs(wq, unbound_std_wq_attrs[highpri]); |
30cdf249 | 3872 | } |
0f900049 TH |
3873 | } |
3874 | ||
f3421797 TH |
3875 | static int wq_clamp_max_active(int max_active, unsigned int flags, |
3876 | const char *name) | |
b71ab8c2 | 3877 | { |
f3421797 TH |
3878 | int lim = flags & WQ_UNBOUND ? WQ_UNBOUND_MAX_ACTIVE : WQ_MAX_ACTIVE; |
3879 | ||
3880 | if (max_active < 1 || max_active > lim) | |
044c782c VI |
3881 | pr_warn("workqueue: max_active %d requested for %s is out of range, clamping between %d and %d\n", |
3882 | max_active, name, 1, lim); | |
b71ab8c2 | 3883 | |
f3421797 | 3884 | return clamp_val(max_active, 1, lim); |
b71ab8c2 TH |
3885 | } |
3886 | ||
b196be89 | 3887 | struct workqueue_struct *__alloc_workqueue_key(const char *fmt, |
d320c038 TH |
3888 | unsigned int flags, |
3889 | int max_active, | |
3890 | struct lock_class_key *key, | |
b196be89 | 3891 | const char *lock_name, ...) |
1da177e4 | 3892 | { |
b196be89 | 3893 | va_list args, args1; |
1da177e4 | 3894 | struct workqueue_struct *wq; |
49e3cf44 | 3895 | struct pool_workqueue *pwq; |
b196be89 TH |
3896 | size_t namelen; |
3897 | ||
3898 | /* determine namelen, allocate wq and format name */ | |
3899 | va_start(args, lock_name); | |
3900 | va_copy(args1, args); | |
3901 | namelen = vsnprintf(NULL, 0, fmt, args) + 1; | |
3902 | ||
3903 | wq = kzalloc(sizeof(*wq) + namelen, GFP_KERNEL); | |
3904 | if (!wq) | |
d2c1d404 | 3905 | return NULL; |
b196be89 TH |
3906 | |
3907 | vsnprintf(wq->name, namelen, fmt, args1); | |
3908 | va_end(args); | |
3909 | va_end(args1); | |
1da177e4 | 3910 | |
d320c038 | 3911 | max_active = max_active ?: WQ_DFL_ACTIVE; |
b196be89 | 3912 | max_active = wq_clamp_max_active(max_active, flags, wq->name); |
3af24433 | 3913 | |
b196be89 | 3914 | /* init wq */ |
97e37d7b | 3915 | wq->flags = flags; |
a0a1a5fd | 3916 | wq->saved_max_active = max_active; |
73f53c4a | 3917 | mutex_init(&wq->flush_mutex); |
112202d9 | 3918 | atomic_set(&wq->nr_pwqs_to_flush, 0); |
30cdf249 | 3919 | INIT_LIST_HEAD(&wq->pwqs); |
73f53c4a TH |
3920 | INIT_LIST_HEAD(&wq->flusher_queue); |
3921 | INIT_LIST_HEAD(&wq->flusher_overflow); | |
493a1724 | 3922 | INIT_LIST_HEAD(&wq->maydays); |
502ca9d8 | 3923 | |
eb13ba87 | 3924 | lockdep_init_map(&wq->lockdep_map, lock_name, key, 0); |
cce1a165 | 3925 | INIT_LIST_HEAD(&wq->list); |
3af24433 | 3926 | |
30cdf249 | 3927 | if (alloc_and_link_pwqs(wq) < 0) |
d2c1d404 | 3928 | goto err_free_wq; |
1537663f | 3929 | |
493008a8 TH |
3930 | /* |
3931 | * Workqueues which may be used during memory reclaim should | |
3932 | * have a rescuer to guarantee forward progress. | |
3933 | */ | |
3934 | if (flags & WQ_MEM_RECLAIM) { | |
e22bee78 TH |
3935 | struct worker *rescuer; |
3936 | ||
d2c1d404 | 3937 | rescuer = alloc_worker(); |
e22bee78 | 3938 | if (!rescuer) |
d2c1d404 | 3939 | goto err_destroy; |
e22bee78 | 3940 | |
111c225a TH |
3941 | rescuer->rescue_wq = wq; |
3942 | rescuer->task = kthread_create(rescuer_thread, rescuer, "%s", | |
b196be89 | 3943 | wq->name); |
d2c1d404 TH |
3944 | if (IS_ERR(rescuer->task)) { |
3945 | kfree(rescuer); | |
3946 | goto err_destroy; | |
3947 | } | |
e22bee78 | 3948 | |
d2c1d404 | 3949 | wq->rescuer = rescuer; |
e22bee78 TH |
3950 | rescuer->task->flags |= PF_THREAD_BOUND; |
3951 | wake_up_process(rescuer->task); | |
3af24433 ON |
3952 | } |
3953 | ||
226223ab TH |
3954 | if ((wq->flags & WQ_SYSFS) && workqueue_sysfs_register(wq)) |
3955 | goto err_destroy; | |
3956 | ||
a0a1a5fd | 3957 | /* |
699ce097 TH |
3958 | * workqueue_lock protects global freeze state and workqueues list. |
3959 | * Grab it, adjust max_active and add the new workqueue to | |
3960 | * workqueues list. | |
a0a1a5fd | 3961 | */ |
e98d5b16 | 3962 | spin_lock_irq(&workqueue_lock); |
a0a1a5fd | 3963 | |
699ce097 TH |
3964 | for_each_pwq(pwq, wq) |
3965 | pwq_adjust_max_active(pwq); | |
a0a1a5fd | 3966 | |
1537663f | 3967 | list_add(&wq->list, &workqueues); |
a0a1a5fd | 3968 | |
e98d5b16 | 3969 | spin_unlock_irq(&workqueue_lock); |
1537663f | 3970 | |
3af24433 | 3971 | return wq; |
d2c1d404 TH |
3972 | |
3973 | err_free_wq: | |
3974 | kfree(wq); | |
3975 | return NULL; | |
3976 | err_destroy: | |
3977 | destroy_workqueue(wq); | |
4690c4ab | 3978 | return NULL; |
3af24433 | 3979 | } |
d320c038 | 3980 | EXPORT_SYMBOL_GPL(__alloc_workqueue_key); |
1da177e4 | 3981 | |
3af24433 ON |
3982 | /** |
3983 | * destroy_workqueue - safely terminate a workqueue | |
3984 | * @wq: target workqueue | |
3985 | * | |
3986 | * Safely destroy a workqueue. All work currently pending will be done first. | |
3987 | */ | |
3988 | void destroy_workqueue(struct workqueue_struct *wq) | |
3989 | { | |
49e3cf44 | 3990 | struct pool_workqueue *pwq; |
3af24433 | 3991 | |
9c5a2ba7 TH |
3992 | /* drain it before proceeding with destruction */ |
3993 | drain_workqueue(wq); | |
c8efcc25 | 3994 | |
76af4d93 TH |
3995 | spin_lock_irq(&workqueue_lock); |
3996 | ||
6183c009 | 3997 | /* sanity checks */ |
49e3cf44 | 3998 | for_each_pwq(pwq, wq) { |
6183c009 TH |
3999 | int i; |
4000 | ||
76af4d93 TH |
4001 | for (i = 0; i < WORK_NR_COLORS; i++) { |
4002 | if (WARN_ON(pwq->nr_in_flight[i])) { | |
4003 | spin_unlock_irq(&workqueue_lock); | |
6183c009 | 4004 | return; |
76af4d93 TH |
4005 | } |
4006 | } | |
4007 | ||
8864b4e5 TH |
4008 | if (WARN_ON(pwq->refcnt > 1) || |
4009 | WARN_ON(pwq->nr_active) || | |
76af4d93 TH |
4010 | WARN_ON(!list_empty(&pwq->delayed_works))) { |
4011 | spin_unlock_irq(&workqueue_lock); | |
6183c009 | 4012 | return; |
76af4d93 | 4013 | } |
6183c009 TH |
4014 | } |
4015 | ||
a0a1a5fd TH |
4016 | /* |
4017 | * wq list is used to freeze wq, remove from list after | |
4018 | * flushing is complete in case freeze races us. | |
4019 | */ | |
d2c1d404 | 4020 | list_del_init(&wq->list); |
76af4d93 | 4021 | |
e98d5b16 | 4022 | spin_unlock_irq(&workqueue_lock); |
3af24433 | 4023 | |
226223ab TH |
4024 | workqueue_sysfs_unregister(wq); |
4025 | ||
493008a8 | 4026 | if (wq->rescuer) { |
e22bee78 | 4027 | kthread_stop(wq->rescuer->task); |
8d9df9f0 | 4028 | kfree(wq->rescuer); |
493008a8 | 4029 | wq->rescuer = NULL; |
e22bee78 TH |
4030 | } |
4031 | ||
8864b4e5 TH |
4032 | if (!(wq->flags & WQ_UNBOUND)) { |
4033 | /* | |
4034 | * The base ref is never dropped on per-cpu pwqs. Directly | |
4035 | * free the pwqs and wq. | |
4036 | */ | |
4037 | free_percpu(wq->cpu_pwqs); | |
4038 | kfree(wq); | |
4039 | } else { | |
4040 | /* | |
4041 | * We're the sole accessor of @wq at this point. Directly | |
4042 | * access the first pwq and put the base ref. As both pwqs | |
4043 | * and pools are sched-RCU protected, the lock operations | |
4044 | * are safe. @wq will be freed when the last pwq is | |
4045 | * released. | |
4046 | */ | |
29c91e99 TH |
4047 | pwq = list_first_entry(&wq->pwqs, struct pool_workqueue, |
4048 | pwqs_node); | |
8864b4e5 TH |
4049 | spin_lock_irq(&pwq->pool->lock); |
4050 | put_pwq(pwq); | |
4051 | spin_unlock_irq(&pwq->pool->lock); | |
29c91e99 | 4052 | } |
3af24433 ON |
4053 | } |
4054 | EXPORT_SYMBOL_GPL(destroy_workqueue); | |
4055 | ||
dcd989cb TH |
4056 | /** |
4057 | * workqueue_set_max_active - adjust max_active of a workqueue | |
4058 | * @wq: target workqueue | |
4059 | * @max_active: new max_active value. | |
4060 | * | |
4061 | * Set max_active of @wq to @max_active. | |
4062 | * | |
4063 | * CONTEXT: | |
4064 | * Don't call from IRQ context. | |
4065 | */ | |
4066 | void workqueue_set_max_active(struct workqueue_struct *wq, int max_active) | |
4067 | { | |
49e3cf44 | 4068 | struct pool_workqueue *pwq; |
dcd989cb | 4069 | |
8719dcea TH |
4070 | /* disallow meddling with max_active for ordered workqueues */ |
4071 | if (WARN_ON(wq->flags & __WQ_ORDERED)) | |
4072 | return; | |
4073 | ||
f3421797 | 4074 | max_active = wq_clamp_max_active(max_active, wq->flags, wq->name); |
dcd989cb | 4075 | |
e98d5b16 | 4076 | spin_lock_irq(&workqueue_lock); |
dcd989cb TH |
4077 | |
4078 | wq->saved_max_active = max_active; | |
4079 | ||
699ce097 TH |
4080 | for_each_pwq(pwq, wq) |
4081 | pwq_adjust_max_active(pwq); | |
93981800 | 4082 | |
e98d5b16 | 4083 | spin_unlock_irq(&workqueue_lock); |
15316ba8 | 4084 | } |
dcd989cb | 4085 | EXPORT_SYMBOL_GPL(workqueue_set_max_active); |
15316ba8 | 4086 | |
e6267616 TH |
4087 | /** |
4088 | * current_is_workqueue_rescuer - is %current workqueue rescuer? | |
4089 | * | |
4090 | * Determine whether %current is a workqueue rescuer. Can be used from | |
4091 | * work functions to determine whether it's being run off the rescuer task. | |
4092 | */ | |
4093 | bool current_is_workqueue_rescuer(void) | |
4094 | { | |
4095 | struct worker *worker = current_wq_worker(); | |
4096 | ||
4097 | return worker && worker == worker->current_pwq->wq->rescuer; | |
4098 | } | |
4099 | ||
eef6a7d5 | 4100 | /** |
dcd989cb TH |
4101 | * workqueue_congested - test whether a workqueue is congested |
4102 | * @cpu: CPU in question | |
4103 | * @wq: target workqueue | |
eef6a7d5 | 4104 | * |
dcd989cb TH |
4105 | * Test whether @wq's cpu workqueue for @cpu is congested. There is |
4106 | * no synchronization around this function and the test result is | |
4107 | * unreliable and only useful as advisory hints or for debugging. | |
eef6a7d5 | 4108 | * |
dcd989cb TH |
4109 | * RETURNS: |
4110 | * %true if congested, %false otherwise. | |
eef6a7d5 | 4111 | */ |
d84ff051 | 4112 | bool workqueue_congested(int cpu, struct workqueue_struct *wq) |
1da177e4 | 4113 | { |
7fb98ea7 | 4114 | struct pool_workqueue *pwq; |
76af4d93 TH |
4115 | bool ret; |
4116 | ||
4117 | preempt_disable(); | |
7fb98ea7 TH |
4118 | |
4119 | if (!(wq->flags & WQ_UNBOUND)) | |
4120 | pwq = per_cpu_ptr(wq->cpu_pwqs, cpu); | |
4121 | else | |
4122 | pwq = first_pwq(wq); | |
dcd989cb | 4123 | |
76af4d93 TH |
4124 | ret = !list_empty(&pwq->delayed_works); |
4125 | preempt_enable(); | |
4126 | ||
4127 | return ret; | |
1da177e4 | 4128 | } |
dcd989cb | 4129 | EXPORT_SYMBOL_GPL(workqueue_congested); |
1da177e4 | 4130 | |
dcd989cb TH |
4131 | /** |
4132 | * work_busy - test whether a work is currently pending or running | |
4133 | * @work: the work to be tested | |
4134 | * | |
4135 | * Test whether @work is currently pending or running. There is no | |
4136 | * synchronization around this function and the test result is | |
4137 | * unreliable and only useful as advisory hints or for debugging. | |
dcd989cb TH |
4138 | * |
4139 | * RETURNS: | |
4140 | * OR'd bitmask of WORK_BUSY_* bits. | |
4141 | */ | |
4142 | unsigned int work_busy(struct work_struct *work) | |
1da177e4 | 4143 | { |
fa1b54e6 | 4144 | struct worker_pool *pool; |
dcd989cb TH |
4145 | unsigned long flags; |
4146 | unsigned int ret = 0; | |
1da177e4 | 4147 | |
dcd989cb TH |
4148 | if (work_pending(work)) |
4149 | ret |= WORK_BUSY_PENDING; | |
1da177e4 | 4150 | |
fa1b54e6 TH |
4151 | local_irq_save(flags); |
4152 | pool = get_work_pool(work); | |
038366c5 | 4153 | if (pool) { |
fa1b54e6 | 4154 | spin_lock(&pool->lock); |
038366c5 LJ |
4155 | if (find_worker_executing_work(pool, work)) |
4156 | ret |= WORK_BUSY_RUNNING; | |
fa1b54e6 | 4157 | spin_unlock(&pool->lock); |
038366c5 | 4158 | } |
fa1b54e6 | 4159 | local_irq_restore(flags); |
1da177e4 | 4160 | |
dcd989cb | 4161 | return ret; |
1da177e4 | 4162 | } |
dcd989cb | 4163 | EXPORT_SYMBOL_GPL(work_busy); |
1da177e4 | 4164 | |
db7bccf4 TH |
4165 | /* |
4166 | * CPU hotplug. | |
4167 | * | |
e22bee78 | 4168 | * There are two challenges in supporting CPU hotplug. Firstly, there |
112202d9 | 4169 | * are a lot of assumptions on strong associations among work, pwq and |
706026c2 | 4170 | * pool which make migrating pending and scheduled works very |
e22bee78 | 4171 | * difficult to implement without impacting hot paths. Secondly, |
94cf58bb | 4172 | * worker pools serve mix of short, long and very long running works making |
e22bee78 TH |
4173 | * blocked draining impractical. |
4174 | * | |
24647570 | 4175 | * This is solved by allowing the pools to be disassociated from the CPU |
628c78e7 TH |
4176 | * running as an unbound one and allowing it to be reattached later if the |
4177 | * cpu comes back online. | |
db7bccf4 | 4178 | */ |
1da177e4 | 4179 | |
706026c2 | 4180 | static void wq_unbind_fn(struct work_struct *work) |
3af24433 | 4181 | { |
38db41d9 | 4182 | int cpu = smp_processor_id(); |
4ce62e9e | 4183 | struct worker_pool *pool; |
db7bccf4 | 4184 | struct worker *worker; |
db7bccf4 | 4185 | int i; |
3af24433 | 4186 | |
f02ae73a | 4187 | for_each_cpu_worker_pool(pool, cpu) { |
6183c009 | 4188 | WARN_ON_ONCE(cpu != smp_processor_id()); |
db7bccf4 | 4189 | |
94cf58bb TH |
4190 | mutex_lock(&pool->assoc_mutex); |
4191 | spin_lock_irq(&pool->lock); | |
3af24433 | 4192 | |
94cf58bb TH |
4193 | /* |
4194 | * We've claimed all manager positions. Make all workers | |
4195 | * unbound and set DISASSOCIATED. Before this, all workers | |
4196 | * except for the ones which are still executing works from | |
4197 | * before the last CPU down must be on the cpu. After | |
4198 | * this, they may become diasporas. | |
4199 | */ | |
4ce62e9e | 4200 | list_for_each_entry(worker, &pool->idle_list, entry) |
403c821d | 4201 | worker->flags |= WORKER_UNBOUND; |
3af24433 | 4202 | |
b67bfe0d | 4203 | for_each_busy_worker(worker, i, pool) |
c9e7cf27 | 4204 | worker->flags |= WORKER_UNBOUND; |
06ba38a9 | 4205 | |
24647570 | 4206 | pool->flags |= POOL_DISASSOCIATED; |
f2d5a0ee | 4207 | |
94cf58bb TH |
4208 | spin_unlock_irq(&pool->lock); |
4209 | mutex_unlock(&pool->assoc_mutex); | |
4210 | } | |
628c78e7 | 4211 | |
e22bee78 | 4212 | /* |
403c821d | 4213 | * Call schedule() so that we cross rq->lock and thus can guarantee |
628c78e7 TH |
4214 | * sched callbacks see the %WORKER_UNBOUND flag. This is necessary |
4215 | * as scheduler callbacks may be invoked from other cpus. | |
e22bee78 | 4216 | */ |
e22bee78 | 4217 | schedule(); |
06ba38a9 | 4218 | |
e22bee78 | 4219 | /* |
628c78e7 TH |
4220 | * Sched callbacks are disabled now. Zap nr_running. After this, |
4221 | * nr_running stays zero and need_more_worker() and keep_working() | |
38db41d9 TH |
4222 | * are always true as long as the worklist is not empty. Pools on |
4223 | * @cpu now behave as unbound (in terms of concurrency management) | |
4224 | * pools which are served by workers tied to the CPU. | |
628c78e7 TH |
4225 | * |
4226 | * On return from this function, the current worker would trigger | |
4227 | * unbound chain execution of pending work items if other workers | |
4228 | * didn't already. | |
e22bee78 | 4229 | */ |
f02ae73a | 4230 | for_each_cpu_worker_pool(pool, cpu) |
e19e397a | 4231 | atomic_set(&pool->nr_running, 0); |
3af24433 | 4232 | } |
3af24433 | 4233 | |
8db25e78 TH |
4234 | /* |
4235 | * Workqueues should be brought up before normal priority CPU notifiers. | |
4236 | * This will be registered high priority CPU notifier. | |
4237 | */ | |
9fdf9b73 | 4238 | static int __cpuinit workqueue_cpu_up_callback(struct notifier_block *nfb, |
8db25e78 TH |
4239 | unsigned long action, |
4240 | void *hcpu) | |
3af24433 | 4241 | { |
d84ff051 | 4242 | int cpu = (unsigned long)hcpu; |
4ce62e9e | 4243 | struct worker_pool *pool; |
3ce63377 | 4244 | |
8db25e78 | 4245 | switch (action & ~CPU_TASKS_FROZEN) { |
3af24433 | 4246 | case CPU_UP_PREPARE: |
f02ae73a | 4247 | for_each_cpu_worker_pool(pool, cpu) { |
3ce63377 TH |
4248 | struct worker *worker; |
4249 | ||
4250 | if (pool->nr_workers) | |
4251 | continue; | |
4252 | ||
4253 | worker = create_worker(pool); | |
4254 | if (!worker) | |
4255 | return NOTIFY_BAD; | |
4256 | ||
d565ed63 | 4257 | spin_lock_irq(&pool->lock); |
3ce63377 | 4258 | start_worker(worker); |
d565ed63 | 4259 | spin_unlock_irq(&pool->lock); |
3af24433 | 4260 | } |
8db25e78 | 4261 | break; |
3af24433 | 4262 | |
db7bccf4 TH |
4263 | case CPU_DOWN_FAILED: |
4264 | case CPU_ONLINE: | |
f02ae73a | 4265 | for_each_cpu_worker_pool(pool, cpu) { |
94cf58bb TH |
4266 | mutex_lock(&pool->assoc_mutex); |
4267 | spin_lock_irq(&pool->lock); | |
4268 | ||
24647570 | 4269 | pool->flags &= ~POOL_DISASSOCIATED; |
94cf58bb TH |
4270 | rebind_workers(pool); |
4271 | ||
4272 | spin_unlock_irq(&pool->lock); | |
4273 | mutex_unlock(&pool->assoc_mutex); | |
4274 | } | |
db7bccf4 | 4275 | break; |
00dfcaf7 | 4276 | } |
65758202 TH |
4277 | return NOTIFY_OK; |
4278 | } | |
4279 | ||
4280 | /* | |
4281 | * Workqueues should be brought down after normal priority CPU notifiers. | |
4282 | * This will be registered as low priority CPU notifier. | |
4283 | */ | |
9fdf9b73 | 4284 | static int __cpuinit workqueue_cpu_down_callback(struct notifier_block *nfb, |
65758202 TH |
4285 | unsigned long action, |
4286 | void *hcpu) | |
4287 | { | |
d84ff051 | 4288 | int cpu = (unsigned long)hcpu; |
8db25e78 TH |
4289 | struct work_struct unbind_work; |
4290 | ||
65758202 TH |
4291 | switch (action & ~CPU_TASKS_FROZEN) { |
4292 | case CPU_DOWN_PREPARE: | |
8db25e78 | 4293 | /* unbinding should happen on the local CPU */ |
706026c2 | 4294 | INIT_WORK_ONSTACK(&unbind_work, wq_unbind_fn); |
7635d2fd | 4295 | queue_work_on(cpu, system_highpri_wq, &unbind_work); |
8db25e78 TH |
4296 | flush_work(&unbind_work); |
4297 | break; | |
65758202 TH |
4298 | } |
4299 | return NOTIFY_OK; | |
4300 | } | |
4301 | ||
2d3854a3 | 4302 | #ifdef CONFIG_SMP |
8ccad40d | 4303 | |
2d3854a3 | 4304 | struct work_for_cpu { |
ed48ece2 | 4305 | struct work_struct work; |
2d3854a3 RR |
4306 | long (*fn)(void *); |
4307 | void *arg; | |
4308 | long ret; | |
4309 | }; | |
4310 | ||
ed48ece2 | 4311 | static void work_for_cpu_fn(struct work_struct *work) |
2d3854a3 | 4312 | { |
ed48ece2 TH |
4313 | struct work_for_cpu *wfc = container_of(work, struct work_for_cpu, work); |
4314 | ||
2d3854a3 RR |
4315 | wfc->ret = wfc->fn(wfc->arg); |
4316 | } | |
4317 | ||
4318 | /** | |
4319 | * work_on_cpu - run a function in user context on a particular cpu | |
4320 | * @cpu: the cpu to run on | |
4321 | * @fn: the function to run | |
4322 | * @arg: the function arg | |
4323 | * | |
31ad9081 RR |
4324 | * This will return the value @fn returns. |
4325 | * It is up to the caller to ensure that the cpu doesn't go offline. | |
6b44003e | 4326 | * The caller must not hold any locks which would prevent @fn from completing. |
2d3854a3 | 4327 | */ |
d84ff051 | 4328 | long work_on_cpu(int cpu, long (*fn)(void *), void *arg) |
2d3854a3 | 4329 | { |
ed48ece2 | 4330 | struct work_for_cpu wfc = { .fn = fn, .arg = arg }; |
6b44003e | 4331 | |
ed48ece2 TH |
4332 | INIT_WORK_ONSTACK(&wfc.work, work_for_cpu_fn); |
4333 | schedule_work_on(cpu, &wfc.work); | |
4334 | flush_work(&wfc.work); | |
2d3854a3 RR |
4335 | return wfc.ret; |
4336 | } | |
4337 | EXPORT_SYMBOL_GPL(work_on_cpu); | |
4338 | #endif /* CONFIG_SMP */ | |
4339 | ||
a0a1a5fd TH |
4340 | #ifdef CONFIG_FREEZER |
4341 | ||
4342 | /** | |
4343 | * freeze_workqueues_begin - begin freezing workqueues | |
4344 | * | |
58a69cb4 | 4345 | * Start freezing workqueues. After this function returns, all freezable |
c5aa87bb | 4346 | * workqueues will queue new works to their delayed_works list instead of |
706026c2 | 4347 | * pool->worklist. |
a0a1a5fd TH |
4348 | * |
4349 | * CONTEXT: | |
d565ed63 | 4350 | * Grabs and releases workqueue_lock and pool->lock's. |
a0a1a5fd TH |
4351 | */ |
4352 | void freeze_workqueues_begin(void) | |
4353 | { | |
17116969 | 4354 | struct worker_pool *pool; |
24b8a847 TH |
4355 | struct workqueue_struct *wq; |
4356 | struct pool_workqueue *pwq; | |
17116969 | 4357 | int id; |
a0a1a5fd | 4358 | |
e98d5b16 | 4359 | spin_lock_irq(&workqueue_lock); |
a0a1a5fd | 4360 | |
6183c009 | 4361 | WARN_ON_ONCE(workqueue_freezing); |
a0a1a5fd TH |
4362 | workqueue_freezing = true; |
4363 | ||
24b8a847 | 4364 | /* set FREEZING */ |
17116969 | 4365 | for_each_pool(pool, id) { |
17116969 | 4366 | spin_lock(&pool->lock); |
17116969 TH |
4367 | WARN_ON_ONCE(pool->flags & POOL_FREEZING); |
4368 | pool->flags |= POOL_FREEZING; | |
24b8a847 TH |
4369 | spin_unlock(&pool->lock); |
4370 | } | |
a0a1a5fd | 4371 | |
24b8a847 TH |
4372 | /* suppress further executions by setting max_active to zero */ |
4373 | list_for_each_entry(wq, &workqueues, list) { | |
699ce097 TH |
4374 | for_each_pwq(pwq, wq) |
4375 | pwq_adjust_max_active(pwq); | |
a0a1a5fd TH |
4376 | } |
4377 | ||
e98d5b16 | 4378 | spin_unlock_irq(&workqueue_lock); |
a0a1a5fd TH |
4379 | } |
4380 | ||
4381 | /** | |
58a69cb4 | 4382 | * freeze_workqueues_busy - are freezable workqueues still busy? |
a0a1a5fd TH |
4383 | * |
4384 | * Check whether freezing is complete. This function must be called | |
4385 | * between freeze_workqueues_begin() and thaw_workqueues(). | |
4386 | * | |
4387 | * CONTEXT: | |
4388 | * Grabs and releases workqueue_lock. | |
4389 | * | |
4390 | * RETURNS: | |
58a69cb4 TH |
4391 | * %true if some freezable workqueues are still busy. %false if freezing |
4392 | * is complete. | |
a0a1a5fd TH |
4393 | */ |
4394 | bool freeze_workqueues_busy(void) | |
4395 | { | |
a0a1a5fd | 4396 | bool busy = false; |
24b8a847 TH |
4397 | struct workqueue_struct *wq; |
4398 | struct pool_workqueue *pwq; | |
a0a1a5fd | 4399 | |
e98d5b16 | 4400 | spin_lock_irq(&workqueue_lock); |
a0a1a5fd | 4401 | |
6183c009 | 4402 | WARN_ON_ONCE(!workqueue_freezing); |
a0a1a5fd | 4403 | |
24b8a847 TH |
4404 | list_for_each_entry(wq, &workqueues, list) { |
4405 | if (!(wq->flags & WQ_FREEZABLE)) | |
4406 | continue; | |
a0a1a5fd TH |
4407 | /* |
4408 | * nr_active is monotonically decreasing. It's safe | |
4409 | * to peek without lock. | |
4410 | */ | |
24b8a847 | 4411 | for_each_pwq(pwq, wq) { |
6183c009 | 4412 | WARN_ON_ONCE(pwq->nr_active < 0); |
112202d9 | 4413 | if (pwq->nr_active) { |
a0a1a5fd TH |
4414 | busy = true; |
4415 | goto out_unlock; | |
4416 | } | |
4417 | } | |
4418 | } | |
4419 | out_unlock: | |
e98d5b16 | 4420 | spin_unlock_irq(&workqueue_lock); |
a0a1a5fd TH |
4421 | return busy; |
4422 | } | |
4423 | ||
4424 | /** | |
4425 | * thaw_workqueues - thaw workqueues | |
4426 | * | |
4427 | * Thaw workqueues. Normal queueing is restored and all collected | |
706026c2 | 4428 | * frozen works are transferred to their respective pool worklists. |
a0a1a5fd TH |
4429 | * |
4430 | * CONTEXT: | |
d565ed63 | 4431 | * Grabs and releases workqueue_lock and pool->lock's. |
a0a1a5fd TH |
4432 | */ |
4433 | void thaw_workqueues(void) | |
4434 | { | |
24b8a847 TH |
4435 | struct workqueue_struct *wq; |
4436 | struct pool_workqueue *pwq; | |
4437 | struct worker_pool *pool; | |
4438 | int id; | |
a0a1a5fd | 4439 | |
e98d5b16 | 4440 | spin_lock_irq(&workqueue_lock); |
a0a1a5fd TH |
4441 | |
4442 | if (!workqueue_freezing) | |
4443 | goto out_unlock; | |
4444 | ||
24b8a847 TH |
4445 | /* clear FREEZING */ |
4446 | for_each_pool(pool, id) { | |
4447 | spin_lock(&pool->lock); | |
4448 | WARN_ON_ONCE(!(pool->flags & POOL_FREEZING)); | |
4449 | pool->flags &= ~POOL_FREEZING; | |
4450 | spin_unlock(&pool->lock); | |
4451 | } | |
8b03ae3c | 4452 | |
24b8a847 TH |
4453 | /* restore max_active and repopulate worklist */ |
4454 | list_for_each_entry(wq, &workqueues, list) { | |
699ce097 TH |
4455 | for_each_pwq(pwq, wq) |
4456 | pwq_adjust_max_active(pwq); | |
a0a1a5fd TH |
4457 | } |
4458 | ||
24b8a847 TH |
4459 | /* kick workers */ |
4460 | for_each_pool(pool, id) { | |
4461 | spin_lock(&pool->lock); | |
4462 | wake_up_worker(pool); | |
4463 | spin_unlock(&pool->lock); | |
4464 | } | |
4465 | ||
a0a1a5fd TH |
4466 | workqueue_freezing = false; |
4467 | out_unlock: | |
e98d5b16 | 4468 | spin_unlock_irq(&workqueue_lock); |
a0a1a5fd TH |
4469 | } |
4470 | #endif /* CONFIG_FREEZER */ | |
4471 | ||
6ee0578b | 4472 | static int __init init_workqueues(void) |
1da177e4 | 4473 | { |
7a4e344c TH |
4474 | int std_nice[NR_STD_WORKER_POOLS] = { 0, HIGHPRI_NICE_LEVEL }; |
4475 | int i, cpu; | |
c34056a3 | 4476 | |
7c3eed5c TH |
4477 | /* make sure we have enough bits for OFFQ pool ID */ |
4478 | BUILD_BUG_ON((1LU << (BITS_PER_LONG - WORK_OFFQ_POOL_SHIFT)) < | |
6be19588 | 4479 | WORK_CPU_END * NR_STD_WORKER_POOLS); |
b5490077 | 4480 | |
e904e6c2 TH |
4481 | WARN_ON(__alignof__(struct pool_workqueue) < __alignof__(long long)); |
4482 | ||
4483 | pwq_cache = KMEM_CACHE(pool_workqueue, SLAB_PANIC); | |
4484 | ||
65758202 | 4485 | cpu_notifier(workqueue_cpu_up_callback, CPU_PRI_WORKQUEUE_UP); |
a5b4e57d | 4486 | hotcpu_notifier(workqueue_cpu_down_callback, CPU_PRI_WORKQUEUE_DOWN); |
8b03ae3c | 4487 | |
706026c2 | 4488 | /* initialize CPU pools */ |
29c91e99 | 4489 | for_each_possible_cpu(cpu) { |
4ce62e9e | 4490 | struct worker_pool *pool; |
8b03ae3c | 4491 | |
7a4e344c | 4492 | i = 0; |
f02ae73a | 4493 | for_each_cpu_worker_pool(pool, cpu) { |
7a4e344c | 4494 | BUG_ON(init_worker_pool(pool)); |
ec22ca5e | 4495 | pool->cpu = cpu; |
29c91e99 | 4496 | cpumask_copy(pool->attrs->cpumask, cpumask_of(cpu)); |
7a4e344c TH |
4497 | pool->attrs->nice = std_nice[i++]; |
4498 | ||
9daf9e67 TH |
4499 | /* alloc pool ID */ |
4500 | BUG_ON(worker_pool_assign_id(pool)); | |
4ce62e9e | 4501 | } |
8b03ae3c TH |
4502 | } |
4503 | ||
e22bee78 | 4504 | /* create the initial worker */ |
29c91e99 | 4505 | for_each_online_cpu(cpu) { |
4ce62e9e | 4506 | struct worker_pool *pool; |
e22bee78 | 4507 | |
f02ae73a | 4508 | for_each_cpu_worker_pool(pool, cpu) { |
4ce62e9e TH |
4509 | struct worker *worker; |
4510 | ||
29c91e99 | 4511 | pool->flags &= ~POOL_DISASSOCIATED; |
24647570 | 4512 | |
bc2ae0f5 | 4513 | worker = create_worker(pool); |
4ce62e9e | 4514 | BUG_ON(!worker); |
d565ed63 | 4515 | spin_lock_irq(&pool->lock); |
4ce62e9e | 4516 | start_worker(worker); |
d565ed63 | 4517 | spin_unlock_irq(&pool->lock); |
4ce62e9e | 4518 | } |
e22bee78 TH |
4519 | } |
4520 | ||
29c91e99 TH |
4521 | /* create default unbound wq attrs */ |
4522 | for (i = 0; i < NR_STD_WORKER_POOLS; i++) { | |
4523 | struct workqueue_attrs *attrs; | |
4524 | ||
4525 | BUG_ON(!(attrs = alloc_workqueue_attrs(GFP_KERNEL))); | |
4526 | ||
4527 | attrs->nice = std_nice[i]; | |
4528 | cpumask_setall(attrs->cpumask); | |
4529 | ||
4530 | unbound_std_wq_attrs[i] = attrs; | |
4531 | } | |
4532 | ||
d320c038 | 4533 | system_wq = alloc_workqueue("events", 0, 0); |
1aabe902 | 4534 | system_highpri_wq = alloc_workqueue("events_highpri", WQ_HIGHPRI, 0); |
d320c038 | 4535 | system_long_wq = alloc_workqueue("events_long", 0, 0); |
f3421797 TH |
4536 | system_unbound_wq = alloc_workqueue("events_unbound", WQ_UNBOUND, |
4537 | WQ_UNBOUND_MAX_ACTIVE); | |
24d51add TH |
4538 | system_freezable_wq = alloc_workqueue("events_freezable", |
4539 | WQ_FREEZABLE, 0); | |
1aabe902 | 4540 | BUG_ON(!system_wq || !system_highpri_wq || !system_long_wq || |
ae930e0f | 4541 | !system_unbound_wq || !system_freezable_wq); |
6ee0578b | 4542 | return 0; |
1da177e4 | 4543 | } |
6ee0578b | 4544 | early_initcall(init_workqueues); |