Skip to content

Commit a22c4d7

Browse files
Ming Linaxboe
authored andcommitted
block: re-add discard_granularity and alignment checks
In commit b49a087("block: remove split code in blkdev_issue_{discard,write_same}"), discard_granularity and alignment checks were removed. Ideally, with bio late splitting, the upper layers shouldn't need to depend on device's limits. Christoph reported a discard regression on the HGST Ultrastar SN100 NVMe device when mkfs.xfs. We have not found the root cause yet. This patch re-adds discard_granularity and alignment checks by reverting the related changes in commit b49a087. The good thing is now we can remove the 2G discard size cap and just use UINT_MAX to avoid bi_size overflow. Reviewed-by: Christoph Hellwig <hch@lst.de> Tested-by: Christoph Hellwig <hch@lst.de> Signed-off-by: Ming Lin <ming.l@ssi.samsung.com> Reviewed-by: Mike Snitzer <snitzer@redhat.com> Signed-off-by: Jens Axboe <axboe@fb.com>
1 parent 23d8827 commit a22c4d7

File tree

1 file changed

+22
-9
lines changed

1 file changed

+22
-9
lines changed

block/blk-lib.c

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,6 @@ static void bio_batch_end_io(struct bio *bio)
2626
bio_put(bio);
2727
}
2828

29-
/*
30-
* Ensure that max discard sectors doesn't overflow bi_size and hopefully
31-
* it is of the proper granularity as long as the granularity is a power
32-
* of two.
33-
*/
34-
#define MAX_BIO_SECTORS ((1U << 31) >> 9)
35-
3629
/**
3730
* blkdev_issue_discard - queue a discard
3831
* @bdev: blockdev to issue discard for
@@ -50,6 +43,8 @@ int blkdev_issue_discard(struct block_device *bdev, sector_t sector,
5043
DECLARE_COMPLETION_ONSTACK(wait);
5144
struct request_queue *q = bdev_get_queue(bdev);
5245
int type = REQ_WRITE | REQ_DISCARD;
46+
unsigned int granularity;
47+
int alignment;
5348
struct bio_batch bb;
5449
struct bio *bio;
5550
int ret = 0;
@@ -61,6 +56,10 @@ int blkdev_issue_discard(struct block_device *bdev, sector_t sector,
6156
if (!blk_queue_discard(q))
6257
return -EOPNOTSUPP;
6358

59+
/* Zero-sector (unknown) and one-sector granularities are the same. */
60+
granularity = max(q->limits.discard_granularity >> 9, 1U);
61+
alignment = (bdev_discard_alignment(bdev) >> 9) % granularity;
62+
6463
if (flags & BLKDEV_DISCARD_SECURE) {
6564
if (!blk_queue_secdiscard(q))
6665
return -EOPNOTSUPP;
@@ -74,16 +73,30 @@ int blkdev_issue_discard(struct block_device *bdev, sector_t sector,
7473
blk_start_plug(&plug);
7574
while (nr_sects) {
7675
unsigned int req_sects;
77-
sector_t end_sect;
76+
sector_t end_sect, tmp;
7877

7978
bio = bio_alloc(gfp_mask, 1);
8079
if (!bio) {
8180
ret = -ENOMEM;
8281
break;
8382
}
8483

85-
req_sects = min_t(sector_t, nr_sects, MAX_BIO_SECTORS);
84+
/* Make sure bi_size doesn't overflow */
85+
req_sects = min_t(sector_t, nr_sects, UINT_MAX >> 9);
86+
87+
/*
88+
* If splitting a request, and the next starting sector would be
89+
* misaligned, stop the discard at the previous aligned sector.
90+
*/
8691
end_sect = sector + req_sects;
92+
tmp = end_sect;
93+
if (req_sects < nr_sects &&
94+
sector_div(tmp, granularity) != alignment) {
95+
end_sect = end_sect - alignment;
96+
sector_div(end_sect, granularity);
97+
end_sect = end_sect * granularity + alignment;
98+
req_sects = end_sect - sector;
99+
}
87100

88101
bio->bi_iter.bi_sector = sector;
89102
bio->bi_end_io = bio_batch_end_io;

0 commit comments

Comments
 (0)