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