History

kbuild: compile constant module information only once

Various information about modules is compiled into the info sections. For that a dedicated .mod.c file is generated by modpost for each module and then linked into the module. However most of the information in the .mod.c is the same for all modules, internal and external. Split the shared information into a dedicated source file that is compiled once and then linked into all modules. This avoids frequent rebuilds for all .mod.c files when using CONFIG_LOCALVERSION_AUTO because the local version ends up in .mod.c through UTS_RELEASE and VERMAGIC_STRING. The modules are still relinked in this case. The code is also easier to maintain as it's now in a proper source file instead of an inline string literal.

Thomas Weißschuh committed 2 years ago

Commit fdf94e41 parent cd615d73 files · +30 −25Browse files

Unified · Split

3 files
  1. scripts/Makefile.modfinal +5 −2
  2. scripts/mod/modpost.c +0 −23
  3. scripts/module-common.c +25 −0
scripts/Makefile.modfinal+5−2
@@ -30,6 +30,9 @@ quiet_cmd_cc_o_c = CC [M] $@ · 29 unmodified lines
3030 %.mod.o: %.mod.c FORCE
3131 $(call if_changed_dep,cc_o_c)
3232
33+$(extmod_prefix).module-common.o: $(srctree)/scripts/module-common.c FORCE
34+ $(call if_changed_dep,cc_o_c)
35+
3336 quiet_cmd_ld_ko_o = LD [M] $@
3437 cmd_ld_ko_o += \
3538 $(LD) -r $(KBUILD_LDFLAGS) \
@@ -54,13 +57,13 @@ if_changed_except = $(if $(call newer_prereqs_except,$(2))$(cmd-check), \ · 18 unmodified lines
5457 printf '%s\n' 'savedcmd_$@ := $(make-cmd)' > $(dot-target).cmd, @:)
5558
5659 # Re-generate module BTFs if either module's .ko or vmlinux changed
57−%.ko: %.o %.mod.o scripts/module.lds $(and $(CONFIG_DEBUG_INFO_BTF_MODULES),$(KBUILD_BUILTIN),vmlinux) FORCE
60+%.ko: %.o %.mod.o $(extmod_prefix).module-common.o scripts/module.lds $(and $(CONFIG_DEBUG_INFO_BTF_MODULES),$(KBUILD_BUILTIN),vmlinux) FORCE
5861 +$(call if_changed_except,ld_ko_o,vmlinux)
5962 ifdef CONFIG_DEBUG_INFO_BTF_MODULES
6063 +$(if $(newer-prereqs),$(call cmd,btf_ko))
6164 endif
6265
63−targets += $(modules:%.o=%.ko) $(modules:%.o=%.mod.o)
66+targets += $(modules:%.o=%.ko) $(modules:%.o=%.mod.o) $(extmod_prefix).module-common.o
6467
6568 # Add FORCE to the prerequisites of a target to force it to be always rebuilt.
6669 # ---------------------------------------------------------------------------
scripts/mod/modpost.c+0−23
@@ -1755,26 +1755,9 @@ static void check_modname_len(struct module *mod) · 1754 unmodified lines
17551755 static void add_header(struct buffer *b, struct module *mod)
17561756 {
17571757 buf_printf(b, "#include <linux/module.h>\n");
1758− /*
1759− * Include build-salt.h after module.h in order to
1760− * inherit the definitions.
1761− */
1762− buf_printf(b, "#define INCLUDE_VERMAGIC\n");
1763− buf_printf(b, "#include <linux/build-salt.h>\n");
1764− buf_printf(b, "#include <linux/elfnote-lto.h>\n");
17651758 buf_printf(b, "#include <linux/export-internal.h>\n");
1766− buf_printf(b, "#include <linux/vermagic.h>\n");
17671759 buf_printf(b, "#include <linux/compiler.h>\n");
17681760 buf_printf(b, "\n");
1769− buf_printf(b, "#ifdef CONFIG_UNWINDER_ORC\n");
1770− buf_printf(b, "#include <asm/orc_header.h>\n");
1771− buf_printf(b, "ORC_HEADER;\n");
1772− buf_printf(b, "#endif\n");
1773− buf_printf(b, "\n");
1774− buf_printf(b, "BUILD_SALT;\n");
1775− buf_printf(b, "BUILD_LTO_INFO;\n");
1776− buf_printf(b, "\n");
1777− buf_printf(b, "MODULE_INFO(vermagic, VERMAGIC_STRING);\n");
17781761 buf_printf(b, "MODULE_INFO(name, KBUILD_MODNAME);\n");
17791762 buf_printf(b, "\n");
17801763 buf_printf(b, "__visible struct module __this_module\n");
@@ -1792,12 +1775,6 @@ static void add_header(struct buffer *b, struct module *mod) · 11 unmodified lines
17921775 if (!external_module)
17931776 buf_printf(b, "\nMODULE_INFO(intree, \"Y\");\n");
17941777
1795− buf_printf(b,
1796− "\n"
1797− "#ifdef CONFIG_MITIGATION_RETPOLINE\n"
1798− "MODULE_INFO(retpoline, \"Y\");\n"
1799− "#endif\n");
1800−
18011778 if (strstarts(mod->name, "drivers/staging"))
18021779 buf_printf(b, "\nMODULE_INFO(staging, \"Y\");\n");
18031780
scripts/module-common.c+25−0
@@ -0,0 +1,25 @@
1+// SPDX-License-Identifier: GPL-2.0
2+
3+#include <linux/module.h>
4+/*
5+ * Include build-salt.h after module.h in order to
6+ * inherit the definitions.
7+ */
8+#define INCLUDE_VERMAGIC
9+#include <linux/build-salt.h>
10+#include <linux/elfnote-lto.h>
11+#include <linux/vermagic.h>
12+
13+#ifdef CONFIG_UNWINDER_ORC
14+#include <asm/orc_header.h>
15+ORC_HEADER;
16+#endif
17+
18+BUILD_SALT;
19+BUILD_LTO_INFO;
20+
21+MODULE_INFO(vermagic, VERMAGIC_STRING);
22+
23+#ifdef CONFIG_MITIGATION_RETPOLINE
24+MODULE_INFO(retpoline, "Y");
25+#endif