Re: [PATCH 4/4] bcache: Release the cache kobject when cache_alloc() fails
From: David Laight
Date: Tue Dec 01 2026 - 03:26:08 EST
On Tue, 1 Dec 2026 12:23:48 +0800
"Coly Li" <colyli@xxxxxxx> 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@xxxxxxx> 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