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.
Signed-off-by: David Woodhouse <[email protected]> --- 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 diff --git a/tools/testing/selftests/kvm/Makefile.kvm b/tools/testing/selftests/kvm/Makefile.kvm index cdc6039d32d2..2926817baad0 100644 --- a/tools/testing/selftests/kvm/Makefile.kvm +++ b/tools/testing/selftests/kvm/Makefile.kvm @@ -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 diff --git a/tools/testing/selftests/kvm/x86/planes_test.c b/tools/testing/selftests/kvm/x86/planes_test.c new file mode 100644 index 000000000000..f7db6cb6807d --- /dev/null +++ b/tools/testing/selftests/kvm/x86/planes_test.c @@ -0,0 +1,137 @@ +// 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. + */ +#include <errno.h> +#include <string.h> + +#include "kvm_util.h" + +/* + * Whether this host lets a VM have more than one plane: the opt-in also + * fails without two-dimensional paging, which the system fd cannot show. + */ +static bool have_multiple_planes(void) +{ + struct kvm_vm *vm = vm_create_barebones_irqchip(true); + bool ok = !__vm_enable_cap(vm, KVM_CAP_PLANES, 2); + + kvm_vm_free(vm); + return ok; +} + +static void test_enable_cap_bounds(void) +{ + int max_planes = kvm_check_cap(KVM_CAP_PLANES); + struct kvm_vm *vm; + + /* Planes require a split irqchip: without one the opt-in fails. */ + vm = vm_create_barebones(); + TEST_ASSERT_EQ(__vm_enable_cap(vm, KVM_CAP_PLANES, 2), -1); + TEST_ASSERT_EQ(errno, EINVAL); + kvm_vm_free(vm); + + /* + * With one, the system fd's value is the most a VM may be given, + * and a full in-kernel irqchip cannot replace the split one later. + */ + vm = vm_create_barebones_irqchip(true); + + TEST_ASSERT_EQ(__vm_enable_cap(vm, KVM_CAP_PLANES, 0), -1); + TEST_ASSERT_EQ(errno, EINVAL); + + TEST_ASSERT(max_planes > 1 && max_planes <= KVM_MAX_PLANES, + "system fd KVM_CAP_PLANES ceiling %d", max_planes); + TEST_ASSERT_EQ(__vm_enable_cap(vm, KVM_CAP_PLANES, max_planes + 1), -1); + TEST_ASSERT_EQ(errno, EINVAL); + + vm_enable_cap(vm, KVM_CAP_PLANES, max_planes); + TEST_ASSERT_EQ(vm_check_cap(vm, KVM_CAP_PLANES), max_planes); + + /* ENOTTY where KVM_CREATE_IRQCHIP is compiled out (KVM_IOAPIC=n). */ + TEST_ASSERT_EQ(__vm_ioctl(vm, KVM_CREATE_IRQCHIP, NULL), -1); + TEST_ASSERT(errno == EEXIST || errno == ENOTTY, + "KVM_CREATE_IRQCHIP after the opt-in: errno %d", errno); + + kvm_vm_free(vm); +} + +static void test_enable_cap_too_late(void) +{ + struct kvm_vm *vm = vm_create_barebones_irqchip(true); + + __vm_vcpu_add(vm, 0); + + TEST_ASSERT_EQ(__vm_enable_cap(vm, KVM_CAP_PLANES, 2), -1); + TEST_ASSERT_EQ(errno, EEXIST); + + kvm_vm_free(vm); +} + +static void test_plane_lifecycle(void) +{ + struct kvm_vm *vm = vm_create_barebones(); + struct kvm_plane_vcpu *vcpu1; + struct kvm_regs regs0, regs1; + struct kvm_plane *plane; + struct kvm_vcpu *vcpu0; + int r; + + vm_enable_split_irqchip(vm); + + /* Without the opt-in a VM has a single plane. */ + TEST_ASSERT_EQ(vm_check_cap(vm, KVM_CAP_PLANES), 1); + TEST_ASSERT_EQ(__vm_ioctl(vm, KVM_CREATE_PLANE, (void *)1UL), -1); + TEST_ASSERT_EQ(errno, EINVAL); + + vm_enable_cap(vm, KVM_CAP_PLANES, 2); + TEST_ASSERT_EQ(vm_check_cap(vm, KVM_CAP_PLANES), 2); + + /* The configured count is a hard bound, and reconfiguration a hard no. */ + TEST_ASSERT_EQ(__vm_ioctl(vm, KVM_CREATE_PLANE, (void *)2UL), -1); + TEST_ASSERT_EQ(errno, EINVAL); + + plane = vm_plane_add(vm, 1); + + TEST_ASSERT_EQ(__vm_enable_cap(vm, KVM_CAP_PLANES, 3), -1); + TEST_ASSERT_EQ(errno, EEXIST); + + /* A plane fd takes plane ioctls only; capabilities are a VM matter. */ + r = __plane_ioctl(plane, KVM_CHECK_EXTENSION, (void *)KVM_CAP_PLANES); + TEST_ASSERT(r == -1 && errno == ENOTTY, + "KVM_CHECK_EXTENSION on a plane fd, expecting ENOTTY. ret: %d, errno: %d", + r, errno); + + vcpu0 = __vm_vcpu_add(vm, 0); + vcpu1 = __vm_plane_vcpu_add(vcpu0, plane); + + /* Register state is per plane: write plane 1, plane 0 unaffected. */ + plane_vcpu_ioctl(vcpu1, KVM_GET_REGS, ®s1); + regs1.rbx = 0x5157ca11; /* arbitrary marker */ + plane_vcpu_ioctl(vcpu1, KVM_SET_REGS, ®s1); + + vcpu_ioctl(vcpu0, KVM_GET_REGS, ®s0); + TEST_ASSERT(regs0.rbx != 0x5157ca11, + "plane 0 RBX changed by plane 1 KVM_SET_REGS"); + + memset(®s1, 0, sizeof(regs1)); + plane_vcpu_ioctl(vcpu1, KVM_GET_REGS, ®s1); + TEST_ASSERT_EQ(regs1.rbx, 0x5157ca11); + + kvm_vm_free(vm); +} + +int main(int argc, char *argv[]) +{ + TEST_REQUIRE(kvm_has_cap(KVM_CAP_PLANES)); + TEST_REQUIRE(kvm_has_cap(KVM_CAP_SPLIT_IRQCHIP)); + TEST_REQUIRE(have_multiple_planes()); + + test_enable_cap_bounds(); + test_enable_cap_too_late(); + test_plane_lifecycle(); + + return 0; +} -- 2.50.1
