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
                   ` (2 more replies)
  0 siblings, 3 replies; 7+ 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] 7+ 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
  2026-12-03 13:14 ` Jaewon Jung
  2 siblings, 0 replies; 7+ 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] 7+ 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
  2026-12-03 13:33   ` Steven Rostedt
  2026-12-03 13:14 ` Jaewon Jung
  2 siblings, 2 replies; 7+ 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] 7+ 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
  2026-12-03 13:33   ` Steven Rostedt
  1 sibling, 0 replies; 7+ 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] 7+ 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 13:14 ` Jaewon Jung
  2026-12-03 13:30   ` sashiko-bot
  2 siblings, 1 reply; 7+ 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 related	[flat|nested] 7+ messages in thread

* Re: [PATCH] tracing: Make event_enable_count_probe() count atomic
  2026-12-03 13:14 ` Jaewon Jung
@ 2026-12-03 13:30   ` sashiko-bot
  0 siblings, 0 replies; 7+ messages in thread
From: sashiko-bot @ 2026-12-03 13:30 UTC (permalink / raw)
  To: Jaewon Jung; +Cc: linux-trace-kernel

Thank you for your contribution! Sashiko AI review found 4 potential
issues to consider:
- [Medium] tracing: TOCTOU race in event_enable_count_probe() exceeds
  configured count
- [Medium] tracing: dropped test_bit() fast path bounces flags in
  update_event_probe()
- [Medium] tracing: signedness mismatch on count in event_enable_func()
  and probe
- [Low] tracing: redundant smp_rmb() before atomic bitop in
  event_enable_count_probe()
--

commit 47ef71f07e0f5d4db4d3227448bffe6c9bf7018f
Author: Jaewon Jung <jaewon8868@gmail.com>

tracing: Make event_enable_count_probe() count atomic

This commit converts struct event_probe_data's count field to atomic_long_t
and updates event_enable_count_probe() to claim the event state transition
with test_and_clear_bit()/test_and_set_bit() before decrementing count in a
cmpxchg loop.

> diff --git a/kernel/trace/trace_events.c b/kernel/trace/trace_events.c
> index 0d4195b43208e..bb1309c9fd00e 100644
> --- a/kernel/trace/trace_events.c
> +++ b/kernel/trace/trace_events.c
> @@ -4402,17 +4402,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);
>  }

[Severity: Medium]
Does removing the read-only test_bit() check cause unnecessary cacheline
bouncing on file->flags?

Previously, event_enable_count_probe() returned early via test_bit() when
the event was already in the target state.  Calling update_event_probe()
directly now runs an atomic bitop on every probed function call while
count != 0, dirtying the trace_event_file cacheline that
trace_trigger_soft_disabled() reads on every event hit across all CPUs.

event_enable_probe() also calls update_event_probe() and ignores the return
value, so on weakly ordered architectures it now pays for a fully ordered
test_and_{clear,set}_bit() instead of an unordered clear_bit()/set_bit().
Keeping a read-only test_bit() pre-check in event_enable_count_probe() and
unordered clear_bit()/set_bit() for event_enable_probe() would avoid both
regressions.

[ ... ]

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

[Severity: Low]
Is this smp_rmb() needed?

update_event_probe() immediately performs test_and_clear_bit() or
test_and_set_bit(), which are already fully ordered.  This smp_rmb() also
has no paired smp_wmb(), so on weakly ordered architectures like arm64 it
emits an extra read barrier on every non-zero-count probe call.

>  
> +	/* 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;
> +		}
> +	}
>  }

[Severity: Medium]
Can a finite-count probe still exceed its configured count here?

event_enable_count_probe() flips EVENT_FILE_FL_SOFT_DISABLED_BIT via
update_event_probe() before decrementing edata->count.  If an opposite
toggle (such as a paired disable_event probe) runs in between, two CPUs can
both observe count == 1 and perform a state transition:

CPU0                                 CPU1
count = atomic_long_read() /* 1 */
update_event_probe() /* clears bit */
                                     /* disable_event sets bit back */
                                     count = atomic_long_read() /* 1 */
                                     update_event_probe()  <-- 2nd flip
                                     atomic_long_cmpxchg(1 -> 0)
atomic_long_cmpxchg(1 -> 0)
  /* sees old == 0, breaks */

The same race occurs if CPU0 pauses between atomic_long_read() and
update_event_probe() while CPU1 finishes the transition and decrements
count to 0.  Because CPU0 breaks out on old <= 0 without reverting the bit
flip, the target event is left enabled or disabled past its configured
limit.

[ ... ]

> @@ -4655,7 +4670,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;

[Severity: Medium]
Can event_enable_func() store a negative count in data->count?

event_enable_func() still parses count into an unsigned long with
kstrtoul() without rejecting values above LONG_MAX, so inputs in
(LONG_MAX, ULONG_MAX - 1] wrap to negative signed values (< -1) in
atomic_long_t.  In event_enable_count_probe(), those values pass both
if (!count) and if (count != -1).

For LONG_MIN, count - 1 causes signed integer underflow inside a notrace
callback, and for any count < -1, a losing CPU in the cmpxchg loop sees
old <= 0 and breaks out without retrying, silently dropping the decrement.
Rejecting values above LONG_MAX (or parsing with kstrtol()) in
event_enable_func() avoids the mismatch and would also allow using
atomic_long_dec_if_positive().

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

^ permalink raw reply	[flat|nested] 7+ 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
@ 2026-12-03 13:33   ` Steven Rostedt
  1 sibling, 0 replies; 7+ 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] 7+ messages in thread

end of thread, other threads:[~2026-12-03 13:33 UTC | newest]

Thread overview: 7+ 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
2026-12-03 13:33   ` Steven Rostedt
2026-12-03 13:14 ` Jaewon Jung
2026-12-03 13:30   ` sashiko-bot

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