Skip to contentSkip to conversation

block/bio-integrity: fix mismatched alloc free

PATCH In review Pan Bian 2 participants 3 messages Aug 19, 2019

Subsystems (2)
lore
Share & tools
lore

Pan Bian

author
#0 lore raw

PATCH v2 1/1 block/bio-integrity: fix mismatched alloc free

The function kmalloc rather than mempool_alloc is called to allocate memory when the memory pool is unavailable. However, mempool_alloc is used to release the memory chunck in both cases when error occurs. This patch fixes the bug.

Fixes 9f060e2231c ("block: Convert integrity to bvec_alloc_bs()")

---
V2: add Fixes and CC tags

---
 block/bio-integrity.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
block/bio-integrity.c +4 -1
index fb95dbb..011dfc8 100644
@@ -75,7 +75,10 @@ struct bio_integrity_payload *bio_integrity_alloc(struct bio *bio,
 
 	return bip;
 err:
-	mempool_free(bip, &bs->bio_integrity_pool);
+	if (!bs || !mempool_initialized(&bs->bio_integrity_pool))
+		kfree(bip);
+	else
+		mempool_free(bip, &bs->bio_integrity_pool);
 	return ERR_PTR(-ENOMEM);
 }
 EXPORT_SYMBOL(bio_integrity_alloc);
Signature · 1 line
2.7.4
#1 lore raw
Quoted 20 lines · Pan Bian

The function kmalloc rather than mempool_alloc is called to allocate memory when the memory pool is unavailable. However, mempool_alloc is used to release the memory chunck in both cases when error occurs. This patch fixes the bug.

Fixes 9f060e2231c ("block: Convert integrity to bvec_alloc_bs()")

---
V2: add Fixes and CC tags

---
 block/bio-integrity.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
block/bio-integrity.c +0 -0
index fb95dbb..011dfc8 100644
@@ -75,7 +75,10 @@ struct bio_integrity_payload *bio_integrity_alloc(struct bio *bio,
 

block/bio-integrity.c struct bio_integrity_payload *bio_integrity_alloc(struct bio *bio, · line 84

Quoted 10 lines
block/bio-integrity.c +4 -1
 	return bip;
 err:
-	mempool_free(bip, &bs->bio_integrity_pool);
+	if (!bs || !mempool_initialized(&bs->bio_integrity_pool))
+		kfree(bip);
+	else
+		mempool_free(bip, &bs->bio_integrity_pool);
 	return ERR_PTR(-ENOMEM);
 }
 EXPORT_SYMBOL(bio_integrity_alloc);

'err' is still reached in case that 'bs' is valid, so fix nothing.

Thanks,
Ming

author
#2 lore raw
Quoted 21 lines · Ming Lei
Quoted 20 lines · Pan Bian

The function kmalloc rather than mempool_alloc is called to allocate memory when the memory pool is unavailable. However, mempool_alloc is used to release the memory chunck in both cases when error occurs. This patch fixes the bug.

Fixes 9f060e2231c ("block: Convert integrity to bvec_alloc_bs()")

---
V2: add Fixes and CC tags

---
 block/bio-integrity.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
block/bio-integrity.c +0 -0
index fb95dbb..011dfc8 100644
@@ -75,7 +75,10 @@ struct bio_integrity_payload *bio_integrity_alloc(struct bio *bio,
 
Quoted 12 lines

block/bio-integrity.c struct bio_integrity_payload *bio_integrity_alloc(struct bio *bio, · line 84

Quoted 10 lines
block/bio-integrity.c +4 -1
 	return bip;
 err:
-	mempool_free(bip, &bs->bio_integrity_pool);
+	if (!bs || !mempool_initialized(&bs->bio_integrity_pool))
+		kfree(bip);
+	else
+		mempool_free(bip, &bs->bio_integrity_pool);
 	return ERR_PTR(-ENOMEM);
 }
 EXPORT_SYMBOL(bio_integrity_alloc);

'err' is still reached in case that 'bs' is valid, so fix nothing.

You are right! It's my fault.

Thanks,
Pan

Quoted 4 lines

Thanks,
Ming