[PATCH v2 2/5] x86/boot/64: Clear BSS as early as possible
From: Brian Gerst
Date: Thu Jul 30 2026 - 16:07:27 EST
Currently, the BSS section is not cleared until x86_64_start_kernel().
Using unitialized BSS data before that point leads to difficult to debug
problems. Fix this by moving clear_bss() to as early as possible.
Signed-off-by: Brian Gerst <brgerst@xxxxxxxxx>
---
arch/x86/include/asm/setup.h | 2 --
arch/x86/kernel/head64.c | 12 ------------
arch/x86/kernel/head_64.S | 20 ++++++++++++++++++++
arch/x86/xen/enlighten_pv.c | 2 --
arch/x86/xen/xen-head.S | 5 ++++-
5 files changed, 24 insertions(+), 17 deletions(-)
diff --git a/arch/x86/include/asm/setup.h b/arch/x86/include/asm/setup.h
index 914eb32581c7..5534f4aaf73b 100644
--- a/arch/x86/include/asm/setup.h
+++ b/arch/x86/include/asm/setup.h
@@ -126,8 +126,6 @@ void *extend_brk(size_t size, size_t align);
extern void probe_roms(void);
-void clear_bss(void);
-
#ifdef __i386__
asmlinkage void __init __noreturn i386_start_kernel(void);
diff --git a/arch/x86/kernel/head64.c b/arch/x86/kernel/head64.c
index fd28b53dbac5..9f19635c6390 100644
--- a/arch/x86/kernel/head64.c
+++ b/arch/x86/kernel/head64.c
@@ -172,16 +172,6 @@ void __init do_early_exception(struct pt_regs *regs, int trapnr)
early_fixup_exception(regs, trapnr);
}
-/* Don't add a printk in there. printk relies on the PDA which is not initialized
- yet. */
-void __init clear_bss(void)
-{
- memset(__bss_start, 0,
- (unsigned long) __bss_stop - (unsigned long) __bss_start);
- memset(__brk_base, 0,
- (unsigned long) __brk_limit - (unsigned long) __brk_base);
-}
-
static unsigned long get_cmd_line_ptr(void)
{
unsigned long cmd_line_ptr = boot_params.hdr.cmd_line_ptr;
@@ -246,8 +236,6 @@ asmlinkage __visible void __init __noreturn x86_64_start_kernel(char * real_mode
vmemmap_base = __VMEMMAP_BASE_L5;
}
- clear_bss();
-
/*
* This needs to happen *before* kasan_early_init() because latter maps stuff
* into that page.
diff --git a/arch/x86/kernel/head_64.S b/arch/x86/kernel/head_64.S
index 7ed5520dd52e..15ef5ea52b9a 100644
--- a/arch/x86/kernel/head_64.S
+++ b/arch/x86/kernel/head_64.S
@@ -37,6 +37,8 @@
.code64
SYM_CODE_START_NOALIGN(startup_64)
UNWIND_HINT_END_OF_STACK
+ cld
+
/*
* At this point the CPU runs in 64bit mode CS.L = 1 CS.D = 0,
* and someone has loaded an identity mapped page table
@@ -61,6 +63,8 @@ SYM_CODE_START_NOALIGN(startup_64)
/* Set up the stack for verify_cpu() */
leaq __top_init_kernel_stack(%rip), %rsp
+ call clear_bss
+
/*
* Set up GSBASE.
* Note that on SMP the boot CPU uses the init data section until
@@ -140,6 +144,22 @@ SYM_CODE_START_NOALIGN(startup_64)
jmp *.Lcommon_startup_64(%rip)
SYM_CODE_END(startup_64)
+SYM_FUNC_START(clear_bss)
+ xorl %eax, %eax
+
+ leaq __bss_start(%rip), %rdi
+ leaq __bss_stop(%rip), %rcx
+ subq %rdi, %rcx
+ rep stosb
+
+ leaq __brk_base(%rip), %rdi
+ leaq __brk_limit(%rip), %rcx
+ subq %rdi, %rcx
+ rep stosb
+
+ RET
+SYM_FUNC_END(clear_bss)
+
__INITRODATA
SYM_DATA_LOCAL(.Lcommon_startup_64, .quad common_startup_64)
diff --git a/arch/x86/xen/enlighten_pv.c b/arch/x86/xen/enlighten_pv.c
index 2c64b388f616..6aa13d19c0a4 100644
--- a/arch/x86/xen/enlighten_pv.c
+++ b/arch/x86/xen/enlighten_pv.c
@@ -1334,8 +1334,6 @@ asmlinkage __visible void __init xen_start_kernel(struct start_info *si)
if (!si)
return;
- clear_bss();
-
xen_start_info = si;
__text_gen_insn(&early_xen_iret_patch,
diff --git a/arch/x86/xen/xen-head.S b/arch/x86/xen/xen-head.S
index 5dad6c51cdc3..9b56659246fe 100644
--- a/arch/x86/xen/xen-head.S
+++ b/arch/x86/xen/xen-head.S
@@ -31,6 +31,9 @@ SYM_CODE_START(startup_xen)
leaq __top_init_kernel_stack(%rip), %rsp
+ movq %rsi, %r15
+ call clear_bss
+
/*
* Set up GSBASE.
* Note that, on SMP, the boot cpu uses init data section until
@@ -41,7 +44,7 @@ SYM_CODE_START(startup_xen)
xorl %edx, %edx
wrmsr
- mov %rsi, %rdi
+ mov %r15, %rdi
call xen_start_kernel
SYM_CODE_END(startup_xen)
__FINIT
--
2.55.0