From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.subspace.kernel.org (Postfix) with ESMTP id A2F18606A32C for ; Fri, 4 Dec 2026 22:40:46 +0000 (UTC) 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=iZjOTB8m Received: by kanga.kvack.org (Postfix) id 808F56B05B5; Fri, 4 Dec 2026 17:40:35 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 7B9366B05B6; Fri, 4 Dec 2026 17:40:35 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 6CFB06B05B7; Fri, 4 Dec 2026 17:40:35 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id D094E6B05B5 for ; Fri, 4 Dec 2026 17:40:34 -0500 (EST) Received: from smtpin08.hostedemail.com (lb01a-stub [10.200.18.249]) by unirelay09.hostedemail.com (Postfix) with ESMTP id 483548061A for ; Fri, 4 Dec 2026 22:40:34 +0000 (UTC) X-FDA: 85507771188.08.5C62306 Received: from mta1.migadu.com (out-79.mta1.migadu.com [95.215.58.79]) by imf31.hostedemail.com (Postfix) with ESMTP id 3B37020005 for ; Fri, 4 Dec 2026 22:40:32 +0000 (UTC) Authentication-Results: imf31.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=iZjOTB8m; spf=pass (imf31.hostedemail.com: domain of shakeel.butt@linux.dev designates 95.215.58.79 as permitted sender) smtp.mailfrom=shakeel.butt@linux.dev; dmarc=pass (policy=none) header.from=linux.dev ARC-Seal: i=1; a=rsa-sha256; d=hostedemail.com; s=arc-20220608; cv=none; t=1796424032; b=0V9kWkMjF/+1IHZH5CoXUuXSl9duJgpyOjuIi4pt0CUU9SRgz7SaHxa+cQQZbW9LwoptKQ qd+zNz4MOnXWLfpk1uGicl57zkKyo1aevnlZtfItQOaFarxL0MDJqbVVlzCV0O0EH0fSI4 KusM6/L27/HlL8r+17zoIMcfc+1FedE= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1796424032; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=XL/GK2IIQ1heuwCjY01SlpVEaufu3v1y0zUZdIKks1g=; b=nclxIw2Lk0XLYic+OdV9k6vDEZ38f8lolFEz1PHIUScVpXPpqT1aN9sV+mZT56/5oEi9Jm fgXMliGizm6DdE9fuiw4w007KfeK826TQfofmh2vryZGDIbx6QLSPZWxfpac4Gwrg7Mqeq h8y6oL6RmSdi9KAFDewRmZplnMUBcyg= ARC-Authentication-Results: i=1; imf31.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=iZjOTB8m; spf=pass (imf31.hostedemail.com: domain of shakeel.butt@linux.dev designates 95.215.58.79 as permitted sender) smtp.mailfrom=shakeel.butt@linux.dev; dmarc=pass (policy=none) header.from=linux.dev X-Envelope-To: linux-mm@kvack.org DKIM-Signature: a=rsa-sha256; bh=tNcjGqnVvFSa+fmK+jKfsr5BKkeuHg5YK6yraJV2ea0=; c=simple/simple; d=linux.dev; h=from:to:subject:date:message-id:mime-version:content-type; s=key1; t=1796424030; v=1; x=1797028830; b=iZjOTB8mqwtSTy7YPrICSbQUvAJnnnzjfc/XLUe/P/mFZck8i+HdZprjuaefnpCtJCREloVW K04AMh+6Ji2nl9dE2+5yhzcuX5ncm190foZ/zYRFFs34iucK4l+rZ7QmvgPUsbH2rtSJtfL4oML J7+dmTcnhKZHvbQtDiHAWdjw= X-Envelope-To: linux-mm@kvack.org Received: by smtp.migadu.com with ESMTPS id ccfbaf284a92199e; Fri, 04 Dec 2026 22:40:29 +0000 X-Mizu-Trace-ID: ccfbaf284a92199e 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 1/2] percpu_ref: make get and put NMI safe where this_cpu ops are not Date: Fri, 4 Dec 2026 14:40:15 -0800 Message-ID: <20261204224016.325944-2-shakeel.butt@linux.dev> X-Mailer: git-send-email 2.53.1 In-Reply-To: <20261204224016.325944-1-shakeel.butt@linux.dev> References: <20261204224016.325944-1-shakeel.butt@linux.dev> X-Archived-By: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Rspam-User: X-Rspamd-Queue-Id: 3B37020005 X-Rspamd-Server: rspam03 X-Stat-Signature: 53wf1fecy3d6mynn94ts3qr8unbxpixh X-Spam-Status: No, score=-19.0 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER,INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_PASS, SPF_PASS,USER_AGENT_GIT autolearn=ham autolearn_force=no version=4.0.1 X-HE-Tag: 1796424031-398385 X-HE-Meta: U2FsdGVkX1/00Lmn5q2fonGKzxLlEbt7muDulLCJ1hYR8qbhbyr0NX9Kzr5qsjvS3Io7/YlUARQZOJXGyLYjRMXsve1hBjmevEd4bD5SUHAJHlYjX67nKMVc2BpTe7la0crSjo8toHjqYnbdiMUIc3VGuva2kO1vyRx0f84PzBhjrSo3MWpfG+nnCo9MGAw/S7gE2yhi6jtpIl/b29K1AbtPgFxwf0Wufi0RKEaVpJCLo6jFHrjYdRAQqwMwgJiYw7M+E8bGqSalOjg0OWo/GuGcnyBBwtNV2KBt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mF/hISK6hU4bEza6wNsVOmyp0yu/wdg9h0/ibTG/qQgiE5IIuti/c+lN7ERt1mo0e7r3Ffr/ACMdhImh5AjEovvYczoITH1/cICgwmzaWAlZnTotmjejfSFMQT8= Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: X-Spam-Checker-Version: SpamAssassin 4.0.1 (2024-03-26) on aws-us-west-2-korg-listserv-alma10-1 percpu_ref gets and puts can run in NMI. BPF tracing programs take and drop cgroup references there. memcg charging from kmalloc_nolock() and alloc_pages_nolock() takes and drops css and objcg references there. Without ARCH_HAS_NMI_SAFE_THIS_CPU_OPS, this_cpu_add() is a load and a store with interrupts disabled, and an NMI can land between the two. If the NMI updates the same counter, one of the updates is lost. A lost get can cause a use-after-free, and a lost put a leak. This affects powerpc and sparc64, for example. In NMI, keep percpu mode gets and puts in a separate atomic counter, ->nmi_count. The switch to atomic mode adds it to ->count along with the percpu counters. The switch waits for an RCU grace period first, and gets and puts run under rcu_read_lock(), so no update is missed. The NMI updates can't go to ->count itself. percpu_ref_kill() tests ->count for zero before the switch is done, and only PERCPU_COUNT_BIAS keeps it from reaching zero. References taken in NMI and dropped outside it, or the reverse, would move ->count for good. On 32-bit, about 2^31 of them would let that test release a ref that is still in use. This needs NMI safe atomics, so it is only built where ARCH_HAVE_NMI_SAFE_CMPXCHG is set and this_cpu ops are not NMI safe. There, gets and puts gain an in_nmi() test, and struct percpu_ref_data grows by a long but stays in the same kmalloc bucket. Nothing changes elsewhere. Fixes: fda01efc6160 ("bpf: Enable cgroups to be used as kptrs") Fixes: e8d78dbd0199 ("mm, bpf: Use memcg in try_alloc_pages().") Signed-off-by: Shakeel Butt --- include/linux/percpu-refcount.h | 62 ++++++++++++++++++++++++++++++--- lib/percpu-refcount.c | 4 +++ 2 files changed, 62 insertions(+), 4 deletions(-) diff --git a/include/linux/percpu-refcount.h b/include/linux/percpu-refcount.h index 1e3212e2c827..c331f4b5b5c5 100644 --- a/include/linux/percpu-refcount.h +++ b/include/linux/percpu-refcount.h @@ -52,6 +52,7 @@ #include #include +#include #include #include #include @@ -92,8 +93,21 @@ enum { PERCPU_REF_ALLOW_REINIT = 1 << 2, }; +/* + * Where this_cpu operations are not NMI safe but atomics are, gets and puts + * made in NMI while the ref is in percpu mode go to ->nmi_count. See + * __percpu_ref_add(). + */ +#if !defined(CONFIG_ARCH_HAS_NMI_SAFE_THIS_CPU_OPS) && \ + defined(CONFIG_ARCH_HAVE_NMI_SAFE_CMPXCHG) +#define PERCPU_REF_NMI_COUNT +#endif + struct percpu_ref_data { atomic_long_t count; +#ifdef PERCPU_REF_NMI_COUNT + atomic_long_t nmi_count; +#endif percpu_ref_func_t *release; percpu_ref_func_t *confirm_switch; bool force_atomic:1; @@ -186,6 +200,46 @@ static inline bool __ref_is_percpu(struct percpu_ref *ref, return true; } +/* + * Internal helpers. Add or subtract @nr while @ref is in percpu mode. + * + * Where this_cpu operations are not NMI safe, they only disable interrupts, + * which does not keep NMIs out. If an NMI changed a percpu counter in the + * middle of such an operation on the same CPU, one of the two changes would + * be lost. So in NMI, change ->nmi_count instead. The switch to atomic mode + * adds it to ->count together with the percpu counters, after an RCU grace + * period that waits for this change to finish. ->count itself must not take + * the change: it holds PERCPU_COUNT_BIAS until the switch, and + * percpu_ref_kill() tests it for zero before the switch is done. + * + * This needs NMI safe atomics. Without them, percpu_ref is not NMI safe. + */ +static __always_inline void __percpu_ref_add(struct percpu_ref *ref, + unsigned long __percpu *percpu_count, + unsigned long nr) +{ +#ifdef PERCPU_REF_NMI_COUNT + if (in_nmi()) { + atomic_long_add(nr, &ref->data->nmi_count); + return; + } +#endif + this_cpu_add(*percpu_count, nr); +} + +static __always_inline void __percpu_ref_sub(struct percpu_ref *ref, + unsigned long __percpu *percpu_count, + unsigned long nr) +{ +#ifdef PERCPU_REF_NMI_COUNT + if (in_nmi()) { + atomic_long_sub(nr, &ref->data->nmi_count); + return; + } +#endif + this_cpu_sub(*percpu_count, nr); +} + /** * percpu_ref_get_many - increment a percpu refcount * @ref: percpu_ref to get @@ -202,7 +256,7 @@ static inline void percpu_ref_get_many(struct percpu_ref *ref, unsigned long nr) rcu_read_lock(); if (__ref_is_percpu(ref, &percpu_count)) - this_cpu_add(*percpu_count, nr); + __percpu_ref_add(ref, percpu_count, nr); else atomic_long_add(nr, &ref->data->count); @@ -241,7 +295,7 @@ static inline bool percpu_ref_tryget_many(struct percpu_ref *ref, rcu_read_lock(); if (__ref_is_percpu(ref, &percpu_count)) { - this_cpu_add(*percpu_count, nr); + __percpu_ref_add(ref, percpu_count, nr); ret = true; } else { ret = atomic_long_add_unless(&ref->data->count, nr, 0); @@ -281,7 +335,7 @@ static inline bool percpu_ref_tryget_live_rcu(struct percpu_ref *ref) WARN_ON_ONCE(!rcu_read_lock_held()); if (likely(__ref_is_percpu(ref, &percpu_count))) { - this_cpu_inc(*percpu_count); + __percpu_ref_add(ref, percpu_count, 1); ret = true; } else if (!(ref->percpu_count_ptr & __PERCPU_REF_DEAD)) { ret = atomic_long_inc_not_zero(&ref->data->count); @@ -331,7 +385,7 @@ static inline void percpu_ref_put_many(struct percpu_ref *ref, unsigned long nr) rcu_read_lock(); if (__ref_is_percpu(ref, &percpu_count)) - this_cpu_sub(*percpu_count, nr); + __percpu_ref_sub(ref, percpu_count, nr); else if (unlikely(atomic_long_sub_and_test(nr, &ref->data->count))) ref->data->release(ref); diff --git a/lib/percpu-refcount.c b/lib/percpu-refcount.c index 538741162758..5def04e3a2c3 100644 --- a/lib/percpu-refcount.c +++ b/lib/percpu-refcount.c @@ -210,6 +210,10 @@ static void percpu_ref_switch_to_atomic_rcu(struct rcu_head *rcu) for_each_possible_cpu(cpu) count += *per_cpu_ptr(percpu_count, cpu); +#ifdef PERCPU_REF_NMI_COUNT + /* the gets and puts made in NMI, see __percpu_ref_add() */ + count += atomic_long_xchg(&data->nmi_count, 0); +#endif pr_debug("global %lu percpu %lu\n", atomic_long_read(&data->count), count); -- 2.53.1-Meta