Received: by 2002:a05:6a10:9848:0:0:0:0 with SMTP id x8csp313410pxf; Thu, 11 Mar 2021 04:40:50 -0800 (PST) X-Google-Smtp-Source: ABdhPJy3RWScEKZB2STtQUk5cOEzgazYIRgzmhbtPkbq0OOfpaXYsqpY/dlesPeqQwpoXT4WJh9W X-Received: by 2002:aa7:c150:: with SMTP id r16mr8113993edp.96.1615466450049; Thu, 11 Mar 2021 04:40:50 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1615466450; cv=none; d=google.com; s=arc-20160816; b=SnlCH7gHnTGsi+z0SyAClJixiyE+/ziumR/hcNE6tvXce0hGqddatN+MEuBoo2k1fe elBvKVT94dOFol43dNmInhIymwd0ID1b8TbehrtRnD4kut+nvCyqdwlb9OXZGyvmqdOn vsRD9XKO5sYD2wiAM24Zau5hKANSscu6tZu6Ai9QMBqBJOa6kq03P2JyPVrY5QEgHRDO JHSaipAUVLEInrRWAYfNFPQ4AADyX4BzlsEtY3g2SQI0fdObn29TrSpEPk2zEtrzqZ8U H+JFw/2TsbVxBV463U87MqXUq25HmaUS3aEsguWH/Rcshk7nu1a0//2ekzBiSMTzxicY 7c1g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=+bvmPsPcaFLehWtUZC4/+T3Wlu1bUjXxW+lzZSgb9m8=; b=YH9e36QBOUZ38QD2jPkl5WJBlVVEggrqtTHHgxKRMvOPViYK/yfbnov7QCTigPrMse ErQdbz3vLbW6Vq83QT/Ecf7SfX6hTw139OWnCfQNbMLqWHBhS4CJ4ofwDgM9PcoW0bVd dQxMpMz/40gO3JBiczHSl6e6OH7K1s7OJNzNupL7xJ4hoZ220BHQl9OGMTpQIuolBbRG AivWLX9TnoZwsC19vPoWbGFGQ71s9k8VpFEjb8qFl0GypfjwlByWeha4chL0RztXjJgW VIggMXuOGA+chc+jQ9E83vHKa2oMaZL/VXZJmpd6vTvDWdAuNALNqB8xImB9gk9XDumL hQug== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=es5hNvnP; 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 f4si1598365ejk.478.2021.03.11.04.40.27; Thu, 11 Mar 2021 04:40:50 -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=es5hNvnP; 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 S232893AbhCKMhd (ORCPT + 99 others); Thu, 11 Mar 2021 07:37:33 -0500 Received: from mail.kernel.org ([198.145.29.99]:59706 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233190AbhCKMhR (ORCPT ); Thu, 11 Mar 2021 07:37:17 -0500 Received: by mail.kernel.org (Postfix) with ESMTPSA id 1794064FE6; Thu, 11 Mar 2021 12:37:14 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1615466237; bh=I4y2AElfIxluZFcgYq0yvzhKqir2bW0RPqelVOJJZ0Q=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=es5hNvnPZVV/ROxHIEBT0u9HtS4yhm0nUd/voR747OBTeM9uIkNm0C3Dkd8WyQV+/ ifgmnR9eQaqD0CtaNDMsHIEtTataXgYm6MpA5E+uAdL/ILtQDwystlPujSJaiVJiU7 Xo3sGkTPpQtCNBCSb8mpF01OjrprfJ7xTmywx0gg0QRB3D/VjUKg8r2WxZkqQonVRL n+GXcxnWaumPbLKnwfCdBsj49WuURlOLNGd7fZfQ+BgMnSRYoM/kQ3KGCJMPZr7U3N NLdPC825j5uWGPLgv3eaeB+NRCuLpzjCb6znQcXrQaO+MQYmSPZozcld4VLsRnncF4 H5dUH2rpzJ4vg== From: Frederic Weisbecker To: Peter Zijlstra , Thomas Gleixner Cc: LKML , "Zhou Ti (x2019cwm)" , "Rafael J . Wysocki" , Yunfeng Ye , Frederic Weisbecker , "Paul E . McKenney" , Marcelo Tosatti , Ingo Molnar Subject: [PATCH 01/10] tick/nohz: Prevent tick_nohz_get_sleep_length() from returning negative value Date: Thu, 11 Mar 2021 13:36:59 +0100 Message-Id: <20210311123708.23501-2-frederic@kernel.org> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20210311123708.23501-1-frederic@kernel.org> References: <20210311123708.23501-1-frederic@kernel.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: "Zhou Ti (x2019cwm)" 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 before 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 Cc: Rafael J. Wysocki Cc: Peter Zijlstra Cc: Thomas Gleixner 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