Re: [PATCH] fix the issue that the tick_nohz_get_sleep_length() function could return a negative value

From: Frederic Weisbecker
Date: Mon Feb 01 2021 - 08:48:10 EST


On Wed, Jan 20, 2021 at 11:49:38PM +0000, Zhou Ti (x2019cwm) wrote:
> 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().

Makes sense, queued, thanks!

>
> Signed-off-by: Ti Zhou <x2019cwm@xxxxxxx>
> ---
> --- 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);
> }
>