From nobody Fri Oct 2 12:19:29 2026 Received: from mout-p-202.mailbox.org (mout-p-202.mailbox.org [80.241.56.172]) (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 E225A31E830; Sat, 1 Aug 2026 11:00:46 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=80.241.56.172 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1785582048; cv=none; b=CguuJIvYtX2nNYAZTG6u+uuZGzYtr7htgSX2m0dYOs0C4gamuEcjfGRR4BIKVV7INQ42DECa4bPGhbA4nuvQsHOqIcEc2NcPorTzDU/s+O7oahRuE+r8CiVcp84Sk2MPDLTuhzIOh/UuTSqh8dwZJQG19XO0ran/yfzcrM2F0LA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1785582048; c=relaxed/simple; bh=ll6nYQOrO2XWydxLfhKt+/rvzaAqc+x2/e8etpbuoBQ=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=IEyyGkbgjJzkaow42Dk7oqhSf5hMmJgVW+tnNVTka30QtW5HeEEqUzyKOSJT/Unyvv2Ux+B8gpIgoQ/TwKR8LlT6BCsTuID07+LJQmD2fxTqcUD/k3mRBg1+7mSbSpc3LCm3ZhmRQz9hldonOCxA9ycZGCz+rXN+ET2bIiGpXv0= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=mavick.dev; spf=pass smtp.mailfrom=mavick.dev; dkim=pass (2048-bit key) header.d=mavick.dev header.i=@mavick.dev header.b=u81cA4nq; arc=none smtp.client-ip=80.241.56.172 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=mavick.dev Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=mavick.dev Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=mavick.dev header.i=@mavick.dev header.b="u81cA4nq" Received: from smtp1.mailbox.org (smtp1.mailbox.org [10.196.197.1]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange x25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by mout-p-202.mailbox.org (Postfix) with ESMTPS id 4hC0NB6WcLzMlGG; Sat, 01 Aug 2026 13:00:42 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=mavick.dev; s=MBO0001; t=1785582042; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=0Bbvo5JM5EzDceFV5HFBn3Tn1Nvzi+vQVAFu4wTmlqI=; b=u81cA4nqP3SwSZYYHDPT/c5iJMa/mvGsUCdVoLwzxrcGgMFdrtexAyfBe4goeV8CnoZjmp Y8QnIl1s+PhdvBc1uZ67SH+P2UaPM0wBkVnrC+24JdQDekZnjHXK0r1hvcI9G8U6J7NYb0 GCCAq5M8x3jgV7eAiA8AiD6S1s+UCp9bvRP16OM6cb3/Jyv5mOV/wewYycedofExfwcFoI B/bCmIOP2cc3iImIeld1Rds/k5ogd28dgZPxvk8gfIb2Kql+LxMpp37/g8R0ra44dqB2oq 9FxmU2xf61PGf8Ow4QVc3bBeLX5rbaVXjqV/rrIUGLiFyYarvXjg33CZOAHzgQ== From: Eugene Mavick Date: Sat, 01 Aug 2026 18:59:41 +0800 Subject: [PATCH v4 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: <20260801-refcount-final-put-trace-v4-1-2e58678f0ffd@mavick.dev> References: <20260801-refcount-final-put-trace-v4-0-2e58678f0ffd@mavick.dev> In-Reply-To: <20260801-refcount-final-put-trace-v4-0-2e58678f0ffd@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-Developer-Signature: v=1; a=openpgp-sha256; l=4872; i=m@mavick.dev; h=from:subject:message-id; bh=ll6nYQOrO2XWydxLfhKt+/rvzaAqc+x2/e8etpbuoBQ=; b=owGbwMvMwCU2V/5U3e1aE3/G02pJDFm5FzdGbl2SrP/R6MkG6f0mqz+8/KArc/GXXJBuwnVnm 8LVi1U2dZSyMIhxMciKKbLUZCW4Z6yclWh8dFIPzBxWJpAhDFycAjCRnGWMDHdzXrOYP4isqmRz VKud4Pf8y/2zOpe+ZH+4tbBw2o2QC16MDA/PHJbIWcL2r+Ttg51bNy/dYv5h9/yWhML3E88wfKt XfsAKAA== X-Developer-Key: i=m@mavick.dev; a=openpgp; fpr=7C6A604768A99A6133C5928C9D1FCA7EDB7D344F 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) - ip: return address of trace wrapper macro call - obj: refcount object(struct percpu_ref, refcount_t) Signed-off-by: Eugene Mavick --- include/linux/ref_trace.h | 40 +++++++++++++++++++++++++++++++ include/trace/events/ref_trace.h | 51 ++++++++++++++++++++++++++++++++++++= ++++ lib/Makefile | 2 ++ lib/ref_trace.c | 13 ++++++++++ 4 files changed, 106 insertions(+) diff --git a/include/linux/ref_trace.h b/include/linux/ref_trace.h new file mode 100644 index 000000000000..c93c30bc20d2 --- /dev/null +++ b/include/linux/ref_trace.h @@ -0,0 +1,40 @@ +/* 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, unsigned long ip,= const void *obj); + +#define do_trace_ref_final_put(obj) \ + do { \ + if (tracepoint_enabled(ref_trace_final_put)) \ + do_ref_trace_final_put(_RET_IP_, _THIS_IP_, obj); \ + } while (0) + +#define do_trace_ref_final_put_cond(cond, obj) \ + do { \ + if (tracepoint_enabled(ref_trace_final_put)) { \ + if (cond) \ + do_ref_trace_final_put(_RET_IP_, _THIS_IP_, obj);\ + } \ + } while (0) + +#else /* !CONFIG_TRACEPOINTS */ +static inline void do_ref_trace_final_put( + unsigned long caller, + unsigned long ip, + const void *obj) +{ +} +#define do_trace_ref_final_put(obj) do { } while (0) +#define do_trace_ref_final_put_cond(cond, 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..8bd9aa30a540 --- /dev/null +++ b/include/trace/events/ref_trace.h @@ -0,0 +1,51 @@ +/* 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: return address of refcount + * function(refcount_sub_and_test, percpu_ref_put_many) + * @ip: return address of trace wrapper macro call, + * inlining may cause it to be something else tho + * @obj: refcount object(struct percpu_ref, refcount_t) + * + * Tracepoint instrumentation can be added using the do_ref_trace_final_put + * macro defined in include/linux/ref_trace.h + * which uses _RET_IP_ and _THIS_IP_ for caller and ip arguments respectiv= ely, + * thus only requiring obj arg to be supplied + */ +TRACE_EVENT(ref_trace_final_put, + + TP_PROTO(unsigned long caller, unsigned long ip, const void *obj), + + TP_ARGS(caller, ip, obj), + + TP_STRUCT__entry( + __field( unsigned long, caller ) + __field( unsigned long, ip ) + __field( const void *, obj ) + ), + + TP_fast_assign( + __entry->caller =3D caller; + __entry->ip =3D ip; + __entry->obj =3D obj; + ), + + TP_printk("caller=3D%pS ip=3D%pS obj=3D%p", + (void *)__entry->caller, + (void *)__entry->ip, + __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..22fe372f468f --- /dev/null +++ b/lib/ref_trace.c @@ -0,0 +1,13 @@ +// SPDX-License-Identifier: GPL-2.0 +#define CREATE_TRACE_POINTS +#include +#include + +//Wrapper function for functions defined entirely in header files +void do_ref_trace_final_put(unsigned long caller, unsigned long ip, const = void *obj) +{ + trace_call__ref_trace_final_put(caller, ip, obj); +} +EXPORT_SYMBOL_GPL(do_ref_trace_final_put); + +EXPORT_TRACEPOINT_SYMBOL_GPL(ref_trace_final_put); --=20 2.51.2 From nobody Fri Oct 2 12:19:29 2026 Received: from mout-p-101.mailbox.org (mout-p-101.mailbox.org [80.241.56.151]) (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 D406231E830; Sat, 1 Aug 2026 11:01:00 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=80.241.56.151 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1785582063; cv=none; b=c6XYHUBwufJ4YqPAXKmMA5iGwKKCmWsfomepo5w7OlgylaLSuExv+PDQUVa2U640fmCJWedY8q1X5dSfOMs8rqPk+TzT0azljxjkfjthD7b0qxwR34IJ4v+bGy3aC2CT1tSiyl3w08V6Z5oEakfaw61ovz2LWyCLQVUaCyKFvEg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1785582063; c=relaxed/simple; bh=6SxDI3lnGUVOqQynlBB5KrL+oi0ZqISwEqlbLLcv/7A=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=kBRgd7BrS8AOY2AsJxHnUbbz8dBPYPPiBERp5GfgBTpJiZXgUoc7E918UmKe9aJVQbslEM1otLiKz/oEzVjB1lT1zNNdjoELnjqZM5DabDBTQBsvUdHqveHYpUK2/pFewhXQBOzuF0Z/DpJxg1YfVtkqWEJt3/2PwLCQrXKN6Dw= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=mavick.dev; spf=pass smtp.mailfrom=mavick.dev; dkim=pass (2048-bit key) header.d=mavick.dev header.i=@mavick.dev header.b=b7oXTNTj; arc=none smtp.client-ip=80.241.56.151 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=mavick.dev Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=mavick.dev Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=mavick.dev header.i=@mavick.dev header.b="b7oXTNTj" Received: from smtp1.mailbox.org (smtp1.mailbox.org [IPv6:2001:67c:2050:b231:465::1]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange x25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by mout-p-101.mailbox.org (Postfix) with ESMTPS id 4hC0NM5NPrz8twy; Sat, 01 Aug 2026 13:00:51 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=mavick.dev; s=MBO0001; t=1785582051; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=C912dpGQcVcNQokG8z6F9kwSDBoO6n2Gknkhy/XwQHc=; b=b7oXTNTjj2ZCjCrjXH+4onJXBZ6okt7C6WdM+iEtplUoOlZh0An8Gt1LjzqSL5V97xIZT4 h7Vkq7kXdpBxiePE+e1Oo7O+4aCj4HDTlio+MwKv31V7yZetw03K+6hRotzC1IvPSipcyp aO9aKbTG+FscVo4yCU020JW1nEY2VAIATiXBYprDl99x7J7KBELWSUFVGyI5wVOidvM3PI ocmxALYuXyiTEcxHaHyxI8wnw5Cc0RQmfx+zteGZobHxeiPopl8XeWsM+Ff54vL8pYq42i CXcKgsPMj9LdyKM6HtN1KmZOGa2Wj7TMU2F6OlBg3h7eEY2e2WkTFlT0uUwVwA== Authentication-Results: outgoing_mbo_mout; dkim=none; spf=pass (outgoing_mbo_mout: domain of m@mavick.dev designates 2001:67c:2050:b231:465::1 as permitted sender) smtp.mailfrom=m@mavick.dev From: Eugene Mavick Date: Sat, 01 Aug 2026 18:59:42 +0800 Subject: [PATCH v4 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: <20260801-refcount-final-put-trace-v4-2-2e58678f0ffd@mavick.dev> References: <20260801-refcount-final-put-trace-v4-0-2e58678f0ffd@mavick.dev> In-Reply-To: <20260801-refcount-final-put-trace-v4-0-2e58678f0ffd@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-Developer-Signature: v=1; a=openpgp-sha256; l=1632; i=m@mavick.dev; h=from:subject:message-id; bh=6SxDI3lnGUVOqQynlBB5KrL+oi0ZqISwEqlbLLcv/7A=; b=owGbwMvMwCU2V/5U3e1aE3/G02pJDFm5FzdeTp94SLNX3H9W4vWzJQy1ntvEAl8fNlzxQov11 zxpDbnFHaUsDGJcDLJiiiw1WQnuGStnJRofndQDM4eVCWQIAxenAEzkvyfDP+vKxR8r+V5M/+XX EisVfOAMe7m22o3lgjd7ilweRTKZWDAyrBTY4lr+ubyZp1b30KMN1yo+a/4Rmctz5tetDbpGEa1 8HAA= X-Developer-Key: i=m@mavick.dev; a=openpgp; fpr=7C6A604768A99A6133C5928C9D1FCA7EDB7D344F X-Rspamd-Queue-Id: 4hC0NM5NPrz8twy 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, trace wrapper macro call address, and the refcount_t address. Signed-off-by: Eugene Mavick --- include/linux/refcount.h | 2 ++ lib/refcount.c | 6 +++++- 2 files changed, 7 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..ab88d4674942 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,10 @@ 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); + + do_trace_ref_final_put_cond(ret, r); + return ret; } EXPORT_SYMBOL(refcount_dec_if_one); =20 --=20 2.51.2 From nobody Fri Oct 2 12:19:29 2026 Received: from mout-p-201.mailbox.org (mout-p-201.mailbox.org [80.241.56.171]) (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 EF6FD34CFC3; Sat, 1 Aug 2026 11:01:08 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=80.241.56.171 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1785582070; cv=none; b=IftwZf2VMI5VGan0Sbav6zVhGTbvHtgaFYe2DT69IeaO8J8VDTKtZZnp2DzXnjmrcY7JAlebbWxMqTV2qUK8ibwmyCgknH7W0yVw1pFRLtR/byXjdS1tApiyZP8ELt3gPzIbGmNK2jzZMR/5YMTgxUzqM3Y+rx4RiMmx1xGzqAM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1785582070; c=relaxed/simple; bh=vVWsfTjSGw6e33WP2d/uyqteLjy9dxiqe2AmCt04NhY=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=FoSwsamcTIEzoisJGFtn4yNHb+PtTbVgYd1AGSETrLkjHxpJP0ZGsmHGK6bhfW9nH+0123EzMvDujWceUOC73KEbjIWB+h0WOOGuzToRh28YwlP1P+FBSR1WGUK7UEFf9D33uBrGMSG3uG+zku+1sws5pSmoRSgiF9CsXtj70IY= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=mavick.dev; spf=pass smtp.mailfrom=mavick.dev; dkim=pass (2048-bit key) header.d=mavick.dev header.i=@mavick.dev header.b=ULv7c+A+; arc=none smtp.client-ip=80.241.56.171 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=mavick.dev Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=mavick.dev Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=mavick.dev header.i=@mavick.dev header.b="ULv7c+A+" Received: from smtp1.mailbox.org (smtp1.mailbox.org [IPv6:2001:67c:2050:b231:465::1]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange x25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by mout-p-201.mailbox.org (Postfix) with ESMTPS id 4hC0NW26qRzMlG2; Sat, 01 Aug 2026 13:00:59 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=mavick.dev; s=MBO0001; t=1785582059; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=+WpDJUffFYkGym8OqsSvgqtunStF+Lue8wI1l/VKbI4=; b=ULv7c+A+XtrEfThv/Q/aTfNHm4m5vuNp8oaxIEWB+EiIKIi1CZzpbfhTbqY8Xv9fWH53/t IApqH1JzM8fgCZN624DOtTl28kZAqS3H0dWz8UnO0Xndus3evT1PK2mNnAnyJN01PWQuaE v4ncoSakk3Z6c5TiqRnlYyEFfE25xdTgn/2KbmoY0Fyjy776rxjweSduYTGdhp8Mj/RunU 7FxUYA4Kgyot1RVQ23atRYHJ3VTRTLugpoaeQ8ZIuQmcDg8Vk2cf97MSndqjd5ejHbP7wa tPGw4RL1smhArVOZ1imMzGZjnO8wLiZf9iS4rfN793SR9oMUEB7cxiHNZeiJ4g== Authentication-Results: outgoing_mbo_mout; dkim=none; spf=pass (outgoing_mbo_mout: domain of m@mavick.dev designates 2001:67c:2050:b231:465::1 as permitted sender) smtp.mailfrom=m@mavick.dev From: Eugene Mavick Date: Sat, 01 Aug 2026 18:59:43 +0800 Subject: [PATCH v4 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: <20260801-refcount-final-put-trace-v4-3-2e58678f0ffd@mavick.dev> References: <20260801-refcount-final-put-trace-v4-0-2e58678f0ffd@mavick.dev> In-Reply-To: <20260801-refcount-final-put-trace-v4-0-2e58678f0ffd@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-Developer-Signature: v=1; a=openpgp-sha256; l=1198; i=m@mavick.dev; h=from:subject:message-id; bh=vVWsfTjSGw6e33WP2d/uyqteLjy9dxiqe2AmCt04NhY=; b=owGbwMvMwCU2V/5U3e1aE3/G02pJDFm5FzdyWXXZb8zp+M/n8N/1V8Y3De8Lh26s7SlbfGn56 /ZHFQ2aHaUsDGJcDLJiiiw1WQnuGStnJRofndQDM4eVCWQIAxenAEzkci3DX0mjUxb3ftbvbzVg q/pX+6Ux8RDb/Q4Dt1l8Eic/T2z5VcXwh3PjSvUA/7lmtze/mStzIVOzZsPxdapdMu2Guyu/6x4 y5AQA X-Developer-Key: i=m@mavick.dev; a=openpgp; fpr=7C6A604768A99A6133C5928C9D1FCA7EDB7D344F X-Rspamd-Queue-Id: 4hC0NW26qRzMlG2 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 Fri Oct 2 12:19:29 2026 Received: from mout-p-201.mailbox.org (mout-p-201.mailbox.org [80.241.56.171]) (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 C633D31E830; Sat, 1 Aug 2026 11:01:09 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=80.241.56.171 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1785582071; cv=none; b=dLk7F15I91eSfzVGMl6ildzzomuyeiQ6QqyTw/diRGvXGRysDD6Sso8wZgmq6OJ+Jm8w1zs7CmjP8KPMHWDZljCkePZScSKtBF6FcVUDhqZaKB2mD7FhbFffFc2ig17gpwzZFOxyPsiYb7sF0Z5azscwREO3CaDw0lX1OlcUdIU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1785582071; c=relaxed/simple; bh=eH7MOUuGodxq09rRikBFhUdpvaOczF07qkwIpXsPdnM=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=bJv4LWnduxfzEyPFK6iujCAXFmBl4gGBs/P3D/AcPqdNRvo/MQ/SClwo96OQDkAvDKKVmTXjPPVFPusG0bvOOWPs5FeZCpPOEb+lkhB8DvSRtXTaUcdFvy5DR1xQFfCm2pE4xkAACh5C78l8SsSIILWMTFaW2gckyRq541DIWgI= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=mavick.dev; spf=pass smtp.mailfrom=mavick.dev; dkim=pass (2048-bit key) header.d=mavick.dev header.i=@mavick.dev header.b=xQS5CzkI; arc=none smtp.client-ip=80.241.56.171 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=mavick.dev Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=mavick.dev Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=mavick.dev header.i=@mavick.dev header.b="xQS5CzkI" Received: from smtp1.mailbox.org (smtp1.mailbox.org [IPv6:2001:67c:2050:b231:465::1]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange x25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by mout-p-201.mailbox.org (Postfix) with ESMTPS id 4hC0Nf70nBzMlGF; Sat, 01 Aug 2026 13:01:06 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=mavick.dev; s=MBO0001; t=1785582067; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=jTMRjFLThSMMc/w3StUWqu1LpTWLAsUOncWy0PHshAk=; b=xQS5CzkIcPO109Cb+436Qqh/cxYj3inN1re6Kjq5Boj4l3I1jsOvvNNKHFxwTEa+mnx5YT x8s+MjZu0OWymu98RI9/82lzQcCq+5xcpxjoM2ziXjyF6FhkJm3vslJ/Mr9rBm1jxMMFPU HNfDwHkPmKwNzJbuXSzQeUPfgKjbDHpJwAQjzOPGqB125yBnQ8tJzMcSotAA5FcPRnveHW OiUDYFDo4YJIFn8+mfHqQu+E79pWcbroR1NktHv5qvX+TePUiUdc+DeZRB4PB+1v0Uycg6 gp3BlzM4yye6JXSBRrlw7mUXCkB4wqZLmBCPZQBorRKi0smgiPiRAjT310k3xA== Authentication-Results: outgoing_mbo_mout; dkim=none; spf=pass (outgoing_mbo_mout: domain of m@mavick.dev designates 2001:67c:2050:b231:465::1 as permitted sender) smtp.mailfrom=m@mavick.dev From: Eugene Mavick Date: Sat, 01 Aug 2026 18:59:44 +0800 Subject: [PATCH v4 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: <20260801-refcount-final-put-trace-v4-4-2e58678f0ffd@mavick.dev> References: <20260801-refcount-final-put-trace-v4-0-2e58678f0ffd@mavick.dev> In-Reply-To: <20260801-refcount-final-put-trace-v4-0-2e58678f0ffd@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-Developer-Signature: v=1; a=openpgp-sha256; l=5222; i=m@mavick.dev; h=from:subject:message-id; bh=eH7MOUuGodxq09rRikBFhUdpvaOczF07qkwIpXsPdnM=; b=owGbwMvMwCU2V/5U3e1aE3/G02pJDFm5Fzd2a7LeeL66USZ99ZaZBwMjjzFOvK/B4bGnofX9+ Wb+hIspHaUsDGJcDLJiiiw1WQnuGStnJRofndQDM4eVCWQIAxenAExk7TSG36yLF5nvyLvHoHNv nZQ0wzRDhdyY6Ih0u79mvhcnaO340cvwv1TgpVYAuwuPSIqFlf4WA7GDt81l+iRYLmrNPH3VJPo bHwA= X-Developer-Key: i=m@mavick.dev; a=openpgp; fpr=7C6A604768A99A6133C5928C9D1FCA7EDB7D344F X-Rspamd-Queue-Id: 4hC0Nf70nBzMlGF 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 --- lib/Kconfig | 10 ++++ lib/tests/Makefile | 1 + lib/tests/ref_trace_kunit.c | 141 ++++++++++++++++++++++++++++++++++++++++= ++++ 3 files changed, 152 insertions(+) diff --git a/lib/Kconfig b/lib/Kconfig index 00a9509636c1..7e2746be6d16 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 && TRACEPOINTS + 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..17f6936ecc1a --- /dev/null +++ b/lib/tests/ref_trace_kunit.c @@ -0,0 +1,141 @@ +// SPDX-License-Identifier: GPL-2.0 +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +struct data { + unsigned long caller; + unsigned long ip; + 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() \ + do { \ + /* wait for probe completion */ \ + int notimeout =3D wait_var_event_timeout( \ + &capture.count, \ + atomic_read_acquire(&capture.count), \ + msecs_to_jiffies(10000) \ + ); \ + \ + unregister_trace_ref_trace_final_put(probe, NULL); \ + tracepoint_synchronize_unregister(); \ + \ + KUNIT_ASSERT_TRUE(test, notimeout); \ + \ + KUNIT_EXPECT_EQ(test, atomic_read_acquire(&capture.count), 1); \ + \ + KUNIT_EXPECT_TRUE(test, __kernel_text_address(capture.caller)); \ + KUNIT_EXPECT_TRUE(test, __kernel_text_address(capture.ip)); \ + \ + KUNIT_EXPECT_PTR_EQ(test, capture.obj, &obj); \ + } while (0) + +static void probe( + void *ignore, + unsigned long caller, + unsigned long ip, + const void *obj) +{ + //prevent non test func final_puts from changing captured values + if (chk_obj !=3D obj) + return; + + capture.caller =3D caller; + capture.ip =3D ip; + capture.obj =3D obj; + + atomic_inc_return_release(&capture.count); //increase count +} + +static 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(); +} + +static 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(); +} +static void dummy_release(struct percpu_ref *ref) {} + +static void test_percpu_ref_put_many(struct kunit *test) +{ + struct percpu_ref obj; + + test_init(); + + KUNIT_ASSERT_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_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 Fri Oct 2 12:19:29 2026 Received: from mout-p-201.mailbox.org (mout-p-201.mailbox.org [80.241.56.171]) (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 6D515434E3F; Sat, 1 Aug 2026 11:01:17 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=80.241.56.171 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1785582079; cv=none; b=CFiZqvgkwTPnQ9ZATHrixy87blYMYurshBbZyVoEWqZJil+Ct3Z3pAQCTnFldbDN5GK/lSmfJkDbBkmXR0yPvL/MGMYw1FAIIUFnjX/ahZQWt0RUxsE/cFlkF9pdopjHgURvCsLqxjyyA+78ypn4RxMRXq7BynuIM4/d1a7sTnk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1785582079; c=relaxed/simple; bh=h+1tyznbFEOyuRy4iHAT3B3e7gGvTqt1Z3Rotq3NoN4=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=qV73fl1/Env1adXHVWJkg+IiV69ncFL26CQmCwtLMqCXbGWQdMRMUW51h9dPJmCRQ/MaskIMCQch/F2hWeVIATCGUBBRcUUobW8k9FsjpAW57w7/C8EtCOtXsQ//Dy532dFLzTYtuPMuBzSm8Fo6go101QhTnCi/q3utf151A44= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=mavick.dev; spf=pass smtp.mailfrom=mavick.dev; dkim=pass (2048-bit key) header.d=mavick.dev header.i=@mavick.dev header.b=AorQS7Si; arc=none smtp.client-ip=80.241.56.171 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=mavick.dev Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=mavick.dev Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=mavick.dev header.i=@mavick.dev header.b="AorQS7Si" Received: from smtp1.mailbox.org (smtp1.mailbox.org [10.196.197.1]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange x25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by mout-p-201.mailbox.org (Postfix) with ESMTPS id 4hC0Np31s8zMlGF; Sat, 01 Aug 2026 13:01:14 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=mavick.dev; s=MBO0001; t=1785582074; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=X2tAcnncj6o0PHFFxyod2DNFn1o5vZ+4t+GMElLi3co=; b=AorQS7Si71jqPNImikVnoCdY3qQL7ZkJwLQy0kMLeWprNO4dp1Mi/DQu7E90vUsqDkQ3M7 CIKww38MOhb6LFa2MtMfYVwU7Di045mM5wTIJUV31WiQUcBbsKa8vGfAt26mSLvoFvIX/c KEBrtvolW72taHH1DAIO4Jz+ewR5dquLLIOX1JvTkOCDSmlljLqkONjT9CJnZjGjBuWwd2 9lHznKBGJELfD1PYQ3fBtrmax/gvFcFP6qV6+SKHJ06Hr0WvVusixGCMRIzuIMlZTAaaYE t2O7JBGq/t+FMsuzJmYrSQBzj1aDaJfxqfZyMSVK0cegXQyUcxC8eNko6YpjFQ== From: Eugene Mavick Date: Sat, 01 Aug 2026 18:59:45 +0800 Subject: [PATCH v4 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: <20260801-refcount-final-put-trace-v4-5-2e58678f0ffd@mavick.dev> References: <20260801-refcount-final-put-trace-v4-0-2e58678f0ffd@mavick.dev> In-Reply-To: <20260801-refcount-final-put-trace-v4-0-2e58678f0ffd@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-Developer-Signature: v=1; a=openpgp-sha256; l=627; i=m@mavick.dev; h=from:subject:message-id; bh=h+1tyznbFEOyuRy4iHAT3B3e7gGvTqt1Z3Rotq3NoN4=; b=kA0DAAoWnR/Kftt9NE8ByyZiAGpt0bGgjb/NhgHvR4Tr44j/3KJicMhoZci7EHLmotrvEr3vp Ih1BAAWCgAdFiEEfGpgR2ipmmEzxZKMnR/Kftt9NE8FAmpt0bEACgkQnR/Kftt9NE+eYwD+LJwi Q+XJqOR3j9bhKBUOvGMA3SsNpH1gfTfMSJdnq7MBANwbO5fyJD8wpKF8RYRyJmx5jOJLld3w9EH 8Mwz7jVQJ X-Developer-Key: i=m@mavick.dev; a=openpgp; fpr=7C6A604768A99A6133C5928C9D1FCA7EDB7D344F Add new files added in the patch series to MAINTAINERS Signed-off-by: Eugene Mavick --- 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