mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Wentao Liang <vulab@iscas.ac.cn>
To: Coly Li <colyli@fygo.io>, Kent Overstreet <kent.overstreet@linux.dev>
Cc: linux-bcache@vger.kernel.org, linux-kernel@vger.kernel.org,
	Wentao Liang <vulab@iscas.ac.cn>,
	stable@vger.kernel.org
Subject: [PATCH RESEND] bcache: Release the cache kobject when cache_alloc() fails
Date: Tue,  3 Nov 2026 09:46:43 +0000	[thread overview]
Message-ID: <20261103094643.1289833-1-vulab@iscas.ac.cn> (raw)

cache_alloc() initializes ca->kobj with kobject_init(), which takes the
initial reference on the kobject, and takes a reference on the bcache
module.  The object is released later by bch_cache_release(), which also
drops the block device, puts the super block folio, frees ca and drops
the module reference.

When one of the allocations in cache_alloc() fails, the function frees
only the buffers it managed to allocate and returns an error.
register_cache() then only calls fput() on the block device and returns,
so the reference taken by kobject_init() is never dropped: ca is leaked
together with the super block folio and the module reference.  The
comment on that path still claims that ca->kobj is not initialized, but
kobject_init() is the first thing cache_alloc() does.

Put ca->kobj on that path so that bch_cache_release() runs and releases
everything.  bch_cache_release() frees ca->buckets and ca->prio_buckets
as well, so clear those pointers in cache_alloc() after freeing them, and
drop the module_put() from the error path because the module reference is
now released by bch_cache_release().  Skip the bucket page free in
bch_cache_release() when cache_alloc() failed before allocating them.

Fixes: cafe56359144 ("bcache: A block layer cache")
Cc: stable@vger.kernel.org
Assisted-by: DeepSeek-V4.1-Flash and OpenAI Codex
Signed-off-by: Wentao Liang <vulab@iscas.ac.cn>
---
 drivers/md/bcache/super.c | 22 ++++++++++++++--------
 1 file changed, 14 insertions(+), 8 deletions(-)

diff --git a/drivers/md/bcache/super.c b/drivers/md/bcache/super.c
index 97d9adb0bf96..5d4975873c2e 100644
--- a/drivers/md/bcache/super.c
+++ b/drivers/md/bcache/super.c
@@ -2221,7 +2221,9 @@ void bch_cache_release(struct kobject *kobj)
 		ca->set->cache = NULL;
 	}
 
-	free_pages((unsigned long) ca->disk_buckets, ilog2(meta_bucket_pages(&ca->sb)));
+	if (ca->disk_buckets)
+		free_pages((unsigned long) ca->disk_buckets,
+			   ilog2(meta_bucket_pages(&ca->sb)));
 	kfree(ca->prio_buckets);
 	vfree(ca->buckets);
 
@@ -2364,8 +2366,10 @@ static int cache_alloc(struct cache *ca)
 
 err_disk_buckets_alloc:
 	kfree(ca->prio_buckets);
+	ca->prio_buckets = NULL;
 err_prio_buckets_alloc:
 	vfree(ca->buckets);
+	ca->buckets = NULL;
 err_buckets_alloc:
 	free_heap(&ca->heap);
 err_heap_alloc:
@@ -2380,7 +2384,10 @@ static int cache_alloc(struct cache *ca)
 	free_fifo(&ca->free[RESERVE_BTREE]);
 err_btree_alloc:
 err_free:
-	module_put(THIS_MODULE);
+	/*
+	 * The module reference taken above is dropped by
+	 * bch_cache_release() when the caller puts ca->kobj.
+	 */
 	if (err)
 		pr_notice("error %pg: %s\n", ca->bdev, err);
 	return ret;
@@ -2408,13 +2415,12 @@ static int register_cache(struct cache_sb *sb, struct cache_sb_disk *sb_disk,
 			err = "cache_alloc(): unknown error";
 		pr_notice("error %pg: %s\n", file_bdev(bdev_file), err);
 		/*
-		 * If we failed here, it means ca->kobj is not initialized yet,
-		 * kobject_put() won't be called and there is no chance to
-		 * call fput() to bdev in bch_cache_release(). So
-		 * we explicitly call fput() on the block device here.
+		 * cache_alloc() has already released the buffers it
+		 * allocated, so go through out: to drop the reference
+		 * taken by kobject_init(); bch_cache_release() then frees
+		 * ca and drops the block device and the super block folio.
 		 */
-		fput(bdev_file);
-		return ret;
+		goto out;
 	}
 
 	if (kobject_add(&ca->kobj, bdev_kobj(file_bdev(bdev_file)), "bcache")) {
-- 
2.34.1


             reply	other threads:[~2026-11-03  9:46 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-03  9:46 Wentao Liang [this message]
2026-11-03 10:26 ` Krzysztof Kozlowski

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=20261103094643.1289833-1-vulab@iscas.ac.cn \
    --to=vulab@iscas.ac.cn \
    --cc=colyli@fygo.io \
    --cc=kent.overstreet@linux.dev \
    --cc=linux-bcache@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=stable@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®