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