public inbox for io-uring@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH v2] io_uring/net: Support multishot receive len cap
@ 2025-07-15 14:02 norman.maurer
  2025-07-15 17:32 ` Jens Axboe
  2025-07-16 14:28 ` Jens Axboe
  0 siblings, 2 replies; 3+ messages in thread
From: norman.maurer @ 2025-07-15 14:02 UTC (permalink / raw)
  To: io-uring; +Cc: axboe, Norman Maurer

From: Norman Maurer <norman_maurer@apple.com>

At the moment its very hard to do fine grained backpressure when using
multishot as the kernel might produce a lot of completions before the
user has a chance to cancel a previous submitted multishot recv.

This change adds support to issue a multishot recv that is capped by a
len, which means the kernel will only rearm until X amount of data is
received. When the limit is reached the completion will signal to the
user that a re-arm needs to happen manually by not setting the IORING_CQE_F_MORE
flag.

Signed-off-by: Norman Maurer <norman_maurer@apple.com>
---
Changes since v1: Correct author, include Signed-off-by, fix merge resolution
---

 io_uring/net.c | 38 ++++++++++++++++++++++++++++++++++----
 1 file changed, 34 insertions(+), 4 deletions(-)

diff --git a/io_uring/net.c b/io_uring/net.c
index 639f111408a1..ba2d0abea349 100644
--- a/io_uring/net.c
+++ b/io_uring/net.c
@@ -75,7 +75,10 @@ struct io_sr_msg {
 	u16				flags;
 	/* initialised and used only by !msg send variants */
 	u16				buf_group;
+	/* per-invocation mshot limit */
 	unsigned			mshot_len;
+	/* overall mshot byte limit */
+	unsigned			mshot_total_len;
 	void __user			*msg_control;
 	/* used only for send zerocopy */
 	struct io_kiocb 		*notif;
@@ -89,10 +92,12 @@ enum sr_retry_flags {
 	IORING_RECV_RETRY	= (1U << 15),
 	IORING_RECV_PARTIAL_MAP	= (1U << 14),
 	IORING_RECV_MSHOT_CAP	= (1U << 13),
+	IORING_RECV_MSHOT_LIM	= (1U << 12),
+	IORING_RECV_MSHOT_DONE	= (1U << 11),
 
 	IORING_RECV_RETRY_CLEAR	= IORING_RECV_RETRY | IORING_RECV_PARTIAL_MAP,
 	IORING_RECV_NO_RETRY	= IORING_RECV_RETRY | IORING_RECV_PARTIAL_MAP |
-				  IORING_RECV_MSHOT_CAP,
+				  IORING_RECV_MSHOT_CAP | IORING_RECV_MSHOT_DONE,
 };
 
 /*
@@ -765,7 +770,7 @@ int io_recvmsg_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
 
 	sr->done_io = 0;
 
-	if (unlikely(sqe->file_index || sqe->addr2))
+	if (unlikely(sqe->addr2))
 		return -EINVAL;
 
 	sr->umsg = u64_to_user_ptr(READ_ONCE(sqe->addr));
@@ -790,16 +795,25 @@ int io_recvmsg_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
 		sr->buf_group = req->buf_index;
 		req->buf_list = NULL;
 	}
-	sr->mshot_len = 0;
+	sr->mshot_total_len = sr->mshot_len = 0;
 	if (sr->flags & IORING_RECV_MULTISHOT) {
 		if (!(req->flags & REQ_F_BUFFER_SELECT))
 			return -EINVAL;
 		if (sr->msg_flags & MSG_WAITALL)
 			return -EINVAL;
-		if (req->opcode == IORING_OP_RECV)
+		if (req->opcode == IORING_OP_RECV) {
 			sr->mshot_len = sr->len;
+			sr->mshot_total_len = READ_ONCE(sqe->optlen);
+			if (sr->mshot_total_len)
+				sr->flags |= IORING_RECV_MSHOT_LIM;
+		} else if (sqe->optlen) {
+			return -EINVAL;
+		}
 		req->flags |= REQ_F_APOLL_MULTISHOT;
+	} else if (sqe->optlen) {
+		return -EINVAL;
 	}
+
 	if (sr->flags & IORING_RECVSEND_BUNDLE) {
 		if (req->opcode == IORING_OP_RECVMSG)
 			return -EINVAL;
@@ -831,6 +845,19 @@ static inline bool io_recv_finish(struct io_kiocb *req, int *ret,
 	if (kmsg->msg.msg_inq > 0)
 		cflags |= IORING_CQE_F_SOCK_NONEMPTY;
 
+	if (*ret > 0 && sr->flags & IORING_RECV_MSHOT_LIM) {
+		/*
+		 * If sr->len hits zero, the limit has been reached. Mark
+		 * mshot as finished, and flag MSHOT_DONE as well to prevent
+		 * a potential bundle from being retried.
+		 */
+		sr->mshot_total_len -= min_t(int, *ret, sr->mshot_total_len);
+		if (!sr->mshot_total_len) {
+			sr->flags |= IORING_RECV_MSHOT_DONE;
+			mshot_finished = true;
+		}
+	}
+
 	if (sr->flags & IORING_RECVSEND_BUNDLE) {
 		size_t this_ret = *ret - sr->done_io;
 
@@ -1094,6 +1121,9 @@ static int io_recv_buf_select(struct io_kiocb *req, struct io_async_msghdr *kmsg
 		else if (kmsg->msg.msg_inq > 1)
 			arg.max_len = min_not_zero(*len, (size_t) kmsg->msg.msg_inq);
 
+		/* if mshot limited, ensure we don't go over */
+		if (sr->flags & IORING_RECV_MSHOT_LIM)
+			arg.max_len = min_not_zero(arg.max_len, sr->mshot_total_len);
 		ret = io_buffers_peek(req, &arg);
 		if (unlikely(ret < 0))
 			return ret;
-- 
2.39.5 (Apple Git-154)


^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [PATCH v2] io_uring/net: Support multishot receive len cap
  2025-07-15 14:02 [PATCH v2] io_uring/net: Support multishot receive len cap norman.maurer
@ 2025-07-15 17:32 ` Jens Axboe
  2025-07-16 14:28 ` Jens Axboe
  1 sibling, 0 replies; 3+ messages in thread
From: Jens Axboe @ 2025-07-15 17:32 UTC (permalink / raw)
  To: norman.maurer, io-uring; +Cc: Norman Maurer

On 7/15/25 8:02 AM, norman.maurer@googlemail.com wrote:
> From: Norman Maurer <norman_maurer@apple.com>
> 
> At the moment its very hard to do fine grained backpressure when using
> multishot as the kernel might produce a lot of completions before the
> user has a chance to cancel a previous submitted multishot recv.
> 
> This change adds support to issue a multishot recv that is capped by a
> len, which means the kernel will only rearm until X amount of data is
> received. When the limit is reached the completion will signal to the
> user that a re-arm needs to happen manually by not setting the IORING_CQE_F_MORE
> flag.

I like this. Worth mentioning that there's already a test case for this
in liburing:

https://git.kernel.dk/cgit/liburing/tree/test/recv-mshot-fair.c

-- 
Jens Axboe

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH v2] io_uring/net: Support multishot receive len cap
  2025-07-15 14:02 [PATCH v2] io_uring/net: Support multishot receive len cap norman.maurer
  2025-07-15 17:32 ` Jens Axboe
@ 2025-07-16 14:28 ` Jens Axboe
  1 sibling, 0 replies; 3+ messages in thread
From: Jens Axboe @ 2025-07-16 14:28 UTC (permalink / raw)
  To: io-uring, norman.maurer; +Cc: Norman Maurer


On Tue, 15 Jul 2025 16:02:50 +0200, norman.maurer@googlemail.com wrote:
> At the moment its very hard to do fine grained backpressure when using
> multishot as the kernel might produce a lot of completions before the
> user has a chance to cancel a previous submitted multishot recv.
> 
> This change adds support to issue a multishot recv that is capped by a
> len, which means the kernel will only rearm until X amount of data is
> received. When the limit is reached the completion will signal to the
> user that a re-arm needs to happen manually by not setting the IORING_CQE_F_MORE
> flag.
> 
> [...]

Applied, thanks!

[1/1] io_uring/net: Support multishot receive len cap
      commit: 0ebc9a7ecf6acecf8bdf3a3cb02b6073df4a2288

Best regards,
-- 
Jens Axboe




^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2025-07-16 14:28 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-07-15 14:02 [PATCH v2] io_uring/net: Support multishot receive len cap norman.maurer
2025-07-15 17:32 ` Jens Axboe
2025-07-16 14:28 ` Jens Axboe

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox