From: Shakeel Butt <shakeel.butt@linux.dev>
To: Andrew Morton <akpm@linux-foundation.org>
Cc: Johannes Weiner <hannes@cmpxchg.org>,
Michal Hocko <mhocko@kernel.org>,
Roman Gushchin <roman.gushchin@linux.dev>,
Muchun Song <muchun.song@linux.dev>,
Alexei Starovoitov <ast@kernel.org>,
Vlastimil Babka <vbabka@kernel.org>, Harry Yoo <harry@kernel.org>,
Tejun Heo <tj@kernel.org>,
cgroups@vger.kernel.org, linux-mm@kvack.org,
linux-kernel@vger.kernel.org,
Meta kernel team <kernel-team@meta.com>,
bpf@vger.kernel.org
Subject: [PATCH 2/2] memcg: make memcg_uncharge_kmem() NMI safe
Date: Fri, 4 Dec 2026 13:57:09 -0800 [thread overview]
Message-ID: <20261204215709.4129739-3-shakeel.butt@linux.dev> (raw)
In-Reply-To: <20261204215709.4129739-1-shakeel.butt@linux.dev>
On architectures where this_cpu operations are not NMI safe but atomics
are (CONFIG_MEMCG_NMI_SAFETY_REQUIRES_ATOMIC), MEMCG_KMEM updates that
can run in NMI must go through account_kmem_nmi_safe(). The objcg charge
and uncharge paths do, but memcg_uncharge_kmem() still calls
mod_memcg_state() directly.
memcg_uncharge_kmem() runs in NMI when kmalloc_nolock() or
kfree_nolock() has to evict an object stock slot that holds at least a
page of charged bytes:
__refill_obj_stock()
drain_obj_stock_slot()
memcg_uncharge_kmem()
mod_memcg_state()
If the NMI interrupted mod_memcg_state() on the same CPU, the nested
read-modify-write of the per-CPU MEMCG_KMEM counter, or of the rstat
update counter, loses one of the two updates.
Use account_kmem_nmi_safe() in memcg_uncharge_kmem(). Outside NMI it
still calls mod_memcg_state(), so nothing changes there.
Hitting this needs slub_debug, because on these architectures
kmalloc_nolock() only works on slab debug caches. It also needs a base
page size below 64K, because an object stock slot holds at most
U16_MAX bytes, and a kernel without PREEMPT_RT, where local_trylock()
always fails in NMI.
Kernels without commit afe9ae3fda96 ("mm: memcontrol: factor out memcg
kmem uncharge sequence") have the same mod_memcg_state() call
open-coded in drain_obj_stock_slot(), or in drain_obj_stock() in older
ones. Backports to them need the change there.
Fixes: af92793e52c3 ("slab: Introduce kmalloc_nolock() and kfree_nolock().")
Signed-off-by: Shakeel Butt <shakeel.butt@linux.dev>
---
mm/memcontrol.c | 3 ++-
1 file changed, 2 insertions(+), 1 deletion(-)
diff --git a/mm/memcontrol.c b/mm/memcontrol.c
index 53e688083350..4289eb5a6d06 100644
--- a/mm/memcontrol.c
+++ b/mm/memcontrol.c
@@ -136,10 +136,11 @@ bool mem_cgroup_kmem_disabled(void)
}
static void memcg_uncharge(struct mem_cgroup *memcg, unsigned int nr_pages);
+static inline void account_kmem_nmi_safe(struct mem_cgroup *memcg, int val);
static void memcg_uncharge_kmem(struct mem_cgroup *memcg, unsigned int nr_pages)
{
- mod_memcg_state(memcg, MEMCG_KMEM, -nr_pages);
+ account_kmem_nmi_safe(memcg, -nr_pages);
memcg1_account_kmem(memcg, -nr_pages);
if (!mem_cgroup_is_root(memcg))
memcg_uncharge(memcg, nr_pages);
--
2.53.1-Meta
prev parent reply other threads:[~2026-12-04 21:57 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-12-04 21:57 [PATCH 0/2] memcg: fix memcg stat updates made in NMI Shakeel Butt
2026-12-04 21:57 ` [PATCH 1/2] memcg: add NMI stat deltas before marking the css updated Shakeel Butt
2026-12-04 21:57 ` Shakeel Butt [this message]
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=20261204215709.4129739-3-shakeel.butt@linux.dev \
--to=shakeel.butt@linux.dev \
--cc=akpm@linux-foundation.org \
--cc=ast@kernel.org \
--cc=bpf@vger.kernel.org \
--cc=cgroups@vger.kernel.org \
--cc=hannes@cmpxchg.org \
--cc=harry@kernel.org \
--cc=kernel-team@meta.com \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mm@kvack.org \
--cc=mhocko@kernel.org \
--cc=muchun.song@linux.dev \
--cc=roman.gushchin@linux.dev \
--cc=tj@kernel.org \
--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