linux-block.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* simplify I/O size calculation helpers v2
@ 2021-10-13 17:12 Christoph Hellwig
  2021-10-13 17:12 ` [PATCH 1/6] block: factor out a chunk_size_left helper Christoph Hellwig
                   ` (5 more replies)
  0 siblings, 6 replies; 9+ messages in thread
From: Christoph Hellwig @ 2021-10-13 17:12 UTC (permalink / raw)
  To: Jens Axboe; +Cc: Mike Snitzer, linux-block, dm-devel

Hi Jens,

this series calculates various I/O size calculations.

Changes since v1:
 - rename chunk_size_left to blk_chunk_sectors_left
 - split a patch into two

Diffstat:
 block/blk-merge.c      |   28 ++++++++++++++++------------
 drivers/md/dm.c        |   18 ++++++------------
 include/linux/blkdev.h |   27 ++++++++-------------------
 3 files changed, 30 insertions(+), 43 deletions(-)

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

* [PATCH 1/6] block: factor out a chunk_size_left helper
  2021-10-13 17:12 simplify I/O size calculation helpers v2 Christoph Hellwig
@ 2021-10-13 17:12 ` Christoph Hellwig
  2021-10-13 17:12 ` [PATCH 2/6] dm: open code blk_max_size_offset in max_io_len Christoph Hellwig
                   ` (4 subsequent siblings)
  5 siblings, 0 replies; 9+ messages in thread
From: Christoph Hellwig @ 2021-10-13 17:12 UTC (permalink / raw)
  To: Jens Axboe; +Cc: Mike Snitzer, linux-block, dm-devel

Factor out a helper from blk_max_size_offset so that it can be reused
independently.

Signed-off-by: Christoph Hellwig <hch@lst.de>
---
 include/linux/blkdev.h | 20 ++++++++++++++------
 1 file changed, 14 insertions(+), 6 deletions(-)

diff --git a/include/linux/blkdev.h b/include/linux/blkdev.h
index 17705c970d7e1..c6ecd996c2d46 100644
--- a/include/linux/blkdev.h
+++ b/include/linux/blkdev.h
@@ -622,6 +622,18 @@ static inline unsigned int blk_queue_get_max_sectors(struct request_queue *q,
 	return q->limits.max_sectors;
 }
 
+/*
+ * Return how much of the chunk sectors is left to be used for an I/O at the
+ * given offset.
+ */
+static inline unsigned int blk_chunk_sectors_left(sector_t offset,
+		unsigned int chunk_sectors)
+{
+	if (unlikely(!is_power_of_2(chunk_sectors)))
+		return chunk_sectors - sector_div(offset, chunk_sectors);
+	return chunk_sectors - (offset & (chunk_sectors - 1));
+}
+
 /*
  * Return maximum size of a request at given offset. Only valid for
  * file system requests.
@@ -637,12 +649,8 @@ static inline unsigned int blk_max_size_offset(struct request_queue *q,
 			return q->limits.max_sectors;
 	}
 
-	if (likely(is_power_of_2(chunk_sectors)))
-		chunk_sectors -= offset & (chunk_sectors - 1);
-	else
-		chunk_sectors -= sector_div(offset, chunk_sectors);
-
-	return min(q->limits.max_sectors, chunk_sectors);
+	return min(q->limits.max_sectors,
+			blk_chunk_sectors_left(offset, chunk_sectors));
 }
 
 /*
-- 
2.30.2


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

* [PATCH 2/6] dm: open code blk_max_size_offset in max_io_len
  2021-10-13 17:12 simplify I/O size calculation helpers v2 Christoph Hellwig
  2021-10-13 17:12 ` [PATCH 1/6] block: factor out a chunk_size_left helper Christoph Hellwig
@ 2021-10-13 17:12 ` Christoph Hellwig
  2021-10-13 17:12 ` [PATCH 3/6] block: only call blk_queue_get_max_sectors once in blk_rq_get_max_sectors Christoph Hellwig
                   ` (3 subsequent siblings)
  5 siblings, 0 replies; 9+ messages in thread
From: Christoph Hellwig @ 2021-10-13 17:12 UTC (permalink / raw)
  To: Jens Axboe; +Cc: Mike Snitzer, linux-block, dm-devel

max_io_len always passed in an explicit, non-zero chunk_sectors into
blk_max_size_offset.  That means much of blk_max_size_offset is not needed
and open coding it simplifies the code.

Signed-off-by: Christoph Hellwig <hch@lst.de>
---
 drivers/md/dm.c | 18 ++++++------------
 1 file changed, 6 insertions(+), 12 deletions(-)

diff --git a/drivers/md/dm.c b/drivers/md/dm.c
index a011d09cb0fac..825ed9d84c9c0 100644
--- a/drivers/md/dm.c
+++ b/drivers/md/dm.c
@@ -944,24 +944,18 @@ static inline sector_t max_io_len_target_boundary(struct dm_target *ti,
 static sector_t max_io_len(struct dm_target *ti, sector_t sector)
 {
 	sector_t target_offset = dm_target_offset(ti, sector);
-	sector_t len = max_io_len_target_boundary(ti, target_offset);
-	sector_t max_len;
+	unsigned int len = max_io_len_target_boundary(ti, target_offset);
 
 	/*
 	 * Does the target need to split IO even further?
 	 * - varied (per target) IO splitting is a tenet of DM; this
 	 *   explains why stacked chunk_sectors based splitting via
-	 *   blk_max_size_offset() isn't possible here. So pass in
-	 *   ti->max_io_len to override stacked chunk_sectors.
+	 *   blk_queue_split() isn't possible here.
 	 */
-	if (ti->max_io_len) {
-		max_len = blk_max_size_offset(ti->table->md->queue,
-					      target_offset, ti->max_io_len);
-		if (len > max_len)
-			len = max_len;
-	}
-
-	return len;
+	if (!ti->max_io_len)
+		return len;
+	return min3(len, ti->table->md->queue->limits.max_sectors,
+		    blk_chunk_sectors_left(target_offset, ti->max_io_len));
 }
 
 int dm_set_target_max_io_len(struct dm_target *ti, sector_t len)
-- 
2.30.2


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

* [PATCH 3/6] block: only call blk_queue_get_max_sectors once in blk_rq_get_max_sectors
  2021-10-13 17:12 simplify I/O size calculation helpers v2 Christoph Hellwig
  2021-10-13 17:12 ` [PATCH 1/6] block: factor out a chunk_size_left helper Christoph Hellwig
  2021-10-13 17:12 ` [PATCH 2/6] dm: open code blk_max_size_offset in max_io_len Christoph Hellwig
@ 2021-10-13 17:12 ` Christoph Hellwig
  2021-10-13 17:12 ` [PATCH 4/6] block: open code blk_max_size_offset " Christoph Hellwig
                   ` (2 subsequent siblings)
  5 siblings, 0 replies; 9+ messages in thread
From: Christoph Hellwig @ 2021-10-13 17:12 UTC (permalink / raw)
  To: Jens Axboe; +Cc: Mike Snitzer, linux-block, dm-devel

Consolidate the two calls to blk_rq_get_max_sectors into one using a
local variable.

Signed-off-by: Christoph Hellwig <hch@lst.de>
---
 block/blk-merge.c | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)

diff --git a/block/blk-merge.c b/block/blk-merge.c
index 14ce19607cd8a..8ed50952e93ad 100644
--- a/block/blk-merge.c
+++ b/block/blk-merge.c
@@ -595,17 +595,18 @@ static inline unsigned int blk_rq_get_max_sectors(struct request *rq,
 						  sector_t offset)
 {
 	struct request_queue *q = rq->q;
+	unsigned int max_sectors;
 
 	if (blk_rq_is_passthrough(rq))
 		return q->limits.max_hw_sectors;
 
+	max_sectors = blk_queue_get_max_sectors(q, req_op(rq));
 	if (!q->limits.chunk_sectors ||
 	    req_op(rq) == REQ_OP_DISCARD ||
 	    req_op(rq) == REQ_OP_SECURE_ERASE)
-		return blk_queue_get_max_sectors(q, req_op(rq));
+		return max_sectors;
 
-	return min(blk_max_size_offset(q, offset, 0),
-			blk_queue_get_max_sectors(q, req_op(rq)));
+	return min(max_sectors, blk_max_size_offset(q, offset, 0));
 }
 
 static inline int ll_new_hw_segment(struct request *req, struct bio *bio,
-- 
2.30.2


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

* [PATCH 4/6] block: open code blk_max_size_offset in blk_rq_get_max_sectors
  2021-10-13 17:12 simplify I/O size calculation helpers v2 Christoph Hellwig
                   ` (2 preceding siblings ...)
  2021-10-13 17:12 ` [PATCH 3/6] block: only call blk_queue_get_max_sectors once in blk_rq_get_max_sectors Christoph Hellwig
@ 2021-10-13 17:12 ` Christoph Hellwig
  2021-10-13 17:12 ` [PATCH 5/6] block: fold blk_max_size_offset into get_max_io_size Christoph Hellwig
  2021-10-13 17:12 ` [PATCH 6/6] block: pass the start sector to get_max_io_size Christoph Hellwig
  5 siblings, 0 replies; 9+ messages in thread
From: Christoph Hellwig @ 2021-10-13 17:12 UTC (permalink / raw)
  To: Jens Axboe; +Cc: Mike Snitzer, linux-block, dm-devel

blk_rq_get_max_sectors always uses q->limits.chunk_sectors as the
chunk_sectors argument, and already checks for max_sectors through the
call to blk_queue_get_max_sectors.  That means much of
blk_max_size_offset is not needed and open coding it simplifies the code.

Signed-off-by: Christoph Hellwig <hch@lst.de>
---
 block/blk-merge.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/block/blk-merge.c b/block/blk-merge.c
index 8ed50952e93ad..3e9dde152fdcd 100644
--- a/block/blk-merge.c
+++ b/block/blk-merge.c
@@ -605,8 +605,8 @@ static inline unsigned int blk_rq_get_max_sectors(struct request *rq,
 	    req_op(rq) == REQ_OP_DISCARD ||
 	    req_op(rq) == REQ_OP_SECURE_ERASE)
 		return max_sectors;
-
-	return min(max_sectors, blk_max_size_offset(q, offset, 0));
+	return min(max_sectors,
+		   blk_chunk_sectors_left(offset, q->limits.chunk_sectors));
 }
 
 static inline int ll_new_hw_segment(struct request *req, struct bio *bio,
-- 
2.30.2


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

* [PATCH 5/6] block: fold blk_max_size_offset into get_max_io_size
  2021-10-13 17:12 simplify I/O size calculation helpers v2 Christoph Hellwig
                   ` (3 preceding siblings ...)
  2021-10-13 17:12 ` [PATCH 4/6] block: open code blk_max_size_offset " Christoph Hellwig
@ 2021-10-13 17:12 ` Christoph Hellwig
  2021-10-13 17:12 ` [PATCH 6/6] block: pass the start sector to get_max_io_size Christoph Hellwig
  5 siblings, 0 replies; 9+ messages in thread
From: Christoph Hellwig @ 2021-10-13 17:12 UTC (permalink / raw)
  To: Jens Axboe; +Cc: Mike Snitzer, linux-block, dm-devel

Fold blk_max_size_offset into the only remaining user.

Signed-off-by: Christoph Hellwig <hch@lst.de>
---
 block/blk-merge.c      | 15 ++++++++++-----
 include/linux/blkdev.h | 19 -------------------
 2 files changed, 10 insertions(+), 24 deletions(-)

diff --git a/block/blk-merge.c b/block/blk-merge.c
index 3e9dde152fdcd..87ea3e7b8ad28 100644
--- a/block/blk-merge.c
+++ b/block/blk-merge.c
@@ -177,14 +177,19 @@ static struct bio *blk_bio_write_same_split(struct request_queue *q,
 static inline unsigned get_max_io_size(struct request_queue *q,
 				       struct bio *bio)
 {
-	unsigned sectors = blk_max_size_offset(q, bio->bi_iter.bi_sector, 0);
-	unsigned max_sectors = sectors;
 	unsigned pbs = queue_physical_block_size(q) >> SECTOR_SHIFT;
 	unsigned lbs = queue_logical_block_size(q) >> SECTOR_SHIFT;
-	unsigned start_offset = bio->bi_iter.bi_sector & (pbs - 1);
+	sector_t sector = bio->bi_iter.bi_sector;
+	unsigned start_offset = sector & (pbs - 1);
+	unsigned sectors = q->limits.max_sectors;
+	unsigned max_sectors;
+
+	if (q->limits.chunk_sectors) {
+		sectors = min(sectors, blk_chunk_sectors_left(sector,
+						q->limits.chunk_sectors));
+	}
 
-	max_sectors += start_offset;
-	max_sectors &= ~(pbs - 1);
+	max_sectors = (sectors + start_offset) & ~(pbs - 1);
 	if (max_sectors > start_offset)
 		return max_sectors - start_offset;
 
diff --git a/include/linux/blkdev.h b/include/linux/blkdev.h
index c6ecd996c2d46..7e6b68969cec0 100644
--- a/include/linux/blkdev.h
+++ b/include/linux/blkdev.h
@@ -634,25 +634,6 @@ static inline unsigned int blk_chunk_sectors_left(sector_t offset,
 	return chunk_sectors - (offset & (chunk_sectors - 1));
 }
 
-/*
- * Return maximum size of a request at given offset. Only valid for
- * file system requests.
- */
-static inline unsigned int blk_max_size_offset(struct request_queue *q,
-					       sector_t offset,
-					       unsigned int chunk_sectors)
-{
-	if (!chunk_sectors) {
-		if (q->limits.chunk_sectors)
-			chunk_sectors = q->limits.chunk_sectors;
-		else
-			return q->limits.max_sectors;
-	}
-
-	return min(q->limits.max_sectors,
-			blk_chunk_sectors_left(offset, chunk_sectors));
-}
-
 /*
  * Access functions for manipulating queue properties
  */
-- 
2.30.2


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

* [PATCH 6/6] block: pass the start sector to get_max_io_size
  2021-10-13 17:12 simplify I/O size calculation helpers v2 Christoph Hellwig
                   ` (4 preceding siblings ...)
  2021-10-13 17:12 ` [PATCH 5/6] block: fold blk_max_size_offset into get_max_io_size Christoph Hellwig
@ 2021-10-13 17:12 ` Christoph Hellwig
  5 siblings, 0 replies; 9+ messages in thread
From: Christoph Hellwig @ 2021-10-13 17:12 UTC (permalink / raw)
  To: Jens Axboe; +Cc: Mike Snitzer, linux-block, dm-devel

Pass the start sector instead of the whole bio.

Signed-off-by: Christoph Hellwig <hch@lst.de>
---
 block/blk-merge.c | 6 ++----
 1 file changed, 2 insertions(+), 4 deletions(-)

diff --git a/block/blk-merge.c b/block/blk-merge.c
index 87ea3e7b8ad28..7498f570aa302 100644
--- a/block/blk-merge.c
+++ b/block/blk-merge.c
@@ -174,12 +174,10 @@ static struct bio *blk_bio_write_same_split(struct request_queue *q,
  * requests that are submitted to a block device if the start of a bio is not
  * aligned to a physical block boundary.
  */
-static inline unsigned get_max_io_size(struct request_queue *q,
-				       struct bio *bio)
+static inline unsigned get_max_io_size(struct request_queue *q, sector_t sector)
 {
 	unsigned pbs = queue_physical_block_size(q) >> SECTOR_SHIFT;
 	unsigned lbs = queue_logical_block_size(q) >> SECTOR_SHIFT;
-	sector_t sector = bio->bi_iter.bi_sector;
 	unsigned start_offset = sector & (pbs - 1);
 	unsigned sectors = q->limits.max_sectors;
 	unsigned max_sectors;
@@ -288,7 +286,7 @@ static struct bio *blk_bio_segment_split(struct request_queue *q,
 	struct bio_vec bv, bvprv, *bvprvp = NULL;
 	struct bvec_iter iter;
 	unsigned nsegs = 0, sectors = 0;
-	const unsigned max_sectors = get_max_io_size(q, bio);
+	const unsigned max_sectors = get_max_io_size(q, bio->bi_iter.bi_sector);
 	const unsigned max_segs = queue_max_segments(q);
 
 	bio_for_each_bvec(bv, bio, iter) {
-- 
2.30.2


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

* Re: [PATCH 2/6] dm: open code blk_max_size_offset in max_io_len
  2022-06-14  9:09 ` [PATCH 2/6] dm: open code blk_max_size_offset in max_io_len Christoph Hellwig
@ 2022-06-16 23:21   ` Mike Snitzer
  0 siblings, 0 replies; 9+ messages in thread
From: Mike Snitzer @ 2022-06-16 23:21 UTC (permalink / raw)
  To: Christoph Hellwig; +Cc: Jens Axboe, dm-devel, linux-block

On Tue, Jun 14 2022 at  5:09P -0400,
Christoph Hellwig <hch@lst.de> wrote:

> max_io_len always passes an explicitly non-zero chunk_sectors into
> blk_max_size_offset.  That means much of blk_max_size_offset is not
> needed and can be open coded to simplify the code.
> 
> Signed-off-by: Christoph Hellwig <hch@lst.de>
> ---
>  drivers/md/dm.c | 17 ++++++-----------
>  1 file changed, 6 insertions(+), 11 deletions(-)
> 
> diff --git a/drivers/md/dm.c b/drivers/md/dm.c
> index d8f16183bf27c..0514358a1f8e5 100644
> --- a/drivers/md/dm.c
> +++ b/drivers/md/dm.c
> @@ -1079,23 +1079,18 @@ static sector_t max_io_len(struct dm_target *ti, sector_t sector)
>  {
>  	sector_t target_offset = dm_target_offset(ti, sector);
>  	sector_t len = max_io_len_target_boundary(ti, target_offset);
> -	sector_t max_len;
>  
>  	/*
>  	 * Does the target need to split IO even further?
>  	 * - varied (per target) IO splitting is a tenet of DM; this
>  	 *   explains why stacked chunk_sectors based splitting via
> -	 *   blk_max_size_offset() isn't possible here. So pass in
> -	 *   ti->max_io_len to override stacked chunk_sectors.
> +	 *   blk_queue_split() isn't possible here.
>  	 */
> -	if (ti->max_io_len) {
> -		max_len = blk_max_size_offset(ti->table->md->queue,
> -					      target_offset, ti->max_io_len);
> -		if (len > max_len)
> -			len = max_len;
> -	}
> -
> -	return len;
> +	if (!ti->max_io_len)
> +		return len;
> +	return min_t(sector_t, len,
> +		min(queue_max_sectors(ti->table->md->queue),
> +		    blk_chunk_sectors_left(target_offset, ti->max_io_len)));
>  }
>  
>  int dm_set_target_max_io_len(struct dm_target *ti, sector_t len)
> -- 
> 2.30.2
> 

Not in love with the nested min() but don't have a better suggestion.

Reviewed-by: Mike Snitzer <snitzer@kernel.org>

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

* [PATCH 2/6] dm: open code blk_max_size_offset in max_io_len
  2022-06-14  9:09 clean up the chunk_sizehandling helpers a little Christoph Hellwig
@ 2022-06-14  9:09 ` Christoph Hellwig
  2022-06-16 23:21   ` Mike Snitzer
  0 siblings, 1 reply; 9+ messages in thread
From: Christoph Hellwig @ 2022-06-14  9:09 UTC (permalink / raw)
  To: Jens Axboe; +Cc: Mike Snitzer, dm-devel, linux-block

max_io_len always passes an explicitly non-zero chunk_sectors into
blk_max_size_offset.  That means much of blk_max_size_offset is not
needed and can be open coded to simplify the code.

Signed-off-by: Christoph Hellwig <hch@lst.de>
---
 drivers/md/dm.c | 17 ++++++-----------
 1 file changed, 6 insertions(+), 11 deletions(-)

diff --git a/drivers/md/dm.c b/drivers/md/dm.c
index d8f16183bf27c..0514358a1f8e5 100644
--- a/drivers/md/dm.c
+++ b/drivers/md/dm.c
@@ -1079,23 +1079,18 @@ static sector_t max_io_len(struct dm_target *ti, sector_t sector)
 {
 	sector_t target_offset = dm_target_offset(ti, sector);
 	sector_t len = max_io_len_target_boundary(ti, target_offset);
-	sector_t max_len;
 
 	/*
 	 * Does the target need to split IO even further?
 	 * - varied (per target) IO splitting is a tenet of DM; this
 	 *   explains why stacked chunk_sectors based splitting via
-	 *   blk_max_size_offset() isn't possible here. So pass in
-	 *   ti->max_io_len to override stacked chunk_sectors.
+	 *   blk_queue_split() isn't possible here.
 	 */
-	if (ti->max_io_len) {
-		max_len = blk_max_size_offset(ti->table->md->queue,
-					      target_offset, ti->max_io_len);
-		if (len > max_len)
-			len = max_len;
-	}
-
-	return len;
+	if (!ti->max_io_len)
+		return len;
+	return min_t(sector_t, len,
+		min(queue_max_sectors(ti->table->md->queue),
+		    blk_chunk_sectors_left(target_offset, ti->max_io_len)));
 }
 
 int dm_set_target_max_io_len(struct dm_target *ti, sector_t len)
-- 
2.30.2


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

end of thread, other threads:[~2022-06-16 23:21 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-10-13 17:12 simplify I/O size calculation helpers v2 Christoph Hellwig
2021-10-13 17:12 ` [PATCH 1/6] block: factor out a chunk_size_left helper Christoph Hellwig
2021-10-13 17:12 ` [PATCH 2/6] dm: open code blk_max_size_offset in max_io_len Christoph Hellwig
2021-10-13 17:12 ` [PATCH 3/6] block: only call blk_queue_get_max_sectors once in blk_rq_get_max_sectors Christoph Hellwig
2021-10-13 17:12 ` [PATCH 4/6] block: open code blk_max_size_offset " Christoph Hellwig
2021-10-13 17:12 ` [PATCH 5/6] block: fold blk_max_size_offset into get_max_io_size Christoph Hellwig
2021-10-13 17:12 ` [PATCH 6/6] block: pass the start sector to get_max_io_size Christoph Hellwig
2022-06-14  9:09 clean up the chunk_sizehandling helpers a little Christoph Hellwig
2022-06-14  9:09 ` [PATCH 2/6] dm: open code blk_max_size_offset in max_io_len Christoph Hellwig
2022-06-16 23:21   ` Mike Snitzer

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).