From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=91.218.175.112 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796424031; b=RQJh8eGS0Q4ooRgJQnv5VqTmUu2+nNJhQFYMhL7xPVu14HARXC3tNL6Rz7M9uCudrnya tFM7lZ5P0YPwVe9fW94fviq1qbD+l2wGzZsvIDl5UJMXDZAUrpNII7NQIox2uMrPryaz3 WXgc/RKghJahUYXvNR+rcXg6KEqFvLNB00FPohOMBJRZ+ulpvIEGupwBSRpxm+5/Ev5hN NA+xCz61CorLIEgaCr/fpuXfVX7gLRbdngrLdCU0CYHVb3zsGMHR/dsqS4bs56F/pTmzq 1ERuLKkofF4wF9MX1Z4qHxP3W7bR6M1SIsuShrF3v1o//NDyGCbIflBdB1Kp70UhaEQ== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796424031; 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=HeGNzG9J++qDY7YKAfy4uq29wp2WYiBH2vxyyiHEuEA=; b=ZzMLNHANSJl1KPhy9oklm9Yz3tp95zXhWpdBaKcCrPT6PtSpz6Ub3RV9s95cVLHCy+Xb s7o1aiGnjtAhBPdWi4LuBG+T3K1l7Wh+/rweOko3xaobFhCE4izxvRvaoSPK5K9WlmEtQ NyocgjE34kDDIdKsWu4npzyb75+FvjbPj6K3Wp4ZKgZIUPNzOJUO8IDSbN4zKyftdVaLw aExUvqZ+AxzPw/ZcTWlWR/bVWbgyWCwSTmNTz6dcMkof7M7wT/O+0yECpKIWhuVJXu7JD iOnYBb0vNFGnaqKc0hac3ddP8vC+41XVcBwRRcKNcsB8MUeVEmorsBfGtnWhBZ73lXA== 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=UGS1HKxA; arc=none smtp.remote-ip=91.218.175.112 Received: from mta0.migadu.com (out-112.mta0.migadu.com [91.218.175.112]) (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 25D73606A322 for ; Fri, 4 Dec 2026 22:40:28 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 25D73606A322 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=UGS1HKxA X-Envelope-To: cgroups@vger.kernel.org DKIM-Signature: a=rsa-sha256; bh=gnIHUeMuDNMb/Cu0K0RhBjmX+QlydJwcJQxIxb1r9cE=; c=simple/simple; d=linux.dev; h=from:to:subject:date:message-id:mime-version:content-type; s=key1; t=1796424026; v=1; x=1797028826; b=UGS1HKxAgsasQPiAk+9Kh+4fq2EbN1WwfEYG7sZxa64z5SAZ/+Ed8sPoLg6eztDC5zFUjKJH 5Uhlniu6dPZHXxAymuWs57BBH/KnqjyX+FlNZbQ1RqGW4/0rihQCy2X+d7xMzre8bBp9ni9oqUq 2mTMnlfax+Gwti8ojWu2Mgd4= X-Envelope-To: cgroups@vger.kernel.org Received: by smtp.migadu.com with ESMTPS id 48970c2db55c258f; Fri, 04 Dec 2026 22:40:23 +0000 X-Mizu-Trace-ID: 48970c2db55c258f X-Migadu-Flow: FLOW_OUT From: Shakeel Butt To: Dennis Zhou , Tejun Heo , Christoph Lameter Cc: Andrew Morton , Johannes Weiner , Michal Hocko , Roman Gushchin , Muchun Song , Alexei Starovoitov , David Vernet , Vlastimil Babka , cgroups@vger.kernel.org, linux-mm@kvack.org, bpf@vger.kernel.org, linux-kernel@vger.kernel.org, Meta kernel team Subject: [PATCH 0/2] percpu_ref: make gets, puts and releases NMI safe Date: Fri, 4 Dec 2026 14:40:14 -0800 Message-ID: <20261204224016.325944-1-shakeel.butt@linux.dev> X-Mailer: git-send-email 2.53.1 X-Mailing-List: cgroups@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 percpu_ref gets and puts can run in NMI. BPF tracing programs take and drop cgroup references there, and memcg charging from kmalloc_nolock() and alloc_pages_nolock() takes and drops css and objcg references. This series fixes two problems: 1. Where this_cpu ops are not NMI safe, as on powerpc and sparc64, a get or put in NMI can interrupt one on the same counter, and an update is lost. Patch 1 keeps NMI updates in a separate atomic counter and adds it in when the ref switches to atomic mode. 2. On any architecture, the last put can happen in NMI, but release functions are not NMI safe. Patch 2 runs the release function from irq_work when the count drops to zero in NMI. I fixed this in percpu_ref because the problem comes from both cgroup core (BPF cgroup kptrs) and memcg. The other option is to defer the release in css_release() and obj_cgroup_release() separately. Cost: patch 2 puts its list node in the same memory as ->rcu, so struct percpu_ref_data does not grow. Patch 1 adds a long to it and an in_nmi() test to gets and puts, but only where atomics are NMI safe and this_cpu ops are not. Nothing changes on x86, arm64, s390 or loongarch. Not covered: - Architectures without NMI safe atomics, such as mips. memcg already treats them as NMI unsafe (CONFIG_MEMCG_NMI_UNSAFE). - Deadlocks outside NMI, when the caller holds a lock that the release function needs. For example, a BPF program on the workqueue_queue_work tracepoint runs with the pool lock held. If it drops the last cgroup reference, queue_work() in css_release() can deadlock. percpu_ref can't detect this. The series is based on mm-nonmm-unstable, which has "percpu_ref: replace the global switch lock with hashed locks". A separate memcg series fixes memcg stat updates made in NMI. The two series don't depend on each other. Testing: - Builds with W=1 on x86-64, and with LLVM on ppc64le (4K and 64K pages) and ppc32, where the new NMI code is compiled in. pahole shows struct percpu_ref_data at 56 bytes on x86-64 (unchanged), 64 on ppc64le (was 56) and 32 on ppc32 (was 28). - An NMI test on x86-64, with ARCH_HAS_NMI_SAFE_THIS_CPU_OPS turned off so the new code runs. It enters NMI with a self NMI IPI. Each case ran three times: - Last put in NMI: the release runs once, from irq_work. Without patch 2 it runs in NMI. - Gets and puts in NMI in percpu mode: they go to the new counter, and the ref is released once, after percpu_ref_kill(). - A get of LONG_MAX in NMI, put back outside, then percpu_ref_kill(): one release, after the kill. If the NMI updates go to ->count instead, the ref is released during percpu_ref_kill() and again at the switch. - cgroup selftests test_core, test_kmem and test_memcontrol in a VM: same results as the base kernel. Shakeel Butt (2): percpu_ref: make get and put NMI safe where this_cpu ops are not percpu_ref: defer the release function when the last put is in NMI include/linux/percpu-refcount.h | 98 ++++++++++++++++++++++++++++++--- lib/percpu-refcount.c | 63 +++++++++++++++++++-- 2 files changed, 149 insertions(+), 12 deletions(-) base-commit: d2cb9e321d95e790c9e7517b4c556e9d3f592cfe -- 2.53.1-Meta