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