Skip to contentSkip to conversation

kbuild: Fix off-by-one error when generate a new version

PATCH In review Andy Shevchenko 3 participants 4 messages Jan 24, 2020 – Feb 10, 2020

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 kbuild: Fix off-by-one error when generate a new version

When build on, for example, x86 using make O=... -j64 the version in the built kernel comes from include/generated/compile.h, which is:

	#define UTS_VERSION "#351 SMP Fri Jan 24 18:46:34 EET 2020"

While at the end the x86 specific Makefile prints the contents of the .version file:

	Kernel: arch/x86/boot/bzImage is ready  (#352)

Obviously the latter is not true. This happens because we first check compile.h and update it and then generate new version, which is incorrect flow:

CHK include/generated/compile.h
UPD include/generated/compile.h
...
GEN .version

In order to fix this, move the version generation from link-vmlinux.sh to scripts/version.sh and re-use it in init/Makefile.

Additionally provide a unified way to get the current version of the build and use this in few callers. This will respect the KBUILD_BUILD_VERSION in case it's provided.

---
 arch/microblaze/Makefile |  4 ++--
 arch/x86/boot/Makefile   |  2 +-
 init/Makefile            |  8 ++++++-
 scripts/kversion.sh      | 45 ++++++++++++++++++++++++++++++++++++++++
 scripts/link-vmlinux.sh  | 10 ---------
 scripts/mkcompile_h      |  9 +++-----
 scripts/package/mkdebian |  2 +-
 scripts/package/mkspec   |  2 +-
 8 files changed, 60 insertions(+), 22 deletions(-)
 create mode 100755 scripts/kversion.sh
arch/microblaze/Makefile +2 -2
index 7b340a35b194..eead2533e2b7 100644
@@ -87,12 +87,12 @@ linux.bin.ub linux.bin.gz: linux.bin
 linux.bin: vmlinux
 linux.bin linux.bin.gz linux.bin.ub:
 	$(Q)$(MAKE) $(build)=$(boot) $(boot)/$@
-	@echo 'Kernel: $(boot)/$@ is ready' ' (#'`cat .version`')'
+	@echo 'Kernel: $(boot)/$@ is ready' ' (#'`$(srctree)/scripts/kversion.sh --show`')'
 
 PHONY += simpleImage.$(DTB)
 simpleImage.$(DTB): vmlinux
 	$(Q)$(MAKE) $(build)=$(boot) $(addprefix $(boot)/$@., ub unstrip strip)
-	@echo 'Kernel: $(boot)/$@ is ready' ' (#'`cat .version`')'
+	@echo 'Kernel: $(boot)/$@ is ready' ' (#'`$(srctree)/scripts/kversion.sh --show`')'
 
 define archhelp
   echo '* linux.bin    - Create raw binary'
arch/x86/boot/Makefile +1 -1
index 9ba7557863e7..2ff13d835cb8 100644
@@ -82,7 +82,7 @@ cmd_image = $(obj)/tools/build $(obj)/setup.bin $(obj)/vmlinux.bin \
 
 $(obj)/bzImage: $(obj)/setup.bin $(obj)/vmlinux.bin $(obj)/tools/build FORCE
 	$(call if_changed,image)
-	@$(kecho) 'Kernel: $@ is ready' ' (#'`cat .version`')'
+	@$(kecho) 'Kernel: $@ is ready' ' (#'`$(srctree)/scripts/kversion.sh --show`')'
 
 OBJCOPYFLAGS_vmlinux.bin := -O binary -R .note -R .comment -S
 $(obj)/vmlinux.bin: $(obj)/compressed/vmlinux FORCE
init/Makefile +7 -1
index 6246a06364d0..f544aeb27901 100644
@@ -31,8 +31,14 @@ $(obj)/version.o: include/generated/compile.h
        chk_compile.h = :
  quiet_chk_compile.h = echo '  CHK     $@'
 silent_chk_compile.h = :
-include/generated/compile.h: FORCE
+include/generated/compile.h: kversion FORCE
 	@$($(quiet)chk_compile.h)
 	$(Q)$(CONFIG_SHELL) $(srctree)/scripts/mkcompile_h $@	\
 	"$(UTS_MACHINE)" "$(CONFIG_SMP)" "$(CONFIG_PREEMPT)"	\
 	"$(CONFIG_PREEMPT_RT)" "$(CC) $(KBUILD_CFLAGS)"
+
+quiet_cmd_kversion = CALL    $<
+      cmd_kversion = $(CONFIG_SHELL) $< --update
+
+kversion: $(srctree)/scripts/kversion.sh FORCE
+	$(call cmd,kversion)
scripts/kversion.sh new file +45 -0

45 lines not on this page · raw message

scripts/link-vmlinux.sh +0 -10
index 1919c311c149..c9a1b4f44f47 100755
@@ -228,16 +228,6 @@ fi
 # We need access to CONFIG_ symbols
 . include/config/auto.conf
 
-# Update version
-info GEN .version
-if [ -r .version ]; then
-	VERSION=$(expr 0$(cat .version) + 1)
-	echo $VERSION > .version
-else
-	rm -f .version
-	echo 1 > .version
-fi;
-
 # final build of init/
 ${MAKE} -f "${srctree}/scripts/Makefile.build" obj=init
 
scripts/mkcompile_h +3 -6
index 3a5a4b210c86..ef9085a63a04 100755
@@ -28,12 +28,6 @@ set -f
 LC_ALL=C
 export LC_ALL
 
-if [ -z "$KBUILD_BUILD_VERSION" ]; then
-	VERSION=$(cat .version 2>/dev/null || echo 1)
-else
-	VERSION=$KBUILD_BUILD_VERSION
-fi
-
 if [ -z "$KBUILD_BUILD_TIMESTAMP" ]; then
 	TIMESTAMP=`date`
 else
@@ -50,7 +44,10 @@ else
 	LINUX_COMPILE_HOST=$KBUILD_BUILD_HOST
 fi
 
+VERSION=$($srctree/scripts/kversion.sh --show)
+
 UTS_VERSION="#$VERSION"
+
 CONFIG_FLAGS=""
 if [ -n "$SMP" ] ; then CONFIG_FLAGS="SMP"; fi
 if [ -n "$PREEMPT" ] ; then CONFIG_FLAGS="$CONFIG_FLAGS PREEMPT"; fi
scripts/package/mkdebian +1 -1
index 357dc56bcf30..a4ef9c6c7d35 100755
@@ -90,7 +90,7 @@ if [ -n "$KDEB_PKGVERSION" ]; then
 	packageversion=$KDEB_PKGVERSION
 	revision=${packageversion##*-}
 else
-	revision=$(cat .version 2>/dev/null||echo 1)
+	revision=$($srctree/scripts/kversion.sh --show)
 	packageversion=$version-$revision
 fi
 sourcename=$KDEB_SOURCENAME
scripts/package/mkspec +1 -1
index 8640c278f1aa..0039eb9d62bd 100755
@@ -42,7 +42,7 @@ sed -e '/^DEL/d' -e 's/^\t*//' <<EOF
 	Name: kernel
 	Summary: The Linux Kernel
 	Version: $__KERNELRELEASE
-	Release: $(cat .version 2>/dev/null || echo 1)
+	Release: $($srctree/scripts/kversion.sh --show)
 	License: GPL
 	Group: System Environment/Kernel
 	Vendor: The Linux Community
Signature · 1 line
2.24.1
#1 lore raw
Quoted 19 lines · Andy Shevchenko

When build on, for example, x86 using make O=... -j64 the version in the built kernel comes from include/generated/compile.h, which is:

	#define UTS_VERSION "#351 SMP Fri Jan 24 18:46:34 EET 2020"

While at the end the x86 specific Makefile prints the contents of the .version file:

	Kernel: arch/x86/boot/bzImage is ready  (#352)

Obviously the latter is not true. This happens because we first check compile.h and update it and then generate new version, which is incorrect flow:

CHK include/generated/compile.h
UPD include/generated/compile.h
...
GEN .version

Quoted 6 lines

In order to fix this, move the version generation from link-vmlinux.sh to scripts/version.sh and re-use it in init/Makefile.

Additionally provide a unified way to get the current version of the build and use this in few callers. This will respect the KBUILD_BUILD_VERSION in case it's provided.

Hmm... It looks like a mess in my build tree.
I have to setup more experiments here.

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#2 lore raw

Hi Andy,

Quoted 24 lines · Andy Shevchenko
Quoted 22 lines · Andy Shevchenko

When build on, for example, x86 using make O=... -j64 the version in the built kernel comes from include/generated/compile.h, which is:

      #define UTS_VERSION "#351 SMP Fri Jan 24 18:46:34 EET 2020"

While at the end the x86 specific Makefile prints the contents of the .version file:

      Kernel: arch/x86/boot/bzImage is ready  (#352)

Obviously the latter is not true. This happens because we first check compile.h and update it and then generate new version, which is incorrect flow:

CHK include/generated/compile.h
UPD include/generated/compile.h
...
GEN .version

In order to fix this, move the version generation from link-vmlinux.sh to scripts/version.sh and re-use it in init/Makefile.

Quoted 7 lines
Quoted 3 lines

Additionally provide a unified way to get the current version of the build and use this in few callers. This will respect the KBUILD_BUILD_VERSION in case it's provided.

Hmm... It looks like a mess in my build tree.
I have to setup more experiments here.

Sorry for the late reply.

I remember I was also hit by this one month ago or so.

I did not dig into it at that time
because the problem disappeared after doing something.

Today, I took a look at this again.
This is a regression caused by 56d589361572

This patch should fix it:
https://patchwork.kernel.org/patch/11374047/

Signature · 2 lines
Best Regards
Masahiro Yamada
#3 lore raw
Quoted 3 lines · Masahiro Yamada
Quoted 1 line · Andy Shevchenko

On Fri, Jan 24, 2020 at 09:58:59PM +0200, Andy Shevchenko wrote:

...

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

Additionally provide a unified way to get the current version of the build and use this in few callers. This will respect the KBUILD_BUILD_VERSION in case it's provided.

I still think there is second part in my patch to be considered, i.e. see above.

...

Quoted 10 lines

I remember I was also hit by this one month ago or so.

I did not dig into it at that time
because the problem disappeared after doing something.

Today, I took a look at this again.
This is a regression caused by 56d589361572

This patch should fix it:
https://patchwork.kernel.org/patch/11374047/

Thank you.

Signature · 2 lines
With Best Regards,
Andy Shevchenko