Skip to contentSkip to conversation

block: fix folio leak in bio_iov_iter_bounce_read()

PATCH In review Jens Axboe 3 participants 4 messages Feb 12 – Feb 17 Block layer

lore
Share & tools
lore

Jens Axboe

author maintainer
#0 lore raw

PATCH 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.

Fixes 8dd5e7c75d7b ("block: add helpers to bounce buffer an iov_iter into bios")

---

block/bio.c +3 -1
index b291b9aaeee1..8203bb7455a9 100644
@@ -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
#1 lore raw

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")

---

block/bio.c +0 -0
index b291b9aaeee1..8203bb7455a9 100644
@@ -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
index 49f7548a31d6..742d395f98e1 100644
@@ -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

#2 lore raw
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.