* [PATCH] tracing: Make event_enable_count_probe() count atomic
@ 2026-12-03 2:51 Jaewon Jung
2026-12-03 3:02 ` sashiko-bot
0 siblings, 1 reply; 2+ 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 related [flat|nested] 2+ messages in thread* Re: [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 3:02 ` sashiko-bot
0 siblings, 0 replies; 2+ messages in thread
From: sashiko-bot @ 2026-12-03 3:02 UTC (permalink / raw)
To: Jaewon Jung; +Cc: linux-trace-kernel
Thank you for your contribution! Sashiko AI review found 2 potential
issues to consider:
- [Medium] tracing: multiple count decrements per transition in
event_enable_count_probe()
- [Medium] tracing: non-NMI-safe atomic64_t used in
event_enable_count_probe()
--
commit a29411e32443080968e8c32bbea059c5a37f2e52
Author: Jaewon Jung <jaewon8868@gmail.com>
tracing: Make event_enable_count_probe() count atomic
This commit converts event_probe_data::count from unsigned long to
atomic64_t and updates event_enable_count_probe() to decrement it with an
atomic64_cmpxchg() loop.
> diff --git a/kernel/trace/trace_events.c b/kernel/trace/trace_events.c
> index 0d4195b43208e..f17ed3b160c80 100644
> --- a/kernel/trace/trace_events.c
> +++ b/kernel/trace/trace_events.c
> @@ -4402,7 +4402,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;
> };
[Severity: Medium]
Can using atomic64_t for event_probe_data::count deadlock in NMI context
on 32-bit architectures with CONFIG_GENERIC_ATOMIC64?
On those platforms, generic_atomic64_read() and
generic_atomic64_cmpxchg() take a hashed spinlock in atomic64_lock using
local_irq_save(), which does not mask NMIs:
current task (32-bit CONFIG_GENERIC_ATOMIC64)
generic_atomic64_*()
local_irq_save(flags)
arch_spin_lock(lock)
<NMI>
function_trace_probe_call()
event_enable_count_probe()
atomic64_read(&edata->count)
arch_spin_lock(lock) <-- self-deadlock on same lock bucket
Using a word-sized atomic_long_t (or long with READ_ONCE() and WRITE_ONCE()
matching update_traceon_count() in kernel/trace/trace_functions.c) is
lockless and NMI-safe on both 32-bit and 64-bit architectures, and also
avoids the mid-block s64 declaration in event_enable_print() and the -1UL
conversion in event_enable_func().
> @@ -4447,7 +4447,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 */
> @@ -4455,8 +4455,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);
> }
[Severity: Medium]
Does this cmpxchg retry loop still decrement edata->count multiple times
for a single enable/disable state transition when multiple CPUs hit the
probed function concurrently?
As described in 3cd715de2611 ("tracing: Add function probe triggers to
enable/disable events"), count tracks how many times the probe switches
the event state, not calls while the event is already in that state.
Because test_bit(EVENT_FILE_FL_SOFT_DISABLED_BIT, &edata->file->flags) is
checked before the loop and update_event_probe() only updates the bit
after the loop, every CPU that passes test_bit() before the bit is updated
retries atomic64_cmpxchg() until it also decrements edata->count:
CPU0 CPU1
event_enable_count_probe() event_enable_count_probe()
test_bit(..., &edata->file->flags) test_bit(..., &edata->file->flags)
atomic64_cmpxchg(&edata->count,
2, 1) == 2
atomic64_cmpxchg(&edata->count,
2, 1) != 2 (retries)
atomic64_cmpxchg(&edata->count,
1, 0) == 1
<-- 2nd decrement for same switch
update_event_probe(edata) update_event_probe(edata)
Notice how the sibling probe counter in update_traceon_count() avoids
this by reading old_count once before checking the state, updating the
state, and storing old_count - 1 (or using a single non-retrying cmpxchg):
kernel/trace/trace_functions.c:update_traceon_count() {
...
old_count = *count;
if (old_count <= 0)
return;
/* Make sure we see count before checking tracing state */
smp_rmb();
if (on == !!tracer_tracing_is_on(tr))
return;
if (on)
tracer_tracing_on(tr);
else
tracer_tracing_off(tr);
/* Make sure tracing state is visible before updating count */
smp_wmb();
*count = old_count - 1;
}
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261203025249.2117813-1-jaewon8868@gmail.com?part=1
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2026-12-03 3:02 UTC | newest]
Thread overview: 2+ 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 3:02 ` sashiko-bot
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.