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