[PATCH RFC 0/4] memcg,slab: kmalloc_nolock() fixes
From: Harry Yoo (Oracle)
Date: Wed Jun 24 2026 - 09:12:09 EST
Apologies for posting another series during the merge window.
But these are bug fixes and there are other features that need to be
rebased on top, so...
Overview
========
This patchset tries to fix three kmalloc_nolock() bugs.
1. obj_cgroup_put() takes a spinlock in the release path
when it is holding the last reference.
(This needs some thoughts from the memcg folks)
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 [2] and 3 [3] were
reported by Sashiko.
To MEMCG folks: obj_cgroup_put() is not safe in unknown context
===============================================================
I tried to fix the bug 1 in the __refill_obj_stock() path in patch 1.
Patch 1 considers correctness aspect only, and performance may
degrade because we have to fall back to per-objcg atomics unless
some else drains it for us.
Ouch, while writing the cover letter, I realized that two paths need
some attention:
1. __memcg_slab_free_hook() -> obj_cgroup_put() and
2. current_obj_cgroup() -> current_objcg_update() -> obj_cgroup_put()
An easy solution would be to somehow defer obj_cgroup_put() or
obj_cgroup_release(). I would like to hear thoughts from the memcg folks
on which is the preferred way.
To BPF folks: do we need to backport kmalloc_nolock() support
for architectures without __CMPXCHG_DOUBLE to v6.18?
=============================================================
Originally I intended to fix this as part of this series.
However, the issue reported by Levi Zim [1] was on kernel v6.19,
(Thanks to Vlastimil for mentioning this), and v6.18 does not use
kmalloc_nolock() for BPF local storage.
There are still few users in v6.18, but I can't tell whether it is
necessary to backport it to v6.18 (hopefully not as urgent as other
bugfixes).
Thoughts?
[1] https://lore.kernel.org/linux-mm/9bea1536-534a-4a59-9b5f-92389fb05688@xxxxxxxx
[2] https://sashiko.dev/#/patchset/20260610-slab_alloc_flags-v2-0-7190909db118%40kernel.org?part=9
[3] https://sashiko.dev/#/patchset/20260615-kfree_rcu_nolock-v3-0-70a54f3775bb%40kernel.org?part=5
Signed-off-by: Harry Yoo (Oracle) <harry@xxxxxxxxxx>
---
Harry Yoo (Oracle) (4):
mm/memcontrol: do not drain objcg stock when spinning is not allowed
mm/slab: handle allow_spin in slab_free_hook() instead of open coding
mm/slab: fix a deadlock in memcg_alloc_abort_single()
mm/slab: serialize defer_free_barrier()
mm/memcontrol.c | 34 ++++++++-----
mm/slab.h | 3 +-
mm/slab_common.c | 5 +-
mm/slub.c | 148 +++++++++++++++++++++++++++++++------------------------
4 files changed, 111 insertions(+), 79 deletions(-)
---
base-commit: 892a7864730775c3dbee2a39e9ead4fa8d4256e7
change-id: 20260624-kmalloc-nolock-fixes-c97675328773
Best regards,
--
Harry Yoo (Oracle) <harry@xxxxxxxxxx>