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

From: Josh Poimboeuf
Date: Thu Jan 28 2021 - 15:13:31 EST


When building an external module, if the compiler version differs from
what the kernel was built with, bad things can happen. Many kernel
features change based on available compiler features. Silently removing
a compiler-dependent feature in the external module build can cause
unpredictable behavior. Right now there are no checks to help prevent
such mismatches.

On the other hand, when a user is building an external module against a
distro kernel, the exact compiler version may not be installed, or in
some cases not even released yet. In fact it's quite common for
external modules to be built with a slightly different version of GCC
than the kernel.

A minor version mismatch should be ok. User space does it all the time.
New compiler features aren't added within a major version.

Add a check for compiler mismatch, but only check the major version.

Signed-off-by: Josh Poimboeuf <jpoimboe@xxxxxxxxxx>
---
This is related to the previous RFC I posted:

https://lkml.kernel.org/r/efe6b039a544da8215d5e54aa7c4b6d1986fc2b0.1611607264.git.jpoimboe@xxxxxxxxxx

The discussion revealed gaps between developer perceptions and distro
realities with respect to external (out-of-tree) modules...

Backing up a bit, let's please decide on what exactly is supported (or
not supported) with respect to mismatched compiler versions. Then let's
try to enforce and/or document the decision.

Please stick to technical arguments...


Makefile | 8 ++++++++
1 file changed, 8 insertions(+)

diff --git a/Makefile b/Makefile
index b0e4767735dc..f281d2587fa5 100644
--- a/Makefile
+++ b/Makefile
@@ -1744,6 +1744,14 @@ help:
# no-op for external module builds
PHONY += prepare modules_prepare

+# External module compiler (major) version must match the kernel
+ifneq ($(shell echo $(CONFIG_GCC_VERSION) | cut -c-2), $(shell $(srctree)/scripts/gcc-version.sh $(CC) | cut -c-2))
+ $(error ERROR: Compiler version mismatch in external module build)
+endif
+ifneq ($(shell echo $(CONFIG_CLANG_VERSION) | cut -c-2), $(shell $(srctree)/scripts/clang-version.sh $(CC) | cut -c-2))
+ $(error ERROR: Compiler version mismatch in external module build)
+endif
+
endif # KBUILD_EXTMOD

# Single targets
--
2.29.2