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