Skip to contentSkip to conversation

blk-mq: add blk_rq_nr_bvec() helper

PATCH Superseded Chaitanya Kulkarni 6 participants 9 messages Nov 11, 2025 – Dec 2, 2025

Subsystems (2)

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 blk-mq: add blk_rq_nr_bvec() helper

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

---

Hi,

During bio submission, the block layer may split a single bvec into multiple physical segments based on device limits:

    submit_bio()
      -> submit_bio_noacct()
        -> __submit_bio_noacct()
          -> __submit_bio()
            -> blk_mq_submit_bio()
              -> __bio_split_to_limits()
                -> bio_split_rw()
                  -> bio_split_rw_at()
                    -> bio_split_io_at()
                      -> bio_for_each_bvec()
                        -> [Fast path] nsegs++ (1 bvec = 1 segment)
                        -> [Slow path] bvec_split_segs()

The bvec_split_segs() function handles the case where a single bvec must be split into multiple segments:

    /**
     * bvec_split_segs - verify whether or not a bvec should be split in the
     *                   middle
     * ...
     * When splitting a bio, it can happen that a bvec is encountered that is
     * too big to fit in a single segment and hence that it has to be split in
     * the middle.
     */
    static bool bvec_split_segs(...)
    {
        while (len && *nsegs < max_segs) {
            seg_size = get_max_segment_size(...);
            (*nsegs)++;
            total_len += seg_size;
            len -= seg_size;
        }
        *bytes += total_len;
        return (len > 0);  // True if bvec was split
    }

Splitting occurs when a bvec exceeds:
- max_segment_size
- segment_boundary_mask (DMA boundary constraints)
- max_segments limit

Result after bio_split_io_at():
- nr_bvec (what rq_for_each_bvec iterates): **1**
- rq->nr_phys_segments: 2

*[ 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*

-ck

---
 drivers/block/loop.c   |  5 ++---
 drivers/block/zloop.c  |  5 ++---
 include/linux/blk-mq.h | 18 ++++++++++++++++++
 3 files changed, 22 insertions(+), 6 deletions(-)
drivers/block/loop.c +2 -3
index 13ce229d450c..7b716d759168 100644
@@ -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) {
 
drivers/block/zloop.c +2 -3
index 92be9f0af00a..857a8de61088 100644
@@ -370,7 +370,7 @@ static void zloop_rw(struct zloop_cmd *cmd)
 	struct iov_iter iter;
 	struct bio_vec tmp;
 	sector_t zone_end;
-	int nr_bvec = 0;
+	unsigned int nr_bvec;
 	int ret;
 
 	atomic_set(&cmd->ref, 2);
@@ -437,8 +437,7 @@ static void zloop_rw(struct zloop_cmd *cmd)
 			zone->cond = BLK_ZONE_COND_FULL;
 	}
 
-	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;
include/linux/blk-mq.h +18 -0
index b25d12545f46..7cedc0eba561 100644
@@ -1185,6 +1185,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)
Signature · 1 line
2.40.0
#1 lore raw
Quoted 4 lines · Chaitanya Kulkarni

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 :-

Perhaps blk_nr_phys_segments is misnamed as it represents device segments, not physical host memory segments. Instead of rewalking to calculate physical segments, maybe just introduce a new field into the request so that we can save both the physical and device segments to avoid having to repeatedly rewalk the same list. There is an ongoing effort to avoid such repeated work.

#2 lore raw
Quoted 11 lines · Keith Busch
Quoted 4 lines · Chaitanya Kulkarni

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 :-

Perhaps blk_nr_phys_segments is misnamed as it represents device segments, not physical host memory segments. Instead of rewalking to calculate physical segments, maybe just introduce a new field into the request so that we can save both the physical and device segments to avoid having to repeatedly rewalk the same list. There is an ongoing effort to avoid such repeated work.

I like the idea, how about something like this on the top of this patch.

Totally untested, incomplete, only good for conceptual discussion :-

  block/blk-merge.c      | 10 ++++++++--
  block/blk-mq.c         |  2 ++
  include/linux/blk-mq.h | 13 +++----------
  3 files changed, 13 insertions(+), 12 deletions(-)
block/blk-merge.c +7 -1
index c47d18587a0b..40b7ba3b6216 100644
@@ -458,6 +458,7 @@ EXPORT_SYMBOL(bio_split_to_limits);
  unsigned int blk_recalc_rq_segments(struct request *rq)
  {
  	unsigned int nr_phys_segs = 0;
+	unsigned int nr_bvecs = 0;
  	unsigned int bytes = 0;
  	struct req_iterator iter;
  	struct bio_vec bv;
@@ -482,9 +483,12 @@ unsigned int blk_recalc_rq_segments(struct request *rq)
  		break;
  	}
 
-	rq_for_each_bvec(bv, rq, iter)
+	rq_for_each_bvec(bv, rq, iter) {
  		bvec_split_segs(&rq->q->limits, &bv, &nr_phys_segs, &bytes,
  				UINT_MAX, UINT_MAX);
+		nr_bvecs++;
+	}
+	rq->nr_bvecs = nr_bvecs;
  	return nr_phys_segs;
  }
 
@@ -531,6 +535,7 @@ static inline int ll_new_hw_segment(struct request *req, struct bio *bio,
  	 * counters.
  	 */
  	req->nr_phys_segments += nr_phys_segs;
+	req->nr_bvecs += bio->bi_vcnt;
  	if (bio_integrity(bio))
  		req->nr_integrity_segments += blk_rq_count_integrity_sg(req->q,
  									bio);
@@ -626,6 +631,7 @@ static int ll_merge_requests_fn(struct request_queue *q, struct request *req,
 
  	/* Merge is OK... */
  	req->nr_phys_segments = total_phys_segments;
+	req->nr_bvecs += next->nr_bvecs;
  	req->nr_integrity_segments += next->nr_integrity_segments;
  	return 1;
  }
block/blk-mq.c +2 -0
index d626d32f6e57..61cd0bd5b5af 100644
@@ -436,6 +436,7 @@ static struct request *blk_mq_rq_ctx_init(struct blk_mq_alloc_data *data,
  	rq->stats_sectors = 0;
  	rq->nr_phys_segments = 0;
  	rq->nr_integrity_segments = 0;
+	rq->nr_bvecs = 0;
  	rq->end_io = NULL;
  	rq->end_io_data = NULL;
 
@@ -2675,6 +2676,7 @@ static void blk_mq_bio_to_request(struct request *rq, struct bio *bio,
  	rq->__sector = bio->bi_iter.bi_sector;
  	rq->__data_len = bio->bi_iter.bi_size;
  	rq->nr_phys_segments = nr_segs;
+	rq->nr_bvecs = bio->bi_vcnt;
  	if (bio_integrity(bio))
  		rq->nr_integrity_segments = blk_rq_count_integrity_sg(rq->q,
  								      bio);
include/linux/blk-mq.h +5 -10
index 7cedc0eba561..c563255586c6 100644
@@ -151,6 +151,7 @@ struct request {
  	 */
  	unsigned short nr_phys_segments;
  	unsigned short nr_integrity_segments;
+	unsigned short nr_bvecs;
 
  #ifdef CONFIG_BLK_INLINE_ENCRYPTION
  	struct bio_crypt_ctx *crypt_ctx;
@@ -1187,20 +1188,14 @@ static inline unsigned short blk_rq_nr_discard_segments(struct request *rq)
 
  /**
   * blk_rq_nr_bvec - return number of bvecs in a request
- * @rq: request to calculate bvecs for
+ * @rq: request to get bvec count for
   *
- * Returns the number of bvecs.
+ * Returns the number of bvecs in the request. This count is maintained
+ * during request initialization and merging to avoid repeated rewalking.
   */
  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;
+	return rq->nr_bvecs;
  }
 
  int __blk_rq_map_sg(struct request *rq, struct scatterlist *sglist,

--

-ck

#3 lore raw
Quoted 78 lines · Chaitanya Kulkarni
Quoted 11 lines · Keith Busch
Quoted 4 lines · Chaitanya Kulkarni

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 :-

Perhaps blk_nr_phys_segments is misnamed as it represents device segments, not physical host memory segments. Instead of rewalking to calculate physical segments, maybe just introduce a new field into the request so that we can save both the physical and device segments to avoid having to repeatedly rewalk the same list. There is an ongoing effort to avoid such repeated work.

I like the idea, how about something like this on the top of this patch.

Totally untested, incomplete, only good for conceptual discussion :-

  block/blk-merge.c      | 10 ++++++++--
  block/blk-mq.c         |  2 ++
  include/linux/blk-mq.h | 13 +++----------
  3 files changed, 13 insertions(+), 12 deletions(-)
block/blk-merge.c +7 -1
index c47d18587a0b..40b7ba3b6216 100644
@@ -458,6 +458,7 @@ EXPORT_SYMBOL(bio_split_to_limits);
  unsigned int blk_recalc_rq_segments(struct request *rq)
  {
        unsigned int nr_phys_segs = 0;
+       unsigned int nr_bvecs = 0;
        unsigned int bytes = 0;
        struct req_iterator iter;
        struct bio_vec bv;
@@ -482,9 +483,12 @@ unsigned int blk_recalc_rq_segments(struct request *rq)
                break;
        }
 
-       rq_for_each_bvec(bv, rq, iter)
+       rq_for_each_bvec(bv, rq, iter) {
                bvec_split_segs(&rq->q->limits, &bv, &nr_phys_segs, &bytes,
                                UINT_MAX, UINT_MAX);
+               nr_bvecs++;
+       }
+       rq->nr_bvecs = nr_bvecs;
        return nr_phys_segs;
  }
 
@@ -531,6 +535,7 @@ static inline int ll_new_hw_segment(struct request *req, struct bio *bio,
         * counters.
         */
        req->nr_phys_segments += nr_phys_segs;
+       req->nr_bvecs += bio->bi_vcnt;
        if (bio_integrity(bio))
                req->nr_integrity_segments += blk_rq_count_integrity_sg(req->q,
                                                                        bio);
@@ -626,6 +631,7 @@ static int ll_merge_requests_fn(struct request_queue *q, struct request *req,
 
        /* Merge is OK... */
        req->nr_phys_segments = total_phys_segments;
+       req->nr_bvecs += next->nr_bvecs;
        req->nr_integrity_segments += next->nr_integrity_segments;
        return 1;
  }
block/blk-mq.c +1 -0
index d626d32f6e57..61cd0bd5b5af 100644
@@ -436,6 +436,7 @@ static struct request *blk_mq_rq_ctx_init(struct blk_mq_alloc_data *data,
        rq->stats_sectors = 0;
        rq->nr_phys_segments = 0;
        rq->nr_integrity_segments = 0;
+       rq->nr_bvecs = 0;
        rq->end_io = NULL;
        rq->end_io_data = NULL;
 

block/blk-mq.c static void blk_mq_bio_to_request(struct request *rq, struct bio *bio, · line 2679

Quoted 4 lines
block/blk-mq.c +1 -0
@@ -2675,6 +2676,7 @@ static void blk_mq_bio_to_request(struct request *rq, struct bio *bio,
        rq->__sector = bio->bi_iter.bi_sector;
        rq->__data_len = bio->bi_iter.bi_size;
        rq->nr_phys_segments = nr_segs;
+       rq->nr_bvecs = bio->bi_vcnt;

Please do not use bio->bi_vcnt, which should only be available for FS bio code.

It is obviously wrong to touch it for cloned bio...

Thanks,
Ming

#4 lore raw
Quoted 11 lines · Ming Lei

block/blk-mq.c static void blk_mq_bio_to_request(struct request *rq, struct bio *bio, · line 2679

Quoted 5 lines
block/blk-mq.c +1 -0
@@ -2675,6 +2676,7 @@ static void blk_mq_bio_to_request(struct request *rq, struct bio *bio,
         rq->__sector = bio->bi_iter.bi_sector;
         rq->__data_len = bio->bi_iter.bi_size;
         rq->nr_phys_segments = nr_segs;
+       rq->nr_bvecs = bio->bi_vcnt;

Please do not use bio->bi_vcnt, which should only be available for FS bio code.

It is obviously wrong to touch it for cloned bio...

Thanks,
Ming

yes, it's wrong and above patch is not functionally correct, purely for conceptual discussion, to add a member to struct request if acceptable ... it's going to increase the size of struct request, but if we can live with it without significant performance cost or adding a new cacheline access maybe it is worth it ? -ck

maintainer
#5 lore raw
In reply to Keith Busch
Quoted 6 lines · Keith Busch
Quoted 4 lines · Chaitanya Kulkarni

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 :-

Quoted 6 lines

Perhaps blk_nr_phys_segments is misnamed as it represents device segments, not physical host memory segments. Instead of rewalking to calculate physical segments, maybe just introduce a new field into the request so that we can save both the physical and device segments to avoid having to repeatedly rewalk the same list. There is an ongoing effort to avoid such repeated work.

Is it used in the fast path? From the initial patch, I only see loop/zloop using it, and that's certainly not enough to warrant further bloating struct request. Just have them iterate the darn thing, it's not a huge deal.

Signature · 1 line
Jens Axboe
#6 lore raw
Quoted 12 lines · Jens Axboe
Quoted 11 lines · Keith Busch
Quoted 4 lines · Chaitanya Kulkarni

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 :-

Perhaps blk_nr_phys_segments is misnamed as it represents device segments, not physical host memory segments. Instead of rewalking to calculate physical segments, maybe just introduce a new field into the request so that we can save both the physical and device segments to avoid having to repeatedly rewalk the same list. There is an ongoing effort to avoid such repeated work.

Quoted 5 lines

Is it used in the fast path? From the initial patch, I only see loop/zloop using it, and that's certainly not enough to warrant further bloating struct request. Just have them iterate the darn thing, it's not a huge deal.

okay, will keep the V2 as is to get reviews.

-ck

Reviewed-by on v2 1/1
#7 lore raw
In reply to Chaitanya Kulkarni
Quoted 21 lines · Chaitanya Kulkarni

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 :-

Quoted 8 lines
[ 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

---

What is the status of this patch?

FWIW, looks good to me:

#8 lore raw
Quoted 11 lines · Niklas Cassel
Quoted 11 lines

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
Quoted 7 lines
Quoted 2 lines

---

What is the status of this patch?

FWIW, looks good to me:

Thanks Niklas, it's not been merged yet.

I'll send rabased V2.

-ck