Skip to contentSkip to conversation

kvm: selftests: Add KVM_CAP_MAX_VCPU_ID cap test

PATCH In review Zeng Guang 2 participants 2 messages May 3, 2022 – May 13, 2022

Subsystems (2)
lore
Share & tools
lore

Zeng Guang

author
#0 lore raw

PATCH v2 1/1 kvm: selftests: Add KVM_CAP_MAX_VCPU_ID cap test

Basic test coverage of KVM_CAP_MAX_VCPU_ID cap.

This capability can be enabled before vCPU creation and only allowed to set once. if assigned vcpu id is beyond KVM_CAP_MAX_VCPU_ID capability, vCPU creation will fail.

---
 tools/testing/selftests/kvm/.gitignore        |  1 +
 tools/testing/selftests/kvm/Makefile          |  1 +
 .../kvm/x86_64/max_vcpuid_cap_test.c          | 59 +++++++++++++++++++
 3 files changed, 61 insertions(+)
 create mode 100644 tools/testing/selftests/kvm/x86_64/max_vcpuid_cap_test.c
tools/testing/selftests/kvm/.gitignore +1 -0
index d1e8f5237469..b860dcfee920 100644
@@ -22,6 +22,7 @@
 /x86_64/hyperv_cpuid
 /x86_64/hyperv_features
 /x86_64/hyperv_svm_test
+/x86_64/max_vcpuid_cap_test
 /x86_64/mmio_warning_test
 /x86_64/mmu_role_test
 /x86_64/platform_info_test
tools/testing/selftests/kvm/Makefile +1 -0
index 21c2dbd21a81..e92dc78de4d0 100644
@@ -86,6 +86,7 @@ TEST_GEN_PROGS_x86_64 += x86_64/xen_shinfo_test
 TEST_GEN_PROGS_x86_64 += x86_64/xen_vmcall_test
 TEST_GEN_PROGS_x86_64 += x86_64/sev_migrate_tests
 TEST_GEN_PROGS_x86_64 += x86_64/amx_test
+TEST_GEN_PROGS_x86_64 += x86_64/max_vcpuid_cap_test
 TEST_GEN_PROGS_x86_64 += access_tracking_perf_test
 TEST_GEN_PROGS_x86_64 += demand_paging_test
 TEST_GEN_PROGS_x86_64 += dirty_log_test
tools/testing/selftests/kvm/x86_64/max_vcpuid_cap_test.c new file +59 -0

59 lines not on this page · raw message

Signature · 1 line
2.27.0
#1 lore raw

tools/testing/selftests/kvm/x86_64/max_vcpuid_cap_test.c line 24

Quoted 10 lines · Zeng Guang
tools/testing/selftests/kvm/x86_64/max_vcpuid_cap_test.c new file +10 -0
+int main(int argc, char *argv[])
+{
+	struct kvm_vm *vm;
+	struct kvm_enable_cap cap = { 0 };
+	int ret;
+
+	vm = vm_create(VM_MODE_DEFAULT, 0, O_RDWR);
+
+	/* Get KVM_CAP_MAX_VCPU_ID cap supported in KVM */
+	ret = vm_check_cap(vm, KVM_CAP_MAX_VCPU_ID);

Because it's trivial to do so, and will avoid a hardcoded "max", what about looping over all possible values? And then some arbitrary number of the max?

	max_nr_vcpus = vm_check_cap(vm, KVM_CAP_MAX_VCPU_ID);
	TEST_ASSERT(max_nr_vcpus > ???, ...)

	for (i = 0; i < max_nr_vcpus; i++) 
		vm = vm_create(VM_MODE_DEFAULT, 0, O_RDWR);

		vm_set_max_nr_vcpus(vm, 0);
		vm_create_invalid_vcpu(vm, 0);

		vm_set_max_nr_vcpus(vm, i);
		vm_set_max_nr_vcpus(vm, i);
		vm_create_invalid_vcpu(vm, i);
		vm_create_invalid_vcpu(vm, i + 1);

		vm_set_invalid_max_nr_vcpus(vm, 0);
		vm_set_invalid_max_nr_vcpus(vm, i + 1);
		vm_set_invalid_max_nr_vcpus(vm, i - 1);
		vm_set_invalid_max_nr_vcpus(vm, max_nr_vcpus);

		close(vm->fd);
	}

	for ( ; i < max_nr_vcpus + 100; i++) {
		vm = vm_create(VM_MODE_DEFAULT, 0, O_RDWR);

		vm_set_invalid_max_nr_vcpus(vm, i);

		close(vm->fd);
	}

tools/testing/selftests/kvm/x86_64/max_vcpuid_cap_test.c line 29

Quoted 5 lines
tools/testing/selftests/kvm/x86_64/max_vcpuid_cap_test.c new file +5 -0
+
+	/* Check failure if set KVM_CAP_MAX_VCPU_ID beyond KVM cap */
+	cap.cap = KVM_CAP_MAX_VCPU_ID;
+	cap.args[0] = ret + 1;
+	ret = ioctl(vm->fd, KVM_ENABLE_CAP, &cap);

A helper or two to set the cap would be, uh, helpful :-) See above for ideas.

tools/testing/selftests/kvm/x86_64/max_vcpuid_cap_test.c line 32

Quoted 3 lines
tools/testing/selftests/kvm/x86_64/max_vcpuid_cap_test.c new file +3 -0
+	TEST_ASSERT(ret < 0,
+		    "Unexpected success to enable KVM_CAP_MAX_VCPU_ID"
+		    "beyond KVM cap!\n");

Please don't wrap quoted strings. Shorten the string and/or let the line run long. For the string/message, prioritize information that the user _can't_ get from looking at the code, and info that is highly relevant to the expectations. E.g. print the the return value, the errno, and the allegedly bad value.

It's definitely helpful to provide context too (KVM Unit Tests drive me bonkers for their terse messages), but for cases like this, it's redundant. "Unexpected success" is redundant because the "ret < 0" conveys that failure was expected, and hopefully most people will intuit that the test was trying "to enable" KVM_CAP_MAX_VCPU_ID. If not, a quick glance at the code (file and line provided) will give them that info.

E.g. assuming this ends up in a helper, something like

	TEST_ASSERT(ret == -1 && errno == EINVAL,
		    "KVM_CAP_MAX_VCPU_ID bug, max ID = %d, ret = %d, errno = %d (%s),
		    max_id, errno, strerror(errno));

IMO it's worth checking errno to reduce the probability of false pass, e.g. if the ioctl() is rejected for some other reason due to a test bug.

Quoted 6 lines
tools/testing/selftests/kvm/x86_64/max_vcpuid_cap_test.c new file +6 -0
+
+	/* Check success if set KVM_CAP_MAX_VCPU_ID */
+	cap.args[0] = MAX_VCPU_ID;
+	ret = ioctl(vm->fd, KVM_ENABLE_CAP, &cap);
+	TEST_ASSERT(ret == 0,
+		    "Unexpected failure to enable KVM_CAP_MAX_VCPU_ID!\n");