From nobody Sun Feb 8 21:33:39 2026 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 5A38225DD1D; Fri, 28 Feb 2025 15:29:33 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1740756573; cv=none; b=WS8nhvAjY6iun8fvJMAIz1EmTZEODKRmtENyJJB7tBiQm1YCM0/HXyY4vUGjefSAmltuEc+Tu/VY1oSw+0x+M4b8xuETaFuWVDxqlUqpQT+/9lkYlgccVByPjqeTgdN4KNrMbABVgGAmj7ovaGiL9RUGcTIbzVdcCGJsVRP8/mM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1740756573; c=relaxed/simple; bh=S8IXHnGBnwalsNxpu40Re63/s2nhkYsvGKsG7Oe2IBQ=; h=Message-ID:Date:From:To:Cc:Subject:References:MIME-Version: Content-Type; b=pj+1eCl0mRCaM0ai+j7ttIkmPt0r++vVpWOxEJSZs3pKBeitLBsL0lQDuNdupLkJ2QAuIipdMHNoT4rd4FHzo2EBqY8/dqBcgrGx5fHRow1Bn9hk2hEcRdIzFqg/2+a959tghwugzIJKRPFTWMWwOrufR6QVyaK/dBHCPH2p3d4= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 Received: by smtp.kernel.org (Postfix) with ESMTPSA id F27F5C4CEEC; Fri, 28 Feb 2025 15:29:32 +0000 (UTC) Received: from rostedt by gandalf with local (Exim 4.98) (envelope-from ) id 1to2Je-0000000ABiB-38qe; Fri, 28 Feb 2025 10:30:18 -0500 Message-ID: <20250228153018.601663962@goodmis.org> User-Agent: quilt/0.68 Date: Fri, 28 Feb 2025 10:30:06 -0500 From: Steven Rostedt To: linux-kernel@vger.kernel.org Cc: Masami Hiramatsu , Mark Rutland , Mathieu Desnoyers , Andrew Morton , stable@vger.kernel.org, Wen Yang , Nikolay Kuratov Subject: [for-linus][PATCH 3/3] ftrace: Avoid potential division by zero in function_stat_show() References: <20250228153003.725613767@goodmis.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Nikolay Kuratov Check whether denominator expression x * (x - 1) * 1000 mod {2^32, 2^64} produce zero and skip stddev computation in that case. For now don't care about rec->counter * rec->counter overflow because rec->time * rec->time overflow will likely happen earlier. Cc: stable@vger.kernel.org Cc: Wen Yang Cc: Mark Rutland Cc: Mathieu Desnoyers Link: https://lore.kernel.org/20250206090156.1561783-1-kniv@yandex-team.ru Fixes: e31f7939c1c27 ("ftrace: Avoid potential division by zero in function= profiler") Signed-off-by: Nikolay Kuratov Signed-off-by: Steven Rostedt (Google) --- kernel/trace/ftrace.c | 27 ++++++++++++--------------- 1 file changed, 12 insertions(+), 15 deletions(-) diff --git a/kernel/trace/ftrace.c b/kernel/trace/ftrace.c index 6b0c25761ccb..fc88e0688daf 100644 --- a/kernel/trace/ftrace.c +++ b/kernel/trace/ftrace.c @@ -540,6 +540,7 @@ static int function_stat_show(struct seq_file *m, void = *v) static struct trace_seq s; unsigned long long avg; unsigned long long stddev; + unsigned long long stddev_denom; #endif guard(mutex)(&ftrace_profile_lock); =20 @@ -559,23 +560,19 @@ static int function_stat_show(struct seq_file *m, voi= d *v) #ifdef CONFIG_FUNCTION_GRAPH_TRACER seq_puts(m, " "); =20 - /* Sample standard deviation (s^2) */ - if (rec->counter <=3D 1) - stddev =3D 0; - else { - /* - * Apply Welford's method: - * s^2 =3D 1 / (n * (n-1)) * (n * \Sum (x_i)^2 - (\Sum x_i)^2) - */ + /* + * Variance formula: + * s^2 =3D 1 / (n * (n-1)) * (n * \Sum (x_i)^2 - (\Sum x_i)^2) + * Maybe Welford's method is better here? + * Divide only by 1000 for ns^2 -> us^2 conversion. + * trace_print_graph_duration will divide by 1000 again. + */ + stddev =3D 0; + stddev_denom =3D rec->counter * (rec->counter - 1) * 1000; + if (stddev_denom) { stddev =3D rec->counter * rec->time_squared - rec->time * rec->time; - - /* - * Divide only 1000 for ns^2 -> us^2 conversion. - * trace_print_graph_duration will divide 1000 again. - */ - stddev =3D div64_ul(stddev, - rec->counter * (rec->counter - 1) * 1000); + stddev =3D div64_ul(stddev, stddev_denom); } =20 trace_seq_init(&s); --=20 2.47.2