From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=91.218.175.109 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796421445; b=WfhmHuytswzuCqcg6TmE7spuhaBEZ7/+j0wHxdRR8ZgCZcxe0iosDXn3vnRGmhakmSeS e6q2fkGb8HmvL+H4uhZfQfcyR9PJQSFSaxJ1I38beu4M1kPMQcvP/XVb3Jn3R6FyAIu91 if2QA3Ay3uyEcfFL+VfxT/rDldAIsriunZV+fggE4fkbmKOl1j3s+rBgSM7mVSQCXgph1 jgZNGLGWGSXy8oD168CtuE29gux9EQGUUqWi4F7jDx/SRF4i5M/8zmUBHTYnpNmUPjUQF gFFs8Pf71wRxbqit1gvYA5GFFp6cN2VSpI2EzndKo5pUuK2QMp9CqF/PCP6036RCFFQ== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796421445; 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=ZHF7yG1A83/mR6U7i0nQPejkEgLzZox9Ic59eTbCw5WjcGmuBWKoUW8gdv0NEk4F3uHX WNE2xT7tgUCyTzLK7G1F2Det2sTcpQltdYCprE8OFK7+2x1f2UmN4cX/7hsuoqDOxOAny orjq7rMxpN/EVijFE+3/VHHhKEtSv1VKWvGOX94My7BZaCIoBCpQabYJroDeOGkVvedLd uktoSfx2+0k21CNZnn9R6m/NW492BYHdZ1k7i4ALLU6A5B9N+c6VZr2Nv7085wH6lB6Tm JIoD5U+DbnHoHvivbDRX+PuNXkWCDBV7cFhkIuNp0jxB1ij09Nusm/9sTIlhNwBDdCA== 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=91.218.175.109 Received: from mta0.migadu.com (out-109.mta0.migadu.com [91.218.175.109]) (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 77AFA606A33C for ; Fri, 4 Dec 2026 21:57:24 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 77AFA606A33C 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: bpf@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: bpf@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: bpf@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