]> git.ipfire.org Git - thirdparty/kernel/linux.git/commitdiff
block: add a BIO_MAX_SIZE constant and use it
authorChristoph Hellwig <hch@lst.de>
Mon, 26 Jan 2026 05:53:32 +0000 (06:53 +0100)
committerJens Axboe <axboe@kernel.dk>
Wed, 28 Jan 2026 12:16:39 +0000 (05:16 -0700)
Currently the only constant for the maximum bio size is BIO_MAX_SECTORS,
which is in units of 512-byte sectors, but a lot of user need a byte
limit.

Add a BIO_MAX_SIZE constant, redefine BIO_MAX_SECTORS in terms of it, and
switch all bio-related uses of UINT_MAX for the maximum size to use the
symbolic names instead.

Signed-off-by: Christoph Hellwig <hch@lst.de>
Reviewed-by: Martin K. Petersen <martin.petersen@oracle.com>
Reviewed-by: Darrick J. Wong <djwong@kernel.org>
Reviewed-by: Anuj Gupta <anuj20.g@samsung.com>
Reviewed-by: Johannes Thumshirn <johannes.thumshirn@wdc.com>
Reviewed-by: Damien Le Moal <dlemoal@kernel.org>
Signed-off-by: Jens Axboe <axboe@kernel.dk>
block/bio.c
block/blk-lib.c
block/blk-merge.c
include/linux/blk_types.h

index 2359c0723b88eb21c362a4ef44b4b5954856c8d8..ac7703e149c6b018816d538b8fde03dd9ec5dbb3 100644 (file)
@@ -924,7 +924,7 @@ static inline bool bio_full(struct bio *bio, unsigned len)
 {
        if (bio->bi_vcnt >= bio->bi_max_vecs)
                return true;
-       if (bio->bi_iter.bi_size > UINT_MAX - len)
+       if (bio->bi_iter.bi_size > BIO_MAX_SIZE - len)
                return true;
        return false;
 }
@@ -1030,7 +1030,7 @@ int bio_add_page(struct bio *bio, struct page *page,
 {
        if (WARN_ON_ONCE(bio_flagged(bio, BIO_CLONED)))
                return 0;
-       if (bio->bi_iter.bi_size > UINT_MAX - len)
+       if (bio->bi_iter.bi_size > BIO_MAX_SIZE - len)
                return 0;
 
        if (bio->bi_vcnt > 0) {
@@ -1057,7 +1057,7 @@ void bio_add_folio_nofail(struct bio *bio, struct folio *folio, size_t len,
 {
        unsigned long nr = off / PAGE_SIZE;
 
-       WARN_ON_ONCE(len > UINT_MAX);
+       WARN_ON_ONCE(len > BIO_MAX_SIZE);
        __bio_add_page(bio, folio_page(folio, nr), len, off % PAGE_SIZE);
 }
 EXPORT_SYMBOL_GPL(bio_add_folio_nofail);
@@ -1081,7 +1081,7 @@ bool bio_add_folio(struct bio *bio, struct folio *folio, size_t len,
 {
        unsigned long nr = off / PAGE_SIZE;
 
-       if (len > UINT_MAX)
+       if (len > BIO_MAX_SIZE)
                return false;
        return bio_add_page(bio, folio_page(folio, nr), len, off % PAGE_SIZE) > 0;
 }
@@ -1238,7 +1238,7 @@ static int __bio_iov_iter_get_pages(struct bio *bio, struct iov_iter *iter)
                extraction_flags |= ITER_ALLOW_P2PDMA;
 
        size = iov_iter_extract_pages(iter, &pages,
-                                     UINT_MAX - bio->bi_iter.bi_size,
+                                     BIO_MAX_SIZE - bio->bi_iter.bi_size,
                                      nr_pages, extraction_flags, &offset);
        if (unlikely(size <= 0))
                return size ? size : -EFAULT;
index 9e2cc58f881f89018b62db85b3664293624be41e..0be3acdc3eb5379c96f154c8d2c313db01c96461 100644 (file)
@@ -32,7 +32,7 @@ static sector_t bio_discard_limit(struct block_device *bdev, sector_t sector)
         * Align the bio size to the discard granularity to make splitting the bio
         * at discard granularity boundaries easier in the driver if needed.
         */
-       return round_down(UINT_MAX, discard_granularity) >> SECTOR_SHIFT;
+       return round_down(BIO_MAX_SIZE, discard_granularity) >> SECTOR_SHIFT;
 }
 
 struct bio *blk_alloc_discard_bio(struct block_device *bdev,
@@ -107,8 +107,7 @@ static sector_t bio_write_zeroes_limit(struct block_device *bdev)
 {
        sector_t bs_mask = (bdev_logical_block_size(bdev) >> 9) - 1;
 
-       return min(bdev_write_zeroes_sectors(bdev),
-               (UINT_MAX >> SECTOR_SHIFT) & ~bs_mask);
+       return min(bdev_write_zeroes_sectors(bdev), BIO_MAX_SECTORS & ~bs_mask);
 }
 
 /*
@@ -337,8 +336,8 @@ int blkdev_issue_secure_erase(struct block_device *bdev, sector_t sector,
        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;
+       if (max_sectors > BIO_MAX_SECTORS)
+               max_sectors = BIO_MAX_SECTORS;
        max_sectors &= ~bs_mask;
 
        if (max_sectors == 0)
index b82c6d3046583671411a658ed622b15ce1c39283..0eb0aef971971d2abc9b5dd10aeffb87d8865b3a 100644 (file)
@@ -95,13 +95,13 @@ static inline bool req_gap_front_merge(struct request *req, struct bio *bio)
 }
 
 /*
- * The max size one bio can handle is UINT_MAX becasue bvec_iter.bi_size
- * is defined as 'unsigned int', meantime it has to be aligned to with the
+ * The maximum size that a bio can fit has to be aligned down to the
  * logical block size, which is the minimum accepted unit by hardware.
  */
 static unsigned int bio_allowed_max_sectors(const struct queue_limits *lim)
 {
-       return round_down(UINT_MAX, lim->logical_block_size) >> SECTOR_SHIFT;
+       return round_down(BIO_MAX_SIZE, lim->logical_block_size) >>
+                       SECTOR_SHIFT;
 }
 
 /*
@@ -502,7 +502,7 @@ unsigned int blk_recalc_rq_segments(struct request *rq)
 
        rq_for_each_bvec(bv, rq, iter)
                bvec_split_segs(&rq->q->limits, &bv, &nr_phys_segs, &bytes,
-                               UINT_MAX, UINT_MAX);
+                               UINT_MAX, BIO_MAX_SIZE);
        return nr_phys_segs;
 }
 
index 19a888a2f104e8399d872f056f83c3c4f8030fe1..d59553324a84be260ba77fedaaba1007e9d952df 100644 (file)
@@ -281,7 +281,8 @@ struct bio {
 };
 
 #define BIO_RESET_BYTES                offsetof(struct bio, bi_max_vecs)
-#define BIO_MAX_SECTORS                (UINT_MAX >> SECTOR_SHIFT)
+#define BIO_MAX_SIZE           UINT_MAX /* max value of bi_iter.bi_size */
+#define BIO_MAX_SECTORS                (BIO_MAX_SIZE >> SECTOR_SHIFT)
 
 static inline struct bio_vec *bio_inline_vecs(struct bio *bio)
 {