Linux kernel -stable discussions
 help / color / mirror / Atom feed
* [PATCH 4/4] bcache: Release the cache kobject when cache_alloc() fails
@ 2026-11-03  9:46 Wentao Liang
  2026-11-30 15:08 ` Coly Li
  0 siblings, 1 reply; 5+ messages in thread
From: Wentao Liang @ 2026-11-03  9:46 UTC (permalink / raw)
  To: Coly Li, Kent Overstreet; +Cc: linux-bcache, linux-kernel, Wentao Liang, stable

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


^ permalink raw reply related	[flat|nested] 5+ messages in thread

* Re: [PATCH 4/4] bcache: Release the cache kobject when cache_alloc() fails
  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
  2026-11-30 18:25   ` David Laight
  0 siblings, 1 reply; 5+ messages in thread
From: Coly Li @ 2026-11-30 15:08 UTC (permalink / raw)
  To: Wentao Liang; +Cc: Kent Overstreet, linux-bcache, linux-kernel, stable

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

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH 4/4] bcache: Release the cache kobject when cache_alloc() fails
  2026-11-30 15:08 ` Coly Li
@ 2026-11-30 18:25   ` David Laight
  2026-12-01  4:23     ` Coly Li
  0 siblings, 1 reply; 5+ messages in thread
From: David Laight @ 2026-11-30 18:25 UTC (permalink / raw)
  To: Coly Li; +Cc: Wentao Liang, Kent Overstreet, linux-bcache, linux-kernel, stable

On Mon, 30 Nov 2026 23:08:20 +0800
"Coly Li" <colyli@fygo.io> wrote:

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

I'd only do that if the common path has NULL pointers.
Otherwise you are optimising for something that doesn't happen and
stand a moderate chance of a mispredicted branch (and likely() may not
help) - that might actually cost 40 clocks.
With luck (and a following wind) the test inside kfree() will be
predicted for 'not NULL' - so you only get the penalty in a (usually)
error path that passes NULL (you might get a mispredict on the next
non-NULL call, but that is really part of the penalty for the NULL).

David

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH 4/4] bcache: Release the cache kobject when cache_alloc() fails
  2026-11-30 18:25   ` David Laight
@ 2026-12-01  4:23     ` Coly Li
  2026-12-01  8:26       ` David Laight
  0 siblings, 1 reply; 5+ messages in thread
From: Coly Li @ 2026-12-01  4:23 UTC (permalink / raw)
  To: David Laight
  Cc: Wentao Liang, Kent Overstreet, linux-bcache, linux-kernel, stable

On Mon, Nov 30, 2026 at 06:25:15PM +0800, David Laight wrote:
> On Mon, 30 Nov 2026 23:08:20 +0800
> "Coly Li" <colyli@fygo.io> wrote:
> 
> > >  	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.
> 
> I'd only do that if the common path has NULL pointers.

This is not a common path, it is on failure handling or cache stop
code path. So it should be executed once for days or months, even for
frequent failures, it is once for several seconds. The code path is
definitely cold, and branch prediction contributes little here.

> Otherwise you are optimising for something that doesn't happen and
> stand a moderate chance of a mispredicted branch (and likely() may not
> help) - that might actually cost 40 clocks.

Yes, I see the cost of mispredict. But I doubt it may happen here.
This is a very cold code path, for this specific condition, considering
kfree() and vfree() will be prefetched while the if-check, so extra cost
of the extra if-check can be almostly ignorable (0~3 cpu cycles).

> With luck (and a following wind) the test inside kfree() will be
> predicted for 'not NULL' - so you only get the penalty in a (usually)
> error path that passes NULL (you might get a mispredict on the next
> non-NULL call, but that is really part of the penalty for the NULL).

In such cold code path, if ca->prio_buckets is NULL, the cost of branch
mispredict is just short jmp and prefetch kfree().
	if (ca->prio_buckets)
		kfree(ca->prio_buckets);
Without the above if-check, directly call kfree() for a NULL pointer
ca->prio_buckets, will do a long jump into kfree() then a long jump back
with a false if-check for the NULL pointer inside kfree(). This is more
expensive IMHO. I guess x2 or x3 more.

In common cases, your opinion works well. But this is very cold code path,
the pipeline of speculated execution is quite short for branch prediction.
False condition in if-check doesn't have so much penalty as hot code path
does.

Coly Li

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH 4/4] bcache: Release the cache kobject when cache_alloc() fails
  2026-12-01  4:23     ` Coly Li
@ 2026-12-01  8:26       ` David Laight
  0 siblings, 0 replies; 5+ messages in thread
From: David Laight @ 2026-12-01  8:26 UTC (permalink / raw)
  To: Coly Li; +Cc: Wentao Liang, Kent Overstreet, linux-bcache, linux-kernel, stable

On Tue, 1 Dec 2026 12:23:48 +0800
"Coly Li" <colyli@fygo.io> wrote:

> On Mon, Nov 30, 2026 at 06:25:15PM +0800, David Laight wrote:
> > On Mon, 30 Nov 2026 23:08:20 +0800
> > "Coly Li" <colyli@fygo.io> wrote:
> >   
> > > >  	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.  
> > 
> > I'd only do that if the common path has NULL pointers.  
> 
> This is not a common path, it is on failure handling or cache stop
> code path. So it should be executed once for days or months, even for
> frequent failures, it is once for several seconds. The code path is
> definitely cold, and branch prediction contributes little here.
> 
> > Otherwise you are optimising for something that doesn't happen and
> > stand a moderate chance of a mispredicted branch (and likely() may not
> > help) - that might actually cost 40 clocks.  
> 
> Yes, I see the cost of mispredict. But I doubt it may happen here.
> This is a very cold code path, for this specific condition, considering
> kfree() and vfree() will be prefetched while the if-check, so extra cost
> of the extra if-check can be almostly ignorable (0~3 cpu cycles).
> 
> > With luck (and a following wind) the test inside kfree() will be
> > predicted for 'not NULL' - so you only get the penalty in a (usually)
> > error path that passes NULL (you might get a mispredict on the next
> > non-NULL call, but that is really part of the penalty for the NULL).  
> 
> In such cold code path, if ca->prio_buckets is NULL, the cost of branch
> mispredict is just short jmp and prefetch kfree().
> 	if (ca->prio_buckets)
> 		kfree(ca->prio_buckets);
> Without the above if-check, directly call kfree() for a NULL pointer
> ca->prio_buckets, will do a long jump into kfree() then a long jump back
> with a false if-check for the NULL pointer inside kfree(). This is more
> expensive IMHO. I guess x2 or x3 more.
> 
> In common cases, your opinion works well. But this is very cold code path,
> the pipeline of speculated execution is quite short for branch prediction.
> False condition in if-check doesn't have so much penalty as hot code path
> does.

Right, so code size wins and you don't have the extra conditional.

David

> 
> Coly Li


^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2026-12-01  8:26 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
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
2026-11-30 18:25   ` David Laight
2026-12-01  4:23     ` Coly Li
2026-12-01  8:26       ` David Laight

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox