Fix the issue of max_timeout being calculated larger than actual value.
The calculation result of freq / (S3C2410_WTCON_PRESCALE_MAX + 1) /
S3C2410_WTCON_MAXDIV is smaller than the actual value because the remainder
is discarded during the calculation process. This leads to a larger
calculated value for max_timeout compared to the actual settable value.
To resolve this issue, the order of calculations in the computation process
has been adjusted.
Reviewed-by: Sam Protsenko <semen.protsenko@linaro.org>
Signed-off-by: Sangwook Shin <sw617.shin@samsung.com>
---
drivers/watchdog/s3c2410_wdt.c | 10 ++++++++--
1 file changed, 8 insertions(+), 2 deletions(-)
diff --git a/drivers/watchdog/s3c2410_wdt.c b/drivers/watchdog/s3c2410_wdt.c
index 95f7207e390a..1e8cf0299713 100644
--- a/drivers/watchdog/s3c2410_wdt.c
+++ b/drivers/watchdog/s3c2410_wdt.c
@@ -27,6 +27,7 @@
#include <linux/mfd/syscon.h>
#include <linux/regmap.h>
#include <linux/delay.h>
+#include <linux/math64.h>
#define S3C2410_WTCON 0x00
#define S3C2410_WTDAT 0x04
@@ -410,9 +411,14 @@ static inline unsigned long s3c2410wdt_get_freq(struct s3c2410_wdt *wdt)
static inline unsigned int s3c2410wdt_max_timeout(struct s3c2410_wdt *wdt)
{
const unsigned long freq = s3c2410wdt_get_freq(wdt);
+ const u64 n_max = (u64)(S3C2410_WTCON_PRESCALE_MAX + 1) *
+ S3C2410_WTCON_MAXDIV * S3C2410_WTCNT_MAXCNT;
+ u64 t_max = div64_ul(n_max, freq);
- return S3C2410_WTCNT_MAXCNT / (freq / (S3C2410_WTCON_PRESCALE_MAX + 1)
- / S3C2410_WTCON_MAXDIV);
+ if (t_max > UINT_MAX)
+ t_max = UINT_MAX;
+
+ return t_max;
}
static int s3c2410wdt_disable_wdt_reset(struct s3c2410_wdt *wdt, bool mask)
--
2.25.1
Hi Sangwook > -----Original Message----- > From: Sangwook Shin <sw617.shin@samsung.com> > Sent: Monday, August 18, 2025 7:48 AM > To: krzk@kernel.org; alim.akhtar@samsung.com; wim@linux-watchdog.org; > linux@roeck-us.net; semen.protsenko@linaro.org; > dongil01.park@samsung.com; khwan.seo@samsung.com > Cc: linux-arm-kernel@lists.infradead.org; linux-samsung-soc@vger.kernel.org; > linux-watchdog@vger.kernel.org; linux-kernel@vger.kernel.org; Sangwook Shin > <sw617.shin@samsung.com> > Subject: [PATCH v6 2/5] watchdog: s3c2410_wdt: Fix max_timeout being > calculated larger > > Fix the issue of max_timeout being calculated larger than actual value. > The calculation result of freq / (S3C2410_WTCON_PRESCALE_MAX + 1) / > S3C2410_WTCON_MAXDIV is smaller than the actual value because the > remainder is discarded during the calculation process. This leads to a larger > calculated value for max_timeout compared to the actual settable value. > To resolve this issue, the order of calculations in the computation process has > been adjusted. > > Reviewed-by: Sam Protsenko <semen.protsenko@linaro.org> > Signed-off-by: Sangwook Shin <sw617.shin@samsung.com> > --- Reviewed-by: Alim Akhtar <alim.akhtar@samsung.com>
On Mon, Aug 18, 2025 at 11:18:23AM +0900, Sangwook Shin wrote: > Fix the issue of max_timeout being calculated larger than actual value. > The calculation result of freq / (S3C2410_WTCON_PRESCALE_MAX + 1) / > S3C2410_WTCON_MAXDIV is smaller than the actual value because the remainder > is discarded during the calculation process. This leads to a larger > calculated value for max_timeout compared to the actual settable value. > To resolve this issue, the order of calculations in the computation process > has been adjusted. > > Reviewed-by: Sam Protsenko <semen.protsenko@linaro.org> > Signed-off-by: Sangwook Shin <sw617.shin@samsung.com> Reviewed-by: Guenter Roeck <linux@roeck-us.net> > --- > drivers/watchdog/s3c2410_wdt.c | 10 ++++++++-- > 1 file changed, 8 insertions(+), 2 deletions(-) > > diff --git a/drivers/watchdog/s3c2410_wdt.c b/drivers/watchdog/s3c2410_wdt.c > index 95f7207e390a..1e8cf0299713 100644 > --- a/drivers/watchdog/s3c2410_wdt.c > +++ b/drivers/watchdog/s3c2410_wdt.c > @@ -27,6 +27,7 @@ > #include <linux/mfd/syscon.h> > #include <linux/regmap.h> > #include <linux/delay.h> > +#include <linux/math64.h> > > #define S3C2410_WTCON 0x00 > #define S3C2410_WTDAT 0x04 > @@ -410,9 +411,14 @@ static inline unsigned long s3c2410wdt_get_freq(struct s3c2410_wdt *wdt) > static inline unsigned int s3c2410wdt_max_timeout(struct s3c2410_wdt *wdt) > { > const unsigned long freq = s3c2410wdt_get_freq(wdt); > + const u64 n_max = (u64)(S3C2410_WTCON_PRESCALE_MAX + 1) * > + S3C2410_WTCON_MAXDIV * S3C2410_WTCNT_MAXCNT; > + u64 t_max = div64_ul(n_max, freq); > > - return S3C2410_WTCNT_MAXCNT / (freq / (S3C2410_WTCON_PRESCALE_MAX + 1) > - / S3C2410_WTCON_MAXDIV); > + if (t_max > UINT_MAX) > + t_max = UINT_MAX; > + > + return t_max; > } > > static int s3c2410wdt_disable_wdt_reset(struct s3c2410_wdt *wdt, bool mask) > -- > 2.25.1 > >
On Sun, Aug 17, 2025 at 9:24 PM Sangwook Shin <sw617.shin@samsung.com> wrote: > > Fix the issue of max_timeout being calculated larger than actual value. > The calculation result of freq / (S3C2410_WTCON_PRESCALE_MAX + 1) / > S3C2410_WTCON_MAXDIV is smaller than the actual value because the remainder > is discarded during the calculation process. This leads to a larger > calculated value for max_timeout compared to the actual settable value. > To resolve this issue, the order of calculations in the computation process > has been adjusted. > > Reviewed-by: Sam Protsenko <semen.protsenko@linaro.org> > Signed-off-by: Sangwook Shin <sw617.shin@samsung.com> > --- > drivers/watchdog/s3c2410_wdt.c | 10 ++++++++-- > 1 file changed, 8 insertions(+), 2 deletions(-) > > diff --git a/drivers/watchdog/s3c2410_wdt.c b/drivers/watchdog/s3c2410_wdt.c > index 95f7207e390a..1e8cf0299713 100644 > --- a/drivers/watchdog/s3c2410_wdt.c > +++ b/drivers/watchdog/s3c2410_wdt.c > @@ -27,6 +27,7 @@ > #include <linux/mfd/syscon.h> > #include <linux/regmap.h> > #include <linux/delay.h> > +#include <linux/math64.h> > > #define S3C2410_WTCON 0x00 > #define S3C2410_WTDAT 0x04 > @@ -410,9 +411,14 @@ static inline unsigned long s3c2410wdt_get_freq(struct s3c2410_wdt *wdt) > static inline unsigned int s3c2410wdt_max_timeout(struct s3c2410_wdt *wdt) > { > const unsigned long freq = s3c2410wdt_get_freq(wdt); > + const u64 n_max = (u64)(S3C2410_WTCON_PRESCALE_MAX + 1) * > + S3C2410_WTCON_MAXDIV * S3C2410_WTCNT_MAXCNT; > + u64 t_max = div64_ul(n_max, freq); > > - return S3C2410_WTCNT_MAXCNT / (freq / (S3C2410_WTCON_PRESCALE_MAX + 1) > - / S3C2410_WTCON_MAXDIV); > + if (t_max > UINT_MAX) > + t_max = UINT_MAX; > + > + return t_max; > } > Thanks for working on this, Sangwook! It looks much better now. > static int s3c2410wdt_disable_wdt_reset(struct s3c2410_wdt *wdt, bool mask) > -- > 2.25.1 >
© 2016 - 2025 Red Hat, Inc.