Re: [PATCH 25/32] mm/mempolicy: don't wait for mems_allowed_seq in kmalloc_nolock()

From: Vlastimil Babka (SUSE)

Date: Tue Dec 01 2026 - 06:45:05 EST


On 12/1/26 10:39 AM, David Hildenbrand (Arm) wrote:> On 11/30/26 19:12,
Gregory Price (Meta) wrote:
>> On 2026-11-29 11:06 +0100, David Hildenbrand (Arm) wrote:
>>> kmalloc_nolock() must not wait for locks, including seqlocks.
>>> get_from_any_partial() therefore skips read_mems_allowed_begin() if
>>> spinning is not allowed.
>>>
>>> However, with an MPOL_INTERLEAVE or MPOL_WEIGHTED_INTERLEAVE task policy,
>>> mempolicy_slab_node() calls interleave_nodes() or
>>> weighted_interleave_nodes(), which both spin on tsk->mems_allowed_seq
>>> since commit 274519ed414b ("mm/mempolicy: protect task interleave
>>> functions with tsk->mems_allowed_seq"). The same applies to
>>> apply_strict_numa_policy() with slab_strict_numa.
>>>
>>> Consequently, a BPF program that allocates using kmalloc_nolock() within
>>> the write section of that seqcount, for example in
>>> cpuset_change_task_nodemask(), can deadlock. On PREEMPT_RT, waiting for
>>> the seqcount can then even sleep in atomic context.
>>>
>>> Let's fix it by passing whether spinning is allowed to
>>> mempolicy_slab_node(), simply using the local node for interleave
>>> policies if disallowed.
>>>
>>> While not perfect, it shouldn't matter in practice, is in line with
>>> !in_task() handling, and slab interleaving is best-effort either way,
>>> unless slab_strict_numa is set.
>>>
>>
>> Should we just blanket ignore mempolicy when spinning is not allowed?
>> If another mempolicy gets added later we have moles to whack.
>
> Yes, that's what we get for merging half-baked things like kmalloc_nolock().
>>
>> I think then this just becomes a slub.c-only change where we ignore
>> mempolicy is spinning isn't allowed and always prefer local.
>>
>> That also makes the behavior consistent - no-spin = no policy.
>
> I'm not sure if that's preferable. @Vlastimil, @Harry what are your thoughts on
> this?

I think it's fine. kmalloc_nolock() are usually bpf allocations from
tracepoints/probes that do their own thing not related to the main code
and its policies. Preferring local makes more sense for them anyway? And
if they get remote allocations, the bpf programs will just be a bit slower?