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>
Subject: Re: [PATCH] ftrace: Avoid potential division by zero in function_stat_show()
Date: Tue, 4 Feb 2025 19:48:20 -0500 [thread overview]
Message-ID: <20250204194820.7ef50d96@gandalf.local.home> (raw)
In-Reply-To: <20250204172045.3a5d8d01@gandalf.local.home>
On Tue, 4 Feb 2025 17:20:45 -0500
Steven Rostedt <rostedt@goodmis.org> wrote:
> x = rec->counter
>
> x * (x - 1) * 1000 = (2^32 - 1) // use minus 1 just to be sure
> x * (x - 1) = (2^32 - 1) / 1000
> x^2 - x = (2^32 - 1) / 1000
> x^2 - x - (2^32 - 1) / 1000 = 0
>
> x = (-b +/- sqrt(b^2 - 4ac)) / 2a
>
> a = 1
> b = -1
> c = -(2^32 - 1) / 1000 = -4294967.295
>
> x = (-1 +/- sqrt((-1)^2 - 4 * -4294967.295)) / 2
And I did make a mistake, as b = -1, and the above should start with -(-1)
or 1 :-p
>
> x = 2071.930 for 32bit
This should be: 2072.930
>
> For 64bit we have
>
> c = -(2^64 - 1) / 1000 = -18446744073709551.615
>
> That makes
>
> x = 135818790.812
And this needs to be: 135818791.812
> +/*
> + * The calculation for stddev will overflow when the counter
> + * algorithm overflows the long size:
> + *
> + * rec->counter * (rec->counter - 1) * 1000 >= 2^BITS_PER_LONG
> + *
> + * Using the quadratic equation: x = (-b +/- sqrt(b^2 - 4ac)) / 2a
> + * we can figure out what the max rec->counter is before it
> + * overflows.
> + *
> + * x = rec->counter
> + * x * (x - 1) * 1000 = 2^BITS_PER_LONG - 1 // -1 for rounding
> + * x * (x - 1) = (2^BITS_PER_LONG - 1) / 1000
> + * x^2 - x = (2^BITS_PER_LONG - 1) / 1000
> + * x^2 - x - (2^BITS_PER_LONG - 1) / 1000 = 0
> + *
> + * a = 1
> + * b = -1
> + * c = -(2^BITS_PER_LONG - 1) / 1000
> + *
> + * x = (1 +/- sqrt(1 - 4 * -(2^BITS_PER_LONG - 1) / 1000)) / 2
> + *
> + * For 32bit that is: x = 2072.930 (or 2072)
> + * For 64bit that is: x = 135818791.812 (or 135818791)
> + */
But in the patch, I redid the numbers and did not copy from the above, and
here I did it correctly.
-- Steve
next prev parent reply other threads:[~2025-02-05 0:47 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
2025-02-04 7:43 ` Nikolay Kuratov
2025-02-04 22:20 ` Steven Rostedt
2025-02-05 0:48 ` Steven Rostedt [this message]
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=20250204194820.7ef50d96@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=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