All of lore.kernel.org
 help / color / mirror / Atom feed
From: "Coly Li" <colyli@fygo.io>
To: "Wentao Liang" <vulab@iscas.ac.cn>
Cc: "Kent Overstreet" <kent.overstreet@linux.dev>,
	 <linux-bcache@vger.kernel.org>, <linux-kernel@vger.kernel.org>,
	 <stable@vger.kernel.org>
Subject: Re: [PATCH 4/4] bcache: Release the cache kobject when cache_alloc() fails
Date: Mon, 30 Nov 2026 23:08:20 +0800	[thread overview]
Message-ID: <aw1tfczkIurBvyEr@studio.local> (raw)
In-Reply-To: <20261103094627.1289665-1-vulab@iscas.ac.cn>

Hi Wentao,

Thanks for the fix. IMHO the fix is valid, but still add some comments
inline.

On Tue, Nov 03, 2026 at 09:46:27AM +0800, Wentao Liang wrote:
> cache_alloc() initializes ca->kobj with kobject_init(), which takes the

Which function does the above 'which' mean? cache_alloc() or
kobject_init()? If it is cache_alloc(), correct; if it is kboject_init()
mistake. I suggest you the make it more explict and clear.

> 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.

I suggest you to list the exact object name explicitly. 


> When one of the allocations in cache_alloc() fails, the function frees
> only the buffers it managed to allocate and returns an error.

I also suggest you to list the exact names of the 'buffers' explicitly.


> 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

Again, you should explicitly point out the reference of ca->kobj.


> 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.

Also should explain why "ca is leaked together with the super block folio
and the module reference".

Because when ca->kobj reference counter reaches 0 by the last
kobject_put(&ca->kobj), then its .release callback bch_cache_release()
is called.

If kobject_put(&ca->kobj) is skipped, then bch_cache_release() will
never be called, then "ca is leaked together with the super block folio
and the module reference".

BTW, if I remember correctly you are not the first one tried to fix this
but you are the firstly correctly understand how bch_cache_release()
works as a callback, and make me understand how to fix it.

> 
> 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.
> 

Again, I expect explict description how you fix the issue, not the
general words like "Put ca->kobj on that path". Normally the implicit
and general descriptions are LLM styles and should not be welcomed in
kernel bug fix commit log.


> 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);

For the above 2 lines, also kfree() and vfree() checks NULL inside,
normally I would suggest to check the following checks,
	if (ca->prio_buckets)
		kfree(ca->prio_buckets);
	if (ca->buckets)
		vfree(ca->buckets);
Then the extra function jumps to kfree() and vfree() can be avoided.

>  
> @@ -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.
> +	 */

The above code comments can be removed.


>  	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.
>  		 */

I feel the above code comments is mistaken and misleading. Maybe you
may consider something like this,

		/*
		 * ca->kobj was initialized by kobject_init() in cache_alloc(),
		 * so the initial reference o ca->kobj must be dropped when
		 * cache_alloc() fails. goto out will drop the initial reference
		 * by call kobject_put(&ca->kobj) and invoke ca->kobj release
		 * callback fucntion bch_cache_release(). This callback will
		 * free ca, release ca->bdev_file and ca->sb_disk.
		 */

For your reference.

> -		fput(bdev_file);
> -		return ret;
> +		goto out;
>  	}
>  
>  	if (kobject_add(&ca->kobj, bdev_kobj(file_bdev(bdev_file)), "bcache")) {

The patch does fix thing, just I want a few commit log, code and comments to be improved.

Thanks.

Coly Li

  reply	other threads:[~2026-11-30 15:08 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-03  9:46 [PATCH 4/4] bcache: Release the cache kobject when cache_alloc() fails Wentao Liang
2026-11-30 15:08 ` Coly Li [this message]
2026-11-30 18:25   ` David Laight
2026-12-01  4:23     ` Coly Li
2026-12-01  8:26       ` David Laight
2026-12-01 16:15         ` Coly Li

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=aw1tfczkIurBvyEr@studio.local \
    --to=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 \
    --cc=vulab@iscas.ac.cn \
    /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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.