Re: [PATCH v3 2/5] refcount: add ref_trace_final_put tracepoint
From: Steven Rostedt <rostedt@goodmis.org>
Date: 2026-07-27 20:27:25
Also in:
linux-mm, lkml
On Sat, 11 Jul 2026 12:40:20 +0800 Eugene Mavick [off-list ref] wrote:
quoted hunk ↗ jump to hunk
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 <redacted> --- 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 <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; }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 <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,12 @@ 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); + + if (ret) + do_trace_ref_final_put(r); + + return ret;
Tracing should avoid adding any open conditionals. This is in the fast
path. No need to add an if statement that is only used when tracing is
enabled.
You can create another macro:
do_trace_ref_final_put_cond(ret, r);
And have that defined as:
#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_, __func__, obj);\
} \
} while (0)
That way the conditional is only checked if tracing is enabled.
-- Steve
} EXPORT_SYMBOL(refcount_dec_if_one);