block: fix folio leak in bio_iov_iter_bounce_read()
Jens Axboe
maintainerPATCH 1/1 block: fix folio leak in bio_iov_iter_bounce_read()
If iov_iter_extract_bvecs() returns an error or zero bytes extracted, then the folio allocated is leaked on return. Ensure it's put before returning.
---
block/bio.c +3 -1
@@ -1382,8 +1382,10 @@ static int bio_iov_iter_bounce_read(struct bio *bio, struct iov_iter *iter) ret =iov_iter_extract_bvecs (iter, bio->bi_io_vec +1 , len, &bio->bi_vcnt, bio->bi_max_vecs -1 ,0 );if (ret <=0 ) { -if (!bio->bi_vcnt) +if (!bio->bi_vcnt) { +folio_put (folio);return ret; + }break ; } len -= ret;
Signature · 1 line
Jens Axboe
Hello,
Quoted 16 lines · Jens Axboe
If
iov_iter_extract_bvecs()returns an error or zero bytes extracted, then the folio allocated is leaked on return. Ensure it's put before returning.Fixes 8dd5e7c75d7b ("block: add helpers to bounce buffer an iov_iter into bios")Signed-off-by Jens Axboe <[email protected]>---
block/bio.c +0
-0@@ -1382,8 +1382,10 @@ static int bio_iov_iter_bounce_read(struct bio *bio, struct iov_iter *iter) ret = iov_iter_extract_bvecs(iter, bio->bi_io_vec + 1, len,
block/bio.c static int bio_iov_iter_bounce_read(struct bio *bio, struct iov_iter *iter) · line 1391
Quoted 10 lines
block/bio.c +3
-1&bio->bi_vcnt, bio->bi_max_vecs - 1, 0);if (ret <=0 ) { -if (!bio->bi_vcnt) +if (!bio->bi_vcnt) { +folio_put (folio);return ret; + }break ; } len -= ret;
Isn't it better to move folio allocation after the while loop instead, right before it is actually used - less error prone in future updates, tighter loop, better cache wise.
Signature · 2 lines
have fun, alex
block/bio.c +4 -4
@@ -1338,10 +1338,6 @@ static int bio_iov_iter_bounce_read(struct bio *bio, struct iov_iter *iter)size_t len =min (iov_iter_count (iter),SZ_1M );struct folio *folio; - folio =folio_alloc_greedy (GFP_KERNEL , &len); -if (!folio) -return -ENOMEM ; - do { ssize_t ret;
@@ -1356,6 +1352,10 @@ static int bio_iov_iter_bounce_read(struct bio *bio, struct iov_iter *iter) bio->bi_iter.bi_size += ret; }while (len && bio->bi_vcnt < bio->bi_max_vecs -1 ); + folio =folio_alloc_greedy (GFP_KERNEL , &len); +if (!folio) +return -ENOMEM ; +/* * Set the folio directly here. The above loop has already
calculated
* the correct bi_size, and we use bi_vcnt for the user
buffers. That
Quoted 3 lines · Alexander Atanasov
Isn't it better to move folio allocation after the while loop instead, right before it is actually used - less error prone in future updates, tighter loop, better cache wise.
We can't do that, as folio_alloc_greedy returns how much we actually were able to allocate.
Quoted 6 lines · Jens Axboe
If
iov_iter_extract_bvecs()returns an error or zero bytes extracted, then the folio allocated is leaked on return. Ensure it's put before returning.Fixes 8dd5e7c75d7b ("block: add helpers to bounce buffer an iov_iter into bios")Signed-off-by Jens Axboe <[email protected]>
Looks good, thanks: