Skip to content

Commit 51fd36f

Browse files
dengrafjohnstultz-work
authored andcommitted
hrtimer: Fix ktime_add_ns() overflow on 32bit architectures
One can trigger an overflow when using ktime_add_ns() on a 32bit architecture not supporting CONFIG_KTIME_SCALAR. When passing a very high value for u64 nsec, e.g. 7881299347898368000 the do_div() function converts this value to seconds (7881299347) which is still to high to pass to the ktime_set() function as long. The result in is a negative value. The problem on my system occurs in the tick-sched.c, tick_nohz_stop_sched_tick() when time_delta is set to timekeeping_max_deferment(). The check for time_delta < KTIME_MAX is valid, thus ktime_add_ns() is called with a too large value resulting in a negative expire value. This leads to an endless loop in the ticker code: time_delta: 7881299347898368000 expires = ktime_add_ns(last_update, time_delta) expires: negative value This fix caps the value to KTIME_MAX. This error doesn't occurs on 64bit or architectures supporting CONFIG_KTIME_SCALAR (e.g. ARM, x86-32). Cc: stable@vger.kernel.org Signed-off-by: David Engraf <david.engraf@sysgo.com> [jstultz: Minor tweaks to commit message & header] Signed-off-by: John Stultz <john.stultz@linaro.org>
1 parent 8f294b5 commit 51fd36f

1 file changed

Lines changed: 4 additions & 0 deletions

File tree

‎kernel/hrtimer.c‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,10 @@ ktime_t ktime_add_ns(const ktime_t kt, u64 nsec)
286286
} else {
287287
unsigned long rem = do_div(nsec, NSEC_PER_SEC);
288288

289+
/* Make sure nsec fits into long */
290+
if (unlikely(nsec > KTIME_SEC_MAX))
291+
return (ktime_t){ .tv64 = KTIME_MAX };
292+
289293
tmp = ktime_set((long)nsec, rem);
290294
}
291295

0 commit comments

Comments
 (0)