Add a helper to size the maximum secure erase bio without exceeding the queue limit or overlowing the bi_size member. Signed-off-by: Christoph Hellwig <hch@xxxxxx> --- block/blk-lib.c | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/block/blk-lib.c b/block/blk-lib.c index 4aabfc4a7eaa20..ff12c3d2de5aae 100644 --- a/block/blk-lib.c +++ b/block/blk-lib.c @@ -285,20 +285,22 @@ int blkdev_issue_zeroout(struct block_device *bdev, sector_t sector, } EXPORT_SYMBOL(blkdev_issue_zeroout); +static sector_t bio_secure_erase_limit(struct block_device *bdev) +{ + sector_t bs_mask = (bdev_logical_block_size(bdev) >> 9) - 1; + + return min(bdev_max_secure_erase_sectors(bdev), + (UINT_MAX >> SECTOR_SHIFT) & ~bs_mask); +} + int blkdev_issue_secure_erase(struct block_device *bdev, sector_t sector, sector_t nr_sects, gfp_t gfp) { - sector_t bs_mask = (bdev_logical_block_size(bdev) >> 9) - 1; - unsigned int max_sectors = bdev_max_secure_erase_sectors(bdev); + unsigned int max_sectors = bio_secure_erase_limit(bdev); struct bio *bio = NULL; struct blk_plug plug; int ret = 0; - /* make sure that "len << SECTOR_SHIFT" doesn't overflow */ - if (max_sectors > UINT_MAX >> SECTOR_SHIFT) - max_sectors = UINT_MAX >> SECTOR_SHIFT; - max_sectors &= ~bs_mask; - blk_start_plug(&plug); while (nr_sects) { unsigned int len = min_t(sector_t, nr_sects, max_sectors); -- 2.43.0