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