Skip to content

Commit eb8305a

Browse files
mettiJessica Yu
authored andcommitted
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 <[email protected]> Signed-off-by: Martijn Coenen <[email protected]> Acked-by: Julia Lawall <[email protected]> Reviewed-by: Greg Kroah-Hartman <[email protected]> Signed-off-by: Matthias Maennich <[email protected]> Signed-off-by: Jessica Yu <[email protected]>
1 parent 1d08277 commit eb8305a

File tree

5 files changed

+101
-1
lines changed

5 files changed

+101
-1
lines changed

MAINTAINERS

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11436,6 +11436,11 @@ S: Maintained
1143611436
T: git git://git.kernel.org/pub/scm/linux/kernel/git/wtarreau/nolibc.git
1143711437
F: tools/include/nolibc/
1143811438

11439+
NSDEPS
11440+
M: Matthias Maennich <[email protected]>
11441+
S: Maintained
11442+
F: scripts/nsdeps
11443+
1143911444
NTB AMD DRIVER
1144011445
M: Shyam Sundar S K <[email protected]>
1144111446

Makefile

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1500,6 +1500,9 @@ help:
15001500
@echo ' headerdep - Detect inclusion cycles in headers'
15011501
@echo ' coccicheck - Check with Coccinelle'
15021502
@echo ''
1503+
@echo 'Tools:'
1504+
@echo ' nsdeps - Generate missing symbol namespace dependencies'
1505+
@echo ''
15031506
@echo 'Kernel selftest:'
15041507
@echo ' kselftest - Build and run kernel selftest (run as root)'
15051508
@echo ' Build, install, and boot kernel before'
@@ -1687,6 +1690,15 @@ quiet_cmd_tags = GEN $@
16871690
tags TAGS cscope gtags: FORCE
16881691
$(call cmd,tags)
16891692

1693+
# Script to generate missing namespace dependencies
1694+
# ---------------------------------------------------------------------------
1695+
1696+
PHONY += nsdeps
1697+
1698+
nsdeps: modules
1699+
$(Q)$(MAKE) -f $(srctree)/scripts/Makefile.modpost nsdeps
1700+
$(Q)$(CONFIG_SHELL) $(srctree)/scripts/$@
1701+
16901702
# Scripts to check various things for consistency
16911703
# ---------------------------------------------------------------------------
16921704

scripts/Makefile.modpost

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,8 @@ MODPOST = scripts/mod/modpost \
5656
$(if $(KBUILD_EXTMOD),$(addprefix -e ,$(KBUILD_EXTRA_SYMBOLS))) \
5757
$(if $(KBUILD_EXTMOD),-o $(modulesymfile)) \
5858
$(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)
6061

6162
ifdef MODPOST_VMLINUX
6263

@@ -134,6 +135,7 @@ $(modules): %.ko :%.o %.mod.o FORCE
134135

135136
targets += $(modules)
136137

138+
nsdeps: __modpost
137139

138140
# Add FORCE to the prequisites of a target to force it to be always rebuilt.
139141
# ---------------------------------------------------------------------------
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
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);

scripts/nsdeps

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
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+

0 commit comments

Comments
 (0)