Received: by 2002:ab2:6857:0:b0:1ef:ffd0:ce49 with SMTP id l23csp2637256lqp; Mon, 25 Mar 2024 05:18:33 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCXW0Sk4+M3ubgppBdN7sUJCUMpLsng90dELt9rei68lWdgzwCTVcK5/hRiJnnJ1WR/x+hfdomRMjP8F7Cs5qA1HROTSylysVxYMEhisSw== X-Google-Smtp-Source: AGHT+IHAO3De39bH7Iq4DE8qhgze5LR5bvNhhRTZKT5G1YfwLxXz5b5+XSyFGjUTA0PTKdDQetrh X-Received: by 2002:a05:6402:50d1:b0:56b:7da8:7ecd with SMTP id h17-20020a05640250d100b0056b7da87ecdmr4319784edb.29.1711369113262; Mon, 25 Mar 2024 05:18:33 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1711369113; cv=pass; d=google.com; s=arc-20160816; b=T0BEMwZ2uTVl9TcBWZTdY7LAs1lO3POCd2odB2ps5TsKm+Q2rYNriOplLPPI9d2oj9 dj5kV4iRaL9szvWVDTMOR0esqU5j9H26siCIeHtCLtb2cJXoFEz8zVDv6we1vrXjZqb6 Nmv8EWzi3ILO225HN986pnJQ3VU1Xe6KJf4vFW7Bwe+X4AkIzjuyIBGLiLRmNDbCzWK/ fS5QokSCESgN4qPMlFq5nj+fTrL6lOOxnyNRdO4/WyDqEv5NtWmn3FdqW75E7FdT9J23 6x30nID+iKtuTMSzGcteSuMnPY9IkMbtJscww9AjFnPPdtQMYw64IMLZfEyVOFtzLqYu dWXw== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:organization:mime-version :list-unsubscribe:list-subscribe:list-id:precedence:references :in-reply-to:message-id:date:subject:cc:to:from:dkim-signature; bh=04azWrAy06OlU2zmrC+8RcNU/rPMPuTZW69l1MhFPrw=; fh=wgq3SFFcpYoLsYNr1Fy+HYH2+WGNJgTNu+AnKLa6P2U=; b=fiA5Jv8wJRlmAQk2R+IPbWROWCsGYZi9U9udhWwsuSVMkdNbBo9HlrfgepPtVNEbL9 cqvS1wKq9lVUGokBQRwX2kuq3JUsbOWHh8UFRA3yWzKdVzK/jn/dVoQpP65SNT8cse3A bQ1tbhRZerzCxllW8jf2m98V8zRS8TZsNHCsj2t325Jqp8/5yVQDNoj+/mrYF8VI2E6m jY4EHEnGUHQi/5vcHp+ler45kni+YaWd5Ma8S+0HcH6MbfnWDYo9I6mLGqjtfVuvgX9V v7UmkamwOX+fbvx3Gfb6eVEtjUCXQB2z6CqZ4CNccp4rGUYKcFjxJcOPXTLEHBWrpyUd o7nQ==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=Ccf6e5TX; arc=pass (i=1 spf=pass spfdomain=intel.com dkim=pass dkdomain=intel.com dmarc=pass fromdomain=intel.com); spf=pass (google.com: domain of linux-kernel+bounces-116587-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-116587-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Return-Path: Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [2604:1380:4601:e00::3]) by mx.google.com with ESMTPS id f21-20020a056402005500b0056b906dc39esi2586312edu.531.2024.03.25.05.18.33 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 25 Mar 2024 05:18:33 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-116587-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) client-ip=2604:1380:4601:e00::3; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=Ccf6e5TX; arc=pass (i=1 spf=pass spfdomain=intel.com dkim=pass dkdomain=intel.com dmarc=pass fromdomain=intel.com); spf=pass (google.com: domain of linux-kernel+bounces-116587-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-116587-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by am.mirrors.kernel.org (Postfix) with ESMTPS id CE45E1F37A1C for ; Mon, 25 Mar 2024 12:18:32 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 2EC22146D7C; Mon, 25 Mar 2024 08:53:38 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="Ccf6e5TX" Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 9BBCF16131F; Mon, 25 Mar 2024 06:42:39 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=192.198.163.17 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711348961; cv=none; b=GZplR6lTWEycOPWKtaSOaVZmc/kmWjXvd+hlCXqWa8d5zPBdCj6c390QdNKjCanNODDiTO5KeVw/PfBM8oGyOw/bQ8tt3iA7ir7jl4BxOpHGdwehXo0shI26sboSZDKvx4ZNbL4JMWN8O2SjdeY0WRndo6KaLfoGlO21QS+Ip5k= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711348961; c=relaxed/simple; bh=qOOcsyBxpUsFxkAH1hi2UNpYCz2148dQEbBRMVKVw/4=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=rAvk1vyUzTjzbcQHFHntucgHdsemm2MLjTib9UItFITf6c1x5AAnZLF6YsClzbG9JFusen9REPTJyG+sc5+VJB0QBRGQYR7zJJ0lR8YD1/a+u/xT9ENhwL1xjhWhLeWqgDrt4XeUybCNIGVXl57ehWIlgPcHx4U37VQRX9OxHts= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com; spf=pass smtp.mailfrom=intel.com; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b=Ccf6e5TX; arc=none smtp.client-ip=192.198.163.17 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=intel.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1711348959; x=1742884959; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=qOOcsyBxpUsFxkAH1hi2UNpYCz2148dQEbBRMVKVw/4=; b=Ccf6e5TX60YVh3RAGKD3v7K8r7FMP6j0jRM+vq7+0d4MdzRYVxan1guz wjBg9bugYYqDetiF6mtAuSfZolfOphFky2rbAwxGjZcQTO2MGxfJjxllV tZ8AD+2EtZZX1TnIuC55tGvCkFaLRxlgjIxes2bLe2VqUtLaAAQS5JtHY HJFFtvI7h/4A6LoqnF5uXX8zVnbbe0C2Dn6nMtkDsrdR/BT+icAlOM54e FfLjaKi1AJiGWWoXVBtIjS2tKUY9xSu9mfPuur7Y/+eHnYxS8Y+vkJ+12 8MnMGxkUoqxC48em02KDlKZW+Ub7CDlJrp/ElQOchg/RCOwjSKBlT2Yv9 g==; X-IronPort-AV: E=McAfee;i="6600,9927,11023"; a="6191594" X-IronPort-AV: E=Sophos;i="6.07,152,1708416000"; d="scan'208";a="6191594" Received: from fmviesa002.fm.intel.com ([10.60.135.142]) by fmvoesa111.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 24 Mar 2024 23:42:38 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.07,152,1708416000"; d="scan'208";a="38629710" Received: from ahunter6-mobl1.ger.corp.intel.com (HELO ahunter-VirtualBox.home\044ger.corp.intel.com) ([10.251.211.155]) by fmviesa002-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 24 Mar 2024 23:42:31 -0700 From: Adrian Hunter To: Thomas Gleixner Cc: Michael Ellerman , Nicholas Piggin , Christophe Leroy , "Aneesh Kumar K.V" , "Naveen N. Rao" , Heiko Carstens , Vasily Gorbik , Alexander Gordeev , Christian Borntraeger , Sven Schnelle , Ingo Molnar , Borislav Petkov , Dave Hansen , x86@kernel.org, "H. Peter Anvin" , Andy Lutomirski , Vincenzo Frascino , John Stultz , Stephen Boyd , Peter Zijlstra , Randy Dunlap , Bjorn Helgaas , Arnd Bergmann , Anna-Maria Behnsen , linuxppc-dev@lists.ozlabs.org, linux-kernel@vger.kernel.org, linux-s390@vger.kernel.org Subject: [PATCH V2 18/19] timekeeping: Let timekeeping_cycles_to_ns() handle both under and overflow Date: Mon, 25 Mar 2024 08:40:22 +0200 Message-Id: <20240325064023.2997-19-adrian.hunter@intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240325064023.2997-1-adrian.hunter@intel.com> References: <20240325064023.2997-1-adrian.hunter@intel.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Organization: Intel Finland Oy, Registered Address: PL 281, 00181 Helsinki, Business Identity Code: 0357606 - 4, Domiciled in Helsinki Content-Transfer-Encoding: 8bit For the case !CONFIG_CLOCKSOURCE_VALIDATE_LAST_CYCLE, forego overflow protection in the range (mask << 1) < delta <= mask, and interpret it always as an inconsistency between CPU clock values. That allows slightly neater code, and it is on a slow path so has no effect on performance. Suggested-by: Thomas Gleixner Signed-off-by: Adrian Hunter --- kernel/time/timekeeping.c | 31 +++++++++++++------------------ 1 file changed, 13 insertions(+), 18 deletions(-) diff --git a/kernel/time/timekeeping.c b/kernel/time/timekeeping.c index 111dfdbd488f..4e18db1819f8 100644 --- a/kernel/time/timekeeping.c +++ b/kernel/time/timekeeping.c @@ -266,17 +266,14 @@ static inline u64 timekeeping_debug_get_ns(const struct tk_read_base *tkr) * Try to catch underflows by checking if we are seeing small * mask-relative negative values. */ - if (unlikely((~delta & mask) < (mask >> 3))) { + if (unlikely((~delta & mask) < (mask >> 3))) tk->underflow_seen = 1; - now = last; - } - /* Cap delta value to the max_cycles values to avoid mult overflows */ - if (unlikely(delta > max)) { + /* Check for multiplication overflows */ + if (unlikely(delta > max)) tk->overflow_seen = 1; - now = last + max; - } + /* timekeeping_cycles_to_ns() handles both under and overflow */ return timekeeping_cycles_to_ns(tkr, now); } #else @@ -375,19 +372,17 @@ static inline u64 timekeeping_cycles_to_ns(const struct tk_read_base *tkr, u64 c u64 mask = tkr->mask, delta = (cycles - tkr->cycle_last) & mask; /* - * This detects the case where the delta overflows the multiplication - * with tkr->mult. + * This detects both negative motion and the case where the delta + * overflows the multiplication with tkr->mult. */ if (unlikely(delta > tkr->clock->max_cycles)) { - if (IS_ENABLED(CONFIG_CLOCKSOURCE_VALIDATE_LAST_CYCLE)) { - /* - * Handle clocksource inconsistency between CPUs to prevent - * time from going backwards by checking for the MSB of the - * mask being set in the delta. - */ - if (unlikely(delta & ~(mask >> 1))) - return tkr->xtime_nsec >> tkr->shift; - } + /* + * Handle clocksource inconsistency between CPUs to prevent + * time from going backwards by checking for the MSB of the + * mask being set in the delta. + */ + if (delta & ~(mask >> 1)) + return tkr->xtime_nsec >> tkr->shift; return delta_to_ns_safe(tkr, delta); } -- 2.34.1