Linux cgroups development
 help / color / mirror / Atom feed
From: Shakeel Butt <shakeel.butt@linux.dev>
To: Dennis Zhou <dennis@kernel.org>, Tejun Heo <tj@kernel.org>,
	Christoph Lameter <cl@gentwo.org>
Cc: Andrew Morton <akpm@linux-foundation.org>,
	Johannes Weiner <hannes@cmpxchg.org>,
	Michal Hocko <mhocko@kernel.org>,
	Roman Gushchin <roman.gushchin@linux.dev>,
	Muchun Song <muchun.song@linux.dev>,
	Alexei Starovoitov <ast@kernel.org>,
	David Vernet <void@manifault.com>,
	Vlastimil Babka <vbabka@kernel.org>,
	cgroups@vger.kernel.org, linux-mm@kvack.org, bpf@vger.kernel.org,
	linux-kernel@vger.kernel.org,
	Meta kernel team <kernel-team@meta.com>
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	[thread overview]
Message-ID: <20261204224016.325944-2-shakeel.butt@linux.dev> (raw)
In-Reply-To: <20261204224016.325944-1-shakeel.butt@linux.dev>

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 <shakeel.butt@linux.dev>
---
 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 <linux/atomic.h>
 #include <linux/percpu.h>
+#include <linux/preempt.h>
 #include <linux/rcupdate.h>
 #include <linux/types.h>
 #include <linux/gfp.h>
@@ -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


  reply	other threads:[~2026-12-04 22:40 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-04 22:40 [PATCH 0/2] percpu_ref: make gets, puts and releases NMI safe Shakeel Butt
2026-12-04 22:40 ` Shakeel Butt [this message]
2026-12-04 22:40 ` [PATCH 2/2] percpu_ref: defer the release function when the last put is in NMI Shakeel Butt

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261204224016.325944-2-shakeel.butt@linux.dev \
    --to=shakeel.butt@linux.dev \
    --cc=akpm@linux-foundation.org \
    --cc=ast@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=cgroups@vger.kernel.org \
    --cc=cl@gentwo.org \
    --cc=dennis@kernel.org \
    --cc=hannes@cmpxchg.org \
    --cc=kernel-team@meta.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-mm@kvack.org \
    --cc=mhocko@kernel.org \
    --cc=muchun.song@linux.dev \
    --cc=roman.gushchin@linux.dev \
    --cc=tj@kernel.org \
    --cc=vbabka@kernel.org \
    --cc=void@manifault.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox