Linux Trace Kernel
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Jaewon Jung" <jaewon8868@gmail.com>
Cc: linux-trace-kernel@vger.kernel.org
Subject: Re: [PATCH] tracing: Make event_enable_count_probe() count atomic
Date: Thu, 03 Dec 2026 13:30:50 +0000	[thread overview]
Message-ID: <sashiko-outbox-247860@kernel.org> (raw)
In-Reply-To: <20261203131451.2470625-1-jaewon8868@gmail.com>

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

      reply	other threads:[~2026-12-03 13:30 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
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 message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=sashiko-outbox-247860@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=jaewon8868@gmail.com \
    --cc=linux-trace-kernel@vger.kernel.org \
    --cc=sashiko-reviews@lists.linux.dev \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox