Skip to content

Commit 6d043ee

Browse files
isilenceaxboe
authored andcommitted
io_uring: do msg_ring in target task via tw
While executing in a context of one io_uring instance msg_ring manipulates another ring. We're trying to keep CQEs posting contained in the context of the ring-owner task, use task_work to send the request to the target ring's task when we're modifying its CQ or trying to install a file. Note, we can't safely use io_uring task_work infra and have to use task_work directly. Signed-off-by: Pavel Begunkov <[email protected]> Link: https://lore.kernel.org/r/4d76c7b28ed5d71b520de4482fbb7f660f21cd80.1670384893.git.asml.silence@gmail.com [axboe: use TWA_SIGNAL_NO_IPI] Signed-off-by: Jens Axboe <[email protected]>
1 parent 1721131 commit 6d043ee

File tree

1 file changed

+53
-3
lines changed

1 file changed

+53
-3
lines changed

io_uring/msg_ring.c

Lines changed: 53 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
struct io_msg {
1717
struct file *file;
1818
struct file *src_file;
19+
struct callback_head tw;
1920
u64 user_data;
2021
u32 len;
2122
u32 cmd;
@@ -35,6 +36,23 @@ void io_msg_ring_cleanup(struct io_kiocb *req)
3536
msg->src_file = NULL;
3637
}
3738

39+
static void io_msg_tw_complete(struct callback_head *head)
40+
{
41+
struct io_msg *msg = container_of(head, struct io_msg, tw);
42+
struct io_kiocb *req = cmd_to_io_kiocb(msg);
43+
struct io_ring_ctx *target_ctx = req->file->private_data;
44+
int ret = 0;
45+
46+
if (current->flags & PF_EXITING)
47+
ret = -EOWNERDEAD;
48+
else if (!io_post_aux_cqe(target_ctx, msg->user_data, msg->len, 0))
49+
ret = -EOVERFLOW;
50+
51+
if (ret < 0)
52+
req_set_fail(req);
53+
io_req_queue_tw_complete(req, ret);
54+
}
55+
3856
static int io_msg_ring_data(struct io_kiocb *req)
3957
{
4058
struct io_ring_ctx *target_ctx = req->file->private_data;
@@ -43,6 +61,15 @@ static int io_msg_ring_data(struct io_kiocb *req)
4361
if (msg->src_fd || msg->dst_fd || msg->flags)
4462
return -EINVAL;
4563

64+
if (target_ctx->task_complete && current != target_ctx->submitter_task) {
65+
init_task_work(&msg->tw, io_msg_tw_complete);
66+
if (task_work_add(target_ctx->submitter_task, &msg->tw,
67+
TWA_SIGNAL_NO_IPI))
68+
return -EOWNERDEAD;
69+
70+
return IOU_ISSUE_SKIP_COMPLETE;
71+
}
72+
4673
if (io_post_aux_cqe(target_ctx, msg->user_data, msg->len, 0))
4774
return 0;
4875

@@ -124,6 +151,19 @@ static int io_msg_install_complete(struct io_kiocb *req, unsigned int issue_flag
124151
return ret;
125152
}
126153

154+
static void io_msg_tw_fd_complete(struct callback_head *head)
155+
{
156+
struct io_msg *msg = container_of(head, struct io_msg, tw);
157+
struct io_kiocb *req = cmd_to_io_kiocb(msg);
158+
int ret = -EOWNERDEAD;
159+
160+
if (!(current->flags & PF_EXITING))
161+
ret = io_msg_install_complete(req, IO_URING_F_UNLOCKED);
162+
if (ret < 0)
163+
req_set_fail(req);
164+
io_req_queue_tw_complete(req, ret);
165+
}
166+
127167
static int io_msg_send_fd(struct io_kiocb *req, unsigned int issue_flags)
128168
{
129169
struct io_ring_ctx *target_ctx = req->file->private_data;
@@ -140,6 +180,15 @@ static int io_msg_send_fd(struct io_kiocb *req, unsigned int issue_flags)
140180
msg->src_file = src_file;
141181
req->flags |= REQ_F_NEED_CLEANUP;
142182
}
183+
184+
if (target_ctx->task_complete && current != target_ctx->submitter_task) {
185+
init_task_work(&msg->tw, io_msg_tw_fd_complete);
186+
if (task_work_add(target_ctx->submitter_task, &msg->tw,
187+
TWA_SIGNAL))
188+
return -EOWNERDEAD;
189+
190+
return IOU_ISSUE_SKIP_COMPLETE;
191+
}
143192
return io_msg_install_complete(req, issue_flags);
144193
}
145194

@@ -185,10 +234,11 @@ int io_msg_ring(struct io_kiocb *req, unsigned int issue_flags)
185234
}
186235

187236
done:
188-
if (ret == -EAGAIN)
189-
return -EAGAIN;
190-
if (ret < 0)
237+
if (ret < 0) {
238+
if (ret == -EAGAIN || ret == IOU_ISSUE_SKIP_COMPLETE)
239+
return ret;
191240
req_set_fail(req);
241+
}
192242
io_req_set_res(req, ret, 0);
193243
return IOU_OK;
194244
}

0 commit comments

Comments
 (0)