Unverified Commit 74c40b7b authored by openeuler-ci-bot's avatar openeuler-ci-bot Committed by Gitee
Browse files

!15740 ftrace: Avoid potential division by zero in function_stat_show()

parents ee997750 e0a4f46c
Loading
Loading
Loading
Loading
+12 −15
Original line number Diff line number Diff line
@@ -544,6 +544,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
	mutex_lock(&ftrace_profile_lock);

@@ -565,23 +566,19 @@ static int function_stat_show(struct seq_file *m, void *v)
#ifdef CONFIG_FUNCTION_GRAPH_TRACER
	seq_puts(m, "    ");

	/* Sample standard deviation (s^2) */
	if (rec->counter <= 1)
		stddev = 0;
	else {
	/*
		 * Apply Welford's method:
	 * Variance formula:
	 * s^2 = 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 = 0;
	stddev_denom = rec->counter * (rec->counter - 1) * 1000;
	if (stddev_denom) {
		stddev = 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 = div64_ul(stddev,
				  rec->counter * (rec->counter - 1) * 1000);
		stddev = div64_ul(stddev, stddev_denom);
	}

	trace_seq_init(&s);