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