All of lore.kernel.org
 help / color / mirror / Atom feed
From: Andrew Morton <akpm@linux-foundation.org>
To: mm-commits@vger.kernel.org,vbabka@kernel.org,tj@kernel.org,
	roman.gushchin@linux.dev,muchun.song@linux.dev,mhocko@kernel.org,
	harry@kernel.org,hannes@cmpxchg.org,ast@kernel.org,shakeel.butt@linux.dev,
	akpm@linux-foundation.org
Subject: + memcg-add-nmi-stat-deltas-before-marking-the-css-updated.patch added to mm-new branch
Date: Fri, 04 Dec 2026 23:09:32 -0800	[thread overview]
Message-ID: <20261205070932.A41EB1F0092B@smtp.kernel.org> (raw)
In-Reply-To: <20261204215709.4129739-2-shakeel.butt@linux.dev>


The patch titled
     Subject: memcg: add NMI stat deltas before marking the css updated
has been added to the -mm mm-new branch.  Its filename is
     memcg-add-nmi-stat-deltas-before-marking-the-css-updated.patch

This patch will shortly appear at
     https://git.kernel.org/pub/scm/linux/kernel/git/akpm/25-new.git/tree/patches/memcg-add-nmi-stat-deltas-before-marking-the-css-updated.patch

This patch will later appear in the mm-new branch at
    git://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm

Note, mm-new is a provisional staging ground for work-in-progress
patches, and acceptance into mm-new is a notification for others take
notice and to finish up reviews.  Please do not hesitate to respond to
review feedback and post updated versions to replace or incrementally
fixup patches in mm-new.

The mm-new branch of mm.git is not included in linux-next

If a few days of testing in mm-new is successful, the patch will me moved
into mm.git's mm-unstable branch, which is included in linux-next

Before you just go and hit "reply", please:
   a) Consider who else should be cc'ed
   b) Prefer to cc a suitable mailing list as well
   c) Ideally: find the original patch on the mailing list and do a
      reply-to-all to that, adding suitable additional cc's

*** Remember to use Documentation/process/submit-checklist.rst when testing your code ***

The -mm tree is included into linux-next via various
branches at git://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm
and is updated there most days

------------------------------------------------------
From: Shakeel Butt <shakeel.butt@linux.dev>
Subject: memcg: add NMI stat deltas before marking the css updated
Date: Fri, 04 Dec 2026 13:57:08 -0800

Patch series "memcg: fix memcg stat updates made in NMI".

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.


This patch (of 2):

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.

Link: https://lore.kernel.org/20261204215709.4129739-1-shakeel.butt@linux.dev
Link: https://patch.msgid.link/20261204215709.4129739-2-shakeel.butt@linux.dev
Fixes: 8dcb0ed834a3 ("memcg: cgroup: call css_rstat_updated irrespective of in_nmi()")
Signed-off-by: Shakeel Butt <shakeel.butt@linux.dev>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Cc: Johannes Weiner <hannes@cmpxchg.org>
Cc: Michal Hocko <mhocko@kernel.org>
Cc: Roman Gushchin <roman.gushchin@linux.dev>
Cc: Muchun Song <muchun.song@linux.dev>
Cc: Alexei Starovoitov <ast@kernel.org>
Cc: Vlastimil Babka <vbabka@kernel.org>
Cc: Harry Yoo <harry@kernel.org>
Cc: Tejun Heo <tj@kernel.org>

 mm/memcontrol.c |   14 ++++++++++----
 1 file changed, 10 insertions(+), 4 deletions(-)

--- a/mm/memcontrol.c~memcg-add-nmi-stat-deltas-before-marking-the-css-updated
+++ a/mm/memcontrol.c
@@ -3016,12 +3016,15 @@ static inline void account_slab_nmi_safe
 	} 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
 	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
_

Patches currently in -mm which might be from shakeel.butt@linux.dev are

memcg-move-memoryhigh-enforcement-out-of-try_charge_memcg.patch
memcg-force-charges-from-reclaim-before-trying-the-limits.patch
memcg-use-high_work-for-kernel-threads.patch
memcg-use-high_work-for-remote-charges.patch
memcg-simplify-high-limit-handling-after-a-charge.patch
memcg-warn-when-memory-cgroup-ids-run-out.patch
memcg-add-nmi-stat-deltas-before-marking-the-css-updated.patch
memcg-make-memcg_uncharge_kmem-nmi-safe.patch
kernfs-report-times-and-link-count-from-the-node-in-getattr.patch
kernfs-dont-copy-times-and-link-count-into-the-inode-on-refresh.patch
kernfs-dont-take-kernfs_iattr_rwsem-for-the-directory-counters.patch
kernfs-dont-take-kernfs_iattr_rwsem-to-publish-kernfs_removing.patch
kernfs-update-a-directorys-times-without-kernfs_iattr_rwsem.patch
kernfs-dont-take-kernfs_iattr_rwsem-in-__kernfs_remove.patch
cgroup-dont-lose-a-task-in-css_task_iter-when-a-migration-fails.patch
cgroup-kill-a-child-forked-while-cgroupkill-runs.patch
cgroup-refuse-cgroupkill-below-a-threaded-cgroup.patch
cgroup-freezer-check-own-tasks-and-descendants-for-the-frozen-state.patch
cgroup-freezer-dont-update-the-frozen-state-of-removed-cgroups.patch
cgroup-freezer-test-the-freeze-trap-bit-under-siglock.patch
selftests-cgroup-test-cgroupprocs-reads-across-a-failed-migration.patch
selftests-cgroup-test-the-frozen-state-of-a-cgroup-with-tasks-and-children.patch
selftests-cgroup-test-cgroupkill-in-and-below-a-threaded-cgroup.patch
cgroup-factor-out-cgroup_kn_break_active-and-cgroup_kn_unbreak_active.patch
cgroup-dont-hold-cgroup_mutex-for-cgroupkill.patch
cgroup-freezer-dont-hold-cgroup_mutex-for-cgroupfreeze.patch
kernfs-dont-look-for-open-files-of-directories-when-draining.patch
kernfs-add-a-sequence-count-to-directories.patch
kernfs-list-directories-without-kernfs_rwsem.patch
percpu_ref-make-get-and-put-nmi-safe-where-this_cpu-ops-are-not.patch
percpu_ref-defer-the-release-function-when-the-last-put-is-in-nmi.patch


  reply	other threads:[~2026-12-05  7:09 UTC|newest]

Thread overview: 5+ 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-05  7:09   ` Andrew Morton [this message]
2026-12-04 21:57 ` [PATCH 2/2] memcg: make memcg_uncharge_kmem() NMI safe Shakeel Butt
2026-12-05  7:09   ` + memcg-make-memcg_uncharge_kmem-nmi-safe.patch added to mm-new branch Andrew Morton

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=20261205070932.A41EB1F0092B@smtp.kernel.org \
    --to=akpm@linux-foundation.org \
    --cc=ast@kernel.org \
    --cc=hannes@cmpxchg.org \
    --cc=harry@kernel.org \
    --cc=mhocko@kernel.org \
    --cc=mm-commits@vger.kernel.org \
    --cc=muchun.song@linux.dev \
    --cc=roman.gushchin@linux.dev \
    --cc=shakeel.butt@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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.