[PATCH 3/3] x86/boot: avoid recompiling kaslr.c for incremental rebuilds

From: Jann Horn
Date: Tue Feb 20 2024 - 14:22:51 EST


Currently, every kernel rebuild needs to compile kaslr.c again because
UTS_VERSION changes on every rebuild.
Move the build string into a separate object file to speed things up.

Signed-off-by: Jann Horn <jannh@xxxxxxxxxx>
---
arch/x86/boot/compressed/dynamic_vars.c | 8 ++++++++
arch/x86/boot/compressed/dynamic_vars.h | 3 +++
arch/x86/boot/compressed/kaslr.c | 10 ++--------
3 files changed, 13 insertions(+), 8 deletions(-)

diff --git a/arch/x86/boot/compressed/dynamic_vars.c b/arch/x86/boot/compressed/dynamic_vars.c
index cda64ff4b6da..15a57fbb05e3 100644
--- a/arch/x86/boot/compressed/dynamic_vars.c
+++ b/arch/x86/boot/compressed/dynamic_vars.c
@@ -1,9 +1,17 @@
// SPDX-License-Identifier: GPL-2.0
#include <linux/const.h>
#include "dynamic_vars.h"
+#include <generated/compile.h>
+#include <generated/utsrelease.h>
+#include <generated/utsversion.h>
#include "../voffset.h"

const unsigned long vo__text = VO__text;
const unsigned long vo___bss_start = VO___bss_start;
const unsigned long vo__end = VO__end;
const unsigned long kernel_total_size = VO__end - VO__text;
+
+/* Simplified build-specific string for starting entropy. */
+const char build_str[] = UTS_RELEASE " (" LINUX_COMPILE_BY "@"
+ LINUX_COMPILE_HOST ") (" LINUX_COMPILER ") " UTS_VERSION;
+unsigned long build_str_len = sizeof(build_str)-1;
diff --git a/arch/x86/boot/compressed/dynamic_vars.h b/arch/x86/boot/compressed/dynamic_vars.h
index a0f7dc359cb6..3ebc4a3144d4 100644
--- a/arch/x86/boot/compressed/dynamic_vars.h
+++ b/arch/x86/boot/compressed/dynamic_vars.h
@@ -9,3 +9,6 @@
/* Variables containing VO__text, VO___bss_start, VO__end */
extern const unsigned long vo__text, vo___bss_start, vo__end;
extern const unsigned long kernel_total_size;
+
+extern const char build_str[];
+extern unsigned long build_str_len;
diff --git a/arch/x86/boot/compressed/kaslr.c b/arch/x86/boot/compressed/kaslr.c
index 3ede59ad67eb..c14e4e7a6b08 100644
--- a/arch/x86/boot/compressed/kaslr.c
+++ b/arch/x86/boot/compressed/kaslr.c
@@ -23,14 +23,12 @@
#include "error.h"
#include "../string.h"
#include "efi.h"
+#include "dynamic_vars.h"

-#include <generated/compile.h>
#include <linux/module.h>
#include <linux/uts.h>
#include <linux/utsname.h>
#include <linux/ctype.h>
-#include <generated/utsversion.h>
-#include <generated/utsrelease.h>

#define _SETUP
#include <asm/setup.h> /* For COMMAND_LINE_SIZE */
@@ -38,10 +36,6 @@

extern unsigned long get_cmd_line_ptr(void);

-/* Simplified build-specific string for starting entropy. */
-static const char build_str[] = UTS_RELEASE " (" LINUX_COMPILE_BY "@"
- LINUX_COMPILE_HOST ") (" LINUX_COMPILER ") " UTS_VERSION;
-
static unsigned long rotate_xor_one(unsigned long hash, unsigned long val)
{
/* Rotate by odd number of bits and XOR. */
@@ -75,7 +69,7 @@ static unsigned long get_boot_seed(void)
{
unsigned long hash = 0;

- hash = rotate_xor(hash, build_str, sizeof(build_str));
+ hash = rotate_xor(hash, build_str, build_str_len);
hash = rotate_xor(hash, boot_params_ptr, sizeof(*boot_params_ptr));

return hash;
--
2.44.0.rc0.258.g7320e95886-goog