Skip to contentSkip to conversation

io_uring/net: fix a multishot termination case for recv

PATCH In review Jens Axboe 2 participants 4 messages Sep 28, 2024 – Sep 30, 2024 io_uring

lore
Share & tools
lore

Jens Axboe

author maintainer
#0 lore raw

PATCH 1/1 io_uring/net: fix a multishot termination case for recv

If the recv returns zero, or an error, then it doesn't matter if more data has already been received for this buffer. A condition like that should terminate the multishot receive. Rather than pass in the collected return value, pass in whether to terminate or keep the recv going separately.

Fixes b3fdea6ecb55 ("io_uring: multishot recv")

---

io_uring/net.c +3 -1
index f10f5a22d66a..18507658a921 100644
@@ -1133,6 +1133,7 @@ int io_recv(struct io_kiocb *req, unsigned int issue_flags)
 	int ret, min_ret = 0;
 	bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK;
 	size_t len = sr->len;
+	bool mshot_finished;
 
 	if (!(req->flags & REQ_F_POLLED) &&
 	    (sr->flags & IORING_RECVSEND_POLL_FIRST))
@@ -1187,6 +1188,7 @@ int io_recv(struct io_kiocb *req, unsigned int issue_flags)
 		req_set_fail(req);
 	}
 
+	mshot_finished = ret <= 0;
 	if (ret > 0)
 		ret += sr->done_io;
 	else if (sr->done_io)
@@ -1194,7 +1196,7 @@ int io_recv(struct io_kiocb *req, unsigned int issue_flags)
 	else
 		io_kbuf_recycle(req, issue_flags);
 
-	if (!io_recv_finish(req, &ret, kmsg, ret <= 0, issue_flags))
+	if (!io_recv_finish(req, &ret, kmsg, mshot_finished, issue_flags))
 		goto retry_multishot;
 
 	return ret;
Signature · 1 line
Jens Axboe
author maintainer
#1 lore raw
Quoted 21 lines · Jens Axboe
io_uring/net.c +2 -0
index f10f5a22d66a..18507658a921 100644
@@ -1133,6 +1133,7 @@ int io_recv(struct io_kiocb *req, unsigned int issue_flags)
 	int ret, min_ret = 0;
 	bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK;
 	size_t len = sr->len;
+	bool mshot_finished;
 
 	if (!(req->flags & REQ_F_POLLED) &&
 	    (sr->flags & IORING_RECVSEND_POLL_FIRST))
@@ -1187,6 +1188,7 @@ int io_recv(struct io_kiocb *req, unsigned int issue_flags)
 		req_set_fail(req);
 	}
 
+	mshot_finished = ret <= 0;
 	if (ret > 0)
 		ret += sr->done_io;
 	else if (sr->done_io)

io_uring/net.c int io_recv(struct io_kiocb *req, unsigned int issue_flags) · line 1202

Quoted 8 lines
io_uring/net.c +1 -1
@@ -1194,7 +1196,7 @@ int io_recv(struct io_kiocb *req, unsigned int issue_flags)
 	else
 		io_kbuf_recycle(req, issue_flags);
 
-	if (!io_recv_finish(req, &ret, kmsg, ret <= 0, issue_flags))
+	if (!io_recv_finish(req, &ret, kmsg, mshot_finished, issue_flags))
 		goto retry_multishot;
 
 	return ret;

On second thought, I don't think we can get into this situation - sr->done_io is only ever used for recv if we had to retry after getting some data. And that only happens if MSG_WAITALL is set, which is not legal for multishot and will result in -EINVAL. So don't quite see how we can run into this issue. But I could be missing something...

Comments?

Signature · 1 line
Jens Axboe
#2 lore raw
Quoted 37 lines · Jens Axboe
Quoted 21 lines · Jens Axboe
io_uring/net.c +2 -0
index f10f5a22d66a..18507658a921 100644
@@ -1133,6 +1133,7 @@ int io_recv(struct io_kiocb *req, unsigned int issue_flags)
  	int ret, min_ret = 0;
  	bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK;
  	size_t len = sr->len;
+	bool mshot_finished;
 
  	if (!(req->flags & REQ_F_POLLED) &&
  	    (sr->flags & IORING_RECVSEND_POLL_FIRST))
@@ -1187,6 +1188,7 @@ int io_recv(struct io_kiocb *req, unsigned int issue_flags)
  		req_set_fail(req);
  	}
 
+	mshot_finished = ret <= 0;
  	if (ret > 0)
  		ret += sr->done_io;
  	else if (sr->done_io)

io_uring/net.c int io_recv(struct io_kiocb *req, unsigned int issue_flags) · line 1202

Quoted 8 lines
io_uring/net.c +1 -1
@@ -1194,7 +1196,7 @@ int io_recv(struct io_kiocb *req, unsigned int issue_flags)
  	else
  		io_kbuf_recycle(req, issue_flags);
 
-	if (!io_recv_finish(req, &ret, kmsg, ret <= 0, issue_flags))
+	if (!io_recv_finish(req, &ret, kmsg, mshot_finished, issue_flags))
  		goto retry_multishot;
 
  	return ret;

On second thought, I don't think we can get into this situation - sr->done_io is only ever used for recv if we had to retry after getting some data. And that only happens if MSG_WAITALL is set, which is not legal for multishot and will result in -EINVAL. So don't quite see how we can run into this issue. But I could be missing something...

Quoted 1 line

Comments?

I noticed the chunk months ago, it's definitely a sloppy one, but I deemed it not to be an actual problem after trying to exploit it at the moment.

Signature · 1 line
Pavel Begunkov
author maintainer
#3 lore raw
Quoted 34 lines · Pavel Begunkov
Quoted 33 lines · Jens Axboe
Quoted 19 lines · Jens Axboe
io_uring/net.c +1 -0
index f10f5a22d66a..18507658a921 100644
@@ -1133,6 +1133,7 @@ int io_recv(struct io_kiocb *req, unsigned int issue_flags)
      int ret, min_ret = 0;
      bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK;
      size_t len = sr->len;
+    bool mshot_finished;
        if (!(req->flags & REQ_F_POLLED) &&
          (sr->flags & IORING_RECVSEND_POLL_FIRST))
@@ -1187,6 +1188,7 @@ int io_recv(struct io_kiocb *req, unsigned int issue_flags)
          req_set_fail(req);
      }
  +    mshot_finished = ret <= 0;
      if (ret > 0)
          ret += sr->done_io;
      else if (sr->done_io)

io_uring/net.c int io_recv(struct io_kiocb *req, unsigned int issue_flags) · line 1201

Quoted 6 lines
io_uring/net.c +1 -0
@@ -1194,7 +1196,7 @@ int io_recv(struct io_kiocb *req, unsigned int issue_flags)
      else
          io_kbuf_recycle(req, issue_flags);
  -    if (!io_recv_finish(req, &ret, kmsg, ret <= 0, issue_flags))
+    if (!io_recv_finish(req, &ret, kmsg, mshot_finished, issue_flags))
          goto retry_multishot;
        return ret;

On second thought, I don't think we can get into this situation - sr->done_io is only ever used for recv if we had to retry after getting some data. And that only happens if MSG_WAITALL is set, which is not legal for multishot and will result in -EINVAL. So don't quite see how we can run into this issue. But I could be missing something...

Quoted 5 lines
Quoted 1 line

Comments?

I noticed the chunk months ago, it's definitely a sloppy one, but I deemed it not to be an actual problem after trying to exploit it at the moment.

Yes, might not be a bad idea to still do the tweak. Not because we can _currently_ hit it, but because it could be possible in the future and just get overlooked.

Thanks for confirming :-)

Signature · 1 line
Jens Axboe