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 03:02:34 +0000	[thread overview]
Message-ID: <sashiko-outbox-247035@kernel.org> (raw)
In-Reply-To: <20261203025249.2117813-1-jaewon8868@gmail.com>

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

      reply	other threads:[~2026-12-03  3:02 UTC|newest]

Thread overview: 2+ 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 [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-247035@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