mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: "Clément Léger" <cleger@meta.com>
To: Jens Axboe <axboe@kernel.dk>
Cc: "Clément Léger" <cleger@meta.com>,
	io-uring@vger.kernel.org, linux-kernel@vger.kernel.org,
	"Juanlu Herrero" <jlhe0197@meta.com>
Subject: [PATCH v2 3/3] io_uring/net: Add bundle support for multishot recvmsg
Date: Mon, 23 Nov 2026 13:39:20 -0800	[thread overview]
Message-ID: <20261123213952.824008-4-cleger@meta.com> (raw)
In-Reply-To: <20261123213952.824008-1-cleger@meta.com>

Enable bundles for multishot recvmsg by reserving the envelope and
control data in the first buffer, and accounting for the payload spread
across the selected buffers. Incremental buffer rings are supported as
well, as the envelope and the payload are consumed contiguously from the
head buffer.

Signed-off-by: Clément Léger <cleger@meta.com>
---
 io_uring/net.c | 70 ++++++++++++++++++++++++++++++++++++++++++++------
 1 file changed, 62 insertions(+), 8 deletions(-)

diff --git a/io_uring/net.c b/io_uring/net.c
index 4c9d21db1409..4e385a2b9b64 100644
--- a/io_uring/net.c
+++ b/io_uring/net.c
@@ -746,6 +746,7 @@ static int io_recvmsg_mshot_prep(struct io_kiocb *req,
 static int io_recvmsg_copy_hdr(struct io_kiocb *req,
 			       struct io_async_msghdr *iomsg)
 {
+	struct io_sr_msg *sr = io_kiocb_to_cmd(req, struct io_sr_msg);
 	struct user_msghdr msg;
 	int ret;
 
@@ -753,6 +754,11 @@ static int io_recvmsg_copy_hdr(struct io_kiocb *req,
 	if (unlikely(ret))
 		return ret;
 
+	iomsg->msg.msg_inq = 0;
+
+	if (sr->flags & IORING_RECVSEND_BUNDLE)
+		sr->mshot_len = sr->len;
+
 	if (!(req->flags & REQ_F_BUFFER_SELECT)) {
 		ret = io_net_import_vec(req, iomsg, msg.msg_iov, msg.msg_iovlen,
 					ITER_DEST);
@@ -847,12 +853,6 @@ int io_recvmsg_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
 		return -EINVAL;
 	}
 
-	if (sr->flags & IORING_RECVSEND_BUNDLE) {
-		if (req->opcode == IORING_OP_RECVMSG &&
-		    sr->flags & IORING_RECV_MULTISHOT)
-			return -EINVAL;
-	}
-
 	if (io_is_compat(req->ctx))
 		sr->msg_flags |= MSG_CMSG_COMPAT;
 
@@ -870,6 +870,29 @@ static size_t io_recvmsg_hdr_len(struct io_async_msghdr *kmsg)
 	       kmsg->controllen;
 }
 
+static int io_recv_payload_len(struct io_kiocb *req,
+			       struct io_async_msghdr *kmsg, int consumed)
+{
+	if (req->opcode != IORING_OP_RECVMSG ||
+	    !(req->flags & REQ_F_APOLL_MULTISHOT) || consumed <= 0)
+		return consumed;
+
+	return consumed - (int)io_recvmsg_hdr_len(kmsg);
+}
+
+static bool io_recvmsg_bundle_bufs_left(struct io_async_msghdr *kmsg,
+					int nbufs)
+{
+	struct iov_iter *iter = &kmsg->msg.msg_iter;
+	const struct iovec *iov;
+
+	if (!iter_is_iovec(iter))
+		return false;
+
+	iov = kmsg->vec.iovec ?: &kmsg->fast_iov;
+	return iter_iov(iter) - iov + iter->nr_segs > nbufs;
+}
+
 /*
  * Finishes io_recv and io_recvmsg.
  *
@@ -905,12 +928,23 @@ static inline bool io_recv_finish(struct io_kiocb *req,
 
 	if (sr->flags & IORING_RECVSEND_BUNDLE) {
 		size_t this_ret = sel->val - sr->done_io;
+		int nbufs;
+		int payload = io_recv_payload_len(req, kmsg, consumed);
 
-		cflags |= io_put_kbufs(req, consumed, sel->buf_list, io_bundle_nbufs(kmsg, consumed));
+		if (req->opcode == IORING_OP_RECVMSG && consumed > 0 && !payload)
+			nbufs = 1;
+		else
+			nbufs = io_bundle_nbufs(kmsg, payload);
+
+		cflags |= io_put_kbufs(req, consumed, sel->buf_list, nbufs);
 		if (sr->flags & IORING_RECV_RETRY)
 			cflags = req->cqe.flags | (cflags & CQE_F_MASK);
 		if (sr->mshot_len && sel->val >= sr->mshot_len)
 			sr->flags |= IORING_RECV_MSHOT_CAP;
+		if (req->opcode == IORING_OP_RECVMSG &&
+		    (io_recvmsg_bundle_bufs_left(kmsg, nbufs) ||
+		     cflags & IORING_CQE_F_BUF_MORE))
+			req->flags &= ~REQ_F_BL_EMPTY;
 		/* bundle with no more immediate buffers, we're done */
 		if (req->flags & REQ_F_BL_EMPTY)
 			goto finish;
@@ -1087,15 +1121,35 @@ static int io_recvmsg_bundle_select(struct io_kiocb *req,
 {
 	struct io_sr_msg *sr = io_kiocb_to_cmd(req, struct io_sr_msg);
 	struct buf_sel_arg arg;
+	size_t iov0_len, max_len;
 	int nr_iovs;
+	int ret;
 
-	nr_iovs = io_recv_bundle_peek(req, kmsg, sel, &arg, sr->len);
+	if (sr->len)
+		max_len = sr->len;
+	else if (kmsg->msg.msg_inq > 1)
+		max_len = kmsg->msg.msg_inq + io_recvmsg_hdr_len(kmsg);
+	else
+		max_len = 0;
+
+	nr_iovs = io_recv_bundle_peek(req, kmsg, sel, &arg, max_len);
 	if (unlikely(nr_iovs <= 0))
 		return !nr_iovs ? -ENOBUFS : nr_iovs;
 
 	if (!sel->buf_list)
 		arg.out_len = arg.iovs[0].iov_len;
 
+	if (req->flags & REQ_F_APOLL_MULTISHOT) {
+		iov0_len = arg.iovs[0].iov_len;
+		ret = io_recvmsg_prep_multishot(kmsg, sr, &arg.iovs[0].iov_base,
+						&iov0_len);
+		if (unlikely(ret))
+			return ret;
+		arg.iovs[0].iov_len = iov0_len;
+		arg.out_len -= io_recvmsg_hdr_len(kmsg);
+		kmsg->payloadlen = arg.out_len;
+	}
+
 	if (nr_iovs == 1)
 		iov_iter_ubuf(&kmsg->msg.msg_iter, ITER_DEST,
 			      arg.iovs[0].iov_base, arg.out_len);
-- 
2.53.1-Meta


      parent reply	other threads:[~2026-11-23 21:40 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-23 21:39 [PATCH v2 0/3] io_uring/net: Add bundle support for recvmsg Clément Léger
2026-11-23 21:39 ` [PATCH v2 1/3] io_uring/net: Factor out receive buffer selection Clément Léger
2026-11-23 21:39 ` [PATCH v2 2/3] io_uring/net: Add bundle support for recvmsg Clément Léger
2026-11-23 21:39 ` Clément Léger [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261123213952.824008-4-cleger@meta.com \
    --to=cleger@meta.com \
    --cc=axboe@kernel.dk \
    --cc=io-uring@vger.kernel.org \
    --cc=jlhe0197@meta.com \
    --cc=linux-kernel@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®