Linux Trace Kernel
 help / color / mirror / Atom feed
* [PATCH] tracing: Make event_enable_count_probe() count atomic
@ 2026-12-03  2:51 Jaewon Jung
  2026-12-03  3:02 ` sashiko-bot
  2026-12-03  4:58 ` 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 related	[flat|nested] 4+ 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
  2026-12-03  4:58 ` Jaewon Jung
  1 sibling, 0 replies; 4+ 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] 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  3:02 ` sashiko-bot
@ 2026-12-03  4:58 ` Jaewon Jung
  2026-12-03  5:16   ` sashiko-bot
  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 related	[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  5:16   ` sashiko-bot
  0 siblings, 0 replies; 4+ messages in thread
From: sashiko-bot @ 2026-12-03  5:16 UTC (permalink / raw)
  To: Jaewon Jung; +Cc: linux-trace-kernel

Thank you for your contribution! Sashiko AI review found 3 potential
issues to consider:
- [Medium] tracing: unchecked cmpxchg loses decrements in
  event_enable_count_probe()
- [Low] tracing: redundant smp_wmb() before atomic_long_cmpxchg() in
  probe callback
- [Low] tracing: commit message omits barrier and reordering rationale
--

commit 41e0885247841d0dfe5e2b4d512d00bff70bfd6e
Author: Jaewon Jung <jaewon8868@gmail.com>

> tracing: Make event_enable_count_probe() count atomic
>
> 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>

[Severity: Low]
Could the commit message also explain why update_event_probe() is moved
ahead of the count decrement, why the memory barriers are added, and why
atomic_long_cmpxchg() ignores failure?  Right now it only mentions making
count atomic and using cmpxchg, leaving the ordering changes and their
rationale undocumented.

> diff --git a/kernel/trace/trace_events.c b/kernel/trace/trace_events.c
> index 0d4195b43208e..72db3921665b1 100644
> --- a/kernel/trace/trace_events.c
> +++ b/kernel/trace/trace_events.c

[ ... ]

> @@ -4447,18 +4447,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();

[Severity: Low]
Is this smp_wmb() redundant before atomic_long_cmpxchg()?

Value-returning atomic RMW operations already imply full memory ordering
on success, and a failed atomic_long_cmpxchg() performs no write before
returning.  On weakly ordered architectures such as arm64, this emits
back-to-back barriers (dmb ishst before casal).  Dropping smp_wmb(), or
pairing atomic_long_read_acquire() with atomic_long_cmpxchg_release(),
avoids the duplicate barrier.

> +
> +		atomic_long_cmpxchg(&edata->count, count, count - 1);
> +	}

[Severity: Medium]
Can discarding the return value of atomic_long_cmpxchg() lose count
decrements or let the probe fire after count reaches zero?

Suppose edata->enable is true, edata->count is 2, and a paired
disable_event probe or tracefs write toggles the event back off between
two concurrent callbacks:

CPU0                     CPU1                   CPU2
event_enable_count_probe()
  read count // 2
  test_bit() // disabled
  update_event_probe()
                         disable event
                                                event_enable_count_probe()
                                                  read count // 2
                                                  test_bit() // disabled
                                                  update_event_probe()
                                                  cmpxchg(2 -> 1) // ok
  cmpxchg(2 -> 1) // fails, ignored  <-- decrement lost

Both CPUs performed an enable transition, but edata->count remains 1
instead of 0, allowing a third enable.  With an initial count of 1, the
same interleaving lets CPU0 call update_event_probe() and re-enable the
event after edata->count has already reached 0.

Since EVENT_FILE_FL_SOFT_DISABLED_BIT is a single bit, using
test_and_clear_bit() / test_and_set_bit() to claim the state transition
and retrying the count decrement on cmpxchg failure would keep the count
accurate.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261203045851.2243395-1-jaewon8868@gmail.com?part=1

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2026-12-03  5:16 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  3:02 ` sashiko-bot
2026-12-03  4:58 ` Jaewon Jung
2026-12-03  5:16   ` sashiko-bot

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox