linux-bcache.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] bcache: Fix memory leak when cache_alloc() return failed
@ 2021-09-11  8:08 Wu Bo
  0 siblings, 0 replies; only message in thread
From: Wu Bo @ 2021-09-11  8:08 UTC (permalink / raw)
  To: colyli, kent.overstreet; +Cc: linux-bcache, linux-kernel, linfeilong, wubo40

From: Wu Bo <wubo40@huawei.com>

If cache_alloc() get error when register a cache device,
the ca->kobj is not initialized, the bch_cache_release() no chance 
to be called. So "ca" object will not be released.

In addition, if register_cache_set() return failed 
when register a cache device, kobject_put(&ca->kobj) will be called 
and "ca" objects will be released in bch_cache_release() function. 
But pr_notice() called after kobject_put(&ca->kobj),
the "ca->cache_dev_name" access memory that has been freed.

Signed-off-by: Wu Bo <wubo40@huawei.com>
---
 drivers/md/bcache/super.c | 10 ++++++----
 1 file changed, 6 insertions(+), 4 deletions(-)

diff --git a/drivers/md/bcache/super.c b/drivers/md/bcache/super.c
index f2874c7..30569f4 100644
--- a/drivers/md/bcache/super.c
+++ b/drivers/md/bcache/super.c
@@ -2366,13 +2366,17 @@ static int register_cache(struct cache_sb *sb, struct cache_sb_disk *sb_disk,
 		 * explicitly call blkdev_put() here.
 		 */
 		blkdev_put(bdev, FMODE_READ|FMODE_WRITE|FMODE_EXCL);
+		if (ca->sb_disk)
+			put_page(virt_to_page(ca->sb_disk));
 		if (ret == -ENOMEM)
 			err = "cache_alloc(): -ENOMEM";
 		else if (ret == -EPERM)
 			err = "cache_alloc(): cache device is too small";
 		else
 			err = "cache_alloc(): unknown error";
-		goto err;
+		pr_notice("error %s: %s\n", ca->cache_dev_name, err);
+		kfree(ca);
+		return ret;
 	}
 
 	if (kobject_add(&ca->kobj, bdev_kobj(bdev), "bcache")) {
@@ -2393,11 +2397,9 @@ static int register_cache(struct cache_sb *sb, struct cache_sb_disk *sb_disk,
 	pr_info("registered cache device %s\n", ca->cache_dev_name);
 
 out:
-	kobject_put(&ca->kobj);
-
-err:
 	if (err)
 		pr_notice("error %s: %s\n", ca->cache_dev_name, err);
+	kobject_put(&ca->kobj);
 
 	return ret;
 }
-- 
1.8.3.1


^ permalink raw reply related	[flat|nested] only message in thread

only message in thread, other threads:[~2021-09-11  7:39 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-09-11  8:08 [PATCH] bcache: Fix memory leak when cache_alloc() return failed Wu Bo

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).