[RFC PATCH 1/1] checkpatch: Add warning for msleep with durations suitable for ssleep

From: Li Chen
Date: Tue Mar 05 2024 - 10:58:55 EST


From: Li Chen <chenl311@xxxxxxxxxxxxxxx>

This patch enhances checkpatch.pl by introducing a warning for instances
where msleep() is used with durations that are multiples of 1000
milliseconds. Such durations are more semantically appropriate for
ssleep().

The goal is to encourage developers to use the most fitting sleep function,
improving code readability.

Warn when msleep(x000); can be replaced with ssleep(x);
Signed-off-by: Li Chen <chenl311@xxxxxxxxxxxxxxx>
---
scripts/checkpatch.pl | 10 ++++++++++
1 file changed, 10 insertions(+)

diff --git a/scripts/checkpatch.pl b/scripts/checkpatch.pl
index 9c4c4a61bc83..a32df4ead5d4 100755
--- a/scripts/checkpatch.pl
+++ b/scripts/checkpatch.pl
@@ -6599,6 +6599,16 @@ sub process {
}
}

+# warn about msleep() calls with durations that should use ssleep()
+if ($line =~ /\bmsleep\s*\((\d+)\);/) {
+ my $ms_duration = $1;
+ if ($ms_duration >= 1000 && ($ms_duration % 1000) == 0) {
+ my $ss_duration = $ms_duration / 1000;
+ WARN("SSLEEP",
+ "Prefer ssleep($ss_duration) over msleep($ms_duration);\n" . $herecurr);
+ }
+}
+
# check for comparisons of jiffies
if ($line =~ /\bjiffies\s*$Compare|$Compare\s*jiffies\b/) {
WARN("JIFFIES_COMPARISON",
--
2.44.0