* [PATCH] tracing: Make event_enable_count_probe() count atomic
@ 2026-12-03 2:51 Jaewon Jung
2026-12-03 4:58 ` Jaewon Jung
2026-12-03 13:14 ` Jaewon Jung
0 siblings, 2 replies; 4+ messages in thread
From: Jaewon Jung @ 2026-12-03 2:51 UTC (permalink / raw)
To: Steven Rostedt, Masami Hiramatsu, Mathieu Desnoyers
Cc: linux-kernel, linux-trace-kernel
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@gmail.com>
---
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
^ permalink raw reply [flat|nested] 4+ messages in thread* [PATCH] tracing: Make event_enable_count_probe() count atomic
2026-12-03 2:51 [PATCH] tracing: Make event_enable_count_probe() count atomic Jaewon Jung
@ 2026-12-03 4:58 ` Jaewon Jung
2026-12-03 13:33 ` Steven Rostedt
2026-12-03 13:14 ` Jaewon Jung
1 sibling, 1 reply; 4+ messages in thread
From: Jaewon Jung @ 2026-12-03 4:58 UTC (permalink / raw)
To: Steven Rostedt, Masami Hiramatsu, Mathieu Desnoyers
Cc: linux-kernel, linux-trace-kernel
Concurrent callbacks race on the plain count read and decrement. This can
lose updates or turn 0 into -1 (unlimited). Use atomic_long_t and cmpxchg
for the decrement.
Fixes: 3cd715de2611 ("tracing: Add function probe triggers to enable/disable events")
Signed-off-by: Jaewon Jung <jaewon8868@gmail.com>
---
kernel/trace/trace_events.c | 27 +++++++++++++++++++--------
1 file changed, 19 insertions(+), 8 deletions(-)
diff --git a/kernel/trace/trace_events.c b/kernel/trace/trace_events.c
index 4722d9a8b222..1cc40213e2a9 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;
+ atomic_long_t count;
int ref;
bool enable;
};
@@ -4444,18 +4444,27 @@ event_enable_count_probe(unsigned long ip, unsigned long parent_ip,
edata = *pdata;
- if (!edata->count)
+ long count = atomic_long_read(&edata->count);
+
+ if (!count)
return;
+ /* Make sure we see count before checking event state */
+ smp_rmb();
+
/* Skip if the event is in a state we want to switch to */
if (edata->enable == !test_bit(EVENT_FILE_FL_SOFT_DISABLED_BIT,
&edata->file->flags))
return;
- if (edata->count != -1)
- (edata->count)--;
-
update_event_probe(edata);
+
+ if (count != -1) {
+ /* Make sure event state is visible before updating count */
+ smp_wmb();
+
+ atomic_long_cmpxchg(&edata->count, count, count - 1);
+ }
}
static int
@@ -4480,10 +4489,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)
+ long count = atomic_long_read(&edata->count);
+
+ if (count == -1)
seq_puts(m, ":unlimited\n");
else
- seq_printf(m, ":count=%ld\n", edata->count);
+ seq_printf(m, ":count=%ld\n", count);
return 0;
}
@@ -4652,7 +4663,7 @@ event_enable_func(struct trace_array *tr, struct ftrace_hash *hash,
goto out_put;
data->enable = enable;
- data->count = count;
+ atomic_long_set(&data->count, count);
data->file = file;
ret = register_ftrace_function_probe(glob, tr, ops, data);
--
2.43.0
^ permalink raw reply [flat|nested] 4+ messages in thread* Re: [PATCH] tracing: Make event_enable_count_probe() count atomic
2026-12-03 4:58 ` Jaewon Jung
@ 2026-12-03 13:33 ` Steven Rostedt
0 siblings, 0 replies; 4+ messages in thread
From: Steven Rostedt @ 2026-12-03 13:33 UTC (permalink / raw)
To: Jaewon Jung
Cc: Masami Hiramatsu, Mathieu Desnoyers, linux-kernel, linux-trace-kernel
On Thu, 3 Dec 2026 13:58:44 +0900
Jaewon Jung <jaewon8868@gmail.com> wrote:
> Concurrent callbacks race on the plain count read and decrement. This can
> lose updates or turn 0 into -1 (unlimited). Use atomic_long_t and cmpxchg
> for the decrement.
I'm curious, do you actually use this feature. It's been something I've
been wanting to deprecate.
-- Steve
^ permalink raw reply [flat|nested] 4+ messages in thread
* [PATCH] tracing: Make event_enable_count_probe() count atomic
2026-12-03 2:51 [PATCH] tracing: Make event_enable_count_probe() count atomic Jaewon Jung
2026-12-03 4:58 ` Jaewon Jung
@ 2026-12-03 13:14 ` Jaewon Jung
1 sibling, 0 replies; 4+ messages in thread
From: Jaewon Jung @ 2026-12-03 13:14 UTC (permalink / raw)
To: Steven Rostedt, Masami Hiramatsu, Mathieu Desnoyers
Cc: linux-kernel, linux-trace-kernel
event_enable_count_probe() can run concurrently on multiple CPUs. Its
plain count read and decrement race with each other, which can lose
updates or turn 0 into -1 (unlimited). KCSAN reports both the read and
the decrement in this callback.
Use atomic_long_t and claim the state transition with test_and_clear_bit()/
test_and_set_bit(). Only the CPU that actually flips the bit proceeds to
decrement count; others skip. Retry cmpxchg on contention to avoid lost
decrements.
Fixes: 3cd715de2611 ("tracing: Add function probe triggers to enable/disable events")
Signed-off-by: Jaewon Jung <jaewon8868@gmail.com>
---
kernel/trace/trace_events.c | 43 +++++++++++++++++++++++++------------
1 file changed, 29 insertions(+), 14 deletions(-)
diff --git a/kernel/trace/trace_events.c b/kernel/trace/trace_events.c
index 4722d9a8b222..4e5f715364a2 100644
--- a/kernel/trace/trace_events.c
+++ b/kernel/trace/trace_events.c
@@ -4399,17 +4399,19 @@ EXPORT_SYMBOL_GPL(trace_put_event_file);
#ifdef CONFIG_DYNAMIC_FTRACE
struct event_probe_data {
struct trace_event_file *file;
- unsigned long count;
+ atomic_long_t count;
int ref;
bool enable;
};
-static void update_event_probe(struct event_probe_data *data)
+static bool update_event_probe(struct event_probe_data *data)
{
if (data->enable)
- clear_bit(EVENT_FILE_FL_SOFT_DISABLED_BIT, &data->file->flags);
+ return test_and_clear_bit(EVENT_FILE_FL_SOFT_DISABLED_BIT,
+ &data->file->flags);
else
- set_bit(EVENT_FILE_FL_SOFT_DISABLED_BIT, &data->file->flags);
+ return !test_and_set_bit(EVENT_FILE_FL_SOFT_DISABLED_BIT,
+ &data->file->flags);
}
static void notrace
@@ -4437,6 +4439,7 @@ event_enable_count_probe(unsigned long ip, unsigned long parent_ip,
struct ftrace_func_mapper *mapper = data;
struct event_probe_data *edata;
void **pdata;
+ long count;
pdata = ftrace_func_mapper_find_ip(mapper, ip);
if (!pdata || !*pdata)
@@ -4444,18 +4447,28 @@ event_enable_count_probe(unsigned long ip, unsigned long parent_ip,
edata = *pdata;
- if (!edata->count)
+ count = atomic_long_read(&edata->count);
+ if (!count)
return;
- /* Skip if the event is in a state we want to switch to */
- if (edata->enable == !test_bit(EVENT_FILE_FL_SOFT_DISABLED_BIT,
- &edata->file->flags))
+ /* Make sure we see count before claiming the state transition */
+ smp_rmb();
+
+ /* Atomically claim the state transition; skip if already there */
+ if (!update_event_probe(edata))
return;
- if (edata->count != -1)
- (edata->count)--;
+ if (count != -1) {
+ /* Retry on contention so a failed cmpxchg does not lose a decrement */
+ for (;;) {
+ long old = atomic_long_cmpxchg(&edata->count, count,
+ count - 1);
- update_event_probe(edata);
+ if (old == count || old <= 0)
+ break;
+ count = old;
+ }
+ }
}
static int
@@ -4480,10 +4493,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)
+ long count = atomic_long_read(&edata->count);
+
+ if (count == -1)
seq_puts(m, ":unlimited\n");
else
- seq_printf(m, ":count=%ld\n", edata->count);
+ seq_printf(m, ":count=%ld\n", count);
return 0;
}
@@ -4652,7 +4667,7 @@ event_enable_func(struct trace_array *tr, struct ftrace_hash *hash,
goto out_put;
data->enable = enable;
- data->count = count;
+ atomic_long_set(&data->count, count);
data->file = file;
ret = register_ftrace_function_probe(glob, tr, ops, data);
--
2.43.0
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2026-12-03 13:33 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-12-03 2:51 [PATCH] tracing: Make event_enable_count_probe() count atomic Jaewon Jung
2026-12-03 4:58 ` Jaewon Jung
2026-12-03 13:33 ` Steven Rostedt
2026-12-03 13:14 ` Jaewon Jung
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®