block: Add bio_max_segs
authorMatthew Wilcox (Oracle) <willy@infradead.org>
Fri, 29 Jan 2021 04:38:57 +0000 (04:38 +0000)
committerJens Axboe <axboe@kernel.dk>
Fri, 26 Feb 2021 22:49:51 +0000 (15:49 -0700)
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>
20 files changed:
block/blk-map.c
drivers/block/xen-blkback/blkback.c
drivers/md/dm-io.c
drivers/md/dm-log-writes.c
drivers/nvme/target/io-cmd-bdev.c
drivers/nvme/target/passthru.c
drivers/target/target_core_iblock.c
drivers/target/target_core_pscsi.c
fs/block_dev.c
fs/direct-io.c
fs/erofs/data.c
fs/ext4/readpage.c
fs/f2fs/data.c
fs/f2fs/node.c
fs/iomap/buffered-io.c
fs/mpage.c
fs/nfs/blocklayout/blocklayout.c
fs/xfs/xfs_bio_io.c
fs/xfs/xfs_buf.c
include/linux/bio.h

index 21630dccac628c3fac3131c5d900fc144ea962e1..369e204d14d013f66e3422444d19361d09a10863 100644 (file)
@@ -150,9 +150,7 @@ static int bio_copy_user_iov(struct request *rq, struct rq_map_data *map_data,
        bmd->is_our_pages = !map_data;
        bmd->is_null_mapped = (map_data && map_data->null_mapped);
 
-       nr_pages = DIV_ROUND_UP(offset + len, PAGE_SIZE);
-       if (nr_pages > BIO_MAX_PAGES)
-               nr_pages = BIO_MAX_PAGES;
+       nr_pages = bio_max_segs(DIV_ROUND_UP(offset + len, PAGE_SIZE));
 
        ret = -ENOMEM;
        bio = bio_kmalloc(gfp_mask, nr_pages);
index da16121140cab23b8840e2aaa72131c795df8099..1cdf09ff67b68c5d704d002ae59a3a6a0c9ba7c2 100644 (file)
@@ -1326,9 +1326,7 @@ static int dispatch_rw_block_io(struct xen_blkif_ring *ring,
                                     pages[i]->page,
                                     seg[i].nsec << 9,
                                     seg[i].offset) == 0)) {
-
-                       int nr_iovecs = min_t(int, (nseg-i), BIO_MAX_PAGES);
-                       bio = bio_alloc(GFP_KERNEL, nr_iovecs);
+                       bio = bio_alloc(GFP_KERNEL, bio_max_segs(nseg - i));
                        if (unlikely(bio == NULL))
                                goto fail_put_bio;
 
index 4312007d2d34b8307059dd0c33c98ceab5e1d86d..2d3cda0acacb6a5c3d916b602d19268dd08ecc38 100644 (file)
@@ -341,8 +341,8 @@ static void do_region(int op, int op_flags, unsigned region,
                        num_bvecs = 1;
                        break;
                default:
-                       num_bvecs = min_t(int, BIO_MAX_PAGES,
-                                         dm_sector_div_up(remaining, (PAGE_SIZE >> SECTOR_SHIFT)));
+                       num_bvecs = bio_max_segs(dm_sector_div_up(remaining,
+                                               (PAGE_SIZE >> SECTOR_SHIFT)));
                }
 
                bio = bio_alloc_bioset(GFP_NOIO, num_bvecs, &io->client->bios);
index e3d35c6c9f714d49d6187fbdc2eac1a9df40e29a..57882654ffee79eee87de2b582ccaf124a559000 100644 (file)
@@ -264,15 +264,14 @@ static int write_inline_data(struct log_writes_c *lc, void *entry,
                             size_t entrylen, void *data, size_t datalen,
                             sector_t sector)
 {
-       int num_pages, bio_pages, pg_datalen, pg_sectorlen, i;
+       int bio_pages, pg_datalen, pg_sectorlen, i;
        struct page *page;
        struct bio *bio;
        size_t ret;
        void *ptr;
 
        while (datalen) {
-               num_pages = ALIGN(datalen, PAGE_SIZE) >> PAGE_SHIFT;
-               bio_pages = min(num_pages, BIO_MAX_PAGES);
+               bio_pages = bio_max_segs(DIV_ROUND_UP(datalen, PAGE_SIZE));
 
                atomic_inc(&lc->io_blocks);
 
@@ -364,7 +363,7 @@ static int log_one_block(struct log_writes_c *lc,
                goto out;
 
        atomic_inc(&lc->io_blocks);
-       bio = bio_alloc(GFP_KERNEL, min(block->vec_cnt, BIO_MAX_PAGES));
+       bio = bio_alloc(GFP_KERNEL, bio_max_segs(block->vec_cnt));
        if (!bio) {
                DMERR("Couldn't alloc log bio");
                goto error;
@@ -386,7 +385,8 @@ static int log_one_block(struct log_writes_c *lc,
                if (ret != block->vecs[i].bv_len) {
                        atomic_inc(&lc->io_blocks);
                        submit_bio(bio);
-                       bio = bio_alloc(GFP_KERNEL, min(block->vec_cnt - i, BIO_MAX_PAGES));
+                       bio = bio_alloc(GFP_KERNEL,
+                                       bio_max_segs(block->vec_cnt - i));
                        if (!bio) {
                                DMERR("Couldn't alloc log bio");
                                goto error;
index 3d9a5d3ed9cdbfeed8f1f9ffea49adf28b675510..9a8b3726a37c455a9e96056331e179b367e6fc9f 100644 (file)
@@ -185,7 +185,7 @@ static int nvmet_bdev_alloc_bip(struct nvmet_req *req, struct bio *bio,
        }
 
        bip = bio_integrity_alloc(bio, GFP_NOIO,
-               min_t(unsigned int, req->metadata_sg_cnt, BIO_MAX_PAGES));
+                                       bio_max_segs(req->metadata_sg_cnt));
        if (IS_ERR(bip)) {
                pr_err("Unable to allocate bio_integrity_payload\n");
                return PTR_ERR(bip);
@@ -225,7 +225,7 @@ static int nvmet_bdev_alloc_bip(struct nvmet_req *req, struct bio *bio,
 
 static void nvmet_bdev_execute_rw(struct nvmet_req *req)
 {
-       int sg_cnt = req->sg_cnt;
+       unsigned int sg_cnt = req->sg_cnt;
        struct bio *bio;
        struct scatterlist *sg;
        struct blk_plug plug;
@@ -262,7 +262,7 @@ static void nvmet_bdev_execute_rw(struct nvmet_req *req)
                bio = &req->b.inline_bio;
                bio_init(bio, req->inline_bvec, ARRAY_SIZE(req->inline_bvec));
        } else {
-               bio = bio_alloc(GFP_KERNEL, min(sg_cnt, BIO_MAX_PAGES));
+               bio = bio_alloc(GFP_KERNEL, bio_max_segs(sg_cnt));
        }
        bio_set_dev(bio, req->ns->bdev);
        bio->bi_iter.bi_sector = sector;
@@ -289,7 +289,7 @@ static void nvmet_bdev_execute_rw(struct nvmet_req *req)
                                }
                        }
 
-                       bio = bio_alloc(GFP_KERNEL, min(sg_cnt, BIO_MAX_PAGES));
+                       bio = bio_alloc(GFP_KERNEL, bio_max_segs(sg_cnt));
                        bio_set_dev(bio, req->ns->bdev);
                        bio->bi_iter.bi_sector = sector;
                        bio->bi_opf = op;
index f50c7b2bf21c44ae310a27d5c0ffcbca8eee601c..26c587ccd152c2649f0aff66a50b880e82f6d04f 100644 (file)
@@ -26,7 +26,7 @@ static u16 nvmet_passthru_override_id_ctrl(struct nvmet_req *req)
        struct nvme_ctrl *pctrl = ctrl->subsys->passthru_ctrl;
        u16 status = NVME_SC_SUCCESS;
        struct nvme_id_ctrl *id;
-       int max_hw_sectors;
+       unsigned int max_hw_sectors;
        int page_shift;
 
        id = kzalloc(sizeof(*id), GFP_KERNEL);
@@ -198,7 +198,7 @@ static int nvmet_passthru_map_sg(struct nvmet_req *req, struct request *rq)
                bio = &req->p.inline_bio;
                bio_init(bio, req->inline_bvec, ARRAY_SIZE(req->inline_bvec));
        } else {
-               bio = bio_alloc(GFP_KERNEL, min(req->sg_cnt, BIO_MAX_PAGES));
+               bio = bio_alloc(GFP_KERNEL, bio_max_segs(req->sg_cnt));
                bio->bi_end_io = bio_put;
        }
        bio->bi_opf = req_op(rq);
index 8ed93fd205c7e83c3bc7eba3bf8ce9ef85890112..ee3d52061281202e209a9d3530e7f7866c2416b5 100644 (file)
@@ -315,10 +315,8 @@ iblock_get_bio(struct se_cmd *cmd, sector_t lba, u32 sg_num, int op,
         * Only allocate as many vector entries as the bio code allows us to,
         * we'll loop later on until we have handled the whole request.
         */
-       if (sg_num > BIO_MAX_PAGES)
-               sg_num = BIO_MAX_PAGES;
-
-       bio = bio_alloc_bioset(GFP_NOIO, sg_num, &ib_dev->ibd_bio_set);
+       bio = bio_alloc_bioset(GFP_NOIO, bio_max_segs(sg_num),
+                               &ib_dev->ibd_bio_set);
        if (!bio) {
                pr_err("Unable to allocate memory for bio\n");
                return NULL;
@@ -638,8 +636,7 @@ iblock_alloc_bip(struct se_cmd *cmd, struct bio *bio,
                return -ENODEV;
        }
 
-       bip = bio_integrity_alloc(bio, GFP_NOIO,
-                       min_t(unsigned int, cmd->t_prot_nents, BIO_MAX_PAGES));
+       bip = bio_integrity_alloc(bio, GFP_NOIO, bio_max_segs(cmd->t_prot_nents));
        if (IS_ERR(bip)) {
                pr_err("Unable to allocate bio_integrity_payload\n");
                return PTR_ERR(bip);
index 33770e5808ce5eb27dbf3fc5ee444bc7aa1b3444..3cbc074992bc86af1606acf481c2ac60f49bba58 100644 (file)
@@ -881,7 +881,7 @@ pscsi_map_sg(struct se_cmd *cmd, struct scatterlist *sgl, u32 sgl_nents,
 
                        if (!bio) {
 new_bio:
-                               nr_vecs = min_t(int, BIO_MAX_PAGES, nr_pages);
+                               nr_vecs = bio_max_segs(nr_pages);
                                nr_pages -= nr_vecs;
                                /*
                                 * Calls bio_kmalloc() and sets bio->bi_end_io()
index ec26179c80623008a83d5b9c15026f77b1173828..0f95ff343d6bad776fa819c237285e1128345aff 100644 (file)
@@ -221,7 +221,7 @@ static void blkdev_bio_end_io_simple(struct bio *bio)
 
 static ssize_t
 __blkdev_direct_IO_simple(struct kiocb *iocb, struct iov_iter *iter,
-               int nr_pages)
+               unsigned int nr_pages)
 {
        struct file *file = iocb->ki_filp;
        struct block_device *bdev = I_BDEV(bdev_file_inode(file));
@@ -355,8 +355,8 @@ static void blkdev_bio_end_io(struct bio *bio)
        }
 }
 
-static ssize_t
-__blkdev_direct_IO(struct kiocb *iocb, struct iov_iter *iter, int nr_pages)
+static ssize_t __blkdev_direct_IO(struct kiocb *iocb, struct iov_iter *iter,
+               unsigned int nr_pages)
 {
        struct file *file = iocb->ki_filp;
        struct inode *inode = bdev_file_inode(file);
@@ -486,7 +486,7 @@ __blkdev_direct_IO(struct kiocb *iocb, struct iov_iter *iter, int nr_pages)
 static ssize_t
 blkdev_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
 {
-       int nr_pages;
+       unsigned int nr_pages;
 
        if (!iov_iter_count(iter))
                return 0;
@@ -495,7 +495,7 @@ blkdev_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
        if (is_sync_kiocb(iocb) && nr_pages <= BIO_MAX_PAGES)
                return __blkdev_direct_IO_simple(iocb, iter, nr_pages);
 
-       return __blkdev_direct_IO(iocb, iter, min(nr_pages, BIO_MAX_PAGES));
+       return __blkdev_direct_IO(iocb, iter, bio_max_segs(nr_pages));
 }
 
 static __init int blkdev_init(void)
index aa1083ecd62392f9c86d8b0d83bb3842f453c458..c9639b4166c23b20b827292204487791c3d2391f 100644 (file)
@@ -695,7 +695,7 @@ static inline int dio_new_bio(struct dio *dio, struct dio_submit *sdio,
        if (ret)
                goto out;
        sector = start_sector << (sdio->blkbits - 9);
-       nr_pages = min(sdio->pages_in_io, BIO_MAX_PAGES);
+       nr_pages = bio_max_segs(sdio->pages_in_io);
        BUG_ON(nr_pages <= 0);
        dio_bio_alloc(dio, sdio, map_bh->b_bdev, sector, nr_pages);
        sdio->boundary = 0;
index ea4f693bee2247327f870af0e6ba3da77b487e58..f88851c5c250b5608a4f40fc221ddee9fa8e2108 100644 (file)
@@ -215,10 +215,8 @@ submit_bio_retry:
                /* max # of continuous pages */
                if (nblocks > DIV_ROUND_UP(map.m_plen, PAGE_SIZE))
                        nblocks = DIV_ROUND_UP(map.m_plen, PAGE_SIZE);
-               if (nblocks > BIO_MAX_PAGES)
-                       nblocks = BIO_MAX_PAGES;
 
-               bio = bio_alloc(GFP_NOIO, nblocks);
+               bio = bio_alloc(GFP_NOIO, bio_max_segs(nblocks));
 
                bio->bi_end_io = erofs_readendio;
                bio_set_dev(bio, sb->s_bdev);
index f014c5e473a9d228d7c3e66ef0214768725a0540..3db9234035053ed976030abc2152acf9c3aac84a 100644 (file)
@@ -371,8 +371,7 @@ int ext4_mpage_readpages(struct inode *inode,
                         * bio_alloc will _always_ be able to allocate a bio if
                         * __GFP_DIRECT_RECLAIM is set, see bio_alloc_bioset().
                         */
-                       bio = bio_alloc(GFP_KERNEL,
-                               min_t(int, nr_pages, BIO_MAX_PAGES));
+                       bio = bio_alloc(GFP_KERNEL, bio_max_segs(nr_pages));
                        fscrypt_set_bio_crypt_ctx(bio, inode, next_block,
                                                  GFP_KERNEL);
                        ext4_set_bio_post_read_ctx(bio, inode, page->index);
index b9721c8f116cade6e3f34f88e1bc87f1263af055..7c95818639a630e1bdcb59af77a38bc4b5d14346 100644 (file)
@@ -969,8 +969,7 @@ static struct bio *f2fs_grab_read_bio(struct inode *inode, block_t blkaddr,
        unsigned int post_read_steps = 0;
 
        bio = bio_alloc_bioset(for_write ? GFP_NOIO : GFP_KERNEL,
-                              min_t(int, nr_pages, BIO_MAX_PAGES),
-                              &f2fs_bioset);
+                              bio_max_segs(nr_pages), &f2fs_bioset);
        if (!bio)
                return ERR_PTR(-ENOMEM);
 
index a8a0fb890e8d665a2f65efc2dd0f10ed5b0c1276..4b0e2e3c2c8867cf5a6dd6b97dcc0b3ad1c0edf4 100644 (file)
@@ -2747,7 +2747,7 @@ int f2fs_restore_node_summary(struct f2fs_sb_info *sbi,
        sum_entry = &sum->entries[0];
 
        for (i = 0; i < last_offset; i += nrpages, addr += nrpages) {
-               nrpages = min(last_offset - i, BIO_MAX_PAGES);
+               nrpages = bio_max_segs(last_offset - i);
 
                /* readahead node pages */
                f2fs_ra_meta_pages(sbi, addr, nrpages, META_POR, true);
index 16a1e82e3aeb0dc3dc9b629397b4652e1aa8c5ad..0d9d1a6a947ed094b56506b876e971c46f98f8fb 100644 (file)
@@ -278,14 +278,14 @@ iomap_readpage_actor(struct inode *inode, loff_t pos, loff_t length, void *data,
        if (!is_contig || bio_full(ctx->bio, plen)) {
                gfp_t gfp = mapping_gfp_constraint(page->mapping, GFP_KERNEL);
                gfp_t orig_gfp = gfp;
-               int nr_vecs = (length + PAGE_SIZE - 1) >> PAGE_SHIFT;
+               unsigned int nr_vecs = DIV_ROUND_UP(length, PAGE_SIZE);
 
                if (ctx->bio)
                        submit_bio(ctx->bio);
 
                if (ctx->rac) /* same as readahead_gfp_mask */
                        gfp |= __GFP_NORETRY | __GFP_NOWARN;
-               ctx->bio = bio_alloc(gfp, min(BIO_MAX_PAGES, nr_vecs));
+               ctx->bio = bio_alloc(gfp, bio_max_segs(nr_vecs));
                /*
                 * If the bio_alloc fails, try it again for a single page to
                 * avoid having to deal with partial page reads.  This emulates
index 830e6cc2a9e727ec37d29ad743fc4e2f8cebe2fe..961234d687792a9b432341e2a7c0eed819f876d1 100644 (file)
@@ -304,9 +304,7 @@ alloc_new:
                                goto out;
                }
                args->bio = mpage_alloc(bdev, blocks[0] << (blkbits - 9),
-                                       min_t(int, args->nr_pages,
-                                             BIO_MAX_PAGES),
-                                       gfp);
+                                       bio_max_segs(args->nr_pages), gfp);
                if (args->bio == NULL)
                        goto confused;
        }
index 1a96ce28efb026db4953914c5348e9462685f499..fe860c53874768552912293fcd22e5c278bcb829 100644 (file)
@@ -115,13 +115,13 @@ bl_submit_bio(struct bio *bio)
        return NULL;
 }
 
-static struct bio *
-bl_alloc_init_bio(int npg, struct block_device *bdev, sector_t disk_sector,
+static struct bio *bl_alloc_init_bio(unsigned int npg,
+               struct block_device *bdev, sector_t disk_sector,
                bio_end_io_t end_io, struct parallel_io *par)
 {
        struct bio *bio;
 
-       npg = min(npg, BIO_MAX_PAGES);
+       npg = bio_max_segs(npg);
        bio = bio_alloc(GFP_NOIO, npg);
        if (bio) {
                bio->bi_iter.bi_sector = disk_sector;
index e2148f2d5d6bc048ebc666fc6ada98fb13fe5f06..17f36db2f792891604611d3b40d18cb281460edc 100644 (file)
@@ -6,7 +6,7 @@
 
 static inline unsigned int bio_max_vecs(unsigned int count)
 {
-       return min_t(unsigned, howmany(count, PAGE_SIZE), BIO_MAX_PAGES);
+       return bio_max_segs(howmany(count, PAGE_SIZE));
 }
 
 int
index f6e5235df7c904a0936bb6862bf585a04ee64732..37a1d12762d8f54c3c438bf1f30eaf5086b8ccd3 100644 (file)
@@ -1480,7 +1480,7 @@ xfs_buf_ioapply_map(
        int             op)
 {
        int             page_index;
-       int             total_nr_pages = bp->b_page_count;
+       unsigned int    total_nr_pages = bp->b_page_count;
        int             nr_pages;
        struct bio      *bio;
        sector_t        sector =  bp->b_maps[map].bm_bn;
@@ -1505,7 +1505,7 @@ xfs_buf_ioapply_map(
 
 next_chunk:
        atomic_inc(&bp->b_io_remaining);
-       nr_pages = min(total_nr_pages, BIO_MAX_PAGES);
+       nr_pages = bio_max_segs(total_nr_pages);
 
        bio = bio_alloc(GFP_NOIO, nr_pages);
        bio_set_dev(bio, bp->b_target->bt_bdev);
index 5b468f2242ff27f44f43c0ca29c6d674a821fbce..983ed2fe7c850479040d42a569191a75206e2fb3 100644 (file)
 #define BIO_BUG_ON
 #endif
 
-#define BIO_MAX_PAGES          256
+#define BIO_MAX_PAGES          256U
+
+static inline unsigned int bio_max_segs(unsigned int nr_segs)
+{
+       return min(nr_segs, BIO_MAX_PAGES);
+}
 
 #define bio_prio(bio)                  (bio)->bi_ioprio
 #define bio_set_prio(bio, prio)                ((bio)->bi_ioprio = prio)