Skip to content

Commit 88390dd

Browse files
C Chengrafaeljw
authored andcommitted
cpuidle: Avoid potential overflow in integer multiplication
In detail: In C language, when you perform a multiplication operation, if both operands are of int type, the multiplication operation is performed on the int type, and then the result is converted to the target type. This means that if the product of int type multiplication exceeds the range that int type can represent, an overflow will occur even if you store the result in a variable of int64_t type. For a multiplication of two int values, it is better to use mul_u32_u32() rather than s->exit_latency_ns = s->exit_latency * NSEC_PER_USEC to avoid potential overflow happenning. Signed-off-by: C Cheng <C.Cheng@mediatek.com> Signed-off-by: Bo Ye <bo.ye@mediatek.com> Reviewed-by: AngeloGioacchino Del Regno <angelogioacchino.delregno@collabora.com> [ rjw: New subject ] Signed-off-by: Rafael J. Wysocki <rafael.j.wysocki@intel.com>
1 parent 496d0a6 commit 88390dd

File tree

1 file changed

+2
-1
lines changed

1 file changed

+2
-1
lines changed

drivers/cpuidle/driver.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include <linux/cpumask.h>
1717
#include <linux/tick.h>
1818
#include <linux/cpu.h>
19+
#include <linux/math64.h>
1920

2021
#include "cpuidle.h"
2122

@@ -187,7 +188,7 @@ static void __cpuidle_driver_init(struct cpuidle_driver *drv)
187188
s->target_residency = div_u64(s->target_residency_ns, NSEC_PER_USEC);
188189

189190
if (s->exit_latency > 0)
190-
s->exit_latency_ns = s->exit_latency * NSEC_PER_USEC;
191+
s->exit_latency_ns = mul_u32_u32(s->exit_latency, NSEC_PER_USEC);
191192
else if (s->exit_latency_ns < 0)
192193
s->exit_latency_ns = 0;
193194
else

0 commit comments

Comments
 (0)