From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-16.0 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 2A964C433E0 for ; Fri, 19 Feb 2021 12:10:33 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id C8BC564EB7 for ; Fri, 19 Feb 2021 12:10:32 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230219AbhBSMKE (ORCPT ); Fri, 19 Feb 2021 07:10:04 -0500 Received: from mail.kernel.org ([198.145.29.99]:40864 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229998AbhBSMJz (ORCPT ); Fri, 19 Feb 2021 07:09:55 -0500 Received: by mail.kernel.org (Postfix) with ESMTPSA id 172CA64EB8; Fri, 19 Feb 2021 12:09:13 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1613736554; bh=S8+swo0kxO+4YEuYc6YnXlZjZV+VM73UWiHZsFpDoJA=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=MNPk+39YlVWWcTpd4pyvv55Vce77wpm1V94AvbhOct8VEqRaju1iNfkJRLt5OeV3d nkN+MQUY4G7tdf/dOYYw2Vf+SB8Nb/QPfxw2/8N24d5rQfGjO+B03/WtcGA0lm6QRu fcg+UFarshoJlMOtXy9HB+TDwBQWOheub0QAg7Dp02BhIDuTdmQmBcFkuqjCuyvGjY beV4dWD8l4tRIYRTmLWtdEYwDAOLK1+zgSmEwpZy5F2gRwpX6apkkH8b3GsWEdhrKO Rf26wH/aFf0FtlLoomA6sKx56DlgxVcsPQsiAs9iAhPXUcRM/+p4abGLXNmnh/Pseb cliCJ8AxUAonQ== Date: Fri, 19 Feb 2021 13:09:11 +0100 From: Frederic Weisbecker To: "Zhou Ti (x2019cwm)" Cc: "fweisbec@gmail.com" , "tglx@linutronix.de" , "mingo@kernel.org" , "linux-kernel@vger.kernel.org" Subject: Re: [PATCH] fix the issue that the tick_nohz_get_sleep_length() function could return a negative value Message-ID: <20210219120911.GA51281@lothringen> References: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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(). > > Signed-off-by: Ti Zhou > --- > --- 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); > } > I have reworked the changelog that way and queued. I'll post a series including it after the merge window. Thanks! --- From: "Zhou Ti (x2019cwm)" Date: Wed, 20 Jan 2021 23:49:38 +0000 Subject: [PATCH] tick/nohz: Prevent tick_nohz_get_sleep_length() from returning negative value If the hardware clock happens to fire its interrupts late, two possible issues can happen while calling tick_nohz_get_sleep_length(). Either: 1) The next clockevent device event is due past the last idle entry time. or: 2) The last timekeeping update happened before the last idle entry time and the next timer callback expires past the last idle entry time. Make sure that both cases are handled to avoid returning a negative duration to the cpuidle governors. Signed-off-by: Ti Zhou Signed-off-by: Frederic Weisbecker --- kernel/time/tick-sched.c | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/kernel/time/tick-sched.c b/kernel/time/tick-sched.c index e10a4af88737..22b6a46818cf 100644 --- a/kernel/time/tick-sched.c +++ b/kernel/time/tick-sched.c @@ -1142,6 +1142,9 @@ ktime_t tick_nohz_get_sleep_length(ktime_t *delta_next) *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_t *delta_next) 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); } -- 2.25.1