public inbox for linux-block@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH] blk-mq: add blk_rq_nr_bvec() helper
@ 2025-11-11 18:17 Chaitanya Kulkarni
  2025-11-11 19:04 ` Chaitanya Kulkarni
  0 siblings, 1 reply; 2+ messages in thread
From: Chaitanya Kulkarni @ 2025-11-11 18:17 UTC (permalink / raw)
  To: linux-block; +Cc: axboe, hch, kch, dlemoal, Chaitanya Kulkarni

Add a new helper function blk_rq_nr_bvec() that returns the number of 
bvec segments in a request. This count represents the number of iterations
rq_for_each_bvec() would perform.

Drivers need to pre-allocate bvec arrays before iterating through
a request's bvecs. Currently, they manually count segments 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 :-

[ 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

Signed-off-by: Chaitanya Kulkarni <ckulkarnilinux@gmail.com>
---
Hi,

This patch also provides a clear API to avoid any potential misuse of
blk_nr_phys_segments() for calculating the bvecs in drivers since :-

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 | 19 +++++++++++++++++++
 3 files changed, 23 insertions(+), 6 deletions(-)

diff --git a/drivers/block/loop.c b/drivers/block/loop.c
index 13ce229d450c..7b716d759168 100644
--- a/drivers/block/loop.c
+++ b/drivers/block/loop.c
@@ -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) {
 
diff --git a/drivers/block/zloop.c b/drivers/block/zloop.c
index 92be9f0af00a..857a8de61088 100644
--- a/drivers/block/zloop.c
+++ b/drivers/block/zloop.c
@@ -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;
diff --git a/include/linux/blk-mq.h b/include/linux/blk-mq.h
index b25d12545f46..ef1a80d41e15 100644
--- a/include/linux/blk-mq.h
+++ b/include/linux/blk-mq.h
@@ -1185,6 +1185,25 @@ 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 bvec segments in a request
+ * @rq: request to calculate bvvecs for
+ *
+ * Returns the number of bvec segments that would be iterated by
+ * rq_for_each_bvec().
+ */
+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)
-- 
2.40.0


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

* Re: [PATCH] blk-mq: add blk_rq_nr_bvec() helper
  2025-11-11 18:17 [PATCH] blk-mq: add blk_rq_nr_bvec() helper Chaitanya Kulkarni
@ 2025-11-11 19:04 ` Chaitanya Kulkarni
  0 siblings, 0 replies; 2+ messages in thread
From: Chaitanya Kulkarni @ 2025-11-11 19:04 UTC (permalink / raw)
  To: Chaitanya Kulkarni, linux-block@vger.kernel.org
  Cc: axboe@kernel.dk, hch@lst.de, Chaitanya Kulkarni,
	dlemoal@kernel.org

On 11/11/25 10:17, Chaitanya Kulkarni wrote:
> Add a new helper function blk_rq_nr_bvec() that returns the number of
> bvec segments in a request. This count represents the number of iterations
> rq_for_each_bvec() would perform.
>
> Drivers need to pre-allocate bvec arrays before iterating through
> a request's bvecs. Currently, they manually count segments 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 :-
>
> [ 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
>
> Signed-off-by: Chaitanya Kulkarni<ckulkarnilinux@gmail.com>

re-sending V2 with typos fixed.

-ck



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

end of thread, other threads:[~2025-11-11 19:04 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-11-11 18:17 [PATCH] blk-mq: add blk_rq_nr_bvec() helper Chaitanya Kulkarni
2025-11-11 19:04 ` Chaitanya Kulkarni

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