All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH 0/2] unify registered buffer iovec imports
@ 2025-03-08 18:21 Pavel Begunkov
  2025-03-08 18:21 ` [PATCH 1/2] io_uring: introduce io_prep_reg_iovec() Pavel Begunkov
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: Pavel Begunkov @ 2025-03-08 18:21 UTC (permalink / raw)
  To: io-uring; +Cc: asml.silence

Extract a helper for some common chunks of registered buffer iovec
handling so that callers don't have to care about offsets.

Pavel Begunkov (2):
  io_uring: introduce io_prep_reg_iovec()
  io_uring: rely on io_prep_reg_vec for iovec placement

 io_uring/net.c  | 27 ++++-----------------------
 io_uring/rsrc.c | 34 ++++++++++++++++++++++++++++++----
 io_uring/rsrc.h |  5 +++--
 io_uring/rw.c   | 24 ++----------------------
 4 files changed, 39 insertions(+), 51 deletions(-)

-- 
2.48.1


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

* [PATCH 1/2] io_uring: introduce io_prep_reg_iovec()
  2025-03-08 18:21 [PATCH 0/2] unify registered buffer iovec imports Pavel Begunkov
@ 2025-03-08 18:21 ` Pavel Begunkov
  2025-03-08 18:21 ` [PATCH 2/2] io_uring: rely on io_prep_reg_vec for iovec placement Pavel Begunkov
  2025-03-10 13:14 ` [PATCH 0/2] unify registered buffer iovec imports Jens Axboe
  2 siblings, 0 replies; 4+ messages in thread
From: Pavel Begunkov @ 2025-03-08 18:21 UTC (permalink / raw)
  To: io-uring; +Cc: asml.silence

iovecs that are turned into registered buffers are imported in a special
way with an offset, so that later we can do an in place translation. Add
a helper function taking care of it.

Signed-off-by: Pavel Begunkov <asml.silence@gmail.com>
---
 io_uring/net.c  | 23 +++--------------------
 io_uring/rsrc.c | 26 ++++++++++++++++++++++++++
 io_uring/rsrc.h |  2 ++
 io_uring/rw.c   | 21 +--------------------
 4 files changed, 32 insertions(+), 40 deletions(-)

diff --git a/io_uring/net.c b/io_uring/net.c
index 9fa5c9570875..6b8dbadf445f 100644
--- a/io_uring/net.c
+++ b/io_uring/net.c
@@ -403,9 +403,7 @@ static int io_sendmsg_zc_setup(struct io_kiocb *req, const struct io_uring_sqe *
 	struct io_sr_msg *sr = io_kiocb_to_cmd(req, struct io_sr_msg);
 	struct io_async_msghdr *kmsg = req->async_data;
 	struct user_msghdr msg;
-	int ret, iovec_off;
-	struct iovec *iov;
-	void *res;
+	int ret;
 
 	if (!(sr->flags & IORING_RECVSEND_FIXED_BUF))
 		return io_sendmsg_setup(req, sqe);
@@ -416,24 +414,9 @@ static int io_sendmsg_zc_setup(struct io_kiocb *req, const struct io_uring_sqe *
 	if (unlikely(ret))
 		return ret;
 	sr->msg_control = kmsg->msg.msg_control_user;
-
-	if (msg.msg_iovlen > kmsg->vec.nr || WARN_ON_ONCE(!kmsg->vec.iovec)) {
-		ret = io_vec_realloc(&kmsg->vec, msg.msg_iovlen);
-		if (ret)
-			return ret;
-		req->flags |= REQ_F_NEED_CLEANUP;
-	}
-	iovec_off = kmsg->vec.nr - msg.msg_iovlen;
-	iov = kmsg->vec.iovec + iovec_off;
-
-	res = iovec_from_user(msg.msg_iov, msg.msg_iovlen, kmsg->vec.nr, iov,
-			      io_is_compat(req->ctx));
-	if (IS_ERR(res))
-		return PTR_ERR(res);
-
 	kmsg->msg.msg_iter.nr_segs = msg.msg_iovlen;
-	req->flags |= REQ_F_IMPORT_BUFFER;
-	return ret;
+
+	return io_prep_reg_iovec(req, &kmsg->vec, msg.msg_iov, msg.msg_iovlen);
 }
 
 #define SENDMSG_FLAGS (IORING_RECVSEND_POLL_FIRST | IORING_RECVSEND_BUNDLE)
diff --git a/io_uring/rsrc.c b/io_uring/rsrc.c
index 71fe47facd4c..0e413e910f3d 100644
--- a/io_uring/rsrc.c
+++ b/io_uring/rsrc.c
@@ -1397,3 +1397,29 @@ int io_import_reg_vec(int ddir, struct iov_iter *iter,
 
 	return io_vec_fill_bvec(ddir, iter, imu, iov, nr_iovs, vec);
 }
+
+int io_prep_reg_iovec(struct io_kiocb *req, struct iou_vec *iv,
+		      const struct iovec __user *uvec, size_t uvec_segs)
+{
+	struct iovec *iov;
+	int iovec_off, ret;
+	void *res;
+
+	if (uvec_segs > iv->nr) {
+		ret = io_vec_realloc(iv, uvec_segs);
+		if (ret)
+			return ret;
+		req->flags |= REQ_F_NEED_CLEANUP;
+	}
+
+	/* pad iovec to the right */
+	iovec_off = iv->nr - uvec_segs;
+	iov = iv->iovec + iovec_off;
+	res = iovec_from_user(uvec, uvec_segs, uvec_segs, iov,
+			      io_is_compat(req->ctx));
+	if (IS_ERR(res))
+		return PTR_ERR(res);
+
+	req->flags |= REQ_F_IMPORT_BUFFER;
+	return 0;
+}
diff --git a/io_uring/rsrc.h b/io_uring/rsrc.h
index b0097c06b577..43f784915573 100644
--- a/io_uring/rsrc.h
+++ b/io_uring/rsrc.h
@@ -67,6 +67,8 @@ int io_import_reg_vec(int ddir, struct iov_iter *iter,
 			struct io_kiocb *req, struct iou_vec *vec,
 			unsigned nr_iovs, unsigned iovec_off,
 			unsigned issue_flags);
+int io_prep_reg_iovec(struct io_kiocb *req, struct iou_vec *iv,
+			const struct iovec __user *uvec, size_t uvec_segs);
 
 int io_register_clone_buffers(struct io_ring_ctx *ctx, void __user *arg);
 int io_sqe_buffers_unregister(struct io_ring_ctx *ctx);
diff --git a/io_uring/rw.c b/io_uring/rw.c
index 50037313555f..4861b876f48e 100644
--- a/io_uring/rw.c
+++ b/io_uring/rw.c
@@ -407,28 +407,9 @@ static int io_rw_prep_reg_vec(struct io_kiocb *req)
 	struct io_rw *rw = io_kiocb_to_cmd(req, struct io_rw);
 	struct io_async_rw *io = req->async_data;
 	const struct iovec __user *uvec;
-	size_t uvec_segs = rw->len;
-	struct iovec *iov;
-	int iovec_off, ret;
-	void *res;
 
-	if (uvec_segs > io->vec.nr) {
-		ret = io_vec_realloc(&io->vec, uvec_segs);
-		if (ret)
-			return ret;
-		req->flags |= REQ_F_NEED_CLEANUP;
-	}
-	/* pad iovec to the right */
-	iovec_off = io->vec.nr - uvec_segs;
-	iov = io->vec.iovec + iovec_off;
 	uvec = u64_to_user_ptr(rw->addr);
-	res = iovec_from_user(uvec, uvec_segs, uvec_segs, iov,
-			      io_is_compat(req->ctx));
-	if (IS_ERR(res))
-		return PTR_ERR(res);
-
-	req->flags |= REQ_F_IMPORT_BUFFER;
-	return 0;
+	return io_prep_reg_iovec(req, &io->vec, uvec, rw->len);
 }
 
 int io_prep_readv_fixed(struct io_kiocb *req, const struct io_uring_sqe *sqe)
-- 
2.48.1


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

* [PATCH 2/2] io_uring: rely on io_prep_reg_vec for iovec placement
  2025-03-08 18:21 [PATCH 0/2] unify registered buffer iovec imports Pavel Begunkov
  2025-03-08 18:21 ` [PATCH 1/2] io_uring: introduce io_prep_reg_iovec() Pavel Begunkov
@ 2025-03-08 18:21 ` Pavel Begunkov
  2025-03-10 13:14 ` [PATCH 0/2] unify registered buffer iovec imports Jens Axboe
  2 siblings, 0 replies; 4+ messages in thread
From: Pavel Begunkov @ 2025-03-08 18:21 UTC (permalink / raw)
  To: io-uring; +Cc: asml.silence

All vectored reg buffer users should use io_import_reg_vec() for iovec
imports, since iovec placement is the function's responsibility and
callers shouldn't know much about it, drop the offset parameter from
io_prep_reg_vec() and calculate it inside.

Signed-off-by: Pavel Begunkov <asml.silence@gmail.com>
---
 io_uring/net.c  | 4 +---
 io_uring/rsrc.c | 8 ++++----
 io_uring/rsrc.h | 3 +--
 io_uring/rw.c   | 3 +--
 4 files changed, 7 insertions(+), 11 deletions(-)

diff --git a/io_uring/net.c b/io_uring/net.c
index 6b8dbadf445f..1e36a72e4008 100644
--- a/io_uring/net.c
+++ b/io_uring/net.c
@@ -1513,12 +1513,10 @@ int io_sendmsg_zc(struct io_kiocb *req, unsigned int issue_flags)
 
 	if (req->flags & REQ_F_IMPORT_BUFFER) {
 		unsigned uvec_segs = kmsg->msg.msg_iter.nr_segs;
-		unsigned iovec_off = kmsg->vec.nr - uvec_segs;
 		int ret;
 
 		ret = io_import_reg_vec(ITER_SOURCE, &kmsg->msg.msg_iter, req,
-					&kmsg->vec, uvec_segs, iovec_off,
-					issue_flags);
+					&kmsg->vec, uvec_segs, issue_flags);
 		if (unlikely(ret))
 			return ret;
 		kmsg->msg.sg_from_iter = io_sg_from_iter;
diff --git a/io_uring/rsrc.c b/io_uring/rsrc.c
index 0e413e910f3d..607b09bd8374 100644
--- a/io_uring/rsrc.c
+++ b/io_uring/rsrc.c
@@ -1349,11 +1349,11 @@ static int io_estimate_bvec_size(struct iovec *iov, unsigned nr_iovs,
 
 int io_import_reg_vec(int ddir, struct iov_iter *iter,
 			struct io_kiocb *req, struct iou_vec *vec,
-			unsigned nr_iovs, unsigned iovec_off,
-			unsigned issue_flags)
+			unsigned nr_iovs, unsigned issue_flags)
 {
 	struct io_rsrc_node *node;
 	struct io_mapped_ubuf *imu;
+	unsigned iovec_off;
 	struct iovec *iov;
 	unsigned nr_segs;
 
@@ -1366,6 +1366,7 @@ int io_import_reg_vec(int ddir, struct iov_iter *iter,
 	if (!(imu->dir & (1 << ddir)))
 		return -EFAULT;
 
+	iovec_off = vec->nr - nr_iovs;
 	iov = vec->iovec + iovec_off;
 	nr_segs = io_estimate_bvec_size(iov, nr_iovs, imu);
 
@@ -1377,8 +1378,7 @@ int io_import_reg_vec(int ddir, struct iov_iter *iter,
 		nr_segs += nr_iovs;
 	}
 
-	if (WARN_ON_ONCE(iovec_off + nr_iovs != vec->nr) ||
-	    nr_segs > vec->nr) {
+	if (nr_segs > vec->nr) {
 		struct iou_vec tmp_vec = {};
 		int ret;
 
diff --git a/io_uring/rsrc.h b/io_uring/rsrc.h
index 43f784915573..b52242852ff3 100644
--- a/io_uring/rsrc.h
+++ b/io_uring/rsrc.h
@@ -65,8 +65,7 @@ int io_import_reg_buf(struct io_kiocb *req, struct iov_iter *iter,
 			unsigned issue_flags);
 int io_import_reg_vec(int ddir, struct iov_iter *iter,
 			struct io_kiocb *req, struct iou_vec *vec,
-			unsigned nr_iovs, unsigned iovec_off,
-			unsigned issue_flags);
+			unsigned nr_iovs, unsigned issue_flags);
 int io_prep_reg_iovec(struct io_kiocb *req, struct iou_vec *iv,
 			const struct iovec __user *uvec, size_t uvec_segs);
 
diff --git a/io_uring/rw.c b/io_uring/rw.c
index 4861b876f48e..246b22225919 100644
--- a/io_uring/rw.c
+++ b/io_uring/rw.c
@@ -390,11 +390,10 @@ static int io_rw_import_reg_vec(struct io_kiocb *req,
 {
 	struct io_rw *rw = io_kiocb_to_cmd(req, struct io_rw);
 	unsigned uvec_segs = rw->len;
-	unsigned iovec_off = io->vec.nr - uvec_segs;
 	int ret;
 
 	ret = io_import_reg_vec(ddir, &io->iter, req, &io->vec,
-				uvec_segs, iovec_off, issue_flags);
+				uvec_segs, issue_flags);
 	if (unlikely(ret))
 		return ret;
 	iov_iter_save_state(&io->iter, &io->iter_state);
-- 
2.48.1


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

* Re: [PATCH 0/2] unify registered buffer iovec imports
  2025-03-08 18:21 [PATCH 0/2] unify registered buffer iovec imports Pavel Begunkov
  2025-03-08 18:21 ` [PATCH 1/2] io_uring: introduce io_prep_reg_iovec() Pavel Begunkov
  2025-03-08 18:21 ` [PATCH 2/2] io_uring: rely on io_prep_reg_vec for iovec placement Pavel Begunkov
@ 2025-03-10 13:14 ` Jens Axboe
  2 siblings, 0 replies; 4+ messages in thread
From: Jens Axboe @ 2025-03-10 13:14 UTC (permalink / raw)
  To: io-uring, Pavel Begunkov


On Sat, 08 Mar 2025 18:21:14 +0000, Pavel Begunkov wrote:
> Extract a helper for some common chunks of registered buffer iovec
> handling so that callers don't have to care about offsets.
> 
> Pavel Begunkov (2):
>   io_uring: introduce io_prep_reg_iovec()
>   io_uring: rely on io_prep_reg_vec for iovec placement
> 
> [...]

Applied, thanks!

[1/2] io_uring: introduce io_prep_reg_iovec()
      commit: d291fb65202051e996cd983b29dce3e390421bc6
[2/2] io_uring: rely on io_prep_reg_vec for iovec placement
      commit: 146acfd0f6494579996ae4168967cc5ada7d0e5a

Best regards,
-- 
Jens Axboe




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

end of thread, other threads:[~2025-03-10 13:14 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-03-08 18:21 [PATCH 0/2] unify registered buffer iovec imports Pavel Begunkov
2025-03-08 18:21 ` [PATCH 1/2] io_uring: introduce io_prep_reg_iovec() Pavel Begunkov
2025-03-08 18:21 ` [PATCH 2/2] io_uring: rely on io_prep_reg_vec for iovec placement Pavel Begunkov
2025-03-10 13:14 ` [PATCH 0/2] unify registered buffer iovec imports Jens Axboe

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.