mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH v2] checkpatch: Add context-analysis attributes
@ 2026-11-16 17:28 Vincent Donnefort
  2026-11-16 21:31 ` Marco Elver
  0 siblings, 1 reply; 2+ messages in thread
From: Vincent Donnefort @ 2026-11-16 17:28 UTC (permalink / raw)
  To: apw, joe, akpm
  Cc: dwaipayanray1, elver, linux-kernel, vdonnefort, kernel-team, mkl

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@google.com>
Signed-off-by: Vincent Donnefort <vdonnefort@google.com>
---
 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


^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: [PATCH v2] checkpatch: Add context-analysis attributes
  2026-11-16 17:28 [PATCH v2] checkpatch: Add context-analysis attributes Vincent Donnefort
@ 2026-11-16 21:31 ` Marco Elver
  0 siblings, 0 replies; 2+ messages in thread
From: Marco Elver @ 2026-11-16 21:31 UTC (permalink / raw)
  To: Vincent Donnefort
  Cc: apw, joe, akpm, dwaipayanray1, linux-kernel, kernel-team, mkl

On Mon, 16 Nov 2026 at 18:28, Vincent Donnefort <vdonnefort@google.com> wrote:
>
> 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@google.com>
> Signed-off-by: Vincent Donnefort <vdonnefort@google.com>

Acked-by: Marco Elver <elver@google.com>

> ---
>  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
>

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2026-11-16 21:32 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-16 17:28 [PATCH v2] checkpatch: Add context-analysis attributes Vincent Donnefort
2026-11-16 21:31 ` Marco Elver

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®