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