[PATCH v2] checkpatch: Add context-analysis attributes

From: Vincent Donnefort

Date: Mon Nov 16 2026 - 12:28:49 EST


Checkpatch doesn't know about the context-analysis attributes and
misparses annotated declarations (e.g. mistaking __guarded_by() fields
for extern prototypes). Add them to the known attributes list with
support for a single nested set of parentheses.

Handle context-analysis attributes with parentheses like __attribute__
in annotate_values() so unary '&' in their arguments is not mistaken for
binary bitwise AND.

Cc: Marco Elver <elver@xxxxxxxxxx>
Signed-off-by: Vincent Donnefort <vdonnefort@xxxxxxxxxx>
---
scripts/checkpatch.pl | 37 ++++++++++++++++++++++++++++++++-----
1 file changed, 32 insertions(+), 5 deletions(-)

diff --git a/scripts/checkpatch.pl b/scripts/checkpatch.pl
index b8b64f720191..5ca2a1fc2607 100755
--- a/scripts/checkpatch.pl
+++ b/scripts/checkpatch.pl
@@ -527,6 +527,32 @@ our $InitAttributeConst = qr{$InitAttributePrefix(?:initconst\b)};
our $InitAttributeInit = qr{$InitAttributePrefix(?:init\b)};
our $InitAttribute = qr{$InitAttributeData|$InitAttributeConst|$InitAttributeInit};

+# Match (...) attribute arguments, allowing one level of nested parens
+# (e.g. __guarded_by(__bitlock(3, &bits))).
+our $AttrParens = qr{\s*\((?:[^()]++|\([^()]*+\))*+\)};
+our $ContextAnalysisWithArgs = qr{
+ __guarded_by|
+ __pt_guarded_by|
+ __must_hold|
+ __must_hold_shared|
+ __must_not_hold|
+ __acquires|
+ __acquires_shared|
+ __cond_acquires|
+ __cond_acquires_shared|
+ __releases|
+ __releases_shared|
+ __cond_releases|
+ __context_unsafe
+ }x;
+our $ContextAnalysis = qr{
+ (?:$ContextAnalysisWithArgs)$AttrParens|
+ __rcu_guarded\b|
+ __no_context_analysis\b|
+ __acquires_ret\b|
+ __acquires_shared_ret\b
+ }x;
+
# Notes to $Attribute:
# We need \b after 'init' otherwise 'initconst' will cause a false positive in a check
our $Attribute = qr{
@@ -551,6 +577,7 @@ our $Attribute = qr{
__ro_after_init|
__kprobes|
$InitAttribute|
+ $ContextAnalysis|
__aligned\s*\(.*\)|
____cacheline_aligned|
____cacheline_aligned_in_smp|
@@ -2141,6 +2168,11 @@ sub annotate_values {
push(@av_paren_type, $type);
$type = 'c';

+ } elsif ($cur =~ /^(__attribute__|$ContextAnalysisWithArgs)\b\s*\(?/o) {
+ print "ATTR($1)\n" if ($dbg_values > 1);
+ $av_pending = $type;
+ $type = 'N';
+
} elsif ($cur =~ /^($Type)\s*(?:$Ident|,|\)|\(|\s*$)/) {
print "DECLARE($1)\n" if ($dbg_values > 1);
$type = 'T';
@@ -2193,11 +2225,6 @@ sub annotate_values {
} elsif ($cur =~ /^(\\\n)/o) {
print "PRECONT($1)\n" if ($dbg_values > 1);

- } elsif ($cur =~ /^(__attribute__)\s*\(?/o) {
- print "ATTR($1)\n" if ($dbg_values > 1);
- $av_pending = $type;
- $type = 'N';
-
} elsif ($cur =~ /^(sizeof)\s*(\()?/o) {
print "SIZEOF($1)\n" if ($dbg_values > 1);
if (defined $2) {

base-commit: 6628e7d4c6b25c8d18f1345e3a20d21829aae7e4
--
2.56.1.703.gf848ed807c-goog