Skip to contentSkip to conversation

io_uring/rsrc: refactor io_import_kbuf() to use single loop

PATCH In review Ming Lei 3 participants 10 messages Dec 17, 2025 – Dec 30, 2025 io_uring

lore
Share & tools
lore

Ming Lei

author
#0 lore raw

PATCH 1/1 io_uring/rsrc: refactor io_import_kbuf() to use single loop

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 <[email protected]>
Cc Caleb Sander Mateos <[email protected]>
---
 io_uring/rsrc.c | 38 ++++++++++++++++++++++++++++----------
 1 file changed, 28 insertions(+), 10 deletions(-)
io_uring/rsrc.c +28 -10
index a63474b331bf..f7ee146d5330 100644
@@ -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;
 }
 
Signature · 1 line
2.47.1
#1 lore raw

The code looks correct to me.

Quoted 1 line

This simplifies the logic

I'm not an expert in Linux development, but from my perspective, the original version seems simpler and more readable. The semantics of iov_iter_advance() are clear and well-understood.

That said, I understand the appeal of merging them into a single loop.

Quoted 1 line

and avoids the overhead of iov_iter_advance()

Could you clarify what overhead you mean? If it's the function call overhead, I think the compiler would inline it anyway. The actual iteration work seems equivalent between both approaches.

Thanks,
huang-jl

author
#2 lore raw
Quoted 8 lines · huang-jl

The code looks correct to me.

Quoted 1 line

This simplifies the logic

I'm not an expert in Linux development, but from my perspective, the original version seems simpler and more readable. The semantics of iov_iter_advance() are clear and well-understood.

Quoted 7 lines

That said, I understand the appeal of merging them into a single loop.

Quoted 1 line

and avoids the overhead of iov_iter_advance()

Could you clarify what overhead you mean? If it's the function call overhead, I think the compiler would inline it anyway. The actual iteration work seems equivalent between both approaches.

iov_iter_advance() is global function, and it can't be inline.

Also single loop is more readable, cause ->iov_offset can be ignored easily.

In theory, re-calculating nr_segs isn't necessary, it is just for avoiding potential split, however not get idea how it is triggered. Nitesh didn't mention the exact reason:

https://lkml.org/lkml/2025/4/16/351

I will look at the reason and see if it can be avoided.

Thanks,
Ming

author
#3 lore raw
Quoted 21 lines · Ming Lei
Quoted 8 lines · huang-jl

The code looks correct to me.

Quoted 1 line

This simplifies the logic

I'm not an expert in Linux development, but from my perspective, the original version seems simpler and more readable. The semantics of iov_iter_advance() are clear and well-understood.

Quoted 7 lines

That said, I understand the appeal of merging them into a single loop.

Quoted 1 line

and avoids the overhead of iov_iter_advance()

Could you clarify what overhead you mean? If it's the function call overhead, I think the compiler would inline it anyway. The actual iteration work seems equivalent between both approaches.

iov_iter_advance() is global function, and it can't be inline.

Also single loop is more readable, cause ->iov_offset can be ignored easily.

Quoted 7 lines

In theory, re-calculating nr_segs isn't necessary, it is just for avoiding potential split, however not get idea how it is triggered. Nitesh didn't mention the exact reason:

https://lkml.org/lkml/2025/4/16/351

I will look at the reason and see if it can be avoided.

The reason is in both bio_iov_bvec_set() and bio_may_need_split().

->bi_vcnt doesn't make sense for cloned bio, and shouldn't be used as multiple segment hint.

However, it also shows bio_split_rw() is too heavy.

Thanks,
Ming

#4 lore raw
Quoted 4 lines

->bi_vcnt doesn't make sense for cloned bio, and shouldn't be used as multiple segment hint.

However, it also shows bio_split_rw() is too heavy.

Sorry, I'm not familiar with this area of the codebase. Perhaps need someone else to provide more valuable suggestions.

--
Thanks,
huang-jl

#5 lore raw
In reply to Ming Lei
Quoted 27 lines · Ming Lei
Quoted 25 lines · Ming Lei
Quoted 8 lines · huang-jl

The code looks correct to me.

Quoted 1 line

This simplifies the logic

I'm not an expert in Linux development, but from my perspective, the original version seems simpler and more readable. The semantics of iov_iter_advance() are clear and well-understood.

Quoted 7 lines

That said, I understand the appeal of merging them into a single loop.

Quoted 1 line

and avoids the overhead of iov_iter_advance()

Could you clarify what overhead you mean? If it's the function call overhead, I think the compiler would inline it anyway. The actual iteration work seems equivalent between both approaches.

iov_iter_advance() is global function, and it can't be inline.

Also single loop is more readable, cause ->iov_offset can be ignored easily.

In theory, re-calculating nr_segs isn't necessary, it is just for avoiding potential split, however not get idea how it is triggered. Nitesh didn't mention the exact reason:

Quoted 5 lines
Quoted 3 lines

https://lkml.org/lkml/2025/4/16/351

I will look at the reason and see if it can be avoided.

The reason is in both bio_iov_bvec_set() and bio_may_need_split().

nr_segs is not just a performance optimization, it's part of the struct iov_iter API and used by iov_iter_extract_bvec_pages(), as huang-jl pointed out. I don't think it's a good idea to assume that nr_segs isn't going to be used and doesn't need to be calculated correctly.

I think this patch is a definite improvement as it reduces the number of assumptions about the internal structure of a bvec iov_iter. The remaining assignment to iter->iov_offset is unfortunate, but I don't see a great way around it.

My only suggestion would be to separate out a "find first bvec" loop and a "find last bvec" loop to make the loop body less branchy. Something like this:
const struct bio_vec *bvec = imu->bvec, bvec_start; size_t remaining;
unsigned bvec_avail;

while (offset >= bvec->bv_len) {
        offset -= bvec->bv_len;
        bvec++;
}

bvec_avail = bvec->bv_len - offset;
bvec_start = bvec;
remaining = len;

while (remaining > bvec_avail) {
        remaining -= bvec_avail;
        bvec++;
        bvec_avail = bvec->bv_len;
}

iov_iter_bvec(iter, ddir, start_bvec, bvec - start_bvec + 1, len); iter->iov_offset = offset;

Best,
Caleb

Quoted 10 lines

->bi_vcnt doesn't make sense for cloned bio, and shouldn't be used as multiple segment hint.

However, it also shows bio_split_rw() is too heavy.

Thanks,
Ming

author
#6 lore raw
Quoted 32 lines · Caleb Sander Mateos
Quoted 31 lines · Ming Lei
Quoted 28 lines · Ming Lei
Quoted 8 lines · huang-jl

The code looks correct to me.

Quoted 1 line

This simplifies the logic

I'm not an expert in Linux development, but from my perspective, the original version seems simpler and more readable. The semantics of iov_iter_advance() are clear and well-understood.

Quoted 7 lines

That said, I understand the appeal of merging them into a single loop.

Quoted 1 line

and avoids the overhead of iov_iter_advance()

Could you clarify what overhead you mean? If it's the function call overhead, I think the compiler would inline it anyway. The actual iteration work seems equivalent between both approaches.

iov_iter_advance() is global function, and it can't be inline.

Also single loop is more readable, cause ->iov_offset can be ignored easily.

In theory, re-calculating nr_segs isn't necessary, it is just for avoiding potential split, however not get idea how it is triggered. Nitesh didn't mention the exact reason:

https://lkml.org/lkml/2025/4/16/351

I will look at the reason and see if it can be avoided.

Quoted 7 lines
Quoted 1 line

The reason is in both bio_iov_bvec_set() and bio_may_need_split().

nr_segs is not just a performance optimization, it's part of the struct iov_iter API and used by iov_iter_extract_bvec_pages(), as huang-jl pointed out. I don't think it's a good idea to assume that nr_segs isn't going to be used and doesn't need to be calculated correctly.

It doesn't have to be exact if the bytes covered by count won't cross nr_segs.

The nr_segs re-calculation is added only for fixing performance regression in the following link:

https://lkml.org/lkml/2025/4/16/351

because bio_iov_bvec_set() takes iter->nr_segs for setting bio->bi_vcnt.

Quoted 6 lines

I think this patch is a definite improvement as it reduces the number of assumptions about the internal structure of a bvec iov_iter. The remaining assignment to iter->iov_offset is unfortunate, but I don't see a great way around it.

The re-calculation can be removed, please see the following patches:

https://lore.kernel.org/linux-block/[email protected]/

Nitesh has verified that it won't cause perf regression by replacing bio->bi_vcnt with __bvec_iter_bvec() in bio_may_need_split().

Thanks,
Ming

#7 lore raw
Quoted 45 lines · Ming Lei
Quoted 32 lines · Caleb Sander Mateos
Quoted 31 lines · Ming Lei
Quoted 28 lines · Ming Lei
Quoted 8 lines · huang-jl

The code looks correct to me.

Quoted 1 line

This simplifies the logic

I'm not an expert in Linux development, but from my perspective, the original version seems simpler and more readable. The semantics of iov_iter_advance() are clear and well-understood.

Quoted 7 lines

That said, I understand the appeal of merging them into a single loop.

Quoted 1 line

and avoids the overhead of iov_iter_advance()

Could you clarify what overhead you mean? If it's the function call overhead, I think the compiler would inline it anyway. The actual iteration work seems equivalent between both approaches.

iov_iter_advance() is global function, and it can't be inline.

Also single loop is more readable, cause ->iov_offset can be ignored easily.

In theory, re-calculating nr_segs isn't necessary, it is just for avoiding potential split, however not get idea how it is triggered. Nitesh didn't mention the exact reason:

https://lkml.org/lkml/2025/4/16/351

I will look at the reason and see if it can be avoided.

Quoted 7 lines
Quoted 1 line

The reason is in both bio_iov_bvec_set() and bio_may_need_split().

nr_segs is not just a performance optimization, it's part of the struct iov_iter API and used by iov_iter_extract_bvec_pages(), as huang-jl pointed out. I don't think it's a good idea to assume that nr_segs isn't going to be used and doesn't need to be calculated correctly.

It doesn't have to be exact if the bytes covered by count won't cross nr_segs.

Quoted 6 lines

The nr_segs re-calculation is added only for fixing performance regression in the following link:

https://lkml.org/lkml/2025/4/16/351

because bio_iov_bvec_set() takes iter->nr_segs for setting bio->bi_vcnt.

But iov_iter_extract_bvec_pages() appears to only use iter->nr_segs and not iter->count. I don't understand how it can get away with an overestimated iter->nr_segs.

Best,
Caleb

Quoted 19 lines
Quoted 7 lines

I think this patch is a definite improvement as it reduces the number of assumptions about the internal structure of a bvec iov_iter. The remaining assignment to iter->iov_offset is unfortunate, but I don't see a great way around it.

The re-calculation can be removed, please see the following patches:

https://lore.kernel.org/linux-block/[email protected]/

Nitesh has verified that it won't cause perf regression by replacing bio->bi_vcnt with __bvec_iter_bvec() in bio_may_need_split().

Thanks,
Ming

author
#8 lore raw
Quoted 49 lines · Caleb Sander Mateos
Quoted 48 lines · Ming Lei
Quoted 32 lines · Caleb Sander Mateos
Quoted 31 lines · Ming Lei
Quoted 28 lines · Ming Lei
Quoted 8 lines · huang-jl

The code looks correct to me.

Quoted 1 line

This simplifies the logic

I'm not an expert in Linux development, but from my perspective, the original version seems simpler and more readable. The semantics of iov_iter_advance() are clear and well-understood.

Quoted 7 lines

That said, I understand the appeal of merging them into a single loop.

Quoted 1 line

and avoids the overhead of iov_iter_advance()

Could you clarify what overhead you mean? If it's the function call overhead, I think the compiler would inline it anyway. The actual iteration work seems equivalent between both approaches.

iov_iter_advance() is global function, and it can't be inline.

Also single loop is more readable, cause ->iov_offset can be ignored easily.

In theory, re-calculating nr_segs isn't necessary, it is just for avoiding potential split, however not get idea how it is triggered. Nitesh didn't mention the exact reason:

https://lkml.org/lkml/2025/4/16/351

I will look at the reason and see if it can be avoided.

Quoted 7 lines
Quoted 1 line

The reason is in both bio_iov_bvec_set() and bio_may_need_split().

nr_segs is not just a performance optimization, it's part of the struct iov_iter API and used by iov_iter_extract_bvec_pages(), as huang-jl pointed out. I don't think it's a good idea to assume that nr_segs isn't going to be used and doesn't need to be calculated correctly.

It doesn't have to be exact if the bytes covered by count won't cross nr_segs.

The nr_segs re-calculation is added only for fixing performance regression in the following link:

Quoted 7 lines
Quoted 3 lines

https://lkml.org/lkml/2025/4/16/351

because bio_iov_bvec_set() takes iter->nr_segs for setting bio->bi_vcnt.

But iov_iter_extract_bvec_pages() appears to only use iter->nr_segs and not iter->count. I don't understand how it can get away with an overestimated iter->nr_segs.

iov_iter_extract_bvec_pages() does pass max_size for using iter->counter, please see the only caller of iov_iter_extract_pages().

iov_iter has to respect both two, otherwise it is a iov_iter bug.

Thanks,
Ming

#9 lore raw
Quoted 55 lines · Ming Lei
Quoted 53 lines · Caleb Sander Mateos
Quoted 51 lines · Ming Lei
Quoted 32 lines · Caleb Sander Mateos
Quoted 31 lines · Ming Lei
Quoted 28 lines · Ming Lei
Quoted 8 lines · huang-jl

The code looks correct to me.

Quoted 1 line

This simplifies the logic

I'm not an expert in Linux development, but from my perspective, the original version seems simpler and more readable. The semantics of iov_iter_advance() are clear and well-understood.

Quoted 7 lines

That said, I understand the appeal of merging them into a single loop.

Quoted 1 line

and avoids the overhead of iov_iter_advance()

Could you clarify what overhead you mean? If it's the function call overhead, I think the compiler would inline it anyway. The actual iteration work seems equivalent between both approaches.

iov_iter_advance() is global function, and it can't be inline.

Also single loop is more readable, cause ->iov_offset can be ignored easily.

In theory, re-calculating nr_segs isn't necessary, it is just for avoiding potential split, however not get idea how it is triggered. Nitesh didn't mention the exact reason:

https://lkml.org/lkml/2025/4/16/351

I will look at the reason and see if it can be avoided.

Quoted 7 lines
Quoted 1 line

The reason is in both bio_iov_bvec_set() and bio_may_need_split().

nr_segs is not just a performance optimization, it's part of the struct iov_iter API and used by iov_iter_extract_bvec_pages(), as huang-jl pointed out. I don't think it's a good idea to assume that nr_segs isn't going to be used and doesn't need to be calculated correctly.

It doesn't have to be exact if the bytes covered by count won't cross nr_segs.

The nr_segs re-calculation is added only for fixing performance regression in the following link:

https://lkml.org/lkml/2025/4/16/351

because bio_iov_bvec_set() takes iter->nr_segs for setting bio->bi_vcnt.

Quoted 8 lines
Quoted 3 lines

But iov_iter_extract_bvec_pages() appears to only use iter->nr_segs and not iter->count. I don't understand how it can get away with an overestimated iter->nr_segs.

iov_iter_extract_bvec_pages() does pass max_size for using iter->counter, please see the only caller of iov_iter_extract_pages().

iov_iter has to respect both two, otherwise it is a iov_iter bug.

I see what you mean, thanks for explaining.

Best,
Caleb