Skip to contentSkip to conversation

clean up the chunk_sizehandling helpers a little

PATCH In review Christoph Hellwig 6 participants 20 messages Jun 14, 2022 – Jun 17, 2022

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

Hi Jens,

this series cleans up a bunch of block layer helpers related to the chunk size.

Diffstat:

 block/blk-merge.c      |   28 ++++++++++++++++------------
 block/blk.h            |   13 +++++++++++++
 drivers/md/dm.c        |   17 ++++++-----------
 include/linux/blkdev.h |   39 +++++++--------------------------------
 4 files changed, 42 insertions(+), 55 deletions(-)
#1 lore raw

PATCH 1/6 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 | 19 +++++++++++++------
 1 file changed, 13 insertions(+), 6 deletions(-)
include/linux/blkdev.h +13 -6
index 914c613d81da7..e66ad451274ec 100644
@@ -933,6 +933,17 @@ static inline unsigned int blk_queue_get_max_sectors(struct request_queue *q,
 	return q->limits.max_sectors;
 }
 
+/*
+ * Return how much of the chunk is left to be used for I/O at a 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.
@@ -948,12 +959,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));
 }
 
 /*
Signature · 1 line
2.30.2
Reviewed-by on v1 1/6
#2 lore raw

Re: [PATCH 1/6] block: factor out a chunk_size_left helper

Quoted 2 lines · Christoph Hellwig

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

#4 lore raw

Re: [PATCH 1/6] block: factor out a chunk_size_left helper

In reply to Christoph Hellwig
Quoted 33 lines · Christoph Hellwig

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

---
 include/linux/blkdev.h | 19 +++++++++++++------
 1 file changed, 13 insertions(+), 6 deletions(-)
include/linux/blkdev.h +11 -0
index 914c613d81da7..e66ad451274ec 100644
@@ -933,6 +933,17 @@ static inline unsigned int blk_queue_get_max_sectors(struct request_queue *q,
 	return q->limits.max_sectors;
 }
 
+/*
+ * Return how much of the chunk is left to be used for I/O at a 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.
@@ -948,12 +959,8 @@ static inline unsigned int blk_max_size_offset(struct request_queue *q,
 			return q->limits.max_sectors;
 	}

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

Quoted 10 lines
include/linux/blkdev.h +2 -6
 
-	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));
 }

While you're at it, any reason not to use queue_max_sectors() here?

#5 lore raw

Re: [PATCH 1/6] block: factor out a chunk_size_left helper

Quoted 5 lines · Mike Snitzer

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

Quoted 3 lines
include/linux/blkdev.h +2 -0
+	return min(q->limits.max_sectors,
+			blk_chunk_sectors_left(offset, chunk_sectors));
 }

While you're at it, any reason not to use queue_max_sectors() here?

I'm not sure if it is a good reason, but this code sniplet goes away later in the series, and all replacemens do use queue_max_sectors.

#6 lore raw

PATCH 2/6 dm: open code blk_max_size_offset in max_io_len

In reply to Christoph Hellwig

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.

---
 drivers/md/dm.c | 17 ++++++-----------
 1 file changed, 6 insertions(+), 11 deletions(-)
drivers/md/dm.c +6 -11
index d8f16183bf27c..0514358a1f8e5 100644
@@ -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)
Signature · 1 line
2.30.2
maintainer Reviewed-by on v1 2/6
#7 lore raw

Re: [PATCH 2/6] dm: open code blk_max_size_offset in max_io_len

Quoted 46 lines · Christoph Hellwig

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.

---
 drivers/md/dm.c | 17 ++++++-----------
 1 file changed, 6 insertions(+), 11 deletions(-)
drivers/md/dm.c +6 -11
index d8f16183bf27c..0514358a1f8e5 100644
@@ -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)
Signature · 1 line
2.30.2

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

#8 lore raw

PATCH 3/6 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 | 9 +++++----
 1 file changed, 5 insertions(+), 4 deletions(-)
block/blk-merge.c +5 -4
index db2e03c8af7f4..df003ecfbd474 100644
@@ -566,17 +566,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 min(blk_max_size_offset(q, offset, 0),
-			blk_queue_get_max_sectors(q, req_op(rq)));
+		return max_sectors;
+	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,
Signature · 1 line
2.30.2
#9 lore raw

Re: [PATCH 3/6] block: open code blk_max_size_offset in blk_rq_get_max_sectors

Quoted 25 lines · 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 | 9 +++++----
  1 file changed, 5 insertions(+), 4 deletions(-)
block/blk-merge.c +2 -0
index db2e03c8af7f4..df003ecfbd474 100644
@@ -566,17 +566,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 ||

block/blk-merge.c static inline unsigned int blk_rq_get_max_sectors(struct request *rq, · line 581

Quoted 10 lines
block/blk-merge.c +3 -4
  	    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)));
+		return max_sectors;
+	return min(max_sectors,
+		   blk_chunk_sectors_left(offset, q->limits.chunk_sectors));
  }

blk_set_default_limits() initializes chunk_sectors to zero and blk_chunk_sectors_left() triggers a division by zero if a zero is passed as the second argument. What am I missing?

Thanks,

Bart.

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

Reviewed-by
#10 lore raw

Re: [PATCH 3/6] block: open code blk_max_size_offset in blk_rq_get_max_sectors

Quoted 29 lines · Bart Van Assche
Quoted 28 lines · 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 | 9 +++++----

  1 file changed, 5 insertions(+), 4 deletions(-)
block/blk-merge.c +2 -0
index db2e03c8af7f4..df003ecfbd474 100644
@@ -566,17 +566,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)); -

Quoted 8 lines
Quoted 6 lines

-    return min(blk_max_size_offset(q, offset, 0),
-            blk_queue_get_max_sectors(q, req_op(rq)));
+        return max_sectors;
+    return min(max_sectors,
+           blk_chunk_sectors_left(offset, q->limits.chunk_sectors)); }

blk_set_default_limits() initializes chunk_sectors to zero and blk_chunk_sectors_left() triggers a division by zero if a zero is passed as the second argument. What am I missing?

Answering my own question: I overlooked one of the return statements.

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

#11 lore raw

PATCH 4/6 block: cleanup variable naming in get_max_io_size

In reply to Christoph Hellwig

get_max_io_size has a very odd choice of variables names and initialization patterns. Switch to more descriptive names and more clear initialization of them.

---
 block/blk-merge.c | 18 ++++++++----------
 1 file changed, 8 insertions(+), 10 deletions(-)
block/blk-merge.c +8 -10
index df003ecfbd474..4da981efddeed 100644
@@ -164,18 +164,16 @@ static struct bio *blk_bio_write_zeroes_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);
-
-	max_sectors += start_offset;
-	max_sectors &= ~(pbs - 1);
-	if (max_sectors > start_offset)
-		return max_sectors - start_offset;
-
-	return sectors & ~(lbs - 1);
+	unsigned max_sectors, start, end;
+
+	max_sectors = blk_max_size_offset(q, bio->bi_iter.bi_sector, 0);
+	start = bio->bi_iter.bi_sector & (pbs - 1);
+	end = (start + max_sectors) & ~(pbs - 1);
+	if (end > start)
+		return end - start;
+	return max_sectors & ~(lbs - 1);
 }
 
 static inline unsigned get_max_segment_size(const struct request_queue *q,
Signature · 1 line
2.30.2
#12 lore raw

Re: [PATCH 4/6] block: cleanup variable naming in get_max_io_size

Quoted 3 lines · Christoph Hellwig

get_max_io_size has a very odd choice of variables names and initialization patterns. Switch to more descriptive names and more clear initialization of them.

Hmm ... what is so odd about the variable names? I have done my best to chose clear and descriptive names when I introduced these names.

Bart.

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

#13 lore raw

Re: [PATCH 4/6] block: cleanup variable naming in get_max_io_size

Quoted 7 lines · Bart Van Assche
Quoted 3 lines · Christoph Hellwig

get_max_io_size has a very odd choice of variables names and initialization patterns. Switch to more descriptive names and more clear initialization of them.

Hmm ... what is so odd about the variable names? I have done my best to chose clear and descriptive names when I introduced these names.

Major confusion:

- we have a local variable to hold the queue max sectors value, but it is callled sectors
- while we have a variable the holds the rounded end of the I/O range that is called max_sectors

Minor confusion:

- a variable that hold that rounded start of the range has a random _offset suffix despite not really being an offset

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

#14 lore raw

PATCH 5/6 block: fold blk_max_size_offset into get_max_io_size

In reply to Christoph Hellwig

Now that blk_max_size_offset has a single caller left, fold it into that and clean up the naming convention for the local variables there.

---
 block/blk-merge.c      |  9 +++++++--
 include/linux/blkdev.h | 19 -------------------
 2 files changed, 7 insertions(+), 21 deletions(-)
block/blk-merge.c +7 -2
index 4da981efddeed..0f5f42ebd0bb0 100644
@@ -166,9 +166,14 @@ static inline unsigned get_max_io_size(struct request_queue *q,
 {
 	unsigned pbs = queue_physical_block_size(q) >> SECTOR_SHIFT;
 	unsigned lbs = queue_logical_block_size(q) >> SECTOR_SHIFT;
-	unsigned max_sectors, start, end;
+	unsigned max_sectors = queue_max_sectors(q), start, end;
+
+	if (q->limits.chunk_sectors) {
+		max_sectors = min(max_sectors,
+			blk_chunk_sectors_left(bio->bi_iter.bi_sector,
+					       q->limits.chunk_sectors));
+	}
 
-	max_sectors = blk_max_size_offset(q, bio->bi_iter.bi_sector, 0);
 	start = bio->bi_iter.bi_sector & (pbs - 1);
 	end = (start + max_sectors) & ~(pbs - 1);
 	if (end > start)
include/linux/blkdev.h +0 -19
index e66ad451274ec..05e60ee269d91 100644
@@ -944,25 +944,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
  */
Signature · 1 line
2.30.2
Reviewed-by on v1 5/6
#16 lore raw

Re: [PATCH 5/6] block: fold blk_max_size_offset into get_max_io_size

In reply to Christoph Hellwig
Quoted 8 lines · Christoph Hellwig

Now that blk_max_size_offset has a single caller left, fold it into that and clean up the naming convention for the local variables there.

---
 block/blk-merge.c      |  9 +++++++--
 include/linux/blkdev.h | 19 -------------------
 2 files changed, 7 insertions(+), 21 deletions(-)

Looks good,

#17 lore raw

PATCH 6/6 block: move blk_queue_get_max_sectors to blk.h

In reply to Christoph Hellwig

blk_queue_get_max_sectors is private to the block layer, so move it out of blkdev.h.

---
 block/blk.h            | 13 +++++++++++++
 include/linux/blkdev.h | 13 -------------
 2 files changed, 13 insertions(+), 13 deletions(-)
block/blk.h +13 -0
index 434017701403f..8e79296ee97a2 100644
@@ -159,6 +159,19 @@ static inline bool blk_discard_mergable(struct request *req)
 	return false;
 }
 
+static inline unsigned int blk_queue_get_max_sectors(struct request_queue *q,
+						     int op)
+{
+	if (unlikely(op == REQ_OP_DISCARD || op == REQ_OP_SECURE_ERASE))
+		return min(q->limits.max_discard_sectors,
+			   UINT_MAX >> SECTOR_SHIFT);
+
+	if (unlikely(op == REQ_OP_WRITE_ZEROES))
+		return q->limits.max_write_zeroes_sectors;
+
+	return q->limits.max_sectors;
+}
+
 #ifdef CONFIG_BLK_DEV_INTEGRITY
 void blk_flush_integrity(void);
 bool __bio_integrity_endio(struct bio *);
include/linux/blkdev.h +0 -13
index 05e60ee269d91..5ef2f061feb08 100644
@@ -920,19 +920,6 @@ static inline unsigned int bio_zone_is_seq(struct bio *bio)
 }
 #endif /* CONFIG_BLK_DEV_ZONED */
 
-static inline unsigned int blk_queue_get_max_sectors(struct request_queue *q,
-						     int op)
-{
-	if (unlikely(op == REQ_OP_DISCARD || op == REQ_OP_SECURE_ERASE))
-		return min(q->limits.max_discard_sectors,
-			   UINT_MAX >> SECTOR_SHIFT);
-
-	if (unlikely(op == REQ_OP_WRITE_ZEROES))
-		return q->limits.max_write_zeroes_sectors;
-
-	return q->limits.max_sectors;
-}
-
 /*
  * Return how much of the chunk is left to be used for I/O at a given offset.
  */
Signature · 1 line
2.30.2
maintainer Applied
#19 lore raw
In reply to Christoph Hellwig
Quoted 11 lines · Christoph Hellwig

this series cleans up a bunch of block layer helpers related to the chunk size.

Diffstat:

 block/blk-merge.c      |   28 ++++++++++++++++------------
 block/blk.h            |   13 +++++++++++++
 drivers/md/dm.c        |   17 ++++++-----------
 include/linux/blkdev.h |   39 +++++++--------------------------------
 4 files changed, 42 insertions(+), 55 deletions(-)

[...]

Applied, thanks!

[1/6] block: factor out a chunk_size_left helper

      commit: d0e3310bb972f65c4b614c29f8022f57a52123c8
[2/6] dm: open code blk_max_size_offset in max_io_len
      commit: 6d5661a5d0e513dde5d49820315c5d6249a5c732
[3/6] block: open code blk_max_size_offset in blk_rq_get_max_sectors
      commit: 92ac28684e7eccf968b556893ca09c57d1fb3cdd
[4/6] block: cleanup variable naming in get_max_io_size
      commit: 08fdba80df1fd78a22b00e96ffd062a5bbaf8d8e
[5/6] block: fold blk_max_size_offset into get_max_io_size
      commit: d8f1d38c87b87ea3a0a0c58b6386333731e29470
[6/6] block: move blk_queue_get_max_sectors to blk.h
      commit: d8fca63495fb21e9b2dfcf722346aa844459139a

Best regards,

Signature · 1 line
Jens Axboe