mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: "Coly Li" <colyli@fygo.io>
To: "David Laight" <david.laight.linux@gmail.com>
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 12:23:48 +0800	[thread overview]
Message-ID: <aw5FNwRfcGa0PI1u@studio.local> (raw)
In-Reply-To: <20261130182515.5a196fe3@pumpkin>

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

  reply	other threads:[~2026-12-01  4:24 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-03  9:46 Wentao Liang
2026-11-30 15:08 ` Coly Li
2026-11-30 18:25   ` David Laight
2026-12-01  4:23     ` Coly Li [this message]
2026-12-01  8:26       ` David Laight

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=aw5FNwRfcGa0PI1u@studio.local \
    --to=colyli@fygo.io \
    --cc=david.laight.linux@gmail.com \
    --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

all inboxes | Powered by JetHome®