From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=95.215.58.48 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796421444; b=QgN5ayFpj8jbKL6yvyyrlsCSHr1sH3CfSph5I+zX7Pilyodv0h/aRhIptHtexIdAhM+4 DSqkINbxy5v9IZIkcgxvBFCggkhzgyASZ73/fGFW5G5YiVzyNJVvFU2fFi1VdJm/SAm1E RsQyic3rGb30QxQKLODjaVxktnruJagRBlACO3vm+KbPBHpGXx+wOIOcqxYkX52Y2+bZb oPBWOeg6gCnugAoIZ4i8N5piqQlkQ4SpgsgI/PnlSFYvp4aB1CtTv9r0OEDMNSP6v7JC+ u8enNXstZKDPrB12oteSS2FmCjzQNKhiRyUEtQ7nCgSCpN78P30L/WOQaXpHxPSmRoA== 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:MIME-Version:Content-Transfer-Encoding; bh=Ay+dLfgGF4W3hHWYnn+yHfmIFmTjsVlLoHOYgEX1JFU=; b=py0rM6g+1uLphPPRnhzwgwotqo7HsoF4ekmPhB++N65KqO8RVf22gh+VjTDMzLi2p6h+ fx7JESnZZTxoQoRyk6a39RgKOXwhnYzjlEZWyI63t4tZEDto85H2LyNBDYQ1ijzrzIAAM nFqgA8Z1gxTsUYGvNv1MrYebOiUX9EbZPy+HtH7Nw6OtGiuJudxc4pHmqacwfASs051Ta 7YdurBYeWX3QHPi8AIWyY5FJmBAQMvSzxhhbj/AeYSXVOFZFgj8PLO9DlMHaD9oxP+BWl yZuwZIfW/Ch5h9ErSUpmRVgGtiZNZP2l8I97qKsUgJVkxV4m2cLqIGIjobbUfcOErlA== 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=95.215.58.48 Received: from mta1.migadu.com (out-48.mta1.migadu.com [95.215.58.48]) (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 0BE8D606A32D for ; Fri, 4 Dec 2026 21:57:21 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 0BE8D606A32D 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: bpf@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: bpf@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: 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 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