Skip to contentSkip to conversation

tracing: add ref_trace_final_put tracing

PATCH Superseded Eugene Mavick 3 participants 9 messages Aug 1 – Aug 3

Subsystems (5)

v4 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#0 lore raw

v4 cover tracing: add ref_trace_final_put tracing

When debugging use-after-free(UAF) bugs, knowing when the object reaches 0 references and enters final release can significantly aid the debugging process.

There is currently no universal way to trace this information.

This patch series implements tracing of the final puts in the most widely used refcounting implementations, refcount_t(and thus kref which uses it), and percpu-ref.

The tracepoint records three fields:
- caller: function that called the refcounting function(refcount_sub_and_test, percpu_ref_put_many)
- ip: return address of the trace wrapper macro call
- obj: refcount object(struct percpu_ref, refcount_t)

---
Changes in v4:
ref-trace:
-remove fn
-add ip variable
-change trace wrapper macro respectively, _THIS_IP_ is used for ip variable -change relevant code respect to fn removal and ip addition
-fix style issues in include/linux/ref_trace.h
-add new macro do_trace_ref_final_put_cond that only calls tracepoint if cond is
true
lib/refcount.c:
-change from do_trace_ref_final_put to *_cond
-remove if statement above since _cond already performs the check KUnit:
-change relevant code respect to fn removal and ip addition
-check if caller and ip are valid addresses
-change timeout from 10 jiffies to 10 seconds
-move didn't timeout assertion from before to after probe
unregistration, to prevent it from impacting next test

Changes in v3:
include/trace/events/ref_trace.h kernel doc comments:
-caller of refcount function -> return address of refcount function -ref_trace_final_put->do_ref_trace_final_put lib/ref_trace.c: add include trace/events/ref_trace.h kunit:
-change Kconfig depends from FTRACE->TRACEPOINTS -EXPECT_FALSE->ASSERT_FALSE for calling percpu_ref_init -add tracepoint_synchronise_unregister to test_exit macro -added timeout to capture.count waiting
-remove noinline and __always_inline from function attributes
(added for testing, but accidentally submitted)
-add period to the end of Kconfig help text
v2 link:
https://lore.kernel.org/all/[email protected]/

Changes in v2:
-include/linux/ref_trace.h: change macro name, use direct tracepoint
call in macro to avoid double check
-add tracepoint to refcount_dec_if_one
-kunit: make significant improvements to design, fix critical bug, add test case for
refcount_dec_if_one()
-Link to v1: https://lore.kernel.org/r/[email protected]

---
Eugene Mavick (5):
tracing: add ref_trace_final_put tracepoint
refcount: add ref_trace_final_put tracepoint
percpu-refcount: add ref_trace_final_put trace
kunit: add test for ref_trace_final_put
MAINTAINERS: add entries for ref_trace_final_put

 MAINTAINERS                      |   3 +
 include/linux/percpu-refcount.h  |   5 +-
 include/linux/ref_trace.h        |  40 +++++++++++
 include/linux/refcount.h         |   2 +
 include/trace/events/ref_trace.h |  51 ++++++++++++++
 lib/Kconfig                      |  10 +++
 lib/Makefile                     |   2 +
 lib/ref_trace.c                  |  13 ++++
 lib/refcount.c                   |   6 +-
 lib/tests/Makefile               |   1 +
 lib/tests/ref_trace_kunit.c      | 141 +++++++++++++++++++++++++++++++++++++++
 11 files changed, 272 insertions(+), 2 deletions(-)

---
base-commit: df685633c3dbc67441cc86f1c3fee58de4652ba2
change-id: 20260624-refcount-final-put-trace-49bd7c39bd5a

Best regards,

Signature · 1 line
Eugene Mavick <[email protected]>
#1 lore raw

PATCH v4 1/5 tracing: add ref_trace_final_put tracepoint

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)

---
 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(+)
include/linux/ref_trace.h new file +40 -0
new file mode 100644
index 000000000000..c93c30bc20d2
@@ -0,0 +1,40 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+#ifndef _LINUX_REF_TRACE_H
+#define _LINUX_REF_TRACE_H
+
+#include <linux/tracepoint-defs.h>
+#include <linux/instruction_pointer.h>
+
+/* 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 */
include/trace/events/ref_trace.h new file +51 -0

51 lines not on this page · raw message

lib/Makefile +2 -0
index f33a24bf1c19..41737090a95d 100644
@@ -335,3 +335,5 @@ CONTEXT_ANALYSIS_test_context-analysis.o := y
 obj-$(CONFIG_CONTEXT_ANALYSIS_TEST) += test_context-analysis.o
 
 subdir-$(CONFIG_FORTIFY_SOURCE) += test_fortify
+
+obj-$(CONFIG_TRACEPOINTS) += ref_trace.o
lib/ref_trace.c new file +13 -0
new file mode 100644
index 000000000000..22fe372f468f
@@ -0,0 +1,13 @@
+// SPDX-License-Identifier: GPL-2.0
+#define CREATE_TRACE_POINTS
+#include <trace/events/ref_trace.h>
+#include <linux/ref_trace.h>
+
+//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);
Signature · 1 line
2.51.2
#2 lore raw

PATCH v4 2/5 refcount: add ref_trace_final_put tracepoint

In reply to 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, trace wrapper macro call address, and the refcount_t address.

---
 include/linux/refcount.h | 2 ++
 lib/refcount.c           | 6 +++++-
 2 files changed, 7 insertions(+), 1 deletion(-)
include/linux/refcount.h +2 -0
index ba7657ced281..70d07a462da1 100644
@@ -107,6 +107,7 @@
 #include <linux/limits.h>
 #include <linux/refcount_types.h>
 #include <linux/spinlock_types.h>
+#include <linux/ref_trace.h>
 
 struct mutex;
 
@@ -393,6 +394,7 @@ bool __refcount_sub_and_test(int i, refcount_t *r, int *oldp)
 
 	if (old > 0 && old == i) {
 		smp_acquire__after_ctrl_dep();
+		do_trace_ref_final_put(r);
 		return true;
 	}
 
lib/refcount.c +5 -1
index a207a8f22b3c..ab88d4674942 100644
@@ -7,6 +7,7 @@
 #include <linux/refcount.h>
 #include <linux/spinlock.h>
 #include <linux/bug.h>
+#include <linux/ref_trace.h>
 
 #define REFCOUNT_WARN(str)	WARN_ONCE(1, "refcount_t: " str ".\n")
 
@@ -56,7 +57,10 @@ bool refcount_dec_if_one(refcount_t *r)
 {
 	int val = 1;
 
-	return atomic_try_cmpxchg_release(&r->refs, &val, 0);
+	bool ret = atomic_try_cmpxchg_release(&r->refs, &val, 0);
+
+	do_trace_ref_final_put_cond(ret, r);
+	return ret;
 }
 EXPORT_SYMBOL(refcount_dec_if_one);
 
Signature · 1 line
2.51.2
#3 lore raw

PATCH v4 3/5 percpu-refcount: add ref_trace_final_put trace

In reply to 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).

---
 include/linux/percpu-refcount.h | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
include/linux/percpu-refcount.h +4 -1
index d73a1c08c3e3..f870ac0e8c06 100644
@@ -55,6 +55,7 @@
 #include <linux/rcupdate.h>
 #include <linux/types.h>
 #include <linux/gfp.h>
+#include <linux/ref_trace.h>
 
 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_ref *ref, unsigned long nr)
 
 	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);
+	}
 
 	rcu_read_unlock();
 }
Signature · 1 line
2.51.2
#4 lore raw

PATCH v4 4/5 kunit: add test for ref_trace_final_put

In reply to 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.

---
 lib/Kconfig                 |  10 ++++
 lib/tests/Makefile          |   1 +
 lib/tests/ref_trace_kunit.c | 141 ++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 152 insertions(+)
lib/Kconfig +10 -0
index 00a9509636c1..7e2746be6d16 100644
@@ -52,6 +52,16 @@ config PACKING_KUNIT_TEST
 
 	  When in doubt, say N.
 
+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
 
lib/tests/Makefile +1 -0
index 7e9c2fa52e35..828a030ad8c7 100644
@@ -57,5 +57,6 @@ obj-$(CONFIG_USERCOPY_KUNIT_TEST) += usercopy_kunit.o
 obj-$(CONFIG_UTIL_MACROS_KUNIT) += util_macros_kunit.o
 obj-$(CONFIG_RATELIMIT_KUNIT_TEST) += test_ratelimit.o
 obj-$(CONFIG_UUID_KUNIT_TEST) += uuid_kunit.o
+obj-$(CONFIG_REF_TRACE_KUNIT_TEST) += ref_trace_kunit.o
 
 obj-$(CONFIG_TEST_RUNTIME_MODULE)		+= module/
lib/tests/ref_trace_kunit.c new file +141 -0

141 lines not on this page · raw message

Signature · 1 line
2.51.2
#5 lore raw

PATCH v4 5/5 MAINTAINERS: add entries for ref_trace_final_put

In reply to Eugene Mavick

Add new files added in the patch series to MAINTAINERS

---
 MAINTAINERS | 3 +++
 1 file changed, 3 insertions(+)
MAINTAINERS +3 -0
index 10e8253181d3..2dbb7441906e 100644
@@ -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/
Signature · 1 line
2.51.2
maintainer
#6 lore raw
In reply to Eugene Mavick
Quoted 10 lines · Eugene Mavick

When debugging use-after-free(UAF) bugs, knowing when the object reaches 0 references and enters final release can significantly aid the debugging process.

There is currently no universal way to trace this information.

This patch series implements tracing of the final puts in the most widely used refcounting implementations, refcount_t(and thus kref which uses it), and percpu-ref.

Quoted 7 lines

The tracepoint records three fields:
- caller: function that called the refcounting function(refcount_sub_and_test, percpu_ref_put_many)
- ip: return address of the trace wrapper macro call
- obj: refcount object(struct percpu_ref, refcount_t)

I'm not sure this Changelog is convincing me. To me this just looks like more weight / bloat without benefit.

#8 lore raw
In reply to Eugene Mavick
Quoted 10 lines · On Sat, 01 Aug 2026 18:59:40 +0800 Eugene Mavick <[email protected]> wrote:

When debugging use-after-free(UAF) bugs, knowing when the object reaches 0 references and enters final release can significantly aid the debugging process.

There is currently no universal way to trace this information.

This patch series implements tracing of the final puts in the most widely used refcounting implementations, refcount_t(and thus kref which uses it), and percpu-ref.

I have a question regarding the event group name. If this is related to refcount, wouldn't it be more appropriate to call it refcount_final_put? Even if it is currently used by ref_trace, it is fundamentally an event belonging to the refcount subsystem.
I believe event names should be based on where the event occurs and what actually happens, rather than on who is using it.

Thank you,

Quoted 80 lines

The tracepoint records three fields:
- caller: function that called the refcounting function(refcount_sub_and_test, percpu_ref_put_many)
- ip: return address of the trace wrapper macro call
- obj: refcount object(struct percpu_ref, refcount_t)

---
Changes in v4:
ref-trace:

(quoted diff) +0 -6
-remove fn
-add ip variable
-change trace wrapper macro respectively, _THIS_IP_ is used for ip variable
-change relevant code respect to fn removal and ip addition
-fix style issues in include/linux/ref_trace.h
-add new macro do_trace_ref_final_put_cond that only calls tracepoint if cond is
 true

lib/refcount.c:

(quoted diff) +0 -2
-change from do_trace_ref_final_put to *_cond
-remove if statement above since _cond already performs the check

KUnit:

(quoted diff) +0 -4
-change relevant code respect to fn removal and ip addition
-check if caller and ip are valid addresses
-change timeout from 10 jiffies to 10 seconds
-move didn't timeout assertion from before to after probe
 unregistration, to prevent it from impacting next test

Changes in v3:
include/trace/events/ref_trace.h kernel doc comments:

(quoted diff) +0 -2
-caller of refcount function -> return address of refcount function
-ref_trace_final_put->do_ref_trace_final_put

lib/ref_trace.c: add include trace/events/ref_trace.h kunit:

(quoted diff) +0 -6
-change Kconfig depends from FTRACE->TRACEPOINTS
-EXPECT_FALSE->ASSERT_FALSE for calling percpu_ref_init
-add tracepoint_synchronise_unregister to test_exit macro
-added timeout to capture.count waiting
-remove noinline and __always_inline from function attributes
 (added for testing, but accidentally submitted)
-add period to the end of Kconfig help text

v2 link:
https://lore.kernel.org/all/[email protected]/

Changes in v2:

(quoted diff) +0 -5
-include/linux/ref_trace.h: change macro name, use direct tracepoint
 call in macro to avoid double check
-add tracepoint to refcount_dec_if_one
-kunit: make significant improvements to design, fix critical bug, add test case for
 refcount_dec_if_one()
-Link to v1: https://lore.kernel.org/r/[email protected]
 
---

Eugene Mavick (5):
tracing: add ref_trace_final_put tracepoint
refcount: add ref_trace_final_put tracepoint
percpu-refcount: add ref_trace_final_put trace
kunit: add test for ref_trace_final_put
MAINTAINERS: add entries for ref_trace_final_put

 MAINTAINERS                      |   3 +
 include/linux/percpu-refcount.h  |   5 +-
 include/linux/ref_trace.h        |  40 +++++++++++
 include/linux/refcount.h         |   2 +
 include/trace/events/ref_trace.h |  51 ++++++++++++++
 lib/Kconfig                      |  10 +++
 lib/Makefile                     |   2 +
 lib/ref_trace.c                  |  13 ++++
 lib/refcount.c                   |   6 +-
 lib/tests/Makefile               |   1 +
 lib/tests/ref_trace_kunit.c      | 141 +++++++++++++++++++++++++++++++++++++++
 11 files changed, 272 insertions(+), 2 deletions(-)

---
base-commit: df685633c3dbc67441cc86f1c3fee58de4652ba2
change-id: 20260624-refcount-final-put-trace-49bd7c39bd5a

Best regards,

Signature · 1 line
Eugene Mavick <[email protected]>
Signature · 1 line
Masami Hiramatsu (Google) <[email protected]>
#7 lore raw
In reply to Peter Zijlstra
Quoted 2 lines · Peter Zijlstra

I'm not sure this Changelog is convincing me. To me this just looks like more weight / bloat without benefit.

The overhead is negligible; like other TRACE_EVENTS this is an if statement when disabled.

Knowledge of when an object is considered end-of-usable-life by the refcount implementation is one of the first steps in UAF debugging. Alternatives to obtain this information (GDB, watchpoints, manual instrumentation) require live analysis.

When debugging from static information of remote origin, like syzbot, this may not be practical. Live analysis requires bug reproduction and manual effort.

Bug reproduction, especially in rare timing-sensitive UAFs that thus require many iterations, may require significant time/hardware resources.

This tracepoint eliminates significant effort/time cost, and removes a hardware barrier for a common debugging scenario at a negligible performance cost.