public inbox for io-uring@vger.kernel.org
 help / color / mirror / Atom feed
From: Pavel Begunkov <asml.silence@gmail.com>
To: io-uring@vger.kernel.org
Cc: asml.silence@gmail.com
Subject: [PATCH 4/4] io_uring: reuse buffer updates for registration
Date: Fri,  4 Apr 2025 17:22:17 +0100	[thread overview]
Message-ID: <8996ffd533db8bd12c84cdc2ccef1fddbbb3da27.1743783348.git.asml.silence@gmail.com> (raw)
In-Reply-To: <cover.1743783348.git.asml.silence@gmail.com>

After registering an empty buffer table, we can reuse the code for
buffer updates to actually register buffers.

Signed-off-by: Pavel Begunkov <asml.silence@gmail.com>
---
 io_uring/rsrc.c | 85 +++++++++++++++++++------------------------------
 1 file changed, 32 insertions(+), 53 deletions(-)

diff --git a/io_uring/rsrc.c b/io_uring/rsrc.c
index 958eee7b4a47..6b5ec1504dcd 100644
--- a/io_uring/rsrc.c
+++ b/io_uring/rsrc.c
@@ -277,9 +277,11 @@ static int __io_sqe_files_update(struct io_ring_ctx *ctx,
 	return done ? done : err;
 }
 
-static int __io_sqe_buffers_update(struct io_ring_ctx *ctx,
-				   struct io_uring_rsrc_update2 *up,
-				   unsigned int nr_args)
+static int io_buffer_table_update(struct io_ring_ctx *ctx,
+				  struct io_rsrc_data *buf_table,
+				  struct io_uring_rsrc_update2 *up,
+				  unsigned int nr_args,
+				  unsigned *last_error)
 {
 	u64 __user *tags = u64_to_user_ptr(up->tags);
 	struct iovec fast_iov, *iov;
@@ -289,9 +291,9 @@ static int __io_sqe_buffers_update(struct io_ring_ctx *ctx,
 	__u32 done;
 	int i, err;
 
-	if (!ctx->buf_table.nr)
+	if (!buf_table->nr)
 		return -ENXIO;
-	if (up->offset + nr_args > ctx->buf_table.nr)
+	if (up->offset + nr_args > buf_table->nr)
 		return -EINVAL;
 
 	for (done = 0; done < nr_args; done++) {
@@ -316,17 +318,26 @@ static int __io_sqe_buffers_update(struct io_ring_ctx *ctx,
 			err = PTR_ERR(node);
 			break;
 		}
-		i = array_index_nospec(up->offset + done, ctx->buf_table.nr);
-		io_reset_rsrc_node(ctx, &ctx->buf_table, i);
-		ctx->buf_table.nodes[i] = node;
+		i = array_index_nospec(up->offset + done, buf_table->nr);
+		io_reset_rsrc_node(ctx, buf_table, i);
+		buf_table->nodes[i] = node;
 		if (ctx->compat)
 			user_data += sizeof(struct compat_iovec);
 		else
 			user_data += sizeof(struct iovec);
 	}
+	if (last_error)
+		*last_error = err;
 	return done ? done : err;
 }
 
+static int __io_sqe_buffers_update(struct io_ring_ctx *ctx,
+				   struct io_uring_rsrc_update2 *up,
+				   unsigned int nr_args)
+{
+	return io_buffer_table_update(ctx, &ctx->buf_table, up, nr_args, NULL);
+}
+
 static int __io_register_rsrc_update(struct io_ring_ctx *ctx, unsigned type,
 				     struct io_uring_rsrc_update2 *up,
 				     unsigned nr_args)
@@ -851,11 +862,8 @@ static struct io_rsrc_node *io_sqe_buffer_register(struct io_ring_ctx *ctx,
 int io_sqe_buffers_register(struct io_ring_ctx *ctx, void __user *arg,
 			    unsigned int nr_args, u64 __user *tags)
 {
-	struct page *last_hpage = NULL;
 	struct io_rsrc_data data;
-	struct iovec fast_iov, *iov = &fast_iov;
-	const struct iovec __user *uvec;
-	int i, ret;
+	int ret, err;
 
 	BUILD_BUG_ON(IORING_MAX_REG_BUFFERS >= (1u << 16));
 
@@ -867,51 +875,22 @@ int io_sqe_buffers_register(struct io_ring_ctx *ctx, void __user *arg,
 	if (ret)
 		return ret;
 
-	if (!arg) {
-		ctx->buf_table = data;
-		return 0;
-	}
-
-	for (i = 0; i < nr_args; i++) {
-		struct io_rsrc_node *node;
-		u64 tag = 0;
-
-		uvec = (struct iovec __user *) arg;
-		iov = iovec_from_user(uvec, 1, 1, &fast_iov, ctx->compat);
-		if (IS_ERR(iov)) {
-			ret = PTR_ERR(iov);
-			break;
-		}
-		ret = io_buffer_validate(iov);
-		if (ret)
-			break;
-		if (ctx->compat)
-			arg += sizeof(struct compat_iovec);
-		else
-			arg += sizeof(struct iovec);
-
-		if (tags) {
-			if (copy_from_user(&tag, &tags[i], sizeof(tag))) {
-				ret = -EFAULT;
-				break;
-			}
-		}
+	if (arg) {
+		struct io_uring_rsrc_update2 update_arg = {
+			.tags = (u64)(unsigned long)tags,
+			.data = (u64)(unsigned long)arg,
+			.offset = 0,
+		};
 
-		node = io_sqe_buffer_register(ctx, iov, &last_hpage, tag);
-		if (IS_ERR(node)) {
-			ret = PTR_ERR(node);
-			break;
+		ret = io_buffer_table_update(ctx, &data, &update_arg, nr_args, &err);
+		if (ret != nr_args) {
+			io_clear_table_tags(&data);
+			io_rsrc_data_free(ctx, &data);
+			return err;
 		}
-		data.nodes[i] = node;
-	}
-
-	if (ret) {
-		io_clear_table_tags(&data);
-		io_rsrc_data_free(ctx, &data);
-		return ret;
 	}
 	ctx->buf_table = data;
-	return ret;
+	return 0;
 }
 
 int io_buffer_register_bvec(struct io_uring_cmd *cmd, struct request *rq,
-- 
2.48.1


  parent reply	other threads:[~2025-04-04 16:21 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-04-04 16:22 [PATCH 0/4] buffer table registration cleanup Pavel Begunkov
2025-04-04 16:22 ` [PATCH 1/4] io_uring/rsrc: avoid assigning buf table on failure Pavel Begunkov
2025-04-04 16:22 ` [PATCH 2/4] io_uring: deduplicate node tagging Pavel Begunkov
2025-04-04 16:22 ` [PATCH 3/4] io_uring: early return for sparse buf table registration Pavel Begunkov
2025-04-04 16:22 ` Pavel Begunkov [this message]
2025-04-04 16:38   ` [PATCH 4/4] io_uring: reuse buffer updates for registration Jens Axboe
2025-04-04 21:38     ` Pavel Begunkov
2025-04-04 22:00       ` Jens Axboe

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=8996ffd533db8bd12c84cdc2ccef1fddbbb3da27.1743783348.git.asml.silence@gmail.com \
    --to=asml.silence@gmail.com \
    --cc=io-uring@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