Linux Trace Kernel
 help / color / mirror / Atom feed
From: "Zhou, Yun" <yun.zhou@windriver.com>
To: sashiko-reviews@lists.linux.dev
Cc: linux-trace-kernel@vger.kernel.org
Subject: Re: [PATCH] ftrace: add max field to function profiler stats
Date: Thu, 24 Sep 2026 13:27:35 +0800	[thread overview]
Message-ID: <23c3853c-61d7-4499-81e8-44ac608443ad@windriver.com> (raw)
In-Reply-To: <20260924031016.88B601F000FF@smtp.kernel.org>



On 9/24/26 11:10, sashiko-bot@kernel.org wrote:
> Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
> - [Medium] ftrace: time_max TOCTOU data race clobbers maximum latency spikes
> --
> 
> commit 9fb82451733a5ec95aac95499945b5c01e39d82b
> Author: Yun Zhou <yun.zhou@windriver.com>
> 
> ftrace: add max field to function profiler stats
> 
> This commit adds a max field to the function profiler statistics to record the
> maximum single-call duration. This allows users to better investigate
> occasional latency spikes when reviewing function execution times.
> 
>> diff --git a/kernel/trace/ftrace.c b/kernel/trace/ftrace.c
>> index 53d5db60bfa56..dfd25e3c87f71 100644
>> --- a/kernel/trace/ftrace.c
>> +++ b/kernel/trace/ftrace.c
> [ ... ]
>> @@ -920,6 +923,8 @@ static void profile_graph_return(struct ftrace_graph_ret *trace,
>>        if (rec) {
>>                rec->time += calltime;
>>                rec->time_squared += calltime * calltime;
>> +             if (calltime > rec->time_max)
>> +                     rec->time_max = calltime;
> 
> [Severity: Medium]
> Could this non-atomic check and update open a time-of-check to time-of-use
> data race against concurrent nested interrupt context tracing on the same CPU?
> 
> Since profile_graph_return() is executed with local interrupts enabled, an
> interrupt or NMI could fire exactly between the condition check and the
> assignment. If the interrupt handler traces the identical function and
> records a massive latency spike, it would update rec->time_max. When the
> interrupt returns, the normal context resumes and executes the assignment,
> overwriting the true maximum latency spike with its smaller value.
> 
> Because ftrace explicitly permits tracing recursion from different contexts
> like hardirq or NMI, does this require an atomic compare-and-exchange loop
> or similar protection to avoid dropping the genuine latency spikes this
> patch intends to capture?
> 

I think this is intentional and consistent with the profiler's design.
profile_graph_return() runs under preempt_notrace() and the existing
time, time_squared and counter updates are all likewise non-atomic best-
effort accounting; the recursion guard only protects record allocation,
not the stat updates. This is a hot path - every traced function return
passes through it, and keeping the observation overhead minimal is more
important here than never dropping a single sample.

      reply	other threads:[~2026-09-24  5:27 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-09-24  3:01 [PATCH] ftrace: add max field to function profiler stats Yun Zhou
2026-09-24  3:10 ` sashiko-bot
2026-09-24  5:27   ` Zhou, Yun [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=23c3853c-61d7-4499-81e8-44ac608443ad@windriver.com \
    --to=yun.zhou@windriver.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