Skip to contentSkip to conversation

KVM: x86: Hyper-V SEND_IPI fix and partial testcase

PATCH Superseded Sean Christopherson 2 participants 12 messages Jan 13, 2025 – Jan 17, 2025

Subsystems (5)
x86 KVM Kselftest KVM x86 +1 more subsystems

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Sean Christopherson

author maintainer
#0 lore raw

cover KVM: x86: Hyper-V SEND_IPI fix and partial testcase

Fix a NULL pointer deref due to exposing Hyper-V enlightments to a guest without an in-kernel local APIC (found by syzkaller, highly unlikely to affect any "real" VMMs). Expand the Hyper-V CPUID test to verify that KVM doesn't incorrectly advertise support.

Sean Christopherson (5):
KVM: x86: Reject Hyper-V's SEND_IPI hypercalls if local APIC isn't
in-kernel
KVM: selftests: Mark test_hv_cpuid_e2big() static in Hyper-V CPUID
test
KVM: selftests: Explicitly free CPUID array at end of Hyper-V CPUID
test
KVM: selftests: Manage CPUID array in Hyper-V CPUID test's core helper
KVM: selftests: Add CPUID tests for Hyper-V features that need
in-kernel APIC

 arch/x86/kvm/hyperv.c                         |  6 ++-
 .../selftests/kvm/x86_64/hyperv_cpuid.c       | 41 ++++++++++++-------
 2 files changed, 31 insertions(+), 16 deletions(-)

base-commit: a5546c2f0dc4f84727a4bb8a91633917929735f5

Signature · 1 line
2.47.1.688.g23fc6f90ad-goog
author maintainer
#1 lore raw

PATCH 1/5 KVM: x86: Reject Hyper-V's SEND_IPI hypercalls if local APIC isn't in-kernel

Advertise support for Hyper-V's SEND_IPI and SEND_IPI_EX hypercalls if and only if the local API is emulated/virtualized by KVM, and explicitly reject said hypercalls if the local APIC is emulated in userspace, i.e. don't rely on userspace to opt-in to KVM_CAP_HYPERV_ENFORCE_CPUID.

Rejecting SEND_IPI and SEND_IPI_EX fixes a NULL-pointer dereference if Hyper-V enlightenments are exposed to the guest without an in-kernel local APIC:

  dump_stack+0xbe/0xfd
  __kasan_report.cold+0x34/0x84
  kasan_report+0x3a/0x50
  __apic_accept_irq+0x3a/0x5c0
  kvm_hv_send_ipi.isra.0+0x34e/0x820
  kvm_hv_hypercall+0x8d9/0x9d0
  kvm_emulate_hypercall+0x506/0x7e0
  __vmx_handle_exit+0x283/0xb60
  vmx_handle_exit+0x1d/0xd0
  vcpu_enter_guest+0x16b0/0x24c0
  vcpu_run+0xc0/0x550
  kvm_arch_vcpu_ioctl_run+0x170/0x6d0
  kvm_vcpu_ioctl+0x413/0xb20
  __se_sys_ioctl+0x111/0x160
  do_syscal1_64+0x30/0x40
  entry_SYSCALL_64_after_hwframe+0x67/0xd1

Note, checking the sending vCPU is sufficient, as the per-VM irqchip_mode can't be modified after vCPUs are created, i.e. if one vCPU has an in-kernel local APIC, then all vCPUs have an in-kernel local APIC.

Fixes 214ff83d4473 ("KVM: x86: hyperv: implement PV IPI send hypercalls")
Fixes 2bc39970e932 ("x86/kvm/hyper-v: Introduce KVM_GET_SUPPORTED_HV_CPUID")
---
 arch/x86/kvm/hyperv.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
arch/x86/kvm/hyperv.c +5 -1
index 4f0a94346d00..44c88537448c 100644
@@ -2226,6 +2226,9 @@ static u64 kvm_hv_send_ipi(struct kvm_vcpu *vcpu, struct kvm_hv_hcall *hc)
 	u32 vector;
 	bool all_cpus;
 
+	if (!lapic_in_kernel(vcpu))
+		return HV_STATUS_INVALID_HYPERCALL_INPUT;
+
 	if (hc->code == HVCALL_SEND_IPI) {
 		if (!hc->fast) {
 			if (unlikely(kvm_read_guest(kvm, hc->ingpa, &send_ipi,
@@ -2852,7 +2855,8 @@ int kvm_get_hv_cpuid(struct kvm_vcpu *vcpu, struct kvm_cpuid2 *cpuid,
 			ent->eax |= HV_X64_REMOTE_TLB_FLUSH_RECOMMENDED;
 			ent->eax |= HV_X64_APIC_ACCESS_RECOMMENDED;
 			ent->eax |= HV_X64_RELAXED_TIMING_RECOMMENDED;
-			ent->eax |= HV_X64_CLUSTER_IPI_RECOMMENDED;
+			if (!vcpu || lapic_in_kernel(vcpu))
+				ent->eax |= HV_X64_CLUSTER_IPI_RECOMMENDED;
 			ent->eax |= HV_X64_EX_PROCESSOR_MASKS_RECOMMENDED;
 			if (evmcs_ver)
 				ent->eax |= HV_X64_ENLIGHTENED_VMCS_RECOMMENDED;
Signature · 1 line
2.47.1.688.g23fc6f90ad-goog
author maintainer
#2 lore raw

Re: [PATCH 1/5] KVM: x86: Reject Hyper-V's SEND_IPI hypercalls if local APIC isn't in-kernel

Quoted 26 lines · Sean Christopherson

Advertise support for Hyper-V's SEND_IPI and SEND_IPI_EX hypercalls if and only if the local API is emulated/virtualized by KVM, and explicitly reject said hypercalls if the local APIC is emulated in userspace, i.e. don't rely on userspace to opt-in to KVM_CAP_HYPERV_ENFORCE_CPUID.

Rejecting SEND_IPI and SEND_IPI_EX fixes a NULL-pointer dereference if Hyper-V enlightenments are exposed to the guest without an in-kernel local APIC:

  dump_stack+0xbe/0xfd
  __kasan_report.cold+0x34/0x84
  kasan_report+0x3a/0x50
  __apic_accept_irq+0x3a/0x5c0
  kvm_hv_send_ipi.isra.0+0x34e/0x820
  kvm_hv_hypercall+0x8d9/0x9d0
  kvm_emulate_hypercall+0x506/0x7e0
  __vmx_handle_exit+0x283/0xb60
  vmx_handle_exit+0x1d/0xd0
  vcpu_enter_guest+0x16b0/0x24c0
  vcpu_run+0xc0/0x550
  kvm_arch_vcpu_ioctl_run+0x170/0x6d0
  kvm_vcpu_ioctl+0x413/0xb20
  __se_sys_ioctl+0x111/0x160
  do_syscal1_64+0x30/0x40
  entry_SYSCALL_64_after_hwframe+0x67/0xd1
Quoted 8 lines

Note, checking the sending vCPU is sufficient, as the per-VM irqchip_mode can't be modified after vCPUs are created, i.e. if one vCPU has an in-kernel local APIC, then all vCPUs have an in-kernel local APIC.

Fixes 214ff83d4473 ("KVM: x86: hyperv: implement PV IPI send hypercalls")
Fixes 2bc39970e932 ("x86/kvm/hyper-v: Introduce KVM_GET_SUPPORTED_HV_CPUID")

Argh, I missed the ".org".

Paolo, if you end up applying this instead of me, can you fixup the email?

maintainer Reviewed-by on v1 1/5
#3 lore raw

Re: [PATCH 1/5] KVM: x86: Reject Hyper-V's SEND_IPI hypercalls if local APIC isn't in-kernel

In reply to Sean Christopherson
Quoted 26 lines · Sean Christopherson

Advertise support for Hyper-V's SEND_IPI and SEND_IPI_EX hypercalls if and only if the local API is emulated/virtualized by KVM, and explicitly reject said hypercalls if the local APIC is emulated in userspace, i.e. don't rely on userspace to opt-in to KVM_CAP_HYPERV_ENFORCE_CPUID.

Rejecting SEND_IPI and SEND_IPI_EX fixes a NULL-pointer dereference if Hyper-V enlightenments are exposed to the guest without an in-kernel local APIC:

  dump_stack+0xbe/0xfd
  __kasan_report.cold+0x34/0x84
  kasan_report+0x3a/0x50
  __apic_accept_irq+0x3a/0x5c0
  kvm_hv_send_ipi.isra.0+0x34e/0x820
  kvm_hv_hypercall+0x8d9/0x9d0
  kvm_emulate_hypercall+0x506/0x7e0
  __vmx_handle_exit+0x283/0xb60
  vmx_handle_exit+0x1d/0xd0
  vcpu_enter_guest+0x16b0/0x24c0
  vcpu_run+0xc0/0x550
  kvm_arch_vcpu_ioctl_run+0x170/0x6d0
  kvm_vcpu_ioctl+0x413/0xb20
  __se_sys_ioctl+0x111/0x160
  do_syscal1_64+0x30/0x40
  entry_SYSCALL_64_after_hwframe+0x67/0xd1
Quoted 8 lines

Note, checking the sending vCPU is sufficient, as the per-VM irqchip_mode can't be modified after vCPUs are created, i.e. if one vCPU has an in-kernel local APIC, then all vCPUs have an in-kernel local APIC.

Fixes 214ff83d4473 ("KVM: x86: hyperv: implement PV IPI send hypercalls")
Fixes 2bc39970e932 ("x86/kvm/hyper-v: Introduce KVM_GET_SUPPORTED_HV_CPUID")

.org, as mentioned already

Quoted 20 lines
---
 arch/x86/kvm/hyperv.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
arch/x86/kvm/hyperv.c +3 -0
index 4f0a94346d00..44c88537448c 100644
@@ -2226,6 +2226,9 @@ static u64 kvm_hv_send_ipi(struct kvm_vcpu *vcpu, struct kvm_hv_hcall *hc)
 	u32 vector;
 	bool all_cpus;
 
+	if (!lapic_in_kernel(vcpu))
+		return HV_STATUS_INVALID_HYPERCALL_INPUT;
+
 	if (hc->code == HVCALL_SEND_IPI) {
 		if (!hc->fast) {
 			if (unlikely(kvm_read_guest(kvm, hc->ingpa, &send_ipi,

arch/x86/kvm/hyperv.c int kvm_get_hv_cpuid(struct kvm_vcpu *vcpu, struct kvm_cpuid2 *cpuid, · line 2862

Quoted 9 lines
arch/x86/kvm/hyperv.c +2 -1
@@ -2852,7 +2855,8 @@ int kvm_get_hv_cpuid(struct kvm_vcpu *vcpu, struct kvm_cpuid2 *cpuid,
 			ent->eax |= HV_X64_REMOTE_TLB_FLUSH_RECOMMENDED;
 			ent->eax |= HV_X64_APIC_ACCESS_RECOMMENDED;
 			ent->eax |= HV_X64_RELAXED_TIMING_RECOMMENDED;
-			ent->eax |= HV_X64_CLUSTER_IPI_RECOMMENDED;
+			if (!vcpu || lapic_in_kernel(vcpu))
+				ent->eax |= HV_X64_CLUSTER_IPI_RECOMMENDED;
 			ent->eax |= HV_X64_EX_PROCESSOR_MASKS_RECOMMENDED;
 			if (evmcs_ver)
 				ent->eax |= HV_X64_ENLIGHTENED_VMCS_RECOMMENDED;
Signature · 1 line
Vitaly
author maintainer
#4 lore raw

PATCH 2/5 KVM: selftests: Mark test_hv_cpuid_e2big() static in Hyper-V CPUID test

In reply to Sean Christopherson

Make the Hyper-V CPUID test's local helper test_hv_cpuid_e2big() static, it's not used outside of the test (and isn't intended to be).

---
 tools/testing/selftests/kvm/x86_64/hyperv_cpuid.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
tools/testing/selftests/kvm/x86_64/hyperv_cpuid.c +1 -1
index 4f5881d4ef66..9a0fcc713350 100644
@@ -111,7 +111,7 @@ static void test_hv_cpuid(const struct kvm_cpuid2 *hv_cpuid_entries,
 	}
 }
 
-void test_hv_cpuid_e2big(struct kvm_vm *vm, struct kvm_vcpu *vcpu)
+static void test_hv_cpuid_e2big(struct kvm_vm *vm, struct kvm_vcpu *vcpu)
 {
 	static struct kvm_cpuid2 cpuid = {.nent = 0};
 	int ret;
Signature · 1 line
2.47.1.688.g23fc6f90ad-goog
maintainer Reviewed-by on v1 2/5
#5 lore raw

Re: [PATCH 2/5] KVM: selftests: Mark test_hv_cpuid_e2big() static in Hyper-V CPUID test

Quoted 14 lines · Sean Christopherson

Make the Hyper-V CPUID test's local helper test_hv_cpuid_e2big() static, it's not used outside of the test (and isn't intended to be).

---
 tools/testing/selftests/kvm/x86_64/hyperv_cpuid.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
tools/testing/selftests/kvm/x86_64/hyperv_cpuid.c +0 -0
index 4f5881d4ef66..9a0fcc713350 100644

tools/testing/selftests/kvm/x86_64/hyperv_cpuid.c static void test_hv_cpuid(const struct kvm_cpuid2 *hv_cpuid_entries, · line 117

Quoted 8 lines
tools/testing/selftests/kvm/x86_64/hyperv_cpuid.c +1 -1
@@ -111,7 +111,7 @@ static void test_hv_cpuid(const struct kvm_cpuid2 *hv_cpuid_entries,
 	}
 }
 
-void test_hv_cpuid_e2big(struct kvm_vm *vm, struct kvm_vcpu *vcpu)
+static void test_hv_cpuid_e2big(struct kvm_vm *vm, struct kvm_vcpu *vcpu)
 {
 	static struct kvm_cpuid2 cpuid = {.nent = 0};
 	int ret;
Signature · 1 line
Vitaly
author maintainer
#6 lore raw

PATCH 3/5 KVM: selftests: Explicitly free CPUID array at end of Hyper-V CPUID test

In reply to Sean Christopherson

Explicitly free the array of CPUID entries at the end of the Hyper-V CPUID test, mainly in anticipation of moving management of the array into the main test helper.

---
 tools/testing/selftests/kvm/x86_64/hyperv_cpuid.c | 1 +
 1 file changed, 1 insertion(+)
tools/testing/selftests/kvm/x86_64/hyperv_cpuid.c +1 -0
index 9a0fcc713350..09f9874d7705 100644
@@ -164,6 +164,7 @@ int main(int argc, char *argv[])
 
 	hv_cpuid_entries = kvm_get_supported_hv_cpuid();
 	test_hv_cpuid(hv_cpuid_entries, kvm_cpu_has(X86_FEATURE_VMX));
+	free((void *)hv_cpuid_entries);
 
 out:
 	kvm_vm_free(vm);
Signature · 1 line
2.47.1.688.g23fc6f90ad-goog
maintainer
#7 lore raw

Re: [PATCH 3/5] KVM: selftests: Explicitly free CPUID array at end of Hyper-V CPUID test

Quoted 15 lines · Sean Christopherson

Explicitly free the array of CPUID entries at the end of the Hyper-V CPUID test, mainly in anticipation of moving management of the array into the main test helper.

---
 tools/testing/selftests/kvm/x86_64/hyperv_cpuid.c | 1 +
 1 file changed, 1 insertion(+)
tools/testing/selftests/kvm/x86_64/hyperv_cpuid.c +0 -0
index 9a0fcc713350..09f9874d7705 100644

tools/testing/selftests/kvm/x86_64/hyperv_cpuid.c int main(int argc, char *argv[]) · line 167

Quoted 4 lines
tools/testing/selftests/kvm/x86_64/hyperv_cpuid.c +1 -0
@@ -164,6 +164,7 @@ int main(int argc, char *argv[])
 
 	hv_cpuid_entries = kvm_get_supported_hv_cpuid();
 	test_hv_cpuid(hv_cpuid_entries, kvm_cpu_has(X86_FEATURE_VMX));
+	free((void *)hv_cpuid_entries);

vcpu_get_supported_hv_cpuid() allocates memory for the resulting array each time, however, kvm_get_supported_hv_cpuid() was designed after what's now kvm_get_supported_cpuid() (afair) so it has an optimization to ask KVM just once:

        static struct kvm_cpuid2 *cpuid;
        int kvm_fd;

        if (cpuid)
                return cpuid;

        cpuid = allocate_kvm_cpuid2(MAX_NR_CPUID_ENTRIES);
        kvm_fd = open_kvm_dev_path_or_exit();
	...

and it seems that if we free hv_cpuid_entries here, next time we call kvm_get_supported_hv_cpuid() an already freed memory will be returned. This doesn't matter in in this patch as we're about to quit anyway but with the next one in the series it becomes problematic.

Quoted 3 lines
tools/testing/selftests/kvm/x86_64/hyperv_cpuid.c +0 -0
 out:
 	kvm_vm_free(vm);
Signature · 1 line
Vitaly
author maintainer
#8 lore raw

Re: [PATCH 3/5] KVM: selftests: Explicitly free CPUID array at end of Hyper-V CPUID test

Quoted 37 lines · Vitaly Kuznetsov
Quoted 15 lines · Sean Christopherson

Explicitly free the array of CPUID entries at the end of the Hyper-V CPUID test, mainly in anticipation of moving management of the array into the main test helper.

---
 tools/testing/selftests/kvm/x86_64/hyperv_cpuid.c | 1 +
 1 file changed, 1 insertion(+)
tools/testing/selftests/kvm/x86_64/hyperv_cpuid.c +0 -0
index 9a0fcc713350..09f9874d7705 100644

tools/testing/selftests/kvm/x86_64/hyperv_cpuid.c int main(int argc, char *argv[]) · line 167

Quoted 4 lines
tools/testing/selftests/kvm/x86_64/hyperv_cpuid.c +1 -0
@@ -164,6 +164,7 @@ int main(int argc, char *argv[])
 
 	hv_cpuid_entries = kvm_get_supported_hv_cpuid();
 	test_hv_cpuid(hv_cpuid_entries, kvm_cpu_has(X86_FEATURE_VMX));
+	free((void *)hv_cpuid_entries);

vcpu_get_supported_hv_cpuid() allocates memory for the resulting array each time, however, kvm_get_supported_hv_cpuid() was designed after what's now kvm_get_supported_cpuid() (afair) so it has an optimization to ask KVM just once:

        static struct kvm_cpuid2 *cpuid;
        int kvm_fd;

        if (cpuid)
                return cpuid;

        cpuid = allocate_kvm_cpuid2(MAX_NR_CPUID_ENTRIES);
        kvm_fd = open_kvm_dev_path_or_exit();
	...
Quoted 4 lines

and it seems that if we free hv_cpuid_entries here, next time we call kvm_get_supported_hv_cpuid() an already freed memory will be returned. This doesn't matter in in this patch as we're about to quit anyway but with the next one in the series it becomes problematic.

Ow. I totally missed that. I'll drop this patch, and then adjust the next one to do:

	/*
	 * Note, the CPUID array returned by the system-scoped helper is a one-
	 * time allocation, i.e. must not be freed.
	 */
	if (vcpu)
		free((void *)hv_cpuid_entries);

I'll post a v2 once I've actually tested.

Thanks!

author maintainer
#9 lore raw

PATCH 4/5 KVM: selftests: Manage CPUID array in Hyper-V CPUID test's core helper

In reply to Sean Christopherson

Allocate, get, and free the CPUID array in the Hyper-V CPUID test in the test's core helper, instead of copy+pasting code at each call site. In addition to deduplicating a small amount of code, restricting visibility of the array to a single invocation of the core test prevents "leaking" an array across test cases. Passing in @vcpu to the helper will also allow pivoting on VM-scoped information without needing to pass more booleans, e.g. to conditionally assert on features that require an in-kernel APIC.

---
 .../selftests/kvm/x86_64/hyperv_cpuid.c       | 25 ++++++++-----------
 1 file changed, 11 insertions(+), 14 deletions(-)
tools/testing/selftests/kvm/x86_64/hyperv_cpuid.c +11 -14
index 09f9874d7705..90c44765d584 100644
@@ -41,13 +41,18 @@ static bool smt_possible(void)
 	return res;
 }
 
-static void test_hv_cpuid(const struct kvm_cpuid2 *hv_cpuid_entries,
-			  bool evmcs_expected)
+static void test_hv_cpuid(struct kvm_vcpu *vcpu, bool evmcs_expected)
 {
+	const struct kvm_cpuid2 *hv_cpuid_entries;
 	int i;
 	int nent_expected = 10;
 	u32 test_val;
 
+	if (vcpu)
+		hv_cpuid_entries = vcpu_get_supported_hv_cpuid(vcpu);
+	else
+		hv_cpuid_entries = kvm_get_supported_hv_cpuid();
+
 	TEST_ASSERT(hv_cpuid_entries->nent == nent_expected,
 		    "KVM_GET_SUPPORTED_HV_CPUID should return %d entries"
 		    " (returned %d)",
@@ -109,6 +114,7 @@ static void test_hv_cpuid(const struct kvm_cpuid2 *hv_cpuid_entries,
 		 *	entry->edx);
 		 */
 	}
+	free((void *)hv_cpuid_entries);
 }
 
 static void test_hv_cpuid_e2big(struct kvm_vm *vm, struct kvm_vcpu *vcpu)
@@ -129,7 +135,6 @@ static void test_hv_cpuid_e2big(struct kvm_vm *vm, struct kvm_vcpu *vcpu)
 int main(int argc, char *argv[])
 {
 	struct kvm_vm *vm;
-	const struct kvm_cpuid2 *hv_cpuid_entries;
 	struct kvm_vcpu *vcpu;
 
 	TEST_REQUIRE(kvm_has_cap(KVM_CAP_HYPERV_CPUID));
@@ -138,10 +143,7 @@ int main(int argc, char *argv[])
 
 	/* Test vCPU ioctl version */
 	test_hv_cpuid_e2big(vm, vcpu);
-
-	hv_cpuid_entries = vcpu_get_supported_hv_cpuid(vcpu);
-	test_hv_cpuid(hv_cpuid_entries, false);
-	free((void *)hv_cpuid_entries);
+	test_hv_cpuid(vcpu, false);
 
 	if (!kvm_cpu_has(X86_FEATURE_VMX) ||
 	    !kvm_has_cap(KVM_CAP_HYPERV_ENLIGHTENED_VMCS)) {
@@ -149,9 +151,7 @@ int main(int argc, char *argv[])
 		goto do_sys;
 	}
 	vcpu_enable_evmcs(vcpu);
-	hv_cpuid_entries = vcpu_get_supported_hv_cpuid(vcpu);
-	test_hv_cpuid(hv_cpuid_entries, true);
-	free((void *)hv_cpuid_entries);
+	test_hv_cpuid(vcpu, true);
 
 do_sys:
 	/* Test system ioctl version */
@@ -161,10 +161,7 @@ int main(int argc, char *argv[])
 	}
 
 	test_hv_cpuid_e2big(vm, NULL);
-
-	hv_cpuid_entries = kvm_get_supported_hv_cpuid();
-	test_hv_cpuid(hv_cpuid_entries, kvm_cpu_has(X86_FEATURE_VMX));
-	free((void *)hv_cpuid_entries);
+	test_hv_cpuid(NULL, kvm_cpu_has(X86_FEATURE_VMX));
 
 out:
 	kvm_vm_free(vm);
Signature · 1 line
2.47.1.688.g23fc6f90ad-goog
maintainer
#10 lore raw

Re: [PATCH 4/5] KVM: selftests: Manage CPUID array in Hyper-V CPUID test's core helper

Quoted 40 lines · Sean Christopherson

Allocate, get, and free the CPUID array in the Hyper-V CPUID test in the test's core helper, instead of copy+pasting code at each call site. In addition to deduplicating a small amount of code, restricting visibility of the array to a single invocation of the core test prevents "leaking" an array across test cases. Passing in @vcpu to the helper will also allow pivoting on VM-scoped information without needing to pass more booleans, e.g. to conditionally assert on features that require an in-kernel APIC.

---
 .../selftests/kvm/x86_64/hyperv_cpuid.c       | 25 ++++++++-----------
 1 file changed, 11 insertions(+), 14 deletions(-)
tools/testing/selftests/kvm/x86_64/hyperv_cpuid.c +7 -2
index 09f9874d7705..90c44765d584 100644
@@ -41,13 +41,18 @@ static bool smt_possible(void)
 	return res;
 }
 
-static void test_hv_cpuid(const struct kvm_cpuid2 *hv_cpuid_entries,
-			  bool evmcs_expected)
+static void test_hv_cpuid(struct kvm_vcpu *vcpu, bool evmcs_expected)
 {
+	const struct kvm_cpuid2 *hv_cpuid_entries;
 	int i;
 	int nent_expected = 10;
 	u32 test_val;
 
+	if (vcpu)
+		hv_cpuid_entries = vcpu_get_supported_hv_cpuid(vcpu);
+	else
+		hv_cpuid_entries = kvm_get_supported_hv_cpuid();
+
 	TEST_ASSERT(hv_cpuid_entries->nent == nent_expected,
 		    "KVM_GET_SUPPORTED_HV_CPUID should return %d entries"
 		    " (returned %d)",

tools/testing/selftests/kvm/x86_64/hyperv_cpuid.c static void test_hv_cpuid(const struct kvm_cpuid2 *hv_cpuid_entries, · line 117

Quoted 4 lines
tools/testing/selftests/kvm/x86_64/hyperv_cpuid.c +1 -0
@@ -109,6 +114,7 @@ static void test_hv_cpuid(const struct kvm_cpuid2 *hv_cpuid_entries,
 		 *	entry->edx);
 		 */
 	}
+	free((void *)hv_cpuid_entries);

(see my comment on "[PATCH 3/5] KVM: selftests: Explicitly free CPUID array at end of Hyper-V CPUID test"):

vcpu_get_supported_hv_cpuid() allocates memory for the resulting array each time, however, kvm_get_supported_hv_cpuid() doesn't so freeing hv_cpuid_entries here will result in returning already freed memory next time kvm_get_supported_hv_cpuid() path is taken.

Quoted 46 lines
tools/testing/selftests/kvm/x86_64/hyperv_cpuid.c +3 -12
 }
 
 static void test_hv_cpuid_e2big(struct kvm_vm *vm, struct kvm_vcpu *vcpu)
@@ -129,7 +135,6 @@ static void test_hv_cpuid_e2big(struct kvm_vm *vm, struct kvm_vcpu *vcpu)
 int main(int argc, char *argv[])
 {
 	struct kvm_vm *vm;
-	const struct kvm_cpuid2 *hv_cpuid_entries;
 	struct kvm_vcpu *vcpu;
 
 	TEST_REQUIRE(kvm_has_cap(KVM_CAP_HYPERV_CPUID));
@@ -138,10 +143,7 @@ int main(int argc, char *argv[])
 
 	/* Test vCPU ioctl version */
 	test_hv_cpuid_e2big(vm, vcpu);
-
-	hv_cpuid_entries = vcpu_get_supported_hv_cpuid(vcpu);
-	test_hv_cpuid(hv_cpuid_entries, false);
-	free((void *)hv_cpuid_entries);
+	test_hv_cpuid(vcpu, false);
 
 	if (!kvm_cpu_has(X86_FEATURE_VMX) ||
 	    !kvm_has_cap(KVM_CAP_HYPERV_ENLIGHTENED_VMCS)) {
@@ -149,9 +151,7 @@ int main(int argc, char *argv[])
 		goto do_sys;
 	}
 	vcpu_enable_evmcs(vcpu);
-	hv_cpuid_entries = vcpu_get_supported_hv_cpuid(vcpu);
-	test_hv_cpuid(hv_cpuid_entries, true);
-	free((void *)hv_cpuid_entries);
+	test_hv_cpuid(vcpu, true);
 
 do_sys:
 	/* Test system ioctl version */
@@ -161,10 +161,7 @@ int main(int argc, char *argv[])
 	}
 
 	test_hv_cpuid_e2big(vm, NULL);
-
-	hv_cpuid_entries = kvm_get_supported_hv_cpuid();
-	test_hv_cpuid(hv_cpuid_entries, kvm_cpu_has(X86_FEATURE_VMX));
-	free((void *)hv_cpuid_entries);
+	test_hv_cpuid(NULL, kvm_cpu_has(X86_FEATURE_VMX));
 
 out:
 	kvm_vm_free(vm);
Signature · 1 line
Vitaly
author maintainer
#11 lore raw

PATCH 5/5 KVM: selftests: Add CPUID tests for Hyper-V features that need in-kernel APIC

In reply to Sean Christopherson

Add testcases to x86's Hyper-V CPUID test to verify that KVM advertises support for features that require an in-kernel local APIC appropriately, i.e. that KVM hides support from the vCPU-scoped ioctl if the VM doesn't have an in-kernel local APIC.

---
 tools/testing/selftests/kvm/x86_64/hyperv_cpuid.c | 15 ++++++++++++++-
 1 file changed, 14 insertions(+), 1 deletion(-)
tools/testing/selftests/kvm/x86_64/hyperv_cpuid.c +14 -1
index 90c44765d584..f76b6a95b530 100644
@@ -43,6 +43,7 @@ static bool smt_possible(void)
 
 static void test_hv_cpuid(struct kvm_vcpu *vcpu, bool evmcs_expected)
 {
+	const bool has_irqchip = !vcpu || vcpu->vm->has_irqchip;
 	const struct kvm_cpuid2 *hv_cpuid_entries;
 	int i;
 	int nent_expected = 10;
@@ -85,12 +86,19 @@ static void test_hv_cpuid(struct kvm_vcpu *vcpu, bool evmcs_expected)
 				    entry->eax, evmcs_expected
 				);
 			break;
+		case 0x40000003:
+			TEST_ASSERT(has_irqchip || !(entry->edx & BIT(19)),
+				    "Synthetic Timers should require in-kernel APIC");
+			break;
 		case 0x40000004:
 			test_val = entry->eax & (1UL << 18);
 
 			TEST_ASSERT(!!test_val == !smt_possible(),
 				    "NoNonArchitecturalCoreSharing bit"
 				    " doesn't reflect SMT setting");
+
+			TEST_ASSERT(has_irqchip || !(entry->eax & BIT(10)),
+				    "Cluster IPI (i.e. SEND_IPI) should require in-kernel APIC");
 			break;
 		case 0x4000000A:
 			TEST_ASSERT(entry->eax & (1UL << 19),
@@ -139,9 +147,14 @@ int main(int argc, char *argv[])
 
 	TEST_REQUIRE(kvm_has_cap(KVM_CAP_HYPERV_CPUID));
 
-	vm = vm_create_with_one_vcpu(&vcpu, guest_code);
+	/* Test the vCPU ioctl without an in-kernel local APIC. */
+	vm = vm_create_barebones();
+	vcpu = __vm_vcpu_add(vm, 0);
+	test_hv_cpuid(vcpu, false);
+	kvm_vm_free(vm);
 
 	/* Test vCPU ioctl version */
+	vm = vm_create_with_one_vcpu(&vcpu, guest_code);
 	test_hv_cpuid_e2big(vm, vcpu);
 	test_hv_cpuid(vcpu, false);
 
Signature · 1 line
2.47.1.688.g23fc6f90ad-goog