[PATCH for-next-fixes v2 0/5] memcg,slab: kmalloc_nolock() fixes

From: Harry Yoo (Meta)

Date: Tue Nov 03 2026 - 11:05:51 EST


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@xxxxxxxxxx

[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@xxxxxxxxxx

---
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