From nobody Sun Jul 26 01:47:31 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 412A2286D70; Sat, 11 Jul 2026 04:41:09 +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=1783744870; cv=none; b=ibHjaoFIIEOzzgDLd+8gjYdRaBCnt7pxBIZQP5U4S4Tpz+tSGZSBDM83lAtDIc60J5U4gZepMlTrdkifKNODECjwPKUByCM2Mx963Xlvak+YW8BotiSUBV1sTF0CyePfBj+iMPVNYjMSY9rBJDxj3j3bGgyEFfcCqSrr/kilvvA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1783744870; c=relaxed/simple; bh=togALykcuUOAaOKZnU5+keMWktwavqkGC+6SZdVlIpU=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=WOS9LrahBEKY/aUvmGq7mx5NrNQ/gw7cfeHC+H4sVHgRBjgpqaY+vqLhjTKG0kvYQmFA1vZkWnSiM8PoRo9UH3VFbspLaPpcObuRbSbNPfEdm1b6G+cfVJC6gxC6x9MckjaQ6Q0UTty/PkMfCEipxRTFnh81viSbYxYuPgSH408= 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=i9Xo9o4D; 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="i9Xo9o4D" 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 SHA512) (No client certificate requested) by mout-p-101.mailbox.org (Postfix) with ESMTPS id 4gxwxt41NSz8v1p; Sat, 11 Jul 2026 06:41:06 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=mavick.dev; s=MBO0001; t=1783744866; 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=5fRYBbl/zFHo/ph55CpxjBKpforkcs9UdExdzuIQ868=; b=i9Xo9o4De01YdLlfiFfiUO4cTTk+rdHz9XoaG27qFEXE8Dr2HpfhUlzoH9sqJ5FJ5+S3p2 1UDvwaIavR7TQSjMd2scSqfFieEPssjD9yEbWuGaHjjl97INZAgw7UhSqPXsG3KssX5oKj xcgRhqffsfD+Qinnf1y0HAwo2t3xOrQyoHAKk1EHFp8fI/j/FMWUnVnO2oX/J4Or3eKz2y pJrR25bJCRYlGvZ+653qV72ImAm3q310S/yZD0u5B2l8qrtCocQ/vcpNM6NYglC458J0Ep lv/gn1+E4i0utnWL+Kf7TIy7X+dxPN3rhbY0b8GqJxF3X25uA6atUto+AAsYCg== 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, 11 Jul 2026 12:40:19 +0800 Subject: [PATCH v3 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: <20260711-refcount-final-put-trace-v3-1-674c8f03d9f9@mavick.dev> References: <20260711-refcount-final-put-trace-v3-0-674c8f03d9f9@mavick.dev> In-Reply-To: <20260711-refcount-final-put-trace-v3-0-674c8f03d9f9@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=4418; i=m@mavick.dev; h=from:subject:message-id; bh=togALykcuUOAaOKZnU5+keMWktwavqkGC+6SZdVlIpU=; b=owGbwMvMwCU2V/5U3e1aE3/G02pJDFmBJ20vBxSl+CqVOyzui55aOcfuqR/rIrPjf5PmrK87L bWtzWxSRykLgxgXg6yYIktNVoJ7xspZicZHJ/XAzGFlAhnCwMUpABMpNGT4K/dCtMKuz5OT9X4p /92Sq1Pn3Shns5Xlrl2m2WT1xm5bFSPDCo7AM3P+er9OesvRs++ogfkx9pdPPt+sDmyqMPwXWHe BGwA= X-Developer-Key: i=m@mavick.dev; a=openpgp; fpr=7C6A604768A99A6133C5928C9D1FCA7EDB7D344F X-Rspamd-Queue-Id: 4gxwxt41NSz8v1p 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 --- include/linux/ref_trace.h | 26 +++++++++++++++++++++++ include/trace/events/ref_trace.h | 46 ++++++++++++++++++++++++++++++++++++= ++++ lib/Makefile | 2 ++ lib/ref_trace.c | 13 ++++++++++++ 4 files changed, 87 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..3bf1b1d390b8 --- /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: return address of refcount + * 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 do_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..466647772ce1 --- /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, 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:47:31 2026 Received: from mout-p-103.mailbox.org (mout-p-103.mailbox.org [80.241.56.161]) (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 07F3728F949; Sat, 11 Jul 2026 04:41:22 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=80.241.56.161 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1783744884; cv=none; b=MKcqQp+m+xYsefrcY1tXEdRkxAPrXKjtxDbcOM//uGQHoi6sOhKEtGlAGcTuCUeJPtPlf6dcMgVA0q8JPUH3jj/Pn5yNB5RuWpymjDM+GPP10+WzEic5pH4HFjTR13BKXoi+gV1lx9ORgyy4kMeqI9oLfZZlufNVQW+NB1EnKGE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1783744884; c=relaxed/simple; bh=aDxzfY6bT7IXzsjvVe+q/A+3qkycq7qCD2NucCrMON0=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=lmHKui8uaDTrTNNQESmwrPL0ry9Kups1PRYYrPNg5I8ju2K9GuyUfWaQiFxAfQYqBHOpciDOvpLTKS9WUOPKYMaq6AtHU955tpqI3H/3d8WM7EcvVnFULjcdal3bRITUvhkKU3AuyvWmYuEOgFAXPjEomnC6gyO+1avMMjBRjHs= 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=g7EnXko/; arc=none smtp.client-ip=80.241.56.161 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="g7EnXko/" 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 SHA512) (No client certificate requested) by mout-p-103.mailbox.org (Postfix) with ESMTPS id 4gxwy20X6BzKnH8; Sat, 11 Jul 2026 06:41:14 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=mavick.dev; s=MBO0001; t=1783744874; 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=cyrDKc6+UkaIRpwi1rdgsrlg/YNeGkCxs/T+ipMB7oQ=; b=g7EnXko/wyf0WQiL5mY/hafIxjSHVbzwUQZMxD7y2AjmpFOraUFpGjgXeQGUdtmooi7BiD +95iclcvJevGEr+gvO5jx6jh8beqtHZ5ASeZeSEoCl7XDwGCtzYthQqS5MAPUY/QLC7TmR HiZqrlD+ldR0967fvHLseFSrwD1RYtD5DdHcOgBN05ptp5FpiAts9oq7CcneBV1l0PBBsb iiUwVdxtrVsxHeQWpr/qFOUHBKLUiF6bl/a1C/pqihKhWsIc70IgwOS6Yo/R8WFQEzFUuX 2M3VEi7sXTuPLGXGXj6uOfkSd8rezz/WHUAHHwapS5yPKFb79KXzQOuMYOfgDg== 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, 11 Jul 2026 12:40:20 +0800 Subject: [PATCH v3 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: <20260711-refcount-final-put-trace-v3-2-674c8f03d9f9@mavick.dev> References: <20260711-refcount-final-put-trace-v3-0-674c8f03d9f9@mavick.dev> In-Reply-To: <20260711-refcount-final-put-trace-v3-0-674c8f03d9f9@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=1624; i=m@mavick.dev; h=from:subject:message-id; bh=aDxzfY6bT7IXzsjvVe+q/A+3qkycq7qCD2NucCrMON0=; b=owGbwMvMwCU2V/5U3e1aE3/G02pJDFmBJ22DJp8//+h3OEPKfJ3Ewv/Xkitub5dM3K7tfU5r/ 5/zC3XYO0pZGMS4GGTFFFlqshLcM1bOSjQ+OqkHZg4rE8gQBi5OAZjIRSeG/yW3P+YbybFllu5j XOG+Q710ps4jq+cLFoXt+vZv/ZQnRwUYGV6lCb9T3Pv7ktqnjtyS83Nfex9mWiTsPs38UpC3TbT NHzYA X-Developer-Key: i=m@mavick.dev; a=openpgp; fpr=7C6A604768A99A6133C5928C9D1FCA7EDB7D344F X-Rspamd-Queue-Id: 4gxwy20X6BzKnH8 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 --- 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:47:31 2026 Received: from mout-p-103.mailbox.org (mout-p-103.mailbox.org [80.241.56.161]) (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 C38CE27E1DC; Sat, 11 Jul 2026 04:41:23 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=80.241.56.161 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1783744885; cv=none; b=XhxIPIXBbRYmUscscdRulEpV7ltoqCUM5MnoobIgiqBjeqVWVqczWM23bhfpwfOkKzJql/FsgN8Re11Gj6cLpOJ0whdvsbHywmxoEgqZK+y67gt/7ktB+L4rrYIqtmG4QO8/E1qmR8BJAEgR+LYPhC2ENioqD9gSUNRsATFSPZw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1783744885; c=relaxed/simple; bh=vVWsfTjSGw6e33WP2d/uyqteLjy9dxiqe2AmCt04NhY=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=QwIbF281s6nZwLRP1qh4b5W08t5AaXgd5fFbE3aKb2BzeBMROcIBloi0yTIK5NCkCGsJ4AJTIBdWetgD1h7EBAJY1iverSnxdyOmuMXgxUdGzsRhlo59PI7/6ku5Fs9Knh/n0wpQlCT5lYpSK86CSJJF0OObY8sV6XSEJ1w/1L4= 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=Q09mZMDB; arc=none smtp.client-ip=80.241.56.161 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="Q09mZMDB" 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 SHA512) (No client certificate requested) by mout-p-103.mailbox.org (Postfix) with ESMTPS id 4gxwy864p4zKnRd; Sat, 11 Jul 2026 06:41:20 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=mavick.dev; s=MBO0001; t=1783744880; 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=Q09mZMDBC3SL2Aw+Qz9LF/nU3tBPOXtA6NdSL96a1e8e8dmySvxy5+Cfav+dkJpUXt22Pi JeN7JHaUTsAqERdODP154c/17aUIO4dKH5MAOuelbkZ5GEFW/i/NKgj/6WlHcW5FpM7uhO i5rLjrNn5a1pz1g3kA1r7mTnhShvmN8lxPAHrUDN+EQafdh5oIfunKkvPD2J+x563LjEOP JRPVKmM3yFLQFzjsRSNWLx2atapc2C4jCSyRqf8Rssc7+dM7+qz1NnnVqBitsri7r/WL+X jRVuFMDF3MjMzuovC779vCM5SA7B8CbytS+qTUxu67X96SFDqcx+jN4JTGBedg== 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, 11 Jul 2026 12:40:21 +0800 Subject: [PATCH v3 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: <20260711-refcount-final-put-trace-v3-3-674c8f03d9f9@mavick.dev> References: <20260711-refcount-final-put-trace-v3-0-674c8f03d9f9@mavick.dev> In-Reply-To: <20260711-refcount-final-put-trace-v3-0-674c8f03d9f9@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/G02pJDFmBJ23vb111w/3P63trb8dvX1hUVb2tbVrViy+RZlzPg soe2hQ+7yhlYRDjYpAVU2SpyUpwz1g5K9H46KQemDmsTCBDGLg4BWAiR58z/C+57H/Vr0pIcnoy z/HdNlUnXF9PqMw9nTRVSt3p+OOmJAGG/+GL125TbDn11LJ25e+wI+kO2X/rNbp9ehKjTgduMxd 14QQA X-Developer-Key: i=m@mavick.dev; a=openpgp; fpr=7C6A604768A99A6133C5928C9D1FCA7EDB7D344F X-Rspamd-Queue-Id: 4gxwy864p4zKnRd 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:47:31 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 01C221D54FA; Sat, 11 Jul 2026 04:41:31 +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=1783744894; cv=none; b=XQsneMOV7k1HJ9DI3BZjAzq3kneeGb5+yL4Wag22+HsFCxfkulpdDKcgWqFcT54yGkXHP3LTrwDkcMFxdjhotWcBQhnG+88HFEUy1hNmsb8TbVvL/mmOl1gGdbmo6jVU/Utvo1O0/e2aJWy1ZCrSnTN2bdYxwU9pEEFPVWyiNZU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1783744894; c=relaxed/simple; bh=Ley3/Ev0Q2dr2jIjm9hqbGpVu2QNbexoD9klE+g+ihI=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=XYIlC+SCOfaXbU2snUCBRUkHS2SKPY4lfKm8dtljRQrxaRTWn7swCdDPZAmSgSSBeaxIJNKg9iiGX14U6kVqTCloMlKx8frg4EYhaTLMzUCL+aHI+V7gEMZUiqlrmq3zvoW5UarsCFoMOHUUSScDfemBn8oaj6apQastSVUbTCU= 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=VX6klacL; 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="VX6klacL" 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 SHA512) (No client certificate requested) by mout-p-202.mailbox.org (Postfix) with ESMTPS id 4gxwyJ1tdCzMlFS; Sat, 11 Jul 2026 06:41:28 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=mavick.dev; s=MBO0001; t=1783744888; 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=gu1/GeFwQRCYl7cwa5Py8T4WqePQUyQJElSNNKRQt2U=; b=VX6klacLvGbraNVI0tsWnJLZ4Z7xEg0Tn9pY1gdHncVs4jXsgW9wvOZFd/RZvnSPLF2AIp LQ/dLfoa4gyQa/96mQvY+Y+qogB3zBhKlJPOAbwRZrh2mehLMDUFpUimdprjJXK4+m1rWs mdhTYOKZQ2QKw45NErA97e2UnWA0DTsK3GbZ96V0hjJ2EU6K+Ww+JAcCejJFhJzYF9+NRb G0HtLdTwxmnqSol6GpF+8nImSCDwJhbalio5DZzXsCpgvBUiFRxZqEiDd53cojfm564ybg Hjs4jB/v610TbOsLdFpyUwmR/8J55l0eJlfCxH77pQlesIUEvWVEa3bhOI0Y5A== 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, 11 Jul 2026 12:40:22 +0800 Subject: [PATCH v3 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: <20260711-refcount-final-put-trace-v3-4-674c8f03d9f9@mavick.dev> References: <20260711-refcount-final-put-trace-v3-0-674c8f03d9f9@mavick.dev> In-Reply-To: <20260711-refcount-final-put-trace-v3-0-674c8f03d9f9@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=5255; i=m@mavick.dev; h=from:subject:message-id; bh=Ley3/Ev0Q2dr2jIjm9hqbGpVu2QNbexoD9klE+g+ihI=; b=owGbwMvMwCU2V/5U3e1aE3/G02pJDFmBJ23LzK0+r1K90OyeKclYb566l3tPX09W++Rwll225 woFDid1lLIwiHExyIopstRkJbhnrJyVaHx0Ug/MHFYmkCEMXJwCMJGwXEaGDfEX3BawGGj8eBP4 lMG29MP9ythbz1blvNjKwrKYtcOFkZFh4pInC6ceuWIdc2R5uEex6eXVhw4aPbsleEarw13oj+x JZgA= X-Developer-Key: i=m@mavick.dev; a=openpgp; fpr=7C6A604768A99A6133C5928C9D1FCA7EDB7D344F X-Rspamd-Queue-Id: 4gxwyJ1tdCzMlFS 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..8f896adc96d1 --- /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 + +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 */ \ + int notimeout =3D wait_var_event_timeout( \ + &capture.count, \ + atomic_read_acquire(&capture.count), \ + 10 \ + ); \ + \ + KUNIT_ASSERT_TRUE(test, notimeout); \ + \ + unregister_trace_ref_trace_final_put(probe, NULL); \ + tracepoint_synchronize_unregister(); \ + \ + 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; + capture.fn =3D fn; + 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(__refcount_sub_and_test); +} + +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(refcount_dec_if_one); +} +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_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:47:31 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 E5E3B2D7D2E; Sat, 11 Jul 2026 04:41:37 +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=1783744900; cv=none; b=gCp1otWMtjnFpgh2msIHIsjxu6AAyBTwyKYWVFNiIGx89/L1TxxTp/23W2svb6qX6/aF2Y84N/t7ODf+C+GJW0VnNtlyngsVz7Z0wDFFBYs1Rzuv9m1a/H+/1Xw8ZmmLL+507XIxr4uikhhZI0H9ZdQ2HV7gbdaqic71UaVEFF4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1783744900; c=relaxed/simple; bh=h+1tyznbFEOyuRy4iHAT3B3e7gGvTqt1Z3Rotq3NoN4=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=cOLCbXVWGG4bDhA/yog5+54dtR48YUht3ycEvq/MM+61LCg/z3oYW6B493v4FByXvNWVzxQI1TyxNjOPELDpUwGdw/llQn+L379Ut74H1nIwIBgentfQs9+sgPG53PwUU6wQ7xFb5Wz+hb4546sqosTrwcidFjOImWIf3edaJWk= 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=YKPgpTkN; 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="YKPgpTkN" 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 SHA512) (No client certificate requested) by mout-p-101.mailbox.org (Postfix) with ESMTPS id 4gxwyR0NHYz8tyn; Sat, 11 Jul 2026 06:41:35 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=mavick.dev; s=MBO0001; t=1783744895; 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=YKPgpTkNGHTkTmCpFJvuS4tmvjFHHnLcQnpxSY0fG3To+T2mWazjnSrVml1aTmPMLJatDv +KYB5awsHiUUOoOPgSNKwCAB8HgkMZCPiyvP6ZszW+J2BDYF6GEMjdoOaSI1QclRAtaU+b VnDUyd9idDjkuYGleHKVxXWpxjN53O00xtUOVNjBq8+pLFUayKd9xOC0iz6lCONcQMbxuY CZMp/DRgOiqSFrWexUbOLasDOnIi6I16CqFxBiqrGoPbPkabvJpHD9t6FcCC5QS3LCb4+M 81c5XFlGW61FpcPANvkVzhuBZ6Z9sJm46WCVC//IyLYqk/3eNs+hSHB4dbfqgA== From: Eugene Mavick Date: Sat, 11 Jul 2026 12:40:23 +0800 Subject: [PATCH v3 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: <20260711-refcount-final-put-trace-v3-5-674c8f03d9f9@mavick.dev> References: <20260711-refcount-final-put-trace-v3-0-674c8f03d9f9@mavick.dev> In-Reply-To: <20260711-refcount-final-put-trace-v3-0-674c8f03d9f9@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=owGbwMvMwCU2V/5U3e1aE3/G02pJDFmBJ21v2yzi+zRlaZGezLG5JizShe9a81LsYvQ/nV1kc muu64IvHaUsDGJcDLJiiiw1WQnuGStnJRofndQDM4eVCWQIAxenAEwk5zgjw6YdTc9mn0rWMTs2 x5ljwrekf347GjOX7hLiMXs1WVH1WjIjw5pCnp3mIY8Wzpjy/JSF97mX+otcX1xIZtBkFpYJqpv UyAUA 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