Linux Audio

Check our new training course

Loading...
v6.13.7
  1// SPDX-License-Identifier: GPL-2.0
  2/*
  3 * Contains the core associated with submission side polling of the SQ
  4 * ring, offloading submissions from the application to a kernel thread.
  5 */
  6#include <linux/kernel.h>
  7#include <linux/errno.h>
  8#include <linux/file.h>
  9#include <linux/mm.h>
 10#include <linux/slab.h>
 11#include <linux/audit.h>
 12#include <linux/security.h>
 13#include <linux/cpuset.h>
 14#include <linux/io_uring.h>
 15
 16#include <uapi/linux/io_uring.h>
 17
 18#include "io_uring.h"
 19#include "napi.h"
 20#include "sqpoll.h"
 21
 22#define IORING_SQPOLL_CAP_ENTRIES_VALUE 8
 23#define IORING_TW_CAP_ENTRIES_VALUE	8
 24
 25enum {
 26	IO_SQ_THREAD_SHOULD_STOP = 0,
 27	IO_SQ_THREAD_SHOULD_PARK,
 28};
 29
 30void io_sq_thread_unpark(struct io_sq_data *sqd)
 31	__releases(&sqd->lock)
 32{
 33	WARN_ON_ONCE(sqd->thread == current);
 34
 35	/*
 36	 * Do the dance but not conditional clear_bit() because it'd race with
 37	 * other threads incrementing park_pending and setting the bit.
 38	 */
 39	clear_bit(IO_SQ_THREAD_SHOULD_PARK, &sqd->state);
 40	if (atomic_dec_return(&sqd->park_pending))
 41		set_bit(IO_SQ_THREAD_SHOULD_PARK, &sqd->state);
 42	mutex_unlock(&sqd->lock);
 43	wake_up(&sqd->wait);
 44}
 45
 46void io_sq_thread_park(struct io_sq_data *sqd)
 47	__acquires(&sqd->lock)
 48{
 49	WARN_ON_ONCE(data_race(sqd->thread) == current);
 50
 51	atomic_inc(&sqd->park_pending);
 52	set_bit(IO_SQ_THREAD_SHOULD_PARK, &sqd->state);
 53	mutex_lock(&sqd->lock);
 54	if (sqd->thread)
 55		wake_up_process(sqd->thread);
 56}
 57
 58void io_sq_thread_stop(struct io_sq_data *sqd)
 59{
 60	WARN_ON_ONCE(sqd->thread == current);
 61	WARN_ON_ONCE(test_bit(IO_SQ_THREAD_SHOULD_STOP, &sqd->state));
 62
 63	set_bit(IO_SQ_THREAD_SHOULD_STOP, &sqd->state);
 64	mutex_lock(&sqd->lock);
 65	if (sqd->thread)
 66		wake_up_process(sqd->thread);
 67	mutex_unlock(&sqd->lock);
 68	wait_for_completion(&sqd->exited);
 69}
 70
 71void io_put_sq_data(struct io_sq_data *sqd)
 72{
 73	if (refcount_dec_and_test(&sqd->refs)) {
 74		WARN_ON_ONCE(atomic_read(&sqd->park_pending));
 75
 76		io_sq_thread_stop(sqd);
 77		kfree(sqd);
 78	}
 79}
 80
 81static __cold void io_sqd_update_thread_idle(struct io_sq_data *sqd)
 82{
 83	struct io_ring_ctx *ctx;
 84	unsigned sq_thread_idle = 0;
 85
 86	list_for_each_entry(ctx, &sqd->ctx_list, sqd_list)
 87		sq_thread_idle = max(sq_thread_idle, ctx->sq_thread_idle);
 88	sqd->sq_thread_idle = sq_thread_idle;
 89}
 90
 91void io_sq_thread_finish(struct io_ring_ctx *ctx)
 92{
 93	struct io_sq_data *sqd = ctx->sq_data;
 94
 95	if (sqd) {
 96		io_sq_thread_park(sqd);
 97		list_del_init(&ctx->sqd_list);
 98		io_sqd_update_thread_idle(sqd);
 99		io_sq_thread_unpark(sqd);
100
101		io_put_sq_data(sqd);
102		ctx->sq_data = NULL;
103	}
104}
105
106static struct io_sq_data *io_attach_sq_data(struct io_uring_params *p)
107{
108	struct io_ring_ctx *ctx_attach;
109	struct io_sq_data *sqd;
110	CLASS(fd, f)(p->wq_fd);
111
112	if (fd_empty(f))
 
113		return ERR_PTR(-ENXIO);
114	if (!io_is_uring_fops(fd_file(f)))
 
115		return ERR_PTR(-EINVAL);
 
116
117	ctx_attach = fd_file(f)->private_data;
118	sqd = ctx_attach->sq_data;
119	if (!sqd)
 
120		return ERR_PTR(-EINVAL);
121	if (sqd->task_tgid != current->tgid)
 
 
122		return ERR_PTR(-EPERM);
 
123
124	refcount_inc(&sqd->refs);
 
125	return sqd;
126}
127
128static struct io_sq_data *io_get_sq_data(struct io_uring_params *p,
129					 bool *attached)
130{
131	struct io_sq_data *sqd;
132
133	*attached = false;
134	if (p->flags & IORING_SETUP_ATTACH_WQ) {
135		sqd = io_attach_sq_data(p);
136		if (!IS_ERR(sqd)) {
137			*attached = true;
138			return sqd;
139		}
140		/* fall through for EPERM case, setup new sqd/task */
141		if (PTR_ERR(sqd) != -EPERM)
142			return sqd;
143	}
144
145	sqd = kzalloc(sizeof(*sqd), GFP_KERNEL);
146	if (!sqd)
147		return ERR_PTR(-ENOMEM);
148
149	atomic_set(&sqd->park_pending, 0);
150	refcount_set(&sqd->refs, 1);
151	INIT_LIST_HEAD(&sqd->ctx_list);
152	mutex_init(&sqd->lock);
153	init_waitqueue_head(&sqd->wait);
154	init_completion(&sqd->exited);
155	return sqd;
156}
157
158static inline bool io_sqd_events_pending(struct io_sq_data *sqd)
159{
160	return READ_ONCE(sqd->state);
161}
162
163static int __io_sq_thread(struct io_ring_ctx *ctx, bool cap_entries)
164{
165	unsigned int to_submit;
166	int ret = 0;
167
168	to_submit = io_sqring_entries(ctx);
169	/* if we're handling multiple rings, cap submit size for fairness */
170	if (cap_entries && to_submit > IORING_SQPOLL_CAP_ENTRIES_VALUE)
171		to_submit = IORING_SQPOLL_CAP_ENTRIES_VALUE;
172
173	if (to_submit || !wq_list_empty(&ctx->iopoll_list)) {
174		const struct cred *creds = NULL;
175
176		if (ctx->sq_creds != current_cred())
177			creds = override_creds(ctx->sq_creds);
178
179		mutex_lock(&ctx->uring_lock);
180		if (!wq_list_empty(&ctx->iopoll_list))
181			io_do_iopoll(ctx, true);
182
183		/*
184		 * Don't submit if refs are dying, good for io_uring_register(),
185		 * but also it is relied upon by io_ring_exit_work()
186		 */
187		if (to_submit && likely(!percpu_ref_is_dying(&ctx->refs)) &&
188		    !(ctx->flags & IORING_SETUP_R_DISABLED))
189			ret = io_submit_sqes(ctx, to_submit);
190		mutex_unlock(&ctx->uring_lock);
191
 
 
 
192		if (to_submit && wq_has_sleeper(&ctx->sqo_sq_wait))
193			wake_up(&ctx->sqo_sq_wait);
194		if (creds)
195			revert_creds(creds);
196	}
197
198	return ret;
199}
200
201static bool io_sqd_handle_event(struct io_sq_data *sqd)
202{
203	bool did_sig = false;
204	struct ksignal ksig;
205
206	if (test_bit(IO_SQ_THREAD_SHOULD_PARK, &sqd->state) ||
207	    signal_pending(current)) {
208		mutex_unlock(&sqd->lock);
209		if (signal_pending(current))
210			did_sig = get_signal(&ksig);
211		wait_event(sqd->wait, !atomic_read(&sqd->park_pending));
212		mutex_lock(&sqd->lock);
213		sqd->sq_cpu = raw_smp_processor_id();
214	}
215	return did_sig || test_bit(IO_SQ_THREAD_SHOULD_STOP, &sqd->state);
216}
217
218/*
219 * Run task_work, processing the retry_list first. The retry_list holds
220 * entries that we passed on in the previous run, if we had more task_work
221 * than we were asked to process. Newly queued task_work isn't run until the
222 * retry list has been fully processed.
223 */
224static unsigned int io_sq_tw(struct llist_node **retry_list, int max_entries)
225{
226	struct io_uring_task *tctx = current->io_uring;
227	unsigned int count = 0;
228
229	if (*retry_list) {
230		*retry_list = io_handle_tw_list(*retry_list, &count, max_entries);
231		if (count >= max_entries)
232			goto out;
233		max_entries -= count;
234	}
235	*retry_list = tctx_task_work_run(tctx, max_entries, &count);
236out:
237	if (task_work_pending(current))
238		task_work_run();
239	return count;
240}
241
242static bool io_sq_tw_pending(struct llist_node *retry_list)
243{
244	struct io_uring_task *tctx = current->io_uring;
245
246	return retry_list || !llist_empty(&tctx->task_list);
247}
248
249static void io_sq_update_worktime(struct io_sq_data *sqd, struct rusage *start)
250{
251	struct rusage end;
252
253	getrusage(current, RUSAGE_SELF, &end);
254	end.ru_stime.tv_sec -= start->ru_stime.tv_sec;
255	end.ru_stime.tv_usec -= start->ru_stime.tv_usec;
256
257	sqd->work_time += end.ru_stime.tv_usec + end.ru_stime.tv_sec * 1000000;
258}
259
260static int io_sq_thread(void *data)
261{
262	struct llist_node *retry_list = NULL;
263	struct io_sq_data *sqd = data;
264	struct io_ring_ctx *ctx;
265	struct rusage start;
266	unsigned long timeout = 0;
267	char buf[TASK_COMM_LEN];
268	DEFINE_WAIT(wait);
269
270	/* offload context creation failed, just exit */
271	if (!current->io_uring) {
272		mutex_lock(&sqd->lock);
273		sqd->thread = NULL;
274		mutex_unlock(&sqd->lock);
275		goto err_out;
276	}
277
278	snprintf(buf, sizeof(buf), "iou-sqp-%d", sqd->task_pid);
279	set_task_comm(current, buf);
280
281	/* reset to our pid after we've set task_comm, for fdinfo */
282	sqd->task_pid = current->pid;
283
284	if (sqd->sq_cpu != -1) {
285		set_cpus_allowed_ptr(current, cpumask_of(sqd->sq_cpu));
286	} else {
287		set_cpus_allowed_ptr(current, cpu_online_mask);
288		sqd->sq_cpu = raw_smp_processor_id();
289	}
290
291	/*
292	 * Force audit context to get setup, in case we do prep side async
293	 * operations that would trigger an audit call before any issue side
294	 * audit has been done.
295	 */
296	audit_uring_entry(IORING_OP_NOP);
297	audit_uring_exit(true, 0);
298
299	mutex_lock(&sqd->lock);
300	while (1) {
301		bool cap_entries, sqt_spin = false;
302
303		if (io_sqd_events_pending(sqd) || signal_pending(current)) {
304			if (io_sqd_handle_event(sqd))
305				break;
306			timeout = jiffies + sqd->sq_thread_idle;
307		}
308
309		cap_entries = !list_is_singular(&sqd->ctx_list);
310		getrusage(current, RUSAGE_SELF, &start);
311		list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) {
312			int ret = __io_sq_thread(ctx, cap_entries);
313
314			if (!sqt_spin && (ret > 0 || !wq_list_empty(&ctx->iopoll_list)))
315				sqt_spin = true;
316		}
317		if (io_sq_tw(&retry_list, IORING_TW_CAP_ENTRIES_VALUE))
318			sqt_spin = true;
319
320		list_for_each_entry(ctx, &sqd->ctx_list, sqd_list)
321			if (io_napi(ctx))
322				io_napi_sqpoll_busy_poll(ctx);
323
324		if (sqt_spin || !time_after(jiffies, timeout)) {
325			if (sqt_spin) {
326				io_sq_update_worktime(sqd, &start);
327				timeout = jiffies + sqd->sq_thread_idle;
328			}
329			if (unlikely(need_resched())) {
330				mutex_unlock(&sqd->lock);
331				cond_resched();
332				mutex_lock(&sqd->lock);
333				sqd->sq_cpu = raw_smp_processor_id();
334			}
335			continue;
336		}
337
338		prepare_to_wait(&sqd->wait, &wait, TASK_INTERRUPTIBLE);
339		if (!io_sqd_events_pending(sqd) && !io_sq_tw_pending(retry_list)) {
340			bool needs_sched = true;
341
342			list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) {
343				atomic_or(IORING_SQ_NEED_WAKEUP,
344						&ctx->rings->sq_flags);
345				if ((ctx->flags & IORING_SETUP_IOPOLL) &&
346				    !wq_list_empty(&ctx->iopoll_list)) {
347					needs_sched = false;
348					break;
349				}
350
351				/*
352				 * Ensure the store of the wakeup flag is not
353				 * reordered with the load of the SQ tail
354				 */
355				smp_mb__after_atomic();
356
357				if (io_sqring_entries(ctx)) {
358					needs_sched = false;
359					break;
360				}
361			}
362
363			if (needs_sched) {
364				mutex_unlock(&sqd->lock);
365				schedule();
366				mutex_lock(&sqd->lock);
367				sqd->sq_cpu = raw_smp_processor_id();
368			}
369			list_for_each_entry(ctx, &sqd->ctx_list, sqd_list)
370				atomic_andnot(IORING_SQ_NEED_WAKEUP,
371						&ctx->rings->sq_flags);
372		}
373
374		finish_wait(&sqd->wait, &wait);
375		timeout = jiffies + sqd->sq_thread_idle;
376	}
377
378	if (retry_list)
379		io_sq_tw(&retry_list, UINT_MAX);
380
381	io_uring_cancel_generic(true, sqd);
382	sqd->thread = NULL;
383	list_for_each_entry(ctx, &sqd->ctx_list, sqd_list)
384		atomic_or(IORING_SQ_NEED_WAKEUP, &ctx->rings->sq_flags);
385	io_run_task_work();
386	mutex_unlock(&sqd->lock);
387err_out:
388	complete(&sqd->exited);
389	do_exit(0);
390}
391
392void io_sqpoll_wait_sq(struct io_ring_ctx *ctx)
393{
394	DEFINE_WAIT(wait);
395
396	do {
397		if (!io_sqring_full(ctx))
398			break;
399		prepare_to_wait(&ctx->sqo_sq_wait, &wait, TASK_INTERRUPTIBLE);
400
401		if (!io_sqring_full(ctx))
402			break;
403		schedule();
404	} while (!signal_pending(current));
405
406	finish_wait(&ctx->sqo_sq_wait, &wait);
407}
408
409__cold int io_sq_offload_create(struct io_ring_ctx *ctx,
410				struct io_uring_params *p)
411{
412	struct task_struct *task_to_put = NULL;
413	int ret;
414
415	/* Retain compatibility with failing for an invalid attach attempt */
416	if ((ctx->flags & (IORING_SETUP_ATTACH_WQ | IORING_SETUP_SQPOLL)) ==
417				IORING_SETUP_ATTACH_WQ) {
418		CLASS(fd, f)(p->wq_fd);
419		if (fd_empty(f))
 
 
420			return -ENXIO;
421		if (!io_is_uring_fops(fd_file(f)))
 
422			return -EINVAL;
 
 
423	}
424	if (ctx->flags & IORING_SETUP_SQPOLL) {
425		struct task_struct *tsk;
426		struct io_sq_data *sqd;
427		bool attached;
428
429		ret = security_uring_sqpoll();
430		if (ret)
431			return ret;
432
433		sqd = io_get_sq_data(p, &attached);
434		if (IS_ERR(sqd)) {
435			ret = PTR_ERR(sqd);
436			goto err;
437		}
438
439		ctx->sq_creds = get_current_cred();
440		ctx->sq_data = sqd;
441		ctx->sq_thread_idle = msecs_to_jiffies(p->sq_thread_idle);
442		if (!ctx->sq_thread_idle)
443			ctx->sq_thread_idle = HZ;
444
445		io_sq_thread_park(sqd);
446		list_add(&ctx->sqd_list, &sqd->ctx_list);
447		io_sqd_update_thread_idle(sqd);
448		/* don't attach to a dying SQPOLL thread, would be racy */
449		ret = (attached && !sqd->thread) ? -ENXIO : 0;
450		io_sq_thread_unpark(sqd);
451
452		if (ret < 0)
453			goto err;
454		if (attached)
455			return 0;
456
457		if (p->flags & IORING_SETUP_SQ_AFF) {
458			cpumask_var_t allowed_mask;
459			int cpu = p->sq_thread_cpu;
460
461			ret = -EINVAL;
462			if (cpu >= nr_cpu_ids || !cpu_online(cpu))
463				goto err_sqpoll;
464			ret = -ENOMEM;
465			if (!alloc_cpumask_var(&allowed_mask, GFP_KERNEL))
466				goto err_sqpoll;
467			ret = -EINVAL;
468			cpuset_cpus_allowed(current, allowed_mask);
469			if (!cpumask_test_cpu(cpu, allowed_mask)) {
470				free_cpumask_var(allowed_mask);
471				goto err_sqpoll;
472			}
473			free_cpumask_var(allowed_mask);
474			sqd->sq_cpu = cpu;
475		} else {
476			sqd->sq_cpu = -1;
477		}
478
479		sqd->task_pid = current->pid;
480		sqd->task_tgid = current->tgid;
481		tsk = create_io_thread(io_sq_thread, sqd, NUMA_NO_NODE);
482		if (IS_ERR(tsk)) {
483			ret = PTR_ERR(tsk);
484			goto err_sqpoll;
485		}
486
487		sqd->thread = tsk;
488		task_to_put = get_task_struct(tsk);
489		ret = io_uring_alloc_task_context(tsk, ctx);
490		wake_up_new_task(tsk);
491		if (ret)
492			goto err;
493	} else if (p->flags & IORING_SETUP_SQ_AFF) {
494		/* Can't have SQ_AFF without SQPOLL */
495		ret = -EINVAL;
496		goto err;
497	}
498
499	if (task_to_put)
500		put_task_struct(task_to_put);
501	return 0;
502err_sqpoll:
503	complete(&ctx->sq_data->exited);
504err:
505	io_sq_thread_finish(ctx);
506	if (task_to_put)
507		put_task_struct(task_to_put);
508	return ret;
509}
510
511__cold int io_sqpoll_wq_cpu_affinity(struct io_ring_ctx *ctx,
512				     cpumask_var_t mask)
513{
514	struct io_sq_data *sqd = ctx->sq_data;
515	int ret = -EINVAL;
516
517	if (sqd) {
518		io_sq_thread_park(sqd);
519		/* Don't set affinity for a dying thread */
520		if (sqd->thread)
521			ret = io_wq_cpu_affinity(sqd->thread->io_uring, mask);
522		io_sq_thread_unpark(sqd);
523	}
524
525	return ret;
526}
v6.9.4
  1// SPDX-License-Identifier: GPL-2.0
  2/*
  3 * Contains the core associated with submission side polling of the SQ
  4 * ring, offloading submissions from the application to a kernel thread.
  5 */
  6#include <linux/kernel.h>
  7#include <linux/errno.h>
  8#include <linux/file.h>
  9#include <linux/mm.h>
 10#include <linux/slab.h>
 11#include <linux/audit.h>
 12#include <linux/security.h>
 
 13#include <linux/io_uring.h>
 14
 15#include <uapi/linux/io_uring.h>
 16
 17#include "io_uring.h"
 18#include "napi.h"
 19#include "sqpoll.h"
 20
 21#define IORING_SQPOLL_CAP_ENTRIES_VALUE 8
 22#define IORING_TW_CAP_ENTRIES_VALUE	8
 23
 24enum {
 25	IO_SQ_THREAD_SHOULD_STOP = 0,
 26	IO_SQ_THREAD_SHOULD_PARK,
 27};
 28
 29void io_sq_thread_unpark(struct io_sq_data *sqd)
 30	__releases(&sqd->lock)
 31{
 32	WARN_ON_ONCE(sqd->thread == current);
 33
 34	/*
 35	 * Do the dance but not conditional clear_bit() because it'd race with
 36	 * other threads incrementing park_pending and setting the bit.
 37	 */
 38	clear_bit(IO_SQ_THREAD_SHOULD_PARK, &sqd->state);
 39	if (atomic_dec_return(&sqd->park_pending))
 40		set_bit(IO_SQ_THREAD_SHOULD_PARK, &sqd->state);
 41	mutex_unlock(&sqd->lock);
 
 42}
 43
 44void io_sq_thread_park(struct io_sq_data *sqd)
 45	__acquires(&sqd->lock)
 46{
 47	WARN_ON_ONCE(sqd->thread == current);
 48
 49	atomic_inc(&sqd->park_pending);
 50	set_bit(IO_SQ_THREAD_SHOULD_PARK, &sqd->state);
 51	mutex_lock(&sqd->lock);
 52	if (sqd->thread)
 53		wake_up_process(sqd->thread);
 54}
 55
 56void io_sq_thread_stop(struct io_sq_data *sqd)
 57{
 58	WARN_ON_ONCE(sqd->thread == current);
 59	WARN_ON_ONCE(test_bit(IO_SQ_THREAD_SHOULD_STOP, &sqd->state));
 60
 61	set_bit(IO_SQ_THREAD_SHOULD_STOP, &sqd->state);
 62	mutex_lock(&sqd->lock);
 63	if (sqd->thread)
 64		wake_up_process(sqd->thread);
 65	mutex_unlock(&sqd->lock);
 66	wait_for_completion(&sqd->exited);
 67}
 68
 69void io_put_sq_data(struct io_sq_data *sqd)
 70{
 71	if (refcount_dec_and_test(&sqd->refs)) {
 72		WARN_ON_ONCE(atomic_read(&sqd->park_pending));
 73
 74		io_sq_thread_stop(sqd);
 75		kfree(sqd);
 76	}
 77}
 78
 79static __cold void io_sqd_update_thread_idle(struct io_sq_data *sqd)
 80{
 81	struct io_ring_ctx *ctx;
 82	unsigned sq_thread_idle = 0;
 83
 84	list_for_each_entry(ctx, &sqd->ctx_list, sqd_list)
 85		sq_thread_idle = max(sq_thread_idle, ctx->sq_thread_idle);
 86	sqd->sq_thread_idle = sq_thread_idle;
 87}
 88
 89void io_sq_thread_finish(struct io_ring_ctx *ctx)
 90{
 91	struct io_sq_data *sqd = ctx->sq_data;
 92
 93	if (sqd) {
 94		io_sq_thread_park(sqd);
 95		list_del_init(&ctx->sqd_list);
 96		io_sqd_update_thread_idle(sqd);
 97		io_sq_thread_unpark(sqd);
 98
 99		io_put_sq_data(sqd);
100		ctx->sq_data = NULL;
101	}
102}
103
104static struct io_sq_data *io_attach_sq_data(struct io_uring_params *p)
105{
106	struct io_ring_ctx *ctx_attach;
107	struct io_sq_data *sqd;
108	struct fd f;
109
110	f = fdget(p->wq_fd);
111	if (!f.file)
112		return ERR_PTR(-ENXIO);
113	if (!io_is_uring_fops(f.file)) {
114		fdput(f);
115		return ERR_PTR(-EINVAL);
116	}
117
118	ctx_attach = f.file->private_data;
119	sqd = ctx_attach->sq_data;
120	if (!sqd) {
121		fdput(f);
122		return ERR_PTR(-EINVAL);
123	}
124	if (sqd->task_tgid != current->tgid) {
125		fdput(f);
126		return ERR_PTR(-EPERM);
127	}
128
129	refcount_inc(&sqd->refs);
130	fdput(f);
131	return sqd;
132}
133
134static struct io_sq_data *io_get_sq_data(struct io_uring_params *p,
135					 bool *attached)
136{
137	struct io_sq_data *sqd;
138
139	*attached = false;
140	if (p->flags & IORING_SETUP_ATTACH_WQ) {
141		sqd = io_attach_sq_data(p);
142		if (!IS_ERR(sqd)) {
143			*attached = true;
144			return sqd;
145		}
146		/* fall through for EPERM case, setup new sqd/task */
147		if (PTR_ERR(sqd) != -EPERM)
148			return sqd;
149	}
150
151	sqd = kzalloc(sizeof(*sqd), GFP_KERNEL);
152	if (!sqd)
153		return ERR_PTR(-ENOMEM);
154
155	atomic_set(&sqd->park_pending, 0);
156	refcount_set(&sqd->refs, 1);
157	INIT_LIST_HEAD(&sqd->ctx_list);
158	mutex_init(&sqd->lock);
159	init_waitqueue_head(&sqd->wait);
160	init_completion(&sqd->exited);
161	return sqd;
162}
163
164static inline bool io_sqd_events_pending(struct io_sq_data *sqd)
165{
166	return READ_ONCE(sqd->state);
167}
168
169static int __io_sq_thread(struct io_ring_ctx *ctx, bool cap_entries)
170{
171	unsigned int to_submit;
172	int ret = 0;
173
174	to_submit = io_sqring_entries(ctx);
175	/* if we're handling multiple rings, cap submit size for fairness */
176	if (cap_entries && to_submit > IORING_SQPOLL_CAP_ENTRIES_VALUE)
177		to_submit = IORING_SQPOLL_CAP_ENTRIES_VALUE;
178
179	if (!wq_list_empty(&ctx->iopoll_list) || to_submit) {
180		const struct cred *creds = NULL;
181
182		if (ctx->sq_creds != current_cred())
183			creds = override_creds(ctx->sq_creds);
184
185		mutex_lock(&ctx->uring_lock);
186		if (!wq_list_empty(&ctx->iopoll_list))
187			io_do_iopoll(ctx, true);
188
189		/*
190		 * Don't submit if refs are dying, good for io_uring_register(),
191		 * but also it is relied upon by io_ring_exit_work()
192		 */
193		if (to_submit && likely(!percpu_ref_is_dying(&ctx->refs)) &&
194		    !(ctx->flags & IORING_SETUP_R_DISABLED))
195			ret = io_submit_sqes(ctx, to_submit);
196		mutex_unlock(&ctx->uring_lock);
197
198		if (io_napi(ctx))
199			ret += io_napi_sqpoll_busy_poll(ctx);
200
201		if (to_submit && wq_has_sleeper(&ctx->sqo_sq_wait))
202			wake_up(&ctx->sqo_sq_wait);
203		if (creds)
204			revert_creds(creds);
205	}
206
207	return ret;
208}
209
210static bool io_sqd_handle_event(struct io_sq_data *sqd)
211{
212	bool did_sig = false;
213	struct ksignal ksig;
214
215	if (test_bit(IO_SQ_THREAD_SHOULD_PARK, &sqd->state) ||
216	    signal_pending(current)) {
217		mutex_unlock(&sqd->lock);
218		if (signal_pending(current))
219			did_sig = get_signal(&ksig);
220		cond_resched();
221		mutex_lock(&sqd->lock);
222		sqd->sq_cpu = raw_smp_processor_id();
223	}
224	return did_sig || test_bit(IO_SQ_THREAD_SHOULD_STOP, &sqd->state);
225}
226
227/*
228 * Run task_work, processing the retry_list first. The retry_list holds
229 * entries that we passed on in the previous run, if we had more task_work
230 * than we were asked to process. Newly queued task_work isn't run until the
231 * retry list has been fully processed.
232 */
233static unsigned int io_sq_tw(struct llist_node **retry_list, int max_entries)
234{
235	struct io_uring_task *tctx = current->io_uring;
236	unsigned int count = 0;
237
238	if (*retry_list) {
239		*retry_list = io_handle_tw_list(*retry_list, &count, max_entries);
240		if (count >= max_entries)
241			goto out;
242		max_entries -= count;
243	}
244	*retry_list = tctx_task_work_run(tctx, max_entries, &count);
245out:
246	if (task_work_pending(current))
247		task_work_run();
248	return count;
249}
250
251static bool io_sq_tw_pending(struct llist_node *retry_list)
252{
253	struct io_uring_task *tctx = current->io_uring;
254
255	return retry_list || !llist_empty(&tctx->task_list);
256}
257
258static void io_sq_update_worktime(struct io_sq_data *sqd, struct rusage *start)
259{
260	struct rusage end;
261
262	getrusage(current, RUSAGE_SELF, &end);
263	end.ru_stime.tv_sec -= start->ru_stime.tv_sec;
264	end.ru_stime.tv_usec -= start->ru_stime.tv_usec;
265
266	sqd->work_time += end.ru_stime.tv_usec + end.ru_stime.tv_sec * 1000000;
267}
268
269static int io_sq_thread(void *data)
270{
271	struct llist_node *retry_list = NULL;
272	struct io_sq_data *sqd = data;
273	struct io_ring_ctx *ctx;
274	struct rusage start;
275	unsigned long timeout = 0;
276	char buf[TASK_COMM_LEN];
277	DEFINE_WAIT(wait);
278
279	/* offload context creation failed, just exit */
280	if (!current->io_uring)
 
 
 
281		goto err_out;
 
282
283	snprintf(buf, sizeof(buf), "iou-sqp-%d", sqd->task_pid);
284	set_task_comm(current, buf);
285
286	/* reset to our pid after we've set task_comm, for fdinfo */
287	sqd->task_pid = current->pid;
288
289	if (sqd->sq_cpu != -1) {
290		set_cpus_allowed_ptr(current, cpumask_of(sqd->sq_cpu));
291	} else {
292		set_cpus_allowed_ptr(current, cpu_online_mask);
293		sqd->sq_cpu = raw_smp_processor_id();
294	}
295
 
 
 
 
 
 
 
 
296	mutex_lock(&sqd->lock);
297	while (1) {
298		bool cap_entries, sqt_spin = false;
299
300		if (io_sqd_events_pending(sqd) || signal_pending(current)) {
301			if (io_sqd_handle_event(sqd))
302				break;
303			timeout = jiffies + sqd->sq_thread_idle;
304		}
305
306		cap_entries = !list_is_singular(&sqd->ctx_list);
307		getrusage(current, RUSAGE_SELF, &start);
308		list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) {
309			int ret = __io_sq_thread(ctx, cap_entries);
310
311			if (!sqt_spin && (ret > 0 || !wq_list_empty(&ctx->iopoll_list)))
312				sqt_spin = true;
313		}
314		if (io_sq_tw(&retry_list, IORING_TW_CAP_ENTRIES_VALUE))
315			sqt_spin = true;
316
 
 
 
 
317		if (sqt_spin || !time_after(jiffies, timeout)) {
318			if (sqt_spin) {
319				io_sq_update_worktime(sqd, &start);
320				timeout = jiffies + sqd->sq_thread_idle;
321			}
322			if (unlikely(need_resched())) {
323				mutex_unlock(&sqd->lock);
324				cond_resched();
325				mutex_lock(&sqd->lock);
326				sqd->sq_cpu = raw_smp_processor_id();
327			}
328			continue;
329		}
330
331		prepare_to_wait(&sqd->wait, &wait, TASK_INTERRUPTIBLE);
332		if (!io_sqd_events_pending(sqd) && !io_sq_tw_pending(retry_list)) {
333			bool needs_sched = true;
334
335			list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) {
336				atomic_or(IORING_SQ_NEED_WAKEUP,
337						&ctx->rings->sq_flags);
338				if ((ctx->flags & IORING_SETUP_IOPOLL) &&
339				    !wq_list_empty(&ctx->iopoll_list)) {
340					needs_sched = false;
341					break;
342				}
343
344				/*
345				 * Ensure the store of the wakeup flag is not
346				 * reordered with the load of the SQ tail
347				 */
348				smp_mb__after_atomic();
349
350				if (io_sqring_entries(ctx)) {
351					needs_sched = false;
352					break;
353				}
354			}
355
356			if (needs_sched) {
357				mutex_unlock(&sqd->lock);
358				schedule();
359				mutex_lock(&sqd->lock);
360				sqd->sq_cpu = raw_smp_processor_id();
361			}
362			list_for_each_entry(ctx, &sqd->ctx_list, sqd_list)
363				atomic_andnot(IORING_SQ_NEED_WAKEUP,
364						&ctx->rings->sq_flags);
365		}
366
367		finish_wait(&sqd->wait, &wait);
368		timeout = jiffies + sqd->sq_thread_idle;
369	}
370
371	if (retry_list)
372		io_sq_tw(&retry_list, UINT_MAX);
373
374	io_uring_cancel_generic(true, sqd);
375	sqd->thread = NULL;
376	list_for_each_entry(ctx, &sqd->ctx_list, sqd_list)
377		atomic_or(IORING_SQ_NEED_WAKEUP, &ctx->rings->sq_flags);
378	io_run_task_work();
379	mutex_unlock(&sqd->lock);
380err_out:
381	complete(&sqd->exited);
382	do_exit(0);
383}
384
385void io_sqpoll_wait_sq(struct io_ring_ctx *ctx)
386{
387	DEFINE_WAIT(wait);
388
389	do {
390		if (!io_sqring_full(ctx))
391			break;
392		prepare_to_wait(&ctx->sqo_sq_wait, &wait, TASK_INTERRUPTIBLE);
393
394		if (!io_sqring_full(ctx))
395			break;
396		schedule();
397	} while (!signal_pending(current));
398
399	finish_wait(&ctx->sqo_sq_wait, &wait);
400}
401
402__cold int io_sq_offload_create(struct io_ring_ctx *ctx,
403				struct io_uring_params *p)
404{
 
405	int ret;
406
407	/* Retain compatibility with failing for an invalid attach attempt */
408	if ((ctx->flags & (IORING_SETUP_ATTACH_WQ | IORING_SETUP_SQPOLL)) ==
409				IORING_SETUP_ATTACH_WQ) {
410		struct fd f;
411
412		f = fdget(p->wq_fd);
413		if (!f.file)
414			return -ENXIO;
415		if (!io_is_uring_fops(f.file)) {
416			fdput(f);
417			return -EINVAL;
418		}
419		fdput(f);
420	}
421	if (ctx->flags & IORING_SETUP_SQPOLL) {
422		struct task_struct *tsk;
423		struct io_sq_data *sqd;
424		bool attached;
425
426		ret = security_uring_sqpoll();
427		if (ret)
428			return ret;
429
430		sqd = io_get_sq_data(p, &attached);
431		if (IS_ERR(sqd)) {
432			ret = PTR_ERR(sqd);
433			goto err;
434		}
435
436		ctx->sq_creds = get_current_cred();
437		ctx->sq_data = sqd;
438		ctx->sq_thread_idle = msecs_to_jiffies(p->sq_thread_idle);
439		if (!ctx->sq_thread_idle)
440			ctx->sq_thread_idle = HZ;
441
442		io_sq_thread_park(sqd);
443		list_add(&ctx->sqd_list, &sqd->ctx_list);
444		io_sqd_update_thread_idle(sqd);
445		/* don't attach to a dying SQPOLL thread, would be racy */
446		ret = (attached && !sqd->thread) ? -ENXIO : 0;
447		io_sq_thread_unpark(sqd);
448
449		if (ret < 0)
450			goto err;
451		if (attached)
452			return 0;
453
454		if (p->flags & IORING_SETUP_SQ_AFF) {
 
455			int cpu = p->sq_thread_cpu;
456
457			ret = -EINVAL;
458			if (cpu >= nr_cpu_ids || !cpu_online(cpu))
459				goto err_sqpoll;
 
 
 
 
 
 
 
 
 
 
460			sqd->sq_cpu = cpu;
461		} else {
462			sqd->sq_cpu = -1;
463		}
464
465		sqd->task_pid = current->pid;
466		sqd->task_tgid = current->tgid;
467		tsk = create_io_thread(io_sq_thread, sqd, NUMA_NO_NODE);
468		if (IS_ERR(tsk)) {
469			ret = PTR_ERR(tsk);
470			goto err_sqpoll;
471		}
472
473		sqd->thread = tsk;
 
474		ret = io_uring_alloc_task_context(tsk, ctx);
475		wake_up_new_task(tsk);
476		if (ret)
477			goto err;
478	} else if (p->flags & IORING_SETUP_SQ_AFF) {
479		/* Can't have SQ_AFF without SQPOLL */
480		ret = -EINVAL;
481		goto err;
482	}
483
 
 
484	return 0;
485err_sqpoll:
486	complete(&ctx->sq_data->exited);
487err:
488	io_sq_thread_finish(ctx);
 
 
489	return ret;
490}
491
492__cold int io_sqpoll_wq_cpu_affinity(struct io_ring_ctx *ctx,
493				     cpumask_var_t mask)
494{
495	struct io_sq_data *sqd = ctx->sq_data;
496	int ret = -EINVAL;
497
498	if (sqd) {
499		io_sq_thread_park(sqd);
500		/* Don't set affinity for a dying thread */
501		if (sqd->thread)
502			ret = io_wq_cpu_affinity(sqd->thread->io_uring, mask);
503		io_sq_thread_unpark(sqd);
504	}
505
506	return ret;
507}