Re: [PATCH 25/32] mm/mempolicy: don't wait for mems_allowed_seq in kmalloc_nolock()
From: Joshua Hahn
Date: Wed Dec 02 2026 - 13:57:47 EST
On Tue, Dec 01, 2026 at 10:40:17AM +0100, David Hildenbrand (Arm) wrote:
> On 11/30/26 16:55, Joshua Hahn wrote:
> > On Sun, Nov 29, 2026 at 11:06:56AM +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.
> >
> > In general this one looks good to me but I just want to note that below,
> > we turn the strict slab_strict_numa into a best-effort case when
> > spinning is not allowed. Maybe something that is worth documenting?
>
> Yes, probably, what would be the best place?
Maybe in Documentation/admin-guide/mm/slab.rst, right below the
slab_strict_numa section? Maybe just a sentence that the memory policies
are best-effort could be good enough:
``slab_strict_numa``
Enables the application of memory policies on each
allocation. This results in more accurate placement of
objects which may result in the reduction of accesses
to remote nodes. The default is to only apply memory
policies at the folio level when a new folio is acquired
or a folio is retrieved from the lists. Enabling this
option reduces the fastpath performance of the slab allocator.
+ Memory policies are still best-effort, and can be ignored and
+ allocated from the local node in contexts that cannot spin.
> Meanwhile I figure out if we can drop mempolicy support for kmalloc_nolock()
> entirely.
Yeah, that sounds good to me too! It would also make the contract a bit
easier to follow as well. I had an AI agent look through to see when the
mempolicy is consulted, and I think without slab_strict_numa it's only
in get_from_any_partial() anyways. New slabs come from
alloc_frozen_pages_nolock() which just uses numa_node_id() :p
Thanks David, have a great day!
Joshua