cpuidle: menu: Avoid overflows when computing variance
authorRafael J. Wysocki <rafael.j.wysocki@intel.com>
Wed, 27 Feb 2019 13:35:50 +0000 (14:35 +0100)
committerRafael J. Wysocki <rafael.j.wysocki@intel.com>
Thu, 7 Mar 2019 09:54:22 +0000 (10:54 +0100)
The variance computation in get_typical_interval() may overflow if
the square of the value of diff exceeds the maximum for the int64_t
data type value which basically is the case when it is of the order
of UINT_MAX.

However, data points so far in the future don't matter for idle
state selection anyway, so change the initial threshold value in
get_typical_interval() to INT_MAX which will cause more "outlying"
data points to be discarded without affecting the selection result.

Reported-by: Randy Dunlap <rdunlap@infradead.org>
Signed-off-by: Rafael J. Wysocki <rafael.j.wysocki@intel.com>
drivers/cpuidle/governors/menu.c

index 61316fc51548a1c72bcf86e1a2ae54b070491b16..5951604e7d5c6fb75152f26746a31a2baf45e6be 100644 (file)
@@ -186,7 +186,7 @@ static unsigned int get_typical_interval(struct menu_device *data,
        unsigned int min, max, thresh, avg;
        uint64_t sum, variance;
 
-       thresh = UINT_MAX; /* Discard outliers above this value */
+       thresh = INT_MAX; /* Discard outliers above this value */
 
 again: