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