[PATCH 2/2] fix potential memory leak in function "register_bcache"

From: Dongbo Cao
Date: Wed Aug 15 2018 - 03:06:06 EST


if register_cache get error, memory pointed by ca should be released.
and also, blkdev_put should be called I think.

Signed-off-by: Dongbo Cao <cdbdyx@xxxxxxx>
---
drivers/md/bcache/super.c | 7 +++++--
1 file changed, 5 insertions(+), 2 deletions(-)

diff --git a/drivers/md/bcache/super.c b/drivers/md/bcache/super.c
index f98352d4..14c31e03 100644
--- a/drivers/md/bcache/super.c
+++ b/drivers/md/bcache/super.c
@@ -2159,6 +2159,7 @@ static ssize_t register_bcache(struct kobject *k, struct kobj_attribute *attr,
struct cache_sb *sb = NULL;
struct block_device *bdev = NULL;
struct page *sb_page = NULL;
+ struct cache *ca = NULL;

if (!try_module_get(THIS_MODULE))
return -EBUSY;
@@ -2206,12 +2207,12 @@ static ssize_t register_bcache(struct kobject *k, struct kobj_attribute *attr,
register_bdev(sb, sb_page, bdev, dc);
mutex_unlock(&bch_register_lock);
} else {
- struct cache *ca = kzalloc(sizeof(*ca), GFP_KERNEL);
+ ca = kzalloc(sizeof(struct cache), GFP_KERNEL);
if (!ca)
goto err_close;

if (register_cache(sb, sb_page, bdev, ca) != 0)
- goto err;
+ goto err_register;
}
out:
if (sb_page)
@@ -2221,6 +2222,8 @@ static ssize_t register_bcache(struct kobject *k, struct kobj_attribute *attr,
module_put(THIS_MODULE);
return ret;

+err_register:
+ kfree(ca);
err_close:
blkdev_put(bdev, FMODE_READ|FMODE_WRITE|FMODE_EXCL);
err:
--
2.17.1