mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Bart Van Assche <bvanassche@acm.org>
To: Joe Perches <joe@perches.com>, Andy Whitcroft <apw@canonical.com>
Cc: Andrew Morton <akpm@linux-foundation.org>,
	linux-kernel@vger.kernel.org,
	Bart Van Assche <bvanassche@acm.org>
Subject: [PATCH 01/10] checkpatch: pre-filter obsolete MAINTAINERS checks and gate on OBSOLETE
Date: Tue,  1 Dec 2026 14:53:45 -0800	[thread overview]
Message-ID: <059159036efcda969eb16477ae36266b4feab5ce.1796165270.git.bvanassche@acm.org> (raw)
In-Reply-To: <cover.1796165270.git.bvanassche@acm.org>

Pre-filter candidate paths in is_maintained_obsolete() by scanning
MAINTAINERS once via load_obsolete_candidates() for sections whose S: line
matches obsolete, and gate the call site in process() on
show_type("OBSOLETE").

Avoid spawning perl scripts/get_maintainer.pl (~210 ms per unique file)
for paths that cannot match any obsolete F: or N: entry and when OBSOLETE
warnings are suppressed via --ignore or --types. Fall back to invoking
scripts/get_maintainer.pl whenever MAINTAINERS is not a regular file, a
.get_maintainer.conf file is present, an obsolete section contains K: or
bracket/brace globs, or an N: regex fails to compile.

Signed-off-by: Bart Van Assche <bvanassche@acm.org>
---
 scripts/checkpatch.pl | 144 +++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 142 insertions(+), 2 deletions(-)

diff --git a/scripts/checkpatch.pl b/scripts/checkpatch.pl
index b8b64f720191..543fd002ee29 100755
--- a/scripts/checkpatch.pl
+++ b/scripts/checkpatch.pl
@@ -11,7 +11,8 @@ use strict;
 use warnings;
 use POSIX;
 use File::Basename;
-use Cwd 'abs_path';
+use Cwd qw(abs_path fastgetcwd);
+use File::Spec::Functions qw(canonpath);
 use Term::ANSIColor qw(:constants);
 use Encode qw(decode encode);
 
@@ -1170,6 +1171,121 @@ sub seed_camelcase_file {
 }
 
 our %maintained_status = ();
+my $obsolete_candidates_loaded = 0;
+my $obsolete_candidates_fallback = 0;
+my @obsolete_candidates = ();
+
+sub flush_obsolete_section {
+	my ($header, $is_obsolete, $has_r, $r_obsolete, $has_k, $fn_ref) = @_;
+
+	return 1 if (!$is_obsolete && !($has_r && ($r_obsolete || $header =~ /obsolete/i)));
+	return 0 if ($has_k);
+
+	foreach my $entry (@$fn_ref) {
+		my ($tag, $pat) = @$entry;
+		return 0 if ($pat eq '');
+		if ($tag eq 'N') {
+			return 0 if ($pat =~ /[^\x00-\x7f]/);
+			local $@;
+			my $nre = eval { no warnings 'regexp'; qr{$pat}x };
+			return 0 if ($@);
+			push(@obsolete_candidates, $nre);
+		} else {
+			return 0 if ($pat =~ /[\[\{\(\)\|\+\\\^\$\x00]/ ||
+				     $pat =~ /[^\x00-\x7f]/);
+			my $re = quotemeta($pat);
+			$re =~ s/\\\*/.*/g;
+			$re =~ s/\\\?/./g;
+			push(@obsolete_candidates, qr{^$re});
+		}
+	}
+	return 1;
+}
+
+sub load_obsolete_candidates {
+	return if ($obsolete_candidates_loaded);
+	$obsolete_candidates_loaded = 1;
+
+	my $cwd = abs_path('.');
+	my $root_abs = abs_path($root);
+	my $home = defined($ENV{HOME}) ? $ENV{HOME} : "";
+	my $has_conf = grep { -e "$_/.get_maintainer.conf" }
+			    split(/:/, ".:$home:.scripts");
+	if (!defined($cwd) || !defined($root_abs) || $cwd ne $root_abs ||
+	    $root =~ /obsolete/i ||
+	    !(-f "MAINTAINERS") ||
+	    $has_conf) {
+		$obsolete_candidates_fallback = 1;
+		return;
+	}
+
+	if (-f ".mailmap") {
+		open(my $mm, '<', ".mailmap") or do {
+			$obsolete_candidates_fallback = 1;
+			return;
+		};
+		while (my $line = <$mm>) {
+			$line =~ s/#.*$//;
+			if ($line =~ /obsolete/i) {
+				$obsolete_candidates_fallback = 1;
+				close($mm);
+				return;
+			}
+		}
+		close($mm);
+	}
+
+	# Decode as UTF-8 like get_maintainer.pl does such that \s matches
+	# the same (Unicode) whitespace characters.
+	open(my $maint, '<:encoding(UTF-8)', "MAINTAINERS") or do {
+		$obsolete_candidates_fallback = 1;
+		return;
+	};
+
+	my $header = '';
+	my $is_obsolete = 0;
+	my $has_r = 0;
+	my $r_obsolete = 0;
+	my $has_k = 0;
+	my @fn = ();
+
+	no warnings 'utf8';
+	while (my $line = <$maint>) {
+		chomp $line;
+		if ($line =~ /^([A-Z]):\s*(.*)/) {
+			my ($tag, $val) = ($1, $2);
+			if ($tag eq 'S') {
+				$is_obsolete = 1 if ($val =~ /obsolete/i);
+			} elsif ($tag eq 'R') {
+				$has_r = 1;
+				$r_obsolete = 1 if ($val =~ /obsolete/i);
+			} elsif ($tag eq 'K') {
+				$has_k = 1;
+			} elsif ($tag eq 'F' || $tag eq 'N') {
+				push(@fn, [$tag, $val]);
+			}
+		} elsif ($line !~ /^\s*$/ && $line !~ /^\s*\#/) {
+			if (!flush_obsolete_section($header, $is_obsolete, $has_r,
+						    $r_obsolete, $has_k, \@fn)) {
+				$obsolete_candidates_fallback = 1;
+				close($maint);
+				return;
+			}
+			$header = $line;
+			$is_obsolete = 0;
+			$has_r = 0;
+			$r_obsolete = 0;
+			$has_k = 0;
+			@fn = ();
+		}
+	}
+	close($maint);
+
+	if (!flush_obsolete_section($header, $is_obsolete, $has_r,
+				    $r_obsolete, $has_k, \@fn)) {
+		$obsolete_candidates_fallback = 1;
+	}
+}
 
 sub is_maintained_obsolete {
 	my ($filename) = @_;
@@ -1177,6 +1293,30 @@ sub is_maintained_obsolete {
 	return 0 if (!$tree || !(-e "$root/scripts/get_maintainer.pl"));
 
 	if (!exists($maintained_status{$filename})) {
+		load_obsolete_candidates();
+		if (!$obsolete_candidates_fallback &&
+		    $filename =~ m{^[A-Za-z0-9_/.+,:\@=%][A-Za-z0-9_/.+,:\-\@=%]*\z} &&
+		    $filename !~ /obsolete/i) {
+			my $norm = canonpath($filename);
+			if ($norm !~ m@(?:^|/)\.\.(?:/|$)@ &&
+			    $norm !~ /\.yaml$/i) {
+				$norm .= '/' if (-d $norm && $norm !~ m@/$@);
+				my $cur_path = fastgetcwd() . '/';
+				$norm =~ s/^\Q$cur_path\E//;
+				$norm =~ s@^\./@@;
+				my $matched = 0;
+				foreach my $re (@obsolete_candidates) {
+					if ($norm =~ $re) {
+						$matched = 1;
+						last;
+					}
+				}
+				if (!$matched) {
+					$maintained_status{$filename} = "";
+					return 0;
+				}
+			}
+		}
 		$maintained_status{$filename} = `perl $root/scripts/get_maintainer.pl --status --nom --nol --nogit --nogit-fallback -f $filename 2>&1`;
 	}
 
@@ -2945,7 +3085,7 @@ sub process {
 		}
 
 		if ($found_file) {
-			if (is_maintained_obsolete($realfile)) {
+			if (show_type("OBSOLETE") && is_maintained_obsolete($realfile)) {
 				WARN("OBSOLETE",
 				     "$realfile is marked as 'obsolete' in the MAINTAINERS hierarchy.  No unnecessary modifications please.\n");
 			}

  reply	other threads:[~2026-12-01 22:54 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-01 22:53 [PATCH 00/10] Make checkpatch faster Bart Van Assche
2026-12-01 22:53 ` Bart Van Assche [this message]
2026-12-01 22:53 ` [PATCH 02/10] checkpatch: cache SPDX license checks and gate on SPDX_LICENSE_TAG Bart Van Assche
2026-12-01 22:53 ` [PATCH 03/10] checkpatch: cache git and PATH lookups and skip git_is_single_file() Bart Van Assche
2026-12-01 22:53 ` [PATCH 04/10] checkpatch: skip redundant build_types() rebuilds Bart Van Assche
2026-12-01 22:53 ` [PATCH 05/10] checkpatch: avoid dynamic regex recompilation in hot checks Bart Van Assche
2026-12-01 22:53 ` [PATCH 06/10] checkpatch: guard expensive per-line regexes with cheap pre-checks Bart Van Assche
2026-12-01 22:53 ` [PATCH 07/10] checkpatch: use hash lookups for single-word spelling checks Bart Van Assche
2026-12-01 22:53 ` [PATCH 08/10] checkpatch: scan ctx_statement_block() in place without slicing Bart Van Assche
2026-12-01 22:53 ` [PATCH 09/10] checkpatch: scan annotate_values() in place with leading-char dispatch Bart Van Assche
2026-12-01 22:53 ` [PATCH 10/10] checkpatch: add fast paths to per-line character-scanning helpers Bart Van Assche
2026-12-01 23:26 ` [PATCH 00/10] Make checkpatch faster Joe Perches
2026-12-01 23:53   ` Bart Van Assche

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=059159036efcda969eb16477ae36266b4feab5ce.1796165270.git.bvanassche@acm.org \
    --to=bvanassche@acm.org \
    --cc=akpm@linux-foundation.org \
    --cc=apw@canonical.com \
    --cc=joe@perches.com \
    --cc=linux-kernel@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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®