public inbox for linux-trace-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: Steven Rostedt <rostedt@goodmis.org>
To: Nikolay Kuratov <kniv@yandex-team.ru>
Cc: linux-kernel@vger.kernel.org, linux-trace-kernel@vger.kernel.org,
	Wen Yang <wenyang@linux.alibaba.com>,
	Mark Rutland <mark.rutland@arm.com>,
	Mathieu Desnoyers <mathieu.desnoyers@efficios.com>,
	stable@vger.kernel.org
Subject: Re: [PATCH] ftrace: Avoid potential division by zero in function_stat_show()
Date: Mon, 3 Feb 2025 10:06:03 -0500	[thread overview]
Message-ID: <20250203100603.5c00df0f@gandalf.local.home> (raw)
In-Reply-To: <20250131155346.1313580-1-kniv@yandex-team.ru>

On Fri, 31 Jan 2025 18:53:46 +0300
Nikolay Kuratov <kniv@yandex-team.ru> wrote:

> --- a/kernel/trace/ftrace.c
> +++ b/kernel/trace/ftrace.c
> @@ -570,12 +570,12 @@ static int function_stat_show(struct seq_file *m, void *v)
>  		stddev = rec->counter * rec->time_squared -
>  			 rec->time * rec->time;
>  
> +		stddev = div64_ul(stddev, rec->counter * (rec->counter - 1));
>  		/*
>  		 * Divide only 1000 for ns^2 -> us^2 conversion.
>  		 * trace_print_graph_duration will divide 1000 again.
>  		 */
> -		stddev = div64_ul(stddev,
> -				  rec->counter * (rec->counter - 1) * 1000);
> +		stddev = div64_ul(stddev, 1000);
>  	}
>  

Why make it more complex than it needs to be? We can simply have:

	if (rec->counter <= 1) {
		stddev = 0;
	} else {
		unsigned long counter = rec->counter * (rec->counter - 1) * 1000;

		stddev = rec->counter * rec->time_squared -
			 rec->time * rec->time;

		/* Check for overflow */
		stddev = counter > rec->counter ? div64_ul(stddev, counter) : 0;
	}

And be done with it.

I'll make the change and give you a "Reported-by".

Thanks,

-- Steve


  reply	other threads:[~2025-02-03 15:05 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-01-31 15:53 [PATCH] ftrace: Avoid potential division by zero in function_stat_show() Nikolay Kuratov
2025-02-03 15:06 ` Steven Rostedt [this message]
2025-02-04  7:43   ` Nikolay Kuratov
2025-02-04 22:20     ` Steven Rostedt
2025-02-05  0:48       ` Steven Rostedt
2025-02-05 10:39         ` Nikolay Kuratov
2025-02-05 11:21           ` Nikolay Kuratov
2025-02-05 14:50             ` Steven Rostedt
2025-02-06  8:57               ` Nikolay Kuratov
2025-02-06  9:01               ` [PATCH v2] " Nikolay Kuratov

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=20250203100603.5c00df0f@gandalf.local.home \
    --to=rostedt@goodmis.org \
    --cc=kniv@yandex-team.ru \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-trace-kernel@vger.kernel.org \
    --cc=mark.rutland@arm.com \
    --cc=mathieu.desnoyers@efficios.com \
    --cc=stable@vger.kernel.org \
    --cc=wenyang@linux.alibaba.com \
    /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