From nobody Sun Jul 26 01:44:35 2026 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 6A7E737996C; Fri, 10 Jul 2026 05:31:16 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1783661476; cv=none; b=PkXwYUUXe/xy6bnfw6G5tpdNMrtL7MFFMrcf3DiJzGlNV8dO5LiolSv09AvTZPk1p65l3pLVOXv4QEtxP9dx+BgtzM+WpyZEAnkNNIscspPov4XsefGj/uHi5p6W76qM7Om9GpUPa0Hxrhg0ZnyRyk9L7X+DCf9ujljJFxfLPSQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1783661476; c=relaxed/simple; bh=sJwNGf50ntTffD4/Akpa0xR1rlsG3ZrhWjyIlK4Mk08=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=r5vlrzeiVFJ3aoyyIOGB7UWSkket2CMMSglpEElYc2ltmdEPmDJTbibCuzirX7oinsSz8fnAT3S471H516fn8zlTBjEntvmSAXWSZ7IIRed2ASXugP9PngR44uEGyaydJut0zfzb/KMlcHhSCIhrV8/fn+PQDT2GTR3bcUqgip0= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=qoyoHKwz; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="qoyoHKwz" Received: by smtp.kernel.org (Postfix) with ESMTPS id 13958C2BCB8; Fri, 10 Jul 2026 05:31:16 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1783661476; bh=sJwNGf50ntTffD4/Akpa0xR1rlsG3ZrhWjyIlK4Mk08=; h=From:Date:Subject:References:In-Reply-To:To:Cc:Reply-To:From; b=qoyoHKwzoiP05HCTetHmLNHeovi0PBuuKIpAM3oXyCwMTQyvrb3PxZbDsNJQ21FqZ qoOyASbGcMF/UWWhC8a0dcoaN83b4n3d5KyKJFAt1wMy8qZ65YkshHhF9GcnnNAK9R y8+7bZnUm5TtZEEdFq+nZgpwoIvUT0uThyWVbjqbhF+uJjrjWHtshiNFAXF4EgHDeH 4kjSk5gyAPRh2YqwCCpGNk79AjrkzhPC8MzAjtPglXbDBj1QgcOhEtGgkJTmMUI4CM WCJHLIl+Xt6MAS5ABuGcVI6vxjo+dYgM1+Up+rg/puVkD4+KpVE5TSYaXJIKLiSfwR dBKZULiGiOBiw== Received: from aws-us-west-2-korg-lkml-1.web.codeaurora.org (localhost.localdomain [127.0.0.1]) by smtp.lore.kernel.org (Postfix) with ESMTP id E912AC44501; Fri, 10 Jul 2026 05:31:15 +0000 (UTC) From: Eugene Mavick via B4 Relay Date: Fri, 10 Jul 2026 13:30:39 +0800 Subject: [PATCH v2 1/5] tracing: add ref_trace_final_put tracepoint Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20260710-refcount-final-put-trace-v2-1-557cfce860a2@mavick.dev> References: <20260710-refcount-final-put-trace-v2-0-557cfce860a2@mavick.dev> In-Reply-To: <20260710-refcount-final-put-trace-v2-0-557cfce860a2@mavick.dev> To: Will Deacon , Peter Zijlstra , Boqun Feng , Mark Rutland , Gary Guo , Steven Rostedt , Masami Hiramatsu , Mathieu Desnoyers , Andrew Morton , Dennis Zhou , Tejun Heo , Christoph Lameter Cc: linux-kernel@vger.kernel.org, linux-trace-kernel@vger.kernel.org, linux-mm@kvack.org, Eugene Mavick X-Mailer: b4 0.14.3 X-Developer-Signature: v=1; a=openpgp-sha256; l=4782; i=m@mavick.dev; h=from:subject:message-id; bh=qR1tr9zkvfO0Ekv8EXksBaXJupN2DKoOXEp8ReORpQI=; b=owGbwMvMwCU2V/5U3e1aE3/G02pJDFkBzT0KGade/c9KdFHS3h8rXtH3ZPqyLVL5tiWiZ997v /MXTEjsKGVhEONikBVTZKnJSnDPWDkr0fjopB6YOaxMIEMYuDgFYCJvtzL8z0vLdXzxyiZUwe4R x0+dqs6sZO7uVRfrkp6uPSuhM9t4G8P/0kO8PGqhJzoeq/yr6Zf+scT6y67vgcwLzlpFrnlt3qL CCwA= X-Developer-Key: i=m@mavick.dev; a=openpgp; fpr=7C6A604768A99A6133C5928C9D1FCA7EDB7D344F X-Endpoint-Received: by B4 Relay for m@mavick.dev/default with auth_id=850 X-Original-From: Eugene Mavick Reply-To: m@mavick.dev From: Eugene Mavick Add ref_trace_final_put tracepoint and related core infrastructure ref_trace_final_put fires when a reference count reaches zero and the object enters its final release path. The tracepoint records three fields: - caller: function that called the refcounting function(refcount_sub_and_test, percpu_ref_put_many) - fn: refcounting function(eg refcount_sub_and_test) - obj: refcount object(struct percpu_ref, refcount_t) Signed-off-by: Eugene Mavick --- v2: -change trace_ref_final_put macro name to do_trace_ref_final_put to avoid overlap with tracepoint names(trace_*) -change, in above mentioned macro, from trace_ref_trace_final_put( to trace_call__ref_trace_final_put( to avoid double check -changes suggested by Steven Rostedt -v1:https://lore.kernel.org/all/20260705-refcount-final-put-trace-v1-1-cdd0= 014626a9@mavick.dev/ --- include/linux/ref_trace.h | 26 +++++++++++++++++++++++ include/trace/events/ref_trace.h | 46 ++++++++++++++++++++++++++++++++++++= ++++ lib/Makefile | 2 ++ lib/ref_trace.c | 12 +++++++++++ 4 files changed, 86 insertions(+) diff --git a/include/linux/ref_trace.h b/include/linux/ref_trace.h new file mode 100644 index 000000000000..6c6600938a47 --- /dev/null +++ b/include/linux/ref_trace.h @@ -0,0 +1,26 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +#ifndef _LINUX_REF_TRACE_H +#define _LINUX_REF_TRACE_H + +#include +#include + +/* Declare the tracepoint so tracepoint_enabled() can be used */ +DECLARE_TRACEPOINT(ref_trace_final_put); + +#ifdef CONFIG_TRACEPOINTS +/* Wrapper function implemented in lib/ref_trace.c */ +extern void do_ref_trace_final_put(unsigned long caller, const char *fn, c= onst void *obj); + +#define do_trace_ref_final_put(obj) \ + do { \ + if (tracepoint_enabled(ref_trace_final_put)) \ + do_ref_trace_final_put(_RET_IP_, __func__, obj); \ + } while (0) + +#else /* !CONFIG_TRACEPOINTS */ +static inline void do_ref_trace_final_put(unsigned long caller, const char= *fn, const void *obj) { } +#define do_trace_ref_final_put(obj) do { } while (0) +#endif + +#endif /* _LINUX_REF_TRACE_H */ diff --git a/include/trace/events/ref_trace.h b/include/trace/events/ref_tr= ace.h new file mode 100644 index 000000000000..e6037a325be2 --- /dev/null +++ b/include/trace/events/ref_trace.h @@ -0,0 +1,46 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +#undef TRACE_SYSTEM +#define TRACE_SYSTEM ref_trace + +#if !defined(_TRACE_REF_TRACE_H) || defined(TRACE_HEADER_MULTI_READ) +#define _TRACE_REF_TRACE_H + +#include + +/** + * ref_trace_final_put - trace when a reference count reaches zero + * @caller: function that called the refcounting + * function(refcount_sub_and_test, percpu_ref_put_many) + * @fn: refcounting function(eg refcount_sub_and_test) + * @obj: refcount object(struct percpu_ref, refcount_t) + * + * Tracepoint instrumentation can be added using the ref_trace_final_put + * macro defined in include/linux/ref_trace.h + * which uses _RET_IP_ and __func__ for caller and fn arguments respective= ly, + * thus only requiring obj arg to be supplied + */ +TRACE_EVENT(ref_trace_final_put, + + TP_PROTO(unsigned long caller, const char *fn, const void *obj), + + TP_ARGS(caller, fn, obj), + + TP_STRUCT__entry( + __field(unsigned long, caller) + __string(fn, fn) + __field(const void *, obj) + ), + + TP_fast_assign( + __entry->caller =3D caller; + __assign_str(fn); + __entry->obj =3D obj; + ), + + TP_printk("caller=3D%pS fn=3D%s obj=3D%p", (void *)__entry->caller, __get= _str(fn), __entry->obj) +); + +#endif /* _TRACE_REF_TRACE_H */ + +/* This part must be outside protection */ +#include diff --git a/lib/Makefile b/lib/Makefile index f33a24bf1c19..41737090a95d 100644 --- a/lib/Makefile +++ b/lib/Makefile @@ -335,3 +335,5 @@ CONTEXT_ANALYSIS_test_context-analysis.o :=3D y obj-$(CONFIG_CONTEXT_ANALYSIS_TEST) +=3D test_context-analysis.o =20 subdir-$(CONFIG_FORTIFY_SOURCE) +=3D test_fortify + +obj-$(CONFIG_TRACEPOINTS) +=3D ref_trace.o diff --git a/lib/ref_trace.c b/lib/ref_trace.c new file mode 100644 index 000000000000..9102dc7117db --- /dev/null +++ b/lib/ref_trace.c @@ -0,0 +1,12 @@ +// SPDX-License-Identifier: GPL-2.0 +#define CREATE_TRACE_POINTS +#include + +//Wrapper function for functions defined entirely in header files +void do_ref_trace_final_put(unsigned long caller, const char *fn, const vo= id *obj) +{ + trace_call__ref_trace_final_put(caller, fn, obj); +} +EXPORT_SYMBOL_GPL(do_ref_trace_final_put); + +EXPORT_TRACEPOINT_SYMBOL_GPL(ref_trace_final_put); --=20 2.51.2 From nobody Sun Jul 26 01:44:35 2026 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 6A75A373C1A; Fri, 10 Jul 2026 05:31:16 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1783661476; cv=none; b=TgzBue0UAk081/SXdtqyiRo8ot4EgCSypxCtq3seYoRg+v6zdhMxfyd3FuTrd0Y1pLy3JZ4UUu23+glioEGZG7KtELDJAmRpmz0DugHx/BxhMOkz72PEOrphmlJFkcpzHfEeU6bK78OGqX+mukWnxsq7RuGd5BJKiQ3VsJDAeIw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1783661476; c=relaxed/simple; bh=BqhzH8win7IsFt3bVsGRfLZJqVQoC3H+EYEyrwbBQro=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=K8X77odM7ye47TwJj9YWz0QtZlpGGSJqagNxVOR3C6o6fUTV1Nm0/p273fb8MEvo/e8g7JmSzvSYnyslLteTPtFLO67qm6DtbwdXHVrsd3iaUAEQNFx/24+ddOJG3fOjM+Z5/BLzQm6fWzQoiKBdDJqxP+gfYb0R0ythTCoMi3Y= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=erKo23p/; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="erKo23p/" Received: by smtp.kernel.org (Postfix) with ESMTPS id 1F0CFC2BCC6; Fri, 10 Jul 2026 05:31:16 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1783661476; bh=BqhzH8win7IsFt3bVsGRfLZJqVQoC3H+EYEyrwbBQro=; h=From:Date:Subject:References:In-Reply-To:To:Cc:Reply-To:From; b=erKo23p/n5kFc0MNyrpiiMJlVgZNgKrb5Cqa0Xj3d4eC7KXuhU3/Kuh2tQ+f54nBT yc/6LvcIXQDlOgq/iaatx+8GAG6oVcSzcNSuQh5U3PMtH7i8oUNvjl8furd1fyhIGp JIUPxGbl4hr55lV+/pU7N3KNl0AGzQAVn6T7bsl+8AhPqQPpGT1NznD653qUcM0I1h IRCSbAdmylu5BRyRNoX7XIXnu+tcMs5qxdI/HBI8mArYQjO12jrJn/tTR4EZ0niG7i c7BMnHzo5VvIy412ijtdWSpq1ybrjDhgy3en1/ttAvo7DqWWXfHEzAR1QQIJjQyiPo qOy+vnlH83AOQ== Received: from aws-us-west-2-korg-lkml-1.web.codeaurora.org (localhost.localdomain [127.0.0.1]) by smtp.lore.kernel.org (Postfix) with ESMTP id 0225EC44507; Fri, 10 Jul 2026 05:31:16 +0000 (UTC) From: Eugene Mavick via B4 Relay Date: Fri, 10 Jul 2026 13:30:40 +0800 Subject: [PATCH v2 2/5] refcount: add ref_trace_final_put tracepoint Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20260710-refcount-final-put-trace-v2-2-557cfce860a2@mavick.dev> References: <20260710-refcount-final-put-trace-v2-0-557cfce860a2@mavick.dev> In-Reply-To: <20260710-refcount-final-put-trace-v2-0-557cfce860a2@mavick.dev> To: Will Deacon , Peter Zijlstra , Boqun Feng , Mark Rutland , Gary Guo , Steven Rostedt , Masami Hiramatsu , Mathieu Desnoyers , Andrew Morton , Dennis Zhou , Tejun Heo , Christoph Lameter Cc: linux-kernel@vger.kernel.org, linux-trace-kernel@vger.kernel.org, linux-mm@kvack.org, Eugene Mavick X-Mailer: b4 0.14.3 X-Developer-Signature: v=1; a=openpgp-sha256; l=1880; i=m@mavick.dev; h=from:subject:message-id; bh=LlgAKLvSTzT+rMKMAXMt37/mjMTpw1NXPXF0CRuqRZw=; b=owGbwMvMwCU2V/5U3e1aE3/G02pJDFkBzT33jpy6k7R1+kNdW73wjXUfb26Iv7hoxdSVcvOmh rm/cvrs2VHKwiDGxSArpshSk5XgnrFyVqLx0Uk9MHNYmUCGMHBxCsBEnKMYGVab93IpL7he/jLA ddYj7ctyQnPDJzFeuLVL5dQpt+0l9UUM/4NabsonXO9+eaX0QMPN2z0a//MWS3reFtgW57Lj7cd mBTYA X-Developer-Key: i=m@mavick.dev; a=openpgp; fpr=7C6A604768A99A6133C5928C9D1FCA7EDB7D344F X-Endpoint-Received: by B4 Relay for m@mavick.dev/default with auth_id=850 X-Original-From: Eugene Mavick Reply-To: m@mavick.dev From: Eugene Mavick Add the ref_trace_final_put tracepoint to __refcount_sub_and_test() and refcount_dec_if_one() This tracepoint fires when a refcount_t reaches zero, capturing the caller address, the function name, and the refcount_t address. Signed-off-by: Eugene Mavick --- v2: -update commit message -add tracepoint to refcount_dec_if_one(), suggested by Usuma Arif -move MAINTAINERS file entries to new MAINTAINERS commit -v1:https://lore.kernel.org/all/20260705-refcount-final-put-trace-v1-2-cdd0= 014626a9@mavick.dev/ --- include/linux/refcount.h | 2 ++ lib/refcount.c | 8 +++++++- 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/include/linux/refcount.h b/include/linux/refcount.h index ba7657ced281..70d07a462da1 100644 --- a/include/linux/refcount.h +++ b/include/linux/refcount.h @@ -107,6 +107,7 @@ #include #include #include +#include =20 struct mutex; =20 @@ -393,6 +394,7 @@ bool __refcount_sub_and_test(int i, refcount_t *r, int = *oldp) =20 if (old > 0 && old =3D=3D i) { smp_acquire__after_ctrl_dep(); + do_trace_ref_final_put(r); return true; } =20 diff --git a/lib/refcount.c b/lib/refcount.c index a207a8f22b3c..cd7e32df3919 100644 --- a/lib/refcount.c +++ b/lib/refcount.c @@ -7,6 +7,7 @@ #include #include #include +#include =20 #define REFCOUNT_WARN(str) WARN_ONCE(1, "refcount_t: " str ".\n") =20 @@ -56,7 +57,12 @@ bool refcount_dec_if_one(refcount_t *r) { int val =3D 1; =20 - return atomic_try_cmpxchg_release(&r->refs, &val, 0); + bool ret =3D atomic_try_cmpxchg_release(&r->refs, &val, 0); + + if (ret) + do_trace_ref_final_put(r); + + return ret; } EXPORT_SYMBOL(refcount_dec_if_one); =20 --=20 2.51.2 From nobody Sun Jul 26 01:44:35 2026 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 77D51379C49; Fri, 10 Jul 2026 05:31:16 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1783661476; cv=none; b=Qt5RxJHEA3o+gDGpUgvhjHjr1eUbwUO9fJ8q1v6P6JXvFu14H4OYSa3UjI4Gfut91Oe5cBCr3/VhNV6afm1cvvM+WBs7o9hgM2TwYT5bLGvC/DCWuZ+tjK3vGsfcA8sQI19MS3Pj49zm0Mkbk4PNi5KT0lPTw9cTTwUj2wyEJeU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1783661476; c=relaxed/simple; bh=hkSle40hvPkgCxPiifxmuclyWzd9fTgjPZ/t/hWhm/o=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=GXXVAMfoNrrIBEQtOQr2jJ6pcOxtmFqJpsUvGLcrpCmVRiUsrxRs5SzpktQdruwyjreQwl2JSg7F2i0TjjR+r7XJP6bx45dzvop07prGjcJIHmyqpNVc/vp2fTVEK8hj/t0aLWd0hAYDxv/XkFDtx5D8yvkWCD6HeLiMlIE8SRU= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=cT1WPQZb; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="cT1WPQZb" Received: by smtp.kernel.org (Postfix) with ESMTPS id 29D40C2BCFA; Fri, 10 Jul 2026 05:31:16 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1783661476; bh=hkSle40hvPkgCxPiifxmuclyWzd9fTgjPZ/t/hWhm/o=; h=From:Date:Subject:References:In-Reply-To:To:Cc:Reply-To:From; b=cT1WPQZbIDi51GeFAFmnTygt0OzLeYXfiVqTqYJchNaPOJxXGkf3HjjBindXmxYsd d7Fo6YU2owLSlVWqN0N55deUIUEqDRG6+wUf0cKAw6puKZLsnF6mAuglfc0+Rg+y5B 0edJGffecRTrLn5hQIHaMa6cMP+08VDzIWNuBtyd9tlDp6Wctk7oc9kSYmcFCD1doR mT9Zh8vaCW52vzvkVBg9nOqbThSQY1dqXR0UBIn6gLgtzr6T6Q+GoSopHvPfmbnKrg d6RmgPlTauhMnhnJSy3vV/qznQq+fekFQ6qT8MV41lLj/+shq899rqQVYlnd0cj8Lu xFluisbdbMAcA== Received: from aws-us-west-2-korg-lkml-1.web.codeaurora.org (localhost.localdomain [127.0.0.1]) by smtp.lore.kernel.org (Postfix) with ESMTP id 0F500C44506; Fri, 10 Jul 2026 05:31:16 +0000 (UTC) From: Eugene Mavick via B4 Relay Date: Fri, 10 Jul 2026 13:30:41 +0800 Subject: [PATCH v2 3/5] percpu-refcount: add ref_trace_final_put trace Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20260710-refcount-final-put-trace-v2-3-557cfce860a2@mavick.dev> References: <20260710-refcount-final-put-trace-v2-0-557cfce860a2@mavick.dev> In-Reply-To: <20260710-refcount-final-put-trace-v2-0-557cfce860a2@mavick.dev> To: Will Deacon , Peter Zijlstra , Boqun Feng , Mark Rutland , Gary Guo , Steven Rostedt , Masami Hiramatsu , Mathieu Desnoyers , Andrew Morton , Dennis Zhou , Tejun Heo , Christoph Lameter Cc: linux-kernel@vger.kernel.org, linux-trace-kernel@vger.kernel.org, linux-mm@kvack.org, Eugene Mavick X-Mailer: b4 0.14.3 X-Developer-Signature: v=1; a=openpgp-sha256; l=1198; i=m@mavick.dev; h=from:subject:message-id; bh=vVWsfTjSGw6e33WP2d/uyqteLjy9dxiqe2AmCt04NhY=; b=owGbwMvMwCU2V/5U3e1aE3/G02pJDFkBzT23Y8+p1rz80Pf3zukIvlrR8BPlk+R2vosu3mV2d LV6zJ2GjlIWBjEuBlkxRZaarAT3jJWzEo2PTuqBmcPKBDKEgYtTACaikcnw33GWVshuP6V7871q F876Xb3cRlpp61dTl08/Ttf0FjyYVcjI0DhBdLP4GyN3Hak/N6we3ausMRAOW5Uovu/0vzVGEf3 9nAA= X-Developer-Key: i=m@mavick.dev; a=openpgp; fpr=7C6A604768A99A6133C5928C9D1FCA7EDB7D344F X-Endpoint-Received: by B4 Relay for m@mavick.dev/default with auth_id=850 X-Original-From: Eugene Mavick Reply-To: m@mavick.dev From: Eugene Mavick Add the ref_trace_final_put tracepoint to percpu_ref_put_many(). The tracepoint fires when the atomic counter reaches zero in the atomic fallback path (after percpu_ref_kill() has been called). Signed-off-by: Eugene Mavick --- include/linux/percpu-refcount.h | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/include/linux/percpu-refcount.h b/include/linux/percpu-refcoun= t.h index d73a1c08c3e3..f870ac0e8c06 100644 --- a/include/linux/percpu-refcount.h +++ b/include/linux/percpu-refcount.h @@ -55,6 +55,7 @@ #include #include #include +#include =20 struct percpu_ref; typedef void (percpu_ref_func_t)(struct percpu_ref *); @@ -331,8 +332,10 @@ static inline void percpu_ref_put_many(struct percpu_r= ef *ref, unsigned long nr) =20 if (__ref_is_percpu(ref, &percpu_count)) this_cpu_sub(*percpu_count, nr); - else if (unlikely(atomic_long_sub_and_test(nr, &ref->data->count))) + else if (unlikely(atomic_long_sub_and_test(nr, &ref->data->count))) { + do_trace_ref_final_put(ref); ref->data->release(ref); + } =20 rcu_read_unlock(); } --=20 2.51.2 From nobody Sun Jul 26 01:44:36 2026 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 77CCB379C2A; Fri, 10 Jul 2026 05:31:16 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1783661476; cv=none; b=dU46q1x9Hy9hRRdSaQw/kaNDDfcg5z+8TXBXZVc/BSQQOB0RRZz4hhenZhTDUGzPB01rsEyckmDkdeOeg48mf0JbOd5O4wdrPI18ejZ4Wm/+MxAo6//g6pQOOGYdB6WeUAXpqRWumqJo31mnyH6XNb9kI/nXAEKinIhJC1oPBNw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1783661476; c=relaxed/simple; bh=hYItlIxI3TFxH56tjyUfH5lTleoN/4M0MjnyxZ2eCSw=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=LCqPxqU+sIbsWYUJU10SULv943cc5F+Bdb1XcVqIxV0e6TeZrX4vzmo9Kg2ZaEA+8PuyM5kbOYktj4YZF+RD3T4a0FWmKTIqveO0BdHp1GqRLUHSIzeBefcpa8DWIV6N4uLIZ/lro6vEVrkQeI+5/VQvnKjL8lUfU7ogi899ZEE= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=l65Scuad; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="l65Scuad" Received: by smtp.kernel.org (Postfix) with ESMTPS id 34CBCC2BCFB; Fri, 10 Jul 2026 05:31:16 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1783661476; bh=hYItlIxI3TFxH56tjyUfH5lTleoN/4M0MjnyxZ2eCSw=; h=From:Date:Subject:References:In-Reply-To:To:Cc:Reply-To:From; b=l65ScuadIrCDCzyddzFeUgvv1Sxf2QFAxdvh+6epe1m+3ntxxcipFUji0CHV7n13f e2X7H0WyQSX+dI95L/fe1pz7Sk1k7gQfxtETgDN1xMoTyq1DPv6fTVJwWO/E6ZND4G c/Of4WSTzT0ASWATk1kCH+EFISLrG3ZQt7J5XV6ROdSxDTLHSAxldCsLKix70ePI3n qRZgRVflHgR1HpWq5JW6T3HjdcWpTydbZAP/ZDJbFV4/IaRWsAcreUFzhYv51uDSqK 2KceLTGKkQU31pb/edjGPJPbkrdL+Rlpo6zbvjZg/pFvRUfOtsJxIwCizfF4V4UcwB s7xMK6KVmo8fA== Received: from aws-us-west-2-korg-lkml-1.web.codeaurora.org (localhost.localdomain [127.0.0.1]) by smtp.lore.kernel.org (Postfix) with ESMTP id 20261C44509; Fri, 10 Jul 2026 05:31:16 +0000 (UTC) From: Eugene Mavick via B4 Relay Date: Fri, 10 Jul 2026 13:30:42 +0800 Subject: [PATCH v2 4/5] kunit: add test for ref_trace_final_put Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20260710-refcount-final-put-trace-v2-4-557cfce860a2@mavick.dev> References: <20260710-refcount-final-put-trace-v2-0-557cfce860a2@mavick.dev> In-Reply-To: <20260710-refcount-final-put-trace-v2-0-557cfce860a2@mavick.dev> To: Will Deacon , Peter Zijlstra , Boqun Feng , Mark Rutland , Gary Guo , Steven Rostedt , Masami Hiramatsu , Mathieu Desnoyers , Andrew Morton , Dennis Zhou , Tejun Heo , Christoph Lameter Cc: linux-kernel@vger.kernel.org, linux-trace-kernel@vger.kernel.org, linux-mm@kvack.org, Eugene Mavick X-Mailer: b4 0.14.3 X-Developer-Signature: v=1; a=openpgp-sha256; l=5506; i=m@mavick.dev; h=from:subject:message-id; bh=/9Dev+Y5OluuMZTbdCo5lc9tYCtS2UAK0B+nMvLxkRA=; b=owGbwMvMwCU2V/5U3e1aE3/G02pJDFkBzT3XlnTrxwqYtC050Bi7wWrFRla/teG+d+LXfH/vu Dqtwzezo5SFQYyLQVZMkaUmK8E9Y+WsROOjk3pg5rAygQxh4OIUgInkyzH80137etGuGXom334l 7cxMMWp0jt3csPWciFXil8ULhTO3iTEyvLTfz7vtbLpA+bapb3MeeP01j1aqVvzZeJ4t8WJ0Ue4 HfgA= X-Developer-Key: i=m@mavick.dev; a=openpgp; fpr=7C6A604768A99A6133C5928C9D1FCA7EDB7D344F X-Endpoint-Received: by B4 Relay for m@mavick.dev/default with auth_id=850 X-Original-From: Eugene Mavick Reply-To: m@mavick.dev From: Eugene Mavick Add a KUnit test suite for the ref_trace_final_put tracepoint. The test registers a probe function and triggers both refcount_t and percpu_ref final put paths, verifying that the tracepoint fires correctly and that the recorded fields match expected values. Signed-off-by: Eugene Mavick --- v2: -fix v1 bug where test gave consistent false pass -make probe minimal, and only check obj and store the values -add test_init macro to register and reset count -move value checking to new test_exit macro -move MAINTAINERS file entries to new MAINTAINERS commit -v1:https://lore.kernel.org/all/20260705-refcount-final-put-trace-v1-4-cdd0= 014626a9@mavick.dev/ --- lib/Kconfig | 10 ++++ lib/tests/Makefile | 1 + lib/tests/ref_trace_kunit.c | 136 ++++++++++++++++++++++++++++++++++++++++= ++++ 3 files changed, 147 insertions(+) diff --git a/lib/Kconfig b/lib/Kconfig index 00a9509636c1..c29b2ccb3c31 100644 --- a/lib/Kconfig +++ b/lib/Kconfig @@ -52,6 +52,16 @@ config PACKING_KUNIT_TEST =20 When in doubt, say N. =20 +config REF_TRACE_KUNIT_TEST + bool "ref_trace kunit test" if !KUNIT_ALL_TESTS + depends on KUNIT && FTRACE + default KUNIT_ALL_TESTS + help + This option enables the KUnit test suite for the ref_trace_final_put + tracepoint. + + If unsure, say N + config BITREVERSE tristate =20 diff --git a/lib/tests/Makefile b/lib/tests/Makefile index 7e9c2fa52e35..828a030ad8c7 100644 --- a/lib/tests/Makefile +++ b/lib/tests/Makefile @@ -57,5 +57,6 @@ obj-$(CONFIG_USERCOPY_KUNIT_TEST) +=3D usercopy_kunit.o obj-$(CONFIG_UTIL_MACROS_KUNIT) +=3D util_macros_kunit.o obj-$(CONFIG_RATELIMIT_KUNIT_TEST) +=3D test_ratelimit.o obj-$(CONFIG_UUID_KUNIT_TEST) +=3D uuid_kunit.o +obj-$(CONFIG_REF_TRACE_KUNIT_TEST) +=3D ref_trace_kunit.o =20 obj-$(CONFIG_TEST_RUNTIME_MODULE) +=3D module/ diff --git a/lib/tests/ref_trace_kunit.c b/lib/tests/ref_trace_kunit.c new file mode 100644 index 000000000000..5c7e9d29a030 --- /dev/null +++ b/lib/tests/ref_trace_kunit.c @@ -0,0 +1,136 @@ +// SPDX-License-Identifier: GPL-2.0 +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +struct data { + unsigned long caller; + const char *fn; + const void *obj; + atomic_t count; +}; + +struct data capture; + +const void *chk_obj; + +#define test_init() \ + do { \ + KUNIT_EXPECT_FALSE( \ + test, register_trace_ref_trace_final_put(probe, NULL)); \ + \ + atomic_set_release(&capture.count, 0); \ + \ + chk_obj =3D &obj; \ + } while (0) + + +#define test_exit(func_name) \ + do { \ + /* wait for probe completion */ \ + wait_var_event( \ + &capture.count, \ + atomic_read_acquire(&capture.count) !=3D 0); \ + \ + unregister_trace_ref_trace_final_put(probe, NULL); \ + \ + KUNIT_EXPECT_EQ(test, atomic_read_acquire(&capture.count), 1); \ + /* \ + * caller testing may be flaky \ + * due to compile optimisations so omit \ + */ \ + KUNIT_EXPECT_STREQ(test, capture.fn, #func_name); \ + KUNIT_EXPECT_PTR_EQ(test, capture.obj, &obj); \ + } while (0) + +static void probe( + void *ignore, + unsigned long caller, + const char *fn, + const void *obj) +{ + //prevent non test func final_puts from changing captured values + if (chk_obj !=3D obj) + return; + + capture.caller =3D caller;=09 + capture.fn =3D fn;=09 + capture.obj =3D obj;=09 + + atomic_inc_return_release(&capture.count); //increase count +} + +static noinline void test_refcount_sub_and_test(struct kunit *test) +{ + refcount_t obj; + + test_init(); + refcount_set(&obj, 2); + + KUNIT_EXPECT_FALSE(test, refcount_dec_and_test(&obj)); + KUNIT_EXPECT_TRUE(test, refcount_dec_and_test(&obj)); + + test_exit(__refcount_sub_and_test); +} + +static __always_inline void test_refcount_dec_if_one(struct kunit *test) +{ + refcount_t obj; + + test_init(); + refcount_set(&obj, 2); + + KUNIT_EXPECT_FALSE(test, refcount_dec_and_test(&obj)); + KUNIT_EXPECT_TRUE(test, refcount_dec_if_one(&obj)); + + test_exit(refcount_dec_if_one); +} +static void dummy_release(struct percpu_ref *ref) {} + +static noinline void test_percpu_ref_put_many(struct kunit *test) +{ + struct percpu_ref obj; + + test_init(); + + KUNIT_EXPECT_FALSE(test, percpu_ref_init(&obj, dummy_release, 0, GFP_KERN= EL)); + + percpu_ref_get(&obj); + percpu_ref_get(&obj); + + percpu_ref_put(&obj); + percpu_ref_put(&obj); + + percpu_ref_switch_to_atomic_sync(&obj); + + percpu_ref_put(&obj); + + test_exit(percpu_ref_put_many); + percpu_ref_exit(&obj); +} + +static struct kunit_case __refdata ref_trace_test_cases[] =3D { + KUNIT_CASE(test_refcount_sub_and_test), + KUNIT_CASE(test_refcount_dec_if_one), + KUNIT_CASE(test_percpu_ref_put_many), + {} +}; + +static struct kunit_suite ref_trace_test_suite =3D { + .name =3D "ref-trace", + .test_cases =3D ref_trace_test_cases +}; + +kunit_test_suites(&ref_trace_test_suite); + +MODULE_AUTHOR("Eugene Mavick "); +MODULE_DESCRIPTION("KUnit test for ref_trace"); +MODULE_LICENSE("GPL"); --=20 2.51.2 From nobody Sun Jul 26 01:44:36 2026 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 9024137AA81; Fri, 10 Jul 2026 05:31:16 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1783661476; cv=none; b=us0gwEihAR9vNoT5H6RLvhJb0sNN7Dy2LmuG/3kySrgH52DdvUTaWcl0DVl1gYXkAhx9tst7pLpV7W1aNQgGjpj8ImuI6yySDJpIC+HIGJdbbxEGtixrXotTEztj1JIP3eU6Qe8dzJsodywy4XNjCtK4m4wg9Nx3R9zqlBKJ7so= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1783661476; c=relaxed/simple; bh=N3UNjWOifoQciMT8bRgQar7ekquskU0egj4OqvXH15w=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=Ng86Nd1hJ8V269RYMr55EGwbrXcCMnn0hd1jFuZHTbv4TSEcsYnOH7KEK/+dimiiE8Xdl+RpvSnESRj6CDgoky7ejnCfHUqr/XrrfyrjV4fWRJxuvLCyfRE26aha1LiQEJDpvKwDysuJNIKpAMZAlivzGjDLCCSkDdGGCqv6aaA= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=WTwPePKK; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="WTwPePKK" Received: by smtp.kernel.org (Postfix) with ESMTPS id 4A299C2BCFC; Fri, 10 Jul 2026 05:31:16 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1783661476; bh=N3UNjWOifoQciMT8bRgQar7ekquskU0egj4OqvXH15w=; h=From:Date:Subject:References:In-Reply-To:To:Cc:Reply-To:From; b=WTwPePKKIBMnJ83AaPUEC7cUUPfM//rKzNoWC2n47+rKDIXlR9KkD2IYKRCa1mz/t y9TvjYaISfnHmEzXrvxC49FYTriD4f+whjvU8AyYf82kRqMBsFeEOiBcRtqNoiQ5e1 8g+G2cJ4MMTnvsPAN+bomjEMkWbeakkfHK1C5Gz2/FxmRJ96C40aYePCDqbMgDLXQF 2BtA7Dfmgy3giEep4D9XWCziwp/vVufH/gqoaDCc7aYuMHuHI9bQMYOGQpL+zKdO7c 3S8FOvIFMxr3JSxDePMPirSxpLd/iEGDFTcDTKtyL92FLuEICXxPsZDilaR59PDYPO Zu+kyupNOvSPg== Received: from aws-us-west-2-korg-lkml-1.web.codeaurora.org (localhost.localdomain [127.0.0.1]) by smtp.lore.kernel.org (Postfix) with ESMTP id 313EFC43458; Fri, 10 Jul 2026 05:31:16 +0000 (UTC) From: Eugene Mavick via B4 Relay Date: Fri, 10 Jul 2026 13:30:43 +0800 Subject: [PATCH v2 5/5] MAINTAINERS: add entries for ref_trace_final_put Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20260710-refcount-final-put-trace-v2-5-557cfce860a2@mavick.dev> References: <20260710-refcount-final-put-trace-v2-0-557cfce860a2@mavick.dev> In-Reply-To: <20260710-refcount-final-put-trace-v2-0-557cfce860a2@mavick.dev> To: Will Deacon , Peter Zijlstra , Boqun Feng , Mark Rutland , Gary Guo , Steven Rostedt , Masami Hiramatsu , Mathieu Desnoyers , Andrew Morton , Dennis Zhou , Tejun Heo , Christoph Lameter Cc: linux-kernel@vger.kernel.org, linux-trace-kernel@vger.kernel.org, linux-mm@kvack.org, Eugene Mavick X-Mailer: b4 0.14.3 X-Developer-Signature: v=1; a=openpgp-sha256; l=665; i=m@mavick.dev; h=from:subject:message-id; bh=qY0jw9qG8rTERqF+Mm/JywN3bXE+e644UzadbgoAM88=; b=owGbwMvMwCU2V/5U3e1aE3/G02pJDFkBzT1/DcKkKm17S69+OZR4JdNRoG2tTpF8UDNXWP7Mx Uyp9nkdpSwMYlwMsmKKLDVZCe4ZK2clGh+d1AMzh5UJZAgDF6cATCR6NcM/I6ODKmyXLLZ/eT5J 9dAyI6OMB79Djq3a8L/smAyrtc/jXkaGdfeUHiw5E3Xwy0SZeqWnt9eVFZw1Wb+ssaP0hajyx/y 1XAA= X-Developer-Key: i=m@mavick.dev; a=openpgp; fpr=7C6A604768A99A6133C5928C9D1FCA7EDB7D344F X-Endpoint-Received: by B4 Relay for m@mavick.dev/default with auth_id=850 X-Original-From: Eugene Mavick Reply-To: m@mavick.dev From: Eugene Mavick Add new files added in the patch series to MAINTAINERS Signed-off-by: Eugene Mavick --- v2: This patch was added in v2 --- MAINTAINERS | 3 +++ 1 file changed, 3 insertions(+) diff --git a/MAINTAINERS b/MAINTAINERS index 10e8253181d3..2dbb7441906e 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -4210,7 +4210,10 @@ S: Maintained F: Documentation/atomic_*.txt F: arch/*/include/asm/atomic*.h F: include/*/atomic*.h +F: include/linux/ref_trace.h F: include/linux/refcount.h +F: lib/ref_trace.c +F: lib/tests/ref_trace_kunit.c F: scripts/atomic/ F: rust/kernel/sync/atomic.rs F: rust/kernel/sync/atomic/ --=20 2.51.2