From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=100.103.45.18 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796454573; b=QITIVvmmQMq577Zhd5SqkY9JQ6dlTA8aHZEGIJUA6kIVHdUmwoHGo8ASF07eYZANsNJ7 KXg/TFSXErwHMys/YSLJeBeNPWYcKX0D2fY7jVBin8DtKkC0c4qbw3BCgI2yEi7+IKU1E ZWgPuXyiQZig8xD9FBfv23a4j+Fbdi+VRdaFaOQDPZ2qbcklSta0pH7YBP5VfiPDjBCZV 6OULl63UUoLD+pOy32E9o//Vtp5cDOaucBIONoFyTTpXauJS5l9ECFJlkQ4pxL6YrydNI NMTQ5xrt6fYH9S2g9O2Yb6SZkzpOr1qjDy+ViAdJWgei24Txc9T7UFHUwjWg1USAV7A== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796454573; h=Received:DKIM-Signature:Date:To:From:In-Reply-To:References:Subject: Message-Id; bh=y5l0Z+gLm8lUyflzgiIMiD7NpVDC0D/b8kW5RI+a7VU=; b=fhauv3l4WiTzMFbPt94qy9NhV3VCTaP2rdksHnGO7KnkyOIt7hPOgKRrX3F5/fJF2FD4 kuicDZQn5Yjivs8vOXEJRNQ2Ot1XVaq3Qfec879WIwv1cfANUol3etv53r9ZgyGKty7xB yx0PwO6Tj+4ysIHqmjA6qTwyU916haG8SYFT1YUIyWrt7cGZj0QCuGU2HUvbyalCQdCCW 6iie4cdka6NWBEKN8PXPSQ6jSynb71BPC0qs1cU8lBcQepfdK37XotLGxRHZMowYd0hgJ I+SRSuuT6UsHG8HOSGkcxWmFbKOSXOpUstlCftUqUwJGGUjY6LTFIKdwpnyjn1pzKDw== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=linux-foundation.org header.i=@linux-foundation.org header.a=rsa-sha256 header.s=korg header.b=HqFPfwA1; arc=none smtp.remote-ip=100.103.45.18 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id EB5D4606A322 for ; Sat, 5 Dec 2026 07:09:32 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux-foundation.org header.i=@linux-foundation.org header.a=rsa-sha256 header.s=korg header.b=HqFPfwA1 Received: by smtp.kernel.org (Postfix) with ESMTPSA id A41EB1F0092B; Sat, 5 Dec 2026 07:09:32 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux-foundation.org; s=korg; t=1796454572; bh=y5l0Z+gLm8lUyflzgiIMiD7NpVDC0D/b8kW5RI+a7VU=; h=Date:To:From:In-Reply-To:References:Subject; b=HqFPfwA14iDn6QG8CCIIidC9GUJDoqqb9O9kB+lS2PLHadIFgJWwGoe5o6Onmdt+X N5D0emJ4Ogc7mgYyANPNih29ClewwFY22xf29h2Y/GPMr/i+wF4YBXo1vLp0bannwb 7HCoBeOUt8n9uXo1OIoQT5afJuwiG5TYZcFvzv5o= Date: Fri, 04 Dec 2026 23:09:32 -0800 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 From: Andrew Morton In-Reply-To: <20261204215709.4129739-2-shakeel.butt@linux.dev> References: <20261204215709.4129739-2-shakeel.butt@linux.dev> Subject: + memcg-add-nmi-stat-deltas-before-marking-the-css-updated.patch added to mm-new branch Message-Id: <20261205070932.A41EB1F0092B@smtp.kernel.org> X-Mailing-List: mm-commits@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list 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 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 Signed-off-by: Andrew Morton Cc: Johannes Weiner Cc: Michal Hocko Cc: Roman Gushchin Cc: Muchun Song Cc: Alexei Starovoitov Cc: Vlastimil Babka Cc: Harry Yoo Cc: Tejun Heo 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