[PATCH v3 09/16] timers: Add a warning to usleep_range_state() for wrong order of arguments

Anna-Maria Behnsen posted 16 patches 1 month, 1 week ago
[PATCH v3 09/16] timers: Add a warning to usleep_range_state() for wrong order of arguments
Posted by Anna-Maria Behnsen 1 month, 1 week ago
There is a warning in checkpatch script that triggers, when min and max
arguments of usleep_range_state() are in reverse order. This check does
only cover callsites which uses constants. Add this check into the code as
a WARN_ON_ONCE() to also cover callsites not using constants and fix the
miss usage by resetting the delta to 0.

Signed-off-by: Anna-Maria Behnsen <anna-maria@linutronix.de>
---
v3: Drop removal of checkpatch check, fix delta value
---
 kernel/time/sleep_timeout.c | 3 +++
 1 file changed, 3 insertions(+)

diff --git a/kernel/time/sleep_timeout.c b/kernel/time/sleep_timeout.c
index f3f246e4c8d1..3054e5232d20 100644
--- a/kernel/time/sleep_timeout.c
+++ b/kernel/time/sleep_timeout.c
@@ -364,6 +364,9 @@ void __sched usleep_range_state(unsigned long min, unsigned long max, unsigned i
 	ktime_t exp = ktime_add_us(ktime_get(), min);
 	u64 delta = (u64)(max - min) * NSEC_PER_USEC;
 
+	if (WARN_ON_ONCE(max < min))
+		delta = 0;
+
 	for (;;) {
 		__set_current_state(state);
 		/* Do not return before the requested sleep time has elapsed */

-- 
2.39.5
Re: [PATCH v3 09/16] timers: Add a warning to usleep_range_state() for wrong order of arguments
Posted by Frederic Weisbecker 1 month, 1 week ago
Le Mon, Oct 14, 2024 at 10:22:26AM +0200, Anna-Maria Behnsen a écrit :
> There is a warning in checkpatch script that triggers, when min and max
> arguments of usleep_range_state() are in reverse order. This check does
> only cover callsites which uses constants. Add this check into the code as
> a WARN_ON_ONCE() to also cover callsites not using constants and fix the
> miss usage by resetting the delta to 0.
> 
> Signed-off-by: Anna-Maria Behnsen <anna-maria@linutronix.de>

Reviewed-by: Frederic Weisbecker <frederic@kernel.org>