Re: [PATCH RFC] kbuild: Prevent compiler mismatch with external modules

From: Masahiro Yamada
Date: Fri Mar 05 2021 - 11:30:35 EST


On Tue, Feb 2, 2021 at 6:13 AM Josh Poimboeuf <jpoimboe@xxxxxxxxxx> wrote:
>
> On Fri, Jan 29, 2021 at 08:17:51AM +0900, Masahiro Yamada wrote:
> > [3]
> > Peterz already pointed out asm-goto as an example of ABI mismatch.
> >
> > I remember a trouble reported in the past due
> > to the mismatch of -mstack-protector-guard-offset.
> >
> > https://bugzilla.kernel.org/show_bug.cgi?id=201891
> >
> > This has already been fixed,
> > and it will no longer happen though.
>
> This is kind of concerning though. It would be nice to somehow store
> KCLAGS in the config and warn if it changes unexpectedly.
>
> This can be a problem not only for OOT modules, but for regular kernel
> builds which have a .config copied from somewhere.
>
> Because of the toolchain-dependent kconfig options, features can
> silently disappear if the toolchain doesn't support them, due to a
> different compiler version, or even a missing library.
>
> > [2]
> >
> > As for this patch, it is wrong to do this check in the Makefile
> > parse stage.
> >
> > "make M=... clean"
> > "make M=... help"
> >
> > etc. will fail.
> > Such targets do not require the compiler in the first place.
> >
> > This check must be done before starting building something,
> >
> > Also, this patch is not applicable.
> > gcc-version.sh and clang-version.sh do not exist.
> > See linux-next.
>
> Something like so?

No, I do not think so.


>
> diff --git a/Makefile b/Makefile
> index 95ab9856f357..10ca621369fb 100644
> --- a/Makefile
> +++ b/Makefile
> @@ -1721,12 +1721,25 @@ KBUILD_MODULES := 1
>
> build-dirs := $(KBUILD_EXTMOD)
> PHONY += modules
> -modules: $(MODORDER)
> +modules: ext_compiler_check $(MODORDER)


For the single thread build, yes
GNU Make will run the targets from left to right,
so ext_compiler_check is run before compiling
any build artifact.

If -j <N> option is given, there is no guarantee
that ext_compiler_check finishes first.



> $(Q)$(MAKE) -f $(srctree)/scripts/Makefile.modpost
>
> $(MODORDER): descend
> @:
>
> +orig_name := $(if $(CONFIG_CC_IS_GCC),GCC,CLANG)
> +orig_minor := $(shell expr $(if $(CONFIG_CC_IS_GCC),$(CONFIG_GCC_VERSION),$(CONFIG_CLANG_VERSION)) / 100)
> +cur_namever := $(shell $(srctree)/scripts/cc-version.sh $(CC))
> +cur_name := $(word 1,$(cur_namever))
> +cur_minor := $(shell expr $(word 2,$(cur_namever)) / 100)

These are still calculated by 'make M=... clean' or 'make M=... help'.
Using '=' assignment solves it, but the code is still ugly.


I attached my alternative implementation.


> +PHONY += ext_compiler_check
> +ext_compiler_check:
> + @if [ $(orig_name) != $(cur_name) ] || [ $(orig_minor) != $(cur_minor) ]; then \
> + echo >&2 "warning: The compiler differs from the version which was used to build the kernel."; \
> + echo >&2 "warning: Some kernel features are compiler-dependent."; \
> + echo >&2 "warning: It's recommended that you change your compiler to match the version in the .config file."; \
> + fi
> +
> PHONY += modules_install
> modules_install: _emodinst_ _emodinst_post
>
>

--
Best Regards
Masahiro Yamada
From 8152c47d19cab5af361d097fcb939a4e8ecefa28 Mon Sep 17 00:00:00 2001
From: Masahiro Yamada <masahiroy@kernel.org>
Date: Fri, 5 Mar 2021 14:10:26 +0900
Subject: [PATCH] kbuild: show notice if a different compiler is used for
external module build

This commit shows a notice unless the compiler is exactly the same.

notice: the compiler differs from the one used to build the kernel
The kernel was built by: gcc (GCC) 10.0.1 20200328 (Red Hat 10.0.1-0.11)
You are using: gcc (GCC) 10.2.1 20201125 (Red Hat 10.2.1-9)

Check the difference, and determine if you are OK with that. If you
believe mixing two compilers is OK, please proceed at your risk.

Signed-off-by: Masahiro Yamada <masahiroy@kernel.org>
---
Makefile | 12 +++++++++++-
1 file changed, 11 insertions(+), 1 deletion(-)

diff --git a/Makefile b/Makefile
index a3336d9b4a22..e592456673a3 100644
--- a/Makefile
+++ b/Makefile
@@ -1779,6 +1779,16 @@ clean-dirs := $(KBUILD_EXTMOD)
clean: rm-files := $(KBUILD_EXTMOD)/Module.symvers $(KBUILD_EXTMOD)/modules.nsdeps \
$(KBUILD_EXTMOD)/compile_commands.json $(KBUILD_EXTMOD)/.thinlto-cache

+PHONY += prepare
+# now expand this into a simple variable to reduce the cost of shell evaluations
+prepare: CC_VERSION_TEXT := $(CC_VERSION_TEXT)
+prepare:
+ @if [ "$(CC_VERSION_TEXT)" != $(CONFIG_CC_VERSION_TEXT) ]; then \
+ echo "notice: the compiler differs from the one used to build the kernel"; \
+ echo " The kernel was built by: "$(CONFIG_CC_VERSION_TEXT); \
+ echo " You are using: $(CC_VERSION_TEXT)"; \
+ fi
+
PHONY += help
help:
@echo ' Building external modules.'
@@ -1790,7 +1800,7 @@ help:
@echo ''

# no-op for external module builds
-PHONY += prepare modules_prepare
+PHONY += modules_prepare

endif # KBUILD_EXTMOD

--
2.27.0