Skip to content

Commit 5f7136d

Browse files
Matthew Wilcox (Oracle)axboe
authored andcommitted
block: Add bio_max_segs
It's often inconvenient to use BIO_MAX_PAGES due to min() requiring the sign to be the same. Introduce bio_max_segs() and change BIO_MAX_PAGES to be unsigned to make it easier for the users. Reviewed-by: Chaitanya Kulkarni <chaitanya.kulkarni@wdc.com> Signed-off-by: Matthew Wilcox (Oracle) <willy@infradead.org> Signed-off-by: Jens Axboe <axboe@kernel.dk>
1 parent 94d4bff commit 5f7136d

20 files changed

Lines changed: 44 additions & 52 deletions

File tree

block/blk-map.c

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -150,9 +150,7 @@ static int bio_copy_user_iov(struct request *rq, struct rq_map_data *map_data,
150150
bmd->is_our_pages = !map_data;
151151
bmd->is_null_mapped = (map_data && map_data->null_mapped);
152152

153-
nr_pages = DIV_ROUND_UP(offset + len, PAGE_SIZE);
154-
if (nr_pages > BIO_MAX_PAGES)
155-
nr_pages = BIO_MAX_PAGES;
153+
nr_pages = bio_max_segs(DIV_ROUND_UP(offset + len, PAGE_SIZE));
156154

157155
ret = -ENOMEM;
158156
bio = bio_kmalloc(gfp_mask, nr_pages);

drivers/block/xen-blkback/blkback.c

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1326,9 +1326,7 @@ static int dispatch_rw_block_io(struct xen_blkif_ring *ring,
13261326
pages[i]->page,
13271327
seg[i].nsec << 9,
13281328
seg[i].offset) == 0)) {
1329-
1330-
int nr_iovecs = min_t(int, (nseg-i), BIO_MAX_PAGES);
1331-
bio = bio_alloc(GFP_KERNEL, nr_iovecs);
1329+
bio = bio_alloc(GFP_KERNEL, bio_max_segs(nseg - i));
13321330
if (unlikely(bio == NULL))
13331331
goto fail_put_bio;
13341332

drivers/md/dm-io.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -341,8 +341,8 @@ static void do_region(int op, int op_flags, unsigned region,
341341
num_bvecs = 1;
342342
break;
343343
default:
344-
num_bvecs = min_t(int, BIO_MAX_PAGES,
345-
dm_sector_div_up(remaining, (PAGE_SIZE >> SECTOR_SHIFT)));
344+
num_bvecs = bio_max_segs(dm_sector_div_up(remaining,
345+
(PAGE_SIZE >> SECTOR_SHIFT)));
346346
}
347347

348348
bio = bio_alloc_bioset(GFP_NOIO, num_bvecs, &io->client->bios);

drivers/md/dm-log-writes.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -264,15 +264,14 @@ static int write_inline_data(struct log_writes_c *lc, void *entry,
264264
size_t entrylen, void *data, size_t datalen,
265265
sector_t sector)
266266
{
267-
int num_pages, bio_pages, pg_datalen, pg_sectorlen, i;
267+
int bio_pages, pg_datalen, pg_sectorlen, i;
268268
struct page *page;
269269
struct bio *bio;
270270
size_t ret;
271271
void *ptr;
272272

273273
while (datalen) {
274-
num_pages = ALIGN(datalen, PAGE_SIZE) >> PAGE_SHIFT;
275-
bio_pages = min(num_pages, BIO_MAX_PAGES);
274+
bio_pages = bio_max_segs(DIV_ROUND_UP(datalen, PAGE_SIZE));
276275

277276
atomic_inc(&lc->io_blocks);
278277

@@ -364,7 +363,7 @@ static int log_one_block(struct log_writes_c *lc,
364363
goto out;
365364

366365
atomic_inc(&lc->io_blocks);
367-
bio = bio_alloc(GFP_KERNEL, min(block->vec_cnt, BIO_MAX_PAGES));
366+
bio = bio_alloc(GFP_KERNEL, bio_max_segs(block->vec_cnt));
368367
if (!bio) {
369368
DMERR("Couldn't alloc log bio");
370369
goto error;
@@ -386,7 +385,8 @@ static int log_one_block(struct log_writes_c *lc,
386385
if (ret != block->vecs[i].bv_len) {
387386
atomic_inc(&lc->io_blocks);
388387
submit_bio(bio);
389-
bio = bio_alloc(GFP_KERNEL, min(block->vec_cnt - i, BIO_MAX_PAGES));
388+
bio = bio_alloc(GFP_KERNEL,
389+
bio_max_segs(block->vec_cnt - i));
390390
if (!bio) {
391391
DMERR("Couldn't alloc log bio");
392392
goto error;

drivers/nvme/target/io-cmd-bdev.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ static int nvmet_bdev_alloc_bip(struct nvmet_req *req, struct bio *bio,
185185
}
186186

187187
bip = bio_integrity_alloc(bio, GFP_NOIO,
188-
min_t(unsigned int, req->metadata_sg_cnt, BIO_MAX_PAGES));
188+
bio_max_segs(req->metadata_sg_cnt));
189189
if (IS_ERR(bip)) {
190190
pr_err("Unable to allocate bio_integrity_payload\n");
191191
return PTR_ERR(bip);
@@ -225,7 +225,7 @@ static int nvmet_bdev_alloc_bip(struct nvmet_req *req, struct bio *bio,
225225

226226
static void nvmet_bdev_execute_rw(struct nvmet_req *req)
227227
{
228-
int sg_cnt = req->sg_cnt;
228+
unsigned int sg_cnt = req->sg_cnt;
229229
struct bio *bio;
230230
struct scatterlist *sg;
231231
struct blk_plug plug;
@@ -262,7 +262,7 @@ static void nvmet_bdev_execute_rw(struct nvmet_req *req)
262262
bio = &req->b.inline_bio;
263263
bio_init(bio, req->inline_bvec, ARRAY_SIZE(req->inline_bvec));
264264
} else {
265-
bio = bio_alloc(GFP_KERNEL, min(sg_cnt, BIO_MAX_PAGES));
265+
bio = bio_alloc(GFP_KERNEL, bio_max_segs(sg_cnt));
266266
}
267267
bio_set_dev(bio, req->ns->bdev);
268268
bio->bi_iter.bi_sector = sector;
@@ -289,7 +289,7 @@ static void nvmet_bdev_execute_rw(struct nvmet_req *req)
289289
}
290290
}
291291

292-
bio = bio_alloc(GFP_KERNEL, min(sg_cnt, BIO_MAX_PAGES));
292+
bio = bio_alloc(GFP_KERNEL, bio_max_segs(sg_cnt));
293293
bio_set_dev(bio, req->ns->bdev);
294294
bio->bi_iter.bi_sector = sector;
295295
bio->bi_opf = op;

drivers/nvme/target/passthru.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ static u16 nvmet_passthru_override_id_ctrl(struct nvmet_req *req)
2626
struct nvme_ctrl *pctrl = ctrl->subsys->passthru_ctrl;
2727
u16 status = NVME_SC_SUCCESS;
2828
struct nvme_id_ctrl *id;
29-
int max_hw_sectors;
29+
unsigned int max_hw_sectors;
3030
int page_shift;
3131

3232
id = kzalloc(sizeof(*id), GFP_KERNEL);
@@ -198,7 +198,7 @@ static int nvmet_passthru_map_sg(struct nvmet_req *req, struct request *rq)
198198
bio = &req->p.inline_bio;
199199
bio_init(bio, req->inline_bvec, ARRAY_SIZE(req->inline_bvec));
200200
} else {
201-
bio = bio_alloc(GFP_KERNEL, min(req->sg_cnt, BIO_MAX_PAGES));
201+
bio = bio_alloc(GFP_KERNEL, bio_max_segs(req->sg_cnt));
202202
bio->bi_end_io = bio_put;
203203
}
204204
bio->bi_opf = req_op(rq);

drivers/target/target_core_iblock.c

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -315,10 +315,8 @@ iblock_get_bio(struct se_cmd *cmd, sector_t lba, u32 sg_num, int op,
315315
* Only allocate as many vector entries as the bio code allows us to,
316316
* we'll loop later on until we have handled the whole request.
317317
*/
318-
if (sg_num > BIO_MAX_PAGES)
319-
sg_num = BIO_MAX_PAGES;
320-
321-
bio = bio_alloc_bioset(GFP_NOIO, sg_num, &ib_dev->ibd_bio_set);
318+
bio = bio_alloc_bioset(GFP_NOIO, bio_max_segs(sg_num),
319+
&ib_dev->ibd_bio_set);
322320
if (!bio) {
323321
pr_err("Unable to allocate memory for bio\n");
324322
return NULL;
@@ -638,8 +636,7 @@ iblock_alloc_bip(struct se_cmd *cmd, struct bio *bio,
638636
return -ENODEV;
639637
}
640638

641-
bip = bio_integrity_alloc(bio, GFP_NOIO,
642-
min_t(unsigned int, cmd->t_prot_nents, BIO_MAX_PAGES));
639+
bip = bio_integrity_alloc(bio, GFP_NOIO, bio_max_segs(cmd->t_prot_nents));
643640
if (IS_ERR(bip)) {
644641
pr_err("Unable to allocate bio_integrity_payload\n");
645642
return PTR_ERR(bip);

drivers/target/target_core_pscsi.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -881,7 +881,7 @@ pscsi_map_sg(struct se_cmd *cmd, struct scatterlist *sgl, u32 sgl_nents,
881881

882882
if (!bio) {
883883
new_bio:
884-
nr_vecs = min_t(int, BIO_MAX_PAGES, nr_pages);
884+
nr_vecs = bio_max_segs(nr_pages);
885885
nr_pages -= nr_vecs;
886886
/*
887887
* Calls bio_kmalloc() and sets bio->bi_end_io()

fs/block_dev.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,7 @@ static void blkdev_bio_end_io_simple(struct bio *bio)
221221

222222
static ssize_t
223223
__blkdev_direct_IO_simple(struct kiocb *iocb, struct iov_iter *iter,
224-
int nr_pages)
224+
unsigned int nr_pages)
225225
{
226226
struct file *file = iocb->ki_filp;
227227
struct block_device *bdev = I_BDEV(bdev_file_inode(file));
@@ -355,8 +355,8 @@ static void blkdev_bio_end_io(struct bio *bio)
355355
}
356356
}
357357

358-
static ssize_t
359-
__blkdev_direct_IO(struct kiocb *iocb, struct iov_iter *iter, int nr_pages)
358+
static ssize_t __blkdev_direct_IO(struct kiocb *iocb, struct iov_iter *iter,
359+
unsigned int nr_pages)
360360
{
361361
struct file *file = iocb->ki_filp;
362362
struct inode *inode = bdev_file_inode(file);
@@ -486,7 +486,7 @@ __blkdev_direct_IO(struct kiocb *iocb, struct iov_iter *iter, int nr_pages)
486486
static ssize_t
487487
blkdev_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
488488
{
489-
int nr_pages;
489+
unsigned int nr_pages;
490490

491491
if (!iov_iter_count(iter))
492492
return 0;
@@ -495,7 +495,7 @@ blkdev_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
495495
if (is_sync_kiocb(iocb) && nr_pages <= BIO_MAX_PAGES)
496496
return __blkdev_direct_IO_simple(iocb, iter, nr_pages);
497497

498-
return __blkdev_direct_IO(iocb, iter, min(nr_pages, BIO_MAX_PAGES));
498+
return __blkdev_direct_IO(iocb, iter, bio_max_segs(nr_pages));
499499
}
500500

501501
static __init int blkdev_init(void)

fs/direct-io.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -695,7 +695,7 @@ static inline int dio_new_bio(struct dio *dio, struct dio_submit *sdio,
695695
if (ret)
696696
goto out;
697697
sector = start_sector << (sdio->blkbits - 9);
698-
nr_pages = min(sdio->pages_in_io, BIO_MAX_PAGES);
698+
nr_pages = bio_max_segs(sdio->pages_in_io);
699699
BUG_ON(nr_pages <= 0);
700700
dio_bio_alloc(dio, sdio, map_bh->b_bdev, sector, nr_pages);
701701
sdio->boundary = 0;

0 commit comments

Comments
 (0)