Skip to contentSkip to conversation

Restore sq_thread 5.11 behavior

PATCH In review Stefan Metzmacher 2 participants 4 messages Mar 7, 2021

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

cover Restore sq_thread 5.11 behavior

Hi Jens,

I guess on top of your "io-wq: always track creds for async issue" we want to following to patches in order to restore the sq_thread behavior of 5.11 (and older) with IORING_SETUP_ATTACH_WQ.

- io_uring: run __io_sq_thread() with the initial creds
- io_uring: kill io_sq_thread_fork() and return -EOWNERDEAD
- io_
I've not tested them, but they compile and as far as I read the 5.11 code, they should restore the old behavior.

metze

#1 lore raw

PATCH 1/2 io_uring: run __io_sq_thread() with the initial creds from io_uring_setup()

With IORING_SETUP_ATTACH_WQ we should let __io_sq_thread() use the initial creds from each ctx.

---
 fs/io_uring.c | 10 ++++++++++
 1 file changed, 10 insertions(+)
fs/io_uring.c +10 -0
index 3b8838123cc5..133b52a9a768 100644
@@ -380,6 +380,7 @@ struct io_ring_ctx {
 	/* Only used for accounting purposes */
 	struct mm_struct	*mm_account;
 
+	const struct cred	*sq_creds;	/* cred used for __io_sq_thread() */
 	struct io_sq_data	*sq_data;	/* if using sq thread polling */
 
 	struct wait_queue_head	sqo_sq_wait;
@@ -6719,7 +6720,13 @@ static int io_sq_thread(void *data)
 		sqt_spin = false;
 		cap_entries = !list_is_singular(&sqd->ctx_list);
 		list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) {
+			const struct cred *creds = NULL;
+
+			if (ctx->sq_creds != current_cred())
+				creds = override_creds(ctx->sq_creds);
 			ret = __io_sq_thread(ctx, cap_entries);
+			if (creds)
+				revert_creds(creds);
 			if (!sqt_spin && (ret > 0 || !list_empty(&ctx->iopoll_list)))
 				sqt_spin = true;
 		}
@@ -7150,6 +7157,8 @@ static void io_sq_thread_finish(struct io_ring_ctx *ctx)
 
 		io_put_sq_data(sqd);
 		ctx->sq_data = NULL;
+		if (ctx->sq_creds)
+			put_cred(ctx->sq_creds);
 	}
 }
 
@@ -7888,6 +7897,7 @@ static int io_sq_offload_create(struct io_ring_ctx *ctx,
 			goto err;
 		}
 
+		ctx->sq_creds = get_current_cred();
 		ctx->sq_data = sqd;
 		io_sq_thread_park(sqd);
 		mutex_lock(&sqd->ctx_lock);
Signature · 1 line
2.25.1
#2 lore raw

PATCH 2/2 io_uring: kill io_sq_thread_fork() and return -EOWNERDEAD if the sq_thread is gone

In reply to Stefan Metzmacher
---
 fs/io_uring.c | 31 +++----------------------------
 1 file changed, 3 insertions(+), 28 deletions(-)
fs/io_uring.c +3 -28
index 133b52a9a768..6487f9b2c3a7 100644
@@ -336,7 +336,6 @@ struct io_ring_ctx {
 		unsigned int		drain_next: 1;
 		unsigned int		eventfd_async: 1;
 		unsigned int		restricted: 1;
-		unsigned int		sqo_exec: 1;
 
 		/*
 		 * Ring buffer of indices into array of io_uring_sqe, which is
@@ -6786,7 +6785,6 @@ static int io_sq_thread(void *data)
 
 	sqd->thread = NULL;
 	list_for_each_entry(ctx, &sqd->ctx_list, sqd_list) {
-		ctx->sqo_exec = 1;
 		io_ring_set_wakeup_flag(ctx);
 	}
 
@@ -7844,26 +7842,6 @@ void __io_uring_free(struct task_struct *tsk)
 	tsk->io_uring = NULL;
 }
 
-static int io_sq_thread_fork(struct io_sq_data *sqd, struct io_ring_ctx *ctx)
-{
-	struct task_struct *tsk;
-	int ret;
-
-	clear_bit(IO_SQ_THREAD_SHOULD_STOP, &sqd->state);
-	reinit_completion(&sqd->parked);
-	ctx->sqo_exec = 0;
-	sqd->task_pid = current->pid;
-	tsk = create_io_thread(io_sq_thread, sqd, NUMA_NO_NODE);
-	if (IS_ERR(tsk))
-		return PTR_ERR(tsk);
-	ret = io_uring_alloc_task_context(tsk, ctx);
-	if (ret)
-		set_bit(IO_SQ_THREAD_SHOULD_STOP, &sqd->state);
-	sqd->thread = tsk;
-	wake_up_new_task(tsk);
-	return ret;
-}
-
 static int io_sq_offload_create(struct io_ring_ctx *ctx,
 				struct io_uring_params *p)
 {
@@ -9197,13 +9175,10 @@ SYSCALL_DEFINE6(io_uring_enter, unsigned int, fd, u32, to_submit,
 	if (ctx->flags & IORING_SETUP_SQPOLL) {
 		io_cqring_overflow_flush(ctx, false, NULL, NULL);
 
-		if (unlikely(ctx->sqo_exec)) {
-			ret = io_sq_thread_fork(ctx->sq_data, ctx);
-			if (ret)
-				goto out;
-			ctx->sqo_exec = 0;
-		}
 		ret = -EOWNERDEAD;
+		if (unlikely(ctx->sq_data->thread == NULL)) {
+			goto out;
+		}
 		if (flags & IORING_ENTER_SQ_WAKEUP)
 			wake_up(&ctx->sq_data->wait);
 		if (flags & IORING_ENTER_SQ_WAIT) {
Signature · 1 line
2.25.1
maintainer
#3 lore raw
In reply to Stefan Metzmacher
Quoted 11 lines · Stefan Metzmacher

Hi Jens,

I guess on top of your "io-wq: always track creds for async issue" we want to following to patches in order to restore the sq_thread behavior of 5.11 (and older) with IORING_SETUP_ATTACH_WQ.

- io_uring: run __io_sq_thread() with the initial creds
- io_uring: kill io_sq_thread_fork() and return -EOWNERDEAD
- io_
I've not tested them, but they compile and as far as I read the 5.11 code, they should restore the old behavior.

Thanks this looks good, I'll apply them. There's really no point to the fork handling of SQPOLL anymore, since the unshare code went away.

Signature · 1 line
Jens Axboe