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