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