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