From nobody Tue Feb 10 12:57:15 2026 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 4B2981922E7 for ; Mon, 25 Nov 2024 10:08:34 +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=1732529316; cv=none; b=MmDkINA4MwnzF8wKiZU7U+neRiHrr1aLDDrPfSi59XDesJZaeZ9vbD/Mz83MOJOpu0YJDRHRFvi+w8HZK6Mo+2o4MzixV2efXbW0gRxDUXGU29w8e6iy0QiurTW7fZ+dDK2WfA85y6Dyqn7yuIkJnWHxHHuhyfntCBzMNFkCbug= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1732529316; c=relaxed/simple; bh=FWVBv0ZSzwZVULdCCxypbPj8dEofVMCq5Doc6P4AXx4=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=tb25TsfEJI5GTvmofKpKABJbbcFrnkg1pDYQh+99WF+BprRGm45vOMZsCfwYVh+OSiL9Dn3LnDWHrFpJpnEuSTVvqPHA0M4Rsi4BChO4xvQrC5sf7Ys1LZcANRI84iwEKb5ex4IYOa3CHMBKfLAlUJQcFhvBc5D5EZ1fcGyIskM= 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=MJt0zpAK; 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="MJt0zpAK" 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:Cc:To:From: Reply-To:Content-Type:Content-ID:Content-Description; bh=kRNwMlvi/Q0U2ObjRznPAi0eKAWL0TLaUoagER3O75M=; b=MJt0zpAKeIFg21TTp5dxDMRdv6 Kb0+PFeeb4YjK78IgvKuU9F1YJRbmRu1xXgK2XMwfY173I0XdsPSMmzdkLUVzYcmK0Y1xzlgX6tV4 VrMlIs035DDatGI9KKHId4+R0q6mT5o+Io0r62+zJ6CuphUdfslcZhXzJT4q4hjGTe/QCFALhS/zp GM3Hoebj4hnN2TxYVne1nCdfA6N3tCCGsX/xTFXvkkz2V35+KbiK6mUKZZ1knsGsnz0tqFS8Gr8e9 4H8cpiGQyd/nK/j35SXddQ+CvWKvz1KEZb3NSa8t8m3oR0HY7WZHfokXXYIXYYe9D1qvBqeWtbary rHJqt7Aw==; 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 1tFW0z-0000000Biqz-0J7b; Mon, 25 Nov 2024 10:08:22 +0000 Received: from dwoodhou by i7.infradead.org with local (Exim 4.98 #2 (Red Hat Linux)) id 1tFW0z-000000000lE-2cyN; Mon, 25 Nov 2024 10:08:21 +0000 From: David Woodhouse To: kexec@lists.infradead.org Cc: Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , x86@kernel.org, "H. Peter Anvin" , David Woodhouse , "Kirill A. Shutemov" , Kai Huang , Nikolay Borisov , linux-kernel@vger.kernel.org, Simon Horman , Dave Young , Peter Zijlstra , jpoimboe@kernel.org, bsz@amazon.de Subject: [RFC PATCH v3 09/20] x86/kexec: Move relocate_kernel to kernel .data section Date: Mon, 25 Nov 2024 09:54:39 +0000 Message-ID: <20241125100815.2512-10-dwmw2@infradead.org> X-Mailer: git-send-email 2.47.0 In-Reply-To: <20241125100815.2512-1-dwmw2@infradead.org> References: <20241125100815.2512-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: David Woodhouse Now that the copy is executed instead of the original, the relocate_kernel page can live in the kernel's .text section. This will allow subsequent commits to actually add real data to it and clean up the code somewhat as well as making the control page ROX. Signed-off-by: David Woodhouse --- arch/x86/include/asm/sections.h | 1 + arch/x86/kernel/callthunks.c | 6 ++++++ arch/x86/kernel/machine_kexec_64.c | 4 +++- arch/x86/kernel/relocate_kernel_64.S | 7 +------ arch/x86/kernel/vmlinux.lds.S | 15 ++++++++++++++- 5 files changed, 25 insertions(+), 8 deletions(-) diff --git a/arch/x86/include/asm/sections.h b/arch/x86/include/asm/section= s.h index 3fa87e5e11ab..30e8ee7006f9 100644 --- a/arch/x86/include/asm/sections.h +++ b/arch/x86/include/asm/sections.h @@ -5,6 +5,7 @@ #include #include =20 +extern char __relocate_kernel_start[], __relocate_kernel_end[]; extern char __brk_base[], __brk_limit[]; extern char __end_rodata_aligned[]; =20 diff --git a/arch/x86/kernel/callthunks.c b/arch/x86/kernel/callthunks.c index 465647456753..51c3e0049152 100644 --- a/arch/x86/kernel/callthunks.c +++ b/arch/x86/kernel/callthunks.c @@ -139,9 +139,15 @@ static bool skip_addr(void *dest) return true; #endif #ifdef CONFIG_KEXEC_CORE +# ifdef CONFIG_X86_64 + if (dest >=3D (void *)__relocate_kernel_start && + dest < (void *)__relocate_kernel_end) + return true; +# else if (dest >=3D (void *)relocate_kernel && dest < (void*)relocate_kernel + KEXEC_CONTROL_CODE_MAX_SIZE) return true; +# endif #endif #ifdef CONFIG_XEN if (dest >=3D (void *)hypercall_page && diff --git a/arch/x86/kernel/machine_kexec_64.c b/arch/x86/kernel/machine_k= exec_64.c index 6fcf54e87d44..76a45a76e0fe 100644 --- a/arch/x86/kernel/machine_kexec_64.c +++ b/arch/x86/kernel/machine_kexec_64.c @@ -313,6 +313,8 @@ static void load_segments(void) int machine_kexec_prepare(struct kimage *image) { void *control_page =3D page_address(image->control_code_page); + unsigned long reloc_start =3D (unsigned long)__relocate_kernel_start; + unsigned long reloc_end =3D (unsigned long)__relocate_kernel_end; int result; =20 /* Setup the identity mapped 64bit page table */ @@ -320,7 +322,7 @@ int machine_kexec_prepare(struct kimage *image) if (result) return result; =20 - __memcpy(control_page, relocate_kernel, KEXEC_CONTROL_CODE_MAX_SIZE); + __memcpy(control_page, __relocate_kernel_start, reloc_end - reloc_start); =20 set_memory_x((unsigned long)control_page, 1); =20 diff --git a/arch/x86/kernel/relocate_kernel_64.S b/arch/x86/kernel/relocat= e_kernel_64.S index b48bd82843fd..01138f862c59 100644 --- a/arch/x86/kernel/relocate_kernel_64.S +++ b/arch/x86/kernel/relocate_kernel_64.S @@ -41,10 +41,8 @@ #define CP_PA_BACKUP_PAGES_MAP DATA(0x30) #define CP_VA_CONTROL_PAGE DATA(0x38) =20 - .text - .align PAGE_SIZE + .section .text.relocate_kernel,"ax"; .code64 -SYM_CODE_START_NOALIGN(relocate_range) SYM_CODE_START_NOALIGN(relocate_kernel) UNWIND_HINT_END_OF_STACK ANNOTATE_NOENDBR @@ -341,6 +339,3 @@ SYM_CODE_START_LOCAL_NOALIGN(swap_pages) ret int3 SYM_CODE_END(swap_pages) - - .skip KEXEC_CONTROL_CODE_MAX_SIZE - (. - relocate_kernel), 0xcc -SYM_CODE_END(relocate_range); diff --git a/arch/x86/kernel/vmlinux.lds.S b/arch/x86/kernel/vmlinux.lds.S index b8c5741d2fb4..1ff23a4bbf03 100644 --- a/arch/x86/kernel/vmlinux.lds.S +++ b/arch/x86/kernel/vmlinux.lds.S @@ -28,6 +28,7 @@ #include #include #include +#include =20 #undef i386 /* in case the preprocessor is a 32bit one */ =20 @@ -95,7 +96,18 @@ const_pcpu_hot =3D pcpu_hot; #define BSS_DECRYPTED =20 #endif - +#if defined(CONFIG_X86_64) && defined(CONFIG_KEXEC_CORE) +#define KEXEC_RELOCATE_KERNEL \ + . =3D ALIGN(0x100); \ + __relocate_kernel_start =3D .; \ + *(.text.relocate_kernel); \ + __relocate_kernel_end =3D .; + +ASSERT(__relocate_kernel_end - __relocate_kernel_start <=3D KEXEC_CONTROL_= CODE_MAX_SIZE, + "relocate_kernel code too large!") +#else +#define KEXEC_RELOCATE_KERNEL +#endif PHDRS { text PT_LOAD FLAGS(5); /* R_E */ data PT_LOAD FLAGS(6); /* RW_ */ @@ -181,6 +193,7 @@ SECTIONS =20 DATA_DATA CONSTRUCTORS + KEXEC_RELOCATE_KERNEL =20 /* rarely changed data like cpu maps */ READ_MOSTLY_DATA(INTERNODE_CACHE_BYTES) --=20 2.47.0