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