checkpatch: suggest fsleep() for short msleep() calls
PATCH 1/1 checkpatch: suggest fsleep() for short msleep() calls
Update the MSLEEP warning to suggest fsleep() as a duration based sleep API. fsleep() autoselects the best sleep mechanism (udelay, usleep_range, or msleep) based on the requested duration, making it the preferred replacement for short msleep() calls.
--- scripts/checkpatch.pl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
scripts/checkpatch.pl +1 -1
@@ -6636,7 +6636,7 @@ sub process { if ($line =~ /\bmsleep\s*\((\d+)\);/) { if ($1 < 20) { WARN("MSLEEP", - "msleep < 20ms can sleep for up to 20ms; see function description of msleep().\n" . $herecurr); + "msleep < 20ms can sleep for up to 20ms; see function description of fsleep().\n" . $herecurr); } }
base-commit: e7aa57247700733e52a8e2e4dee6a52c2a76de02
Signature · 1 line
2.44.0
Joe Perches
maintainerQuoted 4 lines · Neel Bullywon
Update the MSLEEP warning to suggest
fsleep()as a duration based sleep API.fsleep()autoselects the best sleep mechanism (udelay,usleep_range, or msleep) based on the requested duration, making it the preferred replacement for shortmsleep()calls.
[]
scripts/checkpatch.pl
Quoted 1 line
scripts/checkpatch.pl +0
-0
[]
scripts/checkpatch.pl sub process { · line 6641
Quoted 8 lines
scripts/checkpatch.pl +1
-1@@ -6636,7 +6636,7 @@ sub process { if ($line =~ /\bmsleep\s*\((\d+)\);/) { if ($1 < 20) { WARN("MSLEEP", - "msleep < 20ms can sleep for up to 20ms; see function description of msleep().\n" . $herecurr); + "msleep < 20ms can sleep for up to 20ms; see function description of fsleep().\n" . $herecurr); } }
maybe change msleep to a #define macro so if the argument is constant call fsleep instead
Something like:
#define msleep(msecs) \
do { \
if (__builtin_constant_p(msecs) && (msecs) < 20) \
fsleep((msecs) * 1000UL); \
else \
msleep(msecs); \
} while (0)and change the definition of void msleep(unsigned int msecs) to
void (msleep)(unsigned int msecs)
{
etc...
}and remove the checkpatch test.
Quoted 2 lines · Joe Perches
maybe change msleep to a #define macro so if the argument is constant call fsleep instead
Thanks for the suggestion, I'd like to implement this for a v2 but want to clarify a few things.
1. fsleep() is static inline and calls msleep() on line 134 of delay.h. Since inline functions are expanded at the call site, the msleep() macro would expand inside fsleep() at every call site even if the macro is defined after fsleep(). I'd need to change that call to (msleep)() to suppress expansion. Same for ssleep() on line 103. Does that sound right?
2. Should this be a single patch or a small series of patches?
Joe Perches
maintainerQuoted 30 lines · Joe Perches
Quoted 4 lines · Neel Bullywon
Update the MSLEEP warning to suggest
fsleep()as a duration based sleep API.fsleep()autoselects the best sleep mechanism (udelay,usleep_range, or msleep) based on the requested duration, making it the preferred replacement for shortmsleep()calls.[]
scripts/checkpatch.pl
Quoted 1 line
scripts/checkpatch.pl +0
-0[]
scripts/checkpatch.pl sub process { · line 6641
Quoted 8 lines
scripts/checkpatch.pl +1
-1@@ -6636,7 +6636,7 @@ sub process { if ($line =~ /\bmsleep\s*\((\d+)\);/) { if ($1 < 20) { WARN("MSLEEP", - "msleep < 20ms can sleep for up to 20ms; see function description of msleep().\n" . $herecurr); + "msleep < 20ms can sleep for up to 20ms; see function description of fsleep().\n" . $herecurr); } }maybe change msleep to a #define macro so if the argument is constant call fsleep instead
Something like:
#define msleep(msecs) \
do { \ if (__builtin_constant_p(msecs) && (msecs) < 20) \ fsleep((msecs) * 1000UL); \ else \ msleep(msecs); \ } while (0)
Quoted 8 lines
and change the definition of void msleep(unsigned int msecs) to
void (msleep)(unsigned int msecs)
{ etc... }and remove the checkpatch test.
And there are quite a lot of msleep(<20)
$ git grep -Pon '\bmsleep\s*\(\s*(?:\d+)\s*\)' | \
awk -F'(' '{ if (strtonum($2) < 20) print; }' | \
wc -l
1709Quoted 4 lines · Neel Bullywon
Update the MSLEEP warning to suggest
fsleep()as a duration based sleep API.fsleep()autoselects the best sleep mechanism (udelay,usleep_range, or msleep) based on the requested duration, making it the preferred replacement for shortmsleep()calls.
Hmm... My understanding was that the warning itself has a mention of usleep_range(). Looking at the patch I think the current wording is okay, but it may be expanded to suggest fsleep().
Signature · 2 lines
With Best Regards, Andy Shevchenko
Quoted 9 lines · Andy Shevchenko
Quoted 4 lines · Neel Bullywon
Update the MSLEEP warning to suggest
fsleep()as a duration based sleep API.fsleep()autoselects the best sleep mechanism (udelay,usleep_range, or msleep) based on the requested duration, making it the preferred replacement for shortmsleep()calls.Hmm... My understanding was that the warning itself has a mention of
usleep_range(). Looking at the patch I think the current wording is okay, but it may be expanded to suggestfsleep().
Yeah, I would expand this one to make sure we mention fsleep() and update the
# prefer usleep_range over udelay
one to also mention fsleep().
Signature · 2 lines
With Best Regards, Andy Shevchenko