mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: "tip-bot2 for Guenter Roeck" <tip-bot2@linutronix.de>
To: linux-tip-commits@vger.kernel.org
Cc: Guenter Roeck <linux@roeck-us.net>,
	Daniel Lezcano <daniel.lezcano@linaro.org>,
	Ingo Molnar <mingo@kernel.org>, Jon Hunter <jonathanh@nvidia.com>,
	Pohsun Su <pohsuns@nvidia.com>, Robert Lin <robelin@nvidia.com>,
	x86@kernel.org, linux-kernel@vger.kernel.org
Subject: [tip: timers/clocksource] clocksource/drivers/timer-tegra186: Simplify calculating timeleft
Date: Fri, 25 Jul 2025 10:31:36 -0000	[thread overview]
Message-ID: <175343949684.1420.2170175127179290236.tip-bot2@tip-bot2> (raw)
In-Reply-To: <20250614175556.922159-2-linux@roeck-us.net>

The following commit has been merged into the timers/clocksource branch of tip:

Commit-ID:     12c1fe0711d3de30440911f2d3e0147f9831f3c7
Gitweb:        https://git.kernel.org/tip/12c1fe0711d3de30440911f2d3e0147f9831f3c7
Author:        Guenter Roeck <linux@roeck-us.net>
AuthorDate:    Sat, 14 Jun 2025 10:55:56 -07:00
Committer:     Ingo Molnar <mingo@kernel.org>
CommitterDate: Fri, 25 Jul 2025 12:06:33 +02:00

clocksource/drivers/timer-tegra186: Simplify calculating timeleft

It is not necessary to use 64-bit operations to calculate the
remaining watchdog timeout. Simplify to use 32-bit operations,
and add comments explaining why there will be no overflow.

Signed-off-by: Guenter Roeck <linux@roeck-us.net>
Signed-off-by: Daniel Lezcano <daniel.lezcano@linaro.org>
Signed-off-by: Ingo Molnar <mingo@kernel.org>
Reviewed-by: Jon Hunter <jonathanh@nvidia.com>
Cc: Pohsun Su <pohsuns@nvidia.com>
Cc: Robert Lin <robelin@nvidia.com>
Link: https://lore.kernel.org/r/20250614175556.922159-2-linux@roeck-us.net
---
 drivers/clocksource/timer-tegra186.c | 25 +++++++++++++++----------
 1 file changed, 15 insertions(+), 10 deletions(-)

diff --git a/drivers/clocksource/timer-tegra186.c b/drivers/clocksource/timer-tegra186.c
index 76a5481..d403a3f 100644
--- a/drivers/clocksource/timer-tegra186.c
+++ b/drivers/clocksource/timer-tegra186.c
@@ -231,7 +231,7 @@ static unsigned int tegra186_wdt_get_timeleft(struct watchdog_device *wdd)
 {
 	struct tegra186_wdt *wdt = to_tegra186_wdt(wdd);
 	u32 expiration, val;
-	u64 timeleft;
+	u32 timeleft;
 
 	if (!watchdog_active(&wdt->base)) {
 		/* return zero if the watchdog timer is not activated. */
@@ -266,21 +266,26 @@ static unsigned int tegra186_wdt_get_timeleft(struct watchdog_device *wdd)
 	 * Calculate the time remaining by adding the time for the
 	 * counter value to the time of the counter expirations that
 	 * remain.
+	 * Note: Since wdt->base.timeout is bound to 255, the maximum
+	 * value added to timeleft is
+	 *   255 * (1,000,000 / 5) * 4
+	 * = 255 * 200,000 * 4
+	 * = 204,000,000
+	 * TMRSR_PCV is a 29-bit field.
+	 * Its maximum value is 0x1fffffff = 536,870,911.
+	 * 204,000,000 + 536,870,911 = 740,870,911 = 0x2C28CAFF.
+	 * timeleft can therefore not overflow, and 64-bit calculations
+	 * are not necessary.
 	 */
-	timeleft += ((u64)wdt->base.timeout * (USEC_PER_SEC / 5)) * (4 - expiration);
+	timeleft += (wdt->base.timeout * (USEC_PER_SEC / 5)) * (4 - expiration);
 
 	/*
 	 * Convert the current counter value to seconds,
-	 * rounding up to the nearest second. Cast u64 to
-	 * u32 under the assumption that no overflow happens
-	 * when coverting to seconds.
+	 * rounding to the nearest second.
 	 */
-	timeleft = DIV_ROUND_CLOSEST_ULL(timeleft, USEC_PER_SEC);
+	timeleft = DIV_ROUND_CLOSEST(timeleft, USEC_PER_SEC);
 
-	if (WARN_ON_ONCE(timeleft > U32_MAX))
-		return U32_MAX;
-
-	return lower_32_bits(timeleft);
+	return timeleft;
 }
 
 static const struct watchdog_ops tegra186_wdt_ops = {

  parent reply	other threads:[~2025-07-25 10:31 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-06-14 17:55 [PATCH 1/2] clocksource/drivers/timer-tegra186: Avoid 64-bit divide operation Guenter Roeck
2025-06-14 17:55 ` [PATCH 2/2] clocksource/drivers/timer-tegra186: Simplify calculating timeleft Guenter Roeck
2025-06-19  7:25   ` Jon Hunter
2025-07-23  7:17   ` [tip: timers/clocksource] " tip-bot2 for Guenter Roeck
2025-07-25 10:31   ` tip-bot2 for Guenter Roeck [this message]
2025-06-19  7:17 ` [PATCH 1/2] clocksource/drivers/timer-tegra186: Avoid 64-bit divide operation Jon Hunter
2025-06-24 14:32 ` Daniel Lezcano
2025-07-03 12:34   ` Jon Hunter
2025-07-03 12:42     ` Daniel Lezcano
2025-07-15 11:11 ` Daniel Lezcano
2025-07-23  7:17 ` [tip: timers/clocksource] " tip-bot2 for Guenter Roeck
2025-07-25 10:31 ` tip-bot2 for Guenter Roeck
2025-09-25 21:33 [tip: timers/clocksource] clocksource/drivers/timer-tegra186: Simplify calculating timeleft tip-bot2 for Guenter Roeck

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=175343949684.1420.2170175127179290236.tip-bot2@tip-bot2 \
    --to=tip-bot2@linutronix.de \
    --cc=daniel.lezcano@linaro.org \
    --cc=jonathanh@nvidia.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-tip-commits@vger.kernel.org \
    --cc=linux@roeck-us.net \
    --cc=mingo@kernel.org \
    --cc=pohsuns@nvidia.com \
    --cc=robelin@nvidia.com \
    --cc=x86@kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®