The Linux Kernel Mailing List
 help / color / mirror / Atom feed
From: David Laight <david.laight.linux@gmail.com>
To: "Coly Li" <colyli@fygo.io>
Cc: "Wentao Liang" <vulab@iscas.ac.cn>,
	"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: Tue, 1 Dec 2026 08:26:02 +0000	[thread overview]
Message-ID: <20261201082602.1fcb676d@pumpkin> (raw)
In-Reply-To: <aw5FNwRfcGa0PI1u@studio.local>

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


      reply	other threads:[~2026-12-01  8:26 UTC|newest]

Thread overview: 5+ 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
2026-11-30 18:25   ` David Laight
2026-12-01  4:23     ` Coly Li
2026-12-01  8:26       ` David Laight [this message]

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=20261201082602.1fcb676d@pumpkin \
    --to=david.laight.linux@gmail.com \
    --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 \
    --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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox