From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=100.103.45.18 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796454417; b=N7qsY6+seGJb8RKZG0GQk3FWrvrhavkFEF/NdESEo5vS2sOg/R9QRaO9jRqU7+WMvruf aLtAyxPAYY1IdFo0htKbC488Ae6swlUEDOec2rxvMyO0Q0gdymj4+r9HV0LB6iw2nrrrF wVsSLg0FEzkD7+4PgqicNetsxWGh8ogSLQf1g1umxtjikWD/J5iC+u0J2/gK34OZNhyA2 0xAIqUtBMeeq/dNYqmU8YpXn5vP8z1iyBtHtimkJTycH0HnMErzUDdC7nqMljBY0hLcpy +7kzTRNveuosqXv9zwcce+1v/hA5u25vUgxkPcuTa0i2FShZ1grTUmM9NNE95rfHf8A== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796454417; h=Received:DKIM-Signature:Date:To:From:In-Reply-To:References:Subject: Message-Id; bh=h4mTH6/5NojbAUrkkl056bVeCHyHuBEBX50a58Cg4Xo=; b=TJe5gq2osVd6xO9lkDf0EblvKJY16qGZzcr5067AMzllR4tFE926UWUdYFg4z5meGGQY m287hByB/2/Ou6p1o+IsrVYPLAqBxRpK3IYq4xOUk0tIVdC2Ctq4xWqCsE3RXoBJMk6dR Xtu9WHYrDEw6wUz30bbzdeKc/p8UzjEY85Bw4W99jMd1fe5me+6gS9XVVb38xeRXUbImD 2Zly0JTyEn4DsaMqUx6vZCJh3vfMiRfbxsqp55/9iLm+qGENw+l+z8UpyeKFgkwl6Jw1B fwLG8sgyRWVuk+iTSgZX5KvFIlC9wnVdbOTvkPK+Eb4HAlTElWPN4HJEyDV8SoCOCvA== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=linux-foundation.org header.i=@linux-foundation.org header.a=rsa-sha256 header.s=korg header.b=nil8H2Ly; arc=none smtp.remote-ip=100.103.45.18 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 4FA10606A322 for ; Sat, 5 Dec 2026 07:06:56 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux-foundation.org header.i=@linux-foundation.org header.a=rsa-sha256 header.s=korg header.b=nil8H2Ly Received: by smtp.kernel.org (Postfix) with ESMTPSA id A45911F0092B; Sat, 5 Dec 2026 07:06:56 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux-foundation.org; s=korg; t=1796454416; bh=h4mTH6/5NojbAUrkkl056bVeCHyHuBEBX50a58Cg4Xo=; h=Date:To:From:In-Reply-To:References:Subject; b=nil8H2Ly06RI6U7rQupIpb/j0NWrkERrYwlVpKI/9gt7VH/7cXSqrIvGGnwtfaC2F DAA0gT/YMJYXM/UeEpNMn7haRnhw6/IgVnUVl7r0rnSmQKE2XQ0v8Mup07vTNoetjX grNGL6Vq/o9fszd/O+JEET4notAwP55WGn8X8Yjs= Date: Fri, 04 Dec 2026 23:06:56 -0800 To: mm-commits@vger.kernel.org,void@manifault.com,vbabka@kernel.org, tj@kernel.org,roman.gushchin@linux.dev,muchun.song@linux.dev, mhocko@kernel.org,hannes@cmpxchg.org,dennis@kernel.org,cl@gentwo.org, ast@kernel.org,shakeel.butt@linux.dev,akpm@linux-foundation.org From: Andrew Morton In-Reply-To: <20261204224016.325944-2-shakeel.butt@linux.dev> References: <20261204224016.325944-2-shakeel.butt@linux.dev> Subject: + percpu_ref-make-get-and-put-nmi-safe-where-this_cpu-ops-are-not.patch added to mm-nonmm-unstable branch Message-Id: <20261205070656.A45911F0092B@smtp.kernel.org> X-Mailing-List: mm-commits@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list The patch titled Subject: percpu_ref: make get and put NMI safe where this_cpu ops are not has been added to the -mm mm-nonmm-unstable branch. Its filename is percpu_ref-make-get-and-put-nmi-safe-where-this_cpu-ops-are-not.patch This patch will shortly appear at https://git.kernel.org/pub/scm/linux/kernel/git/akpm/25-new.git/tree/patches/percpu_ref-make-get-and-put-nmi-safe-where-this_cpu-ops-are-not.patch This patch will later appear in the mm-nonmm-unstable branch at git://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm Before you just go and hit "reply", please: a) Consider who else should be cc'ed b) Prefer to cc a suitable mailing list as well c) Ideally: find the original patch on the mailing list and do a reply-to-all to that, adding suitable additional cc's *** Remember to use Documentation/process/submit-checklist.rst when testing your code *** The -mm tree is included into linux-next via various branches at git://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm and is updated there most days ------------------------------------------------------ From: Shakeel Butt Subject: percpu_ref: make get and put NMI safe where this_cpu ops are not Date: Fri, 04 Dec 2026 14:40:15 -0800 Patch series "percpu_ref: make gets, puts and releases NMI safe". 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. This patch (of 2): 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. Link: https://lore.kernel.org/20261204224016.325944-1-shakeel.butt@linux.dev Link: https://patch.msgid.link/20261204224016.325944-2-shakeel.butt@linux.dev 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 Signed-off-by: Andrew Morton Cc: Dennis Zhou Cc: Tejun Heo Cc: Christoph Lameter Cc: Johannes Weiner Cc: Michal Hocko Cc: Roman Gushchin Cc: Muchun Song Cc: Alexei Starovoitov Cc: David Vernet Cc: Vlastimil Babka include/linux/percpu-refcount.h | 62 ++++++++++++++++++++++++++++-- lib/percpu-refcount.c | 4 + 2 files changed, 62 insertions(+), 4 deletions(-) --- a/include/linux/percpu-refcount.h~percpu_ref-make-get-and-put-nmi-safe-where-this_cpu-ops-are-not +++ a/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(struc 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(s 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_man 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_liv 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(s 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); --- a/lib/percpu-refcount.c~percpu_ref-make-get-and-put-nmi-safe-where-this_cpu-ops-are-not +++ a/lib/percpu-refcount.c @@ -210,6 +210,10 @@ static void percpu_ref_switch_to_atomic_ 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); _ Patches currently in -mm which might be from shakeel.butt@linux.dev are memcg-move-memoryhigh-enforcement-out-of-try_charge_memcg.patch memcg-force-charges-from-reclaim-before-trying-the-limits.patch memcg-use-high_work-for-kernel-threads.patch memcg-use-high_work-for-remote-charges.patch memcg-simplify-high-limit-handling-after-a-charge.patch memcg-warn-when-memory-cgroup-ids-run-out.patch kernfs-report-times-and-link-count-from-the-node-in-getattr.patch kernfs-dont-copy-times-and-link-count-into-the-inode-on-refresh.patch kernfs-dont-take-kernfs_iattr_rwsem-for-the-directory-counters.patch kernfs-dont-take-kernfs_iattr_rwsem-to-publish-kernfs_removing.patch kernfs-update-a-directorys-times-without-kernfs_iattr_rwsem.patch kernfs-dont-take-kernfs_iattr_rwsem-in-__kernfs_remove.patch cgroup-dont-lose-a-task-in-css_task_iter-when-a-migration-fails.patch cgroup-kill-a-child-forked-while-cgroupkill-runs.patch cgroup-refuse-cgroupkill-below-a-threaded-cgroup.patch cgroup-freezer-check-own-tasks-and-descendants-for-the-frozen-state.patch cgroup-freezer-dont-update-the-frozen-state-of-removed-cgroups.patch cgroup-freezer-test-the-freeze-trap-bit-under-siglock.patch selftests-cgroup-test-cgroupprocs-reads-across-a-failed-migration.patch selftests-cgroup-test-the-frozen-state-of-a-cgroup-with-tasks-and-children.patch selftests-cgroup-test-cgroupkill-in-and-below-a-threaded-cgroup.patch cgroup-factor-out-cgroup_kn_break_active-and-cgroup_kn_unbreak_active.patch cgroup-dont-hold-cgroup_mutex-for-cgroupkill.patch cgroup-freezer-dont-hold-cgroup_mutex-for-cgroupfreeze.patch kernfs-dont-look-for-open-files-of-directories-when-draining.patch kernfs-add-a-sequence-count-to-directories.patch kernfs-list-directories-without-kernfs_rwsem.patch percpu_ref-make-get-and-put-nmi-safe-where-this_cpu-ops-are-not.patch percpu_ref-defer-the-release-function-when-the-last-put-is-in-nmi.patch