Skip to content

Commit e12d7a4

Browse files
committed
io_uring/msg_ring: fix missing lock on overflow for IOPOLL
If the target ring is configured with IOPOLL, then we always need to hold the target ring uring_lock before posting CQEs. We could just grab it unconditionally, but since we don't expect many target rings to be of this type, make grabbing the uring_lock conditional on the ring type. Link: https://lore.kernel.org/io-uring/Y8krlYa52%2F0YGqkg@ip-172-31-85-199.ec2.internal/ Reported-by: Xingyuan Mo <hdthky0@gmail.com> Signed-off-by: Jens Axboe <axboe@kernel.dk>
1 parent 423d508 commit e12d7a4

File tree

1 file changed

+30
-9
lines changed

1 file changed

+30
-9
lines changed

io_uring/msg_ring.c

Lines changed: 30 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -65,20 +65,33 @@ static void io_msg_tw_complete(struct callback_head *head)
6565
struct io_ring_ctx *target_ctx = req->file->private_data;
6666
int ret = 0;
6767

68-
if (current->flags & PF_EXITING)
68+
if (current->flags & PF_EXITING) {
6969
ret = -EOWNERDEAD;
70-
else if (!io_post_aux_cqe(target_ctx, msg->user_data, msg->len, 0))
71-
ret = -EOVERFLOW;
70+
} else {
71+
/*
72+
* If the target ring is using IOPOLL mode, then we need to be
73+
* holding the uring_lock for posting completions. Other ring
74+
* types rely on the regular completion locking, which is
75+
* handled while posting.
76+
*/
77+
if (target_ctx->flags & IORING_SETUP_IOPOLL)
78+
mutex_lock(&target_ctx->uring_lock);
79+
if (!io_post_aux_cqe(target_ctx, msg->user_data, msg->len, 0))
80+
ret = -EOVERFLOW;
81+
if (target_ctx->flags & IORING_SETUP_IOPOLL)
82+
mutex_unlock(&target_ctx->uring_lock);
83+
}
7284

7385
if (ret < 0)
7486
req_set_fail(req);
7587
io_req_queue_tw_complete(req, ret);
7688
}
7789

78-
static int io_msg_ring_data(struct io_kiocb *req)
90+
static int io_msg_ring_data(struct io_kiocb *req, unsigned int issue_flags)
7991
{
8092
struct io_ring_ctx *target_ctx = req->file->private_data;
8193
struct io_msg *msg = io_kiocb_to_cmd(req, struct io_msg);
94+
int ret;
8295

8396
if (msg->src_fd || msg->dst_fd || msg->flags)
8497
return -EINVAL;
@@ -93,10 +106,18 @@ static int io_msg_ring_data(struct io_kiocb *req)
93106
return IOU_ISSUE_SKIP_COMPLETE;
94107
}
95108

96-
if (io_post_aux_cqe(target_ctx, msg->user_data, msg->len, 0))
97-
return 0;
98-
99-
return -EOVERFLOW;
109+
ret = -EOVERFLOW;
110+
if (target_ctx->flags & IORING_SETUP_IOPOLL) {
111+
if (unlikely(io_double_lock_ctx(target_ctx, issue_flags)))
112+
return -EAGAIN;
113+
if (io_post_aux_cqe(target_ctx, msg->user_data, msg->len, 0))
114+
ret = 0;
115+
io_double_unlock_ctx(target_ctx);
116+
} else {
117+
if (io_post_aux_cqe(target_ctx, msg->user_data, msg->len, 0))
118+
ret = 0;
119+
}
120+
return ret;
100121
}
101122

102123
static struct file *io_msg_grab_file(struct io_kiocb *req, unsigned int issue_flags)
@@ -223,7 +244,7 @@ int io_msg_ring(struct io_kiocb *req, unsigned int issue_flags)
223244

224245
switch (msg->cmd) {
225246
case IORING_MSG_DATA:
226-
ret = io_msg_ring_data(req);
247+
ret = io_msg_ring_data(req, issue_flags);
227248
break;
228249
case IORING_MSG_SEND_FD:
229250
ret = io_msg_send_fd(req, issue_flags);

0 commit comments

Comments
 (0)