Linux-mm Archive on lore.kernel.org
 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 0/2] memcg: fix memcg stat updates made in NMI
Date: Fri,  4 Dec 2026 13:57:07 -0800	[thread overview]
Message-ID: <20261204215709.4129739-1-shakeel.butt@linux.dev> (raw)

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



             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 Shakeel Butt [this message]
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

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