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