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 | 1789 | /** |
516d3dc9 WL |
1790 | * unplug_oldest_pwq - unplug the oldest pool_workqueue |
1791 | * @wq: workqueue_struct where its oldest pwq is to be unplugged | |
4c065dbc | 1792 | * |
516d3dc9 WL |
1793 | * This function should only be called for ordered workqueues where only the |
1794 | * oldest pwq is unplugged, the others are plugged to suspend execution to | |
1795 | * ensure proper work item ordering:: | |
4c065dbc WL |
1796 | * |
1797 | * dfl_pwq --------------+ [P] - plugged | |
1798 | * | | |
1799 | * v | |
1800 | * pwqs -> A -> B [P] -> C [P] (newest) | |
1801 | * | | | | |
1802 | * 1 3 5 | |
1803 | * | | | | |
1804 | * 2 4 6 | |
516d3dc9 WL |
1805 | * |
1806 | * When the oldest pwq is drained and removed, this function should be called | |
1807 | * to unplug the next oldest one to start its work item execution. Note that | |
1808 | * pwq's are linked into wq->pwqs with the oldest first, so the first one in | |
1809 | * the list is the oldest. | |
4c065dbc WL |
1810 | */ |
1811 | static void unplug_oldest_pwq(struct workqueue_struct *wq) | |
1812 | { | |
1813 | struct pool_workqueue *pwq; | |
1814 | ||
1815 | lockdep_assert_held(&wq->mutex); | |
1816 | ||
1817 | /* Caller should make sure that pwqs isn't empty before calling */ | |
1818 | pwq = list_first_entry_or_null(&wq->pwqs, struct pool_workqueue, | |
1819 | pwqs_node); | |
1820 | raw_spin_lock_irq(&pwq->pool->lock); | |
1821 | if (pwq->plugged) { | |
1822 | pwq->plugged = false; | |
1823 | if (pwq_activate_first_inactive(pwq, true)) | |
1824 | kick_pool(pwq->pool); | |
1825 | } | |
1826 | raw_spin_unlock_irq(&pwq->pool->lock); | |
1827 | } | |
1828 | ||
5797b1c1 TH |
1829 | /** |
1830 | * node_activate_pending_pwq - Activate a pending pwq on a wq_node_nr_active | |
1831 | * @nna: wq_node_nr_active to activate a pending pwq for | |
1832 | * @caller_pool: worker_pool the caller is locking | |
1833 | * | |
1834 | * Activate a pwq in @nna->pending_pwqs. Called with @caller_pool locked. | |
1835 | * @caller_pool may be unlocked and relocked to lock other worker_pools. | |
1836 | */ | |
1837 | static void node_activate_pending_pwq(struct wq_node_nr_active *nna, | |
1838 | struct worker_pool *caller_pool) | |
1839 | { | |
1840 | struct worker_pool *locked_pool = caller_pool; | |
1841 | struct pool_workqueue *pwq; | |
1842 | struct work_struct *work; | |
1843 | ||
1844 | lockdep_assert_held(&caller_pool->lock); | |
1845 | ||
1846 | raw_spin_lock(&nna->lock); | |
1847 | retry: | |
1848 | pwq = list_first_entry_or_null(&nna->pending_pwqs, | |
1849 | struct pool_workqueue, pending_node); | |
1850 | if (!pwq) | |
1851 | goto out_unlock; | |
1852 | ||
1853 | /* | |
1854 | * If @pwq is for a different pool than @locked_pool, we need to lock | |
1855 | * @pwq->pool->lock. Let's trylock first. If unsuccessful, do the unlock | |
1856 | * / lock dance. For that, we also need to release @nna->lock as it's | |
1857 | * nested inside pool locks. | |
1858 | */ | |
1859 | if (pwq->pool != locked_pool) { | |
1860 | raw_spin_unlock(&locked_pool->lock); | |
1861 | locked_pool = pwq->pool; | |
1862 | if (!raw_spin_trylock(&locked_pool->lock)) { | |
1863 | raw_spin_unlock(&nna->lock); | |
1864 | raw_spin_lock(&locked_pool->lock); | |
1865 | raw_spin_lock(&nna->lock); | |
1866 | goto retry; | |
1867 | } | |
1868 | } | |
1869 | ||
1870 | /* | |
1871 | * $pwq may not have any inactive work items due to e.g. cancellations. | |
1872 | * Drop it from pending_pwqs and see if there's another one. | |
1873 | */ | |
1874 | work = list_first_entry_or_null(&pwq->inactive_works, | |
1875 | struct work_struct, entry); | |
1876 | if (!work) { | |
1877 | list_del_init(&pwq->pending_node); | |
1878 | goto retry; | |
1879 | } | |
1880 | ||
1881 | /* | |
1882 | * Acquire an nr_active count and activate the inactive work item. If | |
1883 | * $pwq still has inactive work items, rotate it to the end of the | |
1884 | * pending_pwqs so that we round-robin through them. This means that | |
1885 | * inactive work items are not activated in queueing order which is fine | |
1886 | * given that there has never been any ordering across different pwqs. | |
1887 | */ | |
1888 | if (likely(tryinc_node_nr_active(nna))) { | |
1889 | pwq->nr_active++; | |
1890 | __pwq_activate_work(pwq, work); | |
1891 | ||
1892 | if (list_empty(&pwq->inactive_works)) | |
1893 | list_del_init(&pwq->pending_node); | |
1894 | else | |
1895 | list_move_tail(&pwq->pending_node, &nna->pending_pwqs); | |
1896 | ||
1897 | /* if activating a foreign pool, make sure it's running */ | |
1898 | if (pwq->pool != caller_pool) | |
1899 | kick_pool(pwq->pool); | |
1900 | } | |
1901 | ||
1902 | out_unlock: | |
1903 | raw_spin_unlock(&nna->lock); | |
1904 | if (locked_pool != caller_pool) { | |
1905 | raw_spin_unlock(&locked_pool->lock); | |
1906 | raw_spin_lock(&caller_pool->lock); | |
1907 | } | |
1908 | } | |
1909 | ||
1c270b79 TH |
1910 | /** |
1911 | * pwq_dec_nr_active - Retire an active count | |
1912 | * @pwq: pool_workqueue of interest | |
1913 | * | |
1914 | * Decrement @pwq's nr_active and try to activate the first inactive work item. | |
5797b1c1 | 1915 | * For unbound workqueues, this function may temporarily drop @pwq->pool->lock. |
1c270b79 TH |
1916 | */ |
1917 | static void pwq_dec_nr_active(struct pool_workqueue *pwq) | |
3aa62497 | 1918 | { |
1c270b79 | 1919 | struct worker_pool *pool = pwq->pool; |
91ccc6e7 | 1920 | struct wq_node_nr_active *nna = wq_node_nr_active(pwq->wq, pool->node); |
3aa62497 | 1921 | |
1c270b79 TH |
1922 | lockdep_assert_held(&pool->lock); |
1923 | ||
91ccc6e7 TH |
1924 | /* |
1925 | * @pwq->nr_active should be decremented for both percpu and unbound | |
1926 | * workqueues. | |
1927 | */ | |
1c270b79 | 1928 | pwq->nr_active--; |
91ccc6e7 TH |
1929 | |
1930 | /* | |
1931 | * For a percpu workqueue, it's simple. Just need to kick the first | |
1932 | * inactive work item on @pwq itself. | |
1933 | */ | |
1934 | if (!nna) { | |
5797b1c1 | 1935 | pwq_activate_first_inactive(pwq, false); |
91ccc6e7 TH |
1936 | return; |
1937 | } | |
1938 | ||
5797b1c1 TH |
1939 | /* |
1940 | * If @pwq is for an unbound workqueue, it's more complicated because | |
1941 | * multiple pwqs and pools may be sharing the nr_active count. When a | |
1942 | * pwq needs to wait for an nr_active count, it puts itself on | |
1943 | * $nna->pending_pwqs. The following atomic_dec_return()'s implied | |
1944 | * memory barrier is paired with smp_mb() in pwq_tryinc_nr_active() to | |
1945 | * guarantee that either we see non-empty pending_pwqs or they see | |
1946 | * decremented $nna->nr. | |
1947 | * | |
1948 | * $nna->max may change as CPUs come online/offline and @pwq->wq's | |
1949 | * max_active gets updated. However, it is guaranteed to be equal to or | |
1950 | * larger than @pwq->wq->min_active which is above zero unless freezing. | |
1951 | * This maintains the forward progress guarantee. | |
1952 | */ | |
1953 | if (atomic_dec_return(&nna->nr) >= READ_ONCE(nna->max)) | |
1954 | return; | |
1955 | ||
1956 | if (!list_empty(&nna->pending_pwqs)) | |
1957 | node_activate_pending_pwq(nna, pool); | |
3aa62497 LJ |
1958 | } |
1959 | ||
bf4ede01 | 1960 | /** |
112202d9 TH |
1961 | * pwq_dec_nr_in_flight - decrement pwq's nr_in_flight |
1962 | * @pwq: pwq of interest | |
c4560c2c | 1963 | * @work_data: work_data of work which left the queue |
bf4ede01 TH |
1964 | * |
1965 | * A work either has completed or is removed from pending queue, | |
112202d9 | 1966 | * decrement nr_in_flight of its pwq and handle workqueue flushing. |
bf4ede01 | 1967 | * |
dd6c3c54 TH |
1968 | * NOTE: |
1969 | * For unbound workqueues, this function may temporarily drop @pwq->pool->lock | |
1970 | * and thus should be called after all other state updates for the in-flight | |
1971 | * work item is complete. | |
1972 | * | |
bf4ede01 | 1973 | * CONTEXT: |
a9b8a985 | 1974 | * raw_spin_lock_irq(pool->lock). |
bf4ede01 | 1975 | */ |
c4560c2c | 1976 | static void pwq_dec_nr_in_flight(struct pool_workqueue *pwq, unsigned long work_data) |
bf4ede01 | 1977 | { |
c4560c2c LJ |
1978 | int color = get_work_color(work_data); |
1979 | ||
1c270b79 TH |
1980 | if (!(work_data & WORK_STRUCT_INACTIVE)) |
1981 | pwq_dec_nr_active(pwq); | |
018f3a13 | 1982 | |
112202d9 | 1983 | pwq->nr_in_flight[color]--; |
bf4ede01 | 1984 | |
bf4ede01 | 1985 | /* is flush in progress and are we at the flushing tip? */ |
112202d9 | 1986 | if (likely(pwq->flush_color != color)) |
8864b4e5 | 1987 | goto out_put; |
bf4ede01 TH |
1988 | |
1989 | /* are there still in-flight works? */ | |
112202d9 | 1990 | if (pwq->nr_in_flight[color]) |
8864b4e5 | 1991 | goto out_put; |
bf4ede01 | 1992 | |
112202d9 TH |
1993 | /* this pwq is done, clear flush_color */ |
1994 | pwq->flush_color = -1; | |
bf4ede01 TH |
1995 | |
1996 | /* | |
112202d9 | 1997 | * If this was the last pwq, wake up the first flusher. It |
bf4ede01 TH |
1998 | * will handle the rest. |
1999 | */ | |
112202d9 TH |
2000 | if (atomic_dec_and_test(&pwq->wq->nr_pwqs_to_flush)) |
2001 | complete(&pwq->wq->first_flusher->done); | |
8864b4e5 TH |
2002 | out_put: |
2003 | put_pwq(pwq); | |
bf4ede01 TH |
2004 | } |
2005 | ||
36e227d2 | 2006 | /** |
bbb68dfa | 2007 | * try_to_grab_pending - steal work item from worklist and disable irq |
36e227d2 TH |
2008 | * @work: work item to steal |
2009 | * @is_dwork: @work is a delayed_work | |
bbb68dfa | 2010 | * @flags: place to store irq state |
36e227d2 TH |
2011 | * |
2012 | * Try to grab PENDING bit of @work. This function can handle @work in any | |
d185af30 | 2013 | * stable state - idle, on timer or on worklist. |
36e227d2 | 2014 | * |
d185af30 | 2015 | * Return: |
3eb6b31b MCC |
2016 | * |
2017 | * ======== ================================================================ | |
36e227d2 TH |
2018 | * 1 if @work was pending and we successfully stole PENDING |
2019 | * 0 if @work was idle and we claimed PENDING | |
2020 | * -EAGAIN if PENDING couldn't be grabbed at the moment, safe to busy-retry | |
bbb68dfa TH |
2021 | * -ENOENT if someone else is canceling @work, this state may persist |
2022 | * for arbitrarily long | |
3eb6b31b | 2023 | * ======== ================================================================ |
36e227d2 | 2024 | * |
d185af30 | 2025 | * Note: |
bbb68dfa | 2026 | * On >= 0 return, the caller owns @work's PENDING bit. To avoid getting |
e0aecdd8 TH |
2027 | * interrupted while holding PENDING and @work off queue, irq must be |
2028 | * disabled on entry. This, combined with delayed_work->timer being | |
2029 | * irqsafe, ensures that we return -EAGAIN for finite short period of time. | |
bbb68dfa TH |
2030 | * |
2031 | * On successful return, >= 0, irq is disabled and the caller is | |
2032 | * responsible for releasing it using local_irq_restore(*@flags). | |
2033 | * | |
e0aecdd8 | 2034 | * This function is safe to call from any context including IRQ handler. |
bf4ede01 | 2035 | */ |
bbb68dfa TH |
2036 | static int try_to_grab_pending(struct work_struct *work, bool is_dwork, |
2037 | unsigned long *flags) | |
bf4ede01 | 2038 | { |
d565ed63 | 2039 | struct worker_pool *pool; |
112202d9 | 2040 | struct pool_workqueue *pwq; |
bf4ede01 | 2041 | |
bbb68dfa TH |
2042 | local_irq_save(*flags); |
2043 | ||
36e227d2 TH |
2044 | /* try to steal the timer if it exists */ |
2045 | if (is_dwork) { | |
2046 | struct delayed_work *dwork = to_delayed_work(work); | |
2047 | ||
e0aecdd8 TH |
2048 | /* |
2049 | * dwork->timer is irqsafe. If del_timer() fails, it's | |
2050 | * guaranteed that the timer is not queued anywhere and not | |
2051 | * running on the local CPU. | |
2052 | */ | |
36e227d2 TH |
2053 | if (likely(del_timer(&dwork->timer))) |
2054 | return 1; | |
2055 | } | |
2056 | ||
2057 | /* try to claim PENDING the normal way */ | |
bf4ede01 TH |
2058 | if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))) |
2059 | return 0; | |
2060 | ||
24acfb71 | 2061 | rcu_read_lock(); |
bf4ede01 TH |
2062 | /* |
2063 | * The queueing is in progress, or it is already queued. Try to | |
2064 | * steal it from ->worklist without clearing WORK_STRUCT_PENDING. | |
2065 | */ | |
d565ed63 TH |
2066 | pool = get_work_pool(work); |
2067 | if (!pool) | |
bbb68dfa | 2068 | goto fail; |
bf4ede01 | 2069 | |
a9b8a985 | 2070 | raw_spin_lock(&pool->lock); |
0b3dae68 | 2071 | /* |
112202d9 TH |
2072 | * work->data is guaranteed to point to pwq only while the work |
2073 | * item is queued on pwq->wq, and both updating work->data to point | |
2074 | * to pwq on queueing and to pool on dequeueing are done under | |
2075 | * pwq->pool->lock. This in turn guarantees that, if work->data | |
2076 | * points to pwq which is associated with a locked pool, the work | |
0b3dae68 LJ |
2077 | * item is currently queued on that pool. |
2078 | */ | |
112202d9 TH |
2079 | pwq = get_work_pwq(work); |
2080 | if (pwq && pwq->pool == pool) { | |
c70e1779 TH |
2081 | unsigned long work_data; |
2082 | ||
16062836 TH |
2083 | debug_work_deactivate(work); |
2084 | ||
2085 | /* | |
018f3a13 LJ |
2086 | * A cancelable inactive work item must be in the |
2087 | * pwq->inactive_works since a queued barrier can't be | |
2088 | * canceled (see the comments in insert_wq_barrier()). | |
2089 | * | |
f97a4a1a | 2090 | * An inactive work item cannot be grabbed directly because |
d812796e | 2091 | * it might have linked barrier work items which, if left |
f97a4a1a | 2092 | * on the inactive_works list, will confuse pwq->nr_active |
16062836 TH |
2093 | * management later on and cause stall. Make sure the work |
2094 | * item is activated before grabbing. | |
2095 | */ | |
4c638030 | 2096 | pwq_activate_work(pwq, work); |
16062836 TH |
2097 | |
2098 | list_del_init(&work->entry); | |
16062836 | 2099 | |
c70e1779 TH |
2100 | /* |
2101 | * work->data points to pwq iff queued. Let's point to pool. As | |
2102 | * this destroys work->data needed by the next step, stash it. | |
2103 | */ | |
2104 | work_data = *work_data_bits(work); | |
16062836 TH |
2105 | set_work_pool_and_keep_pending(work, pool->id); |
2106 | ||
dd6c3c54 | 2107 | /* must be the last step, see the function comment */ |
c70e1779 | 2108 | pwq_dec_nr_in_flight(pwq, work_data); |
dd6c3c54 | 2109 | |
a9b8a985 | 2110 | raw_spin_unlock(&pool->lock); |
24acfb71 | 2111 | rcu_read_unlock(); |
16062836 | 2112 | return 1; |
bf4ede01 | 2113 | } |
a9b8a985 | 2114 | raw_spin_unlock(&pool->lock); |
bbb68dfa | 2115 | fail: |
24acfb71 | 2116 | rcu_read_unlock(); |
bbb68dfa TH |
2117 | local_irq_restore(*flags); |
2118 | if (work_is_canceling(work)) | |
2119 | return -ENOENT; | |
2120 | cpu_relax(); | |
36e227d2 | 2121 | return -EAGAIN; |
bf4ede01 TH |
2122 | } |
2123 | ||
4690c4ab | 2124 | /** |
706026c2 | 2125 | * insert_work - insert a work into a pool |
112202d9 | 2126 | * @pwq: pwq @work belongs to |
4690c4ab TH |
2127 | * @work: work to insert |
2128 | * @head: insertion point | |
2129 | * @extra_flags: extra WORK_STRUCT_* flags to set | |
2130 | * | |
112202d9 | 2131 | * Insert @work which belongs to @pwq after @head. @extra_flags is or'd to |
706026c2 | 2132 | * work_struct flags. |
4690c4ab TH |
2133 | * |
2134 | * CONTEXT: | |
a9b8a985 | 2135 | * raw_spin_lock_irq(pool->lock). |
4690c4ab | 2136 | */ |
112202d9 TH |
2137 | static void insert_work(struct pool_workqueue *pwq, struct work_struct *work, |
2138 | struct list_head *head, unsigned int extra_flags) | |
b89deed3 | 2139 | { |
fe089f87 | 2140 | debug_work_activate(work); |
e22bee78 | 2141 | |
e89a85d6 | 2142 | /* record the work call stack in order to print it in KASAN reports */ |
f70da745 | 2143 | kasan_record_aux_stack_noalloc(work); |
e89a85d6 | 2144 | |
4690c4ab | 2145 | /* we own @work, set data and link */ |
112202d9 | 2146 | set_work_pwq(work, pwq, extra_flags); |
1a4d9b0a | 2147 | list_add_tail(&work->entry, head); |
8864b4e5 | 2148 | get_pwq(pwq); |
b89deed3 ON |
2149 | } |
2150 | ||
c8efcc25 TH |
2151 | /* |
2152 | * Test whether @work is being queued from another work executing on the | |
8d03ecfe | 2153 | * same workqueue. |
c8efcc25 TH |
2154 | */ |
2155 | static bool is_chained_work(struct workqueue_struct *wq) | |
2156 | { | |
8d03ecfe TH |
2157 | struct worker *worker; |
2158 | ||
2159 | worker = current_wq_worker(); | |
2160 | /* | |
bf393fd4 | 2161 | * Return %true iff I'm a worker executing a work item on @wq. If |
8d03ecfe TH |
2162 | * I'm @worker, it's safe to dereference it without locking. |
2163 | */ | |
112202d9 | 2164 | return worker && worker->current_pwq->wq == wq; |
c8efcc25 TH |
2165 | } |
2166 | ||
ef557180 MG |
2167 | /* |
2168 | * When queueing an unbound work item to a wq, prefer local CPU if allowed | |
2169 | * by wq_unbound_cpumask. Otherwise, round robin among the allowed ones to | |
2170 | * avoid perturbing sensitive tasks. | |
2171 | */ | |
2172 | static int wq_select_unbound_cpu(int cpu) | |
2173 | { | |
2174 | int new_cpu; | |
2175 | ||
f303fccb TH |
2176 | if (likely(!wq_debug_force_rr_cpu)) { |
2177 | if (cpumask_test_cpu(cpu, wq_unbound_cpumask)) | |
2178 | return cpu; | |
a8ec5880 AF |
2179 | } else { |
2180 | pr_warn_once("workqueue: round-robin CPU selection forced, expect performance impact\n"); | |
f303fccb TH |
2181 | } |
2182 | ||
ef557180 MG |
2183 | new_cpu = __this_cpu_read(wq_rr_cpu_last); |
2184 | new_cpu = cpumask_next_and(new_cpu, wq_unbound_cpumask, cpu_online_mask); | |
2185 | if (unlikely(new_cpu >= nr_cpu_ids)) { | |
2186 | new_cpu = cpumask_first_and(wq_unbound_cpumask, cpu_online_mask); | |
2187 | if (unlikely(new_cpu >= nr_cpu_ids)) | |
2188 | return cpu; | |
2189 | } | |
2190 | __this_cpu_write(wq_rr_cpu_last, new_cpu); | |
2191 | ||
2192 | return new_cpu; | |
2193 | } | |
2194 | ||
d84ff051 | 2195 | static void __queue_work(int cpu, struct workqueue_struct *wq, |
1da177e4 LT |
2196 | struct work_struct *work) |
2197 | { | |
112202d9 | 2198 | struct pool_workqueue *pwq; |
fe089f87 | 2199 | struct worker_pool *last_pool, *pool; |
8a2e8e5d | 2200 | unsigned int work_flags; |
b75cac93 | 2201 | unsigned int req_cpu = cpu; |
8930caba TH |
2202 | |
2203 | /* | |
2204 | * While a work item is PENDING && off queue, a task trying to | |
2205 | * steal the PENDING will busy-loop waiting for it to either get | |
2206 | * queued or lose PENDING. Grabbing PENDING and queueing should | |
2207 | * happen with IRQ disabled. | |
2208 | */ | |
8e8eb730 | 2209 | lockdep_assert_irqs_disabled(); |
1da177e4 | 2210 | |
1e19ffc6 | 2211 | |
33e3f0a3 RC |
2212 | /* |
2213 | * For a draining wq, only works from the same workqueue are | |
2214 | * allowed. The __WQ_DESTROYING helps to spot the issue that | |
2215 | * queues a new work item to a wq after destroy_workqueue(wq). | |
2216 | */ | |
2217 | if (unlikely(wq->flags & (__WQ_DESTROYING | __WQ_DRAINING) && | |
2218 | WARN_ON_ONCE(!is_chained_work(wq)))) | |
e41e704b | 2219 | return; |
24acfb71 | 2220 | rcu_read_lock(); |
9e8cd2f5 | 2221 | retry: |
c9178087 | 2222 | /* pwq which will be used unless @work is executing elsewhere */ |
636b927e TH |
2223 | if (req_cpu == WORK_CPU_UNBOUND) { |
2224 | if (wq->flags & WQ_UNBOUND) | |
aa202f1f | 2225 | cpu = wq_select_unbound_cpu(raw_smp_processor_id()); |
636b927e | 2226 | else |
aa202f1f | 2227 | cpu = raw_smp_processor_id(); |
aa202f1f | 2228 | } |
dbf2576e | 2229 | |
636b927e | 2230 | pwq = rcu_dereference(*per_cpu_ptr(wq->cpu_pwq, cpu)); |
fe089f87 TH |
2231 | pool = pwq->pool; |
2232 | ||
c9178087 TH |
2233 | /* |
2234 | * If @work was previously on a different pool, it might still be | |
2235 | * running there, in which case the work needs to be queued on that | |
2236 | * pool to guarantee non-reentrancy. | |
2237 | */ | |
2238 | last_pool = get_work_pool(work); | |
fe089f87 | 2239 | if (last_pool && last_pool != pool) { |
c9178087 | 2240 | struct worker *worker; |
18aa9eff | 2241 | |
a9b8a985 | 2242 | raw_spin_lock(&last_pool->lock); |
18aa9eff | 2243 | |
c9178087 | 2244 | worker = find_worker_executing_work(last_pool, work); |
18aa9eff | 2245 | |
c9178087 TH |
2246 | if (worker && worker->current_pwq->wq == wq) { |
2247 | pwq = worker->current_pwq; | |
fe089f87 TH |
2248 | pool = pwq->pool; |
2249 | WARN_ON_ONCE(pool != last_pool); | |
8930caba | 2250 | } else { |
c9178087 | 2251 | /* meh... not running there, queue here */ |
a9b8a985 | 2252 | raw_spin_unlock(&last_pool->lock); |
fe089f87 | 2253 | raw_spin_lock(&pool->lock); |
8930caba | 2254 | } |
f3421797 | 2255 | } else { |
fe089f87 | 2256 | raw_spin_lock(&pool->lock); |
502ca9d8 TH |
2257 | } |
2258 | ||
9e8cd2f5 | 2259 | /* |
636b927e TH |
2260 | * pwq is determined and locked. For unbound pools, we could have raced |
2261 | * with pwq release and it could already be dead. If its refcnt is zero, | |
2262 | * repeat pwq selection. Note that unbound pwqs never die without | |
2263 | * another pwq replacing it in cpu_pwq or while work items are executing | |
2264 | * on it, so the retrying is guaranteed to make forward-progress. | |
9e8cd2f5 TH |
2265 | */ |
2266 | if (unlikely(!pwq->refcnt)) { | |
2267 | if (wq->flags & WQ_UNBOUND) { | |
fe089f87 | 2268 | raw_spin_unlock(&pool->lock); |
9e8cd2f5 TH |
2269 | cpu_relax(); |
2270 | goto retry; | |
2271 | } | |
2272 | /* oops */ | |
2273 | WARN_ONCE(true, "workqueue: per-cpu pwq for %s on cpu%d has 0 refcnt", | |
2274 | wq->name, cpu); | |
2275 | } | |
2276 | ||
112202d9 TH |
2277 | /* pwq determined, queue */ |
2278 | trace_workqueue_queue_work(req_cpu, pwq, work); | |
502ca9d8 | 2279 | |
24acfb71 TG |
2280 | if (WARN_ON(!list_empty(&work->entry))) |
2281 | goto out; | |
1e19ffc6 | 2282 | |
112202d9 TH |
2283 | pwq->nr_in_flight[pwq->work_color]++; |
2284 | work_flags = work_color_to_flags(pwq->work_color); | |
1e19ffc6 | 2285 | |
a045a272 TH |
2286 | /* |
2287 | * Limit the number of concurrently active work items to max_active. | |
2288 | * @work must also queue behind existing inactive work items to maintain | |
2289 | * ordering when max_active changes. See wq_adjust_max_active(). | |
2290 | */ | |
5797b1c1 | 2291 | if (list_empty(&pwq->inactive_works) && pwq_tryinc_nr_active(pwq, false)) { |
fe089f87 TH |
2292 | if (list_empty(&pool->worklist)) |
2293 | pool->watchdog_ts = jiffies; | |
2294 | ||
cdadf009 | 2295 | trace_workqueue_activate_work(work); |
fe089f87 | 2296 | insert_work(pwq, work, &pool->worklist, work_flags); |
0219a352 | 2297 | kick_pool(pool); |
8a2e8e5d | 2298 | } else { |
f97a4a1a | 2299 | work_flags |= WORK_STRUCT_INACTIVE; |
fe089f87 | 2300 | insert_work(pwq, work, &pwq->inactive_works, work_flags); |
8a2e8e5d | 2301 | } |
1e19ffc6 | 2302 | |
24acfb71 | 2303 | out: |
fe089f87 | 2304 | raw_spin_unlock(&pool->lock); |
24acfb71 | 2305 | rcu_read_unlock(); |
1da177e4 LT |
2306 | } |
2307 | ||
0fcb78c2 | 2308 | /** |
c1a220e7 ZR |
2309 | * queue_work_on - queue work on specific cpu |
2310 | * @cpu: CPU number to execute work on | |
0fcb78c2 REB |
2311 | * @wq: workqueue to use |
2312 | * @work: work to queue | |
2313 | * | |
c1a220e7 | 2314 | * We queue the work to a specific CPU, the caller must ensure it |
443378f0 PM |
2315 | * can't go away. Callers that fail to ensure that the specified |
2316 | * CPU cannot go away will execute on a randomly chosen CPU. | |
854f5cc5 PM |
2317 | * But note well that callers specifying a CPU that never has been |
2318 | * online will get a splat. | |
d185af30 YB |
2319 | * |
2320 | * Return: %false if @work was already on a queue, %true otherwise. | |
1da177e4 | 2321 | */ |
d4283e93 TH |
2322 | bool queue_work_on(int cpu, struct workqueue_struct *wq, |
2323 | struct work_struct *work) | |
1da177e4 | 2324 | { |
d4283e93 | 2325 | bool ret = false; |
8930caba | 2326 | unsigned long flags; |
ef1ca236 | 2327 | |
8930caba | 2328 | local_irq_save(flags); |
c1a220e7 | 2329 | |
22df02bb | 2330 | if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))) { |
4690c4ab | 2331 | __queue_work(cpu, wq, work); |
d4283e93 | 2332 | ret = true; |
c1a220e7 | 2333 | } |
ef1ca236 | 2334 | |
8930caba | 2335 | local_irq_restore(flags); |
1da177e4 LT |
2336 | return ret; |
2337 | } | |
ad7b1f84 | 2338 | EXPORT_SYMBOL(queue_work_on); |
1da177e4 | 2339 | |
8204e0c1 | 2340 | /** |
fef59c9c | 2341 | * select_numa_node_cpu - Select a CPU based on NUMA node |
8204e0c1 AD |
2342 | * @node: NUMA node ID that we want to select a CPU from |
2343 | * | |
2344 | * This function will attempt to find a "random" cpu available on a given | |
2345 | * node. If there are no CPUs available on the given node it will return | |
2346 | * WORK_CPU_UNBOUND indicating that we should just schedule to any | |
2347 | * available CPU if we need to schedule this work. | |
2348 | */ | |
fef59c9c | 2349 | static int select_numa_node_cpu(int node) |
8204e0c1 AD |
2350 | { |
2351 | int cpu; | |
2352 | ||
8204e0c1 AD |
2353 | /* Delay binding to CPU if node is not valid or online */ |
2354 | if (node < 0 || node >= MAX_NUMNODES || !node_online(node)) | |
2355 | return WORK_CPU_UNBOUND; | |
2356 | ||
2357 | /* Use local node/cpu if we are already there */ | |
2358 | cpu = raw_smp_processor_id(); | |
2359 | if (node == cpu_to_node(cpu)) | |
2360 | return cpu; | |
2361 | ||
2362 | /* Use "random" otherwise know as "first" online CPU of node */ | |
2363 | cpu = cpumask_any_and(cpumask_of_node(node), cpu_online_mask); | |
2364 | ||
2365 | /* If CPU is valid return that, otherwise just defer */ | |
2366 | return cpu < nr_cpu_ids ? cpu : WORK_CPU_UNBOUND; | |
2367 | } | |
2368 | ||
2369 | /** | |
2370 | * queue_work_node - queue work on a "random" cpu for a given NUMA node | |
2371 | * @node: NUMA node that we are targeting the work for | |
2372 | * @wq: workqueue to use | |
2373 | * @work: work to queue | |
2374 | * | |
2375 | * We queue the work to a "random" CPU within a given NUMA node. The basic | |
2376 | * idea here is to provide a way to somehow associate work with a given | |
2377 | * NUMA node. | |
2378 | * | |
2379 | * This function will only make a best effort attempt at getting this onto | |
2380 | * the right NUMA node. If no node is requested or the requested node is | |
2381 | * offline then we just fall back to standard queue_work behavior. | |
2382 | * | |
2383 | * Currently the "random" CPU ends up being the first available CPU in the | |
2384 | * intersection of cpu_online_mask and the cpumask of the node, unless we | |
2385 | * are running on the node. In that case we just use the current CPU. | |
2386 | * | |
2387 | * Return: %false if @work was already on a queue, %true otherwise. | |
2388 | */ | |
2389 | bool queue_work_node(int node, struct workqueue_struct *wq, | |
2390 | struct work_struct *work) | |
2391 | { | |
2392 | unsigned long flags; | |
2393 | bool ret = false; | |
2394 | ||
2395 | /* | |
2396 | * This current implementation is specific to unbound workqueues. | |
2397 | * Specifically we only return the first available CPU for a given | |
2398 | * node instead of cycling through individual CPUs within the node. | |
2399 | * | |
2400 | * If this is used with a per-cpu workqueue then the logic in | |
2401 | * workqueue_select_cpu_near would need to be updated to allow for | |
2402 | * some round robin type logic. | |
2403 | */ | |
2404 | WARN_ON_ONCE(!(wq->flags & WQ_UNBOUND)); | |
2405 | ||
2406 | local_irq_save(flags); | |
2407 | ||
2408 | if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))) { | |
fef59c9c | 2409 | int cpu = select_numa_node_cpu(node); |
8204e0c1 AD |
2410 | |
2411 | __queue_work(cpu, wq, work); | |
2412 | ret = true; | |
2413 | } | |
2414 | ||
2415 | local_irq_restore(flags); | |
2416 | return ret; | |
2417 | } | |
2418 | EXPORT_SYMBOL_GPL(queue_work_node); | |
2419 | ||
8c20feb6 | 2420 | void delayed_work_timer_fn(struct timer_list *t) |
1da177e4 | 2421 | { |
8c20feb6 | 2422 | struct delayed_work *dwork = from_timer(dwork, t, timer); |
1da177e4 | 2423 | |
e0aecdd8 | 2424 | /* should have been called from irqsafe timer with irq already off */ |
60c057bc | 2425 | __queue_work(dwork->cpu, dwork->wq, &dwork->work); |
1da177e4 | 2426 | } |
1438ade5 | 2427 | EXPORT_SYMBOL(delayed_work_timer_fn); |
1da177e4 | 2428 | |
7beb2edf TH |
2429 | static void __queue_delayed_work(int cpu, struct workqueue_struct *wq, |
2430 | struct delayed_work *dwork, unsigned long delay) | |
1da177e4 | 2431 | { |
7beb2edf TH |
2432 | struct timer_list *timer = &dwork->timer; |
2433 | struct work_struct *work = &dwork->work; | |
7beb2edf | 2434 | |
637fdbae | 2435 | WARN_ON_ONCE(!wq); |
4b243563 | 2436 | WARN_ON_ONCE(timer->function != delayed_work_timer_fn); |
fc4b514f TH |
2437 | WARN_ON_ONCE(timer_pending(timer)); |
2438 | WARN_ON_ONCE(!list_empty(&work->entry)); | |
7beb2edf | 2439 | |
8852aac2 TH |
2440 | /* |
2441 | * If @delay is 0, queue @dwork->work immediately. This is for | |
2442 | * both optimization and correctness. The earliest @timer can | |
2443 | * expire is on the closest next tick and delayed_work users depend | |
2444 | * on that there's no such delay when @delay is 0. | |
2445 | */ | |
2446 | if (!delay) { | |
2447 | __queue_work(cpu, wq, &dwork->work); | |
2448 | return; | |
2449 | } | |
2450 | ||
60c057bc | 2451 | dwork->wq = wq; |
1265057f | 2452 | dwork->cpu = cpu; |
7beb2edf TH |
2453 | timer->expires = jiffies + delay; |
2454 | ||
aae17ebb LB |
2455 | if (housekeeping_enabled(HK_TYPE_TIMER)) { |
2456 | /* If the current cpu is a housekeeping cpu, use it. */ | |
2457 | cpu = smp_processor_id(); | |
2458 | if (!housekeeping_test_cpu(cpu, HK_TYPE_TIMER)) | |
2459 | cpu = housekeeping_any_cpu(HK_TYPE_TIMER); | |
041bd12e | 2460 | add_timer_on(timer, cpu); |
aae17ebb LB |
2461 | } else { |
2462 | if (likely(cpu == WORK_CPU_UNBOUND)) | |
2463 | add_timer(timer); | |
2464 | else | |
2465 | add_timer_on(timer, cpu); | |
2466 | } | |
1da177e4 LT |
2467 | } |
2468 | ||
0fcb78c2 REB |
2469 | /** |
2470 | * queue_delayed_work_on - queue work on specific CPU after delay | |
2471 | * @cpu: CPU number to execute work on | |
2472 | * @wq: workqueue to use | |
af9997e4 | 2473 | * @dwork: work to queue |
0fcb78c2 REB |
2474 | * @delay: number of jiffies to wait before queueing |
2475 | * | |
d185af30 | 2476 | * Return: %false if @work was already on a queue, %true otherwise. If |
715f1300 TH |
2477 | * @delay is zero and @dwork is idle, it will be scheduled for immediate |
2478 | * execution. | |
0fcb78c2 | 2479 | */ |
d4283e93 TH |
2480 | bool queue_delayed_work_on(int cpu, struct workqueue_struct *wq, |
2481 | struct delayed_work *dwork, unsigned long delay) | |
7a6bc1cd | 2482 | { |
52bad64d | 2483 | struct work_struct *work = &dwork->work; |
d4283e93 | 2484 | bool ret = false; |
8930caba | 2485 | unsigned long flags; |
7a6bc1cd | 2486 | |
8930caba TH |
2487 | /* read the comment in __queue_work() */ |
2488 | local_irq_save(flags); | |
7a6bc1cd | 2489 | |
22df02bb | 2490 | if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))) { |
7beb2edf | 2491 | __queue_delayed_work(cpu, wq, dwork, delay); |
d4283e93 | 2492 | ret = true; |
7a6bc1cd | 2493 | } |
8a3e77cc | 2494 | |
8930caba | 2495 | local_irq_restore(flags); |
7a6bc1cd VP |
2496 | return ret; |
2497 | } | |
ad7b1f84 | 2498 | EXPORT_SYMBOL(queue_delayed_work_on); |
c7fc77f7 | 2499 | |
8376fe22 TH |
2500 | /** |
2501 | * mod_delayed_work_on - modify delay of or queue a delayed work on specific CPU | |
2502 | * @cpu: CPU number to execute work on | |
2503 | * @wq: workqueue to use | |
2504 | * @dwork: work to queue | |
2505 | * @delay: number of jiffies to wait before queueing | |
2506 | * | |
2507 | * If @dwork is idle, equivalent to queue_delayed_work_on(); otherwise, | |
2508 | * modify @dwork's timer so that it expires after @delay. If @delay is | |
2509 | * zero, @work is guaranteed to be scheduled immediately regardless of its | |
2510 | * current state. | |
2511 | * | |
d185af30 | 2512 | * Return: %false if @dwork was idle and queued, %true if @dwork was |
8376fe22 TH |
2513 | * pending and its timer was modified. |
2514 | * | |
e0aecdd8 | 2515 | * This function is safe to call from any context including IRQ handler. |
8376fe22 TH |
2516 | * See try_to_grab_pending() for details. |
2517 | */ | |
2518 | bool mod_delayed_work_on(int cpu, struct workqueue_struct *wq, | |
2519 | struct delayed_work *dwork, unsigned long delay) | |
2520 | { | |
2521 | unsigned long flags; | |
2522 | int ret; | |
c7fc77f7 | 2523 | |
8376fe22 TH |
2524 | do { |
2525 | ret = try_to_grab_pending(&dwork->work, true, &flags); | |
2526 | } while (unlikely(ret == -EAGAIN)); | |
63bc0362 | 2527 | |
8376fe22 TH |
2528 | if (likely(ret >= 0)) { |
2529 | __queue_delayed_work(cpu, wq, dwork, delay); | |
2530 | local_irq_restore(flags); | |
7a6bc1cd | 2531 | } |
8376fe22 TH |
2532 | |
2533 | /* -ENOENT from try_to_grab_pending() becomes %true */ | |
7a6bc1cd VP |
2534 | return ret; |
2535 | } | |
8376fe22 TH |
2536 | EXPORT_SYMBOL_GPL(mod_delayed_work_on); |
2537 | ||
05f0fe6b TH |
2538 | static void rcu_work_rcufn(struct rcu_head *rcu) |
2539 | { | |
2540 | struct rcu_work *rwork = container_of(rcu, struct rcu_work, rcu); | |
2541 | ||
2542 | /* read the comment in __queue_work() */ | |
2543 | local_irq_disable(); | |
2544 | __queue_work(WORK_CPU_UNBOUND, rwork->wq, &rwork->work); | |
2545 | local_irq_enable(); | |
2546 | } | |
2547 | ||
2548 | /** | |
2549 | * queue_rcu_work - queue work after a RCU grace period | |
2550 | * @wq: workqueue to use | |
2551 | * @rwork: work to queue | |
2552 | * | |
2553 | * Return: %false if @rwork was already pending, %true otherwise. Note | |
2554 | * that a full RCU grace period is guaranteed only after a %true return. | |
bf393fd4 | 2555 | * While @rwork is guaranteed to be executed after a %false return, the |
05f0fe6b TH |
2556 | * execution may happen before a full RCU grace period has passed. |
2557 | */ | |
2558 | bool queue_rcu_work(struct workqueue_struct *wq, struct rcu_work *rwork) | |
2559 | { | |
2560 | struct work_struct *work = &rwork->work; | |
2561 | ||
2562 | if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))) { | |
2563 | rwork->wq = wq; | |
a7e30c0e | 2564 | call_rcu_hurry(&rwork->rcu, rcu_work_rcufn); |
05f0fe6b TH |
2565 | return true; |
2566 | } | |
2567 | ||
2568 | return false; | |
2569 | } | |
2570 | EXPORT_SYMBOL(queue_rcu_work); | |
2571 | ||
f7537df5 | 2572 | static struct worker *alloc_worker(int node) |
c34056a3 TH |
2573 | { |
2574 | struct worker *worker; | |
2575 | ||
f7537df5 | 2576 | worker = kzalloc_node(sizeof(*worker), GFP_KERNEL, node); |
c8e55f36 TH |
2577 | if (worker) { |
2578 | INIT_LIST_HEAD(&worker->entry); | |
affee4b2 | 2579 | INIT_LIST_HEAD(&worker->scheduled); |
da028469 | 2580 | INIT_LIST_HEAD(&worker->node); |
e22bee78 TH |
2581 | /* on creation a worker is in !idle && prep state */ |
2582 | worker->flags = WORKER_PREP; | |
c8e55f36 | 2583 | } |
c34056a3 TH |
2584 | return worker; |
2585 | } | |
2586 | ||
9546b29e TH |
2587 | static cpumask_t *pool_allowed_cpus(struct worker_pool *pool) |
2588 | { | |
8639eceb TH |
2589 | if (pool->cpu < 0 && pool->attrs->affn_strict) |
2590 | return pool->attrs->__pod_cpumask; | |
2591 | else | |
2592 | return pool->attrs->cpumask; | |
9546b29e TH |
2593 | } |
2594 | ||
4736cbf7 LJ |
2595 | /** |
2596 | * worker_attach_to_pool() - attach a worker to a pool | |
2597 | * @worker: worker to be attached | |
2598 | * @pool: the target pool | |
2599 | * | |
2600 | * Attach @worker to @pool. Once attached, the %WORKER_UNBOUND flag and | |
2601 | * cpu-binding of @worker are kept coordinated with the pool across | |
2602 | * cpu-[un]hotplugs. | |
2603 | */ | |
2604 | static void worker_attach_to_pool(struct worker *worker, | |
4cb1ef64 | 2605 | struct worker_pool *pool) |
4736cbf7 | 2606 | { |
1258fae7 | 2607 | mutex_lock(&wq_pool_attach_mutex); |
4736cbf7 | 2608 | |
4736cbf7 | 2609 | /* |
4cb1ef64 TH |
2610 | * The wq_pool_attach_mutex ensures %POOL_DISASSOCIATED remains stable |
2611 | * across this function. See the comments above the flag definition for | |
2612 | * details. BH workers are, while per-CPU, always DISASSOCIATED. | |
4736cbf7 | 2613 | */ |
4cb1ef64 | 2614 | if (pool->flags & POOL_DISASSOCIATED) { |
4736cbf7 | 2615 | worker->flags |= WORKER_UNBOUND; |
4cb1ef64 TH |
2616 | } else { |
2617 | WARN_ON_ONCE(pool->flags & POOL_BH); | |
5c25b5ff | 2618 | kthread_set_per_cpu(worker->task, pool->cpu); |
4cb1ef64 | 2619 | } |
4736cbf7 | 2620 | |
640f17c8 | 2621 | if (worker->rescue_wq) |
9546b29e | 2622 | set_cpus_allowed_ptr(worker->task, pool_allowed_cpus(pool)); |
640f17c8 | 2623 | |
4736cbf7 | 2624 | list_add_tail(&worker->node, &pool->workers); |
a2d812a2 | 2625 | worker->pool = pool; |
4736cbf7 | 2626 | |
1258fae7 | 2627 | mutex_unlock(&wq_pool_attach_mutex); |
4736cbf7 LJ |
2628 | } |
2629 | ||
60f5a4bc LJ |
2630 | /** |
2631 | * worker_detach_from_pool() - detach a worker from its pool | |
2632 | * @worker: worker which is attached to its pool | |
60f5a4bc | 2633 | * |
4736cbf7 LJ |
2634 | * Undo the attaching which had been done in worker_attach_to_pool(). The |
2635 | * caller worker shouldn't access to the pool after detached except it has | |
2636 | * other reference to the pool. | |
60f5a4bc | 2637 | */ |
a2d812a2 | 2638 | static void worker_detach_from_pool(struct worker *worker) |
60f5a4bc | 2639 | { |
a2d812a2 | 2640 | struct worker_pool *pool = worker->pool; |
60f5a4bc LJ |
2641 | struct completion *detach_completion = NULL; |
2642 | ||
4cb1ef64 TH |
2643 | /* there is one permanent BH worker per CPU which should never detach */ |
2644 | WARN_ON_ONCE(pool->flags & POOL_BH); | |
2645 | ||
1258fae7 | 2646 | mutex_lock(&wq_pool_attach_mutex); |
a2d812a2 | 2647 | |
5c25b5ff | 2648 | kthread_set_per_cpu(worker->task, -1); |
da028469 | 2649 | list_del(&worker->node); |
a2d812a2 TH |
2650 | worker->pool = NULL; |
2651 | ||
e02b9312 | 2652 | if (list_empty(&pool->workers) && list_empty(&pool->dying_workers)) |
60f5a4bc | 2653 | detach_completion = pool->detach_completion; |
1258fae7 | 2654 | mutex_unlock(&wq_pool_attach_mutex); |
60f5a4bc | 2655 | |
b62c0751 LJ |
2656 | /* clear leftover flags without pool->lock after it is detached */ |
2657 | worker->flags &= ~(WORKER_UNBOUND | WORKER_REBOUND); | |
2658 | ||
60f5a4bc LJ |
2659 | if (detach_completion) |
2660 | complete(detach_completion); | |
2661 | } | |
2662 | ||
c34056a3 TH |
2663 | /** |
2664 | * create_worker - create a new workqueue worker | |
63d95a91 | 2665 | * @pool: pool the new worker will belong to |
c34056a3 | 2666 | * |
051e1850 | 2667 | * Create and start a new worker which is attached to @pool. |
c34056a3 TH |
2668 | * |
2669 | * CONTEXT: | |
2670 | * Might sleep. Does GFP_KERNEL allocations. | |
2671 | * | |
d185af30 | 2672 | * Return: |
c34056a3 TH |
2673 | * Pointer to the newly created worker. |
2674 | */ | |
bc2ae0f5 | 2675 | static struct worker *create_worker(struct worker_pool *pool) |
c34056a3 | 2676 | { |
e441b56f ZL |
2677 | struct worker *worker; |
2678 | int id; | |
5d9c7a1e | 2679 | char id_buf[23]; |
c34056a3 | 2680 | |
7cda9aae | 2681 | /* ID is needed to determine kthread name */ |
e441b56f | 2682 | id = ida_alloc(&pool->worker_ida, GFP_KERNEL); |
3f0ea0b8 PM |
2683 | if (id < 0) { |
2684 | pr_err_once("workqueue: Failed to allocate a worker ID: %pe\n", | |
2685 | ERR_PTR(id)); | |
e441b56f | 2686 | return NULL; |
3f0ea0b8 | 2687 | } |
c34056a3 | 2688 | |
f7537df5 | 2689 | worker = alloc_worker(pool->node); |
3f0ea0b8 PM |
2690 | if (!worker) { |
2691 | pr_err_once("workqueue: Failed to allocate a worker\n"); | |
c34056a3 | 2692 | goto fail; |
3f0ea0b8 | 2693 | } |
c34056a3 | 2694 | |
c34056a3 TH |
2695 | worker->id = id; |
2696 | ||
4cb1ef64 TH |
2697 | if (!(pool->flags & POOL_BH)) { |
2698 | if (pool->cpu >= 0) | |
2699 | snprintf(id_buf, sizeof(id_buf), "%d:%d%s", pool->cpu, id, | |
2700 | pool->attrs->nice < 0 ? "H" : ""); | |
2701 | else | |
2702 | snprintf(id_buf, sizeof(id_buf), "u%d:%d", pool->id, id); | |
2703 | ||
2704 | worker->task = kthread_create_on_node(worker_thread, worker, | |
2705 | pool->node, "kworker/%s", id_buf); | |
2706 | if (IS_ERR(worker->task)) { | |
2707 | if (PTR_ERR(worker->task) == -EINTR) { | |
2708 | pr_err("workqueue: Interrupted when creating a worker thread \"kworker/%s\"\n", | |
2709 | id_buf); | |
2710 | } else { | |
2711 | pr_err_once("workqueue: Failed to create a worker thread: %pe", | |
2712 | worker->task); | |
2713 | } | |
2714 | goto fail; | |
60f54038 | 2715 | } |
c34056a3 | 2716 | |
4cb1ef64 TH |
2717 | set_user_nice(worker->task, pool->attrs->nice); |
2718 | kthread_bind_mask(worker->task, pool_allowed_cpus(pool)); | |
2719 | } | |
91151228 | 2720 | |
da028469 | 2721 | /* successful, attach the worker to the pool */ |
4736cbf7 | 2722 | worker_attach_to_pool(worker, pool); |
822d8405 | 2723 | |
051e1850 | 2724 | /* start the newly created worker */ |
a9b8a985 | 2725 | raw_spin_lock_irq(&pool->lock); |
0219a352 | 2726 | |
051e1850 LJ |
2727 | worker->pool->nr_workers++; |
2728 | worker_enter_idle(worker); | |
0219a352 TH |
2729 | |
2730 | /* | |
2731 | * @worker is waiting on a completion in kthread() and will trigger hung | |
6a229b0e TH |
2732 | * check if not woken up soon. As kick_pool() is noop if @pool is empty, |
2733 | * wake it up explicitly. | |
0219a352 | 2734 | */ |
4cb1ef64 TH |
2735 | if (worker->task) |
2736 | wake_up_process(worker->task); | |
0219a352 | 2737 | |
a9b8a985 | 2738 | raw_spin_unlock_irq(&pool->lock); |
051e1850 | 2739 | |
c34056a3 | 2740 | return worker; |
822d8405 | 2741 | |
c34056a3 | 2742 | fail: |
e441b56f | 2743 | ida_free(&pool->worker_ida, id); |
c34056a3 TH |
2744 | kfree(worker); |
2745 | return NULL; | |
2746 | } | |
2747 | ||
793777bc VS |
2748 | static void unbind_worker(struct worker *worker) |
2749 | { | |
2750 | lockdep_assert_held(&wq_pool_attach_mutex); | |
2751 | ||
2752 | kthread_set_per_cpu(worker->task, -1); | |
2753 | if (cpumask_intersects(wq_unbound_cpumask, cpu_active_mask)) | |
2754 | WARN_ON_ONCE(set_cpus_allowed_ptr(worker->task, wq_unbound_cpumask) < 0); | |
2755 | else | |
2756 | WARN_ON_ONCE(set_cpus_allowed_ptr(worker->task, cpu_possible_mask) < 0); | |
2757 | } | |
2758 | ||
e02b9312 VS |
2759 | static void wake_dying_workers(struct list_head *cull_list) |
2760 | { | |
2761 | struct worker *worker, *tmp; | |
2762 | ||
2763 | list_for_each_entry_safe(worker, tmp, cull_list, entry) { | |
2764 | list_del_init(&worker->entry); | |
2765 | unbind_worker(worker); | |
2766 | /* | |
2767 | * If the worker was somehow already running, then it had to be | |
2768 | * in pool->idle_list when set_worker_dying() happened or we | |
2769 | * wouldn't have gotten here. | |
2770 | * | |
2771 | * Thus, the worker must either have observed the WORKER_DIE | |
2772 | * flag, or have set its state to TASK_IDLE. Either way, the | |
2773 | * below will be observed by the worker and is safe to do | |
2774 | * outside of pool->lock. | |
2775 | */ | |
2776 | wake_up_process(worker->task); | |
2777 | } | |
2778 | } | |
2779 | ||
c34056a3 | 2780 | /** |
e02b9312 | 2781 | * set_worker_dying - Tag a worker for destruction |
c34056a3 | 2782 | * @worker: worker to be destroyed |
e02b9312 | 2783 | * @list: transfer worker away from its pool->idle_list and into list |
c34056a3 | 2784 | * |
e02b9312 VS |
2785 | * Tag @worker for destruction and adjust @pool stats accordingly. The worker |
2786 | * should be idle. | |
c8e55f36 TH |
2787 | * |
2788 | * CONTEXT: | |
a9b8a985 | 2789 | * raw_spin_lock_irq(pool->lock). |
c34056a3 | 2790 | */ |
e02b9312 | 2791 | static void set_worker_dying(struct worker *worker, struct list_head *list) |
c34056a3 | 2792 | { |
bd7bdd43 | 2793 | struct worker_pool *pool = worker->pool; |
c34056a3 | 2794 | |
cd549687 | 2795 | lockdep_assert_held(&pool->lock); |
e02b9312 | 2796 | lockdep_assert_held(&wq_pool_attach_mutex); |
cd549687 | 2797 | |
c34056a3 | 2798 | /* sanity check frenzy */ |
6183c009 | 2799 | if (WARN_ON(worker->current_work) || |
73eb7fe7 LJ |
2800 | WARN_ON(!list_empty(&worker->scheduled)) || |
2801 | WARN_ON(!(worker->flags & WORKER_IDLE))) | |
6183c009 | 2802 | return; |
c34056a3 | 2803 | |
73eb7fe7 LJ |
2804 | pool->nr_workers--; |
2805 | pool->nr_idle--; | |
5bdfff96 | 2806 | |
cb444766 | 2807 | worker->flags |= WORKER_DIE; |
e02b9312 VS |
2808 | |
2809 | list_move(&worker->entry, list); | |
2810 | list_move(&worker->node, &pool->dying_workers); | |
c34056a3 TH |
2811 | } |
2812 | ||
3f959aa3 VS |
2813 | /** |
2814 | * idle_worker_timeout - check if some idle workers can now be deleted. | |
2815 | * @t: The pool's idle_timer that just expired | |
2816 | * | |
2817 | * The timer is armed in worker_enter_idle(). Note that it isn't disarmed in | |
2818 | * worker_leave_idle(), as a worker flicking between idle and active while its | |
2819 | * pool is at the too_many_workers() tipping point would cause too much timer | |
2820 | * housekeeping overhead. Since IDLE_WORKER_TIMEOUT is long enough, we just let | |
2821 | * it expire and re-evaluate things from there. | |
2822 | */ | |
32a6c723 | 2823 | static void idle_worker_timeout(struct timer_list *t) |
e22bee78 | 2824 | { |
32a6c723 | 2825 | struct worker_pool *pool = from_timer(pool, t, idle_timer); |
3f959aa3 VS |
2826 | bool do_cull = false; |
2827 | ||
2828 | if (work_pending(&pool->idle_cull_work)) | |
2829 | return; | |
e22bee78 | 2830 | |
a9b8a985 | 2831 | raw_spin_lock_irq(&pool->lock); |
e22bee78 | 2832 | |
3f959aa3 | 2833 | if (too_many_workers(pool)) { |
e22bee78 TH |
2834 | struct worker *worker; |
2835 | unsigned long expires; | |
2836 | ||
2837 | /* idle_list is kept in LIFO order, check the last one */ | |
3f959aa3 VS |
2838 | worker = list_entry(pool->idle_list.prev, struct worker, entry); |
2839 | expires = worker->last_active + IDLE_WORKER_TIMEOUT; | |
2840 | do_cull = !time_before(jiffies, expires); | |
2841 | ||
2842 | if (!do_cull) | |
2843 | mod_timer(&pool->idle_timer, expires); | |
2844 | } | |
2845 | raw_spin_unlock_irq(&pool->lock); | |
2846 | ||
2847 | if (do_cull) | |
2848 | queue_work(system_unbound_wq, &pool->idle_cull_work); | |
2849 | } | |
2850 | ||
2851 | /** | |
2852 | * idle_cull_fn - cull workers that have been idle for too long. | |
2853 | * @work: the pool's work for handling these idle workers | |
2854 | * | |
2855 | * This goes through a pool's idle workers and gets rid of those that have been | |
2856 | * idle for at least IDLE_WORKER_TIMEOUT seconds. | |
e02b9312 VS |
2857 | * |
2858 | * We don't want to disturb isolated CPUs because of a pcpu kworker being | |
2859 | * culled, so this also resets worker affinity. This requires a sleepable | |
2860 | * context, hence the split between timer callback and work item. | |
3f959aa3 VS |
2861 | */ |
2862 | static void idle_cull_fn(struct work_struct *work) | |
2863 | { | |
2864 | struct worker_pool *pool = container_of(work, struct worker_pool, idle_cull_work); | |
9680540c | 2865 | LIST_HEAD(cull_list); |
3f959aa3 | 2866 | |
e02b9312 VS |
2867 | /* |
2868 | * Grabbing wq_pool_attach_mutex here ensures an already-running worker | |
2869 | * cannot proceed beyong worker_detach_from_pool() in its self-destruct | |
2870 | * path. This is required as a previously-preempted worker could run after | |
2871 | * set_worker_dying() has happened but before wake_dying_workers() did. | |
2872 | */ | |
2873 | mutex_lock(&wq_pool_attach_mutex); | |
3f959aa3 VS |
2874 | raw_spin_lock_irq(&pool->lock); |
2875 | ||
2876 | while (too_many_workers(pool)) { | |
2877 | struct worker *worker; | |
2878 | unsigned long expires; | |
2879 | ||
63d95a91 | 2880 | worker = list_entry(pool->idle_list.prev, struct worker, entry); |
e22bee78 TH |
2881 | expires = worker->last_active + IDLE_WORKER_TIMEOUT; |
2882 | ||
3347fc9f | 2883 | if (time_before(jiffies, expires)) { |
63d95a91 | 2884 | mod_timer(&pool->idle_timer, expires); |
3347fc9f | 2885 | break; |
d5abe669 | 2886 | } |
3347fc9f | 2887 | |
e02b9312 | 2888 | set_worker_dying(worker, &cull_list); |
e22bee78 TH |
2889 | } |
2890 | ||
a9b8a985 | 2891 | raw_spin_unlock_irq(&pool->lock); |
e02b9312 VS |
2892 | wake_dying_workers(&cull_list); |
2893 | mutex_unlock(&wq_pool_attach_mutex); | |
e22bee78 | 2894 | } |
d5abe669 | 2895 | |
493a1724 | 2896 | static void send_mayday(struct work_struct *work) |
e22bee78 | 2897 | { |
112202d9 TH |
2898 | struct pool_workqueue *pwq = get_work_pwq(work); |
2899 | struct workqueue_struct *wq = pwq->wq; | |
493a1724 | 2900 | |
2e109a28 | 2901 | lockdep_assert_held(&wq_mayday_lock); |
e22bee78 | 2902 | |
493008a8 | 2903 | if (!wq->rescuer) |
493a1724 | 2904 | return; |
e22bee78 TH |
2905 | |
2906 | /* mayday mayday mayday */ | |
493a1724 | 2907 | if (list_empty(&pwq->mayday_node)) { |
77668c8b LJ |
2908 | /* |
2909 | * If @pwq is for an unbound wq, its base ref may be put at | |
2910 | * any time due to an attribute change. Pin @pwq until the | |
2911 | * rescuer is done with it. | |
2912 | */ | |
2913 | get_pwq(pwq); | |
493a1724 | 2914 | list_add_tail(&pwq->mayday_node, &wq->maydays); |
e22bee78 | 2915 | wake_up_process(wq->rescuer->task); |
725e8ec5 | 2916 | pwq->stats[PWQ_STAT_MAYDAY]++; |
493a1724 | 2917 | } |
e22bee78 TH |
2918 | } |
2919 | ||
32a6c723 | 2920 | static void pool_mayday_timeout(struct timer_list *t) |
e22bee78 | 2921 | { |
32a6c723 | 2922 | struct worker_pool *pool = from_timer(pool, t, mayday_timer); |
e22bee78 TH |
2923 | struct work_struct *work; |
2924 | ||
a9b8a985 SAS |
2925 | raw_spin_lock_irq(&pool->lock); |
2926 | raw_spin_lock(&wq_mayday_lock); /* for wq->maydays */ | |
e22bee78 | 2927 | |
63d95a91 | 2928 | if (need_to_create_worker(pool)) { |
e22bee78 TH |
2929 | /* |
2930 | * We've been trying to create a new worker but | |
2931 | * haven't been successful. We might be hitting an | |
2932 | * allocation deadlock. Send distress signals to | |
2933 | * rescuers. | |
2934 | */ | |
63d95a91 | 2935 | list_for_each_entry(work, &pool->worklist, entry) |
e22bee78 | 2936 | send_mayday(work); |
1da177e4 | 2937 | } |
e22bee78 | 2938 | |
a9b8a985 SAS |
2939 | raw_spin_unlock(&wq_mayday_lock); |
2940 | raw_spin_unlock_irq(&pool->lock); | |
e22bee78 | 2941 | |
63d95a91 | 2942 | mod_timer(&pool->mayday_timer, jiffies + MAYDAY_INTERVAL); |
1da177e4 LT |
2943 | } |
2944 | ||
e22bee78 TH |
2945 | /** |
2946 | * maybe_create_worker - create a new worker if necessary | |
63d95a91 | 2947 | * @pool: pool to create a new worker for |
e22bee78 | 2948 | * |
63d95a91 | 2949 | * Create a new worker for @pool if necessary. @pool is guaranteed to |
e22bee78 TH |
2950 | * have at least one idle worker on return from this function. If |
2951 | * creating a new worker takes longer than MAYDAY_INTERVAL, mayday is | |
63d95a91 | 2952 | * sent to all rescuers with works scheduled on @pool to resolve |
e22bee78 TH |
2953 | * possible allocation deadlock. |
2954 | * | |
c5aa87bb TH |
2955 | * On return, need_to_create_worker() is guaranteed to be %false and |
2956 | * may_start_working() %true. | |
e22bee78 TH |
2957 | * |
2958 | * LOCKING: | |
a9b8a985 | 2959 | * raw_spin_lock_irq(pool->lock) which may be released and regrabbed |
e22bee78 TH |
2960 | * multiple times. Does GFP_KERNEL allocations. Called only from |
2961 | * manager. | |
e22bee78 | 2962 | */ |
29187a9e | 2963 | static void maybe_create_worker(struct worker_pool *pool) |
d565ed63 TH |
2964 | __releases(&pool->lock) |
2965 | __acquires(&pool->lock) | |
1da177e4 | 2966 | { |
e22bee78 | 2967 | restart: |
a9b8a985 | 2968 | raw_spin_unlock_irq(&pool->lock); |
9f9c2364 | 2969 | |
e22bee78 | 2970 | /* if we don't make progress in MAYDAY_INITIAL_TIMEOUT, call for help */ |
63d95a91 | 2971 | mod_timer(&pool->mayday_timer, jiffies + MAYDAY_INITIAL_TIMEOUT); |
e22bee78 TH |
2972 | |
2973 | while (true) { | |
051e1850 | 2974 | if (create_worker(pool) || !need_to_create_worker(pool)) |
e22bee78 | 2975 | break; |
1da177e4 | 2976 | |
e212f361 | 2977 | schedule_timeout_interruptible(CREATE_COOLDOWN); |
9f9c2364 | 2978 | |
63d95a91 | 2979 | if (!need_to_create_worker(pool)) |
e22bee78 TH |
2980 | break; |
2981 | } | |
2982 | ||
63d95a91 | 2983 | del_timer_sync(&pool->mayday_timer); |
a9b8a985 | 2984 | raw_spin_lock_irq(&pool->lock); |
051e1850 LJ |
2985 | /* |
2986 | * This is necessary even after a new worker was just successfully | |
2987 | * created as @pool->lock was dropped and the new worker might have | |
2988 | * already become busy. | |
2989 | */ | |
63d95a91 | 2990 | if (need_to_create_worker(pool)) |
e22bee78 | 2991 | goto restart; |
e22bee78 TH |
2992 | } |
2993 | ||
73f53c4a | 2994 | /** |
e22bee78 TH |
2995 | * manage_workers - manage worker pool |
2996 | * @worker: self | |
73f53c4a | 2997 | * |
706026c2 | 2998 | * Assume the manager role and manage the worker pool @worker belongs |
e22bee78 | 2999 | * to. At any given time, there can be only zero or one manager per |
706026c2 | 3000 | * pool. The exclusion is handled automatically by this function. |
e22bee78 TH |
3001 | * |
3002 | * The caller can safely start processing works on false return. On | |
3003 | * true return, it's guaranteed that need_to_create_worker() is false | |
3004 | * and may_start_working() is true. | |
73f53c4a TH |
3005 | * |
3006 | * CONTEXT: | |
a9b8a985 | 3007 | * raw_spin_lock_irq(pool->lock) which may be released and regrabbed |
e22bee78 TH |
3008 | * multiple times. Does GFP_KERNEL allocations. |
3009 | * | |
d185af30 | 3010 | * Return: |
29187a9e TH |
3011 | * %false if the pool doesn't need management and the caller can safely |
3012 | * start processing works, %true if management function was performed and | |
3013 | * the conditions that the caller verified before calling the function may | |
3014 | * no longer be true. | |
73f53c4a | 3015 | */ |
e22bee78 | 3016 | static bool manage_workers(struct worker *worker) |
73f53c4a | 3017 | { |
63d95a91 | 3018 | struct worker_pool *pool = worker->pool; |
73f53c4a | 3019 | |
692b4825 | 3020 | if (pool->flags & POOL_MANAGER_ACTIVE) |
29187a9e | 3021 | return false; |
692b4825 TH |
3022 | |
3023 | pool->flags |= POOL_MANAGER_ACTIVE; | |
2607d7a6 | 3024 | pool->manager = worker; |
1e19ffc6 | 3025 | |
29187a9e | 3026 | maybe_create_worker(pool); |
e22bee78 | 3027 | |
2607d7a6 | 3028 | pool->manager = NULL; |
692b4825 | 3029 | pool->flags &= ~POOL_MANAGER_ACTIVE; |
d8bb65ab | 3030 | rcuwait_wake_up(&manager_wait); |
29187a9e | 3031 | return true; |
73f53c4a TH |
3032 | } |
3033 | ||
a62428c0 TH |
3034 | /** |
3035 | * process_one_work - process single work | |
c34056a3 | 3036 | * @worker: self |
a62428c0 TH |
3037 | * @work: work to process |
3038 | * | |
3039 | * Process @work. This function contains all the logics necessary to | |
3040 | * process a single work including synchronization against and | |
3041 | * interaction with other workers on the same cpu, queueing and | |
3042 | * flushing. As long as context requirement is met, any worker can | |
3043 | * call this function to process a work. | |
3044 | * | |
3045 | * CONTEXT: | |
a9b8a985 | 3046 | * raw_spin_lock_irq(pool->lock) which is released and regrabbed. |
a62428c0 | 3047 | */ |
c34056a3 | 3048 | static void process_one_work(struct worker *worker, struct work_struct *work) |
d565ed63 TH |
3049 | __releases(&pool->lock) |
3050 | __acquires(&pool->lock) | |
a62428c0 | 3051 | { |
112202d9 | 3052 | struct pool_workqueue *pwq = get_work_pwq(work); |
bd7bdd43 | 3053 | struct worker_pool *pool = worker->pool; |
c4560c2c | 3054 | unsigned long work_data; |
c35aea39 | 3055 | int lockdep_start_depth, rcu_start_depth; |
a62428c0 TH |
3056 | #ifdef CONFIG_LOCKDEP |
3057 | /* | |
3058 | * It is permissible to free the struct work_struct from | |
3059 | * inside the function that is called from it, this we need to | |
3060 | * take into account for lockdep too. To avoid bogus "held | |
3061 | * lock freed" warnings as well as problems when looking into | |
3062 | * work->lockdep_map, make a copy and use that here. | |
3063 | */ | |
4d82a1de PZ |
3064 | struct lockdep_map lockdep_map; |
3065 | ||
3066 | lockdep_copy_map(&lockdep_map, &work->lockdep_map); | |
a62428c0 | 3067 | #endif |
807407c0 | 3068 | /* ensure we're on the correct CPU */ |
85327af6 | 3069 | WARN_ON_ONCE(!(pool->flags & POOL_DISASSOCIATED) && |
ec22ca5e | 3070 | raw_smp_processor_id() != pool->cpu); |
25511a47 | 3071 | |
8930caba | 3072 | /* claim and dequeue */ |
a62428c0 | 3073 | debug_work_deactivate(work); |
c9e7cf27 | 3074 | hash_add(pool->busy_hash, &worker->hentry, (unsigned long)work); |
c34056a3 | 3075 | worker->current_work = work; |
a2c1c57b | 3076 | worker->current_func = work->func; |
112202d9 | 3077 | worker->current_pwq = pwq; |
4cb1ef64 TH |
3078 | if (worker->task) |
3079 | worker->current_at = worker->task->se.sum_exec_runtime; | |
c4560c2c | 3080 | work_data = *work_data_bits(work); |
d812796e | 3081 | worker->current_color = get_work_color(work_data); |
7a22ad75 | 3082 | |
8bf89593 TH |
3083 | /* |
3084 | * Record wq name for cmdline and debug reporting, may get | |
3085 | * overridden through set_worker_desc(). | |
3086 | */ | |
3087 | strscpy(worker->desc, pwq->wq->name, WORKER_DESC_LEN); | |
3088 | ||
a62428c0 TH |
3089 | list_del_init(&work->entry); |
3090 | ||
fb0e7beb | 3091 | /* |
228f1d00 LJ |
3092 | * CPU intensive works don't participate in concurrency management. |
3093 | * They're the scheduler's responsibility. This takes @worker out | |
3094 | * of concurrency management and the next code block will chain | |
3095 | * execution of the pending work items. | |
fb0e7beb | 3096 | */ |
616db877 | 3097 | if (unlikely(pwq->wq->flags & WQ_CPU_INTENSIVE)) |
228f1d00 | 3098 | worker_set_flags(worker, WORKER_CPU_INTENSIVE); |
fb0e7beb | 3099 | |
974271c4 | 3100 | /* |
0219a352 TH |
3101 | * Kick @pool if necessary. It's always noop for per-cpu worker pools |
3102 | * since nr_running would always be >= 1 at this point. This is used to | |
3103 | * chain execution of the pending work items for WORKER_NOT_RUNNING | |
3104 | * workers such as the UNBOUND and CPU_INTENSIVE ones. | |
974271c4 | 3105 | */ |
0219a352 | 3106 | kick_pool(pool); |
974271c4 | 3107 | |
8930caba | 3108 | /* |
7c3eed5c | 3109 | * Record the last pool and clear PENDING which should be the last |
d565ed63 | 3110 | * update to @work. Also, do this inside @pool->lock so that |
23657bb1 TH |
3111 | * PENDING and queued state changes happen together while IRQ is |
3112 | * disabled. | |
8930caba | 3113 | */ |
7c3eed5c | 3114 | set_work_pool_and_clear_pending(work, pool->id); |
a62428c0 | 3115 | |
fe48ba7d | 3116 | pwq->stats[PWQ_STAT_STARTED]++; |
a9b8a985 | 3117 | raw_spin_unlock_irq(&pool->lock); |
a62428c0 | 3118 | |
c35aea39 TH |
3119 | rcu_start_depth = rcu_preempt_depth(); |
3120 | lockdep_start_depth = lockdep_depth(current); | |
a1d14934 | 3121 | lock_map_acquire(&pwq->wq->lockdep_map); |
a62428c0 | 3122 | lock_map_acquire(&lockdep_map); |
e6f3faa7 | 3123 | /* |
f52be570 PZ |
3124 | * Strictly speaking we should mark the invariant state without holding |
3125 | * any locks, that is, before these two lock_map_acquire()'s. | |
e6f3faa7 PZ |
3126 | * |
3127 | * However, that would result in: | |
3128 | * | |
3129 | * A(W1) | |
3130 | * WFC(C) | |
3131 | * A(W1) | |
3132 | * C(C) | |
3133 | * | |
3134 | * Which would create W1->C->W1 dependencies, even though there is no | |
3135 | * actual deadlock possible. There are two solutions, using a | |
3136 | * read-recursive acquire on the work(queue) 'locks', but this will then | |
f52be570 | 3137 | * hit the lockdep limitation on recursive locks, or simply discard |
e6f3faa7 PZ |
3138 | * these locks. |
3139 | * | |
3140 | * AFAICT there is no possible deadlock scenario between the | |
3141 | * flush_work() and complete() primitives (except for single-threaded | |
3142 | * workqueues), so hiding them isn't a problem. | |
3143 | */ | |
f52be570 | 3144 | lockdep_invariant_state(true); |
e36c886a | 3145 | trace_workqueue_execute_start(work); |
a2c1c57b | 3146 | worker->current_func(work); |
e36c886a AV |
3147 | /* |
3148 | * While we must be careful to not use "work" after this, the trace | |
3149 | * point will only record its address. | |
3150 | */ | |
1c5da0ec | 3151 | trace_workqueue_execute_end(work, worker->current_func); |
725e8ec5 | 3152 | pwq->stats[PWQ_STAT_COMPLETED]++; |
a62428c0 | 3153 | lock_map_release(&lockdep_map); |
112202d9 | 3154 | lock_map_release(&pwq->wq->lockdep_map); |
a62428c0 | 3155 | |
c35aea39 TH |
3156 | if (unlikely((worker->task && in_atomic()) || |
3157 | lockdep_depth(current) != lockdep_start_depth || | |
3158 | rcu_preempt_depth() != rcu_start_depth)) { | |
3159 | pr_err("BUG: workqueue leaked atomic, lock or RCU: %s[%d]\n" | |
3160 | " preempt=0x%08x lock=%d->%d RCU=%d->%d workfn=%ps\n", | |
3161 | current->comm, task_pid_nr(current), preempt_count(), | |
3162 | lockdep_start_depth, lockdep_depth(current), | |
3163 | rcu_start_depth, rcu_preempt_depth(), | |
3164 | worker->current_func); | |
a62428c0 TH |
3165 | debug_show_held_locks(current); |
3166 | dump_stack(); | |
3167 | } | |
3168 | ||
b22ce278 | 3169 | /* |
025f50f3 | 3170 | * The following prevents a kworker from hogging CPU on !PREEMPTION |
b22ce278 TH |
3171 | * kernels, where a requeueing work item waiting for something to |
3172 | * happen could deadlock with stop_machine as such work item could | |
3173 | * indefinitely requeue itself while all other CPUs are trapped in | |
789cbbec JL |
3174 | * stop_machine. At the same time, report a quiescent RCU state so |
3175 | * the same condition doesn't freeze RCU. | |
b22ce278 | 3176 | */ |
4cb1ef64 TH |
3177 | if (worker->task) |
3178 | cond_resched(); | |
b22ce278 | 3179 | |
a9b8a985 | 3180 | raw_spin_lock_irq(&pool->lock); |
a62428c0 | 3181 | |
616db877 TH |
3182 | /* |
3183 | * In addition to %WQ_CPU_INTENSIVE, @worker may also have been marked | |
3184 | * CPU intensive by wq_worker_tick() if @work hogged CPU longer than | |
3185 | * wq_cpu_intensive_thresh_us. Clear it. | |
3186 | */ | |
3187 | worker_clr_flags(worker, WORKER_CPU_INTENSIVE); | |
fb0e7beb | 3188 | |
1b69ac6b JW |
3189 | /* tag the worker for identification in schedule() */ |
3190 | worker->last_func = worker->current_func; | |
3191 | ||
a62428c0 | 3192 | /* we're done with it, release */ |
42f8570f | 3193 | hash_del(&worker->hentry); |
c34056a3 | 3194 | worker->current_work = NULL; |
a2c1c57b | 3195 | worker->current_func = NULL; |
112202d9 | 3196 | worker->current_pwq = NULL; |
d812796e | 3197 | worker->current_color = INT_MAX; |
dd6c3c54 TH |
3198 | |
3199 | /* must be the last step, see the function comment */ | |
c4560c2c | 3200 | pwq_dec_nr_in_flight(pwq, work_data); |
a62428c0 TH |
3201 | } |
3202 | ||
affee4b2 TH |
3203 | /** |
3204 | * process_scheduled_works - process scheduled works | |
3205 | * @worker: self | |
3206 | * | |
3207 | * Process all scheduled works. Please note that the scheduled list | |
3208 | * may change while processing a work, so this function repeatedly | |
3209 | * fetches a work from the top and executes it. | |
3210 | * | |
3211 | * CONTEXT: | |
a9b8a985 | 3212 | * raw_spin_lock_irq(pool->lock) which may be released and regrabbed |
affee4b2 TH |
3213 | * multiple times. |
3214 | */ | |
3215 | static void process_scheduled_works(struct worker *worker) | |
1da177e4 | 3216 | { |
c0ab017d TH |
3217 | struct work_struct *work; |
3218 | bool first = true; | |
3219 | ||
3220 | while ((work = list_first_entry_or_null(&worker->scheduled, | |
3221 | struct work_struct, entry))) { | |
3222 | if (first) { | |
3223 | worker->pool->watchdog_ts = jiffies; | |
3224 | first = false; | |
3225 | } | |
c34056a3 | 3226 | process_one_work(worker, work); |
1da177e4 | 3227 | } |
1da177e4 LT |
3228 | } |
3229 | ||
197f6acc TH |
3230 | static void set_pf_worker(bool val) |
3231 | { | |
3232 | mutex_lock(&wq_pool_attach_mutex); | |
3233 | if (val) | |
3234 | current->flags |= PF_WQ_WORKER; | |
3235 | else | |
3236 | current->flags &= ~PF_WQ_WORKER; | |
3237 | mutex_unlock(&wq_pool_attach_mutex); | |
3238 | } | |
3239 | ||
4690c4ab TH |
3240 | /** |
3241 | * worker_thread - the worker thread function | |
c34056a3 | 3242 | * @__worker: self |
4690c4ab | 3243 | * |
c5aa87bb TH |
3244 | * The worker thread function. All workers belong to a worker_pool - |
3245 | * either a per-cpu one or dynamic unbound one. These workers process all | |
3246 | * work items regardless of their specific target workqueue. The only | |
3247 | * exception is work items which belong to workqueues with a rescuer which | |
3248 | * will be explained in rescuer_thread(). | |
d185af30 YB |
3249 | * |
3250 | * Return: 0 | |
4690c4ab | 3251 | */ |
c34056a3 | 3252 | static int worker_thread(void *__worker) |
1da177e4 | 3253 | { |
c34056a3 | 3254 | struct worker *worker = __worker; |
bd7bdd43 | 3255 | struct worker_pool *pool = worker->pool; |
1da177e4 | 3256 | |
e22bee78 | 3257 | /* tell the scheduler that this is a workqueue worker */ |
197f6acc | 3258 | set_pf_worker(true); |
c8e55f36 | 3259 | woke_up: |
a9b8a985 | 3260 | raw_spin_lock_irq(&pool->lock); |
1da177e4 | 3261 | |
a9ab775b TH |
3262 | /* am I supposed to die? */ |
3263 | if (unlikely(worker->flags & WORKER_DIE)) { | |
a9b8a985 | 3264 | raw_spin_unlock_irq(&pool->lock); |
197f6acc | 3265 | set_pf_worker(false); |
60f5a4bc LJ |
3266 | |
3267 | set_task_comm(worker->task, "kworker/dying"); | |
e441b56f | 3268 | ida_free(&pool->worker_ida, worker->id); |
a2d812a2 | 3269 | worker_detach_from_pool(worker); |
e02b9312 | 3270 | WARN_ON_ONCE(!list_empty(&worker->entry)); |
60f5a4bc | 3271 | kfree(worker); |
a9ab775b | 3272 | return 0; |
c8e55f36 | 3273 | } |
affee4b2 | 3274 | |
c8e55f36 | 3275 | worker_leave_idle(worker); |
db7bccf4 | 3276 | recheck: |
e22bee78 | 3277 | /* no more worker necessary? */ |
63d95a91 | 3278 | if (!need_more_worker(pool)) |
e22bee78 TH |
3279 | goto sleep; |
3280 | ||
3281 | /* do we need to manage? */ | |
63d95a91 | 3282 | if (unlikely(!may_start_working(pool)) && manage_workers(worker)) |
e22bee78 TH |
3283 | goto recheck; |
3284 | ||
c8e55f36 TH |
3285 | /* |
3286 | * ->scheduled list can only be filled while a worker is | |
3287 | * preparing to process a work or actually processing it. | |
3288 | * Make sure nobody diddled with it while I was sleeping. | |
3289 | */ | |
6183c009 | 3290 | WARN_ON_ONCE(!list_empty(&worker->scheduled)); |
c8e55f36 | 3291 | |
e22bee78 | 3292 | /* |
a9ab775b TH |
3293 | * Finish PREP stage. We're guaranteed to have at least one idle |
3294 | * worker or that someone else has already assumed the manager | |
3295 | * role. This is where @worker starts participating in concurrency | |
3296 | * management if applicable and concurrency management is restored | |
3297 | * after being rebound. See rebind_workers() for details. | |
e22bee78 | 3298 | */ |
a9ab775b | 3299 | worker_clr_flags(worker, WORKER_PREP | WORKER_REBOUND); |
e22bee78 TH |
3300 | |
3301 | do { | |
c8e55f36 | 3302 | struct work_struct *work = |
bd7bdd43 | 3303 | list_first_entry(&pool->worklist, |
c8e55f36 TH |
3304 | struct work_struct, entry); |
3305 | ||
873eaca6 TH |
3306 | if (assign_work(work, worker, NULL)) |
3307 | process_scheduled_works(worker); | |
63d95a91 | 3308 | } while (keep_working(pool)); |
e22bee78 | 3309 | |
228f1d00 | 3310 | worker_set_flags(worker, WORKER_PREP); |
d313dd85 | 3311 | sleep: |
c8e55f36 | 3312 | /* |
d565ed63 TH |
3313 | * pool->lock is held and there's no work to process and no need to |
3314 | * manage, sleep. Workers are woken up only while holding | |
3315 | * pool->lock or from local cpu, so setting the current state | |
3316 | * before releasing pool->lock is enough to prevent losing any | |
3317 | * event. | |
c8e55f36 TH |
3318 | */ |
3319 | worker_enter_idle(worker); | |
c5a94a61 | 3320 | __set_current_state(TASK_IDLE); |
a9b8a985 | 3321 | raw_spin_unlock_irq(&pool->lock); |
c8e55f36 TH |
3322 | schedule(); |
3323 | goto woke_up; | |
1da177e4 LT |
3324 | } |
3325 | ||
e22bee78 TH |
3326 | /** |
3327 | * rescuer_thread - the rescuer thread function | |
111c225a | 3328 | * @__rescuer: self |
e22bee78 TH |
3329 | * |
3330 | * Workqueue rescuer thread function. There's one rescuer for each | |
493008a8 | 3331 | * workqueue which has WQ_MEM_RECLAIM set. |
e22bee78 | 3332 | * |
706026c2 | 3333 | * Regular work processing on a pool may block trying to create a new |
e22bee78 TH |
3334 | * worker which uses GFP_KERNEL allocation which has slight chance of |
3335 | * developing into deadlock if some works currently on the same queue | |
3336 | * need to be processed to satisfy the GFP_KERNEL allocation. This is | |
3337 | * the problem rescuer solves. | |
3338 | * | |
706026c2 TH |
3339 | * When such condition is possible, the pool summons rescuers of all |
3340 | * workqueues which have works queued on the pool and let them process | |
e22bee78 TH |
3341 | * those works so that forward progress can be guaranteed. |
3342 | * | |
3343 | * This should happen rarely. | |
d185af30 YB |
3344 | * |
3345 | * Return: 0 | |
e22bee78 | 3346 | */ |
111c225a | 3347 | static int rescuer_thread(void *__rescuer) |
e22bee78 | 3348 | { |
111c225a TH |
3349 | struct worker *rescuer = __rescuer; |
3350 | struct workqueue_struct *wq = rescuer->rescue_wq; | |
4d595b86 | 3351 | bool should_stop; |
e22bee78 TH |
3352 | |
3353 | set_user_nice(current, RESCUER_NICE_LEVEL); | |
111c225a TH |
3354 | |
3355 | /* | |
3356 | * Mark rescuer as worker too. As WORKER_PREP is never cleared, it | |
3357 | * doesn't participate in concurrency management. | |
3358 | */ | |
197f6acc | 3359 | set_pf_worker(true); |
e22bee78 | 3360 | repeat: |
c5a94a61 | 3361 | set_current_state(TASK_IDLE); |
e22bee78 | 3362 | |
4d595b86 LJ |
3363 | /* |
3364 | * By the time the rescuer is requested to stop, the workqueue | |
3365 | * shouldn't have any work pending, but @wq->maydays may still have | |
3366 | * pwq(s) queued. This can happen by non-rescuer workers consuming | |
3367 | * all the work items before the rescuer got to them. Go through | |
3368 | * @wq->maydays processing before acting on should_stop so that the | |
3369 | * list is always empty on exit. | |
3370 | */ | |
3371 | should_stop = kthread_should_stop(); | |
e22bee78 | 3372 | |
493a1724 | 3373 | /* see whether any pwq is asking for help */ |
a9b8a985 | 3374 | raw_spin_lock_irq(&wq_mayday_lock); |
493a1724 TH |
3375 | |
3376 | while (!list_empty(&wq->maydays)) { | |
3377 | struct pool_workqueue *pwq = list_first_entry(&wq->maydays, | |
3378 | struct pool_workqueue, mayday_node); | |
112202d9 | 3379 | struct worker_pool *pool = pwq->pool; |
e22bee78 TH |
3380 | struct work_struct *work, *n; |
3381 | ||
3382 | __set_current_state(TASK_RUNNING); | |
493a1724 TH |
3383 | list_del_init(&pwq->mayday_node); |
3384 | ||
a9b8a985 | 3385 | raw_spin_unlock_irq(&wq_mayday_lock); |
e22bee78 | 3386 | |
51697d39 LJ |
3387 | worker_attach_to_pool(rescuer, pool); |
3388 | ||
a9b8a985 | 3389 | raw_spin_lock_irq(&pool->lock); |
e22bee78 TH |
3390 | |
3391 | /* | |
3392 | * Slurp in all works issued via this workqueue and | |
3393 | * process'em. | |
3394 | */ | |
873eaca6 | 3395 | WARN_ON_ONCE(!list_empty(&rescuer->scheduled)); |
82607adc | 3396 | list_for_each_entry_safe(work, n, &pool->worklist, entry) { |
873eaca6 TH |
3397 | if (get_work_pwq(work) == pwq && |
3398 | assign_work(work, rescuer, &n)) | |
725e8ec5 | 3399 | pwq->stats[PWQ_STAT_RESCUED]++; |
82607adc | 3400 | } |
e22bee78 | 3401 | |
873eaca6 | 3402 | if (!list_empty(&rescuer->scheduled)) { |
008847f6 N |
3403 | process_scheduled_works(rescuer); |
3404 | ||
3405 | /* | |
3406 | * The above execution of rescued work items could | |
3407 | * have created more to rescue through | |
f97a4a1a | 3408 | * pwq_activate_first_inactive() or chained |
008847f6 N |
3409 | * queueing. Let's put @pwq back on mayday list so |
3410 | * that such back-to-back work items, which may be | |
3411 | * being used to relieve memory pressure, don't | |
3412 | * incur MAYDAY_INTERVAL delay inbetween. | |
3413 | */ | |
4f3f4cf3 | 3414 | if (pwq->nr_active && need_to_create_worker(pool)) { |
a9b8a985 | 3415 | raw_spin_lock(&wq_mayday_lock); |
e66b39af TH |
3416 | /* |
3417 | * Queue iff we aren't racing destruction | |
3418 | * and somebody else hasn't queued it already. | |
3419 | */ | |
3420 | if (wq->rescuer && list_empty(&pwq->mayday_node)) { | |
3421 | get_pwq(pwq); | |
3422 | list_add_tail(&pwq->mayday_node, &wq->maydays); | |
3423 | } | |
a9b8a985 | 3424 | raw_spin_unlock(&wq_mayday_lock); |
008847f6 N |
3425 | } |
3426 | } | |
7576958a | 3427 | |
77668c8b LJ |
3428 | /* |
3429 | * Put the reference grabbed by send_mayday(). @pool won't | |
13b1d625 | 3430 | * go away while we're still attached to it. |
77668c8b LJ |
3431 | */ |
3432 | put_pwq(pwq); | |
3433 | ||
7576958a | 3434 | /* |
0219a352 TH |
3435 | * Leave this pool. Notify regular workers; otherwise, we end up |
3436 | * with 0 concurrency and stalling the execution. | |
7576958a | 3437 | */ |
0219a352 | 3438 | kick_pool(pool); |
7576958a | 3439 | |
a9b8a985 | 3440 | raw_spin_unlock_irq(&pool->lock); |
13b1d625 | 3441 | |
a2d812a2 | 3442 | worker_detach_from_pool(rescuer); |
13b1d625 | 3443 | |
a9b8a985 | 3444 | raw_spin_lock_irq(&wq_mayday_lock); |
e22bee78 TH |
3445 | } |
3446 | ||
a9b8a985 | 3447 | raw_spin_unlock_irq(&wq_mayday_lock); |
493a1724 | 3448 | |
4d595b86 LJ |
3449 | if (should_stop) { |
3450 | __set_current_state(TASK_RUNNING); | |
197f6acc | 3451 | set_pf_worker(false); |
4d595b86 LJ |
3452 | return 0; |
3453 | } | |
3454 | ||
111c225a TH |
3455 | /* rescuers should never participate in concurrency management */ |
3456 | WARN_ON_ONCE(!(rescuer->flags & WORKER_NOT_RUNNING)); | |
e22bee78 TH |
3457 | schedule(); |
3458 | goto repeat; | |
1da177e4 LT |
3459 | } |
3460 | ||
4cb1ef64 TH |
3461 | static void bh_worker(struct worker *worker) |
3462 | { | |
3463 | struct worker_pool *pool = worker->pool; | |
3464 | int nr_restarts = BH_WORKER_RESTARTS; | |
3465 | unsigned long end = jiffies + BH_WORKER_JIFFIES; | |
3466 | ||
3467 | raw_spin_lock_irq(&pool->lock); | |
3468 | worker_leave_idle(worker); | |
3469 | ||
3470 | /* | |
3471 | * This function follows the structure of worker_thread(). See there for | |
3472 | * explanations on each step. | |
3473 | */ | |
3474 | if (!need_more_worker(pool)) | |
3475 | goto done; | |
3476 | ||
3477 | WARN_ON_ONCE(!list_empty(&worker->scheduled)); | |
3478 | worker_clr_flags(worker, WORKER_PREP | WORKER_REBOUND); | |
3479 | ||
3480 | do { | |
3481 | struct work_struct *work = | |
3482 | list_first_entry(&pool->worklist, | |
3483 | struct work_struct, entry); | |
3484 | ||
3485 | if (assign_work(work, worker, NULL)) | |
3486 | process_scheduled_works(worker); | |
3487 | } while (keep_working(pool) && | |
3488 | --nr_restarts && time_before(jiffies, end)); | |
3489 | ||
3490 | worker_set_flags(worker, WORKER_PREP); | |
3491 | done: | |
3492 | worker_enter_idle(worker); | |
3493 | kick_pool(pool); | |
3494 | raw_spin_unlock_irq(&pool->lock); | |
3495 | } | |
3496 | ||
3497 | /* | |
3498 | * TODO: Convert all tasklet users to workqueue and use softirq directly. | |
3499 | * | |
3500 | * This is currently called from tasklet[_hi]action() and thus is also called | |
3501 | * whenever there are tasklets to run. Let's do an early exit if there's nothing | |
3502 | * queued. Once conversion from tasklet is complete, the need_more_worker() test | |
3503 | * can be dropped. | |
3504 | * | |
3505 | * After full conversion, we'll add worker->softirq_action, directly use the | |
3506 | * softirq action and obtain the worker pointer from the softirq_action pointer. | |
3507 | */ | |
3508 | void workqueue_softirq_action(bool highpri) | |
3509 | { | |
3510 | struct worker_pool *pool = | |
3511 | &per_cpu(bh_worker_pools, smp_processor_id())[highpri]; | |
3512 | if (need_more_worker(pool)) | |
3513 | bh_worker(list_first_entry(&pool->workers, struct worker, node)); | |
3514 | } | |
3515 | ||
fca839c0 TH |
3516 | /** |
3517 | * check_flush_dependency - check for flush dependency sanity | |
3518 | * @target_wq: workqueue being flushed | |
3519 | * @target_work: work item being flushed (NULL for workqueue flushes) | |
3520 | * | |
3521 | * %current is trying to flush the whole @target_wq or @target_work on it. | |
3522 | * If @target_wq doesn't have %WQ_MEM_RECLAIM, verify that %current is not | |
3523 | * reclaiming memory or running on a workqueue which doesn't have | |
3524 | * %WQ_MEM_RECLAIM as that can break forward-progress guarantee leading to | |
3525 | * a deadlock. | |
3526 | */ | |
3527 | static void check_flush_dependency(struct workqueue_struct *target_wq, | |
3528 | struct work_struct *target_work) | |
3529 | { | |
3530 | work_func_t target_func = target_work ? target_work->func : NULL; | |
3531 | struct worker *worker; | |
3532 | ||
3533 | if (target_wq->flags & WQ_MEM_RECLAIM) | |
3534 | return; | |
3535 | ||
3536 | worker = current_wq_worker(); | |
3537 | ||
3538 | WARN_ONCE(current->flags & PF_MEMALLOC, | |
d75f773c | 3539 | "workqueue: PF_MEMALLOC task %d(%s) is flushing !WQ_MEM_RECLAIM %s:%ps", |
fca839c0 | 3540 | current->pid, current->comm, target_wq->name, target_func); |
23d11a58 TH |
3541 | WARN_ONCE(worker && ((worker->current_pwq->wq->flags & |
3542 | (WQ_MEM_RECLAIM | __WQ_LEGACY)) == WQ_MEM_RECLAIM), | |
d75f773c | 3543 | "workqueue: WQ_MEM_RECLAIM %s:%ps is flushing !WQ_MEM_RECLAIM %s:%ps", |
fca839c0 TH |
3544 | worker->current_pwq->wq->name, worker->current_func, |
3545 | target_wq->name, target_func); | |
3546 | } | |
3547 | ||
fc2e4d70 ON |
3548 | struct wq_barrier { |
3549 | struct work_struct work; | |
3550 | struct completion done; | |
2607d7a6 | 3551 | struct task_struct *task; /* purely informational */ |
fc2e4d70 ON |
3552 | }; |
3553 | ||
3554 | static void wq_barrier_func(struct work_struct *work) | |
3555 | { | |
3556 | struct wq_barrier *barr = container_of(work, struct wq_barrier, work); | |
3557 | complete(&barr->done); | |
3558 | } | |
3559 | ||
4690c4ab TH |
3560 | /** |
3561 | * insert_wq_barrier - insert a barrier work | |
112202d9 | 3562 | * @pwq: pwq to insert barrier into |
4690c4ab | 3563 | * @barr: wq_barrier to insert |
affee4b2 TH |
3564 | * @target: target work to attach @barr to |
3565 | * @worker: worker currently executing @target, NULL if @target is not executing | |
4690c4ab | 3566 | * |
affee4b2 TH |
3567 | * @barr is linked to @target such that @barr is completed only after |
3568 | * @target finishes execution. Please note that the ordering | |
3569 | * guarantee is observed only with respect to @target and on the local | |
3570 | * cpu. | |
3571 | * | |
3572 | * Currently, a queued barrier can't be canceled. This is because | |
3573 | * try_to_grab_pending() can't determine whether the work to be | |
3574 | * grabbed is at the head of the queue and thus can't clear LINKED | |
3575 | * flag of the previous work while there must be a valid next work | |
3576 | * after a work with LINKED flag set. | |
3577 | * | |
3578 | * Note that when @worker is non-NULL, @target may be modified | |
112202d9 | 3579 | * underneath us, so we can't reliably determine pwq from @target. |
4690c4ab TH |
3580 | * |
3581 | * CONTEXT: | |
a9b8a985 | 3582 | * raw_spin_lock_irq(pool->lock). |
4690c4ab | 3583 | */ |
112202d9 | 3584 | static void insert_wq_barrier(struct pool_workqueue *pwq, |
affee4b2 TH |
3585 | struct wq_barrier *barr, |
3586 | struct work_struct *target, struct worker *worker) | |
fc2e4d70 | 3587 | { |
4cb1ef64 | 3588 | static __maybe_unused struct lock_class_key bh_key, thr_key; |
d812796e LJ |
3589 | unsigned int work_flags = 0; |
3590 | unsigned int work_color; | |
affee4b2 | 3591 | struct list_head *head; |
affee4b2 | 3592 | |
dc186ad7 | 3593 | /* |
d565ed63 | 3594 | * debugobject calls are safe here even with pool->lock locked |
dc186ad7 TG |
3595 | * as we know for sure that this will not trigger any of the |
3596 | * checks and call back into the fixup functions where we | |
3597 | * might deadlock. | |
4cb1ef64 TH |
3598 | * |
3599 | * BH and threaded workqueues need separate lockdep keys to avoid | |
3600 | * spuriously triggering "inconsistent {SOFTIRQ-ON-W} -> {IN-SOFTIRQ-W} | |
3601 | * usage". | |
dc186ad7 | 3602 | */ |
4cb1ef64 TH |
3603 | INIT_WORK_ONSTACK_KEY(&barr->work, wq_barrier_func, |
3604 | (pwq->wq->flags & WQ_BH) ? &bh_key : &thr_key); | |
22df02bb | 3605 | __set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(&barr->work)); |
52fa5bc5 | 3606 | |
fd1a5b04 BP |
3607 | init_completion_map(&barr->done, &target->lockdep_map); |
3608 | ||
2607d7a6 | 3609 | barr->task = current; |
83c22520 | 3610 | |
5797b1c1 | 3611 | /* The barrier work item does not participate in nr_active. */ |
018f3a13 LJ |
3612 | work_flags |= WORK_STRUCT_INACTIVE; |
3613 | ||
affee4b2 TH |
3614 | /* |
3615 | * If @target is currently being executed, schedule the | |
3616 | * barrier to the worker; otherwise, put it after @target. | |
3617 | */ | |
d812796e | 3618 | if (worker) { |
affee4b2 | 3619 | head = worker->scheduled.next; |
d812796e LJ |
3620 | work_color = worker->current_color; |
3621 | } else { | |
affee4b2 TH |
3622 | unsigned long *bits = work_data_bits(target); |
3623 | ||
3624 | head = target->entry.next; | |
3625 | /* there can already be other linked works, inherit and set */ | |
d21cece0 | 3626 | work_flags |= *bits & WORK_STRUCT_LINKED; |
d812796e | 3627 | work_color = get_work_color(*bits); |
affee4b2 TH |
3628 | __set_bit(WORK_STRUCT_LINKED_BIT, bits); |
3629 | } | |
3630 | ||
d812796e LJ |
3631 | pwq->nr_in_flight[work_color]++; |
3632 | work_flags |= work_color_to_flags(work_color); | |
3633 | ||
d21cece0 | 3634 | insert_work(pwq, &barr->work, head, work_flags); |
fc2e4d70 ON |
3635 | } |
3636 | ||
73f53c4a | 3637 | /** |
112202d9 | 3638 | * flush_workqueue_prep_pwqs - prepare pwqs for workqueue flushing |
73f53c4a TH |
3639 | * @wq: workqueue being flushed |
3640 | * @flush_color: new flush color, < 0 for no-op | |
3641 | * @work_color: new work color, < 0 for no-op | |
3642 | * | |
112202d9 | 3643 | * Prepare pwqs for workqueue flushing. |
73f53c4a | 3644 | * |
112202d9 TH |
3645 | * If @flush_color is non-negative, flush_color on all pwqs should be |
3646 | * -1. If no pwq has in-flight commands at the specified color, all | |
3647 | * pwq->flush_color's stay at -1 and %false is returned. If any pwq | |
3648 | * has in flight commands, its pwq->flush_color is set to | |
3649 | * @flush_color, @wq->nr_pwqs_to_flush is updated accordingly, pwq | |
73f53c4a TH |
3650 | * wakeup logic is armed and %true is returned. |
3651 | * | |
3652 | * The caller should have initialized @wq->first_flusher prior to | |
3653 | * calling this function with non-negative @flush_color. If | |
3654 | * @flush_color is negative, no flush color update is done and %false | |
3655 | * is returned. | |
3656 | * | |
112202d9 | 3657 | * If @work_color is non-negative, all pwqs should have the same |
73f53c4a TH |
3658 | * work_color which is previous to @work_color and all will be |
3659 | * advanced to @work_color. | |
3660 | * | |
3661 | * CONTEXT: | |
3c25a55d | 3662 | * mutex_lock(wq->mutex). |
73f53c4a | 3663 | * |
d185af30 | 3664 | * Return: |
73f53c4a TH |
3665 | * %true if @flush_color >= 0 and there's something to flush. %false |
3666 | * otherwise. | |
3667 | */ | |
112202d9 | 3668 | static bool flush_workqueue_prep_pwqs(struct workqueue_struct *wq, |
73f53c4a | 3669 | int flush_color, int work_color) |
1da177e4 | 3670 | { |
73f53c4a | 3671 | bool wait = false; |
49e3cf44 | 3672 | struct pool_workqueue *pwq; |
1da177e4 | 3673 | |
73f53c4a | 3674 | if (flush_color >= 0) { |
6183c009 | 3675 | WARN_ON_ONCE(atomic_read(&wq->nr_pwqs_to_flush)); |
112202d9 | 3676 | atomic_set(&wq->nr_pwqs_to_flush, 1); |
1da177e4 | 3677 | } |
2355b70f | 3678 | |
49e3cf44 | 3679 | for_each_pwq(pwq, wq) { |
112202d9 | 3680 | struct worker_pool *pool = pwq->pool; |
fc2e4d70 | 3681 | |
a9b8a985 | 3682 | raw_spin_lock_irq(&pool->lock); |
83c22520 | 3683 | |
73f53c4a | 3684 | if (flush_color >= 0) { |
6183c009 | 3685 | WARN_ON_ONCE(pwq->flush_color != -1); |
fc2e4d70 | 3686 | |
112202d9 TH |
3687 | if (pwq->nr_in_flight[flush_color]) { |
3688 | pwq->flush_color = flush_color; | |
3689 | atomic_inc(&wq->nr_pwqs_to_flush); | |
73f53c4a TH |
3690 | wait = true; |
3691 | } | |
3692 | } | |
1da177e4 | 3693 | |
73f53c4a | 3694 | if (work_color >= 0) { |
6183c009 | 3695 | WARN_ON_ONCE(work_color != work_next_color(pwq->work_color)); |
112202d9 | 3696 | pwq->work_color = work_color; |
73f53c4a | 3697 | } |
1da177e4 | 3698 | |
a9b8a985 | 3699 | raw_spin_unlock_irq(&pool->lock); |
1da177e4 | 3700 | } |
2355b70f | 3701 | |
112202d9 | 3702 | if (flush_color >= 0 && atomic_dec_and_test(&wq->nr_pwqs_to_flush)) |
73f53c4a | 3703 | complete(&wq->first_flusher->done); |
14441960 | 3704 | |
73f53c4a | 3705 | return wait; |
1da177e4 LT |
3706 | } |
3707 | ||
c35aea39 TH |
3708 | static void touch_wq_lockdep_map(struct workqueue_struct *wq) |
3709 | { | |
4cb1ef64 TH |
3710 | #ifdef CONFIG_LOCKDEP |
3711 | if (wq->flags & WQ_BH) | |
3712 | local_bh_disable(); | |
3713 | ||
c35aea39 TH |
3714 | lock_map_acquire(&wq->lockdep_map); |
3715 | lock_map_release(&wq->lockdep_map); | |
4cb1ef64 TH |
3716 | |
3717 | if (wq->flags & WQ_BH) | |
3718 | local_bh_enable(); | |
3719 | #endif | |
c35aea39 TH |
3720 | } |
3721 | ||
3722 | static void touch_work_lockdep_map(struct work_struct *work, | |
3723 | struct workqueue_struct *wq) | |
3724 | { | |
4cb1ef64 TH |
3725 | #ifdef CONFIG_LOCKDEP |
3726 | if (wq->flags & WQ_BH) | |
3727 | local_bh_disable(); | |
3728 | ||
c35aea39 TH |
3729 | lock_map_acquire(&work->lockdep_map); |
3730 | lock_map_release(&work->lockdep_map); | |
4cb1ef64 TH |
3731 | |
3732 | if (wq->flags & WQ_BH) | |
3733 | local_bh_enable(); | |
3734 | #endif | |
c35aea39 TH |
3735 | } |
3736 | ||
0fcb78c2 | 3737 | /** |
c4f135d6 | 3738 | * __flush_workqueue - ensure that any scheduled work has run to completion. |
0fcb78c2 | 3739 | * @wq: workqueue to flush |
1da177e4 | 3740 | * |
c5aa87bb TH |
3741 | * This function sleeps until all work items which were queued on entry |
3742 | * have finished execution, but it is not livelocked by new incoming ones. | |
1da177e4 | 3743 | */ |
c4f135d6 | 3744 | void __flush_workqueue(struct workqueue_struct *wq) |
1da177e4 | 3745 | { |
73f53c4a TH |
3746 | struct wq_flusher this_flusher = { |
3747 | .list = LIST_HEAD_INIT(this_flusher.list), | |
3748 | .flush_color = -1, | |
fd1a5b04 | 3749 | .done = COMPLETION_INITIALIZER_ONSTACK_MAP(this_flusher.done, wq->lockdep_map), |
73f53c4a TH |
3750 | }; |
3751 | int next_color; | |
1da177e4 | 3752 | |
3347fa09 TH |
3753 | if (WARN_ON(!wq_online)) |
3754 | return; | |
3755 | ||
c35aea39 | 3756 | touch_wq_lockdep_map(wq); |
87915adc | 3757 | |
3c25a55d | 3758 | mutex_lock(&wq->mutex); |
73f53c4a TH |
3759 | |
3760 | /* | |
3761 | * Start-to-wait phase | |
3762 | */ | |
3763 | next_color = work_next_color(wq->work_color); | |
3764 | ||
3765 | if (next_color != wq->flush_color) { | |
3766 | /* | |
3767 | * Color space is not full. The current work_color | |
3768 | * becomes our flush_color and work_color is advanced | |
3769 | * by one. | |
3770 | */ | |
6183c009 | 3771 | WARN_ON_ONCE(!list_empty(&wq->flusher_overflow)); |
73f53c4a TH |
3772 | this_flusher.flush_color = wq->work_color; |
3773 | wq->work_color = next_color; | |
3774 | ||
3775 | if (!wq->first_flusher) { | |
3776 | /* no flush in progress, become the first flusher */ | |
6183c009 | 3777 | WARN_ON_ONCE(wq->flush_color != this_flusher.flush_color); |
73f53c4a TH |
3778 | |
3779 | wq->first_flusher = &this_flusher; | |
3780 | ||
112202d9 | 3781 | if (!flush_workqueue_prep_pwqs(wq, wq->flush_color, |
73f53c4a TH |
3782 | wq->work_color)) { |
3783 | /* nothing to flush, done */ | |
3784 | wq->flush_color = next_color; | |
3785 | wq->first_flusher = NULL; | |
3786 | goto out_unlock; | |
3787 | } | |
3788 | } else { | |
3789 | /* wait in queue */ | |
6183c009 | 3790 | WARN_ON_ONCE(wq->flush_color == this_flusher.flush_color); |
73f53c4a | 3791 | list_add_tail(&this_flusher.list, &wq->flusher_queue); |
112202d9 | 3792 | flush_workqueue_prep_pwqs(wq, -1, wq->work_color); |
73f53c4a TH |
3793 | } |
3794 | } else { | |
3795 | /* | |
3796 | * Oops, color space is full, wait on overflow queue. | |
3797 | * The next flush completion will assign us | |
3798 | * flush_color and transfer to flusher_queue. | |
3799 | */ | |
3800 | list_add_tail(&this_flusher.list, &wq->flusher_overflow); | |
3801 | } | |
3802 | ||
fca839c0 TH |
3803 | check_flush_dependency(wq, NULL); |
3804 | ||
3c25a55d | 3805 | mutex_unlock(&wq->mutex); |
73f53c4a TH |
3806 | |
3807 | wait_for_completion(&this_flusher.done); | |
3808 | ||
3809 | /* | |
3810 | * Wake-up-and-cascade phase | |
3811 | * | |
3812 | * First flushers are responsible for cascading flushes and | |
3813 | * handling overflow. Non-first flushers can simply return. | |
3814 | */ | |
00d5d15b | 3815 | if (READ_ONCE(wq->first_flusher) != &this_flusher) |
73f53c4a TH |
3816 | return; |
3817 | ||
3c25a55d | 3818 | mutex_lock(&wq->mutex); |
73f53c4a | 3819 | |
4ce48b37 TH |
3820 | /* we might have raced, check again with mutex held */ |
3821 | if (wq->first_flusher != &this_flusher) | |
3822 | goto out_unlock; | |
3823 | ||
00d5d15b | 3824 | WRITE_ONCE(wq->first_flusher, NULL); |
73f53c4a | 3825 | |
6183c009 TH |
3826 | WARN_ON_ONCE(!list_empty(&this_flusher.list)); |
3827 | WARN_ON_ONCE(wq->flush_color != this_flusher.flush_color); | |
73f53c4a TH |
3828 | |
3829 | while (true) { | |
3830 | struct wq_flusher *next, *tmp; | |
3831 | ||
3832 | /* complete all the flushers sharing the current flush color */ | |
3833 | list_for_each_entry_safe(next, tmp, &wq->flusher_queue, list) { | |
3834 | if (next->flush_color != wq->flush_color) | |
3835 | break; | |
3836 | list_del_init(&next->list); | |
3837 | complete(&next->done); | |
3838 | } | |
3839 | ||
6183c009 TH |
3840 | WARN_ON_ONCE(!list_empty(&wq->flusher_overflow) && |
3841 | wq->flush_color != work_next_color(wq->work_color)); | |
73f53c4a TH |
3842 | |
3843 | /* this flush_color is finished, advance by one */ | |
3844 | wq->flush_color = work_next_color(wq->flush_color); | |
3845 | ||
3846 | /* one color has been freed, handle overflow queue */ | |
3847 | if (!list_empty(&wq->flusher_overflow)) { | |
3848 | /* | |
3849 | * Assign the same color to all overflowed | |
3850 | * flushers, advance work_color and append to | |
3851 | * flusher_queue. This is the start-to-wait | |
3852 | * phase for these overflowed flushers. | |
3853 | */ | |
3854 | list_for_each_entry(tmp, &wq->flusher_overflow, list) | |
3855 | tmp->flush_color = wq->work_color; | |
3856 | ||
3857 | wq->work_color = work_next_color(wq->work_color); | |
3858 | ||
3859 | list_splice_tail_init(&wq->flusher_overflow, | |
3860 | &wq->flusher_queue); | |
112202d9 | 3861 | flush_workqueue_prep_pwqs(wq, -1, wq->work_color); |
73f53c4a TH |
3862 | } |
3863 | ||
3864 | if (list_empty(&wq->flusher_queue)) { | |
6183c009 | 3865 | WARN_ON_ONCE(wq->flush_color != wq->work_color); |
73f53c4a TH |
3866 | break; |
3867 | } | |
3868 | ||
3869 | /* | |
3870 | * Need to flush more colors. Make the next flusher | |
112202d9 | 3871 | * the new first flusher and arm pwqs. |
73f53c4a | 3872 | */ |
6183c009 TH |
3873 | WARN_ON_ONCE(wq->flush_color == wq->work_color); |
3874 | WARN_ON_ONCE(wq->flush_color != next->flush_color); | |
73f53c4a TH |
3875 | |
3876 | list_del_init(&next->list); | |
3877 | wq->first_flusher = next; | |
3878 | ||
112202d9 | 3879 | if (flush_workqueue_prep_pwqs(wq, wq->flush_color, -1)) |
73f53c4a TH |
3880 | break; |
3881 | ||
3882 | /* | |
3883 | * Meh... this color is already done, clear first | |
3884 | * flusher and repeat cascading. | |
3885 | */ | |
3886 | wq->first_flusher = NULL; | |
3887 | } | |
3888 | ||
3889 | out_unlock: | |
3c25a55d | 3890 | mutex_unlock(&wq->mutex); |
1da177e4 | 3891 | } |
c4f135d6 | 3892 | EXPORT_SYMBOL(__flush_workqueue); |
1da177e4 | 3893 | |
9c5a2ba7 TH |
3894 | /** |
3895 | * drain_workqueue - drain a workqueue | |
3896 | * @wq: workqueue to drain | |
3897 | * | |
3898 | * Wait until the workqueue becomes empty. While draining is in progress, | |
3899 | * only chain queueing is allowed. IOW, only currently pending or running | |
3900 | * work items on @wq can queue further work items on it. @wq is flushed | |
b749b1b6 | 3901 | * repeatedly until it becomes empty. The number of flushing is determined |
9c5a2ba7 TH |
3902 | * by the depth of chaining and should be relatively short. Whine if it |
3903 | * takes too long. | |
3904 | */ | |
3905 | void drain_workqueue(struct workqueue_struct *wq) | |
3906 | { | |
3907 | unsigned int flush_cnt = 0; | |
49e3cf44 | 3908 | struct pool_workqueue *pwq; |
9c5a2ba7 TH |
3909 | |
3910 | /* | |
3911 | * __queue_work() needs to test whether there are drainers, is much | |
3912 | * hotter than drain_workqueue() and already looks at @wq->flags. | |
618b01eb | 3913 | * Use __WQ_DRAINING so that queue doesn't have to check nr_drainers. |
9c5a2ba7 | 3914 | */ |
87fc741e | 3915 | mutex_lock(&wq->mutex); |
9c5a2ba7 | 3916 | if (!wq->nr_drainers++) |
618b01eb | 3917 | wq->flags |= __WQ_DRAINING; |
87fc741e | 3918 | mutex_unlock(&wq->mutex); |
9c5a2ba7 | 3919 | reflush: |
c4f135d6 | 3920 | __flush_workqueue(wq); |
9c5a2ba7 | 3921 | |
b09f4fd3 | 3922 | mutex_lock(&wq->mutex); |
76af4d93 | 3923 | |
49e3cf44 | 3924 | for_each_pwq(pwq, wq) { |
fa2563e4 | 3925 | bool drained; |
9c5a2ba7 | 3926 | |
a9b8a985 | 3927 | raw_spin_lock_irq(&pwq->pool->lock); |
afa87ce8 | 3928 | drained = pwq_is_empty(pwq); |
a9b8a985 | 3929 | raw_spin_unlock_irq(&pwq->pool->lock); |
fa2563e4 TT |
3930 | |
3931 | if (drained) | |
9c5a2ba7 TH |
3932 | continue; |
3933 | ||
3934 | if (++flush_cnt == 10 || | |
3935 | (flush_cnt % 100 == 0 && flush_cnt <= 1000)) | |
e9ad2eb3 SZ |
3936 | pr_warn("workqueue %s: %s() isn't complete after %u tries\n", |
3937 | wq->name, __func__, flush_cnt); | |
76af4d93 | 3938 | |
b09f4fd3 | 3939 | mutex_unlock(&wq->mutex); |
9c5a2ba7 TH |
3940 | goto reflush; |
3941 | } | |
3942 | ||
9c5a2ba7 | 3943 | if (!--wq->nr_drainers) |
618b01eb | 3944 | wq->flags &= ~__WQ_DRAINING; |
87fc741e | 3945 | mutex_unlock(&wq->mutex); |
9c5a2ba7 TH |
3946 | } |
3947 | EXPORT_SYMBOL_GPL(drain_workqueue); | |
3948 | ||
d6e89786 JB |
3949 | static bool start_flush_work(struct work_struct *work, struct wq_barrier *barr, |
3950 | bool from_cancel) | |
db700897 | 3951 | { |
affee4b2 | 3952 | struct worker *worker = NULL; |
c9e7cf27 | 3953 | struct worker_pool *pool; |
112202d9 | 3954 | struct pool_workqueue *pwq; |
c35aea39 | 3955 | struct workqueue_struct *wq; |
db700897 ON |
3956 | |
3957 | might_sleep(); | |
fa1b54e6 | 3958 | |
24acfb71 | 3959 | rcu_read_lock(); |
c9e7cf27 | 3960 | pool = get_work_pool(work); |
fa1b54e6 | 3961 | if (!pool) { |
24acfb71 | 3962 | rcu_read_unlock(); |
baf59022 | 3963 | return false; |
fa1b54e6 | 3964 | } |
db700897 | 3965 | |
a9b8a985 | 3966 | raw_spin_lock_irq(&pool->lock); |
0b3dae68 | 3967 | /* see the comment in try_to_grab_pending() with the same code */ |
112202d9 TH |
3968 | pwq = get_work_pwq(work); |
3969 | if (pwq) { | |
3970 | if (unlikely(pwq->pool != pool)) | |
4690c4ab | 3971 | goto already_gone; |
606a5020 | 3972 | } else { |
c9e7cf27 | 3973 | worker = find_worker_executing_work(pool, work); |
affee4b2 | 3974 | if (!worker) |
4690c4ab | 3975 | goto already_gone; |
112202d9 | 3976 | pwq = worker->current_pwq; |
606a5020 | 3977 | } |
db700897 | 3978 | |
c35aea39 TH |
3979 | wq = pwq->wq; |
3980 | check_flush_dependency(wq, work); | |
fca839c0 | 3981 | |
112202d9 | 3982 | insert_wq_barrier(pwq, barr, work, worker); |
a9b8a985 | 3983 | raw_spin_unlock_irq(&pool->lock); |
7a22ad75 | 3984 | |
c35aea39 TH |
3985 | touch_work_lockdep_map(work, wq); |
3986 | ||
e159489b | 3987 | /* |
a1d14934 PZ |
3988 | * Force a lock recursion deadlock when using flush_work() inside a |
3989 | * single-threaded or rescuer equipped workqueue. | |
3990 | * | |
3991 | * For single threaded workqueues the deadlock happens when the work | |
3992 | * is after the work issuing the flush_work(). For rescuer equipped | |
3993 | * workqueues the deadlock happens when the rescuer stalls, blocking | |
3994 | * forward progress. | |
e159489b | 3995 | */ |
c35aea39 TH |
3996 | if (!from_cancel && (wq->saved_max_active == 1 || wq->rescuer)) |
3997 | touch_wq_lockdep_map(wq); | |
3998 | ||
24acfb71 | 3999 | rcu_read_unlock(); |
401a8d04 | 4000 | return true; |
4690c4ab | 4001 | already_gone: |
a9b8a985 | 4002 | raw_spin_unlock_irq(&pool->lock); |
24acfb71 | 4003 | rcu_read_unlock(); |
401a8d04 | 4004 | return false; |
db700897 | 4005 | } |
baf59022 | 4006 | |
d6e89786 JB |
4007 | static bool __flush_work(struct work_struct *work, bool from_cancel) |
4008 | { | |
4009 | struct wq_barrier barr; | |
4010 | ||
4011 | if (WARN_ON(!wq_online)) | |
4012 | return false; | |
4013 | ||
4d43d395 TH |
4014 | if (WARN_ON(!work->func)) |
4015 | return false; | |
4016 | ||
d6e89786 JB |
4017 | if (start_flush_work(work, &barr, from_cancel)) { |
4018 | wait_for_completion(&barr.done); | |
4019 | destroy_work_on_stack(&barr.work); | |
4020 | return true; | |
4021 | } else { | |
4022 | return false; | |
4023 | } | |
4024 | } | |
4025 | ||
baf59022 TH |
4026 | /** |
4027 | * flush_work - wait for a work to finish executing the last queueing instance | |
4028 | * @work: the work to flush | |
4029 | * | |
606a5020 TH |
4030 | * Wait until @work has finished execution. @work is guaranteed to be idle |
4031 | * on return if it hasn't been requeued since flush started. | |
baf59022 | 4032 | * |
d185af30 | 4033 | * Return: |
baf59022 TH |
4034 | * %true if flush_work() waited for the work to finish execution, |
4035 | * %false if it was already idle. | |
4036 | */ | |
4037 | bool flush_work(struct work_struct *work) | |
4038 | { | |
d6e89786 | 4039 | return __flush_work(work, false); |
6e84d644 | 4040 | } |
606a5020 | 4041 | EXPORT_SYMBOL_GPL(flush_work); |
6e84d644 | 4042 | |
8603e1b3 | 4043 | struct cwt_wait { |
ac6424b9 | 4044 | wait_queue_entry_t wait; |
8603e1b3 TH |
4045 | struct work_struct *work; |
4046 | }; | |
4047 | ||
ac6424b9 | 4048 | static int cwt_wakefn(wait_queue_entry_t *wait, unsigned mode, int sync, void *key) |
8603e1b3 TH |
4049 | { |
4050 | struct cwt_wait *cwait = container_of(wait, struct cwt_wait, wait); | |
4051 | ||
4052 | if (cwait->work != key) | |
4053 | return 0; | |
4054 | return autoremove_wake_function(wait, mode, sync, key); | |
4055 | } | |
4056 | ||
36e227d2 | 4057 | static bool __cancel_work_timer(struct work_struct *work, bool is_dwork) |
1f1f642e | 4058 | { |
8603e1b3 | 4059 | static DECLARE_WAIT_QUEUE_HEAD(cancel_waitq); |
bbb68dfa | 4060 | unsigned long flags; |
1f1f642e ON |
4061 | int ret; |
4062 | ||
4063 | do { | |
bbb68dfa TH |
4064 | ret = try_to_grab_pending(work, is_dwork, &flags); |
4065 | /* | |
8603e1b3 TH |
4066 | * If someone else is already canceling, wait for it to |
4067 | * finish. flush_work() doesn't work for PREEMPT_NONE | |
4068 | * because we may get scheduled between @work's completion | |
4069 | * and the other canceling task resuming and clearing | |
4070 | * CANCELING - flush_work() will return false immediately | |
4071 | * as @work is no longer busy, try_to_grab_pending() will | |
4072 | * return -ENOENT as @work is still being canceled and the | |
4073 | * other canceling task won't be able to clear CANCELING as | |
4074 | * we're hogging the CPU. | |
4075 | * | |
4076 | * Let's wait for completion using a waitqueue. As this | |
4077 | * may lead to the thundering herd problem, use a custom | |
4078 | * wake function which matches @work along with exclusive | |
4079 | * wait and wakeup. | |
bbb68dfa | 4080 | */ |
8603e1b3 TH |
4081 | if (unlikely(ret == -ENOENT)) { |
4082 | struct cwt_wait cwait; | |
4083 | ||
4084 | init_wait(&cwait.wait); | |
4085 | cwait.wait.func = cwt_wakefn; | |
4086 | cwait.work = work; | |
4087 | ||
4088 | prepare_to_wait_exclusive(&cancel_waitq, &cwait.wait, | |
4089 | TASK_UNINTERRUPTIBLE); | |
4090 | if (work_is_canceling(work)) | |
4091 | schedule(); | |
4092 | finish_wait(&cancel_waitq, &cwait.wait); | |
4093 | } | |
1f1f642e ON |
4094 | } while (unlikely(ret < 0)); |
4095 | ||
bbb68dfa TH |
4096 | /* tell other tasks trying to grab @work to back off */ |
4097 | mark_work_canceling(work); | |
4098 | local_irq_restore(flags); | |
4099 | ||
3347fa09 TH |
4100 | /* |
4101 | * This allows canceling during early boot. We know that @work | |
4102 | * isn't executing. | |
4103 | */ | |
4104 | if (wq_online) | |
d6e89786 | 4105 | __flush_work(work, true); |
3347fa09 | 4106 | |
7a22ad75 | 4107 | clear_work_data(work); |
8603e1b3 TH |
4108 | |
4109 | /* | |
4110 | * Paired with prepare_to_wait() above so that either | |
4111 | * waitqueue_active() is visible here or !work_is_canceling() is | |
4112 | * visible there. | |
4113 | */ | |
4114 | smp_mb(); | |
4115 | if (waitqueue_active(&cancel_waitq)) | |
4116 | __wake_up(&cancel_waitq, TASK_NORMAL, 1, work); | |
4117 | ||
1f1f642e ON |
4118 | return ret; |
4119 | } | |
4120 | ||
6e84d644 | 4121 | /** |
401a8d04 TH |
4122 | * cancel_work_sync - cancel a work and wait for it to finish |
4123 | * @work: the work to cancel | |
6e84d644 | 4124 | * |
401a8d04 TH |
4125 | * Cancel @work and wait for its execution to finish. This function |
4126 | * can be used even if the work re-queues itself or migrates to | |
4127 | * another workqueue. On return from this function, @work is | |
4128 | * guaranteed to be not pending or executing on any CPU. | |
1f1f642e | 4129 | * |
401a8d04 TH |
4130 | * cancel_work_sync(&delayed_work->work) must not be used for |
4131 | * delayed_work's. Use cancel_delayed_work_sync() instead. | |
6e84d644 | 4132 | * |
401a8d04 | 4133 | * The caller must ensure that the workqueue on which @work was last |
6e84d644 | 4134 | * queued can't be destroyed before this function returns. |
401a8d04 | 4135 | * |
d185af30 | 4136 | * Return: |
401a8d04 | 4137 | * %true if @work was pending, %false otherwise. |
6e84d644 | 4138 | */ |
401a8d04 | 4139 | bool cancel_work_sync(struct work_struct *work) |
6e84d644 | 4140 | { |
36e227d2 | 4141 | return __cancel_work_timer(work, false); |
b89deed3 | 4142 | } |
28e53bdd | 4143 | EXPORT_SYMBOL_GPL(cancel_work_sync); |
b89deed3 | 4144 | |
6e84d644 | 4145 | /** |
401a8d04 TH |
4146 | * flush_delayed_work - wait for a dwork to finish executing the last queueing |
4147 | * @dwork: the delayed work to flush | |
6e84d644 | 4148 | * |
401a8d04 TH |
4149 | * Delayed timer is cancelled and the pending work is queued for |
4150 | * immediate execution. Like flush_work(), this function only | |
4151 | * considers the last queueing instance of @dwork. | |
1f1f642e | 4152 | * |
d185af30 | 4153 | * Return: |
401a8d04 TH |
4154 | * %true if flush_work() waited for the work to finish execution, |
4155 | * %false if it was already idle. | |
6e84d644 | 4156 | */ |
401a8d04 TH |
4157 | bool flush_delayed_work(struct delayed_work *dwork) |
4158 | { | |
8930caba | 4159 | local_irq_disable(); |
401a8d04 | 4160 | if (del_timer_sync(&dwork->timer)) |
60c057bc | 4161 | __queue_work(dwork->cpu, dwork->wq, &dwork->work); |
8930caba | 4162 | local_irq_enable(); |
401a8d04 TH |
4163 | return flush_work(&dwork->work); |
4164 | } | |
4165 | EXPORT_SYMBOL(flush_delayed_work); | |
4166 | ||
05f0fe6b TH |
4167 | /** |
4168 | * flush_rcu_work - wait for a rwork to finish executing the last queueing | |
4169 | * @rwork: the rcu work to flush | |
4170 | * | |
4171 | * Return: | |
4172 | * %true if flush_rcu_work() waited for the work to finish execution, | |
4173 | * %false if it was already idle. | |
4174 | */ | |
4175 | bool flush_rcu_work(struct rcu_work *rwork) | |
4176 | { | |
4177 | if (test_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(&rwork->work))) { | |
4178 | rcu_barrier(); | |
4179 | flush_work(&rwork->work); | |
4180 | return true; | |
4181 | } else { | |
4182 | return flush_work(&rwork->work); | |
4183 | } | |
4184 | } | |
4185 | EXPORT_SYMBOL(flush_rcu_work); | |
4186 | ||
f72b8792 JA |
4187 | static bool __cancel_work(struct work_struct *work, bool is_dwork) |
4188 | { | |
4189 | unsigned long flags; | |
4190 | int ret; | |
4191 | ||
4192 | do { | |
4193 | ret = try_to_grab_pending(work, is_dwork, &flags); | |
4194 | } while (unlikely(ret == -EAGAIN)); | |
4195 | ||
4196 | if (unlikely(ret < 0)) | |
4197 | return false; | |
4198 | ||
4199 | set_work_pool_and_clear_pending(work, get_work_pool_id(work)); | |
4200 | local_irq_restore(flags); | |
4201 | return ret; | |
4202 | } | |
4203 | ||
73b4b532 AG |
4204 | /* |
4205 | * See cancel_delayed_work() | |
4206 | */ | |
4207 | bool cancel_work(struct work_struct *work) | |
4208 | { | |
4209 | return __cancel_work(work, false); | |
4210 | } | |
4211 | EXPORT_SYMBOL(cancel_work); | |
4212 | ||
09383498 | 4213 | /** |
57b30ae7 TH |
4214 | * cancel_delayed_work - cancel a delayed work |
4215 | * @dwork: delayed_work to cancel | |
09383498 | 4216 | * |
d185af30 YB |
4217 | * Kill off a pending delayed_work. |
4218 | * | |
4219 | * Return: %true if @dwork was pending and canceled; %false if it wasn't | |
4220 | * pending. | |
4221 | * | |
4222 | * Note: | |
4223 | * The work callback function may still be running on return, unless | |
4224 | * it returns %true and the work doesn't re-arm itself. Explicitly flush or | |
4225 | * use cancel_delayed_work_sync() to wait on it. | |
09383498 | 4226 | * |
57b30ae7 | 4227 | * This function is safe to call from any context including IRQ handler. |
09383498 | 4228 | */ |
57b30ae7 | 4229 | bool cancel_delayed_work(struct delayed_work *dwork) |
09383498 | 4230 | { |
f72b8792 | 4231 | return __cancel_work(&dwork->work, true); |
09383498 | 4232 | } |
57b30ae7 | 4233 | EXPORT_SYMBOL(cancel_delayed_work); |
09383498 | 4234 | |
401a8d04 TH |
4235 | /** |
4236 | * cancel_delayed_work_sync - cancel a delayed work and wait for it to finish | |
4237 | * @dwork: the delayed work cancel | |
4238 | * | |
4239 | * This is cancel_work_sync() for delayed works. | |
4240 | * | |
d185af30 | 4241 | * Return: |
401a8d04 TH |
4242 | * %true if @dwork was pending, %false otherwise. |
4243 | */ | |
4244 | bool cancel_delayed_work_sync(struct delayed_work *dwork) | |
6e84d644 | 4245 | { |
36e227d2 | 4246 | return __cancel_work_timer(&dwork->work, true); |
6e84d644 | 4247 | } |
f5a421a4 | 4248 | EXPORT_SYMBOL(cancel_delayed_work_sync); |
1da177e4 | 4249 | |
b6136773 | 4250 | /** |
31ddd871 | 4251 | * schedule_on_each_cpu - execute a function synchronously on each online CPU |
b6136773 | 4252 | * @func: the function to call |
b6136773 | 4253 | * |
31ddd871 TH |
4254 | * schedule_on_each_cpu() executes @func on each online CPU using the |
4255 | * system workqueue and blocks until all CPUs have completed. | |
b6136773 | 4256 | * schedule_on_each_cpu() is very slow. |
31ddd871 | 4257 | * |
d185af30 | 4258 | * Return: |
31ddd871 | 4259 | * 0 on success, -errno on failure. |
b6136773 | 4260 | */ |
65f27f38 | 4261 | int schedule_on_each_cpu(work_func_t func) |
15316ba8 CL |
4262 | { |
4263 | int cpu; | |
38f51568 | 4264 | struct work_struct __percpu *works; |
15316ba8 | 4265 | |
b6136773 AM |
4266 | works = alloc_percpu(struct work_struct); |
4267 | if (!works) | |
15316ba8 | 4268 | return -ENOMEM; |
b6136773 | 4269 | |
ffd8bea8 | 4270 | cpus_read_lock(); |
93981800 | 4271 | |
15316ba8 | 4272 | for_each_online_cpu(cpu) { |
9bfb1839 IM |
4273 | struct work_struct *work = per_cpu_ptr(works, cpu); |
4274 | ||
4275 | INIT_WORK(work, func); | |
b71ab8c2 | 4276 | schedule_work_on(cpu, work); |
65a64464 | 4277 | } |
93981800 TH |
4278 | |
4279 | for_each_online_cpu(cpu) | |
4280 | flush_work(per_cpu_ptr(works, cpu)); | |
4281 | ||
ffd8bea8 | 4282 | cpus_read_unlock(); |
b6136773 | 4283 | free_percpu(works); |
15316ba8 CL |
4284 | return 0; |
4285 | } | |
4286 | ||
1fa44eca JB |
4287 | /** |
4288 | * execute_in_process_context - reliably execute the routine with user context | |
4289 | * @fn: the function to execute | |
1fa44eca JB |
4290 | * @ew: guaranteed storage for the execute work structure (must |
4291 | * be available when the work executes) | |
4292 | * | |
4293 | * Executes the function immediately if process context is available, | |
4294 | * otherwise schedules the function for delayed execution. | |
4295 | * | |
d185af30 | 4296 | * Return: 0 - function was executed |
1fa44eca JB |
4297 | * 1 - function was scheduled for execution |
4298 | */ | |
65f27f38 | 4299 | int execute_in_process_context(work_func_t fn, struct execute_work *ew) |
1fa44eca JB |
4300 | { |
4301 | if (!in_interrupt()) { | |
65f27f38 | 4302 | fn(&ew->work); |
1fa44eca JB |
4303 | return 0; |
4304 | } | |
4305 | ||
65f27f38 | 4306 | INIT_WORK(&ew->work, fn); |
1fa44eca JB |
4307 | schedule_work(&ew->work); |
4308 | ||
4309 | return 1; | |
4310 | } | |
4311 | EXPORT_SYMBOL_GPL(execute_in_process_context); | |
4312 | ||
6ba94429 FW |
4313 | /** |
4314 | * free_workqueue_attrs - free a workqueue_attrs | |
4315 | * @attrs: workqueue_attrs to free | |
226223ab | 4316 | * |
6ba94429 | 4317 | * Undo alloc_workqueue_attrs(). |
226223ab | 4318 | */ |
513c98d0 | 4319 | void free_workqueue_attrs(struct workqueue_attrs *attrs) |
226223ab | 4320 | { |
6ba94429 FW |
4321 | if (attrs) { |
4322 | free_cpumask_var(attrs->cpumask); | |
9546b29e | 4323 | free_cpumask_var(attrs->__pod_cpumask); |
6ba94429 FW |
4324 | kfree(attrs); |
4325 | } | |
226223ab TH |
4326 | } |
4327 | ||
6ba94429 FW |
4328 | /** |
4329 | * alloc_workqueue_attrs - allocate a workqueue_attrs | |
6ba94429 FW |
4330 | * |
4331 | * Allocate a new workqueue_attrs, initialize with default settings and | |
4332 | * return it. | |
4333 | * | |
4334 | * Return: The allocated new workqueue_attr on success. %NULL on failure. | |
4335 | */ | |
513c98d0 | 4336 | struct workqueue_attrs *alloc_workqueue_attrs(void) |
226223ab | 4337 | { |
6ba94429 | 4338 | struct workqueue_attrs *attrs; |
226223ab | 4339 | |
be69d00d | 4340 | attrs = kzalloc(sizeof(*attrs), GFP_KERNEL); |
6ba94429 FW |
4341 | if (!attrs) |
4342 | goto fail; | |
be69d00d | 4343 | if (!alloc_cpumask_var(&attrs->cpumask, GFP_KERNEL)) |
6ba94429 | 4344 | goto fail; |
9546b29e TH |
4345 | if (!alloc_cpumask_var(&attrs->__pod_cpumask, GFP_KERNEL)) |
4346 | goto fail; | |
6ba94429 FW |
4347 | |
4348 | cpumask_copy(attrs->cpumask, cpu_possible_mask); | |
523a301e | 4349 | attrs->affn_scope = WQ_AFFN_DFL; |
6ba94429 FW |
4350 | return attrs; |
4351 | fail: | |
4352 | free_workqueue_attrs(attrs); | |
4353 | return NULL; | |
226223ab TH |
4354 | } |
4355 | ||
6ba94429 FW |
4356 | static void copy_workqueue_attrs(struct workqueue_attrs *to, |
4357 | const struct workqueue_attrs *from) | |
226223ab | 4358 | { |
6ba94429 FW |
4359 | to->nice = from->nice; |
4360 | cpumask_copy(to->cpumask, from->cpumask); | |
9546b29e | 4361 | cpumask_copy(to->__pod_cpumask, from->__pod_cpumask); |
8639eceb | 4362 | to->affn_strict = from->affn_strict; |
84193c07 | 4363 | |
6ba94429 | 4364 | /* |
84193c07 TH |
4365 | * Unlike hash and equality test, copying shouldn't ignore wq-only |
4366 | * fields as copying is used for both pool and wq attrs. Instead, | |
4367 | * get_unbound_pool() explicitly clears the fields. | |
6ba94429 | 4368 | */ |
84193c07 | 4369 | to->affn_scope = from->affn_scope; |
af73f5c9 | 4370 | to->ordered = from->ordered; |
226223ab TH |
4371 | } |
4372 | ||
5de7a03c TH |
4373 | /* |
4374 | * Some attrs fields are workqueue-only. Clear them for worker_pool's. See the | |
4375 | * comments in 'struct workqueue_attrs' definition. | |
4376 | */ | |
4377 | static void wqattrs_clear_for_pool(struct workqueue_attrs *attrs) | |
4378 | { | |
84193c07 | 4379 | attrs->affn_scope = WQ_AFFN_NR_TYPES; |
5de7a03c TH |
4380 | attrs->ordered = false; |
4381 | } | |
4382 | ||
6ba94429 FW |
4383 | /* hash value of the content of @attr */ |
4384 | static u32 wqattrs_hash(const struct workqueue_attrs *attrs) | |
226223ab | 4385 | { |
6ba94429 | 4386 | u32 hash = 0; |
226223ab | 4387 | |
6ba94429 FW |
4388 | hash = jhash_1word(attrs->nice, hash); |
4389 | hash = jhash(cpumask_bits(attrs->cpumask), | |
4390 | BITS_TO_LONGS(nr_cpumask_bits) * sizeof(long), hash); | |
9546b29e TH |
4391 | hash = jhash(cpumask_bits(attrs->__pod_cpumask), |
4392 | BITS_TO_LONGS(nr_cpumask_bits) * sizeof(long), hash); | |
8639eceb | 4393 | hash = jhash_1word(attrs->affn_strict, hash); |
6ba94429 | 4394 | return hash; |
226223ab | 4395 | } |
226223ab | 4396 | |
6ba94429 FW |
4397 | /* content equality test */ |
4398 | static bool wqattrs_equal(const struct workqueue_attrs *a, | |
4399 | const struct workqueue_attrs *b) | |
226223ab | 4400 | { |
6ba94429 FW |
4401 | if (a->nice != b->nice) |
4402 | return false; | |
4403 | if (!cpumask_equal(a->cpumask, b->cpumask)) | |
4404 | return false; | |
9546b29e TH |
4405 | if (!cpumask_equal(a->__pod_cpumask, b->__pod_cpumask)) |
4406 | return false; | |
8639eceb TH |
4407 | if (a->affn_strict != b->affn_strict) |
4408 | return false; | |
6ba94429 | 4409 | return true; |
226223ab TH |
4410 | } |
4411 | ||
0f36ee24 TH |
4412 | /* Update @attrs with actually available CPUs */ |
4413 | static void wqattrs_actualize_cpumask(struct workqueue_attrs *attrs, | |
4414 | const cpumask_t *unbound_cpumask) | |
4415 | { | |
4416 | /* | |
4417 | * Calculate the effective CPU mask of @attrs given @unbound_cpumask. If | |
4418 | * @attrs->cpumask doesn't overlap with @unbound_cpumask, we fallback to | |
4419 | * @unbound_cpumask. | |
4420 | */ | |
4421 | cpumask_and(attrs->cpumask, attrs->cpumask, unbound_cpumask); | |
4422 | if (unlikely(cpumask_empty(attrs->cpumask))) | |
4423 | cpumask_copy(attrs->cpumask, unbound_cpumask); | |
4424 | } | |
4425 | ||
84193c07 TH |
4426 | /* find wq_pod_type to use for @attrs */ |
4427 | static const struct wq_pod_type * | |
4428 | wqattrs_pod_type(const struct workqueue_attrs *attrs) | |
4429 | { | |
523a301e TH |
4430 | enum wq_affn_scope scope; |
4431 | struct wq_pod_type *pt; | |
4432 | ||
4433 | /* to synchronize access to wq_affn_dfl */ | |
4434 | lockdep_assert_held(&wq_pool_mutex); | |
4435 | ||
4436 | if (attrs->affn_scope == WQ_AFFN_DFL) | |
4437 | scope = wq_affn_dfl; | |
4438 | else | |
4439 | scope = attrs->affn_scope; | |
4440 | ||
4441 | pt = &wq_pod_types[scope]; | |
84193c07 TH |
4442 | |
4443 | if (!WARN_ON_ONCE(attrs->affn_scope == WQ_AFFN_NR_TYPES) && | |
4444 | likely(pt->nr_pods)) | |
4445 | return pt; | |
4446 | ||
4447 | /* | |
4448 | * Before workqueue_init_topology(), only SYSTEM is available which is | |
4449 | * initialized in workqueue_init_early(). | |
4450 | */ | |
4451 | pt = &wq_pod_types[WQ_AFFN_SYSTEM]; | |
4452 | BUG_ON(!pt->nr_pods); | |
4453 | return pt; | |
4454 | } | |
4455 | ||
6ba94429 FW |
4456 | /** |
4457 | * init_worker_pool - initialize a newly zalloc'd worker_pool | |
4458 | * @pool: worker_pool to initialize | |
4459 | * | |
402dd89d | 4460 | * Initialize a newly zalloc'd @pool. It also allocates @pool->attrs. |
6ba94429 FW |
4461 | * |
4462 | * Return: 0 on success, -errno on failure. Even on failure, all fields | |
4463 | * inside @pool proper are initialized and put_unbound_pool() can be called | |
4464 | * on @pool safely to release it. | |
4465 | */ | |
4466 | static int init_worker_pool(struct worker_pool *pool) | |
226223ab | 4467 | { |
a9b8a985 | 4468 | raw_spin_lock_init(&pool->lock); |
6ba94429 FW |
4469 | pool->id = -1; |
4470 | pool->cpu = -1; | |
4471 | pool->node = NUMA_NO_NODE; | |
4472 | pool->flags |= POOL_DISASSOCIATED; | |
82607adc | 4473 | pool->watchdog_ts = jiffies; |
6ba94429 FW |
4474 | INIT_LIST_HEAD(&pool->worklist); |
4475 | INIT_LIST_HEAD(&pool->idle_list); | |
4476 | hash_init(pool->busy_hash); | |
226223ab | 4477 | |
32a6c723 | 4478 | timer_setup(&pool->idle_timer, idle_worker_timeout, TIMER_DEFERRABLE); |
3f959aa3 | 4479 | INIT_WORK(&pool->idle_cull_work, idle_cull_fn); |
226223ab | 4480 | |
32a6c723 | 4481 | timer_setup(&pool->mayday_timer, pool_mayday_timeout, 0); |
226223ab | 4482 | |
6ba94429 | 4483 | INIT_LIST_HEAD(&pool->workers); |
e02b9312 | 4484 | INIT_LIST_HEAD(&pool->dying_workers); |
226223ab | 4485 | |
6ba94429 FW |
4486 | ida_init(&pool->worker_ida); |
4487 | INIT_HLIST_NODE(&pool->hash_node); | |
4488 | pool->refcnt = 1; | |
226223ab | 4489 | |
6ba94429 | 4490 | /* shouldn't fail above this point */ |
be69d00d | 4491 | pool->attrs = alloc_workqueue_attrs(); |
6ba94429 FW |
4492 | if (!pool->attrs) |
4493 | return -ENOMEM; | |
5de7a03c TH |
4494 | |
4495 | wqattrs_clear_for_pool(pool->attrs); | |
4496 | ||
6ba94429 | 4497 | return 0; |
226223ab TH |
4498 | } |
4499 | ||
669de8bd BVA |
4500 | #ifdef CONFIG_LOCKDEP |
4501 | static void wq_init_lockdep(struct workqueue_struct *wq) | |
4502 | { | |
4503 | char *lock_name; | |
4504 | ||
4505 | lockdep_register_key(&wq->key); | |
4506 | lock_name = kasprintf(GFP_KERNEL, "%s%s", "(wq_completion)", wq->name); | |
4507 | if (!lock_name) | |
4508 | lock_name = wq->name; | |
69a106c0 QC |
4509 | |
4510 | wq->lock_name = lock_name; | |
669de8bd BVA |
4511 | lockdep_init_map(&wq->lockdep_map, lock_name, &wq->key, 0); |
4512 | } | |
4513 | ||
4514 | static void wq_unregister_lockdep(struct workqueue_struct *wq) | |
4515 | { | |
4516 | lockdep_unregister_key(&wq->key); | |
4517 | } | |
4518 | ||
4519 | static void wq_free_lockdep(struct workqueue_struct *wq) | |
4520 | { | |
4521 | if (wq->lock_name != wq->name) | |
4522 | kfree(wq->lock_name); | |
4523 | } | |
4524 | #else | |
4525 | static void wq_init_lockdep(struct workqueue_struct *wq) | |
4526 | { | |
4527 | } | |
4528 | ||
4529 | static void wq_unregister_lockdep(struct workqueue_struct *wq) | |
4530 | { | |
4531 | } | |
4532 | ||
4533 | static void wq_free_lockdep(struct workqueue_struct *wq) | |
4534 | { | |
4535 | } | |
4536 | #endif | |
4537 | ||
91ccc6e7 TH |
4538 | static void free_node_nr_active(struct wq_node_nr_active **nna_ar) |
4539 | { | |
4540 | int node; | |
4541 | ||
4542 | for_each_node(node) { | |
4543 | kfree(nna_ar[node]); | |
4544 | nna_ar[node] = NULL; | |
4545 | } | |
4546 | ||
4547 | kfree(nna_ar[nr_node_ids]); | |
4548 | nna_ar[nr_node_ids] = NULL; | |
4549 | } | |
4550 | ||
4551 | static void init_node_nr_active(struct wq_node_nr_active *nna) | |
4552 | { | |
c5f8cd6c | 4553 | nna->max = WQ_DFL_MIN_ACTIVE; |
91ccc6e7 | 4554 | atomic_set(&nna->nr, 0); |
5797b1c1 TH |
4555 | raw_spin_lock_init(&nna->lock); |
4556 | INIT_LIST_HEAD(&nna->pending_pwqs); | |
91ccc6e7 TH |
4557 | } |
4558 | ||
4559 | /* | |
4560 | * Each node's nr_active counter will be accessed mostly from its own node and | |
4561 | * should be allocated in the node. | |
4562 | */ | |
4563 | static int alloc_node_nr_active(struct wq_node_nr_active **nna_ar) | |
4564 | { | |
4565 | struct wq_node_nr_active *nna; | |
4566 | int node; | |
4567 | ||
4568 | for_each_node(node) { | |
4569 | nna = kzalloc_node(sizeof(*nna), GFP_KERNEL, node); | |
4570 | if (!nna) | |
4571 | goto err_free; | |
4572 | init_node_nr_active(nna); | |
4573 | nna_ar[node] = nna; | |
4574 | } | |
4575 | ||
4576 | /* [nr_node_ids] is used as the fallback */ | |
4577 | nna = kzalloc_node(sizeof(*nna), GFP_KERNEL, NUMA_NO_NODE); | |
4578 | if (!nna) | |
4579 | goto err_free; | |
4580 | init_node_nr_active(nna); | |
4581 | nna_ar[nr_node_ids] = nna; | |
4582 | ||
4583 | return 0; | |
4584 | ||
4585 | err_free: | |
4586 | free_node_nr_active(nna_ar); | |
4587 | return -ENOMEM; | |
4588 | } | |
4589 | ||
6ba94429 | 4590 | static void rcu_free_wq(struct rcu_head *rcu) |
226223ab | 4591 | { |
6ba94429 FW |
4592 | struct workqueue_struct *wq = |
4593 | container_of(rcu, struct workqueue_struct, rcu); | |
226223ab | 4594 | |
91ccc6e7 TH |
4595 | if (wq->flags & WQ_UNBOUND) |
4596 | free_node_nr_active(wq->node_nr_active); | |
4597 | ||
669de8bd | 4598 | wq_free_lockdep(wq); |
636b927e TH |
4599 | free_percpu(wq->cpu_pwq); |
4600 | free_workqueue_attrs(wq->unbound_attrs); | |
6ba94429 | 4601 | kfree(wq); |
226223ab TH |
4602 | } |
4603 | ||
6ba94429 | 4604 | static void rcu_free_pool(struct rcu_head *rcu) |
226223ab | 4605 | { |
6ba94429 | 4606 | struct worker_pool *pool = container_of(rcu, struct worker_pool, rcu); |
226223ab | 4607 | |
6ba94429 FW |
4608 | ida_destroy(&pool->worker_ida); |
4609 | free_workqueue_attrs(pool->attrs); | |
4610 | kfree(pool); | |
226223ab TH |
4611 | } |
4612 | ||
6ba94429 FW |
4613 | /** |
4614 | * put_unbound_pool - put a worker_pool | |
4615 | * @pool: worker_pool to put | |
4616 | * | |
24acfb71 | 4617 | * Put @pool. If its refcnt reaches zero, it gets destroyed in RCU |
6ba94429 FW |
4618 | * safe manner. get_unbound_pool() calls this function on its failure path |
4619 | * and this function should be able to release pools which went through, | |
4620 | * successfully or not, init_worker_pool(). | |
4621 | * | |
4622 | * Should be called with wq_pool_mutex held. | |
4623 | */ | |
4624 | static void put_unbound_pool(struct worker_pool *pool) | |
226223ab | 4625 | { |
6ba94429 FW |
4626 | DECLARE_COMPLETION_ONSTACK(detach_completion); |
4627 | struct worker *worker; | |
9680540c | 4628 | LIST_HEAD(cull_list); |
e02b9312 | 4629 | |
6ba94429 | 4630 | lockdep_assert_held(&wq_pool_mutex); |
226223ab | 4631 | |
6ba94429 FW |
4632 | if (--pool->refcnt) |
4633 | return; | |
226223ab | 4634 | |
6ba94429 FW |
4635 | /* sanity checks */ |
4636 | if (WARN_ON(!(pool->cpu < 0)) || | |
4637 | WARN_ON(!list_empty(&pool->worklist))) | |
4638 | return; | |
226223ab | 4639 | |
6ba94429 FW |
4640 | /* release id and unhash */ |
4641 | if (pool->id >= 0) | |
4642 | idr_remove(&worker_pool_idr, pool->id); | |
4643 | hash_del(&pool->hash_node); | |
d55262c4 | 4644 | |
6ba94429 | 4645 | /* |
692b4825 TH |
4646 | * Become the manager and destroy all workers. This prevents |
4647 | * @pool's workers from blocking on attach_mutex. We're the last | |
4648 | * manager and @pool gets freed with the flag set. | |
9ab03be4 VS |
4649 | * |
4650 | * Having a concurrent manager is quite unlikely to happen as we can | |
4651 | * only get here with | |
4652 | * pwq->refcnt == pool->refcnt == 0 | |
4653 | * which implies no work queued to the pool, which implies no worker can | |
4654 | * become the manager. However a worker could have taken the role of | |
4655 | * manager before the refcnts dropped to 0, since maybe_create_worker() | |
4656 | * drops pool->lock | |
6ba94429 | 4657 | */ |
9ab03be4 VS |
4658 | while (true) { |
4659 | rcuwait_wait_event(&manager_wait, | |
4660 | !(pool->flags & POOL_MANAGER_ACTIVE), | |
4661 | TASK_UNINTERRUPTIBLE); | |
e02b9312 VS |
4662 | |
4663 | mutex_lock(&wq_pool_attach_mutex); | |
9ab03be4 VS |
4664 | raw_spin_lock_irq(&pool->lock); |
4665 | if (!(pool->flags & POOL_MANAGER_ACTIVE)) { | |
4666 | pool->flags |= POOL_MANAGER_ACTIVE; | |
4667 | break; | |
4668 | } | |
4669 | raw_spin_unlock_irq(&pool->lock); | |
e02b9312 | 4670 | mutex_unlock(&wq_pool_attach_mutex); |
9ab03be4 | 4671 | } |
692b4825 | 4672 | |
6ba94429 | 4673 | while ((worker = first_idle_worker(pool))) |
e02b9312 | 4674 | set_worker_dying(worker, &cull_list); |
6ba94429 | 4675 | WARN_ON(pool->nr_workers || pool->nr_idle); |
a9b8a985 | 4676 | raw_spin_unlock_irq(&pool->lock); |
d55262c4 | 4677 | |
e02b9312 VS |
4678 | wake_dying_workers(&cull_list); |
4679 | ||
4680 | if (!list_empty(&pool->workers) || !list_empty(&pool->dying_workers)) | |
6ba94429 | 4681 | pool->detach_completion = &detach_completion; |
1258fae7 | 4682 | mutex_unlock(&wq_pool_attach_mutex); |
226223ab | 4683 | |
6ba94429 FW |
4684 | if (pool->detach_completion) |
4685 | wait_for_completion(pool->detach_completion); | |
226223ab | 4686 | |
6ba94429 FW |
4687 | /* shut down the timers */ |
4688 | del_timer_sync(&pool->idle_timer); | |
3f959aa3 | 4689 | cancel_work_sync(&pool->idle_cull_work); |
6ba94429 | 4690 | del_timer_sync(&pool->mayday_timer); |
226223ab | 4691 | |
24acfb71 | 4692 | /* RCU protected to allow dereferences from get_work_pool() */ |
25b00775 | 4693 | call_rcu(&pool->rcu, rcu_free_pool); |
226223ab TH |
4694 | } |
4695 | ||
4696 | /** | |
6ba94429 FW |
4697 | * get_unbound_pool - get a worker_pool with the specified attributes |
4698 | * @attrs: the attributes of the worker_pool to get | |
226223ab | 4699 | * |
6ba94429 FW |
4700 | * Obtain a worker_pool which has the same attributes as @attrs, bump the |
4701 | * reference count and return it. If there already is a matching | |
4702 | * worker_pool, it will be used; otherwise, this function attempts to | |
4703 | * create a new one. | |
226223ab | 4704 | * |
6ba94429 | 4705 | * Should be called with wq_pool_mutex held. |
226223ab | 4706 | * |
6ba94429 FW |
4707 | * Return: On success, a worker_pool with the same attributes as @attrs. |
4708 | * On failure, %NULL. | |
226223ab | 4709 | */ |
6ba94429 | 4710 | static struct worker_pool *get_unbound_pool(const struct workqueue_attrs *attrs) |
226223ab | 4711 | { |
84193c07 | 4712 | struct wq_pod_type *pt = &wq_pod_types[WQ_AFFN_NUMA]; |
6ba94429 FW |
4713 | u32 hash = wqattrs_hash(attrs); |
4714 | struct worker_pool *pool; | |
84193c07 | 4715 | int pod, node = NUMA_NO_NODE; |
226223ab | 4716 | |
6ba94429 | 4717 | lockdep_assert_held(&wq_pool_mutex); |
226223ab | 4718 | |
6ba94429 FW |
4719 | /* do we already have a matching pool? */ |
4720 | hash_for_each_possible(unbound_pool_hash, pool, hash_node, hash) { | |
4721 | if (wqattrs_equal(pool->attrs, attrs)) { | |
4722 | pool->refcnt++; | |
4723 | return pool; | |
4724 | } | |
4725 | } | |
226223ab | 4726 | |
9546b29e | 4727 | /* If __pod_cpumask is contained inside a NUMA pod, that's our node */ |
84193c07 | 4728 | for (pod = 0; pod < pt->nr_pods; pod++) { |
9546b29e | 4729 | if (cpumask_subset(attrs->__pod_cpumask, pt->pod_cpus[pod])) { |
84193c07 TH |
4730 | node = pt->pod_node[pod]; |
4731 | break; | |
e2273584 XP |
4732 | } |
4733 | } | |
4734 | ||
6ba94429 | 4735 | /* nope, create a new one */ |
84193c07 | 4736 | pool = kzalloc_node(sizeof(*pool), GFP_KERNEL, node); |
6ba94429 FW |
4737 | if (!pool || init_worker_pool(pool) < 0) |
4738 | goto fail; | |
4739 | ||
84193c07 | 4740 | pool->node = node; |
5de7a03c TH |
4741 | copy_workqueue_attrs(pool->attrs, attrs); |
4742 | wqattrs_clear_for_pool(pool->attrs); | |
226223ab | 4743 | |
6ba94429 FW |
4744 | if (worker_pool_assign_id(pool) < 0) |
4745 | goto fail; | |
226223ab | 4746 | |
6ba94429 | 4747 | /* create and start the initial worker */ |
3347fa09 | 4748 | if (wq_online && !create_worker(pool)) |
6ba94429 | 4749 | goto fail; |
226223ab | 4750 | |
6ba94429 FW |
4751 | /* install */ |
4752 | hash_add(unbound_pool_hash, &pool->hash_node, hash); | |
226223ab | 4753 | |
6ba94429 FW |
4754 | return pool; |
4755 | fail: | |
4756 | if (pool) | |
4757 | put_unbound_pool(pool); | |
4758 | return NULL; | |
226223ab | 4759 | } |
226223ab | 4760 | |
6ba94429 | 4761 | static void rcu_free_pwq(struct rcu_head *rcu) |
7a4e344c | 4762 | { |
6ba94429 FW |
4763 | kmem_cache_free(pwq_cache, |
4764 | container_of(rcu, struct pool_workqueue, rcu)); | |
7a4e344c TH |
4765 | } |
4766 | ||
6ba94429 | 4767 | /* |
967b494e TH |
4768 | * Scheduled on pwq_release_worker by put_pwq() when an unbound pwq hits zero |
4769 | * refcnt and needs to be destroyed. | |
7a4e344c | 4770 | */ |
687a9aa5 | 4771 | static void pwq_release_workfn(struct kthread_work *work) |
7a4e344c | 4772 | { |
6ba94429 | 4773 | struct pool_workqueue *pwq = container_of(work, struct pool_workqueue, |
687a9aa5 | 4774 | release_work); |
6ba94429 FW |
4775 | struct workqueue_struct *wq = pwq->wq; |
4776 | struct worker_pool *pool = pwq->pool; | |
b42b0bdd | 4777 | bool is_last = false; |
7a4e344c | 4778 | |
b42b0bdd | 4779 | /* |
687a9aa5 | 4780 | * When @pwq is not linked, it doesn't hold any reference to the |
b42b0bdd YY |
4781 | * @wq, and @wq is invalid to access. |
4782 | */ | |
4783 | if (!list_empty(&pwq->pwqs_node)) { | |
b42b0bdd YY |
4784 | mutex_lock(&wq->mutex); |
4785 | list_del_rcu(&pwq->pwqs_node); | |
4786 | is_last = list_empty(&wq->pwqs); | |
4c065dbc WL |
4787 | |
4788 | /* | |
4789 | * For ordered workqueue with a plugged dfl_pwq, restart it now. | |
4790 | */ | |
4791 | if (!is_last && (wq->flags & __WQ_ORDERED)) | |
4792 | unplug_oldest_pwq(wq); | |
4793 | ||
b42b0bdd YY |
4794 | mutex_unlock(&wq->mutex); |
4795 | } | |
6ba94429 | 4796 | |
687a9aa5 TH |
4797 | if (wq->flags & WQ_UNBOUND) { |
4798 | mutex_lock(&wq_pool_mutex); | |
4799 | put_unbound_pool(pool); | |
4800 | mutex_unlock(&wq_pool_mutex); | |
4801 | } | |
6ba94429 | 4802 | |
5797b1c1 TH |
4803 | if (!list_empty(&pwq->pending_node)) { |
4804 | struct wq_node_nr_active *nna = | |
4805 | wq_node_nr_active(pwq->wq, pwq->pool->node); | |
4806 | ||
4807 | raw_spin_lock_irq(&nna->lock); | |
4808 | list_del_init(&pwq->pending_node); | |
4809 | raw_spin_unlock_irq(&nna->lock); | |
4810 | } | |
4811 | ||
25b00775 | 4812 | call_rcu(&pwq->rcu, rcu_free_pwq); |
7a4e344c | 4813 | |
2865a8fb | 4814 | /* |
6ba94429 FW |
4815 | * If we're the last pwq going away, @wq is already dead and no one |
4816 | * is gonna access it anymore. Schedule RCU free. | |
2865a8fb | 4817 | */ |
669de8bd BVA |
4818 | if (is_last) { |
4819 | wq_unregister_lockdep(wq); | |
25b00775 | 4820 | call_rcu(&wq->rcu, rcu_free_wq); |
669de8bd | 4821 | } |
29c91e99 TH |
4822 | } |
4823 | ||
67dc8325 | 4824 | /* initialize newly allocated @pwq which is associated with @wq and @pool */ |
6ba94429 FW |
4825 | static void init_pwq(struct pool_workqueue *pwq, struct workqueue_struct *wq, |
4826 | struct worker_pool *pool) | |
29c91e99 | 4827 | { |
6ba94429 | 4828 | BUG_ON((unsigned long)pwq & WORK_STRUCT_FLAG_MASK); |
29c91e99 | 4829 | |
6ba94429 FW |
4830 | memset(pwq, 0, sizeof(*pwq)); |
4831 | ||
4832 | pwq->pool = pool; | |
4833 | pwq->wq = wq; | |
4834 | pwq->flush_color = -1; | |
4835 | pwq->refcnt = 1; | |
f97a4a1a | 4836 | INIT_LIST_HEAD(&pwq->inactive_works); |
5797b1c1 | 4837 | INIT_LIST_HEAD(&pwq->pending_node); |
6ba94429 FW |
4838 | INIT_LIST_HEAD(&pwq->pwqs_node); |
4839 | INIT_LIST_HEAD(&pwq->mayday_node); | |
687a9aa5 | 4840 | kthread_init_work(&pwq->release_work, pwq_release_workfn); |
29c91e99 TH |
4841 | } |
4842 | ||
6ba94429 FW |
4843 | /* sync @pwq with the current state of its associated wq and link it */ |
4844 | static void link_pwq(struct pool_workqueue *pwq) | |
29c91e99 | 4845 | { |
6ba94429 | 4846 | struct workqueue_struct *wq = pwq->wq; |
29c91e99 | 4847 | |
6ba94429 | 4848 | lockdep_assert_held(&wq->mutex); |
a892cacc | 4849 | |
6ba94429 FW |
4850 | /* may be called multiple times, ignore if already linked */ |
4851 | if (!list_empty(&pwq->pwqs_node)) | |
29c91e99 | 4852 | return; |
29c91e99 | 4853 | |
6ba94429 FW |
4854 | /* set the matching work_color */ |
4855 | pwq->work_color = wq->work_color; | |
29c91e99 | 4856 | |
6ba94429 | 4857 | /* link in @pwq */ |
26fb7e3d | 4858 | list_add_tail_rcu(&pwq->pwqs_node, &wq->pwqs); |
6ba94429 | 4859 | } |
29c91e99 | 4860 | |
6ba94429 FW |
4861 | /* obtain a pool matching @attr and create a pwq associating the pool and @wq */ |
4862 | static struct pool_workqueue *alloc_unbound_pwq(struct workqueue_struct *wq, | |
4863 | const struct workqueue_attrs *attrs) | |
4864 | { | |
4865 | struct worker_pool *pool; | |
4866 | struct pool_workqueue *pwq; | |
60f5a4bc | 4867 | |
6ba94429 | 4868 | lockdep_assert_held(&wq_pool_mutex); |
60f5a4bc | 4869 | |
6ba94429 FW |
4870 | pool = get_unbound_pool(attrs); |
4871 | if (!pool) | |
4872 | return NULL; | |
60f5a4bc | 4873 | |
6ba94429 FW |
4874 | pwq = kmem_cache_alloc_node(pwq_cache, GFP_KERNEL, pool->node); |
4875 | if (!pwq) { | |
4876 | put_unbound_pool(pool); | |
4877 | return NULL; | |
4878 | } | |
29c91e99 | 4879 | |
6ba94429 FW |
4880 | init_pwq(pwq, wq, pool); |
4881 | return pwq; | |
4882 | } | |
29c91e99 | 4883 | |
29c91e99 | 4884 | /** |
fef59c9c | 4885 | * wq_calc_pod_cpumask - calculate a wq_attrs' cpumask for a pod |
042f7df1 | 4886 | * @attrs: the wq_attrs of the default pwq of the target workqueue |
84193c07 | 4887 | * @cpu: the target CPU |
6ba94429 | 4888 | * @cpu_going_down: if >= 0, the CPU to consider as offline |
29c91e99 | 4889 | * |
fef59c9c TH |
4890 | * Calculate the cpumask a workqueue with @attrs should use on @pod. If |
4891 | * @cpu_going_down is >= 0, that cpu is considered offline during calculation. | |
9546b29e | 4892 | * The result is stored in @attrs->__pod_cpumask. |
a892cacc | 4893 | * |
fef59c9c TH |
4894 | * If pod affinity is not enabled, @attrs->cpumask is always used. If enabled |
4895 | * and @pod has online CPUs requested by @attrs, the returned cpumask is the | |
4896 | * intersection of the possible CPUs of @pod and @attrs->cpumask. | |
d185af30 | 4897 | * |
fef59c9c | 4898 | * The caller is responsible for ensuring that the cpumask of @pod stays stable. |
29c91e99 | 4899 | */ |
9546b29e TH |
4900 | static void wq_calc_pod_cpumask(struct workqueue_attrs *attrs, int cpu, |
4901 | int cpu_going_down) | |
29c91e99 | 4902 | { |
84193c07 TH |
4903 | const struct wq_pod_type *pt = wqattrs_pod_type(attrs); |
4904 | int pod = pt->cpu_pod[cpu]; | |
29c91e99 | 4905 | |
fef59c9c | 4906 | /* does @pod have any online CPUs @attrs wants? */ |
9546b29e TH |
4907 | cpumask_and(attrs->__pod_cpumask, pt->pod_cpus[pod], attrs->cpumask); |
4908 | cpumask_and(attrs->__pod_cpumask, attrs->__pod_cpumask, cpu_online_mask); | |
6ba94429 | 4909 | if (cpu_going_down >= 0) |
9546b29e | 4910 | cpumask_clear_cpu(cpu_going_down, attrs->__pod_cpumask); |
29c91e99 | 4911 | |
9546b29e TH |
4912 | if (cpumask_empty(attrs->__pod_cpumask)) { |
4913 | cpumask_copy(attrs->__pod_cpumask, attrs->cpumask); | |
84193c07 TH |
4914 | return; |
4915 | } | |
4c16bd32 | 4916 | |
fef59c9c | 4917 | /* yeap, return possible CPUs in @pod that @attrs wants */ |
9546b29e | 4918 | cpumask_and(attrs->__pod_cpumask, attrs->cpumask, pt->pod_cpus[pod]); |
1ad0f0a7 | 4919 | |
9546b29e | 4920 | if (cpumask_empty(attrs->__pod_cpumask)) |
1ad0f0a7 MB |
4921 | pr_warn_once("WARNING: workqueue cpumask: online intersect > " |
4922 | "possible intersect\n"); | |
4c16bd32 TH |
4923 | } |
4924 | ||
9f66cff2 | 4925 | /* install @pwq into @wq and return the old pwq, @cpu < 0 for dfl_pwq */ |
636b927e TH |
4926 | static struct pool_workqueue *install_unbound_pwq(struct workqueue_struct *wq, |
4927 | int cpu, struct pool_workqueue *pwq) | |
1befcf30 | 4928 | { |
9f66cff2 | 4929 | struct pool_workqueue __rcu **slot = unbound_pwq_slot(wq, cpu); |
1befcf30 TH |
4930 | struct pool_workqueue *old_pwq; |
4931 | ||
5b95e1af | 4932 | lockdep_assert_held(&wq_pool_mutex); |
1befcf30 TH |
4933 | lockdep_assert_held(&wq->mutex); |
4934 | ||
4935 | /* link_pwq() can handle duplicate calls */ | |
4936 | link_pwq(pwq); | |
4937 | ||
9f66cff2 TH |
4938 | old_pwq = rcu_access_pointer(*slot); |
4939 | rcu_assign_pointer(*slot, pwq); | |
1befcf30 TH |
4940 | return old_pwq; |
4941 | } | |
4942 | ||
2d5f0764 LJ |
4943 | /* context to store the prepared attrs & pwqs before applying */ |
4944 | struct apply_wqattrs_ctx { | |
4945 | struct workqueue_struct *wq; /* target workqueue */ | |
4946 | struct workqueue_attrs *attrs; /* attrs to apply */ | |
042f7df1 | 4947 | struct list_head list; /* queued for batching commit */ |
2d5f0764 LJ |
4948 | struct pool_workqueue *dfl_pwq; |
4949 | struct pool_workqueue *pwq_tbl[]; | |
4950 | }; | |
4951 | ||
4952 | /* free the resources after success or abort */ | |
4953 | static void apply_wqattrs_cleanup(struct apply_wqattrs_ctx *ctx) | |
4954 | { | |
4955 | if (ctx) { | |
636b927e | 4956 | int cpu; |
2d5f0764 | 4957 | |
636b927e TH |
4958 | for_each_possible_cpu(cpu) |
4959 | put_pwq_unlocked(ctx->pwq_tbl[cpu]); | |
2d5f0764 LJ |
4960 | put_pwq_unlocked(ctx->dfl_pwq); |
4961 | ||
4962 | free_workqueue_attrs(ctx->attrs); | |
4963 | ||
4964 | kfree(ctx); | |
4965 | } | |
4966 | } | |
4967 | ||
4968 | /* allocate the attrs and pwqs for later installation */ | |
4969 | static struct apply_wqattrs_ctx * | |
4970 | apply_wqattrs_prepare(struct workqueue_struct *wq, | |
99c621ef LJ |
4971 | const struct workqueue_attrs *attrs, |
4972 | const cpumask_var_t unbound_cpumask) | |
9e8cd2f5 | 4973 | { |
2d5f0764 | 4974 | struct apply_wqattrs_ctx *ctx; |
9546b29e | 4975 | struct workqueue_attrs *new_attrs; |
636b927e | 4976 | int cpu; |
9e8cd2f5 | 4977 | |
2d5f0764 | 4978 | lockdep_assert_held(&wq_pool_mutex); |
9e8cd2f5 | 4979 | |
84193c07 TH |
4980 | if (WARN_ON(attrs->affn_scope < 0 || |
4981 | attrs->affn_scope >= WQ_AFFN_NR_TYPES)) | |
4982 | return ERR_PTR(-EINVAL); | |
4983 | ||
636b927e | 4984 | ctx = kzalloc(struct_size(ctx, pwq_tbl, nr_cpu_ids), GFP_KERNEL); |
8719dcea | 4985 | |
be69d00d | 4986 | new_attrs = alloc_workqueue_attrs(); |
9546b29e | 4987 | if (!ctx || !new_attrs) |
2d5f0764 | 4988 | goto out_free; |
13e2e556 | 4989 | |
4c16bd32 TH |
4990 | /* |
4991 | * If something goes wrong during CPU up/down, we'll fall back to | |
4992 | * the default pwq covering whole @attrs->cpumask. Always create | |
4993 | * it even if we don't use it immediately. | |
4994 | */ | |
0f36ee24 TH |
4995 | copy_workqueue_attrs(new_attrs, attrs); |
4996 | wqattrs_actualize_cpumask(new_attrs, unbound_cpumask); | |
9546b29e | 4997 | cpumask_copy(new_attrs->__pod_cpumask, new_attrs->cpumask); |
2d5f0764 LJ |
4998 | ctx->dfl_pwq = alloc_unbound_pwq(wq, new_attrs); |
4999 | if (!ctx->dfl_pwq) | |
5000 | goto out_free; | |
4c16bd32 | 5001 | |
636b927e | 5002 | for_each_possible_cpu(cpu) { |
af73f5c9 | 5003 | if (new_attrs->ordered) { |
2d5f0764 | 5004 | ctx->dfl_pwq->refcnt++; |
636b927e TH |
5005 | ctx->pwq_tbl[cpu] = ctx->dfl_pwq; |
5006 | } else { | |
9546b29e TH |
5007 | wq_calc_pod_cpumask(new_attrs, cpu, -1); |
5008 | ctx->pwq_tbl[cpu] = alloc_unbound_pwq(wq, new_attrs); | |
636b927e TH |
5009 | if (!ctx->pwq_tbl[cpu]) |
5010 | goto out_free; | |
4c16bd32 TH |
5011 | } |
5012 | } | |
5013 | ||
042f7df1 LJ |
5014 | /* save the user configured attrs and sanitize it. */ |
5015 | copy_workqueue_attrs(new_attrs, attrs); | |
5016 | cpumask_and(new_attrs->cpumask, new_attrs->cpumask, cpu_possible_mask); | |
9546b29e | 5017 | cpumask_copy(new_attrs->__pod_cpumask, new_attrs->cpumask); |
2d5f0764 | 5018 | ctx->attrs = new_attrs; |
042f7df1 | 5019 | |
4c065dbc WL |
5020 | /* |
5021 | * For initialized ordered workqueues, there should only be one pwq | |
5022 | * (dfl_pwq). Set the plugged flag of ctx->dfl_pwq to suspend execution | |
5023 | * of newly queued work items until execution of older work items in | |
5024 | * the old pwq's have completed. | |
5025 | */ | |
5026 | if ((wq->flags & __WQ_ORDERED) && !list_empty(&wq->pwqs)) | |
5027 | ctx->dfl_pwq->plugged = true; | |
5028 | ||
2d5f0764 | 5029 | ctx->wq = wq; |
2d5f0764 LJ |
5030 | return ctx; |
5031 | ||
5032 | out_free: | |
2d5f0764 LJ |
5033 | free_workqueue_attrs(new_attrs); |
5034 | apply_wqattrs_cleanup(ctx); | |
84193c07 | 5035 | return ERR_PTR(-ENOMEM); |
2d5f0764 LJ |
5036 | } |
5037 | ||
5038 | /* set attrs and install prepared pwqs, @ctx points to old pwqs on return */ | |
5039 | static void apply_wqattrs_commit(struct apply_wqattrs_ctx *ctx) | |
5040 | { | |
636b927e | 5041 | int cpu; |
9e8cd2f5 | 5042 | |
4c16bd32 | 5043 | /* all pwqs have been created successfully, let's install'em */ |
2d5f0764 | 5044 | mutex_lock(&ctx->wq->mutex); |
a892cacc | 5045 | |
2d5f0764 | 5046 | copy_workqueue_attrs(ctx->wq->unbound_attrs, ctx->attrs); |
4c16bd32 | 5047 | |
9f66cff2 | 5048 | /* save the previous pwqs and install the new ones */ |
636b927e TH |
5049 | for_each_possible_cpu(cpu) |
5050 | ctx->pwq_tbl[cpu] = install_unbound_pwq(ctx->wq, cpu, | |
5051 | ctx->pwq_tbl[cpu]); | |
9f66cff2 | 5052 | ctx->dfl_pwq = install_unbound_pwq(ctx->wq, -1, ctx->dfl_pwq); |
f147f29e | 5053 | |
5797b1c1 TH |
5054 | /* update node_nr_active->max */ |
5055 | wq_update_node_max_active(ctx->wq, -1); | |
5056 | ||
d64f2fa0 JL |
5057 | /* rescuer needs to respect wq cpumask changes */ |
5058 | if (ctx->wq->rescuer) | |
5059 | set_cpus_allowed_ptr(ctx->wq->rescuer->task, | |
5060 | unbound_effective_cpumask(ctx->wq)); | |
5061 | ||
2d5f0764 LJ |
5062 | mutex_unlock(&ctx->wq->mutex); |
5063 | } | |
9e8cd2f5 | 5064 | |
a0111cf6 LJ |
5065 | static int apply_workqueue_attrs_locked(struct workqueue_struct *wq, |
5066 | const struct workqueue_attrs *attrs) | |
2d5f0764 LJ |
5067 | { |
5068 | struct apply_wqattrs_ctx *ctx; | |
4c16bd32 | 5069 | |
2d5f0764 LJ |
5070 | /* only unbound workqueues can change attributes */ |
5071 | if (WARN_ON(!(wq->flags & WQ_UNBOUND))) | |
5072 | return -EINVAL; | |
13e2e556 | 5073 | |
99c621ef | 5074 | ctx = apply_wqattrs_prepare(wq, attrs, wq_unbound_cpumask); |
84193c07 TH |
5075 | if (IS_ERR(ctx)) |
5076 | return PTR_ERR(ctx); | |
2d5f0764 LJ |
5077 | |
5078 | /* the ctx has been prepared successfully, let's commit it */ | |
6201171e | 5079 | apply_wqattrs_commit(ctx); |
2d5f0764 LJ |
5080 | apply_wqattrs_cleanup(ctx); |
5081 | ||
6201171e | 5082 | return 0; |
9e8cd2f5 TH |
5083 | } |
5084 | ||
a0111cf6 LJ |
5085 | /** |
5086 | * apply_workqueue_attrs - apply new workqueue_attrs to an unbound workqueue | |
5087 | * @wq: the target workqueue | |
5088 | * @attrs: the workqueue_attrs to apply, allocated with alloc_workqueue_attrs() | |
5089 | * | |
fef59c9c TH |
5090 | * Apply @attrs to an unbound workqueue @wq. Unless disabled, this function maps |
5091 | * a separate pwq to each CPU pod with possibles CPUs in @attrs->cpumask so that | |
5092 | * work items are affine to the pod it was issued on. Older pwqs are released as | |
5093 | * in-flight work items finish. Note that a work item which repeatedly requeues | |
5094 | * itself back-to-back will stay on its current pwq. | |
a0111cf6 LJ |
5095 | * |
5096 | * Performs GFP_KERNEL allocations. | |
5097 | * | |
ffd8bea8 | 5098 | * Assumes caller has CPU hotplug read exclusion, i.e. cpus_read_lock(). |
509b3204 | 5099 | * |
a0111cf6 LJ |
5100 | * Return: 0 on success and -errno on failure. |
5101 | */ | |
513c98d0 | 5102 | int apply_workqueue_attrs(struct workqueue_struct *wq, |
a0111cf6 LJ |
5103 | const struct workqueue_attrs *attrs) |
5104 | { | |
5105 | int ret; | |
5106 | ||
509b3204 DJ |
5107 | lockdep_assert_cpus_held(); |
5108 | ||
5109 | mutex_lock(&wq_pool_mutex); | |
a0111cf6 | 5110 | ret = apply_workqueue_attrs_locked(wq, attrs); |
509b3204 | 5111 | mutex_unlock(&wq_pool_mutex); |
a0111cf6 LJ |
5112 | |
5113 | return ret; | |
5114 | } | |
5115 | ||
4c16bd32 | 5116 | /** |
fef59c9c | 5117 | * wq_update_pod - update pod affinity of a wq for CPU hot[un]plug |
4c16bd32 | 5118 | * @wq: the target workqueue |
4cbfd3de TH |
5119 | * @cpu: the CPU to update pool association for |
5120 | * @hotplug_cpu: the CPU coming up or going down | |
4c16bd32 TH |
5121 | * @online: whether @cpu is coming up or going down |
5122 | * | |
5123 | * This function is to be called from %CPU_DOWN_PREPARE, %CPU_ONLINE and | |
fef59c9c | 5124 | * %CPU_DOWN_FAILED. @cpu is being hot[un]plugged, update pod affinity of |
4c16bd32 TH |
5125 | * @wq accordingly. |
5126 | * | |
fef59c9c TH |
5127 | * |
5128 | * If pod affinity can't be adjusted due to memory allocation failure, it falls | |
5129 | * back to @wq->dfl_pwq which may not be optimal but is always correct. | |
5130 | * | |
5131 | * Note that when the last allowed CPU of a pod goes offline for a workqueue | |
5132 | * with a cpumask spanning multiple pods, the workers which were already | |
5133 | * executing the work items for the workqueue will lose their CPU affinity and | |
5134 | * may execute on any CPU. This is similar to how per-cpu workqueues behave on | |
5135 | * CPU_DOWN. If a workqueue user wants strict affinity, it's the user's | |
5136 | * responsibility to flush the work item from CPU_DOWN_PREPARE. | |
4c16bd32 | 5137 | */ |
fef59c9c TH |
5138 | static void wq_update_pod(struct workqueue_struct *wq, int cpu, |
5139 | int hotplug_cpu, bool online) | |
4c16bd32 | 5140 | { |
4cbfd3de | 5141 | int off_cpu = online ? -1 : hotplug_cpu; |
4c16bd32 TH |
5142 | struct pool_workqueue *old_pwq = NULL, *pwq; |
5143 | struct workqueue_attrs *target_attrs; | |
4c16bd32 TH |
5144 | |
5145 | lockdep_assert_held(&wq_pool_mutex); | |
5146 | ||
84193c07 | 5147 | if (!(wq->flags & WQ_UNBOUND) || wq->unbound_attrs->ordered) |
4c16bd32 TH |
5148 | return; |
5149 | ||
5150 | /* | |
5151 | * We don't wanna alloc/free wq_attrs for each wq for each CPU. | |
5152 | * Let's use a preallocated one. The following buf is protected by | |
5153 | * CPU hotplug exclusion. | |
5154 | */ | |
fef59c9c | 5155 | target_attrs = wq_update_pod_attrs_buf; |
4c16bd32 | 5156 | |
4c16bd32 | 5157 | copy_workqueue_attrs(target_attrs, wq->unbound_attrs); |
0f36ee24 | 5158 | wqattrs_actualize_cpumask(target_attrs, wq_unbound_cpumask); |
4c16bd32 | 5159 | |
636b927e | 5160 | /* nothing to do if the target cpumask matches the current pwq */ |
9546b29e | 5161 | wq_calc_pod_cpumask(target_attrs, cpu, off_cpu); |
9f66cff2 | 5162 | if (wqattrs_equal(target_attrs, unbound_pwq(wq, cpu)->pool->attrs)) |
636b927e | 5163 | return; |
4c16bd32 | 5164 | |
4c16bd32 TH |
5165 | /* create a new pwq */ |
5166 | pwq = alloc_unbound_pwq(wq, target_attrs); | |
5167 | if (!pwq) { | |
fef59c9c | 5168 | pr_warn("workqueue: allocation failed while updating CPU pod affinity of \"%s\"\n", |
2d916033 | 5169 | wq->name); |
77f300b1 | 5170 | goto use_dfl_pwq; |
4c16bd32 TH |
5171 | } |
5172 | ||
f7142ed4 | 5173 | /* Install the new pwq. */ |
4c16bd32 | 5174 | mutex_lock(&wq->mutex); |
636b927e | 5175 | old_pwq = install_unbound_pwq(wq, cpu, pwq); |
4c16bd32 TH |
5176 | goto out_unlock; |
5177 | ||
5178 | use_dfl_pwq: | |
f7142ed4 | 5179 | mutex_lock(&wq->mutex); |
9f66cff2 TH |
5180 | pwq = unbound_pwq(wq, -1); |
5181 | raw_spin_lock_irq(&pwq->pool->lock); | |
5182 | get_pwq(pwq); | |
5183 | raw_spin_unlock_irq(&pwq->pool->lock); | |
5184 | old_pwq = install_unbound_pwq(wq, cpu, pwq); | |
4c16bd32 TH |
5185 | out_unlock: |
5186 | mutex_unlock(&wq->mutex); | |
5187 | put_pwq_unlocked(old_pwq); | |
5188 | } | |
5189 | ||
30cdf249 | 5190 | static int alloc_and_link_pwqs(struct workqueue_struct *wq) |
0f900049 | 5191 | { |
49e3cf44 | 5192 | bool highpri = wq->flags & WQ_HIGHPRI; |
8a2b7538 | 5193 | int cpu, ret; |
30cdf249 | 5194 | |
636b927e TH |
5195 | wq->cpu_pwq = alloc_percpu(struct pool_workqueue *); |
5196 | if (!wq->cpu_pwq) | |
5197 | goto enomem; | |
30cdf249 | 5198 | |
636b927e | 5199 | if (!(wq->flags & WQ_UNBOUND)) { |
30cdf249 | 5200 | for_each_possible_cpu(cpu) { |
4cb1ef64 TH |
5201 | struct pool_workqueue **pwq_p; |
5202 | struct worker_pool __percpu *pools; | |
5203 | struct worker_pool *pool; | |
5204 | ||
5205 | if (wq->flags & WQ_BH) | |
5206 | pools = bh_worker_pools; | |
5207 | else | |
5208 | pools = cpu_worker_pools; | |
5209 | ||
5210 | pool = &(per_cpu_ptr(pools, cpu)[highpri]); | |
5211 | pwq_p = per_cpu_ptr(wq->cpu_pwq, cpu); | |
687a9aa5 TH |
5212 | |
5213 | *pwq_p = kmem_cache_alloc_node(pwq_cache, GFP_KERNEL, | |
5214 | pool->node); | |
5215 | if (!*pwq_p) | |
5216 | goto enomem; | |
f3421797 | 5217 | |
687a9aa5 | 5218 | init_pwq(*pwq_p, wq, pool); |
f147f29e TH |
5219 | |
5220 | mutex_lock(&wq->mutex); | |
687a9aa5 | 5221 | link_pwq(*pwq_p); |
f147f29e | 5222 | mutex_unlock(&wq->mutex); |
30cdf249 | 5223 | } |
9e8cd2f5 | 5224 | return 0; |
509b3204 DJ |
5225 | } |
5226 | ||
ffd8bea8 | 5227 | cpus_read_lock(); |
509b3204 | 5228 | if (wq->flags & __WQ_ORDERED) { |
9f66cff2 TH |
5229 | struct pool_workqueue *dfl_pwq; |
5230 | ||
8a2b7538 TH |
5231 | ret = apply_workqueue_attrs(wq, ordered_wq_attrs[highpri]); |
5232 | /* there should only be single pwq for ordering guarantee */ | |
9f66cff2 TH |
5233 | dfl_pwq = rcu_access_pointer(wq->dfl_pwq); |
5234 | WARN(!ret && (wq->pwqs.next != &dfl_pwq->pwqs_node || | |
5235 | wq->pwqs.prev != &dfl_pwq->pwqs_node), | |
8a2b7538 | 5236 | "ordering guarantee broken for workqueue %s\n", wq->name); |
30cdf249 | 5237 | } else { |
509b3204 | 5238 | ret = apply_workqueue_attrs(wq, unbound_std_wq_attrs[highpri]); |
30cdf249 | 5239 | } |
ffd8bea8 | 5240 | cpus_read_unlock(); |
509b3204 | 5241 | |
64344553 Z |
5242 | /* for unbound pwq, flush the pwq_release_worker ensures that the |
5243 | * pwq_release_workfn() completes before calling kfree(wq). | |
5244 | */ | |
5245 | if (ret) | |
5246 | kthread_flush_worker(pwq_release_worker); | |
5247 | ||
509b3204 | 5248 | return ret; |
687a9aa5 TH |
5249 | |
5250 | enomem: | |
5251 | if (wq->cpu_pwq) { | |
7b42f401 Z |
5252 | for_each_possible_cpu(cpu) { |
5253 | struct pool_workqueue *pwq = *per_cpu_ptr(wq->cpu_pwq, cpu); | |
5254 | ||
5255 | if (pwq) | |
5256 | kmem_cache_free(pwq_cache, pwq); | |
5257 | } | |
687a9aa5 TH |
5258 | free_percpu(wq->cpu_pwq); |
5259 | wq->cpu_pwq = NULL; | |
5260 | } | |
5261 | return -ENOMEM; | |
0f900049 TH |
5262 | } |
5263 | ||
f3421797 TH |
5264 | static int wq_clamp_max_active(int max_active, unsigned int flags, |
5265 | const char *name) | |
b71ab8c2 | 5266 | { |
636b927e | 5267 | if (max_active < 1 || max_active > WQ_MAX_ACTIVE) |
044c782c | 5268 | pr_warn("workqueue: max_active %d requested for %s is out of range, clamping between %d and %d\n", |
636b927e | 5269 | max_active, name, 1, WQ_MAX_ACTIVE); |
b71ab8c2 | 5270 | |
636b927e | 5271 | return clamp_val(max_active, 1, WQ_MAX_ACTIVE); |
b71ab8c2 TH |
5272 | } |
5273 | ||
983c7515 TH |
5274 | /* |
5275 | * Workqueues which may be used during memory reclaim should have a rescuer | |
5276 | * to guarantee forward progress. | |
5277 | */ | |
5278 | static int init_rescuer(struct workqueue_struct *wq) | |
5279 | { | |
5280 | struct worker *rescuer; | |
b92b36ea | 5281 | int ret; |
983c7515 TH |
5282 | |
5283 | if (!(wq->flags & WQ_MEM_RECLAIM)) | |
5284 | return 0; | |
5285 | ||
5286 | rescuer = alloc_worker(NUMA_NO_NODE); | |
4c0736a7 PM |
5287 | if (!rescuer) { |
5288 | pr_err("workqueue: Failed to allocate a rescuer for wq \"%s\"\n", | |
5289 | wq->name); | |
983c7515 | 5290 | return -ENOMEM; |
4c0736a7 | 5291 | } |
983c7515 TH |
5292 | |
5293 | rescuer->rescue_wq = wq; | |
b6a46f72 | 5294 | rescuer->task = kthread_create(rescuer_thread, rescuer, "kworker/R-%s", wq->name); |
f187b697 | 5295 | if (IS_ERR(rescuer->task)) { |
b92b36ea | 5296 | ret = PTR_ERR(rescuer->task); |
4c0736a7 PM |
5297 | pr_err("workqueue: Failed to create a rescuer kthread for wq \"%s\": %pe", |
5298 | wq->name, ERR_PTR(ret)); | |
983c7515 | 5299 | kfree(rescuer); |
b92b36ea | 5300 | return ret; |
983c7515 TH |
5301 | } |
5302 | ||
5303 | wq->rescuer = rescuer; | |
85f0ab43 | 5304 | if (wq->flags & WQ_UNBOUND) |
49584bb8 | 5305 | kthread_bind_mask(rescuer->task, wq_unbound_cpumask); |
85f0ab43 JL |
5306 | else |
5307 | kthread_bind_mask(rescuer->task, cpu_possible_mask); | |
983c7515 TH |
5308 | wake_up_process(rescuer->task); |
5309 | ||
5310 | return 0; | |
5311 | } | |
5312 | ||
a045a272 TH |
5313 | /** |
5314 | * wq_adjust_max_active - update a wq's max_active to the current setting | |
5315 | * @wq: target workqueue | |
5316 | * | |
5317 | * If @wq isn't freezing, set @wq->max_active to the saved_max_active and | |
5318 | * activate inactive work items accordingly. If @wq is freezing, clear | |
5319 | * @wq->max_active to zero. | |
5320 | */ | |
5321 | static void wq_adjust_max_active(struct workqueue_struct *wq) | |
5322 | { | |
c5404d4e | 5323 | bool activated; |
5797b1c1 | 5324 | int new_max, new_min; |
a045a272 TH |
5325 | |
5326 | lockdep_assert_held(&wq->mutex); | |
5327 | ||
5328 | if ((wq->flags & WQ_FREEZABLE) && workqueue_freezing) { | |
5797b1c1 TH |
5329 | new_max = 0; |
5330 | new_min = 0; | |
5331 | } else { | |
5332 | new_max = wq->saved_max_active; | |
5333 | new_min = wq->saved_min_active; | |
a045a272 TH |
5334 | } |
5335 | ||
5797b1c1 | 5336 | if (wq->max_active == new_max && wq->min_active == new_min) |
a045a272 TH |
5337 | return; |
5338 | ||
5339 | /* | |
5797b1c1 | 5340 | * Update @wq->max/min_active and then kick inactive work items if more |
a045a272 TH |
5341 | * active work items are allowed. This doesn't break work item ordering |
5342 | * because new work items are always queued behind existing inactive | |
5343 | * work items if there are any. | |
5344 | */ | |
5797b1c1 TH |
5345 | WRITE_ONCE(wq->max_active, new_max); |
5346 | WRITE_ONCE(wq->min_active, new_min); | |
5347 | ||
5348 | if (wq->flags & WQ_UNBOUND) | |
5349 | wq_update_node_max_active(wq, -1); | |
5350 | ||
5351 | if (new_max == 0) | |
5352 | return; | |
a045a272 | 5353 | |
c5404d4e TH |
5354 | /* |
5355 | * Round-robin through pwq's activating the first inactive work item | |
5356 | * until max_active is filled. | |
5357 | */ | |
5358 | do { | |
5359 | struct pool_workqueue *pwq; | |
a045a272 | 5360 | |
c5404d4e TH |
5361 | activated = false; |
5362 | for_each_pwq(pwq, wq) { | |
5363 | unsigned long flags; | |
a045a272 | 5364 | |
c5404d4e TH |
5365 | /* can be called during early boot w/ irq disabled */ |
5366 | raw_spin_lock_irqsave(&pwq->pool->lock, flags); | |
5797b1c1 | 5367 | if (pwq_activate_first_inactive(pwq, true)) { |
c5404d4e TH |
5368 | activated = true; |
5369 | kick_pool(pwq->pool); | |
5370 | } | |
5371 | raw_spin_unlock_irqrestore(&pwq->pool->lock, flags); | |
5372 | } | |
5373 | } while (activated); | |
a045a272 TH |
5374 | } |
5375 | ||
a2775bbc | 5376 | __printf(1, 4) |
669de8bd BVA |
5377 | struct workqueue_struct *alloc_workqueue(const char *fmt, |
5378 | unsigned int flags, | |
5379 | int max_active, ...) | |
1da177e4 | 5380 | { |
ecf6881f | 5381 | va_list args; |
1da177e4 | 5382 | struct workqueue_struct *wq; |
91ccc6e7 TH |
5383 | size_t wq_size; |
5384 | int name_len; | |
b196be89 | 5385 | |
4cb1ef64 TH |
5386 | if (flags & WQ_BH) { |
5387 | if (WARN_ON_ONCE(flags & ~__WQ_BH_ALLOWS)) | |
5388 | return NULL; | |
5389 | if (WARN_ON_ONCE(max_active)) | |
5390 | return NULL; | |
5391 | } | |
5392 | ||
cee22a15 VK |
5393 | /* see the comment above the definition of WQ_POWER_EFFICIENT */ |
5394 | if ((flags & WQ_POWER_EFFICIENT) && wq_power_efficient) | |
5395 | flags |= WQ_UNBOUND; | |
5396 | ||
ecf6881f | 5397 | /* allocate wq and format name */ |
91ccc6e7 TH |
5398 | if (flags & WQ_UNBOUND) |
5399 | wq_size = struct_size(wq, node_nr_active, nr_node_ids + 1); | |
5400 | else | |
5401 | wq_size = sizeof(*wq); | |
5402 | ||
5403 | wq = kzalloc(wq_size, GFP_KERNEL); | |
b196be89 | 5404 | if (!wq) |
d2c1d404 | 5405 | return NULL; |
b196be89 | 5406 | |
6029a918 | 5407 | if (flags & WQ_UNBOUND) { |
be69d00d | 5408 | wq->unbound_attrs = alloc_workqueue_attrs(); |
6029a918 TH |
5409 | if (!wq->unbound_attrs) |
5410 | goto err_free_wq; | |
5411 | } | |
5412 | ||
669de8bd | 5413 | va_start(args, max_active); |
91ccc6e7 | 5414 | name_len = vsnprintf(wq->name, sizeof(wq->name), fmt, args); |
b196be89 | 5415 | va_end(args); |
1da177e4 | 5416 | |
91ccc6e7 TH |
5417 | if (name_len >= WQ_NAME_LEN) |
5418 | pr_warn_once("workqueue: name exceeds WQ_NAME_LEN. Truncating to: %s\n", | |
5419 | wq->name); | |
31c89007 | 5420 | |
4cb1ef64 TH |
5421 | if (flags & WQ_BH) { |
5422 | /* | |
5423 | * BH workqueues always share a single execution context per CPU | |
5424 | * and don't impose any max_active limit. | |
5425 | */ | |
5426 | max_active = INT_MAX; | |
5427 | } else { | |
5428 | max_active = max_active ?: WQ_DFL_ACTIVE; | |
5429 | max_active = wq_clamp_max_active(max_active, flags, wq->name); | |
5430 | } | |
3af24433 | 5431 | |
b196be89 | 5432 | /* init wq */ |
97e37d7b | 5433 | wq->flags = flags; |
a045a272 | 5434 | wq->max_active = max_active; |
5797b1c1 TH |
5435 | wq->min_active = min(max_active, WQ_DFL_MIN_ACTIVE); |
5436 | wq->saved_max_active = wq->max_active; | |
5437 | wq->saved_min_active = wq->min_active; | |
3c25a55d | 5438 | mutex_init(&wq->mutex); |
112202d9 | 5439 | atomic_set(&wq->nr_pwqs_to_flush, 0); |
30cdf249 | 5440 | INIT_LIST_HEAD(&wq->pwqs); |
73f53c4a TH |
5441 | INIT_LIST_HEAD(&wq->flusher_queue); |
5442 | INIT_LIST_HEAD(&wq->flusher_overflow); | |
493a1724 | 5443 | INIT_LIST_HEAD(&wq->maydays); |
502ca9d8 | 5444 | |
669de8bd | 5445 | wq_init_lockdep(wq); |
cce1a165 | 5446 | INIT_LIST_HEAD(&wq->list); |
3af24433 | 5447 | |
91ccc6e7 TH |
5448 | if (flags & WQ_UNBOUND) { |
5449 | if (alloc_node_nr_active(wq->node_nr_active) < 0) | |
5450 | goto err_unreg_lockdep; | |
5451 | } | |
5452 | ||
30cdf249 | 5453 | if (alloc_and_link_pwqs(wq) < 0) |
91ccc6e7 | 5454 | goto err_free_node_nr_active; |
1537663f | 5455 | |
40c17f75 | 5456 | if (wq_online && init_rescuer(wq) < 0) |
983c7515 | 5457 | goto err_destroy; |
3af24433 | 5458 | |
226223ab TH |
5459 | if ((wq->flags & WQ_SYSFS) && workqueue_sysfs_register(wq)) |
5460 | goto err_destroy; | |
5461 | ||
a0a1a5fd | 5462 | /* |
68e13a67 LJ |
5463 | * wq_pool_mutex protects global freeze state and workqueues list. |
5464 | * Grab it, adjust max_active and add the new @wq to workqueues | |
5465 | * list. | |
a0a1a5fd | 5466 | */ |
68e13a67 | 5467 | mutex_lock(&wq_pool_mutex); |
a0a1a5fd | 5468 | |
a357fc03 | 5469 | mutex_lock(&wq->mutex); |
a045a272 | 5470 | wq_adjust_max_active(wq); |
a357fc03 | 5471 | mutex_unlock(&wq->mutex); |
a0a1a5fd | 5472 | |
e2dca7ad | 5473 | list_add_tail_rcu(&wq->list, &workqueues); |
a0a1a5fd | 5474 | |
68e13a67 | 5475 | mutex_unlock(&wq_pool_mutex); |
1537663f | 5476 | |
3af24433 | 5477 | return wq; |
d2c1d404 | 5478 | |
91ccc6e7 TH |
5479 | err_free_node_nr_active: |
5480 | if (wq->flags & WQ_UNBOUND) | |
5481 | free_node_nr_active(wq->node_nr_active); | |
82efcab3 | 5482 | err_unreg_lockdep: |
009bb421 BVA |
5483 | wq_unregister_lockdep(wq); |
5484 | wq_free_lockdep(wq); | |
82efcab3 | 5485 | err_free_wq: |
6029a918 | 5486 | free_workqueue_attrs(wq->unbound_attrs); |
d2c1d404 TH |
5487 | kfree(wq); |
5488 | return NULL; | |
5489 | err_destroy: | |
5490 | destroy_workqueue(wq); | |
4690c4ab | 5491 | return NULL; |
3af24433 | 5492 | } |
669de8bd | 5493 | EXPORT_SYMBOL_GPL(alloc_workqueue); |
1da177e4 | 5494 | |
c29eb853 TH |
5495 | static bool pwq_busy(struct pool_workqueue *pwq) |
5496 | { | |
5497 | int i; | |
5498 | ||
5499 | for (i = 0; i < WORK_NR_COLORS; i++) | |
5500 | if (pwq->nr_in_flight[i]) | |
5501 | return true; | |
5502 | ||
9f66cff2 | 5503 | if ((pwq != rcu_access_pointer(pwq->wq->dfl_pwq)) && (pwq->refcnt > 1)) |
c29eb853 | 5504 | return true; |
afa87ce8 | 5505 | if (!pwq_is_empty(pwq)) |
c29eb853 TH |
5506 | return true; |
5507 | ||
5508 | return false; | |
5509 | } | |
5510 | ||
3af24433 ON |
5511 | /** |
5512 | * destroy_workqueue - safely terminate a workqueue | |
5513 | * @wq: target workqueue | |
5514 | * | |
5515 | * Safely destroy a workqueue. All work currently pending will be done first. | |
5516 | */ | |
5517 | void destroy_workqueue(struct workqueue_struct *wq) | |
5518 | { | |
49e3cf44 | 5519 | struct pool_workqueue *pwq; |
636b927e | 5520 | int cpu; |
3af24433 | 5521 | |
def98c84 TH |
5522 | /* |
5523 | * Remove it from sysfs first so that sanity check failure doesn't | |
5524 | * lead to sysfs name conflicts. | |
5525 | */ | |
5526 | workqueue_sysfs_unregister(wq); | |
5527 | ||
33e3f0a3 RC |
5528 | /* mark the workqueue destruction is in progress */ |
5529 | mutex_lock(&wq->mutex); | |
5530 | wq->flags |= __WQ_DESTROYING; | |
5531 | mutex_unlock(&wq->mutex); | |
5532 | ||
9c5a2ba7 TH |
5533 | /* drain it before proceeding with destruction */ |
5534 | drain_workqueue(wq); | |
c8efcc25 | 5535 | |
def98c84 TH |
5536 | /* kill rescuer, if sanity checks fail, leave it w/o rescuer */ |
5537 | if (wq->rescuer) { | |
5538 | struct worker *rescuer = wq->rescuer; | |
5539 | ||
5540 | /* this prevents new queueing */ | |
a9b8a985 | 5541 | raw_spin_lock_irq(&wq_mayday_lock); |
def98c84 | 5542 | wq->rescuer = NULL; |
a9b8a985 | 5543 | raw_spin_unlock_irq(&wq_mayday_lock); |
def98c84 TH |
5544 | |
5545 | /* rescuer will empty maydays list before exiting */ | |
5546 | kthread_stop(rescuer->task); | |
8efe1223 | 5547 | kfree(rescuer); |
def98c84 TH |
5548 | } |
5549 | ||
c29eb853 TH |
5550 | /* |
5551 | * Sanity checks - grab all the locks so that we wait for all | |
5552 | * in-flight operations which may do put_pwq(). | |
5553 | */ | |
5554 | mutex_lock(&wq_pool_mutex); | |
b09f4fd3 | 5555 | mutex_lock(&wq->mutex); |
49e3cf44 | 5556 | for_each_pwq(pwq, wq) { |
a9b8a985 | 5557 | raw_spin_lock_irq(&pwq->pool->lock); |
c29eb853 | 5558 | if (WARN_ON(pwq_busy(pwq))) { |
1d9a6159 KW |
5559 | pr_warn("%s: %s has the following busy pwq\n", |
5560 | __func__, wq->name); | |
c29eb853 | 5561 | show_pwq(pwq); |
a9b8a985 | 5562 | raw_spin_unlock_irq(&pwq->pool->lock); |
b09f4fd3 | 5563 | mutex_unlock(&wq->mutex); |
c29eb853 | 5564 | mutex_unlock(&wq_pool_mutex); |
55df0933 | 5565 | show_one_workqueue(wq); |
6183c009 | 5566 | return; |
76af4d93 | 5567 | } |
a9b8a985 | 5568 | raw_spin_unlock_irq(&pwq->pool->lock); |
6183c009 | 5569 | } |
b09f4fd3 | 5570 | mutex_unlock(&wq->mutex); |
6183c009 | 5571 | |
a0a1a5fd TH |
5572 | /* |
5573 | * wq list is used to freeze wq, remove from list after | |
5574 | * flushing is complete in case freeze races us. | |
5575 | */ | |
e2dca7ad | 5576 | list_del_rcu(&wq->list); |
68e13a67 | 5577 | mutex_unlock(&wq_pool_mutex); |
3af24433 | 5578 | |
636b927e TH |
5579 | /* |
5580 | * We're the sole accessor of @wq. Directly access cpu_pwq and dfl_pwq | |
5581 | * to put the base refs. @wq will be auto-destroyed from the last | |
5582 | * pwq_put. RCU read lock prevents @wq from going away from under us. | |
5583 | */ | |
5584 | rcu_read_lock(); | |
4c16bd32 | 5585 | |
636b927e | 5586 | for_each_possible_cpu(cpu) { |
9f66cff2 TH |
5587 | put_pwq_unlocked(unbound_pwq(wq, cpu)); |
5588 | RCU_INIT_POINTER(*unbound_pwq_slot(wq, cpu), NULL); | |
29c91e99 | 5589 | } |
636b927e | 5590 | |
9f66cff2 TH |
5591 | put_pwq_unlocked(unbound_pwq(wq, -1)); |
5592 | RCU_INIT_POINTER(*unbound_pwq_slot(wq, -1), NULL); | |
636b927e TH |
5593 | |
5594 | rcu_read_unlock(); | |
3af24433 ON |
5595 | } |
5596 | EXPORT_SYMBOL_GPL(destroy_workqueue); | |
5597 | ||
dcd989cb TH |
5598 | /** |
5599 | * workqueue_set_max_active - adjust max_active of a workqueue | |
5600 | * @wq: target workqueue | |
5601 | * @max_active: new max_active value. | |
5602 | * | |
5797b1c1 TH |
5603 | * Set max_active of @wq to @max_active. See the alloc_workqueue() function |
5604 | * comment. | |
dcd989cb TH |
5605 | * |
5606 | * CONTEXT: | |
5607 | * Don't call from IRQ context. | |
5608 | */ | |
5609 | void workqueue_set_max_active(struct workqueue_struct *wq, int max_active) | |
5610 | { | |
4cb1ef64 TH |
5611 | /* max_active doesn't mean anything for BH workqueues */ |
5612 | if (WARN_ON(wq->flags & WQ_BH)) | |
5613 | return; | |
8719dcea | 5614 | /* disallow meddling with max_active for ordered workqueues */ |
3bc1e711 | 5615 | if (WARN_ON(wq->flags & __WQ_ORDERED)) |
8719dcea TH |
5616 | return; |
5617 | ||
f3421797 | 5618 | max_active = wq_clamp_max_active(max_active, wq->flags, wq->name); |
dcd989cb | 5619 | |
a357fc03 | 5620 | mutex_lock(&wq->mutex); |
dcd989cb TH |
5621 | |
5622 | wq->saved_max_active = max_active; | |
5797b1c1 TH |
5623 | if (wq->flags & WQ_UNBOUND) |
5624 | wq->saved_min_active = min(wq->saved_min_active, max_active); | |
5625 | ||
a045a272 | 5626 | wq_adjust_max_active(wq); |
93981800 | 5627 | |
a357fc03 | 5628 | mutex_unlock(&wq->mutex); |
15316ba8 | 5629 | } |
dcd989cb | 5630 | EXPORT_SYMBOL_GPL(workqueue_set_max_active); |
15316ba8 | 5631 | |
8f172181 TH |
5632 | /** |
5633 | * workqueue_set_min_active - adjust min_active of an unbound workqueue | |
5634 | * @wq: target unbound workqueue | |
5635 | * @min_active: new min_active value | |
5636 | * | |
5637 | * Set min_active of an unbound workqueue. Unlike other types of workqueues, an | |
5638 | * unbound workqueue is not guaranteed to be able to process max_active | |
5639 | * interdependent work items. Instead, an unbound workqueue is guaranteed to be | |
5640 | * able to process min_active number of interdependent work items which is | |
5641 | * %WQ_DFL_MIN_ACTIVE by default. | |
5642 | * | |
5643 | * Use this function to adjust the min_active value between 0 and the current | |
5644 | * max_active. | |
5645 | */ | |
5646 | void workqueue_set_min_active(struct workqueue_struct *wq, int min_active) | |
5647 | { | |
5648 | /* min_active is only meaningful for non-ordered unbound workqueues */ | |
5649 | if (WARN_ON((wq->flags & (WQ_BH | WQ_UNBOUND | __WQ_ORDERED)) != | |
5650 | WQ_UNBOUND)) | |
5651 | return; | |
5652 | ||
5653 | mutex_lock(&wq->mutex); | |
5654 | wq->saved_min_active = clamp(min_active, 0, wq->saved_max_active); | |
5655 | wq_adjust_max_active(wq); | |
5656 | mutex_unlock(&wq->mutex); | |
5657 | } | |
5658 | ||
27d4ee03 LW |
5659 | /** |
5660 | * current_work - retrieve %current task's work struct | |
5661 | * | |
5662 | * Determine if %current task is a workqueue worker and what it's working on. | |
5663 | * Useful to find out the context that the %current task is running in. | |
5664 | * | |
5665 | * Return: work struct if %current task is a workqueue worker, %NULL otherwise. | |
5666 | */ | |
5667 | struct work_struct *current_work(void) | |
5668 | { | |
5669 | struct worker *worker = current_wq_worker(); | |
5670 | ||
5671 | return worker ? worker->current_work : NULL; | |
5672 | } | |
5673 | EXPORT_SYMBOL(current_work); | |
5674 | ||
e6267616 TH |
5675 | /** |
5676 | * current_is_workqueue_rescuer - is %current workqueue rescuer? | |
5677 | * | |
5678 | * Determine whether %current is a workqueue rescuer. Can be used from | |
5679 | * work functions to determine whether it's being run off the rescuer task. | |
d185af30 YB |
5680 | * |
5681 | * Return: %true if %current is a workqueue rescuer. %false otherwise. | |
e6267616 TH |
5682 | */ |
5683 | bool current_is_workqueue_rescuer(void) | |
5684 | { | |
5685 | struct worker *worker = current_wq_worker(); | |
5686 | ||
6a092dfd | 5687 | return worker && worker->rescue_wq; |
e6267616 TH |
5688 | } |
5689 | ||
eef6a7d5 | 5690 | /** |
dcd989cb TH |
5691 | * workqueue_congested - test whether a workqueue is congested |
5692 | * @cpu: CPU in question | |
5693 | * @wq: target workqueue | |
eef6a7d5 | 5694 | * |
dcd989cb TH |
5695 | * Test whether @wq's cpu workqueue for @cpu is congested. There is |
5696 | * no synchronization around this function and the test result is | |
5697 | * unreliable and only useful as advisory hints or for debugging. | |
eef6a7d5 | 5698 | * |
d3251859 | 5699 | * If @cpu is WORK_CPU_UNBOUND, the test is performed on the local CPU. |
636b927e TH |
5700 | * |
5701 | * With the exception of ordered workqueues, all workqueues have per-cpu | |
5702 | * pool_workqueues, each with its own congested state. A workqueue being | |
5703 | * congested on one CPU doesn't mean that the workqueue is contested on any | |
5704 | * other CPUs. | |
d3251859 | 5705 | * |
d185af30 | 5706 | * Return: |
dcd989cb | 5707 | * %true if congested, %false otherwise. |
eef6a7d5 | 5708 | */ |
d84ff051 | 5709 | bool workqueue_congested(int cpu, struct workqueue_struct *wq) |
1da177e4 | 5710 | { |
7fb98ea7 | 5711 | struct pool_workqueue *pwq; |
76af4d93 TH |
5712 | bool ret; |
5713 | ||
24acfb71 TG |
5714 | rcu_read_lock(); |
5715 | preempt_disable(); | |
7fb98ea7 | 5716 | |
d3251859 TH |
5717 | if (cpu == WORK_CPU_UNBOUND) |
5718 | cpu = smp_processor_id(); | |
5719 | ||
636b927e | 5720 | pwq = *per_cpu_ptr(wq->cpu_pwq, cpu); |
f97a4a1a | 5721 | ret = !list_empty(&pwq->inactive_works); |
636b927e | 5722 | |
24acfb71 TG |
5723 | preempt_enable(); |
5724 | rcu_read_unlock(); | |
76af4d93 TH |
5725 | |
5726 | return ret; | |
1da177e4 | 5727 | } |
dcd989cb | 5728 | EXPORT_SYMBOL_GPL(workqueue_congested); |
1da177e4 | 5729 | |
dcd989cb TH |
5730 | /** |
5731 | * work_busy - test whether a work is currently pending or running | |
5732 | * @work: the work to be tested | |
5733 | * | |
5734 | * Test whether @work is currently pending or running. There is no | |
5735 | * synchronization around this function and the test result is | |
5736 | * unreliable and only useful as advisory hints or for debugging. | |
dcd989cb | 5737 | * |
d185af30 | 5738 | * Return: |
dcd989cb TH |
5739 | * OR'd bitmask of WORK_BUSY_* bits. |
5740 | */ | |
5741 | unsigned int work_busy(struct work_struct *work) | |
1da177e4 | 5742 | { |
fa1b54e6 | 5743 | struct worker_pool *pool; |
dcd989cb TH |
5744 | unsigned long flags; |
5745 | unsigned int ret = 0; | |
1da177e4 | 5746 | |
dcd989cb TH |
5747 | if (work_pending(work)) |
5748 | ret |= WORK_BUSY_PENDING; | |
1da177e4 | 5749 | |
24acfb71 | 5750 | rcu_read_lock(); |
fa1b54e6 | 5751 | pool = get_work_pool(work); |
038366c5 | 5752 | if (pool) { |
a9b8a985 | 5753 | raw_spin_lock_irqsave(&pool->lock, flags); |
038366c5 LJ |
5754 | if (find_worker_executing_work(pool, work)) |
5755 | ret |= WORK_BUSY_RUNNING; | |
a9b8a985 | 5756 | raw_spin_unlock_irqrestore(&pool->lock, flags); |
038366c5 | 5757 | } |
24acfb71 | 5758 | rcu_read_unlock(); |
1da177e4 | 5759 | |
dcd989cb | 5760 | return ret; |
1da177e4 | 5761 | } |
dcd989cb | 5762 | EXPORT_SYMBOL_GPL(work_busy); |
1da177e4 | 5763 | |
3d1cb205 TH |
5764 | /** |
5765 | * set_worker_desc - set description for the current work item | |
5766 | * @fmt: printf-style format string | |
5767 | * @...: arguments for the format string | |
5768 | * | |
5769 | * This function can be called by a running work function to describe what | |
5770 | * the work item is about. If the worker task gets dumped, this | |
5771 | * information will be printed out together to help debugging. The | |
5772 | * description can be at most WORKER_DESC_LEN including the trailing '\0'. | |
5773 | */ | |
5774 | void set_worker_desc(const char *fmt, ...) | |
5775 | { | |
5776 | struct worker *worker = current_wq_worker(); | |
5777 | va_list args; | |
5778 | ||
5779 | if (worker) { | |
5780 | va_start(args, fmt); | |
5781 | vsnprintf(worker->desc, sizeof(worker->desc), fmt, args); | |
5782 | va_end(args); | |
3d1cb205 TH |
5783 | } |
5784 | } | |
5c750d58 | 5785 | EXPORT_SYMBOL_GPL(set_worker_desc); |
3d1cb205 TH |
5786 | |
5787 | /** | |
5788 | * print_worker_info - print out worker information and description | |
5789 | * @log_lvl: the log level to use when printing | |
5790 | * @task: target task | |
5791 | * | |
5792 | * If @task is a worker and currently executing a work item, print out the | |
5793 | * name of the workqueue being serviced and worker description set with | |
5794 | * set_worker_desc() by the currently executing work item. | |
5795 | * | |
5796 | * This function can be safely called on any task as long as the | |
5797 | * task_struct itself is accessible. While safe, this function isn't | |
5798 | * synchronized and may print out mixups or garbages of limited length. | |
5799 | */ | |
5800 | void print_worker_info(const char *log_lvl, struct task_struct *task) | |
5801 | { | |
5802 | work_func_t *fn = NULL; | |
5803 | char name[WQ_NAME_LEN] = { }; | |
5804 | char desc[WORKER_DESC_LEN] = { }; | |
5805 | struct pool_workqueue *pwq = NULL; | |
5806 | struct workqueue_struct *wq = NULL; | |
3d1cb205 TH |
5807 | struct worker *worker; |
5808 | ||
5809 | if (!(task->flags & PF_WQ_WORKER)) | |
5810 | return; | |
5811 | ||
5812 | /* | |
5813 | * This function is called without any synchronization and @task | |
5814 | * could be in any state. Be careful with dereferences. | |
5815 | */ | |
e700591a | 5816 | worker = kthread_probe_data(task); |
3d1cb205 TH |
5817 | |
5818 | /* | |
8bf89593 TH |
5819 | * Carefully copy the associated workqueue's workfn, name and desc. |
5820 | * Keep the original last '\0' in case the original is garbage. | |
3d1cb205 | 5821 | */ |
fe557319 CH |
5822 | copy_from_kernel_nofault(&fn, &worker->current_func, sizeof(fn)); |
5823 | copy_from_kernel_nofault(&pwq, &worker->current_pwq, sizeof(pwq)); | |
5824 | copy_from_kernel_nofault(&wq, &pwq->wq, sizeof(wq)); | |
5825 | copy_from_kernel_nofault(name, wq->name, sizeof(name) - 1); | |
5826 | copy_from_kernel_nofault(desc, worker->desc, sizeof(desc) - 1); | |
3d1cb205 TH |
5827 | |
5828 | if (fn || name[0] || desc[0]) { | |
d75f773c | 5829 | printk("%sWorkqueue: %s %ps", log_lvl, name, fn); |
8bf89593 | 5830 | if (strcmp(name, desc)) |
3d1cb205 TH |
5831 | pr_cont(" (%s)", desc); |
5832 | pr_cont("\n"); | |
5833 | } | |
5834 | } | |
5835 | ||
3494fc30 TH |
5836 | static void pr_cont_pool_info(struct worker_pool *pool) |
5837 | { | |
5838 | pr_cont(" cpus=%*pbl", nr_cpumask_bits, pool->attrs->cpumask); | |
5839 | if (pool->node != NUMA_NO_NODE) | |
5840 | pr_cont(" node=%d", pool->node); | |
4cb1ef64 TH |
5841 | pr_cont(" flags=0x%x", pool->flags); |
5842 | if (pool->flags & POOL_BH) | |
5843 | pr_cont(" bh%s", | |
5844 | pool->attrs->nice == HIGHPRI_NICE_LEVEL ? "-hi" : ""); | |
5845 | else | |
5846 | pr_cont(" nice=%d", pool->attrs->nice); | |
5847 | } | |
5848 | ||
5849 | static void pr_cont_worker_id(struct worker *worker) | |
5850 | { | |
5851 | struct worker_pool *pool = worker->pool; | |
5852 | ||
5853 | if (pool->flags & WQ_BH) | |
5854 | pr_cont("bh%s", | |
5855 | pool->attrs->nice == HIGHPRI_NICE_LEVEL ? "-hi" : ""); | |
5856 | else | |
5857 | pr_cont("%d%s", task_pid_nr(worker->task), | |
5858 | worker->rescue_wq ? "(RESCUER)" : ""); | |
3494fc30 TH |
5859 | } |
5860 | ||
c76feb0d PM |
5861 | struct pr_cont_work_struct { |
5862 | bool comma; | |
5863 | work_func_t func; | |
5864 | long ctr; | |
5865 | }; | |
5866 | ||
5867 | static void pr_cont_work_flush(bool comma, work_func_t func, struct pr_cont_work_struct *pcwsp) | |
5868 | { | |
5869 | if (!pcwsp->ctr) | |
5870 | goto out_record; | |
5871 | if (func == pcwsp->func) { | |
5872 | pcwsp->ctr++; | |
5873 | return; | |
5874 | } | |
5875 | if (pcwsp->ctr == 1) | |
5876 | pr_cont("%s %ps", pcwsp->comma ? "," : "", pcwsp->func); | |
5877 | else | |
5878 | pr_cont("%s %ld*%ps", pcwsp->comma ? "," : "", pcwsp->ctr, pcwsp->func); | |
5879 | pcwsp->ctr = 0; | |
5880 | out_record: | |
5881 | if ((long)func == -1L) | |
5882 | return; | |
5883 | pcwsp->comma = comma; | |
5884 | pcwsp->func = func; | |
5885 | pcwsp->ctr = 1; | |
5886 | } | |
5887 | ||
5888 | static void pr_cont_work(bool comma, struct work_struct *work, struct pr_cont_work_struct *pcwsp) | |
3494fc30 TH |
5889 | { |
5890 | if (work->func == wq_barrier_func) { | |
5891 | struct wq_barrier *barr; | |
5892 | ||
5893 | barr = container_of(work, struct wq_barrier, work); | |
5894 | ||
c76feb0d | 5895 | pr_cont_work_flush(comma, (work_func_t)-1, pcwsp); |
3494fc30 TH |
5896 | pr_cont("%s BAR(%d)", comma ? "," : "", |
5897 | task_pid_nr(barr->task)); | |
5898 | } else { | |
c76feb0d PM |
5899 | if (!comma) |
5900 | pr_cont_work_flush(comma, (work_func_t)-1, pcwsp); | |
5901 | pr_cont_work_flush(comma, work->func, pcwsp); | |
3494fc30 TH |
5902 | } |
5903 | } | |
5904 | ||
5905 | static void show_pwq(struct pool_workqueue *pwq) | |
5906 | { | |
c76feb0d | 5907 | struct pr_cont_work_struct pcws = { .ctr = 0, }; |
3494fc30 TH |
5908 | struct worker_pool *pool = pwq->pool; |
5909 | struct work_struct *work; | |
5910 | struct worker *worker; | |
5911 | bool has_in_flight = false, has_pending = false; | |
5912 | int bkt; | |
5913 | ||
5914 | pr_info(" pwq %d:", pool->id); | |
5915 | pr_cont_pool_info(pool); | |
5916 | ||
a045a272 TH |
5917 | pr_cont(" active=%d refcnt=%d%s\n", |
5918 | pwq->nr_active, pwq->refcnt, | |
3494fc30 TH |
5919 | !list_empty(&pwq->mayday_node) ? " MAYDAY" : ""); |
5920 | ||
5921 | hash_for_each(pool->busy_hash, bkt, worker, hentry) { | |
5922 | if (worker->current_pwq == pwq) { | |
5923 | has_in_flight = true; | |
5924 | break; | |
5925 | } | |
5926 | } | |
5927 | if (has_in_flight) { | |
5928 | bool comma = false; | |
5929 | ||
5930 | pr_info(" in-flight:"); | |
5931 | hash_for_each(pool->busy_hash, bkt, worker, hentry) { | |
5932 | if (worker->current_pwq != pwq) | |
5933 | continue; | |
5934 | ||
4cb1ef64 TH |
5935 | pr_cont(" %s", comma ? "," : ""); |
5936 | pr_cont_worker_id(worker); | |
5937 | pr_cont(":%ps", worker->current_func); | |
3494fc30 | 5938 | list_for_each_entry(work, &worker->scheduled, entry) |
c76feb0d PM |
5939 | pr_cont_work(false, work, &pcws); |
5940 | pr_cont_work_flush(comma, (work_func_t)-1L, &pcws); | |
3494fc30 TH |
5941 | comma = true; |
5942 | } | |
5943 | pr_cont("\n"); | |
5944 | } | |
5945 | ||
5946 | list_for_each_entry(work, &pool->worklist, entry) { | |
5947 | if (get_work_pwq(work) == pwq) { | |
5948 | has_pending = true; | |
5949 | break; | |
5950 | } | |
5951 | } | |
5952 | if (has_pending) { | |
5953 | bool comma = false; | |
5954 | ||
5955 | pr_info(" pending:"); | |
5956 | list_for_each_entry(work, &pool->worklist, entry) { | |
5957 | if (get_work_pwq(work) != pwq) | |
5958 | continue; | |
5959 | ||
c76feb0d | 5960 | pr_cont_work(comma, work, &pcws); |
3494fc30 TH |
5961 | comma = !(*work_data_bits(work) & WORK_STRUCT_LINKED); |
5962 | } | |
c76feb0d | 5963 | pr_cont_work_flush(comma, (work_func_t)-1L, &pcws); |
3494fc30 TH |
5964 | pr_cont("\n"); |
5965 | } | |
5966 | ||
f97a4a1a | 5967 | if (!list_empty(&pwq->inactive_works)) { |
3494fc30 TH |
5968 | bool comma = false; |
5969 | ||
f97a4a1a LJ |
5970 | pr_info(" inactive:"); |
5971 | list_for_each_entry(work, &pwq->inactive_works, entry) { | |
c76feb0d | 5972 | pr_cont_work(comma, work, &pcws); |
3494fc30 TH |
5973 | comma = !(*work_data_bits(work) & WORK_STRUCT_LINKED); |
5974 | } | |
c76feb0d | 5975 | pr_cont_work_flush(comma, (work_func_t)-1L, &pcws); |
3494fc30 TH |
5976 | pr_cont("\n"); |
5977 | } | |
5978 | } | |
5979 | ||
5980 | /** | |
55df0933 IK |
5981 | * show_one_workqueue - dump state of specified workqueue |
5982 | * @wq: workqueue whose state will be printed | |
3494fc30 | 5983 | */ |
55df0933 | 5984 | void show_one_workqueue(struct workqueue_struct *wq) |
3494fc30 | 5985 | { |
55df0933 IK |
5986 | struct pool_workqueue *pwq; |
5987 | bool idle = true; | |
3494fc30 | 5988 | unsigned long flags; |
3494fc30 | 5989 | |
55df0933 | 5990 | for_each_pwq(pwq, wq) { |
afa87ce8 | 5991 | if (!pwq_is_empty(pwq)) { |
55df0933 IK |
5992 | idle = false; |
5993 | break; | |
3494fc30 | 5994 | } |
55df0933 IK |
5995 | } |
5996 | if (idle) /* Nothing to print for idle workqueue */ | |
5997 | return; | |
3494fc30 | 5998 | |
55df0933 | 5999 | pr_info("workqueue %s: flags=0x%x\n", wq->name, wq->flags); |
3494fc30 | 6000 | |
55df0933 IK |
6001 | for_each_pwq(pwq, wq) { |
6002 | raw_spin_lock_irqsave(&pwq->pool->lock, flags); | |
afa87ce8 | 6003 | if (!pwq_is_empty(pwq)) { |
62635ea8 | 6004 | /* |
55df0933 IK |
6005 | * Defer printing to avoid deadlocks in console |
6006 | * drivers that queue work while holding locks | |
6007 | * also taken in their write paths. | |
62635ea8 | 6008 | */ |
55df0933 IK |
6009 | printk_deferred_enter(); |
6010 | show_pwq(pwq); | |
6011 | printk_deferred_exit(); | |
3494fc30 | 6012 | } |
55df0933 | 6013 | raw_spin_unlock_irqrestore(&pwq->pool->lock, flags); |
62635ea8 SS |
6014 | /* |
6015 | * We could be printing a lot from atomic context, e.g. | |
55df0933 | 6016 | * sysrq-t -> show_all_workqueues(). Avoid triggering |
62635ea8 SS |
6017 | * hard lockup. |
6018 | */ | |
6019 | touch_nmi_watchdog(); | |
3494fc30 TH |
6020 | } |
6021 | ||
55df0933 IK |
6022 | } |
6023 | ||
6024 | /** | |
6025 | * show_one_worker_pool - dump state of specified worker pool | |
6026 | * @pool: worker pool whose state will be printed | |
6027 | */ | |
6028 | static void show_one_worker_pool(struct worker_pool *pool) | |
6029 | { | |
6030 | struct worker *worker; | |
6031 | bool first = true; | |
6032 | unsigned long flags; | |
335a42eb | 6033 | unsigned long hung = 0; |
55df0933 IK |
6034 | |
6035 | raw_spin_lock_irqsave(&pool->lock, flags); | |
6036 | if (pool->nr_workers == pool->nr_idle) | |
6037 | goto next_pool; | |
335a42eb PM |
6038 | |
6039 | /* How long the first pending work is waiting for a worker. */ | |
6040 | if (!list_empty(&pool->worklist)) | |
6041 | hung = jiffies_to_msecs(jiffies - pool->watchdog_ts) / 1000; | |
6042 | ||
55df0933 IK |
6043 | /* |
6044 | * Defer printing to avoid deadlocks in console drivers that | |
6045 | * queue work while holding locks also taken in their write | |
6046 | * paths. | |
6047 | */ | |
6048 | printk_deferred_enter(); | |
6049 | pr_info("pool %d:", pool->id); | |
6050 | pr_cont_pool_info(pool); | |
335a42eb | 6051 | pr_cont(" hung=%lus workers=%d", hung, pool->nr_workers); |
55df0933 IK |
6052 | if (pool->manager) |
6053 | pr_cont(" manager: %d", | |
6054 | task_pid_nr(pool->manager->task)); | |
6055 | list_for_each_entry(worker, &pool->idle_list, entry) { | |
4cb1ef64 TH |
6056 | pr_cont(" %s", first ? "idle: " : ""); |
6057 | pr_cont_worker_id(worker); | |
55df0933 IK |
6058 | first = false; |
6059 | } | |
6060 | pr_cont("\n"); | |
6061 | printk_deferred_exit(); | |
6062 | next_pool: | |
6063 | raw_spin_unlock_irqrestore(&pool->lock, flags); | |
6064 | /* | |
6065 | * We could be printing a lot from atomic context, e.g. | |
6066 | * sysrq-t -> show_all_workqueues(). Avoid triggering | |
6067 | * hard lockup. | |
6068 | */ | |
6069 | touch_nmi_watchdog(); | |
6070 | ||
6071 | } | |
6072 | ||
6073 | /** | |
6074 | * show_all_workqueues - dump workqueue state | |
6075 | * | |
704bc669 | 6076 | * Called from a sysrq handler and prints out all busy workqueues and pools. |
55df0933 IK |
6077 | */ |
6078 | void show_all_workqueues(void) | |
6079 | { | |
6080 | struct workqueue_struct *wq; | |
6081 | struct worker_pool *pool; | |
6082 | int pi; | |
6083 | ||
6084 | rcu_read_lock(); | |
6085 | ||
6086 | pr_info("Showing busy workqueues and worker pools:\n"); | |
6087 | ||
6088 | list_for_each_entry_rcu(wq, &workqueues, list) | |
6089 | show_one_workqueue(wq); | |
6090 | ||
6091 | for_each_pool(pool, pi) | |
6092 | show_one_worker_pool(pool); | |
6093 | ||
24acfb71 | 6094 | rcu_read_unlock(); |
3494fc30 TH |
6095 | } |
6096 | ||
704bc669 JL |
6097 | /** |
6098 | * show_freezable_workqueues - dump freezable workqueue state | |
6099 | * | |
6100 | * Called from try_to_freeze_tasks() and prints out all freezable workqueues | |
6101 | * still busy. | |
6102 | */ | |
6103 | void show_freezable_workqueues(void) | |
6104 | { | |
6105 | struct workqueue_struct *wq; | |
6106 | ||
6107 | rcu_read_lock(); | |
6108 | ||
6109 | pr_info("Showing freezable workqueues that are still busy:\n"); | |
6110 | ||
6111 | list_for_each_entry_rcu(wq, &workqueues, list) { | |
6112 | if (!(wq->flags & WQ_FREEZABLE)) | |
6113 | continue; | |
6114 | show_one_workqueue(wq); | |
6115 | } | |
6116 | ||
6117 | rcu_read_unlock(); | |
6118 | } | |
6119 | ||
6b59808b TH |
6120 | /* used to show worker information through /proc/PID/{comm,stat,status} */ |
6121 | void wq_worker_comm(char *buf, size_t size, struct task_struct *task) | |
6122 | { | |
6b59808b TH |
6123 | int off; |
6124 | ||
6125 | /* always show the actual comm */ | |
6126 | off = strscpy(buf, task->comm, size); | |
6127 | if (off < 0) | |
6128 | return; | |
6129 | ||
197f6acc | 6130 | /* stabilize PF_WQ_WORKER and worker pool association */ |
6b59808b TH |
6131 | mutex_lock(&wq_pool_attach_mutex); |
6132 | ||
197f6acc TH |
6133 | if (task->flags & PF_WQ_WORKER) { |
6134 | struct worker *worker = kthread_data(task); | |
6135 | struct worker_pool *pool = worker->pool; | |
6b59808b | 6136 | |
197f6acc | 6137 | if (pool) { |
a9b8a985 | 6138 | raw_spin_lock_irq(&pool->lock); |
197f6acc TH |
6139 | /* |
6140 | * ->desc tracks information (wq name or | |
6141 | * set_worker_desc()) for the latest execution. If | |
6142 | * current, prepend '+', otherwise '-'. | |
6143 | */ | |
6144 | if (worker->desc[0] != '\0') { | |
6145 | if (worker->current_work) | |
6146 | scnprintf(buf + off, size - off, "+%s", | |
6147 | worker->desc); | |
6148 | else | |
6149 | scnprintf(buf + off, size - off, "-%s", | |
6150 | worker->desc); | |
6151 | } | |
a9b8a985 | 6152 | raw_spin_unlock_irq(&pool->lock); |
6b59808b | 6153 | } |
6b59808b TH |
6154 | } |
6155 | ||
6156 | mutex_unlock(&wq_pool_attach_mutex); | |
6157 | } | |
6158 | ||
66448bc2 MM |
6159 | #ifdef CONFIG_SMP |
6160 | ||
db7bccf4 TH |
6161 | /* |
6162 | * CPU hotplug. | |
6163 | * | |
e22bee78 | 6164 | * There are two challenges in supporting CPU hotplug. Firstly, there |
112202d9 | 6165 | * are a lot of assumptions on strong associations among work, pwq and |
706026c2 | 6166 | * pool which make migrating pending and scheduled works very |
e22bee78 | 6167 | * difficult to implement without impacting hot paths. Secondly, |
94cf58bb | 6168 | * worker pools serve mix of short, long and very long running works making |
e22bee78 TH |
6169 | * blocked draining impractical. |
6170 | * | |
24647570 | 6171 | * This is solved by allowing the pools to be disassociated from the CPU |
628c78e7 TH |
6172 | * running as an unbound one and allowing it to be reattached later if the |
6173 | * cpu comes back online. | |
db7bccf4 | 6174 | */ |
1da177e4 | 6175 | |
e8b3f8db | 6176 | static void unbind_workers(int cpu) |
3af24433 | 6177 | { |
4ce62e9e | 6178 | struct worker_pool *pool; |
db7bccf4 | 6179 | struct worker *worker; |
3af24433 | 6180 | |
f02ae73a | 6181 | for_each_cpu_worker_pool(pool, cpu) { |
1258fae7 | 6182 | mutex_lock(&wq_pool_attach_mutex); |
a9b8a985 | 6183 | raw_spin_lock_irq(&pool->lock); |
3af24433 | 6184 | |
94cf58bb | 6185 | /* |
92f9c5c4 | 6186 | * We've blocked all attach/detach operations. Make all workers |
94cf58bb | 6187 | * unbound and set DISASSOCIATED. Before this, all workers |
11b45b0b | 6188 | * must be on the cpu. After this, they may become diasporas. |
b4ac9384 LJ |
6189 | * And the preemption disabled section in their sched callbacks |
6190 | * are guaranteed to see WORKER_UNBOUND since the code here | |
6191 | * is on the same cpu. | |
94cf58bb | 6192 | */ |
da028469 | 6193 | for_each_pool_worker(worker, pool) |
c9e7cf27 | 6194 | worker->flags |= WORKER_UNBOUND; |
06ba38a9 | 6195 | |
24647570 | 6196 | pool->flags |= POOL_DISASSOCIATED; |
f2d5a0ee | 6197 | |
eb283428 | 6198 | /* |
989442d7 LJ |
6199 | * The handling of nr_running in sched callbacks are disabled |
6200 | * now. Zap nr_running. After this, nr_running stays zero and | |
6201 | * need_more_worker() and keep_working() are always true as | |
6202 | * long as the worklist is not empty. This pool now behaves as | |
6203 | * an unbound (in terms of concurrency management) pool which | |
eb283428 LJ |
6204 | * are served by workers tied to the pool. |
6205 | */ | |
bc35f7ef | 6206 | pool->nr_running = 0; |
eb283428 LJ |
6207 | |
6208 | /* | |
6209 | * With concurrency management just turned off, a busy | |
6210 | * worker blocking could lead to lengthy stalls. Kick off | |
6211 | * unbound chain execution of currently pending work items. | |
6212 | */ | |
0219a352 | 6213 | kick_pool(pool); |
989442d7 | 6214 | |
a9b8a985 | 6215 | raw_spin_unlock_irq(&pool->lock); |
989442d7 | 6216 | |
793777bc VS |
6217 | for_each_pool_worker(worker, pool) |
6218 | unbind_worker(worker); | |
989442d7 LJ |
6219 | |
6220 | mutex_unlock(&wq_pool_attach_mutex); | |
eb283428 | 6221 | } |
3af24433 | 6222 | } |
3af24433 | 6223 | |
bd7c089e TH |
6224 | /** |
6225 | * rebind_workers - rebind all workers of a pool to the associated CPU | |
6226 | * @pool: pool of interest | |
6227 | * | |
a9ab775b | 6228 | * @pool->cpu is coming online. Rebind all workers to the CPU. |
bd7c089e TH |
6229 | */ |
6230 | static void rebind_workers(struct worker_pool *pool) | |
6231 | { | |
a9ab775b | 6232 | struct worker *worker; |
bd7c089e | 6233 | |
1258fae7 | 6234 | lockdep_assert_held(&wq_pool_attach_mutex); |
bd7c089e | 6235 | |
a9ab775b TH |
6236 | /* |
6237 | * Restore CPU affinity of all workers. As all idle workers should | |
6238 | * be on the run-queue of the associated CPU before any local | |
402dd89d | 6239 | * wake-ups for concurrency management happen, restore CPU affinity |
a9ab775b TH |
6240 | * of all workers first and then clear UNBOUND. As we're called |
6241 | * from CPU_ONLINE, the following shouldn't fail. | |
6242 | */ | |
c63a2e52 VS |
6243 | for_each_pool_worker(worker, pool) { |
6244 | kthread_set_per_cpu(worker->task, pool->cpu); | |
6245 | WARN_ON_ONCE(set_cpus_allowed_ptr(worker->task, | |
9546b29e | 6246 | pool_allowed_cpus(pool)) < 0); |
c63a2e52 | 6247 | } |
bd7c089e | 6248 | |
a9b8a985 | 6249 | raw_spin_lock_irq(&pool->lock); |
f7c17d26 | 6250 | |
3de5e884 | 6251 | pool->flags &= ~POOL_DISASSOCIATED; |
bd7c089e | 6252 | |
da028469 | 6253 | for_each_pool_worker(worker, pool) { |
a9ab775b | 6254 | unsigned int worker_flags = worker->flags; |
bd7c089e | 6255 | |
a9ab775b TH |
6256 | /* |
6257 | * We want to clear UNBOUND but can't directly call | |
6258 | * worker_clr_flags() or adjust nr_running. Atomically | |
6259 | * replace UNBOUND with another NOT_RUNNING flag REBOUND. | |
6260 | * @worker will clear REBOUND using worker_clr_flags() when | |
6261 | * it initiates the next execution cycle thus restoring | |
6262 | * concurrency management. Note that when or whether | |
6263 | * @worker clears REBOUND doesn't affect correctness. | |
6264 | * | |
c95491ed | 6265 | * WRITE_ONCE() is necessary because @worker->flags may be |
a9ab775b | 6266 | * tested without holding any lock in |
6d25be57 | 6267 | * wq_worker_running(). Without it, NOT_RUNNING test may |
a9ab775b TH |
6268 | * fail incorrectly leading to premature concurrency |
6269 | * management operations. | |
6270 | */ | |
6271 | WARN_ON_ONCE(!(worker_flags & WORKER_UNBOUND)); | |
6272 | worker_flags |= WORKER_REBOUND; | |
6273 | worker_flags &= ~WORKER_UNBOUND; | |
c95491ed | 6274 | WRITE_ONCE(worker->flags, worker_flags); |
bd7c089e | 6275 | } |
a9ab775b | 6276 | |
a9b8a985 | 6277 | raw_spin_unlock_irq(&pool->lock); |
bd7c089e TH |
6278 | } |
6279 | ||
7dbc725e TH |
6280 | /** |
6281 | * restore_unbound_workers_cpumask - restore cpumask of unbound workers | |
6282 | * @pool: unbound pool of interest | |
6283 | * @cpu: the CPU which is coming up | |
6284 | * | |
6285 | * An unbound pool may end up with a cpumask which doesn't have any online | |
6286 | * CPUs. When a worker of such pool get scheduled, the scheduler resets | |
6287 | * its cpus_allowed. If @cpu is in @pool's cpumask which didn't have any | |
6288 | * online CPU before, cpus_allowed of all its workers should be restored. | |
6289 | */ | |
6290 | static void restore_unbound_workers_cpumask(struct worker_pool *pool, int cpu) | |
6291 | { | |
6292 | static cpumask_t cpumask; | |
6293 | struct worker *worker; | |
7dbc725e | 6294 | |
1258fae7 | 6295 | lockdep_assert_held(&wq_pool_attach_mutex); |
7dbc725e TH |
6296 | |
6297 | /* is @cpu allowed for @pool? */ | |
6298 | if (!cpumask_test_cpu(cpu, pool->attrs->cpumask)) | |
6299 | return; | |
6300 | ||
7dbc725e | 6301 | cpumask_and(&cpumask, pool->attrs->cpumask, cpu_online_mask); |
7dbc725e TH |
6302 | |
6303 | /* as we're called from CPU_ONLINE, the following shouldn't fail */ | |
da028469 | 6304 | for_each_pool_worker(worker, pool) |
d945b5e9 | 6305 | WARN_ON_ONCE(set_cpus_allowed_ptr(worker->task, &cpumask) < 0); |
7dbc725e TH |
6306 | } |
6307 | ||
7ee681b2 TG |
6308 | int workqueue_prepare_cpu(unsigned int cpu) |
6309 | { | |
6310 | struct worker_pool *pool; | |
6311 | ||
6312 | for_each_cpu_worker_pool(pool, cpu) { | |
6313 | if (pool->nr_workers) | |
6314 | continue; | |
6315 | if (!create_worker(pool)) | |
6316 | return -ENOMEM; | |
6317 | } | |
6318 | return 0; | |
6319 | } | |
6320 | ||
6321 | int workqueue_online_cpu(unsigned int cpu) | |
3af24433 | 6322 | { |
4ce62e9e | 6323 | struct worker_pool *pool; |
4c16bd32 | 6324 | struct workqueue_struct *wq; |
7dbc725e | 6325 | int pi; |
3ce63377 | 6326 | |
7ee681b2 | 6327 | mutex_lock(&wq_pool_mutex); |
7dbc725e | 6328 | |
7ee681b2 | 6329 | for_each_pool(pool, pi) { |
4cb1ef64 TH |
6330 | /* BH pools aren't affected by hotplug */ |
6331 | if (pool->flags & POOL_BH) | |
6332 | continue; | |
94cf58bb | 6333 | |
4cb1ef64 | 6334 | mutex_lock(&wq_pool_attach_mutex); |
7ee681b2 TG |
6335 | if (pool->cpu == cpu) |
6336 | rebind_workers(pool); | |
6337 | else if (pool->cpu < 0) | |
6338 | restore_unbound_workers_cpumask(pool, cpu); | |
1258fae7 | 6339 | mutex_unlock(&wq_pool_attach_mutex); |
7ee681b2 | 6340 | } |
6ba94429 | 6341 | |
fef59c9c | 6342 | /* update pod affinity of unbound workqueues */ |
4cbfd3de | 6343 | list_for_each_entry(wq, &workqueues, list) { |
84193c07 TH |
6344 | struct workqueue_attrs *attrs = wq->unbound_attrs; |
6345 | ||
6346 | if (attrs) { | |
6347 | const struct wq_pod_type *pt = wqattrs_pod_type(attrs); | |
6348 | int tcpu; | |
4cbfd3de | 6349 | |
84193c07 | 6350 | for_each_cpu(tcpu, pt->pod_cpus[pt->cpu_pod[cpu]]) |
fef59c9c | 6351 | wq_update_pod(wq, tcpu, cpu, true); |
5797b1c1 TH |
6352 | |
6353 | mutex_lock(&wq->mutex); | |
6354 | wq_update_node_max_active(wq, -1); | |
6355 | mutex_unlock(&wq->mutex); | |
4cbfd3de TH |
6356 | } |
6357 | } | |
6ba94429 | 6358 | |
7ee681b2 TG |
6359 | mutex_unlock(&wq_pool_mutex); |
6360 | return 0; | |
6ba94429 FW |
6361 | } |
6362 | ||
7ee681b2 | 6363 | int workqueue_offline_cpu(unsigned int cpu) |
6ba94429 | 6364 | { |
6ba94429 FW |
6365 | struct workqueue_struct *wq; |
6366 | ||
7ee681b2 | 6367 | /* unbinding per-cpu workers should happen on the local CPU */ |
e8b3f8db LJ |
6368 | if (WARN_ON(cpu != smp_processor_id())) |
6369 | return -1; | |
6370 | ||
6371 | unbind_workers(cpu); | |
7ee681b2 | 6372 | |
fef59c9c | 6373 | /* update pod affinity of unbound workqueues */ |
7ee681b2 | 6374 | mutex_lock(&wq_pool_mutex); |
4cbfd3de | 6375 | list_for_each_entry(wq, &workqueues, list) { |
84193c07 TH |
6376 | struct workqueue_attrs *attrs = wq->unbound_attrs; |
6377 | ||
6378 | if (attrs) { | |
6379 | const struct wq_pod_type *pt = wqattrs_pod_type(attrs); | |
6380 | int tcpu; | |
4cbfd3de | 6381 | |
84193c07 | 6382 | for_each_cpu(tcpu, pt->pod_cpus[pt->cpu_pod[cpu]]) |
fef59c9c | 6383 | wq_update_pod(wq, tcpu, cpu, false); |
5797b1c1 TH |
6384 | |
6385 | mutex_lock(&wq->mutex); | |
6386 | wq_update_node_max_active(wq, cpu); | |
6387 | mutex_unlock(&wq->mutex); | |
4cbfd3de TH |
6388 | } |
6389 | } | |
7ee681b2 TG |
6390 | mutex_unlock(&wq_pool_mutex); |
6391 | ||
7ee681b2 | 6392 | return 0; |
6ba94429 FW |
6393 | } |
6394 | ||
6ba94429 FW |
6395 | struct work_for_cpu { |
6396 | struct work_struct work; | |
6397 | long (*fn)(void *); | |
6398 | void *arg; | |
6399 | long ret; | |
6400 | }; | |
6401 | ||
6402 | static void work_for_cpu_fn(struct work_struct *work) | |
6403 | { | |
6404 | struct work_for_cpu *wfc = container_of(work, struct work_for_cpu, work); | |
6405 | ||
6406 | wfc->ret = wfc->fn(wfc->arg); | |
6407 | } | |
6408 | ||
6409 | /** | |
265f3ed0 | 6410 | * work_on_cpu_key - run a function in thread context on a particular cpu |
6ba94429 FW |
6411 | * @cpu: the cpu to run on |
6412 | * @fn: the function to run | |
6413 | * @arg: the function arg | |
265f3ed0 | 6414 | * @key: The lock class key for lock debugging purposes |
6ba94429 FW |
6415 | * |
6416 | * It is up to the caller to ensure that the cpu doesn't go offline. | |
6417 | * The caller must not hold any locks which would prevent @fn from completing. | |
6418 | * | |
6419 | * Return: The value @fn returns. | |
6420 | */ | |
265f3ed0 FW |
6421 | long work_on_cpu_key(int cpu, long (*fn)(void *), |
6422 | void *arg, struct lock_class_key *key) | |
6ba94429 FW |
6423 | { |
6424 | struct work_for_cpu wfc = { .fn = fn, .arg = arg }; | |
6425 | ||
265f3ed0 | 6426 | INIT_WORK_ONSTACK_KEY(&wfc.work, work_for_cpu_fn, key); |
6ba94429 FW |
6427 | schedule_work_on(cpu, &wfc.work); |
6428 | flush_work(&wfc.work); | |
6429 | destroy_work_on_stack(&wfc.work); | |
6430 | return wfc.ret; | |
6431 | } | |
265f3ed0 | 6432 | EXPORT_SYMBOL_GPL(work_on_cpu_key); |
0e8d6a93 TG |
6433 | |
6434 | /** | |
265f3ed0 | 6435 | * work_on_cpu_safe_key - run a function in thread context on a particular cpu |
0e8d6a93 TG |
6436 | * @cpu: the cpu to run on |
6437 | * @fn: the function to run | |
6438 | * @arg: the function argument | |
265f3ed0 | 6439 | * @key: The lock class key for lock debugging purposes |
0e8d6a93 TG |
6440 | * |
6441 | * Disables CPU hotplug and calls work_on_cpu(). The caller must not hold | |
6442 | * any locks which would prevent @fn from completing. | |
6443 | * | |
6444 | * Return: The value @fn returns. | |
6445 | */ | |
265f3ed0 FW |
6446 | long work_on_cpu_safe_key(int cpu, long (*fn)(void *), |
6447 | void *arg, struct lock_class_key *key) | |
0e8d6a93 TG |
6448 | { |
6449 | long ret = -ENODEV; | |
6450 | ||
ffd8bea8 | 6451 | cpus_read_lock(); |
0e8d6a93 | 6452 | if (cpu_online(cpu)) |
265f3ed0 | 6453 | ret = work_on_cpu_key(cpu, fn, arg, key); |
ffd8bea8 | 6454 | cpus_read_unlock(); |
0e8d6a93 TG |
6455 | return ret; |
6456 | } | |
265f3ed0 | 6457 | EXPORT_SYMBOL_GPL(work_on_cpu_safe_key); |
6ba94429 FW |
6458 | #endif /* CONFIG_SMP */ |
6459 | ||
6460 | #ifdef CONFIG_FREEZER | |
6461 | ||
6462 | /** | |
6463 | * freeze_workqueues_begin - begin freezing workqueues | |
6464 | * | |
6465 | * Start freezing workqueues. After this function returns, all freezable | |
f97a4a1a | 6466 | * workqueues will queue new works to their inactive_works list instead of |
6ba94429 FW |
6467 | * pool->worklist. |
6468 | * | |
6469 | * CONTEXT: | |
6470 | * Grabs and releases wq_pool_mutex, wq->mutex and pool->lock's. | |
6471 | */ | |
6472 | void freeze_workqueues_begin(void) | |
6473 | { | |
6474 | struct workqueue_struct *wq; | |
6ba94429 FW |
6475 | |
6476 | mutex_lock(&wq_pool_mutex); | |
6477 | ||
6478 | WARN_ON_ONCE(workqueue_freezing); | |
6479 | workqueue_freezing = true; | |
6480 | ||
6481 | list_for_each_entry(wq, &workqueues, list) { | |
6482 | mutex_lock(&wq->mutex); | |
a045a272 | 6483 | wq_adjust_max_active(wq); |
6ba94429 FW |
6484 | mutex_unlock(&wq->mutex); |
6485 | } | |
6486 | ||
6487 | mutex_unlock(&wq_pool_mutex); | |
6488 | } | |
6489 | ||
6490 | /** | |
6491 | * freeze_workqueues_busy - are freezable workqueues still busy? | |
6492 | * | |
6493 | * Check whether freezing is complete. This function must be called | |
6494 | * between freeze_workqueues_begin() and thaw_workqueues(). | |
6495 | * | |
6496 | * CONTEXT: | |
6497 | * Grabs and releases wq_pool_mutex. | |
6498 | * | |
6499 | * Return: | |
6500 | * %true if some freezable workqueues are still busy. %false if freezing | |
6501 | * is complete. | |
6502 | */ | |
6503 | bool freeze_workqueues_busy(void) | |
6504 | { | |
6505 | bool busy = false; | |
6506 | struct workqueue_struct *wq; | |
6507 | struct pool_workqueue *pwq; | |
6508 | ||
6509 | mutex_lock(&wq_pool_mutex); | |
6510 | ||
6511 | WARN_ON_ONCE(!workqueue_freezing); | |
6512 | ||
6513 | list_for_each_entry(wq, &workqueues, list) { | |
6514 | if (!(wq->flags & WQ_FREEZABLE)) | |
6515 | continue; | |
6516 | /* | |
6517 | * nr_active is monotonically decreasing. It's safe | |
6518 | * to peek without lock. | |
6519 | */ | |
24acfb71 | 6520 | rcu_read_lock(); |
6ba94429 FW |
6521 | for_each_pwq(pwq, wq) { |
6522 | WARN_ON_ONCE(pwq->nr_active < 0); | |
6523 | if (pwq->nr_active) { | |
6524 | busy = true; | |
24acfb71 | 6525 | rcu_read_unlock(); |
6ba94429 FW |
6526 | goto out_unlock; |
6527 | } | |
6528 | } | |
24acfb71 | 6529 | rcu_read_unlock(); |
6ba94429 FW |
6530 | } |
6531 | out_unlock: | |
6532 | mutex_unlock(&wq_pool_mutex); | |
6533 | return busy; | |
6534 | } | |
6535 | ||
6536 | /** | |
6537 | * thaw_workqueues - thaw workqueues | |
6538 | * | |
6539 | * Thaw workqueues. Normal queueing is restored and all collected | |
6540 | * frozen works are transferred to their respective pool worklists. | |
6541 | * | |
6542 | * CONTEXT: | |
6543 | * Grabs and releases wq_pool_mutex, wq->mutex and pool->lock's. | |
6544 | */ | |
6545 | void thaw_workqueues(void) | |
6546 | { | |
6547 | struct workqueue_struct *wq; | |
6ba94429 FW |
6548 | |
6549 | mutex_lock(&wq_pool_mutex); | |
6550 | ||
6551 | if (!workqueue_freezing) | |
6552 | goto out_unlock; | |
6553 | ||
6554 | workqueue_freezing = false; | |
6555 | ||
6556 | /* restore max_active and repopulate worklist */ | |
6557 | list_for_each_entry(wq, &workqueues, list) { | |
6558 | mutex_lock(&wq->mutex); | |
a045a272 | 6559 | wq_adjust_max_active(wq); |
6ba94429 FW |
6560 | mutex_unlock(&wq->mutex); |
6561 | } | |
6562 | ||
6563 | out_unlock: | |
6564 | mutex_unlock(&wq_pool_mutex); | |
6565 | } | |
6566 | #endif /* CONFIG_FREEZER */ | |
6567 | ||
99c621ef | 6568 | static int workqueue_apply_unbound_cpumask(const cpumask_var_t unbound_cpumask) |
042f7df1 LJ |
6569 | { |
6570 | LIST_HEAD(ctxs); | |
6571 | int ret = 0; | |
6572 | struct workqueue_struct *wq; | |
6573 | struct apply_wqattrs_ctx *ctx, *n; | |
6574 | ||
6575 | lockdep_assert_held(&wq_pool_mutex); | |
6576 | ||
6577 | list_for_each_entry(wq, &workqueues, list) { | |
8eb17dc1 | 6578 | if (!(wq->flags & WQ_UNBOUND) || (wq->flags & __WQ_DESTROYING)) |
042f7df1 | 6579 | continue; |
042f7df1 | 6580 | |
99c621ef | 6581 | ctx = apply_wqattrs_prepare(wq, wq->unbound_attrs, unbound_cpumask); |
84193c07 TH |
6582 | if (IS_ERR(ctx)) { |
6583 | ret = PTR_ERR(ctx); | |
042f7df1 LJ |
6584 | break; |
6585 | } | |
6586 | ||
6587 | list_add_tail(&ctx->list, &ctxs); | |
6588 | } | |
6589 | ||
6590 | list_for_each_entry_safe(ctx, n, &ctxs, list) { | |
6591 | if (!ret) | |
6592 | apply_wqattrs_commit(ctx); | |
6593 | apply_wqattrs_cleanup(ctx); | |
6594 | } | |
6595 | ||
99c621ef LJ |
6596 | if (!ret) { |
6597 | mutex_lock(&wq_pool_attach_mutex); | |
6598 | cpumask_copy(wq_unbound_cpumask, unbound_cpumask); | |
6599 | mutex_unlock(&wq_pool_attach_mutex); | |
6600 | } | |
042f7df1 LJ |
6601 | return ret; |
6602 | } | |
6603 | ||
fe28f631 WL |
6604 | /** |
6605 | * workqueue_unbound_exclude_cpumask - Exclude given CPUs from unbound cpumask | |
6606 | * @exclude_cpumask: the cpumask to be excluded from wq_unbound_cpumask | |
6607 | * | |
6608 | * This function can be called from cpuset code to provide a set of isolated | |
6609 | * CPUs that should be excluded from wq_unbound_cpumask. The caller must hold | |
6610 | * either cpus_read_lock or cpus_write_lock. | |
6611 | */ | |
6612 | int workqueue_unbound_exclude_cpumask(cpumask_var_t exclude_cpumask) | |
6613 | { | |
6614 | cpumask_var_t cpumask; | |
6615 | int ret = 0; | |
6616 | ||
6617 | if (!zalloc_cpumask_var(&cpumask, GFP_KERNEL)) | |
6618 | return -ENOMEM; | |
6619 | ||
6620 | lockdep_assert_cpus_held(); | |
6621 | mutex_lock(&wq_pool_mutex); | |
6622 | ||
6623 | /* Save the current isolated cpumask & export it via sysfs */ | |
6624 | cpumask_copy(wq_isolated_cpumask, exclude_cpumask); | |
6625 | ||
6626 | /* | |
6627 | * If the operation fails, it will fall back to | |
6628 | * wq_requested_unbound_cpumask which is initially set to | |
6629 | * (HK_TYPE_WQ ∩ HK_TYPE_DOMAIN) house keeping mask and rewritten | |
6630 | * by any subsequent write to workqueue/cpumask sysfs file. | |
6631 | */ | |
6632 | if (!cpumask_andnot(cpumask, wq_requested_unbound_cpumask, exclude_cpumask)) | |
6633 | cpumask_copy(cpumask, wq_requested_unbound_cpumask); | |
6634 | if (!cpumask_equal(cpumask, wq_unbound_cpumask)) | |
6635 | ret = workqueue_apply_unbound_cpumask(cpumask); | |
6636 | ||
6637 | mutex_unlock(&wq_pool_mutex); | |
6638 | free_cpumask_var(cpumask); | |
6639 | return ret; | |
6640 | } | |
6641 | ||
63c5484e TH |
6642 | static int parse_affn_scope(const char *val) |
6643 | { | |
6644 | int i; | |
6645 | ||
6646 | for (i = 0; i < ARRAY_SIZE(wq_affn_names); i++) { | |
6647 | if (!strncasecmp(val, wq_affn_names[i], strlen(wq_affn_names[i]))) | |
6648 | return i; | |
6649 | } | |
6650 | return -EINVAL; | |
6651 | } | |
6652 | ||
6653 | static int wq_affn_dfl_set(const char *val, const struct kernel_param *kp) | |
6654 | { | |
523a301e TH |
6655 | struct workqueue_struct *wq; |
6656 | int affn, cpu; | |
63c5484e TH |
6657 | |
6658 | affn = parse_affn_scope(val); | |
6659 | if (affn < 0) | |
6660 | return affn; | |
523a301e TH |
6661 | if (affn == WQ_AFFN_DFL) |
6662 | return -EINVAL; | |
6663 | ||
6664 | cpus_read_lock(); | |
6665 | mutex_lock(&wq_pool_mutex); | |
63c5484e TH |
6666 | |
6667 | wq_affn_dfl = affn; | |
523a301e TH |
6668 | |
6669 | list_for_each_entry(wq, &workqueues, list) { | |
6670 | for_each_online_cpu(cpu) { | |
6671 | wq_update_pod(wq, cpu, cpu, true); | |
6672 | } | |
6673 | } | |
6674 | ||
6675 | mutex_unlock(&wq_pool_mutex); | |
6676 | cpus_read_unlock(); | |
6677 | ||
63c5484e TH |
6678 | return 0; |
6679 | } | |
6680 | ||
6681 | static int wq_affn_dfl_get(char *buffer, const struct kernel_param *kp) | |
6682 | { | |
6683 | return scnprintf(buffer, PAGE_SIZE, "%s\n", wq_affn_names[wq_affn_dfl]); | |
6684 | } | |
6685 | ||
6686 | static const struct kernel_param_ops wq_affn_dfl_ops = { | |
6687 | .set = wq_affn_dfl_set, | |
6688 | .get = wq_affn_dfl_get, | |
6689 | }; | |
6690 | ||
6691 | module_param_cb(default_affinity_scope, &wq_affn_dfl_ops, NULL, 0644); | |
6692 | ||
6ba94429 FW |
6693 | #ifdef CONFIG_SYSFS |
6694 | /* | |
6695 | * Workqueues with WQ_SYSFS flag set is visible to userland via | |
6696 | * /sys/bus/workqueue/devices/WQ_NAME. All visible workqueues have the | |
6697 | * following attributes. | |
6698 | * | |
63c5484e TH |
6699 | * per_cpu RO bool : whether the workqueue is per-cpu or unbound |
6700 | * max_active RW int : maximum number of in-flight work items | |
6ba94429 FW |
6701 | * |
6702 | * Unbound workqueues have the following extra attributes. | |
6703 | * | |
63c5484e TH |
6704 | * nice RW int : nice value of the workers |
6705 | * cpumask RW mask : bitmask of allowed CPUs for the workers | |
6706 | * affinity_scope RW str : worker CPU affinity scope (cache, numa, none) | |
8639eceb | 6707 | * affinity_strict RW bool : worker CPU affinity is strict |
6ba94429 FW |
6708 | */ |
6709 | struct wq_device { | |
6710 | struct workqueue_struct *wq; | |
6711 | struct device dev; | |
6712 | }; | |
6713 | ||
6714 | static struct workqueue_struct *dev_to_wq(struct device *dev) | |
6715 | { | |
6716 | struct wq_device *wq_dev = container_of(dev, struct wq_device, dev); | |
6717 | ||
6718 | return wq_dev->wq; | |
6719 | } | |
6720 | ||
6721 | static ssize_t per_cpu_show(struct device *dev, struct device_attribute *attr, | |
6722 | char *buf) | |
6723 | { | |
6724 | struct workqueue_struct *wq = dev_to_wq(dev); | |
6725 | ||
6726 | return scnprintf(buf, PAGE_SIZE, "%d\n", (bool)!(wq->flags & WQ_UNBOUND)); | |
6727 | } | |
6728 | static DEVICE_ATTR_RO(per_cpu); | |
6729 | ||
6730 | static ssize_t max_active_show(struct device *dev, | |
6731 | struct device_attribute *attr, char *buf) | |
6732 | { | |
6733 | struct workqueue_struct *wq = dev_to_wq(dev); | |
6734 | ||
6735 | return scnprintf(buf, PAGE_SIZE, "%d\n", wq->saved_max_active); | |
6736 | } | |
6737 | ||
6738 | static ssize_t max_active_store(struct device *dev, | |
6739 | struct device_attribute *attr, const char *buf, | |
6740 | size_t count) | |
6741 | { | |
6742 | struct workqueue_struct *wq = dev_to_wq(dev); | |
6743 | int val; | |
6744 | ||
6745 | if (sscanf(buf, "%d", &val) != 1 || val <= 0) | |
6746 | return -EINVAL; | |
6747 | ||
6748 | workqueue_set_max_active(wq, val); | |
6749 | return count; | |
6750 | } | |
6751 | static DEVICE_ATTR_RW(max_active); | |
6752 | ||
6753 | static struct attribute *wq_sysfs_attrs[] = { | |
6754 | &dev_attr_per_cpu.attr, | |
6755 | &dev_attr_max_active.attr, | |
6756 | NULL, | |
6757 | }; | |
6758 | ATTRIBUTE_GROUPS(wq_sysfs); | |
6759 | ||
49277a5b WL |
6760 | static void apply_wqattrs_lock(void) |
6761 | { | |
6762 | /* CPUs should stay stable across pwq creations and installations */ | |
6763 | cpus_read_lock(); | |
6764 | mutex_lock(&wq_pool_mutex); | |
6765 | } | |
6766 | ||
6767 | static void apply_wqattrs_unlock(void) | |
6768 | { | |
6769 | mutex_unlock(&wq_pool_mutex); | |
6770 | cpus_read_unlock(); | |
6771 | } | |
6772 | ||
6ba94429 FW |
6773 | static ssize_t wq_nice_show(struct device *dev, struct device_attribute *attr, |
6774 | char *buf) | |
6775 | { | |
6776 | struct workqueue_struct *wq = dev_to_wq(dev); | |
6777 | int written; | |
6778 | ||
6779 | mutex_lock(&wq->mutex); | |
6780 | written = scnprintf(buf, PAGE_SIZE, "%d\n", wq->unbound_attrs->nice); | |
6781 | mutex_unlock(&wq->mutex); | |
6782 | ||
6783 | return written; | |
6784 | } | |
6785 | ||
6786 | /* prepare workqueue_attrs for sysfs store operations */ | |
6787 | static struct workqueue_attrs *wq_sysfs_prep_attrs(struct workqueue_struct *wq) | |
6788 | { | |
6789 | struct workqueue_attrs *attrs; | |
6790 | ||
899a94fe LJ |
6791 | lockdep_assert_held(&wq_pool_mutex); |
6792 | ||
be69d00d | 6793 | attrs = alloc_workqueue_attrs(); |
6ba94429 FW |
6794 | if (!attrs) |
6795 | return NULL; | |
6796 | ||
6ba94429 | 6797 | copy_workqueue_attrs(attrs, wq->unbound_attrs); |
6ba94429 FW |
6798 | return attrs; |
6799 | } | |
6800 | ||
6801 | static ssize_t wq_nice_store(struct device *dev, struct device_attribute *attr, | |
6802 | const char *buf, size_t count) | |
6803 | { | |
6804 | struct workqueue_struct *wq = dev_to_wq(dev); | |
6805 | struct workqueue_attrs *attrs; | |
d4d3e257 LJ |
6806 | int ret = -ENOMEM; |
6807 | ||
6808 | apply_wqattrs_lock(); | |
6ba94429 FW |
6809 | |
6810 | attrs = wq_sysfs_prep_attrs(wq); | |
6811 | if (!attrs) | |
d4d3e257 | 6812 | goto out_unlock; |
6ba94429 FW |
6813 | |
6814 | if (sscanf(buf, "%d", &attrs->nice) == 1 && | |
6815 | attrs->nice >= MIN_NICE && attrs->nice <= MAX_NICE) | |
d4d3e257 | 6816 | ret = apply_workqueue_attrs_locked(wq, attrs); |
6ba94429 FW |
6817 | else |
6818 | ret = -EINVAL; | |
6819 | ||
d4d3e257 LJ |
6820 | out_unlock: |
6821 | apply_wqattrs_unlock(); | |
6ba94429 FW |
6822 | free_workqueue_attrs(attrs); |
6823 | return ret ?: count; | |
6824 | } | |
6825 | ||
6826 | static ssize_t wq_cpumask_show(struct device *dev, | |
6827 | struct device_attribute *attr, char *buf) | |
6828 | { | |
6829 | struct workqueue_struct *wq = dev_to_wq(dev); | |
6830 | int written; | |
6831 | ||
6832 | mutex_lock(&wq->mutex); | |
6833 | written = scnprintf(buf, PAGE_SIZE, "%*pb\n", | |
6834 | cpumask_pr_args(wq->unbound_attrs->cpumask)); | |
6835 | mutex_unlock(&wq->mutex); | |
6836 | return written; | |
6837 | } | |
6838 | ||
6839 | static ssize_t wq_cpumask_store(struct device *dev, | |
6840 | struct device_attribute *attr, | |
6841 | const char *buf, size_t count) | |
6842 | { | |
6843 | struct workqueue_struct *wq = dev_to_wq(dev); | |
6844 | struct workqueue_attrs *attrs; | |
d4d3e257 LJ |
6845 | int ret = -ENOMEM; |
6846 | ||
6847 | apply_wqattrs_lock(); | |
6ba94429 FW |
6848 | |
6849 | attrs = wq_sysfs_prep_attrs(wq); | |
6850 | if (!attrs) | |
d4d3e257 | 6851 | goto out_unlock; |
6ba94429 FW |
6852 | |
6853 | ret = cpumask_parse(buf, attrs->cpumask); | |
6854 | if (!ret) | |
d4d3e257 | 6855 | ret = apply_workqueue_attrs_locked(wq, attrs); |
6ba94429 | 6856 | |
d4d3e257 LJ |
6857 | out_unlock: |
6858 | apply_wqattrs_unlock(); | |
6ba94429 FW |
6859 | free_workqueue_attrs(attrs); |
6860 | return ret ?: count; | |
6861 | } | |
6862 | ||
63c5484e TH |
6863 | static ssize_t wq_affn_scope_show(struct device *dev, |
6864 | struct device_attribute *attr, char *buf) | |
6865 | { | |
6866 | struct workqueue_struct *wq = dev_to_wq(dev); | |
6867 | int written; | |
6868 | ||
6869 | mutex_lock(&wq->mutex); | |
523a301e TH |
6870 | if (wq->unbound_attrs->affn_scope == WQ_AFFN_DFL) |
6871 | written = scnprintf(buf, PAGE_SIZE, "%s (%s)\n", | |
6872 | wq_affn_names[WQ_AFFN_DFL], | |
6873 | wq_affn_names[wq_affn_dfl]); | |
6874 | else | |
6875 | written = scnprintf(buf, PAGE_SIZE, "%s\n", | |
6876 | wq_affn_names[wq->unbound_attrs->affn_scope]); | |
63c5484e TH |
6877 | mutex_unlock(&wq->mutex); |
6878 | ||
6879 | return written; | |
6880 | } | |
6881 | ||
6882 | static ssize_t wq_affn_scope_store(struct device *dev, | |
6883 | struct device_attribute *attr, | |
6884 | const char *buf, size_t count) | |
6885 | { | |
6886 | struct workqueue_struct *wq = dev_to_wq(dev); | |
6887 | struct workqueue_attrs *attrs; | |
6888 | int affn, ret = -ENOMEM; | |
6889 | ||
6890 | affn = parse_affn_scope(buf); | |
6891 | if (affn < 0) | |
6892 | return affn; | |
6893 | ||
6894 | apply_wqattrs_lock(); | |
6895 | attrs = wq_sysfs_prep_attrs(wq); | |
6896 | if (attrs) { | |
6897 | attrs->affn_scope = affn; | |
6898 | ret = apply_workqueue_attrs_locked(wq, attrs); | |
6899 | } | |
6900 | apply_wqattrs_unlock(); | |
6901 | free_workqueue_attrs(attrs); | |
6902 | return ret ?: count; | |
6903 | } | |
6904 | ||
8639eceb TH |
6905 | static ssize_t wq_affinity_strict_show(struct device *dev, |
6906 | struct device_attribute *attr, char *buf) | |
6907 | { | |
6908 | struct workqueue_struct *wq = dev_to_wq(dev); | |
6909 | ||
6910 | return scnprintf(buf, PAGE_SIZE, "%d\n", | |
6911 | wq->unbound_attrs->affn_strict); | |
6912 | } | |
6913 | ||
6914 | static ssize_t wq_affinity_strict_store(struct device *dev, | |
6915 | struct device_attribute *attr, | |
6916 | const char *buf, size_t count) | |
6917 | { | |
6918 | struct workqueue_struct *wq = dev_to_wq(dev); | |
6919 | struct workqueue_attrs *attrs; | |
6920 | int v, ret = -ENOMEM; | |
6921 | ||
6922 | if (sscanf(buf, "%d", &v) != 1) | |
6923 | return -EINVAL; | |
6924 | ||
6925 | apply_wqattrs_lock(); | |
6926 | attrs = wq_sysfs_prep_attrs(wq); | |
6927 | if (attrs) { | |
6928 | attrs->affn_strict = (bool)v; | |
6929 | ret = apply_workqueue_attrs_locked(wq, attrs); | |
6930 | } | |
6931 | apply_wqattrs_unlock(); | |
6932 | free_workqueue_attrs(attrs); | |
6933 | return ret ?: count; | |
6934 | } | |
6935 | ||
6ba94429 | 6936 | static struct device_attribute wq_sysfs_unbound_attrs[] = { |
6ba94429 FW |
6937 | __ATTR(nice, 0644, wq_nice_show, wq_nice_store), |
6938 | __ATTR(cpumask, 0644, wq_cpumask_show, wq_cpumask_store), | |
63c5484e | 6939 | __ATTR(affinity_scope, 0644, wq_affn_scope_show, wq_affn_scope_store), |
8639eceb | 6940 | __ATTR(affinity_strict, 0644, wq_affinity_strict_show, wq_affinity_strict_store), |
6ba94429 FW |
6941 | __ATTR_NULL, |
6942 | }; | |
8ccad40d | 6943 | |
4f19b8e0 | 6944 | static struct bus_type wq_subsys = { |
6ba94429 FW |
6945 | .name = "workqueue", |
6946 | .dev_groups = wq_sysfs_groups, | |
2d3854a3 RR |
6947 | }; |
6948 | ||
49277a5b WL |
6949 | /** |
6950 | * workqueue_set_unbound_cpumask - Set the low-level unbound cpumask | |
6951 | * @cpumask: the cpumask to set | |
6952 | * | |
6953 | * The low-level workqueues cpumask is a global cpumask that limits | |
6954 | * the affinity of all unbound workqueues. This function check the @cpumask | |
6955 | * and apply it to all unbound workqueues and updates all pwqs of them. | |
6956 | * | |
6957 | * Return: 0 - Success | |
6958 | * -EINVAL - Invalid @cpumask | |
6959 | * -ENOMEM - Failed to allocate memory for attrs or pwqs. | |
6960 | */ | |
6961 | static int workqueue_set_unbound_cpumask(cpumask_var_t cpumask) | |
6962 | { | |
6963 | int ret = -EINVAL; | |
6964 | ||
6965 | /* | |
6966 | * Not excluding isolated cpus on purpose. | |
6967 | * If the user wishes to include them, we allow that. | |
6968 | */ | |
6969 | cpumask_and(cpumask, cpumask, cpu_possible_mask); | |
6970 | if (!cpumask_empty(cpumask)) { | |
6971 | apply_wqattrs_lock(); | |
6972 | cpumask_copy(wq_requested_unbound_cpumask, cpumask); | |
6973 | if (cpumask_equal(cpumask, wq_unbound_cpumask)) { | |
6974 | ret = 0; | |
6975 | goto out_unlock; | |
6976 | } | |
6977 | ||
6978 | ret = workqueue_apply_unbound_cpumask(cpumask); | |
6979 | ||
6980 | out_unlock: | |
6981 | apply_wqattrs_unlock(); | |
6982 | } | |
6983 | ||
6984 | return ret; | |
6985 | } | |
6986 | ||
fe28f631 WL |
6987 | static ssize_t __wq_cpumask_show(struct device *dev, |
6988 | struct device_attribute *attr, char *buf, cpumask_var_t mask) | |
b05a7928 FW |
6989 | { |
6990 | int written; | |
6991 | ||
042f7df1 | 6992 | mutex_lock(&wq_pool_mutex); |
fe28f631 | 6993 | written = scnprintf(buf, PAGE_SIZE, "%*pb\n", cpumask_pr_args(mask)); |
042f7df1 | 6994 | mutex_unlock(&wq_pool_mutex); |
b05a7928 FW |
6995 | |
6996 | return written; | |
6997 | } | |
6998 | ||
fe28f631 WL |
6999 | static ssize_t wq_unbound_cpumask_show(struct device *dev, |
7000 | struct device_attribute *attr, char *buf) | |
7001 | { | |
7002 | return __wq_cpumask_show(dev, attr, buf, wq_unbound_cpumask); | |
7003 | } | |
7004 | ||
7005 | static ssize_t wq_requested_cpumask_show(struct device *dev, | |
7006 | struct device_attribute *attr, char *buf) | |
7007 | { | |
7008 | return __wq_cpumask_show(dev, attr, buf, wq_requested_unbound_cpumask); | |
7009 | } | |
7010 | ||
7011 | static ssize_t wq_isolated_cpumask_show(struct device *dev, | |
7012 | struct device_attribute *attr, char *buf) | |
7013 | { | |
7014 | return __wq_cpumask_show(dev, attr, buf, wq_isolated_cpumask); | |
7015 | } | |
7016 | ||
042f7df1 LJ |
7017 | static ssize_t wq_unbound_cpumask_store(struct device *dev, |
7018 | struct device_attribute *attr, const char *buf, size_t count) | |
7019 | { | |
7020 | cpumask_var_t cpumask; | |
7021 | int ret; | |
7022 | ||
7023 | if (!zalloc_cpumask_var(&cpumask, GFP_KERNEL)) | |
7024 | return -ENOMEM; | |
7025 | ||
7026 | ret = cpumask_parse(buf, cpumask); | |
7027 | if (!ret) | |
7028 | ret = workqueue_set_unbound_cpumask(cpumask); | |
7029 | ||
7030 | free_cpumask_var(cpumask); | |
7031 | return ret ? ret : count; | |
7032 | } | |
7033 | ||
fe28f631 | 7034 | static struct device_attribute wq_sysfs_cpumask_attrs[] = { |
042f7df1 | 7035 | __ATTR(cpumask, 0644, wq_unbound_cpumask_show, |
fe28f631 WL |
7036 | wq_unbound_cpumask_store), |
7037 | __ATTR(cpumask_requested, 0444, wq_requested_cpumask_show, NULL), | |
7038 | __ATTR(cpumask_isolated, 0444, wq_isolated_cpumask_show, NULL), | |
7039 | __ATTR_NULL, | |
7040 | }; | |
b05a7928 | 7041 | |
6ba94429 | 7042 | static int __init wq_sysfs_init(void) |
2d3854a3 | 7043 | { |
686f6697 | 7044 | struct device *dev_root; |
b05a7928 FW |
7045 | int err; |
7046 | ||
7047 | err = subsys_virtual_register(&wq_subsys, NULL); | |
7048 | if (err) | |
7049 | return err; | |
7050 | ||
686f6697 GKH |
7051 | dev_root = bus_get_dev_root(&wq_subsys); |
7052 | if (dev_root) { | |
fe28f631 WL |
7053 | struct device_attribute *attr; |
7054 | ||
7055 | for (attr = wq_sysfs_cpumask_attrs; attr->attr.name; attr++) { | |
7056 | err = device_create_file(dev_root, attr); | |
7057 | if (err) | |
7058 | break; | |
7059 | } | |
686f6697 GKH |
7060 | put_device(dev_root); |
7061 | } | |
7062 | return err; | |
2d3854a3 | 7063 | } |
6ba94429 | 7064 | core_initcall(wq_sysfs_init); |
2d3854a3 | 7065 | |
6ba94429 | 7066 | static void wq_device_release(struct device *dev) |
2d3854a3 | 7067 | { |
6ba94429 | 7068 | struct wq_device *wq_dev = container_of(dev, struct wq_device, dev); |
6b44003e | 7069 | |
6ba94429 | 7070 | kfree(wq_dev); |
2d3854a3 | 7071 | } |
a0a1a5fd TH |
7072 | |
7073 | /** | |
6ba94429 FW |
7074 | * workqueue_sysfs_register - make a workqueue visible in sysfs |
7075 | * @wq: the workqueue to register | |
a0a1a5fd | 7076 | * |
6ba94429 FW |
7077 | * Expose @wq in sysfs under /sys/bus/workqueue/devices. |
7078 | * alloc_workqueue*() automatically calls this function if WQ_SYSFS is set | |
7079 | * which is the preferred method. | |
a0a1a5fd | 7080 | * |
6ba94429 FW |
7081 | * Workqueue user should use this function directly iff it wants to apply |
7082 | * workqueue_attrs before making the workqueue visible in sysfs; otherwise, | |
7083 | * apply_workqueue_attrs() may race against userland updating the | |
7084 | * attributes. | |
7085 | * | |
7086 | * Return: 0 on success, -errno on failure. | |
a0a1a5fd | 7087 | */ |
6ba94429 | 7088 | int workqueue_sysfs_register(struct workqueue_struct *wq) |
a0a1a5fd | 7089 | { |
6ba94429 FW |
7090 | struct wq_device *wq_dev; |
7091 | int ret; | |
a0a1a5fd | 7092 | |
6ba94429 | 7093 | /* |
4c065dbc WL |
7094 | * Adjusting max_active breaks ordering guarantee. Disallow exposing |
7095 | * ordered workqueues. | |
6ba94429 | 7096 | */ |
3bc1e711 | 7097 | if (WARN_ON(wq->flags & __WQ_ORDERED)) |
6ba94429 | 7098 | return -EINVAL; |
a0a1a5fd | 7099 | |
6ba94429 FW |
7100 | wq->wq_dev = wq_dev = kzalloc(sizeof(*wq_dev), GFP_KERNEL); |
7101 | if (!wq_dev) | |
7102 | return -ENOMEM; | |
5bcab335 | 7103 | |
6ba94429 FW |
7104 | wq_dev->wq = wq; |
7105 | wq_dev->dev.bus = &wq_subsys; | |
6ba94429 | 7106 | wq_dev->dev.release = wq_device_release; |
23217b44 | 7107 | dev_set_name(&wq_dev->dev, "%s", wq->name); |
a0a1a5fd | 7108 | |
6ba94429 FW |
7109 | /* |
7110 | * unbound_attrs are created separately. Suppress uevent until | |
7111 | * everything is ready. | |
7112 | */ | |
7113 | dev_set_uevent_suppress(&wq_dev->dev, true); | |
a0a1a5fd | 7114 | |
6ba94429 FW |
7115 | ret = device_register(&wq_dev->dev); |
7116 | if (ret) { | |
537f4146 | 7117 | put_device(&wq_dev->dev); |
6ba94429 FW |
7118 | wq->wq_dev = NULL; |
7119 | return ret; | |
7120 | } | |
a0a1a5fd | 7121 | |
6ba94429 FW |
7122 | if (wq->flags & WQ_UNBOUND) { |
7123 | struct device_attribute *attr; | |
a0a1a5fd | 7124 | |
6ba94429 FW |
7125 | for (attr = wq_sysfs_unbound_attrs; attr->attr.name; attr++) { |
7126 | ret = device_create_file(&wq_dev->dev, attr); | |
7127 | if (ret) { | |
7128 | device_unregister(&wq_dev->dev); | |
7129 | wq->wq_dev = NULL; | |
7130 | return ret; | |
a0a1a5fd TH |
7131 | } |
7132 | } | |
7133 | } | |
6ba94429 FW |
7134 | |
7135 | dev_set_uevent_suppress(&wq_dev->dev, false); | |
7136 | kobject_uevent(&wq_dev->dev.kobj, KOBJ_ADD); | |
7137 | return 0; | |
a0a1a5fd TH |
7138 | } |
7139 | ||
7140 | /** | |
6ba94429 FW |
7141 | * workqueue_sysfs_unregister - undo workqueue_sysfs_register() |
7142 | * @wq: the workqueue to unregister | |
a0a1a5fd | 7143 | * |
6ba94429 | 7144 | * If @wq is registered to sysfs by workqueue_sysfs_register(), unregister. |
a0a1a5fd | 7145 | */ |
6ba94429 | 7146 | static void workqueue_sysfs_unregister(struct workqueue_struct *wq) |
a0a1a5fd | 7147 | { |
6ba94429 | 7148 | struct wq_device *wq_dev = wq->wq_dev; |
8b03ae3c | 7149 | |
6ba94429 FW |
7150 | if (!wq->wq_dev) |
7151 | return; | |
a0a1a5fd | 7152 | |
6ba94429 FW |
7153 | wq->wq_dev = NULL; |
7154 | device_unregister(&wq_dev->dev); | |
a0a1a5fd | 7155 | } |
6ba94429 FW |
7156 | #else /* CONFIG_SYSFS */ |
7157 | static void workqueue_sysfs_unregister(struct workqueue_struct *wq) { } | |
7158 | #endif /* CONFIG_SYSFS */ | |
a0a1a5fd | 7159 | |
82607adc TH |
7160 | /* |
7161 | * Workqueue watchdog. | |
7162 | * | |
7163 | * Stall may be caused by various bugs - missing WQ_MEM_RECLAIM, illegal | |
7164 | * flush dependency, a concurrency managed work item which stays RUNNING | |
7165 | * indefinitely. Workqueue stalls can be very difficult to debug as the | |
7166 | * usual warning mechanisms don't trigger and internal workqueue state is | |
7167 | * largely opaque. | |
7168 | * | |
7169 | * Workqueue watchdog monitors all worker pools periodically and dumps | |
7170 | * state if some pools failed to make forward progress for a while where | |
7171 | * forward progress is defined as the first item on ->worklist changing. | |
7172 | * | |
7173 | * This mechanism is controlled through the kernel parameter | |
7174 | * "workqueue.watchdog_thresh" which can be updated at runtime through the | |
7175 | * corresponding sysfs parameter file. | |
7176 | */ | |
7177 | #ifdef CONFIG_WQ_WATCHDOG | |
7178 | ||
82607adc | 7179 | static unsigned long wq_watchdog_thresh = 30; |
5cd79d6a | 7180 | static struct timer_list wq_watchdog_timer; |
82607adc TH |
7181 | |
7182 | static unsigned long wq_watchdog_touched = INITIAL_JIFFIES; | |
7183 | static DEFINE_PER_CPU(unsigned long, wq_watchdog_touched_cpu) = INITIAL_JIFFIES; | |
7184 | ||
cd2440d6 PM |
7185 | /* |
7186 | * Show workers that might prevent the processing of pending work items. | |
7187 | * The only candidates are CPU-bound workers in the running state. | |
7188 | * Pending work items should be handled by another idle worker | |
7189 | * in all other situations. | |
7190 | */ | |
7191 | static void show_cpu_pool_hog(struct worker_pool *pool) | |
7192 | { | |
7193 | struct worker *worker; | |
7194 | unsigned long flags; | |
7195 | int bkt; | |
7196 | ||
7197 | raw_spin_lock_irqsave(&pool->lock, flags); | |
7198 | ||
7199 | hash_for_each(pool->busy_hash, bkt, worker, hentry) { | |
7200 | if (task_is_running(worker->task)) { | |
7201 | /* | |
7202 | * Defer printing to avoid deadlocks in console | |
7203 | * drivers that queue work while holding locks | |
7204 | * also taken in their write paths. | |
7205 | */ | |
7206 | printk_deferred_enter(); | |
7207 | ||
7208 | pr_info("pool %d:\n", pool->id); | |
7209 | sched_show_task(worker->task); | |
7210 | ||
7211 | printk_deferred_exit(); | |
7212 | } | |
7213 | } | |
7214 | ||
7215 | raw_spin_unlock_irqrestore(&pool->lock, flags); | |
7216 | } | |
7217 | ||
7218 | static void show_cpu_pools_hogs(void) | |
7219 | { | |
7220 | struct worker_pool *pool; | |
7221 | int pi; | |
7222 | ||
7223 | pr_info("Showing backtraces of running workers in stalled CPU-bound worker pools:\n"); | |
7224 | ||
7225 | rcu_read_lock(); | |
7226 | ||
7227 | for_each_pool(pool, pi) { | |
7228 | if (pool->cpu_stall) | |
7229 | show_cpu_pool_hog(pool); | |
7230 | ||
7231 | } | |
7232 | ||
7233 | rcu_read_unlock(); | |
7234 | } | |
7235 | ||
82607adc TH |
7236 | static void wq_watchdog_reset_touched(void) |
7237 | { | |
7238 | int cpu; | |
7239 | ||
7240 | wq_watchdog_touched = jiffies; | |
7241 | for_each_possible_cpu(cpu) | |
7242 | per_cpu(wq_watchdog_touched_cpu, cpu) = jiffies; | |
7243 | } | |
7244 | ||
5cd79d6a | 7245 | static void wq_watchdog_timer_fn(struct timer_list *unused) |
82607adc TH |
7246 | { |
7247 | unsigned long thresh = READ_ONCE(wq_watchdog_thresh) * HZ; | |
7248 | bool lockup_detected = false; | |
cd2440d6 | 7249 | bool cpu_pool_stall = false; |
940d71c6 | 7250 | unsigned long now = jiffies; |
82607adc TH |
7251 | struct worker_pool *pool; |
7252 | int pi; | |
7253 | ||
7254 | if (!thresh) | |
7255 | return; | |
7256 | ||
7257 | rcu_read_lock(); | |
7258 | ||
7259 | for_each_pool(pool, pi) { | |
7260 | unsigned long pool_ts, touched, ts; | |
7261 | ||
cd2440d6 | 7262 | pool->cpu_stall = false; |
82607adc TH |
7263 | if (list_empty(&pool->worklist)) |
7264 | continue; | |
7265 | ||
940d71c6 SS |
7266 | /* |
7267 | * If a virtual machine is stopped by the host it can look to | |
7268 | * the watchdog like a stall. | |
7269 | */ | |
7270 | kvm_check_and_clear_guest_paused(); | |
7271 | ||
82607adc | 7272 | /* get the latest of pool and touched timestamps */ |
89e28ce6 WQ |
7273 | if (pool->cpu >= 0) |
7274 | touched = READ_ONCE(per_cpu(wq_watchdog_touched_cpu, pool->cpu)); | |
7275 | else | |
7276 | touched = READ_ONCE(wq_watchdog_touched); | |
82607adc | 7277 | pool_ts = READ_ONCE(pool->watchdog_ts); |
82607adc TH |
7278 | |
7279 | if (time_after(pool_ts, touched)) | |
7280 | ts = pool_ts; | |
7281 | else | |
7282 | ts = touched; | |
7283 | ||
82607adc | 7284 | /* did we stall? */ |
940d71c6 | 7285 | if (time_after(now, ts + thresh)) { |
82607adc | 7286 | lockup_detected = true; |
4cb1ef64 | 7287 | if (pool->cpu >= 0 && !(pool->flags & POOL_BH)) { |
cd2440d6 PM |
7288 | pool->cpu_stall = true; |
7289 | cpu_pool_stall = true; | |
7290 | } | |
82607adc TH |
7291 | pr_emerg("BUG: workqueue lockup - pool"); |
7292 | pr_cont_pool_info(pool); | |
7293 | pr_cont(" stuck for %us!\n", | |
940d71c6 | 7294 | jiffies_to_msecs(now - pool_ts) / 1000); |
82607adc | 7295 | } |
cd2440d6 PM |
7296 | |
7297 | ||
82607adc TH |
7298 | } |
7299 | ||
7300 | rcu_read_unlock(); | |
7301 | ||
7302 | if (lockup_detected) | |
55df0933 | 7303 | show_all_workqueues(); |
82607adc | 7304 | |
cd2440d6 PM |
7305 | if (cpu_pool_stall) |
7306 | show_cpu_pools_hogs(); | |
7307 | ||
82607adc TH |
7308 | wq_watchdog_reset_touched(); |
7309 | mod_timer(&wq_watchdog_timer, jiffies + thresh); | |
7310 | } | |
7311 | ||
cb9d7fd5 | 7312 | notrace void wq_watchdog_touch(int cpu) |
82607adc TH |
7313 | { |
7314 | if (cpu >= 0) | |
7315 | per_cpu(wq_watchdog_touched_cpu, cpu) = jiffies; | |
89e28ce6 WQ |
7316 | |
7317 | wq_watchdog_touched = jiffies; | |
82607adc TH |
7318 | } |
7319 | ||
7320 | static void wq_watchdog_set_thresh(unsigned long thresh) | |
7321 | { | |
7322 | wq_watchdog_thresh = 0; | |
7323 | del_timer_sync(&wq_watchdog_timer); | |
7324 | ||
7325 | if (thresh) { | |
7326 | wq_watchdog_thresh = thresh; | |
7327 | wq_watchdog_reset_touched(); | |
7328 | mod_timer(&wq_watchdog_timer, jiffies + thresh * HZ); | |
7329 | } | |
7330 | } | |
7331 | ||
7332 | static int wq_watchdog_param_set_thresh(const char *val, | |
7333 | const struct kernel_param *kp) | |
7334 | { | |
7335 | unsigned long thresh; | |
7336 | int ret; | |
7337 | ||
7338 | ret = kstrtoul(val, 0, &thresh); | |
7339 | if (ret) | |
7340 | return ret; | |
7341 | ||
7342 | if (system_wq) | |
7343 | wq_watchdog_set_thresh(thresh); | |
7344 | else | |
7345 | wq_watchdog_thresh = thresh; | |
7346 | ||
7347 | return 0; | |
7348 | } | |
7349 | ||
7350 | static const struct kernel_param_ops wq_watchdog_thresh_ops = { | |
7351 | .set = wq_watchdog_param_set_thresh, | |
7352 | .get = param_get_ulong, | |
7353 | }; | |
7354 | ||
7355 | module_param_cb(watchdog_thresh, &wq_watchdog_thresh_ops, &wq_watchdog_thresh, | |
7356 | 0644); | |
7357 | ||
7358 | static void wq_watchdog_init(void) | |
7359 | { | |
5cd79d6a | 7360 | timer_setup(&wq_watchdog_timer, wq_watchdog_timer_fn, TIMER_DEFERRABLE); |
82607adc TH |
7361 | wq_watchdog_set_thresh(wq_watchdog_thresh); |
7362 | } | |
7363 | ||
7364 | #else /* CONFIG_WQ_WATCHDOG */ | |
7365 | ||
7366 | static inline void wq_watchdog_init(void) { } | |
7367 | ||
7368 | #endif /* CONFIG_WQ_WATCHDOG */ | |
7369 | ||
4a6c5607 TH |
7370 | static void __init restrict_unbound_cpumask(const char *name, const struct cpumask *mask) |
7371 | { | |
7372 | if (!cpumask_intersects(wq_unbound_cpumask, mask)) { | |
7373 | pr_warn("workqueue: Restricting unbound_cpumask (%*pb) with %s (%*pb) leaves no CPU, ignoring\n", | |
7374 | cpumask_pr_args(wq_unbound_cpumask), name, cpumask_pr_args(mask)); | |
7375 | return; | |
7376 | } | |
7377 | ||
7378 | cpumask_and(wq_unbound_cpumask, wq_unbound_cpumask, mask); | |
7379 | } | |
7380 | ||
2fcdb1b4 TH |
7381 | static void __init init_cpu_worker_pool(struct worker_pool *pool, int cpu, int nice) |
7382 | { | |
7383 | BUG_ON(init_worker_pool(pool)); | |
7384 | pool->cpu = cpu; | |
7385 | cpumask_copy(pool->attrs->cpumask, cpumask_of(cpu)); | |
7386 | cpumask_copy(pool->attrs->__pod_cpumask, cpumask_of(cpu)); | |
7387 | pool->attrs->nice = nice; | |
7388 | pool->attrs->affn_strict = true; | |
7389 | pool->node = cpu_to_node(cpu); | |
7390 | ||
7391 | /* alloc pool ID */ | |
7392 | mutex_lock(&wq_pool_mutex); | |
7393 | BUG_ON(worker_pool_assign_id(pool)); | |
7394 | mutex_unlock(&wq_pool_mutex); | |
7395 | } | |
7396 | ||
3347fa09 TH |
7397 | /** |
7398 | * workqueue_init_early - early init for workqueue subsystem | |
7399 | * | |
2930155b TH |
7400 | * This is the first step of three-staged workqueue subsystem initialization and |
7401 | * invoked as soon as the bare basics - memory allocation, cpumasks and idr are | |
7402 | * up. It sets up all the data structures and system workqueues and allows early | |
7403 | * boot code to create workqueues and queue/cancel work items. Actual work item | |
7404 | * execution starts only after kthreads can be created and scheduled right | |
7405 | * before early initcalls. | |
3347fa09 | 7406 | */ |
2333e829 | 7407 | void __init workqueue_init_early(void) |
1da177e4 | 7408 | { |
84193c07 | 7409 | struct wq_pod_type *pt = &wq_pod_types[WQ_AFFN_SYSTEM]; |
7a4e344c TH |
7410 | int std_nice[NR_STD_WORKER_POOLS] = { 0, HIGHPRI_NICE_LEVEL }; |
7411 | int i, cpu; | |
c34056a3 | 7412 | |
10cdb157 | 7413 | BUILD_BUG_ON(__alignof__(struct pool_workqueue) < __alignof__(long long)); |
e904e6c2 | 7414 | |
b05a7928 | 7415 | BUG_ON(!alloc_cpumask_var(&wq_unbound_cpumask, GFP_KERNEL)); |
fe28f631 WL |
7416 | BUG_ON(!alloc_cpumask_var(&wq_requested_unbound_cpumask, GFP_KERNEL)); |
7417 | BUG_ON(!zalloc_cpumask_var(&wq_isolated_cpumask, GFP_KERNEL)); | |
b05a7928 | 7418 | |
4a6c5607 TH |
7419 | cpumask_copy(wq_unbound_cpumask, cpu_possible_mask); |
7420 | restrict_unbound_cpumask("HK_TYPE_WQ", housekeeping_cpumask(HK_TYPE_WQ)); | |
7421 | restrict_unbound_cpumask("HK_TYPE_DOMAIN", housekeeping_cpumask(HK_TYPE_DOMAIN)); | |
ace3c549 | 7422 | if (!cpumask_empty(&wq_cmdline_cpumask)) |
4a6c5607 | 7423 | restrict_unbound_cpumask("workqueue.unbound_cpus", &wq_cmdline_cpumask); |
ace3c549 | 7424 | |
fe28f631 | 7425 | cpumask_copy(wq_requested_unbound_cpumask, wq_unbound_cpumask); |
ace3c549 | 7426 | |
e904e6c2 TH |
7427 | pwq_cache = KMEM_CACHE(pool_workqueue, SLAB_PANIC); |
7428 | ||
2930155b TH |
7429 | wq_update_pod_attrs_buf = alloc_workqueue_attrs(); |
7430 | BUG_ON(!wq_update_pod_attrs_buf); | |
7431 | ||
7bd20b6b MT |
7432 | /* |
7433 | * If nohz_full is enabled, set power efficient workqueue as unbound. | |
7434 | * This allows workqueue items to be moved to HK CPUs. | |
7435 | */ | |
7436 | if (housekeeping_enabled(HK_TYPE_TICK)) | |
7437 | wq_power_efficient = true; | |
7438 | ||
84193c07 TH |
7439 | /* initialize WQ_AFFN_SYSTEM pods */ |
7440 | pt->pod_cpus = kcalloc(1, sizeof(pt->pod_cpus[0]), GFP_KERNEL); | |
7441 | pt->pod_node = kcalloc(1, sizeof(pt->pod_node[0]), GFP_KERNEL); | |
7442 | pt->cpu_pod = kcalloc(nr_cpu_ids, sizeof(pt->cpu_pod[0]), GFP_KERNEL); | |
7443 | BUG_ON(!pt->pod_cpus || !pt->pod_node || !pt->cpu_pod); | |
7444 | ||
7445 | BUG_ON(!zalloc_cpumask_var_node(&pt->pod_cpus[0], GFP_KERNEL, NUMA_NO_NODE)); | |
7446 | ||
84193c07 TH |
7447 | pt->nr_pods = 1; |
7448 | cpumask_copy(pt->pod_cpus[0], cpu_possible_mask); | |
7449 | pt->pod_node[0] = NUMA_NO_NODE; | |
7450 | pt->cpu_pod[0] = 0; | |
7451 | ||
4cb1ef64 | 7452 | /* initialize BH and CPU pools */ |
29c91e99 | 7453 | for_each_possible_cpu(cpu) { |
4ce62e9e | 7454 | struct worker_pool *pool; |
8b03ae3c | 7455 | |
4cb1ef64 TH |
7456 | i = 0; |
7457 | for_each_bh_worker_pool(pool, cpu) { | |
7458 | init_cpu_worker_pool(pool, cpu, std_nice[i++]); | |
7459 | pool->flags |= POOL_BH; | |
7460 | } | |
7461 | ||
7a4e344c | 7462 | i = 0; |
2fcdb1b4 TH |
7463 | for_each_cpu_worker_pool(pool, cpu) |
7464 | init_cpu_worker_pool(pool, cpu, std_nice[i++]); | |
8b03ae3c TH |
7465 | } |
7466 | ||
8a2b7538 | 7467 | /* create default unbound and ordered wq attrs */ |
29c91e99 TH |
7468 | for (i = 0; i < NR_STD_WORKER_POOLS; i++) { |
7469 | struct workqueue_attrs *attrs; | |
7470 | ||
be69d00d | 7471 | BUG_ON(!(attrs = alloc_workqueue_attrs())); |
29c91e99 | 7472 | attrs->nice = std_nice[i]; |
29c91e99 | 7473 | unbound_std_wq_attrs[i] = attrs; |
8a2b7538 TH |
7474 | |
7475 | /* | |
7476 | * An ordered wq should have only one pwq as ordering is | |
7477 | * guaranteed by max_active which is enforced by pwqs. | |
8a2b7538 | 7478 | */ |
be69d00d | 7479 | BUG_ON(!(attrs = alloc_workqueue_attrs())); |
8a2b7538 | 7480 | attrs->nice = std_nice[i]; |
af73f5c9 | 7481 | attrs->ordered = true; |
8a2b7538 | 7482 | ordered_wq_attrs[i] = attrs; |
29c91e99 TH |
7483 | } |
7484 | ||
d320c038 | 7485 | system_wq = alloc_workqueue("events", 0, 0); |
1aabe902 | 7486 | system_highpri_wq = alloc_workqueue("events_highpri", WQ_HIGHPRI, 0); |
d320c038 | 7487 | system_long_wq = alloc_workqueue("events_long", 0, 0); |
f3421797 | 7488 | system_unbound_wq = alloc_workqueue("events_unbound", WQ_UNBOUND, |
636b927e | 7489 | WQ_MAX_ACTIVE); |
24d51add TH |
7490 | system_freezable_wq = alloc_workqueue("events_freezable", |
7491 | WQ_FREEZABLE, 0); | |
0668106c VK |
7492 | system_power_efficient_wq = alloc_workqueue("events_power_efficient", |
7493 | WQ_POWER_EFFICIENT, 0); | |
8318d6a6 | 7494 | system_freezable_power_efficient_wq = alloc_workqueue("events_freezable_pwr_efficient", |
0668106c VK |
7495 | WQ_FREEZABLE | WQ_POWER_EFFICIENT, |
7496 | 0); | |
4cb1ef64 TH |
7497 | system_bh_wq = alloc_workqueue("events_bh", WQ_BH, 0); |
7498 | system_bh_highpri_wq = alloc_workqueue("events_bh_highpri", | |
7499 | WQ_BH | WQ_HIGHPRI, 0); | |
1aabe902 | 7500 | BUG_ON(!system_wq || !system_highpri_wq || !system_long_wq || |
0668106c VK |
7501 | !system_unbound_wq || !system_freezable_wq || |
7502 | !system_power_efficient_wq || | |
4cb1ef64 TH |
7503 | !system_freezable_power_efficient_wq || |
7504 | !system_bh_wq || !system_bh_highpri_wq); | |
3347fa09 TH |
7505 | } |
7506 | ||
aa6fde93 TH |
7507 | static void __init wq_cpu_intensive_thresh_init(void) |
7508 | { | |
7509 | unsigned long thresh; | |
7510 | unsigned long bogo; | |
7511 | ||
dd64c873 Z |
7512 | pwq_release_worker = kthread_create_worker(0, "pool_workqueue_release"); |
7513 | BUG_ON(IS_ERR(pwq_release_worker)); | |
7514 | ||
aa6fde93 TH |
7515 | /* if the user set it to a specific value, keep it */ |
7516 | if (wq_cpu_intensive_thresh_us != ULONG_MAX) | |
7517 | return; | |
7518 | ||
7519 | /* | |
7520 | * The default of 10ms is derived from the fact that most modern (as of | |
7521 | * 2023) processors can do a lot in 10ms and that it's just below what | |
7522 | * most consider human-perceivable. However, the kernel also runs on a | |
7523 | * lot slower CPUs including microcontrollers where the threshold is way | |
7524 | * too low. | |
7525 | * | |
7526 | * Let's scale up the threshold upto 1 second if BogoMips is below 4000. | |
7527 | * This is by no means accurate but it doesn't have to be. The mechanism | |
7528 | * is still useful even when the threshold is fully scaled up. Also, as | |
7529 | * the reports would usually be applicable to everyone, some machines | |
7530 | * operating on longer thresholds won't significantly diminish their | |
7531 | * usefulness. | |
7532 | */ | |
7533 | thresh = 10 * USEC_PER_MSEC; | |
7534 | ||
7535 | /* see init/calibrate.c for lpj -> BogoMIPS calculation */ | |
7536 | bogo = max_t(unsigned long, loops_per_jiffy / 500000 * HZ, 1); | |
7537 | if (bogo < 4000) | |
7538 | thresh = min_t(unsigned long, thresh * 4000 / bogo, USEC_PER_SEC); | |
7539 | ||
7540 | pr_debug("wq_cpu_intensive_thresh: lpj=%lu BogoMIPS=%lu thresh_us=%lu\n", | |
7541 | loops_per_jiffy, bogo, thresh); | |
7542 | ||
7543 | wq_cpu_intensive_thresh_us = thresh; | |
7544 | } | |
7545 | ||
3347fa09 TH |
7546 | /** |
7547 | * workqueue_init - bring workqueue subsystem fully online | |
7548 | * | |
2930155b TH |
7549 | * This is the second step of three-staged workqueue subsystem initialization |
7550 | * and invoked as soon as kthreads can be created and scheduled. Workqueues have | |
7551 | * been created and work items queued on them, but there are no kworkers | |
7552 | * executing the work items yet. Populate the worker pools with the initial | |
7553 | * workers and enable future kworker creations. | |
3347fa09 | 7554 | */ |
2333e829 | 7555 | void __init workqueue_init(void) |
3347fa09 | 7556 | { |
2186d9f9 | 7557 | struct workqueue_struct *wq; |
3347fa09 TH |
7558 | struct worker_pool *pool; |
7559 | int cpu, bkt; | |
7560 | ||
aa6fde93 TH |
7561 | wq_cpu_intensive_thresh_init(); |
7562 | ||
2186d9f9 TH |
7563 | mutex_lock(&wq_pool_mutex); |
7564 | ||
2930155b TH |
7565 | /* |
7566 | * Per-cpu pools created earlier could be missing node hint. Fix them | |
7567 | * up. Also, create a rescuer for workqueues that requested it. | |
7568 | */ | |
2186d9f9 | 7569 | for_each_possible_cpu(cpu) { |
4cb1ef64 TH |
7570 | for_each_bh_worker_pool(pool, cpu) |
7571 | pool->node = cpu_to_node(cpu); | |
7572 | for_each_cpu_worker_pool(pool, cpu) | |
2186d9f9 | 7573 | pool->node = cpu_to_node(cpu); |
2186d9f9 TH |
7574 | } |
7575 | ||
40c17f75 | 7576 | list_for_each_entry(wq, &workqueues, list) { |
40c17f75 TH |
7577 | WARN(init_rescuer(wq), |
7578 | "workqueue: failed to create early rescuer for %s", | |
7579 | wq->name); | |
7580 | } | |
2186d9f9 TH |
7581 | |
7582 | mutex_unlock(&wq_pool_mutex); | |
7583 | ||
4cb1ef64 TH |
7584 | /* |
7585 | * Create the initial workers. A BH pool has one pseudo worker that | |
7586 | * represents the shared BH execution context and thus doesn't get | |
7587 | * affected by hotplug events. Create the BH pseudo workers for all | |
7588 | * possible CPUs here. | |
7589 | */ | |
7590 | for_each_possible_cpu(cpu) | |
7591 | for_each_bh_worker_pool(pool, cpu) | |
7592 | BUG_ON(!create_worker(pool)); | |
7593 | ||
3347fa09 TH |
7594 | for_each_online_cpu(cpu) { |
7595 | for_each_cpu_worker_pool(pool, cpu) { | |
7596 | pool->flags &= ~POOL_DISASSOCIATED; | |
7597 | BUG_ON(!create_worker(pool)); | |
7598 | } | |
7599 | } | |
7600 | ||
7601 | hash_for_each(unbound_pool_hash, bkt, pool, hash_node) | |
7602 | BUG_ON(!create_worker(pool)); | |
7603 | ||
7604 | wq_online = true; | |
82607adc | 7605 | wq_watchdog_init(); |
1da177e4 | 7606 | } |
c4f135d6 | 7607 | |
025e1684 TH |
7608 | /* |
7609 | * Initialize @pt by first initializing @pt->cpu_pod[] with pod IDs according to | |
7610 | * @cpu_shares_pod(). Each subset of CPUs that share a pod is assigned a unique | |
7611 | * and consecutive pod ID. The rest of @pt is initialized accordingly. | |
7612 | */ | |
7613 | static void __init init_pod_type(struct wq_pod_type *pt, | |
7614 | bool (*cpus_share_pod)(int, int)) | |
7615 | { | |
7616 | int cur, pre, cpu, pod; | |
7617 | ||
7618 | pt->nr_pods = 0; | |
7619 | ||
7620 | /* init @pt->cpu_pod[] according to @cpus_share_pod() */ | |
7621 | pt->cpu_pod = kcalloc(nr_cpu_ids, sizeof(pt->cpu_pod[0]), GFP_KERNEL); | |
7622 | BUG_ON(!pt->cpu_pod); | |
7623 | ||
7624 | for_each_possible_cpu(cur) { | |
7625 | for_each_possible_cpu(pre) { | |
7626 | if (pre >= cur) { | |
7627 | pt->cpu_pod[cur] = pt->nr_pods++; | |
7628 | break; | |
7629 | } | |
7630 | if (cpus_share_pod(cur, pre)) { | |
7631 | pt->cpu_pod[cur] = pt->cpu_pod[pre]; | |
7632 | break; | |
7633 | } | |
7634 | } | |
7635 | } | |
7636 | ||
7637 | /* init the rest to match @pt->cpu_pod[] */ | |
7638 | pt->pod_cpus = kcalloc(pt->nr_pods, sizeof(pt->pod_cpus[0]), GFP_KERNEL); | |
7639 | pt->pod_node = kcalloc(pt->nr_pods, sizeof(pt->pod_node[0]), GFP_KERNEL); | |
7640 | BUG_ON(!pt->pod_cpus || !pt->pod_node); | |
7641 | ||
7642 | for (pod = 0; pod < pt->nr_pods; pod++) | |
7643 | BUG_ON(!zalloc_cpumask_var(&pt->pod_cpus[pod], GFP_KERNEL)); | |
7644 | ||
7645 | for_each_possible_cpu(cpu) { | |
7646 | cpumask_set_cpu(cpu, pt->pod_cpus[pt->cpu_pod[cpu]]); | |
7647 | pt->pod_node[pt->cpu_pod[cpu]] = cpu_to_node(cpu); | |
7648 | } | |
7649 | } | |
7650 | ||
63c5484e TH |
7651 | static bool __init cpus_dont_share(int cpu0, int cpu1) |
7652 | { | |
7653 | return false; | |
7654 | } | |
7655 | ||
7656 | static bool __init cpus_share_smt(int cpu0, int cpu1) | |
7657 | { | |
7658 | #ifdef CONFIG_SCHED_SMT | |
7659 | return cpumask_test_cpu(cpu0, cpu_smt_mask(cpu1)); | |
7660 | #else | |
7661 | return false; | |
7662 | #endif | |
7663 | } | |
7664 | ||
025e1684 TH |
7665 | static bool __init cpus_share_numa(int cpu0, int cpu1) |
7666 | { | |
7667 | return cpu_to_node(cpu0) == cpu_to_node(cpu1); | |
7668 | } | |
7669 | ||
2930155b TH |
7670 | /** |
7671 | * workqueue_init_topology - initialize CPU pods for unbound workqueues | |
7672 | * | |
96068b60 | 7673 | * This is the third step of three-staged workqueue subsystem initialization and |
2930155b TH |
7674 | * invoked after SMP and topology information are fully initialized. It |
7675 | * initializes the unbound CPU pods accordingly. | |
7676 | */ | |
7677 | void __init workqueue_init_topology(void) | |
a86feae6 | 7678 | { |
2930155b | 7679 | struct workqueue_struct *wq; |
025e1684 | 7680 | int cpu; |
a86feae6 | 7681 | |
63c5484e TH |
7682 | init_pod_type(&wq_pod_types[WQ_AFFN_CPU], cpus_dont_share); |
7683 | init_pod_type(&wq_pod_types[WQ_AFFN_SMT], cpus_share_smt); | |
7684 | init_pod_type(&wq_pod_types[WQ_AFFN_CACHE], cpus_share_cache); | |
025e1684 | 7685 | init_pod_type(&wq_pod_types[WQ_AFFN_NUMA], cpus_share_numa); |
a86feae6 | 7686 | |
c5f8cd6c TH |
7687 | wq_topo_initialized = true; |
7688 | ||
2930155b | 7689 | mutex_lock(&wq_pool_mutex); |
a86feae6 | 7690 | |
2930155b TH |
7691 | /* |
7692 | * Workqueues allocated earlier would have all CPUs sharing the default | |
7693 | * worker pool. Explicitly call wq_update_pod() on all workqueue and CPU | |
7694 | * combinations to apply per-pod sharing. | |
7695 | */ | |
7696 | list_for_each_entry(wq, &workqueues, list) { | |
5797b1c1 | 7697 | for_each_online_cpu(cpu) |
2930155b | 7698 | wq_update_pod(wq, cpu, cpu, true); |
5797b1c1 TH |
7699 | if (wq->flags & WQ_UNBOUND) { |
7700 | mutex_lock(&wq->mutex); | |
7701 | wq_update_node_max_active(wq, -1); | |
7702 | mutex_unlock(&wq->mutex); | |
2930155b TH |
7703 | } |
7704 | } | |
7705 | ||
7706 | mutex_unlock(&wq_pool_mutex); | |
a86feae6 TH |
7707 | } |
7708 | ||
20bdedaf TH |
7709 | void __warn_flushing_systemwide_wq(void) |
7710 | { | |
7711 | pr_warn("WARNING: Flushing system-wide workqueues will be prohibited in near future.\n"); | |
7712 | dump_stack(); | |
7713 | } | |
c4f135d6 | 7714 | EXPORT_SYMBOL(__warn_flushing_systemwide_wq); |
ace3c549 | 7715 | |
7716 | static int __init workqueue_unbound_cpus_setup(char *str) | |
7717 | { | |
7718 | if (cpulist_parse(str, &wq_cmdline_cpumask) < 0) { | |
7719 | cpumask_clear(&wq_cmdline_cpumask); | |
7720 | pr_warn("workqueue.unbound_cpus: incorrect CPU range, using default\n"); | |
7721 | } | |
7722 | ||
7723 | return 1; | |
7724 | } | |
7725 | __setup("workqueue.unbound_cpus=", workqueue_unbound_cpus_setup); |