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=1796454420; b=P5uF2yM+eU7DzfDOqB9bhnrUYDrEgXWWuGFjTLNyR1Tay5JSYFwYJwYFq5YpI69d2HAa ++NC2ORDvk1Iu4g5zDmPuyjEQxWsKijAfVucYkxCQcO4n6j35CgUx3wdW16Fx3dNDaldz jzc7oiBd/bDD4pexAeHlKl7lNc9ryEeF8HAGe4ZZzALNrpGT7oE6ZcDdna2y3Gl0zflv/ UL+a8ecO9T+ZaO/PB2g+ANjxVPM9ddgJ4V17KVognYRVttnqasem2gioAW9oskfVAjG2e 8wB86q2XqFyrAvbIXlxvo3/2Wammp5l7Kp63LSE75xyre5Rzy/+W/304ri2LqRP8bNw== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796454420; h=Received:DKIM-Signature:Date:To:From:In-Reply-To:References:Subject: Message-Id; bh=fDIDaaTTD/Ojt/Um5xXs9kZBqxj8XGzj+NfKDUaoaic=; b=PyVDD1uRrWync2ELKJW+ux503huwQEQ0RR/ad8TaES32rziziPyaYIUN+kzq5kT3suJ+ SXIlKL5nk4RtMkOSPc3ZeFCqtBPKPrChpfcyvpYThkGwaoYdQWepRx5uCQhqQWQSsjIwV b+ddkA5TAWOivySUaARL0PesY8PVt+uvY7XB0YZ5jcReuE+dMYbsi7ssdAzdf5oKD/CNZ RS1Pwtf/e4UFXkOWVkQ8adWquPTuIlLG5aw7rkKzoPS/ydkxOPW6KYha0J18Px1RddgZn QxafL9RwS+2OC7gJMWStRbQXdLRRwHAvfwogE2HsljmKoK/rs3kdiX4RJg56QVA2OIw== 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="h2/3Jp1a"; 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 151C6606A322 for ; Sat, 5 Dec 2026 07:06:59 +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=h2/3Jp1a Received: by smtp.kernel.org (Postfix) with ESMTPSA id 8B21D1F0092B; Sat, 5 Dec 2026 07:06:59 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux-foundation.org; s=korg; t=1796454419; bh=fDIDaaTTD/Ojt/Um5xXs9kZBqxj8XGzj+NfKDUaoaic=; h=Date:To:From:In-Reply-To:References:Subject; b=h2/3Jp1aYW0xXTUyjAc0aHXNQeFX3NlHf5IOb+3VIYjDVIUkayY9BMZlYxbgGC1GA fv8ql56mMdTaIC6JBzq3NPxmgYeZUx1XCVcaYiP+fJlr/DzIcjRpgIpNP3gzhRF36R DnKYNHxNMVbEqHvRD4FIPPxERr/iQdgmabyeXJ+8= Date: Fri, 04 Dec 2026 23:06:59 -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-3-shakeel.butt@linux.dev> References: <20261204224016.325944-3-shakeel.butt@linux.dev> Subject: + percpu_ref-defer-the-release-function-when-the-last-put-is-in-nmi.patch added to mm-nonmm-unstable branch Message-Id: <20261205070659.8B21D1F0092B@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: defer the release function when the last put is in NMI has been added to the -mm mm-nonmm-unstable branch. Its filename is percpu_ref-defer-the-release-function-when-the-last-put-is-in-nmi.patch This patch will shortly appear at https://git.kernel.org/pub/scm/linux/kernel/git/akpm/25-new.git/tree/patches/percpu_ref-defer-the-release-function-when-the-last-put-is-in-nmi.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: defer the release function when the last put is in NMI Date: Fri, 04 Dec 2026 14:40:16 -0800 The last put can happen in NMI, but release functions are not NMI safe. css_release() calls queue_work(). obj_cgroup_release() takes a spinlock and frees memory. If the NMI interrupted a holder of one of those locks on the same CPU, it deadlocks. This can happen on any architecture, for example: - A BPF tracing program in NMI drops the last reference to a removed cgroup with bpf_cgroup_release(). - free_pages_nolock() or kfree_nolock() in NMI drops the last reference to the objcg of an offline memcg. - refill_stock() in NMI evicts a memcg stock slot that holds the last reference to an offline memcg. drain_all_stock() can leave such slots behind. This one doesn't happen on PREEMPT_RT, where the stocks are not used in NMI. When the count drops to zero in NMI, add the ref to a per-CPU list and call its release function from irq_work. The count is zero, so nobody can take a new reference in the meantime. Puts outside NMI don't change, and the new check only runs when the count reaches zero. The list node shares memory with ->rcu, so struct percpu_ref_data does not grow. This is safe because ->rcu is only used while switching to atomic mode, and the switch holds a reference while it uses ->rcu. A user that can drop its last reference in NMI must wait for the release function before it frees, reinitializes or resurrects the ref. The kernel-doc now says so. css and objcg refs already work this way. Backport note: this adds a call to irq_work_queue(). SLUB selects IRQ_WORK since commit af92793e52c3 ("slab: Introduce kmalloc_nolock() and kfree_nolock()."). Older kernels can be built without IRQ_WORK, so backports may need to enable it. Link: https://patch.msgid.link/20261204224016.325944-3-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 | 36 ++++++++++++++++-- lib/percpu-refcount.c | 59 +++++++++++++++++++++++++++--- 2 files changed, 87 insertions(+), 8 deletions(-) --- a/include/linux/percpu-refcount.h~percpu_ref-defer-the-release-function-when-the-last-put-is-in-nmi +++ a/include/linux/percpu-refcount.h @@ -51,6 +51,7 @@ #define _LINUX_PERCPU_REFCOUNT_H #include +#include #include #include #include @@ -112,7 +113,16 @@ struct percpu_ref_data { percpu_ref_func_t *confirm_switch; bool force_atomic:1; bool allow_reinit:1; - struct rcu_head rcu; + union { + /* used while switching to atomic mode */ + struct rcu_head rcu; + /* + * Queues the release when the last put is in NMI. The count + * can't reach zero while ->rcu is in use, because a switch + * holds a reference until its RCU callback is done with it. + */ + struct llist_node nmi_release_node; + }; struct percpu_ref *ref; }; @@ -145,6 +155,7 @@ void percpu_ref_kill_and_confirm(struct void percpu_ref_resurrect(struct percpu_ref *ref); void percpu_ref_reinit(struct percpu_ref *ref); bool percpu_ref_is_zero(struct percpu_ref *ref); +void __percpu_ref_release_nmi(struct percpu_ref *ref); /** * percpu_ref_kill - drop the initial ref @@ -368,13 +379,29 @@ static inline bool percpu_ref_tryget_liv return ret; } +/* + * Internal helper. Call the release function of @ref, whose count just + * dropped to zero. Release functions are not written to run in NMI, so in + * NMI the call is deferred to irq_work. + */ +static inline void __percpu_ref_release(struct percpu_ref *ref) +{ + if (unlikely(in_nmi())) + __percpu_ref_release_nmi(ref); + else + ref->data->release(ref); +} + /** * percpu_ref_put_many - decrement a percpu refcount * @ref: percpu_ref to put * @nr: number of references to put * * Decrement the refcount, and if 0, call the release function (which was passed - * to percpu_ref_init()) + * to percpu_ref_init()). If the count drops to 0 in NMI, the release function + * runs later from irq_work. A user that can drop the last reference in NMI + * must wait for its release function before it frees, reinitializes or + * resurrects @ref. * * This function is safe to call as long as @ref is between init and exit. */ @@ -387,7 +414,7 @@ static inline void percpu_ref_put_many(s if (__ref_is_percpu(ref, &percpu_count)) __percpu_ref_sub(ref, percpu_count, nr); else if (unlikely(atomic_long_sub_and_test(nr, &ref->data->count))) - ref->data->release(ref); + __percpu_ref_release(ref); rcu_read_unlock(); } @@ -397,7 +424,8 @@ static inline void percpu_ref_put_many(s * @ref: percpu_ref to put * * Decrement the refcount, and if 0, call the release function (which was passed - * to percpu_ref_init()) + * to percpu_ref_init()). If the count drops to 0 in NMI, the release function + * runs later from irq_work, see percpu_ref_put_many(). * * This function is safe to call as long as @ref is between init and exit. */ --- a/lib/percpu-refcount.c~percpu_ref-defer-the-release-function-when-the-last-put-is-in-nmi +++ a/lib/percpu-refcount.c @@ -3,6 +3,7 @@ #include #include +#include #include #include #include @@ -91,7 +92,8 @@ static unsigned long __percpu *percpu_co * to 0. See the definitions of PERCPU_REF_INIT_* flags for flag behaviors. * * Note that @release must not sleep - it may potentially be called from RCU - * callback context by percpu_ref_kill(). + * callback context by percpu_ref_kill(), or from irq_work if the last + * reference is dropped in NMI. */ int percpu_ref_init(struct percpu_ref *ref, percpu_ref_func_t *release, unsigned int flags, gfp_t gfp) @@ -445,7 +447,9 @@ EXPORT_SYMBOL_GPL(percpu_ref_kill_and_co * percpu_ref_is_zero - test whether a percpu refcount reached zero * @ref: percpu_ref to test * - * Returns %true if @ref reached zero. + * Returns %true if @ref reached zero. If the last reference was dropped in NMI, + * this can return %true before the release function has run, see + * percpu_ref_put_many(). * * This function is safe to call as long as @ref is between init and exit. */ @@ -476,7 +480,9 @@ EXPORT_SYMBOL_GPL(percpu_ref_is_zero); * * Re-initialize @ref so that it's in the same state as when it finished * percpu_ref_init() ignoring %PERCPU_REF_INIT_DEAD. @ref must have been - * initialized successfully and reached 0 but not exited. + * initialized successfully and reached 0 but not exited. If the last + * reference can be dropped in NMI, the release function must also have run, + * see percpu_ref_put_many(). * * Note that percpu_ref_tryget[_live]() are safe to perform on @ref while * this function is in progress. @@ -498,7 +504,8 @@ EXPORT_SYMBOL_GPL(percpu_ref_reinit); * * If @ref->release() frees @ref then the caller is responsible for * guaranteeing that @ref->release() does not get called while this - * function is in progress. + * function is in progress. That includes a release that + * percpu_ref_put_many() deferred from NMI. * * Note that percpu_ref_tryget[_live]() are safe to perform on @ref while * this function is in progress. @@ -521,3 +528,47 @@ void percpu_ref_resurrect(struct percpu_ spin_unlock_irqrestore(lock, flags); } EXPORT_SYMBOL_GPL(percpu_ref_resurrect); + +/* + * Release functions take locks, free memory and queue work, none of which is + * safe in NMI. When the last reference is dropped in NMI, the ref is queued + * here and its release function is called from irq_work on the same CPU. + */ +struct percpu_ref_nmi_release { + struct llist_head refs; + struct irq_work work; +}; + +static void percpu_ref_nmi_release_fn(struct irq_work *work); + +static DEFINE_PER_CPU(struct percpu_ref_nmi_release, percpu_ref_nmi_release) = { + .refs = LLIST_HEAD_INIT(refs), + .work = IRQ_WORK_INIT(percpu_ref_nmi_release_fn), +}; + +static void percpu_ref_nmi_release_fn(struct irq_work *work) +{ + struct percpu_ref_nmi_release *nmi_release = + container_of(work, struct percpu_ref_nmi_release, work); + struct percpu_ref_data *data, *next; + struct llist_node *pos; + + pos = llist_del_all(&nmi_release->refs); + /* _safe because the release function may free @data */ + llist_for_each_entry_safe(data, next, pos, nmi_release_node) + data->release(data->ref); +} + +/* + * Called by percpu_ref_put_many() when the count of @ref drops to zero in + * NMI. Don't use outside percpu-refcount proper. + */ +void __percpu_ref_release_nmi(struct percpu_ref *ref) +{ + struct percpu_ref_nmi_release *nmi_release = + this_cpu_ptr(&percpu_ref_nmi_release); + + llist_add(&ref->data->nmi_release_node, &nmi_release->refs); + irq_work_queue(&nmi_release->work); +} +EXPORT_SYMBOL_GPL(__percpu_ref_release_nmi); _ 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