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