From nobody Fri Oct 3 12:25:00 2025 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) (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 7A75B2EC096 for ; Mon, 1 Sep 2025 16:09:43 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.129.124 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1756742985; cv=none; b=dHzcsozc0uZmbVXW3gPlwl/1dyB+lUV8qtLtfOwH/sjJd5c13Tqx0Y2b5amnUV8HmkosfcUTMwrZU0ZYtJpmqpYmhbyZ/eyQweDqm/gb8On0MSbRMZrZr6cMfezsKE4I2RqGBItEPnc6tKRuVzPGXP7o9OLMPwj2eaOGyEIozT4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1756742985; c=relaxed/simple; bh=8o9MSeGyjka2YIkOKa8nP2VBMBZdX1wxLIA+kQOxqmY=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=YbtDnaNG/KotDcMEPAu1Bvc3GnrgPNMHNrvDPeb29jKNArzd+giwdts6bJkmkiWYKFkRQXk5xvX+U4+cIFLhVDM39LBSqiTejtgeZ0Qtv7JNCKXm51Zi+QGJCoLazRLk75cL/p/qVdxQ6XJHGwcI0ENGDRAFWdHuqZVyU0Ld85c= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=UgcAyHFb; arc=none smtp.client-ip=170.10.129.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="UgcAyHFb" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1756742982; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=/ILG8lOYeP81n1upiKDtnLYC19g1Qye1JLya30e5LpQ=; b=UgcAyHFbt5ommCarxCXIgasjfhj5L8Fk+B58RbA/w11yKM4GlfRbXNtLZgEg2PNO6XX9J1 aGLTQq7QHuPaDI1bw344DATofJKC4XijvQlZavlHvw1Fa5HNnIgXJwrqTlakP3+1vWt02i Ftyrkp4NyxEp8nzhtQQWZlxy67hOqQk= Received: from mail-qt1-f199.google.com (mail-qt1-f199.google.com [209.85.160.199]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-683-oKOHndDoP36ewngBwgvvKw-1; Mon, 01 Sep 2025 12:09:41 -0400 X-MC-Unique: oKOHndDoP36ewngBwgvvKw-1 X-Mimecast-MFC-AGG-ID: oKOHndDoP36ewngBwgvvKw_1756742981 Received: by mail-qt1-f199.google.com with SMTP id d75a77b69052e-4b31825af12so52656421cf.0 for ; Mon, 01 Sep 2025 09:09:41 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1756742980; x=1757347780; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=/ILG8lOYeP81n1upiKDtnLYC19g1Qye1JLya30e5LpQ=; b=uFnhllDZS3IUN5SKde4C/XXkLAzPQrQA2eldR8WZd84Y0+tf2m5vKmxPBzkkjJ6RSo C4uUM406IDqBA2LBIeKUIJ+Y1+CjO/WT54A96ITPzVBtDBFdleVS7HiqFLpzOhqkKQI+ 6mJRhgYco2MlbOtXm1t41y/jwcqkiFl19tYrTdN4iumcml+kzGTwHSFvpl3jYfjUn6Bs dhL94GsCaR+3YUipkORpVDchU2ERRzzJTWSPwIP7+jD2q5mG7y5XIqJs3v21awbpHiK9 HPAyc3TDTg9q/XEtXm+0y+M/FC+rlwp4ip/uEd+gAEXJqLnCEs9dLLSB4DB9xQj7s+mm QJgA== X-Gm-Message-State: AOJu0YwX41Vt2PYg2V4rsXA7sMqRPNLV84JkYOyqpVMLB4/DS9upwwGv 5KOhsftZ2rhXWzePOYYlfI3EO2qf4XsEw5CrlMbydVxXa5RPrr12RT/OtiCy43MkpcDiyU4l6EH s092Ze+wdgUCToMaIxkJJ0ZYsMTiyEat1JKaTC3FtI+JkDco3HZkxuP4gbDEox60aJFjOggP4XM uLsp3mxRS2dtEFsHDbeEPAPm6ibh1+kjhZO/iBNT6d5ma2crH/nA== X-Gm-Gg: ASbGncsRhOuXQWcjJY7PZKv0rNsREZWI8MUfPU3RLNWe5zblCnENJLq75Vv7LpgbLcd KsRfwcoBXan4vrp2z/uXBZPxlgB4ANYRgAPekZZxDtfj/RQtfTzlfYdsP8K34LJYzZegTnXRbp9 Z6IjttImzOKALfAlFVT3oK32QpusUIl6P4iCqRzpfUeuF+t2tE+EgPJORoxhezXkF5cqfvFjc5g G2BcVvqnAmUoEmVTAvlrXN7aq8kO7A8QzN8qMQvJxzkq8IpXDSFHNq5Y038pl4ARJ+NClREt9GU apNhgHSxz6kxGPpIwDOYc7rTy4IUeGgKAvRm6XsMUf7FXJmRUK3mUtg7gc2U/qerDx/KmNX6klH +CgyAqCMplU00CwVnNBi3qkvsXuD/3zl6topswJ48VDxDmVQvn7N+i4LYw7S5MN4UZuah X-Received: by 2002:a05:622a:138b:b0:4b0:6c7c:a955 with SMTP id d75a77b69052e-4b31da15393mr88333101cf.35.1756742980259; Mon, 01 Sep 2025 09:09:40 -0700 (PDT) X-Google-Smtp-Source: AGHT+IHDti1nmXO9Q+sLEIfixu7lgNBZ2vr81Q1E5oR04YXTrIW3a1NX5lesAlwj4ITitS6AR6+hhQ== X-Received: by 2002:a05:622a:138b:b0:4b0:6c7c:a955 with SMTP id d75a77b69052e-4b31da15393mr88332171cf.35.1756742979465; Mon, 01 Sep 2025 09:09:39 -0700 (PDT) Received: from [10.201.49.111] (nat-pool-mxp-t.redhat.com. [149.6.153.186]) by smtp.gmail.com with ESMTPSA id af79cd13be357-7fc14849559sm686458885a.41.2025.09.01.09.09.35 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 01 Sep 2025 09:09:38 -0700 (PDT) From: Paolo Bonzini To: linux-kernel@vger.kernel.org, kvm@vger.kernel.org Cc: dave.hansen@intel.com, bp@alien8.de, tglx@linutronix.de, peterz@infradead.org, mingo@redhat.com, hpa@zytor.com, thomas.lendacky@amd.com, x86@kernel.org, kas@kernel.org, rick.p.edgecombe@intel.com, dwmw@amazon.co.uk, kai.huang@intel.com, seanjc@google.com, reinette.chatre@intel.com, isaku.yamahata@intel.com, dan.j.williams@intel.com, ashish.kalra@amd.com, nik.borisov@suse.com, chao.gao@intel.com, sagis@google.com, farrah.chen@intel.com Subject: [PATCH 1/7] x86/kexec: Consolidate relocate_kernel() function parameters Date: Mon, 1 Sep 2025 18:09:24 +0200 Message-ID: <20250901160930.1785244-2-pbonzini@redhat.com> X-Mailer: git-send-email 2.51.0 In-Reply-To: <20250901160930.1785244-1-pbonzini@redhat.com> References: <20250901160930.1785244-1-pbonzini@redhat.com> 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 Content-Type: text/plain; charset="utf-8" From: Kai Huang During kexec, the kernel jumps to the new kernel in relocate_kernel(), which is implemented in assembly and both 32-bit and 64-bit have their own version. Currently, for both 32-bit and 64-bit, the last two parameters of the relocate_kernel() are both 'unsigned int' but actually they only convey a boolean, i.e., one bit information. The 'unsigned int' has enough space to carry two bits information therefore there's no need to pass the two booleans in two separate 'unsigned int'. Consolidate the last two function parameters of relocate_kernel() into a single 'unsigned int' and pass flags instead. Only consolidate the 64-bit version albeit the similar optimization can be done for the 32-bit version too. Don't bother changing the 32-bit version while it is working (since assembly code change is required). Signed-off-by: Kai Huang Reviewed-by: Tom Lendacky Reviewed-by: Borislav Petkov (AMD) Reviewed-by: David Woodhouse Signed-off-by: Paolo Bonzini --- arch/x86/include/asm/kexec.h | 12 ++++++++++-- arch/x86/kernel/machine_kexec_64.c | 22 +++++++++++++--------- arch/x86/kernel/relocate_kernel_64.S | 25 +++++++++++++++---------- 3 files changed, 38 insertions(+), 21 deletions(-) diff --git a/arch/x86/include/asm/kexec.h b/arch/x86/include/asm/kexec.h index f2ad77929d6e..12cebbcdb6c8 100644 --- a/arch/x86/include/asm/kexec.h +++ b/arch/x86/include/asm/kexec.h @@ -13,6 +13,15 @@ # define KEXEC_DEBUG_EXC_HANDLER_SIZE 6 /* PUSHI, PUSHI, 2-byte JMP */ #endif =20 +#ifdef CONFIG_X86_64 + +#include + +#define RELOC_KERNEL_PRESERVE_CONTEXT BIT(0) +#define RELOC_KERNEL_HOST_MEM_ENC_ACTIVE BIT(1) + +#endif + # define KEXEC_CONTROL_PAGE_SIZE 4096 # define KEXEC_CONTROL_CODE_MAX_SIZE 2048 =20 @@ -121,8 +130,7 @@ typedef unsigned long relocate_kernel_fn(unsigned long indirection_page, unsigned long pa_control_page, unsigned long start_address, - unsigned int preserve_context, - unsigned int host_mem_enc_active); + unsigned int flags); #endif extern relocate_kernel_fn relocate_kernel; #define ARCH_HAS_KIMAGE_ARCH diff --git a/arch/x86/kernel/machine_kexec_64.c b/arch/x86/kernel/machine_k= exec_64.c index 8593760c255a..fdd04b5bb70e 100644 --- a/arch/x86/kernel/machine_kexec_64.c +++ b/arch/x86/kernel/machine_kexec_64.c @@ -384,16 +384,10 @@ void __nocfi machine_kexec(struct kimage *image) { unsigned long reloc_start =3D (unsigned long)__relocate_kernel_start; relocate_kernel_fn *relocate_kernel_ptr; - unsigned int host_mem_enc_active; + unsigned int relocate_kernel_flags; int save_ftrace_enabled; void *control_page; =20 - /* - * This must be done before load_segments() since if call depth tracking - * is used then GS must be valid to make any function calls. - */ - host_mem_enc_active =3D cc_platform_has(CC_ATTR_HOST_MEM_ENCRYPT); - #ifdef CONFIG_KEXEC_JUMP if (image->preserve_context) save_processor_state(); @@ -427,6 +421,17 @@ void __nocfi machine_kexec(struct kimage *image) */ relocate_kernel_ptr =3D control_page + (unsigned long)relocate_kernel - r= eloc_start; =20 + relocate_kernel_flags =3D 0; + if (image->preserve_context) + relocate_kernel_flags |=3D RELOC_KERNEL_PRESERVE_CONTEXT; + + /* + * This must be done before load_segments() since if call depth tracking + * is used then GS must be valid to make any function calls. + */ + if (cc_platform_has(CC_ATTR_HOST_MEM_ENCRYPT)) + relocate_kernel_flags |=3D RELOC_KERNEL_HOST_MEM_ENC_ACTIVE; + /* * The segment registers are funny things, they have both a * visible and an invisible part. Whenever the visible part is @@ -443,8 +448,7 @@ void __nocfi machine_kexec(struct kimage *image) image->start =3D relocate_kernel_ptr((unsigned long)image->head, virt_to_phys(control_page), image->start, - image->preserve_context, - host_mem_enc_active); + relocate_kernel_flags); =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 ea604f4d0b52..26e945f85d19 100644 --- a/arch/x86/kernel/relocate_kernel_64.S +++ b/arch/x86/kernel/relocate_kernel_64.S @@ -66,8 +66,7 @@ SYM_CODE_START_NOALIGN(relocate_kernel) * %rdi indirection_page * %rsi pa_control_page * %rdx start address - * %rcx preserve_context - * %r8 host_mem_enc_active + * %rcx flags: RELOC_KERNEL_* */ =20 /* Save the CPU context, used for jumping back */ @@ -111,7 +110,7 @@ SYM_CODE_START_NOALIGN(relocate_kernel) /* save indirection list for jumping back */ movq %rdi, pa_backup_pages_map(%rip) =20 - /* Save the preserve_context to %r11 as swap_pages clobbers %rcx. */ + /* Save the flags to %r11 as swap_pages clobbers %rcx. */ movq %rcx, %r11 =20 /* setup a new stack at the end of the physical control page */ @@ -129,9 +128,8 @@ SYM_CODE_START_LOCAL_NOALIGN(identity_mapped) /* * %rdi indirection page * %rdx start address - * %r8 host_mem_enc_active * %r9 page table page - * %r11 preserve_context + * %r11 flags: RELOC_KERNEL_* * %r13 original CR4 when relocate_kernel() was invoked */ =20 @@ -204,7 +202,7 @@ SYM_CODE_START_LOCAL_NOALIGN(identity_mapped) * entries that will conflict with the now unencrypted memory * used by kexec. Flush the caches before copying the kernel. */ - testq %r8, %r8 + testb $RELOC_KERNEL_HOST_MEM_ENC_ACTIVE, %r11b jz .Lsme_off wbinvd .Lsme_off: @@ -220,7 +218,7 @@ SYM_CODE_START_LOCAL_NOALIGN(identity_mapped) movq %cr3, %rax movq %rax, %cr3 =20 - testq %r11, %r11 /* preserve_context */ + testb $RELOC_KERNEL_PRESERVE_CONTEXT, %r11b jnz .Lrelocate =20 /* @@ -273,7 +271,13 @@ SYM_CODE_START_LOCAL_NOALIGN(identity_mapped) ANNOTATE_NOENDBR andq $PAGE_MASK, %r8 lea PAGE_SIZE(%r8), %rsp - movl $1, %r11d /* Ensure preserve_context flag is set */ + /* + * Ensure RELOC_KERNEL_PRESERVE_CONTEXT flag is set so that + * swap_pages() can swap pages correctly. Note all other + * RELOC_KERNEL_* flags passed to relocate_kernel() are not + * restored. + */ + movl $RELOC_KERNEL_PRESERVE_CONTEXT, %r11d call swap_pages movq kexec_va_control_page(%rip), %rax 0: addq $virtual_mapped - 0b, %rax @@ -321,7 +325,7 @@ SYM_CODE_START_LOCAL_NOALIGN(swap_pages) UNWIND_HINT_END_OF_STACK /* * %rdi indirection page - * %r11 preserve_context + * %r11 flags: RELOC_KERNEL_* */ movq %rdi, %rcx /* Put the indirection_page in %rcx */ xorl %edi, %edi @@ -357,7 +361,8 @@ SYM_CODE_START_LOCAL_NOALIGN(swap_pages) movq %rdi, %rdx /* Save destination page to %rdx */ movq %rsi, %rax /* Save source page to %rax */ =20 - testq %r11, %r11 /* Only actually swap for ::preserve_context */ + /* Only actually swap for ::preserve_context */ + testb $RELOC_KERNEL_PRESERVE_CONTEXT, %r11b jz .Lnoswap =20 /* copy source page to swap page */ --=20 2.51.0