Commit | Line | Data |
---|---|---|
457c8996 | 1 | // SPDX-License-Identifier: GPL-2.0-only |
1da177e4 | 2 | /* |
c54fce6e | 3 | * kernel/workqueue.c - generic async execution with shared worker pool |
1da177e4 | 4 | * |
c54fce6e | 5 | * Copyright (C) 2002 Ingo Molnar |
1da177e4 | 6 | * |
c54fce6e TH |
7 | * Derived from the taskqueue/keventd code by: |
8 | * David Woodhouse <dwmw2@infradead.org> | |
9 | * Andrew Morton | |
10 | * Kai Petzke <wpp@marie.physik.tu-berlin.de> | |
11 | * Theodore Ts'o <tytso@mit.edu> | |
1da177e4 | 12 | * |
c54fce6e | 13 | * Made to use alloc_percpu by Christoph Lameter. |
1da177e4 | 14 | * |
c54fce6e TH |
15 | * Copyright (C) 2010 SUSE Linux Products GmbH |
16 | * Copyright (C) 2010 Tejun Heo <tj@kernel.org> | |
89ada679 | 17 | * |
c54fce6e TH |
18 | * This is the generic async execution mechanism. Work items as are |
19 | * executed in process context. The worker pool is shared and | |
b11895c4 L |
20 | * automatically managed. There are two worker pools for each CPU (one for |
21 | * normal work items and the other for high priority ones) and some extra | |
22 | * pools for workqueues which are not bound to any specific CPU - the | |
23 | * number of these backing pools is dynamic. | |
c54fce6e | 24 | * |
9a261491 | 25 | * Please read Documentation/core-api/workqueue.rst for details. |
1da177e4 LT |
26 | */ |
27 | ||
9984de1a | 28 | #include <linux/export.h> |
1da177e4 LT |
29 | #include <linux/kernel.h> |
30 | #include <linux/sched.h> | |
31 | #include <linux/init.h> | |
4cb1ef64 | 32 | #include <linux/interrupt.h> |
1da177e4 LT |
33 | #include <linux/signal.h> |
34 | #include <linux/completion.h> | |
35 | #include <linux/workqueue.h> | |
36 | #include <linux/slab.h> | |
37 | #include <linux/cpu.h> | |
38 | #include <linux/notifier.h> | |
39 | #include <linux/kthread.h> | |
1fa44eca | 40 | #include <linux/hardirq.h> |
46934023 | 41 | #include <linux/mempolicy.h> |
341a5958 | 42 | #include <linux/freezer.h> |
d5abe669 | 43 | #include <linux/debug_locks.h> |
4e6045f1 | 44 | #include <linux/lockdep.h> |
c34056a3 | 45 | #include <linux/idr.h> |
29c91e99 | 46 | #include <linux/jhash.h> |
42f8570f | 47 | #include <linux/hashtable.h> |
76af4d93 | 48 | #include <linux/rculist.h> |
bce90380 | 49 | #include <linux/nodemask.h> |
4c16bd32 | 50 | #include <linux/moduleparam.h> |
3d1cb205 | 51 | #include <linux/uaccess.h> |
c98a9805 | 52 | #include <linux/sched/isolation.h> |
cd2440d6 | 53 | #include <linux/sched/debug.h> |
62635ea8 | 54 | #include <linux/nmi.h> |
940d71c6 | 55 | #include <linux/kvm_para.h> |
aa6fde93 | 56 | #include <linux/delay.h> |
e22bee78 | 57 | |
ea138446 | 58 | #include "workqueue_internal.h" |
1da177e4 | 59 | |
e563d0a7 | 60 | enum worker_pool_flags { |
24647570 TH |
61 | /* |
62 | * worker_pool flags | |
bc2ae0f5 | 63 | * |
24647570 | 64 | * A bound pool is either associated or disassociated with its CPU. |
bc2ae0f5 TH |
65 | * While associated (!DISASSOCIATED), all workers are bound to the |
66 | * CPU and none has %WORKER_UNBOUND set and concurrency management | |
67 | * is in effect. | |
68 | * | |
69 | * While DISASSOCIATED, the cpu may be offline and all workers have | |
70 | * %WORKER_UNBOUND set and concurrency management disabled, and may | |
24647570 | 71 | * be executing on any CPU. The pool behaves as an unbound one. |
bc2ae0f5 | 72 | * |
bc3a1afc | 73 | * Note that DISASSOCIATED should be flipped only while holding |
1258fae7 | 74 | * wq_pool_attach_mutex to avoid changing binding state while |
4736cbf7 | 75 | * worker_attach_to_pool() is in progress. |
4cb1ef64 TH |
76 | * |
77 | * As there can only be one concurrent BH execution context per CPU, a | |
78 | * BH pool is per-CPU and always DISASSOCIATED. | |
bc2ae0f5 | 79 | */ |
4cb1ef64 TH |
80 | POOL_BH = 1 << 0, /* is a BH pool */ |
81 | POOL_MANAGER_ACTIVE = 1 << 1, /* being managed */ | |
24647570 | 82 | POOL_DISASSOCIATED = 1 << 2, /* cpu can't serve workers */ |
e563d0a7 | 83 | }; |
db7bccf4 | 84 | |
e563d0a7 | 85 | enum worker_flags { |
c8e55f36 | 86 | /* worker flags */ |
c8e55f36 TH |
87 | WORKER_DIE = 1 << 1, /* die die die */ |
88 | WORKER_IDLE = 1 << 2, /* is idle */ | |
e22bee78 | 89 | WORKER_PREP = 1 << 3, /* preparing to run works */ |
fb0e7beb | 90 | WORKER_CPU_INTENSIVE = 1 << 6, /* cpu intensive */ |
f3421797 | 91 | WORKER_UNBOUND = 1 << 7, /* worker is unbound */ |
a9ab775b | 92 | WORKER_REBOUND = 1 << 8, /* worker was rebound */ |
e22bee78 | 93 | |
a9ab775b TH |
94 | WORKER_NOT_RUNNING = WORKER_PREP | WORKER_CPU_INTENSIVE | |
95 | WORKER_UNBOUND | WORKER_REBOUND, | |
e563d0a7 | 96 | }; |
db7bccf4 | 97 | |
e563d0a7 | 98 | enum wq_internal_consts { |
e34cdddb | 99 | NR_STD_WORKER_POOLS = 2, /* # standard pools per cpu */ |
4ce62e9e | 100 | |
29c91e99 | 101 | UNBOUND_POOL_HASH_ORDER = 6, /* hashed by pool->attrs */ |
c8e55f36 | 102 | BUSY_WORKER_HASH_ORDER = 6, /* 64 pointers */ |
db7bccf4 | 103 | |
e22bee78 TH |
104 | MAX_IDLE_WORKERS_RATIO = 4, /* 1/4 of busy can be idle */ |
105 | IDLE_WORKER_TIMEOUT = 300 * HZ, /* keep idle ones for 5 mins */ | |
106 | ||
3233cdbd TH |
107 | MAYDAY_INITIAL_TIMEOUT = HZ / 100 >= 2 ? HZ / 100 : 2, |
108 | /* call for help after 10ms | |
109 | (min two ticks) */ | |
e22bee78 TH |
110 | MAYDAY_INTERVAL = HZ / 10, /* and then every 100ms */ |
111 | CREATE_COOLDOWN = HZ, /* time to breath after fail */ | |
e22bee78 TH |
112 | |
113 | /* | |
114 | * Rescue workers are used only on emergencies and shared by | |
8698a745 | 115 | * all cpus. Give MIN_NICE. |
e22bee78 | 116 | */ |
8698a745 DY |
117 | RESCUER_NICE_LEVEL = MIN_NICE, |
118 | HIGHPRI_NICE_LEVEL = MIN_NICE, | |
ecf6881f | 119 | |
31c89007 | 120 | WQ_NAME_LEN = 32, |
c8e55f36 | 121 | }; |
1da177e4 | 122 | |
4cb1ef64 TH |
123 | /* |
124 | * We don't want to trap softirq for too long. See MAX_SOFTIRQ_TIME and | |
125 | * MAX_SOFTIRQ_RESTART in kernel/softirq.c. These are macros because | |
126 | * msecs_to_jiffies() can't be an initializer. | |
127 | */ | |
128 | #define BH_WORKER_JIFFIES msecs_to_jiffies(2) | |
129 | #define BH_WORKER_RESTARTS 10 | |
130 | ||
1da177e4 | 131 | /* |
4690c4ab TH |
132 | * Structure fields follow one of the following exclusion rules. |
133 | * | |
e41e704b TH |
134 | * I: Modifiable by initialization/destruction paths and read-only for |
135 | * everyone else. | |
4690c4ab | 136 | * |
e22bee78 TH |
137 | * P: Preemption protected. Disabling preemption is enough and should |
138 | * only be modified and accessed from the local cpu. | |
139 | * | |
d565ed63 | 140 | * L: pool->lock protected. Access with pool->lock held. |
4690c4ab | 141 | * |
5797b1c1 TH |
142 | * LN: pool->lock and wq_node_nr_active->lock protected for writes. Either for |
143 | * reads. | |
144 | * | |
bdf8b9bf TH |
145 | * K: Only modified by worker while holding pool->lock. Can be safely read by |
146 | * self, while holding pool->lock or from IRQ context if %current is the | |
147 | * kworker. | |
148 | * | |
149 | * S: Only modified by worker self. | |
150 | * | |
1258fae7 | 151 | * A: wq_pool_attach_mutex protected. |
822d8405 | 152 | * |
68e13a67 | 153 | * PL: wq_pool_mutex protected. |
5bcab335 | 154 | * |
24acfb71 | 155 | * PR: wq_pool_mutex protected for writes. RCU protected for reads. |
76af4d93 | 156 | * |
5b95e1af LJ |
157 | * PW: wq_pool_mutex and wq->mutex protected for writes. Either for reads. |
158 | * | |
159 | * PWR: wq_pool_mutex and wq->mutex protected for writes. Either or | |
24acfb71 | 160 | * RCU for reads. |
5b95e1af | 161 | * |
3c25a55d LJ |
162 | * WQ: wq->mutex protected. |
163 | * | |
24acfb71 | 164 | * WR: wq->mutex protected for writes. RCU protected for reads. |
2e109a28 | 165 | * |
a045a272 TH |
166 | * WO: wq->mutex protected for writes. Updated with WRITE_ONCE() and can be read |
167 | * with READ_ONCE() without locking. | |
168 | * | |
2e109a28 | 169 | * MD: wq_mayday_lock protected. |
cd2440d6 PM |
170 | * |
171 | * WD: Used internally by the watchdog. | |
1da177e4 | 172 | */ |
1da177e4 | 173 | |
2eaebdb3 | 174 | /* struct worker is defined in workqueue_internal.h */ |
c34056a3 | 175 | |
bd7bdd43 | 176 | struct worker_pool { |
a9b8a985 | 177 | raw_spinlock_t lock; /* the pool lock */ |
d84ff051 | 178 | int cpu; /* I: the associated cpu */ |
f3f90ad4 | 179 | int node; /* I: the associated node ID */ |
9daf9e67 | 180 | int id; /* I: pool ID */ |
bc8b50c2 | 181 | unsigned int flags; /* L: flags */ |
bd7bdd43 | 182 | |
82607adc | 183 | unsigned long watchdog_ts; /* L: watchdog timestamp */ |
cd2440d6 | 184 | bool cpu_stall; /* WD: stalled cpu bound pool */ |
82607adc | 185 | |
bc35f7ef LJ |
186 | /* |
187 | * The counter is incremented in a process context on the associated CPU | |
188 | * w/ preemption disabled, and decremented or reset in the same context | |
189 | * but w/ pool->lock held. The readers grab pool->lock and are | |
190 | * guaranteed to see if the counter reached zero. | |
191 | */ | |
192 | int nr_running; | |
84f91c62 | 193 | |
bd7bdd43 | 194 | struct list_head worklist; /* L: list of pending works */ |
ea1abd61 | 195 | |
5826cc8f LJ |
196 | int nr_workers; /* L: total number of workers */ |
197 | int nr_idle; /* L: currently idle workers */ | |
bd7bdd43 | 198 | |
2c1f1a91 | 199 | struct list_head idle_list; /* L: list of idle workers */ |
bd7bdd43 | 200 | struct timer_list idle_timer; /* L: worker idle timeout */ |
3f959aa3 VS |
201 | struct work_struct idle_cull_work; /* L: worker idle cleanup */ |
202 | ||
203 | struct timer_list mayday_timer; /* L: SOS timer for workers */ | |
bd7bdd43 | 204 | |
c5aa87bb | 205 | /* a workers is either on busy_hash or idle_list, or the manager */ |
c9e7cf27 TH |
206 | DECLARE_HASHTABLE(busy_hash, BUSY_WORKER_HASH_ORDER); |
207 | /* L: hash of busy workers */ | |
208 | ||
2607d7a6 | 209 | struct worker *manager; /* L: purely informational */ |
92f9c5c4 | 210 | struct list_head workers; /* A: attached workers */ |
e02b9312 | 211 | struct list_head dying_workers; /* A: workers about to die */ |
60f5a4bc | 212 | struct completion *detach_completion; /* all workers detached */ |
e19e397a | 213 | |
7cda9aae | 214 | struct ida worker_ida; /* worker IDs for task name */ |
e19e397a | 215 | |
7a4e344c | 216 | struct workqueue_attrs *attrs; /* I: worker attributes */ |
68e13a67 LJ |
217 | struct hlist_node hash_node; /* PL: unbound_pool_hash node */ |
218 | int refcnt; /* PL: refcnt for unbound pools */ | |
7a4e344c | 219 | |
29c91e99 | 220 | /* |
24acfb71 | 221 | * Destruction of pool is RCU protected to allow dereferences |
29c91e99 TH |
222 | * from get_work_pool(). |
223 | */ | |
224 | struct rcu_head rcu; | |
84f91c62 | 225 | }; |
8b03ae3c | 226 | |
725e8ec5 TH |
227 | /* |
228 | * Per-pool_workqueue statistics. These can be monitored using | |
229 | * tools/workqueue/wq_monitor.py. | |
230 | */ | |
231 | enum pool_workqueue_stats { | |
232 | PWQ_STAT_STARTED, /* work items started execution */ | |
233 | PWQ_STAT_COMPLETED, /* work items completed execution */ | |
8a1dd1e5 | 234 | PWQ_STAT_CPU_TIME, /* total CPU time consumed */ |
616db877 | 235 | PWQ_STAT_CPU_INTENSIVE, /* wq_cpu_intensive_thresh_us violations */ |
725e8ec5 | 236 | PWQ_STAT_CM_WAKEUP, /* concurrency-management worker wakeups */ |
8639eceb | 237 | PWQ_STAT_REPATRIATED, /* unbound workers brought back into scope */ |
725e8ec5 TH |
238 | PWQ_STAT_MAYDAY, /* maydays to rescuer */ |
239 | PWQ_STAT_RESCUED, /* linked work items executed by rescuer */ | |
240 | ||
241 | PWQ_NR_STATS, | |
242 | }; | |
243 | ||
1da177e4 | 244 | /* |
112202d9 TH |
245 | * The per-pool workqueue. While queued, the lower WORK_STRUCT_FLAG_BITS |
246 | * of work_struct->data are used for flags and the remaining high bits | |
247 | * point to the pwq; thus, pwqs need to be aligned at two's power of the | |
248 | * number of flag bits. | |
1da177e4 | 249 | */ |
112202d9 | 250 | struct pool_workqueue { |
bd7bdd43 | 251 | struct worker_pool *pool; /* I: the associated pool */ |
4690c4ab | 252 | struct workqueue_struct *wq; /* I: the owning workqueue */ |
73f53c4a TH |
253 | int work_color; /* L: current color */ |
254 | int flush_color; /* L: flushing color */ | |
8864b4e5 | 255 | int refcnt; /* L: reference count */ |
73f53c4a TH |
256 | int nr_in_flight[WORK_NR_COLORS]; |
257 | /* L: nr of in_flight works */ | |
4c065dbc | 258 | bool plugged; /* L: execution suspended */ |
018f3a13 LJ |
259 | |
260 | /* | |
261 | * nr_active management and WORK_STRUCT_INACTIVE: | |
262 | * | |
263 | * When pwq->nr_active >= max_active, new work item is queued to | |
264 | * pwq->inactive_works instead of pool->worklist and marked with | |
265 | * WORK_STRUCT_INACTIVE. | |
266 | * | |
5797b1c1 TH |
267 | * All work items marked with WORK_STRUCT_INACTIVE do not participate in |
268 | * nr_active and all work items in pwq->inactive_works are marked with | |
269 | * WORK_STRUCT_INACTIVE. But not all WORK_STRUCT_INACTIVE work items are | |
270 | * in pwq->inactive_works. Some of them are ready to run in | |
271 | * pool->worklist or worker->scheduled. Those work itmes are only struct | |
272 | * wq_barrier which is used for flush_work() and should not participate | |
273 | * in nr_active. For non-barrier work item, it is marked with | |
274 | * WORK_STRUCT_INACTIVE iff it is in pwq->inactive_works. | |
018f3a13 | 275 | */ |
1e19ffc6 | 276 | int nr_active; /* L: nr of active works */ |
f97a4a1a | 277 | struct list_head inactive_works; /* L: inactive works */ |
5797b1c1 | 278 | struct list_head pending_node; /* LN: node on wq_node_nr_active->pending_pwqs */ |
3c25a55d | 279 | struct list_head pwqs_node; /* WR: node on wq->pwqs */ |
2e109a28 | 280 | struct list_head mayday_node; /* MD: node on wq->maydays */ |
8864b4e5 | 281 | |
725e8ec5 TH |
282 | u64 stats[PWQ_NR_STATS]; |
283 | ||
8864b4e5 | 284 | /* |
967b494e | 285 | * Release of unbound pwq is punted to a kthread_worker. See put_pwq() |
687a9aa5 TH |
286 | * and pwq_release_workfn() for details. pool_workqueue itself is also |
287 | * RCU protected so that the first pwq can be determined without | |
967b494e | 288 | * grabbing wq->mutex. |
8864b4e5 | 289 | */ |
687a9aa5 | 290 | struct kthread_work release_work; |
8864b4e5 | 291 | struct rcu_head rcu; |
e904e6c2 | 292 | } __aligned(1 << WORK_STRUCT_FLAG_BITS); |
1da177e4 | 293 | |
73f53c4a TH |
294 | /* |
295 | * Structure used to wait for workqueue flush. | |
296 | */ | |
297 | struct wq_flusher { | |
3c25a55d LJ |
298 | struct list_head list; /* WQ: list of flushers */ |
299 | int flush_color; /* WQ: flush color waiting for */ | |
73f53c4a TH |
300 | struct completion done; /* flush completion */ |
301 | }; | |
302 | ||
226223ab TH |
303 | struct wq_device; |
304 | ||
91ccc6e7 TH |
305 | /* |
306 | * Unlike in a per-cpu workqueue where max_active limits its concurrency level | |
307 | * on each CPU, in an unbound workqueue, max_active applies to the whole system. | |
308 | * As sharing a single nr_active across multiple sockets can be very expensive, | |
309 | * the counting and enforcement is per NUMA node. | |
5797b1c1 TH |
310 | * |
311 | * The following struct is used to enforce per-node max_active. When a pwq wants | |
312 | * to start executing a work item, it should increment ->nr using | |
313 | * tryinc_node_nr_active(). If acquisition fails due to ->nr already being over | |
314 | * ->max, the pwq is queued on ->pending_pwqs. As in-flight work items finish | |
315 | * and decrement ->nr, node_activate_pending_pwq() activates the pending pwqs in | |
316 | * round-robin order. | |
91ccc6e7 TH |
317 | */ |
318 | struct wq_node_nr_active { | |
5797b1c1 TH |
319 | int max; /* per-node max_active */ |
320 | atomic_t nr; /* per-node nr_active */ | |
321 | raw_spinlock_t lock; /* nests inside pool locks */ | |
322 | struct list_head pending_pwqs; /* LN: pwqs with inactive works */ | |
91ccc6e7 TH |
323 | }; |
324 | ||
1da177e4 | 325 | /* |
c5aa87bb TH |
326 | * The externally visible workqueue. It relays the issued work items to |
327 | * the appropriate worker_pool through its pool_workqueues. | |
1da177e4 LT |
328 | */ |
329 | struct workqueue_struct { | |
3c25a55d | 330 | struct list_head pwqs; /* WR: all pwqs of this wq */ |
e2dca7ad | 331 | struct list_head list; /* PR: list of all workqueues */ |
73f53c4a | 332 | |
3c25a55d LJ |
333 | struct mutex mutex; /* protects this wq */ |
334 | int work_color; /* WQ: current work color */ | |
335 | int flush_color; /* WQ: current flush color */ | |
112202d9 | 336 | atomic_t nr_pwqs_to_flush; /* flush in progress */ |
3c25a55d LJ |
337 | struct wq_flusher *first_flusher; /* WQ: first flusher */ |
338 | struct list_head flusher_queue; /* WQ: flush waiters */ | |
339 | struct list_head flusher_overflow; /* WQ: flush overflow list */ | |
73f53c4a | 340 | |
2e109a28 | 341 | struct list_head maydays; /* MD: pwqs requesting rescue */ |
30ae2fc0 | 342 | struct worker *rescuer; /* MD: rescue worker */ |
e22bee78 | 343 | |
87fc741e | 344 | int nr_drainers; /* WQ: drain in progress */ |
5797b1c1 TH |
345 | |
346 | /* See alloc_workqueue() function comment for info on min/max_active */ | |
a045a272 | 347 | int max_active; /* WO: max active works */ |
5797b1c1 | 348 | int min_active; /* WO: min active works */ |
a045a272 | 349 | int saved_max_active; /* WQ: saved max_active */ |
5797b1c1 | 350 | int saved_min_active; /* WQ: saved min_active */ |
226223ab | 351 | |
5b95e1af | 352 | struct workqueue_attrs *unbound_attrs; /* PW: only for unbound wqs */ |
9f66cff2 | 353 | struct pool_workqueue __rcu *dfl_pwq; /* PW: only for unbound wqs */ |
6029a918 | 354 | |
226223ab TH |
355 | #ifdef CONFIG_SYSFS |
356 | struct wq_device *wq_dev; /* I: for sysfs interface */ | |
357 | #endif | |
4e6045f1 | 358 | #ifdef CONFIG_LOCKDEP |
669de8bd BVA |
359 | char *lock_name; |
360 | struct lock_class_key key; | |
4690c4ab | 361 | struct lockdep_map lockdep_map; |
4e6045f1 | 362 | #endif |
ecf6881f | 363 | char name[WQ_NAME_LEN]; /* I: workqueue name */ |
2728fd2f | 364 | |
e2dca7ad | 365 | /* |
24acfb71 TG |
366 | * Destruction of workqueue_struct is RCU protected to allow walking |
367 | * the workqueues list without grabbing wq_pool_mutex. | |
e2dca7ad TH |
368 | * This is used to dump all workqueues from sysrq. |
369 | */ | |
370 | struct rcu_head rcu; | |
371 | ||
2728fd2f TH |
372 | /* hot fields used during command issue, aligned to cacheline */ |
373 | unsigned int flags ____cacheline_aligned; /* WQ: WQ_* flags */ | |
636b927e | 374 | struct pool_workqueue __percpu __rcu **cpu_pwq; /* I: per-cpu pwqs */ |
91ccc6e7 | 375 | struct wq_node_nr_active *node_nr_active[]; /* I: per-node nr_active */ |
1da177e4 LT |
376 | }; |
377 | ||
e904e6c2 TH |
378 | static struct kmem_cache *pwq_cache; |
379 | ||
84193c07 TH |
380 | /* |
381 | * Each pod type describes how CPUs should be grouped for unbound workqueues. | |
382 | * See the comment above workqueue_attrs->affn_scope. | |
383 | */ | |
384 | struct wq_pod_type { | |
385 | int nr_pods; /* number of pods */ | |
386 | cpumask_var_t *pod_cpus; /* pod -> cpus */ | |
387 | int *pod_node; /* pod -> node */ | |
388 | int *cpu_pod; /* cpu -> pod */ | |
389 | }; | |
390 | ||
391 | static struct wq_pod_type wq_pod_types[WQ_AFFN_NR_TYPES]; | |
523a301e | 392 | static enum wq_affn_scope wq_affn_dfl = WQ_AFFN_CACHE; |
63c5484e TH |
393 | |
394 | static const char *wq_affn_names[WQ_AFFN_NR_TYPES] = { | |
523a301e | 395 | [WQ_AFFN_DFL] = "default", |
63c5484e TH |
396 | [WQ_AFFN_CPU] = "cpu", |
397 | [WQ_AFFN_SMT] = "smt", | |
398 | [WQ_AFFN_CACHE] = "cache", | |
399 | [WQ_AFFN_NUMA] = "numa", | |
400 | [WQ_AFFN_SYSTEM] = "system", | |
401 | }; | |
bce90380 | 402 | |
c5f8cd6c TH |
403 | static bool wq_topo_initialized __read_mostly = false; |
404 | ||
616db877 TH |
405 | /* |
406 | * Per-cpu work items which run for longer than the following threshold are | |
407 | * automatically considered CPU intensive and excluded from concurrency | |
408 | * management to prevent them from noticeably delaying other per-cpu work items. | |
aa6fde93 TH |
409 | * ULONG_MAX indicates that the user hasn't overridden it with a boot parameter. |
410 | * The actual value is initialized in wq_cpu_intensive_thresh_init(). | |
616db877 | 411 | */ |
aa6fde93 | 412 | static unsigned long wq_cpu_intensive_thresh_us = ULONG_MAX; |
616db877 TH |
413 | module_param_named(cpu_intensive_thresh_us, wq_cpu_intensive_thresh_us, ulong, 0644); |
414 | ||
cee22a15 | 415 | /* see the comment above the definition of WQ_POWER_EFFICIENT */ |
552f530c | 416 | static bool wq_power_efficient = IS_ENABLED(CONFIG_WQ_POWER_EFFICIENT_DEFAULT); |
cee22a15 VK |
417 | module_param_named(power_efficient, wq_power_efficient, bool, 0444); |
418 | ||
863b710b | 419 | static bool wq_online; /* can kworkers be created yet? */ |
3347fa09 | 420 | |
fef59c9c TH |
421 | /* buf for wq_update_unbound_pod_attrs(), protected by CPU hotplug exclusion */ |
422 | static struct workqueue_attrs *wq_update_pod_attrs_buf; | |
4c16bd32 | 423 | |
68e13a67 | 424 | static DEFINE_MUTEX(wq_pool_mutex); /* protects pools and workqueues list */ |
1258fae7 | 425 | static DEFINE_MUTEX(wq_pool_attach_mutex); /* protects worker attach/detach */ |
a9b8a985 | 426 | static DEFINE_RAW_SPINLOCK(wq_mayday_lock); /* protects wq->maydays list */ |
d8bb65ab SAS |
427 | /* wait for manager to go away */ |
428 | static struct rcuwait manager_wait = __RCUWAIT_INITIALIZER(manager_wait); | |
5bcab335 | 429 | |
e2dca7ad | 430 | static LIST_HEAD(workqueues); /* PR: list of all workqueues */ |
68e13a67 | 431 | static bool workqueue_freezing; /* PL: have wqs started freezing? */ |
7d19c5ce | 432 | |
99c621ef | 433 | /* PL&A: allowable cpus for unbound wqs and work items */ |
ef557180 MG |
434 | static cpumask_var_t wq_unbound_cpumask; |
435 | ||
fe28f631 WL |
436 | /* PL: user requested unbound cpumask via sysfs */ |
437 | static cpumask_var_t wq_requested_unbound_cpumask; | |
438 | ||
439 | /* PL: isolated cpumask to be excluded from unbound cpumask */ | |
440 | static cpumask_var_t wq_isolated_cpumask; | |
441 | ||
ace3c549 | 442 | /* for further constrain wq_unbound_cpumask by cmdline parameter*/ |
443 | static struct cpumask wq_cmdline_cpumask __initdata; | |
444 | ||
ef557180 MG |
445 | /* CPU where unbound work was last round robin scheduled from this CPU */ |
446 | static DEFINE_PER_CPU(int, wq_rr_cpu_last); | |
b05a7928 | 447 | |
f303fccb TH |
448 | /* |
449 | * Local execution of unbound work items is no longer guaranteed. The | |
450 | * following always forces round-robin CPU selection on unbound work items | |
451 | * to uncover usages which depend on it. | |
452 | */ | |
453 | #ifdef CONFIG_DEBUG_WQ_FORCE_RR_CPU | |
454 | static bool wq_debug_force_rr_cpu = true; | |
455 | #else | |
456 | static bool wq_debug_force_rr_cpu = false; | |
457 | #endif | |
458 | module_param_named(debug_force_rr_cpu, wq_debug_force_rr_cpu, bool, 0644); | |
459 | ||
4cb1ef64 TH |
460 | /* the BH worker pools */ |
461 | static DEFINE_PER_CPU_SHARED_ALIGNED(struct worker_pool [NR_STD_WORKER_POOLS], | |
462 | bh_worker_pools); | |
463 | ||
7d19c5ce | 464 | /* the per-cpu worker pools */ |
4cb1ef64 TH |
465 | static DEFINE_PER_CPU_SHARED_ALIGNED(struct worker_pool [NR_STD_WORKER_POOLS], |
466 | cpu_worker_pools); | |
7d19c5ce | 467 | |
68e13a67 | 468 | static DEFINE_IDR(worker_pool_idr); /* PR: idr of all pools */ |
7d19c5ce | 469 | |
68e13a67 | 470 | /* PL: hash of all unbound pools keyed by pool->attrs */ |
29c91e99 TH |
471 | static DEFINE_HASHTABLE(unbound_pool_hash, UNBOUND_POOL_HASH_ORDER); |
472 | ||
c5aa87bb | 473 | /* I: attributes used when instantiating standard unbound pools on demand */ |
29c91e99 TH |
474 | static struct workqueue_attrs *unbound_std_wq_attrs[NR_STD_WORKER_POOLS]; |
475 | ||
8a2b7538 TH |
476 | /* I: attributes used when instantiating ordered pools on demand */ |
477 | static struct workqueue_attrs *ordered_wq_attrs[NR_STD_WORKER_POOLS]; | |
478 | ||
967b494e TH |
479 | /* |
480 | * I: kthread_worker to release pwq's. pwq release needs to be bounced to a | |
481 | * process context while holding a pool lock. Bounce to a dedicated kthread | |
482 | * worker to avoid A-A deadlocks. | |
483 | */ | |
68279f9c | 484 | static struct kthread_worker *pwq_release_worker __ro_after_init; |
967b494e | 485 | |
68279f9c | 486 | struct workqueue_struct *system_wq __ro_after_init; |
ad7b1f84 | 487 | EXPORT_SYMBOL(system_wq); |
68279f9c | 488 | struct workqueue_struct *system_highpri_wq __ro_after_init; |
1aabe902 | 489 | EXPORT_SYMBOL_GPL(system_highpri_wq); |
68279f9c | 490 | struct workqueue_struct *system_long_wq __ro_after_init; |
d320c038 | 491 | EXPORT_SYMBOL_GPL(system_long_wq); |
68279f9c | 492 | struct workqueue_struct *system_unbound_wq __ro_after_init; |
f3421797 | 493 | EXPORT_SYMBOL_GPL(system_unbound_wq); |
68279f9c | 494 | struct workqueue_struct *system_freezable_wq __ro_after_init; |
24d51add | 495 | EXPORT_SYMBOL_GPL(system_freezable_wq); |
68279f9c | 496 | struct workqueue_struct *system_power_efficient_wq __ro_after_init; |
0668106c | 497 | EXPORT_SYMBOL_GPL(system_power_efficient_wq); |
68279f9c | 498 | struct workqueue_struct *system_freezable_power_efficient_wq __ro_after_init; |
0668106c | 499 | EXPORT_SYMBOL_GPL(system_freezable_power_efficient_wq); |
4cb1ef64 TH |
500 | struct workqueue_struct *system_bh_wq; |
501 | EXPORT_SYMBOL_GPL(system_bh_wq); | |
502 | struct workqueue_struct *system_bh_highpri_wq; | |
503 | EXPORT_SYMBOL_GPL(system_bh_highpri_wq); | |
d320c038 | 504 | |
7d19c5ce | 505 | static int worker_thread(void *__worker); |
6ba94429 | 506 | static void workqueue_sysfs_unregister(struct workqueue_struct *wq); |
c29eb853 | 507 | static void show_pwq(struct pool_workqueue *pwq); |
55df0933 | 508 | static void show_one_worker_pool(struct worker_pool *pool); |
7d19c5ce | 509 | |
97bd2347 TH |
510 | #define CREATE_TRACE_POINTS |
511 | #include <trace/events/workqueue.h> | |
512 | ||
68e13a67 | 513 | #define assert_rcu_or_pool_mutex() \ |
24acfb71 | 514 | RCU_LOCKDEP_WARN(!rcu_read_lock_held() && \ |
f78f5b90 | 515 | !lockdep_is_held(&wq_pool_mutex), \ |
24acfb71 | 516 | "RCU or wq_pool_mutex should be held") |
5bcab335 | 517 | |
5b95e1af | 518 | #define assert_rcu_or_wq_mutex_or_pool_mutex(wq) \ |
24acfb71 | 519 | RCU_LOCKDEP_WARN(!rcu_read_lock_held() && \ |
f78f5b90 PM |
520 | !lockdep_is_held(&wq->mutex) && \ |
521 | !lockdep_is_held(&wq_pool_mutex), \ | |
24acfb71 | 522 | "RCU, wq->mutex or wq_pool_mutex should be held") |
5b95e1af | 523 | |
4cb1ef64 TH |
524 | #define for_each_bh_worker_pool(pool, cpu) \ |
525 | for ((pool) = &per_cpu(bh_worker_pools, cpu)[0]; \ | |
526 | (pool) < &per_cpu(bh_worker_pools, cpu)[NR_STD_WORKER_POOLS]; \ | |
527 | (pool)++) | |
528 | ||
f02ae73a TH |
529 | #define for_each_cpu_worker_pool(pool, cpu) \ |
530 | for ((pool) = &per_cpu(cpu_worker_pools, cpu)[0]; \ | |
531 | (pool) < &per_cpu(cpu_worker_pools, cpu)[NR_STD_WORKER_POOLS]; \ | |
7a62c2c8 | 532 | (pool)++) |
4ce62e9e | 533 | |
17116969 TH |
534 | /** |
535 | * for_each_pool - iterate through all worker_pools in the system | |
536 | * @pool: iteration cursor | |
611c92a0 | 537 | * @pi: integer used for iteration |
fa1b54e6 | 538 | * |
24acfb71 | 539 | * This must be called either with wq_pool_mutex held or RCU read |
68e13a67 LJ |
540 | * locked. If the pool needs to be used beyond the locking in effect, the |
541 | * caller is responsible for guaranteeing that the pool stays online. | |
fa1b54e6 TH |
542 | * |
543 | * The if/else clause exists only for the lockdep assertion and can be | |
544 | * ignored. | |
17116969 | 545 | */ |
611c92a0 TH |
546 | #define for_each_pool(pool, pi) \ |
547 | idr_for_each_entry(&worker_pool_idr, pool, pi) \ | |
68e13a67 | 548 | if (({ assert_rcu_or_pool_mutex(); false; })) { } \ |
fa1b54e6 | 549 | else |
17116969 | 550 | |
822d8405 TH |
551 | /** |
552 | * for_each_pool_worker - iterate through all workers of a worker_pool | |
553 | * @worker: iteration cursor | |
822d8405 TH |
554 | * @pool: worker_pool to iterate workers of |
555 | * | |
1258fae7 | 556 | * This must be called with wq_pool_attach_mutex. |
822d8405 TH |
557 | * |
558 | * The if/else clause exists only for the lockdep assertion and can be | |
559 | * ignored. | |
560 | */ | |
da028469 LJ |
561 | #define for_each_pool_worker(worker, pool) \ |
562 | list_for_each_entry((worker), &(pool)->workers, node) \ | |
1258fae7 | 563 | if (({ lockdep_assert_held(&wq_pool_attach_mutex); false; })) { } \ |
822d8405 TH |
564 | else |
565 | ||
49e3cf44 TH |
566 | /** |
567 | * for_each_pwq - iterate through all pool_workqueues of the specified workqueue | |
568 | * @pwq: iteration cursor | |
569 | * @wq: the target workqueue | |
76af4d93 | 570 | * |
24acfb71 | 571 | * This must be called either with wq->mutex held or RCU read locked. |
794b18bc TH |
572 | * If the pwq needs to be used beyond the locking in effect, the caller is |
573 | * responsible for guaranteeing that the pwq stays online. | |
76af4d93 TH |
574 | * |
575 | * The if/else clause exists only for the lockdep assertion and can be | |
576 | * ignored. | |
49e3cf44 TH |
577 | */ |
578 | #define for_each_pwq(pwq, wq) \ | |
49e9d1a9 | 579 | list_for_each_entry_rcu((pwq), &(wq)->pwqs, pwqs_node, \ |
5a644662 | 580 | lockdep_is_held(&(wq->mutex))) |
f3421797 | 581 | |
dc186ad7 TG |
582 | #ifdef CONFIG_DEBUG_OBJECTS_WORK |
583 | ||
f9e62f31 | 584 | static const struct debug_obj_descr work_debug_descr; |
dc186ad7 | 585 | |
99777288 SG |
586 | static void *work_debug_hint(void *addr) |
587 | { | |
588 | return ((struct work_struct *) addr)->func; | |
589 | } | |
590 | ||
b9fdac7f DC |
591 | static bool work_is_static_object(void *addr) |
592 | { | |
593 | struct work_struct *work = addr; | |
594 | ||
595 | return test_bit(WORK_STRUCT_STATIC_BIT, work_data_bits(work)); | |
596 | } | |
597 | ||
dc186ad7 TG |
598 | /* |
599 | * fixup_init is called when: | |
600 | * - an active object is initialized | |
601 | */ | |
02a982a6 | 602 | static bool work_fixup_init(void *addr, enum debug_obj_state state) |
dc186ad7 TG |
603 | { |
604 | struct work_struct *work = addr; | |
605 | ||
606 | switch (state) { | |
607 | case ODEBUG_STATE_ACTIVE: | |
608 | cancel_work_sync(work); | |
609 | debug_object_init(work, &work_debug_descr); | |
02a982a6 | 610 | return true; |
dc186ad7 | 611 | default: |
02a982a6 | 612 | return false; |
dc186ad7 TG |
613 | } |
614 | } | |
615 | ||
dc186ad7 TG |
616 | /* |
617 | * fixup_free is called when: | |
618 | * - an active object is freed | |
619 | */ | |
02a982a6 | 620 | static bool work_fixup_free(void *addr, enum debug_obj_state state) |
dc186ad7 TG |
621 | { |
622 | struct work_struct *work = addr; | |
623 | ||
624 | switch (state) { | |
625 | case ODEBUG_STATE_ACTIVE: | |
626 | cancel_work_sync(work); | |
627 | debug_object_free(work, &work_debug_descr); | |
02a982a6 | 628 | return true; |
dc186ad7 | 629 | default: |
02a982a6 | 630 | return false; |
dc186ad7 TG |
631 | } |
632 | } | |
633 | ||
f9e62f31 | 634 | static const struct debug_obj_descr work_debug_descr = { |
dc186ad7 | 635 | .name = "work_struct", |
99777288 | 636 | .debug_hint = work_debug_hint, |
b9fdac7f | 637 | .is_static_object = work_is_static_object, |
dc186ad7 | 638 | .fixup_init = work_fixup_init, |
dc186ad7 TG |
639 | .fixup_free = work_fixup_free, |
640 | }; | |
641 | ||
642 | static inline void debug_work_activate(struct work_struct *work) | |
643 | { | |
644 | debug_object_activate(work, &work_debug_descr); | |
645 | } | |
646 | ||
647 | static inline void debug_work_deactivate(struct work_struct *work) | |
648 | { | |
649 | debug_object_deactivate(work, &work_debug_descr); | |
650 | } | |
651 | ||
652 | void __init_work(struct work_struct *work, int onstack) | |
653 | { | |
654 | if (onstack) | |
655 | debug_object_init_on_stack(work, &work_debug_descr); | |
656 | else | |
657 | debug_object_init(work, &work_debug_descr); | |
658 | } | |
659 | EXPORT_SYMBOL_GPL(__init_work); | |
660 | ||
661 | void destroy_work_on_stack(struct work_struct *work) | |
662 | { | |
663 | debug_object_free(work, &work_debug_descr); | |
664 | } | |
665 | EXPORT_SYMBOL_GPL(destroy_work_on_stack); | |
666 | ||
ea2e64f2 TG |
667 | void destroy_delayed_work_on_stack(struct delayed_work *work) |
668 | { | |
669 | destroy_timer_on_stack(&work->timer); | |
670 | debug_object_free(&work->work, &work_debug_descr); | |
671 | } | |
672 | EXPORT_SYMBOL_GPL(destroy_delayed_work_on_stack); | |
673 | ||
dc186ad7 TG |
674 | #else |
675 | static inline void debug_work_activate(struct work_struct *work) { } | |
676 | static inline void debug_work_deactivate(struct work_struct *work) { } | |
677 | #endif | |
678 | ||
4e8b22bd | 679 | /** |
67dc8325 | 680 | * worker_pool_assign_id - allocate ID and assign it to @pool |
4e8b22bd LB |
681 | * @pool: the pool pointer of interest |
682 | * | |
683 | * Returns 0 if ID in [0, WORK_OFFQ_POOL_NONE) is allocated and assigned | |
684 | * successfully, -errno on failure. | |
685 | */ | |
9daf9e67 TH |
686 | static int worker_pool_assign_id(struct worker_pool *pool) |
687 | { | |
688 | int ret; | |
689 | ||
68e13a67 | 690 | lockdep_assert_held(&wq_pool_mutex); |
5bcab335 | 691 | |
4e8b22bd LB |
692 | ret = idr_alloc(&worker_pool_idr, pool, 0, WORK_OFFQ_POOL_NONE, |
693 | GFP_KERNEL); | |
229641a6 | 694 | if (ret >= 0) { |
e68035fb | 695 | pool->id = ret; |
229641a6 TH |
696 | return 0; |
697 | } | |
fa1b54e6 | 698 | return ret; |
7c3eed5c TH |
699 | } |
700 | ||
9f66cff2 TH |
701 | static struct pool_workqueue __rcu ** |
702 | unbound_pwq_slot(struct workqueue_struct *wq, int cpu) | |
703 | { | |
704 | if (cpu >= 0) | |
705 | return per_cpu_ptr(wq->cpu_pwq, cpu); | |
706 | else | |
707 | return &wq->dfl_pwq; | |
708 | } | |
709 | ||
710 | /* @cpu < 0 for dfl_pwq */ | |
711 | static struct pool_workqueue *unbound_pwq(struct workqueue_struct *wq, int cpu) | |
712 | { | |
713 | return rcu_dereference_check(*unbound_pwq_slot(wq, cpu), | |
714 | lockdep_is_held(&wq_pool_mutex) || | |
715 | lockdep_is_held(&wq->mutex)); | |
716 | } | |
717 | ||
5797b1c1 TH |
718 | /** |
719 | * unbound_effective_cpumask - effective cpumask of an unbound workqueue | |
720 | * @wq: workqueue of interest | |
721 | * | |
722 | * @wq->unbound_attrs->cpumask contains the cpumask requested by the user which | |
723 | * is masked with wq_unbound_cpumask to determine the effective cpumask. The | |
724 | * default pwq is always mapped to the pool with the current effective cpumask. | |
725 | */ | |
726 | static struct cpumask *unbound_effective_cpumask(struct workqueue_struct *wq) | |
727 | { | |
728 | return unbound_pwq(wq, -1)->pool->attrs->__pod_cpumask; | |
729 | } | |
730 | ||
73f53c4a TH |
731 | static unsigned int work_color_to_flags(int color) |
732 | { | |
733 | return color << WORK_STRUCT_COLOR_SHIFT; | |
734 | } | |
735 | ||
c4560c2c | 736 | static int get_work_color(unsigned long work_data) |
73f53c4a | 737 | { |
c4560c2c | 738 | return (work_data >> WORK_STRUCT_COLOR_SHIFT) & |
73f53c4a TH |
739 | ((1 << WORK_STRUCT_COLOR_BITS) - 1); |
740 | } | |
741 | ||
742 | static int work_next_color(int color) | |
743 | { | |
744 | return (color + 1) % WORK_NR_COLORS; | |
745 | } | |
1da177e4 | 746 | |
14441960 | 747 | /* |
112202d9 TH |
748 | * While queued, %WORK_STRUCT_PWQ is set and non flag bits of a work's data |
749 | * contain the pointer to the queued pwq. Once execution starts, the flag | |
7c3eed5c | 750 | * is cleared and the high bits contain OFFQ flags and pool ID. |
7a22ad75 | 751 | * |
112202d9 TH |
752 | * set_work_pwq(), set_work_pool_and_clear_pending(), mark_work_canceling() |
753 | * and clear_work_data() can be used to set the pwq, pool or clear | |
bbb68dfa TH |
754 | * work->data. These functions should only be called while the work is |
755 | * owned - ie. while the PENDING bit is set. | |
7a22ad75 | 756 | * |
112202d9 | 757 | * get_work_pool() and get_work_pwq() can be used to obtain the pool or pwq |
7c3eed5c | 758 | * corresponding to a work. Pool is available once the work has been |
112202d9 | 759 | * queued anywhere after initialization until it is sync canceled. pwq is |
7c3eed5c | 760 | * available only while the work item is queued. |
7a22ad75 | 761 | * |
bbb68dfa TH |
762 | * %WORK_OFFQ_CANCELING is used to mark a work item which is being |
763 | * canceled. While being canceled, a work item may have its PENDING set | |
764 | * but stay off timer and worklist for arbitrarily long and nobody should | |
765 | * try to steal the PENDING bit. | |
14441960 | 766 | */ |
7a22ad75 TH |
767 | static inline void set_work_data(struct work_struct *work, unsigned long data, |
768 | unsigned long flags) | |
365970a1 | 769 | { |
6183c009 | 770 | WARN_ON_ONCE(!work_pending(work)); |
7a22ad75 TH |
771 | atomic_long_set(&work->data, data | flags | work_static(work)); |
772 | } | |
365970a1 | 773 | |
112202d9 | 774 | static void set_work_pwq(struct work_struct *work, struct pool_workqueue *pwq, |
7a22ad75 TH |
775 | unsigned long extra_flags) |
776 | { | |
112202d9 TH |
777 | set_work_data(work, (unsigned long)pwq, |
778 | WORK_STRUCT_PENDING | WORK_STRUCT_PWQ | extra_flags); | |
365970a1 DH |
779 | } |
780 | ||
4468a00f LJ |
781 | static void set_work_pool_and_keep_pending(struct work_struct *work, |
782 | int pool_id) | |
783 | { | |
784 | set_work_data(work, (unsigned long)pool_id << WORK_OFFQ_POOL_SHIFT, | |
785 | WORK_STRUCT_PENDING); | |
786 | } | |
787 | ||
7c3eed5c TH |
788 | static void set_work_pool_and_clear_pending(struct work_struct *work, |
789 | int pool_id) | |
7a22ad75 | 790 | { |
23657bb1 TH |
791 | /* |
792 | * The following wmb is paired with the implied mb in | |
793 | * test_and_set_bit(PENDING) and ensures all updates to @work made | |
794 | * here are visible to and precede any updates by the next PENDING | |
795 | * owner. | |
796 | */ | |
797 | smp_wmb(); | |
7c3eed5c | 798 | set_work_data(work, (unsigned long)pool_id << WORK_OFFQ_POOL_SHIFT, 0); |
346c09f8 RP |
799 | /* |
800 | * The following mb guarantees that previous clear of a PENDING bit | |
801 | * will not be reordered with any speculative LOADS or STORES from | |
802 | * work->current_func, which is executed afterwards. This possible | |
8bdc6201 | 803 | * reordering can lead to a missed execution on attempt to queue |
346c09f8 RP |
804 | * the same @work. E.g. consider this case: |
805 | * | |
806 | * CPU#0 CPU#1 | |
807 | * ---------------------------- -------------------------------- | |
808 | * | |
809 | * 1 STORE event_indicated | |
810 | * 2 queue_work_on() { | |
811 | * 3 test_and_set_bit(PENDING) | |
812 | * 4 } set_..._and_clear_pending() { | |
813 | * 5 set_work_data() # clear bit | |
814 | * 6 smp_mb() | |
815 | * 7 work->current_func() { | |
816 | * 8 LOAD event_indicated | |
817 | * } | |
818 | * | |
819 | * Without an explicit full barrier speculative LOAD on line 8 can | |
820 | * be executed before CPU#0 does STORE on line 1. If that happens, | |
821 | * CPU#0 observes the PENDING bit is still set and new execution of | |
822 | * a @work is not queued in a hope, that CPU#1 will eventually | |
823 | * finish the queued @work. Meanwhile CPU#1 does not see | |
824 | * event_indicated is set, because speculative LOAD was executed | |
825 | * before actual STORE. | |
826 | */ | |
827 | smp_mb(); | |
7a22ad75 | 828 | } |
f756d5e2 | 829 | |
7a22ad75 | 830 | static void clear_work_data(struct work_struct *work) |
1da177e4 | 831 | { |
7c3eed5c TH |
832 | smp_wmb(); /* see set_work_pool_and_clear_pending() */ |
833 | set_work_data(work, WORK_STRUCT_NO_POOL, 0); | |
1da177e4 LT |
834 | } |
835 | ||
afa4bb77 LT |
836 | static inline struct pool_workqueue *work_struct_pwq(unsigned long data) |
837 | { | |
838 | return (struct pool_workqueue *)(data & WORK_STRUCT_WQ_DATA_MASK); | |
839 | } | |
840 | ||
112202d9 | 841 | static struct pool_workqueue *get_work_pwq(struct work_struct *work) |
b1f4ec17 | 842 | { |
e120153d | 843 | unsigned long data = atomic_long_read(&work->data); |
7a22ad75 | 844 | |
112202d9 | 845 | if (data & WORK_STRUCT_PWQ) |
afa4bb77 | 846 | return work_struct_pwq(data); |
e120153d TH |
847 | else |
848 | return NULL; | |
4d707b9f ON |
849 | } |
850 | ||
7c3eed5c TH |
851 | /** |
852 | * get_work_pool - return the worker_pool a given work was associated with | |
853 | * @work: the work item of interest | |
854 | * | |
68e13a67 | 855 | * Pools are created and destroyed under wq_pool_mutex, and allows read |
24acfb71 TG |
856 | * access under RCU read lock. As such, this function should be |
857 | * called under wq_pool_mutex or inside of a rcu_read_lock() region. | |
fa1b54e6 TH |
858 | * |
859 | * All fields of the returned pool are accessible as long as the above | |
860 | * mentioned locking is in effect. If the returned pool needs to be used | |
861 | * beyond the critical section, the caller is responsible for ensuring the | |
862 | * returned pool is and stays online. | |
d185af30 YB |
863 | * |
864 | * Return: The worker_pool @work was last associated with. %NULL if none. | |
7c3eed5c TH |
865 | */ |
866 | static struct worker_pool *get_work_pool(struct work_struct *work) | |
365970a1 | 867 | { |
e120153d | 868 | unsigned long data = atomic_long_read(&work->data); |
7c3eed5c | 869 | int pool_id; |
7a22ad75 | 870 | |
68e13a67 | 871 | assert_rcu_or_pool_mutex(); |
fa1b54e6 | 872 | |
112202d9 | 873 | if (data & WORK_STRUCT_PWQ) |
afa4bb77 | 874 | return work_struct_pwq(data)->pool; |
7a22ad75 | 875 | |
7c3eed5c TH |
876 | pool_id = data >> WORK_OFFQ_POOL_SHIFT; |
877 | if (pool_id == WORK_OFFQ_POOL_NONE) | |
7a22ad75 TH |
878 | return NULL; |
879 | ||
fa1b54e6 | 880 | return idr_find(&worker_pool_idr, pool_id); |
7c3eed5c TH |
881 | } |
882 | ||
883 | /** | |
884 | * get_work_pool_id - return the worker pool ID a given work is associated with | |
885 | * @work: the work item of interest | |
886 | * | |
d185af30 | 887 | * Return: The worker_pool ID @work was last associated with. |
7c3eed5c TH |
888 | * %WORK_OFFQ_POOL_NONE if none. |
889 | */ | |
890 | static int get_work_pool_id(struct work_struct *work) | |
891 | { | |
54d5b7d0 LJ |
892 | unsigned long data = atomic_long_read(&work->data); |
893 | ||
112202d9 | 894 | if (data & WORK_STRUCT_PWQ) |
afa4bb77 | 895 | return work_struct_pwq(data)->pool->id; |
7c3eed5c | 896 | |
54d5b7d0 | 897 | return data >> WORK_OFFQ_POOL_SHIFT; |
7c3eed5c TH |
898 | } |
899 | ||
bbb68dfa TH |
900 | static void mark_work_canceling(struct work_struct *work) |
901 | { | |
7c3eed5c | 902 | unsigned long pool_id = get_work_pool_id(work); |
bbb68dfa | 903 | |
7c3eed5c TH |
904 | pool_id <<= WORK_OFFQ_POOL_SHIFT; |
905 | set_work_data(work, pool_id | WORK_OFFQ_CANCELING, WORK_STRUCT_PENDING); | |
bbb68dfa TH |
906 | } |
907 | ||
908 | static bool work_is_canceling(struct work_struct *work) | |
909 | { | |
910 | unsigned long data = atomic_long_read(&work->data); | |
911 | ||
112202d9 | 912 | return !(data & WORK_STRUCT_PWQ) && (data & WORK_OFFQ_CANCELING); |
bbb68dfa TH |
913 | } |
914 | ||
e22bee78 | 915 | /* |
3270476a TH |
916 | * Policy functions. These define the policies on how the global worker |
917 | * pools are managed. Unless noted otherwise, these functions assume that | |
d565ed63 | 918 | * they're being called with pool->lock held. |
e22bee78 TH |
919 | */ |
920 | ||
4594bf15 | 921 | /* |
e22bee78 TH |
922 | * Need to wake up a worker? Called from anything but currently |
923 | * running workers. | |
974271c4 TH |
924 | * |
925 | * Note that, because unbound workers never contribute to nr_running, this | |
706026c2 | 926 | * function will always return %true for unbound pools as long as the |
974271c4 | 927 | * worklist isn't empty. |
4594bf15 | 928 | */ |
63d95a91 | 929 | static bool need_more_worker(struct worker_pool *pool) |
365970a1 | 930 | { |
0219a352 | 931 | return !list_empty(&pool->worklist) && !pool->nr_running; |
e22bee78 | 932 | } |
4594bf15 | 933 | |
e22bee78 | 934 | /* Can I start working? Called from busy but !running workers. */ |
63d95a91 | 935 | static bool may_start_working(struct worker_pool *pool) |
e22bee78 | 936 | { |
63d95a91 | 937 | return pool->nr_idle; |
e22bee78 TH |
938 | } |
939 | ||
940 | /* Do I need to keep working? Called from currently running workers. */ | |
63d95a91 | 941 | static bool keep_working(struct worker_pool *pool) |
e22bee78 | 942 | { |
bc35f7ef | 943 | return !list_empty(&pool->worklist) && (pool->nr_running <= 1); |
e22bee78 TH |
944 | } |
945 | ||
946 | /* Do we need a new worker? Called from manager. */ | |
63d95a91 | 947 | static bool need_to_create_worker(struct worker_pool *pool) |
e22bee78 | 948 | { |
63d95a91 | 949 | return need_more_worker(pool) && !may_start_working(pool); |
e22bee78 | 950 | } |
365970a1 | 951 | |
e22bee78 | 952 | /* Do we have too many workers and should some go away? */ |
63d95a91 | 953 | static bool too_many_workers(struct worker_pool *pool) |
e22bee78 | 954 | { |
692b4825 | 955 | bool managing = pool->flags & POOL_MANAGER_ACTIVE; |
63d95a91 TH |
956 | int nr_idle = pool->nr_idle + managing; /* manager is considered idle */ |
957 | int nr_busy = pool->nr_workers - nr_idle; | |
e22bee78 TH |
958 | |
959 | return nr_idle > 2 && (nr_idle - 2) * MAX_IDLE_WORKERS_RATIO >= nr_busy; | |
365970a1 DH |
960 | } |
961 | ||
c54d5046 TH |
962 | /** |
963 | * worker_set_flags - set worker flags and adjust nr_running accordingly | |
964 | * @worker: self | |
965 | * @flags: flags to set | |
966 | * | |
967 | * Set @flags in @worker->flags and adjust nr_running accordingly. | |
c54d5046 TH |
968 | */ |
969 | static inline void worker_set_flags(struct worker *worker, unsigned int flags) | |
970 | { | |
971 | struct worker_pool *pool = worker->pool; | |
972 | ||
bc8b50c2 | 973 | lockdep_assert_held(&pool->lock); |
c54d5046 TH |
974 | |
975 | /* If transitioning into NOT_RUNNING, adjust nr_running. */ | |
976 | if ((flags & WORKER_NOT_RUNNING) && | |
977 | !(worker->flags & WORKER_NOT_RUNNING)) { | |
978 | pool->nr_running--; | |
979 | } | |
980 | ||
981 | worker->flags |= flags; | |
982 | } | |
983 | ||
984 | /** | |
985 | * worker_clr_flags - clear worker flags and adjust nr_running accordingly | |
986 | * @worker: self | |
987 | * @flags: flags to clear | |
988 | * | |
989 | * Clear @flags in @worker->flags and adjust nr_running accordingly. | |
c54d5046 TH |
990 | */ |
991 | static inline void worker_clr_flags(struct worker *worker, unsigned int flags) | |
992 | { | |
993 | struct worker_pool *pool = worker->pool; | |
994 | unsigned int oflags = worker->flags; | |
995 | ||
bc8b50c2 | 996 | lockdep_assert_held(&pool->lock); |
c54d5046 TH |
997 | |
998 | worker->flags &= ~flags; | |
999 | ||
1000 | /* | |
1001 | * If transitioning out of NOT_RUNNING, increment nr_running. Note | |
1002 | * that the nested NOT_RUNNING is not a noop. NOT_RUNNING is mask | |
1003 | * of multiple flags, not a single flag. | |
1004 | */ | |
1005 | if ((flags & WORKER_NOT_RUNNING) && (oflags & WORKER_NOT_RUNNING)) | |
1006 | if (!(worker->flags & WORKER_NOT_RUNNING)) | |
1007 | pool->nr_running++; | |
1008 | } | |
1009 | ||
797e8345 TH |
1010 | /* Return the first idle worker. Called with pool->lock held. */ |
1011 | static struct worker *first_idle_worker(struct worker_pool *pool) | |
1012 | { | |
1013 | if (unlikely(list_empty(&pool->idle_list))) | |
1014 | return NULL; | |
1015 | ||
1016 | return list_first_entry(&pool->idle_list, struct worker, entry); | |
1017 | } | |
1018 | ||
1019 | /** | |
1020 | * worker_enter_idle - enter idle state | |
1021 | * @worker: worker which is entering idle state | |
1022 | * | |
1023 | * @worker is entering idle state. Update stats and idle timer if | |
1024 | * necessary. | |
1025 | * | |
1026 | * LOCKING: | |
1027 | * raw_spin_lock_irq(pool->lock). | |
1028 | */ | |
1029 | static void worker_enter_idle(struct worker *worker) | |
1030 | { | |
1031 | struct worker_pool *pool = worker->pool; | |
1032 | ||
1033 | if (WARN_ON_ONCE(worker->flags & WORKER_IDLE) || | |
1034 | WARN_ON_ONCE(!list_empty(&worker->entry) && | |
1035 | (worker->hentry.next || worker->hentry.pprev))) | |
1036 | return; | |
1037 | ||
1038 | /* can't use worker_set_flags(), also called from create_worker() */ | |
1039 | worker->flags |= WORKER_IDLE; | |
1040 | pool->nr_idle++; | |
1041 | worker->last_active = jiffies; | |
1042 | ||
1043 | /* idle_list is LIFO */ | |
1044 | list_add(&worker->entry, &pool->idle_list); | |
1045 | ||
1046 | if (too_many_workers(pool) && !timer_pending(&pool->idle_timer)) | |
1047 | mod_timer(&pool->idle_timer, jiffies + IDLE_WORKER_TIMEOUT); | |
1048 | ||
1049 | /* Sanity check nr_running. */ | |
1050 | WARN_ON_ONCE(pool->nr_workers == pool->nr_idle && pool->nr_running); | |
1051 | } | |
1052 | ||
1053 | /** | |
1054 | * worker_leave_idle - leave idle state | |
1055 | * @worker: worker which is leaving idle state | |
1056 | * | |
1057 | * @worker is leaving idle state. Update stats. | |
1058 | * | |
1059 | * LOCKING: | |
1060 | * raw_spin_lock_irq(pool->lock). | |
1061 | */ | |
1062 | static void worker_leave_idle(struct worker *worker) | |
1063 | { | |
1064 | struct worker_pool *pool = worker->pool; | |
1065 | ||
1066 | if (WARN_ON_ONCE(!(worker->flags & WORKER_IDLE))) | |
1067 | return; | |
1068 | worker_clr_flags(worker, WORKER_IDLE); | |
1069 | pool->nr_idle--; | |
1070 | list_del_init(&worker->entry); | |
1071 | } | |
1072 | ||
1073 | /** | |
1074 | * find_worker_executing_work - find worker which is executing a work | |
1075 | * @pool: pool of interest | |
1076 | * @work: work to find worker for | |
1077 | * | |
1078 | * Find a worker which is executing @work on @pool by searching | |
1079 | * @pool->busy_hash which is keyed by the address of @work. For a worker | |
1080 | * to match, its current execution should match the address of @work and | |
1081 | * its work function. This is to avoid unwanted dependency between | |
1082 | * unrelated work executions through a work item being recycled while still | |
1083 | * being executed. | |
1084 | * | |
1085 | * This is a bit tricky. A work item may be freed once its execution | |
1086 | * starts and nothing prevents the freed area from being recycled for | |
1087 | * another work item. If the same work item address ends up being reused | |
1088 | * before the original execution finishes, workqueue will identify the | |
1089 | * recycled work item as currently executing and make it wait until the | |
1090 | * current execution finishes, introducing an unwanted dependency. | |
1091 | * | |
1092 | * This function checks the work item address and work function to avoid | |
1093 | * false positives. Note that this isn't complete as one may construct a | |
1094 | * work function which can introduce dependency onto itself through a | |
1095 | * recycled work item. Well, if somebody wants to shoot oneself in the | |
1096 | * foot that badly, there's only so much we can do, and if such deadlock | |
1097 | * actually occurs, it should be easy to locate the culprit work function. | |
1098 | * | |
1099 | * CONTEXT: | |
1100 | * raw_spin_lock_irq(pool->lock). | |
1101 | * | |
1102 | * Return: | |
1103 | * Pointer to worker which is executing @work if found, %NULL | |
1104 | * otherwise. | |
1105 | */ | |
1106 | static struct worker *find_worker_executing_work(struct worker_pool *pool, | |
1107 | struct work_struct *work) | |
1108 | { | |
1109 | struct worker *worker; | |
1110 | ||
1111 | hash_for_each_possible(pool->busy_hash, worker, hentry, | |
1112 | (unsigned long)work) | |
1113 | if (worker->current_work == work && | |
1114 | worker->current_func == work->func) | |
1115 | return worker; | |
1116 | ||
1117 | return NULL; | |
1118 | } | |
1119 | ||
1120 | /** | |
1121 | * move_linked_works - move linked works to a list | |
1122 | * @work: start of series of works to be scheduled | |
1123 | * @head: target list to append @work to | |
1124 | * @nextp: out parameter for nested worklist walking | |
1125 | * | |
873eaca6 TH |
1126 | * Schedule linked works starting from @work to @head. Work series to be |
1127 | * scheduled starts at @work and includes any consecutive work with | |
1128 | * WORK_STRUCT_LINKED set in its predecessor. See assign_work() for details on | |
1129 | * @nextp. | |
797e8345 TH |
1130 | * |
1131 | * CONTEXT: | |
1132 | * raw_spin_lock_irq(pool->lock). | |
1133 | */ | |
1134 | static void move_linked_works(struct work_struct *work, struct list_head *head, | |
1135 | struct work_struct **nextp) | |
1136 | { | |
1137 | struct work_struct *n; | |
1138 | ||
1139 | /* | |
1140 | * Linked worklist will always end before the end of the list, | |
1141 | * use NULL for list head. | |
1142 | */ | |
1143 | list_for_each_entry_safe_from(work, n, NULL, entry) { | |
1144 | list_move_tail(&work->entry, head); | |
1145 | if (!(*work_data_bits(work) & WORK_STRUCT_LINKED)) | |
1146 | break; | |
1147 | } | |
1148 | ||
1149 | /* | |
1150 | * If we're already inside safe list traversal and have moved | |
1151 | * multiple works to the scheduled queue, the next position | |
1152 | * needs to be updated. | |
1153 | */ | |
1154 | if (nextp) | |
1155 | *nextp = n; | |
1156 | } | |
1157 | ||
873eaca6 TH |
1158 | /** |
1159 | * assign_work - assign a work item and its linked work items to a worker | |
1160 | * @work: work to assign | |
1161 | * @worker: worker to assign to | |
1162 | * @nextp: out parameter for nested worklist walking | |
1163 | * | |
1164 | * Assign @work and its linked work items to @worker. If @work is already being | |
1165 | * executed by another worker in the same pool, it'll be punted there. | |
1166 | * | |
1167 | * If @nextp is not NULL, it's updated to point to the next work of the last | |
1168 | * scheduled work. This allows assign_work() to be nested inside | |
1169 | * list_for_each_entry_safe(). | |
1170 | * | |
1171 | * Returns %true if @work was successfully assigned to @worker. %false if @work | |
1172 | * was punted to another worker already executing it. | |
1173 | */ | |
1174 | static bool assign_work(struct work_struct *work, struct worker *worker, | |
1175 | struct work_struct **nextp) | |
1176 | { | |
1177 | struct worker_pool *pool = worker->pool; | |
1178 | struct worker *collision; | |
1179 | ||
1180 | lockdep_assert_held(&pool->lock); | |
1181 | ||
1182 | /* | |
1183 | * A single work shouldn't be executed concurrently by multiple workers. | |
1184 | * __queue_work() ensures that @work doesn't jump to a different pool | |
1185 | * while still running in the previous pool. Here, we should ensure that | |
1186 | * @work is not executed concurrently by multiple workers from the same | |
1187 | * pool. Check whether anyone is already processing the work. If so, | |
1188 | * defer the work to the currently executing one. | |
1189 | */ | |
1190 | collision = find_worker_executing_work(pool, work); | |
1191 | if (unlikely(collision)) { | |
1192 | move_linked_works(work, &collision->scheduled, nextp); | |
1193 | return false; | |
1194 | } | |
1195 | ||
1196 | move_linked_works(work, &worker->scheduled, nextp); | |
1197 | return true; | |
1198 | } | |
1199 | ||
797e8345 | 1200 | /** |
0219a352 TH |
1201 | * kick_pool - wake up an idle worker if necessary |
1202 | * @pool: pool to kick | |
797e8345 | 1203 | * |
0219a352 TH |
1204 | * @pool may have pending work items. Wake up worker if necessary. Returns |
1205 | * whether a worker was woken up. | |
797e8345 | 1206 | */ |
0219a352 | 1207 | static bool kick_pool(struct worker_pool *pool) |
797e8345 TH |
1208 | { |
1209 | struct worker *worker = first_idle_worker(pool); | |
8639eceb | 1210 | struct task_struct *p; |
797e8345 | 1211 | |
0219a352 TH |
1212 | lockdep_assert_held(&pool->lock); |
1213 | ||
1214 | if (!need_more_worker(pool) || !worker) | |
1215 | return false; | |
1216 | ||
4cb1ef64 TH |
1217 | if (pool->flags & POOL_BH) { |
1218 | if (pool->attrs->nice == HIGHPRI_NICE_LEVEL) | |
1219 | raise_softirq_irqoff(HI_SOFTIRQ); | |
1220 | else | |
1221 | raise_softirq_irqoff(TASKLET_SOFTIRQ); | |
1222 | return true; | |
1223 | } | |
1224 | ||
8639eceb TH |
1225 | p = worker->task; |
1226 | ||
1227 | #ifdef CONFIG_SMP | |
1228 | /* | |
1229 | * Idle @worker is about to execute @work and waking up provides an | |
1230 | * opportunity to migrate @worker at a lower cost by setting the task's | |
1231 | * wake_cpu field. Let's see if we want to move @worker to improve | |
1232 | * execution locality. | |
1233 | * | |
1234 | * We're waking the worker that went idle the latest and there's some | |
1235 | * chance that @worker is marked idle but hasn't gone off CPU yet. If | |
1236 | * so, setting the wake_cpu won't do anything. As this is a best-effort | |
1237 | * optimization and the race window is narrow, let's leave as-is for | |
1238 | * now. If this becomes pronounced, we can skip over workers which are | |
1239 | * still on cpu when picking an idle worker. | |
1240 | * | |
1241 | * If @pool has non-strict affinity, @worker might have ended up outside | |
1242 | * its affinity scope. Repatriate. | |
1243 | */ | |
1244 | if (!pool->attrs->affn_strict && | |
1245 | !cpumask_test_cpu(p->wake_cpu, pool->attrs->__pod_cpumask)) { | |
1246 | struct work_struct *work = list_first_entry(&pool->worklist, | |
1247 | struct work_struct, entry); | |
1248 | p->wake_cpu = cpumask_any_distribute(pool->attrs->__pod_cpumask); | |
1249 | get_work_pwq(work)->stats[PWQ_STAT_REPATRIATED]++; | |
1250 | } | |
1251 | #endif | |
1252 | wake_up_process(p); | |
0219a352 | 1253 | return true; |
797e8345 TH |
1254 | } |
1255 | ||
63638450 TH |
1256 | #ifdef CONFIG_WQ_CPU_INTENSIVE_REPORT |
1257 | ||
1258 | /* | |
1259 | * Concurrency-managed per-cpu work items that hog CPU for longer than | |
1260 | * wq_cpu_intensive_thresh_us trigger the automatic CPU_INTENSIVE mechanism, | |
1261 | * which prevents them from stalling other concurrency-managed work items. If a | |
1262 | * work function keeps triggering this mechanism, it's likely that the work item | |
1263 | * should be using an unbound workqueue instead. | |
1264 | * | |
1265 | * wq_cpu_intensive_report() tracks work functions which trigger such conditions | |
1266 | * and report them so that they can be examined and converted to use unbound | |
1267 | * workqueues as appropriate. To avoid flooding the console, each violating work | |
1268 | * function is tracked and reported with exponential backoff. | |
1269 | */ | |
1270 | #define WCI_MAX_ENTS 128 | |
1271 | ||
1272 | struct wci_ent { | |
1273 | work_func_t func; | |
1274 | atomic64_t cnt; | |
1275 | struct hlist_node hash_node; | |
1276 | }; | |
1277 | ||
1278 | static struct wci_ent wci_ents[WCI_MAX_ENTS]; | |
1279 | static int wci_nr_ents; | |
1280 | static DEFINE_RAW_SPINLOCK(wci_lock); | |
1281 | static DEFINE_HASHTABLE(wci_hash, ilog2(WCI_MAX_ENTS)); | |
1282 | ||
1283 | static struct wci_ent *wci_find_ent(work_func_t func) | |
1284 | { | |
1285 | struct wci_ent *ent; | |
1286 | ||
1287 | hash_for_each_possible_rcu(wci_hash, ent, hash_node, | |
1288 | (unsigned long)func) { | |
1289 | if (ent->func == func) | |
1290 | return ent; | |
1291 | } | |
1292 | return NULL; | |
1293 | } | |
1294 | ||
1295 | static void wq_cpu_intensive_report(work_func_t func) | |
1296 | { | |
1297 | struct wci_ent *ent; | |
1298 | ||
1299 | restart: | |
1300 | ent = wci_find_ent(func); | |
1301 | if (ent) { | |
1302 | u64 cnt; | |
1303 | ||
1304 | /* | |
1305 | * Start reporting from the fourth time and back off | |
1306 | * exponentially. | |
1307 | */ | |
1308 | cnt = atomic64_inc_return_relaxed(&ent->cnt); | |
1309 | if (cnt >= 4 && is_power_of_2(cnt)) | |
1310 | printk_deferred(KERN_WARNING "workqueue: %ps hogged CPU for >%luus %llu times, consider switching to WQ_UNBOUND\n", | |
1311 | ent->func, wq_cpu_intensive_thresh_us, | |
1312 | atomic64_read(&ent->cnt)); | |
1313 | return; | |
1314 | } | |
1315 | ||
1316 | /* | |
1317 | * @func is a new violation. Allocate a new entry for it. If wcn_ents[] | |
1318 | * is exhausted, something went really wrong and we probably made enough | |
1319 | * noise already. | |
1320 | */ | |
1321 | if (wci_nr_ents >= WCI_MAX_ENTS) | |
1322 | return; | |
1323 | ||
1324 | raw_spin_lock(&wci_lock); | |
1325 | ||
1326 | if (wci_nr_ents >= WCI_MAX_ENTS) { | |
1327 | raw_spin_unlock(&wci_lock); | |
1328 | return; | |
1329 | } | |
1330 | ||
1331 | if (wci_find_ent(func)) { | |
1332 | raw_spin_unlock(&wci_lock); | |
1333 | goto restart; | |
1334 | } | |
1335 | ||
1336 | ent = &wci_ents[wci_nr_ents++]; | |
1337 | ent->func = func; | |
1338 | atomic64_set(&ent->cnt, 1); | |
1339 | hash_add_rcu(wci_hash, &ent->hash_node, (unsigned long)func); | |
1340 | ||
1341 | raw_spin_unlock(&wci_lock); | |
1342 | } | |
1343 | ||
1344 | #else /* CONFIG_WQ_CPU_INTENSIVE_REPORT */ | |
1345 | static void wq_cpu_intensive_report(work_func_t func) {} | |
1346 | #endif /* CONFIG_WQ_CPU_INTENSIVE_REPORT */ | |
1347 | ||
d302f017 | 1348 | /** |
6d25be57 | 1349 | * wq_worker_running - a worker is running again |
e22bee78 | 1350 | * @task: task waking up |
e22bee78 | 1351 | * |
6d25be57 | 1352 | * This function is called when a worker returns from schedule() |
e22bee78 | 1353 | */ |
6d25be57 | 1354 | void wq_worker_running(struct task_struct *task) |
e22bee78 TH |
1355 | { |
1356 | struct worker *worker = kthread_data(task); | |
1357 | ||
c8f6219b | 1358 | if (!READ_ONCE(worker->sleeping)) |
6d25be57 | 1359 | return; |
07edfece FW |
1360 | |
1361 | /* | |
1362 | * If preempted by unbind_workers() between the WORKER_NOT_RUNNING check | |
1363 | * and the nr_running increment below, we may ruin the nr_running reset | |
1364 | * and leave with an unexpected pool->nr_running == 1 on the newly unbound | |
1365 | * pool. Protect against such race. | |
1366 | */ | |
1367 | preempt_disable(); | |
6d25be57 | 1368 | if (!(worker->flags & WORKER_NOT_RUNNING)) |
bc35f7ef | 1369 | worker->pool->nr_running++; |
07edfece | 1370 | preempt_enable(); |
616db877 TH |
1371 | |
1372 | /* | |
1373 | * CPU intensive auto-detection cares about how long a work item hogged | |
1374 | * CPU without sleeping. Reset the starting timestamp on wakeup. | |
1375 | */ | |
1376 | worker->current_at = worker->task->se.sum_exec_runtime; | |
1377 | ||
c8f6219b | 1378 | WRITE_ONCE(worker->sleeping, 0); |
e22bee78 TH |
1379 | } |
1380 | ||
1381 | /** | |
1382 | * wq_worker_sleeping - a worker is going to sleep | |
1383 | * @task: task going to sleep | |
e22bee78 | 1384 | * |
6d25be57 | 1385 | * This function is called from schedule() when a busy worker is |
ccf45156 | 1386 | * going to sleep. |
e22bee78 | 1387 | */ |
6d25be57 | 1388 | void wq_worker_sleeping(struct task_struct *task) |
e22bee78 | 1389 | { |
cc5bff38 | 1390 | struct worker *worker = kthread_data(task); |
111c225a | 1391 | struct worker_pool *pool; |
e22bee78 | 1392 | |
111c225a TH |
1393 | /* |
1394 | * Rescuers, which may not have all the fields set up like normal | |
1395 | * workers, also reach here, let's not access anything before | |
1396 | * checking NOT_RUNNING. | |
1397 | */ | |
2d64672e | 1398 | if (worker->flags & WORKER_NOT_RUNNING) |
6d25be57 | 1399 | return; |
e22bee78 | 1400 | |
111c225a | 1401 | pool = worker->pool; |
111c225a | 1402 | |
62849a96 | 1403 | /* Return if preempted before wq_worker_running() was reached */ |
c8f6219b | 1404 | if (READ_ONCE(worker->sleeping)) |
6d25be57 TG |
1405 | return; |
1406 | ||
c8f6219b | 1407 | WRITE_ONCE(worker->sleeping, 1); |
a9b8a985 | 1408 | raw_spin_lock_irq(&pool->lock); |
e22bee78 | 1409 | |
45c753f5 FW |
1410 | /* |
1411 | * Recheck in case unbind_workers() preempted us. We don't | |
1412 | * want to decrement nr_running after the worker is unbound | |
1413 | * and nr_running has been reset. | |
1414 | */ | |
1415 | if (worker->flags & WORKER_NOT_RUNNING) { | |
1416 | raw_spin_unlock_irq(&pool->lock); | |
1417 | return; | |
1418 | } | |
1419 | ||
bc35f7ef | 1420 | pool->nr_running--; |
0219a352 | 1421 | if (kick_pool(pool)) |
725e8ec5 | 1422 | worker->current_pwq->stats[PWQ_STAT_CM_WAKEUP]++; |
0219a352 | 1423 | |
a9b8a985 | 1424 | raw_spin_unlock_irq(&pool->lock); |
e22bee78 TH |
1425 | } |
1426 | ||
616db877 TH |
1427 | /** |
1428 | * wq_worker_tick - a scheduler tick occurred while a kworker is running | |
1429 | * @task: task currently running | |
1430 | * | |
1431 | * Called from scheduler_tick(). We're in the IRQ context and the current | |
1432 | * worker's fields which follow the 'K' locking rule can be accessed safely. | |
1433 | */ | |
1434 | void wq_worker_tick(struct task_struct *task) | |
1435 | { | |
1436 | struct worker *worker = kthread_data(task); | |
1437 | struct pool_workqueue *pwq = worker->current_pwq; | |
1438 | struct worker_pool *pool = worker->pool; | |
1439 | ||
1440 | if (!pwq) | |
1441 | return; | |
1442 | ||
8a1dd1e5 TH |
1443 | pwq->stats[PWQ_STAT_CPU_TIME] += TICK_USEC; |
1444 | ||
18c8ae81 Z |
1445 | if (!wq_cpu_intensive_thresh_us) |
1446 | return; | |
1447 | ||
616db877 TH |
1448 | /* |
1449 | * If the current worker is concurrency managed and hogged the CPU for | |
1450 | * longer than wq_cpu_intensive_thresh_us, it's automatically marked | |
1451 | * CPU_INTENSIVE to avoid stalling other concurrency-managed work items. | |
c8f6219b Z |
1452 | * |
1453 | * Set @worker->sleeping means that @worker is in the process of | |
1454 | * switching out voluntarily and won't be contributing to | |
1455 | * @pool->nr_running until it wakes up. As wq_worker_sleeping() also | |
1456 | * decrements ->nr_running, setting CPU_INTENSIVE here can lead to | |
1457 | * double decrements. The task is releasing the CPU anyway. Let's skip. | |
1458 | * We probably want to make this prettier in the future. | |
616db877 | 1459 | */ |
c8f6219b | 1460 | if ((worker->flags & WORKER_NOT_RUNNING) || READ_ONCE(worker->sleeping) || |
616db877 TH |
1461 | worker->task->se.sum_exec_runtime - worker->current_at < |
1462 | wq_cpu_intensive_thresh_us * NSEC_PER_USEC) | |
1463 | return; | |
1464 | ||
1465 | raw_spin_lock(&pool->lock); | |
1466 | ||
1467 | worker_set_flags(worker, WORKER_CPU_INTENSIVE); | |
63638450 | 1468 | wq_cpu_intensive_report(worker->current_func); |
616db877 TH |
1469 | pwq->stats[PWQ_STAT_CPU_INTENSIVE]++; |
1470 | ||
0219a352 | 1471 | if (kick_pool(pool)) |
616db877 | 1472 | pwq->stats[PWQ_STAT_CM_WAKEUP]++; |
616db877 TH |
1473 | |
1474 | raw_spin_unlock(&pool->lock); | |
1475 | } | |
1476 | ||
1b69ac6b JW |
1477 | /** |
1478 | * wq_worker_last_func - retrieve worker's last work function | |
8194fe94 | 1479 | * @task: Task to retrieve last work function of. |
1b69ac6b JW |
1480 | * |
1481 | * Determine the last function a worker executed. This is called from | |
1482 | * the scheduler to get a worker's last known identity. | |
1483 | * | |
1484 | * CONTEXT: | |
a9b8a985 | 1485 | * raw_spin_lock_irq(rq->lock) |
1b69ac6b | 1486 | * |
4b047002 JW |
1487 | * This function is called during schedule() when a kworker is going |
1488 | * to sleep. It's used by psi to identify aggregation workers during | |
1489 | * dequeuing, to allow periodic aggregation to shut-off when that | |
1490 | * worker is the last task in the system or cgroup to go to sleep. | |
1491 | * | |
1492 | * As this function doesn't involve any workqueue-related locking, it | |
1493 | * only returns stable values when called from inside the scheduler's | |
1494 | * queuing and dequeuing paths, when @task, which must be a kworker, | |
1495 | * is guaranteed to not be processing any works. | |
1496 | * | |
1b69ac6b JW |
1497 | * Return: |
1498 | * The last work function %current executed as a worker, NULL if it | |
1499 | * hasn't executed any work yet. | |
1500 | */ | |
1501 | work_func_t wq_worker_last_func(struct task_struct *task) | |
1502 | { | |
1503 | struct worker *worker = kthread_data(task); | |
1504 | ||
1505 | return worker->last_func; | |
1506 | } | |
1507 | ||
91ccc6e7 TH |
1508 | /** |
1509 | * wq_node_nr_active - Determine wq_node_nr_active to use | |
1510 | * @wq: workqueue of interest | |
1511 | * @node: NUMA node, can be %NUMA_NO_NODE | |
1512 | * | |
1513 | * Determine wq_node_nr_active to use for @wq on @node. Returns: | |
1514 | * | |
1515 | * - %NULL for per-cpu workqueues as they don't need to use shared nr_active. | |
1516 | * | |
1517 | * - node_nr_active[nr_node_ids] if @node is %NUMA_NO_NODE. | |
1518 | * | |
1519 | * - Otherwise, node_nr_active[@node]. | |
1520 | */ | |
1521 | static struct wq_node_nr_active *wq_node_nr_active(struct workqueue_struct *wq, | |
1522 | int node) | |
1523 | { | |
1524 | if (!(wq->flags & WQ_UNBOUND)) | |
1525 | return NULL; | |
1526 | ||
1527 | if (node == NUMA_NO_NODE) | |
1528 | node = nr_node_ids; | |
1529 | ||
1530 | return wq->node_nr_active[node]; | |
1531 | } | |
1532 | ||
5797b1c1 TH |
1533 | /** |
1534 | * wq_update_node_max_active - Update per-node max_actives to use | |
1535 | * @wq: workqueue to update | |
1536 | * @off_cpu: CPU that's going down, -1 if a CPU is not going down | |
1537 | * | |
1538 | * Update @wq->node_nr_active[]->max. @wq must be unbound. max_active is | |
1539 | * distributed among nodes according to the proportions of numbers of online | |
1540 | * cpus. The result is always between @wq->min_active and max_active. | |
1541 | */ | |
1542 | static void wq_update_node_max_active(struct workqueue_struct *wq, int off_cpu) | |
1543 | { | |
1544 | struct cpumask *effective = unbound_effective_cpumask(wq); | |
1545 | int min_active = READ_ONCE(wq->min_active); | |
1546 | int max_active = READ_ONCE(wq->max_active); | |
1547 | int total_cpus, node; | |
1548 | ||
1549 | lockdep_assert_held(&wq->mutex); | |
1550 | ||
c5f8cd6c TH |
1551 | if (!wq_topo_initialized) |
1552 | return; | |
1553 | ||
15930da4 | 1554 | if (off_cpu >= 0 && !cpumask_test_cpu(off_cpu, effective)) |
5797b1c1 TH |
1555 | off_cpu = -1; |
1556 | ||
1557 | total_cpus = cpumask_weight_and(effective, cpu_online_mask); | |
1558 | if (off_cpu >= 0) | |
1559 | total_cpus--; | |
1560 | ||
1561 | for_each_node(node) { | |
1562 | int node_cpus; | |
1563 | ||
1564 | node_cpus = cpumask_weight_and(effective, cpumask_of_node(node)); | |
1565 | if (off_cpu >= 0 && cpu_to_node(off_cpu) == node) | |
1566 | node_cpus--; | |
1567 | ||
1568 | wq_node_nr_active(wq, node)->max = | |
1569 | clamp(DIV_ROUND_UP(max_active * node_cpus, total_cpus), | |
1570 | min_active, max_active); | |
1571 | } | |
1572 | ||
1573 | wq_node_nr_active(wq, NUMA_NO_NODE)->max = min_active; | |
1574 | } | |
1575 | ||
8864b4e5 TH |
1576 | /** |
1577 | * get_pwq - get an extra reference on the specified pool_workqueue | |
1578 | * @pwq: pool_workqueue to get | |
1579 | * | |
1580 | * Obtain an extra reference on @pwq. The caller should guarantee that | |
1581 | * @pwq has positive refcnt and be holding the matching pool->lock. | |
1582 | */ | |
1583 | static void get_pwq(struct pool_workqueue *pwq) | |
1584 | { | |
1585 | lockdep_assert_held(&pwq->pool->lock); | |
1586 | WARN_ON_ONCE(pwq->refcnt <= 0); | |
1587 | pwq->refcnt++; | |
1588 | } | |
1589 | ||
1590 | /** | |
1591 | * put_pwq - put a pool_workqueue reference | |
1592 | * @pwq: pool_workqueue to put | |
1593 | * | |
1594 | * Drop a reference of @pwq. If its refcnt reaches zero, schedule its | |
1595 | * destruction. The caller should be holding the matching pool->lock. | |
1596 | */ | |
1597 | static void put_pwq(struct pool_workqueue *pwq) | |
1598 | { | |
1599 | lockdep_assert_held(&pwq->pool->lock); | |
1600 | if (likely(--pwq->refcnt)) | |
1601 | return; | |
8864b4e5 | 1602 | /* |
967b494e TH |
1603 | * @pwq can't be released under pool->lock, bounce to a dedicated |
1604 | * kthread_worker to avoid A-A deadlocks. | |
8864b4e5 | 1605 | */ |
687a9aa5 | 1606 | kthread_queue_work(pwq_release_worker, &pwq->release_work); |
8864b4e5 TH |
1607 | } |
1608 | ||
dce90d47 TH |
1609 | /** |
1610 | * put_pwq_unlocked - put_pwq() with surrounding pool lock/unlock | |
1611 | * @pwq: pool_workqueue to put (can be %NULL) | |
1612 | * | |
1613 | * put_pwq() with locking. This function also allows %NULL @pwq. | |
1614 | */ | |
1615 | static void put_pwq_unlocked(struct pool_workqueue *pwq) | |
1616 | { | |
1617 | if (pwq) { | |
1618 | /* | |
24acfb71 | 1619 | * As both pwqs and pools are RCU protected, the |
dce90d47 TH |
1620 | * following lock operations are safe. |
1621 | */ | |
a9b8a985 | 1622 | raw_spin_lock_irq(&pwq->pool->lock); |
dce90d47 | 1623 | put_pwq(pwq); |
a9b8a985 | 1624 | raw_spin_unlock_irq(&pwq->pool->lock); |
dce90d47 TH |
1625 | } |
1626 | } | |
1627 | ||
afa87ce8 TH |
1628 | static bool pwq_is_empty(struct pool_workqueue *pwq) |
1629 | { | |
1630 | return !pwq->nr_active && list_empty(&pwq->inactive_works); | |
1631 | } | |
1632 | ||
4c638030 TH |
1633 | static void __pwq_activate_work(struct pool_workqueue *pwq, |
1634 | struct work_struct *work) | |
bf4ede01 | 1635 | { |
1c270b79 TH |
1636 | unsigned long *wdb = work_data_bits(work); |
1637 | ||
1638 | WARN_ON_ONCE(!(*wdb & WORK_STRUCT_INACTIVE)); | |
bf4ede01 | 1639 | trace_workqueue_activate_work(work); |
82607adc TH |
1640 | if (list_empty(&pwq->pool->worklist)) |
1641 | pwq->pool->watchdog_ts = jiffies; | |
112202d9 | 1642 | move_linked_works(work, &pwq->pool->worklist, NULL); |
1c270b79 | 1643 | __clear_bit(WORK_STRUCT_INACTIVE_BIT, wdb); |
4c638030 TH |
1644 | } |
1645 | ||
1646 | /** | |
1647 | * pwq_activate_work - Activate a work item if inactive | |
1648 | * @pwq: pool_workqueue @work belongs to | |
1649 | * @work: work item to activate | |
1650 | * | |
1651 | * Returns %true if activated. %false if already active. | |
1652 | */ | |
1653 | static bool pwq_activate_work(struct pool_workqueue *pwq, | |
1654 | struct work_struct *work) | |
1655 | { | |
1656 | struct worker_pool *pool = pwq->pool; | |
91ccc6e7 | 1657 | struct wq_node_nr_active *nna; |
4c638030 TH |
1658 | |
1659 | lockdep_assert_held(&pool->lock); | |
1660 | ||
1661 | if (!(*work_data_bits(work) & WORK_STRUCT_INACTIVE)) | |
1662 | return false; | |
1663 | ||
91ccc6e7 TH |
1664 | nna = wq_node_nr_active(pwq->wq, pool->node); |
1665 | if (nna) | |
1666 | atomic_inc(&nna->nr); | |
1667 | ||
112202d9 | 1668 | pwq->nr_active++; |
4c638030 TH |
1669 | __pwq_activate_work(pwq, work); |
1670 | return true; | |
bf4ede01 TH |
1671 | } |
1672 | ||
5797b1c1 TH |
1673 | static bool tryinc_node_nr_active(struct wq_node_nr_active *nna) |
1674 | { | |
1675 | int max = READ_ONCE(nna->max); | |
1676 | ||
1677 | while (true) { | |
1678 | int old, tmp; | |
1679 | ||
1680 | old = atomic_read(&nna->nr); | |
1681 | if (old >= max) | |
1682 | return false; | |
1683 | tmp = atomic_cmpxchg_relaxed(&nna->nr, old, old + 1); | |
1684 | if (tmp == old) | |
1685 | return true; | |
1686 | } | |
1687 | } | |
1688 | ||
1c270b79 TH |
1689 | /** |
1690 | * pwq_tryinc_nr_active - Try to increment nr_active for a pwq | |
1691 | * @pwq: pool_workqueue of interest | |
5797b1c1 | 1692 | * @fill: max_active may have increased, try to increase concurrency level |
1c270b79 TH |
1693 | * |
1694 | * Try to increment nr_active for @pwq. Returns %true if an nr_active count is | |
1695 | * successfully obtained. %false otherwise. | |
1696 | */ | |
5797b1c1 | 1697 | static bool pwq_tryinc_nr_active(struct pool_workqueue *pwq, bool fill) |
1c270b79 TH |
1698 | { |
1699 | struct workqueue_struct *wq = pwq->wq; | |
1700 | struct worker_pool *pool = pwq->pool; | |
91ccc6e7 | 1701 | struct wq_node_nr_active *nna = wq_node_nr_active(wq, pool->node); |
5797b1c1 | 1702 | bool obtained = false; |
1c270b79 TH |
1703 | |
1704 | lockdep_assert_held(&pool->lock); | |
1705 | ||
5797b1c1 | 1706 | if (!nna) { |
4cb1ef64 | 1707 | /* BH or per-cpu workqueue, pwq->nr_active is sufficient */ |
5797b1c1 TH |
1708 | obtained = pwq->nr_active < READ_ONCE(wq->max_active); |
1709 | goto out; | |
1710 | } | |
1711 | ||
4c065dbc WL |
1712 | if (unlikely(pwq->plugged)) |
1713 | return false; | |
1714 | ||
5797b1c1 TH |
1715 | /* |
1716 | * Unbound workqueue uses per-node shared nr_active $nna. If @pwq is | |
1717 | * already waiting on $nna, pwq_dec_nr_active() will maintain the | |
1718 | * concurrency level. Don't jump the line. | |
1719 | * | |
1720 | * We need to ignore the pending test after max_active has increased as | |
1721 | * pwq_dec_nr_active() can only maintain the concurrency level but not | |
1722 | * increase it. This is indicated by @fill. | |
1723 | */ | |
1724 | if (!list_empty(&pwq->pending_node) && likely(!fill)) | |
1725 | goto out; | |
1726 | ||
1727 | obtained = tryinc_node_nr_active(nna); | |
1728 | if (obtained) | |
1729 | goto out; | |
1730 | ||
1731 | /* | |
1732 | * Lockless acquisition failed. Lock, add ourself to $nna->pending_pwqs | |
1733 | * and try again. The smp_mb() is paired with the implied memory barrier | |
1734 | * of atomic_dec_return() in pwq_dec_nr_active() to ensure that either | |
1735 | * we see the decremented $nna->nr or they see non-empty | |
1736 | * $nna->pending_pwqs. | |
1737 | */ | |
1738 | raw_spin_lock(&nna->lock); | |
1739 | ||
1740 | if (list_empty(&pwq->pending_node)) | |
1741 | list_add_tail(&pwq->pending_node, &nna->pending_pwqs); | |
1742 | else if (likely(!fill)) | |
1743 | goto out_unlock; | |
1744 | ||
1745 | smp_mb(); | |
1746 | ||
1747 | obtained = tryinc_node_nr_active(nna); | |
1c270b79 | 1748 | |
5797b1c1 TH |
1749 | /* |
1750 | * If @fill, @pwq might have already been pending. Being spuriously | |
1751 | * pending in cold paths doesn't affect anything. Let's leave it be. | |
1752 | */ | |
1753 | if (obtained && likely(!fill)) | |
1754 | list_del_init(&pwq->pending_node); | |
1755 | ||
1756 | out_unlock: | |
1757 | raw_spin_unlock(&nna->lock); | |
1758 | out: | |
1759 | if (obtained) | |
1c270b79 TH |
1760 | pwq->nr_active++; |
1761 | return obtained; | |
1762 | } | |
1763 | ||
1764 | /** | |
1765 | * pwq_activate_first_inactive - Activate the first inactive work item on a pwq | |
1766 | * @pwq: pool_workqueue of interest | |
5797b1c1 | 1767 | * @fill: max_active may have increased, try to increase concurrency level |
1c270b79 TH |
1768 | * |
1769 | * Activate the first inactive work item of @pwq if available and allowed by | |
1770 | * max_active limit. | |
1771 | * | |
1772 | * Returns %true if an inactive work item has been activated. %false if no | |
1773 | * inactive work item is found or max_active limit is reached. | |
1774 | */ | |
5797b1c1 | 1775 | static bool pwq_activate_first_inactive(struct pool_workqueue *pwq, bool fill) |
1c270b79 TH |
1776 | { |
1777 | struct work_struct *work = | |
1778 | list_first_entry_or_null(&pwq->inactive_works, | |
1779 | struct work_struct, entry); | |
1780 | ||
5797b1c1 | 1781 | if (work && pwq_tryinc_nr_active(pwq, fill)) { |
1c270b79 TH |
1782 | __pwq_activate_work(pwq, work); |
1783 | return true; | |
1784 | } else { | |
1785 | return false; | |
1786 | } | |
1787 | } | |
1788 | ||
4c065dbc WL |
1789 | /** |
1790 | * unplug_oldest_pwq - restart an oldest plugged pool_workqueue | |
1791 | * @wq: workqueue_struct to be restarted | |
1792 | * | |
1793 | * pwq's are linked into wq->pwqs with the oldest first. For ordered | |
1794 | * workqueues, only the oldest pwq is unplugged, the others are plugged to | |
1795 | * suspend execution until the oldest one is drained. When this happens, the | |
1796 | * next oldest one (first plugged pwq in iteration) will be unplugged to | |
1797 | * restart work item execution to ensure proper work item ordering. | |
1798 | * | |
1799 | * dfl_pwq --------------+ [P] - plugged | |
1800 | * | | |
1801 | * v | |
1802 | * pwqs -> A -> B [P] -> C [P] (newest) | |
1803 | * | | | | |
1804 | * 1 3 5 | |
1805 | * | | | | |
1806 | * 2 4 6 | |
1807 | */ | |
1808 | static void unplug_oldest_pwq(struct workqueue_struct *wq) | |
1809 | { | |
1810 | struct pool_workqueue *pwq; | |
1811 | ||
1812 | lockdep_assert_held(&wq->mutex); | |
1813 | ||
1814 | /* Caller should make sure that pwqs isn't empty before calling */ | |
1815 | pwq = list_first_entry_or_null(&wq->pwqs, struct pool_workqueue, | |
1816 | pwqs_node); | |
1817 | raw_spin_lock_irq(&pwq->pool->lock); | |
1818 | if (pwq->plugged) { | |
1819 | pwq->plugged = false; | |
1820 | if (pwq_activate_first_inactive(pwq, true)) | |
1821 | kick_pool(pwq->pool); | |
1822 | } | |
1823 | raw_spin_unlock_irq(&pwq->pool->lock); | |
1824 | } | |
1825 | ||
5797b1c1 TH |
1826 | /** |
1827 | * node_activate_pending_pwq - Activate a pending pwq on a wq_node_nr_active | |
1828 | * @nna: wq_node_nr_active to activate a pending pwq for | |
1829 | * @caller_pool: worker_pool the caller is locking | |
1830 | * | |
1831 | * Activate a pwq in @nna->pending_pwqs. Called with @caller_pool locked. | |
1832 | * @caller_pool may be unlocked and relocked to lock other worker_pools. | |
1833 | */ | |
1834 | static void node_activate_pending_pwq(struct wq_node_nr_active *nna, | |
1835 | struct worker_pool *caller_pool) | |
1836 | { | |
1837 | struct worker_pool *locked_pool = caller_pool; | |
1838 | struct pool_workqueue *pwq; | |
1839 | struct work_struct *work; | |
1840 | ||
1841 | lockdep_assert_held(&caller_pool->lock); | |
1842 | ||
1843 | raw_spin_lock(&nna->lock); | |
1844 | retry: | |
1845 | pwq = list_first_entry_or_null(&nna->pending_pwqs, | |
1846 | struct pool_workqueue, pending_node); | |
1847 | if (!pwq) | |
1848 | goto out_unlock; | |
1849 | ||
1850 | /* | |
1851 | * If @pwq is for a different pool than @locked_pool, we need to lock | |
1852 | * @pwq->pool->lock. Let's trylock first. If unsuccessful, do the unlock | |
1853 | * / lock dance. For that, we also need to release @nna->lock as it's | |
1854 | * nested inside pool locks. | |
1855 | */ | |
1856 | if (pwq->pool != locked_pool) { | |
1857 | raw_spin_unlock(&locked_pool->lock); | |
1858 | locked_pool = pwq->pool; | |
1859 | if (!raw_spin_trylock(&locked_pool->lock)) { | |
1860 | raw_spin_unlock(&nna->lock); | |
1861 | raw_spin_lock(&locked_pool->lock); | |
1862 | raw_spin_lock(&nna->lock); | |
1863 | goto retry; | |
1864 | } | |
1865 | } | |
1866 | ||
1867 | /* | |
1868 | * $pwq may not have any inactive work items due to e.g. cancellations. | |
1869 | * Drop it from pending_pwqs and see if there's another one. | |
1870 | */ | |
1871 | work = list_first_entry_or_null(&pwq->inactive_works, | |
1872 | struct work_struct, entry); | |
1873 | if (!work) { | |
1874 | list_del_init(&pwq->pending_node); | |
1875 | goto retry; | |
1876 | } | |
1877 | ||
1878 | /* | |
1879 | * Acquire an nr_active count and activate the inactive work item. If | |
1880 | * $pwq still has inactive work items, rotate it to the end of the | |
1881 | * pending_pwqs so that we round-robin through them. This means that | |
1882 | * inactive work items are not activated in queueing order which is fine | |
1883 | * given that there has never been any ordering across different pwqs. | |
1884 | */ | |
1885 | if (likely(tryinc_node_nr_active(nna))) { | |
1886 | pwq->nr_active++; | |
1887 | __pwq_activate_work(pwq, work); | |
1888 | ||
1889 | if (list_empty(&pwq->inactive_works)) | |
1890 | list_del_init(&pwq->pending_node); | |
1891 | else | |
1892 | list_move_tail(&pwq->pending_node, &nna->pending_pwqs); | |
1893 | ||
1894 | /* if activating a foreign pool, make sure it's running */ | |
1895 | if (pwq->pool != caller_pool) | |
1896 | kick_pool(pwq->pool); | |
1897 | } | |
1898 | ||
1899 | out_unlock: | |
1900 | raw_spin_unlock(&nna->lock); | |
1901 | if (locked_pool != caller_pool) { | |
1902 | raw_spin_unlock(&locked_pool->lock); | |
1903 | raw_spin_lock(&caller_pool->lock); | |
1904 | } | |
1905 | } | |
1906 | ||
1c270b79 TH |
1907 | /** |
1908 | * pwq_dec_nr_active - Retire an active count | |
1909 | * @pwq: pool_workqueue of interest | |
1910 | * | |
1911 | * Decrement @pwq's nr_active and try to activate the first inactive work item. | |
5797b1c1 | 1912 | * For unbound workqueues, this function may temporarily drop @pwq->pool->lock. |
1c270b79 TH |
1913 | */ |
1914 | static void pwq_dec_nr_active(struct pool_workqueue *pwq) | |
3aa62497 | 1915 | { |
1c270b79 | 1916 | struct worker_pool *pool = pwq->pool; |
91ccc6e7 | 1917 | struct wq_node_nr_active *nna = wq_node_nr_active(pwq->wq, pool->node); |
3aa62497 | 1918 | |
1c270b79 TH |
1919 | lockdep_assert_held(&pool->lock); |
1920 | ||
91ccc6e7 TH |
1921 | /* |
1922 | * @pwq->nr_active should be decremented for both percpu and unbound | |
1923 | * workqueues. | |
1924 | */ | |
1c270b79 | 1925 | pwq->nr_active--; |
91ccc6e7 TH |
1926 | |
1927 | /* | |
1928 | * For a percpu workqueue, it's simple. Just need to kick the first | |
1929 | * inactive work item on @pwq itself. | |
1930 | */ | |
1931 | if (!nna) { | |
5797b1c1 | 1932 | pwq_activate_first_inactive(pwq, false); |
91ccc6e7 TH |
1933 | return; |
1934 | } | |
1935 | ||
5797b1c1 TH |
1936 | /* |
1937 | * If @pwq is for an unbound workqueue, it's more complicated because | |
1938 | * multiple pwqs and pools may be sharing the nr_active count. When a | |
1939 | * pwq needs to wait for an nr_active count, it puts itself on | |
1940 | * $nna->pending_pwqs. The following atomic_dec_return()'s implied | |
1941 | * memory barrier is paired with smp_mb() in pwq_tryinc_nr_active() to | |
1942 | * guarantee that either we see non-empty pending_pwqs or they see | |
1943 | * decremented $nna->nr. | |
1944 | * | |
1945 | * $nna->max may change as CPUs come online/offline and @pwq->wq's | |
1946 | * max_active gets updated. However, it is guaranteed to be equal to or | |
1947 | * larger than @pwq->wq->min_active which is above zero unless freezing. | |
1948 | * This maintains the forward progress guarantee. | |
1949 | */ | |
1950 | if (atomic_dec_return(&nna->nr) >= READ_ONCE(nna->max)) | |
1951 | return; | |
1952 | ||
1953 | if (!list_empty(&nna->pending_pwqs)) | |
1954 | node_activate_pending_pwq(nna, pool); | |
3aa62497 LJ |
1955 | } |
1956 | ||
bf4ede01 | 1957 | /** |
112202d9 TH |
1958 | * pwq_dec_nr_in_flight - decrement pwq's nr_in_flight |
1959 | * @pwq: pwq of interest | |
c4560c2c | 1960 | * @work_data: work_data of work which left the queue |
bf4ede01 TH |
1961 | * |
1962 | * A work either has completed or is removed from pending queue, | |
112202d9 | 1963 | * decrement nr_in_flight of its pwq and handle workqueue flushing. |
bf4ede01 | 1964 | * |
dd6c3c54 TH |
1965 | * NOTE: |
1966 | * For unbound workqueues, this function may temporarily drop @pwq->pool->lock | |
1967 | * and thus should be called after all other state updates for the in-flight | |
1968 | * work item is complete. | |
1969 | * | |
bf4ede01 | 1970 | * CONTEXT: |
a9b8a985 | 1971 | * raw_spin_lock_irq(pool->lock). |
bf4ede01 | 1972 | */ |
c4560c2c | 1973 | static void pwq_dec_nr_in_flight(struct pool_workqueue *pwq, unsigned long work_data) |
bf4ede01 | 1974 | { |
c4560c2c LJ |
1975 | int color = get_work_color(work_data); |
1976 | ||
1c270b79 TH |
1977 | if (!(work_data & WORK_STRUCT_INACTIVE)) |
1978 | pwq_dec_nr_active(pwq); | |
018f3a13 | 1979 | |
112202d9 | 1980 | pwq->nr_in_flight[color]--; |
bf4ede01 | 1981 | |
bf4ede01 | 1982 | /* is flush in progress and are we at the flushing tip? */ |
112202d9 | 1983 | if (likely(pwq->flush_color != color)) |
8864b4e5 | 1984 | goto out_put; |
bf4ede01 TH |
1985 | |
1986 | /* are there still in-flight works? */ | |
112202d9 | 1987 | if (pwq->nr_in_flight[color]) |
8864b4e5 | 1988 | goto out_put; |
bf4ede01 | 1989 | |
112202d9 TH |
1990 | /* this pwq is done, clear flush_color */ |
1991 | pwq->flush_color = -1; | |
bf4ede01 TH |
1992 | |
1993 | /* | |
112202d9 | 1994 | * If this was the last pwq, wake up the first flusher. It |
bf4ede01 TH |
1995 | * will handle the rest. |
1996 | */ | |
112202d9 TH |
1997 | if (atomic_dec_and_test(&pwq->wq->nr_pwqs_to_flush)) |
1998 | complete(&pwq->wq->first_flusher->done); | |
8864b4e5 TH |
1999 | out_put: |
2000 | put_pwq(pwq); | |
bf4ede01 TH |
2001 | } |
2002 | ||
36e227d2 | 2003 | /** |
bbb68dfa | 2004 | * try_to_grab_pending - steal work item from worklist and disable irq |
36e227d2 TH |
2005 | * @work: work item to steal |
2006 | * @is_dwork: @work is a delayed_work | |
bbb68dfa | 2007 | * @flags: place to store irq state |
36e227d2 TH |
2008 | * |
2009 | * Try to grab PENDING bit of @work. This function can handle @work in any | |
d185af30 | 2010 | * stable state - idle, on timer or on worklist. |
36e227d2 | 2011 | * |
d185af30 | 2012 | * Return: |
3eb6b31b MCC |
2013 | * |
2014 | * ======== ================================================================ | |
36e227d2 TH |
2015 | * 1 if @work was pending and we successfully stole PENDING |
2016 | * 0 if @work was idle and we claimed PENDING | |
2017 | * -EAGAIN if PENDING couldn't be grabbed at the moment, safe to busy-retry | |
bbb68dfa TH |
2018 | * -ENOENT if someone else is canceling @work, this state may persist |
2019 | * for arbitrarily long | |
3eb6b31b | 2020 | * ======== ================================================================ |
36e227d2 | 2021 | * |
d185af30 | 2022 | * Note: |
bbb68dfa | 2023 | * On >= 0 return, the caller owns @work's PENDING bit. To avoid getting |
e0aecdd8 TH |
2024 | * interrupted while holding PENDING and @work off queue, irq must be |
2025 | * disabled on entry. This, combined with delayed_work->timer being | |
2026 | * irqsafe, ensures that we return -EAGAIN for finite short period of time. | |
bbb68dfa TH |
2027 | * |
2028 | * On successful return, >= 0, irq is disabled and the caller is | |
2029 | * responsible for releasing it using local_irq_restore(*@flags). | |
2030 | * | |
e0aecdd8 | 2031 | * This function is safe to call from any context including IRQ handler. |
bf4ede01 | 2032 | */ |
bbb68dfa TH |
2033 | static int try_to_grab_pending(struct work_struct *work, bool is_dwork, |
2034 | unsigned long *flags) | |
bf4ede01 | 2035 | { |
d565ed63 | 2036 | struct worker_pool *pool; |
112202d9 | 2037 | struct pool_workqueue *pwq; |
bf4ede01 | 2038 | |
bbb68dfa TH |
2039 | local_irq_save(*flags); |
2040 | ||
36e227d2 TH |
2041 | /* try to steal the timer if it exists */ |
2042 | if (is_dwork) { | |
2043 | struct delayed_work *dwork = to_delayed_work(work); | |
2044 | ||
e0aecdd8 TH |
2045 | /* |
2046 | * dwork->timer is irqsafe. If del_timer() fails, it's | |
2047 | * guaranteed that the timer is not queued anywhere and not | |
2048 | * running on the local CPU. | |
2049 | */ | |
36e227d2 TH |
2050 | if (likely(del_timer(&dwork->timer))) |
2051 | return 1; | |
2052 | } | |
2053 | ||
2054 | /* try to claim PENDING the normal way */ | |
bf4ede01 TH |
2055 | if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))) |
2056 | return 0; | |
2057 | ||
24acfb71 | 2058 | rcu_read_lock(); |
bf4ede01 TH |
2059 | /* |
2060 | * The queueing is in progress, or it is already queued. Try to | |
2061 | * steal it from ->worklist without clearing WORK_STRUCT_PENDING. | |
2062 | */ | |
d565ed63 TH |
2063 | pool = get_work_pool(work); |
2064 | if (!pool) | |
bbb68dfa | 2065 | goto fail; |
bf4ede01 | 2066 | |
a9b8a985 | 2067 | raw_spin_lock(&pool->lock); |
0b3dae68 | 2068 | /* |
112202d9 TH |
2069 | * work->data is guaranteed to point to pwq only while the work |
2070 | * item is queued on pwq->wq, and both updating work->data to point | |
2071 | * to pwq on queueing and to pool on dequeueing are done under | |
2072 | * pwq->pool->lock. This in turn guarantees that, if work->data | |
2073 | * points to pwq which is associated with a locked pool, the work | |
0b3dae68 LJ |
2074 | * item is currently queued on that pool. |
2075 | */ | |
112202d9 TH |
2076 | pwq = get_work_pwq(work); |
2077 | if (pwq && pwq->pool == pool) { | |
c70e1779 TH |
2078 | unsigned long work_data; |
2079 | ||
16062836 TH |
2080 | debug_work_deactivate(work); |
2081 | ||
2082 | /* | |
018f3a13 LJ |
2083 | * A cancelable inactive work item must be in the |
2084 | * pwq->inactive_works since a queued barrier can't be | |
2085 | * canceled (see the comments in insert_wq_barrier()). | |
2086 | * | |
f97a4a1a | 2087 | * An inactive work item cannot be grabbed directly because |
d812796e | 2088 | * it might have linked barrier work items which, if left |
f97a4a1a | 2089 | * on the inactive_works list, will confuse pwq->nr_active |
16062836 TH |
2090 | * management later on and cause stall. Make sure the work |
2091 | * item is activated before grabbing. | |
2092 | */ | |
4c638030 | 2093 | pwq_activate_work(pwq, work); |
16062836 TH |
2094 | |
2095 | list_del_init(&work->entry); | |
16062836 | 2096 | |
c70e1779 TH |
2097 | /* |
2098 | * work->data points to pwq iff queued. Let's point to pool. As | |
2099 | * this destroys work->data needed by the next step, stash it. | |
2100 | */ | |
2101 | work_data = *work_data_bits(work); | |
16062836 TH |
2102 | set_work_pool_and_keep_pending(work, pool->id); |
2103 | ||
dd6c3c54 | 2104 | /* must be the last step, see the function comment */ |
c70e1779 | 2105 | pwq_dec_nr_in_flight(pwq, work_data); |
dd6c3c54 | 2106 | |
a9b8a985 | 2107 | raw_spin_unlock(&pool->lock); |
24acfb71 | 2108 | rcu_read_unlock(); |
16062836 | 2109 | return 1; |
bf4ede01 | 2110 | } |
a9b8a985 | 2111 | raw_spin_unlock(&pool->lock); |
bbb68dfa | 2112 | fail: |
24acfb71 | 2113 | rcu_read_unlock(); |
bbb68dfa TH |
2114 | local_irq_restore(*flags); |
2115 | if (work_is_canceling(work)) | |
2116 | return -ENOENT; | |
2117 | cpu_relax(); | |
36e227d2 | 2118 | return -EAGAIN; |
bf4ede01 TH |
2119 | } |
2120 | ||
4690c4ab | 2121 | /** |
706026c2 | 2122 | * insert_work - insert a work into a pool |
112202d9 | 2123 | * @pwq: pwq @work belongs to |
4690c4ab TH |
2124 | * @work: work to insert |
2125 | * @head: insertion point | |
2126 | * @extra_flags: extra WORK_STRUCT_* flags to set | |
2127 | * | |
112202d9 | 2128 | * Insert @work which belongs to @pwq after @head. @extra_flags is or'd to |
706026c2 | 2129 | * work_struct flags. |
4690c4ab TH |
2130 | * |
2131 | * CONTEXT: | |
a9b8a985 | 2132 | * raw_spin_lock_irq(pool->lock). |
4690c4ab | 2133 | */ |
112202d9 TH |
2134 | static void insert_work(struct pool_workqueue *pwq, struct work_struct *work, |
2135 | struct list_head *head, unsigned int extra_flags) | |
b89deed3 | 2136 | { |
fe089f87 | 2137 | debug_work_activate(work); |
e22bee78 | 2138 | |
e89a85d6 | 2139 | /* record the work call stack in order to print it in KASAN reports */ |
f70da745 | 2140 | kasan_record_aux_stack_noalloc(work); |
e89a85d6 | 2141 | |
4690c4ab | 2142 | /* we own @work, set data and link */ |
112202d9 | 2143 | set_work_pwq(work, pwq, extra_flags); |
1a4d9b0a | 2144 | list_add_tail(&work->entry, head); |
8864b4e5 | 2145 | get_pwq(pwq); |
b89deed3 ON |
2146 | } |
2147 | ||
c8efcc25 TH |
2148 | /* |
2149 | * Test whether @work is being queued from another work executing on the | |
8d03ecfe | 2150 | * same workqueue. |
c8efcc25 TH |
2151 | */ |
2152 | static bool is_chained_work(struct workqueue_struct *wq) | |
2153 | { | |
8d03ecfe TH |
2154 | struct worker *worker; |
2155 | ||
2156 | worker = current_wq_worker(); | |
2157 | /* | |
bf393fd4 | 2158 | * Return %true iff I'm a worker executing a work item on @wq. If |
8d03ecfe TH |
2159 | * I'm @worker, it's safe to dereference it without locking. |
2160 | */ | |
112202d9 | 2161 | return worker && worker->current_pwq->wq == wq; |
c8efcc25 TH |
2162 | } |
2163 | ||
ef557180 MG |
2164 | /* |
2165 | * When queueing an unbound work item to a wq, prefer local CPU if allowed | |
2166 | * by wq_unbound_cpumask. Otherwise, round robin among the allowed ones to | |
2167 | * avoid perturbing sensitive tasks. | |
2168 | */ | |
2169 | static int wq_select_unbound_cpu(int cpu) | |
2170 | { | |
2171 | int new_cpu; | |
2172 | ||
f303fccb TH |
2173 | if (likely(!wq_debug_force_rr_cpu)) { |
2174 | if (cpumask_test_cpu(cpu, wq_unbound_cpumask)) | |
2175 | return cpu; | |
a8ec5880 AF |
2176 | } else { |
2177 | pr_warn_once("workqueue: round-robin CPU selection forced, expect performance impact\n"); | |
f303fccb TH |
2178 | } |
2179 | ||
ef557180 MG |
2180 | new_cpu = __this_cpu_read(wq_rr_cpu_last); |
2181 | new_cpu = cpumask_next_and(new_cpu, wq_unbound_cpumask, cpu_online_mask); | |
2182 | if (unlikely(new_cpu >= nr_cpu_ids)) { | |
2183 | new_cpu = cpumask_first_and(wq_unbound_cpumask, cpu_online_mask); | |
2184 | if (unlikely(new_cpu >= nr_cpu_ids)) | |
2185 | return cpu; | |
2186 | } | |
2187 | __this_cpu_write(wq_rr_cpu_last, new_cpu); | |
2188 | ||
2189 | return new_cpu; | |
2190 | } | |
2191 | ||
d84ff051 | 2192 | static void __queue_work(int cpu, struct workqueue_struct *wq, |
1da177e4 LT |
2193 | struct work_struct *work) |
2194 | { | |
112202d9 | 2195 | struct pool_workqueue *pwq; |
fe089f87 | 2196 | struct worker_pool *last_pool, *pool; |
8a2e8e5d | 2197 | unsigned int work_flags; |
b75cac93 | 2198 | unsigned int req_cpu = cpu; |
8930caba TH |
2199 | |
2200 | /* | |
2201 | * While a work item is PENDING && off queue, a task trying to | |
2202 | * steal the PENDING will busy-loop waiting for it to either get | |
2203 | * queued or lose PENDING. Grabbing PENDING and queueing should | |
2204 | * happen with IRQ disabled. | |
2205 | */ | |
8e8eb730 | 2206 | lockdep_assert_irqs_disabled(); |
1da177e4 | 2207 | |
1e19ffc6 | 2208 | |
33e3f0a3 RC |
2209 | /* |
2210 | * For a draining wq, only works from the same workqueue are | |
2211 | * allowed. The __WQ_DESTROYING helps to spot the issue that | |
2212 | * queues a new work item to a wq after destroy_workqueue(wq). | |
2213 | */ | |
2214 | if (unlikely(wq->flags & (__WQ_DESTROYING | __WQ_DRAINING) && | |
2215 | WARN_ON_ONCE(!is_chained_work(wq)))) | |
e41e704b | 2216 | return; |
24acfb71 | 2217 | rcu_read_lock(); |
9e8cd2f5 | 2218 | retry: |
c9178087 | 2219 | /* pwq which will be used unless @work is executing elsewhere */ |
636b927e TH |
2220 | if (req_cpu == WORK_CPU_UNBOUND) { |
2221 | if (wq->flags & WQ_UNBOUND) | |
aa202f1f | 2222 | cpu = wq_select_unbound_cpu(raw_smp_processor_id()); |
636b927e | 2223 | else |
aa202f1f | 2224 | cpu = raw_smp_processor_id(); |
aa202f1f | 2225 | } |
dbf2576e | 2226 | |
636b927e | 2227 | pwq = rcu_dereference(*per_cpu_ptr(wq->cpu_pwq, cpu)); |
fe089f87 TH |
2228 | pool = pwq->pool; |
2229 | ||
c9178087 TH |
2230 | /* |
2231 | * If @work was previously on a different pool, it might still be | |
2232 | * running there, in which case the work needs to be queued on that | |
2233 | * pool to guarantee non-reentrancy. | |
2234 | */ | |
2235 | last_pool = get_work_pool(work); | |
fe089f87 | 2236 | if (last_pool && last_pool != pool) { |
c9178087 | 2237 | struct worker *worker; |
18aa9eff | 2238 | |
a9b8a985 | 2239 | raw_spin_lock(&last_pool->lock); |
18aa9eff | 2240 | |
c9178087 | 2241 | worker = find_worker_executing_work(last_pool, work); |
18aa9eff | 2242 | |
c9178087 TH |
2243 | if (worker && worker->current_pwq->wq == wq) { |
2244 | pwq = worker->current_pwq; | |
fe089f87 TH |
2245 | pool = pwq->pool; |
2246 | WARN_ON_ONCE(pool != last_pool); | |
8930caba | 2247 | } else { |
c9178087 | 2248 | /* meh... not running there, queue here */ |
a9b8a985 | 2249 | raw_spin_unlock(&last_pool->lock); |
fe089f87 | 2250 | raw_spin_lock(&pool->lock); |
8930caba | 2251 | } |
f3421797 | 2252 | } else { |
fe089f87 | 2253 | raw_spin_lock(&pool->lock); |
502ca9d8 TH |
2254 | } |
2255 | ||
9e8cd2f5 | 2256 | /* |
636b927e TH |
2257 | * pwq is determined and locked. For unbound pools, we could have raced |
2258 | * with pwq release and it could already be dead. If its refcnt is zero, | |
2259 | * repeat pwq selection. Note that unbound pwqs never die without | |
2260 | * another pwq replacing it in cpu_pwq or while work items are executing | |
2261 | * on it, so the retrying is guaranteed to make forward-progress. | |
9e8cd2f5 TH |
2262 | */ |
2263 | if (unlikely(!pwq->refcnt)) { | |
2264 | if (wq->flags & WQ_UNBOUND) { | |
fe089f87 | 2265 | raw_spin_unlock(&pool->lock); |
9e8cd2f5 TH |
2266 | cpu_relax(); |
2267 | goto retry; | |
2268 | } | |
2269 | /* oops */ | |
2270 | WARN_ONCE(true, "workqueue: per-cpu pwq for %s on cpu%d has 0 refcnt", | |
2271 | wq->name, cpu); | |
2272 | } | |
2273 | ||
112202d9 TH |
2274 | /* pwq determined, queue */ |
2275 | trace_workqueue_queue_work(req_cpu, pwq, work); | |
502ca9d8 | 2276 | |
24acfb71 TG |
2277 | if (WARN_ON(!list_empty(&work->entry))) |
2278 | goto out; | |
1e19ffc6 | 2279 | |
112202d9 TH |
2280 | pwq->nr_in_flight[pwq->work_color]++; |
2281 | work_flags = work_color_to_flags(pwq->work_color); | |
1e19ffc6 | 2282 | |
a045a272 TH |
2283 | /* |
2284 | * Limit the number of concurrently active work items to max_active. | |
2285 | * @work must also queue behind existing inactive work items to maintain | |
2286 | * ordering when max_active changes. See wq_adjust_max_active(). | |
2287 | */ | |
5797b1c1 | 2288 | if (list_empty(&pwq->inactive_works) && pwq_tryinc_nr_active(pwq, false)) { |
fe089f87 TH |
2289 | if (list_empty(&pool->worklist)) |
2290 | pool->watchdog_ts = jiffies; | |
2291 | ||
cdadf009 | 2292 | trace_workqueue_activate_work(work); |
fe089f87 | 2293 | insert_work(pwq, work, &pool->worklist, work_flags); |
0219a352 | 2294 | kick_pool(pool); |
8a2e8e5d | 2295 | } else { |
f97a4a1a | 2296 | work_flags |= WORK_STRUCT_INACTIVE; |
fe089f87 | 2297 | insert_work(pwq, work, &pwq->inactive_works, work_flags); |
8a2e8e5d | 2298 | } |
1e19ffc6 | 2299 | |
24acfb71 | 2300 | out: |
fe089f87 | 2301 | raw_spin_unlock(&pool->lock); |
24acfb71 | 2302 | rcu_read_unlock(); |
1da177e4 LT |
2303 | } |
2304 | ||
0fcb78c2 | 2305 | /** |
c1a220e7 ZR |
2306 | * queue_work_on - queue work on specific cpu |
2307 | * @cpu: CPU number to execute work on | |
0fcb78c2 REB |
2308 | * @wq: workqueue to use |
2309 | * @work: work to queue | |
2310 | * | |
c1a220e7 | 2311 | * We queue the work to a specific CPU, the caller must ensure it |
443378f0 PM |
2312 | * can't go away. Callers that fail to ensure that the specified |
2313 | * CPU cannot go away will execute on a randomly chosen CPU. | |
854f5cc5 PM |
2314 | * But note well that callers specifying a CPU that never has been |
2315 | * online will get a splat. | |
d185af30 YB |
2316 | * |
2317 | * Return: %false if @work was already on a queue, %true otherwise. | |
1da177e4 | 2318 | */ |
d4283e93 TH |
2319 | bool queue_work_on(int cpu, struct workqueue_struct *wq, |
2320 | struct work_struct *work) | |
1da177e4 | 2321 | { |
d4283e93 | 2322 | bool ret = false; |
8930caba | 2323 | unsigned long flags; |
ef1ca236 | 2324 | |
8930caba | 2325 | local_irq_save(flags); |
c1a220e7 | 2326 | |
22df02bb | 2327 | if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))) { |
4690c4ab | 2328 | __queue_work(cpu, wq, work); |
d4283e93 | 2329 | ret = true; |
c1a220e7 | 2330 | } |
ef1ca236 | 2331 | |
8930caba | 2332 | local_irq_restore(flags); |
1da177e4 LT |
2333 | return ret; |
2334 | } | |
ad7b1f84 | 2335 | EXPORT_SYMBOL(queue_work_on); |
1da177e4 | 2336 | |
8204e0c1 | 2337 | /** |
fef59c9c | 2338 | * select_numa_node_cpu - Select a CPU based on NUMA node |
8204e0c1 AD |
2339 | * @node: NUMA node ID that we want to select a CPU from |
2340 | * | |
2341 | * This function will attempt to find a "random" cpu available on a given | |
2342 | * node. If there are no CPUs available on the given node it will return | |
2343 | * WORK_CPU_UNBOUND indicating that we should just schedule to any | |
2344 | * available CPU if we need to schedule this work. | |
2345 | */ | |
fef59c9c | 2346 | static int select_numa_node_cpu(int node) |
8204e0c1 AD |
2347 | { |
2348 | int cpu; | |
2349 | ||
8204e0c1 AD |
2350 | /* Delay binding to CPU if node is not valid or online */ |
2351 | if (node < 0 || node >= MAX_NUMNODES || !node_online(node)) | |
2352 | return WORK_CPU_UNBOUND; | |
2353 | ||
2354 | /* Use local node/cpu if we are already there */ | |
2355 | cpu = raw_smp_processor_id(); | |
2356 | if (node == cpu_to_node(cpu)) | |
2357 | return cpu; | |
2358 | ||
2359 | /* Use "random" otherwise know as "first" online CPU of node */ | |
2360 | cpu = cpumask_any_and(cpumask_of_node(node), cpu_online_mask); | |
2361 | ||
2362 | /* If CPU is valid return that, otherwise just defer */ | |
2363 | return cpu < nr_cpu_ids ? cpu : WORK_CPU_UNBOUND; | |
2364 | } | |
2365 | ||
2366 | /** | |
2367 | * queue_work_node - queue work on a "random" cpu for a given NUMA node | |
2368 | * @node: NUMA node that we are targeting the work for | |
2369 | * @wq: workqueue to use | |
2370 | * @work: work to queue | |
2371 | * | |
2372 | * We queue the work to a "random" CPU within a given NUMA node. The basic | |
2373 | * idea here is to provide a way to somehow associate work with a given | |
2374 | * NUMA node. | |
2375 | * | |
2376 | * This function will only make a best effort attempt at getting this onto | |
2377 | * the right NUMA node. If no node is requested or the requested node is | |
2378 | * offline then we just fall back to standard queue_work behavior. | |
2379 | * | |
2380 | * Currently the "random" CPU ends up being the first available CPU in the | |
2381 | * intersection of cpu_online_mask and the cpumask of the node, unless we | |
2382 | * are running on the node. In that case we just use the current CPU. | |
2383 | * | |
2384 | * Return: %false if @work was already on a queue, %true otherwise. | |
2385 | */ | |
2386 | bool queue_work_node(int node, struct workqueue_struct *wq, | |
2387 | struct work_struct *work) | |
2388 | { | |
2389 | unsigned long flags; | |
2390 | bool ret = false; | |
2391 | ||
2392 | /* | |
2393 | * This current implementation is specific to unbound workqueues. | |
2394 | * Specifically we only return the first available CPU for a given | |
2395 | * node instead of cycling through individual CPUs within the node. | |
2396 | * | |
2397 | * If this is used with a per-cpu workqueue then the logic in | |
2398 | * workqueue_select_cpu_near would need to be updated to allow for | |
2399 | * some round robin type logic. | |
2400 | */ | |
2401 | WARN_ON_ONCE(!(wq->flags & WQ_UNBOUND)); | |
2402 | ||
2403 | local_irq_save(flags); | |
2404 | ||
2405 | if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))) { | |
fef59c9c | 2406 | int cpu = select_numa_node_cpu(node); |
8204e0c1 AD |
2407 | |
2408 | __queue_work(cpu, wq, work); | |
2409 | ret = true; | |
2410 | } | |
2411 | ||
2412 | local_irq_restore(flags); | |
2413 | return ret; | |
2414 | } | |
2415 | EXPORT_SYMBOL_GPL(queue_work_node); | |
2416 | ||
8c20feb6 | 2417 | void delayed_work_timer_fn(struct timer_list *t) |
1da177e4 | 2418 | { |
8c20feb6 | 2419 | struct delayed_work *dwork = from_timer(dwork, t, timer); |
1da177e4 | 2420 | |
e0aecdd8 | 2421 | /* should have been called from irqsafe timer with irq already off */ |
60c057bc | 2422 | __queue_work(dwork->cpu, dwork->wq, &dwork->work); |
1da177e4 | 2423 | } |
1438ade5 | 2424 | EXPORT_SYMBOL(delayed_work_timer_fn); |
1da177e4 | 2425 | |
7beb2edf TH |
2426 | static void __queue_delayed_work(int cpu, struct workqueue_struct *wq, |
2427 | struct delayed_work *dwork, unsigned long delay) | |
1da177e4 | 2428 | { |
7beb2edf TH |
2429 | struct timer_list *timer = &dwork->timer; |
2430 | struct work_struct *work = &dwork->work; | |
7beb2edf | 2431 | |
637fdbae | 2432 | WARN_ON_ONCE(!wq); |
4b243563 | 2433 | WARN_ON_ONCE(timer->function != delayed_work_timer_fn); |
fc4b514f TH |
2434 | WARN_ON_ONCE(timer_pending(timer)); |
2435 | WARN_ON_ONCE(!list_empty(&work->entry)); | |
7beb2edf | 2436 | |
8852aac2 TH |
2437 | /* |
2438 | * If @delay is 0, queue @dwork->work immediately. This is for | |
2439 | * both optimization and correctness. The earliest @timer can | |
2440 | * expire is on the closest next tick and delayed_work users depend | |
2441 | * on that there's no such delay when @delay is 0. | |
2442 | */ | |
2443 | if (!delay) { | |
2444 | __queue_work(cpu, wq, &dwork->work); | |
2445 | return; | |
2446 | } | |
2447 | ||
60c057bc | 2448 | dwork->wq = wq; |
1265057f | 2449 | dwork->cpu = cpu; |
7beb2edf TH |
2450 | timer->expires = jiffies + delay; |
2451 | ||
aae17ebb LB |
2452 | if (housekeeping_enabled(HK_TYPE_TIMER)) { |
2453 | /* If the current cpu is a housekeeping cpu, use it. */ | |
2454 | cpu = smp_processor_id(); | |
2455 | if (!housekeeping_test_cpu(cpu, HK_TYPE_TIMER)) | |
2456 | cpu = housekeeping_any_cpu(HK_TYPE_TIMER); | |
041bd12e | 2457 | add_timer_on(timer, cpu); |
aae17ebb LB |
2458 | } else { |
2459 | if (likely(cpu == WORK_CPU_UNBOUND)) | |
2460 | add_timer(timer); | |
2461 | else | |
2462 | add_timer_on(timer, cpu); | |
2463 | } | |
1da177e4 LT |
2464 | } |
2465 | ||
0fcb78c2 REB |
2466 | /** |
2467 | * queue_delayed_work_on - queue work on specific CPU after delay | |
2468 | * @cpu: CPU number to execute work on | |
2469 | * @wq: workqueue to use | |
af9997e4 | 2470 | * @dwork: work to queue |
0fcb78c2 REB |
2471 | * @delay: number of jiffies to wait before queueing |
2472 | * | |
d185af30 | 2473 | * Return: %false if @work was already on a queue, %true otherwise. If |
715f1300 TH |
2474 | * @delay is zero and @dwork is idle, it will be scheduled for immediate |
2475 | * execution. | |
0fcb78c2 | 2476 | */ |
d4283e93 TH |
2477 | bool queue_delayed_work_on(int cpu, struct workqueue_struct *wq, |
2478 | struct delayed_work *dwork, unsigned long delay) | |
7a6bc1cd | 2479 | { |
52bad64d | 2480 | struct work_struct *work = &dwork->work; |
d4283e93 | 2481 | bool ret = false; |
8930caba | 2482 | unsigned long flags; |
7a6bc1cd | 2483 | |
8930caba TH |
2484 | /* read the comment in __queue_work() */ |
2485 | local_irq_save(flags); | |
7a6bc1cd | 2486 | |
22df02bb | 2487 | if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))) { |
7beb2edf | 2488 | __queue_delayed_work(cpu, wq, dwork, delay); |
d4283e93 | 2489 | ret = true; |
7a6bc1cd | 2490 | } |
8a3e77cc | 2491 | |
8930caba | 2492 | local_irq_restore(flags); |
7a6bc1cd VP |
2493 | return ret; |
2494 | } | |
ad7b1f84 | 2495 | EXPORT_SYMBOL(queue_delayed_work_on); |
c7fc77f7 | 2496 | |
8376fe22 TH |
2497 | /** |
2498 | * mod_delayed_work_on - modify delay of or queue a delayed work on specific CPU | |
2499 | * @cpu: CPU number to execute work on | |
2500 | * @wq: workqueue to use | |
2501 | * @dwork: work to queue | |
2502 | * @delay: number of jiffies to wait before queueing | |
2503 | * | |
2504 | * If @dwork is idle, equivalent to queue_delayed_work_on(); otherwise, | |
2505 | * modify @dwork's timer so that it expires after @delay. If @delay is | |
2506 | * zero, @work is guaranteed to be scheduled immediately regardless of its | |
2507 | * current state. | |
2508 | * | |
d185af30 | 2509 | * Return: %false if @dwork was idle and queued, %true if @dwork was |
8376fe22 TH |
2510 | * pending and its timer was modified. |
2511 | * | |
e0aecdd8 | 2512 | * This function is safe to call from any context including IRQ handler. |
8376fe22 TH |
2513 | * See try_to_grab_pending() for details. |
2514 | */ | |
2515 | bool mod_delayed_work_on(int cpu, struct workqueue_struct *wq, | |
2516 | struct delayed_work *dwork, unsigned long delay) | |
2517 | { | |
2518 | unsigned long flags; | |
2519 | int ret; | |
c7fc77f7 | 2520 | |
8376fe22 TH |
2521 | do { |
2522 | ret = try_to_grab_pending(&dwork->work, true, &flags); | |
2523 | } while (unlikely(ret == -EAGAIN)); | |
63bc0362 | 2524 | |
8376fe22 TH |
2525 | if (likely(ret >= 0)) { |
2526 | __queue_delayed_work(cpu, wq, dwork, delay); | |
2527 | local_irq_restore(flags); | |
7a6bc1cd | 2528 | } |
8376fe22 TH |
2529 | |
2530 | /* -ENOENT from try_to_grab_pending() becomes %true */ | |
7a6bc1cd VP |
2531 | return ret; |
2532 | } | |
8376fe22 TH |
2533 | EXPORT_SYMBOL_GPL(mod_delayed_work_on); |
2534 | ||
05f0fe6b TH |
2535 | static void rcu_work_rcufn(struct rcu_head *rcu) |
2536 | { | |
2537 | struct rcu_work *rwork = container_of(rcu, struct rcu_work, rcu); | |
2538 | ||
2539 | /* read the comment in __queue_work() */ | |
2540 | local_irq_disable(); | |
2541 | __queue_work(WORK_CPU_UNBOUND, rwork->wq, &rwork->work); | |
2542 | local_irq_enable(); | |
2543 | } | |
2544 | ||
2545 | /** | |
2546 | * queue_rcu_work - queue work after a RCU grace period | |
2547 | * @wq: workqueue to use | |
2548 | * @rwork: work to queue | |
2549 | * | |
2550 | * Return: %false if @rwork was already pending, %true otherwise. Note | |
2551 | * that a full RCU grace period is guaranteed only after a %true return. | |
bf393fd4 | 2552 | * While @rwork is guaranteed to be executed after a %false return, the |
05f0fe6b TH |
2553 | * execution may happen before a full RCU grace period has passed. |
2554 | */ | |
2555 | bool queue_rcu_work(struct workqueue_struct *wq, struct rcu_work *rwork) | |
2556 | { | |
2557 | struct work_struct *work = &rwork->work; | |
2558 | ||
2559 | if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))) { | |
2560 | rwork->wq = wq; | |
a7e30c0e | 2561 | call_rcu_hurry(&rwork->rcu, rcu_work_rcufn); |
05f0fe6b TH |
2562 | return true; |
2563 | } | |
2564 | ||
2565 | return false; | |
2566 | } | |
2567 | EXPORT_SYMBOL(queue_rcu_work); | |
2568 | ||
f7537df5 | 2569 | static struct worker *alloc_worker(int node) |
c34056a3 TH |
2570 | { |
2571 | struct worker *worker; | |
2572 | ||
f7537df5 | 2573 | worker = kzalloc_node(sizeof(*worker), GFP_KERNEL, node); |
c8e55f36 TH |
2574 | if (worker) { |
2575 | INIT_LIST_HEAD(&worker->entry); | |
affee4b2 | 2576 | INIT_LIST_HEAD(&worker->scheduled); |
da028469 | 2577 | INIT_LIST_HEAD(&worker->node); |
e22bee78 TH |
2578 | /* on creation a worker is in !idle && prep state */ |
2579 | worker->flags = WORKER_PREP; | |
c8e55f36 | 2580 | } |
c34056a3 TH |
2581 | return worker; |
2582 | } | |
2583 | ||
9546b29e TH |
2584 | static cpumask_t *pool_allowed_cpus(struct worker_pool *pool) |
2585 | { | |
8639eceb TH |
2586 | if (pool->cpu < 0 && pool->attrs->affn_strict) |
2587 | return pool->attrs->__pod_cpumask; | |
2588 | else | |
2589 | return pool->attrs->cpumask; | |
9546b29e TH |
2590 | } |
2591 | ||
4736cbf7 LJ |
2592 | /** |
2593 | * worker_attach_to_pool() - attach a worker to a pool | |
2594 | * @worker: worker to be attached | |
2595 | * @pool: the target pool | |
2596 | * | |
2597 | * Attach @worker to @pool. Once attached, the %WORKER_UNBOUND flag and | |
2598 | * cpu-binding of @worker are kept coordinated with the pool across | |
2599 | * cpu-[un]hotplugs. | |
2600 | */ | |
2601 | static void worker_attach_to_pool(struct worker *worker, | |
4cb1ef64 | 2602 | struct worker_pool *pool) |
4736cbf7 | 2603 | { |
1258fae7 | 2604 | mutex_lock(&wq_pool_attach_mutex); |
4736cbf7 | 2605 | |
4736cbf7 | 2606 | /* |
4cb1ef64 TH |
2607 | * The wq_pool_attach_mutex ensures %POOL_DISASSOCIATED remains stable |
2608 | * across this function. See the comments above the flag definition for | |
2609 | * details. BH workers are, while per-CPU, always DISASSOCIATED. | |
4736cbf7 | 2610 | */ |
4cb1ef64 | 2611 | if (pool->flags & POOL_DISASSOCIATED) { |
4736cbf7 | 2612 | worker->flags |= WORKER_UNBOUND; |
4cb1ef64 TH |
2613 | } else { |
2614 | WARN_ON_ONCE(pool->flags & POOL_BH); | |
5c25b5ff | 2615 | kthread_set_per_cpu(worker->task, pool->cpu); |
4cb1ef64 | 2616 | } |
4736cbf7 | 2617 | |
640f17c8 | 2618 | if (worker->rescue_wq) |
9546b29e | 2619 | set_cpus_allowed_ptr(worker->task, pool_allowed_cpus(pool)); |
640f17c8 | 2620 | |
4736cbf7 | 2621 | list_add_tail(&worker->node, &pool->workers); |
a2d812a2 | 2622 | worker->pool = pool; |
4736cbf7 | 2623 | |
1258fae7 | 2624 | mutex_unlock(&wq_pool_attach_mutex); |
4736cbf7 LJ |
2625 | } |
2626 | ||
60f5a4bc LJ |
2627 | /** |
2628 | * worker_detach_from_pool() - detach a worker from its pool | |
2629 | * @worker: worker which is attached to its pool | |
60f5a4bc | 2630 | * |
4736cbf7 LJ |
2631 | * Undo the attaching which had been done in worker_attach_to_pool(). The |
2632 | * caller worker shouldn't access to the pool after detached except it has | |
2633 | * other reference to the pool. | |
60f5a4bc | 2634 | */ |
a2d812a2 | 2635 | static void worker_detach_from_pool(struct worker *worker) |
60f5a4bc | 2636 | { |
a2d812a2 | 2637 | struct worker_pool *pool = worker->pool; |
60f5a4bc LJ |
2638 | struct completion *detach_completion = NULL; |
2639 | ||
4cb1ef64 TH |
2640 | /* there is one permanent BH worker per CPU which should never detach */ |
2641 | WARN_ON_ONCE(pool->flags & POOL_BH); | |
2642 | ||
1258fae7 | 2643 | mutex_lock(&wq_pool_attach_mutex); |
a2d812a2 | 2644 | |
5c25b5ff | 2645 | kthread_set_per_cpu(worker->task, -1); |
da028469 | 2646 | list_del(&worker->node); |
a2d812a2 TH |
2647 | worker->pool = NULL; |
2648 | ||
e02b9312 | 2649 | if (list_empty(&pool->workers) && list_empty(&pool->dying_workers)) |
60f5a4bc | 2650 | detach_completion = pool->detach_completion; |
1258fae7 | 2651 | mutex_unlock(&wq_pool_attach_mutex); |
60f5a4bc | 2652 | |
b62c0751 LJ |
2653 | /* clear leftover flags without pool->lock after it is detached */ |
2654 | worker->flags &= ~(WORKER_UNBOUND | WORKER_REBOUND); | |
2655 | ||
60f5a4bc LJ |
2656 | if (detach_completion) |
2657 | complete(detach_completion); | |
2658 | } | |
2659 | ||
c34056a3 TH |
2660 | /** |
2661 | * create_worker - create a new workqueue worker | |
63d95a91 | 2662 | * @pool: pool the new worker will belong to |
c34056a3 | 2663 | * |
051e1850 | 2664 | * Create and start a new worker which is attached to @pool. |
c34056a3 TH |
2665 | * |
2666 | * CONTEXT: | |
2667 | * Might sleep. Does GFP_KERNEL allocations. | |
2668 | * | |
d185af30 | 2669 | * Return: |
c34056a3 TH |
2670 | * Pointer to the newly created worker. |
2671 | */ | |
bc2ae0f5 | 2672 | static struct worker *create_worker(struct worker_pool *pool) |
c34056a3 | 2673 | { |
e441b56f ZL |
2674 | struct worker *worker; |
2675 | int id; | |
5d9c7a1e | 2676 | char id_buf[23]; |
c34056a3 | 2677 | |
7cda9aae | 2678 | /* ID is needed to determine kthread name */ |
e441b56f | 2679 | id = ida_alloc(&pool->worker_ida, GFP_KERNEL); |
3f0ea0b8 PM |
2680 | if (id < 0) { |
2681 | pr_err_once("workqueue: Failed to allocate a worker ID: %pe\n", | |
2682 | ERR_PTR(id)); | |
e441b56f | 2683 | return NULL; |
3f0ea0b8 | 2684 | } |
c34056a3 | 2685 | |
f7537df5 | 2686 | worker = alloc_worker(pool->node); |
3f0ea0b8 PM |
2687 | if (!worker) { |
2688 | pr_err_once("workqueue: Failed to allocate a worker\n"); | |
c34056a3 | 2689 | goto fail; |
3f0ea0b8 | 2690 | } |
c34056a3 | 2691 | |
c34056a3 TH |
2692 | worker->id = id; |
2693 | ||
4cb1ef64 TH |
2694 | if (!(pool->flags & POOL_BH)) { |
2695 | if (pool->cpu >= 0) | |
2696 | snprintf(id_buf, sizeof(id_buf), "%d:%d%s", pool->cpu, id, | |
2697 | pool->attrs->nice < 0 ? "H" : ""); | |
2698 | else | |
2699 | snprintf(id_buf, sizeof(id_buf), "u%d:%d", pool->id, id); | |
2700 | ||
2701 | worker->task = kthread_create_on_node(worker_thread, worker, | |
2702 | pool->node, "kworker/%s", id_buf); | |
2703 | if (IS_ERR(worker->task)) { | |
2704 | if (PTR_ERR(worker->task) == -EINTR) { | |
2705 | pr_err("workqueue: Interrupted when creating a worker thread \"kworker/%s\"\n", | |
2706 | id_buf); | |
2707 | } else { | |
2708 | pr_err_once("workqueue: Failed to create a worker thread: %pe", | |
2709 | worker->task); | |
2710 | } | |
2711 | goto fail; | |
60f54038 | 2712 | } |
c34056a3 | 2713 | |
4cb1ef64 TH |
2714 | set_user_nice(worker->task, pool->attrs->nice); |
2715 | kthread_bind_mask(worker->task, pool_allowed_cpus(pool)); | |
2716 | } | |
91151228 | 2717 | |
da028469 | 2718 | /* successful, attach the worker to the pool */ |
4736cbf7 | 2719 | worker_attach_to_pool(worker, pool); |
822d8405 | 2720 | |
051e1850 | 2721 | /* start the newly created worker */ |
a9b8a985 | 2722 | raw_spin_lock_irq(&pool->lock); |
0219a352 | 2723 | |
051e1850 LJ |
2724 | worker->pool->nr_workers++; |
2725 | worker_enter_idle(worker); | |
0219a352 TH |
2726 | |
2727 | /* | |
2728 | * @worker is waiting on a completion in kthread() and will trigger hung | |
6a229b0e TH |
2729 | * check if not woken up soon. As kick_pool() is noop if @pool is empty, |
2730 | * wake it up explicitly. | |
0219a352 | 2731 | */ |
4cb1ef64 TH |
2732 | if (worker->task) |
2733 | wake_up_process(worker->task); | |
0219a352 | 2734 | |
a9b8a985 | 2735 | raw_spin_unlock_irq(&pool->lock); |
051e1850 | 2736 | |
c34056a3 | 2737 | return worker; |
822d8405 | 2738 | |
c34056a3 | 2739 | fail: |
e441b56f | 2740 | ida_free(&pool->worker_ida, id); |
c34056a3 TH |
2741 | kfree(worker); |
2742 | return NULL; | |
2743 | } | |
2744 | ||
793777bc VS |
2745 | static void unbind_worker(struct worker *worker) |
2746 | { | |
2747 | lockdep_assert_held(&wq_pool_attach_mutex); | |
2748 | ||
2749 | kthread_set_per_cpu(worker->task, -1); | |
2750 | if (cpumask_intersects(wq_unbound_cpumask, cpu_active_mask)) | |
2751 | WARN_ON_ONCE(set_cpus_allowed_ptr(worker->task, wq_unbound_cpumask) < 0); | |
2752 | else | |
2753 | WARN_ON_ONCE(set_cpus_allowed_ptr(worker->task, cpu_possible_mask) < 0); | |
2754 | } | |
2755 | ||
e02b9312 VS |
2756 | static void wake_dying_workers(struct list_head *cull_list) |
2757 | { | |
2758 | struct worker *worker, *tmp; | |
2759 | ||
2760 | list_for_each_entry_safe(worker, tmp, cull_list, entry) { | |
2761 | list_del_init(&worker->entry); | |
2762 | unbind_worker(worker); | |
2763 | /* | |
2764 | * If the worker was somehow already running, then it had to be | |
2765 | * in pool->idle_list when set_worker_dying() happened or we | |
2766 | * wouldn't have gotten here. | |
2767 | * | |
2768 | * Thus, the worker must either have observed the WORKER_DIE | |
2769 | * flag, or have set its state to TASK_IDLE. Either way, the | |
2770 | * below will be observed by the worker and is safe to do | |
2771 | * outside of pool->lock. | |
2772 | */ | |
2773 | wake_up_process(worker->task); | |
2774 | } | |
2775 | } | |
2776 | ||
c34056a3 | 2777 | /** |
e02b9312 | 2778 | * set_worker_dying - Tag a worker for destruction |
c34056a3 | 2779 | * @worker: worker to be destroyed |
e02b9312 | 2780 | * @list: transfer worker away from its pool->idle_list and into list |
c34056a3 | 2781 | * |
e02b9312 VS |
2782 | * Tag @worker for destruction and adjust @pool stats accordingly. The worker |
2783 | * should be idle. | |
c8e55f36 TH |
2784 | * |
2785 | * CONTEXT: | |
a9b8a985 | 2786 | * raw_spin_lock_irq(pool->lock). |
c34056a3 | 2787 | */ |
e02b9312 | 2788 | static void set_worker_dying(struct worker *worker, struct list_head *list) |
c34056a3 | 2789 | { |
bd7bdd43 | 2790 | struct worker_pool *pool = worker->pool; |
c34056a3 | 2791 | |
cd549687 | 2792 | lockdep_assert_held(&pool->lock); |
e02b9312 | 2793 | lockdep_assert_held(&wq_pool_attach_mutex); |
cd549687 | 2794 | |
c34056a3 | 2795 | /* sanity check frenzy */ |
6183c009 | 2796 | if (WARN_ON(worker->current_work) || |
73eb7fe7 LJ |
2797 | WARN_ON(!list_empty(&worker->scheduled)) || |
2798 | WARN_ON(!(worker->flags & WORKER_IDLE))) | |
6183c009 | 2799 | return; |
c34056a3 | 2800 | |
73eb7fe7 LJ |
2801 | pool->nr_workers--; |
2802 | pool->nr_idle--; | |
5bdfff96 | 2803 | |
cb444766 | 2804 | worker->flags |= WORKER_DIE; |
e02b9312 VS |
2805 | |
2806 | list_move(&worker->entry, list); | |
2807 | list_move(&worker->node, &pool->dying_workers); | |
c34056a3 TH |
2808 | } |
2809 | ||
3f959aa3 VS |
2810 | /** |
2811 | * idle_worker_timeout - check if some idle workers can now be deleted. | |
2812 | * @t: The pool's idle_timer that just expired | |
2813 | * | |
2814 | * The timer is armed in worker_enter_idle(). Note that it isn't disarmed in | |
2815 | * worker_leave_idle(), as a worker flicking between idle and active while its | |
2816 | * pool is at the too_many_workers() tipping point would cause too much timer | |
2817 | * housekeeping overhead. Since IDLE_WORKER_TIMEOUT is long enough, we just let | |
2818 | * it expire and re-evaluate things from there. | |
2819 | */ | |
32a6c723 | 2820 | static void idle_worker_timeout(struct timer_list *t) |
e22bee78 | 2821 | { |
32a6c723 | 2822 | struct worker_pool *pool = from_timer(pool, t, idle_timer); |
3f959aa3 VS |
2823 | bool do_cull = false; |
2824 | ||
2825 | if (work_pending(&pool->idle_cull_work)) | |
2826 | return; | |
e22bee78 | 2827 | |
a9b8a985 | 2828 | raw_spin_lock_irq(&pool->lock); |
e22bee78 | 2829 | |
3f959aa3 | 2830 | if (too_many_workers(pool)) { |
e22bee78 TH |
2831 | struct worker *worker; |
2832 | unsigned long expires; | |
2833 | ||
2834 | /* idle_list is kept in LIFO order, check the last one */ | |
3f959aa3 VS |
2835 | worker = list_entry(pool->idle_list.prev, struct worker, entry); |
2836 | expires = worker->last_active + IDLE_WORKER_TIMEOUT; | |
2837 | do_cull = !time_before(jiffies, expires); | |
2838 | ||
2839 | if (!do_cull) | |
2840 | mod_timer(&pool->idle_timer, expires); | |
2841 | } | |
2842 | raw_spin_unlock_irq(&pool->lock); | |
2843 | ||
2844 | if (do_cull) | |
2845 | queue_work(system_unbound_wq, &pool->idle_cull_work); | |
2846 | } | |
2847 | ||
2848 | /** | |
2849 | * idle_cull_fn - cull workers that have been idle for too long. | |
2850 | * @work: the pool's work for handling these idle workers | |
2851 | * | |
2852 | * This goes through a pool's idle workers and gets rid of those that have been | |
2853 | * idle for at least IDLE_WORKER_TIMEOUT seconds. | |
e02b9312 VS |
2854 | * |
2855 | * We don't want to disturb isolated CPUs because of a pcpu kworker being | |
2856 | * culled, so this also resets worker affinity. This requires a sleepable | |
2857 | * context, hence the split between timer callback and work item. | |
3f959aa3 VS |
2858 | */ |
2859 | static void idle_cull_fn(struct work_struct *work) | |
2860 | { | |
2861 | struct worker_pool *pool = container_of(work, struct worker_pool, idle_cull_work); | |
9680540c | 2862 | LIST_HEAD(cull_list); |
3f959aa3 | 2863 | |
e02b9312 VS |
2864 | /* |
2865 | * Grabbing wq_pool_attach_mutex here ensures an already-running worker | |
2866 | * cannot proceed beyong worker_detach_from_pool() in its self-destruct | |
2867 | * path. This is required as a previously-preempted worker could run after | |
2868 | * set_worker_dying() has happened but before wake_dying_workers() did. | |
2869 | */ | |
2870 | mutex_lock(&wq_pool_attach_mutex); | |
3f959aa3 VS |
2871 | raw_spin_lock_irq(&pool->lock); |
2872 | ||
2873 | while (too_many_workers(pool)) { | |
2874 | struct worker *worker; | |
2875 | unsigned long expires; | |
2876 | ||
63d95a91 | 2877 | worker = list_entry(pool->idle_list.prev, struct worker, entry); |
e22bee78 TH |
2878 | expires = worker->last_active + IDLE_WORKER_TIMEOUT; |
2879 | ||
3347fc9f | 2880 | if (time_before(jiffies, expires)) { |
63d95a91 | 2881 | mod_timer(&pool->idle_timer, expires); |
3347fc9f | 2882 | break; |
d5abe669 | 2883 | } |
3347fc9f | 2884 | |
e02b9312 | 2885 | set_worker_dying(worker, &cull_list); |
e22bee78 TH |
2886 | } |
2887 | ||
a9b8a985 | 2888 | raw_spin_unlock_irq(&pool->lock); |
e02b9312 VS |
2889 | wake_dying_workers(&cull_list); |
2890 | mutex_unlock(&wq_pool_attach_mutex); | |
e22bee78 | 2891 | } |
d5abe669 | 2892 | |
493a1724 | 2893 | static void send_mayday(struct work_struct *work) |
e22bee78 | 2894 | { |
112202d9 TH |
2895 | struct pool_workqueue *pwq = get_work_pwq(work); |
2896 | struct workqueue_struct *wq = pwq->wq; | |
493a1724 | 2897 | |
2e109a28 | 2898 | lockdep_assert_held(&wq_mayday_lock); |
e22bee78 | 2899 | |
493008a8 | 2900 | if (!wq->rescuer) |
493a1724 | 2901 | return; |
e22bee78 TH |
2902 | |
2903 | /* mayday mayday mayday */ | |
493a1724 | 2904 | if (list_empty(&pwq->mayday_node)) { |
77668c8b LJ |
2905 | /* |
2906 | * If @pwq is for an unbound wq, its base ref may be put at | |
2907 | * any time due to an attribute change. Pin @pwq until the | |
2908 | * rescuer is done with it. | |
2909 | */ | |
2910 | get_pwq(pwq); | |
493a1724 | 2911 | list_add_tail(&pwq->mayday_node, &wq->maydays); |
e22bee78 | 2912 | wake_up_process(wq->rescuer->task); |
725e8ec5 | 2913 | pwq->stats[PWQ_STAT_MAYDAY]++; |
493a1724 | 2914 | } |
e22bee78 TH |
2915 | } |
2916 | ||
32a6c723 | 2917 | static void pool_mayday_timeout(struct timer_list *t) |
e22bee78 | 2918 | { |
32a6c723 | 2919 | struct worker_pool *pool = from_timer(pool, t, mayday_timer); |
e22bee78 TH |
2920 | struct work_struct *work; |
2921 | ||
a9b8a985 SAS |
2922 | raw_spin_lock_irq(&pool->lock); |
2923 | raw_spin_lock(&wq_mayday_lock); /* for wq->maydays */ | |
e22bee78 | 2924 | |
63d95a91 | 2925 | if (need_to_create_worker(pool)) { |
e22bee78 TH |
2926 | /* |
2927 | * We've been trying to create a new worker but | |
2928 | * haven't been successful. We might be hitting an | |
2929 | * allocation deadlock. Send distress signals to | |
2930 | * rescuers. | |
2931 | */ | |
63d95a91 | 2932 | list_for_each_entry(work, &pool->worklist, entry) |
e22bee78 | 2933 | send_mayday(work); |
1da177e4 | 2934 | } |
e22bee78 | 2935 | |
a9b8a985 SAS |
2936 | raw_spin_unlock(&wq_mayday_lock); |
2937 | raw_spin_unlock_irq(&pool->lock); | |
e22bee78 | 2938 | |
63d95a91 | 2939 | mod_timer(&pool->mayday_timer, jiffies + MAYDAY_INTERVAL); |
1da177e4 LT |
2940 | } |
2941 | ||
e22bee78 TH |
2942 | /** |
2943 | * maybe_create_worker - create a new worker if necessary | |
63d95a91 | 2944 | * @pool: pool to create a new worker for |
e22bee78 | 2945 | * |
63d95a91 | 2946 | * Create a new worker for @pool if necessary. @pool is guaranteed to |
e22bee78 TH |
2947 | * have at least one idle worker on return from this function. If |
2948 | * creating a new worker takes longer than MAYDAY_INTERVAL, mayday is | |
63d95a91 | 2949 | * sent to all rescuers with works scheduled on @pool to resolve |
e22bee78 TH |
2950 | * possible allocation deadlock. |
2951 | * | |
c5aa87bb TH |
2952 | * On return, need_to_create_worker() is guaranteed to be %false and |
2953 | * may_start_working() %true. | |
e22bee78 TH |
2954 | * |
2955 | * LOCKING: | |
a9b8a985 | 2956 | * raw_spin_lock_irq(pool->lock) which may be released and regrabbed |
e22bee78 TH |
2957 | * multiple times. Does GFP_KERNEL allocations. Called only from |
2958 | * manager. | |
e22bee78 | 2959 | */ |
29187a9e | 2960 | static void maybe_create_worker(struct worker_pool *pool) |
d565ed63 TH |
2961 | __releases(&pool->lock) |
2962 | __acquires(&pool->lock) | |
1da177e4 | 2963 | { |
e22bee78 | 2964 | restart: |
a9b8a985 | 2965 | raw_spin_unlock_irq(&pool->lock); |
9f9c2364 | 2966 | |
e22bee78 | 2967 | /* if we don't make progress in MAYDAY_INITIAL_TIMEOUT, call for help */ |
63d95a91 | 2968 | mod_timer(&pool->mayday_timer, jiffies + MAYDAY_INITIAL_TIMEOUT); |
e22bee78 TH |
2969 | |
2970 | while (true) { | |
051e1850 | 2971 | if (create_worker(pool) || !need_to_create_worker(pool)) |
e22bee78 | 2972 | break; |
1da177e4 | 2973 | |
e212f361 | 2974 | schedule_timeout_interruptible(CREATE_COOLDOWN); |
9f9c2364 | 2975 | |
63d95a91 | 2976 | if (!need_to_create_worker(pool)) |
e22bee78 TH |
2977 | break; |
2978 | } | |
2979 | ||
63d95a91 | 2980 | del_timer_sync(&pool->mayday_timer); |
a9b8a985 | 2981 | raw_spin_lock_irq(&pool->lock); |
051e1850 LJ |
2982 | /* |
2983 | * This is necessary even after a new worker was just successfully | |
2984 | * created as @pool->lock was dropped and the new worker might have | |
2985 | * already become busy. | |
2986 | */ | |
63d95a91 | 2987 | if (need_to_create_worker(pool)) |
e22bee78 | 2988 | goto restart; |
e22bee78 TH |
2989 | } |
2990 | ||
73f53c4a | 2991 | /** |
e22bee78 TH |
2992 | * manage_workers - manage worker pool |
2993 | * @worker: self | |
73f53c4a | 2994 | * |
706026c2 | 2995 | * Assume the manager role and manage the worker pool @worker belongs |
e22bee78 | 2996 | * to. At any given time, there can be only zero or one manager per |
706026c2 | 2997 | * pool. The exclusion is handled automatically by this function. |
e22bee78 TH |
2998 | * |
2999 | * The caller can safely start processing works on false return. On | |
3000 | * true return, it's guaranteed that need_to_create_worker() is false | |
3001 | * and may_start_working() is true. | |
73f53c4a TH |
3002 | * |
3003 | * CONTEXT: | |
a9b8a985 | 3004 | * raw_spin_lock_irq(pool->lock) which may be released and regrabbed |
e22bee78 TH |
3005 | * multiple times. Does GFP_KERNEL allocations. |
3006 | * | |
d185af30 | 3007 | * Return: |
29187a9e TH |
3008 | * %false if the pool doesn't need management and the caller can safely |
3009 | * start processing works, %true if management function was performed and | |
3010 | * the conditions that the caller verified before calling the function may | |
3011 | * no longer be true. | |
73f53c4a | 3012 | */ |
e22bee78 | 3013 | static bool manage_workers(struct worker *worker) |
73f53c4a | 3014 | { |
63d95a91 | 3015 | struct worker_pool *pool = worker->pool; |
73f53c4a | 3016 | |
692b4825 | 3017 | if (pool->flags & POOL_MANAGER_ACTIVE) |
29187a9e | 3018 | return false; |
692b4825 TH |
3019 | |
3020 | pool->flags |= POOL_MANAGER_ACTIVE; | |
2607d7a6 | 3021 | pool->manager = worker; |
1e19ffc6 | 3022 | |
29187a9e | 3023 | maybe_create_worker(pool); |
e22bee78 | 3024 | |
2607d7a6 | 3025 | pool->manager = NULL; |
692b4825 | 3026 | pool->flags &= ~POOL_MANAGER_ACTIVE; |
d8bb65ab | 3027 | rcuwait_wake_up(&manager_wait); |
29187a9e | 3028 | return true; |
73f53c4a TH |
3029 | } |
3030 | ||
a62428c0 TH |
3031 | /** |
3032 | * process_one_work - process single work | |
c34056a3 | 3033 | * @worker: self |
a62428c0 TH |
3034 | * @work: work to process |
3035 | * | |
3036 | * Process @work. This function contains all the logics necessary to | |
3037 | * process a single work including synchronization against and | |
3038 | * interaction with other workers on the same cpu, queueing and | |
3039 | * flushing. As long as context requirement is met, any worker can | |
3040 | * call this function to process a work. | |
3041 | * | |
3042 | * CONTEXT: | |
a9b8a985 | 3043 | * raw_spin_lock_irq(pool->lock) which is released and regrabbed. |
a62428c0 | 3044 | */ |
c34056a3 | 3045 | static void process_one_work(struct worker *worker, struct work_struct *work) |
d565ed63 TH |
3046 | __releases(&pool->lock) |
3047 | __acquires(&pool->lock) | |
a62428c0 | 3048 | { |
112202d9 | 3049 | struct pool_workqueue *pwq = get_work_pwq(work); |
bd7bdd43 | 3050 | struct worker_pool *pool = worker->pool; |
c4560c2c | 3051 | unsigned long work_data; |
c35aea39 | 3052 | int lockdep_start_depth, rcu_start_depth; |
a62428c0 TH |
3053 | #ifdef CONFIG_LOCKDEP |
3054 | /* | |
3055 | * It is permissible to free the struct work_struct from | |
3056 | * inside the function that is called from it, this we need to | |
3057 | * take into account for lockdep too. To avoid bogus "held | |
3058 | * lock freed" warnings as well as problems when looking into | |
3059 | * work->lockdep_map, make a copy and use that here. | |
3060 | */ | |
4d82a1de PZ |
3061 | struct lockdep_map lockdep_map; |
3062 | ||
3063 | lockdep_copy_map(&lockdep_map, &work->lockdep_map); | |
a62428c0 | 3064 | #endif |
807407c0 | 3065 | /* ensure we're on the correct CPU */ |
85327af6 | 3066 | WARN_ON_ONCE(!(pool->flags & POOL_DISASSOCIATED) && |
ec22ca5e | 3067 | raw_smp_processor_id() != pool->cpu); |
25511a47 | 3068 | |
8930caba | 3069 | /* claim and dequeue */ |
a62428c0 | 3070 | debug_work_deactivate(work); |
c9e7cf27 | 3071 | hash_add(pool->busy_hash, &worker->hentry, (unsigned long)work); |
c34056a3 | 3072 | worker->current_work = work; |
a2c1c57b | 3073 | worker->current_func = work->func; |
112202d9 | 3074 | worker->current_pwq = pwq; |
4cb1ef64 TH |
3075 | if (worker->task) |
3076 | worker->current_at = worker->task->se.sum_exec_runtime; | |
c4560c2c | 3077 | work_data = *work_data_bits(work); |
d812796e | 3078 | worker->current_color = get_work_color(work_data); |
7a22ad75 | 3079 | |
8bf89593 TH |
3080 | /* |
3081 | * Record wq name for cmdline and debug reporting, may get | |
3082 | * overridden through set_worker_desc(). | |
3083 | */ | |
3084 | strscpy(worker->desc, pwq->wq->name, WORKER_DESC_LEN); | |
3085 | ||
a62428c0 TH |
3086 | list_del_init(&work->entry); |
3087 | ||
fb0e7beb | 3088 | /* |
228f1d00 LJ |
3089 | * CPU intensive works don't participate in concurrency management. |
3090 | * They're the scheduler's responsibility. This takes @worker out | |
3091 | * of concurrency management and the next code block will chain | |
3092 | * execution of the pending work items. | |
fb0e7beb | 3093 | */ |
616db877 | 3094 | if (unlikely(pwq->wq->flags & WQ_CPU_INTENSIVE)) |
228f1d00 | 3095 | worker_set_flags(worker, WORKER_CPU_INTENSIVE); |
fb0e7beb | 3096 | |
974271c4 | 3097 | /* |
0219a352 TH |
3098 | * Kick @pool if necessary. It's always noop for per-cpu worker pools |
3099 | * since nr_running would always be >= 1 at this point. This is used to | |
3100 | * chain execution of the pending work items for WORKER_NOT_RUNNING | |
3101 | * workers such as the UNBOUND and CPU_INTENSIVE ones. | |
974271c4 | 3102 | */ |
0219a352 | 3103 | kick_pool(pool); |
974271c4 | 3104 | |
8930caba | 3105 | /* |
7c3eed5c | 3106 | * Record the last pool and clear PENDING which should be the last |
d565ed63 | 3107 | * update to @work. Also, do this inside @pool->lock so that |
23657bb1 TH |
3108 | * PENDING and queued state changes happen together while IRQ is |
3109 | * disabled. | |
8930caba | 3110 | */ |
7c3eed5c | 3111 | set_work_pool_and_clear_pending(work, pool->id); |
a62428c0 | 3112 | |
fe48ba7d | 3113 | pwq->stats[PWQ_STAT_STARTED]++; |
a9b8a985 | 3114 | raw_spin_unlock_irq(&pool->lock); |
a62428c0 | 3115 | |
c35aea39 TH |
3116 | rcu_start_depth = rcu_preempt_depth(); |
3117 | lockdep_start_depth = lockdep_depth(current); | |
a1d14934 | 3118 | lock_map_acquire(&pwq->wq->lockdep_map); |
a62428c0 | 3119 | lock_map_acquire(&lockdep_map); |
e6f3faa7 | 3120 | /* |
f52be570 PZ |
3121 | * Strictly speaking we should mark the invariant state without holding |
3122 | * any locks, that is, before these two lock_map_acquire()'s. | |
e6f3faa7 PZ |
3123 | * |
3124 | * However, that would result in: | |
3125 | * | |
3126 | * A(W1) | |
3127 | * WFC(C) | |
3128 | * A(W1) | |
3129 | * C(C) | |
3130 | * | |
3131 | * Which would create W1->C->W1 dependencies, even though there is no | |
3132 | * actual deadlock possible. There are two solutions, using a | |
3133 | * read-recursive acquire on the work(queue) 'locks', but this will then | |
f52be570 | 3134 | * hit the lockdep limitation on recursive locks, or simply discard |
e6f3faa7 PZ |
3135 | * these locks. |
3136 | * | |
3137 | * AFAICT there is no possible deadlock scenario between the | |
3138 | * flush_work() and complete() primitives (except for single-threaded | |
3139 | * workqueues), so hiding them isn't a problem. | |
3140 | */ | |
f52be570 | 3141 | lockdep_invariant_state(true); |
e36c886a | 3142 | trace_workqueue_execute_start(work); |
a2c1c57b | 3143 | worker->current_func(work); |
e36c886a AV |
3144 | /* |
3145 | * While we must be careful to not use "work" after this, the trace | |
3146 | * point will only record its address. | |
3147 | */ | |
1c5da0ec | 3148 | trace_workqueue_execute_end(work, worker->current_func); |
725e8ec5 | 3149 | pwq->stats[PWQ_STAT_COMPLETED]++; |
a62428c0 | 3150 | lock_map_release(&lockdep_map); |
112202d9 | 3151 | lock_map_release(&pwq->wq->lockdep_map); |
a62428c0 | 3152 | |
c35aea39 TH |
3153 | if (unlikely((worker->task && in_atomic()) || |
3154 | lockdep_depth(current) != lockdep_start_depth || | |
3155 | rcu_preempt_depth() != rcu_start_depth)) { | |
3156 | pr_err("BUG: workqueue leaked atomic, lock or RCU: %s[%d]\n" | |
3157 | " preempt=0x%08x lock=%d->%d RCU=%d->%d workfn=%ps\n", | |
3158 | current->comm, task_pid_nr(current), preempt_count(), | |
3159 | lockdep_start_depth, lockdep_depth(current), | |
3160 | rcu_start_depth, rcu_preempt_depth(), | |
3161 | worker->current_func); | |
a62428c0 TH |
3162 | debug_show_held_locks(current); |
3163 | dump_stack(); | |
3164 | } | |
3165 | ||
b22ce278 | 3166 | /* |
025f50f3 | 3167 | * The following prevents a kworker from hogging CPU on !PREEMPTION |
b22ce278 TH |
3168 | * kernels, where a requeueing work item waiting for something to |
3169 | * happen could deadlock with stop_machine as such work item could | |
3170 | * indefinitely requeue itself while all other CPUs are trapped in | |
789cbbec JL |
3171 | * stop_machine. At the same time, report a quiescent RCU state so |
3172 | * the same condition doesn't freeze RCU. | |
b22ce278 | 3173 | */ |
4cb1ef64 TH |
3174 | if (worker->task) |
3175 | cond_resched(); | |
b22ce278 | 3176 | |
a9b8a985 | 3177 | raw_spin_lock_irq(&pool->lock); |
a62428c0 | 3178 | |
616db877 TH |
3179 | /* |
3180 | * In addition to %WQ_CPU_INTENSIVE, @worker may also have been marked | |
3181 | * CPU intensive by wq_worker_tick() if @work hogged CPU longer than | |
3182 | * wq_cpu_intensive_thresh_us. Clear it. | |
3183 | */ | |
3184 | worker_clr_flags(worker, WORKER_CPU_INTENSIVE); | |
fb0e7beb | 3185 | |
1b69ac6b JW |
3186 | /* tag the worker for identification in schedule() */ |
3187 | worker->last_func = worker->current_func; | |
3188 | ||
a62428c0 | 3189 | /* we're done with it, release */ |
42f8570f | 3190 | hash_del(&worker->hentry); |
c34056a3 | 3191 | worker->current_work = NULL; |
a2c1c57b | 3192 | worker->current_func = NULL; |
112202d9 | 3193 | worker->current_pwq = NULL; |
d812796e | 3194 | worker->current_color = INT_MAX; |
dd6c3c54 TH |
3195 | |
3196 | /* must be the last step, see the function comment */ | |
c4560c2c | 3197 | pwq_dec_nr_in_flight(pwq, work_data); |
a62428c0 TH |
3198 | } |
3199 | ||
affee4b2 TH |
3200 | /** |
3201 | * process_scheduled_works - process scheduled works | |
3202 | * @worker: self | |
3203 | * | |
3204 | * Process all scheduled works. Please note that the scheduled list | |
3205 | * may change while processing a work, so this function repeatedly | |
3206 | * fetches a work from the top and executes it. | |
3207 | * | |
3208 | * CONTEXT: | |
a9b8a985 | 3209 | * raw_spin_lock_irq(pool->lock) which may be released and regrabbed |
affee4b2 TH |
3210 | * multiple times. |
3211 | */ | |
3212 | static void process_scheduled_works(struct worker *worker) | |
1da177e4 | 3213 | { |
c0ab017d TH |
3214 | struct work_struct *work; |
3215 | bool first = true; | |
3216 | ||
3217 | while ((work = list_first_entry_or_null(&worker->scheduled, | |
3218 | struct work_struct, entry))) { | |
3219 | if (first) { | |
3220 | worker->pool->watchdog_ts = jiffies; | |
3221 | first = false; | |
3222 | } | |
c34056a3 | 3223 | process_one_work(worker, work); |
1da177e4 | 3224 | } |
1da177e4 LT |
3225 | } |
3226 | ||
197f6acc TH |
3227 | static void set_pf_worker(bool val) |
3228 | { | |
3229 | mutex_lock(&wq_pool_attach_mutex); | |
3230 | if (val) | |
3231 | current->flags |= PF_WQ_WORKER; | |
3232 | else | |
3233 | current->flags &= ~PF_WQ_WORKER; | |
3234 | mutex_unlock(&wq_pool_attach_mutex); | |
3235 | } | |
3236 | ||
4690c4ab TH |
3237 | /** |
3238 | * worker_thread - the worker thread function | |
c34056a3 | 3239 | * @__worker: self |
4690c4ab | 3240 | * |
c5aa87bb TH |
3241 | * The worker thread function. All workers belong to a worker_pool - |
3242 | * either a per-cpu one or dynamic unbound one. These workers process all | |
3243 | * work items regardless of their specific target workqueue. The only | |
3244 | * exception is work items which belong to workqueues with a rescuer which | |
3245 | * will be explained in rescuer_thread(). | |
d185af30 YB |
3246 | * |
3247 | * Return: 0 | |
4690c4ab | 3248 | */ |
c34056a3 | 3249 | static int worker_thread(void *__worker) |
1da177e4 | 3250 | { |
c34056a3 | 3251 | struct worker *worker = __worker; |
bd7bdd43 | 3252 | struct worker_pool *pool = worker->pool; |
1da177e4 | 3253 | |
e22bee78 | 3254 | /* tell the scheduler that this is a workqueue worker */ |
197f6acc | 3255 | set_pf_worker(true); |
c8e55f36 | 3256 | woke_up: |
a9b8a985 | 3257 | raw_spin_lock_irq(&pool->lock); |
1da177e4 | 3258 | |
a9ab775b TH |
3259 | /* am I supposed to die? */ |
3260 | if (unlikely(worker->flags & WORKER_DIE)) { | |
a9b8a985 | 3261 | raw_spin_unlock_irq(&pool->lock); |
197f6acc | 3262 | set_pf_worker(false); |
60f5a4bc LJ |
3263 | |
3264 | set_task_comm(worker->task, "kworker/dying"); | |
e441b56f | 3265 | ida_free(&pool->worker_ida, worker->id); |
a2d812a2 | 3266 | worker_detach_from_pool(worker); |
e02b9312 | 3267 | WARN_ON_ONCE(!list_empty(&worker->entry)); |
60f5a4bc | 3268 | kfree(worker); |
a9ab775b | 3269 | return 0; |
c8e55f36 | 3270 | } |
affee4b2 | 3271 | |
c8e55f36 | 3272 | worker_leave_idle(worker); |
db7bccf4 | 3273 | recheck: |
e22bee78 | 3274 | /* no more worker necessary? */ |
63d95a91 | 3275 | if (!need_more_worker(pool)) |
e22bee78 TH |
3276 | goto sleep; |
3277 | ||
3278 | /* do we need to manage? */ | |
63d95a91 | 3279 | if (unlikely(!may_start_working(pool)) && manage_workers(worker)) |
e22bee78 TH |
3280 | goto recheck; |
3281 | ||
c8e55f36 TH |
3282 | /* |
3283 | * ->scheduled list can only be filled while a worker is | |
3284 | * preparing to process a work or actually processing it. | |
3285 | * Make sure nobody diddled with it while I was sleeping. | |
3286 | */ | |
6183c009 | 3287 | WARN_ON_ONCE(!list_empty(&worker->scheduled)); |
c8e55f36 | 3288 | |
e22bee78 | 3289 | /* |
a9ab775b TH |
3290 | * Finish PREP stage. We're guaranteed to have at least one idle |
3291 | * worker or that someone else has already assumed the manager | |
3292 | * role. This is where @worker starts participating in concurrency | |
3293 | * management if applicable and concurrency management is restored | |
3294 | * after being rebound. See rebind_workers() for details. | |
e22bee78 | 3295 | */ |
a9ab775b | 3296 | worker_clr_flags(worker, WORKER_PREP | WORKER_REBOUND); |
e22bee78 TH |
3297 | |
3298 | do { | |
c8e55f36 | 3299 | struct work_struct *work = |
bd7bdd43 | 3300 | list_first_entry(&pool->worklist, |
c8e55f36 TH |
3301 | struct work_struct, entry); |
3302 | ||
873eaca6 TH |
3303 | if (assign_work(work, worker, NULL)) |
3304 | process_scheduled_works(worker); | |
63d95a91 | 3305 | } while (keep_working(pool)); |
e22bee78 | 3306 | |
228f1d00 | 3307 | worker_set_flags(worker, WORKER_PREP); |
d313dd85 | 3308 | sleep: |
c8e55f36 | 3309 | /* |
d565ed63 TH |
3310 | * pool->lock is held and there's no work to process and no need to |
3311 | * manage, sleep. Workers are woken up only while holding | |
3312 | * pool->lock or from local cpu, so setting the current state | |
3313 | * before releasing pool->lock is enough to prevent losing any | |
3314 | * event. | |
c8e55f36 TH |
3315 | */ |
3316 | worker_enter_idle(worker); | |
c5a94a61 | 3317 | __set_current_state(TASK_IDLE); |
a9b8a985 | 3318 | raw_spin_unlock_irq(&pool->lock); |
c8e55f36 TH |
3319 | schedule(); |
3320 | goto woke_up; | |
1da177e4 LT |
3321 | } |
3322 | ||
e22bee78 TH |
3323 | /** |
3324 | * rescuer_thread - the rescuer thread function | |
111c225a | 3325 | * @__rescuer: self |
e22bee78 TH |
3326 | * |
3327 | * Workqueue rescuer thread function. There's one rescuer for each | |
493008a8 | 3328 | * workqueue which has WQ_MEM_RECLAIM set. |
e22bee78 | 3329 | * |
706026c2 | 3330 | * Regular work processing on a pool may block trying to create a new |
e22bee78 TH |
3331 | * worker which uses GFP_KERNEL allocation which has slight chance of |
3332 | * developing into deadlock if some works currently on the same queue | |
3333 | * need to be processed to satisfy the GFP_KERNEL allocation. This is | |
3334 | * the problem rescuer solves. | |
3335 | * | |
706026c2 TH |
3336 | * When such condition is possible, the pool summons rescuers of all |
3337 | * workqueues which have works queued on the pool and let them process | |
e22bee78 TH |
3338 | * those works so that forward progress can be guaranteed. |
3339 | * | |
3340 | * This should happen rarely. | |
d185af30 YB |
3341 | * |
3342 | * Return: 0 | |
e22bee78 | 3343 | */ |
111c225a | 3344 | static int rescuer_thread(void *__rescuer) |
e22bee78 | 3345 | { |
111c225a TH |
3346 | struct worker *rescuer = __rescuer; |
3347 | struct workqueue_struct *wq = rescuer->rescue_wq; | |
4d595b86 | 3348 | bool should_stop; |
e22bee78 TH |
3349 | |
3350 | set_user_nice(current, RESCUER_NICE_LEVEL); | |
111c225a TH |
3351 | |
3352 | /* | |
3353 | * Mark rescuer as worker too. As WORKER_PREP is never cleared, it | |
3354 | * doesn't participate in concurrency management. | |
3355 | */ | |
197f6acc | 3356 | set_pf_worker(true); |
e22bee78 | 3357 | repeat: |
c5a94a61 | 3358 | set_current_state(TASK_IDLE); |
e22bee78 | 3359 | |
4d595b86 LJ |
3360 | /* |
3361 | * By the time the rescuer is requested to stop, the workqueue | |
3362 | * shouldn't have any work pending, but @wq->maydays may still have | |
3363 | * pwq(s) queued. This can happen by non-rescuer workers consuming | |
3364 | * all the work items before the rescuer got to them. Go through | |
3365 | * @wq->maydays processing before acting on should_stop so that the | |
3366 | * list is always empty on exit. | |
3367 | */ | |
3368 | should_stop = kthread_should_stop(); | |
e22bee78 | 3369 | |
493a1724 | 3370 | /* see whether any pwq is asking for help */ |
a9b8a985 | 3371 | raw_spin_lock_irq(&wq_mayday_lock); |
493a1724 TH |
3372 | |
3373 | while (!list_empty(&wq->maydays)) { | |
3374 | struct pool_workqueue *pwq = list_first_entry(&wq->maydays, | |
3375 | struct pool_workqueue, mayday_node); | |
112202d9 | 3376 | struct worker_pool *pool = pwq->pool; |
e22bee78 TH |
3377 | struct work_struct *work, *n; |
3378 | ||
3379 | __set_current_state(TASK_RUNNING); | |
493a1724 TH |
3380 | list_del_init(&pwq->mayday_node); |
3381 | ||
a9b8a985 | 3382 | raw_spin_unlock_irq(&wq_mayday_lock); |
e22bee78 | 3383 | |
51697d39 LJ |
3384 | worker_attach_to_pool(rescuer, pool); |
3385 | ||
a9b8a985 | 3386 | raw_spin_lock_irq(&pool->lock); |
e22bee78 TH |
3387 | |
3388 | /* | |
3389 | * Slurp in all works issued via this workqueue and | |
3390 | * process'em. | |
3391 | */ | |
873eaca6 | 3392 | WARN_ON_ONCE(!list_empty(&rescuer->scheduled)); |
82607adc | 3393 | list_for_each_entry_safe(work, n, &pool->worklist, entry) { |
873eaca6 TH |
3394 | if (get_work_pwq(work) == pwq && |
3395 | assign_work(work, rescuer, &n)) | |
725e8ec5 | 3396 | pwq->stats[PWQ_STAT_RESCUED]++; |
82607adc | 3397 | } |
e22bee78 | 3398 | |
873eaca6 | 3399 | if (!list_empty(&rescuer->scheduled)) { |
008847f6 N |
3400 | process_scheduled_works(rescuer); |
3401 | ||
3402 | /* | |
3403 | * The above execution of rescued work items could | |
3404 | * have created more to rescue through | |
f97a4a1a | 3405 | * pwq_activate_first_inactive() or chained |
008847f6 N |
3406 | * queueing. Let's put @pwq back on mayday list so |
3407 | * that such back-to-back work items, which may be | |
3408 | * being used to relieve memory pressure, don't | |
3409 | * incur MAYDAY_INTERVAL delay inbetween. | |
3410 | */ | |
4f3f4cf3 | 3411 | if (pwq->nr_active && need_to_create_worker(pool)) { |
a9b8a985 | 3412 | raw_spin_lock(&wq_mayday_lock); |
e66b39af TH |
3413 | /* |
3414 | * Queue iff we aren't racing destruction | |
3415 | * and somebody else hasn't queued it already. | |
3416 | */ | |
3417 | if (wq->rescuer && list_empty(&pwq->mayday_node)) { | |
3418 | get_pwq(pwq); | |
3419 | list_add_tail(&pwq->mayday_node, &wq->maydays); | |
3420 | } | |
a9b8a985 | 3421 | raw_spin_unlock(&wq_mayday_lock); |
008847f6 N |
3422 | } |
3423 | } | |
7576958a | 3424 | |
77668c8b LJ |
3425 | /* |
3426 | * Put the reference grabbed by send_mayday(). @pool won't | |
13b1d625 | 3427 | * go away while we're still attached to it. |
77668c8b LJ |
3428 | */ |
3429 | put_pwq(pwq); | |
3430 | ||
7576958a | 3431 | /* |
0219a352 TH |
3432 | * Leave this pool. Notify regular workers; otherwise, we end up |
3433 | * with 0 concurrency and stalling the execution. | |
7576958a | 3434 | */ |
0219a352 | 3435 | kick_pool(pool); |
7576958a | 3436 | |
a9b8a985 | 3437 | raw_spin_unlock_irq(&pool->lock); |
13b1d625 | 3438 | |
a2d812a2 | 3439 | worker_detach_from_pool(rescuer); |
13b1d625 | 3440 | |
a9b8a985 | 3441 | raw_spin_lock_irq(&wq_mayday_lock); |
e22bee78 TH |
3442 | } |
3443 | ||
a9b8a985 | 3444 | raw_spin_unlock_irq(&wq_mayday_lock); |
493a1724 | 3445 | |
4d595b86 LJ |
3446 | if (should_stop) { |
3447 | __set_current_state(TASK_RUNNING); | |
197f6acc | 3448 | set_pf_worker(false); |
4d595b86 LJ |
3449 | return 0; |
3450 | } | |
3451 | ||
111c225a TH |
3452 | /* rescuers should never participate in concurrency management */ |
3453 | WARN_ON_ONCE(!(rescuer->flags & WORKER_NOT_RUNNING)); | |
e22bee78 TH |
3454 | schedule(); |
3455 | goto repeat; | |
1da177e4 LT |
3456 | } |
3457 | ||
4cb1ef64 TH |
3458 | static void bh_worker(struct worker *worker) |
3459 | { | |
3460 | struct worker_pool *pool = worker->pool; | |
3461 | int nr_restarts = BH_WORKER_RESTARTS; | |
3462 | unsigned long end = jiffies + BH_WORKER_JIFFIES; | |
3463 | ||
3464 | raw_spin_lock_irq(&pool->lock); | |
3465 | worker_leave_idle(worker); | |
3466 | ||
3467 | /* | |
3468 | * This function follows the structure of worker_thread(). See there for | |
3469 | * explanations on each step. | |
3470 | */ | |
3471 | if (!need_more_worker(pool)) | |
3472 | goto done; | |
3473 | ||
3474 | WARN_ON_ONCE(!list_empty(&worker->scheduled)); | |
3475 | worker_clr_flags(worker, WORKER_PREP | WORKER_REBOUND); | |
3476 | ||
3477 | do { | |
3478 | struct work_struct *work = | |
3479 | list_first_entry(&pool->worklist, | |
3480 | struct work_struct, entry); | |
3481 | ||
3482 | if (assign_work(work, worker, NULL)) | |
3483 | process_scheduled_works(worker); | |
3484 | } while (keep_working(pool) && | |
3485 | --nr_restarts && time_before(jiffies, end)); | |
3486 | ||
3487 | worker_set_flags(worker, WORKER_PREP); | |
3488 | done: | |
3489 | worker_enter_idle(worker); | |
3490 | kick_pool(pool); | |
3491 | raw_spin_unlock_irq(&pool->lock); | |
3492 | } | |
3493 | ||
3494 | /* | |
3495 | * TODO: Convert all tasklet users to workqueue and use softirq directly. | |
3496 | * | |
3497 | * This is currently called from tasklet[_hi]action() and thus is also called | |
3498 | * whenever there are tasklets to run. Let's do an early exit if there's nothing | |
3499 | * queued. Once conversion from tasklet is complete, the need_more_worker() test | |
3500 | * can be dropped. | |
3501 | * | |
3502 | * After full conversion, we'll add worker->softirq_action, directly use the | |
3503 | * softirq action and obtain the worker pointer from the softirq_action pointer. | |
3504 | */ | |
3505 | void workqueue_softirq_action(bool highpri) | |
3506 | { | |
3507 | struct worker_pool *pool = | |
3508 | &per_cpu(bh_worker_pools, smp_processor_id())[highpri]; | |
3509 | if (need_more_worker(pool)) | |
3510 | bh_worker(list_first_entry(&pool->workers, struct worker, node)); | |
3511 | } | |
3512 | ||
fca839c0 TH |
3513 | /** |
3514 | * check_flush_dependency - check for flush dependency sanity | |
3515 | * @target_wq: workqueue being flushed | |
3516 | * @target_work: work item being flushed (NULL for workqueue flushes) | |
3517 | * | |
3518 | * %current is trying to flush the whole @target_wq or @target_work on it. | |
3519 | * If @target_wq doesn't have %WQ_MEM_RECLAIM, verify that %current is not | |
3520 | * reclaiming memory or running on a workqueue which doesn't have | |
3521 | * %WQ_MEM_RECLAIM as that can break forward-progress guarantee leading to | |
3522 | * a deadlock. | |
3523 | */ | |
3524 | static void check_flush_dependency(struct workqueue_struct *target_wq, | |
3525 | struct work_struct *target_work) | |
3526 | { | |
3527 | work_func_t target_func = target_work ? target_work->func : NULL; | |
3528 | struct worker *worker; | |
3529 | ||
3530 | if (target_wq->flags & WQ_MEM_RECLAIM) | |
3531 | return; | |
3532 | ||
3533 | worker = current_wq_worker(); | |
3534 | ||
3535 | WARN_ONCE(current->flags & PF_MEMALLOC, | |
d75f773c | 3536 | "workqueue: PF_MEMALLOC task %d(%s) is flushing !WQ_MEM_RECLAIM %s:%ps", |
fca839c0 | 3537 | current->pid, current->comm, target_wq->name, target_func); |
23d11a58 TH |
3538 | WARN_ONCE(worker && ((worker->current_pwq->wq->flags & |
3539 | (WQ_MEM_RECLAIM | __WQ_LEGACY)) == WQ_MEM_RECLAIM), | |
d75f773c | 3540 | "workqueue: WQ_MEM_RECLAIM %s:%ps is flushing !WQ_MEM_RECLAIM %s:%ps", |
fca839c0 TH |
3541 | worker->current_pwq->wq->name, worker->current_func, |
3542 | target_wq->name, target_func); | |
3543 | } | |
3544 | ||
fc2e4d70 ON |
3545 | struct wq_barrier { |
3546 | struct work_struct work; | |
3547 | struct completion done; | |
2607d7a6 | 3548 | struct task_struct *task; /* purely informational */ |
fc2e4d70 ON |
3549 | }; |
3550 | ||
3551 | static void wq_barrier_func(struct work_struct *work) | |
3552 | { | |
3553 | struct wq_barrier *barr = container_of(work, struct wq_barrier, work); | |
3554 | complete(&barr->done); | |
3555 | } | |
3556 | ||
4690c4ab TH |
3557 | /** |
3558 | * insert_wq_barrier - insert a barrier work | |
112202d9 | 3559 | * @pwq: pwq to insert barrier into |
4690c4ab | 3560 | * @barr: wq_barrier to insert |
affee4b2 TH |
3561 | * @target: target work to attach @barr to |
3562 | * @worker: worker currently executing @target, NULL if @target is not executing | |
4690c4ab | 3563 | * |
affee4b2 TH |
3564 | * @barr is linked to @target such that @barr is completed only after |
3565 | * @target finishes execution. Please note that the ordering | |
3566 | * guarantee is observed only with respect to @target and on the local | |
3567 | * cpu. | |
3568 | * | |
3569 | * Currently, a queued barrier can't be canceled. This is because | |
3570 | * try_to_grab_pending() can't determine whether the work to be | |
3571 | * grabbed is at the head of the queue and thus can't clear LINKED | |
3572 | * flag of the previous work while there must be a valid next work | |
3573 | * after a work with LINKED flag set. | |
3574 | * | |
3575 | * Note that when @worker is non-NULL, @target may be modified | |
112202d9 | 3576 | * underneath us, so we can't reliably determine pwq from @target. |
4690c4ab TH |
3577 | * |
3578 | * CONTEXT: | |
a9b8a985 | 3579 | * raw_spin_lock_irq(pool->lock). |
4690c4ab | 3580 | */ |
112202d9 | 3581 | static void insert_wq_barrier(struct pool_workqueue *pwq, |
affee4b2 TH |
3582 | struct wq_barrier *barr, |
3583 | struct work_struct *target, struct worker *worker) | |
fc2e4d70 | 3584 | { |
4cb1ef64 | 3585 | static __maybe_unused struct lock_class_key bh_key, thr_key; |
d812796e LJ |
3586 | unsigned int work_flags = 0; |
3587 | unsigned int work_color; | |
affee4b2 | 3588 | struct list_head *head; |
affee4b2 | 3589 | |
dc186ad7 | 3590 | /* |
d565ed63 | 3591 | * debugobject calls are safe here even with pool->lock locked |
dc186ad7 TG |
3592 | * as we know for sure that this will not trigger any of the |
3593 | * checks and call back into the fixup functions where we | |
3594 | * might deadlock. | |
4cb1ef64 TH |
3595 | * |
3596 | * BH and threaded workqueues need separate lockdep keys to avoid | |
3597 | * spuriously triggering "inconsistent {SOFTIRQ-ON-W} -> {IN-SOFTIRQ-W} | |
3598 | * usage". | |
dc186ad7 | 3599 | */ |
4cb1ef64 TH |
3600 | INIT_WORK_ONSTACK_KEY(&barr->work, wq_barrier_func, |
3601 | (pwq->wq->flags & WQ_BH) ? &bh_key : &thr_key); | |
22df02bb | 3602 | __set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(&barr->work)); |
52fa5bc5 | 3603 | |
fd1a5b04 BP |
3604 | init_completion_map(&barr->done, &target->lockdep_map); |
3605 | ||
2607d7a6 | 3606 | barr->task = current; |
83c22520 | 3607 | |
5797b1c1 | 3608 | /* The barrier work item does not participate in nr_active. */ |
018f3a13 LJ |
3609 | work_flags |= WORK_STRUCT_INACTIVE; |
3610 | ||
affee4b2 TH |
3611 | /* |
3612 | * If @target is currently being executed, schedule the | |
3613 | * barrier to the worker; otherwise, put it after @target. | |
3614 | */ | |
d812796e | 3615 | if (worker) { |
affee4b2 | 3616 | head = worker->scheduled.next; |
d812796e LJ |
3617 | work_color = worker->current_color; |
3618 | } else { | |
affee4b2 TH |
3619 | unsigned long *bits = work_data_bits(target); |
3620 | ||
3621 | head = target->entry.next; | |
3622 | /* there can already be other linked works, inherit and set */ | |
d21cece0 | 3623 | work_flags |= *bits & WORK_STRUCT_LINKED; |
d812796e | 3624 | work_color = get_work_color(*bits); |
affee4b2 TH |
3625 | __set_bit(WORK_STRUCT_LINKED_BIT, bits); |
3626 | } | |
3627 | ||
d812796e LJ |
3628 | pwq->nr_in_flight[work_color]++; |
3629 | work_flags |= work_color_to_flags(work_color); | |
3630 | ||
d21cece0 | 3631 | insert_work(pwq, &barr->work, head, work_flags); |
fc2e4d70 ON |
3632 | } |
3633 | ||
73f53c4a | 3634 | /** |
112202d9 | 3635 | * flush_workqueue_prep_pwqs - prepare pwqs for workqueue flushing |
73f53c4a TH |
3636 | * @wq: workqueue being flushed |
3637 | * @flush_color: new flush color, < 0 for no-op | |
3638 | * @work_color: new work color, < 0 for no-op | |
3639 | * | |
112202d9 | 3640 | * Prepare pwqs for workqueue flushing. |
73f53c4a | 3641 | * |
112202d9 TH |
3642 | * If @flush_color is non-negative, flush_color on all pwqs should be |
3643 | * -1. If no pwq has in-flight commands at the specified color, all | |
3644 | * pwq->flush_color's stay at -1 and %false is returned. If any pwq | |
3645 | * has in flight commands, its pwq->flush_color is set to | |
3646 | * @flush_color, @wq->nr_pwqs_to_flush is updated accordingly, pwq | |
73f53c4a TH |
3647 | * wakeup logic is armed and %true is returned. |
3648 | * | |
3649 | * The caller should have initialized @wq->first_flusher prior to | |
3650 | * calling this function with non-negative @flush_color. If | |
3651 | * @flush_color is negative, no flush color update is done and %false | |
3652 | * is returned. | |
3653 | * | |
112202d9 | 3654 | * If @work_color is non-negative, all pwqs should have the same |
73f53c4a TH |
3655 | * work_color which is previous to @work_color and all will be |
3656 | * advanced to @work_color. | |
3657 | * | |
3658 | * CONTEXT: | |
3c25a55d | 3659 | * mutex_lock(wq->mutex). |
73f53c4a | 3660 | * |
d185af30 | 3661 | * Return: |
73f53c4a TH |
3662 | * %true if @flush_color >= 0 and there's something to flush. %false |
3663 | * otherwise. | |
3664 | */ | |
112202d9 | 3665 | static bool flush_workqueue_prep_pwqs(struct workqueue_struct *wq, |
73f53c4a | 3666 | int flush_color, int work_color) |
1da177e4 | 3667 | { |
73f53c4a | 3668 | bool wait = false; |
49e3cf44 | 3669 | struct pool_workqueue *pwq; |
1da177e4 | 3670 | |
73f53c4a | 3671 | if (flush_color >= 0) { |
6183c009 | 3672 | WARN_ON_ONCE(atomic_read(&wq->nr_pwqs_to_flush)); |
112202d9 | 3673 | atomic_set(&wq->nr_pwqs_to_flush, 1); |
1da177e4 | 3674 | } |
2355b70f | 3675 | |
49e3cf44 | 3676 | for_each_pwq(pwq, wq) { |
112202d9 | 3677 | struct worker_pool *pool = pwq->pool; |
fc2e4d70 | 3678 | |
a9b8a985 | 3679 | raw_spin_lock_irq(&pool->lock); |
83c22520 | 3680 | |
73f53c4a | 3681 | if (flush_color >= 0) { |
6183c009 | 3682 | WARN_ON_ONCE(pwq->flush_color != -1); |
fc2e4d70 | 3683 | |
112202d9 TH |
3684 | if (pwq->nr_in_flight[flush_color]) { |
3685 | pwq->flush_color = flush_color; | |
3686 | atomic_inc(&wq->nr_pwqs_to_flush); | |
73f53c4a TH |
3687 | wait = true; |
3688 | } | |
3689 | } | |
1da177e4 | 3690 | |
73f53c4a | 3691 | if (work_color >= 0) { |
6183c009 | 3692 | WARN_ON_ONCE(work_color != work_next_color(pwq->work_color)); |
112202d9 | 3693 | pwq->work_color = work_color; |
73f53c4a | 3694 | } |
1da177e4 | 3695 | |
a9b8a985 | 3696 | raw_spin_unlock_irq(&pool->lock); |
1da177e4 | 3697 | } |
2355b70f | 3698 | |
112202d9 | 3699 | if (flush_color >= 0 && atomic_dec_and_test(&wq->nr_pwqs_to_flush)) |
73f53c4a | 3700 | complete(&wq->first_flusher->done); |
14441960 | 3701 | |
73f53c4a | 3702 | return wait; |
1da177e4 LT |
3703 | } |
3704 | ||
c35aea39 TH |
3705 | static void touch_wq_lockdep_map(struct workqueue_struct *wq) |
3706 | { | |
4cb1ef64 TH |
3707 | #ifdef CONFIG_LOCKDEP |
3708 | if (wq->flags & WQ_BH) | |
3709 | local_bh_disable(); | |
3710 | ||
c35aea39 TH |
3711 | lock_map_acquire(&wq->lockdep_map); |
3712 | lock_map_release(&wq->lockdep_map); | |
4cb1ef64 TH |
3713 | |
3714 | if (wq->flags & WQ_BH) | |
3715 | local_bh_enable(); | |
3716 | #endif | |
c35aea39 TH |
3717 | } |
3718 | ||
3719 | static void touch_work_lockdep_map(struct work_struct *work, | |
3720 | struct workqueue_struct *wq) | |
3721 | { | |
4cb1ef64 TH |
3722 | #ifdef CONFIG_LOCKDEP |
3723 | if (wq->flags & WQ_BH) | |
3724 | local_bh_disable(); | |
3725 | ||
c35aea39 TH |
3726 | lock_map_acquire(&work->lockdep_map); |
3727 | lock_map_release(&work->lockdep_map); | |
4cb1ef64 TH |
3728 | |
3729 | if (wq->flags & WQ_BH) | |
3730 | local_bh_enable(); | |
3731 | #endif | |
c35aea39 TH |
3732 | } |
3733 | ||
0fcb78c2 | 3734 | /** |
c4f135d6 | 3735 | * __flush_workqueue - ensure that any scheduled work has run to completion. |
0fcb78c2 | 3736 | * @wq: workqueue to flush |
1da177e4 | 3737 | * |
c5aa87bb TH |
3738 | * This function sleeps until all work items which were queued on entry |
3739 | * have finished execution, but it is not livelocked by new incoming ones. | |
1da177e4 | 3740 | */ |
c4f135d6 | 3741 | void __flush_workqueue(struct workqueue_struct *wq) |
1da177e4 | 3742 | { |
73f53c4a TH |
3743 | struct wq_flusher this_flusher = { |
3744 | .list = LIST_HEAD_INIT(this_flusher.list), | |
3745 | .flush_color = -1, | |
fd1a5b04 | 3746 | .done = COMPLETION_INITIALIZER_ONSTACK_MAP(this_flusher.done, wq->lockdep_map), |
73f53c4a TH |
3747 | }; |
3748 | int next_color; | |
1da177e4 | 3749 | |
3347fa09 TH |
3750 | if (WARN_ON(!wq_online)) |
3751 | return; | |
3752 | ||
c35aea39 | 3753 | touch_wq_lockdep_map(wq); |
87915adc | 3754 | |
3c25a55d | 3755 | mutex_lock(&wq->mutex); |
73f53c4a TH |
3756 | |
3757 | /* | |
3758 | * Start-to-wait phase | |
3759 | */ | |
3760 | next_color = work_next_color(wq->work_color); | |
3761 | ||
3762 | if (next_color != wq->flush_color) { | |
3763 | /* | |
3764 | * Color space is not full. The current work_color | |
3765 | * becomes our flush_color and work_color is advanced | |
3766 | * by one. | |
3767 | */ | |
6183c009 | 3768 | WARN_ON_ONCE(!list_empty(&wq->flusher_overflow)); |
73f53c4a TH |
3769 | this_flusher.flush_color = wq->work_color; |
3770 | wq->work_color = next_color; | |
3771 | ||
3772 | if (!wq->first_flusher) { | |
3773 | /* no flush in progress, become the first flusher */ | |
6183c009 | 3774 | WARN_ON_ONCE(wq->flush_color != this_flusher.flush_color); |
73f53c4a TH |
3775 | |
3776 | wq->first_flusher = &this_flusher; | |
3777 | ||
112202d9 | 3778 | if (!flush_workqueue_prep_pwqs(wq, wq->flush_color, |
73f53c4a TH |
3779 | wq->work_color)) { |
3780 | /* nothing to flush, done */ | |
3781 | wq->flush_color = next_color; | |
3782 | wq->first_flusher = NULL; | |
3783 | goto out_unlock; | |
3784 | } | |
3785 | } else { | |
3786 | /* wait in queue */ | |
6183c009 | 3787 | WARN_ON_ONCE(wq->flush_color == this_flusher.flush_color); |
73f53c4a | 3788 | list_add_tail(&this_flusher.list, &wq->flusher_queue); |
112202d9 | 3789 | flush_workqueue_prep_pwqs(wq, -1, wq->work_color); |
73f53c4a TH |
3790 | } |
3791 | } else { | |
3792 | /* | |
3793 | * Oops, color space is full, wait on overflow queue. | |
3794 | * The next flush completion will assign us | |
3795 | * flush_color and transfer to flusher_queue. | |
3796 | */ | |
3797 | list_add_tail(&this_flusher.list, &wq->flusher_overflow); | |
3798 | } | |
3799 | ||
fca839c0 TH |
3800 | check_flush_dependency(wq, NULL); |
3801 | ||
3c25a55d | 3802 | mutex_unlock(&wq->mutex); |
73f53c4a TH |
3803 | |
3804 | wait_for_completion(&this_flusher.done); | |
3805 | ||
3806 | /* | |
3807 | * Wake-up-and-cascade phase | |
3808 | * | |
3809 | * First flushers are responsible for cascading flushes and | |
3810 | * handling overflow. Non-first flushers can simply return. | |
3811 | */ | |
00d5d15b | 3812 | if (READ_ONCE(wq->first_flusher) != &this_flusher) |
73f53c4a TH |
3813 | return; |
3814 | ||
3c25a55d | 3815 | mutex_lock(&wq->mutex); |
73f53c4a | 3816 | |
4ce48b37 TH |
3817 | /* we might have raced, check again with mutex held */ |
3818 | if (wq->first_flusher != &this_flusher) | |
3819 | goto out_unlock; | |
3820 | ||
00d5d15b | 3821 | WRITE_ONCE(wq->first_flusher, NULL); |
73f53c4a | 3822 | |
6183c009 TH |
3823 | WARN_ON_ONCE(!list_empty(&this_flusher.list)); |
3824 | WARN_ON_ONCE(wq->flush_color != this_flusher.flush_color); | |
73f53c4a TH |
3825 | |
3826 | while (true) { | |
3827 | struct wq_flusher *next, *tmp; | |
3828 | ||
3829 | /* complete all the flushers sharing the current flush color */ | |
3830 | list_for_each_entry_safe(next, tmp, &wq->flusher_queue, list) { | |
3831 | if (next->flush_color != wq->flush_color) | |
3832 | break; | |
3833 | list_del_init(&next->list); | |
3834 | complete(&next->done); | |
3835 | } | |
3836 | ||
6183c009 TH |
3837 | WARN_ON_ONCE(!list_empty(&wq->flusher_overflow) && |
3838 | wq->flush_color != work_next_color(wq->work_color)); | |
73f53c4a TH |
3839 | |
3840 | /* this flush_color is finished, advance by one */ | |
3841 | wq->flush_color = work_next_color(wq->flush_color); | |
3842 | ||
3843 | /* one color has been freed, handle overflow queue */ | |
3844 | if (!list_empty(&wq->flusher_overflow)) { | |
3845 | /* | |
3846 | * Assign the same color to all overflowed | |
3847 | * flushers, advance work_color and append to | |
3848 | * flusher_queue. This is the start-to-wait | |
3849 | * phase for these overflowed flushers. | |
3850 | */ | |
3851 | list_for_each_entry(tmp, &wq->flusher_overflow, list) | |
3852 | tmp->flush_color = wq->work_color; | |
3853 | ||
3854 | wq->work_color = work_next_color(wq->work_color); | |
3855 | ||
3856 | list_splice_tail_init(&wq->flusher_overflow, | |
3857 | &wq->flusher_queue); | |
112202d9 | 3858 | flush_workqueue_prep_pwqs(wq, -1, wq->work_color); |
73f53c4a TH |
3859 | } |
3860 | ||
3861 | if (list_empty(&wq->flusher_queue)) { | |
6183c009 | 3862 | WARN_ON_ONCE(wq->flush_color != wq->work_color); |
73f53c4a TH |
3863 | break; |
3864 | } | |
3865 | ||
3866 | /* | |
3867 | * Need to flush more colors. Make the next flusher | |
112202d9 | 3868 | * the new first flusher and arm pwqs. |
73f53c4a | 3869 | */ |
6183c009 TH |
3870 | WARN_ON_ONCE(wq->flush_color == wq->work_color); |
3871 | WARN_ON_ONCE(wq->flush_color != next->flush_color); | |
73f53c4a TH |
3872 | |
3873 | list_del_init(&next->list); | |
3874 | wq->first_flusher = next; | |
3875 | ||
112202d9 | 3876 | if (flush_workqueue_prep_pwqs(wq, wq->flush_color, -1)) |
73f53c4a TH |
3877 | break; |
3878 | ||
3879 | /* | |
3880 | * Meh... this color is already done, clear first | |
3881 | * flusher and repeat cascading. | |
3882 | */ | |
3883 | wq->first_flusher = NULL; | |
3884 | } | |
3885 | ||
3886 | out_unlock: | |
3c25a55d | 3887 | mutex_unlock(&wq->mutex); |
1da177e4 | 3888 | } |
c4f135d6 | 3889 | EXPORT_SYMBOL(__flush_workqueue); |
1da177e4 | 3890 | |
9c5a2ba7 TH |
3891 | /** |
3892 | * drain_workqueue - drain a workqueue | |
3893 | * @wq: workqueue to drain | |
3894 | * | |
3895 | * Wait until the workqueue becomes empty. While draining is in progress, | |
3896 | * only chain queueing is allowed. IOW, only currently pending or running | |
3897 | * work items on @wq can queue further work items on it. @wq is flushed | |
b749b1b6 | 3898 | * repeatedly until it becomes empty. The number of flushing is determined |
9c5a2ba7 TH |
3899 | * by the depth of chaining and should be relatively short. Whine if it |
3900 | * takes too long. | |
3901 | */ | |
3902 | void drain_workqueue(struct workqueue_struct *wq) | |
3903 | { | |
3904 | unsigned int flush_cnt = 0; | |
49e3cf44 | 3905 | struct pool_workqueue *pwq; |
9c5a2ba7 TH |
3906 | |
3907 | /* | |
3908 | * __queue_work() needs to test whether there are drainers, is much | |
3909 | * hotter than drain_workqueue() and already looks at @wq->flags. | |
618b01eb | 3910 | * Use __WQ_DRAINING so that queue doesn't have to check nr_drainers. |
9c5a2ba7 | 3911 | */ |
87fc741e | 3912 | mutex_lock(&wq->mutex); |
9c5a2ba7 | 3913 | if (!wq->nr_drainers++) |
618b01eb | 3914 | wq->flags |= __WQ_DRAINING; |
87fc741e | 3915 | mutex_unlock(&wq->mutex); |
9c5a2ba7 | 3916 | reflush: |
c4f135d6 | 3917 | __flush_workqueue(wq); |
9c5a2ba7 | 3918 | |
b09f4fd3 | 3919 | mutex_lock(&wq->mutex); |
76af4d93 | 3920 | |
49e3cf44 | 3921 | for_each_pwq(pwq, wq) { |
fa2563e4 | 3922 | bool drained; |
9c5a2ba7 | 3923 | |
a9b8a985 | 3924 | raw_spin_lock_irq(&pwq->pool->lock); |
afa87ce8 | 3925 | drained = pwq_is_empty(pwq); |
a9b8a985 | 3926 | raw_spin_unlock_irq(&pwq->pool->lock); |
fa2563e4 TT |
3927 | |
3928 | if (drained) | |
9c5a2ba7 TH |
3929 | continue; |
3930 | ||
3931 | if (++flush_cnt == 10 || | |
3932 | (flush_cnt % 100 == 0 && flush_cnt <= 1000)) | |
e9ad2eb3 SZ |
3933 | pr_warn("workqueue %s: %s() isn't complete after %u tries\n", |
3934 | wq->name, __func__, flush_cnt); | |
76af4d93 | 3935 | |
b09f4fd3 | 3936 | mutex_unlock(&wq->mutex); |
9c5a2ba7 TH |
3937 | goto reflush; |
3938 | } | |
3939 | ||
9c5a2ba7 | 3940 | if (!--wq->nr_drainers) |
618b01eb | 3941 | wq->flags &= ~__WQ_DRAINING; |
87fc741e | 3942 | mutex_unlock(&wq->mutex); |
9c5a2ba7 TH |
3943 | } |
3944 | EXPORT_SYMBOL_GPL(drain_workqueue); | |
3945 | ||
d6e89786 JB |
3946 | static bool start_flush_work(struct work_struct *work, struct wq_barrier *barr, |
3947 | bool from_cancel) | |
db700897 | 3948 | { |
affee4b2 | 3949 | struct worker *worker = NULL; |
c9e7cf27 | 3950 | struct worker_pool *pool; |
112202d9 | 3951 | struct pool_workqueue *pwq; |
c35aea39 | 3952 | struct workqueue_struct *wq; |
db700897 ON |
3953 | |
3954 | might_sleep(); | |
fa1b54e6 | 3955 | |
24acfb71 | 3956 | rcu_read_lock(); |
c9e7cf27 | 3957 | pool = get_work_pool(work); |
fa1b54e6 | 3958 | if (!pool) { |
24acfb71 | 3959 | rcu_read_unlock(); |
baf59022 | 3960 | return false; |
fa1b54e6 | 3961 | } |
db700897 | 3962 | |
a9b8a985 | 3963 | raw_spin_lock_irq(&pool->lock); |
0b3dae68 | 3964 | /* see the comment in try_to_grab_pending() with the same code */ |
112202d9 TH |
3965 | pwq = get_work_pwq(work); |
3966 | if (pwq) { | |
3967 | if (unlikely(pwq->pool != pool)) | |
4690c4ab | 3968 | goto already_gone; |
606a5020 | 3969 | } else { |
c9e7cf27 | 3970 | worker = find_worker_executing_work(pool, work); |
affee4b2 | 3971 | if (!worker) |
4690c4ab | 3972 | goto already_gone; |
112202d9 | 3973 | pwq = worker->current_pwq; |
606a5020 | 3974 | } |
db700897 | 3975 | |
c35aea39 TH |
3976 | wq = pwq->wq; |
3977 | check_flush_dependency(wq, work); | |
fca839c0 | 3978 | |
112202d9 | 3979 | insert_wq_barrier(pwq, barr, work, worker); |
a9b8a985 | 3980 | raw_spin_unlock_irq(&pool->lock); |
7a22ad75 | 3981 | |
c35aea39 TH |
3982 | touch_work_lockdep_map(work, wq); |
3983 | ||
e159489b | 3984 | /* |
a1d14934 PZ |
3985 | * Force a lock recursion deadlock when using flush_work() inside a |
3986 | * single-threaded or rescuer equipped workqueue. | |
3987 | * | |
3988 | * For single threaded workqueues the deadlock happens when the work | |
3989 | * is after the work issuing the flush_work(). For rescuer equipped | |
3990 | * workqueues the deadlock happens when the rescuer stalls, blocking | |
3991 | * forward progress. | |
e159489b | 3992 | */ |
c35aea39 TH |
3993 | if (!from_cancel && (wq->saved_max_active == 1 || wq->rescuer)) |
3994 | touch_wq_lockdep_map(wq); | |
3995 | ||
24acfb71 | 3996 | rcu_read_unlock(); |
401a8d04 | 3997 | return true; |
4690c4ab | 3998 | already_gone: |
a9b8a985 | 3999 | raw_spin_unlock_irq(&pool->lock); |
24acfb71 | 4000 | rcu_read_unlock(); |
401a8d04 | 4001 | return false; |
db700897 | 4002 | } |
baf59022 | 4003 | |
d6e89786 JB |
4004 | static bool __flush_work(struct work_struct *work, bool from_cancel) |
4005 | { | |
4006 | struct wq_barrier barr; | |
4007 | ||
4008 | if (WARN_ON(!wq_online)) | |
4009 | return false; | |
4010 | ||
4d43d395 TH |
4011 | if (WARN_ON(!work->func)) |
4012 | return false; | |
4013 | ||
d6e89786 JB |
4014 | if (start_flush_work(work, &barr, from_cancel)) { |
4015 | wait_for_completion(&barr.done); | |
4016 | destroy_work_on_stack(&barr.work); | |
4017 | return true; | |
4018 | } else { | |
4019 | return false; | |
4020 | } | |
4021 | } | |
4022 | ||
baf59022 TH |
4023 | /** |
4024 | * flush_work - wait for a work to finish executing the last queueing instance | |
4025 | * @work: the work to flush | |
4026 | * | |
606a5020 TH |
4027 | * Wait until @work has finished execution. @work is guaranteed to be idle |
4028 | * on return if it hasn't been requeued since flush started. | |
baf59022 | 4029 | * |
d185af30 | 4030 | * Return: |
baf59022 TH |
4031 | * %true if flush_work() waited for the work to finish execution, |
4032 | * %false if it was already idle. | |
4033 | */ | |
4034 | bool flush_work(struct work_struct *work) | |
4035 | { | |
d6e89786 | 4036 | return __flush_work(work, false); |
6e84d644 | 4037 | } |
606a5020 | 4038 | EXPORT_SYMBOL_GPL(flush_work); |
6e84d644 | 4039 | |
8603e1b3 | 4040 | struct cwt_wait { |
ac6424b9 | 4041 | wait_queue_entry_t wait; |
8603e1b3 TH |
4042 | struct work_struct *work; |
4043 | }; | |
4044 | ||
ac6424b9 | 4045 | static int cwt_wakefn(wait_queue_entry_t *wait, unsigned mode, int sync, void *key) |
8603e1b3 TH |
4046 | { |
4047 | struct cwt_wait *cwait = container_of(wait, struct cwt_wait, wait); | |
4048 | ||
4049 | if (cwait->work != key) | |
4050 | return 0; | |
4051 | return autoremove_wake_function(wait, mode, sync, key); | |
4052 | } | |
4053 | ||
36e227d2 | 4054 | static bool __cancel_work_timer(struct work_struct *work, bool is_dwork) |
1f1f642e | 4055 | { |
8603e1b3 | 4056 | static DECLARE_WAIT_QUEUE_HEAD(cancel_waitq); |
bbb68dfa | 4057 | unsigned long flags; |
1f1f642e ON |
4058 | int ret; |
4059 | ||
4060 | do { | |
bbb68dfa TH |
4061 | ret = try_to_grab_pending(work, is_dwork, &flags); |
4062 | /* | |
8603e1b3 TH |
4063 | * If someone else is already canceling, wait for it to |
4064 | * finish. flush_work() doesn't work for PREEMPT_NONE | |
4065 | * because we may get scheduled between @work's completion | |
4066 | * and the other canceling task resuming and clearing | |
4067 | * CANCELING - flush_work() will return false immediately | |
4068 | * as @work is no longer busy, try_to_grab_pending() will | |
4069 | * return -ENOENT as @work is still being canceled and the | |
4070 | * other canceling task won't be able to clear CANCELING as | |
4071 | * we're hogging the CPU. | |
4072 | * | |
4073 | * Let's wait for completion using a waitqueue. As this | |
4074 | * may lead to the thundering herd problem, use a custom | |
4075 | * wake function which matches @work along with exclusive | |
4076 | * wait and wakeup. | |
bbb68dfa | 4077 | */ |
8603e1b3 TH |
4078 | if (unlikely(ret == -ENOENT)) { |
4079 | struct cwt_wait cwait; | |
4080 | ||
4081 | init_wait(&cwait.wait); | |
4082 | cwait.wait.func = cwt_wakefn; | |
4083 | cwait.work = work; | |
4084 | ||
4085 | prepare_to_wait_exclusive(&cancel_waitq, &cwait.wait, | |
4086 | TASK_UNINTERRUPTIBLE); | |
4087 | if (work_is_canceling(work)) | |
4088 | schedule(); | |
4089 | finish_wait(&cancel_waitq, &cwait.wait); | |
4090 | } | |
1f1f642e ON |
4091 | } while (unlikely(ret < 0)); |
4092 | ||
bbb68dfa TH |
4093 | /* tell other tasks trying to grab @work to back off */ |
4094 | mark_work_canceling(work); | |
4095 | local_irq_restore(flags); | |
4096 | ||
3347fa09 TH |
4097 | /* |
4098 | * This allows canceling during early boot. We know that @work | |
4099 | * isn't executing. | |
4100 | */ | |
4101 | if (wq_online) | |
d6e89786 | 4102 | __flush_work(work, true); |
3347fa09 | 4103 | |
7a22ad75 | 4104 | clear_work_data(work); |
8603e1b3 TH |
4105 | |
4106 | /* | |
4107 | * Paired with prepare_to_wait() above so that either | |
4108 | * waitqueue_active() is visible here or !work_is_canceling() is | |
4109 | * visible there. | |
4110 | */ | |
4111 | smp_mb(); | |
4112 | if (waitqueue_active(&cancel_waitq)) | |
4113 | __wake_up(&cancel_waitq, TASK_NORMAL, 1, work); | |
4114 | ||
1f1f642e ON |
4115 | return ret; |
4116 | } | |
4117 | ||
6e84d644 | 4118 | /** |
401a8d04 TH |
4119 | * cancel_work_sync - cancel a work and wait for it to finish |
4120 | * @work: the work to cancel | |
6e84d644 | 4121 | * |
401a8d04 TH |
4122 | * Cancel @work and wait for its execution to finish. This function |
4123 | * can be used even if the work re-queues itself or migrates to | |
4124 | * another workqueue. On return from this function, @work is | |
4125 | * guaranteed to be not pending or executing on any CPU. | |
1f1f642e | 4126 | * |
401a8d04 TH |
4127 | * cancel_work_sync(&delayed_work->work) must not be used for |
4128 | * delayed_work's. Use cancel_delayed_work_sync() instead. | |
6e84d644 | 4129 | * |
401a8d04 | 4130 | * The caller must ensure that the workqueue on which @work was last |
6e84d644 | 4131 | * queued can't be destroyed before this function returns. |
401a8d04 | 4132 | * |
d185af30 | 4133 | * Return: |
401a8d04 | 4134 | * %true if @work was pending, %false otherwise. |
6e84d644 | 4135 | */ |
401a8d04 | 4136 | bool cancel_work_sync(struct work_struct *work) |
6e84d644 | 4137 | { |
36e227d2 | 4138 | return __cancel_work_timer(work, false); |
b89deed3 | 4139 | } |
28e53bdd | 4140 | EXPORT_SYMBOL_GPL(cancel_work_sync); |
b89deed3 | 4141 | |
6e84d644 | 4142 | /** |
401a8d04 TH |
4143 | * flush_delayed_work - wait for a dwork to finish executing the last queueing |
4144 | * @dwork: the delayed work to flush | |
6e84d644 | 4145 | * |
401a8d04 TH |
4146 | * Delayed timer is cancelled and the pending work is queued for |
4147 | * immediate execution. Like flush_work(), this function only | |
4148 | * considers the last queueing instance of @dwork. | |
1f1f642e | 4149 | * |
d185af30 | 4150 | * Return: |
401a8d04 TH |
4151 | * %true if flush_work() waited for the work to finish execution, |
4152 | * %false if it was already idle. | |
6e84d644 | 4153 | */ |
401a8d04 TH |
4154 | bool flush_delayed_work(struct delayed_work *dwork) |
4155 | { | |
8930caba | 4156 | local_irq_disable(); |
401a8d04 | 4157 | if (del_timer_sync(&dwork->timer)) |
60c057bc | 4158 | __queue_work(dwork->cpu, dwork->wq, &dwork->work); |
8930caba | 4159 | local_irq_enable(); |
401a8d04 TH |
4160 | return flush_work(&dwork->work); |
4161 | } | |
4162 | EXPORT_SYMBOL(flush_delayed_work); | |
4163 | ||
05f0fe6b TH |
4164 | /** |
4165 | * flush_rcu_work - wait for a rwork to finish executing the last queueing | |
4166 | * @rwork: the rcu work to flush | |
4167 | * | |
4168 | * Return: | |
4169 | * %true if flush_rcu_work() waited for the work to finish execution, | |
4170 | * %false if it was already idle. | |
4171 | */ | |
4172 | bool flush_rcu_work(struct rcu_work *rwork) | |
4173 | { | |
4174 | if (test_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(&rwork->work))) { | |
4175 | rcu_barrier(); | |
4176 | flush_work(&rwork->work); | |
4177 | return true; | |
4178 | } else { | |
4179 | return flush_work(&rwork->work); | |
4180 | } | |
4181 | } | |
4182 | EXPORT_SYMBOL(flush_rcu_work); | |
4183 | ||
f72b8792 JA |
4184 | static bool __cancel_work(struct work_struct *work, bool is_dwork) |
4185 | { | |
4186 | unsigned long flags; | |
4187 | int ret; | |
4188 | ||
4189 | do { | |
4190 | ret = try_to_grab_pending(work, is_dwork, &flags); | |
4191 | } while (unlikely(ret == -EAGAIN)); | |
4192 | ||
4193 | if (unlikely(ret < 0)) | |
4194 | return false; | |
4195 | ||
4196 | set_work_pool_and_clear_pending(work, get_work_pool_id(work)); | |
4197 | local_irq_restore(flags); | |
4198 | return ret; | |
4199 | } | |
4200 | ||
73b4b532 AG |
4201 | /* |
4202 | * See cancel_delayed_work() | |
4203 | */ | |
4204 | bool cancel_work(struct work_struct *work) | |
4205 | { | |
4206 | return __cancel_work(work, false); | |
4207 | } | |
4208 | EXPORT_SYMBOL(cancel_work); | |
4209 | ||
09383498 | 4210 | /** |
57b30ae7 TH |
4211 | * cancel_delayed_work - cancel a delayed work |
4212 | * @dwork: delayed_work to cancel | |
09383498 | 4213 | * |
d185af30 YB |
4214 | * Kill off a pending delayed_work. |
4215 | * | |
4216 | * Return: %true if @dwork was pending and canceled; %false if it wasn't | |
4217 | * pending. | |
4218 | * | |
4219 | * Note: | |
4220 | * The work callback function may still be running on return, unless | |
4221 | * it returns %true and the work doesn't re-arm itself. Explicitly flush or | |
4222 | * use cancel_delayed_work_sync() to wait on it. | |
09383498 | 4223 | * |
57b30ae7 | 4224 | * This function is safe to call from any context including IRQ handler. |
09383498 | 4225 | */ |
57b30ae7 | 4226 | bool cancel_delayed_work(struct delayed_work *dwork) |
09383498 | 4227 | { |
f72b8792 | 4228 | return __cancel_work(&dwork->work, true); |
09383498 | 4229 | } |
57b30ae7 | 4230 | EXPORT_SYMBOL(cancel_delayed_work); |
09383498 | 4231 | |
401a8d04 TH |
4232 | /** |
4233 | * cancel_delayed_work_sync - cancel a delayed work and wait for it to finish | |
4234 | * @dwork: the delayed work cancel | |
4235 | * | |
4236 | * This is cancel_work_sync() for delayed works. | |
4237 | * | |
d185af30 | 4238 | * Return: |
401a8d04 TH |
4239 | * %true if @dwork was pending, %false otherwise. |
4240 | */ | |
4241 | bool cancel_delayed_work_sync(struct delayed_work *dwork) | |
6e84d644 | 4242 | { |
36e227d2 | 4243 | return __cancel_work_timer(&dwork->work, true); |
6e84d644 | 4244 | } |
f5a421a4 | 4245 | EXPORT_SYMBOL(cancel_delayed_work_sync); |
1da177e4 | 4246 | |
b6136773 | 4247 | /** |
31ddd871 | 4248 | * schedule_on_each_cpu - execute a function synchronously on each online CPU |
b6136773 | 4249 | * @func: the function to call |
b6136773 | 4250 | * |
31ddd871 TH |
4251 | * schedule_on_each_cpu() executes @func on each online CPU using the |
4252 | * system workqueue and blocks until all CPUs have completed. | |
b6136773 | 4253 | * schedule_on_each_cpu() is very slow. |
31ddd871 | 4254 | * |
d185af30 | 4255 | * Return: |
31ddd871 | 4256 | * 0 on success, -errno on failure. |
b6136773 | 4257 | */ |
65f27f38 | 4258 | int schedule_on_each_cpu(work_func_t func) |
15316ba8 CL |
4259 | { |
4260 | int cpu; | |
38f51568 | 4261 | struct work_struct __percpu *works; |
15316ba8 | 4262 | |
b6136773 AM |
4263 | works = alloc_percpu(struct work_struct); |
4264 | if (!works) | |
15316ba8 | 4265 | return -ENOMEM; |
b6136773 | 4266 | |
ffd8bea8 | 4267 | cpus_read_lock(); |
93981800 | 4268 | |
15316ba8 | 4269 | for_each_online_cpu(cpu) { |
9bfb1839 IM |
4270 | struct work_struct *work = per_cpu_ptr(works, cpu); |
4271 | ||
4272 | INIT_WORK(work, func); | |
b71ab8c2 | 4273 | schedule_work_on(cpu, work); |
65a64464 | 4274 | } |
93981800 TH |
4275 | |
4276 | for_each_online_cpu(cpu) | |
4277 | flush_work(per_cpu_ptr(works, cpu)); | |
4278 | ||
ffd8bea8 | 4279 | cpus_read_unlock(); |
b6136773 | 4280 | free_percpu(works); |
15316ba8 CL |
4281 | return 0; |
4282 | } | |
4283 | ||
1fa44eca JB |
4284 | /** |
4285 | * execute_in_process_context - reliably execute the routine with user context | |
4286 | * @fn: the function to execute | |
1fa44eca JB |
4287 | * @ew: guaranteed storage for the execute work structure (must |
4288 | * be available when the work executes) | |
4289 | * | |
4290 | * Executes the function immediately if process context is available, | |
4291 | * otherwise schedules the function for delayed execution. | |
4292 | * | |
d185af30 | 4293 | * Return: 0 - function was executed |
1fa44eca JB |
4294 | * 1 - function was scheduled for execution |
4295 | */ | |
65f27f38 | 4296 | int execute_in_process_context(work_func_t fn, struct execute_work *ew) |
1fa44eca JB |
4297 | { |
4298 | if (!in_interrupt()) { | |
65f27f38 | 4299 | fn(&ew->work); |
1fa44eca JB |
4300 | return 0; |
4301 | } | |
4302 | ||
65f27f38 | 4303 | INIT_WORK(&ew->work, fn); |
1fa44eca JB |
4304 | schedule_work(&ew->work); |
4305 | ||
4306 | return 1; | |
4307 | } | |
4308 | EXPORT_SYMBOL_GPL(execute_in_process_context); | |
4309 | ||
6ba94429 FW |
4310 | /** |
4311 | * free_workqueue_attrs - free a workqueue_attrs | |
4312 | * @attrs: workqueue_attrs to free | |
226223ab | 4313 | * |
6ba94429 | 4314 | * Undo alloc_workqueue_attrs(). |
226223ab | 4315 | */ |
513c98d0 | 4316 | void free_workqueue_attrs(struct workqueue_attrs *attrs) |
226223ab | 4317 | { |
6ba94429 FW |
4318 | if (attrs) { |
4319 | free_cpumask_var(attrs->cpumask); | |
9546b29e | 4320 | free_cpumask_var(attrs->__pod_cpumask); |
6ba94429 FW |
4321 | kfree(attrs); |
4322 | } | |
226223ab TH |
4323 | } |
4324 | ||
6ba94429 FW |
4325 | /** |
4326 | * alloc_workqueue_attrs - allocate a workqueue_attrs | |
6ba94429 FW |
4327 | * |
4328 | * Allocate a new workqueue_attrs, initialize with default settings and | |
4329 | * return it. | |
4330 | * | |
4331 | * Return: The allocated new workqueue_attr on success. %NULL on failure. | |
4332 | */ | |
513c98d0 | 4333 | struct workqueue_attrs *alloc_workqueue_attrs(void) |
226223ab | 4334 | { |
6ba94429 | 4335 | struct workqueue_attrs *attrs; |
226223ab | 4336 | |
be69d00d | 4337 | attrs = kzalloc(sizeof(*attrs), GFP_KERNEL); |
6ba94429 FW |
4338 | if (!attrs) |
4339 | goto fail; | |
be69d00d | 4340 | if (!alloc_cpumask_var(&attrs->cpumask, GFP_KERNEL)) |
6ba94429 | 4341 | goto fail; |
9546b29e TH |
4342 | if (!alloc_cpumask_var(&attrs->__pod_cpumask, GFP_KERNEL)) |
4343 | goto fail; | |
6ba94429 FW |
4344 | |
4345 | cpumask_copy(attrs->cpumask, cpu_possible_mask); | |
523a301e | 4346 | attrs->affn_scope = WQ_AFFN_DFL; |
6ba94429 FW |
4347 | return attrs; |
4348 | fail: | |
4349 | free_workqueue_attrs(attrs); | |
4350 | return NULL; | |
226223ab TH |
4351 | } |
4352 | ||
6ba94429 FW |
4353 | static void copy_workqueue_attrs(struct workqueue_attrs *to, |
4354 | const struct workqueue_attrs *from) | |
226223ab | 4355 | { |
6ba94429 FW |
4356 | to->nice = from->nice; |
4357 | cpumask_copy(to->cpumask, from->cpumask); | |
9546b29e | 4358 | cpumask_copy(to->__pod_cpumask, from->__pod_cpumask); |
8639eceb | 4359 | to->affn_strict = from->affn_strict; |
84193c07 | 4360 | |
6ba94429 | 4361 | /* |
84193c07 TH |
4362 | * Unlike hash and equality test, copying shouldn't ignore wq-only |
4363 | * fields as copying is used for both pool and wq attrs. Instead, | |
4364 | * get_unbound_pool() explicitly clears the fields. | |
6ba94429 | 4365 | */ |
84193c07 | 4366 | to->affn_scope = from->affn_scope; |
af73f5c9 | 4367 | to->ordered = from->ordered; |
226223ab TH |
4368 | } |
4369 | ||
5de7a03c TH |
4370 | /* |
4371 | * Some attrs fields are workqueue-only. Clear them for worker_pool's. See the | |
4372 | * comments in 'struct workqueue_attrs' definition. | |
4373 | */ | |
4374 | static void wqattrs_clear_for_pool(struct workqueue_attrs *attrs) | |
4375 | { | |
84193c07 | 4376 | attrs->affn_scope = WQ_AFFN_NR_TYPES; |
5de7a03c TH |
4377 | attrs->ordered = false; |
4378 | } | |
4379 | ||
6ba94429 FW |
4380 | /* hash value of the content of @attr */ |
4381 | static u32 wqattrs_hash(const struct workqueue_attrs *attrs) | |
226223ab | 4382 | { |
6ba94429 | 4383 | u32 hash = 0; |
226223ab | 4384 | |
6ba94429 FW |
4385 | hash = jhash_1word(attrs->nice, hash); |
4386 | hash = jhash(cpumask_bits(attrs->cpumask), | |
4387 | BITS_TO_LONGS(nr_cpumask_bits) * sizeof(long), hash); | |
9546b29e TH |
4388 | hash = jhash(cpumask_bits(attrs->__pod_cpumask), |
4389 | BITS_TO_LONGS(nr_cpumask_bits) * sizeof(long), hash); | |
8639eceb | 4390 | hash = jhash_1word(attrs->affn_strict, hash); |
6ba94429 | 4391 | return hash; |
226223ab | 4392 | } |
226223ab | 4393 | |
6ba94429 FW |
4394 | /* content equality test */ |
4395 | static bool wqattrs_equal(const struct workqueue_attrs *a, | |
4396 | const struct workqueue_attrs *b) | |
226223ab | 4397 | { |
6ba94429 FW |
4398 | if (a->nice != b->nice) |
4399 | return false; | |
4400 | if (!cpumask_equal(a->cpumask, b->cpumask)) | |
4401 | return false; | |
9546b29e TH |
4402 | if (!cpumask_equal(a->__pod_cpumask, b->__pod_cpumask)) |
4403 | return false; | |
8639eceb TH |
4404 | if (a->affn_strict != b->affn_strict) |
4405 | return false; | |
6ba94429 | 4406 | return true; |
226223ab TH |
4407 | } |
4408 | ||
0f36ee24 TH |
4409 | /* Update @attrs with actually available CPUs */ |
4410 | static void wqattrs_actualize_cpumask(struct workqueue_attrs *attrs, | |
4411 | const cpumask_t *unbound_cpumask) | |
4412 | { | |
4413 | /* | |
4414 | * Calculate the effective CPU mask of @attrs given @unbound_cpumask. If | |
4415 | * @attrs->cpumask doesn't overlap with @unbound_cpumask, we fallback to | |
4416 | * @unbound_cpumask. | |
4417 | */ | |
4418 | cpumask_and(attrs->cpumask, attrs->cpumask, unbound_cpumask); | |
4419 | if (unlikely(cpumask_empty(attrs->cpumask))) | |
4420 | cpumask_copy(attrs->cpumask, unbound_cpumask); | |
4421 | } | |
4422 | ||
84193c07 TH |
4423 | /* find wq_pod_type to use for @attrs */ |
4424 | static const struct wq_pod_type * | |
4425 | wqattrs_pod_type(const struct workqueue_attrs *attrs) | |
4426 | { | |
523a301e TH |
4427 | enum wq_affn_scope scope; |
4428 | struct wq_pod_type *pt; | |
4429 | ||
4430 | /* to synchronize access to wq_affn_dfl */ | |
4431 | lockdep_assert_held(&wq_pool_mutex); | |
4432 | ||
4433 | if (attrs->affn_scope == WQ_AFFN_DFL) | |
4434 | scope = wq_affn_dfl; | |
4435 | else | |
4436 | scope = attrs->affn_scope; | |
4437 | ||
4438 | pt = &wq_pod_types[scope]; | |
84193c07 TH |
4439 | |
4440 | if (!WARN_ON_ONCE(attrs->affn_scope == WQ_AFFN_NR_TYPES) && | |
4441 | likely(pt->nr_pods)) | |
4442 | return pt; | |
4443 | ||
4444 | /* | |
4445 | * Before workqueue_init_topology(), only SYSTEM is available which is | |
4446 | * initialized in workqueue_init_early(). | |
4447 | */ | |
4448 | pt = &wq_pod_types[WQ_AFFN_SYSTEM]; | |
4449 | BUG_ON(!pt->nr_pods); | |
4450 | return pt; | |
4451 | } | |
4452 | ||
6ba94429 FW |
4453 | /** |
4454 | * init_worker_pool - initialize a newly zalloc'd worker_pool | |
4455 | * @pool: worker_pool to initialize | |
4456 | * | |
402dd89d | 4457 | * Initialize a newly zalloc'd @pool. It also allocates @pool->attrs. |
6ba94429 FW |
4458 | * |
4459 | * Return: 0 on success, -errno on failure. Even on failure, all fields | |
4460 | * inside @pool proper are initialized and put_unbound_pool() can be called | |
4461 | * on @pool safely to release it. | |
4462 | */ | |
4463 | static int init_worker_pool(struct worker_pool *pool) | |
226223ab | 4464 | { |
a9b8a985 | 4465 | raw_spin_lock_init(&pool->lock); |
6ba94429 FW |
4466 | pool->id = -1; |
4467 | pool->cpu = -1; | |
4468 | pool->node = NUMA_NO_NODE; | |
4469 | pool->flags |= POOL_DISASSOCIATED; | |
82607adc | 4470 | pool->watchdog_ts = jiffies; |
6ba94429 FW |
4471 | INIT_LIST_HEAD(&pool->worklist); |
4472 | INIT_LIST_HEAD(&pool->idle_list); | |
4473 | hash_init(pool->busy_hash); | |
226223ab | 4474 | |
32a6c723 | 4475 | timer_setup(&pool->idle_timer, idle_worker_timeout, TIMER_DEFERRABLE); |
3f959aa3 | 4476 | INIT_WORK(&pool->idle_cull_work, idle_cull_fn); |
226223ab | 4477 | |
32a6c723 | 4478 | timer_setup(&pool->mayday_timer, pool_mayday_timeout, 0); |
226223ab | 4479 | |
6ba94429 | 4480 | INIT_LIST_HEAD(&pool->workers); |
e02b9312 | 4481 | INIT_LIST_HEAD(&pool->dying_workers); |
226223ab | 4482 | |
6ba94429 FW |
4483 | ida_init(&pool->worker_ida); |
4484 | INIT_HLIST_NODE(&pool->hash_node); | |
4485 | pool->refcnt = 1; | |
226223ab | 4486 | |
6ba94429 | 4487 | /* shouldn't fail above this point */ |
be69d00d | 4488 | pool->attrs = alloc_workqueue_attrs(); |
6ba94429 FW |
4489 | if (!pool->attrs) |
4490 | return -ENOMEM; | |
5de7a03c TH |
4491 | |
4492 | wqattrs_clear_for_pool(pool->attrs); | |
4493 | ||
6ba94429 | 4494 | return 0; |
226223ab TH |
4495 | } |
4496 | ||
669de8bd BVA |
4497 | #ifdef CONFIG_LOCKDEP |
4498 | static void wq_init_lockdep(struct workqueue_struct *wq) | |
4499 | { | |
4500 | char *lock_name; | |
4501 | ||
4502 | lockdep_register_key(&wq->key); | |
4503 | lock_name = kasprintf(GFP_KERNEL, "%s%s", "(wq_completion)", wq->name); | |
4504 | if (!lock_name) | |
4505 | lock_name = wq->name; | |
69a106c0 QC |
4506 | |
4507 | wq->lock_name = lock_name; | |
669de8bd BVA |
4508 | lockdep_init_map(&wq->lockdep_map, lock_name, &wq->key, 0); |
4509 | } | |
4510 | ||
4511 | static void wq_unregister_lockdep(struct workqueue_struct *wq) | |
4512 | { | |
4513 | lockdep_unregister_key(&wq->key); | |
4514 | } | |
4515 | ||
4516 | static void wq_free_lockdep(struct workqueue_struct *wq) | |
4517 | { | |
4518 | if (wq->lock_name != wq->name) | |
4519 | kfree(wq->lock_name); | |
4520 | } | |
4521 | #else | |
4522 | static void wq_init_lockdep(struct workqueue_struct *wq) | |
4523 | { | |
4524 | } | |
4525 | ||
4526 | static void wq_unregister_lockdep(struct workqueue_struct *wq) | |
4527 | { | |
4528 | } | |
4529 | ||
4530 | static void wq_free_lockdep(struct workqueue_struct *wq) | |
4531 | { | |
4532 | } | |
4533 | #endif | |
4534 | ||
91ccc6e7 TH |
4535 | static void free_node_nr_active(struct wq_node_nr_active **nna_ar) |
4536 | { | |
4537 | int node; | |
4538 | ||
4539 | for_each_node(node) { | |
4540 | kfree(nna_ar[node]); | |
4541 | nna_ar[node] = NULL; | |
4542 | } | |
4543 | ||
4544 | kfree(nna_ar[nr_node_ids]); | |
4545 | nna_ar[nr_node_ids] = NULL; | |
4546 | } | |
4547 | ||
4548 | static void init_node_nr_active(struct wq_node_nr_active *nna) | |
4549 | { | |
c5f8cd6c | 4550 | nna->max = WQ_DFL_MIN_ACTIVE; |
91ccc6e7 | 4551 | atomic_set(&nna->nr, 0); |
5797b1c1 TH |
4552 | raw_spin_lock_init(&nna->lock); |
4553 | INIT_LIST_HEAD(&nna->pending_pwqs); | |
91ccc6e7 TH |
4554 | } |
4555 | ||
4556 | /* | |
4557 | * Each node's nr_active counter will be accessed mostly from its own node and | |
4558 | * should be allocated in the node. | |
4559 | */ | |
4560 | static int alloc_node_nr_active(struct wq_node_nr_active **nna_ar) | |
4561 | { | |
4562 | struct wq_node_nr_active *nna; | |
4563 | int node; | |
4564 | ||
4565 | for_each_node(node) { | |
4566 | nna = kzalloc_node(sizeof(*nna), GFP_KERNEL, node); | |
4567 | if (!nna) | |
4568 | goto err_free; | |
4569 | init_node_nr_active(nna); | |
4570 | nna_ar[node] = nna; | |
4571 | } | |
4572 | ||
4573 | /* [nr_node_ids] is used as the fallback */ | |
4574 | nna = kzalloc_node(sizeof(*nna), GFP_KERNEL, NUMA_NO_NODE); | |
4575 | if (!nna) | |
4576 | goto err_free; | |
4577 | init_node_nr_active(nna); | |
4578 | nna_ar[nr_node_ids] = nna; | |
4579 | ||
4580 | return 0; | |
4581 | ||
4582 | err_free: | |
4583 | free_node_nr_active(nna_ar); | |
4584 | return -ENOMEM; | |
4585 | } | |
4586 | ||
6ba94429 | 4587 | static void rcu_free_wq(struct rcu_head *rcu) |
226223ab | 4588 | { |
6ba94429 FW |
4589 | struct workqueue_struct *wq = |
4590 | container_of(rcu, struct workqueue_struct, rcu); | |
226223ab | 4591 | |
91ccc6e7 TH |
4592 | if (wq->flags & WQ_UNBOUND) |
4593 | free_node_nr_active(wq->node_nr_active); | |
4594 | ||
669de8bd | 4595 | wq_free_lockdep(wq); |
636b927e TH |
4596 | free_percpu(wq->cpu_pwq); |
4597 | free_workqueue_attrs(wq->unbound_attrs); | |
6ba94429 | 4598 | kfree(wq); |
226223ab TH |
4599 | } |
4600 | ||
6ba94429 | 4601 | static void rcu_free_pool(struct rcu_head *rcu) |
226223ab | 4602 | { |
6ba94429 | 4603 | struct worker_pool *pool = container_of(rcu, struct worker_pool, rcu); |
226223ab | 4604 | |
6ba94429 FW |
4605 | ida_destroy(&pool->worker_ida); |
4606 | free_workqueue_attrs(pool->attrs); | |
4607 | kfree(pool); | |
226223ab TH |
4608 | } |
4609 | ||
6ba94429 FW |
4610 | /** |
4611 | * put_unbound_pool - put a worker_pool | |
4612 | * @pool: worker_pool to put | |
4613 | * | |
24acfb71 | 4614 | * Put @pool. If its refcnt reaches zero, it gets destroyed in RCU |
6ba94429 FW |
4615 | * safe manner. get_unbound_pool() calls this function on its failure path |
4616 | * and this function should be able to release pools which went through, | |
4617 | * successfully or not, init_worker_pool(). | |
4618 | * | |
4619 | * Should be called with wq_pool_mutex held. | |
4620 | */ | |
4621 | static void put_unbound_pool(struct worker_pool *pool) | |
226223ab | 4622 | { |
6ba94429 FW |
4623 | DECLARE_COMPLETION_ONSTACK(detach_completion); |
4624 | struct worker *worker; | |
9680540c | 4625 | LIST_HEAD(cull_list); |
e02b9312 | 4626 | |
6ba94429 | 4627 | lockdep_assert_held(&wq_pool_mutex); |
226223ab | 4628 | |
6ba94429 FW |
4629 | if (--pool->refcnt) |
4630 | return; | |
226223ab | 4631 | |
6ba94429 FW |
4632 | /* sanity checks */ |
4633 | if (WARN_ON(!(pool->cpu < 0)) || | |
4634 | WARN_ON(!list_empty(&pool->worklist))) | |
4635 | return; | |
226223ab | 4636 | |
6ba94429 FW |
4637 | /* release id and unhash */ |
4638 | if (pool->id >= 0) | |
4639 | idr_remove(&worker_pool_idr, pool->id); | |
4640 | hash_del(&pool->hash_node); | |
d55262c4 | 4641 | |
6ba94429 | 4642 | /* |
692b4825 TH |
4643 | * Become the manager and destroy all workers. This prevents |
4644 | * @pool's workers from blocking on attach_mutex. We're the last | |
4645 | * manager and @pool gets freed with the flag set. | |
9ab03be4 VS |
4646 | * |
4647 | * Having a concurrent manager is quite unlikely to happen as we can | |
4648 | * only get here with | |
4649 | * pwq->refcnt == pool->refcnt == 0 | |
4650 | * which implies no work queued to the pool, which implies no worker can | |
4651 | * become the manager. However a worker could have taken the role of | |
4652 | * manager before the refcnts dropped to 0, since maybe_create_worker() | |
4653 | * drops pool->lock | |
6ba94429 | 4654 | */ |
9ab03be4 VS |
4655 | while (true) { |
4656 | rcuwait_wait_event(&manager_wait, | |
4657 | !(pool->flags & POOL_MANAGER_ACTIVE), | |
4658 | TASK_UNINTERRUPTIBLE); | |
e02b9312 VS |
4659 | |
4660 | mutex_lock(&wq_pool_attach_mutex); | |
9ab03be4 VS |
4661 | raw_spin_lock_irq(&pool->lock); |
4662 | if (!(pool->flags & POOL_MANAGER_ACTIVE)) { | |
4663 | pool->flags |= POOL_MANAGER_ACTIVE; | |
4664 | break; | |
4665 | } | |
4666 | raw_spin_unlock_irq(&pool->lock); | |
e02b9312 | 4667 | mutex_unlock(&wq_pool_attach_mutex); |
9ab03be4 | 4668 | } |
692b4825 | 4669 | |
6ba94429 | 4670 | while ((worker = first_idle_worker(pool))) |
e02b9312 | 4671 | set_worker_dying(worker, &cull_list); |
6ba94429 | 4672 | WARN_ON(pool->nr_workers || pool->nr_idle); |
a9b8a985 | 4673 | raw_spin_unlock_irq(&pool->lock); |
d55262c4 | 4674 | |
e02b9312 VS |
4675 | wake_dying_workers(&cull_list); |
4676 | ||
4677 | if (!list_empty(&pool->workers) || !list_empty(&pool->dying_workers)) | |
6ba94429 | 4678 | pool->detach_completion = &detach_completion; |
1258fae7 | 4679 | mutex_unlock(&wq_pool_attach_mutex); |
226223ab | 4680 | |
6ba94429 FW |
4681 | if (pool->detach_completion) |
4682 | wait_for_completion(pool->detach_completion); | |
226223ab | 4683 | |
6ba94429 FW |
4684 | /* shut down the timers */ |
4685 | del_timer_sync(&pool->idle_timer); | |
3f959aa3 | 4686 | cancel_work_sync(&pool->idle_cull_work); |
6ba94429 | 4687 | del_timer_sync(&pool->mayday_timer); |
226223ab | 4688 | |
24acfb71 | 4689 | /* RCU protected to allow dereferences from get_work_pool() */ |
25b00775 | 4690 | call_rcu(&pool->rcu, rcu_free_pool); |
226223ab TH |
4691 | } |
4692 | ||
4693 | /** | |
6ba94429 FW |
4694 | * get_unbound_pool - get a worker_pool with the specified attributes |
4695 | * @attrs: the attributes of the worker_pool to get | |
226223ab | 4696 | * |
6ba94429 FW |
4697 | * Obtain a worker_pool which has the same attributes as @attrs, bump the |
4698 | * reference count and return it. If there already is a matching | |
4699 | * worker_pool, it will be used; otherwise, this function attempts to | |
4700 | * create a new one. | |
226223ab | 4701 | * |
6ba94429 | 4702 | * Should be called with wq_pool_mutex held. |
226223ab | 4703 | * |
6ba94429 FW |
4704 | * Return: On success, a worker_pool with the same attributes as @attrs. |
4705 | * On failure, %NULL. | |
226223ab | 4706 | */ |
6ba94429 | 4707 | static struct worker_pool *get_unbound_pool(const struct workqueue_attrs *attrs) |
226223ab | 4708 | { |
84193c07 | 4709 | struct wq_pod_type *pt = &wq_pod_types[WQ_AFFN_NUMA]; |
6ba94429 FW |
4710 | u32 hash = wqattrs_hash(attrs); |
4711 | struct worker_pool *pool; | |
84193c07 | 4712 | int pod, node = NUMA_NO_NODE; |
226223ab | 4713 | |
6ba94429 | 4714 | lockdep_assert_held(&wq_pool_mutex); |
226223ab | 4715 | |
6ba94429 FW |
4716 | /* do we already have a matching pool? */ |
4717 | hash_for_each_possible(unbound_pool_hash, pool, hash_node, hash) { | |
4718 | if (wqattrs_equal(pool->attrs, attrs)) { | |
4719 | pool->refcnt++; | |
4720 | return pool; | |
4721 | } | |
4722 | } | |
226223ab | 4723 | |
9546b29e | 4724 | /* If __pod_cpumask is contained inside a NUMA pod, that's our node */ |
84193c07 | 4725 | for (pod = 0; pod < pt->nr_pods; pod++) { |
9546b29e | 4726 | if (cpumask_subset(attrs->__pod_cpumask, pt->pod_cpus[pod])) { |
84193c07 TH |
4727 | node = pt->pod_node[pod]; |
4728 | break; | |
e2273584 XP |
4729 | } |
4730 | } | |
4731 | ||
6ba94429 | 4732 | /* nope, create a new one */ |
84193c07 | 4733 | pool = kzalloc_node(sizeof(*pool), GFP_KERNEL, node); |
6ba94429 FW |
4734 | if (!pool || init_worker_pool(pool) < 0) |
4735 | goto fail; | |
4736 | ||
84193c07 | 4737 | pool->node = node; |
5de7a03c TH |
4738 | copy_workqueue_attrs(pool->attrs, attrs); |
4739 | wqattrs_clear_for_pool(pool->attrs); | |
226223ab | 4740 | |
6ba94429 FW |
4741 | if (worker_pool_assign_id(pool) < 0) |
4742 | goto fail; | |
226223ab | 4743 | |
6ba94429 | 4744 | /* create and start the initial worker */ |
3347fa09 | 4745 | if (wq_online && !create_worker(pool)) |
6ba94429 | 4746 | goto fail; |
226223ab | 4747 | |
6ba94429 FW |
4748 | /* install */ |
4749 | hash_add(unbound_pool_hash, &pool->hash_node, hash); | |
226223ab | 4750 | |
6ba94429 FW |
4751 | return pool; |
4752 | fail: | |
4753 | if (pool) | |
4754 | put_unbound_pool(pool); | |
4755 | return NULL; | |
226223ab | 4756 | } |
226223ab | 4757 | |
6ba94429 | 4758 | static void rcu_free_pwq(struct rcu_head *rcu) |
7a4e344c | 4759 | { |
6ba94429 FW |
4760 | kmem_cache_free(pwq_cache, |
4761 | container_of(rcu, struct pool_workqueue, rcu)); | |
7a4e344c TH |
4762 | } |
4763 | ||
6ba94429 | 4764 | /* |
967b494e TH |
4765 | * Scheduled on pwq_release_worker by put_pwq() when an unbound pwq hits zero |
4766 | * refcnt and needs to be destroyed. | |
7a4e344c | 4767 | */ |
687a9aa5 | 4768 | static void pwq_release_workfn(struct kthread_work *work) |
7a4e344c | 4769 | { |
6ba94429 | 4770 | struct pool_workqueue *pwq = container_of(work, struct pool_workqueue, |
687a9aa5 | 4771 | release_work); |
6ba94429 FW |
4772 | struct workqueue_struct *wq = pwq->wq; |
4773 | struct worker_pool *pool = pwq->pool; | |
b42b0bdd | 4774 | bool is_last = false; |
7a4e344c | 4775 | |
b42b0bdd | 4776 | /* |
687a9aa5 | 4777 | * When @pwq is not linked, it doesn't hold any reference to the |
b42b0bdd YY |
4778 | * @wq, and @wq is invalid to access. |
4779 | */ | |
4780 | if (!list_empty(&pwq->pwqs_node)) { | |
b42b0bdd YY |
4781 | mutex_lock(&wq->mutex); |
4782 | list_del_rcu(&pwq->pwqs_node); | |
4783 | is_last = list_empty(&wq->pwqs); | |
4c065dbc WL |
4784 | |
4785 | /* | |
4786 | * For ordered workqueue with a plugged dfl_pwq, restart it now. | |
4787 | */ | |
4788 | if (!is_last && (wq->flags & __WQ_ORDERED)) | |
4789 | unplug_oldest_pwq(wq); | |
4790 | ||
b42b0bdd YY |
4791 | mutex_unlock(&wq->mutex); |
4792 | } | |
6ba94429 | 4793 | |
687a9aa5 TH |
4794 | if (wq->flags & WQ_UNBOUND) { |
4795 | mutex_lock(&wq_pool_mutex); | |
4796 | put_unbound_pool(pool); | |
4797 | mutex_unlock(&wq_pool_mutex); | |
4798 | } | |
6ba94429 | 4799 | |
5797b1c1 TH |
4800 | if (!list_empty(&pwq->pending_node)) { |
4801 | struct wq_node_nr_active *nna = | |
4802 | wq_node_nr_active(pwq->wq, pwq->pool->node); | |
4803 | ||
4804 | raw_spin_lock_irq(&nna->lock); | |
4805 | list_del_init(&pwq->pending_node); | |
4806 | raw_spin_unlock_irq(&nna->lock); | |
4807 | } | |
4808 | ||
25b00775 | 4809 | call_rcu(&pwq->rcu, rcu_free_pwq); |
7a4e344c | 4810 | |
2865a8fb | 4811 | /* |
6ba94429 FW |
4812 | * If we're the last pwq going away, @wq is already dead and no one |
4813 | * is gonna access it anymore. Schedule RCU free. | |
2865a8fb | 4814 | */ |
669de8bd BVA |
4815 | if (is_last) { |
4816 | wq_unregister_lockdep(wq); | |
25b00775 | 4817 | call_rcu(&wq->rcu, rcu_free_wq); |
669de8bd | 4818 | } |
29c91e99 TH |
4819 | } |
4820 | ||
67dc8325 | 4821 | /* initialize newly allocated @pwq which is associated with @wq and @pool */ |
6ba94429 FW |
4822 | static void init_pwq(struct pool_workqueue *pwq, struct workqueue_struct *wq, |
4823 | struct worker_pool *pool) | |
29c91e99 | 4824 | { |
6ba94429 | 4825 | BUG_ON((unsigned long)pwq & WORK_STRUCT_FLAG_MASK); |
29c91e99 | 4826 | |
6ba94429 FW |
4827 | memset(pwq, 0, sizeof(*pwq)); |
4828 | ||
4829 | pwq->pool = pool; | |
4830 | pwq->wq = wq; | |
4831 | pwq->flush_color = -1; | |
4832 | pwq->refcnt = 1; | |
f97a4a1a | 4833 | INIT_LIST_HEAD(&pwq->inactive_works); |
5797b1c1 | 4834 | INIT_LIST_HEAD(&pwq->pending_node); |
6ba94429 FW |
4835 | INIT_LIST_HEAD(&pwq->pwqs_node); |
4836 | INIT_LIST_HEAD(&pwq->mayday_node); | |
687a9aa5 | 4837 | kthread_init_work(&pwq->release_work, pwq_release_workfn); |
29c91e99 TH |
4838 | } |
4839 | ||
6ba94429 FW |
4840 | /* sync @pwq with the current state of its associated wq and link it */ |
4841 | static void link_pwq(struct pool_workqueue *pwq) | |
29c91e99 | 4842 | { |
6ba94429 | 4843 | struct workqueue_struct *wq = pwq->wq; |
29c91e99 | 4844 | |
6ba94429 | 4845 | lockdep_assert_held(&wq->mutex); |
a892cacc | 4846 | |
6ba94429 FW |
4847 | /* may be called multiple times, ignore if already linked */ |
4848 | if (!list_empty(&pwq->pwqs_node)) | |
29c91e99 | 4849 | return; |
29c91e99 | 4850 | |
6ba94429 FW |
4851 | /* set the matching work_color */ |
4852 | pwq->work_color = wq->work_color; | |
29c91e99 | 4853 | |
6ba94429 | 4854 | /* link in @pwq */ |
26fb7e3d | 4855 | list_add_tail_rcu(&pwq->pwqs_node, &wq->pwqs); |
6ba94429 | 4856 | } |
29c91e99 | 4857 | |
6ba94429 FW |
4858 | /* obtain a pool matching @attr and create a pwq associating the pool and @wq */ |
4859 | static struct pool_workqueue *alloc_unbound_pwq(struct workqueue_struct *wq, | |
4860 | const struct workqueue_attrs *attrs) | |
4861 | { | |
4862 | struct worker_pool *pool; | |
4863 | struct pool_workqueue *pwq; | |
60f5a4bc | 4864 | |
6ba94429 | 4865 | lockdep_assert_held(&wq_pool_mutex); |
60f5a4bc | 4866 | |
6ba94429 FW |
4867 | pool = get_unbound_pool(attrs); |
4868 | if (!pool) | |
4869 | return NULL; | |
60f5a4bc | 4870 | |
6ba94429 FW |
4871 | pwq = kmem_cache_alloc_node(pwq_cache, GFP_KERNEL, pool->node); |
4872 | if (!pwq) { | |
4873 | put_unbound_pool(pool); | |
4874 | return NULL; | |
4875 | } | |
29c91e99 | 4876 | |
6ba94429 FW |
4877 | init_pwq(pwq, wq, pool); |
4878 | return pwq; | |
4879 | } | |
29c91e99 | 4880 | |
29c91e99 | 4881 | /** |
fef59c9c | 4882 | * wq_calc_pod_cpumask - calculate a wq_attrs' cpumask for a pod |
042f7df1 | 4883 | * @attrs: the wq_attrs of the default pwq of the target workqueue |
84193c07 | 4884 | * @cpu: the target CPU |
6ba94429 | 4885 | * @cpu_going_down: if >= 0, the CPU to consider as offline |
29c91e99 | 4886 | * |
fef59c9c TH |
4887 | * Calculate the cpumask a workqueue with @attrs should use on @pod. If |
4888 | * @cpu_going_down is >= 0, that cpu is considered offline during calculation. | |
9546b29e | 4889 | * The result is stored in @attrs->__pod_cpumask. |
a892cacc | 4890 | * |
fef59c9c TH |
4891 | * If pod affinity is not enabled, @attrs->cpumask is always used. If enabled |
4892 | * and @pod has online CPUs requested by @attrs, the returned cpumask is the | |
4893 | * intersection of the possible CPUs of @pod and @attrs->cpumask. | |
d185af30 | 4894 | * |
fef59c9c | 4895 | * The caller is responsible for ensuring that the cpumask of @pod stays stable. |
29c91e99 | 4896 | */ |
9546b29e TH |
4897 | static void wq_calc_pod_cpumask(struct workqueue_attrs *attrs, int cpu, |
4898 | int cpu_going_down) | |
29c91e99 | 4899 | { |
84193c07 TH |
4900 | const struct wq_pod_type *pt = wqattrs_pod_type(attrs); |
4901 | int pod = pt->cpu_pod[cpu]; | |
29c91e99 | 4902 | |
fef59c9c | 4903 | /* does @pod have any online CPUs @attrs wants? */ |
9546b29e TH |
4904 | cpumask_and(attrs->__pod_cpumask, pt->pod_cpus[pod], attrs->cpumask); |
4905 | cpumask_and(attrs->__pod_cpumask, attrs->__pod_cpumask, cpu_online_mask); | |
6ba94429 | 4906 | if (cpu_going_down >= 0) |
9546b29e | 4907 | cpumask_clear_cpu(cpu_going_down, attrs->__pod_cpumask); |
29c91e99 | 4908 | |
9546b29e TH |
4909 | if (cpumask_empty(attrs->__pod_cpumask)) { |
4910 | cpumask_copy(attrs->__pod_cpumask, attrs->cpumask); | |
84193c07 TH |
4911 | return; |
4912 | } | |
4c16bd32 | 4913 | |
fef59c9c | 4914 | /* yeap, return possible CPUs in @pod that @attrs wants */ |
9546b29e | 4915 | cpumask_and(attrs->__pod_cpumask, attrs->cpumask, pt->pod_cpus[pod]); |
1ad0f0a7 | 4916 | |
9546b29e | 4917 | if (cpumask_empty(attrs->__pod_cpumask)) |
1ad0f0a7 MB |
4918 | pr_warn_once("WARNING: workqueue cpumask: online intersect > " |
4919 | "possible intersect\n"); | |
4c16bd32 TH |
4920 | } |
4921 | ||
9f66cff2 | 4922 | /* install @pwq into @wq and return the old pwq, @cpu < 0 for dfl_pwq */ |
636b927e TH |
4923 | static struct pool_workqueue *install_unbound_pwq(struct workqueue_struct *wq, |
4924 | int cpu, struct pool_workqueue *pwq) | |
1befcf30 | 4925 | { |
9f66cff2 | 4926 | struct pool_workqueue __rcu **slot = unbound_pwq_slot(wq, cpu); |
1befcf30 TH |
4927 | struct pool_workqueue *old_pwq; |
4928 | ||
5b95e1af | 4929 | lockdep_assert_held(&wq_pool_mutex); |
1befcf30 TH |
4930 | lockdep_assert_held(&wq->mutex); |
4931 | ||
4932 | /* link_pwq() can handle duplicate calls */ | |
4933 | link_pwq(pwq); | |
4934 | ||
9f66cff2 TH |
4935 | old_pwq = rcu_access_pointer(*slot); |
4936 | rcu_assign_pointer(*slot, pwq); | |
1befcf30 TH |
4937 | return old_pwq; |
4938 | } | |
4939 | ||
2d5f0764 LJ |
4940 | /* context to store the prepared attrs & pwqs before applying */ |
4941 | struct apply_wqattrs_ctx { | |
4942 | struct workqueue_struct *wq; /* target workqueue */ | |
4943 | struct workqueue_attrs *attrs; /* attrs to apply */ | |
042f7df1 | 4944 | struct list_head list; /* queued for batching commit */ |
2d5f0764 LJ |
4945 | struct pool_workqueue *dfl_pwq; |
4946 | struct pool_workqueue *pwq_tbl[]; | |
4947 | }; | |
4948 | ||
4949 | /* free the resources after success or abort */ | |
4950 | static void apply_wqattrs_cleanup(struct apply_wqattrs_ctx *ctx) | |
4951 | { | |
4952 | if (ctx) { | |
636b927e | 4953 | int cpu; |
2d5f0764 | 4954 | |
636b927e TH |
4955 | for_each_possible_cpu(cpu) |
4956 | put_pwq_unlocked(ctx->pwq_tbl[cpu]); | |
2d5f0764 LJ |
4957 | put_pwq_unlocked(ctx->dfl_pwq); |
4958 | ||
4959 | free_workqueue_attrs(ctx->attrs); | |
4960 | ||
4961 | kfree(ctx); | |
4962 | } | |
4963 | } | |
4964 | ||
4965 | /* allocate the attrs and pwqs for later installation */ | |
4966 | static struct apply_wqattrs_ctx * | |
4967 | apply_wqattrs_prepare(struct workqueue_struct *wq, | |
99c621ef LJ |
4968 | const struct workqueue_attrs *attrs, |
4969 | const cpumask_var_t unbound_cpumask) | |
9e8cd2f5 | 4970 | { |
2d5f0764 | 4971 | struct apply_wqattrs_ctx *ctx; |
9546b29e | 4972 | struct workqueue_attrs *new_attrs; |
636b927e | 4973 | int cpu; |
9e8cd2f5 | 4974 | |
2d5f0764 | 4975 | lockdep_assert_held(&wq_pool_mutex); |
9e8cd2f5 | 4976 | |
84193c07 TH |
4977 | if (WARN_ON(attrs->affn_scope < 0 || |
4978 | attrs->affn_scope >= WQ_AFFN_NR_TYPES)) | |
4979 | return ERR_PTR(-EINVAL); | |
4980 | ||
636b927e | 4981 | ctx = kzalloc(struct_size(ctx, pwq_tbl, nr_cpu_ids), GFP_KERNEL); |
8719dcea | 4982 | |
be69d00d | 4983 | new_attrs = alloc_workqueue_attrs(); |
9546b29e | 4984 | if (!ctx || !new_attrs) |
2d5f0764 | 4985 | goto out_free; |
13e2e556 | 4986 | |
4c16bd32 TH |
4987 | /* |
4988 | * If something goes wrong during CPU up/down, we'll fall back to | |
4989 | * the default pwq covering whole @attrs->cpumask. Always create | |
4990 | * it even if we don't use it immediately. | |
4991 | */ | |
0f36ee24 TH |
4992 | copy_workqueue_attrs(new_attrs, attrs); |
4993 | wqattrs_actualize_cpumask(new_attrs, unbound_cpumask); | |
9546b29e | 4994 | cpumask_copy(new_attrs->__pod_cpumask, new_attrs->cpumask); |
2d5f0764 LJ |
4995 | ctx->dfl_pwq = alloc_unbound_pwq(wq, new_attrs); |
4996 | if (!ctx->dfl_pwq) | |
4997 | goto out_free; | |
4c16bd32 | 4998 | |
636b927e | 4999 | for_each_possible_cpu(cpu) { |
af73f5c9 | 5000 | if (new_attrs->ordered) { |
2d5f0764 | 5001 | ctx->dfl_pwq->refcnt++; |
636b927e TH |
5002 | ctx->pwq_tbl[cpu] = ctx->dfl_pwq; |
5003 | } else { | |
9546b29e TH |
5004 | wq_calc_pod_cpumask(new_attrs, cpu, -1); |
5005 | ctx->pwq_tbl[cpu] = alloc_unbound_pwq(wq, new_attrs); | |
636b927e TH |
5006 | if (!ctx->pwq_tbl[cpu]) |
5007 | goto out_free; | |
4c16bd32 TH |
5008 | } |
5009 | } | |
5010 | ||
042f7df1 LJ |
5011 | /* save the user configured attrs and sanitize it. */ |
5012 | copy_workqueue_attrs(new_attrs, attrs); | |
5013 | cpumask_and(new_attrs->cpumask, new_attrs->cpumask, cpu_possible_mask); | |
9546b29e | 5014 | cpumask_copy(new_attrs->__pod_cpumask, new_attrs->cpumask); |
2d5f0764 | 5015 | ctx->attrs = new_attrs; |
042f7df1 | 5016 | |
4c065dbc WL |
5017 | /* |
5018 | * For initialized ordered workqueues, there should only be one pwq | |
5019 | * (dfl_pwq). Set the plugged flag of ctx->dfl_pwq to suspend execution | |
5020 | * of newly queued work items until execution of older work items in | |
5021 | * the old pwq's have completed. | |
5022 | */ | |
5023 | if ((wq->flags & __WQ_ORDERED) && !list_empty(&wq->pwqs)) | |
5024 | ctx->dfl_pwq->plugged = true; | |
5025 | ||
2d5f0764 | 5026 | ctx->wq = wq; |
2d5f0764 LJ |
5027 | return ctx; |
5028 | ||
5029 | out_free: | |
2d5f0764 LJ |
5030 | free_workqueue_attrs(new_attrs); |
5031 | apply_wqattrs_cleanup(ctx); | |
84193c07 | 5032 | return ERR_PTR(-ENOMEM); |
2d5f0764 LJ |
5033 | } |
5034 | ||
5035 | /* set attrs and install prepared pwqs, @ctx points to old pwqs on return */ | |
5036 | static void apply_wqattrs_commit(struct apply_wqattrs_ctx *ctx) | |
5037 | { | |
636b927e | 5038 | int cpu; |
9e8cd2f5 | 5039 | |
4c16bd32 | 5040 | /* all pwqs have been created successfully, let's install'em */ |
2d5f0764 | 5041 | mutex_lock(&ctx->wq->mutex); |
a892cacc | 5042 | |
2d5f0764 | 5043 | copy_workqueue_attrs(ctx->wq->unbound_attrs, ctx->attrs); |
4c16bd32 | 5044 | |
9f66cff2 | 5045 | /* save the previous pwqs and install the new ones */ |
636b927e TH |
5046 | for_each_possible_cpu(cpu) |
5047 | ctx->pwq_tbl[cpu] = install_unbound_pwq(ctx->wq, cpu, | |
5048 | ctx->pwq_tbl[cpu]); | |
9f66cff2 | 5049 | ctx->dfl_pwq = install_unbound_pwq(ctx->wq, -1, ctx->dfl_pwq); |
f147f29e | 5050 | |
5797b1c1 TH |
5051 | /* update node_nr_active->max */ |
5052 | wq_update_node_max_active(ctx->wq, -1); | |
5053 | ||
d64f2fa0 JL |
5054 | /* rescuer needs to respect wq cpumask changes */ |
5055 | if (ctx->wq->rescuer) | |
5056 | set_cpus_allowed_ptr(ctx->wq->rescuer->task, | |
5057 | unbound_effective_cpumask(ctx->wq)); | |
5058 | ||
2d5f0764 LJ |
5059 | mutex_unlock(&ctx->wq->mutex); |
5060 | } | |
9e8cd2f5 | 5061 | |
a0111cf6 LJ |
5062 | static int apply_workqueue_attrs_locked(struct workqueue_struct *wq, |
5063 | const struct workqueue_attrs *attrs) | |
2d5f0764 LJ |
5064 | { |
5065 | struct apply_wqattrs_ctx *ctx; | |
4c16bd32 | 5066 | |
2d5f0764 LJ |
5067 | /* only unbound workqueues can change attributes */ |
5068 | if (WARN_ON(!(wq->flags & WQ_UNBOUND))) | |
5069 | return -EINVAL; | |
13e2e556 | 5070 | |
99c621ef | 5071 | ctx = apply_wqattrs_prepare(wq, attrs, wq_unbound_cpumask); |
84193c07 TH |
5072 | if (IS_ERR(ctx)) |
5073 | return PTR_ERR(ctx); | |
2d5f0764 LJ |
5074 | |
5075 | /* the ctx has been prepared successfully, let's commit it */ | |
6201171e | 5076 | apply_wqattrs_commit(ctx); |
2d5f0764 LJ |
5077 | apply_wqattrs_cleanup(ctx); |
5078 | ||
6201171e | 5079 | return 0; |
9e8cd2f5 TH |
5080 | } |
5081 | ||
a0111cf6 LJ |
5082 | /** |
5083 | * apply_workqueue_attrs - apply new workqueue_attrs to an unbound workqueue | |
5084 | * @wq: the target workqueue | |
5085 | * @attrs: the workqueue_attrs to apply, allocated with alloc_workqueue_attrs() | |
5086 | * | |
fef59c9c TH |
5087 | * Apply @attrs to an unbound workqueue @wq. Unless disabled, this function maps |
5088 | * a separate pwq to each CPU pod with possibles CPUs in @attrs->cpumask so that | |
5089 | * work items are affine to the pod it was issued on. Older pwqs are released as | |
5090 | * in-flight work items finish. Note that a work item which repeatedly requeues | |
5091 | * itself back-to-back will stay on its current pwq. | |
a0111cf6 LJ |
5092 | * |
5093 | * Performs GFP_KERNEL allocations. | |
5094 | * | |
ffd8bea8 | 5095 | * Assumes caller has CPU hotplug read exclusion, i.e. cpus_read_lock(). |
509b3204 | 5096 | * |
a0111cf6 LJ |
5097 | * Return: 0 on success and -errno on failure. |
5098 | */ | |
513c98d0 | 5099 | int apply_workqueue_attrs(struct workqueue_struct *wq, |
a0111cf6 LJ |
5100 | const struct workqueue_attrs *attrs) |
5101 | { | |
5102 | int ret; | |
5103 | ||
509b3204 DJ |
5104 | lockdep_assert_cpus_held(); |
5105 | ||
5106 | mutex_lock(&wq_pool_mutex); | |
a0111cf6 | 5107 | ret = apply_workqueue_attrs_locked(wq, attrs); |
509b3204 | 5108 | mutex_unlock(&wq_pool_mutex); |
a0111cf6 LJ |
5109 | |
5110 | return ret; | |
5111 | } | |
5112 | ||
4c16bd32 | 5113 | /** |
fef59c9c | 5114 | * wq_update_pod - update pod affinity of a wq for CPU hot[un]plug |
4c16bd32 | 5115 | * @wq: the target workqueue |
4cbfd3de TH |
5116 | * @cpu: the CPU to update pool association for |
5117 | * @hotplug_cpu: the CPU coming up or going down | |
4c16bd32 TH |
5118 | * @online: whether @cpu is coming up or going down |
5119 | * | |
5120 | * This function is to be called from %CPU_DOWN_PREPARE, %CPU_ONLINE and | |
fef59c9c | 5121 | * %CPU_DOWN_FAILED. @cpu is being hot[un]plugged, update pod affinity of |
4c16bd32 TH |
5122 | * @wq accordingly. |
5123 | * | |
fef59c9c TH |
5124 | * |
5125 | * If pod affinity can't be adjusted due to memory allocation failure, it falls | |
5126 | * back to @wq->dfl_pwq which may not be optimal but is always correct. | |
5127 | * | |
5128 | * Note that when the last allowed CPU of a pod goes offline for a workqueue | |
5129 | * with a cpumask spanning multiple pods, the workers which were already | |
5130 | * executing the work items for the workqueue will lose their CPU affinity and | |
5131 | * may execute on any CPU. This is similar to how per-cpu workqueues behave on | |
5132 | * CPU_DOWN. If a workqueue user wants strict affinity, it's the user's | |
5133 | * responsibility to flush the work item from CPU_DOWN_PREPARE. | |
4c16bd32 | 5134 | */ |
fef59c9c TH |
5135 | static void wq_update_pod(struct workqueue_struct *wq, int cpu, |
5136 | int hotplug_cpu, bool online) | |
4c16bd32 | 5137 | { |
4cbfd3de | 5138 | int off_cpu = online ? -1 : hotplug_cpu; |
4c16bd32 TH |
5139 | struct pool_workqueue *old_pwq = NULL, *pwq; |
5140 | struct workqueue_attrs *target_attrs; | |
4c16bd32 TH |
5141 | |
5142 | lockdep_assert_held(&wq_pool_mutex); | |
5143 | ||
84193c07 | 5144 | if (!(wq->flags & WQ_UNBOUND) || wq->unbound_attrs->ordered) |
4c16bd32 TH |
5145 | return; |
5146 | ||
5147 | /* | |
5148 | * We don't wanna alloc/free wq_attrs for each wq for each CPU. | |
5149 | * Let's use a preallocated one. The following buf is protected by | |
5150 | * CPU hotplug exclusion. | |
5151 | */ | |
fef59c9c | 5152 | target_attrs = wq_update_pod_attrs_buf; |
4c16bd32 | 5153 | |
4c16bd32 | 5154 | copy_workqueue_attrs(target_attrs, wq->unbound_attrs); |
0f36ee24 | 5155 | wqattrs_actualize_cpumask(target_attrs, wq_unbound_cpumask); |
4c16bd32 | 5156 | |
636b927e | 5157 | /* nothing to do if the target cpumask matches the current pwq */ |
9546b29e | 5158 | wq_calc_pod_cpumask(target_attrs, cpu, off_cpu); |
9f66cff2 | 5159 | if (wqattrs_equal(target_attrs, unbound_pwq(wq, cpu)->pool->attrs)) |
636b927e | 5160 | return; |
4c16bd32 | 5161 | |
4c16bd32 TH |
5162 | /* create a new pwq */ |
5163 | pwq = alloc_unbound_pwq(wq, target_attrs); | |
5164 | if (!pwq) { | |
fef59c9c | 5165 | pr_warn("workqueue: allocation failed while updating CPU pod affinity of \"%s\"\n", |
2d916033 | 5166 | wq->name); |
77f300b1 | 5167 | goto use_dfl_pwq; |
4c16bd32 TH |
5168 | } |
5169 | ||
f7142ed4 | 5170 | /* Install the new pwq. */ |
4c16bd32 | 5171 | mutex_lock(&wq->mutex); |
636b927e | 5172 | old_pwq = install_unbound_pwq(wq, cpu, pwq); |
4c16bd32 TH |
5173 | goto out_unlock; |
5174 | ||
5175 | use_dfl_pwq: | |
f7142ed4 | 5176 | mutex_lock(&wq->mutex); |
9f66cff2 TH |
5177 | pwq = unbound_pwq(wq, -1); |
5178 | raw_spin_lock_irq(&pwq->pool->lock); | |
5179 | get_pwq(pwq); | |
5180 | raw_spin_unlock_irq(&pwq->pool->lock); | |
5181 | old_pwq = install_unbound_pwq(wq, cpu, pwq); | |
4c16bd32 TH |
5182 | out_unlock: |
5183 | mutex_unlock(&wq->mutex); | |
5184 | put_pwq_unlocked(old_pwq); | |
5185 | } | |
5186 | ||
30cdf249 | 5187 | static int alloc_and_link_pwqs(struct workqueue_struct *wq) |
0f900049 | 5188 | { |
49e3cf44 | 5189 | bool highpri = wq->flags & WQ_HIGHPRI; |
8a2b7538 | 5190 | int cpu, ret; |
30cdf249 | 5191 | |
636b927e TH |
5192 | wq->cpu_pwq = alloc_percpu(struct pool_workqueue *); |
5193 | if (!wq->cpu_pwq) | |
5194 | goto enomem; | |
30cdf249 | 5195 | |
636b927e | 5196 | if (!(wq->flags & WQ_UNBOUND)) { |
30cdf249 | 5197 | for_each_possible_cpu(cpu) { |
4cb1ef64 TH |
5198 | struct pool_workqueue **pwq_p; |
5199 | struct worker_pool __percpu *pools; | |
5200 | struct worker_pool *pool; | |
5201 | ||
5202 | if (wq->flags & WQ_BH) | |
5203 | pools = bh_worker_pools; | |
5204 | else | |
5205 | pools = cpu_worker_pools; | |
5206 | ||
5207 | pool = &(per_cpu_ptr(pools, cpu)[highpri]); | |
5208 | pwq_p = per_cpu_ptr(wq->cpu_pwq, cpu); | |
687a9aa5 TH |
5209 | |
5210 | *pwq_p = kmem_cache_alloc_node(pwq_cache, GFP_KERNEL, | |
5211 | pool->node); | |
5212 | if (!*pwq_p) | |
5213 | goto enomem; | |
f3421797 | 5214 | |
687a9aa5 | 5215 | init_pwq(*pwq_p, wq, pool); |
f147f29e TH |
5216 | |
5217 | mutex_lock(&wq->mutex); | |
687a9aa5 | 5218 | link_pwq(*pwq_p); |
f147f29e | 5219 | mutex_unlock(&wq->mutex); |
30cdf249 | 5220 | } |
9e8cd2f5 | 5221 | return 0; |
509b3204 DJ |
5222 | } |
5223 | ||
ffd8bea8 | 5224 | cpus_read_lock(); |
509b3204 | 5225 | if (wq->flags & __WQ_ORDERED) { |
9f66cff2 TH |
5226 | struct pool_workqueue *dfl_pwq; |
5227 | ||
8a2b7538 TH |
5228 | ret = apply_workqueue_attrs(wq, ordered_wq_attrs[highpri]); |
5229 | /* there should only be single pwq for ordering guarantee */ | |
9f66cff2 TH |
5230 | dfl_pwq = rcu_access_pointer(wq->dfl_pwq); |
5231 | WARN(!ret && (wq->pwqs.next != &dfl_pwq->pwqs_node || | |
5232 | wq->pwqs.prev != &dfl_pwq->pwqs_node), | |
8a2b7538 | 5233 | "ordering guarantee broken for workqueue %s\n", wq->name); |
30cdf249 | 5234 | } else { |
509b3204 | 5235 | ret = apply_workqueue_attrs(wq, unbound_std_wq_attrs[highpri]); |
30cdf249 | 5236 | } |
ffd8bea8 | 5237 | cpus_read_unlock(); |
509b3204 | 5238 | |
64344553 Z |
5239 | /* for unbound pwq, flush the pwq_release_worker ensures that the |
5240 | * pwq_release_workfn() completes before calling kfree(wq). | |
5241 | */ | |
5242 | if (ret) | |
5243 | kthread_flush_worker(pwq_release_worker); | |
5244 | ||
509b3204 | 5245 | return ret; |
687a9aa5 TH |
5246 | |
5247 | enomem: | |
5248 | if (wq->cpu_pwq) { | |
7b42f401 Z |
5249 | for_each_possible_cpu(cpu) { |
5250 | struct pool_workqueue *pwq = *per_cpu_ptr(wq->cpu_pwq, cpu); | |
5251 | ||
5252 | if (pwq) | |
5253 | kmem_cache_free(pwq_cache, pwq); | |
5254 | } | |
687a9aa5 TH |
5255 | free_percpu(wq->cpu_pwq); |
5256 | wq->cpu_pwq = NULL; | |
5257 | } | |
5258 | return -ENOMEM; | |
0f900049 TH |
5259 | } |
5260 | ||
f3421797 TH |
5261 | static int wq_clamp_max_active(int max_active, unsigned int flags, |
5262 | const char *name) | |
b71ab8c2 | 5263 | { |
636b927e | 5264 | if (max_active < 1 || max_active > WQ_MAX_ACTIVE) |
044c782c | 5265 | pr_warn("workqueue: max_active %d requested for %s is out of range, clamping between %d and %d\n", |
636b927e | 5266 | max_active, name, 1, WQ_MAX_ACTIVE); |
b71ab8c2 | 5267 | |
636b927e | 5268 | return clamp_val(max_active, 1, WQ_MAX_ACTIVE); |
b71ab8c2 TH |
5269 | } |
5270 | ||
983c7515 TH |
5271 | /* |
5272 | * Workqueues which may be used during memory reclaim should have a rescuer | |
5273 | * to guarantee forward progress. | |
5274 | */ | |
5275 | static int init_rescuer(struct workqueue_struct *wq) | |
5276 | { | |
5277 | struct worker *rescuer; | |
b92b36ea | 5278 | int ret; |
983c7515 TH |
5279 | |
5280 | if (!(wq->flags & WQ_MEM_RECLAIM)) | |
5281 | return 0; | |
5282 | ||
5283 | rescuer = alloc_worker(NUMA_NO_NODE); | |
4c0736a7 PM |
5284 | if (!rescuer) { |
5285 | pr_err("workqueue: Failed to allocate a rescuer for wq \"%s\"\n", | |
5286 | wq->name); | |
983c7515 | 5287 | return -ENOMEM; |
4c0736a7 | 5288 | } |
983c7515 TH |
5289 | |
5290 | rescuer->rescue_wq = wq; | |
b6a46f72 | 5291 | rescuer->task = kthread_create(rescuer_thread, rescuer, "kworker/R-%s", wq->name); |
f187b697 | 5292 | if (IS_ERR(rescuer->task)) { |
b92b36ea | 5293 | ret = PTR_ERR(rescuer->task); |
4c0736a7 PM |
5294 | pr_err("workqueue: Failed to create a rescuer kthread for wq \"%s\": %pe", |
5295 | wq->name, ERR_PTR(ret)); | |
983c7515 | 5296 | kfree(rescuer); |
b92b36ea | 5297 | return ret; |
983c7515 TH |
5298 | } |
5299 | ||
5300 | wq->rescuer = rescuer; | |
85f0ab43 JL |
5301 | if (wq->flags & WQ_UNBOUND) |
5302 | kthread_bind_mask(rescuer->task, wq->unbound_attrs->cpumask); | |
5303 | else | |
5304 | kthread_bind_mask(rescuer->task, cpu_possible_mask); | |
983c7515 TH |
5305 | wake_up_process(rescuer->task); |
5306 | ||
5307 | return 0; | |
5308 | } | |
5309 | ||
a045a272 TH |
5310 | /** |
5311 | * wq_adjust_max_active - update a wq's max_active to the current setting | |
5312 | * @wq: target workqueue | |
5313 | * | |
5314 | * If @wq isn't freezing, set @wq->max_active to the saved_max_active and | |
5315 | * activate inactive work items accordingly. If @wq is freezing, clear | |
5316 | * @wq->max_active to zero. | |
5317 | */ | |
5318 | static void wq_adjust_max_active(struct workqueue_struct *wq) | |
5319 | { | |
c5404d4e | 5320 | bool activated; |
5797b1c1 | 5321 | int new_max, new_min; |
a045a272 TH |
5322 | |
5323 | lockdep_assert_held(&wq->mutex); | |
5324 | ||
5325 | if ((wq->flags & WQ_FREEZABLE) && workqueue_freezing) { | |
5797b1c1 TH |
5326 | new_max = 0; |
5327 | new_min = 0; | |
5328 | } else { | |
5329 | new_max = wq->saved_max_active; | |
5330 | new_min = wq->saved_min_active; | |
a045a272 TH |
5331 | } |
5332 | ||
5797b1c1 | 5333 | if (wq->max_active == new_max && wq->min_active == new_min) |
a045a272 TH |
5334 | return; |
5335 | ||
5336 | /* | |
5797b1c1 | 5337 | * Update @wq->max/min_active and then kick inactive work items if more |
a045a272 TH |
5338 | * active work items are allowed. This doesn't break work item ordering |
5339 | * because new work items are always queued behind existing inactive | |
5340 | * work items if there are any. | |
5341 | */ | |
5797b1c1 TH |
5342 | WRITE_ONCE(wq->max_active, new_max); |
5343 | WRITE_ONCE(wq->min_active, new_min); | |
5344 | ||
5345 | if (wq->flags & WQ_UNBOUND) | |
5346 | wq_update_node_max_active(wq, -1); | |
5347 | ||
5348 | if (new_max == 0) | |
5349 | return; | |
a045a272 | 5350 | |
c5404d4e TH |
5351 | /* |
5352 | * Round-robin through pwq's activating the first inactive work item | |
5353 | * until max_active is filled. | |
5354 | */ | |
5355 | do { | |
5356 | struct pool_workqueue *pwq; | |
a045a272 | 5357 | |
c5404d4e TH |
5358 | activated = false; |
5359 | for_each_pwq(pwq, wq) { | |
5360 | unsigned long flags; | |
a045a272 | 5361 | |
c5404d4e TH |
5362 | /* can be called during early boot w/ irq disabled */ |
5363 | raw_spin_lock_irqsave(&pwq->pool->lock, flags); | |
5797b1c1 | 5364 | if (pwq_activate_first_inactive(pwq, true)) { |
c5404d4e TH |
5365 | activated = true; |
5366 | kick_pool(pwq->pool); | |
5367 | } | |
5368 | raw_spin_unlock_irqrestore(&pwq->pool->lock, flags); | |
5369 | } | |
5370 | } while (activated); | |
a045a272 TH |
5371 | } |
5372 | ||
a2775bbc | 5373 | __printf(1, 4) |
669de8bd BVA |
5374 | struct workqueue_struct *alloc_workqueue(const char *fmt, |
5375 | unsigned int flags, | |
5376 | int max_active, ...) | |
1da177e4 | 5377 | { |
ecf6881f | 5378 | va_list args; |
1da177e4 | 5379 | struct workqueue_struct *wq; |
91ccc6e7 TH |
5380 | size_t wq_size; |
5381 | int name_len; | |
b196be89 | 5382 | |
4cb1ef64 TH |
5383 | if (flags & WQ_BH) { |
5384 | if (WARN_ON_ONCE(flags & ~__WQ_BH_ALLOWS)) | |
5385 | return NULL; | |
5386 | if (WARN_ON_ONCE(max_active)) | |
5387 | return NULL; | |
5388 | } | |
5389 | ||
cee22a15 VK |
5390 | /* see the comment above the definition of WQ_POWER_EFFICIENT */ |
5391 | if ((flags & WQ_POWER_EFFICIENT) && wq_power_efficient) | |
5392 | flags |= WQ_UNBOUND; | |
5393 | ||
ecf6881f | 5394 | /* allocate wq and format name */ |
91ccc6e7 TH |
5395 | if (flags & WQ_UNBOUND) |
5396 | wq_size = struct_size(wq, node_nr_active, nr_node_ids + 1); | |
5397 | else | |
5398 | wq_size = sizeof(*wq); | |
5399 | ||
5400 | wq = kzalloc(wq_size, GFP_KERNEL); | |
b196be89 | 5401 | if (!wq) |
d2c1d404 | 5402 | return NULL; |
b196be89 | 5403 | |
6029a918 | 5404 | if (flags & WQ_UNBOUND) { |
be69d00d | 5405 | wq->unbound_attrs = alloc_workqueue_attrs(); |
6029a918 TH |
5406 | if (!wq->unbound_attrs) |
5407 | goto err_free_wq; | |
5408 | } | |
5409 | ||
669de8bd | 5410 | va_start(args, max_active); |
91ccc6e7 | 5411 | name_len = vsnprintf(wq->name, sizeof(wq->name), fmt, args); |
b196be89 | 5412 | va_end(args); |
1da177e4 | 5413 | |
91ccc6e7 TH |
5414 | if (name_len >= WQ_NAME_LEN) |
5415 | pr_warn_once("workqueue: name exceeds WQ_NAME_LEN. Truncating to: %s\n", | |
5416 | wq->name); | |
31c89007 | 5417 | |
4cb1ef64 TH |
5418 | if (flags & WQ_BH) { |
5419 | /* | |
5420 | * BH workqueues always share a single execution context per CPU | |
5421 | * and don't impose any max_active limit. | |
5422 | */ | |
5423 | max_active = INT_MAX; | |
5424 | } else { | |
5425 | max_active = max_active ?: WQ_DFL_ACTIVE; | |
5426 | max_active = wq_clamp_max_active(max_active, flags, wq->name); | |
5427 | } | |
3af24433 | 5428 | |
b196be89 | 5429 | /* init wq */ |
97e37d7b | 5430 | wq->flags = flags; |
a045a272 | 5431 | wq->max_active = max_active; |
5797b1c1 TH |
5432 | wq->min_active = min(max_active, WQ_DFL_MIN_ACTIVE); |
5433 | wq->saved_max_active = wq->max_active; | |
5434 | wq->saved_min_active = wq->min_active; | |
3c25a55d | 5435 | mutex_init(&wq->mutex); |
112202d9 | 5436 | atomic_set(&wq->nr_pwqs_to_flush, 0); |
30cdf249 | 5437 | INIT_LIST_HEAD(&wq->pwqs); |
73f53c4a TH |
5438 | INIT_LIST_HEAD(&wq->flusher_queue); |
5439 | INIT_LIST_HEAD(&wq->flusher_overflow); | |
493a1724 | 5440 | INIT_LIST_HEAD(&wq->maydays); |
502ca9d8 | 5441 | |
669de8bd | 5442 | wq_init_lockdep(wq); |
cce1a165 | 5443 | INIT_LIST_HEAD(&wq->list); |
3af24433 | 5444 | |
91ccc6e7 TH |
5445 | if (flags & WQ_UNBOUND) { |
5446 | if (alloc_node_nr_active(wq->node_nr_active) < 0) | |
5447 | goto err_unreg_lockdep; | |
5448 | } | |
5449 | ||
30cdf249 | 5450 | if (alloc_and_link_pwqs(wq) < 0) |
91ccc6e7 | 5451 | goto err_free_node_nr_active; |
1537663f | 5452 | |
40c17f75 | 5453 | if (wq_online && init_rescuer(wq) < 0) |
983c7515 | 5454 | goto err_destroy; |
3af24433 | 5455 | |
226223ab TH |
5456 | if ((wq->flags & WQ_SYSFS) && workqueue_sysfs_register(wq)) |
5457 | goto err_destroy; | |
5458 | ||
a0a1a5fd | 5459 | /* |
68e13a67 LJ |
5460 | * wq_pool_mutex protects global freeze state and workqueues list. |
5461 | * Grab it, adjust max_active and add the new @wq to workqueues | |
5462 | * list. | |
a0a1a5fd | 5463 | */ |
68e13a67 | 5464 | mutex_lock(&wq_pool_mutex); |
a0a1a5fd | 5465 | |
a357fc03 | 5466 | mutex_lock(&wq->mutex); |
a045a272 | 5467 | wq_adjust_max_active(wq); |
a357fc03 | 5468 | mutex_unlock(&wq->mutex); |
a0a1a5fd | 5469 | |
e2dca7ad | 5470 | list_add_tail_rcu(&wq->list, &workqueues); |
a0a1a5fd | 5471 | |
68e13a67 | 5472 | mutex_unlock(&wq_pool_mutex); |
1537663f | 5473 | |
3af24433 | 5474 | return wq; |
d2c1d404 | 5475 | |
91ccc6e7 TH |
5476 | err_free_node_nr_active: |
5477 | if (wq->flags & WQ_UNBOUND) | |
5478 | free_node_nr_active(wq->node_nr_active); | |
82efcab3 | 5479 | err_unreg_lockdep: |
009bb421 BVA |
5480 | wq_unregister_lockdep(wq); |
5481 | wq_free_lockdep(wq); | |
82efcab3 | 5482 | err_free_wq: |
6029a918 | 5483 | free_workqueue_attrs(wq->unbound_attrs); |
d2c1d404 TH |
5484 | kfree(wq); |
5485 | return NULL; | |
5486 | err_destroy: | |
5487 | destroy_workqueue(wq); | |
4690c4ab | 5488 | return NULL; |
3af24433 | 5489 | } |
669de8bd | 5490 | EXPORT_SYMBOL_GPL(alloc_workqueue); |
1da177e4 | 5491 | |
c29eb853 TH |
5492 | static bool pwq_busy(struct pool_workqueue *pwq) |
5493 | { | |
5494 | int i; | |
5495 | ||
5496 | for (i = 0; i < WORK_NR_COLORS; i++) | |
5497 | if (pwq->nr_in_flight[i]) | |
5498 | return true; | |
5499 | ||
9f66cff2 | 5500 | if ((pwq != rcu_access_pointer(pwq->wq->dfl_pwq)) && (pwq->refcnt > 1)) |
c29eb853 | 5501 | return true; |
afa87ce8 | 5502 | if (!pwq_is_empty(pwq)) |
c29eb853 TH |
5503 | return true; |
5504 | ||
5505 | return false; | |
5506 | } | |
5507 | ||
3af24433 ON |
5508 | /** |
5509 | * destroy_workqueue - safely terminate a workqueue | |
5510 | * @wq: target workqueue | |
5511 | * | |
5512 | * Safely destroy a workqueue. All work currently pending will be done first. | |
5513 | */ | |
5514 | void destroy_workqueue(struct workqueue_struct *wq) | |
5515 | { | |
49e3cf44 | 5516 | struct pool_workqueue *pwq; |
636b927e | 5517 | int cpu; |
3af24433 | 5518 | |
def98c84 TH |
5519 | /* |
5520 | * Remove it from sysfs first so that sanity check failure doesn't | |
5521 | * lead to sysfs name conflicts. | |
5522 | */ | |
5523 | workqueue_sysfs_unregister(wq); | |
5524 | ||
33e3f0a3 RC |
5525 | /* mark the workqueue destruction is in progress */ |
5526 | mutex_lock(&wq->mutex); | |
5527 | wq->flags |= __WQ_DESTROYING; | |
5528 | mutex_unlock(&wq->mutex); | |
5529 | ||
9c5a2ba7 TH |
5530 | /* drain it before proceeding with destruction */ |
5531 | drain_workqueue(wq); | |
c8efcc25 | 5532 | |
def98c84 TH |
5533 | /* kill rescuer, if sanity checks fail, leave it w/o rescuer */ |
5534 | if (wq->rescuer) { | |
5535 | struct worker *rescuer = wq->rescuer; | |
5536 | ||
5537 | /* this prevents new queueing */ | |
a9b8a985 | 5538 | raw_spin_lock_irq(&wq_mayday_lock); |
def98c84 | 5539 | wq->rescuer = NULL; |
a9b8a985 | 5540 | raw_spin_unlock_irq(&wq_mayday_lock); |
def98c84 TH |
5541 | |
5542 | /* rescuer will empty maydays list before exiting */ | |
5543 | kthread_stop(rescuer->task); | |
8efe1223 | 5544 | kfree(rescuer); |
def98c84 TH |
5545 | } |
5546 | ||
c29eb853 TH |
5547 | /* |
5548 | * Sanity checks - grab all the locks so that we wait for all | |
5549 | * in-flight operations which may do put_pwq(). | |
5550 | */ | |
5551 | mutex_lock(&wq_pool_mutex); | |
b09f4fd3 | 5552 | mutex_lock(&wq->mutex); |
49e3cf44 | 5553 | for_each_pwq(pwq, wq) { |
a9b8a985 | 5554 | raw_spin_lock_irq(&pwq->pool->lock); |
c29eb853 | 5555 | if (WARN_ON(pwq_busy(pwq))) { |
1d9a6159 KW |
5556 | pr_warn("%s: %s has the following busy pwq\n", |
5557 | __func__, wq->name); | |
c29eb853 | 5558 | show_pwq(pwq); |
a9b8a985 | 5559 | raw_spin_unlock_irq(&pwq->pool->lock); |
b09f4fd3 | 5560 | mutex_unlock(&wq->mutex); |
c29eb853 | 5561 | mutex_unlock(&wq_pool_mutex); |
55df0933 | 5562 | show_one_workqueue(wq); |
6183c009 | 5563 | return; |
76af4d93 | 5564 | } |
a9b8a985 | 5565 | raw_spin_unlock_irq(&pwq->pool->lock); |
6183c009 | 5566 | } |
b09f4fd3 | 5567 | mutex_unlock(&wq->mutex); |
6183c009 | 5568 | |
a0a1a5fd TH |
5569 | /* |
5570 | * wq list is used to freeze wq, remove from list after | |
5571 | * flushing is complete in case freeze races us. | |
5572 | */ | |
e2dca7ad | 5573 | list_del_rcu(&wq->list); |
68e13a67 | 5574 | mutex_unlock(&wq_pool_mutex); |
3af24433 | 5575 | |
636b927e TH |
5576 | /* |
5577 | * We're the sole accessor of @wq. Directly access cpu_pwq and dfl_pwq | |
5578 | * to put the base refs. @wq will be auto-destroyed from the last | |
5579 | * pwq_put. RCU read lock prevents @wq from going away from under us. | |
5580 | */ | |
5581 | rcu_read_lock(); | |
4c16bd32 | 5582 | |
636b927e | 5583 | for_each_possible_cpu(cpu) { |
9f66cff2 TH |
5584 | put_pwq_unlocked(unbound_pwq(wq, cpu)); |
5585 | RCU_INIT_POINTER(*unbound_pwq_slot(wq, cpu), NULL); | |
29c91e99 | 5586 | } |
636b927e | 5587 | |
9f66cff2 TH |
5588 | put_pwq_unlocked(unbound_pwq(wq, -1)); |
5589 | RCU_INIT_POINTER(*unbound_pwq_slot(wq, -1), NULL); | |
636b927e TH |
5590 | |
5591 | rcu_read_unlock(); | |
3af24433 ON |
5592 | } |
5593 | EXPORT_SYMBOL_GPL(destroy_workqueue); | |
5594 | ||
dcd989cb TH |
5595 | /** |
5596 | * workqueue_set_max_active - adjust max_active of a workqueue | |
5597 | * @wq: target workqueue | |
5598 | * @max_active: new max_active value. | |
5599 | * | |
5797b1c1 TH |
5600 | * Set max_active of @wq to @max_active. See the alloc_workqueue() function |
5601 | * comment. | |
dcd989cb TH |
5602 | * |
5603 | * CONTEXT: | |
5604 | * Don't call from IRQ context. | |
5605 | */ | |
5606 | void workqueue_set_max_active(struct workqueue_struct *wq, int max_active) | |
5607 | { | |
4cb1ef64 TH |
5608 | /* max_active doesn't mean anything for BH workqueues */ |
5609 | if (WARN_ON(wq->flags & WQ_BH)) | |
5610 | return; | |
8719dcea | 5611 | /* disallow meddling with max_active for ordered workqueues */ |
3bc1e711 | 5612 | if (WARN_ON(wq->flags & __WQ_ORDERED)) |
8719dcea TH |
5613 | return; |
5614 | ||
f3421797 | 5615 | max_active = wq_clamp_max_active(max_active, wq->flags, wq->name); |
dcd989cb | 5616 | |
a357fc03 | 5617 | mutex_lock(&wq->mutex); |
dcd989cb TH |
5618 | |
5619 | wq->saved_max_active = max_active; | |
5797b1c1 TH |
5620 | if (wq->flags & WQ_UNBOUND) |
5621 | wq->saved_min_active = min(wq->saved_min_active, max_active); | |
5622 | ||
a045a272 | 5623 | wq_adjust_max_active(wq); |
93981800 | 5624 | |
a357fc03 | 5625 | mutex_unlock(&wq->mutex); |
15316ba8 | 5626 | } |
dcd989cb | 5627 | EXPORT_SYMBOL_GPL(workqueue_set_max_active); |
15316ba8 | 5628 | |
27d4ee03 LW |
5629 | /** |
5630 | * current_work - retrieve %current task's work struct | |
5631 | * | |
5632 | * Determine if %current task is a workqueue worker and what it's working on. | |
5633 | * Useful to find out the context that the %current task is running in. | |
5634 | * | |
5635 | * Return: work struct if %current task is a workqueue worker, %NULL otherwise. | |
5636 | */ | |
5637 | struct work_struct *current_work(void) | |
5638 | { | |
5639 | struct worker *worker = current_wq_worker(); | |
5640 | ||
5641 | return worker ? worker->current_work : NULL; | |
5642 | } | |
5643 | EXPORT_SYMBOL(current_work); | |
5644 | ||
e6267616 TH |
5645 | /** |
5646 | * current_is_workqueue_rescuer - is %current workqueue rescuer? | |
5647 | * | |
5648 | * Determine whether %current is a workqueue rescuer. Can be used from | |
5649 | * work functions to determine whether it's being run off the rescuer task. | |
d185af30 YB |
5650 | * |
5651 | * Return: %true if %current is a workqueue rescuer. %false otherwise. | |
e6267616 TH |
5652 | */ |
5653 | bool current_is_workqueue_rescuer(void) | |
5654 | { | |
5655 | struct worker *worker = current_wq_worker(); | |
5656 | ||
6a092dfd | 5657 | return worker && worker->rescue_wq; |
e6267616 TH |
5658 | } |
5659 | ||
eef6a7d5 | 5660 | /** |
dcd989cb TH |
5661 | * workqueue_congested - test whether a workqueue is congested |
5662 | * @cpu: CPU in question | |
5663 | * @wq: target workqueue | |
eef6a7d5 | 5664 | * |
dcd989cb TH |
5665 | * Test whether @wq's cpu workqueue for @cpu is congested. There is |
5666 | * no synchronization around this function and the test result is | |
5667 | * unreliable and only useful as advisory hints or for debugging. | |
eef6a7d5 | 5668 | * |
d3251859 | 5669 | * If @cpu is WORK_CPU_UNBOUND, the test is performed on the local CPU. |
636b927e TH |
5670 | * |
5671 | * With the exception of ordered workqueues, all workqueues have per-cpu | |
5672 | * pool_workqueues, each with its own congested state. A workqueue being | |
5673 | * congested on one CPU doesn't mean that the workqueue is contested on any | |
5674 | * other CPUs. | |
d3251859 | 5675 | * |
d185af30 | 5676 | * Return: |
dcd989cb | 5677 | * %true if congested, %false otherwise. |
eef6a7d5 | 5678 | */ |
d84ff051 | 5679 | bool workqueue_congested(int cpu, struct workqueue_struct *wq) |
1da177e4 | 5680 | { |
7fb98ea7 | 5681 | struct pool_workqueue *pwq; |
76af4d93 TH |
5682 | bool ret; |
5683 | ||
24acfb71 TG |
5684 | rcu_read_lock(); |
5685 | preempt_disable(); | |
7fb98ea7 | 5686 | |
d3251859 TH |
5687 | if (cpu == WORK_CPU_UNBOUND) |
5688 | cpu = smp_processor_id(); | |
5689 | ||
636b927e | 5690 | pwq = *per_cpu_ptr(wq->cpu_pwq, cpu); |
f97a4a1a | 5691 | ret = !list_empty(&pwq->inactive_works); |
636b927e | 5692 | |
24acfb71 TG |
5693 | preempt_enable(); |
5694 | rcu_read_unlock(); | |
76af4d93 TH |
5695 | |
5696 | return ret; | |
1da177e4 | 5697 | } |
dcd989cb | 5698 | EXPORT_SYMBOL_GPL(workqueue_congested); |
1da177e4 | 5699 | |
dcd989cb TH |
5700 | /** |
5701 | * work_busy - test whether a work is currently pending or running | |
5702 | * @work: the work to be tested | |
5703 | * | |
5704 | * Test whether @work is currently pending or running. There is no | |
5705 | * synchronization around this function and the test result is | |
5706 | * unreliable and only useful as advisory hints or for debugging. | |
dcd989cb | 5707 | * |
d185af30 | 5708 | * Return: |
dcd989cb TH |
5709 | * OR'd bitmask of WORK_BUSY_* bits. |
5710 | */ | |
5711 | unsigned int work_busy(struct work_struct *work) | |
1da177e4 | 5712 | { |
fa1b54e6 | 5713 | struct worker_pool *pool; |
dcd989cb TH |
5714 | unsigned long flags; |
5715 | unsigned int ret = 0; | |
1da177e4 | 5716 | |
dcd989cb TH |
5717 | if (work_pending(work)) |
5718 | ret |= WORK_BUSY_PENDING; | |
1da177e4 | 5719 | |
24acfb71 | 5720 | rcu_read_lock(); |
fa1b54e6 | 5721 | pool = get_work_pool(work); |
038366c5 | 5722 | if (pool) { |
a9b8a985 | 5723 | raw_spin_lock_irqsave(&pool->lock, flags); |
038366c5 LJ |
5724 | if (find_worker_executing_work(pool, work)) |
5725 | ret |= WORK_BUSY_RUNNING; | |
a9b8a985 | 5726 | raw_spin_unlock_irqrestore(&pool->lock, flags); |
038366c5 | 5727 | } |
24acfb71 | 5728 | rcu_read_unlock(); |
1da177e4 | 5729 | |
dcd989cb | 5730 | return ret; |
1da177e4 | 5731 | } |
dcd989cb | 5732 | EXPORT_SYMBOL_GPL(work_busy); |
1da177e4 | 5733 | |
3d1cb205 TH |
5734 | /** |
5735 | * set_worker_desc - set description for the current work item | |
5736 | * @fmt: printf-style format string | |
5737 | * @...: arguments for the format string | |
5738 | * | |
5739 | * This function can be called by a running work function to describe what | |
5740 | * the work item is about. If the worker task gets dumped, this | |
5741 | * information will be printed out together to help debugging. The | |
5742 | * description can be at most WORKER_DESC_LEN including the trailing '\0'. | |
5743 | */ | |
5744 | void set_worker_desc(const char *fmt, ...) | |
5745 | { | |
5746 | struct worker *worker = current_wq_worker(); | |
5747 | va_list args; | |
5748 | ||
5749 | if (worker) { | |
5750 | va_start(args, fmt); | |
5751 | vsnprintf(worker->desc, sizeof(worker->desc), fmt, args); | |
5752 | va_end(args); | |
3d1cb205 TH |
5753 | } |
5754 | } | |
5c750d58 | 5755 | EXPORT_SYMBOL_GPL(set_worker_desc); |
3d1cb205 TH |
5756 | |
5757 | /** | |
5758 | * print_worker_info - print out worker information and description | |
5759 | * @log_lvl: the log level to use when printing | |
5760 | * @task: target task | |
5761 | * | |
5762 | * If @task is a worker and currently executing a work item, print out the | |
5763 | * name of the workqueue being serviced and worker description set with | |
5764 | * set_worker_desc() by the currently executing work item. | |
5765 | * | |
5766 | * This function can be safely called on any task as long as the | |
5767 | * task_struct itself is accessible. While safe, this function isn't | |
5768 | * synchronized and may print out mixups or garbages of limited length. | |
5769 | */ | |
5770 | void print_worker_info(const char *log_lvl, struct task_struct *task) | |
5771 | { | |
5772 | work_func_t *fn = NULL; | |
5773 | char name[WQ_NAME_LEN] = { }; | |
5774 | char desc[WORKER_DESC_LEN] = { }; | |
5775 | struct pool_workqueue *pwq = NULL; | |
5776 | struct workqueue_struct *wq = NULL; | |
3d1cb205 TH |
5777 | struct worker *worker; |
5778 | ||
5779 | if (!(task->flags & PF_WQ_WORKER)) | |
5780 | return; | |
5781 | ||
5782 | /* | |
5783 | * This function is called without any synchronization and @task | |
5784 | * could be in any state. Be careful with dereferences. | |
5785 | */ | |
e700591a | 5786 | worker = kthread_probe_data(task); |
3d1cb205 TH |
5787 | |
5788 | /* | |
8bf89593 TH |
5789 | * Carefully copy the associated workqueue's workfn, name and desc. |
5790 | * Keep the original last '\0' in case the original is garbage. | |
3d1cb205 | 5791 | */ |
fe557319 CH |
5792 | copy_from_kernel_nofault(&fn, &worker->current_func, sizeof(fn)); |
5793 | copy_from_kernel_nofault(&pwq, &worker->current_pwq, sizeof(pwq)); | |
5794 | copy_from_kernel_nofault(&wq, &pwq->wq, sizeof(wq)); | |
5795 | copy_from_kernel_nofault(name, wq->name, sizeof(name) - 1); | |
5796 | copy_from_kernel_nofault(desc, worker->desc, sizeof(desc) - 1); | |
3d1cb205 TH |
5797 | |
5798 | if (fn || name[0] || desc[0]) { | |
d75f773c | 5799 | printk("%sWorkqueue: %s %ps", log_lvl, name, fn); |
8bf89593 | 5800 | if (strcmp(name, desc)) |
3d1cb205 TH |
5801 | pr_cont(" (%s)", desc); |
5802 | pr_cont("\n"); | |
5803 | } | |
5804 | } | |
5805 | ||
3494fc30 TH |
5806 | static void pr_cont_pool_info(struct worker_pool *pool) |
5807 | { | |
5808 | pr_cont(" cpus=%*pbl", nr_cpumask_bits, pool->attrs->cpumask); | |
5809 | if (pool->node != NUMA_NO_NODE) | |
5810 | pr_cont(" node=%d", pool->node); | |
4cb1ef64 TH |
5811 | pr_cont(" flags=0x%x", pool->flags); |
5812 | if (pool->flags & POOL_BH) | |
5813 | pr_cont(" bh%s", | |
5814 | pool->attrs->nice == HIGHPRI_NICE_LEVEL ? "-hi" : ""); | |
5815 | else | |
5816 | pr_cont(" nice=%d", pool->attrs->nice); | |
5817 | } | |
5818 | ||
5819 | static void pr_cont_worker_id(struct worker *worker) | |
5820 | { | |
5821 | struct worker_pool *pool = worker->pool; | |
5822 | ||
5823 | if (pool->flags & WQ_BH) | |
5824 | pr_cont("bh%s", | |
5825 | pool->attrs->nice == HIGHPRI_NICE_LEVEL ? "-hi" : ""); | |
5826 | else | |
5827 | pr_cont("%d%s", task_pid_nr(worker->task), | |
5828 | worker->rescue_wq ? "(RESCUER)" : ""); | |
3494fc30 TH |
5829 | } |
5830 | ||
c76feb0d PM |
5831 | struct pr_cont_work_struct { |
5832 | bool comma; | |
5833 | work_func_t func; | |
5834 | long ctr; | |
5835 | }; | |
5836 | ||
5837 | static void pr_cont_work_flush(bool comma, work_func_t func, struct pr_cont_work_struct *pcwsp) | |
5838 | { | |
5839 | if (!pcwsp->ctr) | |
5840 | goto out_record; | |
5841 | if (func == pcwsp->func) { | |
5842 | pcwsp->ctr++; | |
5843 | return; | |
5844 | } | |
5845 | if (pcwsp->ctr == 1) | |
5846 | pr_cont("%s %ps", pcwsp->comma ? "," : "", pcwsp->func); | |
5847 | else | |
5848 | pr_cont("%s %ld*%ps", pcwsp->comma ? "," : "", pcwsp->ctr, pcwsp->func); | |
5849 | pcwsp->ctr = 0; | |
5850 | out_record: | |
5851 | if ((long)func == -1L) | |
5852 | return; | |
5853 | pcwsp->comma = comma; | |
5854 | pcwsp->func = func; | |
5855 | pcwsp->ctr = 1; | |
5856 | } | |
5857 | ||
5858 | static void pr_cont_work(bool comma, struct work_struct *work, struct pr_cont_work_struct *pcwsp) | |
3494fc30 TH |
5859 | { |
5860 | if (work->func == wq_barrier_func) { | |
5861 | struct wq_barrier *barr; | |
5862 | ||
5863 | barr = container_of(work, struct wq_barrier, work); | |
5864 | ||
c76feb0d | 5865 | pr_cont_work_flush(comma, (work_func_t)-1, pcwsp); |
3494fc30 TH |
5866 | pr_cont("%s BAR(%d)", comma ? "," : "", |
5867 | task_pid_nr(barr->task)); | |
5868 | } else { | |
c76feb0d PM |
5869 | if (!comma) |
5870 | pr_cont_work_flush(comma, (work_func_t)-1, pcwsp); | |
5871 | pr_cont_work_flush(comma, work->func, pcwsp); | |
3494fc30 TH |
5872 | } |
5873 | } | |
5874 | ||
5875 | static void show_pwq(struct pool_workqueue *pwq) | |
5876 | { | |
c76feb0d | 5877 | struct pr_cont_work_struct pcws = { .ctr = 0, }; |
3494fc30 TH |
5878 | struct worker_pool *pool = pwq->pool; |
5879 | struct work_struct *work; | |
5880 | struct worker *worker; | |
5881 | bool has_in_flight = false, has_pending = false; | |
5882 | int bkt; | |
5883 | ||
5884 | pr_info(" pwq %d:", pool->id); | |
5885 | pr_cont_pool_info(pool); | |
5886 | ||
a045a272 TH |
5887 | pr_cont(" active=%d refcnt=%d%s\n", |
5888 | pwq->nr_active, pwq->refcnt, | |
3494fc30 TH |
5889 | !list_empty(&pwq->mayday_node) ? " MAYDAY" : ""); |
5890 | ||
5891 | hash_for_each(pool->busy_hash, bkt, worker, hentry) { | |
5892 | if (worker->current_pwq == pwq) { | |
5893 | has_in_flight = true; | |
5894 | break; | |
5895 | } | |
5896 | } | |
5897 | if (has_in_flight) { | |
5898 | bool comma = false; | |
5899 | ||
5900 | pr_info(" in-flight:"); | |
5901 | hash_for_each(pool->busy_hash, bkt, worker, hentry) { | |
5902 | if (worker->current_pwq != pwq) | |
5903 | continue; | |
5904 | ||
4cb1ef64 TH |
5905 | pr_cont(" %s", comma ? "," : ""); |
5906 | pr_cont_worker_id(worker); | |
5907 | pr_cont(":%ps", worker->current_func); | |
3494fc30 | 5908 | list_for_each_entry(work, &worker->scheduled, entry) |
c76feb0d PM |
5909 | pr_cont_work(false, work, &pcws); |
5910 | pr_cont_work_flush(comma, (work_func_t)-1L, &pcws); | |
3494fc30 TH |
5911 | comma = true; |
5912 | } | |
5913 | pr_cont("\n"); | |
5914 | } | |
5915 | ||
5916 | list_for_each_entry(work, &pool->worklist, entry) { | |
5917 | if (get_work_pwq(work) == pwq) { | |
5918 | has_pending = true; | |
5919 | break; | |
5920 | } | |
5921 | } | |
5922 | if (has_pending) { | |
5923 | bool comma = false; | |
5924 | ||
5925 | pr_info(" pending:"); | |
5926 | list_for_each_entry(work, &pool->worklist, entry) { | |
5927 | if (get_work_pwq(work) != pwq) | |
5928 | continue; | |
5929 | ||
c76feb0d | 5930 | pr_cont_work(comma, work, &pcws); |
3494fc30 TH |
5931 | comma = !(*work_data_bits(work) & WORK_STRUCT_LINKED); |
5932 | } | |
c76feb0d | 5933 | pr_cont_work_flush(comma, (work_func_t)-1L, &pcws); |
3494fc30 TH |
5934 | pr_cont("\n"); |
5935 | } | |
5936 | ||
f97a4a1a | 5937 | if (!list_empty(&pwq->inactive_works)) { |
3494fc30 TH |
5938 | bool comma = false; |
5939 | ||
f97a4a1a LJ |
5940 | pr_info(" inactive:"); |
5941 | list_for_each_entry(work, &pwq->inactive_works, entry) { | |
c76feb0d | 5942 | pr_cont_work(comma, work, &pcws); |
3494fc30 TH |
5943 | comma = !(*work_data_bits(work) & WORK_STRUCT_LINKED); |
5944 | } | |
c76feb0d | 5945 | pr_cont_work_flush(comma, (work_func_t)-1L, &pcws); |
3494fc30 TH |
5946 | pr_cont("\n"); |
5947 | } | |
5948 | } | |
5949 | ||
5950 | /** | |
55df0933 IK |
5951 | * show_one_workqueue - dump state of specified workqueue |
5952 | * @wq: workqueue whose state will be printed | |
3494fc30 | 5953 | */ |
55df0933 | 5954 | void show_one_workqueue(struct workqueue_struct *wq) |
3494fc30 | 5955 | { |
55df0933 IK |
5956 | struct pool_workqueue *pwq; |
5957 | bool idle = true; | |
3494fc30 | 5958 | unsigned long flags; |
3494fc30 | 5959 | |
55df0933 | 5960 | for_each_pwq(pwq, wq) { |
afa87ce8 | 5961 | if (!pwq_is_empty(pwq)) { |
55df0933 IK |
5962 | idle = false; |
5963 | break; | |
3494fc30 | 5964 | } |
55df0933 IK |
5965 | } |
5966 | if (idle) /* Nothing to print for idle workqueue */ | |
5967 | return; | |
3494fc30 | 5968 | |
55df0933 | 5969 | pr_info("workqueue %s: flags=0x%x\n", wq->name, wq->flags); |
3494fc30 | 5970 | |
55df0933 IK |
5971 | for_each_pwq(pwq, wq) { |
5972 | raw_spin_lock_irqsave(&pwq->pool->lock, flags); | |
afa87ce8 | 5973 | if (!pwq_is_empty(pwq)) { |
62635ea8 | 5974 | /* |
55df0933 IK |
5975 | * Defer printing to avoid deadlocks in console |
5976 | * drivers that queue work while holding locks | |
5977 | * also taken in their write paths. | |
62635ea8 | 5978 | */ |
55df0933 IK |
5979 | printk_deferred_enter(); |
5980 | show_pwq(pwq); | |
5981 | printk_deferred_exit(); | |
3494fc30 | 5982 | } |
55df0933 | 5983 | raw_spin_unlock_irqrestore(&pwq->pool->lock, flags); |
62635ea8 SS |
5984 | /* |
5985 | * We could be printing a lot from atomic context, e.g. | |
55df0933 | 5986 | * sysrq-t -> show_all_workqueues(). Avoid triggering |
62635ea8 SS |
5987 | * hard lockup. |
5988 | */ | |
5989 | touch_nmi_watchdog(); | |
3494fc30 TH |
5990 | } |
5991 | ||
55df0933 IK |
5992 | } |
5993 | ||
5994 | /** | |
5995 | * show_one_worker_pool - dump state of specified worker pool | |
5996 | * @pool: worker pool whose state will be printed | |
5997 | */ | |
5998 | static void show_one_worker_pool(struct worker_pool *pool) | |
5999 | { | |
6000 | struct worker *worker; | |
6001 | bool first = true; | |
6002 | unsigned long flags; | |
335a42eb | 6003 | unsigned long hung = 0; |
55df0933 IK |
6004 | |
6005 | raw_spin_lock_irqsave(&pool->lock, flags); | |
6006 | if (pool->nr_workers == pool->nr_idle) | |
6007 | goto next_pool; | |
335a42eb PM |
6008 | |
6009 | /* How long the first pending work is waiting for a worker. */ | |
6010 | if (!list_empty(&pool->worklist)) | |
6011 | hung = jiffies_to_msecs(jiffies - pool->watchdog_ts) / 1000; | |
6012 | ||
55df0933 IK |
6013 | /* |
6014 | * Defer printing to avoid deadlocks in console drivers that | |
6015 | * queue work while holding locks also taken in their write | |
6016 | * paths. | |
6017 | */ | |
6018 | printk_deferred_enter(); | |
6019 | pr_info("pool %d:", pool->id); | |
6020 | pr_cont_pool_info(pool); | |
335a42eb | 6021 | pr_cont(" hung=%lus workers=%d", hung, pool->nr_workers); |
55df0933 IK |
6022 | if (pool->manager) |
6023 | pr_cont(" manager: %d", | |
6024 | task_pid_nr(pool->manager->task)); | |
6025 | list_for_each_entry(worker, &pool->idle_list, entry) { | |
4cb1ef64 TH |
6026 | pr_cont(" %s", first ? "idle: " : ""); |
6027 | pr_cont_worker_id(worker); | |
55df0933 IK |
6028 | first = false; |
6029 | } | |
6030 | pr_cont("\n"); | |
6031 | printk_deferred_exit(); | |
6032 | next_pool: | |
6033 | raw_spin_unlock_irqrestore(&pool->lock, flags); | |
6034 | /* | |
6035 | * We could be printing a lot from atomic context, e.g. | |
6036 | * sysrq-t -> show_all_workqueues(). Avoid triggering | |
6037 | * hard lockup. | |
6038 | */ | |
6039 | touch_nmi_watchdog(); | |
6040 | ||
6041 | } | |
6042 | ||
6043 | /** | |
6044 | * show_all_workqueues - dump workqueue state | |
6045 | * | |
704bc669 | 6046 | * Called from a sysrq handler and prints out all busy workqueues and pools. |
55df0933 IK |
6047 | */ |
6048 | void show_all_workqueues(void) | |
6049 | { | |
6050 | struct workqueue_struct *wq; | |
6051 | struct worker_pool *pool; | |
6052 | int pi; | |
6053 | ||
6054 | rcu_read_lock(); | |
6055 | ||
6056 | pr_info("Showing busy workqueues and worker pools:\n"); | |
6057 | ||
6058 | list_for_each_entry_rcu(wq, &workqueues, list) | |
6059 | show_one_workqueue(wq); | |
6060 | ||
6061 | for_each_pool(pool, pi) | |
6062 | show_one_worker_pool(pool); | |
6063 | ||
24acfb71 | 6064 | rcu_read_unlock(); |
3494fc30 TH |
6065 | } |
6066 | ||
704bc669 JL |
6067 | /** |
6068 | * show_freezable_workqueues - dump freezable workqueue state | |
6069 | * | |
6070 | * Called from try_to_freeze_tasks() and prints out all freezable workqueues | |
6071 | * still busy. | |
6072 | */ | |
6073 | void show_freezable_workqueues(void) | |
6074 | { | |
6075 | struct workqueue_struct *wq; | |
6076 | ||
6077 | rcu_read_lock(); | |
6078 | ||
6079 | pr_info("Showing freezable workqueues that are still busy:\n"); | |
6080 | ||
6081 | list_for_each_entry_rcu(wq, &workqueues, list) { | |
6082 | if (!(wq->flags & WQ_FREEZABLE)) | |
6083 | continue; | |
6084 | show_one_workqueue(wq); | |
6085 | } | |
6086 | ||
6087 | rcu_read_unlock(); | |
6088 | } | |
6089 | ||
6b59808b TH |
6090 | /* used to show worker information through /proc/PID/{comm,stat,status} */ |
6091 | void wq_worker_comm(char *buf, size_t size, struct task_struct *task) | |
6092 | { | |
6b59808b TH |
6093 | int off; |
6094 | ||
6095 | /* always show the actual comm */ | |
6096 | off = strscpy(buf, task->comm, size); | |
6097 | if (off < 0) | |
6098 | return; | |
6099 | ||
197f6acc | 6100 | /* stabilize PF_WQ_WORKER and worker pool association */ |
6b59808b TH |
6101 | mutex_lock(&wq_pool_attach_mutex); |
6102 | ||
197f6acc TH |
6103 | if (task->flags & PF_WQ_WORKER) { |
6104 | struct worker *worker = kthread_data(task); | |
6105 | struct worker_pool *pool = worker->pool; | |
6b59808b | 6106 | |
197f6acc | 6107 | if (pool) { |
a9b8a985 | 6108 | raw_spin_lock_irq(&pool->lock); |
197f6acc TH |
6109 | /* |
6110 | * ->desc tracks information (wq name or | |
6111 | * set_worker_desc()) for the latest execution. If | |
6112 | * current, prepend '+', otherwise '-'. | |
6113 | */ | |
6114 | if (worker->desc[0] != '\0') { | |
6115 | if (worker->current_work) | |
6116 | scnprintf(buf + off, size - off, "+%s", | |
6117 | worker->desc); | |
6118 | else | |
6119 | scnprintf(buf + off, size - off, "-%s", | |
6120 | worker->desc); | |
6121 | } | |
a9b8a985 | 6122 | raw_spin_unlock_irq(&pool->lock); |
6b59808b | 6123 | } |
6b59808b TH |
6124 | } |
6125 | ||
6126 | mutex_unlock(&wq_pool_attach_mutex); | |
6127 | } | |
6128 | ||
66448bc2 MM |
6129 | #ifdef CONFIG_SMP |
6130 | ||
db7bccf4 TH |
6131 | /* |
6132 | * CPU hotplug. | |
6133 | * | |
e22bee78 | 6134 | * There are two challenges in supporting CPU hotplug. Firstly, there |
112202d9 | 6135 | * are a lot of assumptions on strong associations among work, pwq and |
706026c2 | 6136 | * pool which make migrating pending and scheduled works very |
e22bee78 | 6137 | * difficult to implement without impacting hot paths. Secondly, |
94cf58bb | 6138 | * worker pools serve mix of short, long and very long running works making |
e22bee78 TH |
6139 | * blocked draining impractical. |
6140 | * | |
24647570 | 6141 | * This is solved by allowing the pools to be disassociated from the CPU |
628c78e7 TH |
6142 | * running as an unbound one and allowing it to be reattached later if the |
6143 | * cpu comes back online. | |
db7bccf4 | 6144 | */ |
1da177e4 | 6145 | |
e8b3f8db | 6146 | static void unbind_workers(int cpu) |
3af24433 | 6147 | { |
4ce62e9e | 6148 | struct worker_pool *pool; |
db7bccf4 | 6149 | struct worker *worker; |
3af24433 | 6150 | |
f02ae73a | 6151 | for_each_cpu_worker_pool(pool, cpu) { |
1258fae7 | 6152 | mutex_lock(&wq_pool_attach_mutex); |
a9b8a985 | 6153 | raw_spin_lock_irq(&pool->lock); |
3af24433 | 6154 | |
94cf58bb | 6155 | /* |
92f9c5c4 | 6156 | * We've blocked all attach/detach operations. Make all workers |
94cf58bb | 6157 | * unbound and set DISASSOCIATED. Before this, all workers |
11b45b0b | 6158 | * must be on the cpu. After this, they may become diasporas. |
b4ac9384 LJ |
6159 | * And the preemption disabled section in their sched callbacks |
6160 | * are guaranteed to see WORKER_UNBOUND since the code here | |
6161 | * is on the same cpu. | |
94cf58bb | 6162 | */ |
da028469 | 6163 | for_each_pool_worker(worker, pool) |
c9e7cf27 | 6164 | worker->flags |= WORKER_UNBOUND; |
06ba38a9 | 6165 | |
24647570 | 6166 | pool->flags |= POOL_DISASSOCIATED; |
f2d5a0ee | 6167 | |
eb283428 | 6168 | /* |
989442d7 LJ |
6169 | * The handling of nr_running in sched callbacks are disabled |
6170 | * now. Zap nr_running. After this, nr_running stays zero and | |
6171 | * need_more_worker() and keep_working() are always true as | |
6172 | * long as the worklist is not empty. This pool now behaves as | |
6173 | * an unbound (in terms of concurrency management) pool which | |
eb283428 LJ |
6174 | * are served by workers tied to the pool. |
6175 | */ | |
bc35f7ef | 6176 | pool->nr_running = 0; |
eb283428 LJ |
6177 | |
6178 | /* | |
6179 | * With concurrency management just turned off, a busy | |
6180 | * worker blocking could lead to lengthy stalls. Kick off | |
6181 | * unbound chain execution of currently pending work items. | |
6182 | */ | |
0219a352 | 6183 | kick_pool(pool); |
989442d7 | 6184 | |
a9b8a985 | 6185 | raw_spin_unlock_irq(&pool->lock); |
989442d7 | 6186 | |
793777bc VS |
6187 | for_each_pool_worker(worker, pool) |
6188 | unbind_worker(worker); | |
989442d7 LJ |
6189 | |
6190 | mutex_unlock(&wq_pool_attach_mutex); | |
eb283428 | 6191 | } |
3af24433 | 6192 | } |
3af24433 | 6193 | |
bd7c089e TH |
6194 | /** |
6195 | * rebind_workers - rebind all workers of a pool to the associated CPU | |
6196 | * @pool: pool of interest | |
6197 | * | |
a9ab775b | 6198 | * @pool->cpu is coming online. Rebind all workers to the CPU. |
bd7c089e TH |
6199 | */ |
6200 | static void rebind_workers(struct worker_pool *pool) | |
6201 | { | |
a9ab775b | 6202 | struct worker *worker; |
bd7c089e | 6203 | |
1258fae7 | 6204 | lockdep_assert_held(&wq_pool_attach_mutex); |
bd7c089e | 6205 | |
a9ab775b TH |
6206 | /* |
6207 | * Restore CPU affinity of all workers. As all idle workers should | |
6208 | * be on the run-queue of the associated CPU before any local | |
402dd89d | 6209 | * wake-ups for concurrency management happen, restore CPU affinity |
a9ab775b TH |
6210 | * of all workers first and then clear UNBOUND. As we're called |
6211 | * from CPU_ONLINE, the following shouldn't fail. | |
6212 | */ | |
c63a2e52 VS |
6213 | for_each_pool_worker(worker, pool) { |
6214 | kthread_set_per_cpu(worker->task, pool->cpu); | |
6215 | WARN_ON_ONCE(set_cpus_allowed_ptr(worker->task, | |
9546b29e | 6216 | pool_allowed_cpus(pool)) < 0); |
c63a2e52 | 6217 | } |
bd7c089e | 6218 | |
a9b8a985 | 6219 | raw_spin_lock_irq(&pool->lock); |
f7c17d26 | 6220 | |
3de5e884 | 6221 | pool->flags &= ~POOL_DISASSOCIATED; |
bd7c089e | 6222 | |
da028469 | 6223 | for_each_pool_worker(worker, pool) { |
a9ab775b | 6224 | unsigned int worker_flags = worker->flags; |
bd7c089e | 6225 | |
a9ab775b TH |
6226 | /* |
6227 | * We want to clear UNBOUND but can't directly call | |
6228 | * worker_clr_flags() or adjust nr_running. Atomically | |
6229 | * replace UNBOUND with another NOT_RUNNING flag REBOUND. | |
6230 | * @worker will clear REBOUND using worker_clr_flags() when | |
6231 | * it initiates the next execution cycle thus restoring | |
6232 | * concurrency management. Note that when or whether | |
6233 | * @worker clears REBOUND doesn't affect correctness. | |
6234 | * | |
c95491ed | 6235 | * WRITE_ONCE() is necessary because @worker->flags may be |
a9ab775b | 6236 | * tested without holding any lock in |
6d25be57 | 6237 | * wq_worker_running(). Without it, NOT_RUNNING test may |
a9ab775b TH |
6238 | * fail incorrectly leading to premature concurrency |
6239 | * management operations. | |
6240 | */ | |
6241 | WARN_ON_ONCE(!(worker_flags & WORKER_UNBOUND)); | |
6242 | worker_flags |= WORKER_REBOUND; | |
6243 | worker_flags &= ~WORKER_UNBOUND; | |
c95491ed | 6244 | WRITE_ONCE(worker->flags, worker_flags); |
bd7c089e | 6245 | } |
a9ab775b | 6246 | |
a9b8a985 | 6247 | raw_spin_unlock_irq(&pool->lock); |
bd7c089e TH |
6248 | } |
6249 | ||
7dbc725e TH |
6250 | /** |
6251 | * restore_unbound_workers_cpumask - restore cpumask of unbound workers | |
6252 | * @pool: unbound pool of interest | |
6253 | * @cpu: the CPU which is coming up | |
6254 | * | |
6255 | * An unbound pool may end up with a cpumask which doesn't have any online | |
6256 | * CPUs. When a worker of such pool get scheduled, the scheduler resets | |
6257 | * its cpus_allowed. If @cpu is in @pool's cpumask which didn't have any | |
6258 | * online CPU before, cpus_allowed of all its workers should be restored. | |
6259 | */ | |
6260 | static void restore_unbound_workers_cpumask(struct worker_pool *pool, int cpu) | |
6261 | { | |
6262 | static cpumask_t cpumask; | |
6263 | struct worker *worker; | |
7dbc725e | 6264 | |
1258fae7 | 6265 | lockdep_assert_held(&wq_pool_attach_mutex); |
7dbc725e TH |
6266 | |
6267 | /* is @cpu allowed for @pool? */ | |
6268 | if (!cpumask_test_cpu(cpu, pool->attrs->cpumask)) | |
6269 | return; | |
6270 | ||
7dbc725e | 6271 | cpumask_and(&cpumask, pool->attrs->cpumask, cpu_online_mask); |
7dbc725e TH |
6272 | |
6273 | /* as we're called from CPU_ONLINE, the following shouldn't fail */ | |
da028469 | 6274 | for_each_pool_worker(worker, pool) |
d945b5e9 | 6275 | WARN_ON_ONCE(set_cpus_allowed_ptr(worker->task, &cpumask) < 0); |
7dbc725e TH |
6276 | } |
6277 | ||
7ee681b2 TG |
6278 | int workqueue_prepare_cpu(unsigned int cpu) |
6279 | { | |
6280 | struct worker_pool *pool; | |
6281 | ||
6282 | for_each_cpu_worker_pool(pool, cpu) { | |
6283 | if (pool->nr_workers) | |
6284 | continue; | |
6285 | if (!create_worker(pool)) | |
6286 | return -ENOMEM; | |
6287 | } | |
6288 | return 0; | |
6289 | } | |
6290 | ||
6291 | int workqueue_online_cpu(unsigned int cpu) | |
3af24433 | 6292 | { |
4ce62e9e | 6293 | struct worker_pool *pool; |
4c16bd32 | 6294 | struct workqueue_struct *wq; |
7dbc725e | 6295 | int pi; |
3ce63377 | 6296 | |
7ee681b2 | 6297 | mutex_lock(&wq_pool_mutex); |
7dbc725e | 6298 | |
7ee681b2 | 6299 | for_each_pool(pool, pi) { |
4cb1ef64 TH |
6300 | /* BH pools aren't affected by hotplug */ |
6301 | if (pool->flags & POOL_BH) | |
6302 | continue; | |
94cf58bb | 6303 | |
4cb1ef64 | 6304 | mutex_lock(&wq_pool_attach_mutex); |
7ee681b2 TG |
6305 | if (pool->cpu == cpu) |
6306 | rebind_workers(pool); | |
6307 | else if (pool->cpu < 0) | |
6308 | restore_unbound_workers_cpumask(pool, cpu); | |
1258fae7 | 6309 | mutex_unlock(&wq_pool_attach_mutex); |
7ee681b2 | 6310 | } |
6ba94429 | 6311 | |
fef59c9c | 6312 | /* update pod affinity of unbound workqueues */ |
4cbfd3de | 6313 | list_for_each_entry(wq, &workqueues, list) { |
84193c07 TH |
6314 | struct workqueue_attrs *attrs = wq->unbound_attrs; |
6315 | ||
6316 | if (attrs) { | |
6317 | const struct wq_pod_type *pt = wqattrs_pod_type(attrs); | |
6318 | int tcpu; | |
4cbfd3de | 6319 | |
84193c07 | 6320 | for_each_cpu(tcpu, pt->pod_cpus[pt->cpu_pod[cpu]]) |
fef59c9c | 6321 | wq_update_pod(wq, tcpu, cpu, true); |
5797b1c1 TH |
6322 | |
6323 | mutex_lock(&wq->mutex); | |
6324 | wq_update_node_max_active(wq, -1); | |
6325 | mutex_unlock(&wq->mutex); | |
4cbfd3de TH |
6326 | } |
6327 | } | |
6ba94429 | 6328 | |
7ee681b2 TG |
6329 | mutex_unlock(&wq_pool_mutex); |
6330 | return 0; | |
6ba94429 FW |
6331 | } |
6332 | ||
7ee681b2 | 6333 | int workqueue_offline_cpu(unsigned int cpu) |
6ba94429 | 6334 | { |
6ba94429 FW |
6335 | struct workqueue_struct *wq; |
6336 | ||
7ee681b2 | 6337 | /* unbinding per-cpu workers should happen on the local CPU */ |
e8b3f8db LJ |
6338 | if (WARN_ON(cpu != smp_processor_id())) |
6339 | return -1; | |
6340 | ||
6341 | unbind_workers(cpu); | |
7ee681b2 | 6342 | |
fef59c9c | 6343 | /* update pod affinity of unbound workqueues */ |
7ee681b2 | 6344 | mutex_lock(&wq_pool_mutex); |
4cbfd3de | 6345 | list_for_each_entry(wq, &workqueues, list) { |
84193c07 TH |
6346 | struct workqueue_attrs *attrs = wq->unbound_attrs; |
6347 | ||
6348 | if (attrs) { | |
6349 | const struct wq_pod_type *pt = wqattrs_pod_type(attrs); | |
6350 | int tcpu; | |
4cbfd3de | 6351 | |
84193c07 | 6352 | for_each_cpu(tcpu, pt->pod_cpus[pt->cpu_pod[cpu]]) |
fef59c9c | 6353 | wq_update_pod(wq, tcpu, cpu, false); |
5797b1c1 TH |
6354 | |
6355 | mutex_lock(&wq->mutex); | |
6356 | wq_update_node_max_active(wq, cpu); | |
6357 | mutex_unlock(&wq->mutex); | |
4cbfd3de TH |
6358 | } |
6359 | } | |
7ee681b2 TG |
6360 | mutex_unlock(&wq_pool_mutex); |
6361 | ||
7ee681b2 | 6362 | return 0; |
6ba94429 FW |
6363 | } |
6364 | ||
6ba94429 FW |
6365 | struct work_for_cpu { |
6366 | struct work_struct work; | |
6367 | long (*fn)(void *); | |
6368 | void *arg; | |
6369 | long ret; | |
6370 | }; | |
6371 | ||
6372 | static void work_for_cpu_fn(struct work_struct *work) | |
6373 | { | |
6374 | struct work_for_cpu *wfc = container_of(work, struct work_for_cpu, work); | |
6375 | ||
6376 | wfc->ret = wfc->fn(wfc->arg); | |
6377 | } | |
6378 | ||
6379 | /** | |
265f3ed0 | 6380 | * work_on_cpu_key - run a function in thread context on a particular cpu |
6ba94429 FW |
6381 | * @cpu: the cpu to run on |
6382 | * @fn: the function to run | |
6383 | * @arg: the function arg | |
265f3ed0 | 6384 | * @key: The lock class key for lock debugging purposes |
6ba94429 FW |
6385 | * |
6386 | * It is up to the caller to ensure that the cpu doesn't go offline. | |
6387 | * The caller must not hold any locks which would prevent @fn from completing. | |
6388 | * | |
6389 | * Return: The value @fn returns. | |
6390 | */ | |
265f3ed0 FW |
6391 | long work_on_cpu_key(int cpu, long (*fn)(void *), |
6392 | void *arg, struct lock_class_key *key) | |
6ba94429 FW |
6393 | { |
6394 | struct work_for_cpu wfc = { .fn = fn, .arg = arg }; | |
6395 | ||
265f3ed0 | 6396 | INIT_WORK_ONSTACK_KEY(&wfc.work, work_for_cpu_fn, key); |
6ba94429 FW |
6397 | schedule_work_on(cpu, &wfc.work); |
6398 | flush_work(&wfc.work); | |
6399 | destroy_work_on_stack(&wfc.work); | |
6400 | return wfc.ret; | |
6401 | } | |
265f3ed0 | 6402 | EXPORT_SYMBOL_GPL(work_on_cpu_key); |
0e8d6a93 TG |
6403 | |
6404 | /** | |
265f3ed0 | 6405 | * work_on_cpu_safe_key - run a function in thread context on a particular cpu |
0e8d6a93 TG |
6406 | * @cpu: the cpu to run on |
6407 | * @fn: the function to run | |
6408 | * @arg: the function argument | |
265f3ed0 | 6409 | * @key: The lock class key for lock debugging purposes |
0e8d6a93 TG |
6410 | * |
6411 | * Disables CPU hotplug and calls work_on_cpu(). The caller must not hold | |
6412 | * any locks which would prevent @fn from completing. | |
6413 | * | |
6414 | * Return: The value @fn returns. | |
6415 | */ | |
265f3ed0 FW |
6416 | long work_on_cpu_safe_key(int cpu, long (*fn)(void *), |
6417 | void *arg, struct lock_class_key *key) | |
0e8d6a93 TG |
6418 | { |
6419 | long ret = -ENODEV; | |
6420 | ||
ffd8bea8 | 6421 | cpus_read_lock(); |
0e8d6a93 | 6422 | if (cpu_online(cpu)) |
265f3ed0 | 6423 | ret = work_on_cpu_key(cpu, fn, arg, key); |
ffd8bea8 | 6424 | cpus_read_unlock(); |
0e8d6a93 TG |
6425 | return ret; |
6426 | } | |
265f3ed0 | 6427 | EXPORT_SYMBOL_GPL(work_on_cpu_safe_key); |
6ba94429 FW |
6428 | #endif /* CONFIG_SMP */ |
6429 | ||
6430 | #ifdef CONFIG_FREEZER | |
6431 | ||
6432 | /** | |
6433 | * freeze_workqueues_begin - begin freezing workqueues | |
6434 | * | |
6435 | * Start freezing workqueues. After this function returns, all freezable | |
f97a4a1a | 6436 | * workqueues will queue new works to their inactive_works list instead of |
6ba94429 FW |
6437 | * pool->worklist. |
6438 | * | |
6439 | * CONTEXT: | |
6440 | * Grabs and releases wq_pool_mutex, wq->mutex and pool->lock's. | |
6441 | */ | |
6442 | void freeze_workqueues_begin(void) | |
6443 | { | |
6444 | struct workqueue_struct *wq; | |
6ba94429 FW |
6445 | |
6446 | mutex_lock(&wq_pool_mutex); | |
6447 | ||
6448 | WARN_ON_ONCE(workqueue_freezing); | |
6449 | workqueue_freezing = true; | |
6450 | ||
6451 | list_for_each_entry(wq, &workqueues, list) { | |
6452 | mutex_lock(&wq->mutex); | |
a045a272 | 6453 | wq_adjust_max_active(wq); |
6ba94429 FW |
6454 | mutex_unlock(&wq->mutex); |
6455 | } | |
6456 | ||
6457 | mutex_unlock(&wq_pool_mutex); | |
6458 | } | |
6459 | ||
6460 | /** | |
6461 | * freeze_workqueues_busy - are freezable workqueues still busy? | |
6462 | * | |
6463 | * Check whether freezing is complete. This function must be called | |
6464 | * between freeze_workqueues_begin() and thaw_workqueues(). | |
6465 | * | |
6466 | * CONTEXT: | |
6467 | * Grabs and releases wq_pool_mutex. | |
6468 | * | |
6469 | * Return: | |
6470 | * %true if some freezable workqueues are still busy. %false if freezing | |
6471 | * is complete. | |
6472 | */ | |
6473 | bool freeze_workqueues_busy(void) | |
6474 | { | |
6475 | bool busy = false; | |
6476 | struct workqueue_struct *wq; | |
6477 | struct pool_workqueue *pwq; | |
6478 | ||
6479 | mutex_lock(&wq_pool_mutex); | |
6480 | ||
6481 | WARN_ON_ONCE(!workqueue_freezing); | |
6482 | ||
6483 | list_for_each_entry(wq, &workqueues, list) { | |
6484 | if (!(wq->flags & WQ_FREEZABLE)) | |
6485 | continue; | |
6486 | /* | |
6487 | * nr_active is monotonically decreasing. It's safe | |
6488 | * to peek without lock. | |
6489 | */ | |
24acfb71 | 6490 | rcu_read_lock(); |
6ba94429 FW |
6491 | for_each_pwq(pwq, wq) { |
6492 | WARN_ON_ONCE(pwq->nr_active < 0); | |
6493 | if (pwq->nr_active) { | |
6494 | busy = true; | |
24acfb71 | 6495 | rcu_read_unlock(); |
6ba94429 FW |
6496 | goto out_unlock; |
6497 | } | |
6498 | } | |
24acfb71 | 6499 | rcu_read_unlock(); |
6ba94429 FW |
6500 | } |
6501 | out_unlock: | |
6502 | mutex_unlock(&wq_pool_mutex); | |
6503 | return busy; | |
6504 | } | |
6505 | ||
6506 | /** | |
6507 | * thaw_workqueues - thaw workqueues | |
6508 | * | |
6509 | * Thaw workqueues. Normal queueing is restored and all collected | |
6510 | * frozen works are transferred to their respective pool worklists. | |
6511 | * | |
6512 | * CONTEXT: | |
6513 | * Grabs and releases wq_pool_mutex, wq->mutex and pool->lock's. | |
6514 | */ | |
6515 | void thaw_workqueues(void) | |
6516 | { | |
6517 | struct workqueue_struct *wq; | |
6ba94429 FW |
6518 | |
6519 | mutex_lock(&wq_pool_mutex); | |
6520 | ||
6521 | if (!workqueue_freezing) | |
6522 | goto out_unlock; | |
6523 | ||
6524 | workqueue_freezing = false; | |
6525 | ||
6526 | /* restore max_active and repopulate worklist */ | |
6527 | list_for_each_entry(wq, &workqueues, list) { | |
6528 | mutex_lock(&wq->mutex); | |
a045a272 | 6529 | wq_adjust_max_active(wq); |
6ba94429 FW |
6530 | mutex_unlock(&wq->mutex); |
6531 | } | |
6532 | ||
6533 | out_unlock: | |
6534 | mutex_unlock(&wq_pool_mutex); | |
6535 | } | |
6536 | #endif /* CONFIG_FREEZER */ | |
6537 | ||
99c621ef | 6538 | static int workqueue_apply_unbound_cpumask(const cpumask_var_t unbound_cpumask) |
042f7df1 LJ |
6539 | { |
6540 | LIST_HEAD(ctxs); | |
6541 | int ret = 0; | |
6542 | struct workqueue_struct *wq; | |
6543 | struct apply_wqattrs_ctx *ctx, *n; | |
6544 | ||
6545 | lockdep_assert_held(&wq_pool_mutex); | |
6546 | ||
6547 | list_for_each_entry(wq, &workqueues, list) { | |
8eb17dc1 | 6548 | if (!(wq->flags & WQ_UNBOUND) || (wq->flags & __WQ_DESTROYING)) |
042f7df1 | 6549 | continue; |
042f7df1 | 6550 | |
99c621ef | 6551 | ctx = apply_wqattrs_prepare(wq, wq->unbound_attrs, unbound_cpumask); |
84193c07 TH |
6552 | if (IS_ERR(ctx)) { |
6553 | ret = PTR_ERR(ctx); | |
042f7df1 LJ |
6554 | break; |
6555 | } | |
6556 | ||
6557 | list_add_tail(&ctx->list, &ctxs); | |
6558 | } | |
6559 | ||
6560 | list_for_each_entry_safe(ctx, n, &ctxs, list) { | |
6561 | if (!ret) | |
6562 | apply_wqattrs_commit(ctx); | |
6563 | apply_wqattrs_cleanup(ctx); | |
6564 | } | |
6565 | ||
99c621ef LJ |
6566 | if (!ret) { |
6567 | mutex_lock(&wq_pool_attach_mutex); | |
6568 | cpumask_copy(wq_unbound_cpumask, unbound_cpumask); | |
6569 | mutex_unlock(&wq_pool_attach_mutex); | |
6570 | } | |
042f7df1 LJ |
6571 | return ret; |
6572 | } | |
6573 | ||
fe28f631 WL |
6574 | /** |
6575 | * workqueue_unbound_exclude_cpumask - Exclude given CPUs from unbound cpumask | |
6576 | * @exclude_cpumask: the cpumask to be excluded from wq_unbound_cpumask | |
6577 | * | |
6578 | * This function can be called from cpuset code to provide a set of isolated | |
6579 | * CPUs that should be excluded from wq_unbound_cpumask. The caller must hold | |
6580 | * either cpus_read_lock or cpus_write_lock. | |
6581 | */ | |
6582 | int workqueue_unbound_exclude_cpumask(cpumask_var_t exclude_cpumask) | |
6583 | { | |
6584 | cpumask_var_t cpumask; | |
6585 | int ret = 0; | |
6586 | ||
6587 | if (!zalloc_cpumask_var(&cpumask, GFP_KERNEL)) | |
6588 | return -ENOMEM; | |
6589 | ||
6590 | lockdep_assert_cpus_held(); | |
6591 | mutex_lock(&wq_pool_mutex); | |
6592 | ||
6593 | /* Save the current isolated cpumask & export it via sysfs */ | |
6594 | cpumask_copy(wq_isolated_cpumask, exclude_cpumask); | |
6595 | ||
6596 | /* | |
6597 | * If the operation fails, it will fall back to | |
6598 | * wq_requested_unbound_cpumask which is initially set to | |
6599 | * (HK_TYPE_WQ ∩ HK_TYPE_DOMAIN) house keeping mask and rewritten | |
6600 | * by any subsequent write to workqueue/cpumask sysfs file. | |
6601 | */ | |
6602 | if (!cpumask_andnot(cpumask, wq_requested_unbound_cpumask, exclude_cpumask)) | |
6603 | cpumask_copy(cpumask, wq_requested_unbound_cpumask); | |
6604 | if (!cpumask_equal(cpumask, wq_unbound_cpumask)) | |
6605 | ret = workqueue_apply_unbound_cpumask(cpumask); | |
6606 | ||
6607 | mutex_unlock(&wq_pool_mutex); | |
6608 | free_cpumask_var(cpumask); | |
6609 | return ret; | |
6610 | } | |
6611 | ||
63c5484e TH |
6612 | static int parse_affn_scope(const char *val) |
6613 | { | |
6614 | int i; | |
6615 | ||
6616 | for (i = 0; i < ARRAY_SIZE(wq_affn_names); i++) { | |
6617 | if (!strncasecmp(val, wq_affn_names[i], strlen(wq_affn_names[i]))) | |
6618 | return i; | |
6619 | } | |
6620 | return -EINVAL; | |
6621 | } | |
6622 | ||
6623 | static int wq_affn_dfl_set(const char *val, const struct kernel_param *kp) | |
6624 | { | |
523a301e TH |
6625 | struct workqueue_struct *wq; |
6626 | int affn, cpu; | |
63c5484e TH |
6627 | |
6628 | affn = parse_affn_scope(val); | |
6629 | if (affn < 0) | |
6630 | return affn; | |
523a301e TH |
6631 | if (affn == WQ_AFFN_DFL) |
6632 | return -EINVAL; | |
6633 | ||
6634 | cpus_read_lock(); | |
6635 | mutex_lock(&wq_pool_mutex); | |
63c5484e TH |
6636 | |
6637 | wq_affn_dfl = affn; | |
523a301e TH |
6638 | |
6639 | list_for_each_entry(wq, &workqueues, list) { | |
6640 | for_each_online_cpu(cpu) { | |
6641 | wq_update_pod(wq, cpu, cpu, true); | |
6642 | } | |
6643 | } | |
6644 | ||
6645 | mutex_unlock(&wq_pool_mutex); | |
6646 | cpus_read_unlock(); | |
6647 | ||
63c5484e TH |
6648 | return 0; |
6649 | } | |
6650 | ||
6651 | static int wq_affn_dfl_get(char *buffer, const struct kernel_param *kp) | |
6652 | { | |
6653 | return scnprintf(buffer, PAGE_SIZE, "%s\n", wq_affn_names[wq_affn_dfl]); | |
6654 | } | |
6655 | ||
6656 | static const struct kernel_param_ops wq_affn_dfl_ops = { | |
6657 | .set = wq_affn_dfl_set, | |
6658 | .get = wq_affn_dfl_get, | |
6659 | }; | |
6660 | ||
6661 | module_param_cb(default_affinity_scope, &wq_affn_dfl_ops, NULL, 0644); | |
6662 | ||
6ba94429 FW |
6663 | #ifdef CONFIG_SYSFS |
6664 | /* | |
6665 | * Workqueues with WQ_SYSFS flag set is visible to userland via | |
6666 | * /sys/bus/workqueue/devices/WQ_NAME. All visible workqueues have the | |
6667 | * following attributes. | |
6668 | * | |
63c5484e TH |
6669 | * per_cpu RO bool : whether the workqueue is per-cpu or unbound |
6670 | * max_active RW int : maximum number of in-flight work items | |
6ba94429 FW |
6671 | * |
6672 | * Unbound workqueues have the following extra attributes. | |
6673 | * | |
63c5484e TH |
6674 | * nice RW int : nice value of the workers |
6675 | * cpumask RW mask : bitmask of allowed CPUs for the workers | |
6676 | * affinity_scope RW str : worker CPU affinity scope (cache, numa, none) | |
8639eceb | 6677 | * affinity_strict RW bool : worker CPU affinity is strict |
6ba94429 FW |
6678 | */ |
6679 | struct wq_device { | |
6680 | struct workqueue_struct *wq; | |
6681 | struct device dev; | |
6682 | }; | |
6683 | ||
6684 | static struct workqueue_struct *dev_to_wq(struct device *dev) | |
6685 | { | |
6686 | struct wq_device *wq_dev = container_of(dev, struct wq_device, dev); | |
6687 | ||
6688 | return wq_dev->wq; | |
6689 | } | |
6690 | ||
6691 | static ssize_t per_cpu_show(struct device *dev, struct device_attribute *attr, | |
6692 | char *buf) | |
6693 | { | |
6694 | struct workqueue_struct *wq = dev_to_wq(dev); | |
6695 | ||
6696 | return scnprintf(buf, PAGE_SIZE, "%d\n", (bool)!(wq->flags & WQ_UNBOUND)); | |
6697 | } | |
6698 | static DEVICE_ATTR_RO(per_cpu); | |
6699 | ||
6700 | static ssize_t max_active_show(struct device *dev, | |
6701 | struct device_attribute *attr, char *buf) | |
6702 | { | |
6703 | struct workqueue_struct *wq = dev_to_wq(dev); | |
6704 | ||
6705 | return scnprintf(buf, PAGE_SIZE, "%d\n", wq->saved_max_active); | |
6706 | } | |
6707 | ||
6708 | static ssize_t max_active_store(struct device *dev, | |
6709 | struct device_attribute *attr, const char *buf, | |
6710 | size_t count) | |
6711 | { | |
6712 | struct workqueue_struct *wq = dev_to_wq(dev); | |
6713 | int val; | |
6714 | ||
6715 | if (sscanf(buf, "%d", &val) != 1 || val <= 0) | |
6716 | return -EINVAL; | |
6717 | ||
6718 | workqueue_set_max_active(wq, val); | |
6719 | return count; | |
6720 | } | |
6721 | static DEVICE_ATTR_RW(max_active); | |
6722 | ||
6723 | static struct attribute *wq_sysfs_attrs[] = { | |
6724 | &dev_attr_per_cpu.attr, | |
6725 | &dev_attr_max_active.attr, | |
6726 | NULL, | |
6727 | }; | |
6728 | ATTRIBUTE_GROUPS(wq_sysfs); | |
6729 | ||
49277a5b WL |
6730 | static void apply_wqattrs_lock(void) |
6731 | { | |
6732 | /* CPUs should stay stable across pwq creations and installations */ | |
6733 | cpus_read_lock(); | |
6734 | mutex_lock(&wq_pool_mutex); | |
6735 | } | |
6736 | ||
6737 | static void apply_wqattrs_unlock(void) | |
6738 | { | |
6739 | mutex_unlock(&wq_pool_mutex); | |
6740 | cpus_read_unlock(); | |
6741 | } | |
6742 | ||
6ba94429 FW |
6743 | static ssize_t wq_nice_show(struct device *dev, struct device_attribute *attr, |
6744 | char *buf) | |
6745 | { | |
6746 | struct workqueue_struct *wq = dev_to_wq(dev); | |
6747 | int written; | |
6748 | ||
6749 | mutex_lock(&wq->mutex); | |
6750 | written = scnprintf(buf, PAGE_SIZE, "%d\n", wq->unbound_attrs->nice); | |
6751 | mutex_unlock(&wq->mutex); | |
6752 | ||
6753 | return written; | |
6754 | } | |
6755 | ||
6756 | /* prepare workqueue_attrs for sysfs store operations */ | |
6757 | static struct workqueue_attrs *wq_sysfs_prep_attrs(struct workqueue_struct *wq) | |
6758 | { | |
6759 | struct workqueue_attrs *attrs; | |
6760 | ||
899a94fe LJ |
6761 | lockdep_assert_held(&wq_pool_mutex); |
6762 | ||
be69d00d | 6763 | attrs = alloc_workqueue_attrs(); |
6ba94429 FW |
6764 | if (!attrs) |
6765 | return NULL; | |
6766 | ||
6ba94429 | 6767 | copy_workqueue_attrs(attrs, wq->unbound_attrs); |
6ba94429 FW |
6768 | return attrs; |
6769 | } | |
6770 | ||
6771 | static ssize_t wq_nice_store(struct device *dev, struct device_attribute *attr, | |
6772 | const char *buf, size_t count) | |
6773 | { | |
6774 | struct workqueue_struct *wq = dev_to_wq(dev); | |
6775 | struct workqueue_attrs *attrs; | |
d4d3e257 LJ |
6776 | int ret = -ENOMEM; |
6777 | ||
6778 | apply_wqattrs_lock(); | |
6ba94429 FW |
6779 | |
6780 | attrs = wq_sysfs_prep_attrs(wq); | |
6781 | if (!attrs) | |
d4d3e257 | 6782 | goto out_unlock; |
6ba94429 FW |
6783 | |
6784 | if (sscanf(buf, "%d", &attrs->nice) == 1 && | |
6785 | attrs->nice >= MIN_NICE && attrs->nice <= MAX_NICE) | |
d4d3e257 | 6786 | ret = apply_workqueue_attrs_locked(wq, attrs); |
6ba94429 FW |
6787 | else |
6788 | ret = -EINVAL; | |
6789 | ||
d4d3e257 LJ |
6790 | out_unlock: |
6791 | apply_wqattrs_unlock(); | |
6ba94429 FW |
6792 | free_workqueue_attrs(attrs); |
6793 | return ret ?: count; | |
6794 | } | |
6795 | ||
6796 | static ssize_t wq_cpumask_show(struct device *dev, | |
6797 | struct device_attribute *attr, char *buf) | |
6798 | { | |
6799 | struct workqueue_struct *wq = dev_to_wq(dev); | |
6800 | int written; | |
6801 | ||
6802 | mutex_lock(&wq->mutex); | |
6803 | written = scnprintf(buf, PAGE_SIZE, "%*pb\n", | |
6804 | cpumask_pr_args(wq->unbound_attrs->cpumask)); | |
6805 | mutex_unlock(&wq->mutex); | |
6806 | return written; | |
6807 | } | |
6808 | ||
6809 | static ssize_t wq_cpumask_store(struct device *dev, | |
6810 | struct device_attribute *attr, | |
6811 | const char *buf, size_t count) | |
6812 | { | |
6813 | struct workqueue_struct *wq = dev_to_wq(dev); | |
6814 | struct workqueue_attrs *attrs; | |
d4d3e257 LJ |
6815 | int ret = -ENOMEM; |
6816 | ||
6817 | apply_wqattrs_lock(); | |
6ba94429 FW |
6818 | |
6819 | attrs = wq_sysfs_prep_attrs(wq); | |
6820 | if (!attrs) | |
d4d3e257 | 6821 | goto out_unlock; |
6ba94429 FW |
6822 | |
6823 | ret = cpumask_parse(buf, attrs->cpumask); | |
6824 | if (!ret) | |
d4d3e257 | 6825 | ret = apply_workqueue_attrs_locked(wq, attrs); |
6ba94429 | 6826 | |
d4d3e257 LJ |
6827 | out_unlock: |
6828 | apply_wqattrs_unlock(); | |
6ba94429 FW |
6829 | free_workqueue_attrs(attrs); |
6830 | return ret ?: count; | |
6831 | } | |
6832 | ||
63c5484e TH |
6833 | static ssize_t wq_affn_scope_show(struct device *dev, |
6834 | struct device_attribute *attr, char *buf) | |
6835 | { | |
6836 | struct workqueue_struct *wq = dev_to_wq(dev); | |
6837 | int written; | |
6838 | ||
6839 | mutex_lock(&wq->mutex); | |
523a301e TH |
6840 | if (wq->unbound_attrs->affn_scope == WQ_AFFN_DFL) |
6841 | written = scnprintf(buf, PAGE_SIZE, "%s (%s)\n", | |
6842 | wq_affn_names[WQ_AFFN_DFL], | |
6843 | wq_affn_names[wq_affn_dfl]); | |
6844 | else | |
6845 | written = scnprintf(buf, PAGE_SIZE, "%s\n", | |
6846 | wq_affn_names[wq->unbound_attrs->affn_scope]); | |
63c5484e TH |
6847 | mutex_unlock(&wq->mutex); |
6848 | ||
6849 | return written; | |
6850 | } | |
6851 | ||
6852 | static ssize_t wq_affn_scope_store(struct device *dev, | |
6853 | struct device_attribute *attr, | |
6854 | const char *buf, size_t count) | |
6855 | { | |
6856 | struct workqueue_struct *wq = dev_to_wq(dev); | |
6857 | struct workqueue_attrs *attrs; | |
6858 | int affn, ret = -ENOMEM; | |
6859 | ||
6860 | affn = parse_affn_scope(buf); | |
6861 | if (affn < 0) | |
6862 | return affn; | |
6863 | ||
6864 | apply_wqattrs_lock(); | |
6865 | attrs = wq_sysfs_prep_attrs(wq); | |
6866 | if (attrs) { | |
6867 | attrs->affn_scope = affn; | |
6868 | ret = apply_workqueue_attrs_locked(wq, attrs); | |
6869 | } | |
6870 | apply_wqattrs_unlock(); | |
6871 | free_workqueue_attrs(attrs); | |
6872 | return ret ?: count; | |
6873 | } | |
6874 | ||
8639eceb TH |
6875 | static ssize_t wq_affinity_strict_show(struct device *dev, |
6876 | struct device_attribute *attr, char *buf) | |
6877 | { | |
6878 | struct workqueue_struct *wq = dev_to_wq(dev); | |
6879 | ||
6880 | return scnprintf(buf, PAGE_SIZE, "%d\n", | |
6881 | wq->unbound_attrs->affn_strict); | |
6882 | } | |
6883 | ||
6884 | static ssize_t wq_affinity_strict_store(struct device *dev, | |
6885 | struct device_attribute *attr, | |
6886 | const char *buf, size_t count) | |
6887 | { | |
6888 | struct workqueue_struct *wq = dev_to_wq(dev); | |
6889 | struct workqueue_attrs *attrs; | |
6890 | int v, ret = -ENOMEM; | |
6891 | ||
6892 | if (sscanf(buf, "%d", &v) != 1) | |
6893 | return -EINVAL; | |
6894 | ||
6895 | apply_wqattrs_lock(); | |
6896 | attrs = wq_sysfs_prep_attrs(wq); | |
6897 | if (attrs) { | |
6898 | attrs->affn_strict = (bool)v; | |
6899 | ret = apply_workqueue_attrs_locked(wq, attrs); | |
6900 | } | |
6901 | apply_wqattrs_unlock(); | |
6902 | free_workqueue_attrs(attrs); | |
6903 | return ret ?: count; | |
6904 | } | |
6905 | ||
6ba94429 | 6906 | static struct device_attribute wq_sysfs_unbound_attrs[] = { |
6ba94429 FW |
6907 | __ATTR(nice, 0644, wq_nice_show, wq_nice_store), |
6908 | __ATTR(cpumask, 0644, wq_cpumask_show, wq_cpumask_store), | |
63c5484e | 6909 | __ATTR(affinity_scope, 0644, wq_affn_scope_show, wq_affn_scope_store), |
8639eceb | 6910 | __ATTR(affinity_strict, 0644, wq_affinity_strict_show, wq_affinity_strict_store), |
6ba94429 FW |
6911 | __ATTR_NULL, |
6912 | }; | |
8ccad40d | 6913 | |
4f19b8e0 | 6914 | static struct bus_type wq_subsys = { |
6ba94429 FW |
6915 | .name = "workqueue", |
6916 | .dev_groups = wq_sysfs_groups, | |
2d3854a3 RR |
6917 | }; |
6918 | ||
49277a5b WL |
6919 | /** |
6920 | * workqueue_set_unbound_cpumask - Set the low-level unbound cpumask | |
6921 | * @cpumask: the cpumask to set | |
6922 | * | |
6923 | * The low-level workqueues cpumask is a global cpumask that limits | |
6924 | * the affinity of all unbound workqueues. This function check the @cpumask | |
6925 | * and apply it to all unbound workqueues and updates all pwqs of them. | |
6926 | * | |
6927 | * Return: 0 - Success | |
6928 | * -EINVAL - Invalid @cpumask | |
6929 | * -ENOMEM - Failed to allocate memory for attrs or pwqs. | |
6930 | */ | |
6931 | static int workqueue_set_unbound_cpumask(cpumask_var_t cpumask) | |
6932 | { | |
6933 | int ret = -EINVAL; | |
6934 | ||
6935 | /* | |
6936 | * Not excluding isolated cpus on purpose. | |
6937 | * If the user wishes to include them, we allow that. | |
6938 | */ | |
6939 | cpumask_and(cpumask, cpumask, cpu_possible_mask); | |
6940 | if (!cpumask_empty(cpumask)) { | |
6941 | apply_wqattrs_lock(); | |
6942 | cpumask_copy(wq_requested_unbound_cpumask, cpumask); | |
6943 | if (cpumask_equal(cpumask, wq_unbound_cpumask)) { | |
6944 | ret = 0; | |
6945 | goto out_unlock; | |
6946 | } | |
6947 | ||
6948 | ret = workqueue_apply_unbound_cpumask(cpumask); | |
6949 | ||
6950 | out_unlock: | |
6951 | apply_wqattrs_unlock(); | |
6952 | } | |
6953 | ||
6954 | return ret; | |
6955 | } | |
6956 | ||
fe28f631 WL |
6957 | static ssize_t __wq_cpumask_show(struct device *dev, |
6958 | struct device_attribute *attr, char *buf, cpumask_var_t mask) | |
b05a7928 FW |
6959 | { |
6960 | int written; | |
6961 | ||
042f7df1 | 6962 | mutex_lock(&wq_pool_mutex); |
fe28f631 | 6963 | written = scnprintf(buf, PAGE_SIZE, "%*pb\n", cpumask_pr_args(mask)); |
042f7df1 | 6964 | mutex_unlock(&wq_pool_mutex); |
b05a7928 FW |
6965 | |
6966 | return written; | |
6967 | } | |
6968 | ||
fe28f631 WL |
6969 | static ssize_t wq_unbound_cpumask_show(struct device *dev, |
6970 | struct device_attribute *attr, char *buf) | |
6971 | { | |
6972 | return __wq_cpumask_show(dev, attr, buf, wq_unbound_cpumask); | |
6973 | } | |
6974 | ||
6975 | static ssize_t wq_requested_cpumask_show(struct device *dev, | |
6976 | struct device_attribute *attr, char *buf) | |
6977 | { | |
6978 | return __wq_cpumask_show(dev, attr, buf, wq_requested_unbound_cpumask); | |
6979 | } | |
6980 | ||
6981 | static ssize_t wq_isolated_cpumask_show(struct device *dev, | |
6982 | struct device_attribute *attr, char *buf) | |
6983 | { | |
6984 | return __wq_cpumask_show(dev, attr, buf, wq_isolated_cpumask); | |
6985 | } | |
6986 | ||
042f7df1 LJ |
6987 | static ssize_t wq_unbound_cpumask_store(struct device *dev, |
6988 | struct device_attribute *attr, const char *buf, size_t count) | |
6989 | { | |
6990 | cpumask_var_t cpumask; | |
6991 | int ret; | |
6992 | ||
6993 | if (!zalloc_cpumask_var(&cpumask, GFP_KERNEL)) | |
6994 | return -ENOMEM; | |
6995 | ||
6996 | ret = cpumask_parse(buf, cpumask); | |
6997 | if (!ret) | |
6998 | ret = workqueue_set_unbound_cpumask(cpumask); | |
6999 | ||
7000 | free_cpumask_var(cpumask); | |
7001 | return ret ? ret : count; | |
7002 | } | |
7003 | ||
fe28f631 | 7004 | static struct device_attribute wq_sysfs_cpumask_attrs[] = { |
042f7df1 | 7005 | __ATTR(cpumask, 0644, wq_unbound_cpumask_show, |
fe28f631 WL |
7006 | wq_unbound_cpumask_store), |
7007 | __ATTR(cpumask_requested, 0444, wq_requested_cpumask_show, NULL), | |
7008 | __ATTR(cpumask_isolated, 0444, wq_isolated_cpumask_show, NULL), | |
7009 | __ATTR_NULL, | |
7010 | }; | |
b05a7928 | 7011 | |
6ba94429 | 7012 | static int __init wq_sysfs_init(void) |
2d3854a3 | 7013 | { |
686f6697 | 7014 | struct device *dev_root; |
b05a7928 FW |
7015 | int err; |
7016 | ||
7017 | err = subsys_virtual_register(&wq_subsys, NULL); | |
7018 | if (err) | |
7019 | return err; | |
7020 | ||
686f6697 GKH |
7021 | dev_root = bus_get_dev_root(&wq_subsys); |
7022 | if (dev_root) { | |
fe28f631 WL |
7023 | struct device_attribute *attr; |
7024 | ||
7025 | for (attr = wq_sysfs_cpumask_attrs; attr->attr.name; attr++) { | |
7026 | err = device_create_file(dev_root, attr); | |
7027 | if (err) | |
7028 | break; | |
7029 | } | |
686f6697 GKH |
7030 | put_device(dev_root); |
7031 | } | |
7032 | return err; | |
2d3854a3 | 7033 | } |
6ba94429 | 7034 | core_initcall(wq_sysfs_init); |
2d3854a3 | 7035 | |
6ba94429 | 7036 | static void wq_device_release(struct device *dev) |
2d3854a3 | 7037 | { |
6ba94429 | 7038 | struct wq_device *wq_dev = container_of(dev, struct wq_device, dev); |
6b44003e | 7039 | |
6ba94429 | 7040 | kfree(wq_dev); |
2d3854a3 | 7041 | } |
a0a1a5fd TH |
7042 | |
7043 | /** | |
6ba94429 FW |
7044 | * workqueue_sysfs_register - make a workqueue visible in sysfs |
7045 | * @wq: the workqueue to register | |
a0a1a5fd | 7046 | * |
6ba94429 FW |
7047 | * Expose @wq in sysfs under /sys/bus/workqueue/devices. |
7048 | * alloc_workqueue*() automatically calls this function if WQ_SYSFS is set | |
7049 | * which is the preferred method. | |
a0a1a5fd | 7050 | * |
6ba94429 FW |
7051 | * Workqueue user should use this function directly iff it wants to apply |
7052 | * workqueue_attrs before making the workqueue visible in sysfs; otherwise, | |
7053 | * apply_workqueue_attrs() may race against userland updating the | |
7054 | * attributes. | |
7055 | * | |
7056 | * Return: 0 on success, -errno on failure. | |
a0a1a5fd | 7057 | */ |
6ba94429 | 7058 | int workqueue_sysfs_register(struct workqueue_struct *wq) |
a0a1a5fd | 7059 | { |
6ba94429 FW |
7060 | struct wq_device *wq_dev; |
7061 | int ret; | |
a0a1a5fd | 7062 | |
6ba94429 | 7063 | /* |
4c065dbc WL |
7064 | * Adjusting max_active breaks ordering guarantee. Disallow exposing |
7065 | * ordered workqueues. | |
6ba94429 | 7066 | */ |
3bc1e711 | 7067 | if (WARN_ON(wq->flags & __WQ_ORDERED)) |
6ba94429 | 7068 | return -EINVAL; |
a0a1a5fd | 7069 | |
6ba94429 FW |
7070 | wq->wq_dev = wq_dev = kzalloc(sizeof(*wq_dev), GFP_KERNEL); |
7071 | if (!wq_dev) | |
7072 | return -ENOMEM; | |
5bcab335 | 7073 | |
6ba94429 FW |
7074 | wq_dev->wq = wq; |
7075 | wq_dev->dev.bus = &wq_subsys; | |
6ba94429 | 7076 | wq_dev->dev.release = wq_device_release; |
23217b44 | 7077 | dev_set_name(&wq_dev->dev, "%s", wq->name); |
a0a1a5fd | 7078 | |
6ba94429 FW |
7079 | /* |
7080 | * unbound_attrs are created separately. Suppress uevent until | |
7081 | * everything is ready. | |
7082 | */ | |
7083 | dev_set_uevent_suppress(&wq_dev->dev, true); | |
a0a1a5fd | 7084 | |
6ba94429 FW |
7085 | ret = device_register(&wq_dev->dev); |
7086 | if (ret) { | |
537f4146 | 7087 | put_device(&wq_dev->dev); |
6ba94429 FW |
7088 | wq->wq_dev = NULL; |
7089 | return ret; | |
7090 | } | |
a0a1a5fd | 7091 | |
6ba94429 FW |
7092 | if (wq->flags & WQ_UNBOUND) { |
7093 | struct device_attribute *attr; | |
a0a1a5fd | 7094 | |
6ba94429 FW |
7095 | for (attr = wq_sysfs_unbound_attrs; attr->attr.name; attr++) { |
7096 | ret = device_create_file(&wq_dev->dev, attr); | |
7097 | if (ret) { | |
7098 | device_unregister(&wq_dev->dev); | |
7099 | wq->wq_dev = NULL; | |
7100 | return ret; | |
a0a1a5fd TH |
7101 | } |
7102 | } | |
7103 | } | |
6ba94429 FW |
7104 | |
7105 | dev_set_uevent_suppress(&wq_dev->dev, false); | |
7106 | kobject_uevent(&wq_dev->dev.kobj, KOBJ_ADD); | |
7107 | return 0; | |
a0a1a5fd TH |
7108 | } |
7109 | ||
7110 | /** | |
6ba94429 FW |
7111 | * workqueue_sysfs_unregister - undo workqueue_sysfs_register() |
7112 | * @wq: the workqueue to unregister | |
a0a1a5fd | 7113 | * |
6ba94429 | 7114 | * If @wq is registered to sysfs by workqueue_sysfs_register(), unregister. |
a0a1a5fd | 7115 | */ |
6ba94429 | 7116 | static void workqueue_sysfs_unregister(struct workqueue_struct *wq) |
a0a1a5fd | 7117 | { |
6ba94429 | 7118 | struct wq_device *wq_dev = wq->wq_dev; |
8b03ae3c | 7119 | |
6ba94429 FW |
7120 | if (!wq->wq_dev) |
7121 | return; | |
a0a1a5fd | 7122 | |
6ba94429 FW |
7123 | wq->wq_dev = NULL; |
7124 | device_unregister(&wq_dev->dev); | |
a0a1a5fd | 7125 | } |
6ba94429 FW |
7126 | #else /* CONFIG_SYSFS */ |
7127 | static void workqueue_sysfs_unregister(struct workqueue_struct *wq) { } | |
7128 | #endif /* CONFIG_SYSFS */ | |
a0a1a5fd | 7129 | |
82607adc TH |
7130 | /* |
7131 | * Workqueue watchdog. | |
7132 | * | |
7133 | * Stall may be caused by various bugs - missing WQ_MEM_RECLAIM, illegal | |
7134 | * flush dependency, a concurrency managed work item which stays RUNNING | |
7135 | * indefinitely. Workqueue stalls can be very difficult to debug as the | |
7136 | * usual warning mechanisms don't trigger and internal workqueue state is | |
7137 | * largely opaque. | |
7138 | * | |
7139 | * Workqueue watchdog monitors all worker pools periodically and dumps | |
7140 | * state if some pools failed to make forward progress for a while where | |
7141 | * forward progress is defined as the first item on ->worklist changing. | |
7142 | * | |
7143 | * This mechanism is controlled through the kernel parameter | |
7144 | * "workqueue.watchdog_thresh" which can be updated at runtime through the | |
7145 | * corresponding sysfs parameter file. | |
7146 | */ | |
7147 | #ifdef CONFIG_WQ_WATCHDOG | |
7148 | ||
82607adc | 7149 | static unsigned long wq_watchdog_thresh = 30; |
5cd79d6a | 7150 | static struct timer_list wq_watchdog_timer; |
82607adc TH |
7151 | |
7152 | static unsigned long wq_watchdog_touched = INITIAL_JIFFIES; | |
7153 | static DEFINE_PER_CPU(unsigned long, wq_watchdog_touched_cpu) = INITIAL_JIFFIES; | |
7154 | ||
cd2440d6 PM |
7155 | /* |
7156 | * Show workers that might prevent the processing of pending work items. | |
7157 | * The only candidates are CPU-bound workers in the running state. | |
7158 | * Pending work items should be handled by another idle worker | |
7159 | * in all other situations. | |
7160 | */ | |
7161 | static void show_cpu_pool_hog(struct worker_pool *pool) | |
7162 | { | |
7163 | struct worker *worker; | |
7164 | unsigned long flags; | |
7165 | int bkt; | |
7166 | ||
7167 | raw_spin_lock_irqsave(&pool->lock, flags); | |
7168 | ||
7169 | hash_for_each(pool->busy_hash, bkt, worker, hentry) { | |
7170 | if (task_is_running(worker->task)) { | |
7171 | /* | |
7172 | * Defer printing to avoid deadlocks in console | |
7173 | * drivers that queue work while holding locks | |
7174 | * also taken in their write paths. | |
7175 | */ | |
7176 | printk_deferred_enter(); | |
7177 | ||
7178 | pr_info("pool %d:\n", pool->id); | |
7179 | sched_show_task(worker->task); | |
7180 | ||
7181 | printk_deferred_exit(); | |
7182 | } | |
7183 | } | |
7184 | ||
7185 | raw_spin_unlock_irqrestore(&pool->lock, flags); | |
7186 | } | |
7187 | ||
7188 | static void show_cpu_pools_hogs(void) | |
7189 | { | |
7190 | struct worker_pool *pool; | |
7191 | int pi; | |
7192 | ||
7193 | pr_info("Showing backtraces of running workers in stalled CPU-bound worker pools:\n"); | |
7194 | ||
7195 | rcu_read_lock(); | |
7196 | ||
7197 | for_each_pool(pool, pi) { | |
7198 | if (pool->cpu_stall) | |
7199 | show_cpu_pool_hog(pool); | |
7200 | ||
7201 | } | |
7202 | ||
7203 | rcu_read_unlock(); | |
7204 | } | |
7205 | ||
82607adc TH |
7206 | static void wq_watchdog_reset_touched(void) |
7207 | { | |
7208 | int cpu; | |
7209 | ||
7210 | wq_watchdog_touched = jiffies; | |
7211 | for_each_possible_cpu(cpu) | |
7212 | per_cpu(wq_watchdog_touched_cpu, cpu) = jiffies; | |
7213 | } | |
7214 | ||
5cd79d6a | 7215 | static void wq_watchdog_timer_fn(struct timer_list *unused) |
82607adc TH |
7216 | { |
7217 | unsigned long thresh = READ_ONCE(wq_watchdog_thresh) * HZ; | |
7218 | bool lockup_detected = false; | |
cd2440d6 | 7219 | bool cpu_pool_stall = false; |
940d71c6 | 7220 | unsigned long now = jiffies; |
82607adc TH |
7221 | struct worker_pool *pool; |
7222 | int pi; | |
7223 | ||
7224 | if (!thresh) | |
7225 | return; | |
7226 | ||
7227 | rcu_read_lock(); | |
7228 | ||
7229 | for_each_pool(pool, pi) { | |
7230 | unsigned long pool_ts, touched, ts; | |
7231 | ||
cd2440d6 | 7232 | pool->cpu_stall = false; |
82607adc TH |
7233 | if (list_empty(&pool->worklist)) |
7234 | continue; | |
7235 | ||
940d71c6 SS |
7236 | /* |
7237 | * If a virtual machine is stopped by the host it can look to | |
7238 | * the watchdog like a stall. | |
7239 | */ | |
7240 | kvm_check_and_clear_guest_paused(); | |
7241 | ||
82607adc | 7242 | /* get the latest of pool and touched timestamps */ |
89e28ce6 WQ |
7243 | if (pool->cpu >= 0) |
7244 | touched = READ_ONCE(per_cpu(wq_watchdog_touched_cpu, pool->cpu)); | |
7245 | else | |
7246 | touched = READ_ONCE(wq_watchdog_touched); | |
82607adc | 7247 | pool_ts = READ_ONCE(pool->watchdog_ts); |
82607adc TH |
7248 | |
7249 | if (time_after(pool_ts, touched)) | |
7250 | ts = pool_ts; | |
7251 | else | |
7252 | ts = touched; | |
7253 | ||
82607adc | 7254 | /* did we stall? */ |
940d71c6 | 7255 | if (time_after(now, ts + thresh)) { |
82607adc | 7256 | lockup_detected = true; |
4cb1ef64 | 7257 | if (pool->cpu >= 0 && !(pool->flags & POOL_BH)) { |
cd2440d6 PM |
7258 | pool->cpu_stall = true; |
7259 | cpu_pool_stall = true; | |
7260 | } | |
82607adc TH |
7261 | pr_emerg("BUG: workqueue lockup - pool"); |
7262 | pr_cont_pool_info(pool); | |
7263 | pr_cont(" stuck for %us!\n", | |
940d71c6 | 7264 | jiffies_to_msecs(now - pool_ts) / 1000); |
82607adc | 7265 | } |
cd2440d6 PM |
7266 | |
7267 | ||
82607adc TH |
7268 | } |
7269 | ||
7270 | rcu_read_unlock(); | |
7271 | ||
7272 | if (lockup_detected) | |
55df0933 | 7273 | show_all_workqueues(); |
82607adc | 7274 | |
cd2440d6 PM |
7275 | if (cpu_pool_stall) |
7276 | show_cpu_pools_hogs(); | |
7277 | ||
82607adc TH |
7278 | wq_watchdog_reset_touched(); |
7279 | mod_timer(&wq_watchdog_timer, jiffies + thresh); | |
7280 | } | |
7281 | ||
cb9d7fd5 | 7282 | notrace void wq_watchdog_touch(int cpu) |
82607adc TH |
7283 | { |
7284 | if (cpu >= 0) | |
7285 | per_cpu(wq_watchdog_touched_cpu, cpu) = jiffies; | |
89e28ce6 WQ |
7286 | |
7287 | wq_watchdog_touched = jiffies; | |
82607adc TH |
7288 | } |
7289 | ||
7290 | static void wq_watchdog_set_thresh(unsigned long thresh) | |
7291 | { | |
7292 | wq_watchdog_thresh = 0; | |
7293 | del_timer_sync(&wq_watchdog_timer); | |
7294 | ||
7295 | if (thresh) { | |
7296 | wq_watchdog_thresh = thresh; | |
7297 | wq_watchdog_reset_touched(); | |
7298 | mod_timer(&wq_watchdog_timer, jiffies + thresh * HZ); | |
7299 | } | |
7300 | } | |
7301 | ||
7302 | static int wq_watchdog_param_set_thresh(const char *val, | |
7303 | const struct kernel_param *kp) | |
7304 | { | |
7305 | unsigned long thresh; | |
7306 | int ret; | |
7307 | ||
7308 | ret = kstrtoul(val, 0, &thresh); | |
7309 | if (ret) | |
7310 | return ret; | |
7311 | ||
7312 | if (system_wq) | |
7313 | wq_watchdog_set_thresh(thresh); | |
7314 | else | |
7315 | wq_watchdog_thresh = thresh; | |
7316 | ||
7317 | return 0; | |
7318 | } | |
7319 | ||
7320 | static const struct kernel_param_ops wq_watchdog_thresh_ops = { | |
7321 | .set = wq_watchdog_param_set_thresh, | |
7322 | .get = param_get_ulong, | |
7323 | }; | |
7324 | ||
7325 | module_param_cb(watchdog_thresh, &wq_watchdog_thresh_ops, &wq_watchdog_thresh, | |
7326 | 0644); | |
7327 | ||
7328 | static void wq_watchdog_init(void) | |
7329 | { | |
5cd79d6a | 7330 | timer_setup(&wq_watchdog_timer, wq_watchdog_timer_fn, TIMER_DEFERRABLE); |
82607adc TH |
7331 | wq_watchdog_set_thresh(wq_watchdog_thresh); |
7332 | } | |
7333 | ||
7334 | #else /* CONFIG_WQ_WATCHDOG */ | |
7335 | ||
7336 | static inline void wq_watchdog_init(void) { } | |
7337 | ||
7338 | #endif /* CONFIG_WQ_WATCHDOG */ | |
7339 | ||
4a6c5607 TH |
7340 | static void __init restrict_unbound_cpumask(const char *name, const struct cpumask *mask) |
7341 | { | |
7342 | if (!cpumask_intersects(wq_unbound_cpumask, mask)) { | |
7343 | pr_warn("workqueue: Restricting unbound_cpumask (%*pb) with %s (%*pb) leaves no CPU, ignoring\n", | |
7344 | cpumask_pr_args(wq_unbound_cpumask), name, cpumask_pr_args(mask)); | |
7345 | return; | |
7346 | } | |
7347 | ||
7348 | cpumask_and(wq_unbound_cpumask, wq_unbound_cpumask, mask); | |
7349 | } | |
7350 | ||
2fcdb1b4 TH |
7351 | static void __init init_cpu_worker_pool(struct worker_pool *pool, int cpu, int nice) |
7352 | { | |
7353 | BUG_ON(init_worker_pool(pool)); | |
7354 | pool->cpu = cpu; | |
7355 | cpumask_copy(pool->attrs->cpumask, cpumask_of(cpu)); | |
7356 | cpumask_copy(pool->attrs->__pod_cpumask, cpumask_of(cpu)); | |
7357 | pool->attrs->nice = nice; | |
7358 | pool->attrs->affn_strict = true; | |
7359 | pool->node = cpu_to_node(cpu); | |
7360 | ||
7361 | /* alloc pool ID */ | |
7362 | mutex_lock(&wq_pool_mutex); | |
7363 | BUG_ON(worker_pool_assign_id(pool)); | |
7364 | mutex_unlock(&wq_pool_mutex); | |
7365 | } | |
7366 | ||
3347fa09 TH |
7367 | /** |
7368 | * workqueue_init_early - early init for workqueue subsystem | |
7369 | * | |
2930155b TH |
7370 | * This is the first step of three-staged workqueue subsystem initialization and |
7371 | * invoked as soon as the bare basics - memory allocation, cpumasks and idr are | |
7372 | * up. It sets up all the data structures and system workqueues and allows early | |
7373 | * boot code to create workqueues and queue/cancel work items. Actual work item | |
7374 | * execution starts only after kthreads can be created and scheduled right | |
7375 | * before early initcalls. | |
3347fa09 | 7376 | */ |
2333e829 | 7377 | void __init workqueue_init_early(void) |
1da177e4 | 7378 | { |
84193c07 | 7379 | struct wq_pod_type *pt = &wq_pod_types[WQ_AFFN_SYSTEM]; |
7a4e344c TH |
7380 | int std_nice[NR_STD_WORKER_POOLS] = { 0, HIGHPRI_NICE_LEVEL }; |
7381 | int i, cpu; | |
c34056a3 | 7382 | |
10cdb157 | 7383 | BUILD_BUG_ON(__alignof__(struct pool_workqueue) < __alignof__(long long)); |
e904e6c2 | 7384 | |
b05a7928 | 7385 | BUG_ON(!alloc_cpumask_var(&wq_unbound_cpumask, GFP_KERNEL)); |
fe28f631 WL |
7386 | BUG_ON(!alloc_cpumask_var(&wq_requested_unbound_cpumask, GFP_KERNEL)); |
7387 | BUG_ON(!zalloc_cpumask_var(&wq_isolated_cpumask, GFP_KERNEL)); | |
b05a7928 | 7388 | |
4a6c5607 TH |
7389 | cpumask_copy(wq_unbound_cpumask, cpu_possible_mask); |
7390 | restrict_unbound_cpumask("HK_TYPE_WQ", housekeeping_cpumask(HK_TYPE_WQ)); | |
7391 | restrict_unbound_cpumask("HK_TYPE_DOMAIN", housekeeping_cpumask(HK_TYPE_DOMAIN)); | |
ace3c549 | 7392 | if (!cpumask_empty(&wq_cmdline_cpumask)) |
4a6c5607 | 7393 | restrict_unbound_cpumask("workqueue.unbound_cpus", &wq_cmdline_cpumask); |
ace3c549 | 7394 | |
fe28f631 | 7395 | cpumask_copy(wq_requested_unbound_cpumask, wq_unbound_cpumask); |
ace3c549 | 7396 | |
e904e6c2 TH |
7397 | pwq_cache = KMEM_CACHE(pool_workqueue, SLAB_PANIC); |
7398 | ||
2930155b TH |
7399 | wq_update_pod_attrs_buf = alloc_workqueue_attrs(); |
7400 | BUG_ON(!wq_update_pod_attrs_buf); | |
7401 | ||
7bd20b6b MT |
7402 | /* |
7403 | * If nohz_full is enabled, set power efficient workqueue as unbound. | |
7404 | * This allows workqueue items to be moved to HK CPUs. | |
7405 | */ | |
7406 | if (housekeeping_enabled(HK_TYPE_TICK)) | |
7407 | wq_power_efficient = true; | |
7408 | ||
84193c07 TH |
7409 | /* initialize WQ_AFFN_SYSTEM pods */ |
7410 | pt->pod_cpus = kcalloc(1, sizeof(pt->pod_cpus[0]), GFP_KERNEL); | |
7411 | pt->pod_node = kcalloc(1, sizeof(pt->pod_node[0]), GFP_KERNEL); | |
7412 | pt->cpu_pod = kcalloc(nr_cpu_ids, sizeof(pt->cpu_pod[0]), GFP_KERNEL); | |
7413 | BUG_ON(!pt->pod_cpus || !pt->pod_node || !pt->cpu_pod); | |
7414 | ||
7415 | BUG_ON(!zalloc_cpumask_var_node(&pt->pod_cpus[0], GFP_KERNEL, NUMA_NO_NODE)); | |
7416 | ||
84193c07 TH |
7417 | pt->nr_pods = 1; |
7418 | cpumask_copy(pt->pod_cpus[0], cpu_possible_mask); | |
7419 | pt->pod_node[0] = NUMA_NO_NODE; | |
7420 | pt->cpu_pod[0] = 0; | |
7421 | ||
4cb1ef64 | 7422 | /* initialize BH and CPU pools */ |
29c91e99 | 7423 | for_each_possible_cpu(cpu) { |
4ce62e9e | 7424 | struct worker_pool *pool; |
8b03ae3c | 7425 | |
4cb1ef64 TH |
7426 | i = 0; |
7427 | for_each_bh_worker_pool(pool, cpu) { | |
7428 | init_cpu_worker_pool(pool, cpu, std_nice[i++]); | |
7429 | pool->flags |= POOL_BH; | |
7430 | } | |
7431 | ||
7a4e344c | 7432 | i = 0; |
2fcdb1b4 TH |
7433 | for_each_cpu_worker_pool(pool, cpu) |
7434 | init_cpu_worker_pool(pool, cpu, std_nice[i++]); | |
8b03ae3c TH |
7435 | } |
7436 | ||
8a2b7538 | 7437 | /* create default unbound and ordered wq attrs */ |
29c91e99 TH |
7438 | for (i = 0; i < NR_STD_WORKER_POOLS; i++) { |
7439 | struct workqueue_attrs *attrs; | |
7440 | ||
be69d00d | 7441 | BUG_ON(!(attrs = alloc_workqueue_attrs())); |
29c91e99 | 7442 | attrs->nice = std_nice[i]; |
29c91e99 | 7443 | unbound_std_wq_attrs[i] = attrs; |
8a2b7538 TH |
7444 | |
7445 | /* | |
7446 | * An ordered wq should have only one pwq as ordering is | |
7447 | * guaranteed by max_active which is enforced by pwqs. | |
8a2b7538 | 7448 | */ |
be69d00d | 7449 | BUG_ON(!(attrs = alloc_workqueue_attrs())); |
8a2b7538 | 7450 | attrs->nice = std_nice[i]; |
af73f5c9 | 7451 | attrs->ordered = true; |
8a2b7538 | 7452 | ordered_wq_attrs[i] = attrs; |
29c91e99 TH |
7453 | } |
7454 | ||
d320c038 | 7455 | system_wq = alloc_workqueue("events", 0, 0); |
1aabe902 | 7456 | system_highpri_wq = alloc_workqueue("events_highpri", WQ_HIGHPRI, 0); |
d320c038 | 7457 | system_long_wq = alloc_workqueue("events_long", 0, 0); |
f3421797 | 7458 | system_unbound_wq = alloc_workqueue("events_unbound", WQ_UNBOUND, |
636b927e | 7459 | WQ_MAX_ACTIVE); |
24d51add TH |
7460 | system_freezable_wq = alloc_workqueue("events_freezable", |
7461 | WQ_FREEZABLE, 0); | |
0668106c VK |
7462 | system_power_efficient_wq = alloc_workqueue("events_power_efficient", |
7463 | WQ_POWER_EFFICIENT, 0); | |
8318d6a6 | 7464 | system_freezable_power_efficient_wq = alloc_workqueue("events_freezable_pwr_efficient", |
0668106c VK |
7465 | WQ_FREEZABLE | WQ_POWER_EFFICIENT, |
7466 | 0); | |
4cb1ef64 TH |
7467 | system_bh_wq = alloc_workqueue("events_bh", WQ_BH, 0); |
7468 | system_bh_highpri_wq = alloc_workqueue("events_bh_highpri", | |
7469 | WQ_BH | WQ_HIGHPRI, 0); | |
1aabe902 | 7470 | BUG_ON(!system_wq || !system_highpri_wq || !system_long_wq || |
0668106c VK |
7471 | !system_unbound_wq || !system_freezable_wq || |
7472 | !system_power_efficient_wq || | |
4cb1ef64 TH |
7473 | !system_freezable_power_efficient_wq || |
7474 | !system_bh_wq || !system_bh_highpri_wq); | |
3347fa09 TH |
7475 | } |
7476 | ||
aa6fde93 TH |
7477 | static void __init wq_cpu_intensive_thresh_init(void) |
7478 | { | |
7479 | unsigned long thresh; | |
7480 | unsigned long bogo; | |
7481 | ||
dd64c873 Z |
7482 | pwq_release_worker = kthread_create_worker(0, "pool_workqueue_release"); |
7483 | BUG_ON(IS_ERR(pwq_release_worker)); | |
7484 | ||
aa6fde93 TH |
7485 | /* if the user set it to a specific value, keep it */ |
7486 | if (wq_cpu_intensive_thresh_us != ULONG_MAX) | |
7487 | return; | |
7488 | ||
7489 | /* | |
7490 | * The default of 10ms is derived from the fact that most modern (as of | |
7491 | * 2023) processors can do a lot in 10ms and that it's just below what | |
7492 | * most consider human-perceivable. However, the kernel also runs on a | |
7493 | * lot slower CPUs including microcontrollers where the threshold is way | |
7494 | * too low. | |
7495 | * | |
7496 | * Let's scale up the threshold upto 1 second if BogoMips is below 4000. | |
7497 | * This is by no means accurate but it doesn't have to be. The mechanism | |
7498 | * is still useful even when the threshold is fully scaled up. Also, as | |
7499 | * the reports would usually be applicable to everyone, some machines | |
7500 | * operating on longer thresholds won't significantly diminish their | |
7501 | * usefulness. | |
7502 | */ | |
7503 | thresh = 10 * USEC_PER_MSEC; | |
7504 | ||
7505 | /* see init/calibrate.c for lpj -> BogoMIPS calculation */ | |
7506 | bogo = max_t(unsigned long, loops_per_jiffy / 500000 * HZ, 1); | |
7507 | if (bogo < 4000) | |
7508 | thresh = min_t(unsigned long, thresh * 4000 / bogo, USEC_PER_SEC); | |
7509 | ||
7510 | pr_debug("wq_cpu_intensive_thresh: lpj=%lu BogoMIPS=%lu thresh_us=%lu\n", | |
7511 | loops_per_jiffy, bogo, thresh); | |
7512 | ||
7513 | wq_cpu_intensive_thresh_us = thresh; | |
7514 | } | |
7515 | ||
3347fa09 TH |
7516 | /** |
7517 | * workqueue_init - bring workqueue subsystem fully online | |
7518 | * | |
2930155b TH |
7519 | * This is the second step of three-staged workqueue subsystem initialization |
7520 | * and invoked as soon as kthreads can be created and scheduled. Workqueues have | |
7521 | * been created and work items queued on them, but there are no kworkers | |
7522 | * executing the work items yet. Populate the worker pools with the initial | |
7523 | * workers and enable future kworker creations. | |
3347fa09 | 7524 | */ |
2333e829 | 7525 | void __init workqueue_init(void) |
3347fa09 | 7526 | { |
2186d9f9 | 7527 | struct workqueue_struct *wq; |
3347fa09 TH |
7528 | struct worker_pool *pool; |
7529 | int cpu, bkt; | |
7530 | ||
aa6fde93 TH |
7531 | wq_cpu_intensive_thresh_init(); |
7532 | ||
2186d9f9 TH |
7533 | mutex_lock(&wq_pool_mutex); |
7534 | ||
2930155b TH |
7535 | /* |
7536 | * Per-cpu pools created earlier could be missing node hint. Fix them | |
7537 | * up. Also, create a rescuer for workqueues that requested it. | |
7538 | */ | |
2186d9f9 | 7539 | for_each_possible_cpu(cpu) { |
4cb1ef64 TH |
7540 | for_each_bh_worker_pool(pool, cpu) |
7541 | pool->node = cpu_to_node(cpu); | |
7542 | for_each_cpu_worker_pool(pool, cpu) | |
2186d9f9 | 7543 | pool->node = cpu_to_node(cpu); |
2186d9f9 TH |
7544 | } |
7545 | ||
40c17f75 | 7546 | list_for_each_entry(wq, &workqueues, list) { |
40c17f75 TH |
7547 | WARN(init_rescuer(wq), |
7548 | "workqueue: failed to create early rescuer for %s", | |
7549 | wq->name); | |
7550 | } | |
2186d9f9 TH |
7551 | |
7552 | mutex_unlock(&wq_pool_mutex); | |
7553 | ||
4cb1ef64 TH |
7554 | /* |
7555 | * Create the initial workers. A BH pool has one pseudo worker that | |
7556 | * represents the shared BH execution context and thus doesn't get | |
7557 | * affected by hotplug events. Create the BH pseudo workers for all | |
7558 | * possible CPUs here. | |
7559 | */ | |
7560 | for_each_possible_cpu(cpu) | |
7561 | for_each_bh_worker_pool(pool, cpu) | |
7562 | BUG_ON(!create_worker(pool)); | |
7563 | ||
3347fa09 TH |
7564 | for_each_online_cpu(cpu) { |
7565 | for_each_cpu_worker_pool(pool, cpu) { | |
7566 | pool->flags &= ~POOL_DISASSOCIATED; | |
7567 | BUG_ON(!create_worker(pool)); | |
7568 | } | |
7569 | } | |
7570 | ||
7571 | hash_for_each(unbound_pool_hash, bkt, pool, hash_node) | |
7572 | BUG_ON(!create_worker(pool)); | |
7573 | ||
7574 | wq_online = true; | |
82607adc | 7575 | wq_watchdog_init(); |
1da177e4 | 7576 | } |
c4f135d6 | 7577 | |
025e1684 TH |
7578 | /* |
7579 | * Initialize @pt by first initializing @pt->cpu_pod[] with pod IDs according to | |
7580 | * @cpu_shares_pod(). Each subset of CPUs that share a pod is assigned a unique | |
7581 | * and consecutive pod ID. The rest of @pt is initialized accordingly. | |
7582 | */ | |
7583 | static void __init init_pod_type(struct wq_pod_type *pt, | |
7584 | bool (*cpus_share_pod)(int, int)) | |
7585 | { | |
7586 | int cur, pre, cpu, pod; | |
7587 | ||
7588 | pt->nr_pods = 0; | |
7589 | ||
7590 | /* init @pt->cpu_pod[] according to @cpus_share_pod() */ | |
7591 | pt->cpu_pod = kcalloc(nr_cpu_ids, sizeof(pt->cpu_pod[0]), GFP_KERNEL); | |
7592 | BUG_ON(!pt->cpu_pod); | |
7593 | ||
7594 | for_each_possible_cpu(cur) { | |
7595 | for_each_possible_cpu(pre) { | |
7596 | if (pre >= cur) { | |
7597 | pt->cpu_pod[cur] = pt->nr_pods++; | |
7598 | break; | |
7599 | } | |
7600 | if (cpus_share_pod(cur, pre)) { | |
7601 | pt->cpu_pod[cur] = pt->cpu_pod[pre]; | |
7602 | break; | |
7603 | } | |
7604 | } | |
7605 | } | |
7606 | ||
7607 | /* init the rest to match @pt->cpu_pod[] */ | |
7608 | pt->pod_cpus = kcalloc(pt->nr_pods, sizeof(pt->pod_cpus[0]), GFP_KERNEL); | |
7609 | pt->pod_node = kcalloc(pt->nr_pods, sizeof(pt->pod_node[0]), GFP_KERNEL); | |
7610 | BUG_ON(!pt->pod_cpus || !pt->pod_node); | |
7611 | ||
7612 | for (pod = 0; pod < pt->nr_pods; pod++) | |
7613 | BUG_ON(!zalloc_cpumask_var(&pt->pod_cpus[pod], GFP_KERNEL)); | |
7614 | ||
7615 | for_each_possible_cpu(cpu) { | |
7616 | cpumask_set_cpu(cpu, pt->pod_cpus[pt->cpu_pod[cpu]]); | |
7617 | pt->pod_node[pt->cpu_pod[cpu]] = cpu_to_node(cpu); | |
7618 | } | |
7619 | } | |
7620 | ||
63c5484e TH |
7621 | static bool __init cpus_dont_share(int cpu0, int cpu1) |
7622 | { | |
7623 | return false; | |
7624 | } | |
7625 | ||
7626 | static bool __init cpus_share_smt(int cpu0, int cpu1) | |
7627 | { | |
7628 | #ifdef CONFIG_SCHED_SMT | |
7629 | return cpumask_test_cpu(cpu0, cpu_smt_mask(cpu1)); | |
7630 | #else | |
7631 | return false; | |
7632 | #endif | |
7633 | } | |
7634 | ||
025e1684 TH |
7635 | static bool __init cpus_share_numa(int cpu0, int cpu1) |
7636 | { | |
7637 | return cpu_to_node(cpu0) == cpu_to_node(cpu1); | |
7638 | } | |
7639 | ||
2930155b TH |
7640 | /** |
7641 | * workqueue_init_topology - initialize CPU pods for unbound workqueues | |
7642 | * | |
96068b60 | 7643 | * This is the third step of three-staged workqueue subsystem initialization and |
2930155b TH |
7644 | * invoked after SMP and topology information are fully initialized. It |
7645 | * initializes the unbound CPU pods accordingly. | |
7646 | */ | |
7647 | void __init workqueue_init_topology(void) | |
a86feae6 | 7648 | { |
2930155b | 7649 | struct workqueue_struct *wq; |
025e1684 | 7650 | int cpu; |
a86feae6 | 7651 | |
63c5484e TH |
7652 | init_pod_type(&wq_pod_types[WQ_AFFN_CPU], cpus_dont_share); |
7653 | init_pod_type(&wq_pod_types[WQ_AFFN_SMT], cpus_share_smt); | |
7654 | init_pod_type(&wq_pod_types[WQ_AFFN_CACHE], cpus_share_cache); | |
025e1684 | 7655 | init_pod_type(&wq_pod_types[WQ_AFFN_NUMA], cpus_share_numa); |
a86feae6 | 7656 | |
c5f8cd6c TH |
7657 | wq_topo_initialized = true; |
7658 | ||
2930155b | 7659 | mutex_lock(&wq_pool_mutex); |
a86feae6 | 7660 | |
2930155b TH |
7661 | /* |
7662 | * Workqueues allocated earlier would have all CPUs sharing the default | |
7663 | * worker pool. Explicitly call wq_update_pod() on all workqueue and CPU | |
7664 | * combinations to apply per-pod sharing. | |
7665 | */ | |
7666 | list_for_each_entry(wq, &workqueues, list) { | |
5797b1c1 | 7667 | for_each_online_cpu(cpu) |
2930155b | 7668 | wq_update_pod(wq, cpu, cpu, true); |
5797b1c1 TH |
7669 | if (wq->flags & WQ_UNBOUND) { |
7670 | mutex_lock(&wq->mutex); | |
7671 | wq_update_node_max_active(wq, -1); | |
7672 | mutex_unlock(&wq->mutex); | |
2930155b TH |
7673 | } |
7674 | } | |
7675 | ||
7676 | mutex_unlock(&wq_pool_mutex); | |
a86feae6 TH |
7677 | } |
7678 | ||
20bdedaf TH |
7679 | void __warn_flushing_systemwide_wq(void) |
7680 | { | |
7681 | pr_warn("WARNING: Flushing system-wide workqueues will be prohibited in near future.\n"); | |
7682 | dump_stack(); | |
7683 | } | |
c4f135d6 | 7684 | EXPORT_SYMBOL(__warn_flushing_systemwide_wq); |
ace3c549 | 7685 | |
7686 | static int __init workqueue_unbound_cpus_setup(char *str) | |
7687 | { | |
7688 | if (cpulist_parse(str, &wq_cmdline_cpumask) < 0) { | |
7689 | cpumask_clear(&wq_cmdline_cpumask); | |
7690 | pr_warn("workqueue.unbound_cpus: incorrect CPU range, using default\n"); | |
7691 | } | |
7692 | ||
7693 | return 1; | |
7694 | } | |
7695 | __setup("workqueue.unbound_cpus=", workqueue_unbound_cpus_setup); |