From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=91.218.175.104 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796421443; b=UJRn/8FLWL7swL6XZ8EywaDefHzzLfRRREjU3GQIqF7xKVETr39+bh+oVv+4QD0SOenb KT9Urdw4WGY5+sDjrJV5VbRUaxKB0UClo1uj+JR0m6NjnOjLjy8JYd4N5Fc1boNyPXYld Ex4Rlx5ylTa6t+aCyMwLa6tcb8eT8EQs//xOmkS8rCTis3eHUM9NjDtb9Pvmh5IKZAKnq AcPXUEPI4xljlZHc18UiOveOqiFYIP0pP/nKU4MGkIEGAjmxUOf87157hvLf3NLfSqPrq 57jgCnxZBMhBLd5BO7iSR+Ma0yuK/mxx/Rz1z920X9PkH0uZkdUzQxjSPeSv/OXSa7w== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796421443; 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:MIME-Version:Content-Transfer-Encoding; bh=Ay+dLfgGF4W3hHWYnn+yHfmIFmTjsVlLoHOYgEX1JFU=; b=sw53DLKlPQmhocjMr134Ld62Poa+7ivkbt0rprz7CsdsdaSHeMny90AwA+w8HjyZlxrk 5TOx+1DoBwFgA+t4K5Kf+AMSbzu9TwWXgeoI0I24cpMZ8Wsu7pNLj/ZhrXmZECmAT/Lh1 ktE6TEcxZs4v1G5ffKuqrWFMc2AoP7LZPFUixo8ls4cc2dGzQfn6ZiX3HUG2Ur0Q4azyv LIAFUULH12aZ3aZwj3hMLnHKSoVDpAMXPmKsPiDNMMd2bD2dpWOOmzk6S8wbAVz8bcqi0 BUzzhqPOLH9K0KiYiqc8mWM6RZljuf1c3eb87m2OAL2RBTIHFnJlTK0XnmTlPtt/krw== 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="aqH/8cBj"; arc=none smtp.remote-ip=91.218.175.104 Received: from mta0.migadu.com (out-104.mta0.migadu.com [91.218.175.104]) (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 F1BA8606A32C for ; Fri, 4 Dec 2026 21:57:21 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org F1BA8606A32C 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=aqH/8cBj X-Envelope-To: linux-kernel@vger.kernel.org DKIM-Signature: a=rsa-sha256; bh=GZNj3+0yIa9E7ajKLw78AIRoc6KUPyY0AGHPqoEKKIw=; c=simple/simple; d=linux.dev; h=from:to:subject:date:message-id:mime-version:content-type; s=key1; t=1796421440; v=1; x=1797026240; b=aqH/8cBjU5lKI8+zsksFo9AmIoprY45On3V3tDwp4IhPDc44GsRP2JFEwclvzPdYbWH/qfF7 WQOuWgSwreoeJ+DYhqa5HLxhP581c/7ISd/B1ND4BMgIq3vSrhljvNdBiMZiYVVE2XRmrIKo079 cIiu+qLCAxu3u+7X0nOpCtjU= X-Envelope-To: linux-kernel@vger.kernel.org Received: by smtp.migadu.com with ESMTPS id 20a3dc93b3141443; Fri, 04 Dec 2026 21:57:19 +0000 X-Mizu-Trace-ID: 20a3dc93b3141443 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 0/2] memcg: fix memcg stat updates made in NMI Date: Fri, 4 Dec 2026 13:57:07 -0800 Message-ID: <20261204215709.4129739-1-shakeel.butt@linux.dev> X-Mailer: git-send-email 2.53.1 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 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