Skip to contentSkip to conversation

KVM Planes + SEV-SNP Support

PATCH In review Jörg Rödel 10 participants 162 messages Jun 8 – Nov 20 Page 2 of 2

Subsystems (20)
Documentation x86 KVM PowerPC +16 more subsystems
lore
Share & tools
lore
#115 lore raw

Re: [PATCH 56/60] kvm: svm: Implement GET_AP_APIC_IDS NAE event

In reply to Jörg Rödel
Thank you for your contribution! Sashiko AI review found 4 potential issue(s) to consider: - [High] sev_get_apic_ids() returns SUCCESS when the guest provides an insufficiently sized buffer, failing…

Thank you for your contribution! Sashiko AI review found 4 potential issue(s) to consider:
- [High] sev_get_apic_ids() returns SUCCESS when the guest provides an insufficiently sized buffer, failing to notify the guest to retry.
- [High] The new GET_APIC_IDS feature is unconditionally enabled and exposed to the guest, breaking live migration capability negotiation.
- [Medium] The patch completely omits several core features that the commit message explicitly claims to implement.
- [Low] The if (!pages) return; check in sev_get_apic_ids() is mathematically unreachable dead code.
--

commit 63e24b674ae7db27c05f8c784dd84af4c400d3f0
Author: Tom Lendacky <[email protected]>

kvm: svm: Implement GET_AP_APIC_IDS NAE event

This commit implements the GET_APIC_IDS NAE event to gather and return the list of APIC IDs for all vCPUs in the guest.

Quoted 7 lines

Since it is now possible to launch vCPUs without going through the LAUNCH_UPDATE process, be sure to mark the guest state protected and to enable LBR virtualization.

Since it is now possible to launch vCPUs by APIC ID before the first INIT-SIPI request, be sure to check for the AP create event in the kvm_arch_vcpu_ioctl_run() loop when the AP is in the uninitialized state.

[Severity: Medium]
Are these changes missing from the patch? The commit message describes modifications to mark guest state protected, enable LBR virtualization, and check for the AP create event in the kvm_arch_vcpu_ioctl_run() loop, but these features do not appear in the code changes below.

Quoted 4 lines

arch/x86/kvm/svm/sev.c line 46

Quoted 10 lines
arch/x86/kvm/svm/sev.c +4 -3
@@ -40,9 +40,10 @@
 #define GHCB_VERSION_MAX	2ULL
 #define GHCB_VERSION_MIN	1ULL
 
-#define GHCB_HV_FT_SUPPORTED	(GHCB_HV_FT_SNP |		\
-				 GHCB_HV_FT_SNP_AP_CREATION |	\
-				 GHCB_HV_FT_SNP_RINJ)
+#define GHCB_HV_FT_SUPPORTED	(GHCB_HV_FT_SNP			| \
+				 GHCB_HV_FT_SNP_AP_CREATION	| \
+				 GHCB_HV_FT_SNP_RINJ		| \
+				 GHCB_HV_FT_APIC_ID_LIST)

[Severity: High]
Is it safe to expose this new feature to the guest unconditionally?

If a VM discovers the GET_APIC_IDS capability and is later live-migrated to an older kernel that does not support it, will the VM crash upon attempting to use the hypercall? Could this feature be left disabled by default and explicitly gated on host userspace enablement?

[ ... ]

Quoted 26 lines
arch/x86/kvm/svm/sev.c +22 -0
@@ -4419,6 +4424,78 @@ static int sev_snp_hv_ipi(struct vcpu_svm *svm)
 	return 0;
 }
 
+struct sev_apic_id_desc {
+	u32	num_entries;
+	u32	apic_ids[];
+};
+
+static void sev_get_apic_ids(struct vcpu_svm *svm)
+{
+	struct ghcb *ghcb = svm->sev_es.ghcb;
+	struct kvm_vcpu *vcpu = &svm->vcpu, *loop_vcpu;
+	struct kvm *kvm = vcpu->kvm;
+	unsigned int id_desc_size;
+	struct sev_apic_id_desc *desc;
+	struct page *page;
+	gpa_t gpa;
+	u64 pages;
+	unsigned long i;
+	int n;
+
+	pages = vcpu->arch.regs[VCPU_REGS_RAX];
+
+	/* Each APIC ID is 32-bits in size, so make sure there is room */
+	n = atomic_read(&kvm->online_vcpus);

arch/x86/kvm/svm/sev.c static int sev_snp_hv_ipi(struct vcpu_svm *svm) · line 4458

Quoted 10 lines
arch/x86/kvm/svm/sev.c +10 -0
+	/*TODO: is this possible? */
+	if (n < 0)
+		return;
+
+	id_desc_size = sizeof(*desc);
+	id_desc_size += n * sizeof(desc->apic_ids[0]);
+	if (id_desc_size > (pages * PAGE_SIZE)) {
+		vcpu->arch.regs[VCPU_REGS_RAX] = PFN_UP(id_desc_size);
+		return;
+	}

[Severity: High]
Does this early return successfully notify the guest of the failure?

Since svm_vmgexit_success() initializes sw_exit_info_1 and sw_exit_info_2 to 0 (SUCCESS) before sev_get_apic_ids() executes, returning here without updating the error code leaves it as SUCCESS. Will the guest incorrectly assume the APIC IDs buffer is successfully populated instead of allocating a larger buffer to retry?

Quoted 6 lines
arch/x86/kvm/svm/sev.c +6 -0
+
+	gpa = svm->vmcb->control.exit_info_1;
+
+	ghcb_set_sw_exit_info_1(ghcb, 2);
+	ghcb_set_sw_exit_info_2(ghcb, 5);
+

arch/x86/kvm/svm/sev.c static int sev_snp_hv_ipi(struct vcpu_svm *svm) · line 4494

Quoted 10 lines
arch/x86/kvm/svm/sev.c +10 -0
+	if (!page_address_valid(vcpu, gpa))
+		return;
+
+	page = gfn_to_page(kvm, gpa_to_gfn(gpa));
+	kvm_release_page_unused(page);
+	if (!page)
+		return;
+
+	if (!pages)
+		return;

[Severity: Low]
Is this check reachable? Since id_desc_size is strictly greater than 0, if pages is 0, the earlier check for id_desc_size > (pages * PAGE_SIZE) will evaluate to true and return early.

Quoted 3 lines
arch/x86/kvm/svm/sev.c +3 -0
+
+	/* Allocate a buffer to hold the APIC IDs */
+	desc = kvzalloc(id_desc_size, GFP_KERNEL_ACCOUNT);

[ ... ]

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=56
#116 lore raw

PATCH 57/60 kvm: sev: Allow for VMPL level specification in AP create

In reply to Jörg Rödel

From: Tom Lendacky <[email protected]>

Update AP creation to support ADD/DESTROY of VMSAs at levels other than VMPL0 in order to run under an SVSM at VMPL1 or lower. To maintain backwards compatibility, the VMPL is specified in bits 16 to 19 of the AP Creation request in SW_EXITINFO1 of the GHCB.

In order to track the VMSAs at different levels, create arrays for the VMSAs, GHCBs, registered GHCBs and others. When switching VMPL levels, these entries will be used to set the VMSA and GHCB physical addresses in the VMCB for the VMPL level.

In order ensure that the proper responses are returned in the proper GHCB, the GHCB must be unmapped at the current level and saved for restoration later when switching back to that VMPL level.

Additional checks are applied to prevent a non-VMPL0 vCPU from being able to perform an AP creation request at VMPL0. Additionally, a vCPU cannot replace its own VMSA.

---
 arch/x86/include/asm/svm.h      |   9 +++
 arch/x86/include/uapi/asm/svm.h |   2 +
 arch/x86/kvm/svm/sev.c          | 134 +++++++++++++++++++++++++-------
 arch/x86/kvm/svm/svm.h          |   1 +
 arch/x86/kvm/x86.c              |   9 +++
 5 files changed, 126 insertions(+), 29 deletions(-)
arch/x86/include/asm/svm.h +9 -0
index 9822b0b346ae..32a35ee10bce 100644
@@ -345,6 +345,15 @@ static_assert((X2AVIC_4K_MAX_PHYSICAL_ID & AVIC_PHYSICAL_MAX_INDEX_MASK) == X2AV
 
 #define VMCB_ALLOWED_SEV_FEATURES_VALID			BIT_ULL(63)
 
+enum {
+	SVM_SEV_VMPL0 = 0,
+	SVM_SEV_VMPL1,
+	SVM_SEV_VMPL2,
+	SVM_SEV_VMPL3,
+
+	SVM_SEV_VMPL_MAX
+};
+
 struct vmcb_seg {
 	u16 selector;
 	u16 attrib;
arch/x86/include/uapi/asm/svm.h +2 -0
index 91395b82eadd..60b7a52f6f7e 100644
@@ -123,6 +123,8 @@
 #define SVM_VMGEXIT_HVDB_QUERY			2
 #define SVM_VMGEXIT_HVDB_CLEAR			3
 #define SVM_VMGEXIT_HV_IPI                      0x80000015ull
+#define SVM_VMGEXIT_AP_VMPL_MASK		GENMASK(19, 16)
+#define SVM_VMGEXIT_AP_VMPL_SHIFT		16
 #define SVM_VMGEXIT_GET_APIC_IDS		0x80000017ull
 #define SVM_VMGEXIT_SNP_RUN_VMPL		0x80000018ull
 #define SVM_VMGEXIT_SAVIC			0x8000001aull
arch/x86/kvm/svm/sev.c +105 -29

190 lines not on this page · raw message

arch/x86/kvm/svm/svm.h +1 -0
index 57033922ddcf..7e860f2abafb 100644
@@ -282,6 +282,7 @@ struct vcpu_sev_es_state {
 
 	struct mutex snp_vmsa_mutex; /* Used to handle concurrent updates of VMSA. */
 	gpa_t snp_vmsa_gpa;
+	bool snp_ap_runnable;
 	bool snp_ap_waiting_for_reset;
 	bool snp_has_guest_vmsa;
 
arch/x86/kvm/x86.c +9 -0
index 0b9fa1059481..ad05350bb393 100644
@@ -12165,6 +12165,15 @@ static int __kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu)
 		kvm_vcpu_block(vcpu);
 		kvm_vcpu_srcu_read_lock(vcpu);
 
+		/*
+		 * It is possible that the vCPU has never run before. If the
+		 * request is to update the protected guest state (AP Create),
+		 * then ensure that the vCPU can now run.
+		 */
+		if (kvm_test_request(KVM_REQ_UPDATE_PROTECTED_GUEST_STATE, vcpu) &&
+		    vcpu->arch.mp_state == KVM_MP_STATE_UNINITIALIZED)
+			vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
+
 		if (kvm_apic_accept_events(vcpu) < 0) {
 			r = 0;
 			goto out;
Signature · 1 line
2.53.0
#117 lore raw

Re: [PATCH 57/60] kvm: sev: Allow for VMPL level specification in AP create

Thank you for your contribution! Sashiko AI review found 7 potential issue(s) to consider:

Thank you for your contribution! Sashiko AI review found 7 potential issue(s) to consider:

New issues:
- [High] Returning -EINVAL from the complete_userspace_io callback abruptly aborts the entire VM.
- [High] Non-VMPL0 vCPUs are incorrectly permitted to replace their own VMSA.
- [High] Processing an AP_DESTROY request sets the target vCPU to KVM_MP_STATE_HALTED instead of KVM_MP_STATE_UNINITIALIZED and leaves its VMSA PA invalid, exposing the VM to a host crash upon spurious wakeup.
- [Medium] Unsynchronized concurrent access to the shared target_sev_plane->vmsa_features variable.
- [Low] #define SVM_SEV_VMPL_MAX 4 improperly redefines an existing enum constant as a preprocessor macro.
- [Low] Commented-out lines left over from debugging or refactoring.

Pre-existing issues:
- [Critical] Hardware use-after-free vulnerability due to missing lifetime pinning of the VMSA guest_memfd page. --

commit 5d5c749babe4794fbb4e0c07b6fb01ee70a1da40
Author: Tom Lendacky <[email protected]>
Subject: kvm: sev: Allow for VMPL level specification in AP create

This commit updates AP creation to support adding or destroying VMSAs at levels other than VMPL0. It creates tracking arrays for VMSAs and GHCBs across different levels to allow running under an SVSM at VMPL1 or lower.

Quoted 3 lines

Additional checks are applied to prevent a non-VMPL0 vCPU from being able to perform an AP creation request at VMPL0. Additionally, a vCPU cannot replace its own VMSA.

[Severity: High]
Does this logic successfully prevent a non-VMPL0 vCPU from replacing its own VMSA? As implemented in sev_snp_ap_creation() below, the condition evaluates to false for any VMPL greater than 0, which entirely bypasses the target_vcpu check and allows a lower-privileged guest vCPU to replace its own VMSA.

Quoted 4 lines
arch/x86/kvm/svm/sev.c +0 -0
@@ -4131,8 +4137,26 @@ static void sev_snp_init_protected_guest_state(struct kvm_vcpu *vcpu)

[ ... ]

arch/x86/kvm/svm/sev.c static void sev_snp_init_protected_guest_state(struct kvm_vcpu *vcpu) · line 4179

Quoted 7 lines
arch/x86/kvm/svm/sev.c +5 -1
 	/* Mark the vCPU as runnable */
-	kvm_set_mp_state(vcpu, KVM_MP_STATE_RUNNABLE);
+	if (svm->sev_es.snp_ap_runnable) {
+		kvm_set_mp_state(vcpu, KVM_MP_STATE_RUNNABLE);
+	} else {
+		kvm_set_mp_state(vcpu, KVM_MP_STATE_UNINITIALIZED);
+	}

[Severity: High]
Will an AP_DESTROY request successfully reach this new state transition? Earlier in sev_snp_init_protected_guest_state(), there is a check:

	if (!VALID_PAGE(svm->sev_es.snp_vmsa_gpa))
		return;

Since an AP_DESTROY sets snp_vmsa_gpa to INVALID_PAGE, the early return bypasses this MP state transition. This leaves the vCPU in KVM_MP_STATE_HALTED with an invalid vmsa_pa, making it susceptible to standard APIC wakeups that could lead to a #VMEXIT(INVALID) crash.

arch/x86/kvm/svm/sev.c static void sev_snp_init_protected_guest_state(struct kvm_vcpu *vcpu) · line 4167

Quoted 5 lines
arch/x86/kvm/svm/sev.c +0 -0
 	/*
 	 * gmem pages aren't currently migratable, but if this ever changes
@@ -4142,36 +4166,87 @@ static void sev_snp_init_protected_guest_state(struct kvm_vcpu *vcpu)
 	kvm_release_page_clean(page);
 }

[Severity: Critical]
This is a pre-existing issue, but does dropping the software reference to the VMSA page here expose KVM to a use-after-free? KVM writes the pfn directly into the hardware VMCB (svm->vmcb->control.vmsa_pa), but calling kvm_release_page_clean(page) unpins it. If host userspace frees the physical page, the hardware could continue executing with a VMCB pointing to freed memory since MMU notifiers won't intercept hardware VMCB pointers.

Quoted 4 lines
arch/x86/kvm/svm/sev.c +1 -2
-static int sev_snp_ap_creation(struct vcpu_svm *svm)
+static unsigned int get_ap_creation_request(struct vcpu_svm *svm)
 {
-	struct kvm_sev_info_plane *sev_plane = to_kvm_sev_info_plane(svm->vcpu.plane);

arch/x86/kvm/svm/sev.c static void sev_snp_init_protected_guest_state(struct kvm_vcpu *vcpu) · line 4196

Quoted 10 lines
arch/x86/kvm/svm/sev.c +6 -4
-	struct kvm_vcpu *vcpu = &svm->vcpu;
-	struct kvm_vcpu *target_vcpu;
-	struct vcpu_svm *target_svm;
-	unsigned int request;
+//	struct kvm_sev_info_plane *sev_plane = to_kvm_sev_info_plane(svm->vcpu.plane);
+//	struct kvm_vcpu *vcpu = &svm->vcpu;
+	unsigned int req = lower_32_bits(svm->vmcb->control.exit_info_1);
+
+	return req & ~SVM_VMGEXIT_AP_VMPL_MASK;
+}

[Severity: Low]
Are these commented-out lines intended to be removed?

[ ... ]

arch/x86/kvm/svm/sev.c static void sev_snp_init_protected_guest_state(struct kvm_vcpu *vcpu) · line 4213

Quoted 4 lines
arch/x86/kvm/svm/sev.c +4 -0
+#define SVM_SEV_VMPL_MAX	4
+
+static int sev_snp_ap_creation(struct kvm_vcpu *vcpu)
+{

[Severity: Low]
Does this macro redefine the SVM_SEV_VMPL_MAX enum constant added to arch/x86/include/asm/svm.h in this same patch?

[ ... ]

Quoted 5 lines
arch/x86/kvm/svm/sev.c +5 -0
+	/* Request user-space to create target plane VCPU if it does not exist */
+	if (!target_plane || !target_vcpu) {
+		vcpu->arch.complete_userspace_io = sev_snp_ap_creation;
+		return kvm_request_create_plane(vcpu, vmpl, apic_id);
+	}

[ ... ]

arch/x86/kvm/svm/sev.c static void sev_snp_init_protected_guest_state(struct kvm_vcpu *vcpu) · line 4258

Quoted 6 lines
arch/x86/kvm/svm/sev.c +6 -0
+	/* VMPL0 can only be replaced by another vCPU running VMPL0 */
+	if (vmpl == SVM_SEV_VMPL0 &&
+	    (vcpu == target_vcpu || vcpu->plane_level != SVM_SEV_VMPL0)) {
+		vcpu_unimpl(vcpu, "vmgexit: VMPL0 AP action not allowed\n");
+		return -EINVAL;
+	}

[Severity: High]
If this validation check (or the ones further down) fails during the second pass of sev_snp_ap_creation() acting as a complete_userspace_io callback, does returning -EINVAL abort the entire VM? Since it executes via kvm_arch_vcpu_ioctl_run, the error isn't intercepted to inject a guest error code, and instead bubbles up to userspace to terminate the VM.

arch/x86/kvm/svm/sev.c static void sev_snp_init_protected_guest_state(struct kvm_vcpu *vcpu) · line 4265

Quoted 7 lines
arch/x86/kvm/svm/sev.c +3 -1
 	switch (request) {
 	case SVM_VMGEXIT_AP_CREATE_ON_INIT:
 	case SVM_VMGEXIT_AP_CREATE:
-		if (vcpu->arch.regs[VCPU_REGS_RAX] != sev_plane->vmsa_features) {
+		/* Initialize target planes SEV features if necessary */
+		if (target_sev_plane->vmsa_features == 0)
+			target_sev_plane->vmsa_features = vcpu->arch.regs[VCPU_REGS_RAX];

[Severity: Medium]
Can this lead to a data race on the shared target_sev_plane structure? This update modifies a plane-wide shared variable while only protected by the per-target vCPU mutex (target_svm->sev_es.snp_vmsa_mutex). Multiple vCPUs executing AP creation concurrently for different targets on the same uninitialized plane might encounter load/store tearing.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=57
#118 lore raw

PATCH 58/60 kvm: svm: Invoke a specified VMPL level VMSA for the vCPU

In reply to Jörg Rödel

From: Tom Lendacky <[email protected]>

Implement the SNP Run VMPL NAE event and MSR protocol to allow a guest to request a different VMPL level VMSA be run for the vCPU. This allows the guest to "call" an SVSM to process an SVSM request.

---
 arch/x86/include/asm/sev-common.h |  6 +++
 arch/x86/kvm/svm/sev.c            | 71 +++++++++++++++++++++++++++++++
 2 files changed, 77 insertions(+)
arch/x86/include/asm/sev-common.h +6 -0
index cedb7ea91da5..a09cf5690aba 100644
@@ -114,6 +114,8 @@ enum psc_op {
 
 /* GHCB Run at VMPL Request/Response */
 #define GHCB_MSR_VMPL_REQ		0x016
+#define GHCB_MSR_VMPL_LEVEL_POS		32
+#define GHCB_MSR_VMPL_LEVEL_MASK	GENMASK_ULL(7, 0)
 #define GHCB_MSR_VMPL_REQ_LEVEL(v)			\
 	/* GHCBData[39:32] */				\
 	((((u64)(v) & GENMASK_ULL(7, 0)) << 32) |	\
@@ -121,6 +123,10 @@ enum psc_op {
 	GHCB_MSR_VMPL_REQ)
 
 #define GHCB_MSR_VMPL_RESP		0x017
+#define GHCB_MSR_VMPL_ERROR_POS		32
+#define GHCB_MSR_VMPL_ERROR_MASK	GENMASK_ULL(31, 0)
+#define GHCB_MSR_VMPL_RSVD_POS		12
+#define GHCB_MSR_VMPL_RSVD_MASK		GENMASK_ULL(19, 0)
 #define GHCB_MSR_VMPL_RESP_VAL(v)			\
 	/* GHCBData[63:32] */				\
 	(((u64)(v) & GENMASK_ULL(63, 32)) >> 32)
arch/x86/kvm/svm/sev.c +71 -0

95 lines not on this page · raw message

Signature · 1 line
2.53.0
#119 lore raw

Re: [PATCH 58/60] kvm: svm: Invoke a specified VMPL level VMSA for the vCPU

Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider: - [High] The MSR protocol handler for GHCB_MSR_VMPL_REQ propagates `-EINVAL` to KVM_RUN when a requested…

Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider:
- [High] The MSR protocol handler for GHCB_MSR_VMPL_REQ propagates -EINVAL to KVM_RUN when a requested VMPL plane does not exist, causing the VM to crash.
- [High] Requesting a switch to the currently active VMPL puts the active vCPU plane into the STOPPED state, permanently hanging the vCPU thread.
- [High] Missing array_index_nospec() on guest-controlled VMPL index creates a Spectre v1 vulnerability.
--

commit 13abcdff43b65b23c1d479626cab3e83b513a2c8
Author: Tom Lendacky <[email protected]>

kvm: svm: Invoke a specified VMPL level VMSA for the vCPU

Implement the SNP Run VMPL NAE event and MSR protocol to allow a guest to request a different VMPL level VMSA be run for the vCPU. This allows the guest to "call" an SVSM to process an SVSM request.

Quoted 4 lines
arch/x86/kvm/svm/sev.c +0 -0
index 0b531eda062f3..70a06769a4100 100644

[ ... ]

Quoted 9 lines
arch/x86/kvm/svm/sev.c +5 -0
@@ -4573,6 +4577,45 @@ static void sev_get_apic_ids(struct vcpu_svm *svm)
 	kvfree(desc);
 }
 
+static int __sev_snp_run_vmpl(struct vcpu_svm *svm, unsigned int vmpl)
+{
+	struct kvm_vcpu *vcpu = &svm->vcpu;
+	struct kvm_vcpu *target = vcpu->common->vcpus[vmpl];
+	struct vcpu_svm *target_svm = to_svm(target);

arch/x86/kvm/svm/sev.c static void sev_get_apic_ids(struct vcpu_svm *svm) · line 4594

Quoted 10 lines
arch/x86/kvm/svm/sev.c +10 -0
+
+	if (!target)
+		return -EINVAL;
+
+	/* Mark current plane as stopped so it is not selected */
+	kvm_set_mp_state(target, KVM_MP_STATE_RUNNABLE);
+	/* In case KVM_REQ_UPDATE_PROTECTED_GUEST_STATE is set - mark the new VMSA as runnable */
+	target_svm->sev_es.snp_ap_runnable = true;
+	kvm_vcpu_set_plane_runnable(target);
+	kvm_vcpu_set_plane_stopped(vcpu);

[Severity: High]
If a guest requests a switch to the VMPL level it is already executing on, target and vcpu will point to the same object. Will this sequence cause the vCPU to hang?

Since kvm_vcpu_set_plane_runnable(target) is immediately followed by kvm_vcpu_set_plane_stopped(vcpu), the plane's state would be overwritten to STOPPED, indefinitely suspending the vCPU.

Should there be an explicit check for target == vcpu to handle this?

Quoted 7 lines
arch/x86/kvm/svm/sev.c +7 -0
+
+	kvm_make_request(KVM_REQ_PLANE_RESCHED, vcpu);
+
+	return 1;
+}
+
+static int sev_snp_run_vmpl(struct vcpu_svm *svm)

arch/x86/kvm/svm/sev.c static void sev_get_apic_ids(struct vcpu_svm *svm) · line 4631

Quoted 10 lines
arch/x86/kvm/svm/sev.c +10 -0
+{
+	struct ghcb *ghcb = svm->sev_es.ghcb;
+	struct kvm_vcpu *vcpu = &svm->vcpu;
+	unsigned int vmpl;
+
+	vmpl = lower_32_bits(svm->vmcb->control.exit_info_1);
+	if (vmpl >= SVM_SEV_VMPL_MAX) {
+		vcpu_unimpl(vcpu, "vmgexit: invalid VMPL level [%u] from guest\n", vmpl);
+		return -EINVAL;
+	}

[Severity: High]
Since vmpl is guest-controlled and used as an index into the vcpu->common->vcpus[vmpl] array in __sev_snp_run_vmpl(), is there a risk of speculative out-of-bounds execution here?

Should vmpl be sanitized with array_index_nospec() after this bounds check to prevent a potential Spectre v1 vulnerability?

Quoted 7 lines
arch/x86/kvm/svm/sev.c +7 -0
+
+	ghcb_set_sw_exit_info_1(ghcb, 0);
+	ghcb_set_sw_exit_info_2(ghcb, 0);
+
+	return __sev_snp_run_vmpl(svm, vmpl);
+}
+

[ ... ]

Quoted 11 lines
arch/x86/kvm/svm/sev.c +7 -0
@@ -4684,6 +4727,27 @@ static int sev_handle_vmgexit_msr_protocol(struct vcpu_svm *svm)
 
 		ret = snp_begin_psc_msr(svm, control->ghcb_gpa);
 		break;
+	case GHCB_MSR_VMPL_REQ: {
+		unsigned int vmpl;
+
+		if (!sev_snp_guest(vcpu->kvm))
+			goto out_terminate;
+
+		vmpl = get_ghcb_msr_bits(svm, GHCB_MSR_VMPL_LEVEL_MASK, GHCB_MSR_VMPL_LEVEL_POS);

arch/x86/kvm/svm/sev.c static int sev_handle_vmgexit_msr_protocol(struct vcpu_svm *svm) · line 4746

Quoted 10 lines
arch/x86/kvm/svm/sev.c +10 -0
+
+		set_ghcb_msr_bits(svm, 0, GHCB_MSR_VMPL_ERROR_MASK, GHCB_MSR_VMPL_ERROR_POS);
+		set_ghcb_msr_bits(svm, 0, GHCB_MSR_VMPL_RSVD_MASK, GHCB_MSR_VMPL_RSVD_POS);
+		set_ghcb_msr_bits(svm, GHCB_MSR_VMPL_RESP, GHCB_MSR_INFO_MASK, GHCB_MSR_INFO_POS);
+
+		if (vmpl >= SVM_SEV_VMPL_MAX) {
+			vcpu_unimpl(vcpu, "vmgexit: invalid VMPL level [%u] from guest\n", vmpl);
+			set_ghcb_msr_bits(svm, 1, GHCB_MSR_VMPL_ERROR_MASK, GHCB_MSR_VMPL_ERROR_POS);
+			break;
+		}

[Severity: High]
Similar to sev_snp_run_vmpl(), does this guest-controlled vmpl index require array_index_nospec() after the bounds check to mitigate Spectre v1?

arch/x86/kvm/svm/sev.c static int sev_handle_vmgexit_msr_protocol(struct vcpu_svm *svm) · line 4769

Quoted 3 lines
arch/x86/kvm/svm/sev.c +3 -0
+
+		ret = __sev_snp_run_vmpl(svm, vmpl);
+		break;

[Severity: High]
If target == NULL inside __sev_snp_run_vmpl(), it returns -EINVAL which is then assigned to ret here and propagated up. Will this cause the KVM_RUN ioctl to return -EINVAL to userspace?

Since QEMU treats negative returns from KVM_RUN as fatal errors and aborts the VM, could a less privileged guest level (e.g., VMPL3) exploit this to intentionally crash the VM and disrupt more privileged levels?

Should the MSR handler trap the negative return, set GHCB_MSR_VMPL_ERROR_MASK in the response, and return 1 to safely resume the guest instead?

Quoted 4 lines
arch/x86/kvm/svm/sev.c +1 -0
+	}
 	case GHCB_MSR_TERM_REQ: {
 		u64 reason_set, reason_code;
 
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=58
#120 lore raw

PATCH 59/60 kvm: svm: Implement max_planes x86 operation

In reply to Jörg Rödel

From: Tom Lendacky <[email protected]>

Report the number of VMPL levels supported by SEV-SNP guests.

---
 arch/x86/kvm/svm/sev.c | 14 ++++++++++++--
 arch/x86/kvm/svm/svm.c | 12 +++++++++++-
 arch/x86/kvm/svm/svm.h |  3 +++
 3 files changed, 26 insertions(+), 3 deletions(-)
arch/x86/kvm/svm/sev.c +12 -2
index b67566fcb69e..528c8bd3e8fc 100644
@@ -114,6 +114,7 @@ static unsigned long sev_me_mask;
 static unsigned int nr_asids;
 static unsigned long *sev_asid_bitmap;
 static unsigned long *sev_reclaim_asid_bitmap;
+static unsigned int vmpl_levels;
 
 static __always_inline void kvm_lockdep_assert_sev_lock_held(struct kvm *kvm)
 {
@@ -3103,6 +3104,9 @@ void __init sev_hardware_setup(void)
 	/* Set encryption bit location for SEV-ES guests */
 	sev_enc_bit = ebx & 0x3f;
 
+	/* Get the number of supported VMPL levels */
+	vmpl_levels = (ebx >> 12) & 0xf;
+
 	/* Maximum number of encrypted guests supported simultaneously */
 	max_sev_asid = ecx;
 	if (!max_sev_asid)
@@ -3217,9 +3221,10 @@ void __init sev_hardware_setup(void)
 										"disabled",
 			min_sev_es_asid, max_sev_es_asid);
 	if (boot_cpu_has(X86_FEATURE_SEV_SNP))
-		pr_info("SEV-SNP %s (ASIDs %u - %u)\n",
+		pr_info("SEV-SNP %s (ASIDs %u - %u), VMPL Levels %u\n",
 			str_enabled_disabled(sev_snp_supported),
-			min_snp_asid, max_snp_asid);
+			min_snp_asid, max_snp_asid,
+			vmpl_levels);
 
 	sev_enabled = sev_supported;
 	sev_es_enabled = sev_es_supported;
@@ -5852,3 +5857,8 @@ bool sev_snp_blocked(enum inject_type type, struct kvm_vcpu *vcpu)
 
 	return blocked;
 }
+
+int sev_snp_max_planes(struct kvm *kvm)
+{
+	return vmpl_levels;
+}
arch/x86/kvm/svm/svm.c +11 -1
index 2ae82dc058c9..705063c7f0f0 100644
@@ -5321,6 +5321,16 @@ static void svm_free_plane(struct kvm_plane *plane)
 	kfree(svm_plane);
 }
 
+static unsigned svm_max_planes(struct kvm *kvm)
+{
+#ifdef CONFIG_KVM_AMD_SEV
+	if (____sev_snp_guest(kvm))
+		return sev_snp_max_planes(kvm);
+#endif
+
+	return kvm_x86_default_max_planes(kvm);
+}
+
 struct kvm_x86_ops svm_x86_ops __initdata = {
 	.name = KBUILD_MODNAME,
 
@@ -5465,7 +5475,7 @@ struct kvm_x86_ops svm_x86_ops __initdata = {
 
 	.alloc_plane = svm_alloc_plane,
 	.free_plane = svm_free_plane,
-	.max_planes = kvm_x86_default_max_planes,
+	.max_planes = svm_max_planes,
 };
 
 /*
arch/x86/kvm/svm/svm.h +3 -0
index 7e860f2abafb..7aba2cceb44d 100644
@@ -1008,6 +1008,8 @@ static inline bool sev_snp_is_rinj_active(struct kvm_vcpu *vcpu)
 	return is_sev_snp_guest(vcpu) &&
 		(sev->vmsa_features & SVM_SEV_FEAT_RESTRICTED_INJECTION);
 };
+int sev_nr_vcpu_planes(struct kvm *kvm);
+int sev_snp_max_planes(struct kvm *kvm);
 #else
 static inline struct page *snp_safe_alloc_page_node(int node, gfp_t gfp)
 {
@@ -1051,6 +1053,7 @@ static inline bool sev_snp_inject(enum inject_type type, struct kvm_vcpu *vcpu)
 static inline void sev_snp_cancel_injection(struct kvm_vcpu *vcpu) {}
 static inline bool sev_snp_blocked(enum inject_type type, struct kvm_vcpu *vcpu) { return false; }
 static inline bool sev_snp_is_rinj_active(struct kvm_vcpu *vcpu) { return false; }
+static inline unsigned sev_snp_max_planes(struct kvm *kvm) { return 1; }
 #endif
 
 /* vmenter.S */
Signature · 1 line
2.53.0
#121 lore raw

Re: [PATCH 59/60] kvm: svm: Implement max_planes x86 operation

Thank you for your contribution! Sashiko AI review found 5 potential issue(s) to consider:

Thank you for your contribution! Sashiko AI review found 5 potential issue(s) to consider:

New issues:
- [High] sev_snp_max_planes() can return 0, breaking the invariant that KVM_CAP_PLANES is at least 1 and potentially invalidating VM capability checks.
- [High] Data race in svm_max_planes leads to dynamic capability mutation and can trigger a host-userspace reachable WARN_ON_ONCE.
- [Low] Dead declaration of sev_nr_vcpu_planes().
- [Low] Mismatched return types for sev_snp_max_planes() across declarations, definitions, and fallback stubs.

Pre-existing issues:
- [Critical] Dangling plane pointer in error path of kvm_vm_ioctl_create_plane() leads to a Double Free during VM destruction. --

commit c8552c7d8852aecb64608575360c9abf90b077c5
Author: Tom Lendacky <[email protected]>

kvm: svm: Implement max_planes x86 operation

Report the number of VMPL levels supported by SEV-SNP guests.

Quoted 4 lines
arch/x86/kvm/svm/sev.c +0 -0
index 70a06769a4100..4a6166d2a71df 100644

[ ... ]

arch/x86/kvm/svm/sev.c bool sev_snp_blocked(enum inject_type type, struct kvm_vcpu *vcpu) · line 5844

Quoted 9 lines
arch/x86/kvm/svm/sev.c +5 -0
@@ -5832,3 +5837,8 @@ bool sev_snp_blocked(enum inject_type type, struct kvm_vcpu *vcpu)
 
 	return blocked;
 }
+
+int sev_snp_max_planes(struct kvm *kvm)
+{
+	return vmpl_levels;
+}

[Severity: High]
Can this return 0 and break the KVM_CAP_PLANES invariant?

If the CPU supports only VMPL0, the value extracted directly from CPUID 0x8000001F_EBX[15:12] will be 0. Returning 0 without adjusting it contradicts KVM's invariant that all VMs have at least 1 plane.

Could this invalidate capability discovery and prevent userspace from creating any vCPUs if planes are required?

Quoted 5 lines
arch/x86/kvm/svm/svm.c +0 -0
index 2ae82dc058c96..705063c7f0f0a 100644

arch/x86/kvm/svm/svm.c static void svm_free_plane(struct kvm_plane *plane) · line 5329

Quoted 9 lines
arch/x86/kvm/svm/svm.c +6 -0
@@ -5321,6 +5321,16 @@ static void svm_free_plane(struct kvm_plane *plane)
 	kfree(svm_plane);
 }
 
+static unsigned svm_max_planes(struct kvm *kvm)
+{
+#ifdef CONFIG_KVM_AMD_SEV
+	if (____sev_snp_guest(kvm))
+		return sev_snp_max_planes(kvm);
+#endif

[Severity: High]
Can this lockless check observe torn state and trigger a WARN_ON_ONCE?

KVM_CAP_PLANES delegates to svm_max_planes(), which userspace can call locklessly at any time via KVM_CHECK_EXTENSION. The call to ____sev_snp_guest() performs a lockless read of sev->vmsa_features and sev->es_active.

If queried concurrently with KVM_SEV_INIT (which populates these fields non-atomically under kvm->lock), this lockless check could observe SVM_SEV_FEAT_SNP_ACTIVE as true while es_active evaluates to false. This triggers the WARN_ON_ONCE(!____sev_es_guest(kvm)), allowing an unprivileged host-userspace process to cause a host denial of service via kernel log flooding or panic if panic_on_warn is set.

[Severity: Critical]
This is a pre-existing issue, but does exposing max_planes > 1 trigger a double free during VM destruction?

In the error path of kvm_vm_ioctl_create_plane(), if anon_inode_getfile() fails, kvm_destroy_one_plane() is called, but it leaves a dangling pointer in kvm->planes[id]:

virt/kvm/kvm_main.c:kvm_vm_ioctl_create_plane() {
    ...
    kvm_create_plane(kvm, id);
    ...
    fd = anon_inode_getfile(...);
    if (IS_ERR(fd)) {
        ...
put_kvm:
        kvm_destroy_one_plane(plane);
    }
}

When the VM is later destroyed, kvm_destroy_planes() loops over kvm->planes and calls kvm_destroy_one_plane() on the dangling pointer. Previously this was unreachable because max_planes was hardcoded to 1 and kvm->planes[0] is always initialized at VM creation, safely returning -EEXIST early. Now that max_planes can be greater than 1, this double free becomes reachable.

Quoted 3 lines
arch/x86/kvm/svm/svm.c +3 -0
+
+	return kvm_x86_default_max_planes(kvm);
+}

[ ... ]

arch/x86/kvm/svm/svm.h static inline bool sev_snp_is_rinj_active(struct kvm_vcpu *vcpu) · line 1008

Quoted 9 lines
arch/x86/kvm/svm/svm.h +1 -0
index 61f9c4cb4f52a..0c9b81029e241 100644
@@ -1005,6 +1005,8 @@ static inline bool sev_snp_is_rinj_active(struct kvm_vcpu *vcpu)
 	return is_sev_snp_guest(vcpu) &&
 		(sev->vmsa_features & SVM_SEV_FEAT_RESTRICTED_INJECTION);
 };
+int sev_nr_vcpu_planes(struct kvm *kvm);

[Severity: Low]
Is sev_nr_vcpu_planes() a dead declaration? This function doesn't appear to be defined or used anywhere in the codebase.

Quoted 4 lines
arch/x86/kvm/svm/svm.h +1 -0
+int sev_snp_max_planes(struct kvm *kvm);
 #else
 static inline struct page *snp_safe_alloc_page_node(int node, gfp_t gfp)
 {

[ ... ]

arch/x86/kvm/svm/svm.h static inline bool sev_snp_inject(enum inject_type type, struct kvm_vcpu *vcpu) · line 1054

Quoted 6 lines
arch/x86/kvm/svm/svm.h +1 -0
@@ -1048,6 +1050,7 @@ static inline bool sev_snp_inject(enum inject_type type, struct kvm_vcpu *vcpu)
 static inline void sev_snp_cancel_injection(struct kvm_vcpu *vcpu) {}
 static inline bool sev_snp_blocked(enum inject_type type, struct kvm_vcpu *vcpu) { return false; }
 static inline bool sev_snp_is_rinj_active(struct kvm_vcpu *vcpu) { return false; }
+static inline unsigned sev_snp_max_planes(struct kvm *kvm) { return 1; }
 #endif

[Severity: Low]
Does sev_snp_max_planes() have mismatched return types?

It is declared and defined as returning an int, but this fallback stub returns unsigned. It is also assigned to svm_max_planes() which returns unsigned.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=59
#122 lore raw

PATCH 60/60 kvm: svm: Advertise full multi-VMPL support to the SNP guest

In reply to Jörg Rödel

From: Tom Lendacky <[email protected]>

Indicate full multi-VMPL support to the guest through the GHCB feature bitmap.

---
 arch/x86/kvm/svm/sev.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
arch/x86/kvm/svm/sev.c +2 -1
index 528c8bd3e8fc..0736e1c778d9 100644
@@ -43,7 +43,8 @@
 #define GHCB_HV_FT_SUPPORTED	(GHCB_HV_FT_SNP			| \
 				 GHCB_HV_FT_SNP_AP_CREATION	| \
 				 GHCB_HV_FT_SNP_RINJ		| \
-				 GHCB_HV_FT_APIC_ID_LIST)
+				 GHCB_HV_FT_APIC_ID_LIST	| \
+				 GHCB_HV_FT_SNP_MULTI_VMPL)
 
 /*
  * The GHCB spec essentially states that all non-zero error codes other than
Signature · 1 line
2.53.0
#123 lore raw

Re: [PATCH 60/60] kvm: svm: Advertise full multi-VMPL support to the SNP guest

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [High] The patch unconditionally advertises the new GHCB_HV_FT_SNP_MULTI_VMPL feature to guests without…

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] The patch unconditionally advertises the new GHCB_HV_FT_SNP_MULTI_VMPL feature to guests without userspace enumeration or control, violating KVM invariants. --

commit 885b4d7d907e2c3c684356fd3142ddc49a702d2c
Author: Tom Lendacky <[email protected]>

kvm: svm: Advertise full multi-VMPL support to the SNP guest

This commit updates the GHCB hypervisor features bitmap to include the GHCB_HV_FT_SNP_MULTI_VMPL flag. It indicates to the guest that full multi-VMPL support is available.

arch/x86/kvm/svm/sev.c line 47

Quoted 10 lines
arch/x86/kvm/svm/sev.c +2 -1
@@ -43,7 +43,8 @@
 #define GHCB_HV_FT_SUPPORTED	(GHCB_HV_FT_SNP			| \
 				 GHCB_HV_FT_SNP_AP_CREATION	| \
 				 GHCB_HV_FT_SNP_RINJ		| \
-				 GHCB_HV_FT_APIC_ID_LIST)
+				 GHCB_HV_FT_APIC_ID_LIST	| \
+				 GHCB_HV_FT_SNP_MULTI_VMPL)

[Severity: High]
Does unconditionally exposing this feature break KVM live migration and capability negotiation?

It looks like GHCB_HV_FT_SNP_MULTI_VMPL is now enabled by default for all SEV-SNP guests whenever they request HV features, without giving userspace the ability to enumerate or control it.

If a guest queries this newly exposed flag and attempts to switch to a valid VMPL (e.g., vmpl = 1), but userspace has not yet created the target plane, would sev_handle_vmgexit_msr_protocol() return -EINVAL when calling __sev_snp_run_vmpl()? This appears to propagate up to the KVM_RUN loop and abort the VM entirely, meaning the guest could unexpectedly crash its own VM on a host missing userspace configuration.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=60
syzbot Tested-by [email protected] on the v1 series
#124 lore raw
In reply to Jörg Rödel
syzbot ci has tested the following series

syzbot ci has tested the following series

[v1] KVM Planes + SEV-SNP Support
https://lore.kernel.org/all/[email protected]
* [PATCH 01/60] x86/sev: Define the #HV doorbell page structure
* [PATCH 02/60] KVM: SVM: Add support for the SEV-SNP #HV doorbell page NAE event
* [PATCH 03/60] KVM: SVM: Inject #HV when Restricted Injection is active
* [PATCH 04/60] KVM: SVM: Inject NMIs when Restricted Injection is active
* [PATCH 05/60] KVM: SVM: Inject MCEs when Restricted Injection is active
* [PATCH 06/60] KVM: SVM: Enable Restricted Injection for an SEV-SNP guest
* [PATCH 07/60] KVM: SVM: Add support for the SEV-SNP #HV IPI NAE event
* [PATCH 08/60] Documentation: kvm: introduce "VM plane" concept
* [PATCH 09/60] kvm: Introduce struct kvm_plane
* [PATCH 10/60] kvm: Move vcpu_array to struct kvm_plane
* [PATCH 11/60] kvm: Introduce struct kvm_vcpu_common
* [PATCH 12/60] kvm: Move vcpu accounting to struct kvm_vcpu_common
* [PATCH 13/60] kvm: Add read accessors for kvm_vcpu scheduling state
* [PATCH 14/60] kvm: Make kvm_running_vcpus point to struct kvm_vcpu_common
* [PATCH 15/60] kvm: Move VCPU scheduling state to struct kvm_vcpu_common
* [PATCH 16/60] kvm: Add accessors for kvm_vcpu->mutex
* [PATCH 17/60] kvm: Move VCPU locking to struct kvm_vcpu_common
* [PATCH 18/60] kvm: Move kvm_vcpu->rcuwait to struct kvm_vcpu_common
* [PATCH 19/60] kvm: Introduce accessors for kvm_vcpu->mode
* [PATCH 20/60] kvm: Move kvm_vcpu mode and requests field to struct kvm_vcpu_common
* [PATCH 21/60] kvm: Introduce per-plane VCPU requests
* [PATCH 22/60] kvm: Move kvm_vcpu pid members to struct kvm_vcpu_common
* [PATCH 23/60] kvm: Move kvm_vcpu sigset members to struct kvm_vcpu_common
* [PATCH 24/60] kvm: Move kvm_vcpu spinloop members to struct kvm_vcpu_common
* [PATCH 25/60] kvm: Move kvm_vcpu->dirty_ring to struct kvm_vcpu_common
* [PATCH 26/60] kvm: Introduce arch-specific plane state
* [PATCH 27/60] kvm: Introduce arch-specific part of struct kvm_vcpu_common
* [PATCH 28/60] kvm: Implement KVM_CAP_PLANES
* [PATCH 29/60] kvm: Implement KVM_CREATE_PLANE ioctl
* [PATCH 30/60] kvm: Add KVM_EXIT_PLANE_EVENT
* [PATCH 31/60] kvm: Allocate struct kvm_plane in architecture code
* [PATCH 32/60] kvm: Allocate struct kvm_run only for struct kvm_vcpu_common
* [PATCH 33/60] KVM: Implement KVM_CREATE_VCPU ioctl for planes
* [PATCH 34/60] kvm: Keep track of plane VCPUs in struct kvm_vcpu_common
* [PATCH 35/60] kvm: Add VCPU plane-scheduling state and helpers
* [PATCH 36/60] kvm: Add plane_level to kvm_kernel_irq_routing_entry
* [PATCH 37/60] kvm: Pass plane_level to kvm_set_routing_entry()
* [PATCH 38/60] kvm: Make KVM_SIGNAL_MSI per plane
* [PATCH 39/60] kvm: Make KVM_SET_GSI_ROUTING per plane
* [PATCH 40/60] kvm: x86: Handle IOAPIC EOIs per plane
* [PATCH 41/60] kvm: x86: Make apic_map per plane
* [PATCH 42/60] kvm: x86: Make local APIC code aware of planes
* [PATCH 43/60] kvm: x86: Move CPUID state to struct kvm_vcpu_arch_common
* [PATCH 44/60] kvm: x86: Move cpu_caps to struct kvm_vcpu_arch_common
* [PATCH 45/60] kvm: x86: Update state for all plane VCPUs after CPUID update
* [PATCH 46/60] kvm: x86: Share MTRR state across planes
* [PATCH 47/60] kvm: x86: Select a plane to run
* [PATCH 48/60] kvm: x86: Make event injection VCPU requests per-plane
* [PATCH 49/60] kvm: x86: Allow hardware backend to overwrite struct kvm_plane allocation
* [PATCH 50/60] kvm: x86: Make KVM_REQ_UPDATE_PROTECTED_GUEST_STATE per plane
* [PATCH 51/60] kvm: x86: Share pio_data across planes
* [PATCH 52/60] kvm: x86: Switch to plane0 if it has events
* [PATCH 53/60] kvm: x86: Introduce max_planes x86-op
* [PATCH 54/60] kvm: x86: Restrict KVM planes support to KVM_IRQCHIP_SPLIT
* [PATCH 55/60] kvm: svm: Track vmsa_features per plane
* [PATCH 56/60] kvm: svm: Implement GET_AP_APIC_IDS NAE event
* [PATCH 57/60] kvm: sev: Allow for VMPL level specification in AP create
* [PATCH 58/60] kvm: svm: Invoke a specified VMPL level VMSA for the vCPU
* [PATCH 59/60] kvm: svm: Implement max_planes x86 operation
* [PATCH 60/60] kvm: svm: Advertise full multi-VMPL support to the SNP guest

and found the following issue:
WARNING in __kvm_plane_ioctl

Full report is available here:
https://ci.syzbot.org/series/7426b4e7-e1df-4f2e-8464-acda36316e29

***

WARNING in __kvm_plane_ioctl

tree:      torvalds
URL:       https://kernel.googlesource.com/pub/scm/linux/kernel/git/torvalds/linux
base:      2d3090a8aeb596a26935db0955d46c9a5db5c6ce
arch:      amd64
compiler:  Debian clang version 21.1.8 (++20251221033036+2078da43e25a-1~exp1~20251221153213.50), Debian LLD 21.1.8
config:    https://ci.syzbot.org/builds/f9f7af87-a544-413f-b7e0-662f92794f11/config
syz repro: https://ci.syzbot.org/findings/751865bf-494c-4000-9b11-d8aa15f03893/syz_repro
------------[ cut here ]------------
r == -EBUSY
WARNING: arch/x86/kvm/../../../virt/kvm/kvm_main.c:4412 at kvm_plane_ioctl_create_vcpu virt/kvm/kvm_main.c:4412 [inline], CPU#1: syz.2.19/5809
WARNING: arch/x86/kvm/../../../virt/kvm/kvm_main.c:4412 at __kvm_plane_ioctl+0x8bd/0xa70 virt/kvm/kvm_main.c:4908, CPU#1: syz.2.19/5809
Modules linked in:
CPU: 1 UID: 0 PID: 5809 Comm: syz.2.19 Not tainted syzkaller #0 PREEMPT(full) 
Hardware name: QEMU Standard PC (Q35 + ICH9, 2009), BIOS 1.16.2-debian-1.16.2-1 04/01/2014
RIP: 0010:kvm_plane_ioctl_create_vcpu virt/kvm/kvm_main.c:4412 [inline]
RIP: 0010:__kvm_plane_ioctl+0x8bd/0xa70 virt/kvm/kvm_main.c:4908
Code: e8 c8 0a 09 00 4c 89 f7 e8 80 a3 ff ff 48 8b 3d a9 a8 fd 11 4c 89 f6 e8 c1 35 e7 00 49 63 c7 e9 8c fc ff ff e8 94 2f 87 00 90 <0f> 0b 90 41 bf f0 ff ff ff eb bc e8 e3 eb 68 0a 89 f9 80 e1 07 80
RSP: 0018:ffffc9000318f940 EFLAGS: 00010293
RAX: ffffffff813f480c RBX: ffff8881ba113410 RCX: ffff88810d4f4a80
RDX: 0000000000000000 RSI: ffffffff8e6fa0b0 RDI: 00000000fffffff0
RBP: ffffc9000318fa70 R08: ffff88810d4f4a80 R09: 0000000000000002
R10: 00000000fffffff0 R11: 0000000000000000 R12: 1ffff92000631f34
R13: dffffc0000000000 R14: ffff8881b446cb18 R15: 00000000fffffff0
FS:  00007f13e64ad6c0(0000) GS:ffff8882a929c000(0000) knlGS:0000000000000000
CS:  0010 DS: 0000 ES: 0000 CR0: 0000000080050033
CR2: 00007f13e64acff8 CR3: 00000001a6053000 CR4: 0000000000352ef0
Call Trace:
 <TASK>
 kvm_vm_ioctl+0x88e/0xbf0 virt/kvm/kvm_main.c:5546
 vfs_ioctl fs/ioctl.c:51 [inline]
 __do_sys_ioctl fs/ioctl.c:597 [inline]
 __se_sys_ioctl+0xfc/0x170 fs/ioctl.c:583
 do_syscall_x64 arch/x86/entry/syscall_64.c:63 [inline]
 do_syscall_64+0x174/0x580 arch/x86/entry/syscall_64.c:94
 entry_SYSCALL_64_after_hwframe+0x77/0x7f
RIP: 0033:0x7f13e559ce59
Code: ff c3 66 2e 0f 1f 84 00 00 00 00 00 0f 1f 44 00 00 48 89 f8 48 89 f7 48 89 d6 48 89 ca 4d 89 c2 4d 89 c8 4c 8b 4c 24 08 0f 05 <48> 3d 01 f0 ff ff 73 01 c3 48 c7 c1 e8 ff ff ff f7 d8 64 89 01 48
RSP: 002b:00007f13e64ad028 EFLAGS: 00000246 ORIG_RAX: 0000000000000010
RAX: ffffffffffffffda RBX: 00007f13e5816090 RCX: 00007f13e559ce59
RDX: 0000000000000000 RSI: 000000000000ae41 RDI: 0000000000000004
RBP: 00007f13e5632d6f R08: 0000000000000000 R09: 0000000000000000
R10: 0000000000000000 R11: 0000000000000246 R12: 0000000000000000
R13: 00007f13e5816128 R14: 00007f13e5816090 R15: 00007fff90c39748
 </TASK>

***

If these findings have caused you to resend the series or submit a separate fix, please add the following tag to your commit message:

---
This report is generated by a bot. It may contain errors. syzbot ci engineers can be reached at [email protected].

To test a patch for this bug, please reply with #syz test (should be on a separate line).

The patch should be attached to the email.
Note: arguments like custom git repos and branches are not supported.

#125 lore raw
In reply to Jörg Rödel

Hi,

Quoted 5 lines · Jörg Rödel

From: Joerg Roedel <[email protected]>
...
This changes depend on Melodys patches for supporting restricted injection. As they are required to run COCONUT-SVSM, they are included here for completeness.

Btw, I am fixing up the restricted injection patches based on AI reviews, will send the new version out upstream soon. So you dont need to review the current version.

Signature · 2 lines
Thanks,
Melody
maintainer
#126 lore raw

cover KVM: Fixes for the Planes + SEV-SNP series

In reply to Jörg Rödel

From: David Woodhouse <[email protected]>

I rebased Jörg's KVM Planes + SEV-SNP series¹ to 7.4-rc2² and used it to add VSM support³. Here's a batch of fixes.

Thirteen of the sixteen apply in order, without conflicts, to the kvm-planes-v7.1 branch. The other three need what has landed upstream since v7.1: patch 1 fixes the s390 arm64 KVM module, which only arrived in 7.4; patch 12 touches the PID-table clearing in vmx_vcpu_free() from b41f2ca6c060; and patch 16 extends the VMSA invalidation from d1a3c2162334.

The ones that matter:

- 01 s390: arm64: Pass plane_level to the kvm_set_routing_entry() stub The arm64 KVM module on s390 kept the old prototype; the kernel test robot caught it on the rebased tree.

- 05 Initialise Hyper-V state on every plane vCPU kvm_set_cpuid() passed the plane 0 vCPU to kvm_hv_vcpu_init() for every plane, and a twin created late got vp_index -1.

- 08 Publish a new plane only once KVM_CREATE_PLANE cannot fail A failed create left a freed plane in kvm->planes[], where the SNP VMPL switch could still find it, and VM teardown freed it again.

- 10 Keep current_vcpu in sync in vcpu_load()
A reschedule inside an ioctl on a plane fd put and loaded the wrong twin, and a failed twin create left current_vcpu dangling.

- 11 Load each plane twin while updating it after KVM_SET_CPUID On VMX the vendor callback wrote the twins' controls into the caller's VMCS.

- 12 Leave the IPIv PID-table entry to the plane 0 vCPU
A twin has the same vCPU ID as plane 0, so creating or freeing one repointed or cleared plane 0's entry. Latent for now, since planes inhibit APICv.

- 13 Leave TSC matching to the plane 0 vCPUs
Each twin counted as another vCPU with a matching TSC, so the master clock could never come back on.

- 14 Derive runtime CPUID bits from the executing plane OSXSAVE, OSPKE, APIC, MWAIT and the XSAVE sizes follow per-plane registers, but the table they live in is shared.

Three things I've noticed and *not* fixed here:

- The AVIC physical ID table is indexed by vCPU ID too, so it has the same collision as the IPIv PID table in patch 12. It's masked while APICv is inhibited for planes, as it is today.

- A host-side TSC write on plane 0 (or KVM_SET_TSC_KHZ) after the twins exist doesn't reach them. Fixing that means loading each twin, and I'd rather agree how before doing it.

- For Jörg and Tom: the SNP VMPL switch stops the current plane with kvm_vcpu_set_plane_stopped() and no latch, so KVM_GET_PLANE_RUN_STATE reports it as STOPPED, and a VMM that saves and restores that value turns it into a userspace stop. Does VMPL want a notion of KVM-initiated suspension of its own, or is that the intended contract?

¹ https://lore.kernel.org/all/[email protected]/
² https://git.infradead.org/?p=users/dwmw2/linux.git;a=shortlog;h=refs/heads/vsm2/planes-fixes
³ https://lore.kernel.org/kvm/[email protected]/

David Woodhouse (14):
KVM: s390: arm64: Pass plane_level to the kvm_set_routing_entry() stub
KVM: SVM: Derive the plane limit from the immutable VM type
KVM: planes: Report KVM_MAX_PLANES from the system fd
KVM: Guard the per-plane KVM_SIGNAL_MSI cases with __KVM_HAVE_MSI
KVM: x86: Initialise Hyper-V state on every plane vCPU
KVM: x86: Widen the plane vCPU fd ioctl allowlist
KVM: x86/mmu: Free obsolete roots of all planes
KVM: planes: Publish a new plane only once KVM_CREATE_PLANE cannot
fail
KVM: planes: Kick the running twin's pCPU, not the request target's
KVM: planes: Keep current_vcpu in sync in vcpu_load()
KVM: x86: Load each plane twin while updating it after KVM_SET_CPUID
KVM: VMX: Leave the IPIv PID-table entry to the plane 0 vCPU
KVM: planes: Annotate the lockless twin-publication protocol
KVM: SVM: Make KVM_REQ_VMSA_PAGE_RELOAD per plane

Mushahid Hussain (2):
KVM: x86: Leave TSC matching to the plane 0 vCPUs
KVM: x86: Derive runtime CPUID bits from the executing plane

 Documentation/virt/kvm/api.rst  |  21 ++++--
 arch/s390/kvm/arm64/arm.c       |   3 +-
 arch/x86/include/asm/kvm_host.h |   3 +-
 arch/x86/kvm/cpuid.c            | 128 +++++++++++++++++++++++---------
 arch/x86/kvm/cpuid.h            |   6 +-
 arch/x86/kvm/hyperv.c           |   7 +-
 arch/x86/kvm/irq.c              |   5 +-
 arch/x86/kvm/mmu/mmu.c          |  16 +++-
 arch/x86/kvm/svm/sev.c          |  29 ++++++--
 arch/x86/kvm/svm/svm.c          |  10 ++-
 arch/x86/kvm/svm/svm.h          |   2 -
 arch/x86/kvm/vmx/vmx.c          |  10 ++-
 arch/x86/kvm/x86.c              |  87 ++++++++++++++++++++--
 include/linux/kvm_host.h        |   7 +-
 virt/kvm/kvm_main.c             | 115 +++++++++++++++++++++++-----
 15 files changed, 365 insertions(+), 84 deletions(-)
Signature · 1 line
2.50.1
maintainer
#127 lore raw

PATCH 1/16 KVM: s390: arm64: Pass plane_level to the kvm_set_routing_entry() stub

From: David Woodhouse <[email protected]>

kvm_set_routing_entry() gained a plane_level argument, and every architecture's implementation was converted except the stub in the arm64 KVM module on s390, which arrived in the same release. An s390 build with that module then fails:

arch/s390/kvm/arm64/arm.c:745:5: error: conflicting types for 'kvm_set_routing_entry'

Give the stub the new signature. It still refuses every routing entry.

Fixes the s390 arm64 stub that "kvm: Pass plane_level to kvm_set_routing_entry()" did not convert.

---
 arch/s390/kvm/arm64/arm.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
arch/s390/kvm/arm64/arm.c +2 -1
index 64a609f087a8..0f0fab76e71a 100644
@@ -744,7 +744,8 @@ int kvm_set_msi(struct kvm_kernel_irq_routing_entry *e, struct kvm *kvm,
 
 int kvm_set_routing_entry(struct kvm *kvm,
 			  struct kvm_kernel_irq_routing_entry *e,
-			  const struct kvm_irq_routing_entry *ue)
+			  const struct kvm_irq_routing_entry *ue,
+			  unsigned plane_level)
 {
 	return -EINVAL;
 }
Signature · 1 line
2.50.1
maintainer
#128 lore raw

PATCH 2/16 KVM: SVM: Derive the plane limit from the immutable VM type

In reply to David Woodhouse

From: David Woodhouse <[email protected]>

svm_max_planes() calls ____sev_snp_guest() without kvm->lock, from KVM_CHECK_EXTENSION(KVM_CAP_PLANES). That races __sev_guest_init(), which sets and rolls back sev->active/es_active/SNP_ACTIVE under kvm->lock, and userspace can force the rollback (ASID exhaustion, firmware init failure). It is exactly the rule kvm_lockdep_assert_sev_lock_held() documents, dodged by using the assertion-free helper. It also leaves KVM_CREATE_PLANE's pre-lock bound check with a limit that can change under it.

Key SNP-ness off kvm->arch.vm_type == KVM_X86_SNP_VM instead, which is fixed at VM creation. While there, tidy the max_planes plumbing:

- floor sev_snp_max_planes() at 1, so a host CPUID reporting zero VMPLs cannot make the VM report no planes at all;
- drop the sev_nr_vcpu_planes() declaration, which has no definition or caller;
- drop the !CONFIG_KVM_AMD_SEV sev_snp_max_planes() stub, dead since its only caller sits under the same #ifdef (and its return type disagreed with the declaration).

Fixes the unlocked SEV state check in "kvm: svm: Implement max_planes x86 operation".

---
 arch/x86/kvm/svm/sev.c |  6 +++++-
 arch/x86/kvm/svm/svm.c | 11 ++++++++++-
 arch/x86/kvm/svm/svm.h |  2 --
 3 files changed, 15 insertions(+), 4 deletions(-)
arch/x86/kvm/svm/sev.c +5 -1
index bb61bdf9e336..fd7605e47216 100644
@@ -6000,5 +6000,9 @@ bool sev_snp_blocked(enum inject_type type, struct kvm_vcpu *vcpu)
 
 int sev_snp_max_planes(struct kvm *kvm)
 {
-	return vmpl_levels;
+	/*
+	 * CPUID 0x8000001f EBX[15:12] is host-provided data; a zero there
+	 * must not report "no planes at all" (plane 0 always exists).
+	 */
+	return max(vmpl_levels, 1U);
 }
arch/x86/kvm/svm/svm.c +10 -1
index 6cc070952b93..1848136f3bb9 100644
@@ -5517,7 +5517,16 @@ static void svm_free_plane(struct kvm_plane *plane)
 static unsigned svm_max_planes(struct kvm *kvm)
 {
 #ifdef CONFIG_KVM_AMD_SEV
-	if (____sev_snp_guest(kvm))
+	/*
+	 * Key SNP-ness off the immutable vm_type, not the mutable SEV
+	 * state: KVM_CHECK_EXTENSION(KVM_CAP_PLANES) reaches here with no
+	 * lock held, while __sev_guest_init() sets and rolls back
+	 * sev->active/es_active/SNP_ACTIVE under kvm->lock.  The VM type
+	 * is fixed at creation, so the lockless read neither races the
+	 * rollback nor lets the limit change under KVM_CREATE_PLANE's
+	 * pre-lock bound check.
+	 */
+	if (kvm->arch.vm_type == KVM_X86_SNP_VM)
 		return sev_snp_max_planes(kvm);
 #endif
 
arch/x86/kvm/svm/svm.h +0 -2
index 00ce3f4ef497..2b7663722be6 100644
@@ -1069,7 +1069,6 @@ static inline bool sev_snp_is_rinj_active(struct kvm_vcpu *vcpu)
 	return is_sev_snp_guest(vcpu) &&
 		(sev->vmsa_features & SVM_SEV_FEAT_RESTRICTED_INJECTION);
 };
-int sev_nr_vcpu_planes(struct kvm *kvm);
 int sev_snp_max_planes(struct kvm *kvm);
 #else
 static inline struct page *snp_safe_alloc_page_node(int node, gfp_t gfp)
@@ -1104,7 +1103,6 @@ static inline bool sev_snp_inject(enum inject_type type, struct kvm_vcpu *vcpu)
 static inline void sev_snp_cancel_injection(struct kvm_vcpu *vcpu) {}
 static inline bool sev_snp_blocked(enum inject_type type, struct kvm_vcpu *vcpu) { return false; }
 static inline bool sev_snp_is_rinj_active(struct kvm_vcpu *vcpu) { return false; }
-static inline unsigned sev_snp_max_planes(struct kvm *kvm) { return 1; }
 #endif
 
 /* vmenter.S */
Signature · 1 line
2.50.1
maintainer
#129 lore raw

PATCH 3/16 KVM: planes: Report KVM_MAX_PLANES from the system fd

In reply to David Woodhouse

From: David Woodhouse <[email protected]>

KVM_CHECK_EXTENSION(KVM_CAP_PLANES) on the system fd returns a hardcoded 1. The KVM_CAP_PLANES opt-in added later in this series takes its ceiling from exactly that query, so a VMM following the documented negotiation concludes it may request one plane, and never enables VSM at all. Return KVM_MAX_PLANES there instead.

While there, move the id bound check in kvm_vm_ioctl_create_plane() under kvm->lock. Every KVM_ENABLE_CAP path that changes kvm_arch_max_planes() takes that lock, and the limit only ever grows, so an unlocked check could at worst spuriously reject a create racing KVM_CAP_SPLIT_IRQCHIP. Checking under the lock just makes the bound the one the plane is actually installed against.

Found by a local Sashiko review.

Fixes the system-fd KVM_CAP_PLANES query in "kvm: Implement KVM_CAP_PLANES".

---
 arch/x86/kvm/svm/svm.c |  5 ++---
 virt/kvm/kvm_main.c    | 15 +++++++++++++--
 2 files changed, 15 insertions(+), 5 deletions(-)
arch/x86/kvm/svm/svm.c +2 -3
index 1848136f3bb9..1c63eb3fcdc0 100644
@@ -5522,9 +5522,8 @@ static unsigned svm_max_planes(struct kvm *kvm)
 	 * state: KVM_CHECK_EXTENSION(KVM_CAP_PLANES) reaches here with no
 	 * lock held, while __sev_guest_init() sets and rolls back
 	 * sev->active/es_active/SNP_ACTIVE under kvm->lock.  The VM type
-	 * is fixed at creation, so the lockless read neither races the
-	 * rollback nor lets the limit change under KVM_CREATE_PLANE's
-	 * pre-lock bound check.
+	 * is fixed at creation, so the lockless read cannot race the
+	 * rollback.
 	 */
 	if (kvm->arch.vm_type == KVM_X86_SNP_VM)
 		return sev_snp_max_planes(kvm);
virt/kvm/kvm_main.c +13 -2
index 8550303f5757..0136156df252 100644
@@ -5375,7 +5375,12 @@ static int kvm_vm_ioctl_check_extension_generic(struct kvm *kvm, long arg)
 	case KVM_CAP_PLANES:
 		if (kvm)
 			return kvm_arch_max_planes(kvm);
-		return 1;
+		/*
+		 * The system fd reports the ceiling a VM may opt in to via
+		 * KVM_ENABLE_CAP(KVM_CAP_PLANES); a VM fd reports what the
+		 * VM actually has.
+		 */
+		return KVM_MAX_PLANES;
 	default:
 		break;
 	}
@@ -5587,6 +5592,13 @@ static int kvm_vm_ioctl_create_plane(struct kvm *kvm, unsigned id)
 	struct file *file;
 	int r, fd;
 
+	guard(mutex)(&kvm->lock);
+
+	/*
+	 * Check the limit under kvm->lock, where KVM_ENABLE_CAP sets it,
+	 * so a concurrent cap enable cannot change it between this check
+	 * and the installation below.
+	 */
 	if (id >= kvm_arch_max_planes(kvm) ||
 	    WARN_ON_ONCE(id >= KVM_MAX_PLANES))
 		return -EINVAL;
@@ -5595,7 +5607,6 @@ static int kvm_vm_ioctl_create_plane(struct kvm *kvm, unsigned id)
 	if (!kvm_arch_irqchip_in_kernel(kvm))
 		return -EINVAL;
 
-	guard(mutex)(&kvm->lock);
 	if (kvm->planes[id])
 		return -EEXIST;
 
Signature · 1 line
2.50.1
maintainer
#130 lore raw

PATCH 4/16 KVM: Guard the per-plane KVM_SIGNAL_MSI cases with __KVM_HAVE_MSI

In reply to David Woodhouse

From: David Woodhouse <[email protected]>

KVM_SIGNAL_MSI fails with -ENOTTY on both the VM fd and plane fds, while KVM_CHECK_EXTENSION still reports KVM_CAP_SIGNAL_MSI. A VMM that sends MSIs through the ioctl, such as QEMU with a split irqchip, cannot inject device interrupts at all.

The new cases in kvm_vm_ioctl() and __kvm_plane_ioctl() test CONFIG_HAVE_KVM_MSI, which no Kconfig defines; the macro architectures set is __KVM_HAVE_MSI, as the old VM-fd case used. So both compile away. Use __KVM_HAVE_MSI.

test_plane_msi in the x86 planes selftest, added later in this series, catches this.

Fixes the per-plane KVM_SIGNAL_MSI cases added by "kvm: Make KVM_SIGNAL_MSI per plane".

---
 virt/kvm/kvm_main.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
virt/kvm/kvm_main.c +2 -2
index 0136156df252..95de904f0c61 100644
@@ -5016,7 +5016,7 @@ static long __kvm_plane_ioctl(struct kvm_plane *plane, unsigned int ioctl, unsig
 	case KVM_CREATE_VCPU:
 		r = kvm_plane_ioctl_create_vcpu(plane, arg);
 		break;
-#ifdef CONFIG_HAVE_KVM_MSI
+#ifdef __KVM_HAVE_MSI
 	case KVM_SIGNAL_MSI: {
 		void __user *argp = (void __user *)arg;
 		struct kvm_msi msi;
@@ -5662,7 +5662,7 @@ static long kvm_vm_ioctl(struct file *filp,
 		r = kvm_vm_ioctl_create_plane(kvm, arg);
 		break;
 	case KVM_CREATE_VCPU:
-#ifdef CONFIG_HAVE_KVM_MSI
+#ifdef __KVM_HAVE_MSI
 	case KVM_SIGNAL_MSI:
 #endif
 #ifdef CONFIG_HAVE_KVM_IRQ_ROUTING
Signature · 1 line
2.50.1
maintainer
#131 lore raw

PATCH 5/16 KVM: x86: Initialise Hyper-V state on every plane vCPU

In reply to David Woodhouse

From: David Woodhouse <[email protected]>

When userspace sets a Hyper-V CPUID, kvm_set_cpuid() walks the vCPU's planes to allocate the per-plane kvm_vcpu_hv, but passes the plane-0 vCPU to kvm_hv_vcpu_init() on every iteration. Non-zero plane vCPUs are left without hv_vcpu, which trips the WARN_ON_ONCE(vcpu->arch.hyperv_enabled) in kvm_hv_set_cpuid() and leaves their vp_index and SynIC state uninitialised until a lazy init path happens to run. Pass the plane vCPU being iterated.

A twin created *after* KVM_SET_CPUID never passes through kvm_set_cpuid() at all, but shares a CPUID that already advertises Hyper-V. Allocate its context in kvm_arch_vcpu_create(), so that -ENOMEM fails KVM_CREATE_VCPU on the plane fd rather than leaving one twin silently without Hyper-V.

That allocation runs before the twin's vcpu->vcpu_idx is assigned, so kvm_hv_vcpu_init() takes the default VP index from the common, which already holds it. With vcpu->vcpu_idx the twin would latch 0xFFFFFFFF, and a host write of the real index to HV_X64_MSR_VP_INDEX would then decrement num_mismatched_vp_indexes for a mismatch that was never counted. (Assigning vcpu->vcpu_idx earlier instead would defeat the "vcpu_idx < 0 means unreachable" test that to_hv_vcpu()'s lockdep assertion falls back on during creation.)

Also admit KVM_ENABLE_CAP on plane vCPU fds, so userspace can enable per-vCPU capabilities such as KVM_CAP_HYPERV_SYNIC2 on a plane vCPU before it first runs.

Fixes the Hyper-V initialisation of plane vCPUs in "kvm: x86: Update state for all plane VCPUs after CPUID update" and "KVM: Implement KVM_CREATE_VCPU ioctl for planes".

---
 arch/x86/kvm/cpuid.c  |  4 ++--
 arch/x86/kvm/cpuid.h  |  1 +
 arch/x86/kvm/hyperv.c |  7 ++++++-
 arch/x86/kvm/x86.c    | 20 +++++++++++++++++++-
 4 files changed, 28 insertions(+), 4 deletions(-)
arch/x86/kvm/cpuid.c +2 -2
index 7a32f658238a..54bcfedc5226 100644
@@ -326,7 +326,7 @@ static void kvm_update_cpuid_runtime(struct kvm_vcpu *vcpu)
 						 vcpu->arch.ia32_xss, true);
 }
 
-static bool kvm_cpuid_has_hyperv(struct kvm_vcpu *vcpu)
+bool kvm_cpuid_has_hyperv(struct kvm_vcpu *vcpu)
 {
 #ifdef CONFIG_KVM_HYPERV
 	struct kvm_cpuid_entry2 *entry;
@@ -553,7 +553,7 @@ static int kvm_set_cpuid(struct kvm_vcpu *vcpu, struct kvm_cpuid_entry2 *e2,
 #ifdef CONFIG_KVM_HYPERV
 	if (kvm_cpuid_has_hyperv(vcpu)) {
 		vcpu_for_each_plane(common, i, v) {
-			r = kvm_hv_vcpu_init(vcpu);
+			r = kvm_hv_vcpu_init(v);
 			if (r)
 				goto err;
 		}
arch/x86/kvm/cpuid.h +1 -0
index 2abfab4b1c33..02dbd02c8f80 100644
@@ -21,6 +21,7 @@ static inline void kvm_finalize_cpu_caps(void)
 }
 
 void kvm_vcpu_after_set_cpuid(struct kvm_vcpu *vcpu);
+bool kvm_cpuid_has_hyperv(struct kvm_vcpu *vcpu);
 struct kvm_cpuid_entry2 *kvm_find_cpuid_entry2(struct kvm_cpuid_entry2 *entries,
 					       int nent, u32 function, u64 index);
 /*
arch/x86/kvm/hyperv.c +6 -1
index 1855b7e13e82..0b7440d09d49 100644
@@ -997,7 +997,12 @@ int kvm_hv_vcpu_init(struct kvm_vcpu *vcpu)
 	for (i = 0; i < ARRAY_SIZE(hv_vcpu->stimer); i++)
 		stimer_init(&hv_vcpu->stimer[i], i);
 
-	hv_vcpu->vp_index = vcpu->vcpu_idx;
+	/*
+	 * A plane twin is initialised here from kvm_arch_vcpu_create(),
+	 * before vcpu->vcpu_idx is assigned; the index it is about to
+	 * get is already in the shared common.
+	 */
+	hv_vcpu->vp_index = vcpu->common->vcpu_idx;
 
 	for (i = 0; i < HV_NR_TLB_FLUSH_FIFOS; i++) {
 		INIT_KFIFO(hv_vcpu->tlb_flush_fifo[i].entries);
arch/x86/kvm/x86.c +19 -1
index 2e38eaa0b7df..25f574bfbb32 100644
@@ -3467,6 +3467,7 @@ static int kvm_vcpu_ioctl_enable_cap(struct kvm_vcpu *vcpu,
 bool kvm_arch_is_vcpu_plane_ioctl(unsigned ioctl)
 {
 	switch (ioctl) {
+	case KVM_ENABLE_CAP:
 	case KVM_GET_DEBUGREGS:
 	case KVM_SET_DEBUGREGS:
 	case KVM_GET_LAPIC:
@@ -9635,9 +9636,24 @@ int kvm_arch_vcpu_create(struct kvm_vcpu *vcpu)
 	vcpu->arch.hv_root_tdp = INVALID_PAGE;
 #endif
 
+	/*
+	 * A plane twin created after KVM_SET_CPUID shares CPUID that may
+	 * already advertise Hyper-V.  Allocate the Hyper-V context now so
+	 * a failure fails the creation; left to kvm_vcpu_after_set_cpuid()
+	 * below, an -ENOMEM silently downgrades just this twin to no
+	 * Hyper-V (its MSRs fail) while the shared CPUID still advertises
+	 * the Hyper-V signature, and nothing short of another full
+	 * KVM_SET_CPUID repairs it.
+	 */
+	if (kvm_cpuid_has_hyperv(vcpu)) {
+		r = kvm_hv_vcpu_init(vcpu);
+		if (r)
+			goto free_guest_fpu;
+	}
+
 	r = kvm_x86_call(vcpu_create)(vcpu);
 	if (r)
-		goto free_guest_fpu;
+		goto free_hv;
 
 	kvm_xen_init_vcpu(vcpu);
 	vcpu_load(vcpu);
@@ -9648,6 +9664,8 @@ int kvm_arch_vcpu_create(struct kvm_vcpu *vcpu)
 	vcpu_put(vcpu);
 	return 0;
 
+free_hv:
+	kvm_hv_vcpu_uninit(vcpu);
 free_guest_fpu:
 	fpu_free_guest_fpstate(&vcpu->arch.guest_fpu);
 free_emulate_ctxt:
Signature · 1 line
2.50.1
maintainer
#132 lore raw

PATCH 6/16 KVM: x86: Widen the plane vCPU fd ioctl allowlist

In reply to David Woodhouse

From: David Woodhouse <[email protected]>

A VMM that puts an initial VTL context on a higher plane cannot finish the job through the plane vCPU's fd. kvm_arch_is_vcpu_plane_ioctl() refuses the ioctls it needs, while the one capability ioctl it does admit, KVM_ENABLE_CAP, reaches *every* vCPU capability handler.

- KVM_GET/SET_MP_STATE: a plane vCPU for an AP starts in wait-for-INIT like any non-boot x86 vCPU, and nothing ever sends INIT/SIPI to the higher plane of an AP. The VMM has to mark it runnable itself.
- KVM_SET_GUEST_DEBUG: single-step and breakpoints on a non-zero plane. The debug exits surface through the plane-0 KVM_RUN loop like any other plane exit.
- KVM_X86_SETUP_MCE: mcg_cap is per vCPU and every twin starts with the KVM_MAX_MCE_BANKS default, so programming plane 0 alone leaves the planes disagreeing. Hyper-V shares MCG state across VTLs.
- KVM_ENABLE_CAP: accept only the audited capabilities on a plane fd, the SynIC pair and the two CPUID-enforcement toggles, which change state that each twin keeps separately. Anything else gets -EINVAL, as non-allowlisted ioctls already do. The nested paths behind enlightened VMCS and direct TLB flush have never been audited for plane_level > 0, and without the filter any future vCPU cap would work on planes without anyone having decided that it should.

KVM_SET_GUEST_DEBUG on a higher plane can set KVM_GUESTDBG_BLOCKIRQ there, but the VM-wide APICv BLOCKIRQ inhibit is recomputed over kvm_for_each_vcpu(), which walks plane 0 only. The inhibit then never rises for a higher plane, and a plane-0 update drops it while a higher plane still wants it. Recompute it over every twin. Nothing reaches this yet (multi-plane VMs are SEV-SNP only, which inhibits APICv anyway), but it would bite as soon as planes and APICv could coexist.

api.rst marks the new ioctls, and KVM_ENABLE_CAP, as accepted on all planes, and lists the capabilities a plane fd accepts.

The KVM_ENABLE_CAP filter was found by a local Sashiko review.

The KVM_X86_SETUP_MCE part is from Mushahid Hussain.

---
 Documentation/virt/kvm/api.rst | 14 ++++++++-----
 arch/x86/kvm/x86.c             | 38 ++++++++++++++++++++++++++++++----
 2 files changed, 43 insertions(+), 9 deletions(-)
Documentation/virt/kvm/api.rst +9 -5
index 6c701cda61ef..d21378183c51 100644
@@ -1494,7 +1494,7 @@ documentation when it pops into existence).
 
 :Capability: KVM_CAP_ENABLE_CAP
 :Architectures: mips, ppc, s390, x86, loongarch
-:Type: vcpu ioctl
+:Type: vcpu ioctl (all planes)
 :Parameters: struct kvm_enable_cap (in)
 :Returns: 0 on success; -1 on error
 
@@ -1515,6 +1515,10 @@ do support it, it only works for extensions that are supported for enablement.
 To check if a capability can be enabled, the KVM_CHECK_EXTENSION ioctl should
 be used.
 
+On x86, the vCPU fd of a plane other than plane 0 accepts only
+KVM_CAP_HYPERV_SYNIC, KVM_CAP_HYPERV_SYNIC2, KVM_CAP_HYPERV_ENFORCE_CPUID and
+KVM_CAP_ENFORCE_PV_FEATURE_CPUID; any other capability fails with -EINVAL.
+
 ::
 
   struct kvm_enable_cap {
@@ -1549,7 +1553,7 @@ for vm-wide capabilities.
 
 :Capability: KVM_CAP_MP_STATE
 :Architectures: x86, s390, arm64, riscv, loongarch
-:Type: vcpu ioctl
+:Type: vcpu ioctl (all planes)
 :Parameters: struct kvm_mp_state (out)
 :Returns: 0 on success; -1 on error
 
@@ -1632,7 +1636,7 @@ whether the vcpu is runnable.
 
 :Capability: KVM_CAP_MP_STATE
 :Architectures: x86, s390, arm64, riscv, loongarch
-:Type: vcpu ioctl
+:Type: vcpu ioctl (all planes)
 :Parameters: struct kvm_mp_state (in)
 :Returns: 0 on success; -1 on error
 
@@ -3779,7 +3783,7 @@ handled.
 
 :Capability: KVM_CAP_SET_GUEST_DEBUG
 :Architectures: x86, s390, ppc, arm64
-:Type: vcpu ioctl
+:Type: vcpu ioctl (all planes)
 :Parameters: struct kvm_guest_debug (in)
 :Returns: 0 on success; -1 on error
 
@@ -4623,7 +4627,7 @@ capabilities will have the corresponding bits set.
 
 :Capability: KVM_CAP_MCE
 :Architectures: x86
-:Type: vcpu ioctl
+:Type: vcpu ioctl (all planes)
 :Parameters: u64 mcg_cap (in)
 :Returns: 0 on success,
          -EFAULT if u64 mcg_cap cannot be read,
arch/x86/kvm/x86.c +34 -4
index 25f574bfbb32..cd0e9b6cf8f7 100644
@@ -3416,6 +3416,25 @@ static int kvm_vcpu_ioctl_enable_cap(struct kvm_vcpu *vcpu,
 	if (cap->flags)
 		return -EINVAL;
 
+	/*
+	 * KVM_ENABLE_CAP is accepted on plane vCPU fds, but only the caps
+	 * below have been audited for plane_level > 0.  The nested paths
+	 * behind eVMCS and direct TLB flush have not; refuse those rather
+	 * than implicitly extending every current and future vCPU cap to
+	 * planes.
+	 */
+	if (vcpu->plane_level) {
+		switch (cap->cap) {
+		case KVM_CAP_HYPERV_SYNIC:
+		case KVM_CAP_HYPERV_SYNIC2:
+		case KVM_CAP_HYPERV_ENFORCE_CPUID:
+		case KVM_CAP_ENFORCE_PV_FEATURE_CPUID:
+			break;
+		default:
+			return -EINVAL;
+		}
+	}
+
 	switch (cap->cap) {
 #ifdef CONFIG_KVM_HYPERV
 	case KVM_CAP_HYPERV_SYNIC2:
@@ -3468,6 +3487,7 @@ bool kvm_arch_is_vcpu_plane_ioctl(unsigned ioctl)
 {
 	switch (ioctl) {
 	case KVM_ENABLE_CAP:
+	case KVM_SET_GUEST_DEBUG:
 	case KVM_GET_DEBUGREGS:
 	case KVM_SET_DEBUGREGS:
 	case KVM_GET_LAPIC:
@@ -3480,6 +3500,8 @@ bool kvm_arch_is_vcpu_plane_ioctl(unsigned ioctl)
 	case KVM_SET_ONE_REG:
 	case KVM_GET_SREGS2:
 	case KVM_SET_SREGS2:
+	case KVM_GET_MP_STATE:
+	case KVM_SET_MP_STATE:
 	case KVM_GET_VCPU_EVENTS:
 	case KVM_SET_VCPU_EVENTS:
 	case KVM_GET_XCRS:
@@ -3488,6 +3510,7 @@ bool kvm_arch_is_vcpu_plane_ioctl(unsigned ioctl)
 	case KVM_GET_XSAVE2:
 	case KVM_SET_XSAVE:
 	case KVM_GET_REG_LIST:
+	case KVM_X86_SETUP_MCE:
 		return true;
 	default:
 		return false;
@@ -9355,19 +9378,26 @@ EXPORT_SYMBOL_FOR_KVM_INTERNAL(kvm_task_switch);
 static void kvm_arch_vcpu_guestdbg_update_apicv_inhibit(struct kvm *kvm)
 {
 	bool set = false;
-	struct kvm_vcpu *vcpu;
+	struct kvm_vcpu *vcpu, *v;
 	unsigned long i;
+	int p;
 
 	if (!enable_apicv)
 		return;
 
 	down_write(&kvm->arch.apicv_update_lock);
 
+	/*
+	 * KVM_SET_GUEST_DEBUG is accepted on every plane's vCPU fd, and
+	 * kvm_for_each_vcpu() only walks plane 0: look at every twin.
+	 */
 	kvm_for_each_vcpu(i, vcpu, kvm) {
-		if (vcpu->guest_debug & KVM_GUESTDBG_BLOCKIRQ) {
-			set = true;
-			break;
+		vcpu_for_each_plane(vcpu->common, p, v) {
+			if (v->guest_debug & KVM_GUESTDBG_BLOCKIRQ)
+				set = true;
 		}
+		if (set)
+			break;
 	}
 	__kvm_set_or_clear_apicv_inhibit(kvm, APICV_INHIBIT_REASON_BLOCKIRQ, set);
 	up_write(&kvm->arch.apicv_update_lock);
Signature · 1 line
2.50.1
maintainer
#133 lore raw

Re: [PATCH 06/16] KVM: x86: Widen the plane vCPU fd ioctl allowlist

Quoted 16 lines · David Woodhouse

From: David Woodhouse <[email protected]>

A VMM that puts an initial VTL context on a higher plane cannot finish the job through the plane vCPU's fd. kvm_arch_is_vcpu_plane_ioctl() refuses the ioctls it needs, while the one capability ioctl it does admit, KVM_ENABLE_CAP, reaches *every* vCPU capability handler.

- KVM_GET/SET_MP_STATE: a plane vCPU for an AP starts in wait-for-INIT like any non-boot x86 vCPU, and nothing ever sends INIT/SIPI to the higher plane of an AP. The VMM has to mark it runnable itself.
- KVM_SET_GUEST_DEBUG: single-step and breakpoints on a non-zero plane. The debug exits surface through the plane-0 KVM_RUN loop like any other plane exit.
- KVM_X86_SETUP_MCE: mcg_cap is per vCPU and every twin starts with the KVM_MAX_MCE_BANKS default, so programming plane 0 alone leaves

Quoted 8 lines

the planes disagreeing. Hyper-V shares MCG state across VTLs.
- KVM_ENABLE_CAP: accept only the audited capabilities on a plane fd, the SynIC pair and the two CPUID-enforcement toggles, which change state that each twin keeps separately. Anything else gets -EINVAL, as non-allowlisted ioctls already do. The nested paths behind enlightened VMCS and direct TLB flush have never been audited for plane_level > 0, and without the filter any future vCPU cap would work on planes without anyone having decided that it should.

Not sure about KVM_ENABLE_CAP, the other seem sane.

BTW at some point I'll rewrite these commit messages, they're infuriating...

Paolo

maintainer
#134 lore raw

Re: [PATCH 06/16] KVM: x86: Widen the plane vCPU fd ioctl allowlist

Quoted 26 lines · Paolo Bonzini
Quoted 16 lines · David Woodhouse

From: David Woodhouse <[email protected]>

A VMM that puts an initial VTL context on a higher plane cannot finish the job through the plane vCPU's fd. kvm_arch_is_vcpu_plane_ioctl() refuses the ioctls it needs, while the one capability ioctl it does admit, KVM_ENABLE_CAP, reaches *every* vCPU capability handler.

- KVM_GET/SET_MP_STATE: a plane vCPU for an AP starts in wait-for-INIT like any non-boot x86 vCPU, and nothing ever sends INIT/SIPI to the higher plane of an AP. The VMM has to mark it runnable itself.
- KVM_SET_GUEST_DEBUG: single-step and breakpoints on a non-zero plane. The debug exits surface through the plane-0 KVM_RUN loop like any other plane exit.
- KVM_X86_SETUP_MCE: mcg_cap is per vCPU and every twin starts with the KVM_MAX_MCE_BANKS default, so programming plane 0 alone leaves

Quoted 8 lines

the planes disagreeing. Hyper-V shares MCG state across VTLs.
- KVM_ENABLE_CAP: accept only the audited capabilities on a plane fd, the SynIC pair and the two CPUID-enforcement toggles, which change state that each twin keeps separately. Anything else gets -EINVAL, as non-allowlisted ioctls already do. The nested paths behind enlightened VMCS and direct TLB flush have never been audited for plane_level > 0, and without the filter any future vCPU cap would work on planes without anyone having decided that it should.

Quoted 3 lines

Not sure about KVM_ENABLE_CAP, the other seem sane.

BTW at some point I'll rewrite these commit messages, they're infuriating...

Yeah. I've grown kind of immune to them now. Even found myself typing "load-bearing" with my actual meat fingers the other day.

I've also been looking harder at the completion thing. We *can't* (well, I don't want to) enter another plane while there's still a completion waiting on the first plane.

Which means any control register or MSR interception which needs to be handed to another plane has to completed first.

I think we need to be able to complete them with the RIP unchanged (not advanced over the instruction). Then for VSM, it's VTL1 which will handle them and then advance RIP for its VTL0. VTL1 wants to be told the instruction length too; I could make QEMU find it but since KVM tends to have it anyway, I'm looking at how I can export that.

Signature · 3 lines
kvm-riscv mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/kvm-riscv
maintainer
#135 lore raw

Re: [PATCH 06/16] KVM: x86: Widen the plane vCPU fd ioctl allowlist

Quoted 6 lines · David Woodhouse

I've also been looking harder at the completion thing. We *can't* (well, I don't want to) enter another plane while there's still a completion waiting on the first plane.

Which means any control register or MSR interception which needs to be handed to another plane has to completed first.

Aside other things which have been catching fire, I've been playing with that a little.

I really don't want to mess with user completions remaining pending for one (or more!) plane while another plane runs.

So in the case where we take an interception in VTL0 and then let VTL1 decide what to do, we need the completion to be done *before* running VTL1. Which is why my idea of a cancel flag in KVM_SET_VCPU_EVENTS doesn't actually work once I build it into the full VSM stack. It works out OK if the VMM is making the decision, but not if we have to run VTL1.

So Sean was right¹ and we end up with a per-exit field much as he was asking for.

For the various exits with completions that are affected (RDMSR, WRMSR plus the new register write filter and forwarded Hyper-V hypercalls) we therefore want these options as the 'ret' action:

• KVM_INSN_COMPLETE: finish it, as now

 • KVM_INSN_GP:       inject #GP
 • KVM_INSN_UD:       inject #UD
 • KVM_INSN_RESUME:   do nothing; RIP and everything else stay as

userspace left them

So the relatively common cases #GP and #UD are trivial to inject, and any other exception can be injected by using KVM_SET_VCPU_EVENTS and then setting KVM_INSN_RESUME to let the exception take its course. The new VMware hypercall handling could do the same.

Entering KVM_RUN for a new plane would still process the completion for the previously-running plane.

I'd quite like the UAPI to be consistent, and since the MSR exits already use a u8 field for 'error', we can retcon this enum into that field when enabled by the appropriate CAP. (Without the CAP enabled, all non-zero values cause #GP as they do today.)

None of that depends on planes, so I'm building it in independent branches which I can post separately.

What planes does add is two bytes in the kvm_run header, on any VM which enabled KVM_CAP_PLANES:

• the plane which took the exit, on every exit
• the instruction length, for MSR and register write exits

The first means I can drop the 'plane' field I was adding to each new exit, and makes the existing ones (MSR, PIO, MMIO) usable from any plane.

The length is always valid for those two. On SVM without NextRIP the access goes through the emulator, which was going to have to decode it to skip it anyway. Not sure about SEV; not sure if these exits should even be enabled there? I also toyed with exposing the insn_length as a CAP which could then be absent in those cases (and on the ancient AMD CPUs without NextRIP).

I haven't changed the PIO or MMIO completions. At least in VSM those are never handed to another plane; what a higher VTL intercepts there is a memory access, which is a fault before the instruction has done anything. We could expand those later, if there's a need to.

So for VSM, the VMM answers RESUME and runs VTL1. VTL1 either injects the fault into VTL0, or does the write and advances VTL0's RIP itself, which is why it wanted the length.

That boots Windows with VBS on both Intel and AMD, with the VTL switch in the kernel or in userspace.

The whole stack is in my tree². The planes part is in vsm3v/planes-uapi; the parts which don't need planes are the vsm3/exit-insn-v2, vsm3/msr-exit-ret-v4, vsm3/hcall-exit-v7 and vsm3/regwr-filter-v10 branches there.

¹ https://lore.kernel.org/kvm/[email protected]/
² https://git.infradead.org/?p=users/dwmw2/linux.git;a=shortlog;h=refs/heads/vsm3v/p4-startup

maintainer
#136 lore raw

PATCH 7/16 KVM: x86/mmu: Free obsolete roots of all planes

In reply to David Woodhouse

From: David Woodhouse <[email protected]>

KVM_REQ_MMU_FREE_OBSOLETE_ROOTS lives in the request set shared by all planes of a vCPU. A fast zap (e.g. memslot deletion) invalidates *every* plane's TDP roots and raises the request once per vCPU, but whichever plane enters the guest next consumes the shared bit and frees only its own obsolete roots. The other planes keep an obsolete root with a valid-looking root.hpa, kvm_mmu_reload() does nothing for them, and every page fault bails out in is_page_fault_stale() with RET_PF_RETRY. The vCPU livelocks, taking the same EPT violation forever.

Observed with a Windows VSM guest spinning at ~18k EPT violations/s after a memslot update during boot, with the root obsolete (inv=1 obs=1) and the request already consumed (req=0).

Service every plane twin in kvm_mmu_free_obsolete_roots(). The inactive twins cannot be running (all planes of a vCPU share one thread), so their MMUs are stable.

---
 arch/x86/kvm/mmu/mmu.c | 16 ++++++++++++++--
 1 file changed, 14 insertions(+), 2 deletions(-)
arch/x86/kvm/mmu/mmu.c +14 -2
index 89cec6dd757a..e8eb1e48df36 100644
@@ -6401,8 +6401,20 @@ static void __kvm_mmu_free_obsolete_roots(struct kvm *kvm, struct kvm_mmu *mmu)
 
 void kvm_mmu_free_obsolete_roots(struct kvm_vcpu *vcpu)
 {
-	__kvm_mmu_free_obsolete_roots(vcpu->kvm, &vcpu->arch.root_mmu);
-	__kvm_mmu_free_obsolete_roots(vcpu->kvm, &vcpu->arch.guest_mmu);
+	struct kvm_vcpu *v;
+	int i;
+
+	/*
+	 * KVM_REQ_MMU_FREE_OBSOLETE_ROOTS is tracked in the request set
+	 * shared by all of the vCPU's planes, so the plane that consumes
+	 * the request is not necessarily the plane whose root is obsolete.
+	 * Service every plane: the inactive twins cannot be running (all
+	 * planes of a vCPU share one thread), so their MMUs are stable.
+	 */
+	vcpu_for_each_plane(vcpu->common, i, v) {
+		__kvm_mmu_free_obsolete_roots(vcpu->kvm, &v->arch.root_mmu);
+		__kvm_mmu_free_obsolete_roots(vcpu->kvm, &v->arch.guest_mmu);
+	}
 }
 EXPORT_SYMBOL_FOR_KVM_INTERNAL(kvm_mmu_free_obsolete_roots);
 
Signature · 1 line
2.50.1
maintainer
#137 lore raw

PATCH 8/16 KVM: planes: Publish a new plane only once KVM_CREATE_PLANE cannot fail

In reply to David Woodhouse

From: David Woodhouse <[email protected]>

kvm_create_plane() publishes the new plane in kvm->planes[] before kvm_vm_ioctl_create_plane() can still fail at anon_inode_getfile(), and the error path freed the plane without unpublishing it. Anything that later reads kvm->planes[] reaches the freed plane, and VM teardown destroys it a second time.

Unpublishing it on the error path is not enough either. Some readers of kvm->planes[] take no lock: the SEV VMPL-switch target lookup takes the level from the guest, so it can pick up a plane that is published and then freed with no grace period in between.

Publish the plane in kvm_vm_ioctl_create_plane() only after anon_inode_getfile() succeeds, so a failing create frees a plane that no reader has ever seen. kvm_create_vm() publishes plane 0 itself, before the VM is reachable. The store stays smp_store_release(), and the lock-free readers (MSI delivery and the SEV VMPL target lookup) now load the pointer with READ_ONCE() to pair with it.

Found by a local Sashiko review.

Fixes the error path in "kvm: Implement KVM_CREATE_PLANE ioctl".

---
 arch/x86/kvm/irq.c     |  5 +++--
 arch/x86/kvm/svm/sev.c |  2 +-
 virt/kvm/kvm_main.c    | 16 ++++++++++++----
 3 files changed, 16 insertions(+), 7 deletions(-)
arch/x86/kvm/irq.c +3 -2
index f3044aec7188..26912161997e 100644
@@ -235,7 +235,7 @@ int kvm_set_msi(struct kvm_kernel_irq_routing_entry *e,
 		return -1;
 
 	kvm_msi_to_lapic_irq(kvm, e, &irq);
-	plane = kvm->planes[e->msi.plane_level];
+	plane = READ_ONCE(kvm->planes[e->msi.plane_level]);
 
 	return kvm_irq_delivery_to_apic(plane, NULL, &irq);
 }
@@ -260,7 +260,8 @@ int kvm_arch_set_irq_inatomic(struct kvm_kernel_irq_routing_entry *e,
 
 		kvm_msi_to_lapic_irq(kvm, e, &irq);
 
-		if (kvm_irq_delivery_to_apic_fast(kvm->planes[e->msi.plane_level], NULL, &irq, &r))
+		if (kvm_irq_delivery_to_apic_fast(READ_ONCE(kvm->planes[e->msi.plane_level]),
+						  NULL, &irq, &r))
 			return r;
 		break;
 
arch/x86/kvm/svm/sev.c +1 -1
index fd7605e47216..32a4ba4ffdbb 100644
@@ -4249,7 +4249,7 @@ static int sev_snp_ap_creation(struct kvm_vcpu *vcpu)
 	vmpl = array_index_nospec(vmpl, SVM_SEV_VMPL_MAX);
 
 	/* Obtain the target plane and vCPU */
-	target_plane = vcpu->kvm->planes[vmpl];
+	target_plane = READ_ONCE(vcpu->kvm->planes[vmpl]);
 	if (target_plane) {
 		target_vcpu = plane_get_vcpu(target_plane, apic_id);
 	} else {
virt/kvm/kvm_main.c +12 -4
index 95de904f0c61..d256bbf301f4 100644
@@ -1252,8 +1252,7 @@ static struct kvm_plane *kvm_create_plane(struct kvm *kvm, unsigned plane_level)
 	if (kvm_arch_plane_init(kvm, plane, plane_level))
 		goto out_free_plane;
 
-	kvm->planes[plane_level] = plane;
-
+	/* The caller publishes the plane in kvm->planes[]. */
 	return plane;
 
 out_free_plane:
@@ -1312,7 +1311,8 @@ static struct kvm *kvm_create_vm(unsigned long type, const char *fdname)
 	BUILD_BUG_ON(KVM_MEM_SLOTS_NUM > SHRT_MAX);
 
 	/* Initialize planes array and allocate plane 0 */
-	if (kvm_create_plane(kvm, 0) == NULL) {
+	kvm->planes[0] = kvm_create_plane(kvm, 0);
+	if (!kvm->planes[0]) {
 		r = -ENOMEM;
 		goto out_no_planes;
 	}
@@ -5627,13 +5627,21 @@ static int kvm_vm_ioctl_create_plane(struct kvm *kvm, unsigned id)
 		goto put_kvm;
 	}
 
+	/*
+	 * Publish only once nothing can fail, so that the error path frees
+	 * a plane no reader has seen.  Pairs with the READ_ONCE() of the
+	 * lock-free readers of kvm->planes[] (MSI delivery, SEV VMPL
+	 * targeting): the plane's fields must be visible before the
+	 * pointer is.
+	 */
+	smp_store_release(&kvm->planes[id], plane);
 	kvm->has_planes = true;
 	fd_install(fd, file);
 	return fd;
 
 put_kvm:
-	kvm_put_kvm(kvm);
 	kvm_destroy_one_plane(plane);
+	kvm_put_kvm(kvm);
 put_fd:
 	put_unused_fd(fd);
 	return r;
Signature · 1 line
2.50.1
maintainer
#138 lore raw

PATCH 9/16 KVM: planes: Kick the running twin's pCPU, not the request target's

In reply to David Woodhouse

From: David Woodhouse <[email protected]>

The remote kick paths (kvm_make_vcpus_request_mask(), kvm_make_all_cpus_request(), __kvm_vcpu_kick()) read READ_ONCE(vcpu->cpu) of the plane-0 vCPU. But vcpu->cpu is per plane vCPU (kvm_arch_vcpu_load() refreshes only the loaded twin's copy), while vcpu->mode is shared in kvm_vcpu_common. When the vCPU thread has migrated pCPUs while a non-zero plane was loaded, the shared mode reads IN_GUEST_MODE but the IPI goes to plane 0's stale pCPU: a waiting request such as a remote TLB flush completes while the twin keeps running on stale translations.

Read the pCPU from common->current_vcpu in both kick paths. It names the loaded twin, so its cpu field tracks where the vCPU thread runs (on x86, kvm_arch_vcpu_load() refreshes it).

Found by a local Sashiko review.

---
 virt/kvm/kvm_main.c | 17 +++++++++++++++--
 1 file changed, 15 insertions(+), 2 deletions(-)
virt/kvm/kvm_main.c +15 -2
index d256bbf301f4..24d2291b60a2 100644
@@ -244,7 +244,14 @@ static void kvm_make_vcpu_request(struct kvm_vcpu *vcpu, unsigned int req,
 	 * finalized. See kvm_vcpu_kick() for more details on handling requests.
 	 */
 	if (kvm_request_needs_ipi(vcpu, req)) {
-		cpu = READ_ONCE(vcpu->cpu);
+		/*
+		 * vcpu->cpu is per plane vCPU and kvm_arch_vcpu_load()
+		 * refreshes only the loaded twin's, so this plane's copy
+		 * can point at a pCPU the thread has long left while a
+		 * twin runs elsewhere.  The mode just checked is shared;
+		 * the IPI must go where the running twin actually is.
+		 */
+		cpu = READ_ONCE(READ_ONCE(vcpu->common->current_vcpu)->cpu);
 		if (cpu != -1 && cpu != current_cpu)
 			__cpumask_set_cpu(cpu, tmp);
 	}
@@ -4129,7 +4136,13 @@ void __kvm_vcpu_kick(struct kvm_vcpu *vcpu, bool wait)
 	 * vCPU also requires it to leave IN_GUEST_MODE.
 	 */
 	if (kvm_arch_vcpu_should_kick(vcpu)) {
-		cpu = READ_ONCE(vcpu->cpu);
+		/*
+		 * Target the running twin's pCPU: this plane's vcpu->cpu
+		 * goes stale whenever the thread migrates while another
+		 * plane is loaded, and the IN_GUEST_MODE just observed is
+		 * the (shared) mode of whichever twin runs.
+		 */
+		cpu = READ_ONCE(READ_ONCE(vcpu->common->current_vcpu)->cpu);
 		if (cpu != me && (unsigned int)cpu < nr_cpu_ids && cpu_online(cpu)) {
 			/*
 			 * Use a reschedule IPI to kick the vCPU if the caller
Signature · 1 line
2.50.1
maintainer
#139 lore raw

PATCH 10/16 KVM: planes: Keep current_vcpu in sync in vcpu_load()

In reply to David Woodhouse

From: David Woodhouse <[email protected]>

The preempt notifiers save and restore common->current_vcpu (kvm_sched_out()/kvm_sched_in()), but vcpu_load() does not update it.

An ioctl on a plane-N fd loads that plane's vCPU while current_vcpu may still name the plane that ran last. A reschedule inside the ioctl then arch-puts the stale twin, whose state is not resident, and arch-loads it back over the state the ioctl made resident: on VMX the ioctl's VMCS writes land in the other plane's VMCS, and the trailing vcpu_put() unloads a non-resident vCPU, corrupting the per-pCPU loaded_vmcs tracking. The kick paths read current_vcpu->cpu too, and the next patch, which swaps each twin in around its update after KVM_SET_CPUID2, relies on the loaded vCPU being the one the preempt notifiers see.

Set common->current_vcpu in vcpu_load(), so every load path (the run loop and vCPU ioctls on any plane's fd) agrees with what the preempt notifiers will operate on. kvm_vcpu_select_plane() keeps its own assignment; both stores use WRITE_ONCE() to pair with the lock-free readers in the kick paths.

That makes twin creation a writer as well: kvm_arch_vcpu_create() calls vcpu_load() on the new twin while its plane-0 twin may be in KVM_RUN on another pCPU, and the shared preempt notifier would be registered twice. Hold the shared vCPU mutex across creation of a non-zero plane's vCPU, from before kvm_arch_vcpu_create() to the end, including the error-path kvm_arch_vcpu_destroy(), which can vcpu_load() too (nested_vmx_free_vcpu()). Since vcpu_put() leaves current_vcpu naming the new twin, put back the twin that owned it once the arch code returns, on success and on every failure path; otherwise a failed creation (create_vcpu_fd() can be made to fail with RLIMIT_NOFILE) frees a vCPU that the lock-free kick paths still dereference.

Found by a local Sashiko review.

Fixes the current_vcpu tracking in "kvm: Move VCPU scheduling state to struct kvm_vcpu_common".

---
 virt/kvm/kvm_main.c | 54 ++++++++++++++++++++++++++++++++++++++++-----
 1 file changed, 48 insertions(+), 6 deletions(-)
virt/kvm/kvm_main.c +48 -6
index 24d2291b60a2..b6bdbe5c5133 100644
@@ -174,6 +174,16 @@ void vcpu_load(struct kvm_vcpu *vcpu)
 {
 	int cpu = get_cpu();
 
+	/*
+	 * The preempt notifiers operate on common->current_vcpu, so it
+	 * must name the plane vCPU being loaded here: an ioctl taken on
+	 * another plane's fd than the one that ran last would otherwise
+	 * have a preemption arch-put the stale twin (whose state is not
+	 * resident) and arch-load it back over the state this load makes
+	 * resident.  The kick paths also read it to find the running
+	 * twin's pCPU.
+	 */
+	WRITE_ONCE(vcpu->common->current_vcpu, vcpu);
 	__this_cpu_write(kvm_running_vcpu, vcpu->common);
 	preempt_notifier_register(&vcpu->common->preempt_notifier);
 	kvm_arch_vcpu_load(vcpu, cpu);
@@ -4470,7 +4480,7 @@ static void kvm_create_vcpu_debugfs(struct kvm_vcpu *vcpu)
 static int kvm_plane_ioctl_create_vcpu(struct kvm_plane *plane, unsigned long id)
 {
 	struct kvm *kvm = plane->kvm;
-	struct kvm_vcpu *vcpu;
+	struct kvm_vcpu *vcpu, *prev = NULL;
 	int r;
 
 	guard(mutex)(&kvm->lock);
@@ -4513,9 +4523,34 @@ static int kvm_plane_ioctl_create_vcpu(struct kvm_plane *plane, unsigned long id
 
 	kvm_vcpu_init(vcpu, kvm, id);
 
+	/*
+	 * A non-zero plane shares 'common' with a plane-0 twin which may
+	 * already be running.  kvm_arch_vcpu_create() calls vcpu_load(),
+	 * which publishes the half-created vCPU through
+	 * common->current_vcpu and registers the shared preempt notifier,
+	 * so it must not run concurrently with a twin's KVM_RUN: hold the
+	 * shared vCPU mutex from here to the end of creation, including
+	 * the error-path kvm_arch_vcpu_destroy(), which can vcpu_load()
+	 * too (nested VMX teardown).  It nests inside the kvm->lock held
+	 * for all of vCPU creation, the usual kvm->lock -> vcpu mutex order.
+	 *
+	 * vcpu_put() leaves current_vcpu naming the new twin; put back
+	 * the twin that owned it, so that the lock-free kick paths never
+	 * see a vCPU that is not reachable yet, or that a failed creation
+	 * frees.
+	 */
+	if (plane->level > 0) {
+		r = mutex_lock_killable(kvm_vcpu_mutex(vcpu));
+		if (r)
+			goto vcpu_free_common;
+		prev = vcpu->common->current_vcpu;
+	}
+
 	r = kvm_arch_vcpu_create(vcpu);
+	if (plane->level > 0)
+		WRITE_ONCE(vcpu->common->current_vcpu, prev);
 	if (r)
-		goto vcpu_free_common;
+		goto unlock_vcpu_free_common;
 
 	/*
 	 * Set the vCPU's index *before* the vCPU is reachable by other tasks.
@@ -4534,9 +4569,10 @@ static int kvm_plane_ioctl_create_vcpu(struct kvm_plane *plane, unsigned long id
 	 * visible (per online_vcpus), e.g. so that KVM doesn't get tricked
 	 * into a NULL-pointer dereference because KVM thinks the _current_
 	 * vCPU doesn't exist.  As a bonus, taking vcpu->mutex ensures lockdep
-	 * knows it's taken *inside* kvm->lock.
+	 * knows it's taken *inside* kvm->lock.  A twin holds it already.
 	 */
-	kvm_vcpu_lock(vcpu);
+	if (!plane->level)
+		kvm_vcpu_lock(vcpu);
 	kvm_get_kvm(kvm);
 	r = create_vcpu_fd(vcpu);
 	if (r < 0)
@@ -4550,12 +4586,18 @@ static int kvm_plane_ioctl_create_vcpu(struct kvm_plane *plane, unsigned long id
 	return r;
 
 kvm_put_xa_erase:
-	kvm_vcpu_unlock(vcpu);
+	if (!plane->level)
+		kvm_vcpu_unlock(vcpu);
 	kvm_put_kvm_no_destroy(kvm);
 	xa_erase(&plane->vcpu_array, vcpu->vcpu_idx);
 unlock_vcpu_destroy:
 	vcpu->vcpu_idx = -1;
 	kvm_arch_vcpu_destroy(vcpu);
+	if (plane->level > 0)
+		WRITE_ONCE(vcpu->common->current_vcpu, prev);
+unlock_vcpu_free_common:
+	if (plane->level > 0)
+		kvm_vcpu_unlock(vcpu);
 vcpu_free_common:
 	kvm_vcpu_common_destroy(vcpu);
 vcpu_free:
@@ -5136,7 +5178,7 @@ struct kvm_vcpu *kvm_vcpu_select_plane(struct kvm_vcpu *vcpu)
 		ret->plane_state = RUNNABLE;
 	}
 
-	common->current_vcpu = ret;
+	WRITE_ONCE(common->current_vcpu, ret);
 
 	common->plane_switch = false;
 
Signature · 1 line
2.50.1
maintainer
#140 lore raw

PATCH 11/16 KVM: x86: Load each plane twin while updating it after KVM_SET_CPUID

In reply to David Woodhouse

From: David Woodhouse <[email protected]>

kvm_vcpu_ioctl_set_cpuid() runs kvm_vcpu_after_set_cpuid() on every plane twin, but only the calling vCPU is loaded. The vendor callback writes VMCS/VMCB controls, and on VMX those writes go to whatever VMCS is loaded on the current pCPU: the caller's, not the twin's. The twin's controls shadow is updated anyway, so the stale values in its real VMCS can never be corrected later; a twin created before KVM_SET_CPUID2 keeps its empty-CPUID controls (RDSEED/RDRAND exiting among them), and the guest's higher VTL takes a spurious #UD on its first RDSEED.

Swap each twin in as the loaded vCPU for its update, and reload the caller once at the end. Plane ioctls all share the common vCPU mutex, so the twin cannot be loaded elsewhere concurrently. A preemption inside the window is safe only because vcpu_load() now publishes the loaded twin as common->current_vcpu (previous patch); before that, the preempt notifiers would arch-put and arch-load the caller instead.

Fixes the twin updates in "kvm: x86: Update state for all plane VCPUs after CPUID update".

---
 arch/x86/kvm/cpuid.c | 17 ++++++++++++++++-
 1 file changed, 16 insertions(+), 1 deletion(-)
arch/x86/kvm/cpuid.c +16 -1
index 54bcfedc5226..c9b7dfc5c967 100644
@@ -501,7 +501,7 @@ static int kvm_set_cpuid(struct kvm_vcpu *vcpu, struct kvm_cpuid_entry2 *e2,
 {
 	struct kvm_vcpu_common *common = vcpu->common;
 	u32 vcpu_caps[NR_KVM_CPU_CAPS];
-	struct kvm_vcpu *v;
+	struct kvm_vcpu *v, *loaded = vcpu;
 	unsigned i;
 	int r;
 
@@ -568,9 +568,24 @@ static int kvm_set_cpuid(struct kvm_vcpu *vcpu, struct kvm_cpuid_entry2 *e2,
 #ifdef CONFIG_KVM_XEN
 		v->arch.xen.cpuid = kvm_get_hypervisor_cpuid(vcpu, XEN_SIGNATURE);
 #endif
+		/*
+		 * The ioctl loaded the calling vCPU; updating a twin's
+		 * VMCS/VMCB controls requires that twin to be the loaded
+		 * one, so swap it in for the update.
+		 */
+		if (v != loaded) {
+			vcpu_put(loaded);
+			vcpu_load(v);
+			loaded = v;
+		}
 		kvm_vcpu_after_set_cpuid(v);
 	}
 
+	if (loaded != vcpu) {
+		vcpu_put(loaded);
+		vcpu_load(vcpu);
+	}
+
 success:
 	kvfree(e2);
 	return 0;
Signature · 1 line
2.50.1
maintainer
#141 lore raw

PATCH 12/16 KVM: VMX: Leave the IPIv PID-table entry to the plane 0 vCPU

In reply to David Woodhouse

From: David Woodhouse <[email protected]>

The IPI virtualization PID table is indexed by vCPU ID, and a plane vCPU has the same vCPU ID as its plane 0 twin. vmx_vcpu_create() for a twin therefore repoints plane 0's entry at the twin's posted-interrupt descriptor, and vmx_vcpu_free() for a twin (including the error path of a failed KVM_CREATE_VCPU on a plane fd) clears it. Plane 0 is left with no valid entry, or one naming a freed descriptor.

Nothing consumes the table while that is the case today: a VM with planes has APICv inhibited, so IPIv stays off. But the entry must not depend on that. Fill and clear it for the plane 0 vCPU only.

Fixes the IPIv PID-table handling for the plane vCPUs that "KVM:
Implement KVM_CREATE_VCPU ioctl for planes" creates.

---
 arch/x86/kvm/vmx/vmx.c | 10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)
arch/x86/kvm/vmx/vmx.c +8 -2
index 44d0e8eba45b..591c7745e3e9 100644
@@ -7525,7 +7525,8 @@ void vmx_vcpu_free(struct kvm_vcpu *vcpu)
 	free_loaded_vmcs(vmx->loaded_vmcs);
 	free_page((unsigned long)vmx->ve_info);
 
-	if (vmx_can_use_ipiv(vcpu))
+	/* See vmx_vcpu_create(): only the plane 0 vCPU owns the entry. */
+	if (vmx_can_use_ipiv(vcpu) && !vcpu->plane_level)
 		WRITE_ONCE(to_kvm_vmx(vcpu->kvm)->pid_table[vcpu->vcpu_id], 0);
 }
 
@@ -7614,7 +7615,12 @@ int vmx_vcpu_create(struct kvm_vcpu *vcpu)
 		vmx->ve_info = page_to_virt(page);
 	}
 
-	if (vmx_can_use_ipiv(vcpu))
+	/*
+	 * The PID table is indexed by vCPU ID, which a plane vCPU shares
+	 * with its plane 0 twin: only plane 0 fills the entry, or creating
+	 * (and freeing) a twin would repoint (and clear) plane 0's.
+	 */
+	if (vmx_can_use_ipiv(vcpu) && !vcpu->plane_level)
 		WRITE_ONCE(to_kvm_vmx(vcpu->kvm)->pid_table[vcpu->vcpu_id],
 			   __pa(&vmx->vt.pi_desc) | PID_TABLE_ENTRY_VALID);
 
Signature · 1 line
2.50.1
maintainer
#142 lore raw

PATCH 13/16 KVM: x86: Leave TSC matching to the plane 0 vCPUs

In reply to David Woodhouse

From: Mushahid Hussain <[email protected]>

KVM enables the master clock when every vCPU has a matching TSC, judged by nr_vcpus_matched_tsc + 1 == online_vcpus. online_vcpus counts a vCPU once, but each of its plane vCPUs synchronizes its TSC at creation and counts as another matching vCPU. With one higher plane the count can never equal the number of vCPUs again, the master clock stays off and kvmclock loses PVCLOCK_TSC_STABLE_BIT. Among other things the Hyper-V reference TSC page then breaks: KVM writes sequence 0 to it, finds the clock unstable and leaves it that way.

The planes of a vCPU share its TSC. Give a plane vCPU its plane-0 sibling's TSC offset and generation, and leave the matching count and generation changes to the sibling. The offset the caller computed is
ignored: it comes from the VM-global matching window, so it differs from the sibling's whenever the sibling is not in the current generation, or on a host with an unstable TSC, and a host TSC write on a plane fd would otherwise move the twin's TSC away from plane 0's.

Fixes the TSC match count for the plane vCPUs that "KVM: Implement KVM_CREATE_VCPU ioctl for planes" creates.

---
 arch/x86/kvm/x86.c | 29 +++++++++++++++++++++++++++++
 1 file changed, 29 insertions(+)
arch/x86/kvm/x86.c +29 -0
index cd0e9b6cf8f7..f9adb4e81ebc 100644
@@ -1366,6 +1366,35 @@ static void __kvm_synchronize_tsc(struct kvm_vcpu *vcpu, u64 offset, u64 tsc,
 	if (vcpu->arch.guest_tsc_protected)
 		return;
 
+	/*
+	 * The planes of a vCPU share its TSC and online_vcpus counts the
+	 * vCPU once, so a plane vCPU neither counts as a matching vCPU nor
+	 * starts a generation; its plane 0 sibling speaks for it.  Nor may
+	 * it perturb the global matching window (last_tsc_ and
+	 * user_set_tsc): KVM_SET_MSRS is accepted on plane fds, and a TSC
+	 * write on a plane fd moving the window would corrupt the next
+	 * plane-0 write's match decision.
+	 *
+	 * Take the offset and the generation from the plane-0 sibling,
+	 * which exists by construction for a plane twin, and ignore the
+	 * caller's offset: that one comes from the VM-global matching
+	 * window, which may belong to a different vCPU's generation, and
+	 * is recomputed from elapsed time on an unstable host.  A host
+	 * TSC write on a plane fd therefore leaves the TSC alone; plane 0
+	 * owns it.
+	 */
+	if (vcpu->plane_level) {
+		struct kvm_vcpu *sibling = vcpu->common->vcpus[0];
+
+		kvm_vcpu_write_tsc_offset(vcpu, sibling->arch.l1_tsc_offset);
+		vcpu->arch.last_guest_tsc = kvm_read_l1_tsc(vcpu, rdtsc());
+
+		vcpu->arch.this_tsc_generation = sibling->arch.this_tsc_generation;
+		vcpu->arch.this_tsc_nsec = sibling->arch.this_tsc_nsec;
+		vcpu->arch.this_tsc_write = sibling->arch.this_tsc_write;
+		return;
+	}
+
 	if (user_set_tsc)
 		vcpu->kvm->arch.user_set_tsc = true;
 
Signature · 1 line
2.50.1
maintainer
#143 lore raw

PATCH 14/16 KVM: x86: Derive runtime CPUID bits from the executing plane

In reply to David Woodhouse

From: Mushahid Hussain <[email protected]>

Some CPUID bits follow registers of the vCPU: CPUID.1:ECX.OSXSAVE follows CR4.OSXSAVE, CPUID.7:ECX.OSPKE follows CR4.PKE, CPUID.1:EDX.APIC follows the APIC enable, CPUID.1:ECX.MWAIT follows MISC_ENABLE, and the XSAVE area sizes in leaf 0xD follow XCR0 and XSS. KVM keeps the CPUID table of a vCPU in the state its planes share, and refreshes those bits into the table from the state of whichever vCPU runs when the table is dirty. The planes of a vCPU have their own control registers, so after one plane sets CR4.OSXSAVE, CPUID on every plane of that vCPU reports OSXSAVE until a plane with it clear refreshes the table, and the other way round. The architecture requires CPUID to describe the processor that executes it, and for a plane that is the plane's own registers.

A Windows guest with Virtual Secure Mode shows the leak. Its secure kernel runs on a plane whose CR4.OSXSAVE is clear while the normal kernel on the other plane has set it. The secure kernel executes the usual probe, CPUID.1 followed by a test of OSXSAVE, reads the bit set and executes XGETBV, which raises #UD because its own CR4 does not permit it.

Compute the bits that follow vCPU registers when CPUID is emulated, from the registers of the vCPU that executes it, and no longer refresh the shared table on that path. The table keeps the view of plane 0 for KVM_GET_CPUID2 and KVM_SET_CPUID2, which act on the plane 0 vCPU, and their refresh stays as it is.

Fixes the shared runtime CPUID bits in "kvm: x86: Move CPUID state to struct kvm_vcpu_arch_common".

[dwmw2: dropped the plane selftest hunk; this branch has no
plane_x86_test.c. The per-VTL OSXSAVE contract is pinned by the hyperv_vsm tests on the vsm-v2 branch of
git://git.infradead.org/users/dwmw2/kvm-unit-tests.git instead.]

---
 Documentation/virt/kvm/api.rst |   7 +++
 arch/x86/kvm/cpuid.c           | 107 +++++++++++++++++++++++----------
 arch/x86/kvm/cpuid.h           |   5 +-
 3 files changed, 86 insertions(+), 33 deletions(-)
Documentation/virt/kvm/api.rst +7 -0
index d21378183c51..c1a6536354fd 100644
@@ -370,6 +370,13 @@ cpu's hardware control block.
 VCPUs for non-zero planes can only be created if the VCPU the same index has
 already been created for plane zero.
 
+The planes of a vCPU share its CPUID model, set through the plane 0 fd with
+KVM_SET_CPUID2.  The CPUID bits that follow a vCPU's own registers (OSXSAVE
+from CR4.OSXSAVE, OSPKE from CR4.PKE, the APIC enable, MWAIT from
+MISC_ENABLE, and the XSAVE area sizes from XCR0 and XSS) reflect the plane
+that executes CPUID, since every plane has these registers of its own.
+KVM_GET_CPUID2 returns them as plane 0 sees them.
+
 4.8 KVM_GET_DIRTY_LOG
 ---------------------
 
arch/x86/kvm/cpuid.c +76 -31

155 lines not on this page · raw message

arch/x86/kvm/cpuid.h +3 -2
index 02dbd02c8f80..0128cb38b0f2 100644
@@ -276,8 +276,9 @@ static __always_inline bool guest_cpu_cap_has(struct kvm_vcpu *vcpu,
 	struct kvm_vcpu_common *common = vcpu->common;
 
 	/*
-	 * Except for MWAIT, querying dynamic feature bits is disallowed, so
-	 * that KVM can defer runtime updates until the next CPUID emulation.
+	 * Except for MWAIT, querying dynamic feature bits is disallowed: the
+	 * guest gets them from the state of the plane that executes CPUID, and
+	 * the table copy is refreshed only for the CPUID ioctls.
 	 */
 	BUILD_BUG_ON(x86_feature == X86_FEATURE_APIC ||
 		     x86_feature == X86_FEATURE_OSXSAVE ||
Signature · 1 line
2.50.1
maintainer
#144 lore raw

PATCH 15/16 KVM: planes: Annotate the lockless twin-publication protocol

In reply to David Woodhouse

From: David Woodhouse <[email protected]>

Plane twins are published with a plain store to common->vcpus[] plus an smp_wmb() that pairs with online_vcpus, not with twin initialisation, while vcpu_for_each_plane() reads the slots with a bare load. x86-TSO hides it, but the protocol is unannotated generic code: KCSAN reports it, and nothing stops a reordered publication from exposing a half-initialised twin to a lockless iterator.

Publish the slot with smp_store_release(), read it with READ_ONCE() in vcpu_for_each_plane(), and document the pairing at the iterator. The lockless readers here are vcpu_for_each_plane() callers that walk other vCPUs' twins, such as the APICv BLOCKIRQ recompute; the kick paths read common->current_vcpu instead, and the Hyper-V twin lookups that also pair with the release come later, with the VSM code.

No functional change on x86.

---
 include/linux/kvm_host.h | 7 ++++++-
 virt/kvm/kvm_main.c      | 9 ++++++++-
 2 files changed, 14 insertions(+), 2 deletions(-)
include/linux/kvm_host.h +6 -1
index 1f47b3bfa4bb..6922e3063ad8 100644
@@ -393,9 +393,14 @@ struct kvm_vcpu_common {
 	struct kvm_vcpu_arch_common arch;
 };
 
+/*
+ * The slot read pairs with the smp_store_release() publication in
+ * kvm_vcpu_finish_common() for iterators running outside the locks that
+ * serialise twin creation.
+ */
 #define vcpu_for_each_plane(common, i, v)			\
 	for ((i) = 0; (i) < KVM_MAX_PLANES; ++(i))		\
-		if (((v) = common->vcpus[(i)]) != NULL)
+		if (((v) = READ_ONCE(common->vcpus[(i)])) != NULL)
 
 /* Tracked per plane-VCPU - used for deciding which plane-vcpu to run */
 enum kvm_vcpu_state {
virt/kvm/kvm_main.c +8 -1
index b6bdbe5c5133..7f215822e48e 100644
@@ -548,7 +548,14 @@ static int kvm_vcpu_init_common(struct kvm_vcpu *vcpu, struct kvm *kvm, unsigned
 static void kvm_vcpu_finish_common(struct kvm_vcpu *vcpu)
 {
 	WARN_ON(vcpu->common->vcpus[vcpu->plane_level] != NULL);
-	vcpu->common->vcpus[vcpu->plane_level] = vcpu;
+	/*
+	 * Publish with release semantics so that vcpu_for_each_plane()
+	 * callers running outside the locks that serialise twin creation
+	 * (such as the APICv BLOCKIRQ recompute, which walks every vCPU's
+	 * twins) observe a fully initialised vCPU through their READ_ONCE()
+	 * of the slot; the plain store relied on x86-TSO.
+	 */
+	smp_store_release(&vcpu->common->vcpus[vcpu->plane_level], vcpu);
 	smp_wmb();
 	if (vcpu->plane_level == 0) {
 		/*
Signature · 1 line
2.50.1
maintainer
#145 lore raw

PATCH 16/16 KVM: SVM: Make KVM_REQ_VMSA_PAGE_RELOAD per plane

In reply to David Woodhouse

From: David Woodhouse <[email protected]>

An SNP guest with multiple VMPLs gives each plane's vCPU its own VMSA, but KVM_REQ_VMSA_PAGE_RELOAD still treats the vCPU as having one. When guest_memfd invalidates the page behind a higher VMPL's VMSA, sev_gmem_invalidate_range() looks only at the plane-0 vCPUs and never asks the twin to reload. And when a twin's own reload races with an invalidation and re-raises the request, it lands in the shared common requests and is consumed by whichever twin runs first. That reloads *its* VMSA and leaves the target twin entering the guest with a stale vmsa_pa.

Mark the request KVM_REQUEST_PER_PLANE, as KVM_REQ_UPDATE_PROTECTED_GUEST_STATE already is, so it stays with the twin it was raised on, and have the invalidation walk every twin of each vCPU and target the one whose VMSA GPA falls in the range. The kick still reaches whichever twin is running, because mode is shared in the common.

UNTESTED on SNP: build-tested only, as I have no multi-VMPL SNP host.

Fixes the VMSA reload for higher VMPLs in "kvm: sev: Allow for VMPL level specification in AP create".

---
 arch/x86/include/asm/kvm_host.h |  3 ++-
 arch/x86/kvm/svm/sev.c          | 21 +++++++++++++++------
 2 files changed, 17 insertions(+), 7 deletions(-)
arch/x86/include/asm/kvm_host.h +2 -1
index c7471699e2af..a4912735ad3d 100644
@@ -129,7 +129,8 @@
 #define KVM_REQ_HV_TLB_FLUSH \
 	KVM_ARCH_REQ_FLAGS(32, KVM_REQUEST_WAIT | KVM_REQUEST_NO_WAKEUP)
 #define KVM_REQ_VMSA_PAGE_RELOAD \
-	KVM_ARCH_REQ_FLAGS(33, KVM_REQUEST_WAIT | KVM_REQUEST_NO_WAKEUP)
+	KVM_ARCH_REQ_FLAGS(33, KVM_REQUEST_WAIT | KVM_REQUEST_NO_WAKEUP | \
+			       KVM_REQUEST_PER_PLANE)
 #define KVM_REQ_UPDATE_PROTECTED_GUEST_STATE \
 	KVM_ARCH_REQ_FLAGS(34, KVM_REQUEST_WAIT | KVM_REQUEST_PER_PLANE)
 
arch/x86/kvm/svm/sev.c +15 -6
index 32a4ba4ffdbb..c8a9f808dcc4 100644
@@ -5682,7 +5682,13 @@ void sev_gmem_invalidate_range(struct kvm *kvm, struct kvm_gfn_range *range)
 		return;
 
 	kvm_for_each_vcpu(i, vcpu, kvm) {
+		struct kvm_vcpu *twin;
+		int p;
+
 		/*
+		 * Every plane's vCPU has its own VMSA, so check each twin and
+		 * target the request at the twin whose VMSA it is.
+		 *
 		 * Read snp_guest_vmsa_gpa without taking the vCPU's VMSA mutex
 		 * (or its generic mutex) as mmu_lock is held, i.e. this task
 		 * can't sleep.  The VMSA is invalidated outside of mmu_lock,
@@ -5691,12 +5697,15 @@ void sev_gmem_invalidate_range(struct kvm *kvm, struct kvm_gfn_range *range)
 		 * positive is benign, as a spurious request simply forces the
 		 * vCPU to re-establish its VMSA.
 		 */
-		gpa_t gpa = READ_ONCE(to_svm(vcpu)->sev_es.snp_guest_vmsa_gpa);
-
-		if (VALID_PAGE(gpa) &&
-		    gpa_to_gfn(gpa) >= range->start &&
-		    gpa_to_gfn(gpa) < range->end)
-			kvm_make_request_and_kick(KVM_REQ_VMSA_PAGE_RELOAD, vcpu);
+		vcpu_for_each_plane(vcpu->common, p, twin) {
+			gpa_t gpa = READ_ONCE(to_svm(twin)->sev_es.snp_guest_vmsa_gpa);
+
+			if (VALID_PAGE(gpa) &&
+			    gpa_to_gfn(gpa) >= range->start &&
+			    gpa_to_gfn(gpa) < range->end)
+				kvm_make_request_and_kick(KVM_REQ_VMSA_PAGE_RELOAD,
+							  twin);
+		}
 	}
 }
 
Signature · 1 line
2.50.1
maintainer
#146 lore raw

[PATCH 18/16] KVM: SVM: Bound the SNP plane count by the architectural VMPL limit

In reply to David Woodhouse

From: David Woodhouse <[email protected]>

sev_snp_max_planes() reports the VMPL count straight from CPUID 0x8000001f EBX[15:12], a 4-bit field the host provides. A value above four lets userspace create SNP planes that no VMPL switch can ever reach, because the switch already refuses a VMPL at or above SVM_SEV_VMPL_MAX.

Clamp the count to between one and SVM_SEV_VMPL_MAX, so that a VM's plane count never exceeds what the architecture defines.

Fixes the plane count in "kvm: svm: Implement max_planes x86 operation".

---
 arch/x86/kvm/svm/sev.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)
arch/x86/kvm/svm/sev.c +5 -3
index c8a9f808dcc4..eefa8641100b 100644
@@ -6010,8 +6010,10 @@ bool sev_snp_blocked(enum inject_type type, struct kvm_vcpu *vcpu)
 int sev_snp_max_planes(struct kvm *kvm)
 {
 	/*
-	 * CPUID 0x8000001f EBX[15:12] is host-provided data; a zero there
-	 * must not report "no planes at all" (plane 0 always exists).
+	 * CPUID 0x8000001f EBX[15:12] is host-provided data.  A zero there
+	 * must not report "no planes at all" (plane 0 always exists), and
+	 * the architecture defines no more than SVM_SEV_VMPL_MAX VMPLs, the
+	 * bound the VMPL switch already enforces.
 	 */
-	return max(vmpl_levels, 1U);
+	return clamp(vmpl_levels, 1U, SVM_SEV_VMPL_MAX);
 }
Signature · 1 line
2.50.1
maintainer
#147 lore raw

[PATCH 17/16] Documentation: kvm: Renumber KVM_CREATE_PLANE and restore the kvm_run label

In reply to David Woodhouse

From: David Woodhouse <[email protected]>

The KVM_CREATE_PLANE section was inserted between the .. _kvm_run:
label and "5. The kvm_run structure", so the :ref:kvm_run in the KVM_CAP_SYNC_REGS text now links to KVM_CREATE_PLANE. It also took the number 4.145, which KVM_PPC_GET_COMPAT_CAPS already has, leaving 4.147 unused.

Move the label back above section 5 and number the section 4.147.

Found by a local Sashiko review.

Fixes the section placement in "Documentation: kvm: introduce "VM plane" concept".

---
 Documentation/virt/kvm/api.rst | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
Documentation/virt/kvm/api.rst +3 -3
index c1a6536354fd..9aa09d8a39dc 100644
@@ -6821,11 +6821,9 @@ if the guest_memfd memory was pinned in IOMMU page tables.
 
 See also: :ref:`KVM_SET_MEMORY_ATTRIBUTES`.
 
-.. _kvm_run:
-
 .. _KVM_CREATE_PLANE:
 
-4.145 KVM_CREATE_PLANE
+4.147 KVM_CREATE_PLANE
 ----------------------
 
 :Capability: KVM_CAP_PLANES
@@ -6843,6 +6841,8 @@ in the future may use the plane id when planes are supported by hardware (as is
 the case for VMPLs in AMD), or if KVM supports accelerated plane switch
 operations (as might be the case for Hyper-V VTLs).
 
+.. _kvm_run:
+
 5. The kvm_run structure
 ========================
 
Signature · 7 lines
2.50.1


-- 
kvm-riscv mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/kvm-riscv
maintainer
#148 lore raw

cover KVM: planes: Userspace API

In reply to Jörg Rödel

From: David Woodhouse <[email protected]>

The VSM work¹ drives planes from userspace, which takes a little more API than the planes series² itself provides: an explicit userspace opt-in (enabling KVM_CAP_PLANES with the plane count, before any vCPU is created), KVM_GET/SET_PLANE_RUN_STATE vCPU ioctls, and the mechanics to go with them — per-plane requests fanned out to the twin vCPUs, XCR0 shared across twins (with every twin's fpstate sized for the dynamic xfeatures that allows), and APICv inhibited while planes are enabled (for now). With selftests, including Paolo's plane infrastructure tests from the original planes RFC³, ported forward.

Posted separately from the VSM RFC v2 (to follow) so this part can be reviewed — or folded — with the rest of the planes series. Based on that series² with my fixes⁴ applied (including two follow-up patches on that thread); the whole stack is at ⁵.

¹ https://lore.kernel.org/kvm/[email protected]/
² https://lore.kernel.org/kvm/[email protected]/
³ https://lore.kernel.org/all/[email protected]/
⁴ https://lore.kernel.org/kvm/[email protected]/
⁵ https://git.infradead.org/?p=users/dwmw2/linux.git;a=shortlog;h=refs/heads/vsm2/planes-uapi

David Woodhouse (12):
KVM: x86: Let userspace opt in to multiple planes with KVM_CAP_PLANES
KVM: selftests: Add x86 planes lifecycle test
KVM: Add KVM_GET/SET_PLANE_RUN_STATE vCPU ioctls
KVM: selftests: Cover plane run-state ioctls in planes_test
KVM: planes: Fan out per-plane requests to twin vCPUs
KVM: x86: Inhibit APICv when VM planes are enabled
KVM: planes: Enlarge every twin's fpstate for dynamic xfeatures
KVM: x86: Let userspace choose state for plane twins to share
KVM: planes: Let plane twins share XCR0
KVM: planes: Let plane twins share DR6
KVM: selftests: Check CPUID handling of plane twins
KVM: selftests: Cover plane state sharing

Paolo Bonzini (1):
KVM: selftests: Add VM plane infrastructure and tests

 Documentation/virt/kvm/api.rst                | 129 +++-
 arch/powerpc/kvm/trace.h                      |   3 +-
 arch/x86/include/asm/kvm_host.h               |   8 +-
 arch/x86/include/uapi/asm/kvm.h               |   4 +
 arch/x86/kvm/cpuid.c                          |  25 +-
 arch/x86/kvm/mmu/mmu.c                        |  13 +-
 arch/x86/kvm/regs.c                           |   1 +
 arch/x86/kvm/svm/svm.h                        |   3 +-
 arch/x86/kvm/trace.h                          |   3 +-
 arch/x86/kvm/vmx/main.c                       |   3 +-
 arch/x86/kvm/x86.c                            | 221 ++++++-
 arch/x86/kvm/x86.h                            |  21 +
 include/linux/kvm_host.h                      |  36 +-
 include/uapi/linux/kvm.h                      |   7 +
 tools/testing/selftests/kvm/Makefile.kvm      |   3 +
 .../testing/selftests/kvm/include/kvm_util.h  |  73 +++
 .../selftests/kvm/include/x86/processor.h     |  14 +
 tools/testing/selftests/kvm/lib/kvm_util.c    |  74 ++-
 .../testing/selftests/kvm/lib/x86/processor.c |  47 +-
 tools/testing/selftests/kvm/plane_test.c      | 137 ++++
 .../selftests/kvm/x86/plane_x86_test.c        | 331 ++++++++++
 tools/testing/selftests/kvm/x86/planes_test.c | 605 ++++++++++++++++++
 virt/kvm/kvm_main.c                           | 138 +++-
 23 files changed, 1852 insertions(+), 47 deletions(-)
 create mode 100644 tools/testing/selftests/kvm/plane_test.c
 create mode 100644 tools/testing/selftests/kvm/x86/plane_x86_test.c
 create mode 100644 tools/testing/selftests/kvm/x86/planes_test.c
Signature · 7 lines
2.50.1


-- 
kvm-riscv mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/kvm-riscv
maintainer
#149 lore raw

RFC PATCH 1/13 KVM: x86: Let userspace opt in to multiple planes with KVM_CAP_PLANES

From: David Woodhouse <[email protected]>

A VMM implementing a software privilege model such as Hyper-V VSM needs more than one plane, but kvm_arch_max_planes() returns the vendor ceiling, which is 1 everywhere except SEV-SNP (where VMPLs are guest-architectural), so KVM_CREATE_PLANE rejects every id above zero.

Make KVM_CAP_PLANES a VM enable capability taking the number of planes userspace is going to manage, up to KVM_MAX_USER_PLANES (4), which the system fd now reports. It must be set before any vCPU or extra plane exists, so nothing is sized against the old limit, and cannot be changed once set. It also needs a split irqchip, which kvm_arch_max_planes() already requires before it reports more than one plane, so enabling it without one fails instead of silently granting a single plane, and KVM_CREATE_IRQCHIP is refused afterwards. Non-default VM types are
rejected: SEV-SNP keeps deriving its count from the supported VMPLs, and protected-state VM types need their own audit first. So is more than one plane without TDP: with shadow paging every plane would write-track the guest page tables, and the per-gfn write-track count is only 16 bits wide. The same count is why the opt-in stops at four planes even with TDP: nested guests still have write-tracked shadow pages, each plane can add its own for a gfn, and four planes is the most that keeps the count below 2^16. KVM_MAX_PLANES stays 16, the bound for plane ids. A VM that does not enable the capability keeps reporting a single plane.

---
 Documentation/virt/kvm/api.rst  | 36 +++++++++++++++++++++
 arch/x86/include/asm/kvm_host.h |  1 +
 arch/x86/kvm/x86.c              | 55 +++++++++++++++++++++++++++++++--
 include/linux/kvm_host.h        | 12 +++++++
 virt/kvm/kvm_main.c             | 11 ++++---
 5 files changed, 108 insertions(+), 7 deletions(-)
Documentation/virt/kvm/api.rst +36 -0
index 9aa09d8a39dc..25ce35d5361a 100644
@@ -6834,6 +6834,12 @@ See also: :ref:`KVM_SET_MEMORY_ATTRIBUTES`.
 
 Creates a new *plane*, i.e. a separate privilege level for the virtual machine.
 
+The range of valid plane ids is [0, N) where N is the value returned by
+KVM_CHECK_EXTENSION(KVM_CAP_PLANES) on the VM file descriptor.  Plane 0 is
+the VM itself; for VM types where the plane count is not guest-architectural,
+ids above 0 only become valid after userspace enables KVM_CAP_PLANES with
+the number of planes it is going to manage (see :ref:`KVM_CAP_PLANES`).
+
 Each plane has a numeric id that is used when communicating with KVM.  While
 KVM is currently agnostic to whether low ids are more or less privileged, it is
 expected that this will not always be the case in the future.  For example KVM
@@ -9234,6 +9240,36 @@ enabled, cmma can't be enabled anymore and pfmfi and the storage key
 interpretation are disabled. If cmma has already been enabled or the
 hpage_2g module parameter is not set to 1, -EINVAL is returned.
 
+.. _KVM_CAP_PLANES:
+
+7.48 KVM_CAP_PLANES
+-------------------
+
+:Architectures: x86
+:Target: VM
+:Parameters: args[0] contains the number of planes to make available
+
+Opts the VM in to using multiple planes, raising the limit on plane ids
+that KVM_CREATE_PLANE accepts to args[0], which must be between 1 and the
+value KVM_CHECK_EXTENSION(KVM_CAP_PLANES) returns on the system fd.  That
+ceiling is currently 4: on x86 every plane can add its own shadow pages for
+a gfn, and the per-gfn write-tracking count is only 16 bits wide.
+
+Planes other than plane 0 only exist when userspace manages them, so by
+default a VM reports and accepts a single plane.  VM types whose plane
+count is guest-architectural (such as SEV-SNP, where it reflects the
+supported VMPLs) report that count without this capability, and enabling
+it on them fails with -EINVAL.  Multiple planes also require two-dimensional
+paging: without TDP, enabling more than one plane fails with -EINVAL.
+
+Planes require a split irqchip.  Enable KVM_CAP_SPLIT_IRQCHIP first;
+otherwise enabling this capability fails with -EINVAL.  Once it is
+enabled, KVM_CREATE_IRQCHIP fails as well.
+
+This capability must be enabled before any vCPU or extra plane has been
+created, and the chosen count cannot be changed afterwards; otherwise
+-EEXIST is returned.
+
 8. Other capabilities.
 ======================
 
arch/x86/include/asm/kvm_host.h +1 -0
index a4912735ad3d..d4628ab99197 100644
@@ -1193,6 +1193,7 @@ struct kvm_arch {
 	bool has_protected_eoi;
 	bool has_protected_pmu;
 	bool pre_fault_allowed;
+	unsigned int user_max_planes;
 	struct hlist_head *mmu_page_hash;
 	struct list_head active_mmu_pages;
 	struct kvm_possible_nx_huge_pages possible_nx_huge_pages[KVM_NR_MMU_TYPES];
arch/x86/kvm/x86.c +53 -2

77 lines not on this page · raw message

include/linux/kvm_host.h +12 -0
index 6922e3063ad8..562489ab444d 100644
@@ -1195,6 +1195,18 @@ void kvm_unlock_all_vcpus(struct kvm *kvm);
 void vcpu_load(struct kvm_vcpu *vcpu);
 void vcpu_put(struct kvm_vcpu *vcpu);
 
+/*
+ * The most planes userspace may enable with KVM_ENABLE_CAP(KVM_CAP_PLANES).
+ * KVM_MAX_PLANES (16, the Hyper-V VTL limit) stays the bound for plane ids
+ * and per-plane arrays.  The opt-in is lower because on x86 each plane can
+ * shadow a gfn with its own upper-level shadow pages: the per-gfn count of
+ * those is a bit less than 2^14 per plane (see union kvm_mmu_page_role),
+ * and the per-gfn write-track count is 16 bits wide, so four planes is the
+ * most that keeps it from overflowing.  The architecture defines 4 SEV-SNP
+ * VMPLs, so 4 is also the most planes any x86 VM can have.
+ */
+#define KVM_MAX_USER_PLANES	4
+
 unsigned kvm_arch_max_planes(struct kvm *kvm);
 struct kvm_plane *kvm_alloc_plane(void);
 void kvm_free_plane(struct kvm_plane *plane);
virt/kvm/kvm_main.c +6 -5
index 62d33f24dd7f..75ceab2d4838 100644
@@ -5438,10 +5438,11 @@ static int kvm_vm_ioctl_check_extension_generic(struct kvm *kvm, long arg)
 		if (kvm)
 			return kvm_arch_max_planes(kvm);
 		/*
-		 * The system fd reports the ceiling a VM may be given; a VM
-		 * fd reports what the VM actually has.
+		 * The system fd reports the ceiling a VM may opt in to via
+		 * KVM_ENABLE_CAP(KVM_CAP_PLANES); a VM fd reports what the
+		 * VM actually has.
 		 */
-		return KVM_MAX_PLANES;
+		return KVM_MAX_USER_PLANES;
 	default:
 		break;
 	}
@@ -5656,8 +5657,8 @@ static int kvm_vm_ioctl_create_plane(struct kvm *kvm, unsigned id)
 	guard(mutex)(&kvm->lock);
 
 	/*
-	 * Check the limit under kvm->lock, which KVM_CAP_SPLIT_IRQCHIP (and
-	 * later the KVM_CAP_PLANES opt-in) take when they raise the limit.
+	 * Check the limit under kvm->lock, which KVM_CAP_SPLIT_IRQCHIP and
+	 * the KVM_CAP_PLANES opt-in take when they raise the limit.
 	 */
 	if (id >= kvm_arch_max_planes(kvm) ||
 	    WARN_ON_ONCE(id >= KVM_MAX_PLANES))
Signature · 1 line
2.50.1
maintainer
#150 lore raw

RFC PATCH 2/13 KVM: selftests: Add VM plane infrastructure and tests

In reply to David Woodhouse

From: Paolo Bonzini <[email protected]>

Add plane and plane-vCPU wrappers to the selftest library and two tests:
plane_test (plane and plane vCPU creation, sibling rules, register state isolation) and plane_x86_test (per-plane FPU, MSRs and LAPIC; MSI delivery to a plane). Planes require a split IRQ chip, so the tests create their VM with vm_create_barebones_irqchip(true).

The code is from the planes RFC's selftest work, already adapted to the KVM_CREATE_VCPU on a plane fd shape of the posted series.

[hmushi: ported onto the posted series on kvm/next]

[dwmw2: ported to this stack's uAPI: KVM_CAP_PLANES is a VM-level
opt-in taking the plane count and must precede vCPU creation, so the tests enable it with 2 planes right after VM creation. The main() probe uses a split-irqchip VM, because kvm_arch_max_planes() reports 1 without one, and skips when the opt-in is refused. Two subtest groups of the later planes series are not carried: the foreign-thread EBUSY test (this kernel does not restrict vCPU ioctls to the vCPU's own thread) and the userspace plane-switch group (this kernel drives plane switches through the Hyper-V VSM hypercalls). vm_create_irqchip() already falls back to a split irqchip when KVM_CREATE_IRQCHIP fails with ENOTTY. The seven remaining subtests (two in plane_test, five in plane_x86_test) pass on this kernel; the MP_STATE one restores the plane vCPU's LAPIC before its INIT MSI, because a plane's APIC map is built from APIC state writes.]
(re-done by hand from Mushahid's port)
[dwmw2: rebased onto 7.4-rc2: context only in selftests (gmem_ioctl(),
vm_types_test, kvm_free_fd())]
[dwmw2: review fixes: u32 ids, one KVM_SET_MSRS helper for vCPU and
plane vCPU fds, an asserting plane_vcpu_set_msr() for the APICBASE write, apic.h's APIC_SPIV/APIC_IRR, static test functions, no second vcpu_set_cpuid() after vcpu_init_cpuid(), and corrected comments and assertion messages; a u32 plane id for vm_plane_add(), a plane_vcpu_get_msr() helper sharing one KVM_GET_MSRS helper with vcpu_get_msr(), the durable-MSR read-back in plane_vcpu_set_msr(), and a TSC check that orders three reads on one pCPU instead of bounding the elapsed time]

---
 tools/testing/selftests/kvm/Makefile.kvm      |   2 +
 .../testing/selftests/kvm/include/kvm_util.h  |  73 ++++
 .../selftests/kvm/include/x86/processor.h     |  14 +
 tools/testing/selftests/kvm/lib/kvm_util.c    |  74 +++-
 .../testing/selftests/kvm/lib/x86/processor.c |  47 ++-
 tools/testing/selftests/kvm/plane_test.c      | 137 ++++++++
 .../selftests/kvm/x86/plane_x86_test.c        | 331 ++++++++++++++++++
 7 files changed, 667 insertions(+), 11 deletions(-)
 create mode 100644 tools/testing/selftests/kvm/plane_test.c
 create mode 100644 tools/testing/selftests/kvm/x86/plane_x86_test.c
tools/testing/selftests/kvm/Makefile.kvm +2 -0
index 17a4256e9081..cdc6039d32d2 100644
@@ -72,6 +72,7 @@ TEST_GEN_PROGS_COMMON += set_memory_region_test
 TEST_GEN_PROGS_COMMON += memslot_modification_stress_test
 TEST_GEN_PROGS_COMMON += memslot_perf_test
 TEST_GEN_PROGS_COMMON += vm_types_test
+TEST_GEN_PROGS_COMMON += plane_test
 
 # Compiled test targets
 TEST_GEN_PROGS_x86 = $(TEST_GEN_PROGS_COMMON)
@@ -109,6 +110,7 @@ TEST_GEN_PROGS_x86 += x86/nested_tsc_scaling_test
 TEST_GEN_PROGS_x86 += x86/nested_vmsave_vmload_test
 TEST_GEN_PROGS_x86 += x86/nested_x2apic_test
 TEST_GEN_PROGS_x86 += x86/platform_info_test
+TEST_GEN_PROGS_x86 += x86/plane_x86_test
 TEST_GEN_PROGS_x86 += x86/pmu_counters_test
 TEST_GEN_PROGS_x86 += x86/pmu_event_filter_test
 TEST_GEN_PROGS_x86 += x86/private_mem_conversions_test
tools/testing/selftests/kvm/include/kvm_util.h +73 -0

119 lines not on this page · raw message

tools/testing/selftests/kvm/include/x86/processor.h +14 -0
index 5898a277a2e0..f48c6599732b 100644
@@ -1211,7 +1211,21 @@ static inline void vcpu_clear_cpuid_feature(struct kvm_vcpu *vcpu,
 }
 
 u64 vcpu_get_msr(struct kvm_vcpu *vcpu, u64 msr_index);
+u64 plane_vcpu_get_msr(struct kvm_plane_vcpu *plane_vcpu, u64 msr_index);
 int _vcpu_set_msr(struct kvm_vcpu *vcpu, u64 msr_index, u64 msr_value);
+int _plane_vcpu_set_msr(struct kvm_plane_vcpu *plane_vcpu, u64 msr_index, u64 msr_value);
+
+#define plane_vcpu_set_msr(plane_vcpu, msr, val)					\
+do {											\
+	u64 r, v = val;								\
+											\
+	TEST_ASSERT_MSR(_plane_vcpu_set_msr(plane_vcpu, msr, v) == 1,			\
+			"KVM_SET_MSRS failed on %s, value = 0x%lx", msr, #msr, v);	\
+	if (!is_durable_msr(msr))							\
+		break;									\
+	r = plane_vcpu_get_msr(plane_vcpu, msr);					\
+	TEST_ASSERT_MSR(r == v, "Set %s to '0x%lx', got back '0x%lx'", msr, #msr, v, r);\
+} while (0)
 
 /*
  * Assert on an MSR access(es) and pretty print the MSR name when possible.
tools/testing/selftests/kvm/lib/kvm_util.c +69 -5

110 lines not on this page · raw message

tools/testing/selftests/kvm/lib/x86/processor.c +41 -6

75 lines not on this page · raw message

tools/testing/selftests/kvm/plane_test.c new file +137 -0

137 lines not on this page · raw message

tools/testing/selftests/kvm/x86/plane_x86_test.c new file +331 -0

331 lines not on this page · raw message

Signature · 1 line
2.50.1
maintainer
#151 lore raw

RFC PATCH 3/13 KVM: selftests: Add x86 planes lifecycle test

In reply to David Woodhouse

From: David Woodhouse <[email protected]>

Cover KVM_CAP_PLANES argument validation against the system fd's ceiling, its split-irqchip and ordering requirements, plane creation bounds, plane vCPU creation, and per-plane isolation of vCPU register state. Skip on hosts that cannot give a VM more than one plane, such as those without TDP.

---
 tools/testing/selftests/kvm/Makefile.kvm      |   1 +
 tools/testing/selftests/kvm/x86/planes_test.c | 137 ++++++++++++++++++
 2 files changed, 138 insertions(+)
 create mode 100644 tools/testing/selftests/kvm/x86/planes_test.c
tools/testing/selftests/kvm/Makefile.kvm +1 -0
index cdc6039d32d2..2926817baad0 100644
@@ -111,6 +111,7 @@ TEST_GEN_PROGS_x86 += x86/nested_vmsave_vmload_test
 TEST_GEN_PROGS_x86 += x86/nested_x2apic_test
 TEST_GEN_PROGS_x86 += x86/platform_info_test
 TEST_GEN_PROGS_x86 += x86/plane_x86_test
+TEST_GEN_PROGS_x86 += x86/planes_test
 TEST_GEN_PROGS_x86 += x86/pmu_counters_test
 TEST_GEN_PROGS_x86 += x86/pmu_event_filter_test
 TEST_GEN_PROGS_x86 += x86/private_mem_conversions_test
tools/testing/selftests/kvm/x86/planes_test.c new file +137 -0

137 lines not on this page · raw message

Signature · 7 lines
2.50.1


-- 
kvm-riscv mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/kvm-riscv
maintainer
#152 lore raw

RFC PATCH 4/13 KVM: Add KVM_GET/SET_PLANE_RUN_STATE vCPU ioctls

In reply to David Woodhouse

From: David Woodhouse <[email protected]>

A VMM doing VSM in userspace has to switch the executing plane when the guest asks for a privilege transition, but a vCPU's plane run state is kernel-internal, set only by the SEV-SNP VMPL switch, plane-0 event delivery, and vCPU creation. A VTL call forwarded to userspace is a dead end.

Add ioctls that read and write a plane vCPU's run state as three flags. KVM_PLANE_RUN_STATE_USER_HOLD is userspace policy: the plane is stopped, and only KVM_SET_PLANE_RUN_STATE clears it again. KVM_PLANE_RUN_STATE_SUSPENDED marks a plane suspended by a guest privilege transition; KVM does not set it yet, but userspace can restore it. KVM_PLANE_RUN_STATE_INACTIVE marks a non-zero plane that has not been started, which is how every such plane begins; SUSPENDED implies a started plane, so the two together are refused. A plane with no flag is runnable. KVM_RUN already executes the lowest-numbered runnable plane, so a userspace plane switch is just: clear the target's flags, hold the current plane, re-enter KVM_RUN.

A held or suspended plane 0 *stays* stopped. Forcing it runnable on a pending event fits the SEV-SNP model, but under a userspace privilege model plane 0 would preempt the higher VTL on any interrupt and resume mid-hypercall with whatever completion value the shared kvm_run last held; the events stay pending until the VMM switches back. For the same reason KVM_RUN no longer falls back to a held plane 0 when nothing is
runnable: it exits with KVM_EXIT_PLANE_EVENT and the new KVM_PLANE_EVENT_HELD cause, through plane 0's normal exit path, so immediate_exit and the kvm_run state userspace reads behave as for any other exit. When KVM makes a plane runnable it clears the two kernel-owned flags, SUSPENDED and INACTIVE, and never the hold.

The held exit must not disturb what the shared kvm_run carries for a plane. It neither applies kvm_dirty_regs nor refreshes the kvm_valid_regs area; those belong to the plane that runs next. A completion still pending from plane 0's last exit is harder. Running it first, as any other KVM_RUN would, is right only if kvm_run still holds that exit's data, and KVM cannot tell whether it does: a VMM doing VSM holds plane 0 inside a forwarded VtlCall while VTL1 runs, VTL1's exits reuse the same union, and the completion would retire the VtlCall with VTL1's bytes. Exiting with the completion still armed would instead overwrite completion data the VMM did write. So with a completion pending, a held KVM_RUN fails with -EBUSY and leaves kvm_run alone; the completion runs when plane 0 next runs.

The flags round-trip: a VMM that saves them and writes them back, for example on a migration destination, restores the same state, including a plane that was never started. Which runnable plane happens to be executing is not part of the interface.

The ioctls are for planes userspace manages, so they return -EINVAL unless the VM enabled more than one plane with KVM_CAP_PLANES. That leaves SEV-SNP VMPL planes, whose count and switching the architecture defines, outside the interface, and it keeps other architectures out:
only x86 consumes KVM_REQ_PLANE_RESCHED, and elsewhere a stray request bit would abort every subsequent entry attempt, leaving KVM_RUN spinning at 100% with no way for the VMM to recover the vCPU.

---
 Documentation/virt/kvm/api.rst |  69 +++++++++++++++++++++
 arch/x86/kvm/x86.c             |  41 ++++++++++---
 include/linux/kvm_host.h       |  21 ++++++-
 include/uapi/linux/kvm.h       |   6 ++
 virt/kvm/kvm_main.c            | 107 ++++++++++++++++++++++++++++++++-
 5 files changed, 233 insertions(+), 11 deletions(-)
Documentation/virt/kvm/api.rst +69 -0
index 25ce35d5361a..4196f3fc5718 100644
@@ -427,6 +427,9 @@ to be dirty. KVM will not explicitly mark it such.
 Errors:
 
   =======    ==============================================================
+  EBUSY      no plane is runnable, userspace holds plane 0, and plane 0
+             still has an operation from its last exit to complete (x86,
+             see KVM_SET_PLANE_RUN_STATE)
   EINTR      an unmasked signal is pending
   ENOEXEC    the vcpu hasn't been initialized or the guest tried to execute
              instructions from device memory (arm64)
@@ -6847,6 +6850,72 @@ in the future may use the plane id when planes are supported by hardware (as is
 the case for VMPLs in AMD), or if KVM supports accelerated plane switch
 operations (as might be the case for Hyper-V VTLs).
 
+.. _KVM_GET_PLANE_RUN_STATE:
+
+4.148 KVM_GET_PLANE_RUN_STATE / KVM_SET_PLANE_RUN_STATE
+--------------------------------------------------------
+
+:Capability: KVM_CAP_PLANES
+:Architectures: x86
+:Type: vcpu ioctl
+:Parameters: pointer to a __u64 of run state flags
+:Returns: 0 on success, <0 on error
+
+Reads or sets the run state flags of a plane's vCPU:
+
+- KVM_PLANE_RUN_STATE_USER_HOLD (bit 0): userspace has stopped the plane.
+  Only KVM_SET_PLANE_RUN_STATE sets or clears it.
+- KVM_PLANE_RUN_STATE_SUSPENDED (bit 1): the plane is suspended by a
+  guest privilege transition, and resumes when that transition unwinds.
+  KVM does not set it yet; userspace can set it to restore a saved state.
+- KVM_PLANE_RUN_STATE_INACTIVE (bit 2): the plane has not been started.
+  KVM sets it on every vCPU of a non-zero plane at creation and clears it
+  when the plane is made runnable.  It is not accepted on plane 0.
+  KVM_PLANE_RUN_STATE_SUSPENDED implies the plane was started, so setting
+  both flags fails with -EINVAL.
+
+A plane with no flag set is runnable.  Setting any other bit fails
+with -EINVAL.  Writing back the flags previously read restores the same
+state, so a VMM can save and restore the run state of every plane, for
+example across live migration.  Which runnable plane executes at a given
+moment is not part of this state.
+
+When a vCPU thread calls KVM_RUN, KVM executes the lowest-numbered
+runnable plane, falling back to plane 0.  Whenever KVM itself makes a
+plane runnable, it clears KVM_PLANE_RUN_STATE_SUSPENDED and
+KVM_PLANE_RUN_STATE_INACTIVE but leaves a hold alone.  A VMM
+implementing a software privilege model (such as Hyper-V VSM) switches
+planes by clearing the target plane's flags and setting
+KVM_PLANE_RUN_STATE_USER_HOLD on the current one before re-entering
+KVM_RUN.  Pending events for plane 0 make it runnable again on their own
+unless it is held or suspended; a held plane 0 keeps its events pending
+until the VMM clears the hold, so a software privilege model can keep a
+higher plane running undisturbed.  The ioctl is accepted on any plane's
+vCPU fd, on VMs that enabled more than one plane with
+KVM_ENABLE_CAP(KVM_CAP_PLANES); it fails with -EINVAL on other VMs.
+Planes whose count the architecture defines, such as SEV-SNP VMPLs, are
+not managed through this interface.
+
+If no plane is runnable and userspace holds plane 0, KVM_RUN does not
+override the hold.  It returns to userspace through plane 0 with exit
+reason KVM_EXIT_PLANE_EVENT, cause KVM_PLANE_EVENT_HELD and plane 0;
+immediate_exit still yields -EINTR, and the kvm_run fields every exit
+updates, such as if_flag and apic_base, are filled in.  A held KVM_RUN
+leaves kvm_dirty_regs and the kvm_valid_regs register area alone: they
+are applied to, and refreshed from, the plane that runs next.  If plane 0
+still has an operation from its last exit to complete (see the note in
+the description of KVM_EXIT_MMIO), KVM_RUN fails with -EBUSY instead,
+with or without immediate_exit, and leaves exit_reason and the exit
+fields alone.  The operation completes when plane 0 next runs, from what
+kvm_run holds then, so a VMM that ran another plane in between must put
+plane 0's completion data back before releasing it.
+
+The run state is distinct from KVM_GET/SET_MP_STATE, which describes
+the architectural state of a vCPU within its plane.
+
+A newly created vCPU of plane 0 has no flags set; one of a non-zero
+plane has KVM_PLANE_RUN_STATE_INACTIVE.
+
 .. _kvm_run:
 
 5. The kvm_run structure
arch/x86/kvm/x86.c +33 -8

80 lines not on this page · raw message

include/linux/kvm_host.h +20 -1
index 562489ab444d..b30982fcd3e7 100644
@@ -422,6 +422,23 @@ struct kvm_vcpu {
 
 	u64 plane_requests;
 	enum kvm_vcpu_state plane_state;
+	/*
+	 * KVM_PLANE_RUN_STATE_USER_HOLD: userspace policy, changed only by
+	 * KVM_SET_PLANE_RUN_STATE.  Pending plane-0 events no longer make
+	 * a held plane 0 runnable on their own; the VMM owns the switch.
+	 */
+	bool plane_user_hold;
+	/*
+	 * KVM_PLANE_RUN_STATE_SUSPENDED: the plane is suspended by a guest
+	 * privilege transition and resumes when that transition unwinds.
+	 */
+	bool plane_suspended;
+	/*
+	 * KVM_PLANE_RUN_STATE_INACTIVE: a non-zero plane that has not been
+	 * started.  Set at vCPU creation, cleared when the plane is made
+	 * runnable.
+	 */
+	bool plane_inactive;
 
 	/* S390 only */
 	bool valid_wakeup;
@@ -463,7 +480,7 @@ struct kvm_vcpu {
 
 void kvm_vcpu_set_plane_runnable(struct kvm_vcpu *vcpu);
 void kvm_vcpu_set_plane_stopped(struct kvm_vcpu *vcpu);
-struct kvm_vcpu *kvm_vcpu_select_plane(struct kvm_vcpu *vcpu);
+struct kvm_vcpu *kvm_vcpu_select_plane(struct kvm_vcpu *vcpu, bool *held);
 
 static inline bool kvm_vcpu_wants_to_run(struct kvm_vcpu *vcpu)
 {
@@ -1208,6 +1225,7 @@ void vcpu_put(struct kvm_vcpu *vcpu);
 #define KVM_MAX_USER_PLANES	4
 
 unsigned kvm_arch_max_planes(struct kvm *kvm);
+bool kvm_arch_has_user_planes(struct kvm *kvm);
 struct kvm_plane *kvm_alloc_plane(void);
 void kvm_free_plane(struct kvm_plane *plane);
 
@@ -1733,6 +1751,7 @@ void kvm_arch_vcpu_unblocking(struct kvm_vcpu *vcpu);
 bool kvm_vcpu_wake_up(struct kvm_vcpu *vcpu);
 
 int kvm_request_create_plane(struct kvm_vcpu *vcpu, unsigned plane, u64 apic_id);
+int kvm_plane_held_exit(struct kvm_vcpu *vcpu);
 
 #ifndef CONFIG_S390
 void __kvm_vcpu_kick(struct kvm_vcpu *vcpu, bool wait);
include/uapi/linux/kvm.h +6 -0
index af782b001ea8..abf023ed78c4 100644
@@ -148,6 +148,7 @@ struct kvm_exit_snp_req_certs {
 
 struct kvm_plane_event_exit {
 #define KVM_PLANE_EVENT_CREATE_VCPU	1
+#define KVM_PLANE_EVENT_HELD		2
 	__u32 cause;
 	__u32 plane;
 	__u64 extra[8];
@@ -1372,6 +1373,11 @@ struct kvm_s390_keyop {
 #define KVM_HAS_DEVICE_ATTR	  _IOW(KVMIO,  0xe3, struct kvm_device_attr)
 
 #define KVM_CREATE_PLANE	  _IO(KVMIO, 0xe4)
+#define KVM_GET_PLANE_RUN_STATE	  _IOR(KVMIO, 0xe5, __u64)
+#define KVM_SET_PLANE_RUN_STATE	  _IOW(KVMIO, 0xe6, __u64)
+#define   KVM_PLANE_RUN_STATE_USER_HOLD	(1ULL << 0)
+#define   KVM_PLANE_RUN_STATE_SUSPENDED	(1ULL << 1)
+#define   KVM_PLANE_RUN_STATE_INACTIVE	(1ULL << 2)
 
 /*
  * ioctls for vcpu fds
virt/kvm/kvm_main.c +105 -2

157 lines not on this page · raw message

Signature · 1 line
2.50.1
maintainer
#153 lore raw

RFC PATCH 5/13 KVM: selftests: Cover plane run-state ioctls in planes_test

In reply to David Woodhouse

From: David Woodhouse <[email protected]>

Assert that a new plane-1 vCPU starts with KVM_PLANE_RUN_STATE_INACTIVE and plane 0's with no flags, that every valid combination of KVM_PLANE_RUN_STATE_USER_HOLD, KVM_PLANE_RUN_STATE_SUSPENDED and KVM_PLANE_RUN_STATE_INACTIVE reads back as written (so a saved state restores), and that clearing them reads back as runnable. INACTIVE on plane 0, INACTIVE together with SUSPENDED and the lowest undefined bit are refused with EINVAL, and a refused SET leaves an existing hold in place. On a VM that did not enable more than one plane both ioctls fail with EINVAL.

With plane 0 held and nothing runnable, KVM_RUN still honours immediate_exit with EINTR, and otherwise exits with KVM_EXIT_PLANE_EVENT and cause KVM_PLANE_EVENT_HELD, with kvm_run's if_flag and apic_base filled in as on any other exit. That exit leaves registers marked in kvm_dirty_regs unconsumed and unrefreshed, and once plane 1 is released they land in plane 1, not plane 0. If plane 0 holds a pending I/O completion, here from a real-mode OUT, a held KVM_RUN fails with EBUSY, with or without immediate_exit, leaves the KVM_EXIT_IO in kvm_run and plane 0's RIP on the OUT, and the OUT retires once plane 0 is released.

---
 tools/testing/selftests/kvm/x86/planes_test.c | 198 +++++++++++++++++-
 1 file changed, 196 insertions(+), 2 deletions(-)
tools/testing/selftests/kvm/x86/planes_test.c +196 -2
index f7db6cb6807d..968f1b099b8e 100644
@@ -8,6 +8,10 @@
 #include <string.h>
 
 #include "kvm_util.h"
+#include "processor.h"
+
+/* Where the run-state test puts plane 0's one instruction. */
+#define CODE_GPA	0x1000
 
 /*
  * Whether this host lets a VM have more than one plane: the opt-in also
@@ -61,12 +65,22 @@ static void test_enable_cap_bounds(void)
 static void test_enable_cap_too_late(void)
 {
 	struct kvm_vm *vm = vm_create_barebones_irqchip(true);
-
-	__vm_vcpu_add(vm, 0);
+	struct kvm_vcpu *vcpu = __vm_vcpu_add(vm, 0);
 
 	TEST_ASSERT_EQ(__vm_enable_cap(vm, KVM_CAP_PLANES, 2), -1);
 	TEST_ASSERT_EQ(errno, EEXIST);
 
+	/* Without more than one user-managed plane there is no run state. */
+	{
+		u64 flags;
+
+		TEST_ASSERT_EQ(__vcpu_ioctl(vcpu, KVM_GET_PLANE_RUN_STATE, &flags), -1);
+		TEST_ASSERT_EQ(errno, EINVAL);
+		flags = 0;
+		TEST_ASSERT_EQ(__vcpu_ioctl(vcpu, KVM_SET_PLANE_RUN_STATE, &flags), -1);
+		TEST_ASSERT_EQ(errno, EINVAL);
+	}
+
 	kvm_vm_free(vm);
 }
 
@@ -107,6 +121,186 @@ static void test_plane_lifecycle(void)
 	vcpu0 = __vm_vcpu_add(vm, 0);
 	vcpu1 = __vm_plane_vcpu_add(vcpu0, plane);
 
+	/*
+	 * A new plane-1 vCPU is inactive, and plane 0's has no flags.
+	 * Every valid combination reads back as written, so a saved state
+	 * restores; unknown bits, INACTIVE on plane 0 and INACTIVE together
+	 * with SUSPENDED are refused.
+	 */
+	{
+		static const u64 states[] = {
+			KVM_PLANE_RUN_STATE_USER_HOLD,
+			KVM_PLANE_RUN_STATE_SUSPENDED,
+			KVM_PLANE_RUN_STATE_USER_HOLD |
+				KVM_PLANE_RUN_STATE_SUSPENDED,
+			KVM_PLANE_RUN_STATE_INACTIVE,
+			KVM_PLANE_RUN_STATE_USER_HOLD |
+				KVM_PLANE_RUN_STATE_INACTIVE,
+			0,
+		};
+		u64 flags = ~0ULL;
+		int i;
+
+		plane_vcpu_ioctl(vcpu1, KVM_GET_PLANE_RUN_STATE, &flags);
+		TEST_ASSERT_EQ(flags, KVM_PLANE_RUN_STATE_INACTIVE);
+		flags = ~0ULL;
+		vcpu_ioctl(vcpu0, KVM_GET_PLANE_RUN_STATE, &flags);
+		TEST_ASSERT_EQ(flags, 0);
+
+		flags = KVM_PLANE_RUN_STATE_INACTIVE;
+		TEST_ASSERT_EQ(__vcpu_ioctl(vcpu0, KVM_SET_PLANE_RUN_STATE,
+					    &flags), -1);
+		TEST_ASSERT_EQ(errno, EINVAL);
+
+		for (i = 0; i < ARRAY_SIZE(states); i++) {
+			flags = states[i];
+			plane_vcpu_ioctl(vcpu1, KVM_SET_PLANE_RUN_STATE, &flags);
+			flags = ~0ULL;
+			plane_vcpu_ioctl(vcpu1, KVM_GET_PLANE_RUN_STATE, &flags);
+			TEST_ASSERT_EQ(flags, states[i]);
+		}
+
+		/* A refused SET leaves the hold in place. */
+		flags = KVM_PLANE_RUN_STATE_USER_HOLD;
+		plane_vcpu_ioctl(vcpu1, KVM_SET_PLANE_RUN_STATE, &flags);
+
+		flags = KVM_PLANE_RUN_STATE_INACTIVE | KVM_PLANE_RUN_STATE_SUSPENDED;
+		TEST_ASSERT_EQ(__plane_vcpu_ioctl(vcpu1, KVM_SET_PLANE_RUN_STATE,
+						  &flags), -1);
+		TEST_ASSERT_EQ(errno, EINVAL);
+
+		/* The lowest bit that no flag defines. */
+		flags = ~(u64)(KVM_PLANE_RUN_STATE_USER_HOLD |
+			       KVM_PLANE_RUN_STATE_SUSPENDED |
+			       KVM_PLANE_RUN_STATE_INACTIVE);
+		flags &= -flags;
+		TEST_ASSERT_EQ(__plane_vcpu_ioctl(vcpu1, KVM_SET_PLANE_RUN_STATE,
+						  &flags), -1);
+		TEST_ASSERT_EQ(errno, EINVAL);
+		flags = ~0ULL;
+		plane_vcpu_ioctl(vcpu1, KVM_GET_PLANE_RUN_STATE, &flags);
+		TEST_ASSERT_EQ(flags, KVM_PLANE_RUN_STATE_USER_HOLD);
+	}
+
+	/*
+	 * Plane 1 is held from above.  With plane 0 held too, nothing is
+	 * runnable, and KVM_RUN hands that back to userspace instead of
+	 * running the held plane 0.
+	 */
+	{
+		u64 flags = KVM_PLANE_RUN_STATE_USER_HOLD;
+		struct kvm_regs regs;
+
+		vcpu_ioctl(vcpu0, KVM_SET_PLANE_RUN_STATE, &flags);
+
+		/* immediate_exit yields EINTR, as on any KVM_RUN. */
+		vcpu0->run->immediate_exit = 1;
+		TEST_ASSERT_EQ(__vcpu_run(vcpu0), -1);
+		TEST_ASSERT_EQ(errno, EINTR);
+		vcpu0->run->immediate_exit = 0;
+
+		/* The exit fills in kvm_run like any other: poison two fields. */
+		vcpu0->run->if_flag = 0xff;
+		vcpu0->run->apic_base = 0;
+		TEST_ASSERT_EQ(__vcpu_run(vcpu0), 0);
+		TEST_ASSERT_EQ(vcpu0->run->exit_reason, KVM_EXIT_PLANE_EVENT);
+		TEST_ASSERT_EQ(vcpu0->run->plane_event.cause, KVM_PLANE_EVENT_HELD);
+		TEST_ASSERT_EQ(vcpu0->run->plane_event.plane, 0);
+		TEST_ASSERT_EQ(vcpu0->run->if_flag, 0);
+		TEST_ASSERT_EQ(vcpu0->run->apic_base,
+			       vcpu_get_msr(vcpu0, MSR_IA32_APICBASE));
+
+		/*
+		 * Registers userspace marks dirty are for the plane that runs
+		 * next: the held exit neither consumes nor overwrites them.
+		 */
+		plane_vcpu_ioctl(vcpu1, KVM_GET_REGS, &vcpu0->run->s.regs.regs);
+		vcpu0->run->s.regs.regs.rdx = 0x5ca1ab1e;	/* arbitrary marker */
+		vcpu0->run->kvm_valid_regs = KVM_SYNC_X86_REGS;
+		vcpu0->run->kvm_dirty_regs = KVM_SYNC_X86_REGS;
+		TEST_ASSERT_EQ(__vcpu_run(vcpu0), 0);
+		TEST_ASSERT_EQ(vcpu0->run->exit_reason, KVM_EXIT_PLANE_EVENT);
+		TEST_ASSERT_EQ(vcpu0->run->kvm_dirty_regs, KVM_SYNC_X86_REGS);
+		TEST_ASSERT_EQ(vcpu0->run->s.regs.regs.rdx, 0x5ca1ab1e);
+
+		/*
+		 * Released, plane 1 is next and takes them; immediate_exit
+		 * stops it before entry, after the registers are applied.
+		 */
+		flags = 0;
+		plane_vcpu_ioctl(vcpu1, KVM_SET_PLANE_RUN_STATE, &flags);
+		vcpu0->run->immediate_exit = 1;
+		TEST_ASSERT_EQ(__vcpu_run(vcpu0), -1);
+		TEST_ASSERT_EQ(errno, EINTR);
+		vcpu0->run->immediate_exit = 0;
+		TEST_ASSERT_EQ(vcpu0->run->kvm_dirty_regs, 0);
+		vcpu0->run->kvm_valid_regs = 0;
+
+		plane_vcpu_ioctl(vcpu1, KVM_GET_REGS, &regs);
+		TEST_ASSERT_EQ(regs.rdx, 0x5ca1ab1e);
+		vcpu_ioctl(vcpu0, KVM_GET_REGS, &regs);
+		TEST_ASSERT(regs.rdx != 0x5ca1ab1e,
+			    "plane 0 took the registers dirtied while it was held");
+
+		vcpu_ioctl(vcpu0, KVM_SET_PLANE_RUN_STATE, &flags);
+	}
+
+	/*
+	 * A completion plane 0 still owes its last exit stays pending while
+	 * plane 0 is held: KVM_RUN fails with EBUSY and leaves the exit in
+	 * kvm_run, and the completion runs once plane 0 runs again.  The
+	 * exit is a real-mode OUT at CODE_GPA, which userspace handles.
+	 */
+	{
+		static const u8 out_insn[] = { 0xe6, 0x10 };	/* out %al, $0x10 */
+		u64 flags = KVM_PLANE_RUN_STATE_USER_HOLD;
+		struct kvm_sregs sregs;
+		struct kvm_regs regs;
+
+		vm_userspace_mem_region_add(vm, VM_MEM_SRC_ANONYMOUS, CODE_GPA,
+					    1, 1, 0);
+		memcpy(addr_gpa2hva(vm, CODE_GPA), out_insn, sizeof(out_insn));
+
+		vcpu_sregs_get(vcpu0, &sregs);
+		sregs.cs.selector = 0;
+		sregs.cs.base = 0;
+		vcpu_sregs_set(vcpu0, &sregs);
+		vcpu_regs_get(vcpu0, &regs);
+		regs.rip = CODE_GPA;
+		vcpu_regs_set(vcpu0, &regs);
+
+		/* Plane 1 stays held, so only plane 0 can run. */
+		plane_vcpu_ioctl(vcpu1, KVM_SET_PLANE_RUN_STATE, &flags);
+		TEST_ASSERT_EQ(__vcpu_run(vcpu0), 0);
+		TEST_ASSERT_EQ(vcpu0->run->exit_reason, KVM_EXIT_IO);
+		TEST_ASSERT_EQ(vcpu0->run->io.direction, KVM_EXIT_IO_OUT);
+		TEST_ASSERT_EQ(vcpu0->run->io.port, 0x10);
+
+		vcpu_ioctl(vcpu0, KVM_SET_PLANE_RUN_STATE, &flags);
+		vcpu0->run->immediate_exit = 1;
+		TEST_ASSERT_EQ(__vcpu_run(vcpu0), -1);
+		TEST_ASSERT_EQ(errno, EBUSY);
+		vcpu0->run->immediate_exit = 0;
+		TEST_ASSERT_EQ(__vcpu_run(vcpu0), -1);
+		TEST_ASSERT_EQ(errno, EBUSY);
+		TEST_ASSERT_EQ(vcpu0->run->exit_reason, KVM_EXIT_IO);
+		TEST_ASSERT_EQ(vcpu0->run->io.port, 0x10);
+		vcpu_regs_get(vcpu0, &regs);
+		TEST_ASSERT_EQ(regs.rip, CODE_GPA);
+
+		/* Released, plane 0 retires the OUT before immediate_exit. */
+		flags = 0;
+		vcpu_ioctl(vcpu0, KVM_SET_PLANE_RUN_STATE, &flags);
+		vcpu0->run->immediate_exit = 1;
+		TEST_ASSERT_EQ(__vcpu_run(vcpu0), -1);
+		TEST_ASSERT_EQ(errno, EINTR);
+		vcpu0->run->immediate_exit = 0;
+		vcpu_regs_get(vcpu0, &regs);
+		TEST_ASSERT_EQ(regs.rip, CODE_GPA + sizeof(out_insn));
+
+		plane_vcpu_ioctl(vcpu1, KVM_SET_PLANE_RUN_STATE, &flags);
+	}
+
 	/* Register state is per plane: write plane 1, plane 0 unaffected. */
 	plane_vcpu_ioctl(vcpu1, KVM_GET_REGS, &regs1);
 	regs1.rbx = 0x5157ca11;	/* arbitrary marker */
Signature · 1 line
2.50.1
maintainer
#154 lore raw

RFC PATCH 6/13 KVM: planes: Fan out per-plane requests to twin vCPUs

In reply to David Woodhouse

From: David Woodhouse <[email protected]>

A request raised on one plane's vCPU for a VM-wide reason (TLB flush after a host-side zap, APIC page reload, obsolete-roots release) is consumed by whichever twin runs first, so the other planes keep stale translations. The plane-1 twin of a Windows VSM guest then runs on a zapped root and the guest bugchecks.

Mark those three requests per-plane and make kvm_make_vcpu_request() replicate any KVM_REQUEST_PER_PLANE request to every twin of the target vCPU through vcpu_for_each_plane(). Mode and wait live in the shared common and the kick targets the running twin's pCPU, so the existing kick logic already reaches the twins.

The x86 kvm_exit and powerpc kvm_check_requests tracepoints logged only the shared request word, so the three requests would vanish from them. OR in the vCPU's plane requests instead; a separate field would say which word a bit came from, but changes the tracepoint format for little gain.

---
 arch/powerpc/kvm/trace.h        |  3 ++-
 arch/x86/include/asm/kvm_host.h |  6 ++++--
 arch/x86/kvm/mmu/mmu.c          | 13 ++++++++-----
 arch/x86/kvm/trace.h            |  3 ++-
 include/linux/kvm_host.h        |  3 ++-
 virt/kvm/kvm_main.c             | 20 +++++++++++++++++++-
 6 files changed, 37 insertions(+), 11 deletions(-)
arch/powerpc/kvm/trace.h +2 -1
index 35c000d918bb..2fc60a4e159f 100644
@@ -108,7 +108,8 @@ TRACE_EVENT(kvm_check_requests,
 
 	TP_fast_assign(
 		__entry->cpu_nr		= vcpu->vcpu_id;
-		__entry->requests	= vcpu->common->requests;
+		__entry->requests	= READ_ONCE(vcpu->common->requests) |
+					  READ_ONCE(vcpu->plane_requests);
 	),
 
 	TP_printk("vcpu=%x requests=%x",
arch/x86/include/asm/kvm_host.h +4 -2
index d4628ab99197..596be8ad7154 100644
@@ -107,7 +107,8 @@
 	KVM_ARCH_REQ_FLAGS(15, KVM_REQUEST_WAIT | KVM_REQUEST_NO_WAKEUP)
 #define KVM_REQ_GLOBAL_CLOCK_UPDATE	KVM_ARCH_REQ(16)
 #define KVM_REQ_APIC_PAGE_RELOAD \
-	KVM_ARCH_REQ_FLAGS(17, KVM_REQUEST_WAIT | KVM_REQUEST_NO_WAKEUP)
+	KVM_ARCH_REQ_FLAGS(17, KVM_REQUEST_WAIT | KVM_REQUEST_NO_WAKEUP | \
+			   KVM_REQUEST_PER_PLANE)
 #define KVM_REQ_HV_CRASH		KVM_ARCH_REQ(18)
 #define KVM_REQ_IOAPIC_EOI_EXIT		KVM_ARCH_PLANE_REQ(19)
 #define KVM_REQ_HV_RESET		KVM_ARCH_REQ(20)
@@ -125,7 +126,8 @@
 #define KVM_REQ_UPDATE_CPU_DIRTY_LOGGING \
 	KVM_ARCH_REQ_FLAGS(30, KVM_REQUEST_WAIT | KVM_REQUEST_NO_WAKEUP)
 #define KVM_REQ_MMU_FREE_OBSOLETE_ROOTS \
-	KVM_ARCH_REQ_FLAGS(31, KVM_REQUEST_WAIT | KVM_REQUEST_NO_WAKEUP)
+	KVM_ARCH_REQ_FLAGS(31, KVM_REQUEST_WAIT | KVM_REQUEST_NO_WAKEUP | \
+			   KVM_REQUEST_PER_PLANE)
 #define KVM_REQ_HV_TLB_FLUSH \
 	KVM_ARCH_REQ_FLAGS(32, KVM_REQUEST_WAIT | KVM_REQUEST_NO_WAKEUP)
 #define KVM_REQ_VMSA_PAGE_RELOAD \
arch/x86/kvm/mmu/mmu.c +8 -5
index e8eb1e48df36..ef6df08f5a99 100644
@@ -6405,11 +6405,14 @@ void kvm_mmu_free_obsolete_roots(struct kvm_vcpu *vcpu)
 	int i;
 
 	/*
-	 * KVM_REQ_MMU_FREE_OBSOLETE_ROOTS is tracked in the request set
-	 * shared by all of the vCPU's planes, so the plane that consumes
-	 * the request is not necessarily the plane whose root is obsolete.
-	 * Service every plane: the inactive twins cannot be running (all
-	 * planes of a vCPU share one thread), so their MMUs are stable.
+	 * KVM_REQ_MMU_FREE_OBSOLETE_ROOTS fans out to every plane twin,
+	 * but each twin only consumes its own copy when it next runs, and
+	 * a plane that userspace never re-enters would pin its obsolete
+	 * roots indefinitely.  Service every plane from whichever twin
+	 * consumes its request first: the inactive twins cannot be
+	 * running (all planes of a vCPU share one thread), so their MMUs
+	 * are stable, and a twin's own later request finds nothing left
+	 * to free.
 	 */
 	vcpu_for_each_plane(vcpu->common, i, v) {
 		__kvm_mmu_free_obsolete_roots(vcpu->kvm, &v->arch.root_mmu);
arch/x86/kvm/trace.h +2 -1
index 7038e596019a..ad901836c08a 100644
@@ -409,7 +409,8 @@ TRACE_EVENT(name,							     \
 		__entry->guest_rip	= tracing_kvm_rip_read(vcpu);		     \
 		__entry->isa            = isa;				     \
 		__entry->vcpu_id        = vcpu->vcpu_id;		     \
-		__entry->requests       = READ_ONCE(vcpu->common->requests); \
+		__entry->requests       = READ_ONCE(vcpu->common->requests) | \
+					  READ_ONCE(vcpu->plane_requests);   \
 		kvm_x86_call(get_exit_info)(vcpu,			     \
 					    &__entry->exit_reason,	     \
 					    &__entry->info1,		     \
include/linux/kvm_host.h +2 -1
index b30982fcd3e7..6c50c1dc5819 100644
@@ -168,7 +168,8 @@ static inline bool kvm_is_error_gpa(gpa_t gpa)
  * Architecture-independent vcpu->requests bit members
  * Bits 3-7 are reserved for more arch-independent bits.
  */
-#define KVM_REQ_TLB_FLUSH		(0 | KVM_REQUEST_WAIT | KVM_REQUEST_NO_WAKEUP)
+#define KVM_REQ_TLB_FLUSH \
+	(0 | KVM_REQUEST_WAIT | KVM_REQUEST_NO_WAKEUP | KVM_REQUEST_PER_PLANE)
 #define KVM_REQ_VM_DEAD			(1 | KVM_REQUEST_WAIT | KVM_REQUEST_NO_WAKEUP)
 #define KVM_REQ_UNBLOCK			2
 #define KVM_REQ_DIRTY_RING_SOFT_FULL	3
virt/kvm/kvm_main.c +19 -1
index 6a00654490ae..c323d067256e 100644
@@ -237,9 +237,27 @@ static void kvm_make_vcpu_request(struct kvm_vcpu *vcpu, unsigned int req,
 {
 	int cpu;
 
-	if (likely(!(req & KVM_REQUEST_NO_ACTION)))
+	if (likely(!(req & KVM_REQUEST_NO_ACTION))) {
 		__kvm_make_request(req, vcpu);
 
+		/*
+		 * A per-plane request raised on "the vCPU" (TLB flush after
+		 * a host-side zap, obsolete-roots reload, ...) concerns
+		 * every plane: fan it out to the twins, or the plane that
+		 * runs next consumes a bit the others still need.  Mode and
+		 * wait live in the common and the kick below targets the
+		 * running twin's pCPU, so it covers the twins too.
+		 */
+		if (req & KVM_REQUEST_PER_PLANE) {
+			struct kvm_vcpu *twin;
+			int p;
+
+			vcpu_for_each_plane(vcpu->common, p, twin)
+				if (twin != vcpu)
+					__kvm_make_request(req, twin);
+		}
+	}
+
 	if (!(req & KVM_REQUEST_NO_WAKEUP) && kvm_vcpu_wake_up(vcpu))
 		return;
 
Signature · 1 line
2.50.1
maintainer
#155 lore raw

RFC PATCH 7/13 KVM: x86: Inhibit APICv when VM planes are enabled

In reply to David Woodhouse

From: David Woodhouse <[email protected]>

With APICv live on plane-0 vCPUs while their plane-1 twins use software injection, the twins trip the APICv coherency checks: stale vcpu-vs-VM activation state at vcpu_enter_guest(), and timer interrupt injection against a vCPU that is not the running one. Cross-plane event delivery depends on software-injection bookkeeping that hardware acceleration bypasses.

Add APICV_INHIBIT_REASON_PLANES, set it when a plane above 0 is created, and include it in the VMX and AVIC required-inhibit masks; __kvm_set_or_clear_apicv_inhibit() silently ignores a reason the vendor mask does not carry. Drop the inhibit again when the KVM_CREATE_PLANE error path destroys the last nonzero plane of a live VM; at VM teardown plane 0 is already gone and the update is skipped.

---
 arch/x86/kvm/svm/svm.h  |  3 ++-
 arch/x86/kvm/vmx/main.c |  3 ++-
 arch/x86/kvm/x86.c      | 41 +++++++++++++++++++++++++++++++++++++++++
 arch/x86/kvm/x86.h      |  8 ++++++++
 4 files changed, 53 insertions(+), 2 deletions(-)
arch/x86/kvm/svm/svm.h +2 -1
index 2b7663722be6..e7fced399e05 100644
@@ -982,7 +982,8 @@ extern struct kvm_x86_nested_ops svm_nested_ops;
 	BIT(APICV_INHIBIT_REASON_APIC_ID_MODIFIED) |	\
 	BIT(APICV_INHIBIT_REASON_APIC_BASE_MODIFIED) |	\
 	BIT(APICV_INHIBIT_REASON_LOGICAL_ID_ALIASED) |	\
-	BIT(APICV_INHIBIT_REASON_PHYSICAL_ID_TOO_BIG)	\
+	BIT(APICV_INHIBIT_REASON_PHYSICAL_ID_TOO_BIG) |	\
+	BIT(APICV_INHIBIT_REASON_PLANES)		\
 )
 
 bool __init avic_hardware_setup(void);
arch/x86/kvm/vmx/main.c +2 -1
index 2d84910df724..14d21d02bff4 100644
@@ -1004,7 +1004,8 @@ static int vt_handle_exit(struct kvm_vcpu *vcpu,
 	 BIT(APICV_INHIBIT_REASON_BLOCKIRQ) |			\
 	 BIT(APICV_INHIBIT_REASON_PHYSICAL_ID_ALIASED) |	\
 	 BIT(APICV_INHIBIT_REASON_APIC_ID_MODIFIED) |		\
-	 BIT(APICV_INHIBIT_REASON_APIC_BASE_MODIFIED))
+	 BIT(APICV_INHIBIT_REASON_APIC_BASE_MODIFIED) |	\
+	 BIT(APICV_INHIBIT_REASON_PLANES))
 
 struct kvm_x86_ops vt_x86_ops __initdata = {
 	.name = KBUILD_MODNAME,
arch/x86/kvm/x86.c +41 -0

53 lines not on this page · raw message

arch/x86/kvm/x86.h +8 -0
index 498974d38773..fb87ceaa9563 100644
@@ -580,6 +580,13 @@ enum kvm_apicv_inhibit {
 	APICV_INHIBIT_REASON_APIC_ID_MODIFIED,
 	APICV_INHIBIT_REASON_APIC_BASE_MODIFIED,
 
+	/*
+	 * APICv is inhibited when the VM has multiple planes (Hyper-V VTLs).
+	 * Cross-plane event delivery relies on software injection; hardware
+	 * acceleration on one twin would bypass it.
+	 */
+	APICV_INHIBIT_REASON_PLANES,
+
 	/******************************************************/
 	/* INHIBITs that are relevant only to the AMD's AVIC. */
 	/******************************************************/
@@ -637,6 +644,7 @@ enum kvm_apicv_inhibit {
 	__APICV_INHIBIT_REASON(PHYSICAL_ID_ALIASED),	\
 	__APICV_INHIBIT_REASON(APIC_ID_MODIFIED),	\
 	__APICV_INHIBIT_REASON(APIC_BASE_MODIFIED),	\
+	__APICV_INHIBIT_REASON(PLANES),			\
 	__APICV_INHIBIT_REASON(NESTED),			\
 	__APICV_INHIBIT_REASON(IRQWIN),			\
 	__APICV_INHIBIT_REASON(PIT_REINJ),		\
Signature · 7 lines
2.50.1


-- 
kvm-riscv mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/kvm-riscv
maintainer
#156 lore raw

RFC PATCH 8/13 KVM: planes: Enlarge every twin's fpstate for dynamic xfeatures

In reply to David Woodhouse

From: David Woodhouse <[email protected]>

Plane twins share CPUID, so a twin's guest can set the XCR0 bits for a dynamic xfeature such as AMX's XTILEDATA with its own XSETBV. But kvm_check_cpuid() calls fpu_enable_guest_xfd_features() only for the vCPU that KVM_SET_CPUID was issued on, and a twin created afterwards starts with the default fpstate. A twin that then runs AMX code loses its tile data on every fpstate save, because the fpstate it saves into has no room for it. On a host that exposes AMX to the guest, any twin using AMX hits this.

Whether the XRSTORS on the next entry can also leave a previous context's tile registers in place for that twin to read is an open question; it has not been verified.

Enlarge the fpstate of every twin in kvm_check_cpuid(), and give a twin created after KVM_SET_CPUID the dynamic xfeatures of its plane-0 sibling's fpstate.

Only a twin that has not run may have its fpstate enlarged:
fpstate_realloc() hands a guest fpstate that is not loaded a fresh image and copies nothing, so a twin that has run would lose its XSAVE state. kvm_set_cpuid() already accepts only identical CPUID once the vCPU has run, but it checked only the vCPU the ioctl was issued on. With plane 0 held while a higher plane runs, KVM_SET_CPUID2 on plane 0 would pass that check and resize the plane that ran. Since the twins share CPUID, take the identical-CPUID path once any twin has run.

---
 arch/x86/kvm/cpuid.c | 21 +++++++++++++++++++--
 arch/x86/kvm/x86.c   | 17 +++++++++++++++++
 2 files changed, 36 insertions(+), 2 deletions(-)
arch/x86/kvm/cpuid.c +19 -2
index c5fa0551ddd5..971ebd781c6e 100644
@@ -141,7 +141,9 @@ EXPORT_SYMBOL_FOR_KVM_INTERNAL(kvm_find_cpuid_entry2);
 static int kvm_check_cpuid(struct kvm_vcpu *vcpu)
 {
 	struct kvm_cpuid_entry2 *best;
+	struct kvm_vcpu *v;
 	u64 xfeatures;
+	int i, r;
 
 	/*
 	 * The existing code assumes virtual address is 48-bit or 57-bit in the
@@ -168,7 +170,18 @@ static int kvm_check_cpuid(struct kvm_vcpu *vcpu)
 	if (!xfeatures)
 		return 0;
 
-	return fpu_enable_guest_xfd_features(&vcpu->arch.guest_fpu, xfeatures);
+	/*
+	 * The planes share CPUID, so any twin's guest can enable these
+	 * xfeatures in its XCR0: enlarge every twin's fpstate, not just
+	 * this vCPU's.
+	 */
+	vcpu_for_each_plane(vcpu->common, i, v) {
+		r = fpu_enable_guest_xfd_features(&v->arch.guest_fpu, xfeatures);
+		if (r)
+			return r;
+	}
+
+	return 0;
 }
 
 static u32 kvm_apply_cpuid_pv_features_quirk(struct kvm_vcpu *vcpu);
@@ -545,8 +558,12 @@ static int kvm_set_cpuid(struct kvm_vcpu *vcpu, struct kvm_cpuid_entry2 *e2,
 	 * VMMs (e.g. QEMU) reuse vCPU fds for CPU hotplug/unplug and do
 	 * KVM_SET_CPUID{,2} again. To support this legacy behavior, check
 	 * whether the supplied CPUID data is equal to what's already set.
+	 * Plane twins share CPUID, so this applies once any twin has run.
 	 */
-	if (!kvm_can_set_cpuid_and_feature_msrs(vcpu)) {
+	vcpu_for_each_plane(common, i, v) {
+		if (kvm_can_set_cpuid_and_feature_msrs(v))
+			continue;
+
 		r = kvm_cpuid_check_equal(vcpu, e2, nent);
 		if (r)
 			goto err;
arch/x86/kvm/x86.c +17 -0
index 6d3d93e85a65..af28e73e4b9b 100644
@@ -9771,6 +9771,23 @@ int kvm_arch_vcpu_create(struct kvm_vcpu *vcpu)
 	vcpu->arch.hv_root_tdp = INVALID_PAGE;
 #endif
 
+	/*
+	 * A plane twin created after KVM_SET_CPUID shares CPUID that may
+	 * already allow dynamic xfeatures, which KVM_SET_CPUID enabled only
+	 * in the twins that existed at the time.  Give it the dynamic
+	 * xfeatures of its plane-0 sibling's fpstate.
+	 */
+	if (vcpu->plane_level && vcpu->common->vcpus[0]) {
+		u64 dyn = vcpu->common->vcpus[0]->arch.guest_fpu.fpstate->xfeatures &
+			  XFEATURE_MASK_USER_DYNAMIC;
+
+		if (dyn) {
+			r = fpu_enable_guest_xfd_features(&vcpu->arch.guest_fpu, dyn);
+			if (r)
+				goto free_guest_fpu;
+		}
+	}
+
 	/*
 	 * A plane twin created after KVM_SET_CPUID shares CPUID that may
 	 * already advertise Hyper-V.  Allocate the Hyper-V context now so
Signature · 7 lines
2.50.1


-- 
kvm-riscv mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/kvm-riscv
maintainer
#157 lore raw

RFC PATCH 9/13 KVM: x86: Let userspace choose state for plane twins to share

In reply to David Woodhouse

From: David Woodhouse <[email protected]>

Which per-vCPU state the planes of a vCPU share is policy for the VMM. Hyper-V VSM shares XCR0 between VTLs, and whether it shares DR6 depends on the processor, as HvRegisterVsmCapabilities.Dr6Shared reports; a VMM with a different privilege model may want neither. So far the only knob is the plane count, which leaves nothing to key such sharing on but the opt-in itself.

Take a mask of state to share in args[1] of KVM_ENABLE_CAP(KVM_CAP_PLANES), set once together with the count. Unknown bits, and a non-zero args[2] or args[3], fail with -EINVAL. A new KVM_CAP_PLANES_SHARE reports the supported bits on the system fd and the enabled bits on a VM fd, as KVM_CAP_PLANES reports the ceiling and the live count. No bits are defined yet; the following patches add XCR0 and DR6.

---
 Documentation/virt/kvm/api.rst  | 12 +++++++++++-
 arch/x86/include/asm/kvm_host.h |  1 +
 arch/x86/kvm/x86.c              |  8 ++++++++
 arch/x86/kvm/x86.h              | 11 +++++++++++
 include/uapi/linux/kvm.h        |  1 +
 5 files changed, 32 insertions(+), 1 deletion(-)
Documentation/virt/kvm/api.rst +11 -1
index 4196f3fc5718..a6008cb33e99 100644
@@ -9316,7 +9316,8 @@ hpage_2g module parameter is not set to 1, -EINVAL is returned.
 
 :Architectures: x86
 :Target: VM
-:Parameters: args[0] contains the number of planes to make available
+:Parameters: args[0] contains the number of planes to make available,
+             args[1] a mask of state that the planes of a vCPU share
 
 Opts the VM in to using multiple planes, raising the limit on plane ids
 that KVM_CREATE_PLANE accepts to args[0], which must be between 1 and the
@@ -9339,6 +9340,15 @@ This capability must be enabled before any vCPU or extra plane has been
 created, and the chosen count cannot be changed afterwards; otherwise
 -EEXIST is returned.
 
+Each plane of a vCPU normally keeps its own copy of the vCPU's register
+state, apart from the CPUID model, which the planes always share.  args[1]
+selects state that the planes of a vCPU share instead, so that a write
+through one plane is seen by all of them.  KVM_CHECK_EXTENSION
+(KVM_CAP_PLANES_SHARE) returns the bits KVM supports on the system fd, and
+the bits the VM enabled on a VM fd.  Setting an unsupported bit, or a
+non-zero args[2] or args[3], fails with -EINVAL.  Like the count, the mask
+is set once.
+
 8. Other capabilities.
 ======================
 
arch/x86/include/asm/kvm_host.h +1 -0
index 596be8ad7154..99a671ac2386 100644
@@ -1195,6 +1195,7 @@ struct kvm_arch {
 	bool has_protected_eoi;
 	bool has_protected_pmu;
 	bool pre_fault_allowed;
+	u8 plane_share;
 	unsigned int user_max_planes;
 	struct hlist_head *mmu_page_hash;
 	struct list_head active_mmu_pages;
arch/x86/kvm/x86.c +8 -0
index af28e73e4b9b..4d70e111eeb0 100644
@@ -2460,6 +2460,9 @@ int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext)
 	case KVM_CAP_VM_TSC_CONTROL:
 		r = kvm_caps.has_tsc_control;
 		break;
+	case KVM_CAP_PLANES_SHARE:
+		r = kvm ? READ_ONCE(kvm->arch.plane_share) : KVM_PLANE_SHARE_VALID;
+		break;
 	case KVM_CAP_X2APIC_API:
 		r = KVM_X2APIC_API_VALID_FLAGS;
 		if (kvm && !irqchip_split(kvm))
@@ -4174,6 +4177,10 @@ int kvm_vm_ioctl_enable_cap(struct kvm *kvm,
 		r = -EINVAL;
 		if (cap->args[0] < 1 || cap->args[0] > KVM_MAX_USER_PLANES)
 			goto planes_unlock;
+		if (cap->args[1] & ~KVM_PLANE_SHARE_VALID)
+			goto planes_unlock;
+		if (cap->args[2] || cap->args[3])
+			goto planes_unlock;
 		/* Planes are only supported with a split irqchip. */
 		if (!irqchip_split(kvm))
 			goto planes_unlock;
@@ -4195,6 +4202,7 @@ int kvm_vm_ioctl_enable_cap(struct kvm *kvm,
 		if (kvm->created_vcpus || kvm->has_planes ||
 		    kvm->arch.user_max_planes)
 			goto planes_unlock;
+		WRITE_ONCE(kvm->arch.plane_share, cap->args[1]);
 		WRITE_ONCE(kvm->arch.user_max_planes, cap->args[0]);
 		r = 0;
 planes_unlock:
arch/x86/kvm/x86.h +11 -0
index fb87ceaa9563..2f51b476dbd1 100644
@@ -923,4 +923,15 @@ struct kvm_plane *x86_alloc_plane(void);
 void x86_free_plane(struct kvm_plane *plane);
 unsigned kvm_x86_default_max_planes(struct kvm *kvm);
 
+/*
+ * The per-vCPU state that userspace may ask the planes of a vCPU to share,
+ * in args[1] of KVM_ENABLE_CAP(KVM_CAP_PLANES).
+ */
+#define KVM_PLANE_SHARE_VALID	0
+
+static inline bool kvm_plane_shares(struct kvm *kvm, u64 state)
+{
+	return READ_ONCE(kvm->arch.plane_share) & state;
+}
+
 #endif
include/uapi/linux/kvm.h +1 -0
index abf023ed78c4..aef86ef3976f 100644
@@ -1017,6 +1017,7 @@ struct kvm_enable_cap {
 #define KVM_CAP_ARM_PMU_V3_STRICT 251
 #define KVM_CAP_GUEST_MEMFD_MEMORY_ATTRIBUTES 252
 #define KVM_CAP_PLANES 254
+#define KVM_CAP_PLANES_SHARE 256
 
 struct kvm_irq_routing_irqchip {
 	__u32 irqchip;
Signature · 7 lines
2.50.1


-- 
kvm-riscv mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/kvm-riscv
maintainer
#158 lore raw

RFC PATCH 10/13 KVM: planes: Let plane twins share XCR0

In reply to David Woodhouse

From: David Woodhouse <[email protected]>

XCR0/XFEM is shared between VTLs (TLFS 15.4). The planes series keeps arch.xcr0 per vCPU, so an XSETBV in one VTL is invisible in the other, and VTL1 reads a stale XFEM: a feature VTL0 enabled (AVX, for the Windows secure kernel) is not enabled in VTL1's XCR0. The register contents themselves are a separate matter, carried by the code that switches VTLs.

Add KVM_PLANE_SHARE_XCR0 to the KVM_CAP_PLANES share mask. With it set, propagate the validated value to every plane twin in __kvm_set_xcr(), covering both guest XSETBV and the KVM_SET_XCRS ioctl path, and let a twin created later inherit plane 0's current value. The CPUID dynamic-bits dirty marker lives in the shared common state, so the existing single set covers the twins. Update the planes section of api.rst and the kvm_cpuid_apply_runtime() comment, which still listed XCR0 among the per-plane registers.

SEV-SNP VMPLs bank XCR0 per VMSA, and sharing it there would be wrong in two ways: sev_es_sync_from_ghcb() feeds a VMPL's GHCB XCR0 to __kvm_set_xcr(), so a less-privileged VMPL would overwrite its siblings' values (and the MSR-protocol CPUID leaf 0xD sizes would then follow the wrong VMPL); and a pre-launch KVM_SET_XCRS on one twin would change plane 0's VMSA, and with it the launch measurement. An SNP VM cannot set the share mask, since KVM_CAP_PLANES refuses its VM type, so its VMPLs keep XCR0 of their own by construction.

The twins' fpstate sizing does not depend on this bit: the planes share CPUID, so any twin's own XSETBV can enable a dynamic xfeature whether or not XCR0 is shared.

---
 Documentation/virt/kvm/api.rst  | 11 ++++++++---
 arch/x86/include/uapi/asm/kvm.h |  3 +++
 arch/x86/kvm/cpuid.c            |  4 +++-
 arch/x86/kvm/x86.c              | 34 +++++++++++++++++++++++++++++++++
 arch/x86/kvm/x86.h              |  2 +-
 5 files changed, 49 insertions(+), 5 deletions(-)
Documentation/virt/kvm/api.rst +8 -3
index a6008cb33e99..22dc3c793bc4 100644
@@ -374,8 +374,9 @@ The planes of a vCPU share its CPUID model, set through the plane 0 fd with
 KVM_SET_CPUID2.  The CPUID bits that follow a vCPU's own registers (OSXSAVE
 from CR4.OSXSAVE, OSPKE from CR4.PKE, the APIC enable, MWAIT from
 MISC_ENABLE, and the XSAVE area sizes from XCR0 and XSS) reflect the plane
-that executes CPUID, since every plane has these registers of its own.
-KVM_GET_CPUID2 returns them as plane 0 sees them.
+that executes CPUID, since every plane has these registers of its own,
+unless the VM asked the planes to share XCR0 with KVM_PLANE_SHARE_XCR0 (see
+:ref:`KVM_CAP_PLANES`).  KVM_GET_CPUID2 returns them as plane 0 sees them.
 
 4.8 KVM_GET_DIRTY_LOG
 ---------------------
@@ -9347,7 +9348,11 @@ through one plane is seen by all of them.  KVM_CHECK_EXTENSION
 (KVM_CAP_PLANES_SHARE) returns the bits KVM supports on the system fd, and
 the bits the VM enabled on a VM fd.  Setting an unsupported bit, or a
 non-zero args[2] or args[3], fails with -EINVAL.  Like the count, the mask
-is set once.
+is set once.  The bits are:
+
+- KVM_PLANE_SHARE_XCR0: XCR0, whether written by the guest with XSETBV or
+  with KVM_SET_XCRS on any plane's vCPU fd.  A plane vCPU created later
+  starts with plane 0's value.
 
 8. Other capabilities.
 ======================
arch/x86/include/uapi/asm/kvm.h +3 -0
index 00ce265391e1..edec00979acf 100644
@@ -922,6 +922,9 @@ struct kvm_sev_snp_launch_finish {
 #define KVM_X2APIC_ENABLE_SUPPRESS_EOI_BROADCAST	_BITULL(2)
 #define KVM_X2APIC_DISABLE_SUPPRESS_EOI_BROADCAST	_BITULL(3)
 
+/* KVM_CAP_PLANES args[1]: state the planes of a vCPU share */
+#define KVM_PLANE_SHARE_XCR0				_BITULL(0)
+
 struct kvm_hyperv_eventfd {
 	__u32 conn_id;
 	__s32 fd;
arch/x86/kvm/cpuid.c +3 -1
index 971ebd781c6e..aa89d1b6f6c4 100644
@@ -2155,7 +2155,9 @@ get_out_of_range_cpuid_entry(struct kvm_vcpu *vcpu, u32 *fn_ptr, u32 index)
 
 /*
  * The planes share the CPUID table but not CR4, the APIC enable, MISC_ENABLE
- * or XCR0/XSS: the bits that follow those come from the executing plane.
+ * or XSS, nor XCR0 unless userspace asked for KVM_PLANE_SHARE_XCR0, in which
+ * case __kvm_set_xcr() keeps it equal across the twins: the bits that follow
+ * those come from the executing plane.
  */
 static void kvm_cpuid_apply_runtime(struct kvm_vcpu *vcpu, u32 function,
 				    u32 index, u32 *eax, u32 *ebx, u32 *ecx,
arch/x86/kvm/x86.c +34 -0
index 4d70e111eeb0..ceec10d116a9 100644
@@ -799,6 +799,31 @@ int __kvm_set_xcr(struct kvm_vcpu *vcpu, u32 index, u64 xcr)
 
 	vcpu->arch.xcr0 = xcr0;
 
+	/*
+	 * XCR0/XFEM is shared across VTLs (TLFS 15.4): propagate the new
+	 * value to every plane twin so the other VTLs observe it.  This
+	 * covers both guest XSETBV and the KVM_SET_XCRS ioctl path.  The
+	 * CPUID dirty marker below lives in the shared common state, so one
+	 * set covers the twins too.
+	 *
+	 * Only planes that userspace asked to share XCR0 do so.  SEV-SNP
+	 * VMPLs bank it per VMSA, and an SNP VM cannot set the share mask
+	 * (KVM_CAP_PLANES refuses its VM type), so they never fan out.
+	 *
+	 * Skip the fan-out until this vCPU is published in
+	 * common->vcpus[]: the architectural reset of a twin still being
+	 * created must not clobber a live sibling's XCR0 (the new twin
+	 * inherits the shared value instead, in kvm_arch_vcpu_create()).
+	 */
+	if (kvm_plane_shares(vcpu->kvm, KVM_PLANE_SHARE_XCR0) && vcpu->common &&
+	    vcpu->common->vcpus[vcpu->plane_level] == vcpu) {
+		struct kvm_vcpu *twin;
+		int i;
+
+		vcpu_for_each_plane(vcpu->common, i, twin)
+			twin->arch.xcr0 = xcr0;
+	}
+
 	if ((xcr0 ^ old_xcr0) & XFEATURE_MASK_EXTEND)
 		cpuid_set_dirty(vcpu);
 	return 0;
@@ -9820,6 +9845,15 @@ int kvm_arch_vcpu_create(struct kvm_vcpu *vcpu)
 	kvm_vcpu_after_set_cpuid(vcpu);
 	kvm_set_tsc_khz(vcpu, vcpu->kvm->arch.default_tsc_khz);
 	kvm_vcpu_reset(vcpu, false);
+	/*
+	 * With XCR0 shared, a twin created after its plane-0 sibling has run
+	 * (Hyper-V VSM enables VTL1 at runtime) inherits the current shared
+	 * value; the architectural reset value only stands when no sibling
+	 * exists yet, or when the planes keep XCR0 of their own.
+	 */
+	if (kvm_plane_shares(vcpu->kvm, KVM_PLANE_SHARE_XCR0) &&
+	    vcpu->plane_level && vcpu->common->vcpus[0])
+		vcpu->arch.xcr0 = vcpu->common->vcpus[0]->arch.xcr0;
 	kvm_init_mmu(vcpu);
 	vcpu_put(vcpu);
 	return 0;
arch/x86/kvm/x86.h +1 -1
index 2f51b476dbd1..aa8abd219507 100644
@@ -927,7 +927,7 @@ unsigned kvm_x86_default_max_planes(struct kvm *kvm);
  * The per-vCPU state that userspace may ask the planes of a vCPU to share,
  * in args[1] of KVM_ENABLE_CAP(KVM_CAP_PLANES).
  */
-#define KVM_PLANE_SHARE_VALID	0
+#define KVM_PLANE_SHARE_VALID	KVM_PLANE_SHARE_XCR0
 
 static inline bool kvm_plane_shares(struct kvm *kvm, u64 state)
 {
Signature · 7 lines
2.50.1


-- 
kvm-riscv mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/kvm-riscv
maintainer
#159 lore raw

RFC PATCH 11/13 KVM: planes: Let plane twins share DR6

In reply to David Woodhouse

From: David Woodhouse <[email protected]>

The TLFS leaves DR6 sharing between VTLs processor-dependent and tells the guest which way it went in HvRegisterVsmCapabilities.Dr6Shared. KVM cannot know what the VMM advertises there, so make it a share bit and let the VMM keep the two consistent: KVM_PLANE_SHARE_DR6.

The twins keep their own arch.dr6, kept equal whenever no plane is in the guest:

- after each plane's run in the KVM_RUN plane loop, its DR6 is copied to the twins. By then the exit has synced any DR6 the guest owned in hardware back into arch.dr6 (sync_dirty_debug_regs() on VMX and SVM, under KVM_DEBUGREG_WONT_EXIT); otherwise DR accesses are intercepted and arch.dr6 is the only copy. A VTL switch done in the kernel returns through the same loop;
- KVM_SET_DEBUGREGS copies the new value to the twins at once;
- a twin created later starts with plane 0's DR6, as for XCR0.

Converting every arch.dr6 user to a shared location instead would touch the vendor, nested, SMM and exception code for no gain. Writers audited for the copy-after-run rule:

- MOV to DR6 (emulated, or direct under KVM_DEBUGREG_WONT_EXIT), #DB payload delivery (kvm_deliver_exception_payload()), RSM, INIT and nested VMRUN on SVM all write the DR6 of the plane that is running, so the next copy carries them;
- a #DB still pending on a plane that is switched away from updates DR6 when that plane next takes it, which matches a debug exception that has not been delivered yet;
- host-side debugging (KVM_SET_GUEST_DEBUG) reports in kvm_run->debug.arch.dr6 and leaves arch.dr6 alone;
- vCPU ioctls on twins take the shared vCPU mutex, so none of them can observe a plane mid-run.

DR0-DR3 and DR7 stay per plane.

---
 Documentation/virt/kvm/api.rst  |  5 +++++
 arch/x86/include/uapi/asm/kvm.h |  1 +
 arch/x86/kvm/regs.c             |  1 +
 arch/x86/kvm/x86.c              | 25 +++++++++++++++++++++++++
 arch/x86/kvm/x86.h              |  4 +++-
 5 files changed, 35 insertions(+), 1 deletion(-)
Documentation/virt/kvm/api.rst +5 -0
index 22dc3c793bc4..351cc7448fe1 100644
@@ -9353,6 +9353,11 @@ is set once.  The bits are:
 - KVM_PLANE_SHARE_XCR0: XCR0, whether written by the guest with XSETBV or
   with KVM_SET_XCRS on any plane's vCPU fd.  A plane vCPU created later
   starts with plane 0's value.
+- KVM_PLANE_SHARE_DR6: DR6.  A guest write, or a debug exception, in one
+  plane is seen by the others once that plane returns from KVM_RUN or
+  switches to another plane; a KVM_SET_DEBUGREGS on any plane's vCPU fd is
+  seen at once.  A plane vCPU created later starts with plane 0's value.
+  DR0-DR3 and DR7 stay per plane.
 
 8. Other capabilities.
 ======================
arch/x86/include/uapi/asm/kvm.h +1 -0
index edec00979acf..dbfdc1188c20 100644
@@ -924,6 +924,7 @@ struct kvm_sev_snp_launch_finish {
 
 /* KVM_CAP_PLANES args[1]: state the planes of a vCPU share */
 #define KVM_PLANE_SHARE_XCR0				_BITULL(0)
+#define KVM_PLANE_SHARE_DR6				_BITULL(1)
 
 struct kvm_hyperv_eventfd {
 	__u32 conn_id;
arch/x86/kvm/regs.c +1 -0
index 3621f1a47d37..8a856e478095 100644
@@ -876,6 +876,7 @@ int kvm_vcpu_ioctl_x86_set_debugregs(struct kvm_vcpu *vcpu,
 
 	kvm_update_dr0123(vcpu);
 	vcpu->arch.dr6 = dbgregs->dr6;
+	kvm_plane_share_dr6(vcpu);
 	vcpu->arch.dr7 = dbgregs->dr7;
 	kvm_update_dr7(vcpu);
 
arch/x86/kvm/x86.c +25 -0
index ceec10d116a9..7206ee889e9b 100644
@@ -9362,6 +9362,26 @@ static int __kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu, bool held)
 	return r;
 }
 
+/*
+ * With KVM_PLANE_SHARE_DR6, every twin holds the same DR6 whenever no plane
+ * is in the guest.  Copy it from @vcpu to its twins after @vcpu changed it:
+ * after an ioctl write, or after @vcpu ran, by which time its exit has
+ * synced any guest-owned hardware DR6 back into arch.dr6
+ * (sync_dirty_debug_regs() on both vendors).
+ */
+void kvm_plane_share_dr6(struct kvm_vcpu *vcpu)
+{
+	struct kvm_vcpu *twin;
+	int i;
+
+	if (!kvm_plane_shares(vcpu->kvm, KVM_PLANE_SHARE_DR6) ||
+	    vcpu->common->vcpus[vcpu->plane_level] != vcpu)
+		return;
+
+	vcpu_for_each_plane(vcpu->common, i, twin)
+		twin->arch.dr6 = vcpu->arch.dr6;
+}
+
 int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu_plane0)
 {
 	struct kvm_vcpu_common *common = vcpu_plane0->common;
@@ -9374,6 +9394,7 @@ int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu_plane0)
 		common->plane_switch = false;
 
 		ret = __kvm_arch_vcpu_ioctl_run(vcpu, held);
+		kvm_plane_share_dr6(vcpu);
 		if (ret)
 		       break;
 	} while (vcpu_plane0->common->plane_switch);
@@ -9854,6 +9875,10 @@ int kvm_arch_vcpu_create(struct kvm_vcpu *vcpu)
 	if (kvm_plane_shares(vcpu->kvm, KVM_PLANE_SHARE_XCR0) &&
 	    vcpu->plane_level && vcpu->common->vcpus[0])
 		vcpu->arch.xcr0 = vcpu->common->vcpus[0]->arch.xcr0;
+	/* Likewise for a shared DR6. */
+	if (kvm_plane_shares(vcpu->kvm, KVM_PLANE_SHARE_DR6) &&
+	    vcpu->plane_level && vcpu->common->vcpus[0])
+		vcpu->arch.dr6 = vcpu->common->vcpus[0]->arch.dr6;
 	kvm_init_mmu(vcpu);
 	vcpu_put(vcpu);
 	return 0;
arch/x86/kvm/x86.h +3 -1
index aa8abd219507..4da224eb1745 100644
@@ -927,11 +927,13 @@ unsigned kvm_x86_default_max_planes(struct kvm *kvm);
  * The per-vCPU state that userspace may ask the planes of a vCPU to share,
  * in args[1] of KVM_ENABLE_CAP(KVM_CAP_PLANES).
  */
-#define KVM_PLANE_SHARE_VALID	KVM_PLANE_SHARE_XCR0
+#define KVM_PLANE_SHARE_VALID	(KVM_PLANE_SHARE_XCR0 | KVM_PLANE_SHARE_DR6)
 
 static inline bool kvm_plane_shares(struct kvm *kvm, u64 state)
 {
 	return READ_ONCE(kvm->arch.plane_share) & state;
 }
 
+void kvm_plane_share_dr6(struct kvm_vcpu *vcpu);
+
 #endif
Signature · 7 lines
2.50.1


-- 
kvm-riscv mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/kvm-riscv
maintainer
#160 lore raw

RFC PATCH 12/13 KVM: selftests: Check CPUID handling of plane twins

In reply to David Woodhouse

From: David Woodhouse <[email protected]>

Two checks of how plane twins follow the CPUID they share.

A plane vCPU created after KVM_SET_CPUID gets its Hyper-V context in kvm_arch_vcpu_create(), before its vCPU index is assigned, and used to latch a VP index of 0xFFFFFFFF. Create two vCPUs, give them a Hyper-V CPUID, then create their plane-1 twins and check that HV_X64_MSR_VP_INDEX on each twin reads its vCPU index.

Once a twin has run, KVM_SET_CPUID2 must accept only the CPUID already set, even on a plane 0 that has never run. Run plane 1 with plane 0 held (a real-mode OUT, then retired), and check that identical CPUID is accepted and that CPUID with XTILE added to leaf 0xD fails with EINVAL. Before kvm_set_cpuid() checked every twin, that call went on to the fpstate code, which failed it with EPERM, or resized the twin that ran if the process had AMX permission.

---
 tools/testing/selftests/kvm/x86/planes_test.c | 125 +++++++++++++++++-
 1 file changed, 122 insertions(+), 3 deletions(-)
tools/testing/selftests/kvm/x86/planes_test.c +122 -3
index 968f1b099b8e..dd3c251f0b7a 100644
@@ -1,16 +1,19 @@
 // SPDX-License-Identifier: GPL-2.0-only
 /*
  * Test the KVM_CAP_PLANES opt-in and the plane lifecycle uAPI: plane
- * creation bounds, plane vCPU creation, and the independence of
- * per-plane vCPU register state.
+ * creation bounds, plane vCPU creation, the independence of
+ * per-plane vCPU register state, the Hyper-V VP index of a plane
+ * vCPU created after KVM_SET_CPUID, and KVM_SET_CPUID2 once a twin
+ * has run.
  */
 #include <errno.h>
 #include <string.h>
 
 #include "kvm_util.h"
 #include "processor.h"
+#include "hyperv.h"
 
-/* Where the run-state test puts plane 0's one instruction. */
+/* Where the tests that enter a plane put its one instruction. */
 #define CODE_GPA	0x1000
 
 /*
@@ -317,6 +320,120 @@ static void test_plane_lifecycle(void)
 	kvm_vm_free(vm);
 }
 
+/*
+ * A plane vCPU created after KVM_SET_CPUID gets its Hyper-V context at
+ * creation, and its default VP index must be its vCPU index like any
+ * other vCPU's, not the -1 the vCPU carries until it is published.
+ */
+static void test_late_twin_vp_index(void)
+{
+	struct kvm_plane_vcpu *twin;
+	struct kvm_vcpu *vcpu[2];
+	struct kvm_plane *plane;
+	struct kvm_vm *vm;
+	int i;
+
+	if (!kvm_has_cap(KVM_CAP_SYS_HYPERV_CPUID)) {
+		print_skip("KVM_CAP_SYS_HYPERV_CPUID not supported");
+		return;
+	}
+
+	vm = vm_create_barebones_irqchip(true);
+	vm_enable_cap(vm, KVM_CAP_PLANES, 2);
+	plane = vm_plane_add(vm, 1);
+
+	for (i = 0; i < 2; i++) {
+		vcpu[i] = __vm_vcpu_add(vm, i);
+		vcpu_set_hv_cpuid(vcpu[i]);
+	}
+
+	for (i = 0; i < 2; i++) {
+		twin = __vm_plane_vcpu_add(vcpu[i], plane);
+		TEST_ASSERT_EQ(plane_vcpu_get_msr(twin, HV_X64_MSR_VP_INDEX), i);
+	}
+
+	kvm_vm_free(vm);
+}
+
+/*
+ * CPUID is shared between plane twins, so once any twin has run,
+ * KVM_SET_CPUID2 on plane 0 only accepts the CPUID already set, even
+ * while plane 0 itself has never run.  Without that, an enlarged
+ * dynamic xfeature would resize the fpstate of the twin that ran.
+ * Plane 1 runs here with plane 0 held: a real-mode OUT at CODE_GPA,
+ * which is then retired.
+ */
+static void test_cpuid_after_twin_ran(void)
+{
+	static const u8 out_insn[] = { 0xe6, 0x10 };	/* out %al, $0x10 */
+	u64 hold = KVM_PLANE_RUN_STATE_USER_HOLD, none = 0;
+	struct kvm_cpuid_entry2 *leaf_d = NULL;
+	struct kvm_plane_vcpu *vcpu1;
+	struct kvm_cpuid2 *enlarged;
+	struct kvm_plane *plane;
+	struct kvm_sregs sregs;
+	struct kvm_vcpu *vcpu0;
+	struct kvm_regs regs;
+	struct kvm_vm *vm;
+	u32 i;
+
+	vm = vm_create_barebones_irqchip(true);
+	vm_enable_cap(vm, KVM_CAP_PLANES, 2);
+	plane = vm_plane_add(vm, 1);
+	vcpu0 = __vm_vcpu_add(vm, 0);
+	vcpu1 = __vm_plane_vcpu_add(vcpu0, plane);
+	vcpu_init_cpuid(vcpu0, kvm_get_supported_cpuid());
+
+	vm_userspace_mem_region_add(vm, VM_MEM_SRC_ANONYMOUS, CODE_GPA, 1, 1, 0);
+	memcpy(addr_gpa2hva(vm, CODE_GPA), out_insn, sizeof(out_insn));
+
+	plane_vcpu_ioctl(vcpu1, KVM_GET_SREGS, &sregs);
+	sregs.cs.selector = 0;
+	sregs.cs.base = 0;
+	plane_vcpu_ioctl(vcpu1, KVM_SET_SREGS, &sregs);
+	plane_vcpu_ioctl(vcpu1, KVM_GET_REGS, &regs);
+	regs.rip = CODE_GPA;
+	plane_vcpu_ioctl(vcpu1, KVM_SET_REGS, &regs);
+
+	vcpu_ioctl(vcpu0, KVM_SET_PLANE_RUN_STATE, &hold);
+	plane_vcpu_ioctl(vcpu1, KVM_SET_PLANE_RUN_STATE, &none);
+	TEST_ASSERT_EQ(__vcpu_run(vcpu0), 0);
+	TEST_ASSERT_EQ(vcpu0->run->exit_reason, KVM_EXIT_IO);
+	TEST_ASSERT_EQ(vcpu0->run->io.port, 0x10);
+
+	/* Retire the OUT without entering again. */
+	vcpu0->run->immediate_exit = 1;
+	TEST_ASSERT_EQ(__vcpu_run(vcpu0), -1);
+	TEST_ASSERT_EQ(errno, EINTR);
+	vcpu0->run->immediate_exit = 0;
+
+	/* Identical CPUID is still accepted. */
+	TEST_ASSERT_EQ(__vcpu_ioctl(vcpu0, KVM_SET_CPUID2, vcpu0->cpuid), 0);
+
+	/*
+	 * An enlarged dynamic xfeature is refused by the has-run check
+	 * (EINVAL), before the fpstate code could fail it for lack of
+	 * permission (EPERM).
+	 */
+	enlarged = allocate_kvm_cpuid2(vcpu0->cpuid->nent);
+	memcpy(enlarged, vcpu0->cpuid, kvm_cpuid2_size(vcpu0->cpuid->nent));
+	for (i = 0; i < enlarged->nent; i++) {
+		if (enlarged->entries[i].function == 0xd &&
+		    enlarged->entries[i].index == 0)
+			leaf_d = &enlarged->entries[i];
+	}
+	if (leaf_d) {
+		leaf_d->eax |= XFEATURE_MASK_XTILE;
+		TEST_ASSERT_EQ(__vcpu_ioctl(vcpu0, KVM_SET_CPUID2, enlarged), -1);
+		TEST_ASSERT_EQ(errno, EINVAL);
+	} else {
+		print_skip("no CPUID leaf 0xD, enlarged-xfeature check");
+	}
+	free(enlarged);
+
+	kvm_vm_free(vm);
+}
+
 int main(int argc, char *argv[])
 {
 	TEST_REQUIRE(kvm_has_cap(KVM_CAP_PLANES));
@@ -326,6 +443,8 @@ int main(int argc, char *argv[])
 	test_enable_cap_bounds();
 	test_enable_cap_too_late();
 	test_plane_lifecycle();
+	test_late_twin_vp_index();
+	test_cpuid_after_twin_ran();
 
 	return 0;
 }
Signature · 7 lines
2.50.1


-- 
kvm-riscv mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/kvm-riscv
maintainer
#161 lore raw

RFC PATCH 13/13 KVM: selftests: Cover plane state sharing

In reply to David Woodhouse

From: David Woodhouse <[email protected]>

Check the KVM_CAP_PLANES share mask: KVM_CAP_PLANES_SHARE reports XCR0|DR6 on the system fd, 0 on a VM fd before the opt-in and the enabled bits after it; an unknown bit and a non-zero args[2] fail with EINVAL, and a second opt-in with another mask fails with EEXIST.

Then, with no sharing and with XCR0 and DR6 shared, write XCR0 and DR6 through plane 0 before creating a plane-1 twin, and check that the twin inherits them only when shared; write them through plane 1 and check plane 0; and have plane 1 write DR6 with MOV (real mode, plane 0 held) and check that plane 0 sees the guest's value only when DR6 is shared.

---
 tools/testing/selftests/kvm/x86/planes_test.c | 159 +++++++++++++++++-
 1 file changed, 157 insertions(+), 2 deletions(-)
tools/testing/selftests/kvm/x86/planes_test.c +157 -2
index dd3c251f0b7a..02242cc7d576 100644
@@ -3,8 +3,8 @@
  * Test the KVM_CAP_PLANES opt-in and the plane lifecycle uAPI: plane
  * creation bounds, plane vCPU creation, the independence of
  * per-plane vCPU register state, the Hyper-V VP index of a plane
- * vCPU created after KVM_SET_CPUID, and KVM_SET_CPUID2 once a twin
- * has run.
+ * vCPU created after KVM_SET_CPUID, KVM_SET_CPUID2 once a twin has
+ * run, and the KVM_CAP_PLANES share mask.
  */
 #include <errno.h>
 #include <string.h>
@@ -434,6 +434,154 @@ static void test_cpuid_after_twin_ran(void)
 	kvm_vm_free(vm);
 }
 
+static int __enable_planes_share(struct kvm_vm *vm, u64 count, u64 share, u64 arg2)
+{
+	struct kvm_enable_cap cap = {
+		.cap = KVM_CAP_PLANES,
+		.args = { count, share, arg2 },
+	};
+
+	return __vm_ioctl(vm, KVM_ENABLE_CAP, &cap);
+}
+
+/*
+ * KVM_CAP_PLANES_SHARE reports the supported share bits on the system fd
+ * and the enabled ones on a VM fd.  Unknown bits and a non-zero args[2]
+ * are refused, and like the count the mask is set once.
+ */
+static void test_share_opt_in(void)
+{
+	u64 defined = KVM_PLANE_SHARE_XCR0 | KVM_PLANE_SHARE_DR6;
+	u64 unknown = ~defined & -~defined;
+	struct kvm_vm *vm;
+
+	TEST_ASSERT_EQ(kvm_check_cap(KVM_CAP_PLANES_SHARE), defined);
+
+	vm = vm_create_barebones_irqchip(true);
+	TEST_ASSERT_EQ(vm_check_cap(vm, KVM_CAP_PLANES_SHARE), 0);
+	TEST_ASSERT_EQ(__enable_planes_share(vm, 2, unknown, 0), -1);
+	TEST_ASSERT_EQ(errno, EINVAL);
+	TEST_ASSERT_EQ(__enable_planes_share(vm, 2, 0, 1), -1);
+	TEST_ASSERT_EQ(errno, EINVAL);
+	TEST_ASSERT_EQ(vm_check_cap(vm, KVM_CAP_PLANES), 1);
+
+	TEST_ASSERT_EQ(__enable_planes_share(vm, 2, KVM_PLANE_SHARE_DR6, 0), 0);
+	TEST_ASSERT_EQ(vm_check_cap(vm, KVM_CAP_PLANES_SHARE), KVM_PLANE_SHARE_DR6);
+	TEST_ASSERT_EQ(__enable_planes_share(vm, 2, KVM_PLANE_SHARE_XCR0, 0), -1);
+	TEST_ASSERT_EQ(errno, EEXIST);
+	TEST_ASSERT_EQ(vm_check_cap(vm, KVM_CAP_PLANES_SHARE), KVM_PLANE_SHARE_DR6);
+	kvm_vm_free(vm);
+}
+
+static u64 get_xcr0(int fd)
+{
+	struct kvm_xcrs xcrs = {};
+
+	TEST_ASSERT_EQ(ioctl(fd, KVM_GET_XCRS, &xcrs), 0);
+	TEST_ASSERT_EQ(xcrs.nr_xcrs, 1);
+	return xcrs.xcrs[0].value;
+}
+
+static void set_xcr0(int fd, u64 xcr0)
+{
+	struct kvm_xcrs xcrs = { .nr_xcrs = 1, .xcrs[0] = { .xcr = 0, .value = xcr0 } };
+
+	TEST_ASSERT_EQ(ioctl(fd, KVM_SET_XCRS, &xcrs), 0);
+}
+
+static u64 get_dr6(int fd)
+{
+	struct kvm_debugregs dbg;
+
+	TEST_ASSERT_EQ(ioctl(fd, KVM_GET_DEBUGREGS, &dbg), 0);
+	return dbg.dr6;
+}
+
+static void set_dr6(int fd, u64 dr6)
+{
+	struct kvm_debugregs dbg;
+
+	TEST_ASSERT_EQ(ioctl(fd, KVM_GET_DEBUGREGS, &dbg), 0);
+	dbg.dr6 = dr6;
+	TEST_ASSERT_EQ(ioctl(fd, KVM_SET_DEBUGREGS, &dbg), 0);
+}
+
+/*
+ * With @share, a write through one plane is seen through the other, and a
+ * plane vCPU created later starts with plane 0's value; without it each
+ * plane keeps its own.  Checked for XCR0 and DR6 by ioctl, and for DR6 by
+ * a guest MOV in plane 1 (real mode, at CODE_GPA, with plane 0 held).
+ */
+static void test_share_state(u64 share)
+{
+	static const u8 mov_dr6[] = {
+		0x66, 0xb8, 0xf4, 0x0f, 0xff, 0xff,	/* mov $0xffff0ff4, %eax */
+		0x0f, 0x23, 0xf0,			/* mov %eax, %dr6 */
+		0xe6, 0x10,				/* out %al, $0x10 */
+	};
+	bool xcr0_shared = share & KVM_PLANE_SHARE_XCR0;
+	bool dr6_shared = share & KVM_PLANE_SHARE_DR6;
+	u64 hold = KVM_PLANE_RUN_STATE_USER_HOLD, none = 0;
+	struct kvm_plane_vcpu *vcpu1;
+	struct kvm_plane *plane;
+	struct kvm_sregs sregs;
+	struct kvm_vcpu *vcpu0;
+	struct kvm_regs regs;
+	struct kvm_vm *vm;
+
+	vm = vm_create_barebones_irqchip(true);
+	TEST_ASSERT_EQ(__enable_planes_share(vm, 2, share, 0), 0);
+	plane = vm_plane_add(vm, 1);
+	vcpu0 = __vm_vcpu_add(vm, 0);
+	vcpu_init_cpuid(vcpu0, kvm_get_supported_cpuid());
+
+	/* Plane 0 first, then a plane-1 twin that inherits or not. */
+	set_xcr0(vcpu0->fd, XFEATURE_MASK_FP | XFEATURE_MASK_SSE);
+	set_dr6(vcpu0->fd, 0xffff0ff1);
+	vcpu1 = __vm_plane_vcpu_add(vcpu0, plane);
+	TEST_ASSERT_EQ(get_xcr0(vcpu1->fd), xcr0_shared ?
+		       XFEATURE_MASK_FP | XFEATURE_MASK_SSE : XFEATURE_MASK_FP);
+	TEST_ASSERT_EQ(get_dr6(vcpu1->fd), dr6_shared ? 0xffff0ff1 : 0xffff0ff0);
+
+	/* A write through plane 1. */
+	set_xcr0(vcpu1->fd, XFEATURE_MASK_FP);
+	set_dr6(vcpu1->fd, 0xffff0ff2);
+	TEST_ASSERT_EQ(get_xcr0(vcpu0->fd), xcr0_shared ?
+		       XFEATURE_MASK_FP : XFEATURE_MASK_FP | XFEATURE_MASK_SSE);
+	TEST_ASSERT_EQ(get_dr6(vcpu0->fd), dr6_shared ? 0xffff0ff2 : 0xffff0ff1);
+
+	/* A guest write of DR6 in plane 1. */
+	vm_userspace_mem_region_add(vm, VM_MEM_SRC_ANONYMOUS, CODE_GPA, 1, 1, 0);
+	memcpy(addr_gpa2hva(vm, CODE_GPA), mov_dr6, sizeof(mov_dr6));
+	plane_vcpu_ioctl(vcpu1, KVM_GET_SREGS, &sregs);
+	sregs.cs.selector = 0;
+	sregs.cs.base = 0;
+	plane_vcpu_ioctl(vcpu1, KVM_SET_SREGS, &sregs);
+	plane_vcpu_ioctl(vcpu1, KVM_GET_REGS, &regs);
+	regs.rip = CODE_GPA;
+	plane_vcpu_ioctl(vcpu1, KVM_SET_REGS, &regs);
+
+	vcpu_ioctl(vcpu0, KVM_SET_PLANE_RUN_STATE, &hold);
+	plane_vcpu_ioctl(vcpu1, KVM_SET_PLANE_RUN_STATE, &none);
+	TEST_ASSERT_EQ(__vcpu_run(vcpu0), 0);
+	TEST_ASSERT_EQ(vcpu0->run->exit_reason, KVM_EXIT_IO);
+	TEST_ASSERT_EQ(vcpu0->run->io.port, 0x10);
+	vcpu0->run->immediate_exit = 1;
+	TEST_ASSERT_EQ(__vcpu_run(vcpu0), -1);
+	TEST_ASSERT_EQ(errno, EINTR);
+	vcpu0->run->immediate_exit = 0;
+
+	TEST_ASSERT(get_dr6(vcpu1->fd) & BIT(2),
+		    "plane 1 DR6 0x%lx lacks the B2 bit the guest wrote",
+		    (unsigned long)get_dr6(vcpu1->fd));
+	if (dr6_shared)
+		TEST_ASSERT_EQ(get_dr6(vcpu0->fd), get_dr6(vcpu1->fd));
+	else
+		TEST_ASSERT_EQ(get_dr6(vcpu0->fd), 0xffff0ff1);
+
+	kvm_vm_free(vm);
+}
+
 int main(int argc, char *argv[])
 {
 	TEST_REQUIRE(kvm_has_cap(KVM_CAP_PLANES));
@@ -445,6 +593,13 @@ int main(int argc, char *argv[])
 	test_plane_lifecycle();
 	test_late_twin_vp_index();
 	test_cpuid_after_twin_ran();
+	test_share_opt_in();
+	if (kvm_cpu_has(X86_FEATURE_XSAVE)) {
+		test_share_state(0);
+		test_share_state(KVM_PLANE_SHARE_XCR0 | KVM_PLANE_SHARE_DR6);
+	} else {
+		print_skip("XSAVE not supported, plane state sharing");
+	}
 
 	return 0;
 }
Signature · 1 line
2.50.1