[PATCH 25/32] mm/mempolicy: don't wait for mems_allowed_seq in kmalloc_nolock()
From: David Hildenbrand (Arm)
Date: Sun Nov 29 2026 - 05:14:52 EST
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.
This has low urgency, as it requires a BPF program to allocate with
kmalloc_nolock() while the task's mems_allowed are being updated.
Fixes: af92793e52c3 ("slab: Introduce kmalloc_nolock() and kfree_nolock().")
Cc: stable@xxxxxxxxxxxxxxx
Reported-by: Chris Mason <mason@xxxxxxxxxx>
Signed-off-by: David Hildenbrand (Arm) <david@xxxxxxxxxx>
---
include/linux/mempolicy.h | 2 +-
mm/mempolicy.c | 8 +++++++-
mm/slub.c | 15 +++++++++------
3 files changed, 17 insertions(+), 8 deletions(-)
diff --git a/include/linux/mempolicy.h b/include/linux/mempolicy.h
index 828e7de6dee73..93ad83fe25a81 100644
--- a/include/linux/mempolicy.h
+++ b/include/linux/mempolicy.h
@@ -147,7 +147,7 @@ extern int huge_node(struct vm_area_struct *vma,
extern bool init_nodemask_of_mempolicy(nodemask_t *mask);
extern bool mempolicy_in_oom_domain(struct task_struct *tsk,
const nodemask_t *mask);
-extern unsigned int mempolicy_slab_node(void);
+extern unsigned int mempolicy_slab_node(bool allow_spin);
extern enum zone_type policy_zone;
diff --git a/mm/mempolicy.c b/mm/mempolicy.c
index 746fd0a137515..c93a278d840b3 100644
--- a/mm/mempolicy.c
+++ b/mm/mempolicy.c
@@ -2236,7 +2236,7 @@ static unsigned int interleave_nodes(struct mempolicy *policy)
* Depending on the memory policy provide a node from which to allocate the
* next slab entry.
*/
-unsigned int mempolicy_slab_node(void)
+unsigned int mempolicy_slab_node(bool allow_spin)
{
struct mempolicy *policy;
int node = numa_mem_id();
@@ -2253,9 +2253,15 @@ unsigned int mempolicy_slab_node(void)
return first_node(policy->nodes);
case MPOL_INTERLEAVE:
+ /* We'd have to spin on tsk->mems_allowed_seq. */
+ if (!allow_spin)
+ return node;
return interleave_nodes(policy);
case MPOL_WEIGHTED_INTERLEAVE:
+ /* We'd have to spin on tsk->mems_allowed_seq. */
+ if (!allow_spin)
+ return node;
return weighted_interleave_nodes(policy);
case MPOL_BIND:
diff --git a/mm/slub.c b/mm/slub.c
index 5c33d12470601..f970f1e5a9e6a 100644
--- a/mm/slub.c
+++ b/mm/slub.c
@@ -4027,7 +4027,8 @@ static void *get_from_any_partial(struct kmem_cache *s, gfp_t gfp_flags,
if (allow_spin)
cpuset_mems_cookie = read_mems_allowed_begin();
- zonelist = node_zonelist(mempolicy_slab_node(), gfp_flags);
+ zonelist = node_zonelist(mempolicy_slab_node(allow_spin),
+ gfp_flags);
for_each_zone_zonelist(zone, z, zonelist, highest_zoneidx) {
struct kmem_cache_node *n;
@@ -4640,12 +4641,14 @@ static void *___slab_alloc(struct kmem_cache *s, gfp_t gfpflags, int node,
return object;
}
-static __always_inline int apply_strict_numa_policy(int node)
+static __always_inline int apply_strict_numa_policy(int node,
+ unsigned int alloc_flags)
{
#ifdef CONFIG_NUMA
if (static_branch_unlikely(&strict_numa) &&
node == NUMA_NO_NODE) {
+ const bool allow_spin = alloc_flags_allow_spinning(alloc_flags);
struct mempolicy *mpol = current->mempolicy;
if (mpol) {
@@ -4658,7 +4661,7 @@ static __always_inline int apply_strict_numa_policy(int node)
*/
if (mpol->mode != MPOL_BIND ||
!node_isset(numa_mem_id(), mpol->nodes))
- node = mempolicy_slab_node();
+ node = mempolicy_slab_node(allow_spin);
}
}
#endif
@@ -5016,7 +5019,7 @@ static __fastpath_inline void *slab_alloc_node(struct kmem_cache *s,
if (unlikely(object))
goto out;
- node = apply_strict_numa_policy(node);
+ node = apply_strict_numa_policy(node, ac->alloc_flags);
object = alloc_from_pcs(s, gfpflags, ac->alloc_flags, node);
@@ -5577,7 +5580,7 @@ static void *__kmalloc_nolock_noprof(DECL_TOKEN_PARAMS(size, token), gfp_t gfp_f
if (!can_spin_trylock())
return NULL;
- node = apply_strict_numa_policy(node);
+ node = apply_strict_numa_policy(node, ac->alloc_flags);
retry:
if (unlikely(size > KMALLOC_MAX_CACHE_SIZE))
@@ -7481,7 +7484,7 @@ __refill_objects_any(struct kmem_cache *s, void **p, gfp_t gfp, unsigned int min
do {
cpuset_mems_cookie = read_mems_allowed_begin();
- zonelist = node_zonelist(mempolicy_slab_node(), gfp);
+ zonelist = node_zonelist(mempolicy_slab_node(/* allow_spin */ true), gfp);
for_each_zone_zonelist(zone, z, zonelist, highest_zoneidx) {
struct kmem_cache_node *n;
unsigned int r;
--
2.56.1