Skip to contentSkip to conversation

block: make sure discard/writesame bio is aligned with logical block size

PATCH In review Ming Lei 3 participants 8 messages Oct 29, 2018 – Nov 9, 2018

Subsystems (2)
lore
Share & tools
lore

Ming Lei

author
#0 lore raw

v2 cover block: make sure discard/writesame bio is aligned with logical block size

Hi,

The 1st & 3rd patch fixes bio size alignment issue.

The 2nd patch cleans up __blkdev_issue_discard() a bit.

V2:
- introduce helper of bio_allowed_max_sectors()
- add commit log for patch 2

Ming Lei (3):
block: make sure discard bio is aligned with logical block size
block: cleanup __blkdev_issue_discard()
block: make sure writesame bio is aligned with logical block size

 block/blk-lib.c   | 26 +++++++-------------------
 block/blk-merge.c |  3 ++-
 block/blk.h       | 10 ++++++++++
 3 files changed, 19 insertions(+), 20 deletions(-)
Cc Rui Salvaterra <[email protected]>
Cc Mike Snitzer <[email protected]>
Cc Christoph Hellwig <[email protected]>
Cc Xiao Ni <[email protected]>
Cc Mariusz Dabrowski <[email protected]>
Signature · 1 line
2.9.5
author
#1 lore raw

PATCH v2 1/3 block: make sure discard bio is aligned with logical block size

Obviously the created discard bio has to be aligned with logical block size.

This patch introduces the helper of bio_allowed_max_sectors() for this purpose.

Fixes 744889b7cbb56a6 ("block: don't deal with discard limit in blkdev_issue_discard()")
Fixes a22c4d7e34402cc ("block: re-add discard_granularity and alignment checks")
Cc Rui Salvaterra <[email protected]>
Cc Mike Snitzer <[email protected]>
Cc Christoph Hellwig <[email protected]>
Cc Xiao Ni <[email protected]>
Cc Mariusz Dabrowski <[email protected]>
---
 block/blk-lib.c   |  3 +--
 block/blk-merge.c |  3 ++-
 block/blk.h       | 10 ++++++++++
 3 files changed, 13 insertions(+), 3 deletions(-)
block/blk-lib.c +1 -2
index 76f867ea9a9b..d56fd159d2e8 100644
@@ -57,8 +57,7 @@ int __blkdev_issue_discard(struct block_device *bdev, sector_t sector,
 
 		if (!req_sects)
 			goto fail;
-		if (req_sects > UINT_MAX >> 9)
-			req_sects = UINT_MAX >> 9;
+		req_sects = min(req_sects, bio_allowed_max_sectors(q));
 
 		end_sect = sector + req_sects;
 
block/blk-merge.c +2 -1
index 42a46744c11b..507fbaa6b4c0 100644
@@ -90,7 +90,8 @@ static struct bio *blk_bio_discard_split(struct request_queue *q,
 	/* Zero-sector (unknown) and one-sector granularities are the same.  */
 	granularity = max(q->limits.discard_granularity >> 9, 1U);
 
-	max_discard_sectors = min(q->limits.max_discard_sectors, UINT_MAX >> 9);
+	max_discard_sectors = min(q->limits.max_discard_sectors,
+			bio_allowed_max_sectors(q));
 	max_discard_sectors -= max_discard_sectors % granularity;
 
 	if (unlikely(!max_discard_sectors)) {
block/blk.h +10 -0
index a1841b8ff129..2680aa4fcd88 100644
@@ -396,6 +396,16 @@ static inline unsigned long blk_rq_deadline(struct request *rq)
 }
 
 /*
+ * The max size one bio can handle is UINT_MAX becasue bvec_iter.bi_size
+ * is defined as 'unsigned int', meantime it has to aligned to with logical
+ * block size which is the minimum accepted unit by hardware.
+ */
+static inline unsigned int bio_allowed_max_sectors(struct request_queue *q)
+{
+	return round_down(UINT_MAX, queue_logical_block_size(q)) >> 9;
+}
+
+/*
  * Internal io_context interface
  */
 void get_io_context(struct io_context *ioc);
Signature · 1 line
2.9.5
author
#2 lore raw

PATCH v2 2/3 block: cleanup __blkdev_issue_discard()

In reply to Ming Lei

Cleanup __blkdev_issue_discard() a bit:

- remove local variable of 'end_sect'
- remove code block of 'fail'

Cc Rui Salvaterra <[email protected]>
Cc Mike Snitzer <[email protected]>
Cc Christoph Hellwig <[email protected]>
Cc Xiao Ni <[email protected]>
Cc Mariusz Dabrowski <[email protected]>
---
 block/blk-lib.c | 23 ++++++-----------------
 1 file changed, 6 insertions(+), 17 deletions(-)
block/blk-lib.c +6 -17
index d56fd159d2e8..d58d5d87dd88 100644
@@ -51,15 +51,12 @@ int __blkdev_issue_discard(struct block_device *bdev, sector_t sector,
 	if ((sector | nr_sects) & bs_mask)
 		return -EINVAL;
 
-	while (nr_sects) {
-		unsigned int req_sects = nr_sects;
-		sector_t end_sect;
-
-		if (!req_sects)
-			goto fail;
-		req_sects = min(req_sects, bio_allowed_max_sectors(q));
+	if (!nr_sects)
+		return -EINVAL;
 
-		end_sect = sector + req_sects;
+	while (nr_sects) {
+		unsigned int req_sects = min_t(unsigned int, nr_sects,
+				bio_allowed_max_sectors(q));
 
 		bio = blk_next_bio(bio, 0, gfp_mask);
 		bio->bi_iter.bi_sector = sector;
@@ -67,8 +64,8 @@ int __blkdev_issue_discard(struct block_device *bdev, sector_t sector,
 		bio_set_op_attrs(bio, op, 0);
 
 		bio->bi_iter.bi_size = req_sects << 9;
+		sector += req_sects;
 		nr_sects -= req_sects;
-		sector = end_sect;
 
 		/*
 		 * We can loop for a long time in here, if someone does
@@ -81,14 +78,6 @@ int __blkdev_issue_discard(struct block_device *bdev, sector_t sector,
 
 	*biop = bio;
 	return 0;
-
-fail:
-	if (bio) {
-		submit_bio_wait(bio);
-		bio_put(bio);
-	}
-	*biop = NULL;
-	return -EOPNOTSUPP;
 }
 EXPORT_SYMBOL(__blkdev_issue_discard);
 
Signature · 1 line
2.9.5
author
#3 lore raw

PATCH v2 3/3 block: make sure writesame bio is aligned with logical block size

In reply to Ming Lei

Obviously the created writesame bio has to be aligned with logical block size, and use bio_allowed_max_sectors() to retrieve this number.

Fixes b49a0871be31a745b2ef ("block: remove split code in blkdev_issue_{discard,write_same}")
Cc Rui Salvaterra <[email protected]>
Cc Mike Snitzer <[email protected]>
Cc Christoph Hellwig <[email protected]>
Cc Xiao Ni <[email protected]>
Cc Mariusz Dabrowski <[email protected]>
---
 block/blk-lib.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
block/blk-lib.c +1 -1
index d58d5d87dd88..e8b3bb9bf375 100644
@@ -149,7 +149,7 @@ static int __blkdev_issue_write_same(struct block_device *bdev, sector_t sector,
 		return -EOPNOTSUPP;
 
 	/* Ensure that max_write_same_sectors doesn't overflow bi_size */
-	max_write_same_sectors = UINT_MAX >> 9;
+	max_write_same_sectors = bio_allowed_max_sectors(q);
 
 	while (nr_sects) {
 		bio = blk_next_bio(bio, 1, gfp_mask);
Signature · 1 line
2.9.5
author
#4 lore raw
In reply to Ming Lei
Quoted 21 lines · Ming Lei

Hi,

The 1st & 3rd patch fixes bio size alignment issue.

The 2nd patch cleans up __blkdev_issue_discard() a bit.

V2:
- introduce helper of bio_allowed_max_sectors()
- add commit log for patch 2

Ming Lei (3):
block: make sure discard bio is aligned with logical block size
block: cleanup __blkdev_issue_discard()
block: make sure writesame bio is aligned with logical block size

 block/blk-lib.c   | 26 +++++++-------------------
 block/blk-merge.c |  3 ++-
 block/blk.h       | 10 ++++++++++
 3 files changed, 19 insertions(+), 20 deletions(-)
Quoted 6 lines
Cc Rui Salvaterra <[email protected]>
Cc Mike Snitzer <[email protected]>
Cc Christoph Hellwig <[email protected]>
Cc Xiao Ni <[email protected]>
Cc Mariusz Dabrowski <[email protected]>

V2 addresses Christoph's comment by introducing bio_allowed_max_sectors().

Ping...

Thanks,
Ming

Tested-by
#5 lore raw
Quoted 7 lines · Ming Lei

V2 addresses Christoph's comment by introducing bio_allowed_max_sectors().

Ping...

Thanks,
Ming

Hi, Ming,

Sorry for the delay. I tested your V2 against Linux 4.20-rc1 and everything seems fine. FWIW, V2 is also

Thanks again,

Rui

author
#6 lore raw
Quoted 9 lines · Rui Salvaterra
Quoted 7 lines · Ming Lei

V2 addresses Christoph's comment by introducing bio_allowed_max_sectors().

Ping...

Thanks,
Ming

Quoted 6 lines

Hi, Ming,

Sorry for the delay. I tested your V2 against Linux 4.20-rc1 and everything seems fine. FWIW, V2 is also

Rui, thanks for your test on V2.

Jens, what do you think about this patchset?

Thanks,
Ming

maintainer
#7 lore raw
Quoted 12 lines · Ming Lei
Quoted 11 lines · Rui Salvaterra
Quoted 7 lines · Ming Lei

V2 addresses Christoph's comment by introducing bio_allowed_max_sectors().

Ping...

Thanks,
Ming

Hi, Ming,

Quoted 8 lines
Quoted 4 lines

Sorry for the delay. I tested your V2 against Linux 4.20-rc1 and everything seems fine. FWIW, V2 is also

Rui, thanks for your test on V2.

Jens, what do you think about this patchset?

I waffled on this a bit... I generally don't like cleanup patches for fixes that are going into the current series. Those kinds of patches should be as short and sweet as possible, and cleanup part usually ends up being the problematic one, ironically enough. But it looks fine to me, so I've put it in.

Signature · 1 line
Jens Axboe