[RFC][PATCH 1/3] cpuidle: fix underflow in stddev calculation

From: Rik van Riel
Date: Thu Aug 23 2012 - 17:13:53 EST


The calculation to determine the standard deviation used unsigned
integers. Since some of the values are guaranteed to be below the
average, this would always lead to large unsigned 32 bit numbers,
which would then be multiplied and added to a 64 bit integer,
potentially leading to a totally unpredictable result.

I am not sure if/why this code has ever worked.

Signed-off-by: Rik van Riel <riel@xxxxxxxxxx>
---
drivers/cpuidle/governors/menu.c | 7 ++++---
1 files changed, 4 insertions(+), 3 deletions(-)

diff --git a/drivers/cpuidle/governors/menu.c b/drivers/cpuidle/governors/menu.c
index 5b1f2c3..f4fe5c3 100644
--- a/drivers/cpuidle/governors/menu.c
+++ b/drivers/cpuidle/governors/menu.c
@@ -212,9 +212,10 @@ static void detect_repeating_patterns(struct menu_device *data)
if (avg > data->expected_us)
return;

- for (i = 0; i < INTERVALS; i++)
- stddev += (data->intervals[i] - avg) *
- (data->intervals[i] - avg);
+ for (i = 0; i < INTERVALS; i++) {
+ int diff = (int)data->intervals[i] - avg;
+ stddev += diff * diff;
+ }

stddev = stddev / INTERVALS;

--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/