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