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
30%.mod.o: %.mod.c FORCE30%.mod.o: %.mod.c FORCE
31 $(call if_changed_dep,cc_o_c)31 $(call if_changed_dep,cc_o_c)
32 32
33$(extmod_prefix).module-common.o: $(srctree)/scripts/module-common.c FORCE
34 $(call if_changed_dep,cc_o_c)
35
33quiet_cmd_ld_ko_o = LD [M] $@36quiet_cmd_ld_ko_o = LD [M] $@
34 cmd_ld_ko_o += \37 cmd_ld_ko_o += \
35 $(LD) -r $(KBUILD_LDFLAGS) \38 $(LD) -r $(KBUILD_LDFLAGS) \
@@ -54,13 +57,13 @@ if_changed_except = $(if $(call newer_prereqs_except,$(2))$(cmd-check), \ · 18 unmodified lines
54 printf '%s\n' 'savedcmd_$@ := $(make-cmd)' > $(dot-target).cmd, @:)57 printf '%s\n' 'savedcmd_$@ := $(make-cmd)' > $(dot-target).cmd, @:)
55 58
56# Re-generate module BTFs if either module's .ko or vmlinux changed59# 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) FORCE60%.ko: %.o %.mod.o $(extmod_prefix).module-common.o scripts/module.lds $(and $(CONFIG_DEBUG_INFO_BTF_MODULES),$(KBUILD_BUILTIN),vmlinux) FORCE
58 +$(call if_changed_except,ld_ko_o,vmlinux)61 +$(call if_changed_except,ld_ko_o,vmlinux)
59ifdef CONFIG_DEBUG_INFO_BTF_MODULES62ifdef CONFIG_DEBUG_INFO_BTF_MODULES
60 +$(if $(newer-prereqs),$(call cmd,btf_ko))63 +$(if $(newer-prereqs),$(call cmd,btf_ko))
61endif64endif
62 65
63targets += $(modules:%.o=%.ko) $(modules:%.o=%.mod.o)66targets += $(modules:%.o=%.ko) $(modules:%.o=%.mod.o) $(extmod_prefix).module-common.o
64 67
65# Add FORCE to the prerequisites of a target to force it to be always rebuilt.68# Add FORCE to the prerequisites of a target to force it to be always rebuilt.
66# ---------------------------------------------------------------------------69# ---------------------------------------------------------------------------
scripts/mod/modpost.c+0−23
@@ -1755,26 +1755,9 @@ static void check_modname_len(struct module *mod) · 1754 unmodified lines
1755static void add_header(struct buffer *b, struct module *mod)1755static void add_header(struct buffer *b, struct module *mod)
1756{1756{
1757 buf_printf(b, "#include <linux/module.h>\n");1757 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");
1765 buf_printf(b, "#include <linux/export-internal.h>\n");1758 buf_printf(b, "#include <linux/export-internal.h>\n");
1766 buf_printf(b, "#include <linux/vermagic.h>\n");
1767 buf_printf(b, "#include <linux/compiler.h>\n");1759 buf_printf(b, "#include <linux/compiler.h>\n");
1768 buf_printf(b, "\n");1760 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");
1778 buf_printf(b, "MODULE_INFO(name, KBUILD_MODNAME);\n");1761 buf_printf(b, "MODULE_INFO(name, KBUILD_MODNAME);\n");
1779 buf_printf(b, "\n");1762 buf_printf(b, "\n");
1780 buf_printf(b, "__visible struct module __this_module\n");1763 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
1792 if (!external_module)1775 if (!external_module)
1793 buf_printf(b, "\nMODULE_INFO(intree, \"Y\");\n");1776 buf_printf(b, "\nMODULE_INFO(intree, \"Y\");\n");
1794 1777
1795 buf_printf(b,
1796 "\n"
1797 "#ifdef CONFIG_MITIGATION_RETPOLINE\n"
1798 "MODULE_INFO(retpoline, \"Y\");\n"
1799 "#endif\n");
1800
1801 if (strstarts(mod->name, "drivers/staging"))1778 if (strstarts(mod->name, "drivers/staging"))
1802 buf_printf(b, "\nMODULE_INFO(staging, \"Y\");\n");1779 buf_printf(b, "\nMODULE_INFO(staging, \"Y\");\n");
1803 1780
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>
15ORC_HEADER;
16#endif
17
18BUILD_SALT;
19BUILD_LTO_INFO;
20
21MODULE_INFO(vermagic, VERMAGIC_STRING);
22
23#ifdef CONFIG_MITIGATION_RETPOLINE
24MODULE_INFO(retpoline, "Y");
25#endif