* [PATCH 0/2] memcg: fix memcg stat updates made in NMI
@ 2026-12-04 21:57 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 ` [PATCH 2/2] memcg: make memcg_uncharge_kmem() NMI safe Shakeel Butt
0 siblings, 2 replies; 3+ messages in thread
From: Shakeel Butt @ 2026-12-04 21:57 UTC (permalink / raw)
To: Andrew Morton
Cc: Johannes Weiner, Michal Hocko, Roman Gushchin, Muchun Song,
Alexei Starovoitov, Vlastimil Babka, Harry Yoo, Tejun Heo,
cgroups, linux-mm, linux-kernel, Meta kernel team, bpf
Two fixes for memcg stat updates made in NMI. Both affect only the
architectures where this_cpu operations are not NMI safe but atomics
are (CONFIG_MEMCG_NMI_SAFETY_REQUIRES_ATOMIC: powerpc, sparc64, and arm
and sh where cmpxchg is NMI safe). There, stat updates made in NMI go to
atomics, and flush_nmi_stats() folds them in when the css is flushed.
Patch 1 fixes the order in account_kmem_nmi_safe() and
account_slab_nmi_safe(). They mark the css for flushing before they add
the delta, so a flush on another CPU can run in between and miss the
delta. The delta then waits for the next update of that memcg, or never
reaches the ancestors if the memcg is freed first.
Patch 2 sends the MEMCG_KMEM update in memcg_uncharge_kmem() through
account_kmem_nmi_safe(). memcg_uncharge_kmem() runs in NMI when
kmalloc_nolock() or kfree_nolock() evicts an object stock slot, and it
still calls mod_memcg_state(), whose per-CPU read-modify-writes are not
NMI safe there. Hitting it also needs slub_debug and a page size below
64K.
A separate series makes percpu_ref gets, puts and releases NMI safe.
The two series don't depend on each other.
Testing:
- Built for x86-64 (W=1 on mm/memcontrol.o at each patch), and for
ppc64le with 4K pages and ppc32 with LLVM, where
CONFIG_MEMCG_NMI_SAFETY_REQUIRES_ATOMIC=y.
- On x86-64 with ARCH_HAS_NMI_SAFE_THIS_CPU_OPS turned off, so that the
atomic NMI paths are used, a test runs kmalloc_nolock() and
kfree_nolock() in NMI, through a self NMI IPI, across 8 memcgs.
memcg_uncharge_kmem() ran in NMI 7 or 8 times per run, and afterwards
MEMCG_KMEM matched the page counter in every memcg. The race that
patch 1 fixes needs a flush to land between two instructions, and the
test does not reproduce it.
- The cgroup selftests test_core, test_kmem and test_memcontrol in a VM
give the same results with and without the series. The test cases
that fail in that VM also fail on the base kernel.
Shakeel Butt (2):
memcg: add NMI stat deltas before marking the css updated
memcg: make memcg_uncharge_kmem() NMI safe
mm/memcontrol.c | 17 ++++++++++++-----
1 file changed, 12 insertions(+), 5 deletions(-)
base-commit: d0d65ff2e433e741c23715e8359c4a248d432179
--
2.53.1-Meta
^ permalink raw reply [flat|nested] 3+ messages in thread
* [PATCH 1/2] memcg: add NMI stat deltas before marking the css updated
2026-12-04 21:57 [PATCH 0/2] memcg: fix memcg stat updates made in NMI Shakeel Butt
@ 2026-12-04 21:57 ` Shakeel Butt
2026-12-04 21:57 ` [PATCH 2/2] memcg: make memcg_uncharge_kmem() NMI safe Shakeel Butt
1 sibling, 0 replies; 3+ messages in thread
From: Shakeel Butt @ 2026-12-04 21:57 UTC (permalink / raw)
To: Andrew Morton
Cc: Johannes Weiner, Michal Hocko, Roman Gushchin, Muchun Song,
Alexei Starovoitov, Vlastimil Babka, Harry Yoo, Tejun Heo,
cgroups, linux-mm, linux-kernel, Meta kernel team, bpf
On CONFIG_MEMCG_NMI_SAFETY_REQUIRES_ATOMIC architectures,
account_kmem_nmi_safe() and account_slab_nmi_safe() keep stat updates
made in NMI in atomics, which flush_nmi_stats() folds in when the css
is flushed. Since commit 8dcb0ed834a3 ("memcg: cgroup: call
css_rstat_updated irrespective of in_nmi()"), they mark the css updated
first and add the delta second. Regular stat updates do it the other
way around.
With this order, a flush on another CPU can take the css off the update
list and run flush_nmi_stats() between the two steps. The delta then
lands after the flush, while the css is no longer on the update list.
It waits there until the next update marks the css again. If the memcg
is freed first, the delta never reaches the ancestors, because
css_rstat_exit() only flushes csses that are on the update list.
Add the delta first. If the css is not on the update list yet,
__css_rstat_updated() puts it there with fully ordered cmpxchg
operations, so a flush that takes the css off the list also sees the
delta. If the css is already on the list, a concurrent flush can still
miss the update. That is the race described in
css_process_update_tree(), which regular updates have too.
Fixes: 8dcb0ed834a3 ("memcg: cgroup: call css_rstat_updated irrespective of in_nmi()")
Signed-off-by: Shakeel Butt <shakeel.butt@linux.dev>
---
mm/memcontrol.c | 14 ++++++++++----
1 file changed, 10 insertions(+), 4 deletions(-)
diff --git a/mm/memcontrol.c b/mm/memcontrol.c
index b75a61ddeded..53e688083350 100644
--- a/mm/memcontrol.c
+++ b/mm/memcontrol.c
@@ -3016,12 +3016,15 @@ static inline void account_slab_nmi_safe(struct mem_cgroup *memcg,
} else {
struct mem_cgroup_per_node *pn = memcg->nodeinfo[pgdat->node_id];
- /* preemption is disabled in_nmi(). */
- __css_rstat_updated(&memcg->css, smp_processor_id());
if (idx == NR_SLAB_RECLAIMABLE_B)
atomic_add(nr, &pn->slab_reclaimable);
else
atomic_add(nr, &pn->slab_unreclaimable);
+ /*
+ * Mark the css after the update, like regular stat updates
+ * do. Preemption is disabled in_nmi().
+ */
+ __css_rstat_updated(&memcg->css, smp_processor_id());
}
}
#else
@@ -3240,9 +3243,12 @@ static inline void account_kmem_nmi_safe(struct mem_cgroup *memcg, int val)
if (likely(!in_nmi())) {
mod_memcg_state(memcg, MEMCG_KMEM, val);
} else {
- /* preemption is disabled in_nmi(). */
- __css_rstat_updated(&memcg->css, smp_processor_id());
atomic_add(val, &memcg->kmem_stat);
+ /*
+ * Mark the css after the update, like regular stat updates
+ * do. Preemption is disabled in_nmi().
+ */
+ __css_rstat_updated(&memcg->css, smp_processor_id());
}
}
#else
--
2.53.1-Meta
^ permalink raw reply related [flat|nested] 3+ messages in thread
* [PATCH 2/2] memcg: make memcg_uncharge_kmem() NMI safe
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
1 sibling, 0 replies; 3+ messages in thread
From: Shakeel Butt @ 2026-12-04 21:57 UTC (permalink / raw)
To: Andrew Morton
Cc: Johannes Weiner, Michal Hocko, Roman Gushchin, Muchun Song,
Alexei Starovoitov, Vlastimil Babka, Harry Yoo, Tejun Heo,
cgroups, linux-mm, linux-kernel, Meta kernel team, bpf
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
^ permalink raw reply related [flat|nested] 3+ messages in thread
end of thread, other threads:[~2026-12-04 21:57 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
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 ` [PATCH 2/2] memcg: make memcg_uncharge_kmem() NMI safe Shakeel Butt
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox