GNU Linux-libre 5.10.217-gnu1
[releases.git] / io_uring / 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/percpu.h>
13 #include <linux/slab.h>
14 #include <linux/rculist_nulls.h>
15 #include <linux/cpu.h>
16 #include <linux/tracehook.h>
17 #include <uapi/linux/io_uring.h>
18
19 #include "io-wq.h"
20
21 #define WORKER_IDLE_TIMEOUT     (5 * HZ)
22
23 enum {
24         IO_WORKER_F_UP          = 1,    /* up and active */
25         IO_WORKER_F_RUNNING     = 2,    /* account as running */
26         IO_WORKER_F_FREE        = 4,    /* worker on free list */
27         IO_WORKER_F_BOUND       = 8,    /* is doing bounded work */
28 };
29
30 enum {
31         IO_WQ_BIT_EXIT          = 0,    /* wq exiting */
32 };
33
34 enum {
35         IO_ACCT_STALLED_BIT     = 0,    /* stalled on hash */
36 };
37
38 /*
39  * One for each thread in a wqe pool
40  */
41 struct io_worker {
42         refcount_t ref;
43         unsigned flags;
44         struct hlist_nulls_node nulls_node;
45         struct list_head all_list;
46         struct task_struct *task;
47         struct io_wqe *wqe;
48
49         struct io_wq_work *cur_work;
50         spinlock_t lock;
51
52         struct completion ref_done;
53
54         unsigned long create_state;
55         struct callback_head create_work;
56         int create_index;
57
58         union {
59                 struct rcu_head rcu;
60                 struct work_struct work;
61         };
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         int index;
76         atomic_t nr_running;
77         struct io_wq_work_list work_list;
78         unsigned long flags;
79 };
80
81 enum {
82         IO_WQ_ACCT_BOUND,
83         IO_WQ_ACCT_UNBOUND,
84         IO_WQ_ACCT_NR,
85 };
86
87 /*
88  * Per-node worker thread pool
89  */
90 struct io_wqe {
91         raw_spinlock_t lock;
92         struct io_wqe_acct acct[2];
93
94         int node;
95
96         struct hlist_nulls_head free_list;
97         struct list_head all_list;
98
99         struct wait_queue_entry wait;
100
101         struct io_wq *wq;
102         struct io_wq_work *hash_tail[IO_WQ_NR_HASH_BUCKETS];
103
104         cpumask_var_t cpu_mask;
105 };
106
107 /*
108  * Per io_wq state
109   */
110 struct io_wq {
111         unsigned long state;
112
113         free_work_fn *free_work;
114         io_wq_work_fn *do_work;
115
116         struct io_wq_hash *hash;
117
118         atomic_t worker_refs;
119         struct completion worker_done;
120
121         struct hlist_node cpuhp_node;
122
123         struct task_struct *task;
124
125         struct io_wqe *wqes[];
126 };
127
128 static enum cpuhp_state io_wq_online;
129
130 struct io_cb_cancel_data {
131         work_cancel_fn *fn;
132         void *data;
133         int nr_running;
134         int nr_pending;
135         bool cancel_all;
136 };
137
138 static bool create_io_worker(struct io_wq *wq, struct io_wqe *wqe, int index);
139 static void io_wqe_dec_running(struct io_worker *worker);
140 static bool io_acct_cancel_pending_work(struct io_wqe *wqe,
141                                         struct io_wqe_acct *acct,
142                                         struct io_cb_cancel_data *match);
143 static void create_worker_cb(struct callback_head *cb);
144 static void io_wq_cancel_tw_create(struct io_wq *wq);
145
146 static bool io_worker_get(struct io_worker *worker)
147 {
148         return refcount_inc_not_zero(&worker->ref);
149 }
150
151 static void io_worker_release(struct io_worker *worker)
152 {
153         if (refcount_dec_and_test(&worker->ref))
154                 complete(&worker->ref_done);
155 }
156
157 static inline struct io_wqe_acct *io_get_acct(struct io_wqe *wqe, bool bound)
158 {
159         return &wqe->acct[bound ? IO_WQ_ACCT_BOUND : IO_WQ_ACCT_UNBOUND];
160 }
161
162 static inline struct io_wqe_acct *io_work_get_acct(struct io_wqe *wqe,
163                                                    struct io_wq_work *work)
164 {
165         return io_get_acct(wqe, !(work->flags & IO_WQ_WORK_UNBOUND));
166 }
167
168 static inline struct io_wqe_acct *io_wqe_get_acct(struct io_worker *worker)
169 {
170         return io_get_acct(worker->wqe, worker->flags & IO_WORKER_F_BOUND);
171 }
172
173 static void io_worker_ref_put(struct io_wq *wq)
174 {
175         if (atomic_dec_and_test(&wq->worker_refs))
176                 complete(&wq->worker_done);
177 }
178
179 bool io_wq_worker_stopped(void)
180 {
181         struct io_worker *worker = current->pf_io_worker;
182
183         if (WARN_ON_ONCE(!io_wq_current_is_worker()))
184                 return true;
185
186         return test_bit(IO_WQ_BIT_EXIT, &worker->wqe->wq->state);
187 }
188
189 static void io_worker_cancel_cb(struct io_worker *worker)
190 {
191         struct io_wqe_acct *acct = io_wqe_get_acct(worker);
192         struct io_wqe *wqe = worker->wqe;
193         struct io_wq *wq = wqe->wq;
194
195         atomic_dec(&acct->nr_running);
196         raw_spin_lock(&worker->wqe->lock);
197         acct->nr_workers--;
198         raw_spin_unlock(&worker->wqe->lock);
199         io_worker_ref_put(wq);
200         clear_bit_unlock(0, &worker->create_state);
201         io_worker_release(worker);
202 }
203
204 static bool io_task_worker_match(struct callback_head *cb, void *data)
205 {
206         struct io_worker *worker;
207
208         if (cb->func != create_worker_cb)
209                 return false;
210         worker = container_of(cb, struct io_worker, create_work);
211         return worker == data;
212 }
213
214 static void io_worker_exit(struct io_worker *worker)
215 {
216         struct io_wqe *wqe = worker->wqe;
217         struct io_wq *wq = wqe->wq;
218
219         while (1) {
220                 struct callback_head *cb = task_work_cancel_match(wq->task,
221                                                 io_task_worker_match, worker);
222
223                 if (!cb)
224                         break;
225                 io_worker_cancel_cb(worker);
226         }
227
228         if (refcount_dec_and_test(&worker->ref))
229                 complete(&worker->ref_done);
230         wait_for_completion(&worker->ref_done);
231
232         raw_spin_lock(&wqe->lock);
233         if (worker->flags & IO_WORKER_F_FREE)
234                 hlist_nulls_del_rcu(&worker->nulls_node);
235         list_del_rcu(&worker->all_list);
236         preempt_disable();
237         io_wqe_dec_running(worker);
238         worker->flags = 0;
239         current->flags &= ~PF_IO_WORKER;
240         preempt_enable();
241         raw_spin_unlock(&wqe->lock);
242
243         kfree_rcu(worker, rcu);
244         io_worker_ref_put(wqe->wq);
245         do_exit(0);
246 }
247
248 static inline bool io_acct_run_queue(struct io_wqe_acct *acct)
249 {
250         if (!wq_list_empty(&acct->work_list) &&
251             !test_bit(IO_ACCT_STALLED_BIT, &acct->flags))
252                 return true;
253         return false;
254 }
255
256 /*
257  * Check head of free list for an available worker. If one isn't available,
258  * caller must create one.
259  */
260 static bool io_wqe_activate_free_worker(struct io_wqe *wqe,
261                                         struct io_wqe_acct *acct)
262         __must_hold(RCU)
263 {
264         struct hlist_nulls_node *n;
265         struct io_worker *worker;
266
267         /*
268          * Iterate free_list and see if we can find an idle worker to
269          * activate. If a given worker is on the free_list but in the process
270          * of exiting, keep trying.
271          */
272         hlist_nulls_for_each_entry_rcu(worker, n, &wqe->free_list, nulls_node) {
273                 if (!io_worker_get(worker))
274                         continue;
275                 if (io_wqe_get_acct(worker) != acct) {
276                         io_worker_release(worker);
277                         continue;
278                 }
279                 if (wake_up_process(worker->task)) {
280                         io_worker_release(worker);
281                         return true;
282                 }
283                 io_worker_release(worker);
284         }
285
286         return false;
287 }
288
289 /*
290  * We need a worker. If we find a free one, we're good. If not, and we're
291  * below the max number of workers, create one.
292  */
293 static bool io_wqe_create_worker(struct io_wqe *wqe, struct io_wqe_acct *acct)
294 {
295         /*
296          * Most likely an attempt to queue unbounded work on an io_wq that
297          * wasn't setup with any unbounded workers.
298          */
299         if (unlikely(!acct->max_workers))
300                 pr_warn_once("io-wq is not configured for unbound workers");
301
302         raw_spin_lock(&wqe->lock);
303         if (acct->nr_workers >= acct->max_workers) {
304                 raw_spin_unlock(&wqe->lock);
305                 return true;
306         }
307         acct->nr_workers++;
308         raw_spin_unlock(&wqe->lock);
309         atomic_inc(&acct->nr_running);
310         atomic_inc(&wqe->wq->worker_refs);
311         return create_io_worker(wqe->wq, wqe, acct->index);
312 }
313
314 static void io_wqe_inc_running(struct io_worker *worker)
315 {
316         struct io_wqe_acct *acct = io_wqe_get_acct(worker);
317
318         atomic_inc(&acct->nr_running);
319 }
320
321 static void create_worker_cb(struct callback_head *cb)
322 {
323         struct io_worker *worker;
324         struct io_wq *wq;
325         struct io_wqe *wqe;
326         struct io_wqe_acct *acct;
327         bool do_create = false;
328
329         worker = container_of(cb, struct io_worker, create_work);
330         wqe = worker->wqe;
331         wq = wqe->wq;
332         acct = &wqe->acct[worker->create_index];
333         raw_spin_lock(&wqe->lock);
334         if (acct->nr_workers < acct->max_workers) {
335                 acct->nr_workers++;
336                 do_create = true;
337         }
338         raw_spin_unlock(&wqe->lock);
339         if (do_create) {
340                 create_io_worker(wq, wqe, worker->create_index);
341         } else {
342                 atomic_dec(&acct->nr_running);
343                 io_worker_ref_put(wq);
344         }
345         clear_bit_unlock(0, &worker->create_state);
346         io_worker_release(worker);
347 }
348
349 static bool io_queue_worker_create(struct io_worker *worker,
350                                    struct io_wqe_acct *acct,
351                                    task_work_func_t func)
352 {
353         struct io_wqe *wqe = worker->wqe;
354         struct io_wq *wq = wqe->wq;
355
356         /* raced with exit, just ignore create call */
357         if (test_bit(IO_WQ_BIT_EXIT, &wq->state))
358                 goto fail;
359         if (!io_worker_get(worker))
360                 goto fail;
361         /*
362          * create_state manages ownership of create_work/index. We should
363          * only need one entry per worker, as the worker going to sleep
364          * will trigger the condition, and waking will clear it once it
365          * runs the task_work.
366          */
367         if (test_bit(0, &worker->create_state) ||
368             test_and_set_bit_lock(0, &worker->create_state))
369                 goto fail_release;
370
371         atomic_inc(&wq->worker_refs);
372         init_task_work(&worker->create_work, func);
373         worker->create_index = acct->index;
374         if (!task_work_add(wq->task, &worker->create_work, TWA_SIGNAL)) {
375                 /*
376                  * EXIT may have been set after checking it above, check after
377                  * adding the task_work and remove any creation item if it is
378                  * now set. wq exit does that too, but we can have added this
379                  * work item after we canceled in io_wq_exit_workers().
380                  */
381                 if (test_bit(IO_WQ_BIT_EXIT, &wq->state))
382                         io_wq_cancel_tw_create(wq);
383                 io_worker_ref_put(wq);
384                 return true;
385         }
386         io_worker_ref_put(wq);
387         clear_bit_unlock(0, &worker->create_state);
388 fail_release:
389         io_worker_release(worker);
390 fail:
391         atomic_dec(&acct->nr_running);
392         io_worker_ref_put(wq);
393         return false;
394 }
395
396 static void io_wqe_dec_running(struct io_worker *worker)
397         __must_hold(wqe->lock)
398 {
399         struct io_wqe_acct *acct = io_wqe_get_acct(worker);
400         struct io_wqe *wqe = worker->wqe;
401
402         if (!(worker->flags & IO_WORKER_F_UP))
403                 return;
404
405         if (atomic_dec_and_test(&acct->nr_running) && io_acct_run_queue(acct)) {
406                 atomic_inc(&acct->nr_running);
407                 atomic_inc(&wqe->wq->worker_refs);
408                 raw_spin_unlock(&wqe->lock);
409                 io_queue_worker_create(worker, acct, create_worker_cb);
410                 raw_spin_lock(&wqe->lock);
411         }
412 }
413
414 /*
415  * Worker will start processing some work. Move it to the busy list, if
416  * it's currently on the freelist
417  */
418 static void __io_worker_busy(struct io_wqe *wqe, struct io_worker *worker,
419                              struct io_wq_work *work)
420         __must_hold(wqe->lock)
421 {
422         if (worker->flags & IO_WORKER_F_FREE) {
423                 worker->flags &= ~IO_WORKER_F_FREE;
424                 hlist_nulls_del_init_rcu(&worker->nulls_node);
425         }
426 }
427
428 /*
429  * No work, worker going to sleep. Move to freelist, and unuse mm if we
430  * have one attached. Dropping the mm may potentially sleep, so we drop
431  * the lock in that case and return success. Since the caller has to
432  * retry the loop in that case (we changed task state), we don't regrab
433  * the lock if we return success.
434  */
435 static void __io_worker_idle(struct io_wqe *wqe, struct io_worker *worker)
436         __must_hold(wqe->lock)
437 {
438         if (!(worker->flags & IO_WORKER_F_FREE)) {
439                 worker->flags |= IO_WORKER_F_FREE;
440                 hlist_nulls_add_head_rcu(&worker->nulls_node, &wqe->free_list);
441         }
442 }
443
444 static inline unsigned int io_get_work_hash(struct io_wq_work *work)
445 {
446         return work->flags >> IO_WQ_HASH_SHIFT;
447 }
448
449 static bool io_wait_on_hash(struct io_wqe *wqe, unsigned int hash)
450 {
451         struct io_wq *wq = wqe->wq;
452         bool ret = false;
453
454         spin_lock_irq(&wq->hash->wait.lock);
455         if (list_empty(&wqe->wait.entry)) {
456                 __add_wait_queue(&wq->hash->wait, &wqe->wait);
457                 if (!test_bit(hash, &wq->hash->map)) {
458                         __set_current_state(TASK_RUNNING);
459                         list_del_init(&wqe->wait.entry);
460                         ret = true;
461                 }
462         }
463         spin_unlock_irq(&wq->hash->wait.lock);
464         return ret;
465 }
466
467 static struct io_wq_work *io_get_next_work(struct io_wqe_acct *acct,
468                                            struct io_worker *worker)
469         __must_hold(wqe->lock)
470 {
471         struct io_wq_work_node *node, *prev;
472         struct io_wq_work *work, *tail;
473         unsigned int stall_hash = -1U;
474         struct io_wqe *wqe = worker->wqe;
475
476         wq_list_for_each(node, prev, &acct->work_list) {
477                 unsigned int hash;
478
479                 work = container_of(node, struct io_wq_work, list);
480
481                 /* not hashed, can run anytime */
482                 if (!io_wq_is_hashed(work)) {
483                         wq_list_del(&acct->work_list, node, prev);
484                         return work;
485                 }
486
487                 hash = io_get_work_hash(work);
488                 /* all items with this hash lie in [work, tail] */
489                 tail = wqe->hash_tail[hash];
490
491                 /* hashed, can run if not already running */
492                 if (!test_and_set_bit(hash, &wqe->wq->hash->map)) {
493                         wqe->hash_tail[hash] = NULL;
494                         wq_list_cut(&acct->work_list, &tail->list, prev);
495                         return work;
496                 }
497                 if (stall_hash == -1U)
498                         stall_hash = hash;
499                 /* fast forward to a next hash, for-each will fix up @prev */
500                 node = &tail->list;
501         }
502
503         if (stall_hash != -1U) {
504                 bool unstalled;
505
506                 /*
507                  * Set this before dropping the lock to avoid racing with new
508                  * work being added and clearing the stalled bit.
509                  */
510                 set_bit(IO_ACCT_STALLED_BIT, &acct->flags);
511                 raw_spin_unlock(&wqe->lock);
512                 unstalled = io_wait_on_hash(wqe, stall_hash);
513                 raw_spin_lock(&wqe->lock);
514                 if (unstalled) {
515                         clear_bit(IO_ACCT_STALLED_BIT, &acct->flags);
516                         if (wq_has_sleeper(&wqe->wq->hash->wait))
517                                 wake_up(&wqe->wq->hash->wait);
518                 }
519         }
520
521         return NULL;
522 }
523
524 static bool io_flush_signals(void)
525 {
526         if (test_thread_flag(TIF_NOTIFY_SIGNAL) || current->task_works) {
527                 __set_current_state(TASK_RUNNING);
528                 tracehook_notify_signal();
529                 return true;
530         }
531         return false;
532 }
533
534 static void io_assign_current_work(struct io_worker *worker,
535                                    struct io_wq_work *work)
536 {
537         if (work) {
538                 io_flush_signals();
539                 cond_resched();
540         }
541
542         spin_lock(&worker->lock);
543         worker->cur_work = work;
544         spin_unlock(&worker->lock);
545 }
546
547 static void io_wqe_enqueue(struct io_wqe *wqe, struct io_wq_work *work);
548
549 static void io_worker_handle_work(struct io_worker *worker)
550         __releases(wqe->lock)
551 {
552         struct io_wqe_acct *acct = io_wqe_get_acct(worker);
553         struct io_wqe *wqe = worker->wqe;
554         struct io_wq *wq = wqe->wq;
555         bool do_kill = test_bit(IO_WQ_BIT_EXIT, &wq->state);
556
557         do {
558                 struct io_wq_work *work;
559 get_next:
560                 /*
561                  * If we got some work, mark us as busy. If we didn't, but
562                  * the list isn't empty, it means we stalled on hashed work.
563                  * Mark us stalled so we don't keep looking for work when we
564                  * can't make progress, any work completion or insertion will
565                  * clear the stalled flag.
566                  */
567                 work = io_get_next_work(acct, worker);
568                 if (work)
569                         __io_worker_busy(wqe, worker, work);
570
571                 raw_spin_unlock(&wqe->lock);
572                 if (!work)
573                         break;
574                 io_assign_current_work(worker, work);
575                 __set_current_state(TASK_RUNNING);
576
577                 /* handle a whole dependent link */
578                 do {
579                         struct io_wq_work *next_hashed, *linked;
580                         unsigned int hash = io_get_work_hash(work);
581
582                         next_hashed = wq_next_work(work);
583
584                         if (unlikely(do_kill) && (work->flags & IO_WQ_WORK_UNBOUND))
585                                 work->flags |= IO_WQ_WORK_CANCEL;
586                         wq->do_work(work);
587                         io_assign_current_work(worker, NULL);
588
589                         linked = wq->free_work(work);
590                         work = next_hashed;
591                         if (!work && linked && !io_wq_is_hashed(linked)) {
592                                 work = linked;
593                                 linked = NULL;
594                         }
595                         io_assign_current_work(worker, work);
596                         if (linked)
597                                 io_wqe_enqueue(wqe, linked);
598
599                         if (hash != -1U && !next_hashed) {
600                                 /* serialize hash clear with wake_up() */
601                                 spin_lock_irq(&wq->hash->wait.lock);
602                                 clear_bit(hash, &wq->hash->map);
603                                 clear_bit(IO_ACCT_STALLED_BIT, &acct->flags);
604                                 spin_unlock_irq(&wq->hash->wait.lock);
605                                 if (wq_has_sleeper(&wq->hash->wait))
606                                         wake_up(&wq->hash->wait);
607                                 raw_spin_lock(&wqe->lock);
608                                 /* skip unnecessary unlock-lock wqe->lock */
609                                 if (!work)
610                                         goto get_next;
611                                 raw_spin_unlock(&wqe->lock);
612                         }
613                 } while (work);
614
615                 raw_spin_lock(&wqe->lock);
616         } while (1);
617 }
618
619 static int io_wqe_worker(void *data)
620 {
621         struct io_worker *worker = data;
622         struct io_wqe_acct *acct = io_wqe_get_acct(worker);
623         struct io_wqe *wqe = worker->wqe;
624         struct io_wq *wq = wqe->wq;
625         bool last_timeout = false;
626         char buf[TASK_COMM_LEN];
627
628         worker->flags |= (IO_WORKER_F_UP | IO_WORKER_F_RUNNING);
629
630         snprintf(buf, sizeof(buf), "iou-wrk-%d", wq->task->pid);
631         set_task_comm(current, buf);
632
633         while (!test_bit(IO_WQ_BIT_EXIT, &wq->state)) {
634                 long ret;
635
636                 set_current_state(TASK_INTERRUPTIBLE);
637 loop:
638                 raw_spin_lock(&wqe->lock);
639                 if (io_acct_run_queue(acct)) {
640                         io_worker_handle_work(worker);
641                         goto loop;
642                 }
643                 /* timed out, exit unless we're the last worker */
644                 if (last_timeout && acct->nr_workers > 1) {
645                         acct->nr_workers--;
646                         raw_spin_unlock(&wqe->lock);
647                         __set_current_state(TASK_RUNNING);
648                         break;
649                 }
650                 last_timeout = false;
651                 __io_worker_idle(wqe, worker);
652                 raw_spin_unlock(&wqe->lock);
653                 if (io_flush_signals())
654                         continue;
655                 ret = schedule_timeout(WORKER_IDLE_TIMEOUT);
656                 if (signal_pending(current)) {
657                         struct ksignal ksig;
658
659                         if (!get_signal(&ksig))
660                                 continue;
661                         break;
662                 }
663                 last_timeout = !ret;
664         }
665
666         if (test_bit(IO_WQ_BIT_EXIT, &wq->state)) {
667                 raw_spin_lock(&wqe->lock);
668                 io_worker_handle_work(worker);
669         }
670
671         io_worker_exit(worker);
672         return 0;
673 }
674
675 /*
676  * Called when a worker is scheduled in. Mark us as currently running.
677  */
678 void io_wq_worker_running(struct task_struct *tsk)
679 {
680         struct io_worker *worker = tsk->pf_io_worker;
681
682         if (!worker)
683                 return;
684         if (!(worker->flags & IO_WORKER_F_UP))
685                 return;
686         if (worker->flags & IO_WORKER_F_RUNNING)
687                 return;
688         worker->flags |= IO_WORKER_F_RUNNING;
689         io_wqe_inc_running(worker);
690 }
691
692 /*
693  * Called when worker is going to sleep. If there are no workers currently
694  * running and we have work pending, wake up a free one or create a new one.
695  */
696 void io_wq_worker_sleeping(struct task_struct *tsk)
697 {
698         struct io_worker *worker = tsk->pf_io_worker;
699
700         if (!worker)
701                 return;
702         if (!(worker->flags & IO_WORKER_F_UP))
703                 return;
704         if (!(worker->flags & IO_WORKER_F_RUNNING))
705                 return;
706
707         worker->flags &= ~IO_WORKER_F_RUNNING;
708
709         raw_spin_lock(&worker->wqe->lock);
710         io_wqe_dec_running(worker);
711         raw_spin_unlock(&worker->wqe->lock);
712 }
713
714 static void io_init_new_worker(struct io_wqe *wqe, struct io_worker *worker,
715                                struct task_struct *tsk)
716 {
717         tsk->pf_io_worker = worker;
718         worker->task = tsk;
719         set_cpus_allowed_ptr(tsk, wqe->cpu_mask);
720         tsk->flags |= PF_NO_SETAFFINITY;
721
722         raw_spin_lock(&wqe->lock);
723         hlist_nulls_add_head_rcu(&worker->nulls_node, &wqe->free_list);
724         list_add_tail_rcu(&worker->all_list, &wqe->all_list);
725         worker->flags |= IO_WORKER_F_FREE;
726         raw_spin_unlock(&wqe->lock);
727         wake_up_new_task(tsk);
728 }
729
730 static bool io_wq_work_match_all(struct io_wq_work *work, void *data)
731 {
732         return true;
733 }
734
735 static inline bool io_should_retry_thread(long err)
736 {
737         /*
738          * Prevent perpetual task_work retry, if the task (or its group) is
739          * exiting.
740          */
741         if (fatal_signal_pending(current))
742                 return false;
743
744         switch (err) {
745         case -EAGAIN:
746         case -ERESTARTSYS:
747         case -ERESTARTNOINTR:
748         case -ERESTARTNOHAND:
749                 return true;
750         default:
751                 return false;
752         }
753 }
754
755 static void create_worker_cont(struct callback_head *cb)
756 {
757         struct io_worker *worker;
758         struct task_struct *tsk;
759         struct io_wqe *wqe;
760
761         worker = container_of(cb, struct io_worker, create_work);
762         clear_bit_unlock(0, &worker->create_state);
763         wqe = worker->wqe;
764         tsk = create_io_thread(io_wqe_worker, worker, wqe->node);
765         if (!IS_ERR(tsk)) {
766                 io_init_new_worker(wqe, worker, tsk);
767                 io_worker_release(worker);
768                 return;
769         } else if (!io_should_retry_thread(PTR_ERR(tsk))) {
770                 struct io_wqe_acct *acct = io_wqe_get_acct(worker);
771
772                 atomic_dec(&acct->nr_running);
773                 raw_spin_lock(&wqe->lock);
774                 acct->nr_workers--;
775                 if (!acct->nr_workers) {
776                         struct io_cb_cancel_data match = {
777                                 .fn             = io_wq_work_match_all,
778                                 .cancel_all     = true,
779                         };
780
781                         while (io_acct_cancel_pending_work(wqe, acct, &match))
782                                 raw_spin_lock(&wqe->lock);
783                 }
784                 raw_spin_unlock(&wqe->lock);
785                 io_worker_ref_put(wqe->wq);
786                 kfree(worker);
787                 return;
788         }
789
790         /* re-create attempts grab a new worker ref, drop the existing one */
791         io_worker_release(worker);
792         schedule_work(&worker->work);
793 }
794
795 static void io_workqueue_create(struct work_struct *work)
796 {
797         struct io_worker *worker = container_of(work, struct io_worker, work);
798         struct io_wqe_acct *acct = io_wqe_get_acct(worker);
799
800         if (!io_queue_worker_create(worker, acct, create_worker_cont))
801                 kfree(worker);
802 }
803
804 static bool create_io_worker(struct io_wq *wq, struct io_wqe *wqe, int index)
805 {
806         struct io_wqe_acct *acct = &wqe->acct[index];
807         struct io_worker *worker;
808         struct task_struct *tsk;
809
810         __set_current_state(TASK_RUNNING);
811
812         worker = kzalloc_node(sizeof(*worker), GFP_KERNEL, wqe->node);
813         if (!worker) {
814 fail:
815                 atomic_dec(&acct->nr_running);
816                 raw_spin_lock(&wqe->lock);
817                 acct->nr_workers--;
818                 raw_spin_unlock(&wqe->lock);
819                 io_worker_ref_put(wq);
820                 return false;
821         }
822
823         refcount_set(&worker->ref, 1);
824         worker->wqe = wqe;
825         spin_lock_init(&worker->lock);
826         init_completion(&worker->ref_done);
827
828         if (index == IO_WQ_ACCT_BOUND)
829                 worker->flags |= IO_WORKER_F_BOUND;
830
831         tsk = create_io_thread(io_wqe_worker, worker, wqe->node);
832         if (!IS_ERR(tsk)) {
833                 io_init_new_worker(wqe, worker, tsk);
834         } else if (!io_should_retry_thread(PTR_ERR(tsk))) {
835                 kfree(worker);
836                 goto fail;
837         } else {
838                 INIT_WORK(&worker->work, io_workqueue_create);
839                 schedule_work(&worker->work);
840         }
841
842         return true;
843 }
844
845 /*
846  * Iterate the passed in list and call the specific function for each
847  * worker that isn't exiting
848  */
849 static bool io_wq_for_each_worker(struct io_wqe *wqe,
850                                   bool (*func)(struct io_worker *, void *),
851                                   void *data)
852 {
853         struct io_worker *worker;
854         bool ret = false;
855
856         list_for_each_entry_rcu(worker, &wqe->all_list, all_list) {
857                 if (io_worker_get(worker)) {
858                         /* no task if node is/was offline */
859                         if (worker->task)
860                                 ret = func(worker, data);
861                         io_worker_release(worker);
862                         if (ret)
863                                 break;
864                 }
865         }
866
867         return ret;
868 }
869
870 static bool io_wq_worker_wake(struct io_worker *worker, void *data)
871 {
872         set_notify_signal(worker->task);
873         wake_up_process(worker->task);
874         return false;
875 }
876
877 static void io_run_cancel(struct io_wq_work *work, struct io_wqe *wqe)
878 {
879         struct io_wq *wq = wqe->wq;
880
881         do {
882                 work->flags |= IO_WQ_WORK_CANCEL;
883                 wq->do_work(work);
884                 work = wq->free_work(work);
885         } while (work);
886 }
887
888 static void io_wqe_insert_work(struct io_wqe *wqe, struct io_wq_work *work)
889 {
890         struct io_wqe_acct *acct = io_work_get_acct(wqe, work);
891         unsigned int hash;
892         struct io_wq_work *tail;
893
894         if (!io_wq_is_hashed(work)) {
895 append:
896                 wq_list_add_tail(&work->list, &acct->work_list);
897                 return;
898         }
899
900         hash = io_get_work_hash(work);
901         tail = wqe->hash_tail[hash];
902         wqe->hash_tail[hash] = work;
903         if (!tail)
904                 goto append;
905
906         wq_list_add_after(&work->list, &tail->list, &acct->work_list);
907 }
908
909 static bool io_wq_work_match_item(struct io_wq_work *work, void *data)
910 {
911         return work == data;
912 }
913
914 static void io_wqe_enqueue(struct io_wqe *wqe, struct io_wq_work *work)
915 {
916         struct io_wqe_acct *acct = io_work_get_acct(wqe, work);
917         unsigned work_flags = work->flags;
918         bool do_create;
919
920         /*
921          * If io-wq is exiting for this task, or if the request has explicitly
922          * been marked as one that should not get executed, cancel it here.
923          */
924         if (test_bit(IO_WQ_BIT_EXIT, &wqe->wq->state) ||
925             (work->flags & IO_WQ_WORK_CANCEL)) {
926                 io_run_cancel(work, wqe);
927                 return;
928         }
929
930         raw_spin_lock(&wqe->lock);
931         io_wqe_insert_work(wqe, work);
932         clear_bit(IO_ACCT_STALLED_BIT, &acct->flags);
933
934         rcu_read_lock();
935         do_create = !io_wqe_activate_free_worker(wqe, acct);
936         rcu_read_unlock();
937
938         raw_spin_unlock(&wqe->lock);
939
940         if (do_create && ((work_flags & IO_WQ_WORK_CONCURRENT) ||
941             !atomic_read(&acct->nr_running))) {
942                 bool did_create;
943
944                 did_create = io_wqe_create_worker(wqe, acct);
945                 if (likely(did_create))
946                         return;
947
948                 raw_spin_lock(&wqe->lock);
949                 /* fatal condition, failed to create the first worker */
950                 if (!acct->nr_workers) {
951                         struct io_cb_cancel_data match = {
952                                 .fn             = io_wq_work_match_item,
953                                 .data           = work,
954                                 .cancel_all     = false,
955                         };
956
957                         if (io_acct_cancel_pending_work(wqe, acct, &match))
958                                 raw_spin_lock(&wqe->lock);
959                 }
960                 raw_spin_unlock(&wqe->lock);
961         }
962 }
963
964 void io_wq_enqueue(struct io_wq *wq, struct io_wq_work *work)
965 {
966         struct io_wqe *wqe = wq->wqes[numa_node_id()];
967
968         io_wqe_enqueue(wqe, work);
969 }
970
971 /*
972  * Work items that hash to the same value will not be done in parallel.
973  * Used to limit concurrent writes, generally hashed by inode.
974  */
975 void io_wq_hash_work(struct io_wq_work *work, void *val)
976 {
977         unsigned int bit;
978
979         bit = hash_ptr(val, IO_WQ_HASH_ORDER);
980         work->flags |= (IO_WQ_WORK_HASHED | (bit << IO_WQ_HASH_SHIFT));
981 }
982
983 static bool io_wq_worker_cancel(struct io_worker *worker, void *data)
984 {
985         struct io_cb_cancel_data *match = data;
986
987         /*
988          * Hold the lock to avoid ->cur_work going out of scope, caller
989          * may dereference the passed in work.
990          */
991         spin_lock(&worker->lock);
992         if (worker->cur_work &&
993             match->fn(worker->cur_work, match->data)) {
994                 set_notify_signal(worker->task);
995                 match->nr_running++;
996         }
997         spin_unlock(&worker->lock);
998
999         return match->nr_running && !match->cancel_all;
1000 }
1001
1002 static inline void io_wqe_remove_pending(struct io_wqe *wqe,
1003                                          struct io_wq_work *work,
1004                                          struct io_wq_work_node *prev)
1005 {
1006         struct io_wqe_acct *acct = io_work_get_acct(wqe, work);
1007         unsigned int hash = io_get_work_hash(work);
1008         struct io_wq_work *prev_work = NULL;
1009
1010         if (io_wq_is_hashed(work) && work == wqe->hash_tail[hash]) {
1011                 if (prev)
1012                         prev_work = container_of(prev, struct io_wq_work, list);
1013                 if (prev_work && io_get_work_hash(prev_work) == hash)
1014                         wqe->hash_tail[hash] = prev_work;
1015                 else
1016                         wqe->hash_tail[hash] = NULL;
1017         }
1018         wq_list_del(&acct->work_list, &work->list, prev);
1019 }
1020
1021 static bool io_acct_cancel_pending_work(struct io_wqe *wqe,
1022                                         struct io_wqe_acct *acct,
1023                                         struct io_cb_cancel_data *match)
1024         __releases(wqe->lock)
1025 {
1026         struct io_wq_work_node *node, *prev;
1027         struct io_wq_work *work;
1028
1029         wq_list_for_each(node, prev, &acct->work_list) {
1030                 work = container_of(node, struct io_wq_work, list);
1031                 if (!match->fn(work, match->data))
1032                         continue;
1033                 io_wqe_remove_pending(wqe, work, prev);
1034                 raw_spin_unlock(&wqe->lock);
1035                 io_run_cancel(work, wqe);
1036                 match->nr_pending++;
1037                 /* not safe to continue after unlock */
1038                 return true;
1039         }
1040
1041         return false;
1042 }
1043
1044 static void io_wqe_cancel_pending_work(struct io_wqe *wqe,
1045                                        struct io_cb_cancel_data *match)
1046 {
1047         int i;
1048 retry:
1049         raw_spin_lock(&wqe->lock);
1050         for (i = 0; i < IO_WQ_ACCT_NR; i++) {
1051                 struct io_wqe_acct *acct = io_get_acct(wqe, i == 0);
1052
1053                 if (io_acct_cancel_pending_work(wqe, acct, match)) {
1054                         if (match->cancel_all)
1055                                 goto retry;
1056                         return;
1057                 }
1058         }
1059         raw_spin_unlock(&wqe->lock);
1060 }
1061
1062 static void io_wqe_cancel_running_work(struct io_wqe *wqe,
1063                                        struct io_cb_cancel_data *match)
1064 {
1065         rcu_read_lock();
1066         io_wq_for_each_worker(wqe, io_wq_worker_cancel, match);
1067         rcu_read_unlock();
1068 }
1069
1070 enum io_wq_cancel io_wq_cancel_cb(struct io_wq *wq, work_cancel_fn *cancel,
1071                                   void *data, bool cancel_all)
1072 {
1073         struct io_cb_cancel_data match = {
1074                 .fn             = cancel,
1075                 .data           = data,
1076                 .cancel_all     = cancel_all,
1077         };
1078         int node;
1079
1080         /*
1081          * First check pending list, if we're lucky we can just remove it
1082          * from there. CANCEL_OK means that the work is returned as-new,
1083          * no completion will be posted for it.
1084          */
1085         for_each_node(node) {
1086                 struct io_wqe *wqe = wq->wqes[node];
1087
1088                 io_wqe_cancel_pending_work(wqe, &match);
1089                 if (match.nr_pending && !match.cancel_all)
1090                         return IO_WQ_CANCEL_OK;
1091         }
1092
1093         /*
1094          * Now check if a free (going busy) or busy worker has the work
1095          * currently running. If we find it there, we'll return CANCEL_RUNNING
1096          * as an indication that we attempt to signal cancellation. The
1097          * completion will run normally in this case.
1098          */
1099         for_each_node(node) {
1100                 struct io_wqe *wqe = wq->wqes[node];
1101
1102                 io_wqe_cancel_running_work(wqe, &match);
1103                 if (match.nr_running && !match.cancel_all)
1104                         return IO_WQ_CANCEL_RUNNING;
1105         }
1106
1107         if (match.nr_running)
1108                 return IO_WQ_CANCEL_RUNNING;
1109         if (match.nr_pending)
1110                 return IO_WQ_CANCEL_OK;
1111         return IO_WQ_CANCEL_NOTFOUND;
1112 }
1113
1114 static int io_wqe_hash_wake(struct wait_queue_entry *wait, unsigned mode,
1115                             int sync, void *key)
1116 {
1117         struct io_wqe *wqe = container_of(wait, struct io_wqe, wait);
1118         int i;
1119
1120         list_del_init(&wait->entry);
1121
1122         rcu_read_lock();
1123         for (i = 0; i < IO_WQ_ACCT_NR; i++) {
1124                 struct io_wqe_acct *acct = &wqe->acct[i];
1125
1126                 if (test_and_clear_bit(IO_ACCT_STALLED_BIT, &acct->flags))
1127                         io_wqe_activate_free_worker(wqe, acct);
1128         }
1129         rcu_read_unlock();
1130         return 1;
1131 }
1132
1133 struct io_wq *io_wq_create(unsigned bounded, struct io_wq_data *data)
1134 {
1135         int ret, node, i;
1136         struct io_wq *wq;
1137
1138         if (WARN_ON_ONCE(!data->free_work || !data->do_work))
1139                 return ERR_PTR(-EINVAL);
1140         if (WARN_ON_ONCE(!bounded))
1141                 return ERR_PTR(-EINVAL);
1142
1143         wq = kzalloc(struct_size(wq, wqes, nr_node_ids), GFP_KERNEL);
1144         if (!wq)
1145                 return ERR_PTR(-ENOMEM);
1146         ret = cpuhp_state_add_instance_nocalls(io_wq_online, &wq->cpuhp_node);
1147         if (ret)
1148                 goto err_wq;
1149
1150         refcount_inc(&data->hash->refs);
1151         wq->hash = data->hash;
1152         wq->free_work = data->free_work;
1153         wq->do_work = data->do_work;
1154
1155         ret = -ENOMEM;
1156         for_each_node(node) {
1157                 struct io_wqe *wqe;
1158                 int alloc_node = node;
1159
1160                 if (!node_online(alloc_node))
1161                         alloc_node = NUMA_NO_NODE;
1162                 wqe = kzalloc_node(sizeof(struct io_wqe), GFP_KERNEL, alloc_node);
1163                 if (!wqe)
1164                         goto err;
1165                 wq->wqes[node] = wqe;
1166                 if (!alloc_cpumask_var(&wqe->cpu_mask, GFP_KERNEL))
1167                         goto err;
1168                 cpumask_copy(wqe->cpu_mask, cpumask_of_node(node));
1169                 wqe->node = alloc_node;
1170                 wqe->acct[IO_WQ_ACCT_BOUND].max_workers = bounded;
1171                 wqe->acct[IO_WQ_ACCT_UNBOUND].max_workers =
1172                                         task_rlimit(current, RLIMIT_NPROC);
1173                 INIT_LIST_HEAD(&wqe->wait.entry);
1174                 wqe->wait.func = io_wqe_hash_wake;
1175                 for (i = 0; i < IO_WQ_ACCT_NR; i++) {
1176                         struct io_wqe_acct *acct = &wqe->acct[i];
1177
1178                         acct->index = i;
1179                         atomic_set(&acct->nr_running, 0);
1180                         INIT_WQ_LIST(&acct->work_list);
1181                 }
1182                 wqe->wq = wq;
1183                 raw_spin_lock_init(&wqe->lock);
1184                 INIT_HLIST_NULLS_HEAD(&wqe->free_list, 0);
1185                 INIT_LIST_HEAD(&wqe->all_list);
1186         }
1187
1188         wq->task = get_task_struct(data->task);
1189         atomic_set(&wq->worker_refs, 1);
1190         init_completion(&wq->worker_done);
1191         return wq;
1192 err:
1193         io_wq_put_hash(data->hash);
1194         cpuhp_state_remove_instance_nocalls(io_wq_online, &wq->cpuhp_node);
1195         for_each_node(node) {
1196                 if (!wq->wqes[node])
1197                         continue;
1198                 free_cpumask_var(wq->wqes[node]->cpu_mask);
1199                 kfree(wq->wqes[node]);
1200         }
1201 err_wq:
1202         kfree(wq);
1203         return ERR_PTR(ret);
1204 }
1205
1206 static bool io_task_work_match(struct callback_head *cb, void *data)
1207 {
1208         struct io_worker *worker;
1209
1210         if (cb->func != create_worker_cb && cb->func != create_worker_cont)
1211                 return false;
1212         worker = container_of(cb, struct io_worker, create_work);
1213         return worker->wqe->wq == data;
1214 }
1215
1216 void io_wq_exit_start(struct io_wq *wq)
1217 {
1218         set_bit(IO_WQ_BIT_EXIT, &wq->state);
1219 }
1220
1221 static void io_wq_cancel_tw_create(struct io_wq *wq)
1222 {
1223         struct callback_head *cb;
1224
1225         while ((cb = task_work_cancel_match(wq->task, io_task_work_match, wq)) != NULL) {
1226                 struct io_worker *worker;
1227
1228                 worker = container_of(cb, struct io_worker, create_work);
1229                 io_worker_cancel_cb(worker);
1230                 /*
1231                  * Only the worker continuation helper has worker allocated and
1232                  * hence needs freeing.
1233                  */
1234                 if (cb->func == create_worker_cont)
1235                         kfree(worker);
1236         }
1237 }
1238
1239 static void io_wq_exit_workers(struct io_wq *wq)
1240 {
1241         int node;
1242
1243         if (!wq->task)
1244                 return;
1245
1246         io_wq_cancel_tw_create(wq);
1247
1248         rcu_read_lock();
1249         for_each_node(node) {
1250                 struct io_wqe *wqe = wq->wqes[node];
1251
1252                 io_wq_for_each_worker(wqe, io_wq_worker_wake, NULL);
1253         }
1254         rcu_read_unlock();
1255         io_worker_ref_put(wq);
1256         wait_for_completion(&wq->worker_done);
1257
1258         for_each_node(node) {
1259                 spin_lock_irq(&wq->hash->wait.lock);
1260                 list_del_init(&wq->wqes[node]->wait.entry);
1261                 spin_unlock_irq(&wq->hash->wait.lock);
1262         }
1263         put_task_struct(wq->task);
1264         wq->task = NULL;
1265 }
1266
1267 static void io_wq_destroy(struct io_wq *wq)
1268 {
1269         int node;
1270
1271         cpuhp_state_remove_instance_nocalls(io_wq_online, &wq->cpuhp_node);
1272
1273         for_each_node(node) {
1274                 struct io_wqe *wqe = wq->wqes[node];
1275                 struct io_cb_cancel_data match = {
1276                         .fn             = io_wq_work_match_all,
1277                         .cancel_all     = true,
1278                 };
1279                 io_wqe_cancel_pending_work(wqe, &match);
1280                 free_cpumask_var(wqe->cpu_mask);
1281                 kfree(wqe);
1282         }
1283         io_wq_put_hash(wq->hash);
1284         kfree(wq);
1285 }
1286
1287 void io_wq_put_and_exit(struct io_wq *wq)
1288 {
1289         WARN_ON_ONCE(!test_bit(IO_WQ_BIT_EXIT, &wq->state));
1290
1291         io_wq_exit_workers(wq);
1292         io_wq_destroy(wq);
1293 }
1294
1295 struct online_data {
1296         unsigned int cpu;
1297         bool online;
1298 };
1299
1300 static bool io_wq_worker_affinity(struct io_worker *worker, void *data)
1301 {
1302         struct online_data *od = data;
1303
1304         if (od->online)
1305                 cpumask_set_cpu(od->cpu, worker->wqe->cpu_mask);
1306         else
1307                 cpumask_clear_cpu(od->cpu, worker->wqe->cpu_mask);
1308         return false;
1309 }
1310
1311 static int __io_wq_cpu_online(struct io_wq *wq, unsigned int cpu, bool online)
1312 {
1313         struct online_data od = {
1314                 .cpu = cpu,
1315                 .online = online
1316         };
1317         int i;
1318
1319         rcu_read_lock();
1320         for_each_node(i)
1321                 io_wq_for_each_worker(wq->wqes[i], io_wq_worker_affinity, &od);
1322         rcu_read_unlock();
1323         return 0;
1324 }
1325
1326 static int io_wq_cpu_online(unsigned int cpu, struct hlist_node *node)
1327 {
1328         struct io_wq *wq = hlist_entry_safe(node, struct io_wq, cpuhp_node);
1329
1330         return __io_wq_cpu_online(wq, cpu, true);
1331 }
1332
1333 static int io_wq_cpu_offline(unsigned int cpu, struct hlist_node *node)
1334 {
1335         struct io_wq *wq = hlist_entry_safe(node, struct io_wq, cpuhp_node);
1336
1337         return __io_wq_cpu_online(wq, cpu, false);
1338 }
1339
1340 int io_wq_cpu_affinity(struct io_wq *wq, cpumask_var_t mask)
1341 {
1342         int i;
1343
1344         rcu_read_lock();
1345         for_each_node(i) {
1346                 struct io_wqe *wqe = wq->wqes[i];
1347
1348                 if (mask)
1349                         cpumask_copy(wqe->cpu_mask, mask);
1350                 else
1351                         cpumask_copy(wqe->cpu_mask, cpumask_of_node(i));
1352         }
1353         rcu_read_unlock();
1354         return 0;
1355 }
1356
1357 /*
1358  * Set max number of unbounded workers, returns old value. If new_count is 0,
1359  * then just return the old value.
1360  */
1361 int io_wq_max_workers(struct io_wq *wq, int *new_count)
1362 {
1363         int prev[IO_WQ_ACCT_NR];
1364         bool first_node = true;
1365         int i, node;
1366
1367         BUILD_BUG_ON((int) IO_WQ_ACCT_BOUND   != (int) IO_WQ_BOUND);
1368         BUILD_BUG_ON((int) IO_WQ_ACCT_UNBOUND != (int) IO_WQ_UNBOUND);
1369         BUILD_BUG_ON((int) IO_WQ_ACCT_NR      != 2);
1370
1371         for (i = 0; i < 2; i++) {
1372                 if (new_count[i] > task_rlimit(current, RLIMIT_NPROC))
1373                         new_count[i] = task_rlimit(current, RLIMIT_NPROC);
1374         }
1375
1376         for (i = 0; i < IO_WQ_ACCT_NR; i++)
1377                 prev[i] = 0;
1378
1379         rcu_read_lock();
1380         for_each_node(node) {
1381                 struct io_wqe *wqe = wq->wqes[node];
1382                 struct io_wqe_acct *acct;
1383
1384                 raw_spin_lock(&wqe->lock);
1385                 for (i = 0; i < IO_WQ_ACCT_NR; i++) {
1386                         acct = &wqe->acct[i];
1387                         if (first_node)
1388                                 prev[i] = max_t(int, acct->max_workers, prev[i]);
1389                         if (new_count[i])
1390                                 acct->max_workers = new_count[i];
1391                 }
1392                 raw_spin_unlock(&wqe->lock);
1393                 first_node = false;
1394         }
1395         rcu_read_unlock();
1396
1397         for (i = 0; i < IO_WQ_ACCT_NR; i++)
1398                 new_count[i] = prev[i];
1399
1400         return 0;
1401 }
1402
1403 static __init int io_wq_init(void)
1404 {
1405         int ret;
1406
1407         ret = cpuhp_setup_state_multi(CPUHP_AP_ONLINE_DYN, "io-wq/online",
1408                                         io_wq_cpu_online, io_wq_cpu_offline);
1409         if (ret < 0)
1410                 return ret;
1411         io_wq_online = ret;
1412         return 0;
1413 }
1414 subsys_initcall(io_wq_init);