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

Anna-Maria Behnsen posted 15 patches 2 months, 2 weeks ago
There is a newer version of this series
[PATCH v2 09/15] timers: Add a warning to usleep_range_state() for wrong order of arguments
Posted by Anna-Maria Behnsen 2 months, 2 weeks 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. Move this check into the code as
a WARN_ON_ONCE() to also cover callsites not using constants and get rid of
it in checkpatch.

Cc: Andy Whitcroft <apw@canonical.com>
Cc: Joe Perches <joe@perches.com>
Signed-off-by: Anna-Maria Behnsen <anna-maria@linutronix.de>
---
 kernel/time/sleep_timeout.c | 2 ++
 scripts/checkpatch.pl       | 4 ----
 2 files changed, 2 insertions(+), 4 deletions(-)

diff --git a/kernel/time/sleep_timeout.c b/kernel/time/sleep_timeout.c
index 21f412350b15..4b805d7e1903 100644
--- a/kernel/time/sleep_timeout.c
+++ b/kernel/time/sleep_timeout.c
@@ -364,6 +364,8 @@ 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;
 
+	WARN_ON_ONCE(max < min);
+
 	for (;;) {
 		__set_current_state(state);
 		/* Do not return before the requested sleep time has elapsed */
diff --git a/scripts/checkpatch.pl b/scripts/checkpatch.pl
index 39032224d504..ba3359bdd1fa 100755
--- a/scripts/checkpatch.pl
+++ b/scripts/checkpatch.pl
@@ -7088,10 +7088,6 @@ sub process {
 			if ($min eq $max) {
 				WARN("USLEEP_RANGE",
 				     "usleep_range should not use min == max args; see Documentation/timers/timers-howto.rst\n" . "$here\n$stat\n");
-			} elsif ($min =~ /^\d+$/ && $max =~ /^\d+$/ &&
-				 $min > $max) {
-				WARN("USLEEP_RANGE",
-				     "usleep_range args reversed, use min then max; see Documentation/timers/timers-howto.rst\n" . "$here\n$stat\n");
 			}
 		}
 

-- 
2.39.2
Re: [PATCH v2 09/15] timers: Add a warning to usleep_range_state() for wrong order of arguments
Posted by Frederic Weisbecker 1 month, 3 weeks ago
Le Wed, Sep 11, 2024 at 07:13:35AM +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. Move this check into the code as
> a WARN_ON_ONCE() to also cover callsites not using constants and get rid of
> it in checkpatch.
> 
> Cc: Andy Whitcroft <apw@canonical.com>
> Cc: Joe Perches <joe@perches.com>
> Signed-off-by: Anna-Maria Behnsen <anna-maria@linutronix.de>
> ---
>  kernel/time/sleep_timeout.c | 2 ++
>  scripts/checkpatch.pl       | 4 ----
>  2 files changed, 2 insertions(+), 4 deletions(-)
> 
> diff --git a/kernel/time/sleep_timeout.c b/kernel/time/sleep_timeout.c
> index 21f412350b15..4b805d7e1903 100644
> --- a/kernel/time/sleep_timeout.c
> +++ b/kernel/time/sleep_timeout.c
> @@ -364,6 +364,8 @@ 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;
>  
> +	WARN_ON_ONCE(max < min);
> +

Should it try to "fix" to avoid overflow?

if WARN_ON_ONCE(max < min)
    delta = 0

>  	for (;;) {
>  		__set_current_state(state);
>  		/* Do not return before the requested sleep time has elapsed */
> diff --git a/scripts/checkpatch.pl b/scripts/checkpatch.pl
> index 39032224d504..ba3359bdd1fa 100755
> --- a/scripts/checkpatch.pl
> +++ b/scripts/checkpatch.pl
> @@ -7088,10 +7088,6 @@ sub process {
>  			if ($min eq $max) {
>  				WARN("USLEEP_RANGE",
>  				     "usleep_range should not use min == max args; see Documentation/timers/timers-howto.rst\n" . "$here\n$stat\n");
> -			} elsif ($min =~ /^\d+$/ && $max =~ /^\d+$/ &&
> -				 $min > $max) {
> -				WARN("USLEEP_RANGE",
> -				     "usleep_range args reversed, use min then max; see Documentation/timers/timers-howto.rst\n" . "$here\n$stat\n");

Perhaps it doesn't hurt to keep the static check?

Thanks.
>  			}
>  		}
>  
> 
> -- 
> 2.39.2
> 
Re: [PATCH v2 09/15] timers: Add a warning to usleep_range_state() for wrong order of arguments
Posted by Anna-Maria Behnsen 1 month, 2 weeks ago
Frederic Weisbecker <frederic@kernel.org> writes:

> Le Wed, Sep 11, 2024 at 07:13:35AM +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. Move this check into the code as
>> a WARN_ON_ONCE() to also cover callsites not using constants and get rid of
>> it in checkpatch.
>> 
>> Cc: Andy Whitcroft <apw@canonical.com>
>> Cc: Joe Perches <joe@perches.com>
>> Signed-off-by: Anna-Maria Behnsen <anna-maria@linutronix.de>
>> ---
>>  kernel/time/sleep_timeout.c | 2 ++
>>  scripts/checkpatch.pl       | 4 ----
>>  2 files changed, 2 insertions(+), 4 deletions(-)
>> 
>> diff --git a/kernel/time/sleep_timeout.c b/kernel/time/sleep_timeout.c
>> index 21f412350b15..4b805d7e1903 100644
>> --- a/kernel/time/sleep_timeout.c
>> +++ b/kernel/time/sleep_timeout.c
>> @@ -364,6 +364,8 @@ 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;
>>  
>> +	WARN_ON_ONCE(max < min);
>> +
>
> Should it try to "fix" to avoid overflow?
>
> if WARN_ON_ONCE(max < min)
>     delta = 0

yes!

>>  	for (;;) {
>>  		__set_current_state(state);
>>  		/* Do not return before the requested sleep time has elapsed */
>> diff --git a/scripts/checkpatch.pl b/scripts/checkpatch.pl
>> index 39032224d504..ba3359bdd1fa 100755
>> --- a/scripts/checkpatch.pl
>> +++ b/scripts/checkpatch.pl
>> @@ -7088,10 +7088,6 @@ sub process {
>>  			if ($min eq $max) {
>>  				WARN("USLEEP_RANGE",
>>  				     "usleep_range should not use min == max args; see Documentation/timers/timers-howto.rst\n" . "$here\n$stat\n");
>> -			} elsif ($min =~ /^\d+$/ && $max =~ /^\d+$/ &&
>> -				 $min > $max) {
>> -				WARN("USLEEP_RANGE",
>> -				     "usleep_range args reversed, use min then max; see Documentation/timers/timers-howto.rst\n" . "$here\n$stat\n");
>
> Perhaps it doesn't hurt to keep the static check?

I'll keep it but drop the link to the
Documentation/timers/timers-howto.rst which will be removed.

> Thanks.
>>  			}
>>  		}
>>  
>> 
>> -- 
>> 2.39.2
>> 
Re: [PATCH v2 09/15] timers: Add a warning to usleep_range_state() for wrong order of arguments
Posted by Joe Perches 2 months, 2 weeks ago
On Wed, 2024-09-11 at 07:13 +0200, Anna-Maria Behnsen wrote:
> 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. Move this check into the code as
> a WARN_ON_ONCE() to also cover callsites not using constants and get rid of
> it in checkpatch.

I don't disagree that a runtime test is useful
and relatively cost free.

But checkpatch is for patches.

There's no reason as far as I can tell to remove
this source code test.

Why make the test runtime only?


> 
> Cc: Andy Whitcroft <apw@canonical.com>
> Cc: Joe Perches <joe@perches.com>
> Signed-off-by: Anna-Maria Behnsen <anna-maria@linutronix.de>
> ---
>  kernel/time/sleep_timeout.c | 2 ++
>  scripts/checkpatch.pl       | 4 ----

>  2 files changed, 2 insertions(+), 4 deletions(-)
> 
> diff --git a/kernel/time/sleep_timeout.c b/kernel/time/sleep_timeout.c
> index 21f412350b15..4b805d7e1903 100644
> --- a/kernel/time/sleep_timeout.c
> +++ b/kernel/time/sleep_timeout.c
> @@ -364,6 +364,8 @@ 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;
>  
> +	WARN_ON_ONCE(max < min);
> +
>  	for (;;) {
>  		__set_current_state(state);
>  		/* Do not return before the requested sleep time has elapsed */
> diff --git a/scripts/checkpatch.pl b/scripts/checkpatch.pl
> index 39032224d504..ba3359bdd1fa 100755
> --- a/scripts/checkpatch.pl
> +++ b/scripts/checkpatch.pl
> @@ -7088,10 +7088,6 @@ sub process {
>  			if ($min eq $max) {
>  				WARN("USLEEP_RANGE",
>  				     "usleep_range should not use min == max args; see Documentation/timers/timers-howto.rst\n" . "$here\n$stat\n");
> -			} elsif ($min =~ /^\d+$/ && $max =~ /^\d+$/ &&
> -				 $min > $max) {
> -				WARN("USLEEP_RANGE",
> -				     "usleep_range args reversed, use min then max; see Documentation/timers/timers-howto.rst\n" . "$here\n$stat\n");
>  			}
>  		}
>  
> 
Re: [PATCH v2 09/15] timers: Add a warning to usleep_range_state() for wrong order of arguments
Posted by Anna-Maria Behnsen 2 months, 2 weeks ago
Joe Perches <joe@perches.com> writes:

> On Wed, 2024-09-11 at 07:13 +0200, Anna-Maria Behnsen wrote:
>> 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. Move this check into the code as
>> a WARN_ON_ONCE() to also cover callsites not using constants and get rid of
>> it in checkpatch.
>
> I don't disagree that a runtime test is useful
> and relatively cost free.
>
> But checkpatch is for patches.
>
> There's no reason as far as I can tell to remove
> this source code test.
>
> Why make the test runtime only?
>

Sure, we can keep the test in checkpatch as well and I will only add the
runtime check. Then I would change the link to the documentation in
checkpatch into a link to the updated function description. Before I do
any update there, I want to wait for your answer to the other patch of
the queue.

Thanks,

	Anna-Maria