Skip to content

Commit 0031275

Browse files
metze-sambaaxboe
authored andcommitted
io_uring: call req_set_fail_links() on short send[msg]()/recv[msg]() with MSG_WAITALL
Without that it's not safe to use them in a linked combination with others. Now combinations like IORING_OP_SENDMSG followed by IORING_OP_SPLICE should be possible. We already handle short reads and writes for the following opcodes: - IORING_OP_READV - IORING_OP_READ_FIXED - IORING_OP_READ - IORING_OP_WRITEV - IORING_OP_WRITE_FIXED - IORING_OP_WRITE - IORING_OP_SPLICE - IORING_OP_TEE Now we have it for these as well: - IORING_OP_SENDMSG - IORING_OP_SEND - IORING_OP_RECVMSG - IORING_OP_RECV For IORING_OP_RECVMSG we also check for the MSG_TRUNC and MSG_CTRUNC flags in order to call req_set_fail_links(). There might be applications arround depending on the behavior that even short send[msg]()/recv[msg]() retuns continue an IOSQE_IO_LINK chain. It's very unlikely that such applications pass in MSG_WAITALL, which is only defined in 'man 2 recvmsg', but not in 'man 2 sendmsg'. It's expected that the low level sock_sendmsg() call just ignores MSG_WAITALL, as MSG_ZEROCOPY is also ignored without explicitly set SO_ZEROCOPY. We also expect the caller to know about the implicit truncation to MAX_RW_COUNT, which we don't detect. cc: [email protected] Link: https://lore.kernel.org/r/c4e1a4cc0d905314f4d5dc567e65a7b09621aab3.1615908477.git.metze@samba.org Signed-off-by: Stefan Metzmacher <[email protected]> Signed-off-by: Jens Axboe <[email protected]>
1 parent 00ddff4 commit 0031275

File tree

1 file changed

+20
-4
lines changed

1 file changed

+20
-4
lines changed

fs/io_uring.c

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4386,6 +4386,7 @@ static int io_sendmsg(struct io_kiocb *req, unsigned int issue_flags)
43864386
struct io_async_msghdr iomsg, *kmsg;
43874387
struct socket *sock;
43884388
unsigned flags;
4389+
int min_ret = 0;
43894390
int ret;
43904391

43914392
sock = sock_from_file(req->file);
@@ -4406,6 +4407,9 @@ static int io_sendmsg(struct io_kiocb *req, unsigned int issue_flags)
44064407
else if (issue_flags & IO_URING_F_NONBLOCK)
44074408
flags |= MSG_DONTWAIT;
44084409

4410+
if (flags & MSG_WAITALL)
4411+
min_ret = iov_iter_count(&kmsg->msg.msg_iter);
4412+
44094413
ret = __sys_sendmsg_sock(sock, &kmsg->msg, flags);
44104414
if ((issue_flags & IO_URING_F_NONBLOCK) && ret == -EAGAIN)
44114415
return io_setup_async_msg(req, kmsg);
@@ -4416,7 +4420,7 @@ static int io_sendmsg(struct io_kiocb *req, unsigned int issue_flags)
44164420
if (kmsg->free_iov)
44174421
kfree(kmsg->free_iov);
44184422
req->flags &= ~REQ_F_NEED_CLEANUP;
4419-
if (ret < 0)
4423+
if (ret < min_ret)
44204424
req_set_fail_links(req);
44214425
__io_req_complete(req, issue_flags, ret, 0);
44224426
return 0;
@@ -4429,6 +4433,7 @@ static int io_send(struct io_kiocb *req, unsigned int issue_flags)
44294433
struct iovec iov;
44304434
struct socket *sock;
44314435
unsigned flags;
4436+
int min_ret = 0;
44324437
int ret;
44334438

44344439
sock = sock_from_file(req->file);
@@ -4450,14 +4455,17 @@ static int io_send(struct io_kiocb *req, unsigned int issue_flags)
44504455
else if (issue_flags & IO_URING_F_NONBLOCK)
44514456
flags |= MSG_DONTWAIT;
44524457

4458+
if (flags & MSG_WAITALL)
4459+
min_ret = iov_iter_count(&msg.msg_iter);
4460+
44534461
msg.msg_flags = flags;
44544462
ret = sock_sendmsg(sock, &msg);
44554463
if ((issue_flags & IO_URING_F_NONBLOCK) && ret == -EAGAIN)
44564464
return -EAGAIN;
44574465
if (ret == -ERESTARTSYS)
44584466
ret = -EINTR;
44594467

4460-
if (ret < 0)
4468+
if (ret < min_ret)
44614469
req_set_fail_links(req);
44624470
__io_req_complete(req, issue_flags, ret, 0);
44634471
return 0;
@@ -4609,6 +4617,7 @@ static int io_recvmsg(struct io_kiocb *req, unsigned int issue_flags)
46094617
struct socket *sock;
46104618
struct io_buffer *kbuf;
46114619
unsigned flags;
4620+
int min_ret = 0;
46124621
int ret, cflags = 0;
46134622
bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK;
46144623

@@ -4640,6 +4649,9 @@ static int io_recvmsg(struct io_kiocb *req, unsigned int issue_flags)
46404649
else if (force_nonblock)
46414650
flags |= MSG_DONTWAIT;
46424651

4652+
if (flags & MSG_WAITALL)
4653+
min_ret = iov_iter_count(&kmsg->msg.msg_iter);
4654+
46434655
ret = __sys_recvmsg_sock(sock, &kmsg->msg, req->sr_msg.umsg,
46444656
kmsg->uaddr, flags);
46454657
if (force_nonblock && ret == -EAGAIN)
@@ -4653,7 +4665,7 @@ static int io_recvmsg(struct io_kiocb *req, unsigned int issue_flags)
46534665
if (kmsg->free_iov)
46544666
kfree(kmsg->free_iov);
46554667
req->flags &= ~REQ_F_NEED_CLEANUP;
4656-
if (ret < 0)
4668+
if (ret < min_ret || ((flags & MSG_WAITALL) && (kmsg->msg.msg_flags & (MSG_TRUNC | MSG_CTRUNC))))
46574669
req_set_fail_links(req);
46584670
__io_req_complete(req, issue_flags, ret, cflags);
46594671
return 0;
@@ -4668,6 +4680,7 @@ static int io_recv(struct io_kiocb *req, unsigned int issue_flags)
46684680
struct socket *sock;
46694681
struct iovec iov;
46704682
unsigned flags;
4683+
int min_ret = 0;
46714684
int ret, cflags = 0;
46724685
bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK;
46734686

@@ -4699,6 +4712,9 @@ static int io_recv(struct io_kiocb *req, unsigned int issue_flags)
46994712
else if (force_nonblock)
47004713
flags |= MSG_DONTWAIT;
47014714

4715+
if (flags & MSG_WAITALL)
4716+
min_ret = iov_iter_count(&msg.msg_iter);
4717+
47024718
ret = sock_recvmsg(sock, &msg, flags);
47034719
if (force_nonblock && ret == -EAGAIN)
47044720
return -EAGAIN;
@@ -4707,7 +4723,7 @@ static int io_recv(struct io_kiocb *req, unsigned int issue_flags)
47074723
out_free:
47084724
if (req->flags & REQ_F_BUFFER_SELECTED)
47094725
cflags = io_put_recv_kbuf(req);
4710-
if (ret < 0)
4726+
if (ret < min_ret || ((flags & MSG_WAITALL) && (msg.msg_flags & (MSG_TRUNC | MSG_CTRUNC))))
47114727
req_set_fail_links(req);
47124728
__io_req_complete(req, issue_flags, ret, cflags);
47134729
return 0;

0 commit comments

Comments
 (0)