Fix the issue that the tick_nohz_get_sleep_length() function could return a negative value.
The variable "dev->next_event" has a small possibility to be smaller than the variable "now" during running, which would result in a negative value of "*delta_next". The variable "next_event" also has a small posibility to be smaller than the variable "now". Both case could lead to a negative return of function tick_nohz_get_sleep_length(). Signed-off-by: Ti Zhou <[email protected]> --- --- tip/kernel/time/tick-sched.c.orig 2021-01-20 05:34:25.151325912 -0400 +++ tip/kernel/time/tick-sched.c 2021-01-20 19:44:28.238538380 -0400 @@ -1142,6 +1142,9 @@ ktime_t tick_nohz_get_sleep_length(ktime *delta_next = ktime_sub(dev->next_event, now); + if (unlikely(*delta_next < 0)) + *delta_next = 0; + if (!can_stop_idle_tick(cpu, ts)) return *delta_next; @@ -1156,6 +1159,9 @@ ktime_t tick_nohz_get_sleep_length(ktime next_event = min_t(u64, next_event, hrtimer_next_event_without(&ts->sched_timer)); + if (unlikely(next_event < now)) + next_event = now; + return ktime_sub(next_event, now); }

