public inbox for io-uring@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH v6.20] io_uring/rsrc: refactor io_import_kbuf() to use single loop
@ 2025-12-17 12:31 Ming Lei
  2025-12-17 15:16 ` huang-jl
  0 siblings, 1 reply; 9+ messages in thread
From: Ming Lei @ 2025-12-17 12:31 UTC (permalink / raw)
  To: Jens Axboe, io-uring; +Cc: Ming Lei, huang-jl, Caleb Sander Mateos

Replace the iov_iter_advance() call and the separate loop for
calculating nr_segs with a single unified loop that iterates over
bvecs to determine the starting position, iov_offset, and nr_segs.

This simplifies the logic and avoids the overhead of iov_iter_advance().

Cc: huang-jl <huang-jl@deepseek.com>
Cc: Caleb Sander Mateos <csander@purestorage.com>
Signed-off-by: Ming Lei <ming.lei@redhat.com>
---
 io_uring/rsrc.c | 38 ++++++++++++++++++++++++++++----------
 1 file changed, 28 insertions(+), 10 deletions(-)

diff --git a/io_uring/rsrc.c b/io_uring/rsrc.c
index a63474b331bf..f7ee146d5330 100644
--- a/io_uring/rsrc.c
+++ b/io_uring/rsrc.c
@@ -1051,20 +1051,38 @@ static int validate_fixed_range(u64 buf_addr, size_t len,
 static int io_import_kbuf(int ddir, struct iov_iter *iter,
 			  struct io_mapped_ubuf *imu, size_t len, size_t offset)
 {
-	size_t count = len + offset;
-
-	iov_iter_bvec(iter, ddir, imu->bvec, imu->nr_bvecs, count);
-	iov_iter_advance(iter, offset);
+	const struct bio_vec *bvec = imu->bvec;
+	size_t iov_offset = 0;
+	unsigned int nr_segs = 0;
+	size_t remaining = len;
+	unsigned int i;
 
-	if (count < imu->len) {
-		const struct bio_vec *bvec = iter->bvec;
+	/* Single loop to calculate starting bvec, iov_offset, and nr_segs */
+	for (i = 0; i < imu->nr_bvecs; i++, bvec++) {
+		size_t bvec_avail;
 
-		while (len > bvec->bv_len) {
-			len -= bvec->bv_len;
-			bvec++;
+		/* Skip offset bytes to find starting position */
+		if (offset > 0) {
+			if (offset >= bvec->bv_len) {
+				offset -= bvec->bv_len;
+				continue;
+			}
+			iov_offset = offset;
+			bvec_avail = bvec->bv_len - offset;
+			offset = 0;
+		} else {
+			bvec_avail = bvec->bv_len;
 		}
-		iter->nr_segs = 1 + bvec - iter->bvec;
+
+		/* Count bvecs for len bytes */
+		nr_segs++;
+		if (remaining <= bvec_avail)
+			break;
+		remaining -= bvec_avail;
 	}
+
+	iov_iter_bvec(iter, ddir, bvec - nr_segs + 1, nr_segs, len);
+	iter->iov_offset = iov_offset;
 	return 0;
 }
 
-- 
2.47.1


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

end of thread, other threads:[~2025-12-23 22:45 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-12-17 12:31 [PATCH v6.20] io_uring/rsrc: refactor io_import_kbuf() to use single loop Ming Lei
2025-12-17 15:16 ` huang-jl
2025-12-18  1:42   ` Ming Lei
2025-12-18  2:27     ` Ming Lei
2025-12-18  3:19       ` huang-jl
2025-12-22 19:56       ` Caleb Sander Mateos
2025-12-23  2:30         ` Ming Lei
2025-12-23 19:56           ` Caleb Sander Mateos
2025-12-23 22:45             ` Ming Lei

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