Received: by 2002:ab2:3350:0:b0:1f4:6588:b3a7 with SMTP id o16csp1330816lqe; Mon, 8 Apr 2024 06:11:16 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCXyMDPaJsyaczyx8GQkQAzVztypgWVS7bfUEnoENaDdzWsaDRaGVBO1X3iCJACHY1yPNl3G7fsIR4ZRjhE6vrvETfG8fgRF8NajFMi5+w== X-Google-Smtp-Source: AGHT+IFs1VyKBqsQgP4wU2ZQb9p3PT9DTFf6jLNcw9SOMET9TOXnSiJw653dOBe6I13S+WA6rkAK X-Received: by 2002:ad4:5c83:0:b0:69b:ec1:de5b with SMTP id o3-20020ad45c83000000b0069b0ec1de5bmr7395930qvh.22.1712581876594; Mon, 08 Apr 2024 06:11:16 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1712581876; cv=pass; d=google.com; s=arc-20160816; b=GkRQzknzX9fclis/drnGzCO6/vjfwUX4oi/6IhcX5sRCZtXo8pX9wJ+DXlrJu0t6tw 3qpf77lwkxcVyfkruQ7ovPZGE9LAzafdz/OBJlA/uBU4lzuqH2BKgKryTU6Xgv7tRGr/ Mguoya5wxtOT70PEtZg5Hs+wuBjcwdQFaQssS5y9ljmS6k9fmhixV+nSDDrmnKcN+fWU EbzqsDB8sO6kEEDs8EWkp2ybRUesojYITH0gVdeKrqulOc+cEoc0hG17MPFlAGAzyP38 GqE1c8QwohAQPcOvTPECVSZpE+T16r7mKNRWFzOtoWsccW8PRZia8NtGvs5oDL1aWaUs HWhA== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:precedence:robot-unsubscribe:robot-id :message-id:mime-version:list-unsubscribe:list-subscribe:list-id :precedence:references:in-reply-to:cc:subject:to:reply-to:sender :from:dkim-signature:dkim-signature:date; bh=8rVdAt5QbB8Evfb6exw131d2h7rvMCM7Gb4D0X+czEQ=; fh=HIrP3i0WQwu8zLnKtb1acHgteo695uCSG6Yk5OndRkw=; b=P0I2mrw42h+Jq8HQJ9TN/tTdRi3CnG3fW5IjHJevuX8xp9AMDrPVwRlMU5L7xSqmZI szAbrq97I8wpN1NIfN2AMizLua2VCsMgrt1pPBeksHNK7cBN4LTkyk4V3LdeSWgaDyBF CD1yP7XtZZyI9PPJ//oyssOkItMAD/lY/ovOgEuE1QkGtaCqxol9vem/IvNDNSGLCP0H JZZ9QjBEmDMek7FjuELriIFVHCYvGoUx/Mt8JiwsVzcOkIh2X6EDkUg+NVnLNBvwA3B4 iSDTEGxQMXg7dZ+G5+elOfi7UcHN527DS3MOAzg2eqx4gLKKqtb7ZDDugD9Q5nmlAH90 4m3A==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b=lEF+fO4K; dkim=neutral (no key) header.i=@linutronix.de header.s=2020e; arc=pass (i=1 spf=pass spfdomain=linutronix.de dkim=pass dkdomain=linutronix.de dmarc=pass fromdomain=linutronix.de); spf=pass (google.com: domain of linux-kernel+bounces-135391-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-135391-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=linutronix.de Return-Path: Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [147.75.199.223]) by mx.google.com with ESMTPS id jo23-20020a056214501700b006969b6138dfsi9085868qvb.452.2024.04.08.06.11.16 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 08 Apr 2024 06:11:16 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-135391-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) client-ip=147.75.199.223; Authentication-Results: mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b=lEF+fO4K; dkim=neutral (no key) header.i=@linutronix.de header.s=2020e; arc=pass (i=1 spf=pass spfdomain=linutronix.de dkim=pass dkdomain=linutronix.de dmarc=pass fromdomain=linutronix.de); spf=pass (google.com: domain of linux-kernel+bounces-135391-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-135391-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=linutronix.de 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 ny.mirrors.kernel.org (Postfix) with ESMTPS id 4EB451C213C6 for ; Mon, 8 Apr 2024 13:11:16 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 8645D7C6CC; Mon, 8 Apr 2024 13:10:38 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="lEF+fO4K"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="b41N6bNg" Received: from galois.linutronix.de (Galois.linutronix.de [193.142.43.55]) (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 066B77317D; Mon, 8 Apr 2024 13:10:35 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=193.142.43.55 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712581837; cv=none; b=M+1t1OqQ5xnmToTEaCCWXUpVXcnVI2vPOD+yqzIMwFefOawl4yGwIbMBJddjCtN7BCNNuJy3qLcqJjt0iTM08WKAjJh6pN/qx2f6k5x7f4ivLP1wWcwJvqPdVlEirhpLJraIhQh4VQAA98qexMd6oL7EBwnMIFxNhfAFWGiL6l8= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712581837; c=relaxed/simple; bh=FjPL6CatV/Y4na7vOOXtnfbJL77oK9XRYpZ8AX5Vj18=; h=Date:From:To:Subject:Cc:In-Reply-To:References:MIME-Version: Message-ID:Content-Type; b=QVoNUAmdRzyKZhCYv2hA+7W34fLLdjwaRx1Xsf7cfh21uu4x8TI4QrGg7l9ltMh/ui+m+BIXYzzlQTtxqRbm4ZCuBqpuUwhaG4f9cRDbStWmZ4FatzFrm8PVeMJ3i8Y9y3NwaaaXgOdtteq47RWgOHkGVL8Lp48bMKpj9X54E68= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linutronix.de; spf=pass smtp.mailfrom=linutronix.de; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=lEF+fO4K; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=b41N6bNg; arc=none smtp.client-ip=193.142.43.55 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linutronix.de Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linutronix.de Date: Mon, 08 Apr 2024 13:10:33 -0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1712581834; h=from:from:sender:sender:reply-to:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=8rVdAt5QbB8Evfb6exw131d2h7rvMCM7Gb4D0X+czEQ=; b=lEF+fO4K6k620QBpK2qqm6tKK7T9uusFFLGgNZiw1GJiyCjnhQtL3PyYRTfgPXRF12/BDc oBKY9UXKwGZF7cjkDMpn0+IVg6H6mfuXT/fvyk3rNtJywSOUFdW/M4ge42ZF7f7U24Oiyg VSH7n8J7sEf4p1iVuA5zLfeloMYgBwIdbG3ISvChxLpR8h6itfehPOwGvx0lUh5o1eljJ/ xZwInZduQrAQLfA6UM0dx5Pna9npXft1KzWXH8XiXUWNeoFsvcrHVILc2Iaw/cnDl87waI ICSsZbbv33FJka2ofFnY8Kgnqb0BiDrSxDOSag1UFfCEVCZE05C7LIKwCcpUcw== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1712581834; h=from:from:sender:sender:reply-to:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=8rVdAt5QbB8Evfb6exw131d2h7rvMCM7Gb4D0X+czEQ=; b=b41N6bNgpjBXVbDjYuLqi79yyb5hRfSmOvwzmlOKIzt1aWvsaUFmDL8GRu4d9ikodWufoQ k+LbDQ5xMBTMHrAg== From: "tip-bot2 for Adrian Hunter" Sender: tip-bot2@linutronix.de Reply-to: linux-kernel@vger.kernel.org To: linux-tip-commits@vger.kernel.org Subject: [tip: timers/core] timekeeping: Prepare timekeeping_cycles_to_ns() for overflow safety Cc: Thomas Gleixner , Adrian Hunter , x86@kernel.org, linux-kernel@vger.kernel.org In-Reply-To: <20240325064023.2997-17-adrian.hunter@intel.com> References: <20240325064023.2997-17-adrian.hunter@intel.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Message-ID: <171258183338.10875.7234337183877644800.tip-bot2@tip-bot2> Robot-ID: Robot-Unsubscribe: Contact to get blacklisted from these emails Precedence: bulk Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit The following commit has been merged into the timers/core branch of tip: Commit-ID: e809a80aa0bcf802f99407c23fd6be6fd4eb250a Gitweb: https://git.kernel.org/tip/e809a80aa0bcf802f99407c23fd6be6fd4eb250a Author: Adrian Hunter AuthorDate: Mon, 25 Mar 2024 08:40:20 +02:00 Committer: Thomas Gleixner CommitterDate: Mon, 08 Apr 2024 15:03:08 +02:00 timekeeping: Prepare timekeeping_cycles_to_ns() for overflow safety Open code clocksource_delta() in timekeeping_cycles_to_ns() so that overflow safety can be added efficiently. Suggested-by: Thomas Gleixner Signed-off-by: Adrian Hunter Signed-off-by: Thomas Gleixner Link: https://lore.kernel.org/r/20240325064023.2997-17-adrian.hunter@intel.com --- kernel/time/timekeeping.c | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/kernel/time/timekeeping.c b/kernel/time/timekeeping.c index 749387f..d174840 100644 --- a/kernel/time/timekeeping.c +++ b/kernel/time/timekeeping.c @@ -367,7 +367,17 @@ static void tk_setup_internals(struct timekeeper *tk, struct clocksource *clock) static inline u64 timekeeping_cycles_to_ns(const struct tk_read_base *tkr, u64 cycles) { /* Calculate the delta since the last update_wall_time() */ - u64 delta = clocksource_delta(cycles, tkr->cycle_last, tkr->mask); + u64 mask = tkr->mask, delta = (cycles - tkr->cycle_last) & mask; + + 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; + } return ((delta * tkr->mult) + tkr->xtime_nsec) >> tkr->shift; }