From nobody Thu Dec 18 00:07:21 2025 Received: from casper.infradead.org (casper.infradead.org [90.155.50.34]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 1BCC7218EBF for ; Thu, 9 Jan 2025 14:08:32 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=90.155.50.34 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736431714; cv=none; b=I/i46nudsjzxDwi0az1O7NpLp4Zmlu7MXiyg9mSBBabYuQiu6qvVKAusphiUX8BRDnLBiIWfyldoFWOhlTg46KzJBh5l++HLLPep8l/a5ds1hz7M8snvvUjZQtCc1mJiEL3q249qGnKDGD2DozlIXM5bIXrq+xQ/fwQsP4ScVYQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736431714; c=relaxed/simple; bh=rpWT/GDc1n0O+aerKv/DMHw5wA2p1TibxIbW3iHqs7Q=; h=From:To:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=WHRiDLdXE544fYoIjhnZsIuhhESkzCqm3LBdfunD1CEoQaWhz68AmnuhsCzpWbkvhErbhFeSjj2fEjupgZ0ncX7i8F7JPmHufLZRlzLctQbT2FLBjdrrahHZ/3pcFmlKtqx8u24e+/jcYS1o7xTVmNOoON30VIlJiZKQPNIDZzA= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=infradead.org; spf=none smtp.mailfrom=casper.srs.infradead.org; dkim=pass (2048-bit key) header.d=infradead.org header.i=@infradead.org header.b=Qg7ot0dX; arc=none smtp.client-ip=90.155.50.34 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=infradead.org Authentication-Results: smtp.subspace.kernel.org; spf=none smtp.mailfrom=casper.srs.infradead.org Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=infradead.org header.i=@infradead.org header.b="Qg7ot0dX" DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=casper.20170209; h=Sender:Content-Transfer-Encoding: MIME-Version:References:In-Reply-To:Message-ID:Date:Subject:To:From:Reply-To: Cc:Content-Type:Content-ID:Content-Description; bh=lpLME9O4Hrgm0YpKuGQMslbI8cpLYo5TkEgKVoEpH/Y=; b=Qg7ot0dXIkUrzgylpdi1LK+NTp 4UQiZZu6ioGgls8rwTSSTJmJjZ6u5fqTryHrC+c0zH+aqinRTMxTU0nLmFMhrs6pCFCy8q1UxEc4q X2S2KTeijTnBan2P4L26rBVJM/VokS5Ap6i3OZ65qaSYYapabPf/x1b4bNgJVJf8M2/ifNklOKymF 6J6atdSGWLL4LtAYJZKhpdJb6lHv0p9018qxP9jR38r0LulxMCaz960dr9FatHXgAQ7ra1kzITO10 QhE0gWj4986pnc8tZwPrVZgbxYgNHLxyaMcREpHkLGc80HZxfgPuYsi7D7qFkBPOUuXtwdTTlrMng YE0vBURw==; Received: from [2001:8b0:10b:1::ebe] (helo=i7.infradead.org) by casper.infradead.org with esmtpsa (Exim 4.98 #2 (Red Hat Linux)) id 1tVtCZ-00000006IQI-3C1i; Thu, 09 Jan 2025 14:07:59 +0000 Received: from dwoodhou by i7.infradead.org with local (Exim 4.98 #2 (Red Hat Linux)) id 1tVtCY-0000000Bx6F-3GY5; Thu, 09 Jan 2025 14:07:58 +0000 From: David Woodhouse To: Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , x86@kernel.org, "H . Peter Anvin" , Eric Biederman , David Woodhouse , Sourabh Jain , Hari Bathini , Michael Ellerman , Thomas Zimmermann , Andrew Morton , Baoquan He , Yuntao Wang , David Kaplan , Tao Liu , "Kirill A . Shutemov" , Kai Huang , Ard Biesheuvel , Josh Poimboeuf , Breno Leitao , Wei Yang , Rong Xu , =?UTF-8?q?Thomas=20Wei=C3=9Fschuh?= , linux-kernel@vger.kernel.org, kexec@lists.infradead.org, Simon Horman , Dave Young , Peter Zijlstra , bsz@amazon.de, nathan@kernel.org Subject: [PATCH v2 5/9] x86/kexec: Fix location of relocate_kernel with -ffunction-sections Date: Thu, 9 Jan 2025 14:04:17 +0000 Message-ID: <20250109140757.2841269-6-dwmw2@infradead.org> X-Mailer: git-send-email 2.47.0 In-Reply-To: <20250109140757.2841269-1-dwmw2@infradead.org> References: <20250109140757.2841269-1-dwmw2@infradead.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Sender: David Woodhouse X-SRS-Rewrite: SMTP reverse-path rewritten from by casper.infradead.org. See http://www.infradead.org/rpr.html Content-Type: text/plain; charset="utf-8" From: Nathan Chancellor After commit cb33ff9e063c ("x86/kexec: Move relocate_kernel to kernel .data section"), kernels configured with an option that uses -ffunction-sections, such as CONFIG_LTO_CLANG, crash when kexecing because the value of relocate_kernel does not match the value of __relocate_kernel_start so incorrect code gets copied via machine_kexec_prepare(). $ llvm-nm good-vmlinux &| rg relocate_kernel ffffffff83280d41 T __relocate_kernel_end ffffffff83280b00 T __relocate_kernel_start ffffffff83280b00 T relocate_kernel $ llvm-nm bad-vmlinux &| rg relocate_kernel ffffffff83266100 D __relocate_kernel_end ffffffff83266100 D __relocate_kernel_start ffffffff8120b0d8 T relocate_kernel When -ffunction-sections is enabled, TEXT_MAIN matches on '.text.[0-9a-zA-Z_]*' to coalesce the function specific functions back into .text during link time after they have been optimized. Due to the placement of TEXT_TEXT before KEXEC_RELOCATE_KERNEL in the x86 linker script, the .text.relocate_kernel section ends up in .text instead of .data. Use a second dot in the relocate_kernel section name to avoid matching on TEXT_MAIN, which matches a similar situation that happened in commit 79cd2a11224e ("x86/retpoline,kprobes: Fix position of thunk sections with CONFIG_LTO_CLANG"), which allows kexec to function properly. While .data.relocate_kernel still ends up in the .data section via DATA_MAIN -> DATA_DATA, ensure it is located with the .text.relocate_kernel section as intended by performing the same transformation. Fixes: cb33ff9e063c ("x86/kexec: Move relocate_kernel to kernel .data secti= on") Fixes: 8dbec5c77bc3 ("x86/kexec: Add data section to relocate_kernel") Signed-off-by: Nathan Chancellor Signed-off-by: David Woodhouse --- arch/x86/kernel/relocate_kernel_64.S | 6 +++--- arch/x86/kernel/vmlinux.lds.S | 4 ++-- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/arch/x86/kernel/relocate_kernel_64.S b/arch/x86/kernel/relocat= e_kernel_64.S index a95691b42c5c..14ed40b7f322 100644 --- a/arch/x86/kernel/relocate_kernel_64.S +++ b/arch/x86/kernel/relocate_kernel_64.S @@ -23,11 +23,11 @@ #define PAGE_ATTR (_PAGE_PRESENT | _PAGE_RW | _PAGE_ACCESSED | _PAGE_DIRTY) =20 /* - * The .text.relocate_kernel and .data.relocate_kernel sections are copied + * The .text..relocate_kernel and .data..relocate_kernel sections are copi= ed * into the control page, and the remainder of the page is used as the sta= ck. */ =20 - .section .data.relocate_kernel,"a"; + .section .data..relocate_kernel,"a"; /* Minimal CPU state */ SYM_DATA_LOCAL(saved_rsp, .quad 0) SYM_DATA_LOCAL(saved_cr0, .quad 0) @@ -39,7 +39,7 @@ SYM_DATA(kexec_pa_table_page, .quad 0) SYM_DATA(kexec_pa_swap_page, .quad 0) SYM_DATA_LOCAL(pa_backup_pages_map, .quad 0) =20 - .section .text.relocate_kernel,"ax"; + .section .text..relocate_kernel,"ax"; .code64 SYM_CODE_START_NOALIGN(relocate_kernel) UNWIND_HINT_END_OF_STACK diff --git a/arch/x86/kernel/vmlinux.lds.S b/arch/x86/kernel/vmlinux.lds.S index 0c893997f023..63ff60a11be5 100644 --- a/arch/x86/kernel/vmlinux.lds.S +++ b/arch/x86/kernel/vmlinux.lds.S @@ -100,8 +100,8 @@ const_pcpu_hot =3D pcpu_hot; #define KEXEC_RELOCATE_KERNEL \ . =3D ALIGN(0x100); \ __relocate_kernel_start =3D .; \ - *(.text.relocate_kernel); \ - *(.data.relocate_kernel); \ + *(.text..relocate_kernel); \ + *(.data..relocate_kernel); \ __relocate_kernel_end =3D .; =20 ASSERT(__relocate_kernel_end - __relocate_kernel_start <=3D KEXEC_CONTROL_= CODE_MAX_SIZE, --=20 2.47.0