Messages in this thread Patch in this message | | | Date | Thu, 23 Aug 2012 17:11:55 -0400 | From | Rik van Riel <> | Subject | [RFC][PATCH 1/3] cpuidle: fix underflow in stddev calculation |
| |
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@redhat.com> --- 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;
| |