From: "Harry Yoo (Meta)" <harry@kernel.org>
To: Johannes Weiner <hannes@cmpxchg.org>,
Michal Hocko <mhocko@kernel.org>,
Roman Gushchin <roman.gushchin@linux.dev>,
Shakeel Butt <shakeel.butt@linux.dev>,
Muchun Song <muchun.song@linux.dev>,
Andrew Morton <akpm@linux-foundation.org>,
Harry Yoo <harry@kernel.org>,
Alexei Starovoitov <ast@kernel.org>,
Vlastimil Babka <vbabka@kernel.org>, Hao Li <hao.li@linux.dev>,
Christoph Lameter <cl@gentwo.org>,
David Rientjes <rientjes@google.com>,
Sebastian Andrzej Siewior <bigeasy@linutronix.de>,
Clark Williams <clrkwllms@kernel.org>,
Steven Rostedt <rostedt@goodmis.org>
Cc: kernel-team@meta.com, cgroups@vger.kernel.org,
linux-mm@kvack.org, linux-kernel@vger.kernel.org,
linux-rt-devel@lists.linux.dev, Sashiko <sashiko-bot@kernel.org>
Subject: [PATCH for-next-fixes v2 0/5] memcg,slab: kmalloc_nolock() fixes
Date: Tue, 03 Nov 2026 14:47:49 +0000 [thread overview]
Message-ID: <20261103-kmalloc-nolock-fixes-v2-0-19167cfe4779@kernel.org> (raw)
This patchset fixes three kmalloc_nolock() bugs.
1. obj_cgroup_put() takes spinlocks in the release path when it drops
the last reference, but it can be called from any context.
2. A spinlock may be taken in the following path and may lead
to deadlock:
kmalloc_nolock()
-> slab_post_alloc_hook()
-> memcg_slab_post_alloc_hook()
-> memcg_alloc_abort_single().
3. irq_work_sync() is called without synchronization for rcuwait
(on PREEMPT_RT or some architectures), potentially causing a
hang.
Bug 1 was reported by lockdep, and bugs 2 [1] and 3 [2] were
reported by Sashiko.
Patch 1 only touches mm/memcontrol.c and no later patche depends on
it, so it can go through to the memcg tree.
Deferring obj_cgroup_release()
==============================
RFC v1 skipped the stock drain in __refill_obj_stock() when spinning is
not allowed. Making sure "obj_cgroup_put() is not called in a restricted
context that doesn't allow spinning." is easy to get wrong.
v2 makes obj_cgroup_release() safe in any context instead (as mentioned
in cover letter of the RFC v1). The release callback only queues the
objcg (llist_add() + irq_work_queue(), both NMI-safe) and the release
runs from irq_work.
This also fixes a performance regression in RFC v1 in a free-heavy
kmalloc_nolock() microbenchmark.
Changes in v2
=============
- Rebased onto the latest slab/for-next-fixes.
- Rebasing the series, writing patch 1 and 5, and verification was
assisted by AI and carefully reviewed by Harry Yoo.
- Patch 1: replaced with the new approach described above.
- Patch 2:
- Now introduces the allow_spin local variables itself
(previously added by the old patch 1).
- Addressed v1 review comments from Vlastimil (thanks!):
- restored the blank lines.
- Patch 3 (new): call debug_locks and KCSAN checks in kfree_nolock()
regardless of allow_spin, as they acquire a spinlock only when
reporting a bug.
- Patch 5 (patch 4 in v1): Introduce a mutex for
deferred_work_barrier() as it can no longer reuse slab_mutex. In v1
the only caller was kmem_cache_destroy(). The barrier is now also
called from kvfree_rcu_barrier() and friends, which are usable outside
mm where slab_mutex is not visible. Dropped Vlastimil's Acked-by
accordingly.
- Dropped the Cc: stable tags to avoid intrusive backports for unlikely
issues in practice [3].
- Link to v1: https://lore.kernel.org/20260624-kmalloc-nolock-fixes-v1-0-fdf4d17351dd@kernel.org
[1] https://sashiko.dev/#/patchset/20260610-slab_alloc_flags-v2-0-7190909db118%40kernel.org?part=9
[2] https://sashiko.dev/#/patchset/20260615-kfree_rcu_nolock-v3-0-70a54f3775bb%40kernel.org?part=5
[3] https://lore.kernel.org/1816e94b-87e0-4276-8ff7-1fb651326ab3@kernel.org
---
Harry Yoo (Meta) (5):
mm/memcontrol: defer obj_cgroup_release() to irq_work
mm/slab: handle allow_spin in slab_free_hook() instead of open coding
mm/slab: call debug_locks and KCSAN checks in kfree_nolock()
mm/slab: fix a deadlock in memcg_alloc_abort_single()
mm/slab: serialize deferred_work_barrier()
include/linux/memcontrol.h | 2 +
mm/memcontrol.c | 27 +++++++++--
mm/slub.c | 112 ++++++++++++++++++++++++++-------------------
3 files changed, 91 insertions(+), 50 deletions(-)
---
base-commit: 03a176b9248c4513bb705ed3d931af3a7e9c2a08
change-id: 20260624-kmalloc-nolock-fixes-c97675328773
Best regards,
--
Cheers,
Harry / Hyeonggon
next reply other threads:[~2026-11-03 14:48 UTC|newest]
Thread overview: 9+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-03 14:47 Harry Yoo (Meta) [this message]
2026-11-03 14:47 ` [PATCH for-next-fixes v2 1/5] mm/memcontrol: defer obj_cgroup_release() to irq_work Harry Yoo (Meta)
2026-11-03 14:47 ` [PATCH for-next-fixes v2 2/5] mm/slab: handle allow_spin in slab_free_hook() instead of open coding Harry Yoo (Meta)
2026-11-03 14:47 ` [PATCH for-next-fixes v2 3/5] mm/slab: call debug_locks and KCSAN checks in kfree_nolock() Harry Yoo (Meta)
2026-11-03 14:47 ` [PATCH for-next-fixes v2 4/5] mm/slab: fix a deadlock in memcg_alloc_abort_single() Harry Yoo (Meta)
2026-11-03 18:40 ` Alexei Starovoitov
2026-11-03 20:21 ` Harry Yoo
2026-11-03 21:11 ` Alexei Starovoitov
2026-11-03 14:47 ` [PATCH for-next-fixes v2 5/5] mm/slab: serialize deferred_work_barrier() Harry Yoo (Meta)
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=20261103-kmalloc-nolock-fixes-v2-0-19167cfe4779@kernel.org \
--to=harry@kernel.org \
--cc=akpm@linux-foundation.org \
--cc=ast@kernel.org \
--cc=bigeasy@linutronix.de \
--cc=cgroups@vger.kernel.org \
--cc=cl@gentwo.org \
--cc=clrkwllms@kernel.org \
--cc=hannes@cmpxchg.org \
--cc=hao.li@linux.dev \
--cc=kernel-team@meta.com \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mm@kvack.org \
--cc=linux-rt-devel@lists.linux.dev \
--cc=mhocko@kernel.org \
--cc=muchun.song@linux.dev \
--cc=rientjes@google.com \
--cc=roman.gushchin@linux.dev \
--cc=rostedt@goodmis.org \
--cc=sashiko-bot@kernel.org \
--cc=shakeel.butt@linux.dev \
--cc=vbabka@kernel.org \
/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®