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