[v2] block: release bip in a right way in error path
diff mbox series

Message ID 20200624102139.5048-1-cgxu519@mykernel.net
State New
Headers show
Series
  • [v2] block: release bip in a right way in error path
Related show

Commit Message

cgxu June 24, 2020, 10:21 a.m. UTC
Release bip using kfree() in error path when that was allocated
by kmalloc().

Signed-off-by: Chengguang Xu <cgxu519@mykernel.net>
---
v1->v2:
- Introduce a new helper __bio_integrity_free() to reduce duplicated
code.

 block/bio-integrity.c | 23 ++++++++++++++---------
 1 file changed, 14 insertions(+), 9 deletions(-)

Comments

Christoph Hellwig June 24, 2020, 10:34 a.m. UTC | #1
On Wed, Jun 24, 2020 at 06:21:39PM +0800, Chengguang Xu wrote:
> Release bip using kfree() in error path when that was allocated
> by kmalloc().
> 
> Signed-off-by: Chengguang Xu <cgxu519@mykernel.net>

Looks good,

Reviewed-by: Christoph Hellwig <hch@lst.de>
Martin K. Petersen June 24, 2020, 12:07 p.m. UTC | #2
Chengguang,

> Release bip using kfree() in error path when that was allocated
> by kmalloc().

Looks good to me.

Acked-by: Martin K. Petersen <martin.petersen@oracle.com>
Jens Axboe June 24, 2020, 2:49 p.m. UTC | #3
On 6/24/20 4:21 AM, Chengguang Xu wrote:
> Release bip using kfree() in error path when that was allocated
> by kmalloc().

Applied, thanks.

Patch
diff mbox series

diff --git a/block/bio-integrity.c b/block/bio-integrity.c
index bf62c25cde8f..1d173feb3883 100644
--- a/block/bio-integrity.c
+++ b/block/bio-integrity.c
@@ -24,6 +24,18 @@  void blk_flush_integrity(void)
 	flush_workqueue(kintegrityd_wq);
 }
 
+void __bio_integrity_free(struct bio_set *bs, struct bio_integrity_payload *bip)
+{
+	if (bs && mempool_initialized(&bs->bio_integrity_pool)) {
+		if (bip->bip_vec)
+			bvec_free(&bs->bvec_integrity_pool, bip->bip_vec,
+				  bip->bip_slab);
+		mempool_free(bip, &bs->bio_integrity_pool);
+	} else {
+		kfree(bip);
+	}
+}
+
 /**
  * bio_integrity_alloc - Allocate integrity payload and attach it to bio
  * @bio:	bio to attach integrity metadata to
@@ -75,7 +87,7 @@  struct bio_integrity_payload *bio_integrity_alloc(struct bio *bio,
 
 	return bip;
 err:
-	mempool_free(bip, &bs->bio_integrity_pool);
+	__bio_integrity_free(bs, bip);
 	return ERR_PTR(-ENOMEM);
 }
 EXPORT_SYMBOL(bio_integrity_alloc);
@@ -96,14 +108,7 @@  void bio_integrity_free(struct bio *bio)
 		kfree(page_address(bip->bip_vec->bv_page) +
 		      bip->bip_vec->bv_offset);
 
-	if (bs && mempool_initialized(&bs->bio_integrity_pool)) {
-		bvec_free(&bs->bvec_integrity_pool, bip->bip_vec, bip->bip_slab);
-
-		mempool_free(bip, &bs->bio_integrity_pool);
-	} else {
-		kfree(bip);
-	}
-
+	__bio_integrity_free(bs, bip);
 	bio->bi_integrity = NULL;
 	bio->bi_opf &= ~REQ_INTEGRITY;
 }