Commit | Line | Data |
---|---|---|
771b53d0 JA |
1 | // SPDX-License-Identifier: GPL-2.0 |
2 | /* | |
3 | * Basic worker thread pool for io_uring | |
4 | * | |
5 | * Copyright (C) 2019 Jens Axboe | |
6 | * | |
7 | */ | |
8 | #include <linux/kernel.h> | |
9 | #include <linux/init.h> | |
10 | #include <linux/errno.h> | |
11 | #include <linux/sched/signal.h> | |
771b53d0 JA |
12 | #include <linux/percpu.h> |
13 | #include <linux/slab.h> | |
771b53d0 | 14 | #include <linux/rculist_nulls.h> |
43c01fbe | 15 | #include <linux/cpu.h> |
0997aa54 | 16 | #include <linux/cpuset.h> |
355f841a | 17 | #include <linux/task_work.h> |
5bd2182d | 18 | #include <linux/audit.h> |
da64d6db | 19 | #include <linux/mmu_context.h> |
dd47c104 | 20 | #include <uapi/linux/io_uring.h> |
771b53d0 JA |
21 | |
22 | #include "io-wq.h" | |
a6b21fbb | 23 | #include "slist.h" |
024f15e0 | 24 | #include "io_uring.h" |
771b53d0 JA |
25 | |
26 | #define WORKER_IDLE_TIMEOUT (5 * HZ) | |
0453aad6 | 27 | #define WORKER_INIT_LIMIT 3 |
771b53d0 JA |
28 | |
29 | enum { | |
8a565304 BL |
30 | IO_WORKER_F_UP = 0, /* up and active */ |
31 | IO_WORKER_F_RUNNING = 1, /* account as running */ | |
32 | IO_WORKER_F_FREE = 2, /* worker on free list */ | |
771b53d0 JA |
33 | }; |
34 | ||
35 | enum { | |
36 | IO_WQ_BIT_EXIT = 0, /* wq exiting */ | |
771b53d0 JA |
37 | }; |
38 | ||
39 | enum { | |
f95dc207 | 40 | IO_ACCT_STALLED_BIT = 0, /* stalled on hash */ |
771b53d0 JA |
41 | }; |
42 | ||
43 | /* | |
eb47943f | 44 | * One for each thread in a wq pool |
771b53d0 JA |
45 | */ |
46 | struct io_worker { | |
47 | refcount_t ref; | |
8a565304 | 48 | unsigned long flags; |
771b53d0 | 49 | struct hlist_nulls_node nulls_node; |
e61df66c | 50 | struct list_head all_list; |
771b53d0 | 51 | struct task_struct *task; |
eb47943f | 52 | struct io_wq *wq; |
3d3bafd3 | 53 | struct io_wq_acct *acct; |
36c2f922 | 54 | |
771b53d0 | 55 | struct io_wq_work *cur_work; |
081b5820 | 56 | raw_spinlock_t lock; |
771b53d0 | 57 | |
eb2de941 JA |
58 | struct completion ref_done; |
59 | ||
d3e9f732 JA |
60 | unsigned long create_state; |
61 | struct callback_head create_work; | |
0453aad6 | 62 | int init_retries; |
d3e9f732 | 63 | |
3146cba9 JA |
64 | union { |
65 | struct rcu_head rcu; | |
13918315 | 66 | struct delayed_work work; |
3146cba9 | 67 | }; |
771b53d0 JA |
68 | }; |
69 | ||
771b53d0 JA |
70 | #if BITS_PER_LONG == 64 |
71 | #define IO_WQ_HASH_ORDER 6 | |
72 | #else | |
73 | #define IO_WQ_HASH_ORDER 5 | |
74 | #endif | |
75 | ||
86f3cd1b PB |
76 | #define IO_WQ_NR_HASH_BUCKETS (1u << IO_WQ_HASH_ORDER) |
77 | ||
dfd63baf | 78 | struct io_wq_acct { |
751eedc4 MK |
79 | /** |
80 | * Protects access to the worker lists. | |
81 | */ | |
82 | raw_spinlock_t workers_lock; | |
83 | ||
c5def4ab JA |
84 | unsigned nr_workers; |
85 | unsigned max_workers; | |
86 | atomic_t nr_running; | |
751eedc4 MK |
87 | |
88 | /** | |
89 | * The list of free workers. Protected by #workers_lock | |
90 | * (write) and RCU (read). | |
91 | */ | |
92 | struct hlist_nulls_head free_list; | |
93 | ||
94 | /** | |
95 | * The list of all workers. Protected by #workers_lock | |
96 | * (write) and RCU (read). | |
97 | */ | |
98 | struct list_head all_list; | |
99 | ||
42abc95f | 100 | raw_spinlock_t lock; |
f95dc207 JA |
101 | struct io_wq_work_list work_list; |
102 | unsigned long flags; | |
c5def4ab JA |
103 | }; |
104 | ||
105 | enum { | |
106 | IO_WQ_ACCT_BOUND, | |
107 | IO_WQ_ACCT_UNBOUND, | |
f95dc207 | 108 | IO_WQ_ACCT_NR, |
c5def4ab JA |
109 | }; |
110 | ||
771b53d0 JA |
111 | /* |
112 | * Per io_wq state | |
113 | */ | |
114 | struct io_wq { | |
771b53d0 | 115 | unsigned long state; |
771b53d0 | 116 | |
e941894e JA |
117 | struct io_wq_hash *hash; |
118 | ||
fb3a1f6c JA |
119 | atomic_t worker_refs; |
120 | struct completion worker_done; | |
121 | ||
43c01fbe | 122 | struct hlist_node cpuhp_node; |
3bfe6106 | 123 | |
685fe7fe | 124 | struct task_struct *task; |
c7f405d6 | 125 | |
dfd63baf GKB |
126 | struct io_wq_acct acct[IO_WQ_ACCT_NR]; |
127 | ||
eb47943f GKB |
128 | struct wait_queue_entry wait; |
129 | ||
130 | struct io_wq_work *hash_tail[IO_WQ_NR_HASH_BUCKETS]; | |
131 | ||
132 | cpumask_var_t cpu_mask; | |
771b53d0 JA |
133 | }; |
134 | ||
43c01fbe JA |
135 | static enum cpuhp_state io_wq_online; |
136 | ||
f0127254 JA |
137 | struct io_cb_cancel_data { |
138 | work_cancel_fn *fn; | |
139 | void *data; | |
140 | int nr_running; | |
141 | int nr_pending; | |
142 | bool cancel_all; | |
143 | }; | |
144 | ||
3d3bafd3 | 145 | static bool create_io_worker(struct io_wq *wq, struct io_wq_acct *acct); |
eb47943f GKB |
146 | static void io_wq_dec_running(struct io_worker *worker); |
147 | static bool io_acct_cancel_pending_work(struct io_wq *wq, | |
dfd63baf | 148 | struct io_wq_acct *acct, |
3146cba9 | 149 | struct io_cb_cancel_data *match); |
1d5f5ea7 | 150 | static void create_worker_cb(struct callback_head *cb); |
71a85387 | 151 | static void io_wq_cancel_tw_create(struct io_wq *wq); |
f0127254 | 152 | |
e37dfc05 JA |
153 | static inline unsigned int __io_get_work_hash(unsigned int work_flags) |
154 | { | |
155 | return work_flags >> IO_WQ_HASH_SHIFT; | |
156 | } | |
157 | ||
158 | static inline unsigned int io_get_work_hash(struct io_wq_work *work) | |
159 | { | |
160 | return __io_get_work_hash(atomic_read(&work->flags)); | |
161 | } | |
162 | ||
771b53d0 JA |
163 | static bool io_worker_get(struct io_worker *worker) |
164 | { | |
165 | return refcount_inc_not_zero(&worker->ref); | |
166 | } | |
167 | ||
168 | static void io_worker_release(struct io_worker *worker) | |
169 | { | |
170 | if (refcount_dec_and_test(&worker->ref)) | |
eb2de941 | 171 | complete(&worker->ref_done); |
771b53d0 JA |
172 | } |
173 | ||
dfd63baf | 174 | static inline struct io_wq_acct *io_get_acct(struct io_wq *wq, bool bound) |
8418f22a | 175 | { |
dfd63baf | 176 | return &wq->acct[bound ? IO_WQ_ACCT_BOUND : IO_WQ_ACCT_UNBOUND]; |
8418f22a PB |
177 | } |
178 | ||
dfd63baf | 179 | static inline struct io_wq_acct *io_work_get_acct(struct io_wq *wq, |
6ee78354 | 180 | unsigned int work_flags) |
c5def4ab | 181 | { |
6ee78354 | 182 | return io_get_acct(wq, !(work_flags & IO_WQ_WORK_UNBOUND)); |
c5def4ab JA |
183 | } |
184 | ||
dfd63baf | 185 | static inline struct io_wq_acct *io_wq_get_acct(struct io_worker *worker) |
c5def4ab | 186 | { |
3d3bafd3 | 187 | return worker->acct; |
c5def4ab JA |
188 | } |
189 | ||
685fe7fe JA |
190 | static void io_worker_ref_put(struct io_wq *wq) |
191 | { | |
192 | if (atomic_dec_and_test(&wq->worker_refs)) | |
193 | complete(&wq->worker_done); | |
194 | } | |
195 | ||
45500dc4 PB |
196 | bool io_wq_worker_stopped(void) |
197 | { | |
198 | struct io_worker *worker = current->worker_private; | |
199 | ||
200 | if (WARN_ON_ONCE(!io_wq_current_is_worker())) | |
201 | return true; | |
202 | ||
203 | return test_bit(IO_WQ_BIT_EXIT, &worker->wq->state); | |
204 | } | |
205 | ||
1d5f5ea7 PB |
206 | static void io_worker_cancel_cb(struct io_worker *worker) |
207 | { | |
dfd63baf | 208 | struct io_wq_acct *acct = io_wq_get_acct(worker); |
eb47943f | 209 | struct io_wq *wq = worker->wq; |
1d5f5ea7 PB |
210 | |
211 | atomic_dec(&acct->nr_running); | |
751eedc4 | 212 | raw_spin_lock(&acct->workers_lock); |
1d5f5ea7 | 213 | acct->nr_workers--; |
751eedc4 | 214 | raw_spin_unlock(&acct->workers_lock); |
1d5f5ea7 PB |
215 | io_worker_ref_put(wq); |
216 | clear_bit_unlock(0, &worker->create_state); | |
217 | io_worker_release(worker); | |
218 | } | |
219 | ||
220 | static bool io_task_worker_match(struct callback_head *cb, void *data) | |
221 | { | |
222 | struct io_worker *worker; | |
223 | ||
224 | if (cb->func != create_worker_cb) | |
225 | return false; | |
226 | worker = container_of(cb, struct io_worker, create_work); | |
227 | return worker == data; | |
228 | } | |
229 | ||
771b53d0 JA |
230 | static void io_worker_exit(struct io_worker *worker) |
231 | { | |
eb47943f | 232 | struct io_wq *wq = worker->wq; |
751eedc4 | 233 | struct io_wq_acct *acct = io_wq_get_acct(worker); |
771b53d0 | 234 | |
1d5f5ea7 PB |
235 | while (1) { |
236 | struct callback_head *cb = task_work_cancel_match(wq->task, | |
237 | io_task_worker_match, worker); | |
238 | ||
239 | if (!cb) | |
240 | break; | |
241 | io_worker_cancel_cb(worker); | |
242 | } | |
771b53d0 | 243 | |
c907e52c | 244 | io_worker_release(worker); |
eb2de941 | 245 | wait_for_completion(&worker->ref_done); |
771b53d0 | 246 | |
751eedc4 | 247 | raw_spin_lock(&acct->workers_lock); |
8a565304 | 248 | if (test_bit(IO_WORKER_F_FREE, &worker->flags)) |
bf1daa4b | 249 | hlist_nulls_del_rcu(&worker->nulls_node); |
e61df66c | 250 | list_del_rcu(&worker->all_list); |
751eedc4 | 251 | raw_spin_unlock(&acct->workers_lock); |
eb47943f | 252 | io_wq_dec_running(worker); |
adeaa3f2 JA |
253 | /* |
254 | * this worker is a goner, clear ->worker_private to avoid any | |
255 | * inc/dec running calls that could happen as part of exit from | |
256 | * touching 'worker'. | |
257 | */ | |
258 | current->worker_private = NULL; | |
771b53d0 | 259 | |
364b05fd | 260 | kfree_rcu(worker, rcu); |
eb47943f | 261 | io_worker_ref_put(wq); |
46fe18b1 | 262 | do_exit(0); |
771b53d0 JA |
263 | } |
264 | ||
de36a15f | 265 | static inline bool __io_acct_run_queue(struct io_wq_acct *acct) |
c5def4ab | 266 | { |
de36a15f JA |
267 | return !test_bit(IO_ACCT_STALLED_BIT, &acct->flags) && |
268 | !wq_list_empty(&acct->work_list); | |
269 | } | |
e13fb1fe | 270 | |
de36a15f JA |
271 | /* |
272 | * If there's work to do, returns true with acct->lock acquired. If not, | |
273 | * returns false with no lock held. | |
274 | */ | |
275 | static inline bool io_acct_run_queue(struct io_wq_acct *acct) | |
276 | __acquires(&acct->lock) | |
277 | { | |
e13fb1fe | 278 | raw_spin_lock(&acct->lock); |
de36a15f JA |
279 | if (__io_acct_run_queue(acct)) |
280 | return true; | |
e13fb1fe | 281 | |
de36a15f JA |
282 | raw_spin_unlock(&acct->lock); |
283 | return false; | |
c5def4ab JA |
284 | } |
285 | ||
286 | /* | |
287 | * Check head of free list for an available worker. If one isn't available, | |
685fe7fe | 288 | * caller must create one. |
c5def4ab | 289 | */ |
751eedc4 | 290 | static bool io_acct_activate_free_worker(struct io_wq_acct *acct) |
c5def4ab JA |
291 | __must_hold(RCU) |
292 | { | |
293 | struct hlist_nulls_node *n; | |
294 | struct io_worker *worker; | |
295 | ||
83d6c393 JA |
296 | /* |
297 | * Iterate free_list and see if we can find an idle worker to | |
298 | * activate. If a given worker is on the free_list but in the process | |
299 | * of exiting, keep trying. | |
300 | */ | |
751eedc4 | 301 | hlist_nulls_for_each_entry_rcu(worker, n, &acct->free_list, nulls_node) { |
83d6c393 JA |
302 | if (!io_worker_get(worker)) |
303 | continue; | |
22f7fb80 JA |
304 | /* |
305 | * If the worker is already running, it's either already | |
306 | * starting work or finishing work. In either case, if it does | |
307 | * to go sleep, we'll kick off a new task for this work anyway. | |
308 | */ | |
309 | wake_up_process(worker->task); | |
c5def4ab | 310 | io_worker_release(worker); |
22f7fb80 | 311 | return true; |
c5def4ab JA |
312 | } |
313 | ||
314 | return false; | |
315 | } | |
316 | ||
317 | /* | |
318 | * We need a worker. If we find a free one, we're good. If not, and we're | |
685fe7fe | 319 | * below the max number of workers, create one. |
c5def4ab | 320 | */ |
eb47943f | 321 | static bool io_wq_create_worker(struct io_wq *wq, struct io_wq_acct *acct) |
c5def4ab | 322 | { |
c5def4ab JA |
323 | /* |
324 | * Most likely an attempt to queue unbounded work on an io_wq that | |
325 | * wasn't setup with any unbounded workers. | |
326 | */ | |
e6ab8991 PB |
327 | if (unlikely(!acct->max_workers)) |
328 | pr_warn_once("io-wq is not configured for unbound workers"); | |
c5def4ab | 329 | |
751eedc4 | 330 | raw_spin_lock(&acct->workers_lock); |
bc369921 | 331 | if (acct->nr_workers >= acct->max_workers) { |
751eedc4 | 332 | raw_spin_unlock(&acct->workers_lock); |
7a842fb5 | 333 | return true; |
94ffb0a2 | 334 | } |
7a842fb5 | 335 | acct->nr_workers++; |
751eedc4 | 336 | raw_spin_unlock(&acct->workers_lock); |
7a842fb5 | 337 | atomic_inc(&acct->nr_running); |
eb47943f | 338 | atomic_inc(&wq->worker_refs); |
3d3bafd3 | 339 | return create_io_worker(wq, acct); |
c5def4ab JA |
340 | } |
341 | ||
eb47943f | 342 | static void io_wq_inc_running(struct io_worker *worker) |
c5def4ab | 343 | { |
dfd63baf | 344 | struct io_wq_acct *acct = io_wq_get_acct(worker); |
c5def4ab JA |
345 | |
346 | atomic_inc(&acct->nr_running); | |
347 | } | |
348 | ||
685fe7fe JA |
349 | static void create_worker_cb(struct callback_head *cb) |
350 | { | |
d3e9f732 | 351 | struct io_worker *worker; |
685fe7fe | 352 | struct io_wq *wq; |
eb47943f | 353 | |
dfd63baf | 354 | struct io_wq_acct *acct; |
05c5f4ee | 355 | bool do_create = false; |
685fe7fe | 356 | |
d3e9f732 | 357 | worker = container_of(cb, struct io_worker, create_work); |
eb47943f | 358 | wq = worker->wq; |
3d3bafd3 | 359 | acct = worker->acct; |
751eedc4 | 360 | raw_spin_lock(&acct->workers_lock); |
eb47943f | 361 | |
49e7f0c7 | 362 | if (acct->nr_workers < acct->max_workers) { |
21698274 | 363 | acct->nr_workers++; |
49e7f0c7 HX |
364 | do_create = true; |
365 | } | |
751eedc4 | 366 | raw_spin_unlock(&acct->workers_lock); |
49e7f0c7 | 367 | if (do_create) { |
3d3bafd3 | 368 | create_io_worker(wq, acct); |
49e7f0c7 HX |
369 | } else { |
370 | atomic_dec(&acct->nr_running); | |
371 | io_worker_ref_put(wq); | |
372 | } | |
d3e9f732 JA |
373 | clear_bit_unlock(0, &worker->create_state); |
374 | io_worker_release(worker); | |
685fe7fe JA |
375 | } |
376 | ||
3146cba9 | 377 | static bool io_queue_worker_create(struct io_worker *worker, |
dfd63baf | 378 | struct io_wq_acct *acct, |
3146cba9 | 379 | task_work_func_t func) |
685fe7fe | 380 | { |
eb47943f | 381 | struct io_wq *wq = worker->wq; |
685fe7fe JA |
382 | |
383 | /* raced with exit, just ignore create call */ | |
384 | if (test_bit(IO_WQ_BIT_EXIT, &wq->state)) | |
385 | goto fail; | |
d3e9f732 JA |
386 | if (!io_worker_get(worker)) |
387 | goto fail; | |
388 | /* | |
389 | * create_state manages ownership of create_work/index. We should | |
390 | * only need one entry per worker, as the worker going to sleep | |
391 | * will trigger the condition, and waking will clear it once it | |
392 | * runs the task_work. | |
393 | */ | |
394 | if (test_bit(0, &worker->create_state) || | |
395 | test_and_set_bit_lock(0, &worker->create_state)) | |
396 | goto fail_release; | |
685fe7fe | 397 | |
71a85387 | 398 | atomic_inc(&wq->worker_refs); |
3146cba9 | 399 | init_task_work(&worker->create_work, func); |
71a85387 JA |
400 | if (!task_work_add(wq->task, &worker->create_work, TWA_SIGNAL)) { |
401 | /* | |
402 | * EXIT may have been set after checking it above, check after | |
403 | * adding the task_work and remove any creation item if it is | |
404 | * now set. wq exit does that too, but we can have added this | |
405 | * work item after we canceled in io_wq_exit_workers(). | |
406 | */ | |
407 | if (test_bit(IO_WQ_BIT_EXIT, &wq->state)) | |
408 | io_wq_cancel_tw_create(wq); | |
409 | io_worker_ref_put(wq); | |
3146cba9 | 410 | return true; |
71a85387 JA |
411 | } |
412 | io_worker_ref_put(wq); | |
d3e9f732 JA |
413 | clear_bit_unlock(0, &worker->create_state); |
414 | fail_release: | |
415 | io_worker_release(worker); | |
685fe7fe JA |
416 | fail: |
417 | atomic_dec(&acct->nr_running); | |
418 | io_worker_ref_put(wq); | |
3146cba9 | 419 | return false; |
685fe7fe JA |
420 | } |
421 | ||
0b2b066f JA |
422 | /* Defer if current and next work are both hashed to the same chain */ |
423 | static bool io_wq_hash_defer(struct io_wq_work *work, struct io_wq_acct *acct) | |
424 | { | |
425 | unsigned int hash, work_flags; | |
426 | struct io_wq_work *next; | |
427 | ||
428 | lockdep_assert_held(&acct->lock); | |
429 | ||
430 | work_flags = atomic_read(&work->flags); | |
431 | if (!__io_wq_is_hashed(work_flags)) | |
432 | return false; | |
433 | ||
434 | /* should not happen, io_acct_run_queue() said we had work */ | |
435 | if (wq_list_empty(&acct->work_list)) | |
436 | return true; | |
437 | ||
438 | hash = __io_get_work_hash(work_flags); | |
439 | next = container_of(acct->work_list.first, struct io_wq_work, list); | |
440 | work_flags = atomic_read(&next->flags); | |
441 | if (!__io_wq_is_hashed(work_flags)) | |
442 | return false; | |
443 | return hash == __io_get_work_hash(work_flags); | |
444 | } | |
445 | ||
eb47943f | 446 | static void io_wq_dec_running(struct io_worker *worker) |
c5def4ab | 447 | { |
dfd63baf | 448 | struct io_wq_acct *acct = io_wq_get_acct(worker); |
eb47943f | 449 | struct io_wq *wq = worker->wq; |
c5def4ab | 450 | |
8a565304 | 451 | if (!test_bit(IO_WORKER_F_UP, &worker->flags)) |
685fe7fe JA |
452 | return; |
453 | ||
42abc95f HX |
454 | if (!atomic_dec_and_test(&acct->nr_running)) |
455 | return; | |
8343cae3 JA |
456 | if (!worker->cur_work) |
457 | return; | |
e13fb1fe | 458 | if (!io_acct_run_queue(acct)) |
42abc95f | 459 | return; |
0b2b066f JA |
460 | if (io_wq_hash_defer(worker->cur_work, acct)) { |
461 | raw_spin_unlock(&acct->lock); | |
462 | return; | |
463 | } | |
42abc95f | 464 | |
de36a15f | 465 | raw_spin_unlock(&acct->lock); |
42abc95f | 466 | atomic_inc(&acct->nr_running); |
eb47943f | 467 | atomic_inc(&wq->worker_refs); |
42abc95f | 468 | io_queue_worker_create(worker, acct, create_worker_cb); |
c5def4ab JA |
469 | } |
470 | ||
771b53d0 JA |
471 | /* |
472 | * Worker will start processing some work. Move it to the busy list, if | |
473 | * it's currently on the freelist | |
474 | */ | |
751eedc4 | 475 | static void __io_worker_busy(struct io_wq_acct *acct, struct io_worker *worker) |
771b53d0 | 476 | { |
8a565304 BL |
477 | if (test_bit(IO_WORKER_F_FREE, &worker->flags)) { |
478 | clear_bit(IO_WORKER_F_FREE, &worker->flags); | |
751eedc4 | 479 | raw_spin_lock(&acct->workers_lock); |
771b53d0 | 480 | hlist_nulls_del_init_rcu(&worker->nulls_node); |
751eedc4 | 481 | raw_spin_unlock(&acct->workers_lock); |
771b53d0 | 482 | } |
771b53d0 JA |
483 | } |
484 | ||
485 | /* | |
07d99096 | 486 | * No work, worker going to sleep. Move to freelist. |
771b53d0 | 487 | */ |
751eedc4 MK |
488 | static void __io_worker_idle(struct io_wq_acct *acct, struct io_worker *worker) |
489 | __must_hold(acct->workers_lock) | |
771b53d0 | 490 | { |
8a565304 BL |
491 | if (!test_bit(IO_WORKER_F_FREE, &worker->flags)) { |
492 | set_bit(IO_WORKER_F_FREE, &worker->flags); | |
751eedc4 | 493 | hlist_nulls_add_head_rcu(&worker->nulls_node, &acct->free_list); |
771b53d0 | 494 | } |
771b53d0 JA |
495 | } |
496 | ||
eb47943f | 497 | static bool io_wait_on_hash(struct io_wq *wq, unsigned int hash) |
e941894e | 498 | { |
d3e3c102 | 499 | bool ret = false; |
e941894e | 500 | |
08bdbd39 | 501 | spin_lock_irq(&wq->hash->wait.lock); |
eb47943f GKB |
502 | if (list_empty(&wq->wait.entry)) { |
503 | __add_wait_queue(&wq->hash->wait, &wq->wait); | |
e941894e JA |
504 | if (!test_bit(hash, &wq->hash->map)) { |
505 | __set_current_state(TASK_RUNNING); | |
eb47943f | 506 | list_del_init(&wq->wait.entry); |
d3e3c102 | 507 | ret = true; |
e941894e JA |
508 | } |
509 | } | |
08bdbd39 | 510 | spin_unlock_irq(&wq->hash->wait.lock); |
d3e3c102 | 511 | return ret; |
e941894e JA |
512 | } |
513 | ||
dfd63baf | 514 | static struct io_wq_work *io_get_next_work(struct io_wq_acct *acct, |
7d568502 | 515 | struct io_wq *wq) |
42abc95f | 516 | __must_hold(acct->lock) |
771b53d0 | 517 | { |
6206f0e1 | 518 | struct io_wq_work_node *node, *prev; |
86f3cd1b | 519 | struct io_wq_work *work, *tail; |
e941894e | 520 | unsigned int stall_hash = -1U; |
771b53d0 | 521 | |
f95dc207 | 522 | wq_list_for_each(node, prev, &acct->work_list) { |
6ee78354 | 523 | unsigned int work_flags; |
e941894e JA |
524 | unsigned int hash; |
525 | ||
6206f0e1 JA |
526 | work = container_of(node, struct io_wq_work, list); |
527 | ||
771b53d0 | 528 | /* not hashed, can run anytime */ |
6ee78354 MK |
529 | work_flags = atomic_read(&work->flags); |
530 | if (!__io_wq_is_hashed(work_flags)) { | |
f95dc207 | 531 | wq_list_del(&acct->work_list, node, prev); |
771b53d0 JA |
532 | return work; |
533 | } | |
534 | ||
6ee78354 | 535 | hash = __io_get_work_hash(work_flags); |
e941894e | 536 | /* all items with this hash lie in [work, tail] */ |
eb47943f | 537 | tail = wq->hash_tail[hash]; |
e941894e JA |
538 | |
539 | /* hashed, can run if not already running */ | |
eb47943f GKB |
540 | if (!test_and_set_bit(hash, &wq->hash->map)) { |
541 | wq->hash_tail[hash] = NULL; | |
f95dc207 | 542 | wq_list_cut(&acct->work_list, &tail->list, prev); |
771b53d0 JA |
543 | return work; |
544 | } | |
e941894e JA |
545 | if (stall_hash == -1U) |
546 | stall_hash = hash; | |
547 | /* fast forward to a next hash, for-each will fix up @prev */ | |
548 | node = &tail->list; | |
549 | } | |
550 | ||
551 | if (stall_hash != -1U) { | |
d3e3c102 JA |
552 | bool unstalled; |
553 | ||
0242f642 JA |
554 | /* |
555 | * Set this before dropping the lock to avoid racing with new | |
556 | * work being added and clearing the stalled bit. | |
557 | */ | |
f95dc207 | 558 | set_bit(IO_ACCT_STALLED_BIT, &acct->flags); |
42abc95f | 559 | raw_spin_unlock(&acct->lock); |
eb47943f | 560 | unstalled = io_wait_on_hash(wq, stall_hash); |
42abc95f | 561 | raw_spin_lock(&acct->lock); |
d3e3c102 JA |
562 | if (unstalled) { |
563 | clear_bit(IO_ACCT_STALLED_BIT, &acct->flags); | |
eb47943f GKB |
564 | if (wq_has_sleeper(&wq->hash->wait)) |
565 | wake_up(&wq->hash->wait); | |
d3e3c102 | 566 | } |
771b53d0 JA |
567 | } |
568 | ||
569 | return NULL; | |
570 | } | |
571 | ||
dc026a73 PB |
572 | static void io_assign_current_work(struct io_worker *worker, |
573 | struct io_wq_work *work) | |
574 | { | |
d78298e7 | 575 | if (work) { |
024f15e0 | 576 | io_run_task_work(); |
d78298e7 PB |
577 | cond_resched(); |
578 | } | |
dc026a73 | 579 | |
081b5820 | 580 | raw_spin_lock(&worker->lock); |
dc026a73 | 581 | worker->cur_work = work; |
081b5820 | 582 | raw_spin_unlock(&worker->lock); |
dc026a73 PB |
583 | } |
584 | ||
de36a15f JA |
585 | /* |
586 | * Called with acct->lock held, drops it before returning | |
587 | */ | |
588 | static void io_worker_handle_work(struct io_wq_acct *acct, | |
589 | struct io_worker *worker) | |
590 | __releases(&acct->lock) | |
771b53d0 | 591 | { |
eb47943f | 592 | struct io_wq *wq = worker->wq; |
c60eb049 | 593 | bool do_kill = test_bit(IO_WQ_BIT_EXIT, &wq->state); |
771b53d0 JA |
594 | |
595 | do { | |
86f3cd1b | 596 | struct io_wq_work *work; |
73031f76 | 597 | |
771b53d0 JA |
598 | /* |
599 | * If we got some work, mark us as busy. If we didn't, but | |
600 | * the list isn't empty, it means we stalled on hashed work. | |
601 | * Mark us stalled so we don't keep looking for work when we | |
602 | * can't make progress, any work completion or insertion will | |
603 | * clear the stalled flag. | |
604 | */ | |
7d568502 | 605 | work = io_get_next_work(acct, wq); |
361aee45 | 606 | if (work) { |
361aee45 JA |
607 | /* |
608 | * Make sure cancelation can find this, even before | |
609 | * it becomes the active work. That avoids a window | |
610 | * where the work has been removed from our general | |
611 | * work list, but isn't yet discoverable as the | |
612 | * current work item for this worker. | |
613 | */ | |
614 | raw_spin_lock(&worker->lock); | |
24c3fc5c | 615 | worker->cur_work = work; |
361aee45 | 616 | raw_spin_unlock(&worker->lock); |
42abc95f | 617 | } |
068c27e3 GKB |
618 | |
619 | raw_spin_unlock(&acct->lock); | |
620 | ||
621 | if (!work) | |
622 | break; | |
623 | ||
751eedc4 | 624 | __io_worker_busy(acct, worker); |
068c27e3 | 625 | |
58e39319 | 626 | io_assign_current_work(worker, work); |
e941894e | 627 | __set_current_state(TASK_RUNNING); |
36c2f922 | 628 | |
dc026a73 PB |
629 | /* handle a whole dependent link */ |
630 | do { | |
5280f7e5 | 631 | struct io_wq_work *next_hashed, *linked; |
6ee78354 | 632 | unsigned int work_flags = atomic_read(&work->flags); |
486ba4d8 MK |
633 | unsigned int hash = __io_wq_is_hashed(work_flags) |
634 | ? __io_get_work_hash(work_flags) | |
635 | : -1U; | |
dc026a73 | 636 | |
86f3cd1b | 637 | next_hashed = wq_next_work(work); |
c60eb049 | 638 | |
3474d1b9 | 639 | if (do_kill && |
6ee78354 | 640 | (work_flags & IO_WQ_WORK_UNBOUND)) |
3474d1b9 | 641 | atomic_or(IO_WQ_WORK_CANCEL, &work->flags); |
9fe99eed | 642 | io_wq_submit_work(work); |
5280f7e5 | 643 | io_assign_current_work(worker, NULL); |
dc026a73 | 644 | |
9fe99eed | 645 | linked = io_wq_free_work(work); |
86f3cd1b PB |
646 | work = next_hashed; |
647 | if (!work && linked && !io_wq_is_hashed(linked)) { | |
648 | work = linked; | |
649 | linked = NULL; | |
650 | } | |
651 | io_assign_current_work(worker, work); | |
86f3cd1b | 652 | if (linked) |
eb47943f | 653 | io_wq_enqueue(wq, linked); |
86f3cd1b PB |
654 | |
655 | if (hash != -1U && !next_hashed) { | |
d3e3c102 JA |
656 | /* serialize hash clear with wake_up() */ |
657 | spin_lock_irq(&wq->hash->wait.lock); | |
e941894e | 658 | clear_bit(hash, &wq->hash->map); |
f95dc207 | 659 | clear_bit(IO_ACCT_STALLED_BIT, &acct->flags); |
d3e3c102 | 660 | spin_unlock_irq(&wq->hash->wait.lock); |
e941894e JA |
661 | if (wq_has_sleeper(&wq->hash->wait)) |
662 | wake_up(&wq->hash->wait); | |
7d723065 | 663 | } |
58e39319 | 664 | } while (work); |
de36a15f JA |
665 | |
666 | if (!__io_acct_run_queue(acct)) | |
667 | break; | |
668 | raw_spin_lock(&acct->lock); | |
771b53d0 JA |
669 | } while (1); |
670 | } | |
671 | ||
eb47943f | 672 | static int io_wq_worker(void *data) |
771b53d0 JA |
673 | { |
674 | struct io_worker *worker = data; | |
dfd63baf | 675 | struct io_wq_acct *acct = io_wq_get_acct(worker); |
eb47943f | 676 | struct io_wq *wq = worker->wq; |
01e68ce0 | 677 | bool exit_mask = false, last_timeout = false; |
3a3f61ce | 678 | char buf[TASK_COMM_LEN] = {}; |
771b53d0 | 679 | |
8a565304 BL |
680 | set_mask_bits(&worker->flags, 0, |
681 | BIT(IO_WORKER_F_UP) | BIT(IO_WORKER_F_RUNNING)); | |
46fe18b1 | 682 | |
685fe7fe | 683 | snprintf(buf, sizeof(buf), "iou-wrk-%d", wq->task->pid); |
46fe18b1 | 684 | set_task_comm(current, buf); |
771b53d0 JA |
685 | |
686 | while (!test_bit(IO_WQ_BIT_EXIT, &wq->state)) { | |
16efa4fc JA |
687 | long ret; |
688 | ||
506d95ff | 689 | set_current_state(TASK_INTERRUPTIBLE); |
de36a15f JA |
690 | |
691 | /* | |
692 | * If we have work to do, io_acct_run_queue() returns with | |
693 | * the acct->lock held. If not, it will drop it. | |
694 | */ | |
e13fb1fe | 695 | while (io_acct_run_queue(acct)) |
de36a15f | 696 | io_worker_handle_work(acct, worker); |
e13fb1fe | 697 | |
751eedc4 | 698 | raw_spin_lock(&acct->workers_lock); |
01e68ce0 JA |
699 | /* |
700 | * Last sleep timed out. Exit if we're not the last worker, | |
701 | * or if someone modified our affinity. | |
702 | */ | |
703 | if (last_timeout && (exit_mask || acct->nr_workers > 1)) { | |
767a65e9 | 704 | acct->nr_workers--; |
751eedc4 | 705 | raw_spin_unlock(&acct->workers_lock); |
05c5f4ee JA |
706 | __set_current_state(TASK_RUNNING); |
707 | break; | |
708 | } | |
709 | last_timeout = false; | |
751eedc4 MK |
710 | __io_worker_idle(acct, worker); |
711 | raw_spin_unlock(&acct->workers_lock); | |
024f15e0 | 712 | if (io_run_task_work()) |
00ddff43 | 713 | continue; |
16efa4fc | 714 | ret = schedule_timeout(WORKER_IDLE_TIMEOUT); |
dbe1bdbb JA |
715 | if (signal_pending(current)) { |
716 | struct ksignal ksig; | |
717 | ||
718 | if (!get_signal(&ksig)) | |
719 | continue; | |
78f8876c | 720 | break; |
dbe1bdbb | 721 | } |
01e68ce0 JA |
722 | if (!ret) { |
723 | last_timeout = true; | |
724 | exit_mask = !cpumask_test_cpu(raw_smp_processor_id(), | |
eb47943f | 725 | wq->cpu_mask); |
01e68ce0 | 726 | } |
771b53d0 JA |
727 | } |
728 | ||
de36a15f JA |
729 | if (test_bit(IO_WQ_BIT_EXIT, &wq->state) && io_acct_run_queue(acct)) |
730 | io_worker_handle_work(acct, worker); | |
771b53d0 JA |
731 | |
732 | io_worker_exit(worker); | |
733 | return 0; | |
734 | } | |
735 | ||
771b53d0 JA |
736 | /* |
737 | * Called when a worker is scheduled in. Mark us as currently running. | |
738 | */ | |
739 | void io_wq_worker_running(struct task_struct *tsk) | |
740 | { | |
e32cf5df | 741 | struct io_worker *worker = tsk->worker_private; |
771b53d0 | 742 | |
3bfe6106 JA |
743 | if (!worker) |
744 | return; | |
8a565304 | 745 | if (!test_bit(IO_WORKER_F_UP, &worker->flags)) |
771b53d0 | 746 | return; |
8a565304 | 747 | if (test_bit(IO_WORKER_F_RUNNING, &worker->flags)) |
771b53d0 | 748 | return; |
8a565304 | 749 | set_bit(IO_WORKER_F_RUNNING, &worker->flags); |
eb47943f | 750 | io_wq_inc_running(worker); |
771b53d0 JA |
751 | } |
752 | ||
753 | /* | |
754 | * Called when worker is going to sleep. If there are no workers currently | |
685fe7fe | 755 | * running and we have work pending, wake up a free one or create a new one. |
771b53d0 JA |
756 | */ |
757 | void io_wq_worker_sleeping(struct task_struct *tsk) | |
758 | { | |
e32cf5df | 759 | struct io_worker *worker = tsk->worker_private; |
771b53d0 | 760 | |
3bfe6106 JA |
761 | if (!worker) |
762 | return; | |
8a565304 | 763 | if (!test_bit(IO_WORKER_F_UP, &worker->flags)) |
771b53d0 | 764 | return; |
8a565304 | 765 | if (!test_bit(IO_WORKER_F_RUNNING, &worker->flags)) |
771b53d0 JA |
766 | return; |
767 | ||
8a565304 | 768 | clear_bit(IO_WORKER_F_RUNNING, &worker->flags); |
eb47943f | 769 | io_wq_dec_running(worker); |
771b53d0 JA |
770 | } |
771 | ||
751eedc4 | 772 | static void io_init_new_worker(struct io_wq *wq, struct io_wq_acct *acct, struct io_worker *worker, |
3146cba9 JA |
773 | struct task_struct *tsk) |
774 | { | |
e32cf5df | 775 | tsk->worker_private = worker; |
3146cba9 | 776 | worker->task = tsk; |
eb47943f | 777 | set_cpus_allowed_ptr(tsk, wq->cpu_mask); |
3146cba9 | 778 | |
751eedc4 MK |
779 | raw_spin_lock(&acct->workers_lock); |
780 | hlist_nulls_add_head_rcu(&worker->nulls_node, &acct->free_list); | |
781 | list_add_tail_rcu(&worker->all_list, &acct->all_list); | |
8a565304 | 782 | set_bit(IO_WORKER_F_FREE, &worker->flags); |
751eedc4 | 783 | raw_spin_unlock(&acct->workers_lock); |
3146cba9 JA |
784 | wake_up_new_task(tsk); |
785 | } | |
786 | ||
787 | static bool io_wq_work_match_all(struct io_wq_work *work, void *data) | |
788 | { | |
789 | return true; | |
790 | } | |
791 | ||
0453aad6 | 792 | static inline bool io_should_retry_thread(struct io_worker *worker, long err) |
3146cba9 | 793 | { |
a226abcd JA |
794 | /* |
795 | * Prevent perpetual task_work retry, if the task (or its group) is | |
796 | * exiting. | |
797 | */ | |
798 | if (fatal_signal_pending(current)) | |
799 | return false; | |
0453aad6 PB |
800 | if (worker->init_retries++ >= WORKER_INIT_LIMIT) |
801 | return false; | |
a226abcd | 802 | |
3146cba9 JA |
803 | switch (err) { |
804 | case -EAGAIN: | |
805 | case -ERESTARTSYS: | |
806 | case -ERESTARTNOINTR: | |
807 | case -ERESTARTNOHAND: | |
808 | return true; | |
809 | default: | |
810 | return false; | |
811 | } | |
812 | } | |
813 | ||
13918315 US |
814 | static void queue_create_worker_retry(struct io_worker *worker) |
815 | { | |
816 | /* | |
817 | * We only bother retrying because there's a chance that the | |
818 | * failure to create a worker is due to some temporary condition | |
819 | * in the forking task (e.g. outstanding signal); give the task | |
820 | * some time to clear that condition. | |
821 | */ | |
822 | schedule_delayed_work(&worker->work, | |
823 | msecs_to_jiffies(worker->init_retries * 5)); | |
824 | } | |
825 | ||
3146cba9 JA |
826 | static void create_worker_cont(struct callback_head *cb) |
827 | { | |
828 | struct io_worker *worker; | |
829 | struct task_struct *tsk; | |
eb47943f | 830 | struct io_wq *wq; |
751eedc4 | 831 | struct io_wq_acct *acct; |
3146cba9 JA |
832 | |
833 | worker = container_of(cb, struct io_worker, create_work); | |
834 | clear_bit_unlock(0, &worker->create_state); | |
eb47943f | 835 | wq = worker->wq; |
751eedc4 | 836 | acct = io_wq_get_acct(worker); |
eb47943f | 837 | tsk = create_io_thread(io_wq_worker, worker, NUMA_NO_NODE); |
3146cba9 | 838 | if (!IS_ERR(tsk)) { |
751eedc4 | 839 | io_init_new_worker(wq, acct, worker, tsk); |
3146cba9 JA |
840 | io_worker_release(worker); |
841 | return; | |
0453aad6 | 842 | } else if (!io_should_retry_thread(worker, PTR_ERR(tsk))) { |
3146cba9 | 843 | atomic_dec(&acct->nr_running); |
751eedc4 | 844 | raw_spin_lock(&acct->workers_lock); |
3146cba9 JA |
845 | acct->nr_workers--; |
846 | if (!acct->nr_workers) { | |
847 | struct io_cb_cancel_data match = { | |
848 | .fn = io_wq_work_match_all, | |
849 | .cancel_all = true, | |
850 | }; | |
851 | ||
751eedc4 | 852 | raw_spin_unlock(&acct->workers_lock); |
eb47943f | 853 | while (io_acct_cancel_pending_work(wq, acct, &match)) |
42abc95f HX |
854 | ; |
855 | } else { | |
751eedc4 | 856 | raw_spin_unlock(&acct->workers_lock); |
3146cba9 | 857 | } |
eb47943f | 858 | io_worker_ref_put(wq); |
66e70be7 | 859 | kfree(worker); |
3146cba9 JA |
860 | return; |
861 | } | |
862 | ||
863 | /* re-create attempts grab a new worker ref, drop the existing one */ | |
864 | io_worker_release(worker); | |
13918315 | 865 | queue_create_worker_retry(worker); |
3146cba9 JA |
866 | } |
867 | ||
868 | static void io_workqueue_create(struct work_struct *work) | |
869 | { | |
13918315 US |
870 | struct io_worker *worker = container_of(work, struct io_worker, |
871 | work.work); | |
dfd63baf | 872 | struct io_wq_acct *acct = io_wq_get_acct(worker); |
3146cba9 | 873 | |
71e1cef2 | 874 | if (!io_queue_worker_create(worker, acct, create_worker_cont)) |
66e70be7 | 875 | kfree(worker); |
3146cba9 JA |
876 | } |
877 | ||
3d3bafd3 | 878 | static bool create_io_worker(struct io_wq *wq, struct io_wq_acct *acct) |
3bfe6106 JA |
879 | { |
880 | struct io_worker *worker; | |
46fe18b1 | 881 | struct task_struct *tsk; |
3bfe6106 | 882 | |
8b3e78b5 JA |
883 | __set_current_state(TASK_RUNNING); |
884 | ||
da64d6db | 885 | worker = kzalloc(sizeof(*worker), GFP_KERNEL); |
3146cba9 | 886 | if (!worker) { |
685fe7fe JA |
887 | fail: |
888 | atomic_dec(&acct->nr_running); | |
751eedc4 | 889 | raw_spin_lock(&acct->workers_lock); |
3d4e4fac | 890 | acct->nr_workers--; |
751eedc4 | 891 | raw_spin_unlock(&acct->workers_lock); |
685fe7fe | 892 | io_worker_ref_put(wq); |
3146cba9 | 893 | return false; |
3bfe6106 | 894 | } |
46fe18b1 | 895 | |
3146cba9 | 896 | refcount_set(&worker->ref, 1); |
eb47943f | 897 | worker->wq = wq; |
3d3bafd3 | 898 | worker->acct = acct; |
081b5820 | 899 | raw_spin_lock_init(&worker->lock); |
3146cba9 | 900 | init_completion(&worker->ref_done); |
46fe18b1 | 901 | |
eb47943f | 902 | tsk = create_io_thread(io_wq_worker, worker, NUMA_NO_NODE); |
3146cba9 | 903 | if (!IS_ERR(tsk)) { |
751eedc4 | 904 | io_init_new_worker(wq, acct, worker, tsk); |
0453aad6 | 905 | } else if (!io_should_retry_thread(worker, PTR_ERR(tsk))) { |
66e70be7 | 906 | kfree(worker); |
3146cba9 JA |
907 | goto fail; |
908 | } else { | |
13918315 US |
909 | INIT_DELAYED_WORK(&worker->work, io_workqueue_create); |
910 | queue_create_worker_retry(worker); | |
3146cba9 JA |
911 | } |
912 | ||
913 | return true; | |
771b53d0 JA |
914 | } |
915 | ||
c4068bf8 HD |
916 | /* |
917 | * Iterate the passed in list and call the specific function for each | |
918 | * worker that isn't exiting | |
919 | */ | |
751eedc4 MK |
920 | static bool io_acct_for_each_worker(struct io_wq_acct *acct, |
921 | bool (*func)(struct io_worker *, void *), | |
922 | void *data) | |
c4068bf8 HD |
923 | { |
924 | struct io_worker *worker; | |
925 | bool ret = false; | |
926 | ||
751eedc4 | 927 | list_for_each_entry_rcu(worker, &acct->all_list, all_list) { |
c4068bf8 HD |
928 | if (io_worker_get(worker)) { |
929 | /* no task if node is/was offline */ | |
930 | if (worker->task) | |
931 | ret = func(worker, data); | |
932 | io_worker_release(worker); | |
933 | if (ret) | |
934 | break; | |
935 | } | |
936 | } | |
937 | ||
938 | return ret; | |
939 | } | |
940 | ||
751eedc4 MK |
941 | static bool io_wq_for_each_worker(struct io_wq *wq, |
942 | bool (*func)(struct io_worker *, void *), | |
943 | void *data) | |
944 | { | |
945 | for (int i = 0; i < IO_WQ_ACCT_NR; i++) { | |
946 | if (!io_acct_for_each_worker(&wq->acct[i], func, data)) | |
947 | return false; | |
948 | } | |
949 | ||
950 | return true; | |
951 | } | |
952 | ||
c4068bf8 HD |
953 | static bool io_wq_worker_wake(struct io_worker *worker, void *data) |
954 | { | |
6cf5862e | 955 | __set_notify_signal(worker->task); |
c4068bf8 HD |
956 | wake_up_process(worker->task); |
957 | return false; | |
958 | } | |
959 | ||
eb47943f | 960 | static void io_run_cancel(struct io_wq_work *work, struct io_wq *wq) |
fc04c39b PB |
961 | { |
962 | do { | |
3474d1b9 | 963 | atomic_or(IO_WQ_WORK_CANCEL, &work->flags); |
9fe99eed CSM |
964 | io_wq_submit_work(work); |
965 | work = io_wq_free_work(work); | |
fc04c39b PB |
966 | } while (work); |
967 | } | |
968 | ||
6ee78354 MK |
969 | static void io_wq_insert_work(struct io_wq *wq, struct io_wq_acct *acct, |
970 | struct io_wq_work *work, unsigned int work_flags) | |
86f3cd1b PB |
971 | { |
972 | unsigned int hash; | |
973 | struct io_wq_work *tail; | |
974 | ||
6ee78354 | 975 | if (!__io_wq_is_hashed(work_flags)) { |
86f3cd1b | 976 | append: |
f95dc207 | 977 | wq_list_add_tail(&work->list, &acct->work_list); |
86f3cd1b PB |
978 | return; |
979 | } | |
980 | ||
6ee78354 | 981 | hash = __io_get_work_hash(work_flags); |
eb47943f GKB |
982 | tail = wq->hash_tail[hash]; |
983 | wq->hash_tail[hash] = work; | |
86f3cd1b PB |
984 | if (!tail) |
985 | goto append; | |
986 | ||
f95dc207 | 987 | wq_list_add_after(&work->list, &tail->list, &acct->work_list); |
86f3cd1b PB |
988 | } |
989 | ||
713b9825 PB |
990 | static bool io_wq_work_match_item(struct io_wq_work *work, void *data) |
991 | { | |
992 | return work == data; | |
993 | } | |
994 | ||
eb47943f | 995 | void io_wq_enqueue(struct io_wq *wq, struct io_wq_work *work) |
771b53d0 | 996 | { |
3474d1b9 | 997 | unsigned int work_flags = atomic_read(&work->flags); |
6ee78354 | 998 | struct io_wq_acct *acct = io_work_get_acct(wq, work_flags); |
91215f70 SH |
999 | struct io_cb_cancel_data match = { |
1000 | .fn = io_wq_work_match_item, | |
1001 | .data = work, | |
1002 | .cancel_all = false, | |
1003 | }; | |
94ffb0a2 | 1004 | bool do_create; |
771b53d0 | 1005 | |
991468dc JA |
1006 | /* |
1007 | * If io-wq is exiting for this task, or if the request has explicitly | |
1008 | * been marked as one that should not get executed, cancel it here. | |
1009 | */ | |
eb47943f | 1010 | if (test_bit(IO_WQ_BIT_EXIT, &wq->state) || |
3474d1b9 | 1011 | (work_flags & IO_WQ_WORK_CANCEL)) { |
eb47943f | 1012 | io_run_cancel(work, wq); |
4fb6ac32 JA |
1013 | return; |
1014 | } | |
1015 | ||
42abc95f | 1016 | raw_spin_lock(&acct->lock); |
6ee78354 | 1017 | io_wq_insert_work(wq, acct, work, work_flags); |
f95dc207 | 1018 | clear_bit(IO_ACCT_STALLED_BIT, &acct->flags); |
42abc95f | 1019 | raw_spin_unlock(&acct->lock); |
94ffb0a2 JA |
1020 | |
1021 | rcu_read_lock(); | |
751eedc4 | 1022 | do_create = !io_acct_activate_free_worker(acct); |
94ffb0a2 JA |
1023 | rcu_read_unlock(); |
1024 | ||
94ffb0a2 | 1025 | if (do_create && ((work_flags & IO_WQ_WORK_CONCURRENT) || |
3146cba9 JA |
1026 | !atomic_read(&acct->nr_running))) { |
1027 | bool did_create; | |
1028 | ||
eb47943f | 1029 | did_create = io_wq_create_worker(wq, acct); |
713b9825 PB |
1030 | if (likely(did_create)) |
1031 | return; | |
1032 | ||
751eedc4 | 1033 | raw_spin_lock(&acct->workers_lock); |
42abc95f | 1034 | if (acct->nr_workers) { |
751eedc4 | 1035 | raw_spin_unlock(&acct->workers_lock); |
42abc95f | 1036 | return; |
3146cba9 | 1037 | } |
751eedc4 | 1038 | raw_spin_unlock(&acct->workers_lock); |
42abc95f HX |
1039 | |
1040 | /* fatal condition, failed to create the first worker */ | |
eb47943f | 1041 | io_acct_cancel_pending_work(wq, acct, &match); |
3146cba9 | 1042 | } |
771b53d0 JA |
1043 | } |
1044 | ||
771b53d0 | 1045 | /* |
8766dd51 PB |
1046 | * Work items that hash to the same value will not be done in parallel. |
1047 | * Used to limit concurrent writes, generally hashed by inode. | |
771b53d0 | 1048 | */ |
8766dd51 | 1049 | void io_wq_hash_work(struct io_wq_work *work, void *val) |
771b53d0 | 1050 | { |
8766dd51 | 1051 | unsigned int bit; |
771b53d0 JA |
1052 | |
1053 | bit = hash_ptr(val, IO_WQ_HASH_ORDER); | |
3474d1b9 | 1054 | atomic_or(IO_WQ_WORK_HASHED | (bit << IO_WQ_HASH_SHIFT), &work->flags); |
771b53d0 JA |
1055 | } |
1056 | ||
361aee45 JA |
1057 | static bool __io_wq_worker_cancel(struct io_worker *worker, |
1058 | struct io_cb_cancel_data *match, | |
1059 | struct io_wq_work *work) | |
1060 | { | |
1061 | if (work && match->fn(work, match->data)) { | |
3474d1b9 | 1062 | atomic_or(IO_WQ_WORK_CANCEL, &work->flags); |
6cf5862e | 1063 | __set_notify_signal(worker->task); |
361aee45 JA |
1064 | return true; |
1065 | } | |
1066 | ||
1067 | return false; | |
1068 | } | |
1069 | ||
2293b419 | 1070 | static bool io_wq_worker_cancel(struct io_worker *worker, void *data) |
62755e35 | 1071 | { |
2293b419 | 1072 | struct io_cb_cancel_data *match = data; |
62755e35 JA |
1073 | |
1074 | /* | |
1075 | * Hold the lock to avoid ->cur_work going out of scope, caller | |
36c2f922 | 1076 | * may dereference the passed in work. |
62755e35 | 1077 | */ |
081b5820 | 1078 | raw_spin_lock(&worker->lock); |
24c3fc5c | 1079 | if (__io_wq_worker_cancel(worker, match, worker->cur_work)) |
4f26bda1 | 1080 | match->nr_running++; |
081b5820 | 1081 | raw_spin_unlock(&worker->lock); |
771b53d0 | 1082 | |
4f26bda1 | 1083 | return match->nr_running && !match->cancel_all; |
771b53d0 JA |
1084 | } |
1085 | ||
eb47943f | 1086 | static inline void io_wq_remove_pending(struct io_wq *wq, |
3c75635f | 1087 | struct io_wq_acct *acct, |
204361a7 PB |
1088 | struct io_wq_work *work, |
1089 | struct io_wq_work_node *prev) | |
1090 | { | |
1091 | unsigned int hash = io_get_work_hash(work); | |
1092 | struct io_wq_work *prev_work = NULL; | |
1093 | ||
eb47943f | 1094 | if (io_wq_is_hashed(work) && work == wq->hash_tail[hash]) { |
204361a7 PB |
1095 | if (prev) |
1096 | prev_work = container_of(prev, struct io_wq_work, list); | |
1097 | if (prev_work && io_get_work_hash(prev_work) == hash) | |
eb47943f | 1098 | wq->hash_tail[hash] = prev_work; |
204361a7 | 1099 | else |
eb47943f | 1100 | wq->hash_tail[hash] = NULL; |
204361a7 | 1101 | } |
f95dc207 | 1102 | wq_list_del(&acct->work_list, &work->list, prev); |
204361a7 PB |
1103 | } |
1104 | ||
eb47943f | 1105 | static bool io_acct_cancel_pending_work(struct io_wq *wq, |
dfd63baf | 1106 | struct io_wq_acct *acct, |
3146cba9 | 1107 | struct io_cb_cancel_data *match) |
771b53d0 | 1108 | { |
6206f0e1 | 1109 | struct io_wq_work_node *node, *prev; |
771b53d0 | 1110 | struct io_wq_work *work; |
771b53d0 | 1111 | |
42abc95f | 1112 | raw_spin_lock(&acct->lock); |
3146cba9 JA |
1113 | wq_list_for_each(node, prev, &acct->work_list) { |
1114 | work = container_of(node, struct io_wq_work, list); | |
1115 | if (!match->fn(work, match->data)) | |
1116 | continue; | |
3c75635f | 1117 | io_wq_remove_pending(wq, acct, work, prev); |
42abc95f | 1118 | raw_spin_unlock(&acct->lock); |
eb47943f | 1119 | io_run_cancel(work, wq); |
3146cba9 JA |
1120 | match->nr_pending++; |
1121 | /* not safe to continue after unlock */ | |
1122 | return true; | |
1123 | } | |
42abc95f | 1124 | raw_spin_unlock(&acct->lock); |
3146cba9 JA |
1125 | |
1126 | return false; | |
1127 | } | |
1128 | ||
eb47943f | 1129 | static void io_wq_cancel_pending_work(struct io_wq *wq, |
dfd63baf | 1130 | struct io_cb_cancel_data *match) |
3146cba9 JA |
1131 | { |
1132 | int i; | |
4f26bda1 | 1133 | retry: |
f95dc207 | 1134 | for (i = 0; i < IO_WQ_ACCT_NR; i++) { |
eb47943f | 1135 | struct io_wq_acct *acct = io_get_acct(wq, i == 0); |
4f26bda1 | 1136 | |
eb47943f | 1137 | if (io_acct_cancel_pending_work(wq, acct, match)) { |
3146cba9 JA |
1138 | if (match->cancel_all) |
1139 | goto retry; | |
36e4c58b | 1140 | break; |
f95dc207 | 1141 | } |
771b53d0 | 1142 | } |
f4c2665e PB |
1143 | } |
1144 | ||
751eedc4 MK |
1145 | static void io_acct_cancel_running_work(struct io_wq_acct *acct, |
1146 | struct io_cb_cancel_data *match) | |
1147 | { | |
1148 | raw_spin_lock(&acct->workers_lock); | |
1149 | io_acct_for_each_worker(acct, io_wq_worker_cancel, match); | |
1150 | raw_spin_unlock(&acct->workers_lock); | |
1151 | } | |
1152 | ||
eb47943f | 1153 | static void io_wq_cancel_running_work(struct io_wq *wq, |
f4c2665e PB |
1154 | struct io_cb_cancel_data *match) |
1155 | { | |
771b53d0 | 1156 | rcu_read_lock(); |
751eedc4 MK |
1157 | |
1158 | for (int i = 0; i < IO_WQ_ACCT_NR; i++) | |
1159 | io_acct_cancel_running_work(&wq->acct[i], match); | |
1160 | ||
771b53d0 | 1161 | rcu_read_unlock(); |
771b53d0 JA |
1162 | } |
1163 | ||
2293b419 | 1164 | enum io_wq_cancel io_wq_cancel_cb(struct io_wq *wq, work_cancel_fn *cancel, |
4f26bda1 | 1165 | void *data, bool cancel_all) |
771b53d0 | 1166 | { |
2293b419 | 1167 | struct io_cb_cancel_data match = { |
4f26bda1 PB |
1168 | .fn = cancel, |
1169 | .data = data, | |
1170 | .cancel_all = cancel_all, | |
00bcda13 | 1171 | }; |
771b53d0 | 1172 | |
f4c2665e PB |
1173 | /* |
1174 | * First check pending list, if we're lucky we can just remove it | |
1175 | * from there. CANCEL_OK means that the work is returned as-new, | |
1176 | * no completion will be posted for it. | |
efdf5184 JA |
1177 | * |
1178 | * Then check if a free (going busy) or busy worker has the work | |
f4c2665e PB |
1179 | * currently running. If we find it there, we'll return CANCEL_RUNNING |
1180 | * as an indication that we attempt to signal cancellation. The | |
1181 | * completion will run normally in this case. | |
efdf5184 | 1182 | * |
751eedc4 | 1183 | * Do both of these while holding the acct->workers_lock, to ensure that |
efdf5184 | 1184 | * we'll find a work item regardless of state. |
f4c2665e | 1185 | */ |
eb47943f | 1186 | io_wq_cancel_pending_work(wq, &match); |
da64d6db BL |
1187 | if (match.nr_pending && !match.cancel_all) |
1188 | return IO_WQ_CANCEL_OK; | |
efdf5184 | 1189 | |
eb47943f | 1190 | io_wq_cancel_running_work(wq, &match); |
da64d6db BL |
1191 | if (match.nr_running && !match.cancel_all) |
1192 | return IO_WQ_CANCEL_RUNNING; | |
f4c2665e | 1193 | |
4f26bda1 PB |
1194 | if (match.nr_running) |
1195 | return IO_WQ_CANCEL_RUNNING; | |
1196 | if (match.nr_pending) | |
1197 | return IO_WQ_CANCEL_OK; | |
f4c2665e | 1198 | return IO_WQ_CANCEL_NOTFOUND; |
771b53d0 JA |
1199 | } |
1200 | ||
eb47943f | 1201 | static int io_wq_hash_wake(struct wait_queue_entry *wait, unsigned mode, |
e941894e JA |
1202 | int sync, void *key) |
1203 | { | |
eb47943f | 1204 | struct io_wq *wq = container_of(wait, struct io_wq, wait); |
f95dc207 | 1205 | int i; |
e941894e JA |
1206 | |
1207 | list_del_init(&wait->entry); | |
1208 | ||
1209 | rcu_read_lock(); | |
f95dc207 | 1210 | for (i = 0; i < IO_WQ_ACCT_NR; i++) { |
dfd63baf | 1211 | struct io_wq_acct *acct = &wq->acct[i]; |
f95dc207 JA |
1212 | |
1213 | if (test_and_clear_bit(IO_ACCT_STALLED_BIT, &acct->flags)) | |
751eedc4 | 1214 | io_acct_activate_free_worker(acct); |
f95dc207 | 1215 | } |
e941894e | 1216 | rcu_read_unlock(); |
e941894e JA |
1217 | return 1; |
1218 | } | |
1219 | ||
576a347b | 1220 | struct io_wq *io_wq_create(unsigned bounded, struct io_wq_data *data) |
771b53d0 | 1221 | { |
da64d6db | 1222 | int ret, i; |
771b53d0 JA |
1223 | struct io_wq *wq; |
1224 | ||
e6ab8991 PB |
1225 | if (WARN_ON_ONCE(!bounded)) |
1226 | return ERR_PTR(-EINVAL); | |
e9fd9396 | 1227 | |
da64d6db | 1228 | wq = kzalloc(sizeof(struct io_wq), GFP_KERNEL); |
771b53d0 JA |
1229 | if (!wq) |
1230 | return ERR_PTR(-ENOMEM); | |
771b53d0 | 1231 | |
e941894e JA |
1232 | refcount_inc(&data->hash->refs); |
1233 | wq->hash = data->hash; | |
7d723065 | 1234 | |
43c01fbe | 1235 | ret = -ENOMEM; |
da64d6db | 1236 | |
eb47943f | 1237 | if (!alloc_cpumask_var(&wq->cpu_mask, GFP_KERNEL)) |
da64d6db | 1238 | goto err; |
84eacf17 | 1239 | cpuset_cpus_allowed(data->task, wq->cpu_mask); |
dfd63baf GKB |
1240 | wq->acct[IO_WQ_ACCT_BOUND].max_workers = bounded; |
1241 | wq->acct[IO_WQ_ACCT_UNBOUND].max_workers = | |
da64d6db | 1242 | task_rlimit(current, RLIMIT_NPROC); |
eb47943f GKB |
1243 | INIT_LIST_HEAD(&wq->wait.entry); |
1244 | wq->wait.func = io_wq_hash_wake; | |
da64d6db | 1245 | for (i = 0; i < IO_WQ_ACCT_NR; i++) { |
dfd63baf | 1246 | struct io_wq_acct *acct = &wq->acct[i]; |
da64d6db | 1247 | |
da64d6db | 1248 | atomic_set(&acct->nr_running, 0); |
751eedc4 MK |
1249 | |
1250 | raw_spin_lock_init(&acct->workers_lock); | |
1251 | INIT_HLIST_NULLS_HEAD(&acct->free_list, 0); | |
1252 | INIT_LIST_HEAD(&acct->all_list); | |
1253 | ||
da64d6db BL |
1254 | INIT_WQ_LIST(&acct->work_list); |
1255 | raw_spin_lock_init(&acct->lock); | |
771b53d0 | 1256 | } |
eb47943f | 1257 | |
685fe7fe | 1258 | wq->task = get_task_struct(data->task); |
685fe7fe JA |
1259 | atomic_set(&wq->worker_refs, 1); |
1260 | init_completion(&wq->worker_done); | |
0f8baa3c | 1261 | ret = cpuhp_state_add_instance_nocalls(io_wq_online, &wq->cpuhp_node); |
89465d92 PJ |
1262 | if (ret) { |
1263 | put_task_struct(wq->task); | |
0f8baa3c | 1264 | goto err; |
89465d92 | 1265 | } |
0f8baa3c | 1266 | |
685fe7fe | 1267 | return wq; |
b60fda60 | 1268 | err: |
dc7bbc9e | 1269 | io_wq_put_hash(data->hash); |
eb47943f | 1270 | free_cpumask_var(wq->cpu_mask); |
b60fda60 | 1271 | kfree(wq); |
771b53d0 JA |
1272 | return ERR_PTR(ret); |
1273 | } | |
1274 | ||
c80ca470 JA |
1275 | static bool io_task_work_match(struct callback_head *cb, void *data) |
1276 | { | |
d3e9f732 | 1277 | struct io_worker *worker; |
c80ca470 | 1278 | |
3b33e3f4 | 1279 | if (cb->func != create_worker_cb && cb->func != create_worker_cont) |
c80ca470 | 1280 | return false; |
d3e9f732 | 1281 | worker = container_of(cb, struct io_worker, create_work); |
eb47943f | 1282 | return worker->wq == data; |
c80ca470 JA |
1283 | } |
1284 | ||
17a91051 PB |
1285 | void io_wq_exit_start(struct io_wq *wq) |
1286 | { | |
1287 | set_bit(IO_WQ_BIT_EXIT, &wq->state); | |
1288 | } | |
1289 | ||
71a85387 | 1290 | static void io_wq_cancel_tw_create(struct io_wq *wq) |
afcc4015 | 1291 | { |
685fe7fe | 1292 | struct callback_head *cb; |
685fe7fe | 1293 | |
c80ca470 | 1294 | while ((cb = task_work_cancel_match(wq->task, io_task_work_match, wq)) != NULL) { |
d3e9f732 | 1295 | struct io_worker *worker; |
685fe7fe | 1296 | |
d3e9f732 | 1297 | worker = container_of(cb, struct io_worker, create_work); |
1d5f5ea7 | 1298 | io_worker_cancel_cb(worker); |
e6db6f93 JA |
1299 | /* |
1300 | * Only the worker continuation helper has worker allocated and | |
1301 | * hence needs freeing. | |
1302 | */ | |
1303 | if (cb->func == create_worker_cont) | |
1304 | kfree(worker); | |
685fe7fe | 1305 | } |
71a85387 JA |
1306 | } |
1307 | ||
1308 | static void io_wq_exit_workers(struct io_wq *wq) | |
1309 | { | |
71a85387 JA |
1310 | if (!wq->task) |
1311 | return; | |
1312 | ||
1313 | io_wq_cancel_tw_create(wq); | |
685fe7fe JA |
1314 | |
1315 | rcu_read_lock(); | |
eb47943f | 1316 | io_wq_for_each_worker(wq, io_wq_worker_wake, NULL); |
685fe7fe JA |
1317 | rcu_read_unlock(); |
1318 | io_worker_ref_put(wq); | |
1319 | wait_for_completion(&wq->worker_done); | |
3743c172 | 1320 | |
da64d6db | 1321 | spin_lock_irq(&wq->hash->wait.lock); |
eb47943f | 1322 | list_del_init(&wq->wait.entry); |
da64d6db BL |
1323 | spin_unlock_irq(&wq->hash->wait.lock); |
1324 | ||
685fe7fe JA |
1325 | put_task_struct(wq->task); |
1326 | wq->task = NULL; | |
afcc4015 JA |
1327 | } |
1328 | ||
4fb6ac32 | 1329 | static void io_wq_destroy(struct io_wq *wq) |
771b53d0 | 1330 | { |
da64d6db BL |
1331 | struct io_cb_cancel_data match = { |
1332 | .fn = io_wq_work_match_all, | |
1333 | .cancel_all = true, | |
1334 | }; | |
771b53d0 | 1335 | |
43c01fbe | 1336 | cpuhp_state_remove_instance_nocalls(io_wq_online, &wq->cpuhp_node); |
eb47943f GKB |
1337 | io_wq_cancel_pending_work(wq, &match); |
1338 | free_cpumask_var(wq->cpu_mask); | |
e941894e | 1339 | io_wq_put_hash(wq->hash); |
771b53d0 | 1340 | kfree(wq); |
4fb6ac32 JA |
1341 | } |
1342 | ||
afcc4015 JA |
1343 | void io_wq_put_and_exit(struct io_wq *wq) |
1344 | { | |
17a91051 PB |
1345 | WARN_ON_ONCE(!test_bit(IO_WQ_BIT_EXIT, &wq->state)); |
1346 | ||
685fe7fe | 1347 | io_wq_exit_workers(wq); |
382cb030 | 1348 | io_wq_destroy(wq); |
afcc4015 JA |
1349 | } |
1350 | ||
0e03496d JA |
1351 | struct online_data { |
1352 | unsigned int cpu; | |
1353 | bool online; | |
1354 | }; | |
1355 | ||
43c01fbe JA |
1356 | static bool io_wq_worker_affinity(struct io_worker *worker, void *data) |
1357 | { | |
0e03496d | 1358 | struct online_data *od = data; |
e0051d7d | 1359 | |
0e03496d | 1360 | if (od->online) |
eb47943f | 1361 | cpumask_set_cpu(od->cpu, worker->wq->cpu_mask); |
0e03496d | 1362 | else |
eb47943f | 1363 | cpumask_clear_cpu(od->cpu, worker->wq->cpu_mask); |
43c01fbe JA |
1364 | return false; |
1365 | } | |
1366 | ||
0e03496d | 1367 | static int __io_wq_cpu_online(struct io_wq *wq, unsigned int cpu, bool online) |
43c01fbe | 1368 | { |
0e03496d JA |
1369 | struct online_data od = { |
1370 | .cpu = cpu, | |
1371 | .online = online | |
1372 | }; | |
43c01fbe JA |
1373 | |
1374 | rcu_read_lock(); | |
eb47943f | 1375 | io_wq_for_each_worker(wq, io_wq_worker_affinity, &od); |
43c01fbe JA |
1376 | rcu_read_unlock(); |
1377 | return 0; | |
1378 | } | |
1379 | ||
0e03496d JA |
1380 | static int io_wq_cpu_online(unsigned int cpu, struct hlist_node *node) |
1381 | { | |
1382 | struct io_wq *wq = hlist_entry_safe(node, struct io_wq, cpuhp_node); | |
1383 | ||
1384 | return __io_wq_cpu_online(wq, cpu, true); | |
1385 | } | |
1386 | ||
1387 | static int io_wq_cpu_offline(unsigned int cpu, struct hlist_node *node) | |
1388 | { | |
1389 | struct io_wq *wq = hlist_entry_safe(node, struct io_wq, cpuhp_node); | |
1390 | ||
1391 | return __io_wq_cpu_online(wq, cpu, false); | |
1392 | } | |
1393 | ||
ebdfefc0 | 1394 | int io_wq_cpu_affinity(struct io_uring_task *tctx, cpumask_var_t mask) |
fe76421d | 1395 | { |
0997aa54 FM |
1396 | cpumask_var_t allowed_mask; |
1397 | int ret = 0; | |
1398 | ||
ebdfefc0 JA |
1399 | if (!tctx || !tctx->io_wq) |
1400 | return -EINVAL; | |
1401 | ||
0997aa54 FM |
1402 | if (!alloc_cpumask_var(&allowed_mask, GFP_KERNEL)) |
1403 | return -ENOMEM; | |
1404 | ||
fe76421d | 1405 | rcu_read_lock(); |
0997aa54 FM |
1406 | cpuset_cpus_allowed(tctx->io_wq->task, allowed_mask); |
1407 | if (mask) { | |
1408 | if (cpumask_subset(mask, allowed_mask)) | |
1409 | cpumask_copy(tctx->io_wq->cpu_mask, mask); | |
1410 | else | |
1411 | ret = -EINVAL; | |
1412 | } else { | |
1413 | cpumask_copy(tctx->io_wq->cpu_mask, allowed_mask); | |
1414 | } | |
fe76421d | 1415 | rcu_read_unlock(); |
da64d6db | 1416 | |
0997aa54 FM |
1417 | free_cpumask_var(allowed_mask); |
1418 | return ret; | |
fe76421d JA |
1419 | } |
1420 | ||
2e480058 JA |
1421 | /* |
1422 | * Set max number of unbounded workers, returns old value. If new_count is 0, | |
1423 | * then just return the old value. | |
1424 | */ | |
1425 | int io_wq_max_workers(struct io_wq *wq, int *new_count) | |
1426 | { | |
dfd63baf | 1427 | struct io_wq_acct *acct; |
71c9ce27 | 1428 | int prev[IO_WQ_ACCT_NR]; |
da64d6db | 1429 | int i; |
2e480058 | 1430 | |
dd47c104 ES |
1431 | BUILD_BUG_ON((int) IO_WQ_ACCT_BOUND != (int) IO_WQ_BOUND); |
1432 | BUILD_BUG_ON((int) IO_WQ_ACCT_UNBOUND != (int) IO_WQ_UNBOUND); | |
1433 | BUILD_BUG_ON((int) IO_WQ_ACCT_NR != 2); | |
1434 | ||
86127bb1 | 1435 | for (i = 0; i < IO_WQ_ACCT_NR; i++) { |
2e480058 JA |
1436 | if (new_count[i] > task_rlimit(current, RLIMIT_NPROC)) |
1437 | new_count[i] = task_rlimit(current, RLIMIT_NPROC); | |
1438 | } | |
1439 | ||
71c9ce27 BZ |
1440 | for (i = 0; i < IO_WQ_ACCT_NR; i++) |
1441 | prev[i] = 0; | |
1442 | ||
2e480058 | 1443 | rcu_read_lock(); |
2e480058 | 1444 | |
da64d6db | 1445 | for (i = 0; i < IO_WQ_ACCT_NR; i++) { |
dfd63baf | 1446 | acct = &wq->acct[i]; |
751eedc4 | 1447 | raw_spin_lock(&acct->workers_lock); |
da64d6db BL |
1448 | prev[i] = max_t(int, acct->max_workers, prev[i]); |
1449 | if (new_count[i]) | |
1450 | acct->max_workers = new_count[i]; | |
751eedc4 | 1451 | raw_spin_unlock(&acct->workers_lock); |
2e480058 JA |
1452 | } |
1453 | rcu_read_unlock(); | |
71c9ce27 BZ |
1454 | |
1455 | for (i = 0; i < IO_WQ_ACCT_NR; i++) | |
1456 | new_count[i] = prev[i]; | |
1457 | ||
2e480058 JA |
1458 | return 0; |
1459 | } | |
1460 | ||
43c01fbe JA |
1461 | static __init int io_wq_init(void) |
1462 | { | |
1463 | int ret; | |
1464 | ||
1465 | ret = cpuhp_setup_state_multi(CPUHP_AP_ONLINE_DYN, "io-wq/online", | |
0e03496d | 1466 | io_wq_cpu_online, io_wq_cpu_offline); |
43c01fbe JA |
1467 | if (ret < 0) |
1468 | return ret; | |
1469 | io_wq_online = ret; | |
1470 | return 0; | |
1471 | } | |
1472 | subsys_initcall(io_wq_init); |