Skip to contentSkip to conversation

KVM: x86: hyper-v: make KVM_GET_SUPPORTED_HV_CPUID more useful

PATCH Superseded Vitaly Kuznetsov 1 participant 9 messages Aug 7, 2020 – Sep 1, 2020

Subsystems (6)
Documentation x86 KVM Kselftest +2 more subsystems

v1 of 4 Superseded Latest is v4 →

lore
Share & tools
lore

Vitaly Kuznetsov

author maintainer
#0 lore raw

cover KVM: x86: hyper-v: make KVM_GET_SUPPORTED_HV_CPUID more useful

KVM_GET_SUPPORTED_HV_CPUID was initially implemented as a vCPU ioctl but this is not very useful when VMM is just trying to query which Hyper-V features are supported by the host prior to creating VM/vCPUs. The data in KVM_GET_SUPPORTED_HV_CPUID is mostly static with a few exceptions but it seems we can change this. Add support for KVM_GET_SUPPORTED_HV_CPUID as a system ioctl as well.

QEMU specific description:
In some cases QEMU needs to collect the information about which Hyper-V features are supported by KVM and pass it up the stack. For non-hyper-v features this is done with system-wide KVM_GET_SUPPORTED_CPUID/ KVM_GET_MSRS ioctls but Hyper-V specific features don't get in the output (as Hyper-V CPUIDs intersect with KVM's). In QEMU, CPU feature expansion happens before any KVM vcpus are created so KVM_GET_SUPPORTED_HV_CPUID can't be used in its current shape.

Vitaly Kuznetsov (7):
KVM: x86: hyper-v: Mention SynDBG CPUID leaves in api.rst
KVM: x86: hyper-v: disallow configuring SynIC timers with no SynIC
KVM: x86: hyper-v: make KVM_GET_SUPPORTED_HV_CPUID output independent
of eVMCS enablement
KVM: x86: hyper-v: always advertise HV_STIMER_DIRECT_MODE_AVAILABLE
KVM: x86: hyper-v: drop now unneeded vcpu parameter from
kvm_vcpu_ioctl_get_hv_cpuid()
KVM: x86: hyper-v: allow KVM_GET_SUPPORTED_HV_CPUID as a system ioctl
KVM: selftests: test KVM_GET_SUPPORTED_HV_CPUID as a system ioctl

 Documentation/virt/kvm/api.rst                | 12 +--
 arch/x86/include/asm/kvm_host.h               |  2 +-
 arch/x86/kvm/hyperv.c                         | 30 ++++----
 arch/x86/kvm/hyperv.h                         |  3 +-
 arch/x86/kvm/vmx/evmcs.c                      |  8 +-
 arch/x86/kvm/vmx/evmcs.h                      |  2 +-
 arch/x86/kvm/x86.c                            | 44 ++++++-----
 include/uapi/linux/kvm.h                      |  4 +-
 .../testing/selftests/kvm/include/kvm_util.h  |  2 +
 tools/testing/selftests/kvm/lib/kvm_util.c    | 26 +++++++
 .../selftests/kvm/x86_64/hyperv_cpuid.c       | 77 +++++++++----------
 11 files changed, 120 insertions(+), 90 deletions(-)
Signature · 1 line
2.25.4
author maintainer
#1 lore raw

PATCH 1/7 KVM: x86: hyper-v: Mention SynDBG CPUID leaves in api.rst

We forgot to update KVM_GET_SUPPORTED_HV_CPUID's documentation in api.rst when SynDBG leaves were added.

While on it, fix 'KVM_GET_SUPPORTED_CPUID' copy-paste error.

Fixes f97f5a56f597 ("x86/kvm/hyper-v: Add support for synthetic debugger interface")
---
 Documentation/virt/kvm/api.rst | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
Documentation/virt/kvm/api.rst +4 -1
index 644e5326aa50..c0ccad0def5a 100644
@@ -4494,11 +4494,14 @@ Currently, the following list of CPUID leaves are returned:
  - HYPERV_CPUID_ENLIGHTMENT_INFO
  - HYPERV_CPUID_IMPLEMENT_LIMITS
  - HYPERV_CPUID_NESTED_FEATURES
+ - HYPERV_CPUID_SYNDBG_VENDOR_AND_MAX_FUNCTIONS
+ - HYPERV_CPUID_SYNDBG_INTERFACE
+ - HYPERV_CPUID_SYNDBG_PLATFORM_CAPABILITIES
 
 HYPERV_CPUID_NESTED_FEATURES leaf is only exposed when Enlightened VMCS was
 enabled on the corresponding vCPU (KVM_CAP_HYPERV_ENLIGHTENED_VMCS).
 
-Userspace invokes KVM_GET_SUPPORTED_CPUID by passing a kvm_cpuid2 structure
+Userspace invokes KVM_GET_SUPPORTED_HV_CPUID by passing a kvm_cpuid2 structure
 with the 'nent' field indicating the number of entries in the variable-size
 array 'entries'.  If the number of entries is too low to describe all Hyper-V
 feature leaves, an error (E2BIG) is returned. If the number is more or equal
Signature · 1 line
2.25.4
author maintainer
#2 lore raw

PATCH 2/7 KVM: x86: hyper-v: disallow configuring SynIC timers with no SynIC

In reply to Vitaly Kuznetsov

Hyper-V Synthetic timers require SynIC but we don't seem to check that upon HV_X64_MSR_STIMER[X]_CONFIG/HV_X64_MSR_STIMER0_COUNT writes. Make the behavior match synic_set_msr().

---
 arch/x86/kvm/hyperv.c | 11 +++++++++++
 1 file changed, 11 insertions(+)
arch/x86/kvm/hyperv.c +11 -0
index af9cdb426dd2..4291943d1028 100644
@@ -633,6 +633,11 @@ static int stimer_set_config(struct kvm_vcpu_hv_stimer *stimer, u64 config,
 {
 	union hv_stimer_config new_config = {.as_uint64 = config},
 		old_config = {.as_uint64 = stimer->config.as_uint64};
+	struct kvm_vcpu *vcpu = stimer_to_vcpu(stimer);
+	struct kvm_vcpu_hv_synic *synic = vcpu_to_synic(vcpu);
+
+	if (!synic->active && !host)
+		return 1;
 
 	trace_kvm_hv_stimer_set_config(stimer_to_vcpu(stimer)->vcpu_id,
 				       stimer->index, config, host);
@@ -652,6 +657,12 @@ static int stimer_set_config(struct kvm_vcpu_hv_stimer *stimer, u64 config,
 static int stimer_set_count(struct kvm_vcpu_hv_stimer *stimer, u64 count,
 			    bool host)
 {
+	struct kvm_vcpu *vcpu = stimer_to_vcpu(stimer);
+	struct kvm_vcpu_hv_synic *synic = vcpu_to_synic(vcpu);
+
+	if (!synic->active && !host)
+		return 1;
+
 	trace_kvm_hv_stimer_set_count(stimer_to_vcpu(stimer)->vcpu_id,
 				      stimer->index, count, host);
 
Signature · 1 line
2.25.4
author maintainer
#3 lore raw

PATCH 3/7 KVM: x86: hyper-v: make KVM_GET_SUPPORTED_HV_CPUID output independent of eVMCS enablement

In reply to Vitaly Kuznetsov

It is really inconvenient when KVM_GET_SUPPORTED_HV_CPUID output depends on whether some other capabilities are enabled as VMM needs to be aware of the correct sequence (which becomes an API).

Enlightened VMCS is not the only feature which requires explicit enablement. In particular, SynIC has to be enabled explicitly too but the corresponding features are always present in KVM_GET_SUPPORTED_HV_CPUID, this is inconsistent. In any case, userspace can't blindly copy the output of KVM_GET_SUPPORTED_HV_CPUID to guest's CPUIDs and expect everything to work.

Previously, we were also using EVMCS enablement to decide whether or not to report HYPERV_CPUID_NESTED_FEATURES feature leaf as the last available leaf in HYPERV_CPUID_SYNDBG_PLATFORM_CAPABILITIES but this is already gone with SYNDBG and nobody seemed to complain.

As a preparation to making KVM_GET_SUPPORTED_HV_CPUID a system ioctl, make EVMCS feature bits work the same way as all other bits, nothing should break (famous last words).

---
 Documentation/virt/kvm/api.rst                |  3 --
 arch/x86/include/asm/kvm_host.h               |  2 +-
 arch/x86/kvm/hyperv.c                         |  8 ++---
 arch/x86/kvm/vmx/evmcs.c                      |  8 ++---
 arch/x86/kvm/vmx/evmcs.h                      |  2 +-
 .../selftests/kvm/x86_64/hyperv_cpuid.c       | 35 +++++--------------
 6 files changed, 15 insertions(+), 43 deletions(-)
Documentation/virt/kvm/api.rst +0 -3
index c0ccad0def5a..6acdc6f80aee 100644
@@ -4498,9 +4498,6 @@ Currently, the following list of CPUID leaves are returned:
  - HYPERV_CPUID_SYNDBG_INTERFACE
  - HYPERV_CPUID_SYNDBG_PLATFORM_CAPABILITIES
 
-HYPERV_CPUID_NESTED_FEATURES leaf is only exposed when Enlightened VMCS was
-enabled on the corresponding vCPU (KVM_CAP_HYPERV_ENLIGHTENED_VMCS).
-
 Userspace invokes KVM_GET_SUPPORTED_HV_CPUID by passing a kvm_cpuid2 structure
 with the 'nent' field indicating the number of entries in the variable-size
 array 'entries'.  If the number of entries is too low to describe all Hyper-V
arch/x86/include/asm/kvm_host.h +1 -1
index 1bab87a444d7..cd82559490a5 100644
@@ -1243,7 +1243,7 @@ struct kvm_x86_nested_ops {
 
 	int (*enable_evmcs)(struct kvm_vcpu *vcpu,
 			    uint16_t *vmcs_version);
-	uint16_t (*get_evmcs_version)(struct kvm_vcpu *vcpu);
+	uint16_t (*get_evmcs_version)(void);
 };
 
 struct kvm_x86_init_ops {
arch/x86/kvm/hyperv.c +2 -6
index 4291943d1028..5a6831799544 100644
@@ -1961,19 +1961,15 @@ int kvm_vcpu_ioctl_get_hv_cpuid(struct kvm_vcpu *vcpu, struct kvm_cpuid2 *cpuid,
 		{ .function = HYPERV_CPUID_FEATURES },
 		{ .function = HYPERV_CPUID_ENLIGHTMENT_INFO },
 		{ .function = HYPERV_CPUID_IMPLEMENT_LIMITS },
+		{ .function = HYPERV_CPUID_NESTED_FEATURES },
 		{ .function = HYPERV_CPUID_SYNDBG_VENDOR_AND_MAX_FUNCTIONS },
 		{ .function = HYPERV_CPUID_SYNDBG_INTERFACE },
 		{ .function = HYPERV_CPUID_SYNDBG_PLATFORM_CAPABILITIES	},
-		{ .function = HYPERV_CPUID_NESTED_FEATURES },
 	};
 	int i, nent = ARRAY_SIZE(cpuid_entries);
 
 	if (kvm_x86_ops.nested_ops->get_evmcs_version)
-		evmcs_ver = kvm_x86_ops.nested_ops->get_evmcs_version(vcpu);
-
-	/* Skip NESTED_FEATURES if eVMCS is not supported */
-	if (!evmcs_ver)
-		--nent;
+		evmcs_ver = kvm_x86_ops.nested_ops->get_evmcs_version();
 
 	if (cpuid->nent < nent)
 		return -E2BIG;
arch/x86/kvm/vmx/evmcs.c +3 -5
index e5325bd0f304..4559d5851bf0 100644
@@ -325,17 +325,15 @@ bool nested_enlightened_vmentry(struct kvm_vcpu *vcpu, u64 *evmcs_gpa)
 	return true;
 }
 
-uint16_t nested_get_evmcs_version(struct kvm_vcpu *vcpu)
+uint16_t nested_get_evmcs_version(void)
 {
-	struct vcpu_vmx *vmx = to_vmx(vcpu);
 	/*
 	 * vmcs_version represents the range of supported Enlightened VMCS
 	 * versions: lower 8 bits is the minimal version, higher 8 bits is the
 	 * maximum supported version. KVM supports versions from 1 to
 	 * KVM_EVMCS_VERSION.
 	 */
-	if (kvm_cpu_cap_get(X86_FEATURE_VMX) &&
-	    vmx->nested.enlightened_vmcs_enabled)
+	if (kvm_cpu_cap_get(X86_FEATURE_VMX))
 		return (KVM_EVMCS_VERSION << 8) | 1;
 
 	return 0;
@@ -427,7 +425,7 @@ int nested_enable_evmcs(struct kvm_vcpu *vcpu,
 	vmx->nested.enlightened_vmcs_enabled = true;
 
 	if (vmcs_version)
-		*vmcs_version = nested_get_evmcs_version(vcpu);
+		*vmcs_version = nested_get_evmcs_version();
 
 	return 0;
 }
arch/x86/kvm/vmx/evmcs.h +1 -1
index e5f7a7ebf27d..8e6429ec9ace 100644
@@ -206,7 +206,7 @@ enum nested_evmptrld_status {
 };
 
 bool nested_enlightened_vmentry(struct kvm_vcpu *vcpu, u64 *evmcs_gpa);
-uint16_t nested_get_evmcs_version(struct kvm_vcpu *vcpu);
+uint16_t nested_get_evmcs_version(void);
 int nested_enable_evmcs(struct kvm_vcpu *vcpu,
 			uint16_t *vmcs_version);
 void nested_evmcs_filter_control_msr(u32 msr_index, u64 *pdata);
tools/testing/selftests/kvm/x86_64/hyperv_cpuid.c +8 -27
index 745b708c2d3b..8b24cb2e6a19 100644
@@ -45,20 +45,16 @@ static bool smt_possible(void)
 	return res;
 }
 
-static void test_hv_cpuid(struct kvm_cpuid2 *hv_cpuid_entries,
-			  bool evmcs_enabled)
+static void test_hv_cpuid(struct kvm_cpuid2 *hv_cpuid_entries)
 {
 	int i;
-	int nent = 9;
+	int nent = 10;
 	u32 test_val;
 
-	if (evmcs_enabled)
-		nent += 1; /* 0x4000000A */
-
 	TEST_ASSERT(hv_cpuid_entries->nent == nent,
 		    "KVM_GET_SUPPORTED_HV_CPUID should return %d entries"
-		    " with evmcs=%d (returned %d)",
-		    nent, evmcs_enabled, hv_cpuid_entries->nent);
+		    " (returned %d)",
+		    nent, hv_cpuid_entries->nent);
 
 	for (i = 0; i < hv_cpuid_entries->nent; i++) {
 		struct kvm_cpuid_entry2 *entry = &hv_cpuid_entries->entries[i];
@@ -68,9 +64,6 @@ static void test_hv_cpuid(struct kvm_cpuid2 *hv_cpuid_entries,
 			    "function %x is our of supported range",
 			    entry->function);
 
-		TEST_ASSERT(evmcs_enabled || (entry->function != 0x4000000A),
-			    "0x4000000A leaf should not be reported");
-
 		TEST_ASSERT(entry->index == 0,
 			    ".index field should be zero");
 
@@ -85,9 +78,8 @@ static void test_hv_cpuid(struct kvm_cpuid2 *hv_cpuid_entries,
 			test_val = 0x40000082;
 
 			TEST_ASSERT(entry->eax == test_val,
-				    "Wrong max leaf report in 0x40000000.EAX: %x"
-				    " (evmcs=%d)",
-				    entry->eax, evmcs_enabled
+				    "Wrong max leaf report in 0x40000000.EAX: %x",
+				    entry->eax
 				);
 			break;
 		case 0x40000004:
@@ -148,7 +140,6 @@ int main(int argc, char *argv[])
 	struct kvm_vm *vm;
 	int rv, stage;
 	struct kvm_cpuid2 *hv_cpuid_entries;
-	bool evmcs_enabled;
 
 	/* Tell stdout not to buffer its content */
 	setbuf(stdout, NULL);
@@ -159,8 +150,7 @@ int main(int argc, char *argv[])
 		exit(KSFT_SKIP);
 	}
 
-	for (stage = 0; stage < 3; stage++) {
-		evmcs_enabled = false;
+	for (stage = 0; stage < 2; stage++) {
 
 		vm = vm_create_default(VCPU_ID, 0, guest_code);
 		switch (stage) {
@@ -169,19 +159,10 @@ int main(int argc, char *argv[])
 			continue;
 		case 1:
 			break;
-		case 2:
-			if (!nested_vmx_supported() ||
-			    !kvm_check_cap(KVM_CAP_HYPERV_ENLIGHTENED_VMCS)) {
-				print_skip("Enlightened VMCS is unsupported");
-				continue;
-			}
-			vcpu_enable_evmcs(vm, VCPU_ID);
-			evmcs_enabled = true;
-			break;
 		}
 
 		hv_cpuid_entries = kvm_get_supported_hv_cpuid(vm);
-		test_hv_cpuid(hv_cpuid_entries, evmcs_enabled);
+		test_hv_cpuid(hv_cpuid_entries);
 		free(hv_cpuid_entries);
 		kvm_vm_free(vm);
 	}
Signature · 1 line
2.25.4
author maintainer
#4 lore raw

PATCH 4/7 KVM: x86: hyper-v: always advertise HV_STIMER_DIRECT_MODE_AVAILABLE

In reply to Vitaly Kuznetsov

HV_STIMER_DIRECT_MODE_AVAILABLE is the last conditionally set feature bit in KVM_GET_SUPPORTED_HV_CPUID but it doesn't have to be conditional: first, this bit is only an indication to userspace VMM that direct mode stimers are supported, it still requires manual enablement (enabling SynIC) to work so no VMM should just blindly copy it to guest CPUIDs. Second, lapic_in_kernel() is a must for SynIC. Expose the bit unconditionally.

---
 arch/x86/kvm/hyperv.c | 8 +-------
 1 file changed, 1 insertion(+), 7 deletions(-)
arch/x86/kvm/hyperv.c +1 -7
index 5a6831799544..a8ebaa66a8e1 100644
@@ -2027,13 +2027,7 @@ int kvm_vcpu_ioctl_get_hv_cpuid(struct kvm_vcpu *vcpu, struct kvm_cpuid2 *cpuid,
 			ent->ebx |= HV_DEBUGGING;
 			ent->edx |= HV_X64_GUEST_DEBUGGING_AVAILABLE;
 			ent->edx |= HV_FEATURE_DEBUG_MSRS_AVAILABLE;
-
-			/*
-			 * Direct Synthetic timers only make sense with in-kernel
-			 * LAPIC
-			 */
-			if (lapic_in_kernel(vcpu))
-				ent->edx |= HV_STIMER_DIRECT_MODE_AVAILABLE;
+			ent->edx |= HV_STIMER_DIRECT_MODE_AVAILABLE;
 
 			break;
 
Signature · 1 line
2.25.4
author maintainer
#5 lore raw

PATCH 5/7 KVM: x86: hyper-v: drop now unneeded vcpu parameter from kvm_vcpu_ioctl_get_hv_cpuid()

In reply to Vitaly Kuznetsov

kvm_vcpu_ioctl_get_hv_cpuid() doesn't use its vcpu parameter anymore, drop it. Also, the function is now untied from vcpu, rename it accordingly.

No functional change intended.

---
 arch/x86/kvm/hyperv.c | 3 +--
 arch/x86/kvm/hyperv.h | 3 +--
 arch/x86/kvm/x86.c    | 3 +--
 3 files changed, 3 insertions(+), 6 deletions(-)
arch/x86/kvm/hyperv.c +1 -2
index a8ebaa66a8e1..daed68139765 100644
@@ -1950,8 +1950,7 @@ int kvm_vm_ioctl_hv_eventfd(struct kvm *kvm, struct kvm_hyperv_eventfd *args)
 	return kvm_hv_eventfd_assign(kvm, args->conn_id, args->fd);
 }
 
-int kvm_vcpu_ioctl_get_hv_cpuid(struct kvm_vcpu *vcpu, struct kvm_cpuid2 *cpuid,
-				struct kvm_cpuid_entry2 __user *entries)
+int kvm_get_hv_cpuid(struct kvm_cpuid2 *cpuid, struct kvm_cpuid_entry2 __user *entries)
 {
 	uint16_t evmcs_ver = 0;
 	struct kvm_cpuid_entry2 cpuid_entries[] = {
arch/x86/kvm/hyperv.h +1 -2
index e68c6c2e9649..1fc4997e7f01 100644
@@ -126,7 +126,6 @@ void kvm_hv_setup_tsc_page(struct kvm *kvm,
 void kvm_hv_init_vm(struct kvm *kvm);
 void kvm_hv_destroy_vm(struct kvm *kvm);
 int kvm_vm_ioctl_hv_eventfd(struct kvm *kvm, struct kvm_hyperv_eventfd *args);
-int kvm_vcpu_ioctl_get_hv_cpuid(struct kvm_vcpu *vcpu, struct kvm_cpuid2 *cpuid,
-				struct kvm_cpuid_entry2 __user *entries);
+int kvm_get_hv_cpuid(struct kvm_cpuid2 *cpuid, struct kvm_cpuid_entry2 __user *entries);
 
 #endif
arch/x86/kvm/x86.c +1 -2
index 95ef62922869..4659067c2e53 100644
@@ -4749,8 +4749,7 @@ long kvm_arch_vcpu_ioctl(struct file *filp,
 		if (copy_from_user(&cpuid, cpuid_arg, sizeof(cpuid)))
 			goto out;
 
-		r = kvm_vcpu_ioctl_get_hv_cpuid(vcpu, &cpuid,
-						cpuid_arg->entries);
+		r = kvm_get_hv_cpuid(&cpuid, cpuid_arg->entries);
 		if (r)
 			goto out;
 
Signature · 1 line
2.25.4
author maintainer
#6 lore raw

PATCH 6/7 KVM: x86: hyper-v: allow KVM_GET_SUPPORTED_HV_CPUID as a system ioctl

In reply to Vitaly Kuznetsov

KVM_GET_SUPPORTED_HV_CPUID is a vCPU ioctl but its output is now independent from vCPU and in some cases VMMs may want to use it as a system ioctl instead. In particular, QEMU doesn CPU feature expansion before any vCPU gets created so KVM_GET_SUPPORTED_HV_CPUID can't be used.

Convert KVM_GET_SUPPORTED_HV_CPUID to 'dual' system/vCPU ioctl with the same meaning.

---
 Documentation/virt/kvm/api.rst |  4 ++--
 arch/x86/kvm/x86.c             | 43 ++++++++++++++++++++--------------
 include/uapi/linux/kvm.h       |  4 ++--
 3 files changed, 30 insertions(+), 21 deletions(-)
Documentation/virt/kvm/api.rst +2 -2
index 6acdc6f80aee..4672d6e4d955 100644
@@ -4451,9 +4451,9 @@ that KVM_CAP_MANUAL_DIRTY_LOG_PROTECT2 is present.
 4.118 KVM_GET_SUPPORTED_HV_CPUID
 --------------------------------
 
-:Capability: KVM_CAP_HYPERV_CPUID
+:Capability: KVM_CAP_HYPERV_CPUID (vcpu), KVM_CAP_SYS_HYPERV_CPUID (system)
 :Architectures: x86
-:Type: vcpu ioctl
+:Type: system ioctl, vcpu ioctl
 :Parameters: struct kvm_cpuid2 (in/out)
 :Returns: 0 on success, -1 on error
 
arch/x86/kvm/x86.c +26 -17

68 lines not on this page · raw message

include/uapi/linux/kvm.h +2 -2
index 2c73dcfb3dbb..05459dd3dc70 100644
@@ -1034,7 +1034,7 @@ struct kvm_ppc_resize_hpt {
 #define KVM_CAP_ASYNC_PF_INT 183
 #define KVM_CAP_LAST_CPU 184
 #define KVM_CAP_SMALLER_MAXPHYADDR 185
-
+#define KVM_CAP_SYS_HYPERV_CPUID 186
 
 #ifdef KVM_CAP_IRQ_ROUTING
 
@@ -1493,7 +1493,7 @@ struct kvm_enc_region {
 /* Available with KVM_CAP_MANUAL_DIRTY_LOG_PROTECT_2 */
 #define KVM_CLEAR_DIRTY_LOG          _IOWR(KVMIO, 0xc0, struct kvm_clear_dirty_log)
 
-/* Available with KVM_CAP_HYPERV_CPUID */
+/* Available with KVM_CAP_HYPERV_CPUID (vcpu) / KVM_CAP_SYS_HYPERV_CPUID (system) */
 #define KVM_GET_SUPPORTED_HV_CPUID _IOWR(KVMIO, 0xc1, struct kvm_cpuid2)
 
 /* Available with KVM_CAP_ARM_SVE */
Signature · 1 line
2.25.4
author maintainer
#7 lore raw

PATCH 7/7 KVM: selftests: test KVM_GET_SUPPORTED_HV_CPUID as a system ioctl

In reply to Vitaly Kuznetsov

KVM_GET_SUPPORTED_HV_CPUID is now supported as both vCPU and VM ioctl, test that.

---
 .../testing/selftests/kvm/include/kvm_util.h  |  2 +
 tools/testing/selftests/kvm/lib/kvm_util.c    | 26 +++++++++++
 .../selftests/kvm/x86_64/hyperv_cpuid.c       | 46 +++++++++++++------
 3 files changed, 59 insertions(+), 15 deletions(-)
tools/testing/selftests/kvm/include/kvm_util.h +2 -0
index 919e161dd289..59482e4eb308 100644
@@ -112,6 +112,8 @@ void vcpu_ioctl(struct kvm_vm *vm, uint32_t vcpuid, unsigned long ioctl,
 int _vcpu_ioctl(struct kvm_vm *vm, uint32_t vcpuid, unsigned long ioctl,
 		void *arg);
 void vm_ioctl(struct kvm_vm *vm, unsigned long ioctl, void *arg);
+void kvm_ioctl(struct kvm_vm *vm, unsigned long ioctl, void *arg);
+int _kvm_ioctl(struct kvm_vm *vm, unsigned long ioctl, void *arg);
 void vm_mem_region_set_flags(struct kvm_vm *vm, uint32_t slot, uint32_t flags);
 void vm_mem_region_move(struct kvm_vm *vm, uint32_t slot, uint64_t new_gpa);
 void vm_mem_region_delete(struct kvm_vm *vm, uint32_t slot);
tools/testing/selftests/kvm/lib/kvm_util.c +26 -0
index 74776ee228f2..d49e24a15836 100644
@@ -1518,6 +1518,32 @@ void vm_ioctl(struct kvm_vm *vm, unsigned long cmd, void *arg)
 		cmd, ret, errno, strerror(errno));
 }
 
+/*
+ * KVM system ioctl
+ *
+ * Input Args:
+ *   vm - Virtual Machine
+ *   cmd - Ioctl number
+ *   arg - Argument to pass to the ioctl
+ *
+ * Return: None
+ *
+ * Issues an arbitrary ioctl on a KVM fd.
+ */
+void kvm_ioctl(struct kvm_vm *vm, unsigned long cmd, void *arg)
+{
+	int ret;
+
+	ret = ioctl(vm->kvm_fd, cmd, arg);
+	TEST_ASSERT(ret == 0, "KVM ioctl %lu failed, rc: %i errno: %i (%s)",
+		cmd, ret, errno, strerror(errno));
+}
+
+int _kvm_ioctl(struct kvm_vm *vm, unsigned long cmd, void *arg)
+{
+	return ioctl(vm->kvm_fd, cmd, arg);
+}
+
 /*
  * VM Dump
  *
tools/testing/selftests/kvm/x86_64/hyperv_cpuid.c +31 -15

80 lines not on this page · raw message

Signature · 1 line
2.25.4
author maintainer
#8 lore raw
In reply to Vitaly Kuznetsov
Quoted 7 lines · Vitaly Kuznetsov

KVM_GET_SUPPORTED_HV_CPUID was initially implemented as a vCPU ioctl but this is not very useful when VMM is just trying to query which Hyper-V features are supported by the host prior to creating VM/vCPUs. The data in KVM_GET_SUPPORTED_HV_CPUID is mostly static with a few exceptions but it seems we can change this. Add support for KVM_GET_SUPPORTED_HV_CPUID as a system ioctl as well.

Quoted 9 lines

QEMU specific description:
In some cases QEMU needs to collect the information about which Hyper-V features are supported by KVM and pass it up the stack. For non-hyper-v features this is done with system-wide KVM_GET_SUPPORTED_CPUID/ KVM_GET_MSRS ioctls but Hyper-V specific features don't get in the output (as Hyper-V CPUIDs intersect with KVM's). In QEMU, CPU feature expansion happens before any KVM vcpus are created so KVM_GET_SUPPORTED_HV_CPUID can't be used in its current shape.

Ping ;-)

I know Hyper-V emulation in KVM is a very specific topic so it's not very easy to find reviewers but I'd be very happy if someone could provide any feedback on the idea (above) in general.

Signature · 1 line
Vitaly