KVM: Add idempotent controls for migrating system counter state
Subsystems (7)
v2 cover KVM: Add idempotent controls for migrating system counter state
KVM's current means of saving/restoring system counters is plagued with temporal issues. At least on ARM64 and x86, we migrate the guest's system counter by-value through the respective guest system register values (cntvct_el0, ia32_tsc). Restoring system counters by-value is brittle as the state is not idempotent: the host system counter is still oscillating between the attempted save and restore. Furthermore, VMMs may wish to transparently live migrate guest VMs, meaning that they include the elapsed time due to live migration blackout in the guest system counter view. The VMM thread could be preempted for any number of reasons (scheduler, L0 hypervisor under nested) between the time that it calculates the desired guest counter value and when KVM actually sets this counter state.
Despite the value-based interface that we present to userspace, KVM actually has idempotent guest controls by way of system counter offsets. We can avoid all of the issues associated with a value-based interface by abstracting these offset controls in new ioctls. This series introduces new vCPU device attributes to provide userspace access to the vCPU's system counter offset.
Patch 1 adopts Paolo's suggestion, augmenting the KVM_{GET,SET}_CLOCK ioctls to provide userspace with a (host_tsc, realtime) instant. This is essential for a VMM to perform precise migration of the guest's system counters.
Patches 2-3 add support for x86 by shoehorning the new controls into the pre-existing synchronization heuristics.
Patches 4-5 implement a test for the new additions to KVM_{GET,SET}_CLOCK.
Patches 6-7 implement at test for the tsc offset attribute introduced in patch 3.
Patch 8 adds a device attribute for the arm64 virtual counter-timer offset.
Patch 9 extends the test from patch 7 to cover the arm64 virtual counter-timer offset.
Patch 10 adds a device attribute for the arm64 physical counter-timer offset. Currently, this is implemented as a synthetic register, forcing the guest to trap to the host and emulating the offset in the fast exit path. Later down the line we will have hardware with FEAT_ECV, which allows the hypervisor to perform physical counter-timer offsetting in hardware (CNTPOFF_EL2).
Patch 11 extends the test from patch 7 to cover the arm64 physical counter-timer offset.
Patch 12 introduces a benchmark to measure the overhead of emulation in patch 10.
Physical counter benchmark
--------------------------
The following data was collected by running 10000 iterations of the benchmark test from Patch 6 on an Ampere Mt. Jade reference server, A 2S machine with 2 80-core Ampere Altra SoCs. Measurements were collected for both VHE and nVHE operation using the kvm-arm.mode= command-line parameter.
nVHE
----
+--------------------+--------+---------+ | Metric | Native | Trapped | +--------------------+--------+---------+ | Average | 54ns | 148ns | | Standard Deviation | 124ns | 122ns | | 95th Percentile | 258ns | 348ns | +--------------------+--------+---------+
VHE
---
+--------------------+--------+---------+ | Metric | Native | Trapped | +--------------------+--------+---------+ | Average | 53ns | 152ns | | Standard Deviation | 92ns | 94ns | | 95th Percentile | 204ns | 307ns | +--------------------+--------+---------+
This series applies cleanly to the following commit:
1889228d80fe ("KVM: selftests: smm_test: Test SMM enter from L2")
v1 -> v2:
- Reimplemented as vCPU device attributes instead of a distinct ioctl.
- Added the (realtime, host_tsc) instant support to
KVM_{GET,SET}_CLOCK - Changed the arm64 implementation to broadcast counter offset values to all vCPUs in a guest. This upholds the architectural expectations of a consistent counter-timer across CPUs.
- Fixed a bug with traps in VHE mode. We now configure traps on every transition into a guest to handle differing VMs (trapped, emulated).
Oliver Upton (12):
KVM: x86: Report host tsc and realtime values in KVM_GET_CLOCK
KVM: x86: Refactor tsc synchronization code
KVM: x86: Expose TSC offset controls to userspace
tools: arch: x86: pull in pvclock headers
selftests: KVM: Add test for KVM_{GET,SET}_CLOCK
selftests: KVM: Add helpers for vCPU device attributes
selftests: KVM: Introduce system counter offset test
KVM: arm64: Allow userspace to configure a vCPU's virtual offset
selftests: KVM: Add support for aarch64 to system_counter_offset_test
KVM: arm64: Provide userspace access to the physical counter offset
selftests: KVM: Test physical counter offsetting
selftests: KVM: Add counter emulation benchmark
Documentation/virt/kvm/api.rst | 42 +- Documentation/virt/kvm/locking.rst | 11 + arch/arm64/include/asm/kvm_host.h | 1 + arch/arm64/include/asm/kvm_hyp.h | 2 - arch/arm64/include/asm/sysreg.h | 1 + arch/arm64/include/uapi/asm/kvm.h | 2 + arch/arm64/kvm/arch_timer.c | 118 ++++- arch/arm64/kvm/arm.c | 4 +- arch/arm64/kvm/hyp/include/hyp/switch.h | 23 + arch/arm64/kvm/hyp/include/hyp/timer-sr.h | 26 ++ arch/arm64/kvm/hyp/nvhe/switch.c | 2 - arch/arm64/kvm/hyp/nvhe/timer-sr.c | 21 +- arch/arm64/kvm/hyp/vhe/timer-sr.c | 27 ++ arch/x86/include/asm/kvm_host.h | 4 + arch/x86/include/uapi/asm/kvm.h | 4 + arch/x86/kvm/x86.c | 421 ++++++++++++++---- include/kvm/arm_arch_timer.h | 2 - include/uapi/linux/kvm.h | 7 +- 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 | 215 +++++++++ .../selftests/kvm/include/aarch64/processor.h | 24 + .../testing/selftests/kvm/include/kvm_util.h | 11 + tools/testing/selftests/kvm/lib/kvm_util.c | 38 ++ .../kvm/system_counter_offset_test.c | 206 +++++++++ .../selftests/kvm/x86_64/kvm_clock_test.c | 210 +++++++++ 28 files changed, 1447 insertions(+), 133 deletions(-) create mode 100644 arch/arm64/kvm/hyp/include/hyp/timer-sr.h 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 · 1 line
2.32.0.402.g57bb445576-goog
PATCH v2 1/12 KVM: x86: Report host tsc and realtime values in KVM_GET_CLOCK
Handling the migration of TSCs correctly is difficult, in part because Linux does not provide userspace with the ability to retrieve a (TSC, realtime) clock pair for a single instant in time. In lieu of a more convenient facility, KVM can report similar information in the kvm_clock structure.
Provide userspace with a host TSC & realtime pair iff the realtime clock is based on the TSC. If userspace provides KVM_SET_CLOCK with a valid realtime value, advance the KVM clock by the amount of elapsed time. Do not step the KVM clock backwards, though, as it is a monotonic oscillator.
--- Documentation/virt/kvm/api.rst | 42 +++++++-- arch/x86/include/asm/kvm_host.h | 3 + arch/x86/kvm/x86.c | 149 ++++++++++++++++++++------------ include/uapi/linux/kvm.h | 7 +- 4 files changed, 137 insertions(+), 64 deletions(-)
Documentation/virt/kvm/api.rst +33 -9
@@ -993,20 +993,34 @@ such as migration. When KVM_CAP_ADJUST_CLOCK is passed to KVM_CHECK_EXTENSION, it returns the set of bits that KVM can return in struct kvm_clock_data's flag member. -The only flag defined now is KVM_CLOCK_TSC_STABLE. If set, the returned -value is the exact kvmclock value seen by all VCPUs at the instant -when KVM_GET_CLOCK was called. If clear, the returned value is simply -CLOCK_MONOTONIC plus a constant offset; the offset can be modified -with KVM_SET_CLOCK. KVM will try to make all VCPUs follow this clock, -but the exact value read by each VCPU could differ, because the host -TSC is not stable. +FLAGS: + +KVM_CLOCK_TSC_STABLE. If set, the returned value is the exact kvmclock +value seen by all VCPUs at the instant when KVM_GET_CLOCK was called. +If clear, the returned value is simply CLOCK_MONOTONIC plus a constant +offset; the offset can be modified with KVM_SET_CLOCK. KVM will try +to make all VCPUs follow this clock, but the exact value read by each +VCPU could differ, because the host TSC is not stable. + +KVM_CLOCK_REAL_TIME. If set, the `realtime` field in the kvm_clock_data +structure is populated with the value of the host's real time +clocksource at the instant when KVM_GET_CLOCK was called. If clear, +the `realtime` field does not contain a value. + +KVM_CLOCK_HOST_TSC. If set, the `host_tsc` field in the kvm_clock_data +structure is populated with the value of the host's timestamp counter (TSC) +at the instant when KVM_GET_CLOCK was called. If clear, the `host_tsc` field +does not contain a value. :: struct kvm_clock_data { __u64 clock; /* kvmclock current value */ __u32 flags; - __u32 pad[9]; + __u32 pad0; + __u64 realtime; + __u64 host_tsc; + __u32 pad[4]; };
@@ -1023,12 +1037,22 @@ Sets the current timestamp of kvmclock to the value specified in its parameter. In conjunction with KVM_GET_CLOCK, it is used to ensure monotonicity on scenarios such as migration. +FLAGS: + +KVM_CLOCK_REAL_TIME. If set, KVM will compare the value of the `realtime` field +with the value of the host's real time clocksource at the instant when +KVM_SET_CLOCK was called. The difference in elapsed time is added to the final +kvmclock value that will be provided to guests. + :: struct kvm_clock_data { __u64 clock; /* kvmclock current value */ __u32 flags; - __u32 pad[9]; + __u32 pad0; + __u64 realtime; + __u64 host_tsc; + __u32 pad[4]; };
arch/x86/include/asm/kvm_host.h +3 -0
@@ -1936,4 +1936,7 @@ int kvm_cpu_dirty_log_size(void);int alloc_all_memslots_rmaps (struct kvm *kvm); +#define KVM_CLOCK_VALID_FLAGS \ + (KVM_CLOCK_TSC_STABLE | KVM_CLOCK_REAL_TIME | KVM_CLOCK_HOST_TSC) +#endif /* _ASM_X86_KVM_HOST_H */
arch/x86/kvm/x86.c +95 -54
200 lines not on this page · raw message
include/uapi/linux/kvm.h +6 -1
@@ -1223,11 +1223,16 @@ struct kvm_irqfd {/* Do not use 1, KVM_CHECK_EXTENSION returned it before we had flags. */ #define KVM_CLOCK_TSC_STABLE 2 +#define KVM_CLOCK_REAL_TIME (1 << 2) +#define KVM_CLOCK_HOST_TSC (1 << 3)struct kvm_clock_data {__u64 clock;__u32 flags; -__u32 pad[9 ]; +__u32 pad0; +__u64 realtime; +__u64 host_tsc; +__u32 pad[4 ]; };/* For KVM_CAP_SW_TLB */
Signature · 1 line
2.32.0.402.g57bb445576-goog
kernel test robot
build botRe: [PATCH v2 01/12] KVM: x86: Report host tsc and realtime values in KVM_GET_CLOCK
Thank you for the patch! Perhaps something to improve:
Hi Oliver,
Thank you for the patch! Perhaps something to improve:
[auto build test WARNING on kvm/queue]
[also build test WARNING on vhost/linux-next v5.14-rc1 next-20210716] [cannot apply to kvmarm/next]
[If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch]
url: https://github.com/0day-ci/linux/commits/Oliver-Upton/KVM-Add-idempotent-controls-for-migrating-system-counter-state/20210718-103407 base: https://git.kernel.org/pub/scm/virt/kvm/kvm.git queue config: x86_64-randconfig-a014-20210718 (attached as .config) compiler: clang version 13.0.0 (https://github.com/llvm/llvm-project 5d5b08761f944d5b9822d582378333cc4b36a0a7)
reproduce (this is a W=1 build):
wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross chmod +x ~/bin/make.cross
# install x86_64 cross compiling tool for clang build
# apt-get install binutils-x86-64-linux-gnu
# https://github.com/0day-ci/linux/commit/72b6d584e6ac692038a7d70e8782f0dfa179e5fb
git remote add linux-review https://github.com/0day-ci/linux
git fetch --no-tags linux-review Oliver-Upton/KVM-Add-idempotent-controls-for-migrating-system-counter-state/20210718-103407 git checkout 72b6d584e6ac692038a7d70e8782f0dfa179e5fb
# save the attached .config to linux build tree
COMPILER_INSTALL_PATH=$HOME/0day COMPILER=clang make.cross ARCH=x86_64
If you fix the issue, kindly add following tag as appropriate
All warnings (new ones prefixed by >>):
>> arch/x86/kvm/x86.c:2784: warning: This comment starts with '/**', but isn't a kernel-doc comment. Refer Documentation/doc-guide/kernel-doc.rst
* Returns true if realtime and TSC values were written back to the caller.
vim +2784 arch/x86/kvm/x86.c
2782
2783 /**
> 2784 * Returns true if realtime and TSC values were written back to the caller.
2785 * Returns false if a clock triplet cannot be obtained, such as if the host's
2786 * realtime clock is not based on the TSC.
2787 */
2788 static bool get_kvmclock_and_realtime(struct kvm *kvm, u64 *kvmclock_ns,
2789 u64 *realtime_ns, u64 *tsc)
2790 {
2791 struct kvm_arch *ka = &kvm->arch;
2792 struct pvclock_vcpu_time_info hv_clock;
2793 unsigned long flags;
2794 bool ret = false;
2795
2796 spin_lock_irqsave(&ka->pvclock_gtod_sync_lock, flags);
2797 if (!ka->use_master_clock) {
2798 spin_unlock_irqrestore(&ka->pvclock_gtod_sync_lock, flags);
2799 *kvmclock_ns = get_kvmclock_base_ns() + ka->kvmclock_offset;
2800 return false;
2801 }
2802
2803 hv_clock.tsc_timestamp = ka->master_cycle_now;
2804 hv_clock.system_time = ka->master_kernel_ns + ka->kvmclock_offset;
2805 spin_unlock_irqrestore(&ka->pvclock_gtod_sync_lock, flags);
2806
2807 /* both __this_cpu_read() and rdtsc() should be on the same cpu */
2808 get_cpu();
2809
2810 if (__this_cpu_read(cpu_tsc_khz)) {
2811 struct timespec64 ts;
2812 u64 tsc_val;
2813
2814 kvm_get_time_scale(NSEC_PER_SEC, __this_cpu_read(cpu_tsc_khz) * 1000LL,
2815 &hv_clock.tsc_shift,
2816 &hv_clock.tsc_to_system_mul);
2817
2818 if (kvm_get_walltime_and_clockread(&ts, &tsc_val)) {
2819 *realtime_ns = ts.tv_nsec + NSEC_PER_SEC * ts.tv_sec;
2820 *tsc = tsc_val;
2821 ret = true;
2822 }
2823
2824 *kvmclock_ns = __pvclock_read_cycles(&hv_clock, tsc_val);
2825 } else
2826 *kvmclock_ns = get_kvmclock_base_ns() + ka->kvmclock_offset;
2827
2828 put_cpu();
2829
2830 return ret;
2831 }
2832
---
0-DAY CI Kernel Test Service, Intel Corporation
https://lists.01.org/hyperkitty/list/kbuild-all(a)lists.01.org
kernel test robot
build botRe: [PATCH v2 01/12] KVM: x86: Report host tsc and realtime values in KVM_GET_CLOCK
In reply to Oliver UptonThank you for the patch! Perhaps something to improve:
Hi Oliver,
Thank you for the patch! Perhaps something to improve:
[auto build test WARNING on kvm/queue]
[also build test WARNING on vhost/linux-next v5.14-rc1 next-20210716] [cannot apply to kvmarm/next]
[If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch]
url: https://github.com/0day-ci/linux/commits/Oliver-Upton/KVM-Add-idempotent-controls-for-migrating-system-counter-state/20210718-103407 base: https://git.kernel.org/pub/scm/virt/kvm/kvm.git queue config: x86_64-rhel-8.3-kselftests (attached as .config) compiler: gcc-10 (Ubuntu 10.3.0-1ubuntu1~20.04) 10.3.0 reproduce: # apt-get install sparse # sparse version: v0.6.3-341-g8af24329-dirty # https://github.com/0day-ci/linux/commit/72b6d584e6ac692038a7d70e8782f0dfa179e5fb git remote add linux-review https://github.com/0day-ci/linux git fetch --no-tags linux-review Oliver-Upton/KVM-Add-idempotent-controls-for-migrating-system-counter-state/20210718-103407 git checkout 72b6d584e6ac692038a7d70e8782f0dfa179e5fb # save the attached .config to linux build tree make W=1 C=1 CF='-fdiagnostic-prefix -D__CHECK_ENDIAN__' ARCH=x86_64
If you fix the issue, kindly add following tag as appropriate
All warnings (new ones prefixed by >>):
>> arch/x86/kvm/x86.c:2784: warning: This comment starts with '/**', but isn't a kernel-doc comment. Refer Documentation/doc-guide/kernel-doc.rst
* Returns true if realtime and TSC values were written back to the caller.
vim +2784 arch/x86/kvm/x86.c
2782
2783 /**
> 2784 * Returns true if realtime and TSC values were written back to the caller.
2785 * Returns false if a clock triplet cannot be obtained, such as if the host's
2786 * realtime clock is not based on the TSC.
2787 */
2788 static bool get_kvmclock_and_realtime(struct kvm *kvm, u64 *kvmclock_ns,
2789 u64 *realtime_ns, u64 *tsc)
2790 {
2791 struct kvm_arch *ka = &kvm->arch;
2792 struct pvclock_vcpu_time_info hv_clock;
2793 unsigned long flags;
2794 bool ret = false;
2795
2796 spin_lock_irqsave(&ka->pvclock_gtod_sync_lock, flags);
2797 if (!ka->use_master_clock) {
2798 spin_unlock_irqrestore(&ka->pvclock_gtod_sync_lock, flags);
2799 *kvmclock_ns = get_kvmclock_base_ns() + ka->kvmclock_offset;
2800 return false;
2801 }
2802
2803 hv_clock.tsc_timestamp = ka->master_cycle_now;
2804 hv_clock.system_time = ka->master_kernel_ns + ka->kvmclock_offset;
2805 spin_unlock_irqrestore(&ka->pvclock_gtod_sync_lock, flags);
2806
2807 /* both __this_cpu_read() and rdtsc() should be on the same cpu */
2808 get_cpu();
2809
2810 if (__this_cpu_read(cpu_tsc_khz)) {
2811 struct timespec64 ts;
2812 u64 tsc_val;
2813
2814 kvm_get_time_scale(NSEC_PER_SEC, __this_cpu_read(cpu_tsc_khz) * 1000LL,
2815 &hv_clock.tsc_shift,
2816 &hv_clock.tsc_to_system_mul);
2817
2818 if (kvm_get_walltime_and_clockread(&ts, &tsc_val)) {
2819 *realtime_ns = ts.tv_nsec + NSEC_PER_SEC * ts.tv_sec;
2820 *tsc = tsc_val;
2821 ret = true;
2822 }
2823
2824 *kvmclock_ns = __pvclock_read_cycles(&hv_clock, tsc_val);
2825 } else
2826 *kvmclock_ns = get_kvmclock_base_ns() + ka->kvmclock_offset;
2827
2828 put_cpu();
2829
2830 return ret;
2831 }
2832
---
0-DAY CI Kernel Test Service, Intel Corporation
https://lists.01.org/hyperkitty/list/kbuild-all(a)lists.01.org
kernel test robot
build botRe: [PATCH v2 01/12] KVM: x86: Report host tsc and realtime values in KVM_GET_CLOCK
In reply to Oliver UptonThank you for the patch! Yet something to improve:
Hi Oliver,
Thank you for the patch! Yet something to improve:
[auto build test ERROR on kvm/queue]
[also build test ERROR on vhost/linux-next v5.14-rc2 next-20210716] [cannot apply to kvmarm/next]
[If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch]
url: https://github.com/0day-ci/linux/commits/Oliver-Upton/KVM-Add-idempotent-controls-for-migrating-system-counter-state/20210718-103407 base: https://git.kernel.org/pub/scm/virt/kvm/kvm.git queue config: i386-randconfig-a013-20210718 (attached as .config) compiler: gcc-10 (Ubuntu 10.3.0-1ubuntu1~20.04) 10.3.0 reproduce (this is a W=1 build): # https://github.com/0day-ci/linux/commit/72b6d584e6ac692038a7d70e8782f0dfa179e5fb git remote add linux-review https://github.com/0day-ci/linux git fetch --no-tags linux-review Oliver-Upton/KVM-Add-idempotent-controls-for-migrating-system-counter-state/20210718-103407 git checkout 72b6d584e6ac692038a7d70e8782f0dfa179e5fb # save the attached .config to linux build tree make W=1 ARCH=i386
If you fix the issue, kindly add following tag as appropriate
All errors (new ones prefixed by >>):
arch/x86/kvm/x86.c: In function 'get_kvmclock_and_realtime':
>> arch/x86/kvm/x86.c:2818:7: error: implicit declaration of function 'kvm_get_walltime_and_clockread' [-Werror=implicit-function-declaration]
2818 | if (kvm_get_walltime_and_clockread(&ts, &tsc_val)) {
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~cc1: some warnings being treated as errors
vim +/kvm_get_walltime_and_clockread +2818 arch/x86/kvm/x86.c
2782
2783 /**
2784 * Returns true if realtime and TSC values were written back to the caller.
2785 * Returns false if a clock triplet cannot be obtained, such as if the host's
2786 * realtime clock is not based on the TSC.
2787 */
2788 static bool get_kvmclock_and_realtime(struct kvm *kvm, u64 *kvmclock_ns,
2789 u64 *realtime_ns, u64 *tsc)
2790 {
2791 struct kvm_arch *ka = &kvm->arch;
2792 struct pvclock_vcpu_time_info hv_clock;
2793 unsigned long flags;
2794 bool ret = false;
2795
2796 spin_lock_irqsave(&ka->pvclock_gtod_sync_lock, flags);
2797 if (!ka->use_master_clock) {
2798 spin_unlock_irqrestore(&ka->pvclock_gtod_sync_lock, flags);
2799 *kvmclock_ns = get_kvmclock_base_ns() + ka->kvmclock_offset;
2800 return false;
2801 }
2802
2803 hv_clock.tsc_timestamp = ka->master_cycle_now;
2804 hv_clock.system_time = ka->master_kernel_ns + ka->kvmclock_offset;
2805 spin_unlock_irqrestore(&ka->pvclock_gtod_sync_lock, flags);
2806
2807 /* both __this_cpu_read() and rdtsc() should be on the same cpu */
2808 get_cpu();
2809
2810 if (__this_cpu_read(cpu_tsc_khz)) {
2811 struct timespec64 ts;
2812 u64 tsc_val;
2813
2814 kvm_get_time_scale(NSEC_PER_SEC, __this_cpu_read(cpu_tsc_khz) * 1000LL,
2815 &hv_clock.tsc_shift,
2816 &hv_clock.tsc_to_system_mul);
2817
> 2818 if (kvm_get_walltime_and_clockread(&ts, &tsc_val)) { 2819 *realtime_ns = ts.tv_nsec + NSEC_PER_SEC * ts.tv_sec;
2820 *tsc = tsc_val;
2821 ret = true;
2822 }
2823
2824 *kvmclock_ns = __pvclock_read_cycles(&hv_clock, tsc_val);
2825 } else
2826 *kvmclock_ns = get_kvmclock_base_ns() + ka->kvmclock_offset;
2827
2828 put_cpu();
2829
2830 return ret;
2831 }
2832
---
0-DAY CI Kernel Test Service, Intel Corporation
https://lists.01.org/hyperkitty/list/kbuild-all(a)lists.01.org
PATCH v2 2/12 KVM: x86: Refactor tsc synchronization code
In reply to Oliver UptonRefactor kvm_synchronize_tsc to make a new function that allows callers to specify TSC parameters (offset, value, nanoseconds, etc.) explicitly for the sake of participating in TSC synchronization.
This changes the locking semantics around TSC writes. Writes to the TSC will now take the pvclock gtod lock while holding the tsc write lock, whereas before these locks were disjoint.
--- Documentation/virt/kvm/locking.rst | 11 +++ arch/x86/kvm/x86.c | 106 +++++++++++++++++------------ 2 files changed, 74 insertions(+), 43 deletions(-)
Documentation/virt/kvm/locking.rst +11 -0
@@ -30,6 +30,9 @@ On x86: holding kvm->arch.mmu_lock (typically with ``read_lock``, otherwise there's no need to take kvm->arch.tdp_mmu_pages_lock at all). +- kvm->arch.tsc_write_lock is taken outside + kvm->arch.pvclock_gtod_sync_lock + Everything else is a leaf: no other lock is taken inside the critical sections.
@@ -216,6 +219,14 @@ time it will be set using the Dirty tracking mechanism described above. :Comment: 'raw' because hardware enabling/disabling must be atomic /wrt migration. +:Name: kvm_arch::pvclock_gtod_sync_lock +:Type: raw_spinlock_t +:Arch: x86 +:Protects: kvm_arch::{cur_tsc_generation,cur_tsc_nsec,cur_tsc_write, + cur_tsc_offset,nr_vcpus_matched_tsc} +:Comment: 'raw' because updating the kvm master clock must not be + preempted. + :Name: kvm_arch::tsc_write_lock :Type: raw_spinlock :Arch: x86
arch/x86/kvm/x86.c +63 -43
127 lines not on this page · raw message
Signature · 1 line
2.32.0.402.g57bb445576-goog
PATCH v2 3/12 KVM: x86: Expose TSC offset controls to userspace
In reply to Oliver UptonTo date, VMM-directed TSC synchronization and migration has been a bit messy. KVM has some baked-in heuristics around TSC writes to infer if the VMM is attempting to synchronize. This is problematic, as it depends on host userspace writing to the guest's TSC within 1 second of the last write.
A much cleaner approach to configuring the guest's views of the TSC is to simply migrate the TSC offset for every vCPU. Offsets are idempotent, and thus not subject to change depending on when the VMM actually reads/writes values from/to KVM. The VMM can then read the TSC once with KVM_GET_CLOCK to capture a (realtime, host_tsc) pair at the instant when the guest is paused.
--- arch/x86/include/asm/kvm_host.h | 1 + arch/x86/include/uapi/asm/kvm.h | 4 + arch/x86/kvm/x86.c | 166 ++++++++++++++++++++++++++++++++ 3 files changed, 171 insertions(+)
arch/x86/include/asm/kvm_host.h +1 -0
@@ -1070,6 +1070,7 @@ struct kvm_arch {u64 last_tsc_nsec;u64 last_tsc_write;u32 last_tsc_khz; +u64 last_tsc_offset;u64 cur_tsc_nsec;u64 cur_tsc_write;u64 cur_tsc_offset;
arch/x86/include/uapi/asm/kvm.h +4 -0
@@ -503,4 +503,8 @@ struct kvm_pmu_event_filter {#define KVM_PMU_EVENT_ALLOW 0#define KVM_PMU_EVENT_DENY 1 +/* for KVM_{GET,SET,HAS}_DEVICE_ATTR */ +#define KVM_VCPU_TSC_CTRL 0/* control group for the timestamp counter (TSC) */ +#define KVM_VCPU_TSC_OFFSET 0/* attribute for the TSC offset */ +#endif /* _ASM_X86_KVM_H */
arch/x86/kvm/x86.c +166 -0
190 lines not on this page · raw message
Signature · 1 line
2.32.0.402.g57bb445576-goog
Paolo Bonzini
maintainerRe: [PATCH v2 03/12] KVM: x86: Expose TSC offset controls to userspace
Quoted 13 lines · Oliver Upton
To date, VMM-directed TSC synchronization and migration has been a bit messy. KVM has some baked-in heuristics around TSC writes to infer if the VMM is attempting to synchronize. This is problematic, as it depends on host userspace writing to the guest's TSC within 1 second of the last write.
A much cleaner approach to configuring the guest's views of the TSC is to simply migrate the TSC offset for every vCPU. Offsets are idempotent, and thus not subject to change depending on when the VMM actually reads/writes values from/to KVM. The VMM can then read the TSC once with
KVM_GET_CLOCKto capture a (realtime,host_tsc) pair at the instant when the guest is paused.
Quoted 7 lines
Cc David Matlack <[email protected]>Signed-off-by Oliver Upton <[email protected]>--- arch/x86/include/asm/kvm_host.h | 1 + arch/x86/include/uapi/asm/kvm.h | 4 + arch/x86/kvm/x86.c | 166 ++++++++++++++++++++++++++++++++ 3 files changed, 171 insertions(+)
This is missing documentation. The documentation should also include the algorithm in https://www.spinics.net/lists/kvm-arm/msg47383.html (modulo the fact that KVM_GET/SET_CLOCK return or pass realtime_ns rather than kvmclock_ns - relatime_ns; which is fine of course).
Paolo
Quoted 224 lines
arch/x86/include/asm/kvm_host.h +1
-0@@ -1070,6 +1070,7 @@ struct kvm_arch {u64 last_tsc_nsec;u64 last_tsc_write;u32 last_tsc_khz; +u64 last_tsc_offset;u64 cur_tsc_nsec;u64 cur_tsc_write;u64 cur_tsc_offset;arch/x86/include/uapi/asm/kvm.h +4
-0@@ -503,4 +503,8 @@ struct kvm_pmu_event_filter {#define KVM_PMU_EVENT_ALLOW 0#define KVM_PMU_EVENT_DENY 1 +/* for KVM_{GET,SET,HAS}_DEVICE_ATTR */ +#define KVM_VCPU_TSC_CTRL 0/* control group for the timestamp counter (TSC) */ +#define KVM_VCPU_TSC_OFFSET 0/* attribute for the TSC offset */ +#endif /* _ASM_X86_KVM_H */ arch/x86/kvm/x86.c +166
-0@@ -2411,6 +2411,11 @@ static void kvm_vcpu_write_tsc_offset(struct kvm_vcpu *vcpu, u64 l1_offset)static_call (kvm_x86_write_tsc_offset)(vcpu, vcpu->arch.tsc_offset); } +static u64 kvm_vcpu_read_tsc_offset (struct kvm_vcpu *vcpu) +{ +return vcpu->arch.l1_tsc_offset; +} +static void kvm_vcpu_write_tsc_multiplier (struct kvm_vcpu *vcpu,u64 l1_multiplier) { vcpu->arch.l1_tsc_scaling_ratio = l1_multiplier;@@ -2467,6 +2472,7 @@ static void __kvm_synchronize_tsc(struct kvm_vcpu *vcpu, u64 offset, u64 tsc, kvm->arch.last_tsc_nsec = ns; kvm->arch.last_tsc_write = tsc; kvm->arch.last_tsc_khz = vcpu->arch.virtual_tsc_khz; + kvm->arch.last_tsc_offset = offset; vcpu->arch.last_guest_tsc = tsc;@@ -4914,6 +4920,136 @@ static int kvm_set_guest_paused(struct kvm_vcpu *vcpu)return 0 ; } +static int kvm_arch_tsc_has_attr (struct kvm_vcpu *vcpu, +struct kvm_device_attr *attr) +{ +int r; + +switch (attr->attr) { +case KVM_VCPU_TSC_OFFSET : + r =0 ; +break ; +default : + r = -ENXIO ; + } + +return r; +} + +static int kvm_arch_tsc_get_attr (struct kvm_vcpu *vcpu, +struct kvm_device_attr *attr) +{ +void __user *uaddr = (void __user *)attr->addr; +int r; + +switch (attr->attr) { +case KVM_VCPU_TSC_OFFSET : { +u64 offset; + + offset =kvm_vcpu_read_tsc_offset (vcpu); + r = -EFAULT ; +if (copy_to_user (uaddr, &offset,sizeof (offset))) +break ; + + r =0 ; + } +default : + r = -ENXIO ; + } + +return r; +} + +static int kvm_arch_tsc_set_attr (struct kvm_vcpu *vcpu, +struct kvm_device_attr *attr) +{ +void __user *uaddr = (void __user *)attr->addr; +struct kvm *kvm = vcpu->kvm; +int r; + +switch (attr->attr) { +case KVM_VCPU_TSC_OFFSET : { +u64 offset, tsc, ns; +unsigned long flags; +bool matched; + + r = -EFAULT ; +if (copy_from_user (&offset, uaddr,sizeof (offset))) +break ; + +raw_spin_lock_irqsave (&kvm->arch.tsc_write_lock, flags); + + matched = (vcpu->arch.virtual_tsc_khz && + kvm->arch.last_tsc_khz == vcpu->arch.virtual_tsc_khz && + kvm->arch.last_tsc_offset == offset); + + tsc =kvm_scale_tsc (vcpu,rdtsc (), vcpu->arch.l1_tsc_scaling_ratio) + offset; + ns =get_kvmclock_base_ns (); + +__kvm_synchronize_tsc (vcpu, offset, tsc, ns, matched); +raw_spin_unlock_irqrestore (&kvm->arch.tsc_write_lock, flags); + + r =0 ; +break ; + } +default : + r = -ENXIO ; + } + +return r; +} + +static int kvm_vcpu_ioctl_has_device_attr (struct kvm_vcpu *vcpu, +struct kvm_device_attr *attr) +{ +int r; + +switch (attr->group) { +case KVM_VCPU_TSC_CTRL : + r =kvm_arch_tsc_has_attr (vcpu, attr); +break ; +default : + r = -ENXIO ; +break ; + } + +return r; +} + +static int kvm_vcpu_ioctl_get_device_attr (struct kvm_vcpu *vcpu, +struct kvm_device_attr *attr) +{ +int r; + +switch (attr->group) { +case KVM_VCPU_TSC_CTRL : + r =kvm_arch_tsc_get_attr (vcpu, attr); +break ; +default : + r = -ENXIO ; +break ; + } + +return r; +} + +static int kvm_vcpu_ioctl_set_device_attr (struct kvm_vcpu *vcpu, +struct kvm_device_attr *attr) +{ +int r; + +switch (attr->group) { +case KVM_VCPU_TSC_CTRL : + r =kvm_arch_tsc_set_attr (vcpu, attr); +break ; +default : + r = -ENXIO ; +break ; + } + +return r; +} +static int kvm_vcpu_ioctl_enable_cap (struct kvm_vcpu *vcpu,struct kvm_enable_cap *cap) {@@ -5368,6 +5504,36 @@ long kvm_arch_vcpu_ioctl(struct file *filp, r =__set_sregs2 (vcpu, u.sregs2);break ; } +case KVM_HAS_DEVICE_ATTR : { +struct kvm_device_attr attr; + + r = -EFAULT ; +if (copy_from_user (&attr, argp,sizeof (attr))) + goto out; + + r =kvm_vcpu_ioctl_has_device_attr (vcpu, &attr); +break ; + } +case KVM_GET_DEVICE_ATTR : { +struct kvm_device_attr attr; + + r = -EFAULT ; +if (copy_from_user (&attr, argp,sizeof (attr))) + goto out; + + r =kvm_vcpu_ioctl_get_device_attr (vcpu, &attr); +break ; + } +case KVM_SET_DEVICE_ATTR : { +struct kvm_device_attr attr; + + r = -EFAULT ; +if (copy_from_user (&attr, argp,sizeof (attr))) + goto out; + + r =kvm_vcpu_ioctl_set_device_attr (vcpu, &attr); +break ; + }default : r = -EINVAL ; }
kernel test robot
build botRe: [PATCH v2 03/12] KVM: x86: Expose TSC offset controls to userspace
In reply to Oliver UptonThank you for the patch! Perhaps something to improve:
Hi Oliver,
Thank you for the patch! Perhaps something to improve:
[auto build test WARNING on kvm/queue]
[also build test WARNING on vhost/linux-next v5.14-rc1 next-20210716] [cannot apply to kvmarm/next]
[If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch]
url: https://github.com/0day-ci/linux/commits/Oliver-Upton/KVM-Add-idempotent-controls-for-migrating-system-counter-state/20210718-103407 base: https://git.kernel.org/pub/scm/virt/kvm/kvm.git queue config: i386-randconfig-a013-20210718 (attached as .config) compiler: gcc-10 (Ubuntu 10.3.0-1ubuntu1~20.04) 10.3.0 reproduce (this is a W=1 build): # https://github.com/0day-ci/linux/commit/1e9a9a8e956c70659f72ad66a7d8176ff4756061 git remote add linux-review https://github.com/0day-ci/linux git fetch --no-tags linux-review Oliver-Upton/KVM-Add-idempotent-controls-for-migrating-system-counter-state/20210718-103407 git checkout 1e9a9a8e956c70659f72ad66a7d8176ff4756061 # save the attached .config to linux build tree make W=1 ARCH=i386
If you fix the issue, kindly add following tag as appropriate
All warnings (new ones prefixed by >>):
arch/x86/kvm/x86.c: In function 'get_kvmclock_and_realtime':
arch/x86/kvm/x86.c:2844:7: error: implicit declaration of function 'kvm_get_walltime_and_clockread' [-Werror=implicit-function-declaration]
2844 | if (kvm_get_walltime_and_clockread(&ts, &tsc_val)) {
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
arch/x86/kvm/x86.c: In function 'kvm_arch_tsc_get_attr':
>> arch/x86/kvm/x86.c:4945:23: warning: cast to pointer from integer of different size [-Wint-to-pointer-cast]
4945 | void __user *uaddr = (void __user *)attr->addr;
| ^
arch/x86/kvm/x86.c: In function 'kvm_arch_tsc_set_attr':
arch/x86/kvm/x86.c:4969:23: warning: cast to pointer from integer of different size [-Wint-to-pointer-cast]
4969 | void __user *uaddr = (void __user *)attr->addr;
| ^
arch/x86/kvm/x86.c: In function 'kvm_arch_tsc_get_attr':
arch/x86/kvm/x86.c:4949:28: warning: this statement may fall through [-Wimplicit-fallthrough=]
4949 | case KVM_VCPU_TSC_OFFSET: {
| ^
arch/x86/kvm/x86.c:4959:2: note: here
4959 | default:
| ^~~~~~~cc1: some warnings being treated as errors
vim +4945 arch/x86/kvm/x86.c
4941
4942 static int kvm_arch_tsc_get_attr(struct kvm_vcpu *vcpu,
4943 struct kvm_device_attr *attr)
4944 {
> 4945 void __user *uaddr = (void __user *)attr->addr;
4946 int r;
4947
4948 switch (attr->attr) {
4949 case KVM_VCPU_TSC_OFFSET: {
4950 u64 offset;
4951
4952 offset = kvm_vcpu_read_tsc_offset(vcpu);
4953 r = -EFAULT;
4954 if (copy_to_user(uaddr, &offset, sizeof(offset)))
4955 break;
4956
4957 r = 0;
4958 }
4959 default:
4960 r = -ENXIO;
4961 }
4962
4963 return r;
4964 }
4965
---
0-DAY CI Kernel Test Service, Intel Corporation
https://lists.01.org/hyperkitty/list/kbuild-all(a)lists.01.org
PATCH v2 4/12 tools: arch: x86: pull in pvclock headers
In reply to Oliver UptonCopy 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
@@ -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 · 1 line
2.32.0.402.g57bb445576-goog
PATCH v2 5/12 selftests: KVM: Add test for KVM_{GET,SET}_CLOCK
In reply to Oliver UptonAdd 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 | 210 ++++++++++++++++++ 4 files changed, 214 insertions(+) create mode 100644 tools/testing/selftests/kvm/x86_64/kvm_clock_test.c
tools/testing/selftests/kvm/.gitignore +1 -0
@@ -11,6 +11,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
@@ -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
@@ -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 +210 -0
210 lines not on this page · raw message
Signature · 1 line
2.32.0.402.g57bb445576-goog
Re: [PATCH v2 05/12] selftests: KVM: Add test for KVM_{GET,SET}_CLOCK
Quoted 249 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.
Signed-off-by Oliver Upton <[email protected]>--- 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 | 210 ++++++++++++++++++ 4 files changed, 214 insertions(+) create mode 100644 tools/testing/selftests/kvm/x86_64/kvm_clock_test.ctools/testing/selftests/kvm/.gitignore +1
-0@@ -11,6 +11,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_cpuidtools/testing/selftests/kvm/Makefile +1
-0@@ -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_testtools/testing/selftests/kvm/include/kvm_util.h +2
-0@@ -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 +192
-0@@ -0,0 +1,210 @@ +// 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 ())); + } + +GUEST_DONE (); +} + +#define EXPECTED_FLAGS (KVM_CLOCK_REAL_TIME | 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_REAL_TIME ; +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 ); + +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); +TEST_ASSERT (run->exit_reason ==KVM_EXIT_IO , +"unexpected exit reason: %u (%s)" , + run->exit_reason,exit_reason_str (run->exit_reason)); + +switch (get_ucall (vm,VCPU_ID , &uc)) { +case UCALL_SYNC : +handle_sync (&uc, &start, &end); +break ; +case UCALL_ABORT : +handle_abort (&uc); +return ; +case UCALL_DONE : +return ; + } + } +} + +#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 ) +{ +struct kvm_cpuid2 *best; +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_REAL_TIME )) { + print_skip("KVM_CLOCK_REAL_TIME not supported; flags: %x",
tools/testing/selftests/kvm/x86_64/kvm_clock_test.c line 202
Quoted 10 lines
tools/testing/selftests/kvm/x86_64/kvm_clock_test.c new file +10
-0+ flags); +exit (KSFT_SKIP ); + } + +check_clocksource (); + + vm =vm_create_default (VCPU_ID ,0 , guest_main); + + best =kvm_get_supported_cpuid (); +vcpu_set_cpuid (vm,VCPU_ID , best);
Default vm types (which use default vcpu types) will already set cpuids like this for x86. See the bottom of lib/x86_64/processor.c:vm_vcpu_add_default
Thanks,
drew
Quoted 16 lines
tools/testing/selftests/kvm/x86_64/kvm_clock_test.c new file +8
-0+ + 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 · 6 lines
2.32.0.402.g57bb445576-goog _______________________________________________ kvmarm mailing list [email protected] https://lists.cs.columbia.edu/mailman/listinfo/kvmarm
PATCH v2 6/12 selftests: KVM: Add helpers for vCPU device attributes
In reply to Oliver UptonvCPU 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
@@ -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
@@ -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 · 1 line
2.32.0.402.g57bb445576-goog
Re: [PATCH v2 06/12] selftests: KVM: Add helpers for vCPU device attributes
Quoted 66 lines · 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.
Signed-off-by Oliver Upton <[email protected]>--- .../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@@ -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 +27
-0@@ -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);
tools/testing/selftests/kvm/lib/kvm_util.c int kvm_device_access(int dev_fd, uint32_t group, uint64_t attr, · line 2079
Quoted 10 lines
tools/testing/selftests/kvm/lib/kvm_util.c +10
-0+} + +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; +}
The 'assert !ret's are correct here. I see they are not correct in
kvm_device_check_attr
kvm_create_device
kvm_device_access
though, as they are 'assert ret >= 0', but the documentation says 0 on success. It'd be nice to get that fixed before we build more API on top of it.
Thanks,
drew
Quoted 12 lines
tools/testing/selftests/kvm/lib/kvm_util.c +1
-0+/* * VM Dump * Signature · 6 lines
2.32.0.402.g57bb445576-goog _______________________________________________ kvmarm mailing list [email protected] https://lists.cs.columbia.edu/mailman/listinfo/kvmarm
PATCH v2 7/12 selftests: KVM: Introduce system counter offset test
In reply to Oliver UptonIntroduce 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 | 133 ++++++++++++++++++ 3 files changed, 135 insertions(+) create mode 100644 tools/testing/selftests/kvm/system_counter_offset_test.c
tools/testing/selftests/kvm/.gitignore +1 -0
@@ -50,3 +50,4 @@ /set_memory_region_test /steal_time /kvm_binary_stats_test +/system_counter_offset_test
tools/testing/selftests/kvm/Makefile +1 -0
@@ -83,6 +83,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 +133 -0
133 lines not on this page · raw message
Signature · 1 line
2.32.0.402.g57bb445576-goog
Re: [PATCH v2 07/12] selftests: KVM: Introduce system counter offset test
Quoted 180 lines · 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.
Signed-off-by Oliver Upton <[email protected]>--- tools/testing/selftests/kvm/.gitignore | 1 + tools/testing/selftests/kvm/Makefile | 1 + .../kvm/system_counter_offset_test.c | 133 ++++++++++++++++++ 3 files changed, 135 insertions(+) create mode 100644 tools/testing/selftests/kvm/system_counter_offset_test.ctools/testing/selftests/kvm/.gitignore +1
-0@@ -50,3 +50,4 @@ /set_memory_region_test /steal_time /kvm_binary_stats_test +/system_counter_offset_testtools/testing/selftests/kvm/Makefile +1
-0@@ -83,6 +83,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-listtools/testing/selftests/kvm/system_counter_offset_test.c new file +133
-0@@ -0,0 +1,133 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* +* Copyright (C) 2021, Google LLC. +* +* Tests for adjusting the system counter from userspace +*/ +#include <asm/kvm_para.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 + +#ifdef __x86_64__ + +struct test_case { +uint64_t tsc_offset; +}; + +static struct test_case test_cases[] = { + {0 }, + {180 *NSEC_PER_SEC }, + {-180 *NSEC_PER_SEC }, +}; + +static void check_preconditions (struct kvm_vm *vm) +{ +if (!_vcpu_has_device_attr (vm,VCPU_ID ,KVM_VCPU_TSC_CTRL ,KVM_VCPU_TSC_OFFSET )) +return ; + +print_skip ("KVM_VCPU_TSC_OFFSET not supported; skipping test" ); +exit (KSFT_SKIP ); +} + +static void setup_system_counter (struct kvm_vm *vm,struct test_case *test) +{ +vcpu_access_device_attr (vm,VCPU_ID ,KVM_VCPU_TSC_CTRL , +KVM_VCPU_TSC_OFFSET , &test->tsc_offset, true); +} + +static uint64_t guest_read_system_counter (struct test_case *test) +{ +return rdtsc (); +} + +static uint64_t host_read_guest_system_counter (struct test_case *test) +{ +return rdtsc () + test->tsc_offset; +} + +#else /* __x86_64__ */ + +#error test not implemented for this architecture! + +#endif + +#define GUEST_SYNC_CLOCK (__stage, __val) \ + GUEST_SYNC_ARGS(__stage, __val, 0, 0, 0) + +static void guest_main (void ) +{ +int i; + +for (i =0 ; i <ARRAY_SIZE (test_cases); i++) { +struct test_case *test = &test_cases[i]; + +GUEST_SYNC_CLOCK (i,guest_read_system_counter (test)); + } + +GUEST_DONE (); +} + +static void handle_sync (struct ucall *uc,uint64_t start,uint64_t end) +{ +uint64_t obs = uc->args[2 ]; + +TEST_ASSERT (start <= obs && obs <= end, +"unexpected system counter value: %" PRIu64" expected range: [%" PRIu64", %" PRIu64"]" , + obs, start, end); + +pr_info ("system counter value: %" PRIu64" expected range [%" PRIu64", %" PRIu64"]\n" , + obs, start, end); +} + +static void handle_abort (struct ucall *uc) +{ +TEST_FAIL ("%s at %s:%ld" , (const char *)uc->args[0 ], + __FILE__, uc->args[1 ]); +} + +static void enter_guest (struct kvm_vm *vm) +{ +uint64_t start, end; +struct ucall uc; +int i; + +for (i =0 ; i <ARRAY_SIZE (test_cases); i++) { +struct test_case *test = &test_cases[i]; + +setup_system_counter (vm, test); + start =host_read_guest_system_counter (test); +vcpu_run (vm,VCPU_ID ); + end =host_read_guest_system_counter (test); + +switch (get_ucall (vm,VCPU_ID , &uc)) { +case UCALL_SYNC : +handle_sync (&uc, start, end); +break ; +case UCALL_ABORT : +handle_abort (&uc); +return ; +case UCALL_DONE : +return ; + } + } +} + +int main (void ) +{ +struct kvm_vm *vm; + + vm =vm_create_default (VCPU_ID ,0 , guest_main); +check_preconditions (vm); +ucall_init (vm,NULL ); + +enter_guest (vm); +kvm_vm_free (vm); +}Signature · 6 lines
2.32.0.402.g57bb445576-goog _______________________________________________ kvmarm mailing list [email protected] https://lists.cs.columbia.edu/mailman/listinfo/kvmarm
PATCH v2 8/12 KVM: arm64: Allow userspace to configure a vCPU's virtual offset
In reply to Oliver UptonAdd a new vCPU attribute that allows userspace to directly manipulate the virtual counter-timer offset. Exposing such an interface allows for the precise migration of guest virtual counter-timers, as it is an indepotent interface.
Uphold the existing behavior of writes to CNTVOFF_EL2 for this new interface, wherein a write to a single vCPU is broadcasted to all vCPUs within a VM.
--- arch/arm64/include/uapi/asm/kvm.h | 1 + arch/arm64/kvm/arch_timer.c | 68 ++++++++++++++++++++++++++++++- 2 files changed, 67 insertions(+), 2 deletions(-)
arch/arm64/include/uapi/asm/kvm.h +1 -0
@@ -365,6 +365,7 @@ struct kvm_arm_copy_mte_tags {#define KVM_ARM_VCPU_TIMER_CTRL 1#define KVM_ARM_VCPU_TIMER_IRQ_VTIMER 0#define KVM_ARM_VCPU_TIMER_IRQ_PTIMER 1 +#define KVM_ARM_VCPU_TIMER_OFFSET_VTIMER 2#define KVM_ARM_VCPU_PVTIME_CTRL 2#define KVM_ARM_VCPU_PVTIME_IPA 0
arch/arm64/kvm/arch_timer.c +66 -2
91 lines not on this page · raw message
Signature · 1 line
2.32.0.402.g57bb445576-goog
PATCH v2 9/12 selftests: KVM: Add support for aarch64 to system_counter_offset_test
In reply to Oliver UptonKVM/arm64 now allows userspace to adjust the guest virtual counter-timer via a vCPU device attribute. 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 | 54 ++++++++++++++++++- 3 files changed, 66 insertions(+), 1 deletion(-)
tools/testing/selftests/kvm/Makefile +1 -0
@@ -96,6 +96,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
@@ -129,4 +129,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 +53 -1
60 lines not on this page · raw message
Signature · 1 line
2.32.0.402.g57bb445576-goog
PATCH v2 10/12 KVM: arm64: Provide userspace access to the physical counter offset
In reply to Oliver UptonPresently, KVM provides no facilities for correctly migrating a guest that depends on the physical counter-timer. While most guests (barring NV, of course) should not depend on the physical counter-timer, an operator may still wish to provide a consistent view of the physical counter-timer across migrations.
Provide userspace with a new vCPU attribute to modify the guest physical counter-timer offset. Since the base architecture doesn't provide a physical counter-timer offset register, emulate the correct behavior by trapping accesses to the physical counter-timer whenever the offset value is non-zero.
Uphold the same behavior as CNTVOFF_EL2 and broadcast the physical offset to all vCPUs whenever written. This guarantees that the counter-timer we provide the guest remains architectural, wherein all views of the counter-timer are consistent across vCPUs. Reconfigure timer traps for VHE on every guest entry, as different VMs will now have different traps enabled. Enable physical counter traps for nVHE whenever the offset is nonzero (we already trap physical timer registers in nVHE).
FEAT_ECV provides a guest physical counter-timer offset register (CNTPOFF_EL2), but ECV-enabled hardware is nonexistent at the time of writing so support for it was elided for the sake of the author :)
--- arch/arm64/include/asm/kvm_host.h | 1 + arch/arm64/include/asm/kvm_hyp.h | 2 - arch/arm64/include/asm/sysreg.h | 1 + arch/arm64/include/uapi/asm/kvm.h | 1 + arch/arm64/kvm/arch_timer.c | 50 ++++++++++++++++++++--- arch/arm64/kvm/arm.c | 4 +- arch/arm64/kvm/hyp/include/hyp/switch.h | 23 +++++++++++ arch/arm64/kvm/hyp/include/hyp/timer-sr.h | 26 ++++++++++++ arch/arm64/kvm/hyp/nvhe/switch.c | 2 - arch/arm64/kvm/hyp/nvhe/timer-sr.c | 21 +++++----- arch/arm64/kvm/hyp/vhe/timer-sr.c | 27 ++++++++++++ include/kvm/arm_arch_timer.h | 2 - 12 files changed, 136 insertions(+), 24 deletions(-) create mode 100644 arch/arm64/kvm/hyp/include/hyp/timer-sr.h
arch/arm64/include/asm/kvm_host.h +1 -0
@@ -204,6 +204,7 @@ enum vcpu_sysreg {SP_EL1 ,SPSR_EL1 , +CNTPOFF_EL2 ,CNTVOFF_EL2 ,CNTV_CVAL_EL0 , CNTV_CTL_EL0,
arch/arm64/include/asm/kvm_hyp.h +0 -2
@@ -65,10 +65,8 @@ void __vgic_v3_save_aprs(struct vgic_v3_cpu_if *cpu_if);void __vgic_v3_restore_aprs (struct vgic_v3_cpu_if *cpu_if);int __vgic_v3_perform_cpuif_access (struct kvm_vcpu *vcpu); -#ifdef __KVM_NVHE_HYPERVISOR__void __timer_enable_traps (struct kvm_vcpu *vcpu);void __timer_disable_traps (struct kvm_vcpu *vcpu); -#endif #ifdef __KVM_NVHE_HYPERVISOR__void __sysreg_save_state_nvhe (struct kvm_cpu_context *ctxt);
arch/arm64/include/asm/sysreg.h +1 -0
@@ -505,6 +505,7 @@#define SYS_AMEVCNTR0_MEM_STALL SYS_AMEVCNTR0_EL0(3)#define SYS_CNTFRQ_EL0 sys_reg(3, 3, 14, 0, 0) +#define SYS_CNTPCT_EL0 sys_reg(3, 3, 14, 0, 1)#define SYS_CNTP_TVAL_EL0 sys_reg(3, 3, 14, 2, 0)#define SYS_CNTP_CTL_EL0 sys_reg(3, 3, 14, 2, 1)
arch/arm64/include/uapi/asm/kvm.h +1 -0
@@ -366,6 +366,7 @@ struct kvm_arm_copy_mte_tags {#define KVM_ARM_VCPU_TIMER_IRQ_VTIMER 0#define KVM_ARM_VCPU_TIMER_IRQ_PTIMER 1#define KVM_ARM_VCPU_TIMER_OFFSET_VTIMER 2 +#define KVM_ARM_VCPU_TIMER_OFFSET_PTIMER 3#define KVM_ARM_VCPU_PVTIME_CTRL 2#define KVM_ARM_VCPU_PVTIME_IPA 0
arch/arm64/kvm/arch_timer.c +45 -5
121 lines not on this page · raw message
arch/arm64/kvm/arm.c +1 -3
@@ -1549,9 +1549,7 @@ static void cpu_hyp_reinit(void)cpu_hyp_reset (); -if (is_kernel_in_hyp_mode ()) -kvm_timer_init_vhe (); -else +if (!is_kernel_in_hyp_mode ())cpu_init_hyp_mode ();cpu_set_hyp_vector ();
arch/arm64/kvm/hyp/include/hyp/switch.h +23 -0
@@ -8,6 +8,7 @@#define __ARM64_KVM_HYP_SWITCH_H__#include <hyp/adjust_pc.h> +#include <hyp/timer-sr.h> #include <linux/arm-smccc.h> #include <linux/kvm_host.h>
@@ -113,6 +114,8 @@ static inline void ___activate_traps(struct kvm_vcpu *vcpu)if (cpus_have_final_cap (ARM64_HAS_RAS_EXTN ) && (hcr &HCR_VSE ))write_sysreg_s (vcpu->arch.vsesr_el2,SYS_VSESR_EL2 ); + +__timer_enable_traps (vcpu); }static inline void ___deactivate_traps (struct kvm_vcpu *vcpu)
@@ -127,6 +130,8 @@ static inline void ___deactivate_traps(struct kvm_vcpu *vcpu) vcpu->arch.hcr_el2 &= ~HCR_VSE ; vcpu->arch.hcr_el2 |=read_sysreg (hcr_el2) &HCR_VSE ; } + +__timer_disable_traps (vcpu); }static inline bool __translate_far_to_hpfar (u64 far,u64 *hpfar)
@@ -405,6 +410,21 @@ static inline bool __hyp_handle_ptrauth(struct kvm_vcpu *vcpu)return true; } +static inline bool __hyp_handle_counter (struct kvm_vcpu *vcpu) +{ +u32 sysreg =esr_sys64_to_sysreg (kvm_vcpu_get_esr (vcpu)); +int rt =kvm_vcpu_sys_get_rt (vcpu); +u64 rv; + +if (sysreg !=SYS_CNTPCT_EL0 ) +return false; + + rv =__timer_read_cntpct (vcpu); +vcpu_set_reg (vcpu, rt, rv); +__kvm_skip_instr (vcpu); +return true; +} +/* * Return true when we were able to fixup the guest exit and should return to * the guest, false when we should restore the host state and return to the
@@ -439,6 +459,9 @@ static inline bool fixup_guest_exit(struct kvm_vcpu *vcpu, u64 *exit_code)if (*exit_code !=ARM_EXCEPTION_TRAP ) goto exit; +if (__hyp_handle_counter (vcpu)) + goto guest; +if (cpus_have_final_cap (ARM64_WORKAROUND_CAVIUM_TX2_219_TVM ) &&kvm_vcpu_trap_get_class (vcpu) == ESR_ELx_EC_SYS64 &&handle_tx2_tvm (vcpu))
arch/arm64/kvm/hyp/include/hyp/timer-sr.h new file +26 -0
@@ -0,0 +1,26 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +/* +* Copyright (C) 2021 Google LLC +* Author: Oliver Upton <[email protected]> +*/ + +#ifndef __ARM64_KVM_HYP_TIMER_SR_H__ +#define __ARM64_KVM_HYP_TIMER_SR_H__ + +#include <linux/compiler.h> +#include <linux/kvm_host.h> + +#include <asm/kvm_asm.h> +#include <asm/kvm_hyp.h> + +static inline bool __timer_physical_emulation_required (struct kvm_vcpu *vcpu) +{ +return __vcpu_sys_reg (vcpu,CNTPOFF_EL2 ); +} + +static inline u64 __timer_read_cntpct (struct kvm_vcpu *vcpu) +{ +return read_sysreg (cntpct_el0) -__vcpu_sys_reg (vcpu,CNTPOFF_EL2 ); +} + +#endif /* __ARM64_KVM_HYP_TIMER_SR_H__ */
arch/arm64/kvm/hyp/nvhe/switch.c +0 -2
@@ -217,7 +217,6 @@ int __kvm_vcpu_run(struct kvm_vcpu *vcpu)__activate_traps (vcpu);__hyp_vgic_restore_state (vcpu); -__timer_enable_traps (vcpu);__debug_switch_to_guest (vcpu);
@@ -230,7 +229,6 @@ int __kvm_vcpu_run(struct kvm_vcpu *vcpu)__sysreg_save_state_nvhe (guest_ctxt);__sysreg32_save_state (vcpu); -__timer_disable_traps (vcpu);__hyp_vgic_save_state (vcpu);__deactivate_traps (vcpu);
arch/arm64/kvm/hyp/nvhe/timer-sr.c +11 -10
@@ -9,16 +9,13 @@#include <linux/kvm_host.h> #include <asm/kvm_hyp.h> +#include <hyp/timer-sr.h> void __kvm_timer_set_cntvoff (u64 cntvoff) {write_sysreg (cntvoff, cntvoff_el2); } -/* -* Should only be called on non-VHE systems. -* VHE systems use EL2 timers and configure EL1 timers in kvm_timer_init_vhe(). -*/ void __timer_disable_traps (struct kvm_vcpu *vcpu) {u64 val;
@@ -29,20 +26,24 @@ void __timer_disable_traps(struct kvm_vcpu *vcpu)write_sysreg (val, cnthctl_el2); } -/* -* Should only be called on non-VHE systems. -* VHE systems use EL2 timers and configure EL1 timers in kvm_timer_init_vhe(). -*/ void __timer_enable_traps (struct kvm_vcpu *vcpu) {u64 val;/* * Disallow physical timer access for the guest -* Physical counter access is allowed */ val =read_sysreg (cnthctl_el2); val &= ~CNTHCTL_EL1PCEN ; - val |=CNTHCTL_EL1PCTEN ; + +/* +* Disallow physical counter access for the guest if offsetting is +* requested. +*/ +if (__timer_physical_emulation_required (vcpu)) + val &= ~CNTHCTL_EL1PCTEN ; +else + val |=CNTHCTL_EL1PCTEN ; +write_sysreg (val, cnthctl_el2); }
arch/arm64/kvm/hyp/vhe/timer-sr.c +27 -0
@@ -4,9 +4,36 @@* Author: Marc Zyngier <[email protected]> */ +#include <asm/kvm_host.h> #include <asm/kvm_hyp.h> +#include <hyp/timer-sr.h> void __kvm_timer_set_cntvoff (u64 cntvoff) {write_sysreg (cntvoff, cntvoff_el2); } + +void __timer_enable_traps (struct kvm_vcpu *vcpu) +{ +/* When HCR_EL2.E2H == 1, EL1PCEN nad EL1PCTEN are shifted by 10 */ +u32 cnthctl_shift =10 ; +u64 val, mask; + + mask =CNTHCTL_EL1PCEN << cnthctl_shift; + mask |=CNTHCTL_EL1PCTEN << cnthctl_shift; + + val =read_sysreg (cnthctl_el2); + +/* +* VHE systems allow the guest direct access to the EL1 physical +* timer/counter if offsetting isn't requested. +*/ +if (__timer_physical_emulation_required (vcpu)) + val &= ~mask; +else + val |= mask; + +write_sysreg (val, cnthctl_el2); +} + +void __timer_disable_traps (struct kvm_vcpu *vcpu) {}
include/kvm/arm_arch_timer.h +0 -2
@@ -83,8 +83,6 @@ u64 kvm_phys_timer_read(void);void kvm_timer_vcpu_load (struct kvm_vcpu *vcpu);void kvm_timer_vcpu_put (struct kvm_vcpu *vcpu); -void kvm_timer_init_vhe (void ); -bool kvm_arch_timer_get_input_level (int vintid);#define vcpu_timer (v) (&(v)->arch.timer_cpu)
Signature · 1 line
2.32.0.402.g57bb445576-goog
PATCH v2 11/12 selftests: KVM: Test physical counter offsetting
In reply to Oliver UptonTest that userpace adjustment of the guest physical counter-timer results in the correct view of within the guest.
--- .../selftests/kvm/include/aarch64/processor.h | 12 ++++++++ .../kvm/system_counter_offset_test.c | 29 ++++++++++++++++--- 2 files changed, 37 insertions(+), 4 deletions(-)
tools/testing/selftests/kvm/include/aarch64/processor.h +12 -0
@@ -141,4 +141,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 +25 -4
@@ -57,6 +57,7 @@ static uint64_t host_read_guest_system_counter(struct test_case *test)enum arch_counter {VIRTUAL , +PHYSICAL , };struct test_case {
@@ -68,23 +69,41 @@ 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 }, };static void check_preconditions (struct kvm_vm *vm) {if (!_vcpu_has_device_attr (vm,VCPU_ID ,KVM_ARM_VCPU_TIMER_CTRL , -KVM_ARM_VCPU_TIMER_OFFSET_VTIMER )) +KVM_ARM_VCPU_TIMER_OFFSET_VTIMER ) && + !_vcpu_has_device_attr (vm,VCPU_ID ,KVM_ARM_VCPU_TIMER_CTRL , +KVM_ARM_VCPU_TIMER_OFFSET_PTIMER ))return ; -print_skip ("KVM_ARM_VCPU_TIMER_OFFSET_VTIMER not supported; skipping test" ); +print_skip ("KVM_ARM_VCPU_TIMER_OFFSET_{VTIMER,PTIMER} not supported; skipping test" );exit (KSFT_SKIP ); }static void setup_system_counter (struct kvm_vm *vm,struct test_case *test) { +u64 attr =0 ; + +switch (test->counter) { +case VIRTUAL : + attr =KVM_ARM_VCPU_TIMER_OFFSET_VTIMER ; +break ; +case PHYSICAL : + attr =KVM_ARM_VCPU_TIMER_OFFSET_PTIMER ; +break ; +default : +TEST_ASSERT (false,"unrecognized counter index %u" , + test->counter); + } +vcpu_access_device_attr (vm,VCPU_ID ,KVM_ARM_VCPU_TIMER_CTRL , -KVM_ARM_VCPU_TIMER_OFFSET_VTIMER , &test->offset, - true); + attr, &test->offset, true); }static uint64_t guest_read_system_counter (struct test_case *test)
@@ -92,6 +111,8 @@ static uint64_t guest_read_system_counter(struct test_case *test)switch (test->counter) {case VIRTUAL :return read_cntvct_ordered (); +case PHYSICAL : +return read_cntpct_ordered ();default :GUEST_ASSERT (0 ); }
Signature · 1 line
2.32.0.402.g57bb445576-goog
PATCH v2 12/12 selftests: KVM: Add counter emulation benchmark
In reply to Oliver UptonAdd 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 | 215 ++++++++++++++++++ 3 files changed, 217 insertions(+) create mode 100644 tools/testing/selftests/kvm/aarch64/counter_emulation_benchmark.c
tools/testing/selftests/kvm/.gitignore +1 -0
@@ -1,5 +1,6 @@ # SPDX-License-Identifier: GPL-2.0-only /aarch64/debug-exceptions +/aarch64/counter_emulation_benchmark /aarch64/get-reg-list /aarch64/vgic_init /s390x/memop
tools/testing/selftests/kvm/Makefile +1 -0
@@ -86,6 +86,7 @@ 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/counter_emulation_benchmark TEST_GEN_PROGS_aarch64 += aarch64/get-reg-list TEST_GEN_PROGS_aarch64 += aarch64/vgic_init TEST_GEN_PROGS_aarch64 += demand_paging_test
tools/testing/selftests/kvm/aarch64/counter_emulation_benchmark.c new file +215 -0
215 lines not on this page · raw message
Signature · 1 line
2.32.0.402.g57bb445576-goog
Quoted 85 lines · Oliver Upton
KVM's current means of saving/restoring system counters is plagued with temporal issues. At least on ARM64 and x86, we migrate the guest's system counter by-value through the respective guest system register values (
cntvct_el0,ia32_tsc). Restoring system counters by-value is brittle as the state is not idempotent: the host system counter is still oscillating between the attempted save and restore. Furthermore, VMMs may wish to transparently live migrate guest VMs, meaning that they include the elapsed time due to live migration blackout in the guest system counter view. The VMM thread could be preempted for any number of reasons (scheduler, L0 hypervisor under nested) between the time that it calculates the desired guest counter value and when KVM actually sets this counter state.Despite the value-based interface that we present to userspace, KVM actually has idempotent guest controls by way of system counter offsets. We can avoid all of the issues associated with a value-based interface by abstracting these offset controls in new ioctls. This series introduces new vCPU device attributes to provide userspace access to the vCPU's system counter offset.
Patch 1 adopts Paolo's suggestion, augmenting the KVM_{GET,SET}_CLOCK ioctls to provide userspace with a (
host_tsc, realtime) instant. This is essential for a VMM to perform precise migration of the guest's system counters.Patches 2-3 add support for x86 by shoehorning the new controls into the pre-existing synchronization heuristics.
Patches 4-5 implement a test for the new additions to KVM_{GET,SET}_CLOCK.
Patches 6-7 implement at test for the tsc offset attribute introduced in patch 3.
Patch 8 adds a device attribute for the arm64 virtual counter-timer offset.
Patch 9 extends the test from patch 7 to cover the arm64 virtual counter-timer offset.
Patch 10 adds a device attribute for the arm64 physical counter-timer offset. Currently, this is implemented as a synthetic register, forcing the guest to trap to the host and emulating the offset in the fast exit path. Later down the line we will have hardware with
FEAT_ECV, which allows the hypervisor to perform physical counter-timer offsetting in hardware (CNTPOFF_EL2).Patch 11 extends the test from patch 7 to cover the arm64 physical counter-timer offset.
Patch 12 introduces a benchmark to measure the overhead of emulation in patch 10.
Physical counter benchmark
--------------------------The following data was collected by running 10000 iterations of the benchmark test from Patch 6 on an Ampere Mt. Jade reference server, A 2S machine with 2 80-core Ampere Altra SoCs. Measurements were collected for both VHE and nVHE operation using the
kvm-arm.mode=command-line parameter.nVHE
----+--------------------+--------+---------+ | Metric | Native | Trapped | +--------------------+--------+---------+ | Average | 54ns | 148ns | | Standard Deviation | 124ns | 122ns | | 95th Percentile | 258ns | 348ns | +--------------------+--------+---------+VHE
---+--------------------+--------+---------+ | Metric | Native | Trapped | +--------------------+--------+---------+ | Average | 53ns | 152ns | | Standard Deviation | 92ns | 94ns | | 95th Percentile | 204ns | 307ns | +--------------------+--------+---------+
Quoted 3 lines
This series applies cleanly to the following commit:
1889228d80fe("KVM: selftests:smm_test: Test SMM enter from L2")
v1: https://lore.kernel.org/kvm/[email protected]/
Quoted 60 lines
v1 -> v2:
- Reimplemented as vCPU device attributes instead of a distinct ioctl.
- Added the (realtime,host_tsc) instant support toKVM_{GET,SET}_CLOCK- Changed the arm64 implementation to broadcast counter offset values to all vCPUs in a guest. This upholds the architectural expectations of a consistent counter-timer across CPUs.
- Fixed a bug with traps in VHE mode. We now configure traps on every transition into a guest to handle differing VMs (trapped, emulated).Oliver Upton (12):
KVM: x86: Report host tsc and realtime values inKVM_GET_CLOCK
KVM: x86: Refactor tsc synchronization code
KVM: x86: Expose TSC offset controls to userspace
tools: arch: x86: pull in pvclock headers
selftests: KVM: Add test for KVM_{GET,SET}_CLOCK
selftests: KVM: Add helpers for vCPU device attributes
selftests: KVM: Introduce system counter offset test
KVM: arm64: Allow userspace to configure a vCPU's virtual offset
selftests: KVM: Add support for aarch64 tosystem_counter_offset_test
KVM: arm64: Provide userspace access to the physical counter offset
selftests: KVM: Test physical counter offsetting
selftests: KVM: Add counter emulation benchmarkDocumentation/virt/kvm/api.rst | 42 +- Documentation/virt/kvm/locking.rst | 11 + arch/arm64/include/asm/kvm_host.h | 1 + arch/arm64/include/asm/kvm_hyp.h | 2 - arch/arm64/include/asm/sysreg.h | 1 + arch/arm64/include/uapi/asm/kvm.h | 2 + arch/arm64/kvm/arch_timer.c | 118 ++++- arch/arm64/kvm/arm.c | 4 +- arch/arm64/kvm/hyp/include/hyp/switch.h | 23 + arch/arm64/kvm/hyp/include/hyp/timer-sr.h | 26 ++ arch/arm64/kvm/hyp/nvhe/switch.c | 2 - arch/arm64/kvm/hyp/nvhe/timer-sr.c | 21 +- arch/arm64/kvm/hyp/vhe/timer-sr.c | 27 ++ arch/x86/include/asm/kvm_host.h | 4 + arch/x86/include/uapi/asm/kvm.h | 4 + arch/x86/kvm/x86.c | 421 ++++++++++++++---- include/kvm/arm_arch_timer.h | 2 - include/uapi/linux/kvm.h | 7 +- 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 | 215 +++++++++ .../selftests/kvm/include/aarch64/processor.h | 24 + .../testing/selftests/kvm/include/kvm_util.h | 11 + tools/testing/selftests/kvm/lib/kvm_util.c | 38 ++ .../kvm/system_counter_offset_test.c | 206 +++++++++ .../selftests/kvm/x86_64/kvm_clock_test.c | 210 +++++++++ 28 files changed, 1447 insertions(+), 133 deletions(-) create mode 100644 arch/arm64/kvm/hyp/include/hyp/timer-sr.h 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
Quoted 3 lines
--
2.32.0.402.g57bb445576-goog
_______________________________________________
kvmarm mailing list
[email protected]
https://lists.cs.columbia.edu/mailman/listinfo/kvmarm
Quoted 91 lines · Oliver Upton
KVM's current means of saving/restoring system counters is plagued with temporal issues. At least on ARM64 and x86, we migrate the guest's system counter by-value through the respective guest system register values (
cntvct_el0,ia32_tsc). Restoring system counters by-value is brittle as the state is not idempotent: the host system counter is still oscillating between the attempted save and restore. Furthermore, VMMs may wish to transparently live migrate guest VMs, meaning that they include the elapsed time due to live migration blackout in the guest system counter view. The VMM thread could be preempted for any number of reasons (scheduler, L0 hypervisor under nested) between the time that it calculates the desired guest counter value and when KVM actually sets this counter state.Despite the value-based interface that we present to userspace, KVM actually has idempotent guest controls by way of system counter offsets. We can avoid all of the issues associated with a value-based interface by abstracting these offset controls in new ioctls. This series introduces new vCPU device attributes to provide userspace access to the vCPU's system counter offset.
Patch 1 adopts Paolo's suggestion, augmenting the KVM_{GET,SET}_CLOCK ioctls to provide userspace with a (
host_tsc, realtime) instant. This is essential for a VMM to perform precise migration of the guest's system counters.Patches 2-3 add support for x86 by shoehorning the new controls into the pre-existing synchronization heuristics.
Patches 4-5 implement a test for the new additions to KVM_{GET,SET}_CLOCK.
Patches 6-7 implement at test for the tsc offset attribute introduced in patch 3.
Patch 8 adds a device attribute for the arm64 virtual counter-timer offset.
Patch 9 extends the test from patch 7 to cover the arm64 virtual counter-timer offset.
Patch 10 adds a device attribute for the arm64 physical counter-timer offset. Currently, this is implemented as a synthetic register, forcing the guest to trap to the host and emulating the offset in the fast exit path. Later down the line we will have hardware with
FEAT_ECV, which allows the hypervisor to perform physical counter-timer offsetting in hardware (CNTPOFF_EL2).Patch 11 extends the test from patch 7 to cover the arm64 physical counter-timer offset.
Patch 12 introduces a benchmark to measure the overhead of emulation in patch 10.
Physical counter benchmark
--------------------------The following data was collected by running 10000 iterations of the benchmark test from Patch 6 on an Ampere Mt. Jade reference server, A 2S machine with 2 80-core Ampere Altra SoCs. Measurements were collected for both VHE and nVHE operation using the
kvm-arm.mode=command-line parameter.nVHE
----+--------------------+--------+---------+ | Metric | Native | Trapped | +--------------------+--------+---------+ | Average | 54ns | 148ns | | Standard Deviation | 124ns | 122ns | | 95th Percentile | 258ns | 348ns | +--------------------+--------+---------+VHE
---+--------------------+--------+---------+ | Metric | Native | Trapped | +--------------------+--------+---------+ | Average | 53ns | 152ns | | Standard Deviation | 92ns | 94ns | | 95th Percentile | 204ns | 307ns | +--------------------+--------+---------+This series applies cleanly to the following commit:
1889228d80fe("KVM: selftests:smm_test: Test SMM enter from L2")v1 -> v2:
- Reimplemented as vCPU device attributes instead of a distinct ioctl.
- Added the (realtime,host_tsc) instant support to
Quoted 7 lines
KVM_{GET,SET}_CLOCK- Changed the arm64 implementation to broadcast counter offset values to all vCPUs in a guest. This upholds the architectural expectations of a consistent counter-timer across CPUs.
- Fixed a bug with traps in VHE mode. We now configure traps on every transition into a guest to handle differing VMs (trapped, emulated).
Oops, I see there's a v3 of this series. I'll switch to reviewing that. I think my comments / r-b's apply to that version as well though.
Thanks,
drew
_______________________________________________
kvmarm mailing list
[email protected]
https://lists.cs.columbia.edu/mailman/listinfo/kvmarm
Quoted 101 lines · Andrew Jones
Quoted 91 lines · Oliver Upton
KVM's current means of saving/restoring system counters is plagued with temporal issues. At least on ARM64 and x86, we migrate the guest's system counter by-value through the respective guest system register values (
cntvct_el0,ia32_tsc). Restoring system counters by-value is brittle as the state is not idempotent: the host system counter is still oscillating between the attempted save and restore. Furthermore, VMMs may wish to transparently live migrate guest VMs, meaning that they include the elapsed time due to live migration blackout in the guest system counter view. The VMM thread could be preempted for any number of reasons (scheduler, L0 hypervisor under nested) between the time that it calculates the desired guest counter value and when KVM actually sets this counter state.Despite the value-based interface that we present to userspace, KVM actually has idempotent guest controls by way of system counter offsets. We can avoid all of the issues associated with a value-based interface by abstracting these offset controls in new ioctls. This series introduces new vCPU device attributes to provide userspace access to the vCPU's system counter offset.
Patch 1 adopts Paolo's suggestion, augmenting the KVM_{GET,SET}_CLOCK ioctls to provide userspace with a (
host_tsc, realtime) instant. This is essential for a VMM to perform precise migration of the guest's system counters.Patches 2-3 add support for x86 by shoehorning the new controls into the pre-existing synchronization heuristics.
Patches 4-5 implement a test for the new additions to KVM_{GET,SET}_CLOCK.
Patches 6-7 implement at test for the tsc offset attribute introduced in patch 3.
Patch 8 adds a device attribute for the arm64 virtual counter-timer offset.
Patch 9 extends the test from patch 7 to cover the arm64 virtual counter-timer offset.
Patch 10 adds a device attribute for the arm64 physical counter-timer offset. Currently, this is implemented as a synthetic register, forcing the guest to trap to the host and emulating the offset in the fast exit path. Later down the line we will have hardware with
FEAT_ECV, which allows the hypervisor to perform physical counter-timer offsetting in hardware (CNTPOFF_EL2).Patch 11 extends the test from patch 7 to cover the arm64 physical counter-timer offset.
Patch 12 introduces a benchmark to measure the overhead of emulation in patch 10.
Physical counter benchmark
--------------------------The following data was collected by running 10000 iterations of the benchmark test from Patch 6 on an Ampere Mt. Jade reference server, A 2S machine with 2 80-core Ampere Altra SoCs. Measurements were collected for both VHE and nVHE operation using the
kvm-arm.mode=command-line parameter.nVHE
----+--------------------+--------+---------+ | Metric | Native | Trapped | +--------------------+--------+---------+ | Average | 54ns | 148ns | | Standard Deviation | 124ns | 122ns | | 95th Percentile | 258ns | 348ns | +--------------------+--------+---------+VHE
---+--------------------+--------+---------+ | Metric | Native | Trapped | +--------------------+--------+---------+ | Average | 53ns | 152ns | | Standard Deviation | 92ns | 94ns | | 95th Percentile | 204ns | 307ns | +--------------------+--------+---------+This series applies cleanly to the following commit:
1889228d80fe("KVM: selftests:smm_test: Test SMM enter from L2")v1 -> v2:
- Reimplemented as vCPU device attributes instead of a distinct ioctl.
- Added the (realtime,host_tsc) instant support toQuoted 7 lines
KVM_{GET,SET}_CLOCK- Changed the arm64 implementation to broadcast counter offset values to all vCPUs in a guest. This upholds the architectural expectations of a consistent counter-timer across CPUs.
- Fixed a bug with traps in VHE mode. We now configure traps on every transition into a guest to handle differing VMs (trapped, emulated).
Quoted 2 lines
Oops, I see there's a v3 of this series. I'll switch to reviewing that. I think my comments / r-b's apply to that version as well though.
Hey Drew,
Thanks for the review. I'll address your comments from both v2 and v3 in the next series.
--
Thanks,
Oliver