Skip to content

Commit

Permalink
cpuidle: Check called function parameter in get_typical_interval()
Browse files Browse the repository at this point in the history
get_typical_interval() uses int_sqrt() in calculation of standard
deviation. The formal parameter of int_sqrt() is unsigned long, which
may on some platforms be smaller than the 64 bit unsigned integer used
as the actual parameter. The overflow can occur frequently when actual
idle period lengths are in hundreds of milliseconds.

This patch adds a check for such overflow and rejects the candidate
average when an overflow would occur.

Signed-off-by: Tuukka Tikkanen <[email protected]>
Signed-off-by: Rafael J. Wysocki <[email protected]>
  • Loading branch information
Tuukka Tikkanen authored and rafaeljw committed Aug 22, 2013
1 parent 017099e commit 0d6a7ff
Showing 1 changed file with 13 additions and 5 deletions.
18 changes: 13 additions & 5 deletions drivers/cpuidle/governors/menu.c
Original file line number Diff line number Diff line change
Expand Up @@ -226,18 +226,26 @@ static void get_typical_interval(struct menu_device *data)
}
}
do_div(stddev, divisor);
stddev = int_sqrt(stddev);
/*
* The typical interval is obtained when standard deviation is small
* or standard deviation is small compared to the average interval.
*
* int_sqrt() formal parameter type is unsigned long. When the
* greatest difference to an outlier exceeds ~65 ms * sqrt(divisor)
* the resulting squared standard deviation exceeds the input domain
* of int_sqrt on platforms where unsigned long is 32 bits in size.
* In such case reject the candidate average.
*
* Use this result only if there is no timer to wake us up sooner.
*/
if (((avg > stddev * 6) && (divisor * 4 >= INTERVALS * 3))
if (likely(stddev <= ULONG_MAX)) {
stddev = int_sqrt(stddev);
if (((avg > stddev * 6) && (divisor * 4 >= INTERVALS * 3))
|| stddev <= 20) {
if (data->expected_us > avg)
data->predicted_us = avg;
return;
if (data->expected_us > avg)
data->predicted_us = avg;
return;
}
}

/*
Expand Down

0 comments on commit 0d6a7ff

Please sign in to comment.