[PATCH] tracing: Make event_enable_count_probe() count atomic

From: Jaewon Jung

Date: Wed Dec 02 2026 - 21:53:00 EST


Concurrent callbacks race on the plain count read and decrement. This can
lose updates or turn 0 into -1 (unlimited). Use atomic64_t and cmpxchg
for the decrement.

Fixes: 3cd715de2611 ("tracing: Add function probe triggers to enable/disable events")
Signed-off-by: Jaewon Jung <jaewon8868@xxxxxxxxx>
---
kernel/trace/trace_events.c | 24 +++++++++++++++++-------
1 file changed, 17 insertions(+), 7 deletions(-)

diff --git a/kernel/trace/trace_events.c b/kernel/trace/trace_events.c
index 4722d9a8b222..a9528c4bdd8f 100644
--- a/kernel/trace/trace_events.c
+++ b/kernel/trace/trace_events.c
@@ -4399,7 +4399,7 @@ EXPORT_SYMBOL_GPL(trace_put_event_file);
#ifdef CONFIG_DYNAMIC_FTRACE
struct event_probe_data {
struct trace_event_file *file;
- unsigned long count;
+ atomic64_t count;
int ref;
bool enable;
};
@@ -4444,7 +4444,7 @@ event_enable_count_probe(unsigned long ip, unsigned long parent_ip,

edata = *pdata;

- if (!edata->count)
+ if (!atomic64_read(&edata->count))
return;

/* Skip if the event is in a state we want to switch to */
@@ -4452,8 +4452,16 @@ event_enable_count_probe(unsigned long ip, unsigned long parent_ip,
&edata->file->flags))
return;

- if (edata->count != -1)
- (edata->count)--;
+ for (;;) {
+ s64 count = atomic64_read(&edata->count);
+
+ if (!count)
+ return;
+ if (count == -1)
+ break;
+ if (atomic64_cmpxchg(&edata->count, count, count - 1) == count)
+ break;
+ }

update_event_probe(edata);
}
@@ -4480,10 +4488,12 @@ event_enable_print(struct seq_file *m, unsigned long ip,
edata->file->event_call->class->system,
trace_event_name(edata->file->event_call));

- if (edata->count == -1)
+ s64 count = atomic64_read(&edata->count);
+
+ if (count == -1)
seq_puts(m, ":unlimited\n");
else
- seq_printf(m, ":count=%ld\n", edata->count);
+ seq_printf(m, ":count=%lld\n", count);

return 0;
}
@@ -4652,7 +4662,7 @@ event_enable_func(struct trace_array *tr, struct ftrace_hash *hash,
goto out_put;

data->enable = enable;
- data->count = count;
+ atomic64_set(&data->count, count == -1UL ? -1 : (s64)count);
data->file = file;

ret = register_ftrace_function_probe(glob, tr, ops, data);
--
2.43.0