Skip to content

Commit b7dca6d

Browse files
committed
kbuild: create *.mod with full directory path and remove MODVERDIR
While descending directories, Kbuild produces objects for modules, but do not link final *.ko files; it is done in the modpost. To keep track of modules, Kbuild creates a *.mod file in $(MODVERDIR) for every module it is building. Some post-processing steps read the necessary information from *.mod files. This avoids descending into directories again. This mechanism was introduced in 2003 or so. Later, commit 551559e ("kbuild: implement modules.order") added modules.order. So, we can simply read it out to know all the modules with directory paths. This is easier than parsing the first line of *.mod files. $(MODVERDIR) has a flat directory structure, that is, *.mod files are named only with base names. This is based on the assumption that the module name is unique across the tree. This assumption is really fragile. Stephen Rothwell reported a race condition caused by a module name conflict: https://lkml.org/lkml/2019/5/13/991 In parallel building, two different threads could write to the same $(MODVERDIR)/*.mod simultaneously. Non-unique module names are the source of all kind of troubles, hence commit 3a48a91 ("kbuild: check uniqueness of module names") introduced a new checker script. However, it is still fragile in the build system point of view because this race happens before scripts/modules-check.sh is invoked. If it happens again, the modpost will emit unclear error messages. To fix this issue completely, create *.mod with full directory path so that two threads never attempt to write to the same file. $(MODVERDIR) is no longer needed. Since modules with directory paths are listed in modules.order, Kbuild is still able to find *.mod files without additional descending. I also killed cmd_secanalysis; scripts/mod/sumversion.c computes MD4 hash for modules with MODULE_VERSION(). When CONFIG_DEBUG_SECTION_MISMATCH=y, it occurs not only in the modpost stage, but also during directory descending, where sumversion.c may parse stale *.mod files. It would emit 'No such file or directory' warning when an object consisting a module is renamed, or when a single-obj module is turned into a multi-obj module or vice versa. Signed-off-by: Masahiro Yamada <yamada.masahiro@socionext.com> Acked-by: Nicolas Pitre <nico@fluxnic.net>
1 parent 7deb55f commit b7dca6d

File tree

10 files changed

+22
-67
lines changed

10 files changed

+22
-67
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
*.lz4
3131
*.lzma
3232
*.lzo
33+
*.mod
3334
*.mod.c
3435
*.o
3536
*.o.*

Documentation/dontdiff

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
*.lzo
3131
*.mo
3232
*.moc
33+
*.mod
3334
*.mod.c
3435
*.o
3536
*.o.*

Makefile

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -486,11 +486,6 @@ export KBUILD_AFLAGS_MODULE KBUILD_CFLAGS_MODULE KBUILD_LDFLAGS_MODULE
486486
export KBUILD_AFLAGS_KERNEL KBUILD_CFLAGS_KERNEL
487487
export KBUILD_ARFLAGS
488488

489-
# When compiling out-of-tree modules, put MODVERDIR in the module
490-
# tree rather than in the kernel tree. The kernel tree might
491-
# even be read-only.
492-
export MODVERDIR := $(if $(KBUILD_EXTMOD),$(firstword $(KBUILD_EXTMOD))/).tmp_versions
493-
494489
# Files to ignore in find ... statements
495490

496491
export RCS_FIND_IGNORE := \( -name SCCS -o -name BitKeeper -o -name .svn -o \
@@ -1029,8 +1024,8 @@ vmlinux-deps := $(KBUILD_LDS) $(KBUILD_VMLINUX_OBJS) $(KBUILD_VMLINUX_LIBS)
10291024

10301025
# Recurse until adjust_autoksyms.sh is satisfied
10311026
PHONY += autoksyms_recursive
1032-
autoksyms_recursive: $(vmlinux-deps)
10331027
ifdef CONFIG_TRIM_UNUSED_KSYMS
1028+
autoksyms_recursive: $(vmlinux-deps) modules.order
10341029
$(Q)$(CONFIG_SHELL) $(srctree)/scripts/adjust_autoksyms.sh \
10351030
"$(MAKE) -f $(srctree)/Makefile vmlinux"
10361031
endif
@@ -1113,7 +1108,6 @@ endif
11131108

11141109
prepare1: prepare3 outputmakefile asm-generic $(version_h) $(autoksyms_h) \
11151110
include/generated/utsrelease.h
1116-
$(cmd_crmodverdir)
11171111

11181112
archprepare: archheaders archscripts prepare1 scripts
11191113

@@ -1371,7 +1365,7 @@ endif # CONFIG_MODULES
13711365
# make distclean Remove editor backup files, patch leftover files and the like
13721366

13731367
# Directories & files removed with 'make clean'
1374-
CLEAN_DIRS += $(MODVERDIR) include/ksym
1368+
CLEAN_DIRS += include/ksym
13751369
CLEAN_FILES += modules.builtin.modinfo
13761370

13771371
# Directories & files removed with 'make mrproper'
@@ -1641,7 +1635,6 @@ PHONY += $(clean-dirs) clean
16411635
$(clean-dirs):
16421636
$(Q)$(MAKE) $(clean)=$(patsubst _clean_%,%,$@)
16431637

1644-
clean: rm-dirs := $(MODVERDIR)
16451638
clean: rm-files := $(KBUILD_EXTMOD)/Module.symvers
16461639

16471640
PHONY += help
@@ -1655,8 +1648,6 @@ help:
16551648
@echo ''
16561649

16571650
PHONY += prepare
1658-
prepare:
1659-
$(cmd_crmodverdir)
16601651
endif # KBUILD_EXTMOD
16611652

16621653
clean: $(clean-dirs)
@@ -1667,7 +1658,7 @@ clean: $(clean-dirs)
16671658
-o -name '*.ko.*' \
16681659
-o -name '*.dtb' -o -name '*.dtb.S' -o -name '*.dt.yaml' \
16691660
-o -name '*.dwo' -o -name '*.lst' \
1670-
-o -name '*.su' \
1661+
-o -name '*.su' -o -name '*.mod' \
16711662
-o -name '.*.d' -o -name '.*.tmp' -o -name '*.mod.c' \
16721663
-o -name '*.lex.c' -o -name '*.tab.[ch]' \
16731664
-o -name '*.asn1.[ch]' \
@@ -1794,11 +1785,6 @@ quiet_cmd_depmod = DEPMOD $(KERNELRELEASE)
17941785
cmd_depmod = $(CONFIG_SHELL) $(srctree)/scripts/depmod.sh $(DEPMOD) \
17951786
$(KERNELRELEASE)
17961787

1797-
# Create temporary dir for module support files
1798-
# clean it up only when building all modules
1799-
cmd_crmodverdir = $(Q)mkdir -p $(MODVERDIR) \
1800-
$(if $(KBUILD_MODULES),; rm -f $(MODVERDIR)/*)
1801-
18021788
# read saved command lines for existing targets
18031789
existing-targets := $(wildcard $(sort $(targets)))
18041790

lib/Kconfig.debug

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -353,23 +353,13 @@ config DEBUG_SECTION_MISMATCH
353353
which results in the code/data being placed in specific sections.
354354
The section mismatch analysis is always performed after a full
355355
kernel build, and enabling this option causes the following
356-
additional steps to occur:
356+
additional step to occur:
357357
- Add the option -fno-inline-functions-called-once to gcc commands.
358358
When inlining a function annotated with __init in a non-init
359359
function, we would lose the section information and thus
360360
the analysis would not catch the illegal reference.
361361
This option tells gcc to inline less (but it does result in
362362
a larger kernel).
363-
- Run the section mismatch analysis for each module/built-in.a file.
364-
When we run the section mismatch analysis on vmlinux.o, we
365-
lose valuable information about where the mismatch was
366-
introduced.
367-
Running the analysis for each module/built-in.a file
368-
tells where the mismatch happens much closer to the
369-
source. The drawback is that the same mismatch is
370-
reported at least twice.
371-
- Enable verbose reporting from modpost in order to help resolve
372-
the section mismatches that are reported.
373363

374364
config SECTION_MISMATCH_WARN_ONLY
375365
bool "Make section mismatch errors non-fatal"

scripts/Makefile.build

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,6 @@ ifeq ($(CONFIG_MODULES)$(need-modorder),y1)
6767
modorder-target := $(obj)/modules.order
6868
endif
6969

70-
# We keep a list of all modules in $(MODVERDIR)
71-
7270
__build: $(if $(KBUILD_BUILTIN),$(builtin-target) $(lib-target) $(extra-y)) \
7371
$(if $(KBUILD_MODULES),$(obj-m) $(modorder-target)) \
7472
$(subdir-ym) $(always)
@@ -87,11 +85,6 @@ ifneq ($(KBUILD_ENABLE_EXTRA_GCC_CHECKS),)
8785
cmd_checkdoc = $(srctree)/scripts/kernel-doc -none $<
8886
endif
8987

90-
# Do section mismatch analysis for each module/built-in.a
91-
ifdef CONFIG_DEBUG_SECTION_MISMATCH
92-
cmd_secanalysis = ; scripts/mod/modpost $@
93-
endif
94-
9588
# Compile C sources (.c)
9689
# ---------------------------------------------------------------------------
9790

@@ -278,13 +271,11 @@ $(obj)/%.o: $(src)/%.c $(recordmcount_source) $(objtool_dep) FORCE
278271
$(call cmd,force_checksrc)
279272
$(call if_changed_rule,cc_o_c)
280273

281-
# Single-part modules are special since we need to mark them in $(MODVERDIR)
282-
283274
$(single-used-m): $(obj)/%.o: $(src)/%.c $(recordmcount_source) $(objtool_dep) FORCE
284275
$(call cmd,force_checksrc)
285276
$(call if_changed_rule,cc_o_c)
286277
@{ echo $(@:.o=.ko); echo $@; \
287-
$(cmd_undef_syms); } > $(MODVERDIR)/$(@F:.o=.mod)
278+
$(cmd_undef_syms); } > $(patsubst %.o,%.mod,$@)
288279

289280
quiet_cmd_cc_lst_c = MKLST $@
290281
cmd_cc_lst_c = $(CC) $(c_flags) -g -c -o $*.o $< && \
@@ -461,12 +452,12 @@ endif
461452
# module is turned into a multi object module, $^ will contain header file
462453
# dependencies recorded in the .*.cmd file.
463454
quiet_cmd_link_multi-m = LD [M] $@
464-
cmd_link_multi-m = $(LD) $(ld_flags) -r -o $@ $(filter %.o,$^) $(cmd_secanalysis)
455+
cmd_link_multi-m = $(LD) $(ld_flags) -r -o $@ $(filter %.o,$^)
465456

466457
$(multi-used-m): FORCE
467458
$(call if_changed,link_multi-m)
468459
@{ echo $(@:.o=.ko); echo $(filter %.o,$^); \
469-
$(cmd_undef_syms); } > $(MODVERDIR)/$(@F:.o=.mod)
460+
$(cmd_undef_syms); } > $(patsubst %.o,%.mod,$@)
470461
$(call multi_depend, $(multi-used-m), .o, -objs -y -m)
471462

472463
targets += $(multi-used-m)

scripts/Makefile.modpost

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66
# Stage one of module building created the following:
77
# a) The individual .o files used for the module
88
# b) A <module>.o file which is the .o files above linked together
9-
# c) A <module>.mod file in $(MODVERDIR)/, listing the name of the
10-
# the preliminary <module>.o file, plus all .o files
9+
# c) A <module>.mod file, listing the name of the preliminary <module>.o file,
10+
# plus all .o files
1111
# d) modules.order, which lists all the modules
1212

1313
# Stage 2 is handled by this file and does the following

scripts/adjust_autoksyms.sh

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,7 @@
88
#
99

1010
# Create/update the include/generated/autoksyms.h file from the list
11-
# of all module's needed symbols as recorded on the third line of
12-
# .tmp_versions/*.mod files.
11+
# of all module's needed symbols as recorded on the third line of *.mod files.
1312
#
1413
# For each symbol being added or removed, the corresponding dependency
1514
# file's timestamp is updated to force a rebuild of the affected source
@@ -47,13 +46,10 @@ cat > "$new_ksyms_file" << EOT
4746
*/
4847
4948
EOT
50-
[ "$(ls -A "$MODVERDIR")" ] &&
51-
for mod in "$MODVERDIR"/*.mod; do
52-
sed -n -e '3{s/ /\n/g;/^$/!p;}' "$mod"
53-
done | sort -u |
54-
while read sym; do
55-
echo "#define __KSYM_${sym} 1"
56-
done >> "$new_ksyms_file"
49+
sed 's/ko$/mod/' modules.order |
50+
xargs -n1 sed -n -e '3{s/ /\n/g;/^$/!p;}' -- |
51+
sort -u |
52+
sed -e 's/\(.*\)/#define __KSYM_\1 1/' >> "$new_ksyms_file"
5753

5854
# Special case for modversions (see modpost.c)
5955
if [ -n "$CONFIG_MODVERSIONS" ]; then

scripts/mod/sumversion.c

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -396,21 +396,11 @@ void get_src_version(const char *modname, char sum[], unsigned sumlen)
396396
unsigned long len;
397397
struct md4_ctx md;
398398
char *sources, *end, *fname;
399-
const char *basename;
400399
char filelist[PATH_MAX + 1];
401-
char *modverdir = getenv("MODVERDIR");
402400

403-
if (!modverdir)
404-
modverdir = ".";
405-
406-
/* Source files for module are in .tmp_versions/modname.mod,
407-
after the first line. */
408-
if (strrchr(modname, '/'))
409-
basename = strrchr(modname, '/') + 1;
410-
else
411-
basename = modname;
412-
snprintf(filelist, sizeof(filelist), "%s/%.*s.mod", modverdir,
413-
(int) strlen(basename) - 2, basename);
401+
/* objects for a module are listed in the second line of *.mod file. */
402+
snprintf(filelist, sizeof(filelist), "%.*smod",
403+
(int)strlen(modname) - 1, modname);
414404

415405
file = grab_file(filelist, &len);
416406
if (!file)

scripts/package/mkspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ fi
2929

3030
PROVIDES="$PROVIDES kernel-$KERNELRELEASE"
3131
__KERNELRELEASE=$(echo $KERNELRELEASE | sed -e "s/-/_/g")
32-
EXCLUDES="$RCS_TAR_IGNORE --exclude=.tmp_versions --exclude=*vmlinux* \
32+
EXCLUDES="$RCS_TAR_IGNORE --exclude=*vmlinux* --exclude=*.mod \
3333
--exclude=*.o --exclude=*.ko --exclude=*.cmd --exclude=Documentation \
3434
--exclude=.config.old --exclude=.missing-syscalls.d --exclude=*.s"
3535

tools/power/cpupower/debug/kernel/Makefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ default:
1212
$(MAKE) -C $(KDIR) M=$(CURDIR)
1313

1414
clean:
15-
- rm -rf *.o *.ko .tmp-versions .*.cmd .*.mod.* *.mod.c
16-
- rm -rf .tmp_versions* Module.symvers modules.order
15+
- rm -rf *.o *.ko .*.cmd .*.mod.* *.mod.c
16+
- rm -rf Module.symvers modules.order
1717

1818
install: default
1919
install -d $(KMISC)

0 commit comments

Comments
 (0)