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