From: Brian Gerst <brgerst@gmail.com>
To: linux-kernel@vger.kernel.org, x86@kernel.org
Cc: Thomas Gleixner <tglx@linutronix.de>,
Borislav Petkov <bp@alien8.de>, Ard Biesheuvel <ardb@kernel.org>,
Juergen Gross <jgross@suse.com>,
Tom Lendacky <thomas.lendacky@amd.com>,
Brian Gerst <brgerst@gmail.com>
Subject: [PATCH 2/5] x86/boot/64: Clear BSS as early as possible
Date: Thu, 23 Jul 2026 23:02:53 -0400 [thread overview]
Message-ID: <20260724030256.232690-3-brgerst@gmail.com> (raw)
In-Reply-To: <20260724030256.232690-1-brgerst@gmail.com>
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@gmail.com>
---
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
next prev parent reply other threads:[~2026-07-24 3:03 UTC|newest]
Thread overview: 8+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-07-24 3:02 [PATCH 0/5] x86/boot: Early boot cleanups Brian Gerst
2026-07-24 3:02 ` [PATCH 1/5] x86/sme: Clear decrypted BSS separately Brian Gerst
2026-07-24 3:02 ` Brian Gerst [this message]
2026-07-24 11:04 ` [PATCH 2/5] x86/boot/64: Clear BSS as early as possible Nikolay Borisov
2026-07-24 11:34 ` Brian Gerst
2026-07-24 3:02 ` [PATCH 3/5] x86/boot: Remove hardcoded boot_param constants Brian Gerst
2026-07-24 3:02 ` [PATCH 4/5] x86/boot/64: Remove copy_bootdata() call Brian Gerst
2026-07-24 3:02 ` [PATCH 5/5] x86/boot/64: Copy boot parameters and command line earlier Brian Gerst
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20260724030256.232690-3-brgerst@gmail.com \
--to=brgerst@gmail.com \
--cc=ardb@kernel.org \
--cc=bp@alien8.de \
--cc=jgross@suse.com \
--cc=linux-kernel@vger.kernel.org \
--cc=tglx@linutronix.de \
--cc=thomas.lendacky@amd.com \
--cc=x86@kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox