Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

scripts: Coccinelle script for namespace dependencies.

A script that uses the '<module>.ns_deps' files generated by modpost to
automatically add the required symbol namespace dependencies to each
module.

Usage:
1) Move some symbols to a namespace with EXPORT_SYMBOL_NS() or define
DEFAULT_SYMBOL_NAMESPACE
2) Run 'make' (or 'make modules') and get warnings about modules not
importing that namespace.
3) Run 'make nsdeps' to automatically add required import statements
to said modules.

This makes it easer for subsystem maintainers to introduce and maintain
symbol namespaces into their codebase.

Co-developed-by: Martijn Coenen <maco@android.com>
Signed-off-by: Martijn Coenen <maco@android.com>
Acked-by: Julia Lawall <julia.lawall@lip6.fr>
Reviewed-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Signed-off-by: Matthias Maennich <maennich@google.com>
Signed-off-by: Jessica Yu <jeyu@kernel.org>

authored by

Matthias Maennich and committed by
Jessica Yu
eb8305ae 1d082773

+101 -1
+5
MAINTAINERS
··· 11436 11436 T: git git://git.kernel.org/pub/scm/linux/kernel/git/wtarreau/nolibc.git 11437 11437 F: tools/include/nolibc/ 11438 11438 11439 + NSDEPS 11440 + M: Matthias Maennich <maennich@google.com> 11441 + S: Maintained 11442 + F: scripts/nsdeps 11443 + 11439 11444 NTB AMD DRIVER 11440 11445 M: Shyam Sundar S K <Shyam-sundar.S-k@amd.com> 11441 11446 L: linux-ntb@googlegroups.com
+12
Makefile
··· 1500 1500 @echo ' headerdep - Detect inclusion cycles in headers' 1501 1501 @echo ' coccicheck - Check with Coccinelle' 1502 1502 @echo '' 1503 + @echo 'Tools:' 1504 + @echo ' nsdeps - Generate missing symbol namespace dependencies' 1505 + @echo '' 1503 1506 @echo 'Kernel selftest:' 1504 1507 @echo ' kselftest - Build and run kernel selftest (run as root)' 1505 1508 @echo ' Build, install, and boot kernel before' ··· 1689 1686 1690 1687 tags TAGS cscope gtags: FORCE 1691 1688 $(call cmd,tags) 1689 + 1690 + # Script to generate missing namespace dependencies 1691 + # --------------------------------------------------------------------------- 1692 + 1693 + PHONY += nsdeps 1694 + 1695 + nsdeps: modules 1696 + $(Q)$(MAKE) -f $(srctree)/scripts/Makefile.modpost nsdeps 1697 + $(Q)$(CONFIG_SHELL) $(srctree)/scripts/$@ 1692 1698 1693 1699 # Scripts to check various things for consistency 1694 1700 # ---------------------------------------------------------------------------
+3 -1
scripts/Makefile.modpost
··· 56 56 $(if $(KBUILD_EXTMOD),$(addprefix -e ,$(KBUILD_EXTRA_SYMBOLS))) \ 57 57 $(if $(KBUILD_EXTMOD),-o $(modulesymfile)) \ 58 58 $(if $(CONFIG_SECTION_MISMATCH_WARN_ONLY),,-E) \ 59 - $(if $(KBUILD_MODPOST_WARN),-w) 59 + $(if $(KBUILD_MODPOST_WARN),-w) \ 60 + $(if $(filter nsdeps,$(MAKECMDGOALS)),-d) 60 61 61 62 ifdef MODPOST_VMLINUX 62 63 ··· 135 134 136 135 targets += $(modules) 137 136 137 + nsdeps: __modpost 138 138 139 139 # Add FORCE to the prequisites of a target to force it to be always rebuilt. 140 140 # ---------------------------------------------------------------------------
+23
scripts/coccinelle/misc/add_namespace.cocci
··· 1 + // SPDX-License-Identifier: GPL-2.0-only 2 + // 3 + /// Adds missing MODULE_IMPORT_NS statements to source files 4 + /// 5 + /// This script is usually called from scripts/nsdeps with -D ns=<namespace> to 6 + /// add a missing namespace tag to a module source file. 7 + /// 8 + 9 + @has_ns_import@ 10 + declarer name MODULE_IMPORT_NS; 11 + identifier virtual.ns; 12 + @@ 13 + MODULE_IMPORT_NS(ns); 14 + 15 + // Add missing imports, but only adjacent to a MODULE_LICENSE statement. 16 + // That ensures we are adding it only to the main module source file. 17 + @do_import depends on !has_ns_import@ 18 + declarer name MODULE_LICENSE; 19 + expression license; 20 + identifier virtual.ns; 21 + @@ 22 + MODULE_LICENSE(license); 23 + + MODULE_IMPORT_NS(ns);
+58
scripts/nsdeps
··· 1 + #!/bin/bash 2 + # SPDX-License-Identifier: GPL-2.0 3 + # Linux kernel symbol namespace import generator 4 + # 5 + # This script requires a minimum spatch version. 6 + SPATCH_REQ_VERSION="1.0.4" 7 + 8 + DIR="$(dirname $(readlink -f $0))/.." 9 + SPATCH="`which ${SPATCH:=spatch}`" 10 + if [ ! -x "$SPATCH" ]; then 11 + echo 'spatch is part of the Coccinelle project and is available at http://coccinelle.lip6.fr/' 12 + exit 1 13 + fi 14 + 15 + SPATCH_REQ_VERSION_NUM=$(echo $SPATCH_REQ_VERSION | ${DIR}/scripts/ld-version.sh) 16 + SPATCH_VERSION=$($SPATCH --version | head -1 | awk '{print $3}') 17 + SPATCH_VERSION_NUM=$(echo $SPATCH_VERSION | ${DIR}/scripts/ld-version.sh) 18 + 19 + if [ "$SPATCH_VERSION_NUM" -lt "$SPATCH_REQ_VERSION_NUM" ] ; then 20 + echo "spatch needs to be version $SPATCH_REQ_VERSION or higher" 21 + exit 1 22 + fi 23 + 24 + generate_deps_for_ns() { 25 + $SPATCH --very-quiet --in-place --sp-file \ 26 + $srctree/scripts/coccinelle/misc/add_namespace.cocci -D ns=$1 $2 27 + } 28 + 29 + generate_deps() { 30 + local mod_name=`basename $@ .ko` 31 + local mod_file=`echo $@ | sed -e 's/\.ko/\.mod/'` 32 + local ns_deps_file=`echo $@ | sed -e 's/\.ko/\.ns_deps/'` 33 + if [ ! -f "$ns_deps_file" ]; then return; fi 34 + local mod_source_files=`cat $mod_file | sed -n 1p \ 35 + | sed -e 's/\.o/\.c/g' \ 36 + | sed "s/[^ ]* */${srctree}\/&/g"` 37 + for ns in `cat $ns_deps_file`; do 38 + echo "Adding namespace $ns to module $mod_name (if needed)." 39 + generate_deps_for_ns $ns $mod_source_files 40 + # sort the imports 41 + for source_file in $mod_source_files; do 42 + sed '/MODULE_IMPORT_NS/Q' $source_file > ${source_file}.tmp 43 + offset=$(wc -l ${source_file}.tmp | awk '{print $1;}') 44 + cat $source_file | grep MODULE_IMPORT_NS | sort -u >> ${source_file}.tmp 45 + tail -n +$((offset +1)) ${source_file} | grep -v MODULE_IMPORT_NS >> ${source_file}.tmp 46 + if ! diff -q ${source_file} ${source_file}.tmp; then 47 + mv ${source_file}.tmp ${source_file} 48 + else 49 + rm ${source_file}.tmp 50 + fi 51 + done 52 + done 53 + } 54 + 55 + for f in `cat $objtree/modules.order`; do 56 + generate_deps $f 57 + done 58 +