From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=95.215.58.53 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796421444; b=B1lS53ak15Nd1lndLJGmhxECmAH83pXkQS/WgQBX/rlqPHAJOGh1q0C1+ZMQdTAlu8yY oMR0cyaq2ciDrxyfJ8F2RvfhgwPt11NOYE/5jtK8nbKmNsG5ufmq27wft+hGSqLX+5g5/ SYixgDjczkxxZBCyrRWw1LGurFqOfqpylEXeZ17U9GEXM4+hRPPXDwm0kwQ8wmWpT8mwq ZdcQGnvcW8gSX8o8A+VXzyKzO9XQBvhKOVA7NiZNElI03tv6JEk2iT0qFaET2pZebwxv1 29KFzHEQ9X2Sd/qO49msNOPhN/F0qbmABQIC/DEFSZwuY6fFvNWBqkI+zn5wym1krzw== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796421444; h=DMARC-Filter:X-Envelope-To:DKIM-Signature:X-Envelope-To:Received: X-Mizu-Trace-ID:X-Migadu-Flow:From:To:Cc:Subject:Date:Message-ID: X-Mailer:In-Reply-To:References:MIME-Version: Content-Transfer-Encoding; bh=zs7kFyJAO7P0Xz0KHFYYD7mngFzhhBRL42x4zQsfLPE=; b=lev9orf2gk4H+onULTIFYnAVqLzITp9yLZ5PmYocQxwRUK6loBxORSeMvfgZ5Pll0KYs jDeE4+YradgKLB+jI4fM5qb9h3NwQevUxdCcGInnA5cFteQ5hGO+Cpgee029n50WAaW5R vCb8yQ/8pUjMkS1ynoczy1Vpb7/H3trCfz124VwNT9elFOi9fIrikInvzwajUGhfUb2+H UhsHjpDkWdn5ebDJ8uwYijsZPeHrPZ0uScIEl+IBaRy9kuE1fr2Csg+sChRmz5of+r8Y0 RhfYwelhY322veLntUSA2MmAJ47ajkYLwwjkAZrUgaSUiYPaGN7LhaPX9oVFU0yReRw== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=linux.dev; spf=pass smtp.mailfrom=linux.dev; dkim=pass header.d=linux.dev header.i=@linux.dev header.a=rsa-sha256 header.s=key1 header.b=SKcNYCvi; arc=none smtp.remote-ip=95.215.58.53 Received: from mta1.migadu.com (out-53.mta1.migadu.com [95.215.58.53]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 12A31606A335 for ; Fri, 4 Dec 2026 21:57:23 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 12A31606A335 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.dev Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key, unprotected) header.d=linux.dev header.i=@linux.dev header.a=rsa-sha256 header.s=key1 header.b=SKcNYCvi X-Envelope-To: linux-kernel@vger.kernel.org DKIM-Signature: a=rsa-sha256; bh=9Ut/WOtIX/IT6tQ1WVFks8ZWaWPvdx/pBwRL+EflUCw=; c=simple/simple; d=linux.dev; h=from:to:subject:date:message-id:mime-version:content-type; s=key1; t=1796421442; v=1; x=1797026242; b=SKcNYCviiaWqP6sHModKu+rpKeq0NtJ3f83aeeAixrTzsMpjkQzg1aS72oyqnzUeOdEn0Yeh 6JaI//NU+YZIa6qs/SR6TvaFXkclVqFgp4V+OP3DgU1CinG0N/ATuZD6P4OY0bok3WTvwjz4FxO OJPuTmKpyonDU4kjaA5eUouI= X-Envelope-To: linux-kernel@vger.kernel.org Received: by smtp.migadu.com with ESMTPS id 7301218105de585b; Fri, 04 Dec 2026 21:57:22 +0000 X-Mizu-Trace-ID: 7301218105de585b X-Migadu-Flow: FLOW_OUT From: Shakeel Butt To: Andrew Morton Cc: Johannes Weiner , Michal Hocko , Roman Gushchin , Muchun Song , Alexei Starovoitov , Vlastimil Babka , Harry Yoo , Tejun Heo , cgroups@vger.kernel.org, linux-mm@kvack.org, linux-kernel@vger.kernel.org, Meta kernel team , 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 Message-ID: <20261204215709.4129739-2-shakeel.butt@linux.dev> X-Mailer: git-send-email 2.53.1 In-Reply-To: <20261204215709.4129739-1-shakeel.butt@linux.dev> References: <20261204215709.4129739-1-shakeel.butt@linux.dev> X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 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 --- 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