aboutsummaryrefslogtreecommitdiffstats
path: root/block/blk-lib.c
diff options
context:
space:
mode:
authorDamien Le Moal <damien.lemoal@wdc.com>2017-07-06 07:21:15 -0400
committerJens Axboe <axboe@kernel.dk>2017-07-06 11:43:20 -0400
commit615d22a51c04856efe62af6e1d5b450aaf5cc2c0 (patch)
tree2d80a79b5a229c70969784fe3b1408e32b32679c /block/blk-lib.c
parent8a05aa4ce3bf767b7bd72851da89c23d084d1ecb (diff)
block: Fix __blkdev_issue_zeroout loop
The BIO issuing loop in __blkdev_issue_zeroout() is allocating BIOs with a maximum number of bvec (pages) equal to min(nr_sects, (sector_t)BIO_MAX_PAGES) This works since the requested number of bvecs will always be limited to the absolute maximum number supported (BIO_MAX_PAGES), but this is ineficient as too many bvec entries may be requested due to the different units being used in the min() operation (number of sectors vs number of pages). To fix this, introduce the helper __blkdev_sectors_to_bio_pages() to correctly calculate the number of bvecs for zeroout BIOs as the issuing loop progresses. The calculation is done using consistent units and makes sure that the number of pages return is at least 1 (for cases where the number of sectors is less that the number of sectors in a page). Also remove a trailing space after the bit shift in the internal loop min() call. Signed-off-by: Damien Le Moal <damien.lemoal@wdc.com> Reviewed-by: Christoph Hellwig <hch@lst.de> Signed-off-by: Jens Axboe <axboe@kernel.dk>
Diffstat (limited to 'block/blk-lib.c')
-rw-r--r--block/blk-lib.c23
1 files changed, 18 insertions, 5 deletions
diff --git a/block/blk-lib.c b/block/blk-lib.c
index e8caecd71688..3fe0aec90597 100644
--- a/block/blk-lib.c
+++ b/block/blk-lib.c
@@ -261,6 +261,19 @@ static int __blkdev_issue_write_zeroes(struct block_device *bdev,
261 return 0; 261 return 0;
262} 262}
263 263
264/*
265 * Convert a number of 512B sectors to a number of pages.
266 * The result is limited to a number of pages that can fit into a BIO.
267 * Also make sure that the result is always at least 1 (page) for the cases
268 * where nr_sects is lower than the number of sectors in a page.
269 */
270static unsigned int __blkdev_sectors_to_bio_pages(sector_t nr_sects)
271{
272 sector_t bytes = (nr_sects << 9) + PAGE_SIZE - 1;
273
274 return min(bytes >> PAGE_SHIFT, (sector_t)BIO_MAX_PAGES);
275}
276
264/** 277/**
265 * __blkdev_issue_zeroout - generate number of zero filed write bios 278 * __blkdev_issue_zeroout - generate number of zero filed write bios
266 * @bdev: blockdev to issue 279 * @bdev: blockdev to issue
@@ -307,18 +320,18 @@ int __blkdev_issue_zeroout(struct block_device *bdev, sector_t sector,
307 320
308 ret = 0; 321 ret = 0;
309 while (nr_sects != 0) { 322 while (nr_sects != 0) {
310 bio = next_bio(bio, min(nr_sects, (sector_t)BIO_MAX_PAGES), 323 bio = next_bio(bio, __blkdev_sectors_to_bio_pages(nr_sects),
311 gfp_mask); 324 gfp_mask);
312 bio->bi_iter.bi_sector = sector; 325 bio->bi_iter.bi_sector = sector;
313 bio->bi_bdev = bdev; 326 bio->bi_bdev = bdev;
314 bio_set_op_attrs(bio, REQ_OP_WRITE, 0); 327 bio_set_op_attrs(bio, REQ_OP_WRITE, 0);
315 328
316 while (nr_sects != 0) { 329 while (nr_sects != 0) {
317 sz = min((sector_t) PAGE_SIZE >> 9 , nr_sects); 330 sz = min((sector_t) PAGE_SIZE, nr_sects << 9);
318 bi_size = bio_add_page(bio, ZERO_PAGE(0), sz << 9, 0); 331 bi_size = bio_add_page(bio, ZERO_PAGE(0), sz, 0);
319 nr_sects -= bi_size >> 9; 332 nr_sects -= bi_size >> 9;
320 sector += bi_size >> 9; 333 sector += bi_size >> 9;
321 if (bi_size < (sz << 9)) 334 if (bi_size < sz)
322 break; 335 break;
323 } 336 }
324 cond_resched(); 337 cond_resched();