Skip to contentSkip to conversation

simplify I/O size calculation helpers

PATCH In review Christoph Hellwig 2 participants 7 messages Oct 12, 2021

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

Hi Jens,

this series calculates various I/O size calculations.

Diffstat:

 block/blk-merge.c      |   33 ++++++++++++++++++---------------
 drivers/md/dm.c        |   18 ++++++------------
 include/linux/blkdev.h |   27 ++++++++-------------------
 3 files changed, 32 insertions(+), 46 deletions(-)
#1 lore raw

PATCH 1/5 block: factor out a chunk_size_left helper

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

---
 include/linux/blkdev.h | 20 ++++++++++++++------
 1 file changed, 14 insertions(+), 6 deletions(-)
include/linux/blkdev.h +14 -6
index 17705c970d7e1..0b020bb45a3e7 100644
@@ -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 chunk_size_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,
+			chunk_size_left(offset, chunk_sectors));
 }
 
 /*
Signature · 1 line
2.30.2
#2 lore raw

Re: [dm-devel] [PATCH 1/5] block: factor out a chunk_size_left helper

include/linux/blkdev.h static inline unsigned int blk_queue_get_max_sectors(struct request_queue *q, · line 635

Quoted 11 lines · Christoph Hellwig
include/linux/blkdev.h +11 -0
+/*
+ * Return how much of the chunk sectors is left to be used for an I/O at the
+ * given offset.
+ */
+static inline unsigned int chunk_size_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));
+}

No "blk_" prefix for the function name? I think most other functions declared or defined in this header file have such a prefix.

Thanks,

Bart.

#3 lore raw

PATCH 2/5 dm: open code blk_max_size_offset in max_io_len

In reply to Christoph Hellwig

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.

---
 drivers/md/dm.c | 18 ++++++------------
 1 file changed, 6 insertions(+), 12 deletions(-)
drivers/md/dm.c +6 -12
index a011d09cb0fac..0b6dfe8667438 100644
@@ -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,
+		    chunk_size_left(target_offset, ti->max_io_len));
 }
 
 int dm_set_target_max_io_len(struct dm_target *ti, sector_t len)
Signature · 1 line
2.30.2
#4 lore raw

PATCH 3/5 block: open code blk_max_size_offset in blk_rq_get_max_sectors

In reply to Christoph Hellwig

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.

---
 block/blk-merge.c | 15 ++++++++-------
 1 file changed, 8 insertions(+), 7 deletions(-)
block/blk-merge.c +8 -7
index 14ce19607cd8a..b3da43160032f 100644
@@ -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;
 
-	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 min(blk_max_size_offset(q, offset, 0),
-			blk_queue_get_max_sectors(q, req_op(rq)));
+	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)
+		max_sectors = min(max_sectors,
+			chunk_size_left(offset, q->limits.chunk_sectors));
+	return max_sectors;
 }
 
 static inline int ll_new_hw_segment(struct request *req, struct bio *bio,
Signature · 6 lines
2.30.2

--
dm-devel mailing list
[email protected]
https://listman.redhat.com/mailman/listinfo/dm-devel
#5 lore raw

PATCH 4/5 block: fold blk_max_size_offset into get_max_io_size

In reply to Christoph Hellwig

Fold blk_max_size_offset into the only remaining user.

---
 block/blk-merge.c      | 14 +++++++++-----
 include/linux/blkdev.h | 19 -------------------
 2 files changed, 9 insertions(+), 24 deletions(-)
block/blk-merge.c +9 -5
index b3da43160032f..15b2aef1507e5 100644
@@ -177,14 +177,18 @@ 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;
 
-	max_sectors += start_offset;
-	max_sectors &= ~(pbs - 1);
+	if (q->limits.chunk_sectors)
+		sectors = min(chunk_size_left(sector, q->limits.chunk_sectors),
+			      sectors);
+
+	max_sectors = (sectors + start_offset) & ~(pbs - 1);
 	if (max_sectors > start_offset)
 		return max_sectors - start_offset;
 
include/linux/blkdev.h +0 -19
index 0b020bb45a3e7..c2fa4666f25e3 100644
@@ -634,25 +634,6 @@ static inline unsigned int chunk_size_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,
-			chunk_size_left(offset, chunk_sectors));
-}
-
 /*
  * Access functions for manipulating queue properties
  */
Signature · 1 line
2.30.2
#6 lore raw

PATCH 5/5 block: pass the start sector to get_max_io_size

In reply to Christoph Hellwig

Pass the start sector instead of the whole bio.

---
 block/blk-merge.c | 6 ++----
 1 file changed, 2 insertions(+), 4 deletions(-)
block/blk-merge.c +2 -4
index 15b2aef1507e5..b397972b526a0 100644
@@ -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;
@@ -287,7 +285,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) {
Signature · 1 line
2.30.2