Re: [RFC] making nested spin_trylock() work on UP?
From: Harry Yoo (Oracle)
Date: Thu Apr 16 2026 - 22:38:41 EST
On Thu, Apr 16, 2026 at 07:37:49AM -0700, Alexei Starovoitov wrote:
> On Thu, Apr 16, 2026 at 7:35 AM Harry Yoo (Oracle) <harry@xxxxxxxxxx> wrote:
> >
> > On Thu, Apr 16, 2026 at 07:26:36AM -0700, Alexei Starovoitov wrote:
> > > On Thu Apr 16, 2026 at 3:05 AM PDT, Vlastimil Babka (SUSE) wrote:
> > > >> I think we need a special spinlock type that wraps something like this
> > > >> and use them when spinlocks can be trylock'd in an unknown context:
> > > >> pcp lock, zone lock, per-node partial slab list lock,
> > > >> per-node barn lock, etc.
> > > >
> > > > Soudns like a lot of hassle for a niche config (SMP=n) where nobody would
> > > > use e.g. bpf tracing anyway. We already have this in kmalloc_nolock():
> > > >
> > > > /*
> > > > * See the comment for the same check in
> > > > * alloc_frozen_pages_nolock_noprof()
> > > > */
> > > > if (IS_ENABLED(CONFIG_PREEMPT_RT) && (in_nmi() || in_hardirq()))
> > > > return NULL;
> > > >
> > > > It would be trivial to extend this to !SMP. However it wouldn't cover the
> > > > kprobe context. Any idea Alexei?
> >
> > I think Vlastimil meant it'd be trivial to do:
> >
> > if ((IS_ENABLED(CONFIG_PREEMPT_RT) || !IS_ENABLED(CONFIG_SMP))
> > && (in_nmi() || in_hardirq()))
> > return NULL;
>
> This one.
Thanks for clarifying. You mean not covering the kprobe context is fine.
But I have to ask; how is that fine? Wouldn't this leave a small
possibility for a kmalloc_nolock() caller to trigger
e.g.) use-after-free bug even without noticing? (yeah, very unlikely
for somebody to trigger in practice, but not impossible)
If it's unlikely to use bpf tracing on UP anyway, it'd be safer to just
disallow that to happen to begin with.
> > But it doesn't cover the case where kprobe hooks an arbitrary function
> > (in the middle of kmalloc() or kfree()) and calls kmalloc_nolock()?
> >
> > > Yeah. Totally fine with that.
> >
> > So I'm confused exactly what you're fine with. Did you mean this?
> >
> > if (!IS_ENABLED(CONFIG_SMP) ||
> > (IS_ENABLED(CONFIG_PREEMPT_RT) && (in_nmi() || in_hardirq())))
> > return NULL;
>
> Doesn't need to be that drastic.
--
Cheers,
Harry / Hyeonggon