Received: by 2002:a05:6a10:8c0a:0:0:0:0 with SMTP id go10csp7969552pxb; Fri, 19 Feb 2021 04:11:46 -0800 (PST) X-Google-Smtp-Source: ABdhPJxF12yxYTn3codlXFzsOjqOYe9l4wJvkO/IaWC+0U96+F/JB3YnTnSfcQ04wWxo49/IOP5i X-Received: by 2002:a17:906:5357:: with SMTP id j23mr8342736ejo.407.1613736705836; Fri, 19 Feb 2021 04:11:45 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1613736705; cv=none; d=google.com; s=arc-20160816; b=XeY25+MQHwsP0XLt5I1hkXIZ7tnd7ztra6MvCIZFbxLwSjyHdLV0uh+FjQeix3Ou6B VGivHjQpq7+UU39fYJHqg8BPG5ganRQjLlpKZfFbEzAz5Ou1OD62ZIB5ovWe0avr1Q76 06JPRTCgS/DConPq+KQn+iXWv84LhzuFsIqp8roU7crdYTttViW82GP+uDKhVZly8b5C d+8GwchNBXrTM7dc5ZOghy5m80qVfNXKFGWRI18zFLp1SUzy31JKpmVUSEgn9SaEHgfJ uevB5S1obVrMGpHVzUf2rzwkIxoaPpDF1OgIdg3i8RC0/XLeLHCcDf+0TQ7+RxVah/Ib gjvg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date:dkim-signature; bh=X905L4N9LFfp3lSWeA5WonRup+SFCJeq24JqwI/b64A=; b=Xto+XS4SAp2FsPl88OwUyEpYvVj4aUR7j2FzGciI4EuK0rQVM3mHnzBER7hy0Kiy44 W5o+SLWIpjxU7NdkLItUeE/y+MEyPjV7z2YSu+gddI4eG9OU9J65rYEjM6GTHzdkm16e hLcTNKLeMFwK6MzRA0hSo8hGJAj2hMHDCBfxXl+lI+Yc2uvW2KqlGxrz++7sE7vlTE+y VPDS1x8RDGstPn0XfUiNAeT7Xfm+ky9RnjebeHH1J1b4afZ/p4ucQga+LpFilP0t1syr pAixITb3bSyjK8ktin0hWqG8IMYCxMCHPh4lFCuq991PnNebkVlDKNEhKbqvGxnykqtX SNhw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=MNPk+39Y; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id z19si6531835edr.437.2021.02.19.04.11.22; Fri, 19 Feb 2021 04:11:45 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=MNPk+39Y; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230184AbhBSMKA (ORCPT + 99 others); Fri, 19 Feb 2021 07:10:00 -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