mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Wu Bo <wubo40@huawei.com>
To: <colyli@suse.de>, <kent.overstreet@gmail.com>
Cc: <linux-bcache@vger.kernel.org>, <linux-kernel@vger.kernel.org>,
	<linfeilong@huawei.com>, <wubo40@huawei.com>
Subject: [PATCH] bcache: Fix memory leak when cache_alloc() return failed
Date: Sat, 11 Sep 2021 16:08:54 +0800	[thread overview]
Message-ID: <1631347734-9950-1-git-send-email-wubo40@huawei.com> (raw)

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


                 reply	other threads:[~2021-09-11  7:39 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1631347734-9950-1-git-send-email-wubo40@huawei.com \
    --to=wubo40@huawei.com \
    --cc=colyli@suse.de \
    --cc=kent.overstreet@gmail.com \
    --cc=linfeilong@huawei.com \
    --cc=linux-bcache@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®