Skip to content

Commit 1adfc5e

Browse files
Ming Leiaxboe
authored andcommitted
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. Cc: stable@vger.kernel.org Cc: Mike Snitzer <snitzer@redhat.com> Cc: Christoph Hellwig <hch@lst.de> Cc: Xiao Ni <xni@redhat.com> Cc: Mariusz Dabrowski <mariusz.dabrowski@intel.com> Fixes: 744889b ("block: don't deal with discard limit in blkdev_issue_discard()") Fixes: a22c4d7 ("block: re-add discard_granularity and alignment checks") Reported-by: Rui Salvaterra <rsalvaterra@gmail.com> Tested-by: Rui Salvaterra <rsalvaterra@gmail.com> Signed-off-by: Ming Lei <ming.lei@redhat.com> Signed-off-by: Jens Axboe <axboe@kernel.dk>
1 parent d39aa49 commit 1adfc5e

File tree

3 files changed

+13
-3
lines changed

3 files changed

+13
-3
lines changed

block/blk-lib.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,7 @@ int __blkdev_issue_discard(struct block_device *bdev, sector_t sector,
5757

5858
if (!req_sects)
5959
goto fail;
60-
if (req_sects > UINT_MAX >> 9)
61-
req_sects = UINT_MAX >> 9;
60+
req_sects = min(req_sects, bio_allowed_max_sectors(q));
6261

6362
end_sect = sector + req_sects;
6463

block/blk-merge.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,8 @@ static struct bio *blk_bio_discard_split(struct request_queue *q,
9090
/* Zero-sector (unknown) and one-sector granularities are the same. */
9191
granularity = max(q->limits.discard_granularity >> 9, 1U);
9292

93-
max_discard_sectors = min(q->limits.max_discard_sectors, UINT_MAX >> 9);
93+
max_discard_sectors = min(q->limits.max_discard_sectors,
94+
bio_allowed_max_sectors(q));
9495
max_discard_sectors -= max_discard_sectors % granularity;
9596

9697
if (unlikely(!max_discard_sectors)) {

block/blk.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -395,6 +395,16 @@ static inline unsigned long blk_rq_deadline(struct request *rq)
395395
return rq->__deadline & ~0x1UL;
396396
}
397397

398+
/*
399+
* The max size one bio can handle is UINT_MAX becasue bvec_iter.bi_size
400+
* is defined as 'unsigned int', meantime it has to aligned to with logical
401+
* block size which is the minimum accepted unit by hardware.
402+
*/
403+
static inline unsigned int bio_allowed_max_sectors(struct request_queue *q)
404+
{
405+
return round_down(UINT_MAX, queue_logical_block_size(q)) >> 9;
406+
}
407+
398408
/*
399409
* Internal io_context interface
400410
*/

0 commit comments

Comments
 (0)