Skip to content

Commit 34cdb8c

Browse files
keithbuschaxboe
authored andcommitted
block: ensure bio_iov_add_page can't fail
Adding the page could fail on the bio_full() condition, which checks for either exceeding the bio's max segments or total size exceeding UINT_MAX. We already ensure the max segments can't be exceeded, so just ensure the total size won't reach the limit. This simplifies error handling and removes unnecessary repeated bio_full() checks. Signed-off-by: Keith Busch <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Jens Axboe <[email protected]>
1 parent 325347d commit 34cdb8c

File tree

1 file changed

+9
-10
lines changed

1 file changed

+9
-10
lines changed

block/bio.c

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1165,8 +1165,6 @@ static int bio_iov_add_page(struct bio *bio, struct page *page,
11651165
bool same_page = false;
11661166

11671167
if (!__bio_try_merge_page(bio, page, len, offset, &same_page)) {
1168-
if (WARN_ON_ONCE(bio_full(bio, len)))
1169-
return -EINVAL;
11701168
__bio_add_page(bio, page, len, offset);
11711169
return 0;
11721170
}
@@ -1228,7 +1226,8 @@ static int __bio_iov_iter_get_pages(struct bio *bio, struct iov_iter *iter)
12281226
* result to ensure the bio's total size is correct. The remainder of
12291227
* the iov data will be picked up in the next bio iteration.
12301228
*/
1231-
size = iov_iter_get_pages(iter, pages, LONG_MAX, nr_pages, &offset);
1229+
size = iov_iter_get_pages(iter, pages, UINT_MAX - bio->bi_iter.bi_size,
1230+
nr_pages, &offset);
12321231
if (size > 0)
12331232
size = ALIGN_DOWN(size, bdev_logical_block_size(bio->bi_bdev));
12341233
if (unlikely(size <= 0))
@@ -1238,16 +1237,16 @@ static int __bio_iov_iter_get_pages(struct bio *bio, struct iov_iter *iter)
12381237
struct page *page = pages[i];
12391238

12401239
len = min_t(size_t, PAGE_SIZE - offset, left);
1241-
if (bio_op(bio) == REQ_OP_ZONE_APPEND)
1240+
if (bio_op(bio) == REQ_OP_ZONE_APPEND) {
12421241
ret = bio_iov_add_zone_append_page(bio, page, len,
12431242
offset);
1244-
else
1245-
ret = bio_iov_add_page(bio, page, len, offset);
1243+
if (ret) {
1244+
bio_put_pages(pages + i, left, offset);
1245+
break;
1246+
}
1247+
} else
1248+
bio_iov_add_page(bio, page, len, offset);
12461249

1247-
if (ret) {
1248-
bio_put_pages(pages + i, left, offset);
1249-
break;
1250-
}
12511250
offset = 0;
12521251
}
12531252

0 commit comments

Comments
 (0)