]> git.ipfire.org Git - thirdparty/kernel/linux.git/commitdiff
blk-mq: add blk_rq_nr_bvec() helper
authorChaitanya Kulkarni <ckulkarnilinux@gmail.com>
Wed, 3 Dec 2025 03:58:09 +0000 (19:58 -0800)
committerJens Axboe <axboe@kernel.dk>
Thu, 4 Dec 2025 14:19:26 +0000 (07:19 -0700)
Add a new helper function blk_rq_nr_bvec() that returns the number of
bvecs in a request. This count represents the number of iterations
rq_for_each_bvec() would perform on a request.

Drivers need to pre-allocate bvec arrays before iterating through
a request's bvecs. Currently, they manually count bvecs using
rq_for_each_bvec() in a loop, which is repetitive. The new helper
centralizes this logic.

This pattern exists in loop and zloop drivers, where multi-bio requests
require copying bvecs into a contiguous array before creating
an iov_iter for file operations.

Update loop and zloop drivers to use the new helper, eliminating
duplicate code.

This patch also provides a clear API to avoid any potential misuse of
blk_nr_phys_segments() for calculating the bvecs since, one bvec can
have more than one segments and use of blk_nr_phys_segments() can
lead to extra memory allocation :-

[ 6155.673749] nullb_bio: 128K bio as ONE bvec: sector=0, size=131072
[ 6155.673846] null_blk: #### null_handle_data_transfer:1375
[ 6155.673850] null_blk: nr_bvec=1 blk_rq_nr_phys_segments=2
[ 6155.674263] null_blk: #### null_handle_data_transfer:1375
[ 6155.674267] null_blk: nr_bvec=1 blk_rq_nr_phys_segments=1

Reviewed-by: Niklas Cassel <cassel@kernel.org>
Signed-off-by: Chaitanya Kulkarni <ckulkarnilinux@gmail.com>
Reviewed-by: Christoph Hellwig <hch@lst.de>
Signed-off-by: Jens Axboe <axboe@kernel.dk>
drivers/block/loop.c
drivers/block/zloop.c
include/linux/blk-mq.h

index ebe751f3974267c11d94858a3bccfe82665d67a8..272bc608e528244761f3406799f5fc16e04f1844 100644 (file)
@@ -348,11 +348,10 @@ static int lo_rw_aio(struct loop_device *lo, struct loop_cmd *cmd,
        struct file *file = lo->lo_backing_file;
        struct bio_vec tmp;
        unsigned int offset;
-       int nr_bvec = 0;
+       unsigned int nr_bvec;
        int ret;
 
-       rq_for_each_bvec(tmp, rq, rq_iter)
-               nr_bvec++;
+       nr_bvec = blk_rq_nr_bvec(rq);
 
        if (rq->bio != rq->biotail) {
 
index 3f50321aa4a7501a27c290dd0b1866878e7e0254..77bd6081b244524db854c693c11a011ea66f8ef3 100644 (file)
@@ -394,7 +394,7 @@ static void zloop_rw(struct zloop_cmd *cmd)
        struct bio_vec tmp;
        unsigned long flags;
        sector_t zone_end;
-       int nr_bvec = 0;
+       unsigned int nr_bvec;
        int ret;
 
        atomic_set(&cmd->ref, 2);
@@ -487,8 +487,7 @@ static void zloop_rw(struct zloop_cmd *cmd)
                spin_unlock_irqrestore(&zone->wp_lock, flags);
        }
 
-       rq_for_each_bvec(tmp, rq, rq_iter)
-               nr_bvec++;
+       nr_bvec = blk_rq_nr_bvec(rq);
 
        if (rq->bio != rq->biotail) {
                struct bio_vec *bvec;
index eb7254b3ddddbb14f17b5dfca84e40bca7501293..cae9e857aea4285ae6f6a01dedd460a691d1e081 100644 (file)
@@ -1213,6 +1213,24 @@ static inline unsigned short blk_rq_nr_discard_segments(struct request *rq)
        return max_t(unsigned short, rq->nr_phys_segments, 1);
 }
 
+/**
+ * blk_rq_nr_bvec - return number of bvecs in a request
+ * @rq: request to calculate bvecs for
+ *
+ * Returns the number of bvecs.
+ */
+static inline unsigned int blk_rq_nr_bvec(struct request *rq)
+{
+       struct req_iterator rq_iter;
+       struct bio_vec bv;
+       unsigned int nr_bvec = 0;
+
+       rq_for_each_bvec(bv, rq, rq_iter)
+               nr_bvec++;
+
+       return nr_bvec;
+}
+
 int __blk_rq_map_sg(struct request *rq, struct scatterlist *sglist,
                struct scatterlist **last_sg);
 static inline int blk_rq_map_sg(struct request *rq, struct scatterlist *sglist)