From nobody Tue May 21 13:04:02 2024 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (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 B94B081734; Wed, 17 Apr 2024 21:54:01 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713390841; cv=none; b=CgYq2Sa5ncewmq7Kl/pJugMnn+OMFkI/nqrv3jfNCP0VmOa4hod1deIT7D8D1J/TJiv0igHWrKO0n+IS+Vlf273yiM/m12fUA15KsPU+P4+aWMUQXIItKpx8daj1lxOhAk8SJhzltDHlPykmV/qPrjHoiBOeswTTGOWcoCUVOm0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713390841; c=relaxed/simple; bh=nj6IuYV9udEmOQvECHtrngWjjKkWXQbGOmaAIgtPHYA=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=MC1dKDBcvse8HXiBmzJOluBYDx5oGAOVwPlWQxQ+QVlv9Iwz9HJ/AdkB0SuI+0LlP51P9JNaXgLS4rkd9wY4jPCf5h3fQOFLo7gY1VW73AziNb63Lw7q/HQOCtNW5mz3wbY0DDOqiUmj7nOo3Gc26WNG81Br3QliZw47MAjMw48= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=VGFaPXXU; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="VGFaPXXU" Received: by smtp.kernel.org (Postfix) with ESMTPSA id BF9CBC116B1; Wed, 17 Apr 2024 21:54:00 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1713390841; bh=nj6IuYV9udEmOQvECHtrngWjjKkWXQbGOmaAIgtPHYA=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=VGFaPXXUmIkW6Tlkt3PGpz/07oiNJqYKB2SwV69x1kaVPe8kCXWsb7Q0y5QHKi785 RGqPZlWTx6t4BufOHgkDay91ja/+QHAJfqPkifUo/5Z38FEnOpjuEtVdTx1dhcFzTE 41hAAVqrmLRsI3X7kORW0toPVK93nqctb1//tfFDVaN2pfPc5LGhGCCLvYLrNuXSTS NaVkpR3KJZTpQHeO4CnQd/BCFn2z7M/T0HDOElQUpLNFGc9qvzWhz07oR6W/T7oHX6 8Z6SF7JSF7uluepfj5JMB83mnvW7WPM/FsuRb5XH5w3mlFzhVBQuGFgiDGcUJxa8Sp qF9bP9F9XojBQ== From: Nathan Chancellor Date: Wed, 17 Apr 2024 14:53:45 -0700 Subject: [PATCH 1/2] x86/purgatory: Add a linker script Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20240417-x86-fix-kexec-with-llvm-18-v1-1-5383121e8fb7@kernel.org> References: <20240417-x86-fix-kexec-with-llvm-18-v1-0-5383121e8fb7@kernel.org> In-Reply-To: <20240417-x86-fix-kexec-with-llvm-18-v1-0-5383121e8fb7@kernel.org> To: tglx@linutronix.de, mingo@redhat.com, bp@alien8.de, dave.hansen@linux.intel.com Cc: x86@kernel.org, hpa@zytor.com, ndesaulniers@google.com, morbo@google.com, justinstitt@google.com, song@kernel.org, ribalda@chromium.org, linux-kernel@vger.kernel.org, llvm@lists.linux.dev, patches@lists.linux.dev, ns <0n-s@users.noreply.github.com>, Nathan Chancellor X-Mailer: b4 0.14-dev X-Developer-Signature: v=1; a=openpgp-sha256; l=6028; i=nathan@kernel.org; h=from:subject:message-id; bh=nj6IuYV9udEmOQvECHtrngWjjKkWXQbGOmaAIgtPHYA=; b=owGbwMvMwCUmm602sfCA1DTG02pJDGkKLt993T59+ZxxYueKJqnn29VYza74f3W3bJ11wU3C6 M/RnPCqjlIWBjEuBlkxRZbqx6rHDQ3nnGW8cWoSzBxWJpAhDFycAjCRWQkMv5iE1C3vNfrcs7vU saDgmFP56xuLzq2y/2W/k6Hd12RT81lGhlu/A2r2vzmrfv3G//Dv6z5/NOZ/ErtN0VdRYNUaPXf l9cwA X-Developer-Key: i=nathan@kernel.org; a=openpgp; fpr=2437CB76E544CB6AB3D9DFD399739260CB6CB716 Commit 8652d44f466a ("kexec: support purgatories with .text.hot sections") added a warning when the purgatory has more than one .text section, which is unsupported. A couple of changes have been made to the x86 purgatory's Makefile to prevent the compiler from splitting the .text section as a result: 97b6b9cbba40 ("x86/purgatory: remove PGO flags") 75b2f7e4c9e0 ("x86/purgatory: Remove LTO flags") Unfortunately, there may be compiler optimizations that add other text sections that cannot be disabled. For example, starting with LLVM 18, large text is emitted in '.ltext', which happens for the purgatory due to commit e16c2983fba0 ("x86/purgatory: Change compiler flags from -mcmodel=3Dkernel to -mcmodel=3Dlarge to fix kexec relocation errors"), but there are out of line assembly files that use '.text'. $ llvm-readelf -S arch/x86/purgatory/purgatory.ro | rg ' .[a-z]?text' [ 1] .text PROGBITS 0000000000000000 000040 0000d0 0= 0 AX 0 0 16 [ 2] .ltext PROGBITS 0000000000000000 000110 0015a6 0= 0 AXl 0 0 16 To avoid the runtime warning when the purgatory has been built with LLVM 18, add a linker script that explicitly describes the sections of the purgatory.ro and use it to merge '.ltext' and '.lrodata' back into '.text' and '.rodata' to match the behavior of GCC and LLVM prior to the optimization, as the distinction between small and large text is not important in this case. This results in no warnings with '--orphan-handling=3Dwarn' with either GNU or LLVM toolchains and the resulting kernels can properly kexec other kernels. This linker script is based on arch/s390/purgatory/purgatory.lds.S and Ricardo Ribalda's prior attempt to add one for arch/x86 [1]. As a consequence of this change, the aforementioned flag changes can be reverted because the '.text.*' sections generated by those options will be combined properly by the linker script, which avoids the only reason they were added in the first place. kexec continues to work with LTO enabled. [1]: https://lore.kernel.org/20230321-kexec_clang16-v5-2-5563bf7c4173@chrom= ium.org/ Reported-by: ns <0n-s@users.noreply.github.com> Closes: https://github.com/ClangBuiltLinux/linux/issues/2016 Signed-off-by: Nathan Chancellor --- arch/x86/purgatory/.gitignore | 1 + arch/x86/purgatory/Makefile | 19 +++--------- arch/x86/purgatory/purgatory.lds.S | 63 ++++++++++++++++++++++++++++++++++= ++++ 3 files changed, 69 insertions(+), 14 deletions(-) diff --git a/arch/x86/purgatory/.gitignore b/arch/x86/purgatory/.gitignore index d2be1500671d..71bd99d98906 100644 --- a/arch/x86/purgatory/.gitignore +++ b/arch/x86/purgatory/.gitignore @@ -1 +1,2 @@ purgatory.chk +purgatory.lds diff --git a/arch/x86/purgatory/Makefile b/arch/x86/purgatory/Makefile index bc31863c5ee6..dfc030a4cca9 100644 --- a/arch/x86/purgatory/Makefile +++ b/arch/x86/purgatory/Makefile @@ -1,7 +1,7 @@ # SPDX-License-Identifier: GPL-2.0 OBJECT_FILES_NON_STANDARD :=3D y =20 -purgatory-y :=3D purgatory.o stack.o setup-x86_$(BITS).o sha256.o entry64.= o string.o +purgatory-y :=3D purgatory.o purgatory.lds stack.o setup-x86_$(BITS).o sha= 256.o entry64.o string.o =20 targets +=3D $(purgatory-y) PURGATORY_OBJS =3D $(addprefix $(obj)/,$(purgatory-y)) @@ -14,20 +14,11 @@ $(obj)/sha256.o: $(srctree)/lib/crypto/sha256.c FORCE =20 CFLAGS_sha256.o :=3D -D__DISABLE_EXPORTS -D__NO_FORTIFY =20 -# When profile-guided optimization is enabled, llvm emits two different -# overlapping text sections, which is not supported by kexec. Remove profi= le -# optimization flags. -KBUILD_CFLAGS :=3D $(filter-out -fprofile-sample-use=3D% -fprofile-use=3D%= ,$(KBUILD_CFLAGS)) - -# When LTO is enabled, llvm emits many text sections, which is not support= ed -# by kexec. Remove -flto=3D* flags. -KBUILD_CFLAGS :=3D $(filter-out $(CC_FLAGS_LTO),$(KBUILD_CFLAGS)) - # When linking purgatory.ro with -r unresolved symbols are not checked, # also link a purgatory.chk binary without -r to check for unresolved symb= ols. -PURGATORY_LDFLAGS :=3D -e purgatory_start -z nodefaultlib -LDFLAGS_purgatory.ro :=3D -r $(PURGATORY_LDFLAGS) -LDFLAGS_purgatory.chk :=3D $(PURGATORY_LDFLAGS) +PURGATORY_LDFLAGS :=3D -z nodefaultlib +LDFLAGS_purgatory.ro :=3D -r $(PURGATORY_LDFLAGS) -T +LDFLAGS_purgatory.chk :=3D -e purgatory_start $(PURGATORY_LDFLAGS) targets +=3D purgatory.ro purgatory.chk =20 # Sanitizer, etc. runtimes are unavailable and cannot be linked here. @@ -80,7 +71,7 @@ CFLAGS_string.o +=3D $(PURGATORY_CFLAGS) =20 asflags-remove-y +=3D $(foreach x, -g -gdwarf-4 -gdwarf-5, $(x) -Wa,$(x)) =20 -$(obj)/purgatory.ro: $(PURGATORY_OBJS) FORCE +$(obj)/purgatory.ro: $(obj)/purgatory.lds $(PURGATORY_OBJS) FORCE $(call if_changed,ld) =20 $(obj)/purgatory.chk: $(obj)/purgatory.ro FORCE diff --git a/arch/x86/purgatory/purgatory.lds.S b/arch/x86/purgatory/purgat= ory.lds.S new file mode 100644 index 000000000000..4fb155942642 --- /dev/null +++ b/arch/x86/purgatory/purgatory.lds.S @@ -0,0 +1,63 @@ +/* SPDX-License-Identifier: GPL-2.0 */ + +#include +#include + +OUTPUT_FORMAT(CONFIG_OUTPUT_FORMAT) + +#undef i386 + +#ifdef CONFIG_X86_64 +OUTPUT_ARCH(i386:x86-64) +#else +OUTPUT_ARCH(i386) +#endif + +ENTRY(purgatory_start) + +SECTIONS +{ + . =3D 0; + + .kexec-purgatory : { + *(.kexec-purgatory) + } + + .text : { + _text =3D .; + *(.text .text.*) + *(.ltext .ltext.*) + _etext =3D .; + } + + .rodata : { + _rodata =3D .; + *(.rodata .rodata.*) + *(.lrodata .lrodata.*) + _erodata =3D .; + } + + .data : { + _data =3D .; + *(.data .data.*) + _edata =3D .; + } + + . =3D ALIGN(L1_CACHE_BYTES); + .bss : { + _bss =3D .; + *(.bss .bss.*) + *(COMMON) + . =3D ALIGN(8); /* For convenience during zeroing */ + _ebss =3D .; + } + _end =3D .; + + ELF_DETAILS + + DISCARDS + /DISCARD/ : { + *(.note.GNU-stack .note.gnu.property) + *(.llvm_addrsig) + } +} --=20 2.44.0 From nobody Tue May 21 13:04:02 2024 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (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 B099A13C3F9; Wed, 17 Apr 2024 21:54:02 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713390842; cv=none; b=PsmZCtv5iOONTEZcInN1q0GOUQ2BgrqwOlma/6yqFqFbRwoAxtB9FflBrkabaSQAnfrbhyrBlkqltaQPG/RB/0/K32JuBWg7MlcwLH4kXkDZBXBRNZ8jSQ2InamlmeV6ol0VIq2QaxfHavA6mVcxxHXC47LFGUr4KD3iLQCmSxE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713390842; c=relaxed/simple; bh=y/oIuT0t5fQdzfr7m5A1SD/C2aOn4sapsexfuFNbY9A=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=qDd66lJiwvBbs2qyQ2tBfXSPP+/fNddUzT+WrmDbKU1W6FsGHIxzt7pG7ym30e7lHddF6j307jfZOFTkTT0vkY2TnNFvhKiNwmTebh4ztNbywNaQkB6UDIn6Lh8NLjlr5uroilQtMDDBKJcjA6ZqJ2Cyi/rNybZJIIcbri/+rgI= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=Uma00F9J; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="Uma00F9J" Received: by smtp.kernel.org (Postfix) with ESMTPSA id C5350C4AF08; Wed, 17 Apr 2024 21:54:01 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1713390842; bh=y/oIuT0t5fQdzfr7m5A1SD/C2aOn4sapsexfuFNbY9A=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=Uma00F9JLR/MDDk8Ai4ss95HRMdT+/3/4abNgJvFKaR+uQOI07KPlMMMwnPDJHtrL twPGdo+SrIY+zn0ogxQob6IJxEQp6VUHInw+8Nj93souXMgPXJWyR5zrkQIFYOPFlc o9Xq5uHoy3u7EBoLly1bWdX6+gdOs5orvNsgW6lFyc/MX7nMOgZuLKzZ4G2YBHqdCn Ilxj2xiUfPiP03tLDXUHCc76yM4x9nzQWM96/mznvTUcMtPAV3Ww7JqIsF6hZKFnXL 8cKlRqwuuf6u6wRZaaJIrh6vlr5kNW/Iu7j9i0Y40IWw6bWFsYRTL7hprddMUFgaB/ HtzgAjksBrGSA== From: Nathan Chancellor Date: Wed, 17 Apr 2024 14:53:46 -0700 Subject: [PATCH 2/2] x86/purgatory: Enable orphan section warnings Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20240417-x86-fix-kexec-with-llvm-18-v1-2-5383121e8fb7@kernel.org> References: <20240417-x86-fix-kexec-with-llvm-18-v1-0-5383121e8fb7@kernel.org> In-Reply-To: <20240417-x86-fix-kexec-with-llvm-18-v1-0-5383121e8fb7@kernel.org> To: tglx@linutronix.de, mingo@redhat.com, bp@alien8.de, dave.hansen@linux.intel.com Cc: x86@kernel.org, hpa@zytor.com, ndesaulniers@google.com, morbo@google.com, justinstitt@google.com, song@kernel.org, ribalda@chromium.org, linux-kernel@vger.kernel.org, llvm@lists.linux.dev, patches@lists.linux.dev, Nathan Chancellor X-Mailer: b4 0.14-dev X-Developer-Signature: v=1; a=openpgp-sha256; l=1061; i=nathan@kernel.org; h=from:subject:message-id; bh=y/oIuT0t5fQdzfr7m5A1SD/C2aOn4sapsexfuFNbY9A=; b=owGbwMvMwCUmm602sfCA1DTG02pJDGkKLt8PhWsHZx2oyzv68Upp7qGDh1ebxX3UNlt6Ss81t fKyWh93RykLgxgXg6yYIkv1Y9XjhoZzzjLeODUJZg4rE8gQBi5OAZgIpxPDP7vJTiUzA7wNrmd/ cGS8+5dXT+uqyd9JTzwfT9//UJmB+z8jw1mOjeoPI0Mr/CfITf4gf5en9bnT2/hr/FFWV97LuRW o8QEA X-Developer-Key: i=nathan@kernel.org; a=openpgp; fpr=2437CB76E544CB6AB3D9DFD399739260CB6CB716 Now that the purgatory has a linker script that explicitly describes all of its sections, turn on orphan section warnings for it so that new sections do not appear without notice. Signed-off-by: Nathan Chancellor --- arch/x86/purgatory/Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/arch/x86/purgatory/Makefile b/arch/x86/purgatory/Makefile index dfc030a4cca9..a6c8239abcba 100644 --- a/arch/x86/purgatory/Makefile +++ b/arch/x86/purgatory/Makefile @@ -17,7 +17,7 @@ CFLAGS_sha256.o :=3D -D__DISABLE_EXPORTS -D__NO_FORTIFY # When linking purgatory.ro with -r unresolved symbols are not checked, # also link a purgatory.chk binary without -r to check for unresolved symb= ols. PURGATORY_LDFLAGS :=3D -z nodefaultlib -LDFLAGS_purgatory.ro :=3D -r $(PURGATORY_LDFLAGS) -T +LDFLAGS_purgatory.ro :=3D --orphan-handling=3D$(CONFIG_LD_ORPHAN_WARN_LEVE= L) -r $(PURGATORY_LDFLAGS) -T LDFLAGS_purgatory.chk :=3D -e purgatory_start $(PURGATORY_LDFLAGS) targets +=3D purgatory.ro purgatory.chk =20 --=20 2.44.0