scripts/misc-check: check unnecessary #include <linux/export.h> when W=1
authorMasahiro Yamada <masahiroy@kernel.org>
Sun, 1 Jun 2025 13:31:30 +0000 (22:31 +0900)
committerMasahiro Yamada <masahiroy@kernel.org>
Thu, 5 Jun 2025 20:40:25 +0000 (05:40 +0900)
Another issue with <linux/export.h> is that it is sometimes included
even when EXPORT_SYMBOL() is not used at all.

Some headers (e.g. include/linux/linkage.h>) cannot be fixed for now
for the reason described in the previous commit.

This commit adds a warning for *.c files that include <linux/export.h>
but do not use EXPORT_SYMBOL() when the kernel is built with W=1.

Signed-off-by: Masahiro Yamada <masahiroy@kernel.org>
scripts/misc-check

index 7cb61841a1254351d536344c4d5add6bfdabec4b..a74450e799d1b6794896352a9b1c0375d50f3229 100755 (executable)
@@ -51,5 +51,17 @@ check_missing_include_linux_export_h () {
        xargs -r printf "%s: warning: EXPORT_SYMBOL() is used, but #include <linux/export.h> is missing\n" >&2
 }
 
+# If you do not use EXPORT_SYMBOL(), please do not include <linux/export.h>.
+# Currently, this is checked for *.c files, but not for *.h files, because some
+# *.c files rely on <linux/export.h> being included indirectly.
+check_unnecessary_include_linux_export_h () {
+
+       git -C "${srctree:-.}" grep --files-with-matches '#include[[:space:]]*<linux/export\.h>' \
+           -- '*.[c]' :^tools/ |
+       xargs -r git -C "${srctree:-.}" grep --files-without-match -E 'EXPORT_SYMBOL((_NS)?(_GPL)?|_GPL_FOR_MODULES)\(.*\)' |
+       xargs -r printf "%s: warning: EXPORT_SYMBOL() is not used, but #include <linux/export.h> is present\n" >&2
+}
+
 check_tracked_ignored_files
 check_missing_include_linux_export_h
+check_unnecessary_include_linux_export_h