From nobody Wed Dec 17 21:28:10 2025 Received: from galois.linutronix.de (Galois.linutronix.de [193.142.43.55]) (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 23324200BAB; Fri, 6 Dec 2024 10:16:49 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=193.142.43.55 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1733480213; cv=none; b=kK20ziZljEYkXsbQtvGOPBCFzp2Cs21KSAMdmoqPEGZFVskF4WUWp87UKiw5bfRaskUbY6lw2Yi2dAOpJTQd8YDdckb+mkYrEiV+/Q1N1k9JeagAk03w2yaTCdzvDb/4e9MHD6WK+yqMl+fFtik/scvTOSodwov4dILSjw/0A5Y= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1733480213; c=relaxed/simple; bh=9/NEJUDEDT1rKAy3cCiAr46CCRvs+WHOCAp6xM0LRVA=; h=Date:From:To:Subject:Cc:In-Reply-To:References:MIME-Version: Message-ID:Content-Type; b=TvZFf7ISRu4NzcDtGmyicJyZzcb/XsWK9ODXECu3sn5WqOcep/r4zXgJ3aVQ3odqdl8XhFLdoLpXwJWhUnb9GBMe63Sr2EKMFQMK9sjxU+DB2OsnE2JmIcuCGzFgqLBZ4RHRpyontfe56EcOFkqa/jDPxUM1e9Mv+SfkX12NSTs= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linutronix.de; spf=pass smtp.mailfrom=linutronix.de; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=StMPYouJ; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=tLiG6CcM; arc=none smtp.client-ip=193.142.43.55 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linutronix.de Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linutronix.de Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="StMPYouJ"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="tLiG6CcM" Date: Fri, 06 Dec 2024 10:16:47 -0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1733480208; h=from:from:sender:sender:reply-to:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=MH16SAvwMqR2uP/bXkPRKLemXxdVVS/Zt8Khw+GWdds=; b=StMPYouJjfD+p0X2rEoVXs5ehIHO1unjRmqNVgdq3jJmON51N9PKUyRfwh/UAWQbobJDFW HDGr3QOJuwqR/Et6GE88hMLKfwXjp2I6b6bbBr6z8T+SuhPiQ4ecQQIKtDH8lpI3jFPJDZ Fz3gp1Jg/V85cVlNYcNN9wTQgUP/l/0eL5k4lkNsE2nrxDMCIxtXCn89XXWINXPQcehNU6 Y6QpmzlMvYAT0Me/1vSPVl+nc9FVWm6Gc5PHoArw+Xs4eeET7+lz3aQSf/XZkR5dZeeMeq HTT5Qjtkh5TOx1CtTUua091jueDG9r2l0Hr8xQthZdoomWnFMPNkNuDmIW2XUg== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1733480208; h=from:from:sender:sender:reply-to:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=MH16SAvwMqR2uP/bXkPRKLemXxdVVS/Zt8Khw+GWdds=; b=tLiG6CcMCnyCzyQTAI7ZFMw2u5ZDcwUAA1bCrCmvsxY9ZOn7lvBGGPYkws7Je3KpNSevhh NUKGkREMpjKiWjAg== From: "tip-bot2 for David Woodhouse" Sender: tip-bot2@linutronix.de Reply-to: linux-kernel@vger.kernel.org To: linux-tip-commits@vger.kernel.org Subject: [tip: x86/boot] x86/kexec: Invoke copy of relocate_kernel() instead of the original Cc: David Woodhouse , Ingo Molnar , Baoquan He , Vivek Goyal , Dave Young , Eric Biederman , Ard Biesheuvel , "H. Peter Anvin" , x86@kernel.org, linux-kernel@vger.kernel.org In-Reply-To: <20241205153343.3275139-8-dwmw2@infradead.org> References: <20241205153343.3275139-8-dwmw2@infradead.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Message-ID: <173348020740.412.15453271806956709702.tip-bot2@tip-bot2> Robot-ID: Robot-Unsubscribe: Contact to get blacklisted from these emails Precedence: bulk Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable The following commit has been merged into the x86/boot branch of tip: Commit-ID: eeebbde57113730db7b3ec7380ada61a0193d27c Gitweb: https://git.kernel.org/tip/eeebbde57113730db7b3ec7380ada61a0= 193d27c Author: David Woodhouse AuthorDate: Thu, 05 Dec 2024 15:05:13=20 Committer: Ingo Molnar CommitterDate: Fri, 06 Dec 2024 10:41:59 +01:00 x86/kexec: Invoke copy of relocate_kernel() instead of the original This currently calls set_memory_x() from machine_kexec_prepare() just like the 32-bit version does. That's actually a bit earlier than I'd like, as it leaves the page RWX all the time the image is even *loaded*. Subsequent commits will eliminate all the writes to the page between the point it's marked executable in machine_kexec_prepare() the time that relocate_kernel() is running and has switched to the identmap %cr3, so that it can be ROX. But that can't happen until it's moved to the .data section of the kernel, and *that* can't happen until we start executing the copy instead of executing it in place in the kernel .text. So break the circular dependency in those commits by letting it be RWX for now. Signed-off-by: David Woodhouse Signed-off-by: Ingo Molnar Cc: Baoquan He Cc: Vivek Goyal Cc: Dave Young Cc: Eric Biederman Cc: Ard Biesheuvel Cc: "H. Peter Anvin" Link: https://lore.kernel.org/r/20241205153343.3275139-8-dwmw2@infradead.org --- arch/x86/kernel/machine_kexec_64.c | 30 +++++++++++++++++++++------ arch/x86/kernel/relocate_kernel_64.S | 5 ++++- 2 files changed, 28 insertions(+), 7 deletions(-) diff --git a/arch/x86/kernel/machine_kexec_64.c b/arch/x86/kernel/machine_k= exec_64.c index 3a4cbac..9567347 100644 --- a/arch/x86/kernel/machine_kexec_64.c +++ b/arch/x86/kernel/machine_kexec_64.c @@ -157,7 +157,12 @@ static int init_transition_pgtable(struct kimage *imag= e, pgd_t *pgd, pmd_t *pmd; pte_t *pte; =20 - vaddr =3D (unsigned long)relocate_kernel; + /* + * For the transition to the identity mapped page tables, the control + * code page also needs to be mapped at the virtual address it starts + * off running from. + */ + vaddr =3D (unsigned long)__va(control_page); paddr =3D control_page; pgd +=3D pgd_index(vaddr); if (!pgd_present(*pgd)) { @@ -311,11 +316,17 @@ int machine_kexec_prepare(struct kimage *image) =20 __memcpy(control_page, relocate_kernel, KEXEC_CONTROL_CODE_MAX_SIZE); =20 + set_memory_x((unsigned long)control_page, 1); + return 0; } =20 void machine_kexec_cleanup(struct kimage *image) { + void *control_page =3D page_address(image->control_code_page); + + set_memory_nx((unsigned long)control_page, 1); + free_transition_pgtable(image); } =20 @@ -325,6 +336,11 @@ void machine_kexec_cleanup(struct kimage *image) */ void machine_kexec(struct kimage *image) { + unsigned long (*relocate_kernel_ptr)(unsigned long indirection_page, + unsigned long page_list, + unsigned long start_address, + unsigned int preserve_context, + unsigned int host_mem_enc_active); unsigned long page_list[PAGES_NR]; unsigned int host_mem_enc_active; int save_ftrace_enabled; @@ -371,6 +387,8 @@ void machine_kexec(struct kimage *image) page_list[PA_SWAP_PAGE] =3D (page_to_pfn(image->swap_page) << PAGE_SHIFT); =20 + relocate_kernel_ptr =3D control_page; + /* * The segment registers are funny things, they have both a * visible and an invisible part. Whenever the visible part is @@ -390,11 +408,11 @@ void machine_kexec(struct kimage *image) native_gdt_invalidate(); =20 /* now call it */ - image->start =3D relocate_kernel((unsigned long)image->head, - (unsigned long)page_list, - image->start, - image->preserve_context, - host_mem_enc_active); + image->start =3D relocate_kernel_ptr((unsigned long)image->head, + (unsigned long)page_list, + image->start, + image->preserve_context, + host_mem_enc_active); =20 #ifdef CONFIG_KEXEC_JUMP if (image->preserve_context) diff --git a/arch/x86/kernel/relocate_kernel_64.S b/arch/x86/kernel/relocat= e_kernel_64.S index ca7f1e1..d0a87b3 100644 --- a/arch/x86/kernel/relocate_kernel_64.S +++ b/arch/x86/kernel/relocate_kernel_64.S @@ -39,6 +39,7 @@ #define CP_PA_TABLE_PAGE DATA(0x20) #define CP_PA_SWAP_PAGE DATA(0x28) #define CP_PA_BACKUP_PAGES_MAP DATA(0x30) +#define CP_VA_CONTROL_PAGE DATA(0x38) =20 .text .align PAGE_SIZE @@ -99,6 +100,7 @@ SYM_CODE_START_NOALIGN(relocate_kernel) movq %r9, CP_PA_TABLE_PAGE(%r11) movq %r10, CP_PA_SWAP_PAGE(%r11) movq %rdi, CP_PA_BACKUP_PAGES_MAP(%r11) + movq %r11, CP_VA_CONTROL_PAGE(%r11) =20 /* Save the preserve_context to %r11 as swap_pages clobbers %rcx. */ movq %rcx, %r11 @@ -235,7 +237,8 @@ SYM_CODE_START_LOCAL_NOALIGN(identity_mapped) movq %rax, %cr3 lea PAGE_SIZE(%r8), %rsp call swap_pages - movq $virtual_mapped, %rax + movq CP_VA_CONTROL_PAGE(%r8), %rax + addq $(virtual_mapped - relocate_kernel), %rax pushq %rax ANNOTATE_UNRET_SAFE ret