OSDN Git Service

io-wq: remove extra space characters
[tomoyo/tomoyo-test1.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/mmu_context.h>
14 #include <linux/sched/mm.h>
15 #include <linux/percpu.h>
16 #include <linux/slab.h>
17 #include <linux/kthread.h>
18 #include <linux/rculist_nulls.h>
19
20 #include "io-wq.h"
21
22 #define WORKER_IDLE_TIMEOUT     (5 * HZ)
23
24 enum {
25         IO_WORKER_F_UP          = 1,    /* up and active */
26         IO_WORKER_F_RUNNING     = 2,    /* account as running */
27         IO_WORKER_F_FREE        = 4,    /* worker on free list */
28         IO_WORKER_F_EXITING     = 8,    /* worker exiting */
29         IO_WORKER_F_FIXED       = 16,   /* static idle worker */
30         IO_WORKER_F_BOUND       = 32,   /* is doing bounded work */
31 };
32
33 enum {
34         IO_WQ_BIT_EXIT          = 0,    /* wq exiting */
35         IO_WQ_BIT_CANCEL        = 1,    /* cancel work on list */
36         IO_WQ_BIT_ERROR         = 2,    /* error on setup */
37 };
38
39 enum {
40         IO_WQE_FLAG_STALLED     = 1,    /* stalled on hash */
41 };
42
43 /*
44  * One for each thread in a wqe pool
45  */
46 struct io_worker {
47         refcount_t ref;
48         unsigned flags;
49         struct hlist_nulls_node nulls_node;
50         struct list_head all_list;
51         struct task_struct *task;
52         wait_queue_head_t wait;
53         struct io_wqe *wqe;
54
55         struct io_wq_work *cur_work;
56         spinlock_t lock;
57
58         struct rcu_head rcu;
59         struct mm_struct *mm;
60         struct files_struct *restore_files;
61 };
62
63 #if BITS_PER_LONG == 64
64 #define IO_WQ_HASH_ORDER        6
65 #else
66 #define IO_WQ_HASH_ORDER        5
67 #endif
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                 spinlock_t lock;
86                 struct list_head work_list;
87                 unsigned long hash_map;
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 hlist_nulls_head busy_list;
96         struct list_head all_list;
97
98         struct io_wq *wq;
99 };
100
101 /*
102  * Per io_wq state
103   */
104 struct io_wq {
105         struct io_wqe **wqes;
106         unsigned long state;
107         unsigned nr_wqes;
108
109         get_work_fn *get_work;
110         put_work_fn *put_work;
111
112         struct task_struct *manager;
113         struct user_struct *user;
114         struct mm_struct *mm;
115         refcount_t refs;
116         struct completion done;
117 };
118
119 static bool io_worker_get(struct io_worker *worker)
120 {
121         return refcount_inc_not_zero(&worker->ref);
122 }
123
124 static void io_worker_release(struct io_worker *worker)
125 {
126         if (refcount_dec_and_test(&worker->ref))
127                 wake_up_process(worker->task);
128 }
129
130 /*
131  * Note: drops the wqe->lock if returning true! The caller must re-acquire
132  * the lock in that case. Some callers need to restart handling if this
133  * happens, so we can't just re-acquire the lock on behalf of the caller.
134  */
135 static bool __io_worker_unuse(struct io_wqe *wqe, struct io_worker *worker)
136 {
137         bool dropped_lock = false;
138
139         if (current->files != worker->restore_files) {
140                 __acquire(&wqe->lock);
141                 spin_unlock_irq(&wqe->lock);
142                 dropped_lock = true;
143
144                 task_lock(current);
145                 current->files = worker->restore_files;
146                 task_unlock(current);
147         }
148
149         /*
150          * If we have an active mm, we need to drop the wq lock before unusing
151          * it. If we do, return true and let the caller retry the idle loop.
152          */
153         if (worker->mm) {
154                 if (!dropped_lock) {
155                         __acquire(&wqe->lock);
156                         spin_unlock_irq(&wqe->lock);
157                         dropped_lock = true;
158                 }
159                 __set_current_state(TASK_RUNNING);
160                 set_fs(KERNEL_DS);
161                 unuse_mm(worker->mm);
162                 mmput(worker->mm);
163                 worker->mm = NULL;
164         }
165
166         return dropped_lock;
167 }
168
169 static inline struct io_wqe_acct *io_work_get_acct(struct io_wqe *wqe,
170                                                    struct io_wq_work *work)
171 {
172         if (work->flags & IO_WQ_WORK_UNBOUND)
173                 return &wqe->acct[IO_WQ_ACCT_UNBOUND];
174
175         return &wqe->acct[IO_WQ_ACCT_BOUND];
176 }
177
178 static inline struct io_wqe_acct *io_wqe_get_acct(struct io_wqe *wqe,
179                                                   struct io_worker *worker)
180 {
181         if (worker->flags & IO_WORKER_F_BOUND)
182                 return &wqe->acct[IO_WQ_ACCT_BOUND];
183
184         return &wqe->acct[IO_WQ_ACCT_UNBOUND];
185 }
186
187 static void io_worker_exit(struct io_worker *worker)
188 {
189         struct io_wqe *wqe = worker->wqe;
190         struct io_wqe_acct *acct = io_wqe_get_acct(wqe, worker);
191         unsigned nr_workers;
192
193         /*
194          * If we're not at zero, someone else is holding a brief reference
195          * to the worker. Wait for that to go away.
196          */
197         set_current_state(TASK_INTERRUPTIBLE);
198         if (!refcount_dec_and_test(&worker->ref))
199                 schedule();
200         __set_current_state(TASK_RUNNING);
201
202         preempt_disable();
203         current->flags &= ~PF_IO_WORKER;
204         if (worker->flags & IO_WORKER_F_RUNNING)
205                 atomic_dec(&acct->nr_running);
206         if (!(worker->flags & IO_WORKER_F_BOUND))
207                 atomic_dec(&wqe->wq->user->processes);
208         worker->flags = 0;
209         preempt_enable();
210
211         spin_lock_irq(&wqe->lock);
212         hlist_nulls_del_rcu(&worker->nulls_node);
213         list_del_rcu(&worker->all_list);
214         if (__io_worker_unuse(wqe, worker)) {
215                 __release(&wqe->lock);
216                 spin_lock_irq(&wqe->lock);
217         }
218         acct->nr_workers--;
219         nr_workers = wqe->acct[IO_WQ_ACCT_BOUND].nr_workers +
220                         wqe->acct[IO_WQ_ACCT_UNBOUND].nr_workers;
221         spin_unlock_irq(&wqe->lock);
222
223         /* all workers gone, wq exit can proceed */
224         if (!nr_workers && refcount_dec_and_test(&wqe->wq->refs))
225                 complete(&wqe->wq->done);
226
227         kfree_rcu(worker, rcu);
228 }
229
230 static inline bool io_wqe_run_queue(struct io_wqe *wqe)
231         __must_hold(wqe->lock)
232 {
233         if (!list_empty(&wqe->work_list) && !(wqe->flags & IO_WQE_FLAG_STALLED))
234                 return true;
235         return false;
236 }
237
238 /*
239  * Check head of free list for an available worker. If one isn't available,
240  * caller must wake up the wq manager to create one.
241  */
242 static bool io_wqe_activate_free_worker(struct io_wqe *wqe)
243         __must_hold(RCU)
244 {
245         struct hlist_nulls_node *n;
246         struct io_worker *worker;
247
248         n = rcu_dereference(hlist_nulls_first_rcu(&wqe->free_list));
249         if (is_a_nulls(n))
250                 return false;
251
252         worker = hlist_nulls_entry(n, struct io_worker, nulls_node);
253         if (io_worker_get(worker)) {
254                 wake_up(&worker->wait);
255                 io_worker_release(worker);
256                 return true;
257         }
258
259         return false;
260 }
261
262 /*
263  * We need a worker. If we find a free one, we're good. If not, and we're
264  * below the max number of workers, wake up the manager to create one.
265  */
266 static void io_wqe_wake_worker(struct io_wqe *wqe, struct io_wqe_acct *acct)
267 {
268         bool ret;
269
270         /*
271          * Most likely an attempt to queue unbounded work on an io_wq that
272          * wasn't setup with any unbounded workers.
273          */
274         WARN_ON_ONCE(!acct->max_workers);
275
276         rcu_read_lock();
277         ret = io_wqe_activate_free_worker(wqe);
278         rcu_read_unlock();
279
280         if (!ret && acct->nr_workers < acct->max_workers)
281                 wake_up_process(wqe->wq->manager);
282 }
283
284 static void io_wqe_inc_running(struct io_wqe *wqe, struct io_worker *worker)
285 {
286         struct io_wqe_acct *acct = io_wqe_get_acct(wqe, worker);
287
288         atomic_inc(&acct->nr_running);
289 }
290
291 static void io_wqe_dec_running(struct io_wqe *wqe, struct io_worker *worker)
292         __must_hold(wqe->lock)
293 {
294         struct io_wqe_acct *acct = io_wqe_get_acct(wqe, worker);
295
296         if (atomic_dec_and_test(&acct->nr_running) && io_wqe_run_queue(wqe))
297                 io_wqe_wake_worker(wqe, acct);
298 }
299
300 static void io_worker_start(struct io_wqe *wqe, struct io_worker *worker)
301 {
302         allow_kernel_signal(SIGINT);
303
304         current->flags |= PF_IO_WORKER;
305
306         worker->flags |= (IO_WORKER_F_UP | IO_WORKER_F_RUNNING);
307         worker->restore_files = current->files;
308         io_wqe_inc_running(wqe, worker);
309 }
310
311 /*
312  * Worker will start processing some work. Move it to the busy list, if
313  * it's currently on the freelist
314  */
315 static void __io_worker_busy(struct io_wqe *wqe, struct io_worker *worker,
316                              struct io_wq_work *work)
317         __must_hold(wqe->lock)
318 {
319         bool worker_bound, work_bound;
320
321         if (worker->flags & IO_WORKER_F_FREE) {
322                 worker->flags &= ~IO_WORKER_F_FREE;
323                 hlist_nulls_del_init_rcu(&worker->nulls_node);
324                 hlist_nulls_add_head_rcu(&worker->nulls_node, &wqe->busy_list);
325         }
326
327         /*
328          * If worker is moving from bound to unbound (or vice versa), then
329          * ensure we update the running accounting.
330          */
331         worker_bound = (worker->flags & IO_WORKER_F_BOUND) != 0;
332         work_bound = (work->flags & IO_WQ_WORK_UNBOUND) == 0;
333         if (worker_bound != work_bound) {
334                 io_wqe_dec_running(wqe, worker);
335                 if (work_bound) {
336                         worker->flags |= IO_WORKER_F_BOUND;
337                         wqe->acct[IO_WQ_ACCT_UNBOUND].nr_workers--;
338                         wqe->acct[IO_WQ_ACCT_BOUND].nr_workers++;
339                         atomic_dec(&wqe->wq->user->processes);
340                 } else {
341                         worker->flags &= ~IO_WORKER_F_BOUND;
342                         wqe->acct[IO_WQ_ACCT_UNBOUND].nr_workers++;
343                         wqe->acct[IO_WQ_ACCT_BOUND].nr_workers--;
344                         atomic_inc(&wqe->wq->user->processes);
345                 }
346                 io_wqe_inc_running(wqe, worker);
347          }
348 }
349
350 /*
351  * No work, worker going to sleep. Move to freelist, and unuse mm if we
352  * have one attached. Dropping the mm may potentially sleep, so we drop
353  * the lock in that case and return success. Since the caller has to
354  * retry the loop in that case (we changed task state), we don't regrab
355  * the lock if we return success.
356  */
357 static bool __io_worker_idle(struct io_wqe *wqe, struct io_worker *worker)
358         __must_hold(wqe->lock)
359 {
360         if (!(worker->flags & IO_WORKER_F_FREE)) {
361                 worker->flags |= IO_WORKER_F_FREE;
362                 hlist_nulls_del_init_rcu(&worker->nulls_node);
363                 hlist_nulls_add_head_rcu(&worker->nulls_node, &wqe->free_list);
364         }
365
366         return __io_worker_unuse(wqe, worker);
367 }
368
369 static struct io_wq_work *io_get_next_work(struct io_wqe *wqe, unsigned *hash)
370         __must_hold(wqe->lock)
371 {
372         struct io_wq_work *work;
373
374         list_for_each_entry(work, &wqe->work_list, list) {
375                 /* not hashed, can run anytime */
376                 if (!(work->flags & IO_WQ_WORK_HASHED)) {
377                         list_del(&work->list);
378                         return work;
379                 }
380
381                 /* hashed, can run if not already running */
382                 *hash = work->flags >> IO_WQ_HASH_SHIFT;
383                 if (!(wqe->hash_map & BIT_ULL(*hash))) {
384                         wqe->hash_map |= BIT_ULL(*hash);
385                         list_del(&work->list);
386                         return work;
387                 }
388         }
389
390         return NULL;
391 }
392
393 static void io_worker_handle_work(struct io_worker *worker)
394         __releases(wqe->lock)
395 {
396         struct io_wq_work *work, *old_work = NULL, *put_work = NULL;
397         struct io_wqe *wqe = worker->wqe;
398         struct io_wq *wq = wqe->wq;
399
400         do {
401                 unsigned hash = -1U;
402
403                 /*
404                  * If we got some work, mark us as busy. If we didn't, but
405                  * the list isn't empty, it means we stalled on hashed work.
406                  * Mark us stalled so we don't keep looking for work when we
407                  * can't make progress, any work completion or insertion will
408                  * clear the stalled flag.
409                  */
410                 work = io_get_next_work(wqe, &hash);
411                 if (work)
412                         __io_worker_busy(wqe, worker, work);
413                 else if (!list_empty(&wqe->work_list))
414                         wqe->flags |= IO_WQE_FLAG_STALLED;
415
416                 spin_unlock_irq(&wqe->lock);
417                 if (put_work && wq->put_work)
418                         wq->put_work(old_work);
419                 if (!work)
420                         break;
421 next:
422                 /* flush any pending signals before assigning new work */
423                 if (signal_pending(current))
424                         flush_signals(current);
425
426                 spin_lock_irq(&worker->lock);
427                 worker->cur_work = work;
428                 spin_unlock_irq(&worker->lock);
429
430                 if ((work->flags & IO_WQ_WORK_NEEDS_FILES) &&
431                     current->files != work->files) {
432                         task_lock(current);
433                         current->files = work->files;
434                         task_unlock(current);
435                 }
436                 if ((work->flags & IO_WQ_WORK_NEEDS_USER) && !worker->mm &&
437                     wq->mm && mmget_not_zero(wq->mm)) {
438                         use_mm(wq->mm);
439                         set_fs(USER_DS);
440                         worker->mm = wq->mm;
441                 }
442                 if (test_bit(IO_WQ_BIT_CANCEL, &wq->state))
443                         work->flags |= IO_WQ_WORK_CANCEL;
444                 if (worker->mm)
445                         work->flags |= IO_WQ_WORK_HAS_MM;
446
447                 if (wq->get_work && !(work->flags & IO_WQ_WORK_INTERNAL)) {
448                         put_work = work;
449                         wq->get_work(work);
450                 }
451
452                 old_work = work;
453                 work->func(&work);
454
455                 spin_lock_irq(&worker->lock);
456                 worker->cur_work = NULL;
457                 spin_unlock_irq(&worker->lock);
458
459                 spin_lock_irq(&wqe->lock);
460
461                 if (hash != -1U) {
462                         wqe->hash_map &= ~BIT_ULL(hash);
463                         wqe->flags &= ~IO_WQE_FLAG_STALLED;
464                 }
465                 if (work && work != old_work) {
466                         spin_unlock_irq(&wqe->lock);
467
468                         if (put_work && wq->put_work) {
469                                 wq->put_work(put_work);
470                                 put_work = NULL;
471                         }
472
473                         /* dependent work not hashed */
474                         hash = -1U;
475                         goto next;
476                 }
477         } while (1);
478 }
479
480 static int io_wqe_worker(void *data)
481 {
482         struct io_worker *worker = data;
483         struct io_wqe *wqe = worker->wqe;
484         struct io_wq *wq = wqe->wq;
485         DEFINE_WAIT(wait);
486
487         io_worker_start(wqe, worker);
488
489         while (!test_bit(IO_WQ_BIT_EXIT, &wq->state)) {
490                 prepare_to_wait(&worker->wait, &wait, TASK_INTERRUPTIBLE);
491
492                 spin_lock_irq(&wqe->lock);
493                 if (io_wqe_run_queue(wqe)) {
494                         __set_current_state(TASK_RUNNING);
495                         io_worker_handle_work(worker);
496                         continue;
497                 }
498                 /* drops the lock on success, retry */
499                 if (__io_worker_idle(wqe, worker)) {
500                         __release(&wqe->lock);
501                         continue;
502                 }
503                 spin_unlock_irq(&wqe->lock);
504                 if (signal_pending(current))
505                         flush_signals(current);
506                 if (schedule_timeout(WORKER_IDLE_TIMEOUT))
507                         continue;
508                 /* timed out, exit unless we're the fixed worker */
509                 if (test_bit(IO_WQ_BIT_EXIT, &wq->state) ||
510                     !(worker->flags & IO_WORKER_F_FIXED))
511                         break;
512         }
513
514         finish_wait(&worker->wait, &wait);
515
516         if (test_bit(IO_WQ_BIT_EXIT, &wq->state)) {
517                 spin_lock_irq(&wqe->lock);
518                 if (!list_empty(&wqe->work_list))
519                         io_worker_handle_work(worker);
520                 else
521                         spin_unlock_irq(&wqe->lock);
522         }
523
524         io_worker_exit(worker);
525         return 0;
526 }
527
528 /*
529  * Called when a worker is scheduled in. Mark us as currently running.
530  */
531 void io_wq_worker_running(struct task_struct *tsk)
532 {
533         struct io_worker *worker = kthread_data(tsk);
534         struct io_wqe *wqe = worker->wqe;
535
536         if (!(worker->flags & IO_WORKER_F_UP))
537                 return;
538         if (worker->flags & IO_WORKER_F_RUNNING)
539                 return;
540         worker->flags |= IO_WORKER_F_RUNNING;
541         io_wqe_inc_running(wqe, worker);
542 }
543
544 /*
545  * Called when worker is going to sleep. If there are no workers currently
546  * running and we have work pending, wake up a free one or have the manager
547  * set one up.
548  */
549 void io_wq_worker_sleeping(struct task_struct *tsk)
550 {
551         struct io_worker *worker = kthread_data(tsk);
552         struct io_wqe *wqe = worker->wqe;
553
554         if (!(worker->flags & IO_WORKER_F_UP))
555                 return;
556         if (!(worker->flags & IO_WORKER_F_RUNNING))
557                 return;
558
559         worker->flags &= ~IO_WORKER_F_RUNNING;
560
561         spin_lock_irq(&wqe->lock);
562         io_wqe_dec_running(wqe, worker);
563         spin_unlock_irq(&wqe->lock);
564 }
565
566 static bool create_io_worker(struct io_wq *wq, struct io_wqe *wqe, int index)
567 {
568         struct io_wqe_acct *acct =&wqe->acct[index];
569         struct io_worker *worker;
570
571         worker = kcalloc_node(1, 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         init_waitqueue_head(&worker->wait);
578         worker->wqe = wqe;
579         spin_lock_init(&worker->lock);
580
581         worker->task = kthread_create_on_node(io_wqe_worker, worker, wqe->node,
582                                 "io_wqe_worker-%d/%d", index, wqe->node);
583         if (IS_ERR(worker->task)) {
584                 kfree(worker);
585                 return false;
586         }
587
588         spin_lock_irq(&wqe->lock);
589         hlist_nulls_add_head_rcu(&worker->nulls_node, &wqe->free_list);
590         list_add_tail_rcu(&worker->all_list, &wqe->all_list);
591         worker->flags |= IO_WORKER_F_FREE;
592         if (index == IO_WQ_ACCT_BOUND)
593                 worker->flags |= IO_WORKER_F_BOUND;
594         if (!acct->nr_workers && (worker->flags & IO_WORKER_F_BOUND))
595                 worker->flags |= IO_WORKER_F_FIXED;
596         acct->nr_workers++;
597         spin_unlock_irq(&wqe->lock);
598
599         if (index == IO_WQ_ACCT_UNBOUND)
600                 atomic_inc(&wq->user->processes);
601
602         wake_up_process(worker->task);
603         return true;
604 }
605
606 static inline bool io_wqe_need_worker(struct io_wqe *wqe, int index)
607         __must_hold(wqe->lock)
608 {
609         struct io_wqe_acct *acct = &wqe->acct[index];
610
611         /* if we have available workers or no work, no need */
612         if (!hlist_nulls_empty(&wqe->free_list) || !io_wqe_run_queue(wqe))
613                 return false;
614         return acct->nr_workers < acct->max_workers;
615 }
616
617 /*
618  * Manager thread. Tasked with creating new workers, if we need them.
619  */
620 static int io_wq_manager(void *data)
621 {
622         struct io_wq *wq = data;
623         int i;
624
625         /* create fixed workers */
626         refcount_set(&wq->refs, wq->nr_wqes);
627         for (i = 0; i < wq->nr_wqes; i++) {
628                 if (create_io_worker(wq, wq->wqes[i], IO_WQ_ACCT_BOUND))
629                         continue;
630                 goto err;
631         }
632
633         complete(&wq->done);
634
635         while (!kthread_should_stop()) {
636                 for (i = 0; i < wq->nr_wqes; i++) {
637                         struct io_wqe *wqe = wq->wqes[i];
638                         bool fork_worker[2] = { false, false };
639
640                         spin_lock_irq(&wqe->lock);
641                         if (io_wqe_need_worker(wqe, IO_WQ_ACCT_BOUND))
642                                 fork_worker[IO_WQ_ACCT_BOUND] = true;
643                         if (io_wqe_need_worker(wqe, IO_WQ_ACCT_UNBOUND))
644                                 fork_worker[IO_WQ_ACCT_UNBOUND] = true;
645                         spin_unlock_irq(&wqe->lock);
646                         if (fork_worker[IO_WQ_ACCT_BOUND])
647                                 create_io_worker(wq, wqe, IO_WQ_ACCT_BOUND);
648                         if (fork_worker[IO_WQ_ACCT_UNBOUND])
649                                 create_io_worker(wq, wqe, IO_WQ_ACCT_UNBOUND);
650                 }
651                 set_current_state(TASK_INTERRUPTIBLE);
652                 schedule_timeout(HZ);
653         }
654
655         return 0;
656 err:
657         set_bit(IO_WQ_BIT_ERROR, &wq->state);
658         set_bit(IO_WQ_BIT_EXIT, &wq->state);
659         if (refcount_sub_and_test(wq->nr_wqes - i, &wq->refs))
660                 complete(&wq->done);
661         return 0;
662 }
663
664 static bool io_wq_can_queue(struct io_wqe *wqe, struct io_wqe_acct *acct,
665                             struct io_wq_work *work)
666 {
667         bool free_worker;
668
669         if (!(work->flags & IO_WQ_WORK_UNBOUND))
670                 return true;
671         if (atomic_read(&acct->nr_running))
672                 return true;
673
674         rcu_read_lock();
675         free_worker = !hlist_nulls_empty(&wqe->free_list);
676         rcu_read_unlock();
677         if (free_worker)
678                 return true;
679
680         if (atomic_read(&wqe->wq->user->processes) >= acct->max_workers &&
681             !(capable(CAP_SYS_RESOURCE) || capable(CAP_SYS_ADMIN)))
682                 return false;
683
684         return true;
685 }
686
687 static void io_wqe_enqueue(struct io_wqe *wqe, struct io_wq_work *work)
688 {
689         struct io_wqe_acct *acct = io_work_get_acct(wqe, work);
690         unsigned long flags;
691
692         /*
693          * Do early check to see if we need a new unbound worker, and if we do,
694          * if we're allowed to do so. This isn't 100% accurate as there's a
695          * gap between this check and incrementing the value, but that's OK.
696          * It's close enough to not be an issue, fork() has the same delay.
697          */
698         if (unlikely(!io_wq_can_queue(wqe, acct, work))) {
699                 work->flags |= IO_WQ_WORK_CANCEL;
700                 work->func(&work);
701                 return;
702         }
703
704         spin_lock_irqsave(&wqe->lock, flags);
705         list_add_tail(&work->list, &wqe->work_list);
706         wqe->flags &= ~IO_WQE_FLAG_STALLED;
707         spin_unlock_irqrestore(&wqe->lock, flags);
708
709         if (!atomic_read(&acct->nr_running))
710                 io_wqe_wake_worker(wqe, acct);
711 }
712
713 void io_wq_enqueue(struct io_wq *wq, struct io_wq_work *work)
714 {
715         struct io_wqe *wqe = wq->wqes[numa_node_id()];
716
717         io_wqe_enqueue(wqe, work);
718 }
719
720 /*
721  * Enqueue work, hashed by some key. Work items that hash to the same value
722  * will not be done in parallel. Used to limit concurrent writes, generally
723  * hashed by inode.
724  */
725 void io_wq_enqueue_hashed(struct io_wq *wq, struct io_wq_work *work, void *val)
726 {
727         struct io_wqe *wqe = wq->wqes[numa_node_id()];
728         unsigned bit;
729
730
731         bit = hash_ptr(val, IO_WQ_HASH_ORDER);
732         work->flags |= (IO_WQ_WORK_HASHED | (bit << IO_WQ_HASH_SHIFT));
733         io_wqe_enqueue(wqe, work);
734 }
735
736 static bool io_wqe_worker_send_sig(struct io_worker *worker, void *data)
737 {
738         send_sig(SIGINT, worker->task, 1);
739         return false;
740 }
741
742 /*
743  * Iterate the passed in list and call the specific function for each
744  * worker that isn't exiting
745  */
746 static bool io_wq_for_each_worker(struct io_wqe *wqe,
747                                   bool (*func)(struct io_worker *, void *),
748                                   void *data)
749 {
750         struct io_worker *worker;
751         bool ret = false;
752
753         list_for_each_entry_rcu(worker, &wqe->all_list, all_list) {
754                 if (io_worker_get(worker)) {
755                         ret = func(worker, data);
756                         io_worker_release(worker);
757                         if (ret)
758                                 break;
759                 }
760         }
761
762         return ret;
763 }
764
765 void io_wq_cancel_all(struct io_wq *wq)
766 {
767         int i;
768
769         set_bit(IO_WQ_BIT_CANCEL, &wq->state);
770
771         /*
772          * Browse both lists, as there's a gap between handing work off
773          * to a worker and the worker putting itself on the busy_list
774          */
775         rcu_read_lock();
776         for (i = 0; i < wq->nr_wqes; i++) {
777                 struct io_wqe *wqe = wq->wqes[i];
778
779                 io_wq_for_each_worker(wqe, io_wqe_worker_send_sig, NULL);
780         }
781         rcu_read_unlock();
782 }
783
784 struct io_cb_cancel_data {
785         struct io_wqe *wqe;
786         work_cancel_fn *cancel;
787         void *caller_data;
788 };
789
790 static bool io_work_cancel(struct io_worker *worker, void *cancel_data)
791 {
792         struct io_cb_cancel_data *data = cancel_data;
793         unsigned long flags;
794         bool ret = false;
795
796         /*
797          * Hold the lock to avoid ->cur_work going out of scope, caller
798          * may dereference the passed in work.
799          */
800         spin_lock_irqsave(&worker->lock, flags);
801         if (worker->cur_work &&
802             data->cancel(worker->cur_work, data->caller_data)) {
803                 send_sig(SIGINT, worker->task, 1);
804                 ret = true;
805         }
806         spin_unlock_irqrestore(&worker->lock, flags);
807
808         return ret;
809 }
810
811 static enum io_wq_cancel io_wqe_cancel_cb_work(struct io_wqe *wqe,
812                                                work_cancel_fn *cancel,
813                                                void *cancel_data)
814 {
815         struct io_cb_cancel_data data = {
816                 .wqe = wqe,
817                 .cancel = cancel,
818                 .caller_data = cancel_data,
819         };
820         struct io_wq_work *work;
821         unsigned long flags;
822         bool found = false;
823
824         spin_lock_irqsave(&wqe->lock, flags);
825         list_for_each_entry(work, &wqe->work_list, list) {
826                 if (cancel(work, cancel_data)) {
827                         list_del(&work->list);
828                         found = true;
829                         break;
830                 }
831         }
832         spin_unlock_irqrestore(&wqe->lock, flags);
833
834         if (found) {
835                 work->flags |= IO_WQ_WORK_CANCEL;
836                 work->func(&work);
837                 return IO_WQ_CANCEL_OK;
838         }
839
840         rcu_read_lock();
841         found = io_wq_for_each_worker(wqe, io_work_cancel, &data);
842         rcu_read_unlock();
843         return found ? IO_WQ_CANCEL_RUNNING : IO_WQ_CANCEL_NOTFOUND;
844 }
845
846 enum io_wq_cancel io_wq_cancel_cb(struct io_wq *wq, work_cancel_fn *cancel,
847                                   void *data)
848 {
849         enum io_wq_cancel ret = IO_WQ_CANCEL_NOTFOUND;
850         int i;
851
852         for (i = 0; i < wq->nr_wqes; i++) {
853                 struct io_wqe *wqe = wq->wqes[i];
854
855                 ret = io_wqe_cancel_cb_work(wqe, cancel, data);
856                 if (ret != IO_WQ_CANCEL_NOTFOUND)
857                         break;
858         }
859
860         return ret;
861 }
862
863 static bool io_wq_worker_cancel(struct io_worker *worker, void *data)
864 {
865         struct io_wq_work *work = data;
866         unsigned long flags;
867         bool ret = false;
868
869         if (worker->cur_work != work)
870                 return false;
871
872         spin_lock_irqsave(&worker->lock, flags);
873         if (worker->cur_work == work) {
874                 send_sig(SIGINT, worker->task, 1);
875                 ret = true;
876         }
877         spin_unlock_irqrestore(&worker->lock, flags);
878
879         return ret;
880 }
881
882 static enum io_wq_cancel io_wqe_cancel_work(struct io_wqe *wqe,
883                                             struct io_wq_work *cwork)
884 {
885         struct io_wq_work *work;
886         unsigned long flags;
887         bool found = false;
888
889         cwork->flags |= IO_WQ_WORK_CANCEL;
890
891         /*
892          * First check pending list, if we're lucky we can just remove it
893          * from there. CANCEL_OK means that the work is returned as-new,
894          * no completion will be posted for it.
895          */
896         spin_lock_irqsave(&wqe->lock, flags);
897         list_for_each_entry(work, &wqe->work_list, list) {
898                 if (work == cwork) {
899                         list_del(&work->list);
900                         found = true;
901                         break;
902                 }
903         }
904         spin_unlock_irqrestore(&wqe->lock, flags);
905
906         if (found) {
907                 work->flags |= IO_WQ_WORK_CANCEL;
908                 work->func(&work);
909                 return IO_WQ_CANCEL_OK;
910         }
911
912         /*
913          * Now check if a free (going busy) or busy worker has the work
914          * currently running. If we find it there, we'll return CANCEL_RUNNING
915          * as an indication that we attempte to signal cancellation. The
916          * completion will run normally in this case.
917          */
918         rcu_read_lock();
919         found = io_wq_for_each_worker(wqe, io_wq_worker_cancel, cwork);
920         rcu_read_unlock();
921         return found ? IO_WQ_CANCEL_RUNNING : IO_WQ_CANCEL_NOTFOUND;
922 }
923
924 enum io_wq_cancel io_wq_cancel_work(struct io_wq *wq, struct io_wq_work *cwork)
925 {
926         enum io_wq_cancel ret = IO_WQ_CANCEL_NOTFOUND;
927         int i;
928
929         for (i = 0; i < wq->nr_wqes; i++) {
930                 struct io_wqe *wqe = wq->wqes[i];
931
932                 ret = io_wqe_cancel_work(wqe, cwork);
933                 if (ret != IO_WQ_CANCEL_NOTFOUND)
934                         break;
935         }
936
937         return ret;
938 }
939
940 struct io_wq_flush_data {
941         struct io_wq_work work;
942         struct completion done;
943 };
944
945 static void io_wq_flush_func(struct io_wq_work **workptr)
946 {
947         struct io_wq_work *work = *workptr;
948         struct io_wq_flush_data *data;
949
950         data = container_of(work, struct io_wq_flush_data, work);
951         complete(&data->done);
952 }
953
954 /*
955  * Doesn't wait for previously queued work to finish. When this completes,
956  * it just means that previously queued work was started.
957  */
958 void io_wq_flush(struct io_wq *wq)
959 {
960         struct io_wq_flush_data data;
961         int i;
962
963         for (i = 0; i < wq->nr_wqes; i++) {
964                 struct io_wqe *wqe = wq->wqes[i];
965
966                 init_completion(&data.done);
967                 INIT_IO_WORK(&data.work, io_wq_flush_func);
968                 data.work.flags |= IO_WQ_WORK_INTERNAL;
969                 io_wqe_enqueue(wqe, &data.work);
970                 wait_for_completion(&data.done);
971         }
972 }
973
974 struct io_wq *io_wq_create(unsigned bounded, struct mm_struct *mm,
975                            struct user_struct *user, get_work_fn *get_work,
976                            put_work_fn *put_work)
977 {
978         int ret = -ENOMEM, i, node;
979         struct io_wq *wq;
980
981         wq = kcalloc(1, sizeof(*wq), GFP_KERNEL);
982         if (!wq)
983                 return ERR_PTR(-ENOMEM);
984
985         wq->nr_wqes = num_online_nodes();
986         wq->wqes = kcalloc(wq->nr_wqes, sizeof(struct io_wqe *), GFP_KERNEL);
987         if (!wq->wqes) {
988                 kfree(wq);
989                 return ERR_PTR(-ENOMEM);
990         }
991
992         wq->get_work = get_work;
993         wq->put_work = put_work;
994
995         /* caller must already hold a reference to this */
996         wq->user = user;
997
998         i = 0;
999         for_each_online_node(node) {
1000                 struct io_wqe *wqe;
1001
1002                 wqe = kcalloc_node(1, sizeof(struct io_wqe), GFP_KERNEL, node);
1003                 if (!wqe)
1004                         break;
1005                 wq->wqes[i] = wqe;
1006                 wqe->node = node;
1007                 wqe->acct[IO_WQ_ACCT_BOUND].max_workers = bounded;
1008                 atomic_set(&wqe->acct[IO_WQ_ACCT_BOUND].nr_running, 0);
1009                 if (user) {
1010                         wqe->acct[IO_WQ_ACCT_UNBOUND].max_workers =
1011                                         task_rlimit(current, RLIMIT_NPROC);
1012                 }
1013                 atomic_set(&wqe->acct[IO_WQ_ACCT_UNBOUND].nr_running, 0);
1014                 wqe->node = node;
1015                 wqe->wq = wq;
1016                 spin_lock_init(&wqe->lock);
1017                 INIT_LIST_HEAD(&wqe->work_list);
1018                 INIT_HLIST_NULLS_HEAD(&wqe->free_list, 0);
1019                 INIT_HLIST_NULLS_HEAD(&wqe->busy_list, 1);
1020                 INIT_LIST_HEAD(&wqe->all_list);
1021
1022                 i++;
1023         }
1024
1025         init_completion(&wq->done);
1026
1027         if (i != wq->nr_wqes)
1028                 goto err;
1029
1030         /* caller must have already done mmgrab() on this mm */
1031         wq->mm = mm;
1032
1033         wq->manager = kthread_create(io_wq_manager, wq, "io_wq_manager");
1034         if (!IS_ERR(wq->manager)) {
1035                 wake_up_process(wq->manager);
1036                 wait_for_completion(&wq->done);
1037                 if (test_bit(IO_WQ_BIT_ERROR, &wq->state)) {
1038                         ret = -ENOMEM;
1039                         goto err;
1040                 }
1041                 reinit_completion(&wq->done);
1042                 return wq;
1043         }
1044
1045         ret = PTR_ERR(wq->manager);
1046         complete(&wq->done);
1047 err:
1048         for (i = 0; i < wq->nr_wqes; i++)
1049                 kfree(wq->wqes[i]);
1050         kfree(wq->wqes);
1051         kfree(wq);
1052         return ERR_PTR(ret);
1053 }
1054
1055 static bool io_wq_worker_wake(struct io_worker *worker, void *data)
1056 {
1057         wake_up_process(worker->task);
1058         return false;
1059 }
1060
1061 void io_wq_destroy(struct io_wq *wq)
1062 {
1063         int i;
1064
1065         set_bit(IO_WQ_BIT_EXIT, &wq->state);
1066         if (wq->manager)
1067                 kthread_stop(wq->manager);
1068
1069         rcu_read_lock();
1070         for (i = 0; i < wq->nr_wqes; i++) {
1071                 struct io_wqe *wqe = wq->wqes[i];
1072
1073                 if (!wqe)
1074                         continue;
1075                 io_wq_for_each_worker(wqe, io_wq_worker_wake, NULL);
1076         }
1077         rcu_read_unlock();
1078
1079         wait_for_completion(&wq->done);
1080
1081         for (i = 0; i < wq->nr_wqes; i++)
1082                 kfree(wq->wqes[i]);
1083         kfree(wq->wqes);
1084         kfree(wq);
1085 }