Re: [PATCH] io_uring/rsrc: send exact nr_segs for fixed buffer

From: Pavel Begunkov
Date: Thu Apr 17 2025 - 05:11:11 EST


On 4/16/25 23:23, Jens Axboe wrote:
...
Theoretically it is, but it always makes me a bit nervous as there are
some _really_ odd iov_iter use cases out there. And passing down known
wrong segment counts is pretty wonky.

Btw, where exactly does it stumble in there? I'd assume we don't

Because segments != 1, and then that hits the slower path.

Right, but walking over entire bvec is not fast either. Sounds
like you're saying it's even more expensive and that's slightly
concerning.

need to do the segment correction for kbuf as the bio splitting
can do it (and probably does) in exactly the same way?

It doesn't strictly need to, but we should handle that case too. That'd
basically just be the loop addition I already did, something ala the
below on top for both of them:

diff --git a/io_uring/rsrc.c b/io_uring/rsrc.c
index d8fa7158e598..767ac89c8426 100644
--- a/io_uring/rsrc.c
+++ b/io_uring/rsrc.c
@@ -1032,6 +1032,25 @@ static int validate_fixed_range(u64 buf_addr, size_t len,
return 0;
}
+static int io_import_kbuf(int ddir, struct iov_iter *iter,
+ struct io_mapped_ubuf *imu, size_t len, size_t offset)
+{
+ iov_iter_bvec(iter, ddir, iter->bvec, imu->nr_bvecs, len + offset);
+ iov_iter_advance(iter, offset);
+
+ if (len + offset < imu->len) {

It should always be less or equal, are you trying to handle
the latter?

+ const struct bio_vec *bvec = iter->bvec;
+
+ while (len > bvec->bv_len) {
+ len -= bvec->bv_len;
+ bvec++;
+ }
+ iter->nr_segs = bvec - iter->bvec;
+ }
+
+ return 0;
+}
+

--
Pavel Begunkov