From: "David Hildenbrand (Arm)" <david@kernel.org>
To: Hugh Dickins <hughd@google.com>,
Baolin Wang <baolin.wang@linux.alibaba.com>,
Andrew Morton <akpm@linux-foundation.org>,
Mike Rapoport <rppt@kernel.org>, Peter Xu <peterx@redhat.com>,
Michal Hocko <mhocko@suse.com>, Kiryl Shutsemau <kas@kernel.org>,
Manfred Spraul <manfred@colorfullife.com>,
Davidlohr Bueso <dave@stgolabs.net>,
Cyrill Gorcunov <gorcunov@gmail.com>, Zi Yan <ziy@nvidia.com>,
"Liam R. Howlett" <liam@infradead.org>,
Nico Pache <nico.pache@linux.dev>,
Ryan Roberts <ryan.roberts@arm.com>, Dev Jain <dev.jain@arm.com>,
Barry Song <baohua@kernel.org>,
Lance Yang <lance.yang@linux.dev>,
Usama Arif <usama.arif@linux.dev>,
Lorenzo Stoakes <ljs@kernel.org>,
Rik van Riel <riel@surriel.com>,
Vlastimil Babka <vbabka@kernel.org>,
Harry Yoo <harry@kernel.org>, Jann Horn <jannh@google.com>,
Yang Shi <shy828301@gmail.com>,
Zach O'Keefe <zokeefe@google.com>, Song Liu <song@kernel.org>,
David Stevens <stevensd@chromium.org>,
Kefeng Wang <wangkefeng.wang@huawei.com>,
Alex Williamson <alex@shazbot.org>,
Suren Baghdasaryan <surenb@google.com>,
Dave Jiang <dave.jiang@intel.com>,
Matthew Wilcox <willy@infradead.org>,
Miaohe Lin <linmiaohe@huawei.com>,
David Rientjes <rientjes@google.com>,
Shakeel Butt <shakeel.butt@linux.dev>, Jan Kara <jack@suse.cz>,
Mel Gorman <mgorman@suse.de>, Jason Gunthorpe <jgg@ziepe.ca>,
John Hubbard <jhubbard@nvidia.com>,
Gerd Hoffmann <kraxel@redhat.com>,
Vivek Kasireddy <vivek.kasireddy@intel.com>,
Dave Airlie <airlied@redhat.com>,
Matthew Brost <matthew.brost@intel.com>,
Joshua Hahn <joshua.hahnjy@gmail.com>,
Byungchul Park <byungchul@sk.com>,
Gregory Price <gourry@gourry.net>,
Ying Huang <ying.huang@linux.alibaba.com>,
Alistair Popple <apopple@nvidia.com>,
Hagen Paul Pfeifer <hagen@jauu.net>,
James Bottomley <James.Bottomley@HansenPartnership.com>,
Felix Kuehling <Felix.Kuehling@amd.com>,
Alex Sierra <alex.sierra@amd.com>,
Balbir Singh <balbirs@nvidia.com>,
Kairui Song <kasong@tencent.com>, Qi Zheng <qi.zheng@linux.dev>,
Axel Rasmussen <axelrasmussen@google.com>,
Yuanchu Xie <yuanchu@google.com>, Wei Xu <weixugc@google.com>,
Baoquan He <baoquan.he@linux.dev>,
Oscar Salvador <osalvador@suse.de>,
Muchun Song <muchun.song@linux.dev>,
Dan Williams <djbw@kernel.org>, Rakie Kim <rakie.kim@sk.com>,
Hao Li <hao.li@linux.dev>, Christoph Lameter <cl@gentwo.org>,
Roman Gushchin <roman.gushchin@linux.dev>,
Sebastian Andrzej Siewior <bigeasy@linutronix.de>,
Clark Williams <clrkwllms@kernel.org>,
Steven Rostedt <rostedt@goodmis.org>,
Alexei Starovoitov <ast@kernel.org>, Xu Xin <xu.xin@linux.dev>,
Chengming Zhou <chengming.zhou@linux.dev>,
Shivank Garg <shivankg@amd.com>
Cc: linux-mm@kvack.org, linux-kernel@vger.kernel.org,
linux-fsdevel@vger.kernel.org, linux-cxl@vger.kernel.org,
linux-rt-devel@lists.linux.dev, SJ Park <sj@kernel.org>,
"David Hildenbrand (Arm)" <david@kernel.org>,
stable@vger.kernel.org, Chris Mason <mason@kernel.org>
Subject: [PATCH 25/32] mm/mempolicy: don't wait for mems_allowed_seq in kmalloc_nolock()
Date: Sun, 29 Nov 2026 11:06:56 +0100 [thread overview]
Message-ID: <20261129-fixes_core_3-v1-25-f66094116801@kernel.org> (raw)
In-Reply-To: <20261129-fixes_core_3-v1-0-f66094116801@kernel.org>
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@vger.kernel.org
Reported-by: Chris Mason <mason@kernel.org>
Signed-off-by: David Hildenbrand (Arm) <david@kernel.org>
---
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
next prev parent reply other threads:[~2026-11-29 10:14 UTC|newest]
Thread overview: 47+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-29 10:06 [PATCH 00/32] mm: various (urgent?) fixes, part 3 David Hildenbrand (Arm)
2026-11-29 10:06 ` [PATCH 01/32] mm/shmem: count only the swapped pages in the requested range David Hildenbrand (Arm)
2026-11-29 10:06 ` [PATCH 02/32] mm/shmem: don't skip parts of a split swap entry when punching holes David Hildenbrand (Arm)
2026-11-29 10:06 ` [PATCH 03/32] mm/shmem: reject userfaultfd fills during hole punch David Hildenbrand (Arm)
2026-11-29 19:47 ` Jose A. Perez de Azpillaga
2026-11-29 10:06 ` [PATCH 04/32] mm/shmem: fix commit limit bypass with very large shmem sizes David Hildenbrand (Arm)
2026-11-29 10:06 ` [PATCH 05/32] mm/shmem: don't pin inodes with eviction held off in the huge shrinker David Hildenbrand (Arm)
2026-11-29 10:06 ` [PATCH 06/32] mm/shmem: seal the size of SysV and shared anon files David Hildenbrand (Arm)
2026-11-29 10:06 ` [PATCH 07/32] mm/khugepaged: check the VMA before walking page tables when collapsing David Hildenbrand (Arm)
2026-11-29 19:55 ` Jose A. Perez de Azpillaga
2026-11-29 10:06 ` [PATCH 08/32] mm/khugepaged: flush file after unlocking folios in collapse_file() David Hildenbrand (Arm)
2026-11-29 19:58 ` Jose A. Perez de Azpillaga
2026-11-29 10:06 ` [PATCH 09/32] mm/khugepaged: check for uffd outside the i_pages lock David Hildenbrand (Arm)
2026-11-29 20:07 ` Jose A. Perez de Azpillaga
2026-11-29 10:06 ` [PATCH 10/32] mm/khugepaged: don't wait for userfaultfd David Hildenbrand (Arm)
2026-11-29 20:16 ` Jose A. Perez de Azpillaga
2026-11-29 10:06 ` [PATCH 11/32] mm/khugepaged: store new folio before adding it to LRU David Hildenbrand (Arm)
2026-11-29 20:22 ` Jose A. Perez de Azpillaga
2026-11-29 10:06 ` [PATCH 12/32] mm/khugepaged: check PMD support with the folios locked David Hildenbrand (Arm)
2026-11-29 20:27 ` Jose A. Perez de Azpillaga
2026-11-29 10:06 ` [PATCH 13/32] mm/khugepaged: stop MADV_COLLAPSE on a fatal signal David Hildenbrand (Arm)
2026-11-29 20:29 ` Jose A. Perez de Azpillaga
2026-11-29 10:06 ` [PATCH 14/32] mm/huge_memory: split huge zero PMDs on unshare faults David Hildenbrand (Arm)
2026-11-29 20:33 ` Jose A. Perez de Azpillaga
2026-11-29 10:06 ` [PATCH 15/32] mm/huge_memory: don't map huge PFN entries if the arch restricts PFNs David Hildenbrand (Arm)
2026-11-29 20:41 ` Jose A. Perez de Azpillaga
2026-11-29 10:06 ` [PATCH 16/32] mm/memory: don't clear a PUD leaf as bad in a zap David Hildenbrand (Arm)
2026-11-29 20:47 ` Jose A. Perez de Azpillaga
2026-11-29 10:06 ` [PATCH 17/32] mm/memory: drop the swap device ref before do_wp_page() David Hildenbrand (Arm)
2026-11-29 20:50 ` Jose A. Perez de Azpillaga
2026-11-29 10:06 ` [PATCH 18/32] mm: consistently check MMF_UNSTABLE when populating in CoW mappings David Hildenbrand (Arm)
2026-11-29 20:58 ` Jose A. Perez de Azpillaga
2026-11-29 10:06 ` [PATCH 19/32] mm/gup: check for fatal signals in memfd_pin_folios() David Hildenbrand (Arm)
2026-11-29 10:06 ` [PATCH 20/32] mm/migrate_device: don't collect secretmem pages David Hildenbrand (Arm)
2026-11-29 10:06 ` [PATCH 21/32] mm/migrate_device: mark folios leaving device memory dirty David Hildenbrand (Arm)
2026-11-29 10:06 ` [PATCH 22/32] mm: taint device sections before setting subsections valid David Hildenbrand (Arm)
2026-11-29 10:06 ` [PATCH 23/32] mm/mempolicy: check access under exec_update_lock David Hildenbrand (Arm)
2026-11-29 10:06 ` [PATCH 24/32] mm/mempolicy: reject an empty set of allowed nodes David Hildenbrand (Arm)
2026-11-29 10:06 ` David Hildenbrand (Arm) [this message]
2026-11-29 21:27 ` [PATCH 25/32] mm/mempolicy: don't wait for mems_allowed_seq in kmalloc_nolock() Jose A. Perez de Azpillaga
2026-11-29 10:06 ` [PATCH 26/32] mm/ksm: look up the folio again in cmp_and_merge_page() David Hildenbrand (Arm)
2026-11-29 10:06 ` [PATCH 27/32] mm/ksm: copy KSM folios at swap-in without VM_MERGEABLE David Hildenbrand (Arm)
2026-11-29 10:06 ` [PATCH 28/32] mm/ksm: don't convert a shared anon folio into a KSM folio David Hildenbrand (Arm)
2026-11-29 10:07 ` [PATCH 29/32] mm/ksm: use a migration counter to detect KSM folio migration David Hildenbrand (Arm)
2026-11-29 10:07 ` [PATCH 30/32] mm/migrate: fix KSM oddity by checking expected refcount without mappings David Hildenbrand (Arm)
2026-11-29 10:07 ` [PATCH 31/32] mm/migrate_device: don't collect KSM folios David Hildenbrand (Arm)
2026-11-29 10:07 ` [PATCH 32/32] mm/migrate: use consistent folio locking logic David Hildenbrand (Arm)
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20261129-fixes_core_3-v1-25-f66094116801@kernel.org \
--to=david@kernel.org \
--cc=Felix.Kuehling@amd.com \
--cc=James.Bottomley@HansenPartnership.com \
--cc=airlied@redhat.com \
--cc=akpm@linux-foundation.org \
--cc=alex.sierra@amd.com \
--cc=alex@shazbot.org \
--cc=apopple@nvidia.com \
--cc=ast@kernel.org \
--cc=axelrasmussen@google.com \
--cc=balbirs@nvidia.com \
--cc=baohua@kernel.org \
--cc=baolin.wang@linux.alibaba.com \
--cc=baoquan.he@linux.dev \
--cc=bigeasy@linutronix.de \
--cc=byungchul@sk.com \
--cc=chengming.zhou@linux.dev \
--cc=cl@gentwo.org \
--cc=clrkwllms@kernel.org \
--cc=dave.jiang@intel.com \
--cc=dave@stgolabs.net \
--cc=dev.jain@arm.com \
--cc=djbw@kernel.org \
--cc=gorcunov@gmail.com \
--cc=gourry@gourry.net \
--cc=hagen@jauu.net \
--cc=hao.li@linux.dev \
--cc=harry@kernel.org \
--cc=hughd@google.com \
--cc=jack@suse.cz \
--cc=jannh@google.com \
--cc=jgg@ziepe.ca \
--cc=jhubbard@nvidia.com \
--cc=joshua.hahnjy@gmail.com \
--cc=kas@kernel.org \
--cc=kasong@tencent.com \
--cc=kraxel@redhat.com \
--cc=lance.yang@linux.dev \
--cc=liam@infradead.org \
--cc=linmiaohe@huawei.com \
--cc=linux-cxl@vger.kernel.org \
--cc=linux-fsdevel@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mm@kvack.org \
--cc=linux-rt-devel@lists.linux.dev \
--cc=ljs@kernel.org \
--cc=manfred@colorfullife.com \
--cc=mason@kernel.org \
--cc=matthew.brost@intel.com \
--cc=mgorman@suse.de \
--cc=mhocko@suse.com \
--cc=muchun.song@linux.dev \
--cc=nico.pache@linux.dev \
--cc=osalvador@suse.de \
--cc=peterx@redhat.com \
--cc=qi.zheng@linux.dev \
--cc=rakie.kim@sk.com \
--cc=riel@surriel.com \
--cc=rientjes@google.com \
--cc=roman.gushchin@linux.dev \
--cc=rostedt@goodmis.org \
--cc=rppt@kernel.org \
--cc=ryan.roberts@arm.com \
--cc=shakeel.butt@linux.dev \
--cc=shivankg@amd.com \
--cc=shy828301@gmail.com \
--cc=sj@kernel.org \
--cc=song@kernel.org \
--cc=stable@vger.kernel.org \
--cc=stevensd@chromium.org \
--cc=surenb@google.com \
--cc=usama.arif@linux.dev \
--cc=vbabka@kernel.org \
--cc=vivek.kasireddy@intel.com \
--cc=wangkefeng.wang@huawei.com \
--cc=weixugc@google.com \
--cc=willy@infradead.org \
--cc=xu.xin@linux.dev \
--cc=ying.huang@linux.alibaba.com \
--cc=yuanchu@google.com \
--cc=ziy@nvidia.com \
--cc=zokeefe@google.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®