From: Sean Christopherson <seanjc@google.com>
To: Yosry Ahmed <yosry@kernel.org>
Cc: Paolo Bonzini <pbonzini@redhat.com>,
kvm@vger.kernel.org, linux-kernel@vger.kernel.org,
f734222792@gmail.com, Vitaly Kuznetsov <vkuznets@redhat.com>,
Sashiko Bot <sashiko-bot@kernel.org>
Subject: Re: [PATCH v3 12/13] KVM: selftests: Dedup assembly code for VMLAUNCH and VMRESUME
Date: Thu, 27 Aug 2026 09:40:56 -0700 [thread overview]
Message-ID: <apBomFOn1ZqHEnAQ@google.com> (raw)
In-Reply-To: <CAO9r8zOuD+wU1Q80z077WHHpFBWvKomLk=TaXjH3gVnqj=mqFQ@mail.gmail.com>
On Wed, Aug 26, 2026, Yosry Ahmed wrote:
> On Wed, Aug 26, 2026 at 4:39 PM Sean Christopherson <seanjc@google.com> wrote:
> >
> > Dedup the assembly code from VMLAUNCH vs. VMRESUME, the difference is
> > literally only the actual VM-Enter instruction.
>
> You couldn't resist macrofiying this too.
It's either that or throw a branch in the asm blob, which is surprisingly
difficult because VMX_SWITCH_GPRS_ASM subtly clobbers RFLAGS and obviously
clobbers GPRs. Which basically leaves pushing the value on the stack, which is
totally doable, but still annoying, and needs to be done immediately, before
vmcs.HOST_RSP is loaded. Branching at runtime also makes the generated code
harder to read, e.g. when debugging, as it's not immediately obvious which
instruction was actually attempted.
That said, looking at this again made me realize evmcs_{vmlaunch,vmresume}() have
the same core copy+paste mess. Blech. Macrofying the standard flows but not the
eVMCS flows is rather silly.
Inlining these blobs *twice* (VMCS vs. eVMCS) at every VM-Enter is also ridiculous.
E.g. at a glance, it's responsible for something ~100k bytes of code in the compiled
state_test. While the code footprint of selftests isn't a priority, that's still
absurd.
So instead of macrofying everything, how about this? Depending on how one feels
about macro shenanigans, it's either horrific or amazing. Or both. But IMO it's
worth eliminating all of the copy+paste, and it makes the control flow much easier
to read, which in practice is likely what most people care about? At least until
the entry/exit sequence fails and they have to debug macro hell :-)
vmx.c:
#define __BUILD_VMX_VM_ENTRY_HELPER(insn, prefix, vmwrite_insn, vmwrite_operand, \
__host_rsp, __host_rip) \
static int __##prefix##_##insn(void) \
{ \
int ret; \
\
__asm__ __volatile__("push $0;" \
__stringify(vmwrite_insn) " %%rsp, %[host_rsp];" \
"lea 1f(%%rip), %%rax;" \
__stringify(vmwrite_insn) " %%rax, %[host_rip];" \
VMX_SWITCH_GPRS_ASM \
__stringify(insn)";" \
"incq (%%rsp);" \
"1: ;" \
VMX_SWITCH_GPRS_ASM \
"pop %%rax;" \
: [ret]"=&a"(ret) \
: [host_rsp]__stringify(vmwrite_operand)(__host_rsp), \
[host_rip]__stringify(vmwrite_operand)(__host_rip), \
GUEST_REGS_OFFSETS \
: "memory", "cc"); \
return ret; \
}
#define BUILD_VMX_VM_ENTRY_HELPER(insn) \
__BUILD_VMX_VM_ENTRY_HELPER(insn, _, vmwrite, r, (u64)HOST_RSP, (u64)HOST_RIP) \
__BUILD_VMX_VM_ENTRY_HELPER(insn, __evmcs, mov, m, \
current_evmcs->host_rsp, current_evmcs->host_rip)
BUILD_VMX_VM_ENTRY_HELPER(vmlaunch)
BUILD_VMX_VM_ENTRY_HELPER(vmresume)
int __vmlaunch(void)
{
if (enable_evmcs) {
current_evmcs->hv_clean_fields = 0;
return ____evmcs_vmlaunch();
}
return ____vmlaunch();
}
int __vmresume(void)
{
if (enable_evmcs) {
/* HOST_RIP */
current_evmcs->hv_clean_fields &= ~HV_VMX_ENLIGHTENED_CLEAN_FIELD_HOST_GRP1;
/* HOST_RSP */
current_evmcs->hv_clean_fields &= ~HV_VMX_ENLIGHTENED_CLEAN_FIELD_HOST_POINTER;
return ____evmcs_vmresume();
}
return ____vmresume();
}
vmx.h:
int __vmlaunch(void);
int __vmresume(void);
static inline int vmlaunch(void)
{
return __vmlaunch();
}
static inline int vmresume(void)
{
return __vmresume();
}
And then the next patch becomes:
KVM: selftests: Assert success in vmlaunch() and vmresume()
Assert success in the "outer" APIs for VMLAUNCH and VMRESUME to dedup a
pile of code, and switch to the double-underscores versions as necessary,
to make it more obvious which paths expect VM-Enter *failure*.
Signed-off-by: Sean Christopherson <seanjc@google.com>
diff --git tools/testing/selftests/kvm/include/x86/vmx.h tools/testing/selftests/kvm/include/x86/vmx.h
index d0ac8e128d98..1419043b41dd 100644
--- tools/testing/selftests/kvm/include/x86/vmx.h
+++ tools/testing/selftests/kvm/include/x86/vmx.h
@@ -362,14 +362,14 @@ static inline u64 vmptrst(void)
int __vmlaunch(void);
int __vmresume(void);
-static inline int vmlaunch(void)
+static inline void vmlaunch(void)
{
- return __vmlaunch();
+ __GUEST_ASSERT(!__vmlaunch(), "vmlaunch hit VM-Fail");
}
-static inline int vmresume(void)
+static inline void vmresume(void)
{
- return __vmresume();
+ __GUEST_ASSERT(!__vmresume(), "vmresume hit VM-Fail");
}
static inline void vmcall(void)
> > +#define BUILD_VMX_VM_ENTRY_HELPERS(insn) \
>
> s/HELPERS/HELPER?
>
> or maybe better: DEFINE_VMX_VM_ENTRY_HELPER()?
Because after the next patch, there are two helpers for each instruction, vmresume()
and __vmresume(), and churning the macros to add the 'S' felt silly. But, with the
above, this is a moot point.
> With the rename:
>
> Reviewed-by: Yosry Ahmed <yosry@kernel.org>
>
> (Who's f734222792@gmail.com?)
An anonymous bug reporter: https://bugzilla.kernel.org/show_bug.cgi?id=221841
next prev parent reply other threads:[~2026-08-27 16:40 UTC|newest]
Thread overview: 30+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-08-26 23:39 [PATCH v3 00/13] KVM: nVMX: Adjust VMPTRLD/VMPTRST behavior with active eVMCS Sean Christopherson
2026-08-26 23:39 ` [PATCH v3 01/13] KVM: nVMX: Make VMPTRLD result in #UD when eVMCS is used Sean Christopherson
2026-08-26 23:39 ` [PATCH v3 02/13] KVM: nVMX: Make VMPTRST return eVMCS GPA when it " Sean Christopherson
2026-08-26 23:39 ` [PATCH v3 03/13] KVM: selftests: Don't clobber RFLAGS in happy path of __KVM_ASM_SAFE() Sean Christopherson
2026-08-26 23:39 ` [PATCH v3 04/13] KVM: selftests: Adapt to the updated VMPTRST behavior when eVMCS is used Sean Christopherson
2026-08-26 23:39 ` [PATCH v3 05/13] KVM: selftests: Check VMPTRLD with active eVMCS Sean Christopherson
2026-08-26 23:39 ` [PATCH v3 06/13] KVM: selftests: Assert success in vmptrst(), kill off vmptrstz() Sean Christopherson
2026-08-26 23:39 ` [PATCH v3 07/13] KVM: selftests: Always assert that vmxon() and prepare_for_vmx_operation() succeed Sean Christopherson
2026-08-26 23:39 ` [PATCH v3 08/13] KVM: selftests: Always assert that vmclear() succeeds Sean Christopherson
2026-08-26 23:39 ` [PATCH v3 09/13] KVM: selftests: Always assert that vmptrld() succeeds Sean Christopherson
2026-08-26 23:39 ` [PATCH v3 10/13] KVM: selftests: Drop useless return code from load_vmcs() Sean Christopherson
2026-08-26 23:39 ` [PATCH v3 11/13] KVM: selftests: Add macros to handle simple VMX instructions Sean Christopherson
2026-08-26 23:39 ` [PATCH v3 12/13] KVM: selftests: Dedup assembly code for VMLAUNCH and VMRESUME Sean Christopherson
2026-08-27 6:45 ` Yosry Ahmed
2026-08-27 16:40 ` Sean Christopherson [this message]
2026-08-27 16:49 ` Yosry Ahmed
2026-08-27 17:17 ` Sean Christopherson
2026-08-27 17:21 ` Yosry Ahmed
2026-08-27 17:33 ` Sean Christopherson
2026-08-27 17:50 ` Yosry Ahmed
2026-08-27 18:07 ` Sean Christopherson
2026-08-27 18:21 ` Yosry Ahmed
2026-08-26 23:39 ` [PATCH v3 13/13] KVM: selftests: Add and use double-underscore versions of vmlaunch() and vmresume() Sean Christopherson
2026-08-27 6:51 ` Yosry Ahmed
2026-08-27 20:28 ` Sean Christopherson
2026-08-27 20:37 ` Yosry Ahmed
2026-08-27 20:48 ` Sean Christopherson
2026-08-27 20:56 ` Yosry Ahmed
2026-08-27 21:02 ` Sean Christopherson
2026-08-27 21:05 ` Yosry Ahmed
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=apBomFOn1ZqHEnAQ@google.com \
--to=seanjc@google.com \
--cc=f734222792@gmail.com \
--cc=kvm@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=pbonzini@redhat.com \
--cc=sashiko-bot@kernel.org \
--cc=vkuznets@redhat.com \
--cc=yosry@kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.