* Re: [PATCH bpf-next v1 1/4] bpf: Add call for selecting kmalloc or kmalloc_nolock at call time
[not found] ` <DN38HGVL8JHQ.2ZVZQCKFXVA9T@gmail.com>
@ 2026-11-24 13:59 ` Harry Yoo
2026-11-24 18:07 ` Emil Tsalapatis
` (2 more replies)
0 siblings, 3 replies; 14+ messages in thread
From: Harry Yoo @ 2026-11-24 13:59 UTC (permalink / raw)
To: Alexei Starovoitov
Cc: Emil Tsalapatis, bpf, andrii, eddyz87, memxor, daniel,
Sebastian Andrzej Siewior, Clark Williams, Steven Rostedt,
linux-rt-devel, Vlastimil Babka, Hao Li, Andrew Morton,
Suren Baghdasaryan, Michal Hocko, Brendan Jackman,
Johannes Weiner, Zi Yan, linux-mm, David Hildenbrand,
Peter Zijlstra, Ingo Molnar, Will Deacon, Boqun Feng, Waiman Long,
linux-kernel
[+Cc Relevant folks]
And a bit of context on the underlying problem.
kmalloc_nolock() could fail when there is a contention on locks
shared across CPUs (e.g., n->list_lock in slab, zone->lock in buddy),
or there is a decent amount of memory pressure. It's more restrictive
than GFP_ATOMIC.
Unconditionally using kmalloc_nolock() in BPF local storage is
unnecessarily restrictive when the program can sleep or spin, and
Emil's patchset teaches the local storage whether the BPF program
accessing the local storage could sleep or spin on a lock.
Answering the question "Does this context allow sleeping, or spinning
on a lock" is extremely tricky in the Linux kernel as BPF programs
could run and call kfuncs almost at any point in the kernel code and
the "context" is usually encoded in the GFP flags or the process flags.
Tracking whether certain locks are taken by current context is not
ideal either as it adds unnecessary overhead for everybody.
On Tue, Nov 24, 2026 at 06:40:11AM +0000, Alexei Starovoitov wrote:
> On Mon, Nov 23, 2026 at 10:03 PM Emil Tsalapatis <emil@etsalapatis.com> wrote:
> > + /* Only try the spinning allocation when preemption disables are tracked. */
> > + if (IS_ENABLED(CONFIG_PREEMPT_COUNT) && can_spin_in_alloc() &&
> > + size <= KMALLOC_MAX_CACHE_SIZE && !(flags & ~nolock_flags))
> > + ptr = kmalloc(size, flags | GFP_NOWAIT | __GFP_NOMEMALLOC);
>
> It works, but I after a bit of brainstorming the following
> might be a better fix, since it will address the root cause:
> Pls run it by Vlastimil, Harry, Sebastian.
>
> diff --git a/mm/page_alloc.c b/mm/page_alloc.c
> index 2fef409..4368ab5 100644
> --- a/mm/page_alloc.c
> +++ b/mm/page_alloc.c
> @@ -2536,6 +2536,24 @@ __rmqueue(struct zone *zone, unsigned int order, int migratetype,
> return NULL;
> }
>
> +static DEFINE_WAIT_OVERRIDE_MAP(zone_nolock_map, LD_WAIT_CONFIG);
> +
> +/*
> + * zone->lock is always held with IRQs disabled. A caller that runs with IRQs
> + * enabled is not nested inside of it, so it can wait for the owner.
> + */
> +static bool zone_lock_nolock(struct zone *zone, unsigned long *flags)
> +{
> + if (IS_ENABLED(CONFIG_PREEMPT_RT) || irqs_disabled() || in_nmi())
> + return spin_trylock_irqsave(&zone->lock, *flags);
> +
> + /* The caller may hold a raw_spinlock_t. */
> + lock_map_acquire_try(&zone_nolock_map);
> + spin_lock_irqsave(&zone->lock, *flags);
> + lock_map_release(&zone_nolock_map);
> + return true;
> +}
I'm not sure this is a better solution unless this could be generalized
or abstracted further.
> /*
> * Obtain a specified number of elements from the buddy allocator, all under
> * a single hold of the lock, for efficiency. Add them to the supplied list.
> @@ -2550,7 +2568,7 @@ static int rmqueue_bulk(struct zone *zone, unsigned int order,
> int i;
>
> if (unlikely(alloc_flags & ALLOC_NOLOCK)) {
> - if (!spin_trylock_irqsave(&zone->lock, flags))
> + if (!zone_lock_nolock(zone, &flags))
> return 0;
> } else {
> spin_lock_irqsave(&zone->lock, flags);
> @@ -3237,7 +3255,7 @@ struct page *rmqueue_buddy(struct zone *preferred_zone, struct zone *zone,
> do {
> page = NULL;
> if (unlikely(alloc_flags & ALLOC_NOLOCK)) {
> - if (!spin_trylock_irqsave(&zone->lock, flags))
> + if (!zone_lock_nolock(zone, &flags))
> return NULL;
> } else {
> spin_lock_irqsave(&zone->lock, flags);
>
> pw-bot: cr
--
Cheers,
Harry / Hyeonggon
^ permalink raw reply [flat|nested] 14+ messages in thread
* Re: [PATCH bpf-next v1 1/4] bpf: Add call for selecting kmalloc or kmalloc_nolock at call time
2026-11-24 13:59 ` [PATCH bpf-next v1 1/4] bpf: Add call for selecting kmalloc or kmalloc_nolock at call time Harry Yoo
@ 2026-11-24 18:07 ` Emil Tsalapatis
2026-11-25 4:03 ` Alexei Starovoitov
2026-11-25 14:09 ` Peter Zijlstra
2 siblings, 0 replies; 14+ messages in thread
From: Emil Tsalapatis @ 2026-11-24 18:07 UTC (permalink / raw)
To: Harry Yoo, Alexei Starovoitov
Cc: Emil Tsalapatis, bpf, andrii, eddyz87, memxor, daniel,
Sebastian Andrzej Siewior, Clark Williams, Steven Rostedt,
linux-rt-devel, Vlastimil Babka, Hao Li, Andrew Morton,
Suren Baghdasaryan, Michal Hocko, Brendan Jackman,
Johannes Weiner, Zi Yan, linux-mm, David Hildenbrand,
Peter Zijlstra, Ingo Molnar, Will Deacon, Boqun Feng, Waiman Long,
linux-kernel
On Tue Nov 24, 2026 at 1:59 PM UTC, Harry Yoo wrote:
> [+Cc Relevant folks]
>
> And a bit of context on the underlying problem.
>
> kmalloc_nolock() could fail when there is a contention on locks
> shared across CPUs (e.g., n->list_lock in slab, zone->lock in buddy),
> or there is a decent amount of memory pressure. It's more restrictive
> than GFP_ATOMIC.
>
> Unconditionally using kmalloc_nolock() in BPF local storage is
> unnecessarily restrictive when the program can sleep or spin, and
> Emil's patchset teaches the local storage whether the BPF program
> accessing the local storage could sleep or spin on a lock.
>
> Answering the question "Does this context allow sleeping, or spinning
> on a lock" is extremely tricky in the Linux kernel as BPF programs
> could run and call kfuncs almost at any point in the kernel code and
> the "context" is usually encoded in the GFP flags or the process flags.
>
> Tracking whether certain locks are taken by current context is not
> ideal either as it adds unnecessary overhead for everybody.
Yes, we considered the page_alloc approach with Harry offlist and I think what
it comes down to is that one approach is the most turnkey for users while
the other has the clearest semantics. I went with the latter because
making the selection straight in the BPF code makes the behavior of the
top-level local storage alloc call ends up easier to understand and
explicit in the BPF subsystem.
>
> On Tue, Nov 24, 2026 at 06:40:11AM +0000, Alexei Starovoitov wrote:
>> On Mon, Nov 23, 2026 at 10:03 PM Emil Tsalapatis <emil@etsalapatis.com> wrote:
>> > + /* Only try the spinning allocation when preemption disables are tracked. */
>> > + if (IS_ENABLED(CONFIG_PREEMPT_COUNT) && can_spin_in_alloc() &&
>> > + size <= KMALLOC_MAX_CACHE_SIZE && !(flags & ~nolock_flags))
>> > + ptr = kmalloc(size, flags | GFP_NOWAIT | __GFP_NOMEMALLOC);
>>
>> It works, but I after a bit of brainstorming the following
>> might be a better fix, since it will address the root cause:
>> Pls run it by Vlastimil, Harry, Sebastian.
>>
>> diff --git a/mm/page_alloc.c b/mm/page_alloc.c
>> index 2fef409..4368ab5 100644
>> --- a/mm/page_alloc.c
>> +++ b/mm/page_alloc.c
>> @@ -2536,6 +2536,24 @@ __rmqueue(struct zone *zone, unsigned int order, int migratetype,
>> return NULL;
>> }
>>
>> +static DEFINE_WAIT_OVERRIDE_MAP(zone_nolock_map, LD_WAIT_CONFIG);
>> +
>> +/*
>> + * zone->lock is always held with IRQs disabled. A caller that runs with IRQs
>> + * enabled is not nested inside of it, so it can wait for the owner.
>> + */
>> +static bool zone_lock_nolock(struct zone *zone, unsigned long *flags)
>> +{
>> + if (IS_ENABLED(CONFIG_PREEMPT_RT) || irqs_disabled() || in_nmi())
>> + return spin_trylock_irqsave(&zone->lock, *flags);
>> +
>> + /* The caller may hold a raw_spinlock_t. */
>> + lock_map_acquire_try(&zone_nolock_map);
>> + spin_lock_irqsave(&zone->lock, *flags);
>> + lock_map_release(&zone_nolock_map);
>> + return true;
>> +}
>
> I'm not sure this is a better solution unless this could be generalized
> or abstracted further.
>
>> /*
>> * Obtain a specified number of elements from the buddy allocator, all under
>> * a single hold of the lock, for efficiency. Add them to the supplied list.
>> @@ -2550,7 +2568,7 @@ static int rmqueue_bulk(struct zone *zone, unsigned int order,
>> int i;
>>
>> if (unlikely(alloc_flags & ALLOC_NOLOCK)) {
>> - if (!spin_trylock_irqsave(&zone->lock, flags))
>> + if (!zone_lock_nolock(zone, &flags))
>> return 0;
>> } else {
>> spin_lock_irqsave(&zone->lock, flags);
>> @@ -3237,7 +3255,7 @@ struct page *rmqueue_buddy(struct zone *preferred_zone, struct zone *zone,
>> do {
>> page = NULL;
>> if (unlikely(alloc_flags & ALLOC_NOLOCK)) {
>> - if (!spin_trylock_irqsave(&zone->lock, flags))
>> + if (!zone_lock_nolock(zone, &flags))
>> return NULL;
>> } else {
>> spin_lock_irqsave(&zone->lock, flags);
>>
>> pw-bot: cr
^ permalink raw reply [flat|nested] 14+ messages in thread
* Re: [PATCH bpf-next v1 1/4] bpf: Add call for selecting kmalloc or kmalloc_nolock at call time
2026-11-24 13:59 ` [PATCH bpf-next v1 1/4] bpf: Add call for selecting kmalloc or kmalloc_nolock at call time Harry Yoo
2026-11-24 18:07 ` Emil Tsalapatis
@ 2026-11-25 4:03 ` Alexei Starovoitov
2026-11-25 14:09 ` Peter Zijlstra
2 siblings, 0 replies; 14+ messages in thread
From: Alexei Starovoitov @ 2026-11-25 4:03 UTC (permalink / raw)
To: Harry Yoo
Cc: Emil Tsalapatis, bpf, andrii, eddyz87, memxor, daniel,
Sebastian Andrzej Siewior, Clark Williams, Steven Rostedt,
linux-rt-devel, Vlastimil Babka, Hao Li, Andrew Morton,
Suren Baghdasaryan, Michal Hocko, Brendan Jackman,
Johannes Weiner, Zi Yan, linux-mm, David Hildenbrand,
Peter Zijlstra, Ingo Molnar, Will Deacon, Boqun Feng, Waiman Long,
linux-kernel
On Tue, Nov 24, 2026 at 05:59 AM Harry Yoo <harry@kernel.org> wrote:
>> + /* The caller may hold a raw_spinlock_t. */
>> + lock_map_acquire_try(&zone_nolock_map);
>> + spin_lock_irqsave(&zone->lock, *flags);
>> + lock_map_release(&zone_nolock_map);
>> + return true;
>> +}
>
> I'm not sure this is a better solution unless this could be generalized
> or abstracted further.
Alright. Here is generalized one.
diff --git a/mm/internal.h b/mm/internal.h
index 85cf7be..1de6292 100644
--- a/mm/internal.h
+++ b/mm/internal.h
@@ -1688,6 +1688,44 @@ static inline bool can_spin_trylock(void)
return true;
}
+/*
+ * An IRQ handler can allocate at any point where IRQs are enabled, so a
+ * _nolock() caller can wait there for a lock that is IRQ-safe.
+ */
+static inline bool nolock_can_wait(void)
+{
+ return !IS_ENABLED(CONFIG_PREEMPT_RT) && !irqs_disabled() && !in_nmi();
+}
+
+/* @lock must always be held with IRQs disabled. */
+static inline bool spin_lock_nolock(spinlock_t *lock, unsigned long *flags)
+ __cond_acquires(true, lock)
+{
+ if (!nolock_can_wait())
+ return spin_trylock_irqsave(lock, *flags);
+
+ spin_lock_irqsave(lock, *flags);
+ return true;
+}
+
+/*
+ * The caller may hold a raw_spinlock_t. The map has to cover the critical
+ * sections of spin_lock_nolock() up to the unlock.
+ */
+extern struct lockdep_map nolock_wait_map;
+
+static inline void nolock_wait_begin(void)
+{
+ if (IS_ENABLED(CONFIG_PROVE_RAW_LOCK_NESTING) && nolock_can_wait())
+ lock_map_acquire_try(&nolock_wait_map);
+}
+
+static inline void nolock_wait_end(void)
+{
+ if (IS_ENABLED(CONFIG_PROVE_RAW_LOCK_NESTING) && nolock_can_wait())
+ lock_map_release(&nolock_wait_map);
+}
+
/* char-mem.c */
bool file_is_dev_zero(const struct file *file);
diff --git a/mm/page_alloc.c b/mm/page_alloc.c
index 2fef409..f22ae49 100644
--- a/mm/page_alloc.c
+++ b/mm/page_alloc.c
@@ -2550,7 +2550,7 @@ static int rmqueue_bulk(struct zone *zone, unsigned int order,
int i;
if (unlikely(alloc_flags & ALLOC_NOLOCK)) {
- if (!spin_trylock_irqsave(&zone->lock, flags))
+ if (!spin_lock_nolock(&zone->lock, &flags))
return 0;
} else {
spin_lock_irqsave(&zone->lock, flags);
@@ -3237,7 +3237,7 @@ struct page *rmqueue_buddy(struct zone *preferred_zone, struct zone *zone,
do {
page = NULL;
if (unlikely(alloc_flags & ALLOC_NOLOCK)) {
- if (!spin_trylock_irqsave(&zone->lock, flags))
+ if (!spin_lock_nolock(&zone->lock, &flags))
return NULL;
} else {
spin_lock_irqsave(&zone->lock, flags);
@@ -8115,12 +8115,19 @@ static bool __free_unaccepted(struct page *page)
#endif /* CONFIG_UNACCEPTED_MEMORY */
+DEFINE_WAIT_OVERRIDE_MAP(nolock_wait_map, LD_WAIT_CONFIG);
+
struct page *alloc_frozen_pages_nolock_noprof(gfp_t gfp_flags, int nid, unsigned int order)
{
+ struct page *page;
+
if (nid == NUMA_NO_NODE)
nid = numa_node_id();
- return __alloc_frozen_pages_noprof(gfp_flags, order, nid, NULL, ALLOC_NOLOCK);
+ nolock_wait_begin();
+ page = __alloc_frozen_pages_noprof(gfp_flags, order, nid, NULL, ALLOC_NOLOCK);
+ nolock_wait_end();
+ return page;
}
/**
* alloc_pages_nolock - opportunistic reentrant allocation from any context
diff --git a/mm/slub.c b/mm/slub.c
index 5c33d12..5bd00d1 100644
--- a/mm/slub.c
+++ b/mm/slub.c
@@ -3821,7 +3821,7 @@ static void *alloc_single_from_new_slab(struct kmem_cache *s, struct slab *slab,
n = get_node(s, slab_nid(slab));
if (allow_spin) {
spin_lock_irqsave(&n->list_lock, flags);
- } else if (!spin_trylock_irqsave(&n->list_lock, flags)) {
+ } else if (!spin_lock_nolock(&n->list_lock, &flags)) {
/*
* Unlucky, discard newly allocated slab.
* The slab is not fully free, but it's fine as
@@ -3939,7 +3939,7 @@ static void *get_from_partial_node(struct kmem_cache *s,
if (alloc_flags_allow_spinning(ac->alloc_flags))
spin_lock_irqsave(&n->list_lock, flags);
- else if (!spin_trylock_irqsave(&n->list_lock, flags))
+ else if (!spin_lock_nolock(&n->list_lock, &flags))
return NULL;
list_for_each_entry_safe(slab, slab2, &n->partial, slab_list) {
@@ -4543,7 +4543,7 @@ static unsigned int alloc_from_new_slab(struct kmem_cache *s, struct slab *slab,
if (allow_spin) {
spin_lock_irqsave(&n->list_lock, flags);
- } else if (!spin_trylock_irqsave(&n->list_lock, flags)) {
+ } else if (!spin_lock_nolock(&n->list_lock, &flags)) {
/*
* Unlucky, discard newly allocated slab.
* The slab is not fully free, but it's fine as
@@ -5608,7 +5608,9 @@ retry:
* kfence_alloc. Hence call ___slab_alloc() (at most twice)
* and slab_post_alloc_hook() directly.
*/
+ nolock_wait_begin();
ret = ___slab_alloc(s, gfp_flags, node, ac);
+ nolock_wait_end();
/*
* It's possible we failed due to trylock as we preempted someone with
^ permalink raw reply related [flat|nested] 14+ messages in thread
* Re: [PATCH bpf-next v1 1/4] bpf: Add call for selecting kmalloc or kmalloc_nolock at call time
2026-11-24 13:59 ` [PATCH bpf-next v1 1/4] bpf: Add call for selecting kmalloc or kmalloc_nolock at call time Harry Yoo
2026-11-24 18:07 ` Emil Tsalapatis
2026-11-25 4:03 ` Alexei Starovoitov
@ 2026-11-25 14:09 ` Peter Zijlstra
2026-11-25 14:30 ` Harry Yoo
2026-11-26 1:04 ` Alexei Starovoitov
2 siblings, 2 replies; 14+ messages in thread
From: Peter Zijlstra @ 2026-11-25 14:09 UTC (permalink / raw)
To: Harry Yoo
Cc: Alexei Starovoitov, Emil Tsalapatis, bpf, andrii, eddyz87, memxor,
daniel, Sebastian Andrzej Siewior, Clark Williams, Steven Rostedt,
linux-rt-devel, Vlastimil Babka, Hao Li, Andrew Morton,
Suren Baghdasaryan, Michal Hocko, Brendan Jackman,
Johannes Weiner, Zi Yan, linux-mm, David Hildenbrand, Ingo Molnar,
Will Deacon, Boqun Feng, Waiman Long, linux-kernel
On Tue, Nov 24, 2026 at 01:59:31PM +0000, Harry Yoo wrote:
> [+Cc Relevant folks]
>
> And a bit of context on the underlying problem.
>
> kmalloc_nolock() could fail when there is a contention on locks
> shared across CPUs (e.g., n->list_lock in slab, zone->lock in buddy),
> or there is a decent amount of memory pressure. It's more restrictive
> than GFP_ATOMIC.
>
> Unconditionally using kmalloc_nolock() in BPF local storage is
> unnecessarily restrictive when the program can sleep or spin, and
> Emil's patchset teaches the local storage whether the BPF program
> accessing the local storage could sleep or spin on a lock.
>
> Answering the question "Does this context allow sleeping, or spinning
> on a lock" is extremely tricky in the Linux kernel as BPF programs
> could run and call kfuncs almost at any point in the kernel code and
> the "context" is usually encoded in the GFP flags or the process flags.
>
> Tracking whether certain locks are taken by current context is not
> ideal either as it adds unnecessary overhead for everybody.
>
> On Tue, Nov 24, 2026 at 06:40:11AM +0000, Alexei Starovoitov wrote:
> > On Mon, Nov 23, 2026 at 10:03 PM Emil Tsalapatis <emil@etsalapatis.com> wrote:
> > > + /* Only try the spinning allocation when preemption disables are tracked. */
> > > + if (IS_ENABLED(CONFIG_PREEMPT_COUNT) && can_spin_in_alloc() &&
> > > + size <= KMALLOC_MAX_CACHE_SIZE && !(flags & ~nolock_flags))
> > > + ptr = kmalloc(size, flags | GFP_NOWAIT | __GFP_NOMEMALLOC);
> >
> > It works, but I after a bit of brainstorming the following
> > might be a better fix, since it will address the root cause:
> > Pls run it by Vlastimil, Harry, Sebastian.
> >
> > diff --git a/mm/page_alloc.c b/mm/page_alloc.c
> > index 2fef409..4368ab5 100644
> > --- a/mm/page_alloc.c
> > +++ b/mm/page_alloc.c
> > @@ -2536,6 +2536,24 @@ __rmqueue(struct zone *zone, unsigned int order, int migratetype,
> > return NULL;
> > }
> >
> > +static DEFINE_WAIT_OVERRIDE_MAP(zone_nolock_map, LD_WAIT_CONFIG);
> > +
> > +/*
> > + * zone->lock is always held with IRQs disabled. A caller that runs with IRQs
> > + * enabled is not nested inside of it, so it can wait for the owner.
> > + */
> > +static bool zone_lock_nolock(struct zone *zone, unsigned long *flags)
> > +{
> > + if (IS_ENABLED(CONFIG_PREEMPT_RT) || irqs_disabled() || in_nmi())
> > + return spin_trylock_irqsave(&zone->lock, *flags);
> > +
> > + /* The caller may hold a raw_spinlock_t. */
> > + lock_map_acquire_try(&zone_nolock_map);
> > + spin_lock_irqsave(&zone->lock, *flags);
> > + lock_map_release(&zone_nolock_map);
> > + return true;
> > +}
>
> I'm not sure this is a better solution unless this could be generalized
> or abstracted further.
This is absolute atrocious shite. You cannot attempt a spin_trylock from
NMI context. That thing might be a mutex.
This should not work; if it does, that is an accident we need to fix.
^ permalink raw reply [flat|nested] 14+ messages in thread
* Re: [PATCH bpf-next v1 1/4] bpf: Add call for selecting kmalloc or kmalloc_nolock at call time
2026-11-25 14:09 ` Peter Zijlstra
@ 2026-11-25 14:30 ` Harry Yoo
2026-11-25 16:04 ` Peter Zijlstra
2026-11-26 1:04 ` Alexei Starovoitov
1 sibling, 1 reply; 14+ messages in thread
From: Harry Yoo @ 2026-11-25 14:30 UTC (permalink / raw)
To: Peter Zijlstra
Cc: Alexei Starovoitov, Emil Tsalapatis, bpf, andrii, eddyz87, memxor,
daniel, Sebastian Andrzej Siewior, Clark Williams, Steven Rostedt,
linux-rt-devel, Vlastimil Babka, Hao Li, Andrew Morton,
Suren Baghdasaryan, Michal Hocko, Brendan Jackman,
Johannes Weiner, Zi Yan, linux-mm, David Hildenbrand, Ingo Molnar,
Will Deacon, Boqun Feng, Waiman Long, linux-kernel
On Wed, Nov 25, 2026 at 03:09:02PM +0100, Peter Zijlstra wrote:
> On Tue, Nov 24, 2026 at 01:59:31PM +0000, Harry Yoo wrote:
> > [+Cc Relevant folks]
> > On Tue, Nov 24, 2026 at 06:40:11AM +0000, Alexei Starovoitov wrote:
> > > On Mon, Nov 23, 2026 at 10:03 PM Emil Tsalapatis <emil@etsalapatis.com> wrote:
> > > > + /* Only try the spinning allocation when preemption disables are tracked. */
> > > > + if (IS_ENABLED(CONFIG_PREEMPT_COUNT) && can_spin_in_alloc() &&
> > > > + size <= KMALLOC_MAX_CACHE_SIZE && !(flags & ~nolock_flags))
> > > > + ptr = kmalloc(size, flags | GFP_NOWAIT | __GFP_NOMEMALLOC);
> > >
> > > It works, but I after a bit of brainstorming the following
> > > might be a better fix, since it will address the root cause:
> > > Pls run it by Vlastimil, Harry, Sebastian.
> > >
> > > diff --git a/mm/page_alloc.c b/mm/page_alloc.c
> > > index 2fef409..4368ab5 100644
> > > --- a/mm/page_alloc.c
> > > +++ b/mm/page_alloc.c
> > > @@ -2536,6 +2536,24 @@ __rmqueue(struct zone *zone, unsigned int order, int migratetype,
> > > return NULL;
> > > }
> > >
> > > +static DEFINE_WAIT_OVERRIDE_MAP(zone_nolock_map, LD_WAIT_CONFIG);
> > > +
> > > +/*
> > > + * zone->lock is always held with IRQs disabled. A caller that runs with IRQs
> > > + * enabled is not nested inside of it, so it can wait for the owner.
> > > + */
> > > +static bool zone_lock_nolock(struct zone *zone, unsigned long *flags)
> > > +{
> > > + if (IS_ENABLED(CONFIG_PREEMPT_RT) || irqs_disabled() || in_nmi())
> > > + return spin_trylock_irqsave(&zone->lock, *flags);
> > > +
> > > + /* The caller may hold a raw_spinlock_t. */
> > > + lock_map_acquire_try(&zone_nolock_map);
> > > + spin_lock_irqsave(&zone->lock, *flags);
> > > + lock_map_release(&zone_nolock_map);
> > > + return true;
> > > +}
> >
> > I'm not sure this is a better solution unless this could be generalized
> > or abstracted further.
>
> This is absolute atrocious shite. You cannot attempt a spin_trylock from
> NMI context. That thing might be a mutex.
Uh, right. We went through this when kmalloc_nolock() was introduced.
> This should not work; if it does, that is an accident we need to fix.
Current implementation handles this by can_spin_trylock() in
mm/internal.h.
One remaining accident we're aware is that we cannot attempt a
spin_trylock or local_trylock under pi_lock (even on !RT).
Currently I don't have a good idea to fix this :/
--
Cheers,
Harry / Hyeonggon
^ permalink raw reply [flat|nested] 14+ messages in thread
* Re: [PATCH bpf-next v1 1/4] bpf: Add call for selecting kmalloc or kmalloc_nolock at call time
2026-11-25 14:30 ` Harry Yoo
@ 2026-11-25 16:04 ` Peter Zijlstra
2026-11-26 1:07 ` Alexei Starovoitov
0 siblings, 1 reply; 14+ messages in thread
From: Peter Zijlstra @ 2026-11-25 16:04 UTC (permalink / raw)
To: Harry Yoo
Cc: Alexei Starovoitov, Emil Tsalapatis, bpf, andrii, eddyz87, memxor,
daniel, Sebastian Andrzej Siewior, Clark Williams, Steven Rostedt,
linux-rt-devel, Vlastimil Babka, Hao Li, Andrew Morton,
Suren Baghdasaryan, Michal Hocko, Brendan Jackman,
Johannes Weiner, Zi Yan, linux-mm, David Hildenbrand, Ingo Molnar,
Will Deacon, Boqun Feng, Waiman Long, linux-kernel
On Wed, Nov 25, 2026 at 02:30:52PM +0000, Harry Yoo wrote:
> > This is absolute atrocious shite. You cannot attempt a spin_trylock from
> > NMI context. That thing might be a mutex.
>
> Uh, right. We went through this when kmalloc_nolock() was introduced.
>
> > This should not work; if it does, that is an accident we need to fix.
>
> Current implementation handles this by can_spin_trylock() in
> mm/internal.h.
>
> One remaining accident we're aware is that we cannot attempt a
> spin_trylock or local_trylock under pi_lock (even on !RT).
>
> Currently I don't have a good idea to fix this :/
Yeah, me neither, other than creating specific classes of
tracepoints/whatnot where things are explicitly allowed.
^ permalink raw reply [flat|nested] 14+ messages in thread
* Re: [PATCH bpf-next v1 1/4] bpf: Add call for selecting kmalloc or kmalloc_nolock at call time
2026-11-25 14:09 ` Peter Zijlstra
2026-11-25 14:30 ` Harry Yoo
@ 2026-11-26 1:04 ` Alexei Starovoitov
2026-11-26 9:44 ` Peter Zijlstra
1 sibling, 1 reply; 14+ messages in thread
From: Alexei Starovoitov @ 2026-11-26 1:04 UTC (permalink / raw)
To: Peter Zijlstra, Harry Yoo
Cc: Emil Tsalapatis, bpf, andrii, eddyz87, memxor, daniel,
Sebastian Andrzej Siewior, Clark Williams, Steven Rostedt,
linux-rt-devel, Vlastimil Babka, Hao Li, Andrew Morton,
Suren Baghdasaryan, Michal Hocko, Brendan Jackman,
Johannes Weiner, Zi Yan, linux-mm, David Hildenbrand, Ingo Molnar,
Will Deacon, Boqun Feng, Waiman Long, linux-kernel
On Wed, Nov 25, 2026 at 06:09 AM Peter Zijlstra <peterz@infradead.org> wrote:
> This is absolute atrocious shite. You cannot attempt a spin_trylock from
> NMI context. That thing might be a mutex.
cannot spin_trylock from NMI _on PREEMPT_RT_ ... correct.
And the patch guard against that.
On RT the nolock path doesn't reach zone->lock in NMI or hardirq:
__alloc_frozen_pages_noprof() alloc_flags has ALLOC_NOLOCK
alloc_nolock_allowed()
can_spin_trylock()
if (IS_ENABLED(CONFIG_PREEMPT_RT) && (in_nmi() || in_hardirq()))
return false;
fwiw kmalloc_nolock() has the same check.
> This should not work; if it does, that is an accident we need to fix.
It's not an accident.
In NMI only !RT SMP kernels reach the trylock.
^ permalink raw reply [flat|nested] 14+ messages in thread
* Re: [PATCH bpf-next v1 1/4] bpf: Add call for selecting kmalloc or kmalloc_nolock at call time
2026-11-25 16:04 ` Peter Zijlstra
@ 2026-11-26 1:07 ` Alexei Starovoitov
0 siblings, 0 replies; 14+ messages in thread
From: Alexei Starovoitov @ 2026-11-26 1:07 UTC (permalink / raw)
To: Peter Zijlstra, Harry Yoo
Cc: Emil Tsalapatis, bpf, andrii, eddyz87, memxor, daniel,
Sebastian Andrzej Siewior, Clark Williams, Steven Rostedt,
linux-rt-devel, Vlastimil Babka, Hao Li, Andrew Morton,
Suren Baghdasaryan, Michal Hocko, Brendan Jackman,
Johannes Weiner, Zi Yan, linux-mm, David Hildenbrand, Ingo Molnar,
Will Deacon, Boqun Feng, Waiman Long, linux-kernel
On Wed, Nov 25, 2026 at 08:04 AM Peter Zijlstra <peterz@infradead.org> wrote:
> On Wed, Nov 25, 2026 at 02:30:52PM +0000, Harry Yoo wrote:
>> One remaining accident we're aware is that we cannot attempt a
>> spin_trylock or local_trylock under pi_lock (even on !RT).
No. It's RT only. Already answered in the other email.
Nothing wrong with !RT.
^ permalink raw reply [flat|nested] 14+ messages in thread
* Re: [PATCH bpf-next v1 1/4] bpf: Add call for selecting kmalloc or kmalloc_nolock at call time
2026-11-26 1:04 ` Alexei Starovoitov
@ 2026-11-26 9:44 ` Peter Zijlstra
2026-11-26 9:46 ` David Hildenbrand (Arm)
2026-11-26 20:39 ` Alexei Starovoitov
0 siblings, 2 replies; 14+ messages in thread
From: Peter Zijlstra @ 2026-11-26 9:44 UTC (permalink / raw)
To: Alexei Starovoitov
Cc: Harry Yoo, Emil Tsalapatis, bpf, andrii, eddyz87, memxor, daniel,
Sebastian Andrzej Siewior, Clark Williams, Steven Rostedt,
linux-rt-devel, Vlastimil Babka, Hao Li, Andrew Morton,
Suren Baghdasaryan, Michal Hocko, Brendan Jackman,
Johannes Weiner, Zi Yan, linux-mm, David Hildenbrand, Ingo Molnar,
Will Deacon, Boqun Feng, Waiman Long, linux-kernel
On Thu, Nov 26, 2026 at 01:04:27AM +0000, Alexei Starovoitov wrote:
> On Wed, Nov 25, 2026 at 06:09 AM Peter Zijlstra <peterz@infradead.org> wrote:
> > This is absolute atrocious shite. You cannot attempt a spin_trylock from
> > NMI context. That thing might be a mutex.
>
> cannot spin_trylock from NMI _on PREEMPT_RT_ ... correct.
> And the patch guard against that.
>
> On RT the nolock path doesn't reach zone->lock in NMI or hardirq:
>
> __alloc_frozen_pages_noprof() alloc_flags has ALLOC_NOLOCK
> alloc_nolock_allowed()
> can_spin_trylock()
> if (IS_ENABLED(CONFIG_PREEMPT_RT) && (in_nmi() || in_hardirq()))
> return false;
>
> fwiw kmalloc_nolock() has the same check.
>
> > This should not work; if it does, that is an accident we need to fix.
>
> It's not an accident.
> In NMI only !RT SMP kernels reach the trylock.
Allowing this on !RT makes people think this is 'OK' and then the RT
people get to fix endless crap again :-(
Locking constructs should behave identical across the configurations to
avoid divergence and create problems.
You're actively creating something that doesn't and cannot work on RT.
That is not cool.
^ permalink raw reply [flat|nested] 14+ messages in thread
* Re: [PATCH bpf-next v1 1/4] bpf: Add call for selecting kmalloc or kmalloc_nolock at call time
2026-11-26 9:44 ` Peter Zijlstra
@ 2026-11-26 9:46 ` David Hildenbrand (Arm)
2026-11-26 20:40 ` Alexei Starovoitov
2026-11-26 20:39 ` Alexei Starovoitov
1 sibling, 1 reply; 14+ messages in thread
From: David Hildenbrand (Arm) @ 2026-11-26 9:46 UTC (permalink / raw)
To: Peter Zijlstra, Alexei Starovoitov
Cc: Harry Yoo, Emil Tsalapatis, bpf, andrii, eddyz87, memxor, daniel,
Sebastian Andrzej Siewior, Clark Williams, Steven Rostedt,
linux-rt-devel, Vlastimil Babka, Hao Li, Andrew Morton,
Suren Baghdasaryan, Michal Hocko, Brendan Jackman,
Johannes Weiner, Zi Yan, linux-mm, Ingo Molnar, Will Deacon,
Boqun Feng, Waiman Long, linux-kernel
On 11/26/26 10:44, Peter Zijlstra wrote:
> On Thu, Nov 26, 2026 at 01:04:27AM +0000, Alexei Starovoitov wrote:
>> On Wed, Nov 25, 2026 at 06:09 AM Peter Zijlstra <peterz@infradead.org> wrote:
>>> This is absolute atrocious shite. You cannot attempt a spin_trylock from
>>> NMI context. That thing might be a mutex.
>>
>> cannot spin_trylock from NMI _on PREEMPT_RT_ ... correct.
>> And the patch guard against that.
>>
>> On RT the nolock path doesn't reach zone->lock in NMI or hardirq:
>>
>> __alloc_frozen_pages_noprof() alloc_flags has ALLOC_NOLOCK
>> alloc_nolock_allowed()
>> can_spin_trylock()
>> if (IS_ENABLED(CONFIG_PREEMPT_RT) && (in_nmi() || in_hardirq()))
>> return false;
>>
>> fwiw kmalloc_nolock() has the same check.
>>
>>> This should not work; if it does, that is an accident we need to fix.
>>
>> It's not an accident.
>> In NMI only !RT SMP kernels reach the trylock.
>
> Allowing this on !RT makes people think this is 'OK' and then the RT
> people get to fix endless crap again :-(
>
> Locking constructs should behave identical across the configurations to
> avoid divergence and create problems.
>
> You're actively creating something that doesn't and cannot work on RT.
> That is not cool.
This is all just way too complicated and shaky, sorry to say it.
--
Cheers,
David
^ permalink raw reply [flat|nested] 14+ messages in thread
* Re: [PATCH bpf-next v1 1/4] bpf: Add call for selecting kmalloc or kmalloc_nolock at call time
2026-11-26 9:44 ` Peter Zijlstra
2026-11-26 9:46 ` David Hildenbrand (Arm)
@ 2026-11-26 20:39 ` Alexei Starovoitov
1 sibling, 0 replies; 14+ messages in thread
From: Alexei Starovoitov @ 2026-11-26 20:39 UTC (permalink / raw)
To: Peter Zijlstra
Cc: Harry Yoo, Emil Tsalapatis, bpf, andrii, eddyz87, memxor, daniel,
Sebastian Andrzej Siewior, Clark Williams, Steven Rostedt,
linux-rt-devel, Vlastimil Babka, Hao Li, Andrew Morton,
Suren Baghdasaryan, Michal Hocko, Brendan Jackman,
Johannes Weiner, Zi Yan, linux-mm, David Hildenbrand, Ingo Molnar,
Will Deacon, Boqun Feng, Waiman Long, linux-kernel
On Thu, Nov 26, 2026 at 01:44 AM Peter Zijlstra <peterz@infradead.org> wrote:
> Allowing this on !RT makes people think this is 'OK' and then the RT
> people get to fix endless crap again :-(
...
> You're actively creating something that doesn't and cannot work on RT.
There is nothing for RT people to follow up or to fix.
The diff doesn't introduce any new failure mode or a path.
alloc_pages_nolock() works this way since v6.15.
On RT it returns NULL in NMI and in hard IRQ.
It allocates in all other contexts.
The API that people call is kmalloc_nolock() or alloc_pages_nolock()
and the contract is the same in every config:
any context, and NULL is a normal result.
The caller's code is the same on RT and on !RT.
> Locking constructs should behave identical across the configurations to
> avoid divergence and create problems.
Conceptually I agree, but my bot points out plenty of practical
RT vs !RT differences which are way bigger than
this internals of kmalloc_nolock():
where !RT RT
------------------------- ------------------------- ------------------
debug_objects_fill_pool() allocates in any context preemptible() only
printk(), legacy console spinlock_t under raw lock printer thread
vmbus_isr() spinlock_t in hard IRQ irq thread
kvfree_call_rcu() uses kfree_rcu_sheaf() skips it
local_trylock() works in NMI and hard IRQ fails there
^ permalink raw reply [flat|nested] 14+ messages in thread
* Re: [PATCH bpf-next v1 1/4] bpf: Add call for selecting kmalloc or kmalloc_nolock at call time
2026-11-26 9:46 ` David Hildenbrand (Arm)
@ 2026-11-26 20:40 ` Alexei Starovoitov
2026-11-26 20:45 ` David Hildenbrand (Arm)
0 siblings, 1 reply; 14+ messages in thread
From: Alexei Starovoitov @ 2026-11-26 20:40 UTC (permalink / raw)
To: David Hildenbrand (Arm), Peter Zijlstra
Cc: Harry Yoo, Emil Tsalapatis, bpf, andrii, eddyz87, memxor, daniel,
Sebastian Andrzej Siewior, Clark Williams, Steven Rostedt,
linux-rt-devel, Vlastimil Babka, Hao Li, Andrew Morton,
Suren Baghdasaryan, Michal Hocko, Brendan Jackman,
Johannes Weiner, Zi Yan, linux-mm, Ingo Molnar, Will Deacon,
Boqun Feng, Waiman Long, linux-kernel
On Thu, Nov 26, 2026 at 01:46 AM David Hildenbrand (Arm) <david@kernel.org> wrote:
> This is all just way too complicated and shaky, sorry to say it.
Which part?
I don't see any new concepts of failure modes vs existing code.
^ permalink raw reply [flat|nested] 14+ messages in thread
* Re: [PATCH bpf-next v1 1/4] bpf: Add call for selecting kmalloc or kmalloc_nolock at call time
2026-11-26 20:40 ` Alexei Starovoitov
@ 2026-11-26 20:45 ` David Hildenbrand (Arm)
2026-11-26 21:04 ` Alexei Starovoitov
0 siblings, 1 reply; 14+ messages in thread
From: David Hildenbrand (Arm) @ 2026-11-26 20:45 UTC (permalink / raw)
To: Alexei Starovoitov, Peter Zijlstra
Cc: Harry Yoo, Emil Tsalapatis, bpf, andrii, eddyz87, memxor, daniel,
Sebastian Andrzej Siewior, Clark Williams, Steven Rostedt,
linux-rt-devel, Vlastimil Babka, Hao Li, Andrew Morton,
Suren Baghdasaryan, Michal Hocko, Brendan Jackman,
Johannes Weiner, Zi Yan, linux-mm, Ingo Molnar, Will Deacon,
Boqun Feng, Waiman Long, linux-kernel
On 11/26/26 21:40, Alexei Starovoitov wrote:
> On Thu, Nov 26, 2026 at 01:46 AM David Hildenbrand (Arm) <david@kernel.org> wrote:
>> This is all just way too complicated and shaky, sorry to say it.
>
> Which part?
kmalloc_nolock :)
> I don't see any new concepts of failure modes vs existing code.
I hope we can sort out the rough edges.
--
Cheers,
David
^ permalink raw reply [flat|nested] 14+ messages in thread
* Re: [PATCH bpf-next v1 1/4] bpf: Add call for selecting kmalloc or kmalloc_nolock at call time
2026-11-26 20:45 ` David Hildenbrand (Arm)
@ 2026-11-26 21:04 ` Alexei Starovoitov
0 siblings, 0 replies; 14+ messages in thread
From: Alexei Starovoitov @ 2026-11-26 21:04 UTC (permalink / raw)
To: David Hildenbrand (Arm), Peter Zijlstra
Cc: Harry Yoo, Emil Tsalapatis, bpf, andrii, eddyz87, memxor, daniel,
Sebastian Andrzej Siewior, Clark Williams, Steven Rostedt,
linux-rt-devel, Vlastimil Babka, Hao Li, Andrew Morton,
Suren Baghdasaryan, Michal Hocko, Brendan Jackman,
Johannes Weiner, Zi Yan, linux-mm, Ingo Molnar, Will Deacon,
Boqun Feng, Waiman Long, linux-kernel
On Thu, Nov 26, 2026 at 12:45 PM David Hildenbrand (Arm) <david@kernel.org> wrote:
>> Which part?
>
> kmalloc_nolock :)
lol. Agree. I guess it is in some ways :)
>> I don't see any new concepts of failure modes vs existing code.
>
> I hope we can sort out the rough edges.
100%
That's the goal of this diff. It removes one of this rough edges
for user's of kmalloc_nolock().
Today it can return NULL when another CPU holds zone->lock
or n->list_lock. It took long time to debug in production,
since it didn't look like lock contention at all.
With this short diff that rouge edge/corner case goes away
and spurious NULL returns disappear.
^ permalink raw reply [flat|nested] 14+ messages in thread
end of thread, other threads:[~2026-11-26 21:04 UTC | newest]
Thread overview: 14+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
[not found] <20261124060357.11700-1-emil@etsalapatis.com>
[not found] ` <20261124060357.11700-2-emil@etsalapatis.com>
[not found] ` <DN38HGVL8JHQ.2ZVZQCKFXVA9T@gmail.com>
2026-11-24 13:59 ` [PATCH bpf-next v1 1/4] bpf: Add call for selecting kmalloc or kmalloc_nolock at call time Harry Yoo
2026-11-24 18:07 ` Emil Tsalapatis
2026-11-25 4:03 ` Alexei Starovoitov
2026-11-25 14:09 ` Peter Zijlstra
2026-11-25 14:30 ` Harry Yoo
2026-11-25 16:04 ` Peter Zijlstra
2026-11-26 1:07 ` Alexei Starovoitov
2026-11-26 1:04 ` Alexei Starovoitov
2026-11-26 9:44 ` Peter Zijlstra
2026-11-26 9:46 ` David Hildenbrand (Arm)
2026-11-26 20:40 ` Alexei Starovoitov
2026-11-26 20:45 ` David Hildenbrand (Arm)
2026-11-26 21:04 ` Alexei Starovoitov
2026-11-26 20:39 ` Alexei Starovoitov
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox