From nobody Sun May 24 20:33:21 2026 Received: from out-189.mta0.migadu.com (out-189.mta0.migadu.com [91.218.175.189]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 30D36396B9A for ; Thu, 21 May 2026 20:44:42 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=91.218.175.189 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1779396285; cv=none; b=d6687Cgz9uaG2aO+AysPKj6ciGpDOEy0BikKTha1iMGTQhAWsuqNHa6bi98HRadZ+2YbyuSKA95in2TtYSnywf/4gxs3Dhx5sZ5A0VZXWkcqvvWfYeCnueHrgRQ3/nAetwBMKSeAiCsY+pVWdDvaj6d52mLTQr8iTK7RSo0YM6Q= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1779396285; c=relaxed/simple; bh=E62WoMDMeA6lp5oQKoenHB30TlhusdZNfid/wtZiBhs=; h=From:To:Cc:Subject:Date:Message-ID:MIME-Version; b=c3I4TjUv/Zqfttv9PSHzHnvTiDSdDVdUY8KiwIR5Ew6GWd8fCA8aOxp4Y2VPZhsMUZoKhihy3vgKxvtyFVR6U0UDMn4zj3wd/Rpfvxu00UMdScyIRItK4F+zHjj75fs6myppZ9Ly6k6NzdlVvGFgCwcKzFcw3/WWpS0mZ3MMZ2M= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev; spf=pass smtp.mailfrom=linux.dev; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b=ftm6ejcN; arc=none smtp.client-ip=91.218.175.189 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.dev Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b="ftm6ejcN" X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.dev; s=key1; t=1779396281; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding; bh=7q+/bm/RlhAf61yRZBt6GodYIkGkEo1zXsrnX6ECiLs=; b=ftm6ejcN7ujEwS7C0ioLx0DpgJc+3BBLaTXa8PLESbN13/YigyXp43QA/wjMQjVtp/ELlf Y20jWugyILqQORVODP5XcDn5k1YhMKox36RYBoePsixF4GkxkSev2C6s4EDemgpoGIIXkV eaCGMuZ+Hwxz31n1t9bmtPUEl0+QLGE= From: Andrew Jones To: linux-kbuild@vger.kernel.org, linux-kernel@vger.kernel.org Cc: nathan@kernel.org, nsc@kernel.org, andriy.shevchenko@linux.intel.com, rdunlap@infradead.org, julianbraha@gmail.com Subject: [PATCH v3] kconfig: add kconfig-sym-check static checker Date: Thu, 21 May 2026 15:44:35 -0500 Message-ID: <20260521204435.534773-1-andrew.jones@linux.dev> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Migadu-Flow: FLOW_OUT Content-Type: text/plain; charset="utf-8" Add 'make kconfig-sym-check', a static checker that finds Kconfig symbols referenced in expressions (select, depends on, default, etc.) but never defined via config/menuconfig anywhere in the tree. New dangling symbols are reported as errors (exit 1) unless they are listed in an exclusion file, e.g. KCONFIG_SYM_CHECK_EXCLUDES=3Dsym-check-excludes make kconfig-sym-check The exclusion file lists one symbol per line; blank lines and lines starting with '#' are ignored. The checker also warns about uppercase N/Y/M used as tristate literal values following the same logic as checkpatch. This new static checker is the script used for [1] with a few improvements to avoid some false positives. Link: https://bugzilla.kernel.org/show_bug.cgi?id=3D216748 [1] Assisted-by: Claude:claude-sonnet-4-6 Signed-off-by: Andrew Jones Acked-by: Andy Shevchenko Acked-by: Randy Dunlap Tested-by: Randy Dunlap Tested-by: Julian Braha --- v3: - Filter out scripts/kconfig/tests Kconfig files since they may be wrong on purpose and indeed there is a 'config Y' in there which would mask improper use of 'Y'. [Julian and Randy] - Fixed breakage introduced in v2 when attempting to be too clever... - More changes from another sashiko review which required the Perl to get even uglier. So ugly that I enlisted Claude to help generate it. - Added a sentence to the commit message to describe the excludes file format. v2: - Added Andy's and Randy's tags - Accept srctree as first argument so the Makefile can drop 'cd $(srctree= )' [Nathan] - Replace git ls-files with git+find fallback [Nathan and Andy] - Changes thanks to sashiko's review - strip quoted strings before inline comments to avoid '#' inside a str= ing - use [^)]* instead of .* in macro strip regex to avoid greedy match eating tokens between adjacent $(macro) expansions Makefile | 23 +++-- scripts/kconfig/kconfig-sym-check.pl | 123 +++++++++++++++++++++++++++ 2 files changed, 137 insertions(+), 9 deletions(-) create mode 100755 scripts/kconfig/kconfig-sym-check.pl diff --git a/Makefile b/Makefile index fbc67fcb6cdb..92c8ed867d0d 100644 --- a/Makefile +++ b/Makefile @@ -293,6 +293,7 @@ version_h :=3D include/generated/uapi/linux/version.h clean-targets :=3D %clean mrproper cleandocs no-dot-config-targets :=3D $(clean-targets) \ cscope gtags TAGS tags help% %docs check% coccicheck \ + kconfig-sym-check \ $(version_h) headers headers_% archheaders archscripts \ %asm-generic kernelversion %src-pkg dt_binding_check \ outputmakefile rustavailable rustfmt rustfmtcheck \ @@ -1805,14 +1806,15 @@ help: echo ' (default: $(INSTALL_HDR_PATH))'; \ echo '' @echo 'Static analysers:' - @echo ' checkstack - Generate a list of stack hogs and consider al= l functions' - @echo ' with a stack size larger than MINSTACKSIZE (d= efault: 100)' - @echo ' versioncheck - Sanity check on version.h usage' - @echo ' includecheck - Check for duplicate included header files' - @echo ' headerdep - Detect inclusion cycles in headers' - @echo ' coccicheck - Check with Coccinelle' - @echo ' clang-analyzer - Check with clang static analyzer' - @echo ' clang-tidy - Check with clang-tidy' + @echo ' checkstack - Generate a list of stack hogs and consider = all functions' + @echo ' with a stack size larger than MINSTACKSIZE = (default: 100)' + @echo ' versioncheck - Sanity check on version.h usage' + @echo ' includecheck - Check for duplicate included header files' + @echo ' headerdep - Detect inclusion cycles in headers' + @echo ' coccicheck - Check with Coccinelle' + @echo ' kconfig-sym-check - Check for dangling Kconfig symbol reference= s' + @echo ' clang-analyzer - Check with clang static analyzer' + @echo ' clang-tidy - Check with clang-tidy' @echo '' @echo 'Tools:' @echo ' nsdeps - Generate missing symbol namespace dependencie= s' @@ -2232,7 +2234,7 @@ endif # Scripts to check various things for consistency # ------------------------------------------------------------------------= --- =20 -PHONY +=3D includecheck versioncheck coccicheck +PHONY +=3D includecheck versioncheck coccicheck kconfig-sym-check =20 includecheck: find $(srctree)/* $(RCS_FIND_IGNORE) \ @@ -2247,6 +2249,9 @@ versioncheck: coccicheck: $(Q)$(BASH) $(srctree)/scripts/$@ =20 +kconfig-sym-check: + $(Q)$(PERL) $(srctree)/scripts/kconfig/kconfig-sym-check.pl $(srctree) $(= KCONFIG_SYM_CHECK_EXCLUDES) + PHONY +=3D checkstack kernelrelease kernelversion image_name =20 # UML needs a little special treatment here. It wants to use the host diff --git a/scripts/kconfig/kconfig-sym-check.pl b/scripts/kconfig/kconfig= -sym-check.pl new file mode 100755 index 000000000000..3c3f989c05a7 --- /dev/null +++ b/scripts/kconfig/kconfig-sym-check.pl @@ -0,0 +1,123 @@ +#!/usr/bin/env perl +# SPDX-License-Identifier: GPL-2.0 + +use warnings; +use strict; + +my $srctree =3D shift @ARGV; +my $kconfig_sym_check_excludes =3D defined $ARGV[0] ? $ARGV[0] : undef; + +sub indent_depth { + my ($ws) =3D @_; + my $col =3D 0; + for my $c (split //, $ws) { + $col =3D $c eq "\t" ? int($col / 8) * 8 + 8 : $col + 1; + } + return $col; +} + +my @files =3D `git -C \Q$srctree\E ls-files '*Kconfig*' 2>/dev/null`; +if (@files) { + chomp @files; + @files =3D map { "$srctree/$_" } @files; +} else { + @files =3D `find \Q$srctree\E -name '*Kconfig*'`; + chomp @files; +} + +@files =3D grep { !m{/scripts/kconfig/tests/} } @files; + +my %configs =3D (); +my %refs =3D (); + +foreach my $file (@files) { + open F, $file or die "Cannot open $file: $!"; + + my $help =3D 0; + my $help_level; + my $level; + + while () { + chomp; + + while (/\\\s*$/) { + s/\\\s*$/ /; + my $cont =3D // last; + chomp $cont; + $_ .=3D $cont; + } + + next if /^\s*$/; + next if /^\s*#/; + + /^(\s*)/; + $level =3D indent_depth($1); + + if ($help && $level < $help_level) { + $help =3D 0; + } + + next if ($help); + + if (/^\s*(help|\-\-\-help\-\-\-)$/) { + $help =3D 1; + my $next; + while (defined($next =3D )) { + last unless $next =3D~ /^\s*(?:#.*)?$/; + } + last unless defined $next; + $next =3D~ /^(\s*)/; + if (indent_depth($1) >=3D $level) { + $help_level =3D indent_depth($1); + } else { + $help =3D 0; + } + $_ =3D $next; + redo; + } + + if (/^\s*(config|menuconfig)\s+([a-zA-Z0-9_]+)\s*(#.*)?$/) { + $configs{$2}++; + next; + } + + if (/^\s*(default|def_bool|def_tristate|select|depends\s+on|imply|visibl= e\s+if|range|if|bool|tristate|int|hex|string|prompt)\s+(.+)\s*$/) { + my $s =3D $2; + $s =3D~ s/"(?:[^"\\]|\\.)*"|'(?:[^'\\]|\\.)*'//g; + $s =3D~ s/#.*//; + $s =3D~ s/\$\(.*\)//g; + $s =3D~ s/%%[^%]*%%//g; + my @syms =3D split /[^a-zA-Z0-9_]+/, $s; + map { + $refs{$_}++ if (/[a-zA-Z]/ && $_ ne "if" && $_ ne "y" && $_ ne "n" && = $_ ne "m" && !/^0[xX][0-9a-fA-F]+$/); + } @syms + } + } + + close F; +} + +my %known_syms =3D (); +if (defined $kconfig_sym_check_excludes) { + my $file =3D $kconfig_sym_check_excludes; + open(F, "<", $file) or die "Cannot open $file: $!"; + while () { + chomp; + next if /^\s*$/; + next if /^\s*#/; + $known_syms{$1}++ if (/^\s*([a-zA-Z0-9_]+)\s*(#.*)?$/); + } +} + +my $ret =3D 0; +foreach my $k (sort keys %refs) { + next if (exists $configs{$k} || exists $known_syms{$k}); + + print "$k"; + print " - warning: '$k' is probably not what you want; Kconfig tristate l= iterals are always lowercase ('n', 'y', 'm')" if ($k eq "N" || $k eq "Y" ||= $k eq "M"); + print "\n"; + + $ret =3D 1; +} + +exit $ret; --=20 2.43.0