From: Harry Yoo <harry@kernel.org>
To: "Vlastimil Babka (SUSE)" <vbabka@kernel.org>
Cc: 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>,
Hao Li <hao.li@linux.dev>, Christoph Lameter <cl@gentwo.org>,
David Rientjes <rientjes@google.com>,
Alexei Starovoitov <ast@kernel.org>,
Pedro Falcato <pfalcato@suse.de>,
cgroups@vger.kernel.org, linux-mm@kvack.org,
linux-kernel@vger.kernel.org, bpf@vger.kernel.org
Subject: Re: [PATCH RFC 1/4] mm/memcontrol: do not drain objcg stock when spinning is not allowed
Date: Tue, 3 Nov 2026 14:54:36 +0000 [thread overview]
Message-ID: <aun1FofGFtjPjQHy@thinkstation> (raw)
In-Reply-To: <104794ff-9f04-48be-91e8-89d5d2269f95@kernel.org>
On Fri, Jun 26, 2026 at 04:30:48PM +0200, Vlastimil Babka (SUSE) wrote:
> On 6/24/26 15:11, Harry Yoo (Oracle) wrote:
> > When kmalloc_nolock() drains objcg stock, the stock might be holding
> > the last reference to the objcg. Since obj_cgroup_release() is a
> > callback for percpu refcount and does not know whether spinning is
> > allowed, it is not safe to invoke obj_cgroup_put().
> >
> > This was caught by lockdep on PREEMPT_RT because acquiring
> > a sleeping lock (objcg_lock) violates lock nesting rules:
> >
> > kernel: BUG: sleeping function called from invalid context at kernel/locking/spinlock_rt.c:48
> > kernel: in_atomic(): 1, irqs_disabled(): 0, non_block: 0, pid: 1267, name: systemd-resolve
> > preempt_count: 1, expected: 0
> > RCU nest depth: 3, expected: 3
> > 6 locks held by systemd-resolve/1267:
> > #0: ffff888a8165fa20 ((&pcs->lock)){+.+.}-{3:3}, at: kmem_cache_alloc_noprof+0x185/0xa20
> > #1: ffffffff9658a4c0 (rcu_read_lock){....}-{1:3}, at: rt_spin_trylock+0x74/0x2a0
> > #2: ffff888a81648598 ((lock)#4){+.+.}-{3:3}, at: trylock_stock+0x118/0x380
> > #3: ffffffff9658a4c0 (rcu_read_lock){....}-{1:3}, at: rt_spin_trylock+0x74/0x2a0
> > #4: ffffffff9658a4c0 (rcu_read_lock){....}-{1:3}, at: percpu_ref_put_many.constprop.0+0x40/0x270
> > #5: ffffffff96af11d8 (objcg_lock){+.+.}-{3:3}, at: obj_cgroup_release+0x8a/0x410
> > [...]
> > Call Trace:
> > <TASK>
> > dump_stack_lvl+0x8a/0xe0
> > dump_stack+0x14/0x1c
> > __might_resched.cold+0x233/0x2bb
> > rt_spin_lock+0xd3/0x410
> > obj_cgroup_release+0x8a/0x410
> > percpu_ref_put_many.constprop.0+0x226/0x270
> > drain_obj_stock_slot+0x27e/0x8d0
> > __refill_obj_stock+0x409/0x6d0
> > __memcg_slab_post_alloc_hook+0xa45/0x1500
> > __kmalloc_nolock_noprof+0x988/0xc40
> > [...]
> >
> > However, this is illegal in !RT kernels too because the objcg release
> > callback acquires a spinlock even when spinning is not allowed.
> >
> > To fix this issue, fall back to atomics when the cached objcg doesn't
> > match, but it is unsafe to drain because spinning is not allowed.
> >
> > This is expected to affect performance of kmalloc_nolock() since
> > it can no longer drain and refill the stock and falls back to a
> > per-objcg atomic counter (objcg->nr_charged_bytes).
>
> Maybe it's not bad that the constrained operation will not do a drain? Could
> we say it's likely that both _nolock() and normal operations will likely be
> happening on the same cpu for the same memcg, so a normal one will soon
> handle it?
I ended up making obj_cgroup_put() and obj_cgroup_release()
safe to call from any context as in RFC it's easy to overlook that
obj_cgroup_put() is not allowed if !allow_spin.
--
Cheers,
Harry / Hyeonggon
next prev parent reply other threads:[~2026-11-03 14:54 UTC|newest]
Thread overview: 17+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-06-24 13:11 [PATCH RFC 0/4] memcg,slab: kmalloc_nolock() fixes Harry Yoo (Oracle)
2026-06-24 13:11 ` [PATCH RFC 1/4] mm/memcontrol: do not drain objcg stock when spinning is not allowed Harry Yoo (Oracle)
2026-06-26 14:30 ` Vlastimil Babka (SUSE)
2026-11-03 14:54 ` Harry Yoo [this message]
2026-06-24 13:11 ` [PATCH RFC 2/4] mm/slab: handle allow_spin in slab_free_hook() instead of open coding Harry Yoo (Oracle)
2026-06-26 14:41 ` Vlastimil Babka (SUSE)
2026-11-03 14:58 ` Harry Yoo
2026-06-24 13:11 ` [PATCH RFC 3/4] mm/slab: fix a deadlock in memcg_alloc_abort_single() Harry Yoo (Oracle)
2026-06-26 14:57 ` Vlastimil Babka (SUSE)
2026-06-24 13:11 ` [PATCH RFC 4/4] mm/slab: serialize defer_free_barrier() Harry Yoo (Oracle)
2026-06-26 15:00 ` Vlastimil Babka (SUSE)
2026-11-03 14:51 ` Harry Yoo
2026-06-24 16:30 ` [PATCH RFC 0/4] memcg,slab: kmalloc_nolock() fixes Alexei Starovoitov
2026-06-24 20:19 ` Harry Yoo
2026-06-26 15:09 ` Vlastimil Babka (SUSE)
2026-06-26 6:04 ` Harry Yoo
2026-06-26 15:10 ` Vlastimil Babka (SUSE)
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=aun1FofGFtjPjQHy@thinkstation \
--to=harry@kernel.org \
--cc=akpm@linux-foundation.org \
--cc=ast@kernel.org \
--cc=bpf@vger.kernel.org \
--cc=cgroups@vger.kernel.org \
--cc=cl@gentwo.org \
--cc=hannes@cmpxchg.org \
--cc=hao.li@linux.dev \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mm@kvack.org \
--cc=mhocko@kernel.org \
--cc=muchun.song@linux.dev \
--cc=pfalcato@suse.de \
--cc=rientjes@google.com \
--cc=roman.gushchin@linux.dev \
--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®