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