clocksource/drivers/timer-tegra186: Avoid 64-bit divide operation
Subsystems (2)
PATCH 1/2 clocksource/drivers/timer-tegra186: Avoid 64-bit divide operation
Building the driver on xtensa fails with
tensa-linux-ld: drivers/clocksource/timer-tegra186.o:
in function `tegra186_timer_remove':
timer-tegra186.c:(.text+0x350):
undefined reference to `__udivdi3'
Avoid the problem by rearranging the offending code to avoid the 64-bit divide operation.
--- drivers/clocksource/timer-tegra186.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/clocksource/timer-tegra186.c +1 -1
@@ -267,7 +267,7 @@ static unsigned int tegra186_wdt_get_timeleft(struct watchdog_device *wdd)* counter value to the time of the counter expirations that * remain. */ - timeleft += (((u64 )wdt->base.timeout *USEC_PER_SEC ) /5 ) * (4 - expiration); + timeleft += ((u64 )wdt->base.timeout * (USEC_PER_SEC /5 )) * (4 - expiration);/* * Convert the current counter value to seconds,
Signature · 1 line
2.45.2
PATCH 2/2 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.
--- drivers/clocksource/timer-tegra186.c | 25 +++++++++++++++---------- 1 file changed, 15 insertions(+), 10 deletions(-)
drivers/clocksource/timer-tegra186.c +15 -10
@@ -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 = {
Signature · 1 line
2.45.2
Re: [PATCH 2/2] clocksource/drivers/timer-tegra186: Simplify calculating timeleft
Quoted 28 lines · Guenter Roeck
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.
Cc Pohsun Su <[email protected]>Cc Robert Lin <[email protected]>Signed-off-by Guenter Roeck <[email protected]>--- drivers/clocksource/timer-tegra186.c | 25 +++++++++++++++---------- 1 file changed, 15 insertions(+), 10 deletions(-)drivers/clocksource/timer-tegra186.c +1
-1@@ -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.
drivers/clocksource/timer-tegra186.c static unsigned int tegra186_wdt_get_timeleft(struct watchdog_device *wdd) · line 278
Quoted 10 lines
drivers/clocksource/timer-tegra186.c +10
-0+* 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.
Nit, I may have worded this the other way around and said that 'timeleft
cannot overflow a 32-bit type and so 32-bit variables are sufficient
for this calculation'. Simply because there are no longer any 64-bit variables used now. Anyway from the history of the change it will be clear where this came from.
Quoted 12 lines
drivers/clocksource/timer-tegra186.c +2
-5*/ - 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 );
drivers/clocksource/timer-tegra186.c static unsigned int tegra186_wdt_get_timeleft(struct watchdog_device *wdd) · line 291
Quoted 10 lines
drivers/clocksource/timer-tegra186.c +2
-4+ 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 = {
Otherwise ...
Thanks!
Jon
Signature · 1 line
nvpublic
[tip: timers/clocksource] clocksource/drivers/timer-tegra186: Simplify calculating timeleft
In reply to Guenter RoeckThe following commit has been merged into the timers/clocksource branch of tip:
The following commit has been merged into the timers/clocksource branch of tip:
Commit-ID: 05065b2a86f86603e5c35dad4274e66be25b2d88 Gitweb: https://git.kernel.org/tip/05065b2a86f86603e5c35dad4274e66be25b2d88 Author: Guenter Roeck <[email protected]> AuthorDate: Sat, 14 Jun 2025 10:55:56 -07:00 Committer: Daniel Lezcano <[email protected]> CommitterDate: Tue, 15 Jul 2025 13:08:07 +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.
--- drivers/clocksource/timer-tegra186.c | 25 +++++++++++++++---------- 1 file changed, 15 insertions(+), 10 deletions(-)
drivers/clocksource/timer-tegra186.c +15 -10
@@ -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 = {
[tip: timers/clocksource] clocksource/drivers/timer-tegra186: Simplify calculating timeleft
In reply to Guenter RoeckThe following commit has been merged into the timers/clocksource branch of tip:
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 <[email protected]> AuthorDate: Sat, 14 Jun 2025 10:55:56 -07:00 Committer: Ingo Molnar <[email protected]> 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.
--- drivers/clocksource/timer-tegra186.c | 25 +++++++++++++++---------- 1 file changed, 15 insertions(+), 10 deletions(-)
drivers/clocksource/timer-tegra186.c +15 -10
@@ -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 = {
Quoted 23 lines · Guenter Roeck
Building the driver on xtensa fails with
tensa-linux-ld:
drivers/clocksource/timer-tegra186.o:
in function `tegra186_timer_remove':
timer-tegra186.c:(.text+0x350):
undefined reference to `__udivdi3'Avoid the problem by rearranging the offending code to avoid the 64-bit divide operation.
Fixes 28c842c8b0f5 ("clocksource/drivers/timer-tegra186: Add WDIOC_GETTIMELEFT support")Cc Pohsun Su <[email protected]>Cc Robert Lin <[email protected]>Signed-off-by Guenter Roeck <[email protected]>--- drivers/clocksource/timer-tegra186.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)drivers/clocksource/timer-tegra186.c +0
-0
drivers/clocksource/timer-tegra186.c static unsigned int tegra186_wdt_get_timeleft(struct watchdog_device *wdd) · line 273
Quoted 8 lines
drivers/clocksource/timer-tegra186.c +1
-1@@ -267,7 +267,7 @@ static unsigned int tegra186_wdt_get_timeleft(struct watchdog_device *wdd)* counter value to the time of the counter expirations that * remain. */ - timeleft += (((u64 )wdt->base.timeout *USEC_PER_SEC ) /5 ) * (4 - expiration); + timeleft += ((u64 )wdt->base.timeout * (USEC_PER_SEC /5 )) * (4 - expiration);/* * Convert the current counter value to seconds,
Thanks!
Jon
Signature · 1 line
nvpublic
Quoted 10 lines · Guenter Roeck
Building the driver on xtensa fails with
tensa-linux-ld:
drivers/clocksource/timer-tegra186.o:
in function `tegra186_timer_remove':
timer-tegra186.c:(.text+0x350):
undefined reference to `__udivdi3'Avoid the problem by rearranging the offending code to avoid the 64-bit divide operation.
Hi Guenter,
Arnd posted a version which seems more complete.
https://lore.kernel.org/all/[email protected]/
Quoted 24 lines
Fixes 28c842c8b0f5 ("clocksource/drivers/timer-tegra186: Add WDIOC_GETTIMELEFT support")Cc Pohsun Su <[email protected]>Cc Robert Lin <[email protected]>Signed-off-by Guenter Roeck <[email protected]>--- drivers/clocksource/timer-tegra186.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)drivers/clocksource/timer-tegra186.c +1
-1@@ -267,7 +267,7 @@ static unsigned int tegra186_wdt_get_timeleft(struct watchdog_device *wdd)* counter value to the time of the counter expirations that * remain. */ - timeleft += (((u64 )wdt->base.timeout *USEC_PER_SEC ) /5 ) * (4 - expiration); + timeleft += ((u64 )wdt->base.timeout * (USEC_PER_SEC /5 )) * (4 - expiration);/* * Convert the current counter value to seconds, Signature · 1 line
2.45.2
Signature · 5 lines
<http://www.linaro.org/> Linaro.org │ Open source software for ARM SoCs Follow Linaro: <http://www.facebook.com/pages/Linaro> Facebook | <http://twitter.com/#!/linaroorg> Twitter | <http://www.linaro.org/linaro-blog/> Blog
Jon Hunter
maintainerHi Daniel,
Quoted 12 lines · Daniel Lezcano
Quoted 10 lines · Guenter Roeck
Building the driver on xtensa fails with
tensa-linux-ld:
drivers/clocksource/timer-tegra186.o:
in function `tegra186_timer_remove':
timer-tegra186.c:(.text+0x350):
undefined reference to `__udivdi3'Avoid the problem by rearranging the offending code to avoid the 64-bit divide operation.
Quoted 5 lines
Hi Guenter,
Arnd posted a version which seems more complete.
Actually Arnd responded here saying this version looks better :-)
https://lore.kernel.org/all/[email protected]/
OK, to pick up this series now?
Thanks
Jon
Signature · 1 line
nvpublic
Quoted 19 lines · Jon Hunter
Hi Daniel,
Quoted 16 lines · Daniel Lezcano
Quoted 10 lines · Guenter Roeck
Building the driver on xtensa fails with
tensa-linux-ld:
drivers/clocksource/timer-tegra186.o:
in function `tegra186_timer_remove':
timer-tegra186.c:(.text+0x350):
undefined reference to `__udivdi3'Avoid the problem by rearranging the offending code to avoid the 64-bit divide operation.
Hi Guenter,
Arnd posted a version which seems more complete.
Quoted 7 lines
Quoted 1 line
Actually Arnd responded here saying this version looks better :-)
https://lore.kernel.org/all/5a571236-7de0-42bc- [email protected]/
OK, to pick up this series now?
Yes, I'm ok.
May be Thomas can pick them up for the next -rc ?
Signature · 5 lines
<http://www.linaro.org/> Linaro.org │ Open source software for ARM SoCs Follow Linaro: <http://www.facebook.com/pages/Linaro> Facebook | <http://twitter.com/#!/linaroorg> Twitter | <http://www.linaro.org/linaro-blog/> Blog
Daniel Lezcano
AppliedQuoted 7 lines · Guenter Roeck
Building the driver on xtensa fails with
tensa-linux-ld:
drivers/clocksource/timer-tegra186.o:
in function `tegra186_timer_remove':
timer-tegra186.c:(.text+0x350):
undefined reference to `__udivdi3'
Quoted 8 lines
Avoid the problem by rearranging the offending code to avoid the 64-bit divide operation.
Fixes 28c842c8b0f5 ("clocksource/drivers/timer-tegra186: Add WDIOC_GETTIMELEFT support")Cc Pohsun Su <[email protected]>Cc Robert Lin <[email protected]>Signed-off-by Guenter Roeck <[email protected]>---
Applied, thanks
Signature · 5 lines
<http://www.linaro.org/> Linaro.org │ Open source software for ARM SoCs Follow Linaro: <http://www.facebook.com/pages/Linaro> Facebook | <http://twitter.com/#!/linaroorg> Twitter | <http://www.linaro.org/linaro-blog/> Blog
The following commit has been merged into the timers/clocksource branch of tip:
The following commit has been merged into the timers/clocksource branch of tip:
Commit-ID: eda68f63d2bbc16a485f9f0f4df79253ae5353f2 Gitweb: https://git.kernel.org/tip/eda68f63d2bbc16a485f9f0f4df79253ae5353f2 Author: Guenter Roeck <[email protected]> AuthorDate: Sat, 14 Jun 2025 10:55:55 -07:00 Committer: Daniel Lezcano <[email protected]> CommitterDate: Tue, 15 Jul 2025 13:08:07 +02:00
clocksource/drivers/timer-tegra186: Avoid 64-bit divide operation
Building the driver on xtensa fails with
tensa-linux-ld: drivers/clocksource/timer-tegra186.o:
in function `tegra186_timer_remove':
timer-tegra186.c:(.text+0x350):
undefined reference to `__udivdi3'
Avoid the problem by rearranging the offending code to avoid the 64-bit divide operation.
--- drivers/clocksource/timer-tegra186.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/clocksource/timer-tegra186.c +1 -1
@@ -267,7 +267,7 @@ static unsigned int tegra186_wdt_get_timeleft(struct watchdog_device *wdd)* counter value to the time of the counter expirations that * remain. */ - timeleft += (((u64 )wdt->base.timeout *USEC_PER_SEC ) /5 ) * (4 - expiration); + timeleft += ((u64 )wdt->base.timeout * (USEC_PER_SEC /5 )) * (4 - expiration);/* * Convert the current counter value to seconds,
The following commit has been merged into the timers/clocksource branch of tip:
The following commit has been merged into the timers/clocksource branch of tip:
Commit-ID: ed1d4c331f9f161788903885ce9ddfd665bdbe18 Gitweb: https://git.kernel.org/tip/ed1d4c331f9f161788903885ce9ddfd665bdbe18 Author: Guenter Roeck <[email protected]> AuthorDate: Sat, 14 Jun 2025 10:55:55 -07:00 Committer: Ingo Molnar <[email protected]> CommitterDate: Fri, 25 Jul 2025 12:06:28 +02:00
clocksource/drivers/timer-tegra186: Avoid 64-bit divide operation
Building the driver on xtensa fails with:
tensa-linux-ld: drivers/clocksource/timer-tegra186.o:
in function `tegra186_timer_remove':
timer-tegra186.c:(.text+0x350):
undefined reference to `__udivdi3'
Avoid the problem by rearranging the offending code to avoid the 64-bit divide operation.
--- drivers/clocksource/timer-tegra186.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/clocksource/timer-tegra186.c +1 -1
@@ -267,7 +267,7 @@ static unsigned int tegra186_wdt_get_timeleft(struct watchdog_device *wdd)* counter value to the time of the counter expirations that * remain. */ - timeleft += (((u64 )wdt->base.timeout *USEC_PER_SEC ) /5 ) * (4 - expiration); + timeleft += ((u64 )wdt->base.timeout * (USEC_PER_SEC /5 )) * (4 - expiration);/* * Convert the current counter value to seconds,