Skip to contentSkip to conversation

selftests: KVM: Test offset-based counter controls

PATCH Applied Oliver Upton 3 participants 14 messages Sep 16, 2021 – Oct 5, 2021

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

v8 cover selftests: KVM: Test offset-based counter controls

This series implements new tests for the x86 and arm64 counter migration changes that I've mailed out. These are sent separately as a dependent change since there are cross-arch dependencies here.

Patch 1 yanks the pvclock headers into the tools/ directory so we can make use of them within a KVM selftest guest.

Patch 2 tests the new capabilities of the KVM_*_CLOCK ioctls, ensuring that the kernel accounts for elapsed time when restoring the KVM clock.

Patches 3-4 add some device attribute helpers and clean up some mistakes in the assertions thereof.

Patch 5 implements a test for the KVM_VCPU_TSC_OFFSET attribute, asserting that manipulation of the offset results in correct TSC values within the guest.

Patch 6 adds basic arm64 support to the counter offset test, checking that the virtual counter-timer offset works correctly. Patch 7 does the same for the physical counter-timer offset.

Patch 8 adds a benchmark for physical counter offsetting, since most implementations available right now will rely on emulation.

Lastly, patch 9 extends the get-reg-list test to check for KVM_REG_ARM_TIMER_OFFSET if userspace opts-in to the kernel capability.

This series applies cleanly to 5.15-rc1

Tests were ran against the respective architecture changes on the following systems:

- Haswell (x86)
- Ampere Mt. Jade (non-ECV, nVHE and VHE)

v7: https://lore.kernel.org/r/[email protected]

v7 -> v8:
- Rebased to 5.15-rc1
- Dropped helper for checking if reg exists in reg list (no longer necessary)
- Test and enable KVM_CAP_ARM_VTIMER_OFFSET
- Add get-reg-list changes

Oliver Upton (9):
tools: arch: x86: pull in pvclock headers
selftests: KVM: Add test for KVM_{GET,SET}_CLOCK
selftests: KVM: Fix kvm device helper ioctl assertions
selftests: KVM: Add helpers for vCPU device attributes
selftests: KVM: Introduce system counter offset test
selftests: KVM: Add support for aarch64 to system_counter_offset_test
selftests: KVM: Test physical counter offsetting
selftests: KVM: Add counter emulation benchmark
selftests: KVM: Test vtimer offset reg in get-reg-list

 tools/arch/x86/include/asm/pvclock-abi.h      |  48 ++++
 tools/arch/x86/include/asm/pvclock.h          | 103 ++++++++
 tools/testing/selftests/kvm/.gitignore        |   3 +
 tools/testing/selftests/kvm/Makefile          |   4 +
 .../kvm/aarch64/counter_emulation_benchmark.c | 207 ++++++++++++++++
 .../selftests/kvm/aarch64/get-reg-list.c      |  42 ++++
 .../selftests/kvm/include/aarch64/processor.h |  24 ++
 .../testing/selftests/kvm/include/kvm_util.h  |  11 +
 tools/testing/selftests/kvm/lib/kvm_util.c    |  44 +++-
 .../kvm/system_counter_offset_test.c          | 220 ++++++++++++++++++
 .../selftests/kvm/x86_64/kvm_clock_test.c     | 204 ++++++++++++++++
 11 files changed, 907 insertions(+), 3 deletions(-)
 create mode 100644 tools/arch/x86/include/asm/pvclock-abi.h
 create mode 100644 tools/arch/x86/include/asm/pvclock.h
 create mode 100644 tools/testing/selftests/kvm/aarch64/counter_emulation_benchmark.c
 create mode 100644 tools/testing/selftests/kvm/system_counter_offset_test.c
 create mode 100644 tools/testing/selftests/kvm/x86_64/kvm_clock_test.c
Signature · 6 lines
2.33.0.464.g1972c5931b-goog

_______________________________________________
kvmarm mailing list
[email protected]
https://lists.cs.columbia.edu/mailman/listinfo/kvmarm
#1 lore raw

PATCH v8 1/9 tools: arch: x86: pull in pvclock headers

Copy over approximately clean versions of the pvclock headers into tools. Reconcile headers/symbols missing in tools that are unneeded.

---
 tools/arch/x86/include/asm/pvclock-abi.h |  48 +++++++++++
 tools/arch/x86/include/asm/pvclock.h     | 103 +++++++++++++++++++++++
 2 files changed, 151 insertions(+)
 create mode 100644 tools/arch/x86/include/asm/pvclock-abi.h
 create mode 100644 tools/arch/x86/include/asm/pvclock.h
tools/arch/x86/include/asm/pvclock-abi.h new file +48 -0
new file mode 100644
index 000000000000..1436226efe3e
@@ -0,0 +1,48 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+#ifndef _ASM_X86_PVCLOCK_ABI_H
+#define _ASM_X86_PVCLOCK_ABI_H
+#ifndef __ASSEMBLY__
+
+/*
+ * These structs MUST NOT be changed.
+ * They are the ABI between hypervisor and guest OS.
+ * Both Xen and KVM are using this.
+ *
+ * pvclock_vcpu_time_info holds the system time and the tsc timestamp
+ * of the last update. So the guest can use the tsc delta to get a
+ * more precise system time.  There is one per virtual cpu.
+ *
+ * pvclock_wall_clock references the point in time when the system
+ * time was zero (usually boot time), thus the guest calculates the
+ * current wall clock by adding the system time.
+ *
+ * Protocol for the "version" fields is: hypervisor raises it (making
+ * it uneven) before it starts updating the fields and raises it again
+ * (making it even) when it is done.  Thus the guest can make sure the
+ * time values it got are consistent by checking the version before
+ * and after reading them.
+ */
+
+struct pvclock_vcpu_time_info {
+	u32   version;
+	u32   pad0;
+	u64   tsc_timestamp;
+	u64   system_time;
+	u32   tsc_to_system_mul;
+	s8    tsc_shift;
+	u8    flags;
+	u8    pad[2];
+} __attribute__((__packed__)); /* 32 bytes */
+
+struct pvclock_wall_clock {
+	u32   version;
+	u32   sec;
+	u32   nsec;
+} __attribute__((__packed__));
+
+#define PVCLOCK_TSC_STABLE_BIT	(1 << 0)
+#define PVCLOCK_GUEST_STOPPED	(1 << 1)
+/* PVCLOCK_COUNTS_FROM_ZERO broke ABI and can't be used anymore. */
+#define PVCLOCK_COUNTS_FROM_ZERO (1 << 2)
+#endif /* __ASSEMBLY__ */
+#endif /* _ASM_X86_PVCLOCK_ABI_H */
tools/arch/x86/include/asm/pvclock.h new file +103 -0

103 lines not on this page · raw message

Signature · 6 lines
2.33.0.464.g1972c5931b-goog

_______________________________________________
kvmarm mailing list
[email protected]
https://lists.cs.columbia.edu/mailman/listinfo/kvmarm
#2 lore raw

PATCH v8 2/9 selftests: KVM: Add test for KVM_{GET,SET}_CLOCK

In reply to Oliver Upton

Add a selftest for the new KVM clock UAPI that was introduced. Ensure that the KVM clock is consistent between userspace and the guest, and that the difference in realtime will only ever cause the KVM clock to advance forward.

---
 tools/testing/selftests/kvm/.gitignore        |   1 +
 tools/testing/selftests/kvm/Makefile          |   1 +
 .../testing/selftests/kvm/include/kvm_util.h  |   2 +
 .../selftests/kvm/x86_64/kvm_clock_test.c     | 204 ++++++++++++++++++
 4 files changed, 208 insertions(+)
 create mode 100644 tools/testing/selftests/kvm/x86_64/kvm_clock_test.c
tools/testing/selftests/kvm/.gitignore +1 -0
index 98053d3afbda..86a063d1cd3e 100644
@@ -12,6 +12,7 @@
 /x86_64/emulator_error_test
 /x86_64/get_cpuid_test
 /x86_64/get_msr_index_features
+/x86_64/kvm_clock_test
 /x86_64/kvm_pv_test
 /x86_64/hyperv_clock
 /x86_64/hyperv_cpuid
tools/testing/selftests/kvm/Makefile +1 -0
index 5d05801ab816..1f969b0192f6 100644
@@ -46,6 +46,7 @@ TEST_GEN_PROGS_x86_64 += x86_64/get_cpuid_test
 TEST_GEN_PROGS_x86_64 += x86_64/hyperv_clock
 TEST_GEN_PROGS_x86_64 += x86_64/hyperv_cpuid
 TEST_GEN_PROGS_x86_64 += x86_64/hyperv_features
+TEST_GEN_PROGS_x86_64 += x86_64/kvm_clock_test
 TEST_GEN_PROGS_x86_64 += x86_64/kvm_pv_test
 TEST_GEN_PROGS_x86_64 += x86_64/mmio_warning_test
 TEST_GEN_PROGS_x86_64 += x86_64/mmu_role_test
tools/testing/selftests/kvm/include/kvm_util.h +2 -0
index 010b59b13917..a8ac5d52e17b 100644
@@ -19,6 +19,8 @@
 #define KVM_DEV_PATH "/dev/kvm"
 #define KVM_MAX_VCPUS 512
 
+#define NSEC_PER_SEC 1000000000L
+
 /*
  * Callers of kvm_util only have an incomplete/opaque description of the
  * structure kvm_util is using to maintain the state of a VM.
tools/testing/selftests/kvm/x86_64/kvm_clock_test.c new file +204 -0

204 lines not on this page · raw message

Signature · 6 lines
2.33.0.464.g1972c5931b-goog

_______________________________________________
kvmarm mailing list
[email protected]
https://lists.cs.columbia.edu/mailman/listinfo/kvmarm
#4 lore raw

PATCH v8 3/9 selftests: KVM: Fix kvm device helper ioctl assertions

In reply to Oliver Upton

The KVM_CREATE_DEVICE and KVM_{GET,SET}_DEVICE_ATTR ioctls are defined to return a value of zero on success. As such, tighten the assertions in the helper functions to only pass if the return code is zero.

---
 tools/testing/selftests/kvm/lib/kvm_util.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
tools/testing/selftests/kvm/lib/kvm_util.c +3 -3
index 10a8ed691c66..0ffc2d39c80d 100644
@@ -1984,7 +1984,7 @@ int kvm_device_check_attr(int dev_fd, uint32_t group, uint64_t attr)
 {
 	int ret = _kvm_device_check_attr(dev_fd, group, attr);
 
-	TEST_ASSERT(ret >= 0, "KVM_HAS_DEVICE_ATTR failed, rc: %i errno: %i", ret, errno);
+	TEST_ASSERT(!ret, "KVM_HAS_DEVICE_ATTR failed, rc: %i errno: %i", ret, errno);
 	return ret;
 }
 
@@ -2008,7 +2008,7 @@ int kvm_create_device(struct kvm_vm *vm, uint64_t type, bool test)
 	ret = _kvm_create_device(vm, type, test, &fd);
 
 	if (!test) {
-		TEST_ASSERT(ret >= 0,
+		TEST_ASSERT(!ret,
 			    "KVM_CREATE_DEVICE IOCTL failed, rc: %i errno: %i", ret, errno);
 		return fd;
 	}
@@ -2036,7 +2036,7 @@ int kvm_device_access(int dev_fd, uint32_t group, uint64_t attr,
 {
 	int ret = _kvm_device_access(dev_fd, group, attr, val, write);
 
-	TEST_ASSERT(ret >= 0, "KVM_SET|GET_DEVICE_ATTR IOCTL failed, rc: %i errno: %i", ret, errno);
+	TEST_ASSERT(!ret, "KVM_SET|GET_DEVICE_ATTR IOCTL failed, rc: %i errno: %i", ret, errno);
 	return ret;
 }
 
Signature · 6 lines
2.33.0.464.g1972c5931b-goog

_______________________________________________
kvmarm mailing list
[email protected]
https://lists.cs.columbia.edu/mailman/listinfo/kvmarm
#5 lore raw

PATCH v8 4/9 selftests: KVM: Add helpers for vCPU device attributes

In reply to Oliver Upton

vCPU file descriptors are abstracted away from test code in KVM selftests, meaning that tests cannot directly access a vCPU's device attributes. Add helpers that tests can use to get at vCPU device attributes.

---
 .../testing/selftests/kvm/include/kvm_util.h  |  9 +++++
 tools/testing/selftests/kvm/lib/kvm_util.c    | 38 +++++++++++++++++++
 2 files changed, 47 insertions(+)
tools/testing/selftests/kvm/include/kvm_util.h +9 -0
index a8ac5d52e17b..1b3ef5757819 100644
@@ -240,6 +240,15 @@ int _kvm_device_access(int dev_fd, uint32_t group, uint64_t attr,
 int kvm_device_access(int dev_fd, uint32_t group, uint64_t attr,
 		      void *val, bool write);
 
+int _vcpu_has_device_attr(struct kvm_vm *vm, uint32_t vcpuid, uint32_t group,
+			  uint64_t attr);
+int vcpu_has_device_attr(struct kvm_vm *vm, uint32_t vcpuid, uint32_t group,
+			 uint64_t attr);
+int _vcpu_access_device_attr(struct kvm_vm *vm, uint32_t vcpuid, uint32_t group,
+			  uint64_t attr, void *val, bool write);
+int vcpu_access_device_attr(struct kvm_vm *vm, uint32_t vcpuid, uint32_t group,
+			 uint64_t attr, void *val, bool write);
+
 const char *exit_reason_str(unsigned int exit_reason);
 
 void virt_pgd_alloc(struct kvm_vm *vm);
tools/testing/selftests/kvm/lib/kvm_util.c +38 -0
index 0ffc2d39c80d..0fe66ca6139a 100644
@@ -2040,6 +2040,44 @@ int kvm_device_access(int dev_fd, uint32_t group, uint64_t attr,
 	return ret;
 }
 
+int _vcpu_has_device_attr(struct kvm_vm *vm, uint32_t vcpuid, uint32_t group,
+			  uint64_t attr)
+{
+	struct vcpu *vcpu = vcpu_find(vm, vcpuid);
+
+	TEST_ASSERT(vcpu, "nonexistent vcpu id: %d", vcpuid);
+
+	return _kvm_device_check_attr(vcpu->fd, group, attr);
+}
+
+int vcpu_has_device_attr(struct kvm_vm *vm, uint32_t vcpuid, uint32_t group,
+				 uint64_t attr)
+{
+	int ret = _vcpu_has_device_attr(vm, vcpuid, group, attr);
+
+	TEST_ASSERT(!ret, "KVM_HAS_DEVICE_ATTR IOCTL failed, rc: %i errno: %i", ret, errno);
+	return ret;
+}
+
+int _vcpu_access_device_attr(struct kvm_vm *vm, uint32_t vcpuid, uint32_t group,
+			     uint64_t attr, void *val, bool write)
+{
+	struct vcpu *vcpu = vcpu_find(vm, vcpuid);
+
+	TEST_ASSERT(vcpu, "nonexistent vcpu id: %d", vcpuid);
+
+	return _kvm_device_access(vcpu->fd, group, attr, val, write);
+}
+
+int vcpu_access_device_attr(struct kvm_vm *vm, uint32_t vcpuid, uint32_t group,
+			    uint64_t attr, void *val, bool write)
+{
+	int ret = _vcpu_access_device_attr(vm, vcpuid, group, attr, val, write);
+
+	TEST_ASSERT(!ret, "KVM_SET|GET_DEVICE_ATTR IOCTL failed, rc: %i errno: %i", ret, errno);
+	return ret;
+}
+
 /*
  * VM Dump
  *
Signature · 6 lines
2.33.0.464.g1972c5931b-goog

_______________________________________________
kvmarm mailing list
[email protected]
https://lists.cs.columbia.edu/mailman/listinfo/kvmarm
#6 lore raw

PATCH v8 5/9 selftests: KVM: Introduce system counter offset test

In reply to Oliver Upton

Introduce a KVM selftest to verify that userspace manipulation of the TSC (via the new vCPU attribute) results in the correct behavior within the guest.

---
 tools/testing/selftests/kvm/.gitignore        |   1 +
 tools/testing/selftests/kvm/Makefile          |   1 +
 .../kvm/system_counter_offset_test.c          | 132 ++++++++++++++++++
 3 files changed, 134 insertions(+)
 create mode 100644 tools/testing/selftests/kvm/system_counter_offset_test.c
tools/testing/selftests/kvm/.gitignore +1 -0
index 86a063d1cd3e..aa5a5197716e 100644
@@ -52,3 +52,4 @@
 /set_memory_region_test
 /steal_time
 /kvm_binary_stats_test
+/system_counter_offset_test
tools/testing/selftests/kvm/Makefile +1 -0
index 1f969b0192f6..225803ac95bb 100644
@@ -84,6 +84,7 @@ TEST_GEN_PROGS_x86_64 += memslot_perf_test
 TEST_GEN_PROGS_x86_64 += set_memory_region_test
 TEST_GEN_PROGS_x86_64 += steal_time
 TEST_GEN_PROGS_x86_64 += kvm_binary_stats_test
+TEST_GEN_PROGS_x86_64 += system_counter_offset_test
 
 TEST_GEN_PROGS_aarch64 += aarch64/debug-exceptions
 TEST_GEN_PROGS_aarch64 += aarch64/get-reg-list
tools/testing/selftests/kvm/system_counter_offset_test.c new file +132 -0

132 lines not on this page · raw message

Signature · 6 lines
2.33.0.464.g1972c5931b-goog

_______________________________________________
kvmarm mailing list
[email protected]
https://lists.cs.columbia.edu/mailman/listinfo/kvmarm
#7 lore raw

PATCH v8 6/9 selftests: KVM: Add support for aarch64 to system_counter_offset_test

In reply to Oliver Upton

KVM/arm64 now allows userspace to adjust the guest virtual counter-timer via a vCPU register. Test that changes to the virtual counter-timer offset result in the correct view being presented to the guest.

---
 tools/testing/selftests/kvm/Makefile          |  1 +
 .../selftests/kvm/include/aarch64/processor.h | 12 ++++
 .../kvm/system_counter_offset_test.c          | 64 ++++++++++++++++++-
 3 files changed, 76 insertions(+), 1 deletion(-)
tools/testing/selftests/kvm/Makefile +1 -0
index 225803ac95bb..fd61d0063c50 100644
@@ -98,6 +98,7 @@ TEST_GEN_PROGS_aarch64 += kvm_page_table_test
 TEST_GEN_PROGS_aarch64 += set_memory_region_test
 TEST_GEN_PROGS_aarch64 += steal_time
 TEST_GEN_PROGS_aarch64 += kvm_binary_stats_test
+TEST_GEN_PROGS_aarch64 += system_counter_offset_test
 
 TEST_GEN_PROGS_s390x = s390x/memop
 TEST_GEN_PROGS_s390x += s390x/resets
tools/testing/selftests/kvm/include/aarch64/processor.h +12 -0
index c0273aefa63d..4c7472823df3 100644
@@ -132,4 +132,16 @@ void vm_install_sync_handler(struct kvm_vm *vm,
 
 #define isb()	asm volatile("isb" : : : "memory")
 
+static inline uint64_t read_cntvct_ordered(void)
+{
+	uint64_t r;
+
+	__asm__ __volatile__("isb\n\t"
+			     "mrs %0, cntvct_el0\n\t"
+			     "isb\n\t"
+			     : "=r"(r));
+
+	return r;
+}
+
 #endif /* SELFTEST_KVM_PROCESSOR_H */
tools/testing/selftests/kvm/system_counter_offset_test.c +63 -1

70 lines not on this page · raw message

Signature · 6 lines
2.33.0.464.g1972c5931b-goog

_______________________________________________
kvmarm mailing list
[email protected]
https://lists.cs.columbia.edu/mailman/listinfo/kvmarm
#10 lore raw

PATCH v8 9/9 selftests: KVM: Test vtimer offset reg in get-reg-list

In reply to Oliver Upton

Assert that KVM exposes KVM_REG_ARM_TIMER_OFFSET in the KVM_GET_REG_LIST ioctl when userspace buys in to the new behavior.

---
 .../selftests/kvm/aarch64/get-reg-list.c      | 42 +++++++++++++++++++
 1 file changed, 42 insertions(+)
tools/testing/selftests/kvm/aarch64/get-reg-list.c +42 -0
index cc898181faab..4f337d8b793a 100644
@@ -40,6 +40,7 @@ static __u64 *blessed_reg, blessed_n;
 struct reg_sublist {
 	const char *name;
 	long capability;
+	long enable_capability;
 	int feature;
 	bool finalize;
 	__u64 *regs;
@@ -397,6 +398,19 @@ static void check_supported(struct vcpu_config *c)
 	}
 }
 
+static void enable_caps(struct kvm_vm *vm, struct vcpu_config *c)
+{
+	struct kvm_enable_cap cap = {0};
+	struct reg_sublist *s;
+
+	for_each_sublist(c, s) {
+		if (s->enable_capability) {
+			cap.cap = s->enable_capability;
+			vm_enable_cap(vm, &cap);
+		}
+	}
+}
+
 static bool print_list;
 static bool print_filtered;
 static bool fixup_core_regs;
@@ -412,6 +426,8 @@ static void run_test(struct vcpu_config *c)
 	check_supported(c);
 
 	vm = vm_create(VM_MODE_DEFAULT, DEFAULT_GUEST_PHY_PAGES, O_RDWR);
+	enable_caps(vm, c);
+
 	prepare_vcpu_init(c, &init);
 	aarch64_vcpu_add_default(vm, 0, &init, NULL);
 	finalize_vcpu(vm, 0, c);
@@ -1014,6 +1030,10 @@ static __u64 sve_rejects_set[] = {
 	KVM_REG_ARM64_SVE_VLS,
 };
 
+static __u64 vtimer_offset_regs[] = {
+	KVM_REG_ARM_TIMER_OFFSET,
+};
+
 #define BASE_SUBLIST \
 	{ "base", .regs = base_regs, .regs_n = ARRAY_SIZE(base_regs), }
 #define VREGS_SUBLIST \
@@ -1025,6 +1045,10 @@ static __u64 sve_rejects_set[] = {
 	{ "sve", .capability = KVM_CAP_ARM_SVE, .feature = KVM_ARM_VCPU_SVE, .finalize = true, \
 	  .regs = sve_regs, .regs_n = ARRAY_SIZE(sve_regs), \
 	  .rejects_set = sve_rejects_set, .rejects_set_n = ARRAY_SIZE(sve_rejects_set), }
+#define VTIMER_OFFSET_SUBLIST \
+	{ "vtimer_offset", .capability = KVM_CAP_ARM_VTIMER_OFFSET, \
+	  .enable_capability = KVM_CAP_ARM_VTIMER_OFFSET, .regs = vtimer_offset_regs, \
+	  .regs_n = ARRAY_SIZE(vtimer_offset_regs), }
 
 static struct vcpu_config vregs_config = {
 	.sublists = {
@@ -1041,6 +1065,14 @@ static struct vcpu_config vregs_pmu_config = {
 	{0},
 	},
 };
+static struct vcpu_config vregs_vtimer_config = {
+	.sublists = {
+	BASE_SUBLIST,
+	VREGS_SUBLIST,
+	VTIMER_OFFSET_SUBLIST,
+	{0},
+	},
+};
 static struct vcpu_config sve_config = {
 	.sublists = {
 	BASE_SUBLIST,
@@ -1056,11 +1088,21 @@ static struct vcpu_config sve_pmu_config = {
 	{0},
 	},
 };
+static struct vcpu_config sve_vtimer_config = {
+	.sublists = {
+	BASE_SUBLIST,
+	SVE_SUBLIST,
+	VTIMER_OFFSET_SUBLIST,
+	{0},
+	},
+};
 
 static struct vcpu_config *vcpu_configs[] = {
 	&vregs_config,
 	&vregs_pmu_config,
+	&vregs_vtimer_config,
 	&sve_config,
 	&sve_pmu_config,
+	&sve_vtimer_config,
 };
 static int vcpu_configs_n = ARRAY_SIZE(vcpu_configs);
Signature · 6 lines
2.33.0.464.g1972c5931b-goog

_______________________________________________
kvmarm mailing list
[email protected]
https://lists.cs.columbia.edu/mailman/listinfo/kvmarm
#8 lore raw

PATCH v8 7/9 selftests: KVM: Test physical counter offsetting

In reply to Oliver Upton

Test that userspace adjustment of the guest physical counter-timer results in the correct view within the guest.

---
 .../selftests/kvm/include/aarch64/processor.h | 12 +++++++
 .../kvm/system_counter_offset_test.c          | 34 ++++++++++++++++---
 2 files changed, 42 insertions(+), 4 deletions(-)
tools/testing/selftests/kvm/include/aarch64/processor.h +12 -0
index 4c7472823df3..57b7802cf9e2 100644
@@ -144,4 +144,16 @@ static inline uint64_t read_cntvct_ordered(void)
 	return r;
 }
 
+static inline uint64_t read_cntpct_ordered(void)
+{
+	uint64_t r;
+
+	__asm__ __volatile__("isb\n\t"
+			     "mrs %0, cntpct_el0\n\t"
+			     "isb\n\t"
+			     : "=r"(r));
+
+	return r;
+}
+
 #endif /* SELFTEST_KVM_PROCESSOR_H */
tools/testing/selftests/kvm/system_counter_offset_test.c +30 -4
index 7ea406fdd56f..757b5b2e960e 100644
@@ -57,6 +57,9 @@ static uint64_t host_read_guest_system_counter(struct test_case *test)
 
 enum arch_counter {
 	VIRTUAL,
+	PHYSICAL,
+	/* offset physical, read virtual */
+	PHYSICAL_READ_VIRTUAL,
 };
 
 struct test_case {
@@ -68,6 +71,12 @@ static struct test_case test_cases[] = {
 	{ .counter = VIRTUAL, .offset = 0 },
 	{ .counter = VIRTUAL, .offset = 180 * NSEC_PER_SEC },
 	{ .counter = VIRTUAL, .offset = -180 * NSEC_PER_SEC },
+	{ .counter = PHYSICAL, .offset = 0 },
+	{ .counter = PHYSICAL, .offset = 180 * NSEC_PER_SEC },
+	{ .counter = PHYSICAL, .offset = -180 * NSEC_PER_SEC },
+	{ .counter = PHYSICAL_READ_VIRTUAL, .offset = 0 },
+	{ .counter = PHYSICAL_READ_VIRTUAL, .offset = 180 * NSEC_PER_SEC },
+	{ .counter = PHYSICAL_READ_VIRTUAL, .offset = -180 * NSEC_PER_SEC },
 };
 
 static void check_preconditions(struct kvm_vm *vm)
@@ -76,32 +85,49 @@ static void check_preconditions(struct kvm_vm *vm)
 		.cap = KVM_CAP_ARM_VTIMER_OFFSET,
 	};
 
-	if (vcpu_has_reg(vm, VCPU_ID, KVM_REG_ARM_TIMER_OFFSET))
-		return;
-
 	if (!kvm_check_cap(KVM_CAP_ARM_VTIMER_OFFSET)) {
 		print_skip("KVM_REG_ARM_TIMER_OFFSET not supported");
 		exit(KSFT_SKIP);
 	}
 
 	vm_enable_cap(vm, &cap);
+
+	if (_vcpu_has_device_attr(vm, VCPU_ID, KVM_ARM_VCPU_TIMER_CTRL,
+				  KVM_ARM_VCPU_TIMER_PHYS_OFFSET)) {
+		print_skip("KVM_ARM_VCPU_TIMER_PHYS_OFFSET not supported");
+		exit(KSFT_SKIP);
+	}
 }
 
 static void setup_system_counter(struct kvm_vm *vm, struct test_case *test)
 {
+	uint64_t cntvoff, cntpoff;
 	struct kvm_one_reg reg = {
 		.id = KVM_REG_ARM_TIMER_OFFSET,
-		.addr = (__u64)&test->offset,
+		.addr = (__u64)&cntvoff,
 	};
 
+	if (test->counter == VIRTUAL) {
+		cntvoff = test->offset;
+		cntpoff = 0;
+	} else {
+		cntvoff = 0;
+		cntpoff = test->offset;
+	}
+
 	vcpu_set_reg(vm, VCPU_ID, &reg);
+	vcpu_access_device_attr(vm, VCPU_ID, KVM_ARM_VCPU_TIMER_CTRL,
+				KVM_ARM_VCPU_TIMER_PHYS_OFFSET, &cntpoff, true);
 }
 
 static uint64_t guest_read_system_counter(struct test_case *test)
 {
 	switch (test->counter) {
 	case VIRTUAL:
+	case PHYSICAL_READ_VIRTUAL:
 		return read_cntvct_ordered();
+	case PHYSICAL:
+		return read_cntpct_ordered();
 	default:
 		GUEST_ASSERT(0);
 	}
Signature · 6 lines
2.33.0.464.g1972c5931b-goog

_______________________________________________
kvmarm mailing list
[email protected]
https://lists.cs.columbia.edu/mailman/listinfo/kvmarm
#9 lore raw

PATCH v8 8/9 selftests: KVM: Add counter emulation benchmark

In reply to Oliver Upton

Add a test case for counter emulation on arm64. A side effect of how KVM handles physical counter offsetting on non-ECV systems is that the virtual counter will always hit hardware and the physical could be emulated. Force emulation by writing a nonzero offset to the physical counter and compare the elapsed cycles to a direct read of the hardware register.

---
 tools/testing/selftests/kvm/.gitignore        |   1 +
 tools/testing/selftests/kvm/Makefile          |   1 +
 .../kvm/aarch64/counter_emulation_benchmark.c | 207 ++++++++++++++++++
 3 files changed, 209 insertions(+)
 create mode 100644 tools/testing/selftests/kvm/aarch64/counter_emulation_benchmark.c
tools/testing/selftests/kvm/.gitignore +1 -0
index aa5a5197716e..ae2564b27af0 100644
@@ -1,4 +1,5 @@
 # SPDX-License-Identifier: GPL-2.0-only
+/aarch64/counter_emulation_benchmark
 /aarch64/debug-exceptions
 /aarch64/get-reg-list
 /aarch64/psci_cpu_on_test
tools/testing/selftests/kvm/Makefile +1 -0
index fd61d0063c50..3c80899b40ee 100644
@@ -86,6 +86,7 @@ TEST_GEN_PROGS_x86_64 += steal_time
 TEST_GEN_PROGS_x86_64 += kvm_binary_stats_test
 TEST_GEN_PROGS_x86_64 += system_counter_offset_test
 
+TEST_GEN_PROGS_aarch64 += aarch64/counter_emulation_benchmark
 TEST_GEN_PROGS_aarch64 += aarch64/debug-exceptions
 TEST_GEN_PROGS_aarch64 += aarch64/get-reg-list
 TEST_GEN_PROGS_aarch64 += aarch64/psci_cpu_on_test
tools/testing/selftests/kvm/aarch64/counter_emulation_benchmark.c new file +207 -0

207 lines not on this page · raw message

Signature · 6 lines
2.33.0.464.g1972c5931b-goog

_______________________________________________
kvmarm mailing list
[email protected]
https://lists.cs.columbia.edu/mailman/listinfo/kvmarm
Reviewed-by on v8 9/9
#11 lore raw

Re: [PATCH v8 9/9] selftests: KVM: Test vtimer offset reg in get-reg-list

In reply to Oliver Upton
Quoted 13 lines · Oliver Upton

Assert that KVM exposes KVM_REG_ARM_TIMER_OFFSET in the KVM_GET_REG_LIST ioctl when userspace buys in to the new behavior.

---
 .../selftests/kvm/aarch64/get-reg-list.c      | 42 +++++++++++++++++++
 1 file changed, 42 insertions(+)
tools/testing/selftests/kvm/aarch64/get-reg-list.c +0 -0
index cc898181faab..4f337d8b793a 100644

tools/testing/selftests/kvm/aarch64/get-reg-list.c static __u64 *blessed_reg, blessed_n; · line 43

Quoted 4 lines
tools/testing/selftests/kvm/aarch64/get-reg-list.c +1 -0
@@ -40,6 +40,7 @@ static __u64 *blessed_reg, blessed_n;
 struct reg_sublist {
 	const char *name;
 	long capability;
+	long enable_capability;

cap.cap is a __u32

Quoted 94 lines
tools/testing/selftests/kvm/aarch64/get-reg-list.c +41 -0
 	int feature;
 	bool finalize;
 	__u64 *regs;
@@ -397,6 +398,19 @@ static void check_supported(struct vcpu_config *c)
 	}
 }
 
+static void enable_caps(struct kvm_vm *vm, struct vcpu_config *c)
+{
+	struct kvm_enable_cap cap = {0};
+	struct reg_sublist *s;
+
+	for_each_sublist(c, s) {
+		if (s->enable_capability) {
+			cap.cap = s->enable_capability;
+			vm_enable_cap(vm, &cap);
+		}
+	}
+}
+
 static bool print_list;
 static bool print_filtered;
 static bool fixup_core_regs;
@@ -412,6 +426,8 @@ static void run_test(struct vcpu_config *c)
 	check_supported(c);
 
 	vm = vm_create(VM_MODE_DEFAULT, DEFAULT_GUEST_PHY_PAGES, O_RDWR);
+	enable_caps(vm, c);
+
 	prepare_vcpu_init(c, &init);
 	aarch64_vcpu_add_default(vm, 0, &init, NULL);
 	finalize_vcpu(vm, 0, c);
@@ -1014,6 +1030,10 @@ static __u64 sve_rejects_set[] = {
 	KVM_REG_ARM64_SVE_VLS,
 };
 
+static __u64 vtimer_offset_regs[] = {
+	KVM_REG_ARM_TIMER_OFFSET,
+};
+
 #define BASE_SUBLIST \
 	{ "base", .regs = base_regs, .regs_n = ARRAY_SIZE(base_regs), }
 #define VREGS_SUBLIST \
@@ -1025,6 +1045,10 @@ static __u64 sve_rejects_set[] = {
 	{ "sve", .capability = KVM_CAP_ARM_SVE, .feature = KVM_ARM_VCPU_SVE, .finalize = true, \
 	  .regs = sve_regs, .regs_n = ARRAY_SIZE(sve_regs), \
 	  .rejects_set = sve_rejects_set, .rejects_set_n = ARRAY_SIZE(sve_rejects_set), }
+#define VTIMER_OFFSET_SUBLIST \
+	{ "vtimer_offset", .capability = KVM_CAP_ARM_VTIMER_OFFSET, \
+	  .enable_capability = KVM_CAP_ARM_VTIMER_OFFSET, .regs = vtimer_offset_regs, \
+	  .regs_n = ARRAY_SIZE(vtimer_offset_regs), }
 
 static struct vcpu_config vregs_config = {
 	.sublists = {
@@ -1041,6 +1065,14 @@ static struct vcpu_config vregs_pmu_config = {
 	{0},
 	},
 };
+static struct vcpu_config vregs_vtimer_config = {
+	.sublists = {
+	BASE_SUBLIST,
+	VREGS_SUBLIST,
+	VTIMER_OFFSET_SUBLIST,
+	{0},
+	},
+};
 static struct vcpu_config sve_config = {
 	.sublists = {
 	BASE_SUBLIST,
@@ -1056,11 +1088,21 @@ static struct vcpu_config sve_pmu_config = {
 	{0},
 	},
 };
+static struct vcpu_config sve_vtimer_config = {
+	.sublists = {
+	BASE_SUBLIST,
+	SVE_SUBLIST,
+	VTIMER_OFFSET_SUBLIST,
+	{0},
+	},
+};
 
 static struct vcpu_config *vcpu_configs[] = {
 	&vregs_config,
 	&vregs_pmu_config,
+	&vregs_vtimer_config,
 	&sve_config,
 	&sve_pmu_config,
+	&sve_vtimer_config,
 };
 static int vcpu_configs_n = ARRAY_SIZE(vcpu_configs);
Signature · 1 line
2.33.0.464.g1972c5931b-goog

Other than the enable_capability type nit

maintainer Applied
#12 lore raw
In reply to Oliver Upton
Quoted 27 lines · Oliver Upton

This series implements new tests for the x86 and arm64 counter migration changes that I've mailed out. These are sent separately as a dependent change since there are cross-arch dependencies here.

Patch 1 yanks the pvclock headers into the tools/ directory so we can make use of them within a KVM selftest guest.

Patch 2 tests the new capabilities of the KVM_*_CLOCK ioctls, ensuring that the kernel accounts for elapsed time when restoring the KVM clock.

Patches 3-4 add some device attribute helpers and clean up some mistakes in the assertions thereof.

Patch 5 implements a test for the KVM_VCPU_TSC_OFFSET attribute, asserting that manipulation of the offset results in correct TSC values within the guest.

Patch 6 adds basic arm64 support to the counter offset test, checking that the virtual counter-timer offset works correctly. Patch 7 does the same for the physical counter-timer offset.

Patch 8 adds a benchmark for physical counter offsetting, since most implementations available right now will rely on emulation.

Lastly, patch 9 extends the get-reg-list test to check for KVM_REG_ARM_TIMER_OFFSET if userspace opts-in to the kernel capability.

Quoted 7 lines

This series applies cleanly to 5.15-rc1

Tests were ran against the respective architecture changes on the following systems:

- Haswell (x86)
- Ampere Mt. Jade (non-ECV, nVHE and VHE)

Queued patches 1-5, thanks.

Paolo

Quoted 38 lines

v7: https://lore.kernel.org/r/[email protected]

v7 -> v8:
- Rebased to 5.15-rc1
- Dropped helper for checking if reg exists in reg list (no longer necessary)
- Test and enable KVM_CAP_ARM_VTIMER_OFFSET
- Add get-reg-list changes

Oliver Upton (9):
tools: arch: x86: pull in pvclock headers
selftests: KVM: Add test for KVM_{GET,SET}_CLOCK
selftests: KVM: Fix kvm device helper ioctl assertions
selftests: KVM: Add helpers for vCPU device attributes
selftests: KVM: Introduce system counter offset test
selftests: KVM: Add support for aarch64 to system_counter_offset_test
selftests: KVM: Test physical counter offsetting
selftests: KVM: Add counter emulation benchmark
selftests: KVM: Test vtimer offset reg in get-reg-list

  tools/arch/x86/include/asm/pvclock-abi.h      |  48 ++++
  tools/arch/x86/include/asm/pvclock.h          | 103 ++++++++
  tools/testing/selftests/kvm/.gitignore        |   3 +
  tools/testing/selftests/kvm/Makefile          |   4 +
  .../kvm/aarch64/counter_emulation_benchmark.c | 207 ++++++++++++++++
  .../selftests/kvm/aarch64/get-reg-list.c      |  42 ++++
  .../selftests/kvm/include/aarch64/processor.h |  24 ++
  .../testing/selftests/kvm/include/kvm_util.h  |  11 +
  tools/testing/selftests/kvm/lib/kvm_util.c    |  44 +++-
  .../kvm/system_counter_offset_test.c          | 220 ++++++++++++++++++
  .../selftests/kvm/x86_64/kvm_clock_test.c     | 204 ++++++++++++++++
  11 files changed, 907 insertions(+), 3 deletions(-)

create mode 100644 tools/arch/x86/include/asm/pvclock-abi.h
create mode 100644 tools/arch/x86/include/asm/pvclock.h
create mode 100644 tools/testing/selftests/kvm/aarch64/counter_emulation_benchmark.c create mode 100644 tools/testing/selftests/kvm/system_counter_offset_test.c create mode 100644 tools/testing/selftests/kvm/x86_64/kvm_clock_test.c

#3 lore raw

Re: [PATCH v8 2/9] selftests: KVM: Add test for KVM_{GET,SET}_CLOCK

In reply to Oliver Upton
Quoted 173 lines · Oliver Upton

Add a selftest for the new KVM clock UAPI that was introduced. Ensure that the KVM clock is consistent between userspace and the guest, and that the difference in realtime will only ever cause the KVM clock to advance forward.

---
 tools/testing/selftests/kvm/.gitignore        |   1 +
 tools/testing/selftests/kvm/Makefile          |   1 +
 .../testing/selftests/kvm/include/kvm_util.h  |   2 +
 .../selftests/kvm/x86_64/kvm_clock_test.c     | 204 ++++++++++++++++++
 4 files changed, 208 insertions(+)
 create mode 100644 tools/testing/selftests/kvm/x86_64/kvm_clock_test.c
tools/testing/selftests/kvm/.gitignore +1 -0
index 98053d3afbda..86a063d1cd3e 100644
@@ -12,6 +12,7 @@
 /x86_64/emulator_error_test
 /x86_64/get_cpuid_test
 /x86_64/get_msr_index_features
+/x86_64/kvm_clock_test
 /x86_64/kvm_pv_test
 /x86_64/hyperv_clock
 /x86_64/hyperv_cpuid
tools/testing/selftests/kvm/Makefile +1 -0
index 5d05801ab816..1f969b0192f6 100644
@@ -46,6 +46,7 @@ TEST_GEN_PROGS_x86_64 += x86_64/get_cpuid_test
 TEST_GEN_PROGS_x86_64 += x86_64/hyperv_clock
 TEST_GEN_PROGS_x86_64 += x86_64/hyperv_cpuid
 TEST_GEN_PROGS_x86_64 += x86_64/hyperv_features
+TEST_GEN_PROGS_x86_64 += x86_64/kvm_clock_test
 TEST_GEN_PROGS_x86_64 += x86_64/kvm_pv_test
 TEST_GEN_PROGS_x86_64 += x86_64/mmio_warning_test
 TEST_GEN_PROGS_x86_64 += x86_64/mmu_role_test
tools/testing/selftests/kvm/include/kvm_util.h +2 -0
index 010b59b13917..a8ac5d52e17b 100644
@@ -19,6 +19,8 @@
 #define KVM_DEV_PATH "/dev/kvm"
 #define KVM_MAX_VCPUS 512
 
+#define NSEC_PER_SEC 1000000000L
+
 /*
  * Callers of kvm_util only have an incomplete/opaque description of the
  * structure kvm_util is using to maintain the state of a VM.
tools/testing/selftests/kvm/x86_64/kvm_clock_test.c new file +115 -0
new file mode 100644
index 000000000000..e0dcc27ae9f1
@@ -0,0 +1,204 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * Copyright (C) 2021, Google LLC.
+ *
+ * Tests for adjusting the KVM clock from userspace
+ */
+#include <asm/kvm_para.h>
+#include <asm/pvclock.h>
+#include <asm/pvclock-abi.h>
+#include <stdint.h>
+#include <string.h>
+#include <sys/stat.h>
+#include <time.h>
+
+#include "test_util.h"
+#include "kvm_util.h"
+#include "processor.h"
+
+#define VCPU_ID 0
+
+struct test_case {
+	uint64_t kvmclock_base;
+	int64_t realtime_offset;
+};
+
+static struct test_case test_cases[] = {
+	{ .kvmclock_base = 0 },
+	{ .kvmclock_base = 180 * NSEC_PER_SEC },
+	{ .kvmclock_base = 0, .realtime_offset = -180 * NSEC_PER_SEC },
+	{ .kvmclock_base = 0, .realtime_offset = 180 * NSEC_PER_SEC },
+};
+
+#define GUEST_SYNC_CLOCK(__stage, __val)			\
+		GUEST_SYNC_ARGS(__stage, __val, 0, 0, 0)
+
+static void guest_main(vm_paddr_t pvti_pa, struct pvclock_vcpu_time_info *pvti)
+{
+	int i;
+
+	wrmsr(MSR_KVM_SYSTEM_TIME_NEW, pvti_pa | KVM_MSR_ENABLED);
+	for (i = 0; i < ARRAY_SIZE(test_cases); i++)
+		GUEST_SYNC_CLOCK(i, __pvclock_read_cycles(pvti, rdtsc()));
+}
+
+#define EXPECTED_FLAGS (KVM_CLOCK_REALTIME | KVM_CLOCK_HOST_TSC)
+
+static inline void assert_flags(struct kvm_clock_data *data)
+{
+	TEST_ASSERT((data->flags & EXPECTED_FLAGS) == EXPECTED_FLAGS,
+		    "unexpected clock data flags: %x (want set: %x)",
+		    data->flags, EXPECTED_FLAGS);
+}
+
+static void handle_sync(struct ucall *uc, struct kvm_clock_data *start,
+			struct kvm_clock_data *end)
+{
+	uint64_t obs, exp_lo, exp_hi;
+
+	obs = uc->args[2];
+	exp_lo = start->clock;
+	exp_hi = end->clock;
+
+	assert_flags(start);
+	assert_flags(end);
+
+	TEST_ASSERT(exp_lo <= obs && obs <= exp_hi,
+		    "unexpected kvm-clock value: %"PRIu64" expected range: [%"PRIu64", %"PRIu64"]",
+		    obs, exp_lo, exp_hi);
+
+	pr_info("kvm-clock value: %"PRIu64" expected range [%"PRIu64", %"PRIu64"]\n",
+		obs, exp_lo, exp_hi);
+}
+
+static void handle_abort(struct ucall *uc)
+{
+	TEST_FAIL("%s at %s:%ld", (const char *)uc->args[0],
+		  __FILE__, uc->args[1]);
+}
+
+static void setup_clock(struct kvm_vm *vm, struct test_case *test_case)
+{
+	struct kvm_clock_data data;
+
+	memset(&data, 0, sizeof(data));
+
+	data.clock = test_case->kvmclock_base;
+	if (test_case->realtime_offset) {
+		struct timespec ts;
+		int r;
+
+		data.flags |= KVM_CLOCK_REALTIME;
+		do {
+			r = clock_gettime(CLOCK_REALTIME, &ts);
+			if (!r)
+				break;
+		} while (errno == EINTR);
+
+		TEST_ASSERT(!r, "clock_gettime() failed: %d\n", r);
+
+		data.realtime = ts.tv_sec * NSEC_PER_SEC;
+		data.realtime += ts.tv_nsec;
+		data.realtime += test_case->realtime_offset;
+	}
+
+	vm_ioctl(vm, KVM_SET_CLOCK, &data);
+}
+
+static void enter_guest(struct kvm_vm *vm)
+{
+	struct kvm_clock_data start, end;
+	struct kvm_run *run;
+	struct ucall uc;
+	int i, r;
+
+	run = vcpu_state(vm, VCPU_ID);

tools/testing/selftests/kvm/x86_64/kvm_clock_test.c line 125

Quoted 10 lines
tools/testing/selftests/kvm/x86_64/kvm_clock_test.c new file +10 -0
+
+	for (i = 0; i < ARRAY_SIZE(test_cases); i++) {
+		setup_clock(vm, &test_cases[i]);
+
+		vm_ioctl(vm, KVM_GET_CLOCK, &start);
+
+		r = _vcpu_run(vm, VCPU_ID);
+		vm_ioctl(vm, KVM_GET_CLOCK, &end);
+
+		TEST_ASSERT(!r, "vcpu_run failed: %d\n", r);

_vcpu_run doesn't return until success, so I don't think this assert is necessary.

tools/testing/selftests/kvm/x86_64/kvm_clock_test.c line 128

Quoted 3 lines
tools/testing/selftests/kvm/x86_64/kvm_clock_test.c new file +3 -0
+		TEST_ASSERT(run->exit_reason == KVM_EXIT_IO,
+			    "unexpected exit reason: %u (%s)",
+			    run->exit_reason, exit_reason_str(run->exit_reason));

You can leave this to the ucall switch, since if exit_reason != KVM_EXIT_IO, then you'll get UCALL_NONE

tools/testing/selftests/kvm/x86_64/kvm_clock_test.c line 136

Quoted 8 lines
tools/testing/selftests/kvm/x86_64/kvm_clock_test.c new file +8 -0
+
+		switch (get_ucall(vm, VCPU_ID, &uc)) {
+		case UCALL_SYNC:
+			handle_sync(&uc, &start, &end);
+			break;
+		case UCALL_ABORT:
+			handle_abort(&uc);
+			return;

guest_main() doesn't call GUEST_ASSERT, so this case can't be used, but maybe it's fine to leave it if there's a chance somebody will add a GUEST_ASSERT later.

tools/testing/selftests/kvm/x86_64/kvm_clock_test.c line 139

Quoted 3 lines
tools/testing/selftests/kvm/x86_64/kvm_clock_test.c new file +3 -0
+		default:
+			TEST_ASSERT(0, "unhandled ucall: %ld\n",
+				    get_ucall(vm, VCPU_ID, &uc));

This can be a TEST_FAIL and no need to refetch uc, can just print uc.cmd.

Quoted 68 lines
tools/testing/selftests/kvm/x86_64/kvm_clock_test.c new file +65 -0
+		}
+	}
+}
+
+#define CLOCKSOURCE_PATH "/sys/devices/system/clocksource/clocksource0/current_clocksource"
+
+static void check_clocksource(void)
+{
+	char *clk_name;
+	struct stat st;
+	FILE *fp;
+
+	fp = fopen(CLOCKSOURCE_PATH, "r");
+	if (!fp) {
+		pr_info("failed to open clocksource file: %d; assuming TSC.\n",
+			errno);
+		return;
+	}
+
+	if (fstat(fileno(fp), &st)) {
+		pr_info("failed to stat clocksource file: %d; assuming TSC.\n",
+			errno);
+		goto out;
+	}
+
+	clk_name = malloc(st.st_size);
+	TEST_ASSERT(clk_name, "failed to allocate buffer to read file\n");
+
+	if (!fgets(clk_name, st.st_size, fp)) {
+		pr_info("failed to read clocksource file: %d; assuming TSC.\n",
+			ferror(fp));
+		goto out;
+	}
+
+	TEST_ASSERT(!strncmp(clk_name, "tsc\n", st.st_size),
+		    "clocksource not supported: %s", clk_name);
+out:
+	fclose(fp);
+}
+
+int main(void)
+{
+	vm_vaddr_t pvti_gva;
+	vm_paddr_t pvti_gpa;
+	struct kvm_vm *vm;
+	int flags;
+
+	flags = kvm_check_cap(KVM_CAP_ADJUST_CLOCK);
+	if (!(flags & KVM_CLOCK_REALTIME)) {
+		print_skip("KVM_CLOCK_REALTIME not supported; flags: %x",
+			   flags);
+		exit(KSFT_SKIP);
+	}
+
+	check_clocksource();
+
+	vm = vm_create_default(VCPU_ID, 0, guest_main);
+
+	pvti_gva = vm_vaddr_alloc(vm, getpagesize(), 0x10000);
+	pvti_gpa = addr_gva2gpa(vm, pvti_gva);
+	vcpu_args_set(vm, VCPU_ID, 2, pvti_gpa, pvti_gva);
+
+	enter_guest(vm);
+	kvm_vm_free(vm);
+}
Signature · 1 line
2.33.0.464.g1972c5931b-goog

All I did was a drive-by review wrt framework APIs, but, besides the ucall nits, the test looks good to me.

Thanks,
drew

maintainer Applied
#13 lore raw
In reply to Oliver Upton
Quoted 67 lines · Oliver Upton

This series implements new tests for the x86 and arm64 counter migration changes that I've mailed out. These are sent separately as a dependent change since there are cross-arch dependencies here.

Patch 1 yanks the pvclock headers into the tools/ directory so we can make use of them within a KVM selftest guest.

Patch 2 tests the new capabilities of the KVM_*_CLOCK ioctls, ensuring that the kernel accounts for elapsed time when restoring the KVM clock.

Patches 3-4 add some device attribute helpers and clean up some mistakes in the assertions thereof.

Patch 5 implements a test for the KVM_VCPU_TSC_OFFSET attribute, asserting that manipulation of the offset results in correct TSC values within the guest.

Patch 6 adds basic arm64 support to the counter offset test, checking that the virtual counter-timer offset works correctly. Patch 7 does the same for the physical counter-timer offset.

Patch 8 adds a benchmark for physical counter offsetting, since most implementations available right now will rely on emulation.

Lastly, patch 9 extends the get-reg-list test to check for KVM_REG_ARM_TIMER_OFFSET if userspace opts-in to the kernel capability.

This series applies cleanly to 5.15-rc1

Tests were ran against the respective architecture changes on the following systems:

- Haswell (x86)
- Ampere Mt. Jade (non-ECV, nVHE and VHE)

v7: https://lore.kernel.org/r/[email protected]

v7 -> v8:
- Rebased to 5.15-rc1
- Dropped helper for checking if reg exists in reg list (no longer necessary)
- Test and enable KVM_CAP_ARM_VTIMER_OFFSET
- Add get-reg-list changes

Oliver Upton (9):
tools: arch: x86: pull in pvclock headers
selftests: KVM: Add test for KVM_{GET,SET}_CLOCK
selftests: KVM: Fix kvm device helper ioctl assertions
selftests: KVM: Add helpers for vCPU device attributes
selftests: KVM: Introduce system counter offset test
selftests: KVM: Add support for aarch64 to system_counter_offset_test
selftests: KVM: Test physical counter offsetting
selftests: KVM: Add counter emulation benchmark
selftests: KVM: Test vtimer offset reg in get-reg-list

  tools/arch/x86/include/asm/pvclock-abi.h      |  48 ++++
  tools/arch/x86/include/asm/pvclock.h          | 103 ++++++++
  tools/testing/selftests/kvm/.gitignore        |   3 +
  tools/testing/selftests/kvm/Makefile          |   4 +
  .../kvm/aarch64/counter_emulation_benchmark.c | 207 ++++++++++++++++
  .../selftests/kvm/aarch64/get-reg-list.c      |  42 ++++
  .../selftests/kvm/include/aarch64/processor.h |  24 ++
  .../testing/selftests/kvm/include/kvm_util.h  |  11 +
  tools/testing/selftests/kvm/lib/kvm_util.c    |  44 +++-
  .../kvm/system_counter_offset_test.c          | 220 ++++++++++++++++++
  .../selftests/kvm/x86_64/kvm_clock_test.c     | 204 ++++++++++++++++
  11 files changed, 907 insertions(+), 3 deletions(-)
Quoted 6 lines

create mode 100644 tools/arch/x86/include/asm/pvclock-abi.h
create mode 100644 tools/arch/x86/include/asm/pvclock.h
create mode 100644 tools/testing/selftests/kvm/aarch64/counter_emulation_benchmark.c create mode 100644 tools/testing/selftests/kvm/system_counter_offset_test.c create mode 100644 tools/testing/selftests/kvm/x86_64/kvm_clock_test.c

Queued patches 1-5, thanks.

Paolo