block: move the bi_size overflow check in __bio_try_merge_page
authorChristoph Hellwig <hch@lst.de>
Mon, 24 Jul 2023 16:54:30 +0000 (09:54 -0700)
committerJens Axboe <axboe@kernel.dk>
Tue, 25 Jul 2023 01:55:16 +0000 (19:55 -0600)
Checking for availability in bi_size in a function that attempts to
merge into an existing segment is a bit odd, as the limit also applies
when adding a new segment.  This code works fine as we always call
__bio_try_merge_page, but contributes to sub-optimal calling conventions
and doesn't lead to clear code.

Move it to two of the callers instead, the third one already has a more
strict check that includes max_hw_segments anyway.

Signed-off-by: Christoph Hellwig <hch@lst.de>
Reviewed-by: Jinyoung Choi <j-young.choi@samsung.com>
Reviewed-by: Johannes Thumshirn <johannes.thumshirn@wdc.com>
Link: https://lore.kernel.org/r/20230724165433.117645-6-hch@lst.de
Signed-off-by: Jens Axboe <axboe@kernel.dk>
block/bio.c

index 3dcbe98..17f57fd 100644 (file)
@@ -949,10 +949,6 @@ static bool __bio_try_merge_page(struct bio *bio, struct page *page,
 
        if (!page_is_mergeable(bv, page, len, off, same_page))
                return false;
-       if (bio->bi_iter.bi_size > UINT_MAX - len) {
-               *same_page = false;
-               return false;
-       }
        bv->bv_len += len;
        bio->bi_iter.bi_size += len;
        return true;
@@ -1123,6 +1119,8 @@ 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)
+               return 0;
 
        if (bio->bi_vcnt > 0 &&
            __bio_try_merge_page(bio, page, len, offset, &same_page))
@@ -1204,6 +1202,9 @@ static int bio_iov_add_page(struct bio *bio, struct page *page,
 {
        bool same_page = false;
 
+       if (WARN_ON_ONCE(bio->bi_iter.bi_size > UINT_MAX - len))
+               return -EIO;
+
        if (bio->bi_vcnt > 0 &&
            __bio_try_merge_page(bio, page, len, offset, &same_page)) {
                if (same_page)