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