lkml.org 
[lkml]   [2012]   [Aug]   [23]   [last100]   RSS Feed
Views: [wrap][no wrap]   [headers]  [forward] 
 
Messages in this thread
Patch in this message
/
Date
From
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;


\
 
 \ /
  Last update: 2012-08-24 00:01    [W:0.068 / U:0.404 seconds]
©2003-2020 Jasper Spaans|hosted at Digital Ocean and TransIP|Read the blog|Advertise on this site