Skip to contentSkip to conversation

watchdog: Use DIV_ROUND_UP() instead of open-coding it

PATCH In review Lars-Peter Clausen 2 participants 2 messages Mar 20, 2021

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 watchdog: Use DIV_ROUND_UP() instead of open-coding it

Use DIV_ROUND_UP() instead of open-coding it. This makes it more clear what is going on for the casual reviewer.

Generated using the following the Coccinelle semantic patch.

// <smpl>
@r1@
expression x;
constant C1;
constant C2;
@@
((x) + C1) / C2

@script:python@
C1 << r1.C1;
C2 << r1.C2;
@@
print C1, C2
try:

	if int(C1) != int(C2) - 1 or int(C1) == 1:
		cocci.include_match(False)

except:
cocci.include_match(False)

@@
expression r1.x;
constant r1.C1;
constant r1.C2;
@@
-(((x) + C1) / C2)
+DIV_ROUND_UP(x, C2)
// </smpl>

---
 drivers/watchdog/riowd.c       | 2 +-
 drivers/watchdog/w83977f_wdt.c | 2 +-
 drivers/watchdog/wdrtas.c      | 2 +-
 drivers/watchdog/wdt977.c      | 2 +-
 4 files changed, 4 insertions(+), 4 deletions(-)
drivers/watchdog/riowd.c +1 -1
index 7008596a575f..aa8a929505b5 100644
@@ -132,7 +132,7 @@ static long riowd_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
 			return -EFAULT;
 		if ((new_margin < 60) || (new_margin > (255 * 60)))
 			return -EINVAL;
-		riowd_timeout = (new_margin + 59) / 60;
+		riowd_timeout = DIV_ROUND_UP(new_margin, 60);
 		riowd_writereg(p, riowd_timeout, WDTO_INDEX);
 		fallthrough;
 
drivers/watchdog/w83977f_wdt.c +1 -1
index fd64ae77780a..fbc31c4a1e0b 100644
@@ -231,7 +231,7 @@ static int wdt_set_timeout(int t)
 	if (t < 15)
 		return -EINVAL;
 
-	tmrval = ((t + 15) + 29) / 30;
+	tmrval = DIV_ROUND_UP(t + 15, 30);
 
 	if (tmrval > 255)
 		return -EINVAL;
drivers/watchdog/wdrtas.c +1 -1
index c00627825de8..0c21ee535a74 100644
@@ -75,7 +75,7 @@ static int wdrtas_set_interval(int interval)
 	static int print_msg = 10;
 
 	/* rtas uses minutes */
-	interval = (interval + 59) / 60;
+	interval = DIV_ROUND_UP(interval, 60);
 
 	result = rtas_call(wdrtas_token_set_indicator, 3, 1, NULL,
 			   WDRTAS_SURVEILLANCE_IND, 0, interval);
drivers/watchdog/wdt977.c +1 -1
index c9b8e863f70f..7cff1300be3e 100644
@@ -201,7 +201,7 @@ static int wdt977_set_timeout(int t)
 	int tmrval;
 
 	/* convert seconds to minutes, rounding up */
-	tmrval = (t + 59) / 60;
+	tmrval = DIV_ROUND_UP(t, 60);
 
 	if (machine_is_netwinder()) {
 		/* we have a hw bug somewhere, so each 977 minute is actually
Signature · 1 line
2.20.1
maintainer
#1 lore raw
Quoted 3 lines · Lars-Peter Clausen

Use DIV_ROUND_UP() instead of open-coding it. This makes it more clear what is going on for the casual reviewer.

Those are all old watchdog drivers, which are not worth cleaning up.

As I said before, those drivers should only be touched to either fix a real bug, to convert them to new style watchdog drivers, or to drop them.

Thanks,
Guenter

Quoted 91 lines

Generated using the following the Coccinelle semantic patch.

// <smpl>
@r1@
expression x;
constant C1;
constant C2;
@@
((x) + C1) / C2

@script:python@
C1 << r1.C1;
C2 << r1.C2;
@@
print C1, C2
try:

	if int(C1) != int(C2) - 1 or int(C1) == 1:
		cocci.include_match(False)

except:
cocci.include_match(False)

@@
expression r1.x;
constant r1.C1;
constant r1.C2;
@@

(quoted diff) +1 -1
-(((x) + C1) / C2)
+DIV_ROUND_UP(x, C2)

// </smpl>

---
 drivers/watchdog/riowd.c       | 2 +-
 drivers/watchdog/w83977f_wdt.c | 2 +-
 drivers/watchdog/wdrtas.c      | 2 +-
 drivers/watchdog/wdt977.c      | 2 +-
 4 files changed, 4 insertions(+), 4 deletions(-)
drivers/watchdog/riowd.c +1 -1
index 7008596a575f..aa8a929505b5 100644
@@ -132,7 +132,7 @@ static long riowd_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
 			return -EFAULT;
 		if ((new_margin < 60) || (new_margin > (255 * 60)))
 			return -EINVAL;
-		riowd_timeout = (new_margin + 59) / 60;
+		riowd_timeout = DIV_ROUND_UP(new_margin, 60);
 		riowd_writereg(p, riowd_timeout, WDTO_INDEX);
 		fallthrough;
 
drivers/watchdog/w83977f_wdt.c +1 -1
index fd64ae77780a..fbc31c4a1e0b 100644
@@ -231,7 +231,7 @@ static int wdt_set_timeout(int t)
 	if (t < 15)
 		return -EINVAL;
 
-	tmrval = ((t + 15) + 29) / 30;
+	tmrval = DIV_ROUND_UP(t + 15, 30);
 
 	if (tmrval > 255)
 		return -EINVAL;
drivers/watchdog/wdrtas.c +1 -1
index c00627825de8..0c21ee535a74 100644
@@ -75,7 +75,7 @@ static int wdrtas_set_interval(int interval)
 	static int print_msg = 10;
 
 	/* rtas uses minutes */
-	interval = (interval + 59) / 60;
+	interval = DIV_ROUND_UP(interval, 60);
 
 	result = rtas_call(wdrtas_token_set_indicator, 3, 1, NULL,
 			   WDRTAS_SURVEILLANCE_IND, 0, interval);
drivers/watchdog/wdt977.c +1 -1
index c9b8e863f70f..7cff1300be3e 100644
@@ -201,7 +201,7 @@ static int wdt977_set_timeout(int t)
 	int tmrval;
 
 	/* convert seconds to minutes, rounding up */
-	tmrval = (t + 59) / 60;
+	tmrval = DIV_ROUND_UP(t, 60);
 
 	if (machine_is_netwinder()) {
 		/* we have a hw bug somewhere, so each 977 minute is actually