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