Skip to content

Commit 14657ef

Browse files
Ming Leigregkh
authored andcommitted
block: make sure discard bio is aligned with logical block size
commit 1adfc5e upstream. 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: [email protected] Cc: Mike Snitzer <[email protected]> Cc: Christoph Hellwig <[email protected]> Cc: Xiao Ni <[email protected]> Cc: Mariusz Dabrowski <[email protected]> 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 <[email protected]> Tested-by: Rui Salvaterra <[email protected]> Signed-off-by: Ming Lei <[email protected]> Signed-off-by: Jens Axboe <[email protected]> Signed-off-by: Greg Kroah-Hartman <[email protected]>
1 parent cf8d097 commit 14657ef

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
@@ -58,8 +58,7 @@ int __blkdev_issue_discard(struct block_device *bdev, sector_t sector,
5858

5959
if (!req_sects)
6060
goto fail;
61-
if (req_sects > UINT_MAX >> 9)
62-
req_sects = UINT_MAX >> 9;
61+
req_sects = min(req_sects, bio_allowed_max_sectors(q));
6362

6463
end_sect = sector + req_sects;
6564

block/blk-merge.c

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

30-
max_discard_sectors = min(q->limits.max_discard_sectors, UINT_MAX >> 9);
30+
max_discard_sectors = min(q->limits.max_discard_sectors,
31+
bio_allowed_max_sectors(q));
3132
max_discard_sectors -= max_discard_sectors % granularity;
3233

3334
if (unlikely(!max_discard_sectors)) {

block/blk.h

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

331+
/*
332+
* The max size one bio can handle is UINT_MAX becasue bvec_iter.bi_size
333+
* is defined as 'unsigned int', meantime it has to aligned to with logical
334+
* block size which is the minimum accepted unit by hardware.
335+
*/
336+
static inline unsigned int bio_allowed_max_sectors(struct request_queue *q)
337+
{
338+
return round_down(UINT_MAX, queue_logical_block_size(q)) >> 9;
339+
}
340+
331341
/*
332342
* Internal io_context interface
333343
*/

0 commit comments

Comments
 (0)