Skip to contentSkip to conversation

timers: Fix overflow in get_next_timer_interrupt

PATCH In review Matija Glavinic Pecotic 3 participants 3 messages Aug 1, 2017 High-resolution timers, timer wheel, clockevents

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 timers: Fix overflow in get_next_timer_interrupt

For e.g. HZ=100, timer being 430 jiffies in the future, and 32 bit unsigned int, there is an overflow on unsigned int right-hand side of the expression which results with wrong values being returned.

Problem was observed on tickless core and with following applied:

	sched/nohz: add debugfs control over sched_tick_max_deferment
	https://lkml.org/lkml/2013/9/16/499
---
 kernel/time/timer.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
kernel/time/timer.c +1 -1
index 71ce3f4..8f5d1bf 100644
@@ -1495,7 +1495,7 @@ u64 get_next_timer_interrupt(unsigned long basej, u64 basem)
 		base->is_idle = false;
 	} else {
 		if (!is_max_delta)
-			expires = basem + (nextevt - basej) * TICK_NSEC;
+			expires = basem + (u64)(nextevt - basej) * TICK_NSEC;
 		/*
 		 * If we expect to sleep more than a tick, mark the base idle:
 		 */
Signature · 1 line
2.1.4
Reviewed-by on v1 1/1
#1 lore raw
Quoted 10 lines · Matija Glavinic Pecotic

For e.g. HZ=100, timer being 430 jiffies in the future, and 32 bit unsigned int, there is an overflow on unsigned int right-hand side of the expression which results with wrong values being returned.

Problem was observed on tickless core and with following applied:

	sched/nohz: add debugfs control over sched_tick_max_deferment
	https://lkml.org/lkml/2013/9/16/499
Quoted 17 lines
---
 kernel/time/timer.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
kernel/time/timer.c +1 -1
index 71ce3f4..8f5d1bf 100644
@@ -1495,7 +1495,7 @@ u64 get_next_timer_interrupt(unsigned long basej, u64 basem)
 		base->is_idle = false;
 	} else {
 		if (!is_max_delta)
-			expires = basem + (nextevt - basej) * TICK_NSEC;
+			expires = basem + (u64)(nextevt - basej) * TICK_NSEC;
 		/*
 		 * If we expect to sleep more than a tick, mark the base idle:
 		 */
Signature · 2 lines
Best regards,
Alexander Sverdlin.
#2 lore raw
In reply to Matija Glavinic Pecotic
timers: Fix overflow in get_next_timer_interrupt
Commit-ID:  34f41c0316ed52b0b44542491d89278efdaa70e4
Gitweb:     http://git.kernel.org/tip/34f41c0316ed52b0b44542491d89278efdaa70e4
Author:     Matija Glavinic Pecotic <[email protected]>
AuthorDate: Tue, 1 Aug 2017 09:11:52 +0200
Committer:  Thomas Gleixner <[email protected]>
CommitDate: Tue, 1 Aug 2017 14:20:53 +0200

timers: Fix overflow in get_next_timer_interrupt

For e.g. HZ=100, timer being 430 jiffies in the future, and 32 bit unsigned int, there is an overflow on unsigned int right-hand side of the expression which results with wrong values being returned.

Type cast the multiplier to 64bit to avoid that issue.

---
 kernel/time/timer.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
kernel/time/timer.c +1 -1
index 71ce3f4..8f5d1bf 100644
@@ -1495,7 +1495,7 @@ u64 get_next_timer_interrupt(unsigned long basej, u64 basem)
 		base->is_idle = false;
 	} else {
 		if (!is_max_delta)
-			expires = basem + (nextevt - basej) * TICK_NSEC;
+			expires = basem + (u64)(nextevt - basej) * TICK_NSEC;
 		/*
 		 * If we expect to sleep more than a tick, mark the base idle:
 		 */