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 DF9A6606A32C for ; Fri, 4 Dec 2026 22:40:55 +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=he/5dFZg Received: by kanga.kvack.org (Postfix) id D12C56B05B7; Fri, 4 Dec 2026 17:40:54 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id CEA456B05B8; Fri, 4 Dec 2026 17:40:54 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id BDBE76B05B9; Fri, 4 Dec 2026 17:40:54 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id 417156B05B7 for ; Fri, 4 Dec 2026 17:40:54 -0500 (EST) Received: from smtpin18.hostedemail.com (lb01a-stub [10.200.18.249]) by unirelay04.hostedemail.com (Postfix) with ESMTP id A320E1A023C for ; Fri, 4 Dec 2026 22:40:53 +0000 (UTC) X-FDA: 85507771986.18.1E60996 Received: from mta1.migadu.com (out-87.mta1.migadu.com [95.215.58.87]) by imf05.hostedemail.com (Postfix) with ESMTP id ABD6E10000B for ; Fri, 4 Dec 2026 22:40:51 +0000 (UTC) Authentication-Results: imf05.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b="he/5dFZg"; dmarc=pass (policy=none) header.from=linux.dev; spf=pass (imf05.hostedemail.com: domain of shakeel.butt@linux.dev designates 95.215.58.87 as permitted sender) smtp.mailfrom=shakeel.butt@linux.dev ARC-Seal: i=1; a=rsa-sha256; d=hostedemail.com; s=arc-20220608; cv=none; t=1796424052; b=XwckLySY4AK87IyyYGt7Xzsq0geLl1c6wsmcMSCs+Ig2nQ+Im+7G7ki1A3FTEVeLnN2SV1 vFzA3DCmdB2r9p+04GitP6dM71X/yWesRKTxAAsImXu7/VTyXqJqs5TqEzw98Yvh7srWx8 A7Z6BKhnXhVwjPZuQPpTl+CYZaRTg1g= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1796424052; 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=8UGfdkysyyU3mbA2RdXtwN1hR633cIPXs0CtkBL38hk=; b=LOe9tY7tfRL1wn/VCYveBGhzj4ZafJb2JWfwXG5Uv1Ir8BWcd6WG7nnazuX+4FBv1CjqsK tSoHfrvB9I1DK2t8eFMXQm9yA1977cH2P3FQ2+fDqKOXBDCpIQZe58MgbdrKMebl6ZmVPe xayp2ButS+2/J2P0UFWvvV7lSn4Yb/U= ARC-Authentication-Results: i=1; imf05.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b="he/5dFZg"; dmarc=pass (policy=none) header.from=linux.dev; spf=pass (imf05.hostedemail.com: domain of shakeel.butt@linux.dev designates 95.215.58.87 as permitted sender) smtp.mailfrom=shakeel.butt@linux.dev X-Envelope-To: linux-mm@kvack.org DKIM-Signature: a=rsa-sha256; bh=yS/8X5nq7YoVBwEneL2zbVnVE0WXMlyPzeGhxVJc4Tw=; c=simple/simple; d=linux.dev; h=from:to:subject:date:message-id:mime-version:content-type; s=key1; t=1796424050; v=1; x=1797028850; b=he/5dFZgfWoJ6YLT72dQrlGeuPTP3aBWInCZ2E3xSzajAdBjr6QhLTHwEEUInxkFEqvntPX1 jfUfB/D5eBL/PNm66tz/GvUoy29uKP+nsbecy3dFXA0Tejm3u8yk3/MBguf6+6hkUNKZ5SSlaVP pGhIhBpg3rKHlAIyZ74d2WOM= X-Envelope-To: linux-mm@kvack.org Received: by smtp.migadu.com with ESMTPS id 237bbfc479ac3414; Fri, 04 Dec 2026 22:40:33 +0000 X-Mizu-Trace-ID: 237bbfc479ac3414 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 2/2] percpu_ref: defer the release function when the last put is in NMI Date: Fri, 4 Dec 2026 14:40:16 -0800 Message-ID: <20261204224016.325944-3-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-Rspamd-Server: rspam11 X-Rspam-User: X-Stat-Signature: g4g9ke4k5exk8z9dxs8qtaiehbqghpje 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-Rspamd-Queue-Id: ABD6E10000B X-HE-Tag: 1796424051-8965 X-HE-Meta: U2FsdGVkX1+gYLdphQ4E0HQ6HWmytzQHLwXOroYkdlcEiQq58MRibojX5KQUAu4bLLK4WAWr+4iwhUQeScABcsXe3yX1muEumPIHbDqoPnvn3g+0UPrG3cxHkECcSVuouLCaSH2Loi54ruxBzJYGsui01aYuEY6ZxyStb7ITwfqRJjdvtxFcFEHAXag1Wl8Jl956evlPl1RhjtiFoazMrdsE2xM/SMXnWtgUGpbBuhgCpyTHLiNwzcshKdWiXF77t8FmsiD+xNM0dESffKgQaqOCrLaa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i4dnEoZtVjedZ4l++pvutJjap+m+N1Z4YpUmiE2cVD365ad+ve2sCEFq2oLwPvGBtoVx7QrQZyKtPIgAo1mGJAT6HyKS08Tu/IVxHp6h4vCs5z6aWKuifad+kORHvRjq+XiTBeFDB1jfVCxRukE 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 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. 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 | 36 +++++++++++++++++--- lib/percpu-refcount.c | 59 ++++++++++++++++++++++++++++++--- 2 files changed, 87 insertions(+), 8 deletions(-) diff --git a/include/linux/percpu-refcount.h b/include/linux/percpu-refcount.h index c331f4b5b5c5..cf2938fa8216 100644 --- a/include/linux/percpu-refcount.h +++ b/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 percpu_ref *ref, 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_live(struct percpu_ref *ref) 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(struct percpu_ref *ref, unsigned long nr) 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(struct percpu_ref *ref, unsigned long nr) * @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. */ diff --git a/lib/percpu-refcount.c b/lib/percpu-refcount.c index 5def04e3a2c3..b12cc1568d29 100644 --- a/lib/percpu-refcount.c +++ b/lib/percpu-refcount.c @@ -3,6 +3,7 @@ #include #include +#include #include #include #include @@ -91,7 +92,8 @@ static unsigned long __percpu *percpu_count_ptr(struct percpu_ref *ref) * 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_confirm); * 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_ref *ref) 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); -- 2.53.1-Meta