checkpatch: userspace improvements
v6 cover checkpatch: userspace improvements
Few improvements for user space code + --userspace option for projects which vendored checkpatch.pl.
There could be probably more checks which are kernel space only.
v6:
- Remove 4th commit "checkpatch: Factor out the check for UAPI files" which added is_uapi(), which was wrong. I overlook that regex on BIT() macro is different from the others, matching only ^include/uapi/.
- Fix 5th commit which wrongly called is_uapi() instead of is_userspace().
Link to v5:
https://lore.kernel.org/lkml/[email protected]/
Link to v4:
https://lore.kernel.org/lkml/[email protected]/
Petr Vorel (4):
checkpatch: Add more userspace directories to is_userspace()
checkpatch: Ignore <inttypes.h> format macros for userspace tools
checkpatch: Add --userspace to force userspace rules
checkpatch: Skip kernel specific checks for userspace
scripts/checkpatch.pl | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-)
Signature · 1 line
2.55.0
PATCH v6 1/4 checkpatch: Add more userspace directories to is_userspace()
arch/ directory contains subdirectories with userspace tools (at least arch/*/tools/ and arch/*/boot/tools/). Add check to consider any arch/.*/tools/ subdirectory as userspace tools directory.
This helps not only to strscpy() checks but also to CamelCase checks in the next commit to be more precise.
Follow-up: 99b70ece33d8 ("checkpatch: suppress strscpy warnings for userspace tools")
---
The same as in v5.
scripts/checkpatch.pl | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)
scripts/checkpatch.pl +3 -1
@@ -2667,7 +2667,9 @@ sub exclude_global_initialisers { sub is_userspace { my ($realfile) = @_; - return ($realfile =~ m@^tools/@ || $realfile =~ m@^scripts/@); + return ($realfile =~ m@^tools/@ || + $realfile =~ m@^scripts/@ || + $realfile =~ m@^arch/.*/tools/@); } sub process {
Signature · 1 line
2.55.0
PATCH v6 2/4 checkpatch: Ignore <inttypes.h> format macros for userspace tools
In reply to Petr VorelConstants from <inttypes.h> are used only in userspace tools, they are from ISO C99, let's don't report it:
arch/mips/boot/tools/relocs.c:572: CHECK: Avoid CamelCase: <PRIx32> arch/s390/tools/relocs.c:52: CHECK: Avoid CamelCase: <PRIu64> tools/testing/selftests/mm/vm_util.c:244: CHECK: Avoid CamelCase: <SCNu64>
---
The same as in v5 and earlier versions.
scripts/checkpatch.pl | 2 ++ 1 file changed, 2 insertions(+)
scripts/checkpatch.pl +2 -0
@@ -5950,6 +5950,8 @@ sub process { #Ignore SI style variants like nS, mV and dB #(ie: max_uV, regulator_min_uA_show, RANGE_mA_VALUE) $var !~ /^(?:[a-z0-9_]*|[A-Z0-9_]*)?_?[a-z][A-Z](?:_[a-z0-9_]+|_[A-Z0-9_]+)?$/ && +#Ignore <inttypes.h> format macros (e.g. PRIu64, SCNu64) + (is_userspace($realfile) ? $var !~ /^(?:PRI|SCN)[dioux][A-Z0-9]+$/ : 1) && #Ignore some three character SI units explicitly, like MiB and KHz $var !~ /^(?:[a-z_]*?)_?(?:[KMGT]iB|[KMGT]?Hz)(?:_[a-z_]+)?$/) { while ($var =~ m{\b($Ident)}g) {
Signature · 1 line
2.55.0
PATCH v6 3/4 checkpatch: Add --userspace to force userspace rules
In reply to Petr VorelAlso allow to use --no-userspace for userspace projects which vendored checkpatch.pl and use --userspace globally to be able switch it off for files with kernel code.
---
The same as in v5.
scripts/checkpatch.pl | 6 ++++++ 1 file changed, 6 insertions(+)
scripts/checkpatch.pl +6 -0
@@ -63,6 +63,7 @@ my $env_config_dir = 'CHECKPATCH_CONFIG_DIR'; my $max_line_length = 100; my $ignore_perl_version = 0; my $spdx_cxx_comments = 0; +my $userspace; my $minimum_perl_version = 5.10.0; my $min_conf_desc_length = 4; my $spelling_file = "$D/spelling.txt";
@@ -143,6 +144,7 @@ Options: (required by old toolchains), allow also C++ comments (//). NOTE: it should *not* be used for Linux mainline. + --userspace Force rules specific for userspace. --codespell Use the codespell dictionary for spelling/typos (default:$codespellfile) --codespellfile Use this codespell dictionary
@@ -358,6 +360,7 @@ GetOptions( 'codespell!' => \$codespell, 'codespellfile=s' => \$user_codespellfile, 'typedefsfile=s' => \$typedefsfile, + 'userspace!' => \$userspace, 'color=s' => \$color, 'no-color' => \$color, #keep old behaviors of -nocolor 'nocolor' => \$color, #keep old behaviors of -nocolor
@@ -2667,6 +2670,9 @@ sub exclude_global_initialisers { sub is_userspace { my ($realfile) = @_; + + return $userspace if (defined $userspace); + return ($realfile =~ m@^tools/@ || $realfile =~ m@^scripts/@ || $realfile =~ m@^arch/.*/tools/@);
Signature · 1 line
2.55.0
PATCH v6 4/4 checkpatch: Skip kernel specific checks for userspace
In reply to Petr VorelThese check are kernel specific, do not warn about it when testing userspace code:
* BIT_MACRO
* LONG_UDELAY
* MSLEEP
* PREFER_KERNEL_TYPES
* USLEEP_RANGE
Follow-up: 99b70ece33d8 ("checkpatch: suppress strscpy warnings for userspace tools")
---
v6: Fix: use is_userspace instead of is_uapi().
scripts/checkpatch.pl | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-)
scripts/checkpatch.pl +7 -3
@@ -6696,7 +6696,8 @@ sub process { } # prefer usleep_range over udelay - if ($line =~ /\budelay\s*\(\s*(\d+)\s*\)/) { + if (!is_userspace($realfile) && + $line =~ /\budelay\s*\(\s*(\d+)\s*\)/) { my $delay = $1; # ignore udelay's < 10, however if (! ($delay < 10) ) {
@@ -6710,7 +6711,8 @@ sub process { } # warn about unexpectedly long msleep's - if ($line =~ /\bmsleep\s*\((\d+)\);/) { + if (!is_userspace($realfile) && + $line =~ /\bmsleep\s*\((\d+)\);/) { if ($1 < 20) { WARN("MSLEEP", "msleep < 20ms can sleep for up to 20ms; see function description of msleep().\n" . $herecurr);
@@ -6932,7 +6934,7 @@ sub process { # check for c99 types like uint8_t used outside of uapi/ and tools/ if ($realfile !~ m@\binclude/uapi/@ && - $realfile !~ m@\btools/@ && + !is_userspace($realfile) && $line =~ /\b($Declare)\s*$Ident\s*[=;,\[]/) { my $type = $1; if ($type =~ /\b($typeC99Typedefs)\b/) {
@@ -7182,6 +7184,7 @@ sub process { # check usleep_range arguments if ($perl_version_ok && defined $stat && + !is_userspace($realfile) && $stat =~ /^\+(?:.*?)\busleep_range\s*\(\s*($FuncArg)\s*,\s*($FuncArg)\s*\)/) { my $min = $1; my $max = $7;
@@ -7425,6 +7428,7 @@ sub process { # check for #defines like: 1 << <digit> that could be BIT(digit), it is not exported to uapi if ($realfile !~ m@^include/uapi/@ && + !is_userspace($realfile) && $line =~ /#\s*define\s+\w+\s+\(?\s*1\s*([ulUL]*)\s*\<\<\s*(?:\d+|$Ident)\s*\)?/) { my $ull = ""; $ull = "_ULL" if (defined($1) && $1 =~ /ll/i);
Signature · 1 line
2.55.0
Quoted 4 lines · On Fri, 4 Sep 2026 13:02:26 +0200 Petr Vorel <[email protected]> wrote:
Few improvements for user space code + --userspace option for projects which vendored checkpatch.pl.
There could be probably more checks which are kernel space only.
Thanks. Let me put this into mm.git's mm-nonmm-unstable branch (and hence into linux-next) for testing and additional review.
Quoted 1 line
Follow-up:
99b70ece33d8("checkpatch: suppress strscpy warnings for userspace tools")
Sorry, I'm not a fan of inventing new tags. This isn't a documented tag and nobody has ever used it before. I altered these to
: This is a follow-up to 99b70ece33d8 ("checkpatch: suppress strscpy : warnings for userspace tools").