[PATCH 01/10] checkpatch: pre-filter obsolete MAINTAINERS checks and gate on OBSOLETE

From: Bart Van Assche

Date: Tue Dec 01 2026 - 17:54:21 EST


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@xxxxxxx>
---
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");
}