BPF List
 help / color / mirror / Atom feed
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 1/2] memcg: add NMI stat deltas before marking the css updated
Date: Fri,  4 Dec 2026 13:57:08 -0800	[thread overview]
Message-ID: <20261204215709.4129739-2-shakeel.butt@linux.dev> (raw)
In-Reply-To: <20261204215709.4129739-1-shakeel.butt@linux.dev>

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


  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 ` Shakeel Butt [this message]
2026-12-04 21:57 ` [PATCH 2/2] memcg: make memcg_uncharge_kmem() NMI safe Shakeel Butt

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