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