diff mbox series

[v2] modpost: compile constant module information only once

Message ID 20240901-modinfo-const-v2-1-ece53ca15075@weissschuh.net (mailing list archive)
State New
Headers show
Series [v2] modpost: compile constant module information only once | expand

Commit Message

Thomas Weißschuh Sept. 1, 2024, 5:55 p.m. UTC
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.

Signed-off-by: Thomas Weißschuh <linux@weissschuh.net>
---
Changes in v2:
- Remove RFC status
- Incorporate Masahiro's proposals
  - Rename modinfo.o to .module-common.o
  - Build a dedicated .module-common.o for external modules
- Link to v1: https://lore.kernel.org/r/20240824-modinfo-const-v1-1-485f9c64b868@weissschuh.net
---
Masahiro, feel free to add some attribution for yourself when applying.
The new appraoch is pleasantly simpler.
---
 scripts/Makefile.modfinal |  7 +++++--
 scripts/mod/modpost.c     | 23 -----------------------
 scripts/module-common.c   | 25 +++++++++++++++++++++++++
 3 files changed, 30 insertions(+), 25 deletions(-)


---
base-commit: 9f18baf3dd656e7ca166038d51e0b54a892d87db
change-id: 20240824-modinfo-const-6f0d67e2b301

Best regards,

Comments

Masahiro Yamada Sept. 3, 2024, 2:04 p.m. UTC | #1
On Mon, Sep 2, 2024 at 2:56 AM Thomas Weißschuh <linux@weissschuh.net> wrote:
>
> 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.
>
> Signed-off-by: Thomas Weißschuh <linux@weissschuh.net>


Applied to linux-kbuild. Thanks!


> ---
> Changes in v2:
> - Remove RFC status
> - Incorporate Masahiro's proposals
>   - Rename modinfo.o to .module-common.o
>   - Build a dedicated .module-common.o for external modules
> - Link to v1: https://lore.kernel.org/r/20240824-modinfo-const-v1-1-485f9c64b868@weissschuh.net
> ---
> Masahiro, feel free to add some attribution for yourself when applying.
> The new appraoch is pleasantly simpler.
> ---
>  scripts/Makefile.modfinal |  7 +++++--
>  scripts/mod/modpost.c     | 23 -----------------------
>  scripts/module-common.c   | 25 +++++++++++++++++++++++++
>  3 files changed, 30 insertions(+), 25 deletions(-)
>
> diff --git a/scripts/Makefile.modfinal b/scripts/Makefile.modfinal
> index 306a6bb86e4d..6b1b72257b29 100644
> --- a/scripts/Makefile.modfinal
> +++ b/scripts/Makefile.modfinal
> @@ -30,6 +30,9 @@ quiet_cmd_cc_o_c = CC [M]  $@
>  %.mod.o: %.mod.c FORCE
>         $(call if_changed_dep,cc_o_c)
>
> +$(extmod_prefix).module-common.o: $(srctree)/scripts/module-common.c FORCE
> +       $(call if_changed_dep,cc_o_c)
> +
>  quiet_cmd_ld_ko_o = LD [M]  $@
>        cmd_ld_ko_o +=                                                   \
>         $(LD) -r $(KBUILD_LDFLAGS)                                      \
> @@ -54,13 +57,13 @@ if_changed_except = $(if $(call newer_prereqs_except,$(2))$(cmd-check),      \
>         printf '%s\n' 'savedcmd_$@ := $(make-cmd)' > $(dot-target).cmd, @:)
>
>  # Re-generate module BTFs if either module's .ko or vmlinux changed
> -%.ko: %.o %.mod.o scripts/module.lds $(and $(CONFIG_DEBUG_INFO_BTF_MODULES),$(KBUILD_BUILTIN),vmlinux) FORCE
> +%.ko: %.o %.mod.o $(extmod_prefix).module-common.o scripts/module.lds $(and $(CONFIG_DEBUG_INFO_BTF_MODULES),$(KBUILD_BUILTIN),vmlinux) FORCE
>         +$(call if_changed_except,ld_ko_o,vmlinux)
>  ifdef CONFIG_DEBUG_INFO_BTF_MODULES
>         +$(if $(newer-prereqs),$(call cmd,btf_ko))
>  endif
>
> -targets += $(modules:%.o=%.ko) $(modules:%.o=%.mod.o)
> +targets += $(modules:%.o=%.ko) $(modules:%.o=%.mod.o) $(extmod_prefix).module-common.o
>
>  # Add FORCE to the prerequisites of a target to force it to be always rebuilt.
>  # ---------------------------------------------------------------------------
> diff --git a/scripts/mod/modpost.c b/scripts/mod/modpost.c
> index c8cd5d822bb6..107393a8c48a 100644
> --- a/scripts/mod/modpost.c
> +++ b/scripts/mod/modpost.c
> @@ -1755,26 +1755,9 @@ static void check_modname_len(struct module *mod)
>  static void add_header(struct buffer *b, struct module *mod)
>  {
>         buf_printf(b, "#include <linux/module.h>\n");
> -       /*
> -        * Include build-salt.h after module.h in order to
> -        * inherit the definitions.
> -        */
> -       buf_printf(b, "#define INCLUDE_VERMAGIC\n");
> -       buf_printf(b, "#include <linux/build-salt.h>\n");
> -       buf_printf(b, "#include <linux/elfnote-lto.h>\n");
>         buf_printf(b, "#include <linux/export-internal.h>\n");
> -       buf_printf(b, "#include <linux/vermagic.h>\n");
>         buf_printf(b, "#include <linux/compiler.h>\n");
>         buf_printf(b, "\n");
> -       buf_printf(b, "#ifdef CONFIG_UNWINDER_ORC\n");
> -       buf_printf(b, "#include <asm/orc_header.h>\n");
> -       buf_printf(b, "ORC_HEADER;\n");
> -       buf_printf(b, "#endif\n");
> -       buf_printf(b, "\n");
> -       buf_printf(b, "BUILD_SALT;\n");
> -       buf_printf(b, "BUILD_LTO_INFO;\n");
> -       buf_printf(b, "\n");
> -       buf_printf(b, "MODULE_INFO(vermagic, VERMAGIC_STRING);\n");
>         buf_printf(b, "MODULE_INFO(name, KBUILD_MODNAME);\n");
>         buf_printf(b, "\n");
>         buf_printf(b, "__visible struct module __this_module\n");
> @@ -1792,12 +1775,6 @@ static void add_header(struct buffer *b, struct module *mod)
>         if (!external_module)
>                 buf_printf(b, "\nMODULE_INFO(intree, \"Y\");\n");
>
> -       buf_printf(b,
> -                  "\n"
> -                  "#ifdef CONFIG_MITIGATION_RETPOLINE\n"
> -                  "MODULE_INFO(retpoline, \"Y\");\n"
> -                  "#endif\n");
> -
>         if (strstarts(mod->name, "drivers/staging"))
>                 buf_printf(b, "\nMODULE_INFO(staging, \"Y\");\n");
>
> diff --git a/scripts/module-common.c b/scripts/module-common.c
> new file mode 100644
> index 000000000000..12fbc6d3aae8
> --- /dev/null
> +++ b/scripts/module-common.c
> @@ -0,0 +1,25 @@
> +// SPDX-License-Identifier: GPL-2.0
> +
> +#include <linux/module.h>
> +/*
> + * Include build-salt.h after module.h in order to
> + * inherit the definitions.
> + */
> +#define INCLUDE_VERMAGIC
> +#include <linux/build-salt.h>
> +#include <linux/elfnote-lto.h>
> +#include <linux/vermagic.h>
> +
> +#ifdef CONFIG_UNWINDER_ORC
> +#include <asm/orc_header.h>
> +ORC_HEADER;
> +#endif
> +
> +BUILD_SALT;
> +BUILD_LTO_INFO;
> +
> +MODULE_INFO(vermagic, VERMAGIC_STRING);
> +
> +#ifdef CONFIG_MITIGATION_RETPOLINE
> +MODULE_INFO(retpoline, "Y");
> +#endif
>
> ---
> base-commit: 9f18baf3dd656e7ca166038d51e0b54a892d87db
> change-id: 20240824-modinfo-const-6f0d67e2b301
>
> Best regards,
> --
> Thomas Weißschuh <linux@weissschuh.net>
>
diff mbox series

Patch

diff --git a/scripts/Makefile.modfinal b/scripts/Makefile.modfinal
index 306a6bb86e4d..6b1b72257b29 100644
--- a/scripts/Makefile.modfinal
+++ b/scripts/Makefile.modfinal
@@ -30,6 +30,9 @@  quiet_cmd_cc_o_c = CC [M]  $@
 %.mod.o: %.mod.c FORCE
 	$(call if_changed_dep,cc_o_c)
 
+$(extmod_prefix).module-common.o: $(srctree)/scripts/module-common.c FORCE
+	$(call if_changed_dep,cc_o_c)
+
 quiet_cmd_ld_ko_o = LD [M]  $@
       cmd_ld_ko_o +=							\
 	$(LD) -r $(KBUILD_LDFLAGS)					\
@@ -54,13 +57,13 @@  if_changed_except = $(if $(call newer_prereqs_except,$(2))$(cmd-check),      \
 	printf '%s\n' 'savedcmd_$@ := $(make-cmd)' > $(dot-target).cmd, @:)
 
 # Re-generate module BTFs if either module's .ko or vmlinux changed
-%.ko: %.o %.mod.o scripts/module.lds $(and $(CONFIG_DEBUG_INFO_BTF_MODULES),$(KBUILD_BUILTIN),vmlinux) FORCE
+%.ko: %.o %.mod.o $(extmod_prefix).module-common.o scripts/module.lds $(and $(CONFIG_DEBUG_INFO_BTF_MODULES),$(KBUILD_BUILTIN),vmlinux) FORCE
 	+$(call if_changed_except,ld_ko_o,vmlinux)
 ifdef CONFIG_DEBUG_INFO_BTF_MODULES
 	+$(if $(newer-prereqs),$(call cmd,btf_ko))
 endif
 
-targets += $(modules:%.o=%.ko) $(modules:%.o=%.mod.o)
+targets += $(modules:%.o=%.ko) $(modules:%.o=%.mod.o) $(extmod_prefix).module-common.o
 
 # Add FORCE to the prerequisites of a target to force it to be always rebuilt.
 # ---------------------------------------------------------------------------
diff --git a/scripts/mod/modpost.c b/scripts/mod/modpost.c
index c8cd5d822bb6..107393a8c48a 100644
--- a/scripts/mod/modpost.c
+++ b/scripts/mod/modpost.c
@@ -1755,26 +1755,9 @@  static void check_modname_len(struct module *mod)
 static void add_header(struct buffer *b, struct module *mod)
 {
 	buf_printf(b, "#include <linux/module.h>\n");
-	/*
-	 * Include build-salt.h after module.h in order to
-	 * inherit the definitions.
-	 */
-	buf_printf(b, "#define INCLUDE_VERMAGIC\n");
-	buf_printf(b, "#include <linux/build-salt.h>\n");
-	buf_printf(b, "#include <linux/elfnote-lto.h>\n");
 	buf_printf(b, "#include <linux/export-internal.h>\n");
-	buf_printf(b, "#include <linux/vermagic.h>\n");
 	buf_printf(b, "#include <linux/compiler.h>\n");
 	buf_printf(b, "\n");
-	buf_printf(b, "#ifdef CONFIG_UNWINDER_ORC\n");
-	buf_printf(b, "#include <asm/orc_header.h>\n");
-	buf_printf(b, "ORC_HEADER;\n");
-	buf_printf(b, "#endif\n");
-	buf_printf(b, "\n");
-	buf_printf(b, "BUILD_SALT;\n");
-	buf_printf(b, "BUILD_LTO_INFO;\n");
-	buf_printf(b, "\n");
-	buf_printf(b, "MODULE_INFO(vermagic, VERMAGIC_STRING);\n");
 	buf_printf(b, "MODULE_INFO(name, KBUILD_MODNAME);\n");
 	buf_printf(b, "\n");
 	buf_printf(b, "__visible struct module __this_module\n");
@@ -1792,12 +1775,6 @@  static void add_header(struct buffer *b, struct module *mod)
 	if (!external_module)
 		buf_printf(b, "\nMODULE_INFO(intree, \"Y\");\n");
 
-	buf_printf(b,
-		   "\n"
-		   "#ifdef CONFIG_MITIGATION_RETPOLINE\n"
-		   "MODULE_INFO(retpoline, \"Y\");\n"
-		   "#endif\n");
-
 	if (strstarts(mod->name, "drivers/staging"))
 		buf_printf(b, "\nMODULE_INFO(staging, \"Y\");\n");
 
diff --git a/scripts/module-common.c b/scripts/module-common.c
new file mode 100644
index 000000000000..12fbc6d3aae8
--- /dev/null
+++ b/scripts/module-common.c
@@ -0,0 +1,25 @@ 
+// SPDX-License-Identifier: GPL-2.0
+
+#include <linux/module.h>
+/*
+ * Include build-salt.h after module.h in order to
+ * inherit the definitions.
+ */
+#define INCLUDE_VERMAGIC
+#include <linux/build-salt.h>
+#include <linux/elfnote-lto.h>
+#include <linux/vermagic.h>
+
+#ifdef CONFIG_UNWINDER_ORC
+#include <asm/orc_header.h>
+ORC_HEADER;
+#endif
+
+BUILD_SALT;
+BUILD_LTO_INFO;
+
+MODULE_INFO(vermagic, VERMAGIC_STRING);
+
+#ifdef CONFIG_MITIGATION_RETPOLINE
+MODULE_INFO(retpoline, "Y");
+#endif