arm64: Stolen time support
Subsystems (8)
cover arm64: Stolen time support
This series add support for paravirtualized time for arm64 guests and KVM hosts following the specification in Arm's document DEN 0057A:
https://developer.arm.com/docs/den0057/a
It implements support for stolen time, allowing the guest to identify time when it is forcibly not executing.
It doesn't implement support for Live Physical Time (LPT) as there are some concerns about the overheads and approach in the above specification, and I expect an updated version of the specification to be released soon with just the stolen time parts.
I previously posted a series including LPT (as well as stolen time):
https://lore.kernel.org/kvmarm/[email protected]/
Patches 2, 5, 7 and 8 are cleanup patches and could be taken separately.
Christoffer Dall (1):
KVM: arm/arm64: Factor out hypercall handling from PSCI code
Steven Price (8):
KVM: arm64: Document PV-time interface
KVM: arm64: Implement PV_FEATURES call
KVM: arm64: Support stolen time reporting via shared structure
KVM: Allow kvm_device_ops to be const
KVM: arm64: Provide a PV_TIME device to user space arm/arm64: Provide a wrapper for SMCCC 1.1 calls arm/arm64: Make use of the SMCCC 1.1 wrapper
arm64: Retrieve stolen time as paravirtualized guest
Documentation/virtual/kvm/arm/pvtime.txt | 107 +++++++++++++ arch/arm/kvm/Makefile | 2 +- arch/arm/kvm/handle_exit.c | 2 +- arch/arm/mm/proc-v7-bugs.c | 13 +- arch/arm64/include/asm/kvm_host.h | 13 +- arch/arm64/include/asm/kvm_mmu.h | 2 + arch/arm64/include/asm/pvclock-abi.h | 20 +++ arch/arm64/include/uapi/asm/kvm.h | 6 + arch/arm64/kernel/Makefile | 1 + arch/arm64/kernel/cpu_errata.c | 80 ++++------ arch/arm64/kernel/kvm.c | 155 ++++++++++++++++++ arch/arm64/kvm/Kconfig | 1 + arch/arm64/kvm/Makefile | 2 + arch/arm64/kvm/handle_exit.c | 4 +- include/kvm/arm_hypercalls.h | 44 ++++++ include/kvm/arm_psci.h | 2 +- include/linux/arm-smccc.h | 58 +++++++ include/linux/cpuhotplug.h | 1 + include/linux/kvm_host.h | 4 +- include/linux/kvm_types.h | 2 + include/uapi/linux/kvm.h | 2 + virt/kvm/arm/arm.c | 18 +++ virt/kvm/arm/hypercalls.c | 138 ++++++++++++++++ virt/kvm/arm/mmu.c | 44 ++++++ virt/kvm/arm/psci.c | 84 +--------- virt/kvm/arm/pvtime.c | 190 +++++++++++++++++++++++ virt/kvm/kvm_main.c | 6 +- 27 files changed, 848 insertions(+), 153 deletions(-) create mode 100644 Documentation/virtual/kvm/arm/pvtime.txt create mode 100644 arch/arm64/include/asm/pvclock-abi.h create mode 100644 arch/arm64/kernel/kvm.c create mode 100644 include/kvm/arm_hypercalls.h create mode 100644 virt/kvm/arm/hypercalls.c create mode 100644 virt/kvm/arm/pvtime.c
Signature · 6 lines
2.20.1 _______________________________________________ kvmarm mailing list [email protected] https://lists.cs.columbia.edu/mailman/listinfo/kvmarm
PATCH 1/9 KVM: arm64: Document PV-time interface
Introduce a paravirtualization interface for KVM/arm64 based on the "Arm Paravirtualized Time for Arm-Base Systems" specification DEN 0057A.
This only adds the details about "Stolen Time" as the details of "Live Physical Time" have not been fully agreed.
User space can specify a reserved area of memory for the guest and inform KVM to populate the memory with information on time that the host kernel has stolen from the guest.
A hypercall interface is provided for the guest to interrogate the hypervisor's support for this interface and the location of the shared memory structures.
--- Documentation/virtual/kvm/arm/pvtime.txt | 107 +++++++++++++++++++++++ 1 file changed, 107 insertions(+) create mode 100644 Documentation/virtual/kvm/arm/pvtime.txt
Documentation/virtual/kvm/arm/pvtime.txt new file +107 -0
@@ -0,0 +1,107 @@ +Paravirtualized time support for arm64 +====================================== + +Arm specification DEN0057/A defined a standard for paravirtualised time +support for Aarch64 guests: + +https://developer.arm.com/docs/den0057/a + +KVM/Arm64 implements the stolen time part of this specification by providing +some hypervisor service calls to support a paravirtualized guest obtaining a +view of the amount of time stolen from its execution. + +Two new SMCCC compatible hypercalls are defined: + +PV_FEATURES 0xC5000020 +PV_TIME_ST 0xC5000022 + +These are only available in the SMC64/HVC64 calling convention as +paravirtualized time is not available to 32 bit Arm guests. + +PV_FEATURES + Function ID: (uint32) : 0xC5000020 + PV_func_id: (uint32) : Either PV_TIME_LPT or PV_TIME_ST + Return value: (int32) : NOT_SUPPORTED (-1) or SUCCESS (0) if the relevant + PV-time feature is supported by the hypervisor. + +PV_TIME_ST + Function ID: (uint32) : 0xC5000022 + Return value: (int64) : IPA of the stolen time data structure for this + (V)CPU. On failure: + NOT_SUPPORTED (-1) + +Stolen Time +----------- + +The structure pointed to by the PV_TIME_ST hypercall is as follows: + + Field | Byte Length | Byte Offset | Description + ----------- | ----------- | ----------- | -------------------------- + Revision | 4 | 0 | Must be 0 for version 0.1 + Attributes | 4 | 4 | Must be 0 + Stolen time | 8 | 8 | Stolen time in unsigned + | | | nanoseconds indicating how + | | | much time this VCPU thread + | | | was involuntarily not + | | | running on a physical CPU. + +The structure will be updated by the hypervisor periodically as time is stolen +from the VCPU. It will be present within a reserved region of the normal +memory given to the guest. The guest should not attempt to write into this +memory. There is a structure by VCPU of the guest. + +User space interface +==================== + +User space can request that KVM provide the paravirtualized time interface to +a guest by creating a KVM_DEV_TYPE_ARM_PV_TIME device, for example: + + struct kvm_create_device pvtime_device = { + .type = KVM_DEV_TYPE_ARM_PV_TIME, + .attr = 0, + .flags = 0, + }; + + pvtime_fd = ioctl(vm_fd, KVM_CREATE_DEVICE, &pvtime_device); + +The guest IPA of the structures must be given to KVM. This is the base address +of an array of stolen time structures (one for each VCPU). For example: + + struct kvm_device_attr st_base = { + .group = KVM_DEV_ARM_PV_TIME_PADDR, + .attr = KVM_DEV_ARM_PV_TIME_ST, + .addr = (u64)(unsigned long)&st_paddr + }; + + ioctl(pvtime_fd, KVM_SET_DEVICE_ATTR, &st_base); + +For migration (or save/restore) of a guest it is necessary to save the contents +of the shared page(s) and later restore them. KVM_DEV_ARM_PV_TIME_STATE_SIZE +provides the size of this data and KVM_DEV_ARM_PV_TIME_STATE allows the state +to be read/written. + +It is also necessary for the physical address to be set identically when +restoring. + + void *save_state(int fd, u64 attr, u32 *size) { + struct kvm_device_attr get_size = { + .group = KVM_DEV_ARM_PV_TIME_STATE_SIZE, + .attr = attr, + .addr = (u64)(unsigned long)size + }; + + ioctl(fd, KVM_GET_DEVICE_ATTR, get_size); + + void *buffer = malloc(*size); + + struct kvm_device_attr get_state = { + .group = KVM_DEV_ARM_PV_TIME_STATE, + .attr = attr, + .addr = (u64)(unsigned long)size + }; + + ioctl(fd, KVM_GET_DEVICE_ATTR, buffer); + } + + void *st_state = save_state(pvtime_fd, KVM_DEV_ARM_PV_TIME_ST, &st_size); +
Signature · 1 line
2.20.1
PATCH 2/9 KVM: arm/arm64: Factor out hypercall handling from PSCI code
In reply to Steven PriceFrom: Christoffer Dall <[email protected]>
We currently intertwine the KVM PSCI implementation with the general dispatch of hypercall handling, which makes perfect sense because PSCI is the only category of hypercalls we support.
However, as we are about to support additional hypercalls, factor out this functionality into a separate hypercall handler file.
--- arch/arm/kvm/Makefile | 2 +- arch/arm/kvm/handle_exit.c | 2 +- arch/arm64/kvm/Makefile | 1 + arch/arm64/kvm/handle_exit.c | 4 +- include/kvm/arm_hypercalls.h | 43 ++++++++++++++++++ include/kvm/arm_psci.h | 2 +- virt/kvm/arm/hypercalls.c | 59 +++++++++++++++++++++++++ virt/kvm/arm/psci.c | 84 +----------------------------------- 8 files changed, 110 insertions(+), 87 deletions(-) create mode 100644 include/kvm/arm_hypercalls.h create mode 100644 virt/kvm/arm/hypercalls.c
arch/arm/kvm/Makefile +1 -1
@@ -23,7 +23,7 @@ obj-y += kvm-arm.o init.o interrupts.o obj-y += handle_exit.o guest.o emulate.o reset.o obj-y += coproc.o coproc_a15.o coproc_a7.o vgic-v3-coproc.o obj-y += $(KVM)/arm/arm.o $(KVM)/arm/mmu.o $(KVM)/arm/mmio.o -obj-y += $(KVM)/arm/psci.o $(KVM)/arm/perf.o +obj-y += $(KVM)/arm/psci.o $(KVM)/arm/perf.o $(KVM)/arm/hypercalls.o obj-y += $(KVM)/arm/aarch32.o obj-y += $(KVM)/arm/vgic/vgic.o
arch/arm/kvm/handle_exit.c +1 -1
@@ -9,7 +9,7 @@#include <asm/kvm_emulate.h> #include <asm/kvm_coproc.h> #include <asm/kvm_mmu.h> -#include <kvm/arm_psci.h> +#include <kvm/arm_hypercalls.h> #include <trace/events/kvm.h> #include "trace.h"
arch/arm64/kvm/Makefile +1 -0
@@ -13,6 +13,7 @@ obj-$(CONFIG_KVM_ARM_HOST) += hyp/ kvm-$(CONFIG_KVM_ARM_HOST) += $(KVM)/kvm_main.o $(KVM)/coalesced_mmio.o $(KVM)/eventfd.o $(KVM)/vfio.o kvm-$(CONFIG_KVM_ARM_HOST) += $(KVM)/arm/arm.o $(KVM)/arm/mmu.o $(KVM)/arm/mmio.o kvm-$(CONFIG_KVM_ARM_HOST) += $(KVM)/arm/psci.o $(KVM)/arm/perf.o +kvm-$(CONFIG_KVM_ARM_HOST) += $(KVM)/arm/hypercalls.o kvm-$(CONFIG_KVM_ARM_HOST) += inject_fault.o regmap.o va_layout.o kvm-$(CONFIG_KVM_ARM_HOST) += hyp.o hyp-init.o handle_exit.o
arch/arm64/kvm/handle_exit.c +2 -2
@@ -11,8 +11,6 @@#include <linux/kvm.h> #include <linux/kvm_host.h> -#include <kvm/arm_psci.h> -#include <asm/esr.h> #include <asm/exception.h> #include <asm/kvm_asm.h>
@@ -22,6 +20,8 @@#include <asm/debug-monitors.h> #include <asm/traps.h> +#include <kvm/arm_hypercalls.h> +#define CREATE_TRACE_POINTS #include "trace.h"
include/kvm/arm_hypercalls.h new file +43 -0
43 lines not on this page · raw message
include/kvm/arm_psci.h +1 -1
@@ -40,7 +40,7 @@ static inline int kvm_psci_version(struct kvm_vcpu *vcpu, struct kvm *kvm) } -int kvm_hvc_call_handler (struct kvm_vcpu *vcpu); +int kvm_psci_call (struct kvm_vcpu *vcpu);struct kvm_one_reg ;
virt/kvm/arm/hypercalls.c new file +59 -0
59 lines not on this page · raw message
virt/kvm/arm/psci.c +2 -82
108 lines not on this page · raw message
Signature · 1 line
2.20.1
PATCH 3/9 KVM: arm64: Implement PV_FEATURES call
In reply to Steven PriceThis provides a mechanism for querying which paravirtualized features are available in this hypervisor.
Also add the header file which defines the ABI for the paravirtualized clock features we're about to add.
--- arch/arm64/include/asm/pvclock-abi.h | 20 ++++++++++++++++++++ include/linux/arm-smccc.h | 14 ++++++++++++++ virt/kvm/arm/hypercalls.c | 9 +++++++++ 3 files changed, 43 insertions(+) create mode 100644 arch/arm64/include/asm/pvclock-abi.h
arch/arm64/include/asm/pvclock-abi.h new file +20 -0
@@ -0,0 +1,20 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* Copyright (C) 2019 Arm Ltd. */ + +#ifndef __ASM_PVCLOCK_ABI_H +#define __ASM_PVCLOCK_ABI_H + +/* The below structures and constants are defined in ARM DEN0057A */ + +struct pvclock_vcpu_stolen_time_info { +__le32 revision; +__le32 attributes; +__le64 stolen_time; +/* Structure must be 64 byte aligned, pad to that size */ +u8 padding[48 ]; +} __packed; + +#define PV_VM_TIME_NOT_SUPPORTED -1 +#define PV_VM_TIME_INVALID_PARAMETERS -2 + +#endif
include/linux/arm-smccc.h +14 -0
@@ -45,6 +45,7 @@#define ARM_SMCCC_OWNER_SIP 2#define ARM_SMCCC_OWNER_OEM 3#define ARM_SMCCC_OWNER_STANDARD 4 +#define ARM_SMCCC_OWNER_STANDARD_HYP 5#define ARM_SMCCC_OWNER_TRUSTED_APP 48#define ARM_SMCCC_OWNER_TRUSTED_APP_END 49#define ARM_SMCCC_OWNER_TRUSTED_OS 50
@@ -302,5 +303,18 @@ asmlinkage void __arm_smccc_hvc(unsigned long a0, unsigned long a1,#define SMCCC_RET_NOT_SUPPORTED -1#define SMCCC_RET_NOT_REQUIRED -2 +/* Paravirtualised time calls (defined by ARM DEN0057A) */ +#define ARM_SMCCC_HV_PV_FEATURES \ + ARM_SMCCC_CALL_VAL(ARM_SMCCC_FAST_CALL, \ + ARM_SMCCC_SMC_64, \ + ARM_SMCCC_OWNER_STANDARD_HYP, \ + 0x20) + +#define ARM_SMCCC_HV_PV_TIME_ST \ + ARM_SMCCC_CALL_VAL(ARM_SMCCC_FAST_CALL, \ + ARM_SMCCC_SMC_64, \ + ARM_SMCCC_OWNER_STANDARD_HYP, \ + 0x22) +#endif /*__ASSEMBLY__*/ #endif /*__LINUX_ARM_SMCCC_H*/
virt/kvm/arm/hypercalls.c +9 -0
@@ -5,6 +5,7 @@#include <linux/kvm_host.h> #include <asm/kvm_emulate.h> +#include <asm/pvclock-abi.h> #include <kvm/arm_hypercalls.h> #include <kvm/arm_psci.h>
@@ -48,6 +49,14 @@ int kvm_hvc_call_handler(struct kvm_vcpu *vcpu)break ; }break ; +case ARM_SMCCC_HV_PV_FEATURES : + val =SMCCC_RET_SUCCESS ; +break ; + } +break ; +case ARM_SMCCC_HV_PV_FEATURES : + feature =smccc_get_arg1 (vcpu); +switch (feature) { }break ;default :
Signature · 6 lines
2.20.1 _______________________________________________ kvmarm mailing list [email protected] https://lists.cs.columbia.edu/mailman/listinfo/kvmarm
PATCH 4/9 KVM: arm64: Support stolen time reporting via shared structure
In reply to Steven PriceImplement the service call for configuring a shared structre between a VCPU and the hypervisor in which the hypervisor can write the time stolen from the VCPU's execution time by other tasks on the host.
The hypervisor allocates memory which is placed at an IPA chosen by user space. The hypervisor then uses WRITE_ONCE() to update the shared structre ensuring single copy atomicity of the 64-bit unsigned value that reports stolen time in nanoseconds.
Whenever stolen time is enabled by the guest, the stolen time counter is reset.
The stolen time itself is retrieved from the sched_info structure maintained by the Linux scheduler code. We enable SCHEDSTATS when selecting KVM Kconfig to ensure this value is meaningful.
--- arch/arm64/include/asm/kvm_host.h | 13 +++++- arch/arm64/kvm/Kconfig | 1 + include/kvm/arm_hypercalls.h | 1 + include/linux/kvm_types.h | 2 + virt/kvm/arm/arm.c | 18 ++++++++ virt/kvm/arm/hypercalls.c | 70 +++++++++++++++++++++++++++++++ 6 files changed, 104 insertions(+), 1 deletion(-)
arch/arm64/include/asm/kvm_host.h +12 -1
@@ -44,6 +44,7 @@ KVM_ARCH_REQ_FLAGS(0, KVM_REQUEST_WAIT | KVM_REQUEST_NO_WAKEUP)#define KVM_REQ_IRQ_PENDING KVM_ARCH_REQ(1)#define KVM_REQ_VCPU_RESET KVM_ARCH_REQ(2) +#define KVM_REQ_RECORD_STEAL KVM_ARCH_REQ(3)DECLARE_STATIC_KEY_FALSE (userspace_irqchip_in_use);
@@ -83,6 +84,11 @@ struct kvm_arch {/* Mandated version of PSCI */ u32 psci_version; + +struct kvm_arch_pvtime { +void *st; +gpa_t st_base; + } pvtime; };#define KVM_NR_MEM_OBJS 40
@@ -338,8 +344,13 @@ struct kvm_vcpu_arch {/* True when deferrable sysregs are loaded on the physical CPU, * see kvm_vcpu_load_sysregs and kvm_vcpu_put_sysregs. */ bool sysregs_loaded_on_cpu; -}; +/* Guest PV state */ +struct { +u64 steal; +u64 last_steal; + } steal; +};/* Pointer to the vcpu's SVE FFR for sve_{save,load}_state() */ #define vcpu_sve_pffr (vcpu) ((void *)((char *)((vcpu)->arch.sve_state) + \ sve_ffr_offset((vcpu)->arch.sve_max_vl)))
arch/arm64/kvm/Kconfig +1 -0
@@ -39,6 +39,7 @@ config KVM select IRQ_BYPASS_MANAGER select HAVE_KVM_IRQ_BYPASS select HAVE_KVM_VCPU_RUN_PID_CHANGE + select SCHEDSTATS ---help--- Support hosting virtualized guest machines. We don't support KVM with 16K page tables yet, due to the multiple
include/kvm/arm_hypercalls.h +1 -0
@@ -7,6 +7,7 @@#include <asm/kvm_emulate.h> int kvm_hvc_call_handler (struct kvm_vcpu *vcpu); +int kvm_update_stolen_time (struct kvm_vcpu *vcpu);static inline u32 smccc_get_function (struct kvm_vcpu *vcpu) {
include/linux/kvm_types.h +2 -0
@@ -35,6 +35,8 @@ typedef unsigned long gva_t;typedef u64 gpa_t ;typedef u64 gfn_t ; +#define GPA_INVALID (~(gpa_t)0) +typedef unsigned long hva_t ;typedef u64 hpa_t ;typedef u64 hfn_t ;
virt/kvm/arm/arm.c +18 -0
@@ -40,6 +40,10 @@#include <asm/kvm_coproc.h> #include <asm/sections.h> +#include <kvm/arm_hypercalls.h> +#include <kvm/arm_pmu.h> +#include <kvm/arm_psci.h> +#ifdef REQUIRES_VIRT __asm__(".arch_extension virt" );#endif
@@ -135,6 +139,7 @@ int kvm_arch_init_vm(struct kvm *kvm, unsigned long type) kvm->arch.max_vcpus = vgic_present ?kvm_vgic_get_max_vcpus () :KVM_MAX_VCPUS ; + kvm->arch.pvtime.st_base =GPA_INVALID ;return ret; out_free_stage2_pgd:kvm_free_stage2_pgd (kvm);
@@ -371,6 +376,7 @@ void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu)kvm_vcpu_load_sysregs (vcpu);kvm_arch_vcpu_load_fp (vcpu);kvm_vcpu_pmu_restore_guest (vcpu); +kvm_make_request (KVM_REQ_RECORD_STEAL , vcpu);if (single_task_running ())vcpu_clear_wfe_traps (vcpu);
@@ -617,6 +623,15 @@ static void vcpu_req_sleep(struct kvm_vcpu *vcpu)smp_rmb (); } +static void vcpu_req_record_steal (struct kvm_vcpu *vcpu) +{ +int idx; + + idx =srcu_read_lock (&vcpu->kvm->srcu); +kvm_update_stolen_time (vcpu); +srcu_read_unlock (&vcpu->kvm->srcu, idx); +} +static int kvm_vcpu_initialized (struct kvm_vcpu *vcpu) {return vcpu->arch.target >=0 ;
@@ -636,6 +651,9 @@ static void check_vcpu_requests(struct kvm_vcpu *vcpu)* that a VCPU sees new virtual interrupts. */ kvm_check_request (KVM_REQ_IRQ_PENDING , vcpu); + +if (kvm_check_request (KVM_REQ_RECORD_STEAL , vcpu)) +vcpu_req_record_steal (vcpu); } }
virt/kvm/arm/hypercalls.c +70 -0
84 lines not on this page · raw message
Signature · 1 line
2.20.1
PATCH 5/9 KVM: Allow kvm_device_ops to be const
In reply to Steven PriceCurrently a kvm_device_ops structure cannot be const without triggering compiler warnings. However the structure doesn't need to be written to and, by marking it const, it can be read-only in memory. Add some more const keywords to allow this.
--- include/linux/kvm_host.h | 4 ++-- virt/kvm/kvm_main.c | 6 +++--- 2 files changed, 5 insertions(+), 5 deletions(-)
include/linux/kvm_host.h +2 -2
@@ -1236,7 +1236,7 @@ extern unsigned int halt_poll_ns_grow_start;extern unsigned int halt_poll_ns_shrink;struct kvm_device { -struct kvm_device_ops *ops; +const struct kvm_device_ops *ops;struct kvm *kvm;void *private;struct list_head vm_node;
@@ -1289,7 +1289,7 @@ struct kvm_device_ops {void kvm_device_get (struct kvm_device *dev);void kvm_device_put (struct kvm_device *dev);struct kvm_device *kvm_device_from_filp (struct file *filp); -int kvm_register_device_ops (struct kvm_device_ops *ops,u32 type); +int kvm_register_device_ops (const struct kvm_device_ops *ops,u32 type);void kvm_unregister_device_ops (u32 type);extern struct kvm_device_ops kvm_mpic_ops;
virt/kvm/kvm_main.c +3 -3
@@ -3035,14 +3035,14 @@ struct kvm_device *kvm_device_from_filp(struct file *filp)return filp->private_data; } -static struct kvm_device_ops *kvm_device_ops_table[KVM_DEV_TYPE_MAX ] = { +static const struct kvm_device_ops *kvm_device_ops_table[KVM_DEV_TYPE_MAX ] = { #ifdef CONFIG_KVM_MPIC [KVM_DEV_TYPE_FSL_MPIC_20 ] = &kvm_mpic_ops, [KVM_DEV_TYPE_FSL_MPIC_42 ] = &kvm_mpic_ops, #endif }; -int kvm_register_device_ops (struct kvm_device_ops *ops,u32 type) +int kvm_register_device_ops (const struct kvm_device_ops *ops,u32 type) {if (type >=ARRAY_SIZE (kvm_device_ops_table))return -ENOSPC ;
@@ -3063,7 +3063,7 @@ void kvm_unregister_device_ops(u32 type)static int kvm_ioctl_create_device (struct kvm *kvm,struct kvm_create_device *cd) { -struct kvm_device_ops *ops =NULL ; +const struct kvm_device_ops *ops =NULL ;struct kvm_device *dev;bool test = cd->flags &KVM_CREATE_DEVICE_TEST ;int type;
Signature · 6 lines
2.20.1 _______________________________________________ kvmarm mailing list [email protected] https://lists.cs.columbia.edu/mailman/listinfo/kvmarm
PATCH 7/9 arm/arm64: Provide a wrapper for SMCCC 1.1 calls
In reply to Steven PriceSMCCC 1.1 calls may use either HVC or SMC depending on the PSCI conduit. Rather than coding this in every call site provide a macro which uses the correct instruction. The macro also handles the case where no PSCI conduit is configured returning a not supported error in res, along with returning the conduit used for the call.
This allow us to remove some duplicated code and will be useful later when adding paravirtualized time hypervisor calls.
--- include/linux/arm-smccc.h | 44 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+)
include/linux/arm-smccc.h +44 -0
@@ -303,6 +303,50 @@ asmlinkage void __arm_smccc_hvc(unsigned long a0, unsigned long a1,#define SMCCC_RET_NOT_SUPPORTED -1#define SMCCC_RET_NOT_REQUIRED -2 +/* Like arm_smccc_1_1* but always returns SMCCC_RET_NOT_SUPPORTED. +* Used when the PSCI conduit is not defined. The empty asm statement +* avoids compiler warnings about unused variables. +*/ +#define __fail_smccc_1_1 (...) \ + do { \ + __declare_args(__count_args(__VA_ARGS__), __VA_ARGS__); \ + asm ("" __constraints(__count_args(__VA_ARGS__))); \ + if (___res) \ + ___res->a0 = SMCCC_RET_NOT_SUPPORTED; \ + } while (0) + +/* +* arm_smccc_1_1_invoke() - make an SMCCC v1.1 compliant call +* +* This is a variadic macro taking one to eight source arguments, and +* an optional return structure. +* +* @a0-a7: arguments passed in registers 0 to 7 +* @res: result values from registers 0 to 3 +* +* This macro will make either an HVC call or an SMC call depending on the +* current PSCI conduit. If no valid conduit is available then -1 +* (SMCCC_RET_NOT_SUPPORTED) is returned in @res.a0 (if supplied). +* +* The return value also provides the conduit that was used. +*/ +#define arm_smccc_1_1_invoke (...) ({ \ + int method = psci_ops.conduit; \ + switch (method) { \ + case PSCI_CONDUIT_HVC: \ + arm_smccc_1_1_hvc(__VA_ARGS__); \ + break; \ + case PSCI_CONDUIT_SMC: \ + arm_smccc_1_1_smc(__VA_ARGS__); \ + break; \ + default: \ + __fail_smccc_1_1(__VA_ARGS__); \ + method = PSCI_CONDUIT_NONE; \ + break; \ + } \ + method; \ + }) +/* Paravirtualised time calls (defined by ARM DEN0057A) */ #define ARM_SMCCC_HV_PV_FEATURES \ ARM_SMCCC_CALL_VAL(ARM_SMCCC_FAST_CALL, \
Signature · 1 line
2.20.1
PATCH 6/9 KVM: arm64: Provide a PV_TIME device to user space
In reply to Steven PriceAllow user space to inform the KVM host where in the physical memory map the paravirtualized time structures should be located.
A device is created which provides the base address of an array of Stolen Time (ST) structures, one for each VCPU. There must be (64 * total number of VCPUs) bytes of memory available at this location.
The address is given in terms of the physical address visible to the guest and must be 64 byte aligned. The memory should be marked as reserved to the guest to stop it allocating it for other purposes.
--- arch/arm64/include/asm/kvm_mmu.h | 2 + arch/arm64/include/uapi/asm/kvm.h | 6 + arch/arm64/kvm/Makefile | 1 + include/uapi/linux/kvm.h | 2 + virt/kvm/arm/mmu.c | 44 +++++++ virt/kvm/arm/pvtime.c | 190 ++++++++++++++++++++++++++++++ 6 files changed, 245 insertions(+) create mode 100644 virt/kvm/arm/pvtime.c
arch/arm64/include/asm/kvm_mmu.h +2 -0
@@ -157,6 +157,8 @@ int kvm_alloc_stage2_pgd(struct kvm *kvm);void kvm_free_stage2_pgd (struct kvm *kvm);int kvm_phys_addr_ioremap (struct kvm *kvm,phys_addr_t guest_ipa,phys_addr_t pa,unsigned long size,bool writable); +int kvm_phys_addr_memremap (struct kvm *kvm,phys_addr_t guest_ipa, +phys_addr_t pa,unsigned long size,bool writable);int kvm_handle_guest_abort (struct kvm_vcpu *vcpu,struct kvm_run *run);
arch/arm64/include/uapi/asm/kvm.h +6 -0
@@ -367,6 +367,12 @@ struct kvm_vcpu_events {#define KVM_PSCI_RET_INVAL PSCI_RET_INVALID_PARAMS#define KVM_PSCI_RET_DENIED PSCI_RET_DENIED +/* Device Control API: PV_TIME */ +#define KVM_DEV_ARM_PV_TIME_PADDR 0 +#define KVM_DEV_ARM_PV_TIME_ST 0 +#define KVM_DEV_ARM_PV_TIME_STATE_SIZE 1 +#define KVM_DEV_ARM_PV_TIME_STATE 2 +#endif #endif /* __ARM_KVM_H__ */
arch/arm64/kvm/Makefile +1 -0
@@ -14,6 +14,7 @@ kvm-$(CONFIG_KVM_ARM_HOST) += $(KVM)/kvm_main.o $(KVM)/coalesced_mmio.o $(KVM)/e kvm-$(CONFIG_KVM_ARM_HOST) += $(KVM)/arm/arm.o $(KVM)/arm/mmu.o $(KVM)/arm/mmio.o kvm-$(CONFIG_KVM_ARM_HOST) += $(KVM)/arm/psci.o $(KVM)/arm/perf.o kvm-$(CONFIG_KVM_ARM_HOST) += $(KVM)/arm/hypercalls.o +kvm-$(CONFIG_KVM_ARM_HOST) += $(KVM)/arm/pvtime.o kvm-$(CONFIG_KVM_ARM_HOST) += inject_fault.o regmap.o va_layout.o kvm-$(CONFIG_KVM_ARM_HOST) += hyp.o hyp-init.o handle_exit.o
include/uapi/linux/kvm.h +2 -0
@@ -1222,6 +1222,8 @@ enum kvm_device_type {#define KVM_DEV_TYPE_ARM_VGIC_ITS KVM_DEV_TYPE_ARM_VGIC_ITS KVM_DEV_TYPE_XIVE,#define KVM_DEV_TYPE_XIVE KVM_DEV_TYPE_XIVE + KVM_DEV_TYPE_ARM_PV_TIME, +#define KVM_DEV_TYPE_ARM_PV_TIME KVM_DEV_TYPE_ARM_PV_TIME KVM_DEV_TYPE_MAX, };
virt/kvm/arm/mmu.c +44 -0
50 lines not on this page · raw message
virt/kvm/arm/pvtime.c new file +190 -0
190 lines not on this page · raw message
Signature · 6 lines
2.20.1 _______________________________________________ kvmarm mailing list [email protected] https://lists.cs.columbia.edu/mailman/listinfo/kvmarm
PATCH 8/9 arm/arm64: Make use of the SMCCC 1.1 wrapper
In reply to Steven PriceRather than directly choosing which function to use based on psci_ops.conduit, use the new arm_smccc_1_1 wrapper instead.
In some cases we still need to do some operations based on the conduit, but the code duplication is removed.
No functional change.
--- arch/arm/mm/proc-v7-bugs.c | 13 +++--- arch/arm64/kernel/cpu_errata.c | 80 ++++++++++++---------------------- 2 files changed, 33 insertions(+), 60 deletions(-)
arch/arm/mm/proc-v7-bugs.c +5 -8
@@ -78,12 +78,13 @@ static void cpu_v7_spectre_init(void)if (psci_ops.smccc_version ==SMCCC_VERSION_1_0 )break ; +arm_smccc_1_1_invoke (ARM_SMCCC_ARCH_FEATURES_FUNC_ID , +ARM_SMCCC_ARCH_WORKAROUND_1 , &res); +if ((int )res.a0 !=0 ) +return ; +switch (psci_ops.conduit) {case PSCI_CONDUIT_HVC : -arm_smccc_1_1_hvc (ARM_SMCCC_ARCH_FEATURES_FUNC_ID , -ARM_SMCCC_ARCH_WORKAROUND_1 , &res); -if ((int )res.a0 !=0 ) -break ;per_cpu (harden_branch_predictor_fn, cpu) = call_hvc_arch_workaround_1; cpu_do_switch_mm = cpu_v7_hvc_switch_mm;
@@ -91,10 +92,6 @@ static void cpu_v7_spectre_init(void)break ;case PSCI_CONDUIT_SMC : -arm_smccc_1_1_smc (ARM_SMCCC_ARCH_FEATURES_FUNC_ID , -ARM_SMCCC_ARCH_WORKAROUND_1 , &res); -if ((int )res.a0 !=0 ) -break ;per_cpu (harden_branch_predictor_fn, cpu) = call_smc_arch_workaround_1; cpu_do_switch_mm = cpu_v7_smc_switch_mm;
arch/arm64/kernel/cpu_errata.c +28 -52
117 lines not on this page · raw message
Signature · 6 lines
2.20.1 _______________________________________________ kvmarm mailing list [email protected] https://lists.cs.columbia.edu/mailman/listinfo/kvmarm
PATCH 9/9 arm64: Retrieve stolen time as paravirtualized guest
In reply to Steven PriceEnable paravirtualization features when running under a hypervisor supporting the PV_TIME_ST hypercall.
For each (v)CPU, we ask the hypervisor for the location of a shared page which the hypervisor will use to report stolen time to us. We set pv_time_ops to the stolen time function which simply reads the stolen value from the shared page for a VCPU. We guarantee single-copy atomicity using READ_ONCE which means we can also read the stolen time for another VCPU than the currently running one while it is potentially being updated by the hypervisor.
--- arch/arm64/kernel/Makefile | 1 + arch/arm64/kernel/kvm.c | 155 +++++++++++++++++++++++++++++++++++++ include/linux/cpuhotplug.h | 1 + 3 files changed, 157 insertions(+) create mode 100644 arch/arm64/kernel/kvm.c
arch/arm64/kernel/Makefile +1 -0
@@ -63,6 +63,7 @@ obj-$(CONFIG_CRASH_CORE) += crash_core.o obj-$(CONFIG_ARM_SDE_INTERFACE) += sdei.o obj-$(CONFIG_ARM64_SSBD) += ssbd.o obj-$(CONFIG_ARM64_PTR_AUTH) += pointer_auth.o +obj-$(CONFIG_PARAVIRT) += kvm.o obj-y += vdso/ probes/ obj-$(CONFIG_COMPAT_VDSO) += vdso32/
arch/arm64/kernel/kvm.c new file +155 -0
155 lines not on this page · raw message
include/linux/cpuhotplug.h +1 -0
@@ -136,6 +136,7 @@ enum cpuhp_state {/* Must be the last timer callback */ CPUHP_AP_DUMMY_TIMER_STARTING ,CPUHP_AP_ARM_XEN_STARTING , +CPUHP_AP_ARM_KVMPV_STARTING ,CPUHP_AP_ARM_CORESIGHT_STARTING ,CPUHP_AP_ARM64_ISNDEP_STARTING , CPUHP_AP_SMPCFD_DYING,
Signature · 6 lines
2.20.1 _______________________________________________ kvmarm mailing list [email protected] https://lists.cs.columbia.edu/mailman/listinfo/kvmarm
Marc Zyngier
maintainerRe: [PATCH 1/9] KVM: arm64: Document PV-time interface
In reply to Steven PriceOn Fri, 2 Aug 2019 15:50:09 +0100
Steven Price <[email protected]> wrote:
[+Peter for the userspace aspect of things]
Hi Steve,
Quoted 26 lines
Introduce a paravirtualization interface for KVM/arm64 based on the "Arm Paravirtualized Time for Arm-Base Systems" specification DEN 0057A.
This only adds the details about "Stolen Time" as the details of "Live Physical Time" have not been fully agreed.
User space can specify a reserved area of memory for the guest and inform KVM to populate the memory with information on time that the host kernel has stolen from the guest.
A hypercall interface is provided for the guest to interrogate the hypervisor's support for this interface and the location of the shared memory structures.
Signed-off-by Steven Price <[email protected]>--- Documentation/virtual/kvm/arm/pvtime.txt | 107 +++++++++++++++++++++++ 1 file changed, 107 insertions(+) create mode 100644 Documentation/virtual/kvm/arm/pvtime.txtDocumentation/virtual/kvm/arm/pvtime.txt new file +0
-0
Documentation/virtual/kvm/arm/pvtime.txt line 5
Quoted 5 lines
Documentation/virtual/kvm/arm/pvtime.txt new file +5
-0@@ -0,0 +1,107 @@ +Paravirtualized time support for arm64 +====================================== + +Arm specification DEN0057/A defined a standard for paravirtualised time +support for Aarch64 guests:
nit: AArch64
Documentation/virtual/kvm/arm/pvtime.txt line 7
Quoted 2 lines
Documentation/virtual/kvm/arm/pvtime.txt new file +2
-0+ +https://developer.arm.com/docs/den0057/a
Between this file and the above document, which one is authoritative?
Documentation/virtual/kvm/arm/pvtime.txt line 9
Quoted 2 lines
Documentation/virtual/kvm/arm/pvtime.txt new file +2
-0+ +KVM/Arm64 implements the stolen time part of this specification by providing
nit: KVM/arm64
Quoted 6 lines
Documentation/virtual/kvm/arm/pvtime.txt new file +6
-0+some hypervisor service calls to support a paravirtualized guest obtaining a +view of the amount of time stolen from its execution. + +Two new SMCCC compatible hypercalls are defined: + +PV_FEATURES 0xC5000020
Documentation/virtual/kvm/arm/pvtime.txt line 25
Quoted 10 lines
Documentation/virtual/kvm/arm/pvtime.txt new file +10
-0+PV_TIME_ST 0xC5000022 + +These are only available in the SMC64/HVC64 calling convention as +paravirtualized time is not available to 32 bit Arm guests. + +PV_FEATURES + Function ID: (uint32) : 0xC5000020 + PV_func_id: (uint32) : Either PV_TIME_LPT or PV_TIME_ST + Return value: (int32) : NOT_SUPPORTED (-1) or SUCCESS (0) if the relevant + PV-time feature is supported by the hypervisor.
How is PV_FEATURES discovered? Is the intention to make it a generic ARM-wide PV discovery mechanism, not specific to PV time?
Documentation/virtual/kvm/arm/pvtime.txt line 32
Quoted 7 lines
Documentation/virtual/kvm/arm/pvtime.txt new file +7
-0+ +PV_TIME_ST + Function ID: (uint32) : 0xC5000022 + Return value: (int64) : IPA of the stolen time data structure for this + (V)CPU. On failure: + NOT_SUPPORTED (-1) +
Is the size implicit? What are the memory attributes? This either needs documenting here, or point to the right bit to the spec.
Quoted 6 lines
Documentation/virtual/kvm/arm/pvtime.txt new file +6
-0+Stolen Time +----------- + +The structure pointed to by the PV_TIME_ST hypercall is as follows: + + Field | Byte Length | Byte Offset | Description
Documentation/virtual/kvm/arm/pvtime.txt line 48
Quoted 10 lines
Documentation/virtual/kvm/arm/pvtime.txt new file +10
-0+ ----------- | ----------- | ----------- | -------------------------- + Revision | 4 | 0 | Must be 0 for version 0.1 + Attributes | 4 | 4 | Must be 0 + Stolen time | 8 | 8 | Stolen time in unsigned + | | | nanoseconds indicating how + | | | much time this VCPU thread + | | | was involuntarily not + | | | running on a physical CPU. + +The structure will be updated by the hypervisor periodically as time is stolen
Is it really periodic? If so, when is the update frequency?
Documentation/virtual/kvm/arm/pvtime.txt line 51
Quoted 3 lines
Documentation/virtual/kvm/arm/pvtime.txt new file +3
-0+from the VCPU. It will be present within a reserved region of the normal +memory given to the guest. The guest should not attempt to write into this +memory. There is a structure by VCPU of the guest.
What if the vcpu writes to it? Does it get a fault? If there is a structure per vcpu, what is the layout in memory? How does a vcpu find its own data structure? Is that the address returned by PV_TIME_ST?
Quoted 6 lines
Documentation/virtual/kvm/arm/pvtime.txt new file +6
-0+ +User space interface +==================== + +User space can request that KVM provide the paravirtualized time interface to +a guest by creating a KVM_DEV_TYPE_ARM_PV_TIME device, for example:
Documentation/virtual/kvm/arm/pvtime.txt line 67
Quoted 10 lines
Documentation/virtual/kvm/arm/pvtime.txt new file +10
-0+ + struct kvm_create_device pvtime_device = { + .type = KVM_DEV_TYPE_ARM_PV_TIME, + .attr = 0, + .flags = 0, + }; + + pvtime_fd = ioctl(vm_fd, KVM_CREATE_DEVICE, &pvtime_device); + +The guest IPA of the structures must be given to KVM. This is the base address
nit: s/guest //
Documentation/virtual/kvm/arm/pvtime.txt line 76
Quoted 9 lines
Documentation/virtual/kvm/arm/pvtime.txt new file +9
-0+of an array of stolen time structures (one for each VCPU). For example: + + struct kvm_device_attr st_base = { + .group = KVM_DEV_ARM_PV_TIME_PADDR, + .attr = KVM_DEV_ARM_PV_TIME_ST, + .addr = (u64)(unsigned long)&st_paddr + }; + + ioctl(pvtime_fd, KVM_SET_DEVICE_ATTR, &st_base);
So the allocation itself is performed by the kernel? What are the ordering requirements between creating vcpus and the device? What are the alignment requirements for the base address?
Documentation/virtual/kvm/arm/pvtime.txt line 81
Quoted 5 lines
Documentation/virtual/kvm/arm/pvtime.txt new file +5
-0+ +For migration (or save/restore) of a guest it is necessary to save the contents +of the shared page(s) and later restore them. KVM_DEV_ARM_PV_TIME_STATE_SIZE +provides the size of this data and KVM_DEV_ARM_PV_TIME_STATE allows the state +to be read/written.
Is the size variable depending on the number of vcpus?
Quoted 16 lines
Documentation/virtual/kvm/arm/pvtime.txt new file +16
-0+ +It is also necessary for the physical address to be set identically when +restoring. + + void *save_state(int fd, u64 attr, u32 *size) { + struct kvm_device_attr get_size = { + .group = KVM_DEV_ARM_PV_TIME_STATE_SIZE, + .attr = attr, + .addr = (u64)(unsigned long)size + }; + + ioctl(fd, KVM_GET_DEVICE_ATTR, get_size); + + void *buffer = malloc(*size); + + struct kvm_device_attr get_state = {
Documentation/virtual/kvm/arm/pvtime.txt line 107
Quoted 10 lines
Documentation/virtual/kvm/arm/pvtime.txt new file +10
-0+ .group = KVM_DEV_ARM_PV_TIME_STATE, + .attr = attr, + .addr = (u64)(unsigned long)size + }; + + ioctl(fd, KVM_GET_DEVICE_ATTR, buffer); + } + + void *st_state = save_state(pvtime_fd, KVM_DEV_ARM_PV_TIME_ST, &st_size); +
Thanks,
M.
Signature · 1 line
Without deviation from the norm, progress is not possible.
Marc Zyngier
maintainerRe: [PATCH 3/9] KVM: arm64: Implement PV_FEATURES call
In reply to Steven PriceQuoted 27 lines · On Fri, 2 Aug 2019 15:50:11 +0100 Steven Price <[email protected]> wrote:
This provides a mechanism for querying which paravirtualized features are available in this hypervisor.
Also add the header file which defines the ABI for the paravirtualized clock features we're about to add.
Signed-off-by Steven Price <[email protected]>--- arch/arm64/include/asm/pvclock-abi.h | 20 ++++++++++++++++++++ include/linux/arm-smccc.h | 14 ++++++++++++++ virt/kvm/arm/hypercalls.c | 9 +++++++++ 3 files changed, 43 insertions(+) create mode 100644 arch/arm64/include/asm/pvclock-abi.harch/arm64/include/asm/pvclock-abi.h new file +7
-0@@ -0,0 +1,20 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* Copyright (C) 2019 Arm Ltd. */ + +#ifndef __ASM_PVCLOCK_ABI_H +#define __ASM_PVCLOCK_ABI_H + +/* The below structures and constants are defined in ARM DEN0057A */
arch/arm64/include/asm/pvclock-abi.h line 17
Quoted 10 lines
arch/arm64/include/asm/pvclock-abi.h new file +10
-0+ +struct pvclock_vcpu_stolen_time_info { +__le32 revision; +__le32 attributes; +__le64 stolen_time; +/* Structure must be 64 byte aligned, pad to that size */ +u8 padding[48 ]; +} __packed; + +#define PV_VM_TIME_NOT_SUPPORTED -1
Isn't the intent for this to be the same value asSMCCC_RET_NOT_SUPPORTED?
arch/arm64/include/asm/pvclock-abi.h line 18
Quoted 1 line
arch/arm64/include/asm/pvclock-abi.h new file +1
-0+#define PV_VM_TIME_INVALID_PARAMETERS -2
It overlaps with SMCCC_RET_NOT_REQUIRED. Is that a problem? Should we consider a spec change for this?
Quoted 50 lines
arch/arm64/include/asm/pvclock-abi.h new file +2
-0+ +#endif include/linux/arm-smccc.h +14
-0@@ -45,6 +45,7 @@#define ARM_SMCCC_OWNER_SIP 2#define ARM_SMCCC_OWNER_OEM 3#define ARM_SMCCC_OWNER_STANDARD 4 +#define ARM_SMCCC_OWNER_STANDARD_HYP 5#define ARM_SMCCC_OWNER_TRUSTED_APP 48#define ARM_SMCCC_OWNER_TRUSTED_APP_END 49#define ARM_SMCCC_OWNER_TRUSTED_OS 50@@ -302,5 +303,18 @@ asmlinkage void __arm_smccc_hvc(unsigned long a0, unsigned long a1,#define SMCCC_RET_NOT_SUPPORTED -1#define SMCCC_RET_NOT_REQUIRED -2 +/* Paravirtualised time calls (defined by ARM DEN0057A) */ +#define ARM_SMCCC_HV_PV_FEATURES \ + ARM_SMCCC_CALL_VAL(ARM_SMCCC_FAST_CALL, \ + ARM_SMCCC_SMC_64, \ + ARM_SMCCC_OWNER_STANDARD_HYP, \ + 0x20) + +#define ARM_SMCCC_HV_PV_TIME_ST \ + ARM_SMCCC_CALL_VAL(ARM_SMCCC_FAST_CALL, \ + ARM_SMCCC_SMC_64, \ + ARM_SMCCC_OWNER_STANDARD_HYP, \ + 0x22) +#endif /*__ASSEMBLY__*/ #endif /*__LINUX_ARM_SMCCC_H*/ virt/kvm/arm/hypercalls.c +2
-0@@ -5,6 +5,7 @@#include <linux/kvm_host.h> #include <asm/kvm_emulate.h> +#include <asm/pvclock-abi.h> #include <kvm/arm_hypercalls.h> #include <kvm/arm_psci.h> @@ -48,6 +49,14 @@ int kvm_hvc_call_handler(struct kvm_vcpu *vcpu)break ; }break ; +case ARM_SMCCC_HV_PV_FEATURES :
virt/kvm/arm/hypercalls.c int kvm_hvc_call_handler(struct kvm_vcpu *vcpu) · line 62
Quoted 10 lines
virt/kvm/arm/hypercalls.c +7
-0+ val =SMCCC_RET_SUCCESS ; +break ; + } +break ; +case ARM_SMCCC_HV_PV_FEATURES : + feature =smccc_get_arg1 (vcpu); +switch (feature) { }break ;default :
Thanks,
M.
Signature · 1 line
Without deviation from the norm, progress is not possible.
Marc Zyngier
maintainerRe: [PATCH 4/9] KVM: arm64: Support stolen time reporting via shared structure
In reply to Steven PriceQuoted 1 line · On Fri, 2 Aug 2019 15:50:12 +0100 Steven Price <[email protected]> wrote:
Implement the service call for configuring a shared structre between a
structure
Quoted 6 lines
VCPU and the hypervisor in which the hypervisor can write the time stolen from the VCPU's execution time by other tasks on the host.
The hypervisor allocates memory which is placed at an IPA chosen by user space. The hypervisor then uses
WRITE_ONCE()to update the shared structre ensuring single copy atomicity of the 64-bit unsigned value
structure
Quoted 32 lines
that reports stolen time in nanoseconds.
Whenever stolen time is enabled by the guest, the stolen time counter is reset.
The stolen time itself is retrieved from the
sched_infostructure maintained by the Linux scheduler code. We enable SCHEDSTATS when selecting KVMKconfigto ensure this value is meaningful.Signed-off-by Steven Price <[email protected]>--- arch/arm64/include/asm/kvm_host.h | 13 +++++- arch/arm64/kvm/Kconfig | 1 + include/kvm/arm_hypercalls.h | 1 + include/linux/kvm_types.h | 2 + virt/kvm/arm/arm.c | 18 ++++++++ virt/kvm/arm/hypercalls.c | 70 +++++++++++++++++++++++++++++++ 6 files changed, 104 insertions(+), 1 deletion(-)arch/arm64/include/asm/kvm_host.h +1
-0@@ -44,6 +44,7 @@ KVM_ARCH_REQ_FLAGS(0, KVM_REQUEST_WAIT | KVM_REQUEST_NO_WAKEUP)#define KVM_REQ_IRQ_PENDING KVM_ARCH_REQ(1)#define KVM_REQ_VCPU_RESET KVM_ARCH_REQ(2) +#define KVM_REQ_RECORD_STEAL KVM_ARCH_REQ(3)DECLARE_STATIC_KEY_FALSE (userspace_irqchip_in_use);
arch/arm64/include/asm/kvm_host.h struct kvm_arch { · line 89
Quoted 6 lines
arch/arm64/include/asm/kvm_host.h +3
-0@@ -83,6 +84,11 @@ struct kvm_arch {/* Mandated version of PSCI */ u32 psci_version; + +struct kvm_arch_pvtime { +void *st;
Is it really a void *? I'm sure you can use a proper type here...
Quoted 119 lines
arch/arm64/include/asm/kvm_host.h +8
-1+gpa_t st_base; + } pvtime; };#define KVM_NR_MEM_OBJS 40@@ -338,8 +344,13 @@ struct kvm_vcpu_arch {/* True when deferrable sysregs are loaded on the physical CPU, * see kvm_vcpu_load_sysregs and kvm_vcpu_put_sysregs. */ bool sysregs_loaded_on_cpu; -}; +/* Guest PV state */ +struct { +u64 steal; +u64 last_steal; + } steal; +};/* Pointer to the vcpu's SVE FFR for sve_{save,load}_state() */ #define vcpu_sve_pffr (vcpu) ((void *)((char *)((vcpu)->arch.sve_state) + \ sve_ffr_offset((vcpu)->arch.sve_max_vl)))arch/arm64/kvm/Kconfig +1
-0@@ -39,6 +39,7 @@ config KVM select IRQ_BYPASS_MANAGER select HAVE_KVM_IRQ_BYPASS select HAVE_KVM_VCPU_RUN_PID_CHANGE + select SCHEDSTATS ---help--- Support hosting virtualized guest machines. We don't support KVM with 16K page tables yet, due to the multipleinclude/kvm/arm_hypercalls.h +1
-0@@ -7,6 +7,7 @@#include <asm/kvm_emulate.h> int kvm_hvc_call_handler (struct kvm_vcpu *vcpu); +int kvm_update_stolen_time (struct kvm_vcpu *vcpu);static inline u32 smccc_get_function (struct kvm_vcpu *vcpu) {include/linux/kvm_types.h +2
-0@@ -35,6 +35,8 @@ typedef unsigned long gva_t;typedef u64 gpa_t ;typedef u64 gfn_t ; +#define GPA_INVALID (~(gpa_t)0) +typedef unsigned long hva_t ;typedef u64 hpa_t ;typedef u64 hfn_t ;virt/kvm/arm/arm.c +18
-0@@ -40,6 +40,10 @@#include <asm/kvm_coproc.h> #include <asm/sections.h> +#include <kvm/arm_hypercalls.h> +#include <kvm/arm_pmu.h> +#include <kvm/arm_psci.h> +#ifdef REQUIRES_VIRT __asm__(".arch_extension virt" );#endif @@ -135,6 +139,7 @@ int kvm_arch_init_vm(struct kvm *kvm, unsigned long type) kvm->arch.max_vcpus = vgic_present ?kvm_vgic_get_max_vcpus () :KVM_MAX_VCPUS ; + kvm->arch.pvtime.st_base =GPA_INVALID ;return ret; out_free_stage2_pgd:kvm_free_stage2_pgd (kvm);@@ -371,6 +376,7 @@ void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu)kvm_vcpu_load_sysregs (vcpu);kvm_arch_vcpu_load_fp (vcpu);kvm_vcpu_pmu_restore_guest (vcpu); +kvm_make_request (KVM_REQ_RECORD_STEAL , vcpu);if (single_task_running ())vcpu_clear_wfe_traps (vcpu);@@ -617,6 +623,15 @@ static void vcpu_req_sleep(struct kvm_vcpu *vcpu)smp_rmb (); } +static void vcpu_req_record_steal (struct kvm_vcpu *vcpu) +{ +int idx; + + idx =srcu_read_lock (&vcpu->kvm->srcu); +kvm_update_stolen_time (vcpu); +srcu_read_unlock (&vcpu->kvm->srcu, idx); +} +static int kvm_vcpu_initialized (struct kvm_vcpu *vcpu) {return vcpu->arch.target >=0 ;@@ -636,6 +651,9 @@ static void check_vcpu_requests(struct kvm_vcpu *vcpu)* that a VCPU sees new virtual interrupts. */ kvm_check_request (KVM_REQ_IRQ_PENDING , vcpu); + +if (kvm_check_request (KVM_REQ_RECORD_STEAL , vcpu)) +vcpu_req_record_steal (vcpu); } }virt/kvm/arm/hypercalls.c +0
-0
virt/kvm/arm/hypercalls.c line 15
Quoted 6 lines
virt/kvm/arm/hypercalls.c +3
-0@@ -10,6 +10,70 @@#include <kvm/arm_hypercalls.h> #include <kvm/arm_psci.h> + +static struct pvclock_vcpu_stolen_time_info *pvtime_get_st ( +struct kvm_vcpu *vcpu)
nit: on a single line.
Quoted 6 lines
virt/kvm/arm/hypercalls.c +6
-0+{ +struct pvclock_vcpu_stolen_time_info *st = vcpu->kvm->arch.pvtime.st; + +if (!st) +return NULL ; +
virt/kvm/arm/hypercalls.c line 31
Quoted 10 lines
virt/kvm/arm/hypercalls.c +10
-0+return &st[kvm_vcpu_get_idx (vcpu)]; +} + +int kvm_update_stolen_time (struct kvm_vcpu *vcpu) +{ +u64 steal; +struct pvclock_vcpu_stolen_time_info *kaddr; + +if (vcpu->kvm->arch.pvtime.st_base ==GPA_INVALID ) +return -ENOTSUPP ;
So for a guest that doesn't have stolen time support (which is 100% of them for the foreseeable future), we still set a request, take the srcu lock and end-up here for nothing. I'd rather we test this st_base early, as it should never change once the guest has started.
virt/kvm/arm/hypercalls.c line 36
Quoted 5 lines
virt/kvm/arm/hypercalls.c +5
-0+ + kaddr =pvtime_get_st (vcpu); + +if (!kaddr) +return -ENOTSUPP ;
How can this happen?
virt/kvm/arm/hypercalls.c line 39
Quoted 3 lines
virt/kvm/arm/hypercalls.c +3
-0+ + kaddr->revision =0 ; + kaddr->attributes =0 ;
Why does this need to be written each time we update the stolen time? I have the feeling this would be better moved to the hypercall initializing the data structure.
virt/kvm/arm/hypercalls.c line 48
Quoted 9 lines
virt/kvm/arm/hypercalls.c +9
-0+ +/* Let's do the local bookkeeping */ + steal = vcpu->arch.steal.steal; + steal += current->sched_info.run_delay - vcpu->arch.steal.last_steal; + vcpu->arch.steal.last_steal = current->sched_info.run_delay; + vcpu->arch.steal.steal = steal; + +/* Now write out the value to the shared page */ +WRITE_ONCE (kaddr->stolen_time,cpu_to_le64 (steal));
Is there any requirement for this to be visible to another CPU than the one this is being written from?
Quoted 36 lines
virt/kvm/arm/hypercalls.c +29
-0+ +return 0 ; +} + +static int kvm_hypercall_stolen_time (struct kvm_vcpu *vcpu) +{ +u64 ret; +int err; + +/* +* Start counting stolen time from the time the guest requests +* the feature enabled. +*/ + vcpu->arch.steal.steal =0 ; + vcpu->arch.steal.last_steal = current->sched_info.run_delay; + + err =kvm_update_stolen_time (vcpu); + +if (err) + ret =SMCCC_RET_NOT_SUPPORTED ; +else + ret = vcpu->kvm->arch.pvtime.st_base + + (sizeof (struct pvclock_vcpu_stolen_time_info ) * +kvm_vcpu_get_idx (vcpu)); + +smccc_set_retval (vcpu, ret,0 ,0 ,0 ); +return 1 ; +}int kvm_hvc_call_handler (struct kvm_vcpu *vcpu) {u32 func_id =smccc_get_function (vcpu);@@ -57,8 +121,14 @@ int kvm_hvc_call_handler(struct kvm_vcpu *vcpu)case ARM_SMCCC_HV_PV_FEATURES : feature =smccc_get_arg1 (vcpu);switch (feature) { +case ARM_SMCCC_HV_PV_FEATURES :
virt/kvm/arm/hypercalls.c int kvm_hvc_call_handler(struct kvm_vcpu *vcpu) · line 134
Quoted 10 lines
virt/kvm/arm/hypercalls.c +5
-0+case ARM_SMCCC_HV_PV_TIME_ST : + val =SMCCC_RET_SUCCESS ; +break ; }break ; +case ARM_SMCCC_HV_PV_TIME_ST : +return kvm_hypercall_stolen_time (vcpu);default :return kvm_psci_call (vcpu); }
Thanks,
M.
Signature · 1 line
Without deviation from the norm, progress is not possible.
Marc Zyngier
maintainerRe: [PATCH 6/9] KVM: arm64: Provide a PV_TIME device to user space
In reply to Steven PriceQuoted 10 lines · On Fri, 2 Aug 2019 15:50:14 +0100 Steven Price <[email protected]> wrote:
Allow user space to inform the KVM host where in the physical memory map the paravirtualized time structures should be located.
A device is created which provides the base address of an array of Stolen Time (ST) structures, one for each VCPU. There must be (64 * total number of VCPUs) bytes of memory available at this location.
The address is given in terms of the physical address visible to the guest and must be 64 byte aligned. The memory should be marked as reserved to the guest to stop it allocating it for other purposes.
Why? You seem to be allocating the memory from the kernel, so as far as the guest is concerned, this isn't generally usable memory.
Quoted 108 lines
Signed-off-by Steven Price <[email protected]>--- arch/arm64/include/asm/kvm_mmu.h | 2 + arch/arm64/include/uapi/asm/kvm.h | 6 + arch/arm64/kvm/Makefile | 1 + include/uapi/linux/kvm.h | 2 + virt/kvm/arm/mmu.c | 44 +++++++ virt/kvm/arm/pvtime.c | 190 ++++++++++++++++++++++++++++++ 6 files changed, 245 insertions(+) create mode 100644 virt/kvm/arm/pvtime.carch/arm64/include/asm/kvm_mmu.h +2
-0@@ -157,6 +157,8 @@ int kvm_alloc_stage2_pgd(struct kvm *kvm);void kvm_free_stage2_pgd (struct kvm *kvm);int kvm_phys_addr_ioremap (struct kvm *kvm,phys_addr_t guest_ipa,phys_addr_t pa,unsigned long size,bool writable); +int kvm_phys_addr_memremap (struct kvm *kvm,phys_addr_t guest_ipa, +phys_addr_t pa,unsigned long size,bool writable);int kvm_handle_guest_abort (struct kvm_vcpu *vcpu,struct kvm_run *run);arch/arm64/include/uapi/asm/kvm.h +6
-0@@ -367,6 +367,12 @@ struct kvm_vcpu_events {#define KVM_PSCI_RET_INVAL PSCI_RET_INVALID_PARAMS#define KVM_PSCI_RET_DENIED PSCI_RET_DENIED +/* Device Control API: PV_TIME */ +#define KVM_DEV_ARM_PV_TIME_PADDR 0 +#define KVM_DEV_ARM_PV_TIME_ST 0 +#define KVM_DEV_ARM_PV_TIME_STATE_SIZE 1 +#define KVM_DEV_ARM_PV_TIME_STATE 2 +#endif #endif /* __ARM_KVM_H__ */ arch/arm64/kvm/Makefile +1
-0@@ -14,6 +14,7 @@ kvm-$(CONFIG_KVM_ARM_HOST) += $(KVM)/kvm_main.o $(KVM)/coalesced_mmio.o $(KVM)/e kvm-$(CONFIG_KVM_ARM_HOST) += $(KVM)/arm/arm.o $(KVM)/arm/mmu.o $(KVM)/arm/mmio.o kvm-$(CONFIG_KVM_ARM_HOST) += $(KVM)/arm/psci.o $(KVM)/arm/perf.o kvm-$(CONFIG_KVM_ARM_HOST) += $(KVM)/arm/hypercalls.o +kvm-$(CONFIG_KVM_ARM_HOST) += $(KVM)/arm/pvtime.o kvm-$(CONFIG_KVM_ARM_HOST) += inject_fault.o regmap.o va_layout.o kvm-$(CONFIG_KVM_ARM_HOST) += hyp.o hyp-init.o handle_exit.oinclude/uapi/linux/kvm.h +2
-0@@ -1222,6 +1222,8 @@ enum kvm_device_type {#define KVM_DEV_TYPE_ARM_VGIC_ITS KVM_DEV_TYPE_ARM_VGIC_ITS KVM_DEV_TYPE_XIVE,#define KVM_DEV_TYPE_XIVE KVM_DEV_TYPE_XIVE + KVM_DEV_TYPE_ARM_PV_TIME, +#define KVM_DEV_TYPE_ARM_PV_TIME KVM_DEV_TYPE_ARM_PV_TIME KVM_DEV_TYPE_MAX, };virt/kvm/arm/mmu.c +33
-0@@ -1368,6 +1368,50 @@ int kvm_phys_addr_ioremap(struct kvm *kvm, phys_addr_t guest_ipa,return ret; } +/** +* kvm_phys_addr_memremap - map a memory range to guest IPA +* +* @kvm: The KVM pointer +* @guest_ipa: The IPA at which to insert the mapping +* @pa: The physical address of the memory +* @size: The size of the mapping +*/ +int kvm_phys_addr_memremap (struct kvm *kvm,phys_addr_t guest_ipa, +phys_addr_t pa,unsigned long size,bool writable) +{ +phys_addr_t addr, end; +int ret =0 ; +unsigned long pfn; +struct kvm_mmu_memory_cache cache = {0 , }; + + end = (guest_ipa + size +PAGE_SIZE -1 ) &PAGE_MASK ; + pfn =__phys_to_pfn (pa); + +for (addr = guest_ipa; addr < end; addr +=PAGE_SIZE ) { +pte_t pte =pfn_pte (pfn,PAGE_S2 ); + +if (writable) + pte =kvm_s2pte_mkwrite (pte); + + ret =mmu_topup_memory_cache (&cache, +kvm_mmu_cache_min_pages (kvm), +KVM_NR_MEM_OBJS ); +if (ret) + goto out; +spin_lock (&kvm->mmu_lock); + ret =stage2_set_pte (kvm, &cache, addr, &pte,0 ); +spin_unlock (&kvm->mmu_lock);
virt/kvm/arm/mmu.c int kvm_phys_addr_ioremap(struct kvm *kvm, phys_addr_t guest_ipa, · line 1413
Quoted 10 lines
virt/kvm/arm/mmu.c +10
-0+if (ret) + goto out; + + pfn++; + } + +out: +mmu_free_memory_cache (&cache); +return ret; +}
This is an exact copy of kvm_phys_addr_ioremap(), with only the memory attributes changing. Surely we can have a shared implementation that takes the memory attribute as a parameter.
Quoted 10 lines
virt/kvm/arm/mmu.c +1
-0+static bool transparent_hugepage_adjust (kvm_pfn_t *pfnp,phys_addr_t *ipap) {kvm_pfn_t pfn = *pfnp;virt/kvm/arm/pvtime.c new file +0
-0
virt/kvm/arm/pvtime.c line 8
Quoted 8 lines
virt/kvm/arm/pvtime.c new file +8
-0@@ -0,0 +1,190 @@ +// SPDX-License-Identifier: GPL-2.0 +// Copyright (C) 2019 Arm Ltd. + +#include <linux/kvm_host.h> +#include <asm/kvm_mmu.h> + +/* We currently only support PV time on ARM64 */ +#ifdef CONFIG_ARM64
And we're only compiling it on arm64, so why the #ifdef?
virt/kvm/arm/pvtime.c line 14
Quoted 6 lines
virt/kvm/arm/pvtime.c new file +6
-0+ +#include <asm/pvclock-abi.h> + +static int max_stolen_size (void ) +{ +int size =KVM_MAX_VCPUS *sizeof (struct pvclock_vcpu_stolen_time_info );
So we're always allocating enough memory for 512 CPUs? That's an additional 32kB of contiguous memory...
virt/kvm/arm/pvtime.c line 26
Quoted 12 lines
virt/kvm/arm/pvtime.c new file +12
-0+ +return ALIGN (size,PAGE_SIZE ); +} + +static int kvm_arm_pvtime_create (struct kvm_device *dev,u32 type) +{ +struct kvm_arch_pvtime *pvtime = &dev->kvm->arch.pvtime; + + pvtime->st =alloc_pages_exact (max_stolen_size (), +GFP_KERNEL | __GFP_ZERO); +if (!pvtime->st) +return -ENOMEM ;
Is there any chance we could use a vmalloc allocation instead? This would lift the requirement on having physically contiguous memory.
Quoted 66 lines
virt/kvm/arm/pvtime.c new file +66
-0+ +return 0 ; +} + +static void kvm_arm_pvtime_destroy (struct kvm_device *dev) +{ +struct kvm_arch_pvtime *pvtime = &dev->kvm->arch.pvtime; + + pvtime->st_base =GPA_INVALID ; +free_pages_exact (pvtime->st,max_stolen_size ()); +kfree (dev); +} + +static int pvtime_map_pages (struct kvm *kvm,gpa_t guest_paddr, +void *kaddr,int size) +{ +return kvm_phys_addr_memremap (kvm, guest_paddr, +virt_to_phys (kaddr), + size, false); +} + +static int pvtime_save_state (struct kvm *kvm,u64 type,void __user *user) +{ +void *source; +size_t size; + +switch (type) { +case KVM_DEV_ARM_PV_TIME_ST : + source = kvm->arch.pvtime.st; + size =sizeof (struct pvclock_vcpu_stolen_time_info ) * +atomic_read (&kvm->online_vcpus); +break ; +default : +return -ENXIO ; + } + +if (copy_to_user (user, source, size)) +return -EFAULT ; +return 0 ; +} + +static int pvtime_restore_state (struct kvm *kvm,u64 type,void __user *user) +{ +void *dest; +size_t size; + +switch (type) { +case KVM_DEV_ARM_PV_TIME_ST : + dest = kvm->arch.pvtime.st; + size =sizeof (struct pvclock_vcpu_stolen_time_info ) * +atomic_read (&kvm->online_vcpus); +break ; +default : +return -ENXIO ; + } + +if (copy_from_user (dest, user, size)) +return -EFAULT ; + +return 0 ; +} + +static int kvm_arm_pvtime_set_attr (struct kvm_device *dev, +struct kvm_device_attr *attr) +{ +struct kvm_arch_pvtime *pvtime = &dev->kvm->arch.pvtime;
virt/kvm/arm/pvtime.c line 102
Quoted 10 lines
virt/kvm/arm/pvtime.c new file +10
-0+u64 __user *user = (u64 __user *)attr->addr; +u64 paddr; +int ret; + +switch (attr->group) { +case KVM_DEV_ARM_PV_TIME_PADDR : +if (get_user (paddr, user)) +return -EFAULT ; +if (paddr &63 ) +return -EINVAL ;
You should check whether the device fits into the IPA space for this guest, and whether it overlaps with anything else.
virt/kvm/arm/pvtime.c line 108
Quoted 6 lines
virt/kvm/arm/pvtime.c new file +6
-0+switch (attr->attr) { +case KVM_DEV_ARM_PV_TIME_ST : +if (pvtime->st_base !=GPA_INVALID ) +return -EEXIST ; + ret =pvtime_map_pages (dev->kvm, paddr, pvtime->st, +max_stolen_size ());
Consider moving the size directly into pvtime_map_pages(), and dropping the pvtime->st parameter. All you need is kvm and paddr.
Quoted 72 lines
virt/kvm/arm/pvtime.c new file +72
-0+if (ret) +return ret; + pvtime->st_base = paddr; +return 0 ; + } +break ; +case KVM_DEV_ARM_PV_TIME_STATE_SIZE : +return -EPERM ; +case KVM_DEV_ARM_PV_TIME_STATE : +return pvtime_restore_state (dev->kvm, attr->attr, user); + } +return -ENXIO ; +} + +static int kvm_arm_pvtime_get_attr (struct kvm_device *dev, +struct kvm_device_attr *attr) +{ +u64 __user *user = (u64 __user *)attr->addr; +u32 size; + +switch (attr->group) { +case KVM_DEV_ARM_PV_TIME_PADDR : +switch (attr->attr) { +case KVM_DEV_ARM_PV_TIME_ST : +if (put_user (dev->kvm->arch.pvtime.st_base, user)) +return -EFAULT ; +return 0 ; + } +break ; +case KVM_DEV_ARM_PV_TIME_STATE_SIZE : +switch (attr->attr) { +case KVM_DEV_ARM_PV_TIME_ST : + size =sizeof (struct pvclock_vcpu_stolen_time_info ); + size *=atomic_read (&dev->kvm->online_vcpus); +break ; +default : +return -ENXIO ; + } +if (put_user (size, user)) +return -EFAULT ; +return 0 ; +case KVM_DEV_ARM_PV_TIME_STATE : +return pvtime_save_state (dev->kvm, attr->attr, user); + } +return -ENXIO ; +} + +static int kvm_arm_pvtime_has_attr (struct kvm_device *dev, +struct kvm_device_attr *attr) +{ +switch (attr->group) { +case KVM_DEV_ARM_PV_TIME_PADDR : +case KVM_DEV_ARM_PV_TIME_STATE_SIZE : +case KVM_DEV_ARM_PV_TIME_STATE : +switch (attr->attr) { +case KVM_DEV_ARM_PV_TIME_ST : +return 0 ; + } +break ; + } +return -ENXIO ; +} + +static const struct kvm_device_ops pvtime_ops = { +"Arm PV time" , + .create = kvm_arm_pvtime_create, + .destroy = kvm_arm_pvtime_destroy, + .set_attr = kvm_arm_pvtime_set_attr, + .get_attr = kvm_arm_pvtime_get_attr, + .has_attr = kvm_arm_pvtime_has_attr +}; +
virt/kvm/arm/pvtime.c line 190
Quoted 10 lines
virt/kvm/arm/pvtime.c new file +10
-0+static int __init kvm_pvtime_init (void ) +{ +kvm_register_device_ops (&pvtime_ops,KVM_DEV_TYPE_ARM_PV_TIME ); + +return 0 ; +} + +late_initcall (kvm_pvtime_init); + +#endif
Thanks,
M.
Signature · 1 line
Without deviation from the norm, progress is not possible.
Marc Zyngier
maintainerRe: [PATCH 6/9] KVM: arm64: Provide a PV_TIME device to user space
On Sat, 3 Aug 2019 13:51:13 +0100
Marc Zyngier <[email protected]> wrote:
[forgot that one]
Quoted 2 lines
On Fri, 2 Aug 2019 15:50:14 +0100
Steven Price <[email protected]> wrote:
[...]
Quoted 8 lines
virt/kvm/arm/pvtime.c line 188
Quoted 8 lines
virt/kvm/arm/pvtime.c new file +8
-0+static int __init kvm_pvtime_init (void ) +{ +kvm_register_device_ops (&pvtime_ops,KVM_DEV_TYPE_ARM_PV_TIME ); + +return 0 ; +} + +late_initcall (kvm_pvtime_init);
Why is it an initcall? So far, the only initcall we've used is the one that initializes KVM itself. Can't we just the device_ops just like we do for the vgic?
M.
Signature · 1 line
Without deviation from the norm, progress is not possible.
Marc Zyngier
maintainerRe: [PATCH 4/9] KVM: arm64: Support stolen time reporting via shared structure
In reply to Steven PriceQuoted 150 lines · On Fri, 2 Aug 2019 15:50:12 +0100 Steven Price <[email protected]> wrote:
Implement the service call for configuring a shared structre between a VCPU and the hypervisor in which the hypervisor can write the time stolen from the VCPU's execution time by other tasks on the host.
The hypervisor allocates memory which is placed at an IPA chosen by user space. The hypervisor then uses
WRITE_ONCE()to update the shared structre ensuring single copy atomicity of the 64-bit unsigned value that reports stolen time in nanoseconds.Whenever stolen time is enabled by the guest, the stolen time counter is reset.
The stolen time itself is retrieved from the
sched_infostructure maintained by the Linux scheduler code. We enable SCHEDSTATS when selecting KVMKconfigto ensure this value is meaningful.Signed-off-by Steven Price <[email protected]>--- arch/arm64/include/asm/kvm_host.h | 13 +++++- arch/arm64/kvm/Kconfig | 1 + include/kvm/arm_hypercalls.h | 1 + include/linux/kvm_types.h | 2 + virt/kvm/arm/arm.c | 18 ++++++++ virt/kvm/arm/hypercalls.c | 70 +++++++++++++++++++++++++++++++ 6 files changed, 104 insertions(+), 1 deletion(-)arch/arm64/include/asm/kvm_host.h +12
-1@@ -44,6 +44,7 @@ KVM_ARCH_REQ_FLAGS(0, KVM_REQUEST_WAIT | KVM_REQUEST_NO_WAKEUP)#define KVM_REQ_IRQ_PENDING KVM_ARCH_REQ(1)#define KVM_REQ_VCPU_RESET KVM_ARCH_REQ(2) +#define KVM_REQ_RECORD_STEAL KVM_ARCH_REQ(3)DECLARE_STATIC_KEY_FALSE (userspace_irqchip_in_use);@@ -83,6 +84,11 @@ struct kvm_arch {/* Mandated version of PSCI */ u32 psci_version; + +struct kvm_arch_pvtime { +void *st; +gpa_t st_base; + } pvtime; };#define KVM_NR_MEM_OBJS 40@@ -338,8 +344,13 @@ struct kvm_vcpu_arch {/* True when deferrable sysregs are loaded on the physical CPU, * see kvm_vcpu_load_sysregs and kvm_vcpu_put_sysregs. */ bool sysregs_loaded_on_cpu; -}; +/* Guest PV state */ +struct { +u64 steal; +u64 last_steal; + } steal; +};/* Pointer to the vcpu's SVE FFR for sve_{save,load}_state() */ #define vcpu_sve_pffr (vcpu) ((void *)((char *)((vcpu)->arch.sve_state) + \ sve_ffr_offset((vcpu)->arch.sve_max_vl)))arch/arm64/kvm/Kconfig +1
-0@@ -39,6 +39,7 @@ config KVM select IRQ_BYPASS_MANAGER select HAVE_KVM_IRQ_BYPASS select HAVE_KVM_VCPU_RUN_PID_CHANGE + select SCHEDSTATS ---help--- Support hosting virtualized guest machines. We don't support KVM with 16K page tables yet, due to the multipleinclude/kvm/arm_hypercalls.h +1
-0@@ -7,6 +7,7 @@#include <asm/kvm_emulate.h> int kvm_hvc_call_handler (struct kvm_vcpu *vcpu); +int kvm_update_stolen_time (struct kvm_vcpu *vcpu);static inline u32 smccc_get_function (struct kvm_vcpu *vcpu) {include/linux/kvm_types.h +2
-0@@ -35,6 +35,8 @@ typedef unsigned long gva_t;typedef u64 gpa_t ;typedef u64 gfn_t ; +#define GPA_INVALID (~(gpa_t)0) +typedef unsigned long hva_t ;typedef u64 hpa_t ;typedef u64 hfn_t ;virt/kvm/arm/arm.c +15
-0@@ -40,6 +40,10 @@#include <asm/kvm_coproc.h> #include <asm/sections.h> +#include <kvm/arm_hypercalls.h> +#include <kvm/arm_pmu.h> +#include <kvm/arm_psci.h> +#ifdef REQUIRES_VIRT __asm__(".arch_extension virt" );#endif @@ -135,6 +139,7 @@ int kvm_arch_init_vm(struct kvm *kvm, unsigned long type) kvm->arch.max_vcpus = vgic_present ?kvm_vgic_get_max_vcpus () :KVM_MAX_VCPUS ; + kvm->arch.pvtime.st_base =GPA_INVALID ;return ret; out_free_stage2_pgd:kvm_free_stage2_pgd (kvm);@@ -371,6 +376,7 @@ void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu)kvm_vcpu_load_sysregs (vcpu);kvm_arch_vcpu_load_fp (vcpu);kvm_vcpu_pmu_restore_guest (vcpu); +kvm_make_request (KVM_REQ_RECORD_STEAL , vcpu);if (single_task_running ())vcpu_clear_wfe_traps (vcpu);@@ -617,6 +623,15 @@ static void vcpu_req_sleep(struct kvm_vcpu *vcpu)smp_rmb (); } +static void vcpu_req_record_steal (struct kvm_vcpu *vcpu) +{ +int idx; + + idx =srcu_read_lock (&vcpu->kvm->srcu); +kvm_update_stolen_time (vcpu); +srcu_read_unlock (&vcpu->kvm->srcu, idx); +} +static int kvm_vcpu_initialized (struct kvm_vcpu *vcpu) {return vcpu->arch.target >=0 ;
virt/kvm/arm/arm.c static void check_vcpu_requests(struct kvm_vcpu *vcpu) · line 656
Quoted 6 lines
virt/kvm/arm/arm.c +3
-0@@ -636,6 +651,9 @@ static void check_vcpu_requests(struct kvm_vcpu *vcpu)* that a VCPU sees new virtual interrupts. */ kvm_check_request (KVM_REQ_IRQ_PENDING , vcpu); + +if (kvm_check_request (KVM_REQ_RECORD_STEAL , vcpu)) +vcpu_req_record_steal (vcpu);
Something troubles me. Here, you've set the request on load. But you can be preempted at any time (preemption gets disabled just after).
I have the feeling that should you get preempted right here, you'll end-up having accumulated the wrong amount of steal time, as the request put via load when you'll get scheduled back in will only get processed after a full round of entry/exit/entry, which doesn't look great.
Am I getting it right?
M.
Signature · 1 line
Without deviation from the norm, progress is not possible.
Marc Zyngier
maintainerOn Fri, 2 Aug 2019 15:50:08 +0100
Steven Price <[email protected]> wrote:
Hi Steven,
Quoted 5 lines
This series add support for paravirtualized time for arm64 guests and KVM hosts following the specification in Arm's document DEN 0057A:
Quoted 7 lines
It implements support for stolen time, allowing the guest to identify time when it is forcibly not executing.
It doesn't implement support for Live Physical Time (LPT) as there are some concerns about the overheads and approach in the above specification, and I expect an updated version of the specification to be released soon with just the stolen time parts.
Thanks for posting this.
My current concern with this series is around the fact that we allocate memory from the kernel on behalf of the guest. It is the first example of such thing in the ARM port, and I can't really say I'm fond of it.
x86 seems to get away with it by having the memory allocated from userspace, why I tend to like more. Yes, put_user is more expensive than a straight store, but this isn't done too often either.
What is the rational for your current approach?
Thanks,
M.
Signature · 1 line
Without deviation from the norm, progress is not possible.
Marc Zyngier
maintainerRe: [PATCH 4/9] KVM: arm64: Support stolen time reporting via shared structure
In reply to Marc ZyngierQuoted 163 lines · On Sat, 3 Aug 2019 18:58:17 +0100 Marc Zyngier <[email protected]> wrote:
Quoted 150 lines · On Fri, 2 Aug 2019 15:50:12 +0100 Steven Price <[email protected]> wrote:
Implement the service call for configuring a shared structre between a VCPU and the hypervisor in which the hypervisor can write the time stolen from the VCPU's execution time by other tasks on the host.
The hypervisor allocates memory which is placed at an IPA chosen by user space. The hypervisor then uses
WRITE_ONCE()to update the shared structre ensuring single copy atomicity of the 64-bit unsigned value that reports stolen time in nanoseconds.Whenever stolen time is enabled by the guest, the stolen time counter is reset.
The stolen time itself is retrieved from the
sched_infostructure maintained by the Linux scheduler code. We enable SCHEDSTATS when selecting KVMKconfigto ensure this value is meaningful.Signed-off-by Steven Price <[email protected]>--- arch/arm64/include/asm/kvm_host.h | 13 +++++- arch/arm64/kvm/Kconfig | 1 + include/kvm/arm_hypercalls.h | 1 + include/linux/kvm_types.h | 2 + virt/kvm/arm/arm.c | 18 ++++++++ virt/kvm/arm/hypercalls.c | 70 +++++++++++++++++++++++++++++++ 6 files changed, 104 insertions(+), 1 deletion(-)arch/arm64/include/asm/kvm_host.h +12
-1@@ -44,6 +44,7 @@ KVM_ARCH_REQ_FLAGS(0, KVM_REQUEST_WAIT | KVM_REQUEST_NO_WAKEUP)#define KVM_REQ_IRQ_PENDING KVM_ARCH_REQ(1)#define KVM_REQ_VCPU_RESET KVM_ARCH_REQ(2) +#define KVM_REQ_RECORD_STEAL KVM_ARCH_REQ(3)DECLARE_STATIC_KEY_FALSE (userspace_irqchip_in_use);@@ -83,6 +84,11 @@ struct kvm_arch {/* Mandated version of PSCI */ u32 psci_version; + +struct kvm_arch_pvtime { +void *st; +gpa_t st_base; + } pvtime; };#define KVM_NR_MEM_OBJS 40@@ -338,8 +344,13 @@ struct kvm_vcpu_arch {/* True when deferrable sysregs are loaded on the physical CPU, * see kvm_vcpu_load_sysregs and kvm_vcpu_put_sysregs. */ bool sysregs_loaded_on_cpu; -}; +/* Guest PV state */ +struct { +u64 steal; +u64 last_steal; + } steal; +};/* Pointer to the vcpu's SVE FFR for sve_{save,load}_state() */ #define vcpu_sve_pffr (vcpu) ((void *)((char *)((vcpu)->arch.sve_state) + \ sve_ffr_offset((vcpu)->arch.sve_max_vl)))arch/arm64/kvm/Kconfig +1
-0@@ -39,6 +39,7 @@ config KVM select IRQ_BYPASS_MANAGER select HAVE_KVM_IRQ_BYPASS select HAVE_KVM_VCPU_RUN_PID_CHANGE + select SCHEDSTATS ---help--- Support hosting virtualized guest machines. We don't support KVM with 16K page tables yet, due to the multipleinclude/kvm/arm_hypercalls.h +1
-0@@ -7,6 +7,7 @@#include <asm/kvm_emulate.h> int kvm_hvc_call_handler (struct kvm_vcpu *vcpu); +int kvm_update_stolen_time (struct kvm_vcpu *vcpu);static inline u32 smccc_get_function (struct kvm_vcpu *vcpu) {include/linux/kvm_types.h +2
-0@@ -35,6 +35,8 @@ typedef unsigned long gva_t;typedef u64 gpa_t ;typedef u64 gfn_t ; +#define GPA_INVALID (~(gpa_t)0) +typedef unsigned long hva_t ;typedef u64 hpa_t ;typedef u64 hfn_t ;virt/kvm/arm/arm.c +15
-0@@ -40,6 +40,10 @@#include <asm/kvm_coproc.h> #include <asm/sections.h> +#include <kvm/arm_hypercalls.h> +#include <kvm/arm_pmu.h> +#include <kvm/arm_psci.h> +#ifdef REQUIRES_VIRT __asm__(".arch_extension virt" );#endif @@ -135,6 +139,7 @@ int kvm_arch_init_vm(struct kvm *kvm, unsigned long type) kvm->arch.max_vcpus = vgic_present ?kvm_vgic_get_max_vcpus () :KVM_MAX_VCPUS ; + kvm->arch.pvtime.st_base =GPA_INVALID ;return ret; out_free_stage2_pgd:kvm_free_stage2_pgd (kvm);@@ -371,6 +376,7 @@ void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu)kvm_vcpu_load_sysregs (vcpu);kvm_arch_vcpu_load_fp (vcpu);kvm_vcpu_pmu_restore_guest (vcpu); +kvm_make_request (KVM_REQ_RECORD_STEAL , vcpu);if (single_task_running ())vcpu_clear_wfe_traps (vcpu);@@ -617,6 +623,15 @@ static void vcpu_req_sleep(struct kvm_vcpu *vcpu)smp_rmb (); } +static void vcpu_req_record_steal (struct kvm_vcpu *vcpu) +{ +int idx; + + idx =srcu_read_lock (&vcpu->kvm->srcu); +kvm_update_stolen_time (vcpu); +srcu_read_unlock (&vcpu->kvm->srcu, idx); +} +static int kvm_vcpu_initialized (struct kvm_vcpu *vcpu) {return vcpu->arch.target >=0 ;virt/kvm/arm/arm.c static void check_vcpu_requests(struct kvm_vcpu *vcpu) · line 656
Quoted 6 lines
virt/kvm/arm/arm.c +3
-0@@ -636,6 +651,9 @@ static void check_vcpu_requests(struct kvm_vcpu *vcpu)* that a VCPU sees new virtual interrupts. */ kvm_check_request (KVM_REQ_IRQ_PENDING , vcpu); + +if (kvm_check_request (KVM_REQ_RECORD_STEAL , vcpu)) +vcpu_req_record_steal (vcpu);Something troubles me. Here, you've set the request on load. But you can be preempted at any time (preemption gets disabled just after).
Quoted 5 lines
I have the feeling that should you get preempted right here, you'll end-up having accumulated the wrong amount of steal time, as the request put via load when you'll get scheduled back in will only get processed after a full round of entry/exit/entry, which doesn't look great.
Ah, no. We're saved by the check for pending requests right before we jump in the guest, causing an early exit and the whole shebang to be restarted.
M.
Signature · 1 line
Without deviation from the norm, progress is not possible.
Marc Zyngier
maintainerRe: [PATCH 9/9] arm64: Retrieve stolen time as paravirtualized guest
In reply to Steven PriceQuoted 11 lines · On Fri, 2 Aug 2019 15:50:17 +0100 Steven Price <[email protected]> wrote:
Enable paravirtualization features when running under a hypervisor supporting the
PV_TIME_SThypercall.For each (v)CPU, we ask the hypervisor for the location of a shared page which the hypervisor will use to report stolen time to us. We set
pv_time_opsto the stolen time function which simply reads the stolen value from the shared page for a VCPU. We guarantee single-copy atomicity usingREAD_ONCEwhich means we can also read the stolen time for another VCPU than the currently running one while it is potentially being updated by the hypervisor.
Quoted 4 lines
Signed-off-by Steven Price <[email protected]>--- arch/arm64/kernel/Makefile | 1 + arch/arm64/kernel/kvm.c | 155 +++++++++++++++++++++++++++++++++++++
nit: Why not using paravirt.c, which clearly states what it does? The alternative would be to name it kvm-pv.c.
Quoted 86 lines
include/linux/cpuhotplug.h | 1 + 3 files changed, 157 insertions(+) create mode 100644 arch/arm64/kernel/kvm.carch/arm64/kernel/Makefile +1
-0@@ -63,6 +63,7 @@ obj-$(CONFIG_CRASH_CORE) += crash_core.o obj-$(CONFIG_ARM_SDE_INTERFACE) += sdei.o obj-$(CONFIG_ARM64_SSBD) += ssbd.o obj-$(CONFIG_ARM64_PTR_AUTH) += pointer_auth.o +obj-$(CONFIG_PARAVIRT) += kvm.o obj-y += vdso/ probes/ obj-$(CONFIG_COMPAT_VDSO) += vdso32/arch/arm64/kernel/kvm.c new file +64
-0@@ -0,0 +1,155 @@ +// SPDX-License-Identifier: GPL-2.0 +// Copyright (C) 2019 Arm Ltd. + +#define pr_fmt (fmt) "kvmarm-pv: " fmt + +#include <linux/arm-smccc.h> +#include <linux/cpuhotplug.h> +#include <linux/io.h> +#include <linux/printk.h> +#include <linux/psci.h> +#include <linux/reboot.h> +#include <linux/slab.h> + +#include <asm/paravirt.h> +#include <asm/pvclock-abi.h> +#include <asm/smp_plat.h> + +struct kvmarm_stolen_time_region { +struct pvclock_vcpu_stolen_time_info *kaddr; +}; + +static DEFINE_PER_CPU(struct kvmarm_stolen_time_region, stolen_time_region); + +static bool steal_acc = true; +static int __init parse_no_stealacc (char *arg) +{ + steal_acc = false; +return 0 ; +} +early_param ("no-steal-acc" , parse_no_stealacc); + +/* return stolen time in ns by asking the hypervisor */ +static u64 kvm_steal_clock (int cpu) +{ +struct kvmarm_stolen_time_region *reg; + + reg =per_cpu_ptr (&stolen_time_region, cpu); +if (!reg->kaddr) { +pr_warn_once ("stolen time enabled but not configured for cpu %d\n" , + cpu); +return 0 ; + } + +return le64_to_cpu (READ_ONCE (reg->kaddr->stolen_time)); +} + +static int disable_stolen_time_current_cpu (void ) +{ +struct kvmarm_stolen_time_region *reg; + + reg =this_cpu_ptr (&stolen_time_region); +if (!reg->kaddr) +return 0 ; + +memunmap (reg->kaddr); +memset (reg,0 ,sizeof (*reg)); + +return 0 ; +} + +static int stolen_time_dying_cpu (unsigned int cpu) +{ +return disable_stolen_time_current_cpu (); +}
arch/arm64/kernel/kvm.c line 74
Quoted 10 lines
arch/arm64/kernel/kvm.c new file +10
-0+ +static int init_stolen_time_cpu (unsigned int cpu) +{ +struct kvmarm_stolen_time_region *reg; +struct arm_smccc_res res; + + reg =this_cpu_ptr (&stolen_time_region); + +if (reg->kaddr) +return 0 ;
Can this actually happen? It'd take two CPU_UP calls from the HP notifiers to get in that situation...
arch/arm64/kernel/kvm.c line 87
Quoted 13 lines
arch/arm64/kernel/kvm.c new file +13
-0+ +arm_smccc_1_1_invoke (ARM_SMCCC_HV_PV_TIME_ST , &res); + +if ((long )res.a0 <0 ) +return -EINVAL ; + + reg->kaddr =memremap (res.a0, +sizeof (struct pvclock_vcpu_stolen_time_info ), +MEMREMAP_WB ); + +if (reg->kaddr ==NULL ) { +pr_warn ("Failed to map stolen time data structure\n" ); +return -EINVAL ;
-ENOMEM is the expected return code.
Quoted 58 lines
arch/arm64/kernel/kvm.c new file +58
-0+ } + +if (le32_to_cpu (reg->kaddr->revision) !=0 || +le32_to_cpu (reg->kaddr->attributes) !=0 ) { +pr_warn ("Unexpected revision or attributes in stolen time data\n" ); +return -ENXIO ; + } + +return 0 ; +} + +static int kvm_arm_init_stolen_time (void ) +{ +int ret; + + ret =cpuhp_setup_state (CPUHP_AP_ARM_KVMPV_STARTING , +"hypervisor/kvmarm/pv:starting" , + init_stolen_time_cpu, stolen_time_dying_cpu); +if (ret <0 ) +return ret; +return 0 ; +} + +static bool has_kvm_steal_clock (void ) +{ +struct arm_smccc_res res; + +/* To detect the presence of PV time support we require SMCCC 1.1+ */ +if (psci_ops.smccc_version <SMCCC_VERSION_1_1 ) +return false; + +arm_smccc_1_1_invoke (ARM_SMCCC_ARCH_FEATURES_FUNC_ID , +ARM_SMCCC_HV_PV_FEATURES , &res); + +if (res.a0 !=SMCCC_RET_SUCCESS ) +return false; + +arm_smccc_1_1_invoke (ARM_SMCCC_HV_PV_FEATURES , +ARM_SMCCC_HV_PV_TIME_ST , &res); + +if (res.a0 !=SMCCC_RET_SUCCESS ) +return false; + +return true; +} + +static int __init kvm_guest_init (void ) +{ +int ret =0 ; + +if (!has_kvm_steal_clock ()) +return 0 ; + + ret =kvm_arm_init_stolen_time (); +if (ret) +return ret; + + pv_ops.time.steal_clock = kvm_steal_clock;
arch/arm64/kernel/kvm.c line 155
Quoted 10 lines
arch/arm64/kernel/kvm.c new file +10
-0+ +static_key_slow_inc (¶virt_steal_enabled); +if (steal_acc) +static_key_slow_inc (¶virt_steal_rq_enabled); + +pr_info ("using stolen time PV\n" ); + +return 0 ; +} +early_initcall (kvm_guest_init);
Is there any reason why we wouldn't directly call into this rather than using an initcall?
include/linux/cpuhotplug.h enum cpuhp_state { · line 142
Quoted 12 lines
include/linux/cpuhotplug.h +1
-0@@ -136,6 +136,7 @@ enum cpuhp_state {/* Must be the last timer callback */ CPUHP_AP_DUMMY_TIMER_STARTING ,CPUHP_AP_ARM_XEN_STARTING , +CPUHP_AP_ARM_KVMPV_STARTING ,CPUHP_AP_ARM_CORESIGHT_STARTING ,CPUHP_AP_ARM64_ISNDEP_STARTING , CPUHP_AP_SMPCFD_DYING,
Thanks,
M.
Signature · 1 line
Without deviation from the norm, progress is not possible.
Zenghui Yu
reviewerRe: [PATCH 1/9] KVM: arm64: Document PV-time interface
In reply to Steven PriceHi Steven,
Quoted 14 lines · Steven Price
Introduce a paravirtualization interface for KVM/arm64 based on the "Arm Paravirtualized Time for Arm-Base Systems" specification DEN 0057A.
This only adds the details about "Stolen Time" as the details of "Live Physical Time" have not been fully agreed.
User space can specify a reserved area of memory for the guest and inform KVM to populate the memory with information on time that the host kernel has stolen from the guest.
A hypercall interface is provided for the guest to interrogate the hypervisor's support for this interface and the location of the shared memory structures.
Quoted 5 lines
Signed-off-by Steven Price <[email protected]>--- Documentation/virtual/kvm/arm/pvtime.txt | 107 +++++++++++++++++++++++ 1 file changed, 107 insertions(+)create mode 100644
Documentation/virtual/kvm/arm/pvtime.txt
^^^^^^^
This directory has been renamed recently, see:
https://patchwork.ozlabs.org/patch/1136104/
Zenghui
Quoted 115 lines
Documentation/virtual/kvm/arm/pvtime.txt new file +107
-0@@ -0,0 +1,107 @@ +Paravirtualized time support for arm64 +====================================== + +Arm specification DEN0057/A defined a standard for paravirtualised time +support for Aarch64 guests: + +https://developer.arm.com/docs/den0057/a + +KVM/Arm64 implements the stolen time part of this specification by providing +some hypervisor service calls to support a paravirtualized guest obtaining a +view of the amount of time stolen from its execution. + +Two new SMCCC compatible hypercalls are defined: + +PV_FEATURES 0xC5000020 +PV_TIME_ST 0xC5000022 + +These are only available in the SMC64/HVC64 calling convention as +paravirtualized time is not available to 32 bit Arm guests. + +PV_FEATURES + Function ID: (uint32) : 0xC5000020 + PV_func_id: (uint32) : Either PV_TIME_LPT or PV_TIME_ST + Return value: (int32) : NOT_SUPPORTED (-1) or SUCCESS (0) if the relevant + PV-time feature is supported by the hypervisor. + +PV_TIME_ST + Function ID: (uint32) : 0xC5000022 + Return value: (int64) : IPA of the stolen time data structure for this + (V)CPU. On failure: + NOT_SUPPORTED (-1) + +Stolen Time +----------- + +The structure pointed to by the PV_TIME_ST hypercall is as follows: + + Field | Byte Length | Byte Offset | Description + ----------- | ----------- | ----------- | -------------------------- + Revision | 4 | 0 | Must be 0 for version 0.1 + Attributes | 4 | 4 | Must be 0 + Stolen time | 8 | 8 | Stolen time in unsigned + | | | nanoseconds indicating how + | | | much time this VCPU thread + | | | was involuntarily not + | | | running on a physical CPU. + +The structure will be updated by the hypervisor periodically as time is stolen +from the VCPU. It will be present within a reserved region of the normal +memory given to the guest. The guest should not attempt to write into this +memory. There is a structure by VCPU of the guest. + +User space interface +==================== + +User space can request that KVM provide the paravirtualized time interface to +a guest by creating a KVM_DEV_TYPE_ARM_PV_TIME device, for example: + + struct kvm_create_device pvtime_device = { + .type = KVM_DEV_TYPE_ARM_PV_TIME, + .attr = 0, + .flags = 0, + }; + + pvtime_fd = ioctl(vm_fd, KVM_CREATE_DEVICE, &pvtime_device); + +The guest IPA of the structures must be given to KVM. This is the base address +of an array of stolen time structures (one for each VCPU). For example: + + struct kvm_device_attr st_base = { + .group = KVM_DEV_ARM_PV_TIME_PADDR, + .attr = KVM_DEV_ARM_PV_TIME_ST, + .addr = (u64)(unsigned long)&st_paddr + }; + + ioctl(pvtime_fd, KVM_SET_DEVICE_ATTR, &st_base); + +For migration (or save/restore) of a guest it is necessary to save the contents +of the shared page(s) and later restore them. KVM_DEV_ARM_PV_TIME_STATE_SIZE +provides the size of this data and KVM_DEV_ARM_PV_TIME_STATE allows the state +to be read/written. + +It is also necessary for the physical address to be set identically when +restoring. + + void *save_state(int fd, u64 attr, u32 *size) { + struct kvm_device_attr get_size = { + .group = KVM_DEV_ARM_PV_TIME_STATE_SIZE, + .attr = attr, + .addr = (u64)(unsigned long)size + }; + + ioctl(fd, KVM_GET_DEVICE_ATTR, get_size); + + void *buffer = malloc(*size); + + struct kvm_device_attr get_state = { + .group = KVM_DEV_ARM_PV_TIME_STATE, + .attr = attr, + .addr = (u64)(unsigned long)size + }; + + ioctl(fd, KVM_GET_DEVICE_ATTR, buffer); + } + + void *st_state = save_state(pvtime_fd, KVM_DEV_ARM_PV_TIME_ST, &st_size); +
Re: [PATCH 7/9] arm/arm64: Provide a wrapper for SMCCC 1.1 calls
In reply to Steven PriceQuoted 6 lines · Steven Price
SMCCC 1.1 calls may use either HVC or SMC depending on the PSCI conduit. Rather than coding this in every call site provide a macro which uses the correct instruction. The macro also handles the case where no PSCI conduit is configured returning a not supported error in res, along with returning the conduit used for the call.
Quoted 7 lines
This allow us to remove some duplicated code and will be useful later when adding paravirtualized time hypervisor calls.
Signed-off-by Steven Price <[email protected]>--- include/linux/arm-smccc.h | 44 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+)
Will
Re: [PATCH 1/9] KVM: arm64: Document PV-time interface
In reply to Zenghui YuQuoted 22 lines · Zenghui Yu
Hi Steven,
Quoted 14 lines · Steven Price
Introduce a paravirtualization interface for KVM/arm64 based on the "Arm Paravirtualized Time for Arm-Base Systems" specification DEN 0057A.
This only adds the details about "Stolen Time" as the details of "Live Physical Time" have not been fully agreed.
User space can specify a reserved area of memory for the guest and inform KVM to populate the memory with information on time that the host kernel has stolen from the guest.
A hypercall interface is provided for the guest to interrogate the hypervisor's support for this interface and the location of the shared memory structures.
Quoted 5 lines
Signed-off-by Steven Price <[email protected]>---
Documentation/virtual/kvm/arm/pvtime.txt| 107 +++++++++++++++++++++++1 file changed, 107 insertions(+)create mode 100644
Documentation/virtual/kvm/arm/pvtime.txt
Quoted 4 lines
^^^^^^^
This directory has been renamed recently, see:
Thanks for pointing that out - I'll move it in the next version.
Steve
_______________________________________________
linux-arm-kernel mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel
Re: [PATCH 1/9] KVM: arm64: Document PV-time interface
In reply to Marc ZyngierQuoted 41 lines · Marc Zyngier
On Fri, 2 Aug 2019 15:50:09 +0100
Steven Price <[email protected]> wrote:[+Peter for the userspace aspect of things]
Hi Steve,
Quoted 26 lines
Introduce a paravirtualization interface for KVM/arm64 based on the "Arm Paravirtualized Time for Arm-Base Systems" specification DEN 0057A.
This only adds the details about "Stolen Time" as the details of "Live Physical Time" have not been fully agreed.
User space can specify a reserved area of memory for the guest and inform KVM to populate the memory with information on time that the host kernel has stolen from the guest.
A hypercall interface is provided for the guest to interrogate the hypervisor's support for this interface and the location of the shared memory structures.
Signed-off-by Steven Price <[email protected]>--- Documentation/virtual/kvm/arm/pvtime.txt | 107 +++++++++++++++++++++++ 1 file changed, 107 insertions(+) create mode 100644 Documentation/virtual/kvm/arm/pvtime.txtDocumentation/virtual/kvm/arm/pvtime.txt new file +0
-0Documentation/virtual/kvm/arm/pvtime.txt line 5
Quoted 5 lines
Documentation/virtual/kvm/arm/pvtime.txt new file +5
-0@@ -0,0 +1,107 @@ +Paravirtualized time support for arm64 +====================================== + +Arm specification DEN0057/A defined a standard for paravirtualised time +support for Aarch64 guests:nit: AArch64
Quoted 4 lines
Documentation/virtual/kvm/arm/pvtime.txt line 7
Quoted 2 lines
Documentation/virtual/kvm/arm/pvtime.txt new file +2
-0+ +https://developer.arm.com/docs/den0057/aBetween this file and the above document, which one is authoritative?
The above document should be authoritative - although I'm still waiting for the final version to be published. I'm not expecting any changes to the stolen time part though.
Quoted 16 lines
Documentation/virtual/kvm/arm/pvtime.txt line 9
Quoted 2 lines
Documentation/virtual/kvm/arm/pvtime.txt new file +2
-0+ +KVM/Arm64 implements the stolen time part of this specification by providingnit: KVM/arm64
Quoted 11 lines
Documentation/virtual/kvm/arm/pvtime.txt new file +11
-0+some hypervisor service calls to support a paravirtualized guest obtaining a +view of the amount of time stolen from its execution. + +Two new SMCCC compatible hypercalls are defined: + +PV_FEATURES 0xC5000020 +PV_TIME_ST 0xC5000022 + +These are only available in the SMC64/HVC64 calling convention as +paravirtualized time is not available to 32 bit Arm guests. +
Quoted 8 lines
Documentation/virtual/kvm/arm/pvtime.txt line 25
Quoted 5 lines
Documentation/virtual/kvm/arm/pvtime.txt new file +5
-0+PV_FEATURES + Function ID: (uint32) : 0xC5000020 + PV_func_id: (uint32) : Either PV_TIME_LPT or PV_TIME_ST + Return value: (int32) : NOT_SUPPORTED (-1) or SUCCESS (0) if the relevant + PV-time feature is supported by the hypervisor.How is
PV_FEATURESdiscovered? Is the intention to make it a generic ARM-wide PV discovery mechanism, not specific to PV time?
SMCCC is mandated for PV time. So, assuming the hypervisor supports SMCCC, the "NOT_SUPPORTED" return is mandated by SMCCC if PV time isn't supported.
However, we do also use the SMCCC_ARCH_FEATURES mechanism to check the existence of PV_FEATURES before use. I'll update the document to call this out.
Quoted 10 lines
Documentation/virtual/kvm/arm/pvtime.txt line 32
Quoted 7 lines
Documentation/virtual/kvm/arm/pvtime.txt new file +7
-0+ +PV_TIME_ST + Function ID: (uint32) : 0xC5000022 + Return value: (int64) : IPA of the stolen time data structure for this + (V)CPU. On failure: + NOT_SUPPORTED (-1) +Is the size implicit? What are the memory attributes? This either needs documenting here, or point to the right bit to the spec.
The size is implicit - it's a pointer to the below structure, so the guest can only rely on the first 16 bytes being valid. The memory attributes are described in the specification as:
"The calling guest can map the IPA into normal memory with inner and outer write back caching attributes, in the inner sharable domain"
I'll put those details in this document for completeness.
Quoted 10 lines
Quoted 10 lines
Documentation/virtual/kvm/arm/pvtime.txt new file +10
-0+Stolen Time +----------- + +The structure pointed to by the PV_TIME_ST hypercall is as follows: + + Field | Byte Length | Byte Offset | Description + ----------- | ----------- | ----------- | -------------------------- + Revision | 4 | 0 | Must be 0 for version 0.1 + Attributes | 4 | 4 | Must be 0 + Stolen time | 8 | 8 | Stolen time in unsigned
Quoted 8 lines
Documentation/virtual/kvm/arm/pvtime.txt line 48
Quoted 6 lines
Documentation/virtual/kvm/arm/pvtime.txt new file +6
-0+ | | | nanoseconds indicating how + | | | much time this VCPU thread + | | | was involuntarily not + | | | running on a physical CPU. + +The structure will be updated by the hypervisor periodically as time is stolenIs it really periodic? If so, when is the update frequency?
Hmm, periodic might be the wrong term - there is no guaranteed frequency of update. The spec says:
"The hypervisor must update this value prior to scheduling a virtual CPU"
I guess that's probably the best description.
Quoted 5 lines
Documentation/virtual/kvm/arm/pvtime.txt line 51
Quoted 3 lines
Documentation/virtual/kvm/arm/pvtime.txt new file +3
-0+from the VCPU. It will be present within a reserved region of the normal +memory given to the guest. The guest should not attempt to write into this +memory. There is a structure by VCPU of the guest.What if the vcpu writes to it? Does it get a fault?
From the perspective from the specification this is undefined. A fault would therefore be acceptable but isn't generated in the implementation defined here.
Quoted 3 lines
If there is a
structure per vcpu, what is the layout in memory? How does a vcpu find its own data structure? Is that the address returned byPV_TIME_ST?
A call to PV_TIME_ST returns the structure for the calling vCPU - I'll make that explicit. The layout is therefore defined by the hypervisor and cannot be relied on by the guest. As below this implementation uses a simple array of structures.
Quoted 19 lines
Quoted 6 lines
Documentation/virtual/kvm/arm/pvtime.txt new file +6
-0+ +User space interface +==================== + +User space can request that KVM provide the paravirtualized time interface to +a guest by creating a KVM_DEV_TYPE_ARM_PV_TIME device, for example:Documentation/virtual/kvm/arm/pvtime.txt line 67
Quoted 10 lines
Documentation/virtual/kvm/arm/pvtime.txt new file +10
-0+ + struct kvm_create_device pvtime_device = { + .type = KVM_DEV_TYPE_ARM_PV_TIME, + .attr = 0, + .flags = 0, + }; + + pvtime_fd = ioctl(vm_fd, KVM_CREATE_DEVICE, &pvtime_device); + +The guest IPA of the structures must be given to KVM. This is the base addressnit: s/guest //
Quoted 13 lines
Documentation/virtual/kvm/arm/pvtime.txt line 76
Quoted 9 lines
Documentation/virtual/kvm/arm/pvtime.txt new file +9
-0+of an array of stolen time structures (one for each VCPU). For example: + + struct kvm_device_attr st_base = { + .group = KVM_DEV_ARM_PV_TIME_PADDR, + .attr = KVM_DEV_ARM_PV_TIME_ST, + .addr = (u64)(unsigned long)&st_paddr + }; + + ioctl(pvtime_fd, KVM_SET_DEVICE_ATTR, &st_base);So the allocation itself is performed by the kernel? What are the ordering requirements between creating vcpus and the device? What are the alignment requirements for the base address?
The base address should be page aligned - I'll spell that out.
There are currently no ordering requirements between creating vcpus and the device. However...
Quoted 7 lines
Documentation/virtual/kvm/arm/pvtime.txt line 81
Quoted 5 lines
Documentation/virtual/kvm/arm/pvtime.txt new file +5
-0+ +For migration (or save/restore) of a guest it is necessary to save the contents +of the shared page(s) and later restore them. KVM_DEV_ARM_PV_TIME_STATE_SIZE +provides the size of this data and KVM_DEV_ARM_PV_TIME_STATE allows the state +to be read/written.Is the size variable depending on the number of vcpus?
...yes - so restoring the state after migration must be done after creating the vcpus. I'll point out that the device should created after.
Thanks for the review,
Steve
_______________________________________________
kvmarm mailing list
[email protected]
https://lists.cs.columbia.edu/mailman/listinfo/kvmarm
Quoted 24 lines · Marc Zyngier
On Fri, 2 Aug 2019 15:50:08 +0100
Steven Price <[email protected]> wrote:Hi Steven,
Quoted 5 lines
This series add support for paravirtualized time for arm64 guests and KVM hosts following the specification in Arm's document DEN 0057A:
Quoted 7 lines
It implements support for stolen time, allowing the guest to identify time when it is forcibly not executing.
It doesn't implement support for Live Physical Time (LPT) as there are some concerns about the overheads and approach in the above specification, and I expect an updated version of the specification to be released soon with just the stolen time parts.
Thanks for posting this.
My current concern with this series is around the fact that we allocate memory from the kernel on behalf of the guest. It is the first example of such thing in the ARM port, and I can't really say I'm fond of it.
Quoted 5 lines
x86 seems to get away with it by having the memory allocated from userspace, why I tend to like more. Yes,
put_useris more expensive than a straight store, but this isn't done too often either.What is the rational for your current approach?
As I see it there are 3 approaches that can be taken here:
1. Hypervisor allocates memory and adds it to the virtual machine. This means that everything to do with the 'device' is encapsulated behind the KVM_CREATE_DEVICE / KVM_[GS]ET_DEVICE_ATTR ioctls. But since we want the stolen time structure to be fast it cannot be a trapping region and has to be backed by real memory - in this case allocated by the host kernel.
2. Host user space allocates memory. Similar to above, but this time user space needs to manage the memory region as well as the usual KVM_CREATE_DEVICE dance. I've no objection to this, but it means kvmtool/QEMU needs to be much more aware of what is going on (e.g. how to size the memory region).
3. Guest kernel "donates" the memory to the hypervisor for the structure. As far as I'm aware this is what x86 does. The problems I see this approach are:
a) kexec becomes much more tricky - there needs to be a disabling
mechanism for the guest to stop the hypervisor scribbling on memory before starting the new kernel.
b) If there is more than one entity that is interested in the
information (e.g. firmware and kernel) then this requires some form of arbitration in the guest because the hypervisor doesn't want to have to track an arbitrary number of regions to update.
c) Performance can suffer if the host kernel doesn't have a suitably
aligned/sized area to use. As you say - put_user() is more expensive. The structure is updated on every return to the VM.
Of course x86 does prove the third approach can work, but I'm not sure which is actually better. Avoid the kexec cancellation requirements was the main driver of the current approach. Although many of the conversations about this were also tied up with Live Physical Time which adds its own complications.
Steve
Re: [PATCH 3/9] KVM: arm64: Implement PV_FEATURES call
In reply to Marc ZyngierQuoted 35 lines · Marc Zyngier
Quoted 32 lines · On Fri, 2 Aug 2019 15:50:11 +0100 Steven Price <[email protected]> wrote:
This provides a mechanism for querying which paravirtualized features are available in this hypervisor.
Also add the header file which defines the ABI for the paravirtualized clock features we're about to add.
Signed-off-by Steven Price <[email protected]>--- arch/arm64/include/asm/pvclock-abi.h | 20 ++++++++++++++++++++ include/linux/arm-smccc.h | 14 ++++++++++++++ virt/kvm/arm/hypercalls.c | 9 +++++++++ 3 files changed, 43 insertions(+) create mode 100644 arch/arm64/include/asm/pvclock-abi.harch/arm64/include/asm/pvclock-abi.h new file +12
-0@@ -0,0 +1,20 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* Copyright (C) 2019 Arm Ltd. */ + +#ifndef __ASM_PVCLOCK_ABI_H +#define __ASM_PVCLOCK_ABI_H + +/* The below structures and constants are defined in ARM DEN0057A */ + +struct pvclock_vcpu_stolen_time_info { + __le32 revision; + __le32 attributes; + __le64 stolen_time;
Quoted 8 lines
arch/arm64/include/asm/pvclock-abi.h line 17
Quoted 5 lines
arch/arm64/include/asm/pvclock-abi.h new file +5
-0+/* Structure must be 64 byte aligned, pad to that size */ +u8 padding[48 ]; +} __packed; + +#define PV_VM_TIME_NOT_SUPPORTED -1Isn't the intent for this to be the same value as
SMCCC_RET_NOT_SUPPORTED?
Yes it is, I guess there's not much point defining it again.
Quoted 4 lines
arch/arm64/include/asm/pvclock-abi.h line 18
Quoted 1 line
arch/arm64/include/asm/pvclock-abi.h new file +1
-0+#define PV_VM_TIME_INVALID_PARAMETERS -2It overlaps with
SMCCC_RET_NOT_REQUIRED. Is that a problem? Should we consider a spec change for this?
Actually INVALID_PARAMETERS is only for Live Physical Time, since we're not implementing it here, this can go as well.
Thanks,
Steve
_______________________________________________
kvmarm mailing list
[email protected]
https://lists.cs.columbia.edu/mailman/listinfo/kvmarm
Marc Zyngier
maintainerQuoted 63 lines · Steven Price
Quoted 24 lines · Marc Zyngier
On Fri, 2 Aug 2019 15:50:08 +0100
Steven Price <[email protected]> wrote:Hi Steven,
Quoted 5 lines
This series add support for paravirtualized time for arm64 guests and KVM hosts following the specification in Arm's document DEN 0057A:
Quoted 7 lines
It implements support for stolen time, allowing the guest to identify time when it is forcibly not executing.
It doesn't implement support for Live Physical Time (LPT) as there are some concerns about the overheads and approach in the above specification, and I expect an updated version of the specification to be released soon with just the stolen time parts.
Thanks for posting this.
My current concern with this series is around the fact that we allocate memory from the kernel on behalf of the guest. It is the first example of such thing in the ARM port, and I can't really say I'm fond of it.
Quoted 5 lines
x86 seems to get away with it by having the memory allocated from userspace, why I tend to like more. Yes,
put_useris more expensive than a straight store, but this isn't done too often either.What is the rational for your current approach?
As I see it there are 3 approaches that can be taken here:
1. Hypervisor allocates memory and adds it to the virtual machine. This means that everything to do with the 'device' is encapsulated behind the
KVM_CREATE_DEVICE/ KVM_[GS]ET_DEVICE_ATTR ioctls. But since we want the stolen time structure to be fast it cannot be a trapping region and has to be backed by real memory - in this case allocated by the host kernel.2. Host user space allocates memory. Similar to above, but this time user space needs to manage the memory region as well as the usual
KVM_CREATE_DEVICEdance. I've no objection to this, but it means kvmtool/QEMU needs to be much more aware of what is going on (e.g. how to size the memory region).3. Guest kernel "donates" the memory to the hypervisor for the structure. As far as I'm aware this is what x86 does. The problems I see this approach are:
a) kexec becomes much more tricky - there needs to be a disabling
mechanism for the guest to stop the hypervisor scribbling on memory before starting the new kernel.b) If there is more than one entity that is interested in the
information (e.g. firmware and kernel) then this requires some form of arbitration in the guest because the hypervisor doesn't want to have to track an arbitrary number of regions to update.c) Performance can suffer if the host kernel doesn't have a suitably
aligned/sized area to use. As you say -put_user()is more expensive. The structure is updated on every return to the VM.
Quoted 5 lines
Of course x86 does prove the third approach can work, but I'm not sure which is actually better. Avoid the kexec cancellation requirements was the main driver of the current approach. Although many of the conversations about this were also tied up with Live Physical Time which adds its own complications.
My current train of thoughts is around (2):
- We don't need a new mechanism to track pages or deal with overlapping IPA ranges
- We can get rid of the save/restore interface
The drawback is that the amount of memory required per vcpu becomes ABI. I don't think that's a huge deal, as the hypervisor has the same contract with the guest.
We also take a small hit with put_user(), but this is only done as a consequence of vcpu_load() (and not on every entry as you suggest above). It'd be worth quantifying this overhead before making any decision one way or another.
Thanks,
M.
Signature · 1 line
Jazz is not dead, it just smells funny...
Re: [PATCH 4/9] KVM: arm64: Support stolen time reporting via shared structure
In reply to Marc ZyngierQuoted 7 lines · Marc Zyngier
Quoted 1 line · On Fri, 2 Aug 2019 15:50:12 +0100 Steven Price <[email protected]> wrote:
Implement the service call for configuring a shared structre between a
structure
Quoted 8 lines
Quoted 6 lines
VCPU and the hypervisor in which the hypervisor can write the time stolen from the VCPU's execution time by other tasks on the host.
The hypervisor allocates memory which is placed at an IPA chosen by user space. The hypervisor then uses
WRITE_ONCE()to update the shared structre ensuring single copy atomicity of the 64-bit unsigned valuestructure
Twice in one commit message... thanks for spotting! :)
Quoted 33 lines
Quoted 33 lines
that reports stolen time in nanoseconds.
Whenever stolen time is enabled by the guest, the stolen time counter is reset.
The stolen time itself is retrieved from the
sched_infostructure maintained by the Linux scheduler code. We enable SCHEDSTATS when selecting KVMKconfigto ensure this value is meaningful.Signed-off-by Steven Price <[email protected]>--- arch/arm64/include/asm/kvm_host.h | 13 +++++- arch/arm64/kvm/Kconfig | 1 + include/kvm/arm_hypercalls.h | 1 + include/linux/kvm_types.h | 2 + virt/kvm/arm/arm.c | 18 ++++++++ virt/kvm/arm/hypercalls.c | 70 +++++++++++++++++++++++++++++++ 6 files changed, 104 insertions(+), 1 deletion(-)arch/arm64/include/asm/kvm_host.h +1
-0@@ -44,6 +44,7 @@ KVM_ARCH_REQ_FLAGS(0, KVM_REQUEST_WAIT | KVM_REQUEST_NO_WAKEUP)#define KVM_REQ_IRQ_PENDING KVM_ARCH_REQ(1)#define KVM_REQ_VCPU_RESET KVM_ARCH_REQ(2) +#define KVM_REQ_RECORD_STEAL KVM_ARCH_REQ(3)DECLARE_STATIC_KEY_FALSE (userspace_irqchip_in_use);@@ -83,6 +84,11 @@ struct kvm_arch {
Quoted 7 lines
arch/arm64/include/asm/kvm_host.h struct kvm_arch { · line 89
Quoted 5 lines
arch/arm64/include/asm/kvm_host.h +3
-0/* Mandated version of PSCI */ u32 psci_version; + +struct kvm_arch_pvtime { +void *st;Is it really a void *? I'm sure you can use a proper type here...
Indeed that sounds like a good idea!
Quoted 141 lines
Quoted 119 lines
arch/arm64/include/asm/kvm_host.h +8
-1+gpa_t st_base; + } pvtime; };#define KVM_NR_MEM_OBJS 40@@ -338,8 +344,13 @@ struct kvm_vcpu_arch {/* True when deferrable sysregs are loaded on the physical CPU, * see kvm_vcpu_load_sysregs and kvm_vcpu_put_sysregs. */ bool sysregs_loaded_on_cpu; -}; +/* Guest PV state */ +struct { +u64 steal; +u64 last_steal; + } steal; +};/* Pointer to the vcpu's SVE FFR for sve_{save,load}_state() */ #define vcpu_sve_pffr (vcpu) ((void *)((char *)((vcpu)->arch.sve_state) + \ sve_ffr_offset((vcpu)->arch.sve_max_vl)))arch/arm64/kvm/Kconfig +1
-0@@ -39,6 +39,7 @@ config KVM select IRQ_BYPASS_MANAGER select HAVE_KVM_IRQ_BYPASS select HAVE_KVM_VCPU_RUN_PID_CHANGE + select SCHEDSTATS ---help--- Support hosting virtualized guest machines. We don't support KVM with 16K page tables yet, due to the multipleinclude/kvm/arm_hypercalls.h +1
-0@@ -7,6 +7,7 @@#include <asm/kvm_emulate.h> int kvm_hvc_call_handler (struct kvm_vcpu *vcpu); +int kvm_update_stolen_time (struct kvm_vcpu *vcpu);static inline u32 smccc_get_function (struct kvm_vcpu *vcpu) {include/linux/kvm_types.h +2
-0@@ -35,6 +35,8 @@ typedef unsigned long gva_t;typedef u64 gpa_t ;typedef u64 gfn_t ; +#define GPA_INVALID (~(gpa_t)0) +typedef unsigned long hva_t ;typedef u64 hpa_t ;typedef u64 hfn_t ;virt/kvm/arm/arm.c +18
-0@@ -40,6 +40,10 @@#include <asm/kvm_coproc.h> #include <asm/sections.h> +#include <kvm/arm_hypercalls.h> +#include <kvm/arm_pmu.h> +#include <kvm/arm_psci.h> +#ifdef REQUIRES_VIRT __asm__(".arch_extension virt" );#endif @@ -135,6 +139,7 @@ int kvm_arch_init_vm(struct kvm *kvm, unsigned long type) kvm->arch.max_vcpus = vgic_present ?kvm_vgic_get_max_vcpus () :KVM_MAX_VCPUS ; + kvm->arch.pvtime.st_base =GPA_INVALID ;return ret; out_free_stage2_pgd:kvm_free_stage2_pgd (kvm);@@ -371,6 +376,7 @@ void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu)kvm_vcpu_load_sysregs (vcpu);kvm_arch_vcpu_load_fp (vcpu);kvm_vcpu_pmu_restore_guest (vcpu); +kvm_make_request (KVM_REQ_RECORD_STEAL , vcpu);if (single_task_running ())vcpu_clear_wfe_traps (vcpu);@@ -617,6 +623,15 @@ static void vcpu_req_sleep(struct kvm_vcpu *vcpu)smp_rmb (); } +static void vcpu_req_record_steal (struct kvm_vcpu *vcpu) +{ +int idx; + + idx =srcu_read_lock (&vcpu->kvm->srcu); +kvm_update_stolen_time (vcpu); +srcu_read_unlock (&vcpu->kvm->srcu, idx); +} +static int kvm_vcpu_initialized (struct kvm_vcpu *vcpu) {return vcpu->arch.target >=0 ;@@ -636,6 +651,9 @@ static void check_vcpu_requests(struct kvm_vcpu *vcpu)* that a VCPU sees new virtual interrupts. */ kvm_check_request (KVM_REQ_IRQ_PENDING , vcpu); + +if (kvm_check_request (KVM_REQ_RECORD_STEAL , vcpu)) +vcpu_req_record_steal (vcpu); } }virt/kvm/arm/hypercalls.c +0
-0virt/kvm/arm/hypercalls.c line 15
Quoted 6 lines
virt/kvm/arm/hypercalls.c +3
-0@@ -10,6 +10,70 @@#include <kvm/arm_hypercalls.h> #include <kvm/arm_psci.h> + +static struct pvclock_vcpu_stolen_time_info *pvtime_get_st ( +struct kvm_vcpu *vcpu)nit: on a single line.
Quoted 13 lines
virt/kvm/arm/hypercalls.c +13
-0+{ +struct pvclock_vcpu_stolen_time_info *st = vcpu->kvm->arch.pvtime.st; + +if (!st) +return NULL ; + +return &st[kvm_vcpu_get_idx (vcpu)]; +} + +int kvm_update_stolen_time (struct kvm_vcpu *vcpu) +{ +u64 steal; +struct pvclock_vcpu_stolen_time_info *kaddr;
Quoted 8 lines
virt/kvm/arm/hypercalls.c line 31
Quoted 3 lines
virt/kvm/arm/hypercalls.c +3
-0+ +if (vcpu->kvm->arch.pvtime.st_base ==GPA_INVALID ) +return -ENOTSUPP ;So for a guest that doesn't have stolen time support (which is 100% of them for the foreseeable future), we still set a request, take the srcu lock and end-up here for nothing. I'd rather we test this
st_baseearly, as it should never change once the guest has started.
Good point - I'll make the call to kvm_make_request() conditional on st_base being set.
Quoted 7 lines
virt/kvm/arm/hypercalls.c line 36
Quoted 5 lines
virt/kvm/arm/hypercalls.c +5
-0+ + kaddr =pvtime_get_st (vcpu); + +if (!kaddr) +return -ENOTSUPP ;How can this happen?
Good question, and it makes the pvtime_get_st() helper rather pointless. Will remove.
Quoted 7 lines
virt/kvm/arm/hypercalls.c line 39
Quoted 3 lines
virt/kvm/arm/hypercalls.c +3
-0+ + kaddr->revision =0 ; + kaddr->attributes =0 ;Why does this need to be written each time we update the stolen time? I have the feeling this would be better moved to the hypercall initializing the data structure.
Sure.
Quoted 4 lines
Quoted 4 lines
virt/kvm/arm/hypercalls.c +4
-0+ +/* Let's do the local bookkeeping */ + steal = vcpu->arch.steal.steal; + steal += current->sched_info.run_delay - vcpu->arch.steal.last_steal;
Quoted 8 lines
virt/kvm/arm/hypercalls.c line 48
Quoted 5 lines
virt/kvm/arm/hypercalls.c +5
-0+ vcpu->arch.steal.last_steal = current->sched_info.run_delay; + vcpu->arch.steal.steal = steal; + +/* Now write out the value to the shared page */ +WRITE_ONCE (kaddr->stolen_time,cpu_to_le64 (steal));Is there any requirement for this to be visible to another CPU than the one this is being written from?
I don't believe there is a requirement for this to be visible synchronously - there is an implicit race here if another vCPU is accessing the structure (because the hypervisor can preempt and update at any point), so we don't need to push this out with a barrier. However we must use 64-bit single-copy atomicity writes to ensure that another vCPU can't see a half-updated value.
Thanks,
Steve
Re: [PATCH 4/9] KVM: arm64: Support stolen time reporting via shared structure
In reply to Marc ZyngierQuoted 172 lines · Marc Zyngier
Quoted 163 lines · On Sat, 3 Aug 2019 18:58:17 +0100 Marc Zyngier <[email protected]> wrote:
Quoted 150 lines · On Fri, 2 Aug 2019 15:50:12 +0100 Steven Price <[email protected]> wrote:
Implement the service call for configuring a shared structre between a VCPU and the hypervisor in which the hypervisor can write the time stolen from the VCPU's execution time by other tasks on the host.
The hypervisor allocates memory which is placed at an IPA chosen by user space. The hypervisor then uses
WRITE_ONCE()to update the shared structre ensuring single copy atomicity of the 64-bit unsigned value that reports stolen time in nanoseconds.Whenever stolen time is enabled by the guest, the stolen time counter is reset.
The stolen time itself is retrieved from the
sched_infostructure maintained by the Linux scheduler code. We enable SCHEDSTATS when selecting KVMKconfigto ensure this value is meaningful.Signed-off-by Steven Price <[email protected]>--- arch/arm64/include/asm/kvm_host.h | 13 +++++- arch/arm64/kvm/Kconfig | 1 + include/kvm/arm_hypercalls.h | 1 + include/linux/kvm_types.h | 2 + virt/kvm/arm/arm.c | 18 ++++++++ virt/kvm/arm/hypercalls.c | 70 +++++++++++++++++++++++++++++++ 6 files changed, 104 insertions(+), 1 deletion(-)arch/arm64/include/asm/kvm_host.h +12
-1@@ -44,6 +44,7 @@ KVM_ARCH_REQ_FLAGS(0, KVM_REQUEST_WAIT | KVM_REQUEST_NO_WAKEUP)#define KVM_REQ_IRQ_PENDING KVM_ARCH_REQ(1)#define KVM_REQ_VCPU_RESET KVM_ARCH_REQ(2) +#define KVM_REQ_RECORD_STEAL KVM_ARCH_REQ(3)DECLARE_STATIC_KEY_FALSE (userspace_irqchip_in_use);@@ -83,6 +84,11 @@ struct kvm_arch {/* Mandated version of PSCI */ u32 psci_version; + +struct kvm_arch_pvtime { +void *st; +gpa_t st_base; + } pvtime; };#define KVM_NR_MEM_OBJS 40@@ -338,8 +344,13 @@ struct kvm_vcpu_arch {/* True when deferrable sysregs are loaded on the physical CPU, * see kvm_vcpu_load_sysregs and kvm_vcpu_put_sysregs. */ bool sysregs_loaded_on_cpu; -}; +/* Guest PV state */ +struct { +u64 steal; +u64 last_steal; + } steal; +};/* Pointer to the vcpu's SVE FFR for sve_{save,load}_state() */ #define vcpu_sve_pffr (vcpu) ((void *)((char *)((vcpu)->arch.sve_state) + \ sve_ffr_offset((vcpu)->arch.sve_max_vl)))arch/arm64/kvm/Kconfig +1
-0@@ -39,6 +39,7 @@ config KVM select IRQ_BYPASS_MANAGER select HAVE_KVM_IRQ_BYPASS select HAVE_KVM_VCPU_RUN_PID_CHANGE + select SCHEDSTATS ---help--- Support hosting virtualized guest machines. We don't support KVM with 16K page tables yet, due to the multipleinclude/kvm/arm_hypercalls.h +1
-0@@ -7,6 +7,7 @@#include <asm/kvm_emulate.h> int kvm_hvc_call_handler (struct kvm_vcpu *vcpu); +int kvm_update_stolen_time (struct kvm_vcpu *vcpu);static inline u32 smccc_get_function (struct kvm_vcpu *vcpu) {include/linux/kvm_types.h +2
-0@@ -35,6 +35,8 @@ typedef unsigned long gva_t;typedef u64 gpa_t ;typedef u64 gfn_t ; +#define GPA_INVALID (~(gpa_t)0) +typedef unsigned long hva_t ;typedef u64 hpa_t ;typedef u64 hfn_t ;virt/kvm/arm/arm.c +15
-0@@ -40,6 +40,10 @@#include <asm/kvm_coproc.h> #include <asm/sections.h> +#include <kvm/arm_hypercalls.h> +#include <kvm/arm_pmu.h> +#include <kvm/arm_psci.h> +#ifdef REQUIRES_VIRT __asm__(".arch_extension virt" );#endif @@ -135,6 +139,7 @@ int kvm_arch_init_vm(struct kvm *kvm, unsigned long type) kvm->arch.max_vcpus = vgic_present ?kvm_vgic_get_max_vcpus () :KVM_MAX_VCPUS ; + kvm->arch.pvtime.st_base =GPA_INVALID ;return ret; out_free_stage2_pgd:kvm_free_stage2_pgd (kvm);@@ -371,6 +376,7 @@ void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu)kvm_vcpu_load_sysregs (vcpu);kvm_arch_vcpu_load_fp (vcpu);kvm_vcpu_pmu_restore_guest (vcpu); +kvm_make_request (KVM_REQ_RECORD_STEAL , vcpu);if (single_task_running ())vcpu_clear_wfe_traps (vcpu);@@ -617,6 +623,15 @@ static void vcpu_req_sleep(struct kvm_vcpu *vcpu)smp_rmb (); } +static void vcpu_req_record_steal (struct kvm_vcpu *vcpu) +{ +int idx; + + idx =srcu_read_lock (&vcpu->kvm->srcu); +kvm_update_stolen_time (vcpu); +srcu_read_unlock (&vcpu->kvm->srcu, idx); +} +static int kvm_vcpu_initialized (struct kvm_vcpu *vcpu) {return vcpu->arch.target >=0 ;virt/kvm/arm/arm.c static void check_vcpu_requests(struct kvm_vcpu *vcpu) · line 656
Quoted 6 lines
virt/kvm/arm/arm.c +3
-0@@ -636,6 +651,9 @@ static void check_vcpu_requests(struct kvm_vcpu *vcpu)* that a VCPU sees new virtual interrupts. */ kvm_check_request (KVM_REQ_IRQ_PENDING , vcpu); + +if (kvm_check_request (KVM_REQ_RECORD_STEAL , vcpu)) +vcpu_req_record_steal (vcpu);Something troubles me. Here, you've set the request on load. But you can be preempted at any time (preemption gets disabled just after).
Quoted 5 lines
I have the feeling that should you get preempted right here, you'll end-up having accumulated the wrong amount of steal time, as the request put via load when you'll get scheduled back in will only get processed after a full round of entry/exit/entry, which doesn't look great.
Quoted 3 lines
Ah, no. We're saved by the check for pending requests right before we jump in the guest, causing an early exit and the whole shebang to be restarted.
Yes, that's my understanding. Obviously not ideal if it happens in that small window, but everything is redone to get the right values in the end.
Steve
Re: [PATCH 6/9] KVM: arm64: Provide a PV_TIME device to user space
In reply to Marc ZyngierQuoted 10 lines · Marc Zyngier
Quoted 7 lines · On Fri, 2 Aug 2019 15:50:14 +0100 Steven Price <[email protected]> wrote:
Allow user space to inform the KVM host where in the physical memory map the paravirtualized time structures should be located.
A device is created which provides the base address of an array of Stolen Time (ST) structures, one for each VCPU. There must be (64 * total number of VCPUs) bytes of memory available at this location.
Quoted 6 lines
Quoted 3 lines
The address is given in terms of the physical address visible to the guest and must be 64 byte aligned. The memory should be marked as reserved to the guest to stop it allocating it for other purposes.
Why? You seem to be allocating the memory from the kernel, so as far as the guest is concerned, this isn't generally usable memory.
I obviously didn't word it very well - that's what I meant. The "memory" that represents the stolen time structure shouldn't be shown to the guest as normal memory, but "reserved" for the purpose of stolen time.
To be honest it looks like I forgot to rewrite this commit message - which 64 byte alignment is all that the guest can rely on (because each vCPU has it's own structure), the actual array of structures needs to be page aligned to ensure we can safely map it into the guest.
Quoted 114 lines
Quoted 114 lines
Signed-off-by Steven Price <[email protected]>--- arch/arm64/include/asm/kvm_mmu.h | 2 + arch/arm64/include/uapi/asm/kvm.h | 6 + arch/arm64/kvm/Makefile | 1 + include/uapi/linux/kvm.h | 2 + virt/kvm/arm/mmu.c | 44 +++++++ virt/kvm/arm/pvtime.c | 190 ++++++++++++++++++++++++++++++ 6 files changed, 245 insertions(+) create mode 100644 virt/kvm/arm/pvtime.carch/arm64/include/asm/kvm_mmu.h +2
-0@@ -157,6 +157,8 @@ int kvm_alloc_stage2_pgd(struct kvm *kvm);void kvm_free_stage2_pgd (struct kvm *kvm);int kvm_phys_addr_ioremap (struct kvm *kvm,phys_addr_t guest_ipa,phys_addr_t pa,unsigned long size,bool writable); +int kvm_phys_addr_memremap (struct kvm *kvm,phys_addr_t guest_ipa, +phys_addr_t pa,unsigned long size,bool writable);int kvm_handle_guest_abort (struct kvm_vcpu *vcpu,struct kvm_run *run);arch/arm64/include/uapi/asm/kvm.h +6
-0@@ -367,6 +367,12 @@ struct kvm_vcpu_events {#define KVM_PSCI_RET_INVAL PSCI_RET_INVALID_PARAMS#define KVM_PSCI_RET_DENIED PSCI_RET_DENIED +/* Device Control API: PV_TIME */ +#define KVM_DEV_ARM_PV_TIME_PADDR 0 +#define KVM_DEV_ARM_PV_TIME_ST 0 +#define KVM_DEV_ARM_PV_TIME_STATE_SIZE 1 +#define KVM_DEV_ARM_PV_TIME_STATE 2 +#endif #endif /* __ARM_KVM_H__ */ arch/arm64/kvm/Makefile +1
-0@@ -14,6 +14,7 @@ kvm-$(CONFIG_KVM_ARM_HOST) += $(KVM)/kvm_main.o $(KVM)/coalesced_mmio.o $(KVM)/e kvm-$(CONFIG_KVM_ARM_HOST) += $(KVM)/arm/arm.o $(KVM)/arm/mmu.o $(KVM)/arm/mmio.o kvm-$(CONFIG_KVM_ARM_HOST) += $(KVM)/arm/psci.o $(KVM)/arm/perf.o kvm-$(CONFIG_KVM_ARM_HOST) += $(KVM)/arm/hypercalls.o +kvm-$(CONFIG_KVM_ARM_HOST) += $(KVM)/arm/pvtime.o kvm-$(CONFIG_KVM_ARM_HOST) += inject_fault.o regmap.o va_layout.o kvm-$(CONFIG_KVM_ARM_HOST) += hyp.o hyp-init.o handle_exit.oinclude/uapi/linux/kvm.h +2
-0@@ -1222,6 +1222,8 @@ enum kvm_device_type {#define KVM_DEV_TYPE_ARM_VGIC_ITS KVM_DEV_TYPE_ARM_VGIC_ITS KVM_DEV_TYPE_XIVE,#define KVM_DEV_TYPE_XIVE KVM_DEV_TYPE_XIVE + KVM_DEV_TYPE_ARM_PV_TIME, +#define KVM_DEV_TYPE_ARM_PV_TIME KVM_DEV_TYPE_ARM_PV_TIME KVM_DEV_TYPE_MAX, };virt/kvm/arm/mmu.c +39
-0@@ -1368,6 +1368,50 @@ int kvm_phys_addr_ioremap(struct kvm *kvm, phys_addr_t guest_ipa,return ret; } +/** +* kvm_phys_addr_memremap - map a memory range to guest IPA +* +* @kvm: The KVM pointer +* @guest_ipa: The IPA at which to insert the mapping +* @pa: The physical address of the memory +* @size: The size of the mapping +*/ +int kvm_phys_addr_memremap (struct kvm *kvm,phys_addr_t guest_ipa, +phys_addr_t pa,unsigned long size,bool writable) +{ +phys_addr_t addr, end; +int ret =0 ; +unsigned long pfn; +struct kvm_mmu_memory_cache cache = {0 , }; + + end = (guest_ipa + size +PAGE_SIZE -1 ) &PAGE_MASK ; + pfn =__phys_to_pfn (pa); + +for (addr = guest_ipa; addr < end; addr +=PAGE_SIZE ) { +pte_t pte =pfn_pte (pfn,PAGE_S2 ); + +if (writable) + pte =kvm_s2pte_mkwrite (pte); + + ret =mmu_topup_memory_cache (&cache, +kvm_mmu_cache_min_pages (kvm), +KVM_NR_MEM_OBJS ); +if (ret) + goto out; +spin_lock (&kvm->mmu_lock); + ret =stage2_set_pte (kvm, &cache, addr, &pte,0 ); +spin_unlock (&kvm->mmu_lock); +if (ret) + goto out; + + pfn++; + } +
Quoted 8 lines
virt/kvm/arm/mmu.c int kvm_phys_addr_ioremap(struct kvm *kvm, phys_addr_t guest_ipa, · line 1413
Quoted 4 lines
virt/kvm/arm/mmu.c +4
-0+out: +mmu_free_memory_cache (&cache); +return ret; +}This is an exact copy of
kvm_phys_addr_ioremap(), with only the memory attributes changing. Surely we can have a shared implementation that takes the memory attribute as a parameter.
Good point - although due to below I'm going to need something which can deal with non-contiguous memory...
Quoted 12 lines
Quoted 12 lines
virt/kvm/arm/mmu.c +1
-0+static bool transparent_hugepage_adjust (kvm_pfn_t *pfnp,phys_addr_t *ipap) {kvm_pfn_t pfn = *pfnp;virt/kvm/arm/pvtime.c new file +2
-0@@ -0,0 +1,190 @@ +// SPDX-License-Identifier: GPL-2.0 +// Copyright (C) 2019 Arm Ltd.
Quoted 8 lines
virt/kvm/arm/pvtime.c line 8
Quoted 6 lines
virt/kvm/arm/pvtime.c new file +6
-0+ +#include <linux/kvm_host.h> +#include <asm/kvm_mmu.h> + +/* We currently only support PV time on ARM64 */ +#ifdef CONFIG_ARM64 And we're only compiling it on arm64, so why the #ifdef?
Another good point - will remove.
Quoted 10 lines
virt/kvm/arm/pvtime.c line 14
Quoted 6 lines
virt/kvm/arm/pvtime.c new file +6
-0+ +#include <asm/pvclock-abi.h> + +static int max_stolen_size (void ) +{ +int size =KVM_MAX_VCPUS *sizeof (struct pvclock_vcpu_stolen_time_info );So we're always allocating enough memory for 512 CPUs? That's an additional 32kB of contiguous memory...
Quoted 15 lines
virt/kvm/arm/pvtime.c line 26
Quoted 12 lines
virt/kvm/arm/pvtime.c new file +12
-0+ +return ALIGN (size,PAGE_SIZE ); +} + +static int kvm_arm_pvtime_create (struct kvm_device *dev,u32 type) +{ +struct kvm_arch_pvtime *pvtime = &dev->kvm->arch.pvtime; + + pvtime->st =alloc_pages_exact (max_stolen_size (), +GFP_KERNEL | __GFP_ZERO); +if (!pvtime->st) +return -ENOMEM ;Is there any chance we could use a vmalloc allocation instead? This would lift the requirement on having physically contiguous memory.
Yes I think it should be possible to use non-contiguous memory and vmalloc.
Quoted 71 lines
Quoted 71 lines
virt/kvm/arm/pvtime.c new file +71
-0+ +return 0 ; +} + +static void kvm_arm_pvtime_destroy (struct kvm_device *dev) +{ +struct kvm_arch_pvtime *pvtime = &dev->kvm->arch.pvtime; + + pvtime->st_base =GPA_INVALID ; +free_pages_exact (pvtime->st,max_stolen_size ()); +kfree (dev); +} + +static int pvtime_map_pages (struct kvm *kvm,gpa_t guest_paddr, +void *kaddr,int size) +{ +return kvm_phys_addr_memremap (kvm, guest_paddr, +virt_to_phys (kaddr), + size, false); +} + +static int pvtime_save_state (struct kvm *kvm,u64 type,void __user *user) +{ +void *source; +size_t size; + +switch (type) { +case KVM_DEV_ARM_PV_TIME_ST : + source = kvm->arch.pvtime.st; + size =sizeof (struct pvclock_vcpu_stolen_time_info ) * +atomic_read (&kvm->online_vcpus); +break ; +default : +return -ENXIO ; + } + +if (copy_to_user (user, source, size)) +return -EFAULT ; +return 0 ; +} + +static int pvtime_restore_state (struct kvm *kvm,u64 type,void __user *user) +{ +void *dest; +size_t size; + +switch (type) { +case KVM_DEV_ARM_PV_TIME_ST : + dest = kvm->arch.pvtime.st; + size =sizeof (struct pvclock_vcpu_stolen_time_info ) * +atomic_read (&kvm->online_vcpus); +break ; +default : +return -ENXIO ; + } + +if (copy_from_user (dest, user, size)) +return -EFAULT ; + +return 0 ; +} + +static int kvm_arm_pvtime_set_attr (struct kvm_device *dev, +struct kvm_device_attr *attr) +{ +struct kvm_arch_pvtime *pvtime = &dev->kvm->arch.pvtime; +u64 __user *user = (u64 __user *)attr->addr; +u64 paddr; +int ret; + +switch (attr->group) {
Quoted 8 lines
virt/kvm/arm/pvtime.c line 102
Quoted 5 lines
virt/kvm/arm/pvtime.c new file +5
-0+case KVM_DEV_ARM_PV_TIME_PADDR : +if (get_user (paddr, user)) +return -EFAULT ; +if (paddr &63 ) +return -EINVAL ;You should check whether the device fits into the IPA space for this guest, and whether it overlaps with anything else.
pvtime_map_pages() should fail in the case of overlap. That seems sufficient to me - do you think we need something stronger?
Quoted 9 lines
virt/kvm/arm/pvtime.c line 108
Quoted 6 lines
virt/kvm/arm/pvtime.c new file +6
-0+switch (attr->attr) { +case KVM_DEV_ARM_PV_TIME_ST : +if (pvtime->st_base !=GPA_INVALID ) +return -EEXIST ; + ret =pvtime_map_pages (dev->kvm, paddr, pvtime->st, +max_stolen_size ());Consider moving the size directly into
pvtime_map_pages(), and dropping thepvtime->stparameter. All you need is kvm and paddr.
Will do.
Thanks,
Steve
Marc Zyngier
maintainerRe: [PATCH 6/9] KVM: arm64: Provide a PV_TIME device to user space
Quoted 38 lines · Steven Price
Quoted 10 lines · Marc Zyngier
Quoted 7 lines · On Fri, 2 Aug 2019 15:50:14 +0100 Steven Price <[email protected]> wrote:
Allow user space to inform the KVM host where in the physical memory map the paravirtualized time structures should be located.
A device is created which provides the base address of an array of Stolen Time (ST) structures, one for each VCPU. There must be (64 * total number of VCPUs) bytes of memory available at this location.
Quoted 6 lines
Quoted 3 lines
The address is given in terms of the physical address visible to the guest and must be 64 byte aligned. The memory should be marked as reserved to the guest to stop it allocating it for other purposes.
Why? You seem to be allocating the memory from the kernel, so as far as the guest is concerned, this isn't generally usable memory.
I obviously didn't word it very well - that's what I meant. The "memory" that represents the stolen time structure shouldn't be shown to the guest as normal memory, but "reserved" for the purpose of stolen time.
To be honest it looks like I forgot to rewrite this commit message - which 64 byte alignment is all that the guest can rely on (because each vCPU has it's own structure), the actual array of structures needs to be page aligned to ensure we can safely map it into the guest.
Quoted 11 lines
Quoted 11 lines
Signed-off-by Steven Price <[email protected]>--- arch/arm64/include/asm/kvm_mmu.h | 2 + arch/arm64/include/uapi/asm/kvm.h | 6 + arch/arm64/kvm/Makefile | 1 + include/uapi/linux/kvm.h | 2 + virt/kvm/arm/mmu.c | 44 +++++++ virt/kvm/arm/pvtime.c | 190 ++++++++++++++++++++++++++++++ 6 files changed, 245 insertions(+) create mode 100644 virt/kvm/arm/pvtime.c
[...]
Quoted 18 lines
Quoted 9 lines
Quoted 9 lines
virt/kvm/arm/pvtime.c new file +9
-0+static int kvm_arm_pvtime_set_attr (struct kvm_device *dev, +struct kvm_device_attr *attr) +{ +struct kvm_arch_pvtime *pvtime = &dev->kvm->arch.pvtime; +u64 __user *user = (u64 __user *)attr->addr; +u64 paddr; +int ret; + +switch (attr->group) {Quoted 8 lines
virt/kvm/arm/pvtime.c line 102
Quoted 5 lines
virt/kvm/arm/pvtime.c new file +5
-0+case KVM_DEV_ARM_PV_TIME_PADDR : +if (get_user (paddr, user)) +return -EFAULT ; +if (paddr &63 ) +return -EINVAL ;You should check whether the device fits into the IPA space for this guest, and whether it overlaps with anything else.
Quoted 2 lines
pvtime_map_pages()should fail in the case of overlap. That seems sufficient to me - do you think we need something stronger?
Definitely. stage2_set_pte() won't fail for a non-IO overlapping mapping, and will just treat it as guest memory. If this overlaps with a memslot, we'll never be able to fault that page in, ending up with interesting memory corruption... :-/
That's one of the reasons why I think option (2) in your earlier email is an interesting one, as it sidesteps a whole lot of ugly and hard to test corner cases.
Thanks,
M.
Signature · 5 lines
Jazz is not dead, it just smells funny... _______________________________________________ kvmarm mailing list [email protected] https://lists.cs.columbia.edu/mailman/listinfo/kvmarm
Re: [PATCH 1/9] KVM: arm64: Document PV-time interface
In reply to Steven PriceQuoted 6 lines · Steven Price
Introduce a paravirtualization interface for KVM/arm64 based on the "Arm Paravirtualized Time for Arm-Base Systems" specification DEN 0057A.
This only adds the details about "Stolen Time" as the details of "Live Physical Time" have not been fully agreed.
[...]
Quoted 5 lines
Documentation/virtual/kvm/arm/pvtime.txt new file +5
-0+ +Stolen Time +----------- + +The structure pointed to by the PV_TIME_ST hypercall is as follows:
Documentation/virtual/kvm/arm/pvtime.txt line 46
Quoted 10 lines
Documentation/virtual/kvm/arm/pvtime.txt new file +10
-0+ + Field | Byte Length | Byte Offset | Description + ----------- | ----------- | ----------- | -------------------------- + Revision | 4 | 0 | Must be 0 for version 0.1 + Attributes | 4 | 4 | Must be 0 + Stolen time | 8 | 8 | Stolen time in unsigned + | | | nanoseconds indicating how + | | | much time this VCPU thread + | | | was involuntarily not + | | | running on a physical CPU.
I know very little about the topic, but I don't understand how the spec as proposed allows an accurate reading of the relation between physical time and stolen time simultaneously. In other words, could you draw Figure 1 of the spec from within the guest? Or is it a non-objective?
For example, if you read the stolen time before you read CNTVCT_EL0, isn't it possible for a lengthy event like a migration to occur between the two reads, causing the stolen time to be obsolete and off by seconds?
--
Cheers,
Christophe de Dinechin (IRC c3d)
Re: [PATCH 1/9] KVM: arm64: Document PV-time interface
Quoted 23 lines · Christophe de Dinechin
Quoted 6 lines · Steven Price
Introduce a paravirtualization interface for KVM/arm64 based on the "Arm Paravirtualized Time for Arm-Base Systems" specification DEN 0057A.
This only adds the details about "Stolen Time" as the details of "Live Physical Time" have not been fully agreed.
[...]
Quoted 12 lines
Documentation/virtual/kvm/arm/pvtime.txt new file +12
-0+ +Stolen Time +----------- + +The structure pointed to by the PV_TIME_ST hypercall is as follows: + + Field | Byte Length | Byte Offset | Description + ----------- | ----------- | ----------- | -------------------------- + Revision | 4 | 0 | Must be 0 for version 0.1 + Attributes | 4 | 4 | Must be 0 + Stolen time | 8 | 8 | Stolen time in unsigned + | | | nanoseconds indicating how
Quoted 8 lines
Documentation/virtual/kvm/arm/pvtime.txt line 46
Quoted 3 lines
Documentation/virtual/kvm/arm/pvtime.txt new file +3
-0+ | | | much time this VCPU thread + | | | was involuntarily not + | | | running on a physical CPU.I know very little about the topic, but I don't understand how the spec as proposed allows an accurate reading of the relation between physical time and stolen time simultaneously. In other words, could you draw Figure 1 of the spec from within the guest? Or is it a non-objective?
Figure 1 is mostly attempting to explain Live Physical Time (LPT), which is not part of this patch series. But it does touch on stolen time by the difference between "live physical time" and "virtual time".
I'm not sure what you mean by "from within the guest". From the perspective of the guest the parts of the diagram where the guest isn't running don't exist (therefore there are discontinuities in the "physical time" and "live physical time" lines).
This patch series doesn't attempt to provide the guest with a view of "physical time" (or LPT) - but it might be able to observe that by consulting something external (e.g. an NTP server, or an emulated RTC which reports wall-clock time).
What it does provide is a mechanism for obtaining the difference (as reported by the host) between "live physical time" and "virtual time" - this is reported in nanoseconds in the above structure.
Quoted 3 lines
For example, if you read the stolen time before you read
CNTVCT_EL0, isn't it possible for a lengthy event like a migration to occur between the two reads, causing the stolen time to be obsolete and off by seconds?
"Lengthy events" like migration are represented by the "paused" state in the diagram - i.e. it's the difference between "physical time" and "live physical time". So stolen time doesn't attempt to represent that.
And yes, there is a race between reading CNTVCT_EL0 and reading stolen time - but in practice this doesn't really matter. The usual pseudo-code way of using stolen time is:
* scheduler captures stolen time from structure and CNTVCT_EL0:
before_timer = CNTVCT_EL0
before_stolen = stolen
* schedule in process
* process is pre-empted (or blocked in some way)
* scheduler captures stolen time from structure and CNTVCT_EL0:
after_timer = CNTVCT_EL0
after_stolen = stolen
time = to_nsecs(after_timer - before_timer) -
(after_stolen - before_stolen)
The scheduler can then charge the process for "time" nanoseconds of time. This ensures that a process isn't unfairly penalised if the host doesn't schedule the VCPU while it is supposed to be running.
The race is very small in comparison to the time the process is running, and in the worst case just means the process is charged slightly more (or less) than it should be.
I guess if you're really worried about it, you could do a dance like:
do {
before = stolen
timer = CNTVCT_EL0
after = stolen
} while (before != after);
But I don't see the need to have such an accurate view of elapsed time that the VCPU was scheduled. And of course at the moment (without this series) the guest has no idea about time stolen by the host.
Steve
Re: [PATCH 6/9] KVM: arm64: Provide a PV_TIME device to user space
In reply to Marc ZyngierQuoted 10 lines · Marc Zyngier
On Sat, 3 Aug 2019 13:51:13 +0100
Marc Zyngier <[email protected]> wrote:[forgot that one]
Quoted 2 lines
On Fri, 2 Aug 2019 15:50:14 +0100
Steven Price <[email protected]> wrote:[...]
Quoted 12 lines
Quoted 8 lines
virt/kvm/arm/pvtime.c line 188
Quoted 8 lines
virt/kvm/arm/pvtime.c new file +8
-0+static int __init kvm_pvtime_init (void ) +{ +kvm_register_device_ops (&pvtime_ops,KVM_DEV_TYPE_ARM_PV_TIME ); + +return 0 ; +} + +late_initcall (kvm_pvtime_init);Why is it an initcall? So far, the only initcall we've used is the one that initializes KVM itself. Can't we just the
device_opsjust like we do for the vgic?
So would you prefer a direct call from init_subsystems() in virt/kvm/arm/arm.c?
The benefit of initcall is just that it keeps the code self-contained. In init_subsystems() I'd either need a #ifdef CONFIG_ARM64 or a dummy function for arm.
Steve
Marc Zyngier
maintainerRe: [PATCH 6/9] KVM: arm64: Provide a PV_TIME device to user space
Quoted 24 lines · Steven Price
Quoted 10 lines · Marc Zyngier
On Sat, 3 Aug 2019 13:51:13 +0100
Marc Zyngier <[email protected]> wrote:[forgot that one]
Quoted 2 lines
On Fri, 2 Aug 2019 15:50:14 +0100
Steven Price <[email protected]> wrote:[...]
Quoted 12 lines
Quoted 8 lines
virt/kvm/arm/pvtime.c line 188
Quoted 8 lines
virt/kvm/arm/pvtime.c new file +8
-0+static int __init kvm_pvtime_init (void ) +{ +kvm_register_device_ops (&pvtime_ops,KVM_DEV_TYPE_ARM_PV_TIME ); + +return 0 ; +} + +late_initcall (kvm_pvtime_init);Why is it an initcall? So far, the only initcall we've used is the one that initializes KVM itself. Can't we just the
device_opsjust like we do for the vgic?
Quoted 2 lines
So would you prefer a direct call from
init_subsystems()invirt/kvm/arm/arm.c?
Yes. Consistency is important.
Quoted 3 lines
The benefit of initcall is just that it keeps the code self-contained. In
init_subsystems()I'd either need a #ifdefCONFIG_ARM64or a dummy function for arm.
Having a dummy function for 32bit ARM is fine. Most of the code we add to the 32bit port is made of empty stubs anyway.
Thanks,
M.
Signature · 1 line
Jazz is not dead, it just smells funny...
Re: [PATCH 1/9] KVM: arm64: Document PV-time interface
In reply to Steven PriceQuoted 37 lines · Steven Price
Quoted 23 lines · Christophe de Dinechin
Quoted 6 lines · Steven Price
Introduce a paravirtualization interface for KVM/arm64 based on the "Arm Paravirtualized Time for Arm-Base Systems" specification DEN 0057A.
This only adds the details about "Stolen Time" as the details of "Live Physical Time" have not been fully agreed.
[...]
Quoted 12 lines
Documentation/virtual/kvm/arm/pvtime.txt new file +12
-0+ +Stolen Time +----------- + +The structure pointed to by the PV_TIME_ST hypercall is as follows: + + Field | Byte Length | Byte Offset | Description + ----------- | ----------- | ----------- | -------------------------- + Revision | 4 | 0 | Must be 0 for version 0.1 + Attributes | 4 | 4 | Must be 0 + Stolen time | 8 | 8 | Stolen time in unsigned + | | | nanoseconds indicating howQuoted 8 lines
Documentation/virtual/kvm/arm/pvtime.txt line 46
Quoted 3 lines
Documentation/virtual/kvm/arm/pvtime.txt new file +3
-0+ | | | much time this VCPU thread + | | | was involuntarily not + | | | running on a physical CPU.I know very little about the topic, but I don't understand how the spec as proposed allows an accurate reading of the relation between physical time and stolen time simultaneously. In other words, could you draw Figure 1 of the spec from within the guest? Or is it a non-objective?
Quoted 8 lines
Figure 1 is mostly attempting to explain Live Physical Time (LPT), which is not part of this patch series. But it does touch on stolen time by the difference between "live physical time" and "virtual time".
I'm not sure what you mean by "from within the guest". From the perspective of the guest the parts of the diagram where the guest isn't running don't exist (therefore there are discontinuities in the "physical time" and "live physical time" lines).
I meant: If I run code within the guest that attempts to draw Figure 1, race conditions may cause the diagram actually drawn by your guest program to look completely wrong on occasions.
Quoted 4 lines
This patch series doesn't attempt to provide the guest with a view of "physical time" (or LPT) - but it might be able to observe that by consulting something external (e.g. an NTP server, or an emulated RTC which reports wall-clock time).
… with what appear to be like a built-in race condition, as you correctly identified. I was wondering if the built-in race condition was deliberate and/or necessary, or if it was irrelevant for the planned uses of the value.
Quoted 8 lines
What it does provide is a mechanism for obtaining the difference (as reported by the host) between "live physical time" and "virtual time" - this is reported in nanoseconds in the above structure.
Quoted 3 lines
For example, if you read the stolen time before you read
CNTVCT_EL0, isn't it possible for a lengthy event like a migration to occur between the two reads, causing the stolen time to be obsolete and off by seconds?
Quoted 7 lines
"Lengthy events" like migration are represented by the "paused" state in the diagram - i.e. it's the difference between "physical time" and "live physical time". So stolen time doesn't attempt to represent that.
And yes, there is a race between reading
CNTVCT_EL0and reading stolen time - but in practice this doesn't really matter. The usual pseudo-code way of using stolen time is:
I’m assuming this is the guest scheduler you are talking about, and I’m assuming virtualization can preempt that code anywhere. Maybe that’s where I’m wrong?
For the sake of the argument, assume there is a 1s pause. Not completely unreasonable in a migration scenario.
Quoted 2 lines
* scheduler captures stolen time from structure and
CNTVCT_EL0:
before_timer=CNTVCT_EL0
[insert optional 1s pause here, case A]
Quoted 5 lines
before_stolen= stolen
* schedule in process
* process is pre-empted (or blocked in some way)
* scheduler captures stolen time from structure andCNTVCT_EL0:
after_timer=CNTVCT_EL0
[insert optional 1s pause here, case B]
Quoted 3 lines
after_stolen= stolen
time =to_nsecs(after_timer - before_timer)-
(after_stolen-before_stolen)
In case A, time is too big by one second. In case B, it is too small, to the point where your code might need to be ready for “time” unexpectedly showing up as negative.
Quoted 8 lines
The scheduler can then charge the process for "time" nanoseconds of time. This ensures that a process isn't unfairly penalised if the host doesn't schedule the VCPU while it is supposed to be running.
The race is very small in comparison to the time the process is running, and in the worst case just means the process is charged slightly more (or less) than it should be.
At this point, what I don’t understand is why the race would be “very small” or why you would only be charged “slightly” more or less?
Quoted 7 lines
I guess if you're really worried about it, you could do a dance like:
do {
before = stolen
timer =CNTVCT_EL0
after = stolen
} while (before != after);
That will work as long as nothing in that loop requires something that would cause stolen to jump. If there is such a guarantee, then that’s even efficient, because in most cases the loop would only run once, at the cost of one extra read and one test.
Quoted 3 lines
But I don't see the need to have such an accurate view of elapsed time that the VCPU was scheduled. And of course at the moment (without this series) the guest has no idea about time stolen by the host.
I’m certainly not arguing that exposing stolen time is a bad idea, I’m only wondering if the proposed solution is racy, and if so, if it is intentional.
If it’s indeed racy, the problem could be mitigated in a number of ways
a) document your loop or something similar as being the recommended way to avoid the race, and then ensure that the loop actually will always work as intended. The upside is that it’s just a change in some comments or documentation.
b) having a single interface that exposes multiple times. For example, you could have a copy of CNTVCT_EL0 written alongside stolen time, and then the scheduler could use that copy for its decision.
Thanks
Christophe
_______________________________________________
kvmarm mailing list
[email protected]
https://lists.cs.columbia.edu/mailman/listinfo/kvmarm
Re: [PATCH 1/9] KVM: arm64: Document PV-time interface
Quoted 72 lines · Christophe de Dinechin
Quoted 38 lines
On 7 Aug 2019, at 15:21, Steven Price <[email protected] <mailto:[email protected]>> wrote:
Quoted 23 lines · Christophe de Dinechin
Quoted 6 lines · Steven Price
Introduce a paravirtualization interface for KVM/arm64 based on the "Arm Paravirtualized Time for Arm-Base Systems" specification DEN 0057A.
This only adds the details about "Stolen Time" as the details of "Live Physical Time" have not been fully agreed.
[...]
Quoted 12 lines
Documentation/virtual/kvm/arm/pvtime.txt new file +12
-0+ +Stolen Time +----------- + +The structure pointed to by the PV_TIME_ST hypercall is as follows: + + Field | Byte Length | Byte Offset | Description + ----------- | ----------- | ----------- | -------------------------- + Revision | 4 | 0 | Must be 0 for version 0.1 + Attributes | 4 | 4 | Must be 0 + Stolen time | 8 | 8 | Stolen time in unsigned + | | | nanoseconds indicating howQuoted 8 lines
Quoted 3 lines
+ | | | much time this VCPU thread + | | | was involuntarily not + | | | running on a physical CPU.I know very little about the topic, but I don't understand how the spec as proposed allows an accurate reading of the relation between physical time and stolen time simultaneously. In other words, could you draw Figure 1 of the spec from within the guest? Or is it a non-objective?
Quoted 8 lines
Figure 1 is mostly attempting to explain Live Physical Time (LPT), which is not part of this patch series. But it does touch on stolen time by the difference between "live physical time" and "virtual time".
I'm not sure what you mean by "from within the guest". From the perspective of the guest the parts of the diagram where the guest isn't running don't exist (therefore there are discontinuities in the "physical time" and "live physical time" lines).
I meant: If I run code within the guest that attempts to draw Figure 1, race conditions may cause the diagram actually drawn by your guest program to look completely wrong on occasions.
Quoted 4 lines
This patch series doesn't attempt to provide the guest with a view of "physical time" (or LPT) - but it might be able to observe that by consulting something external (e.g. an NTP server, or an emulated RTC which reports wall-clock time).
… with what appear to be like a built-in race condition, as you correctly identified. I was wondering if the built-in race condition was deliberate and/or necessary, or if it was irrelevant for the planned uses of the value.
Quoted 12 lines
What it does provide is a mechanism for obtaining the difference (as reported by the host) between "live physical time" and "virtual time" - this is reported in nanoseconds in the above structure.
Quoted 3 lines
For example, if you read the stolen time before you read
CNTVCT_EL0, isn't it possible for a lengthy event like a migration to occur between the two reads, causing the stolen time to be obsolete and off by seconds?"Lengthy events" like migration are represented by the "paused" state in the diagram - i.e. it's the difference between "physical time" and "live physical time". So stolen time doesn't attempt to represent that.
Quoted 5 lines
Quoted 3 lines
And yes, there is a race between reading
CNTVCT_EL0and reading stolen time - but in practice this doesn't really matter. The usual pseudo-code way of using stolen time is:I’m assuming this is the guest scheduler you are talking about,
yes
Quoted 2 lines
and I’m assuming virtualization can preempt that code anywhere. Maybe that’s where I’m wrong?
You are correct, the guest can be preempted at any point.
Quoted 3 lines
For the sake of the argument, assume there is a 1s pause. Not completely unreasonable in a migration scenario.
As I mentioned before, events like migration are not represented by stolen time. They would be represented by CNTVCT_EL0 appearing to pause during the migration (so showing a difference between "physical time" and "live physical time"). The stolen time value would not be incremented.
Quoted 13 lines
Quoted 2 lines
* scheduler captures stolen time from structure and
CNTVCT_EL0:
before_timer =CNTVCT_EL0[insert optional 1s pause here, case A]
Quoted 5 lines
before_stolen = stolen
* schedule in process
* process is pre-empted (or blocked in some way)
* scheduler captures stolen time from structure andCNTVCT_EL0:
after_timer =CNTVCT_EL0[insert optional 1s pause here, case B]
Quoted 7 lines
Quoted 3 lines
after_stolen = stolen
time =to_nsecs(after_timer - before_timer)- (after_stolen-before_stolen)In case A, time is too big by one second. In case B, it is too small, to the point where your code might need to be ready for “time” unexpectedly showing up as negative.
So a 1 second pause is unlikely for stolen time - this means that the VCPU was ready to run, but the host didn't run it for some reason. But in theory you are correct this could happen. The core code deals with it like this (update_rq_clock_task):
Quoted 10 lines
if (static_key_false((¶virt_steal_rq_enabled))) {
steal = paravirt_steal_clock(cpu_of(rq));
steal -= rq->prev_steal_time_rq;
if (unlikely(steal > delta))
steal = delta;
rq->prev_steal_time_rq += steal;
delta -= steal;
}So if (steal > delta) then steal is capped to delta, preventing the final delta from going negative.
Quoted 11 lines
Quoted 8 lines
The scheduler can then charge the process for "time" nanoseconds of time. This ensures that a process isn't unfairly penalised if the host doesn't schedule the VCPU while it is supposed to be running.
The race is very small in comparison to the time the process is running, and in the worst case just means the process is charged slightly more (or less) than it should be.
At this point, what I don’t understand is why the race would be “very small” or why you would only be charged “slightly” more or less?
The window between measuring the time using CNTVCT_EL0 and getting the stolen time from the hypervisor is pretty short. The amount of time that is (normally) stolen in one go is also small. So the race is unlikely and the error when it occurs is (usually) small.
Long events (such as migration or pausing the guest) are not considered "stolen time" and should be reflected to the guest in other ways.
Quoted 8 lines
Quoted 7 lines
I guess if you're really worried about it, you could do a dance like:
do {
before = stolen
timer =CNTVCT_EL0
after = stolen
} while (before != after);
Quoted 4 lines
That will work as long as nothing in that loop requires something that would cause
stolento jump. If there is such a guarantee, then that’s even efficient, because in most cases the loop would only run once, at the cost of one extra read and one test.
Note that other architectures don't have such loops, so arm64 is just following the lead of existing architecture.
Quoted 11 lines
Quoted 3 lines
But I don't see the need to have such an accurate view of elapsed time that the VCPU was scheduled. And of course at the moment (without this series) the guest has no idea about time stolen by the host.
I’m certainly not arguing that exposing stolen time is a bad idea, I’m only wondering if the proposed solution is racy, and if so, if it is intentional.
If it’s indeed racy, the problem could be mitigated in a number of ways
Quoted 8 lines
a) document your loop or something similar as being the recommended way to avoid the race, and then ensure that the loop actually will always work as intended. The upside is that it’s just a change in some comments or documentation.
b) having a single interface that exposes multiple times. For example, you could have a copy of
CNTVCT_EL0written alongside stolen time, and then the scheduler could use that copy for its decision.
That would still be racy - the structure can be updated at any time (as the host could interrupt the VCPU at any time), so you would still be left with the problem of reading both atomically - which would mean going back to the loop. This is the approach that LPT takes and is documented in the spec.
Also I can't see why you would want to know the CNTVCT_EL0 value at the point the stolen time was updated, it's much more useful to know the current CNTVCT_EL0 value.
Ultimately reading the stolen time is always going to be slightly racy because you are including some of the scheduler's time in the calculation of how much time the process was running for. The pauses you describe above are instances where time has been stolen from the scheduler, but that time is being accounted for/against a user space process. While the algorithm could be changed so that it's always a positive for the user space process I'm not sure that's a benefit (it's probably better that statistically it can go either way).
Steve
Re: [PATCH 9/9] arm64: Retrieve stolen time as paravirtualized guest
In reply to Marc ZyngierQuoted 14 lines · Marc Zyngier
Quoted 11 lines · On Fri, 2 Aug 2019 15:50:17 +0100 Steven Price <[email protected]> wrote:
Enable paravirtualization features when running under a hypervisor supporting the
PV_TIME_SThypercall.For each (v)CPU, we ask the hypervisor for the location of a shared page which the hypervisor will use to report stolen time to us. We set
pv_time_opsto the stolen time function which simply reads the stolen value from the shared page for a VCPU. We guarantee single-copy atomicity usingREAD_ONCEwhich means we can also read the stolen time for another VCPU than the currently running one while it is potentially being updated by the hypervisor.
Quoted 7 lines
Quoted 4 lines
Signed-off-by Steven Price <[email protected]>--- arch/arm64/kernel/Makefile | 1 + arch/arm64/kernel/kvm.c | 155 +++++++++++++++++++++++++++++++++++++nit: Why not using
paravirt.c, which clearly states what it does? The alternative would be to name itkvm-pv.c.
I can move it to paravirt.c - seems reasonable.
Quoted 91 lines
Quoted 91 lines
include/linux/cpuhotplug.h | 1 + 3 files changed, 157 insertions(+) create mode 100644 arch/arm64/kernel/kvm.carch/arm64/kernel/Makefile +1
-0@@ -63,6 +63,7 @@ obj-$(CONFIG_CRASH_CORE) += crash_core.o obj-$(CONFIG_ARM_SDE_INTERFACE) += sdei.o obj-$(CONFIG_ARM64_SSBD) += ssbd.o obj-$(CONFIG_ARM64_PTR_AUTH) += pointer_auth.o +obj-$(CONFIG_PARAVIRT) += kvm.o obj-y += vdso/ probes/ obj-$(CONFIG_COMPAT_VDSO) += vdso32/arch/arm64/kernel/kvm.c new file +69
-0@@ -0,0 +1,155 @@ +// SPDX-License-Identifier: GPL-2.0 +// Copyright (C) 2019 Arm Ltd. + +#define pr_fmt (fmt) "kvmarm-pv: " fmt + +#include <linux/arm-smccc.h> +#include <linux/cpuhotplug.h> +#include <linux/io.h> +#include <linux/printk.h> +#include <linux/psci.h> +#include <linux/reboot.h> +#include <linux/slab.h> + +#include <asm/paravirt.h> +#include <asm/pvclock-abi.h> +#include <asm/smp_plat.h> + +struct kvmarm_stolen_time_region { +struct pvclock_vcpu_stolen_time_info *kaddr; +}; + +static DEFINE_PER_CPU(struct kvmarm_stolen_time_region, stolen_time_region); + +static bool steal_acc = true; +static int __init parse_no_stealacc (char *arg) +{ + steal_acc = false; +return 0 ; +} +early_param ("no-steal-acc" , parse_no_stealacc); + +/* return stolen time in ns by asking the hypervisor */ +static u64 kvm_steal_clock (int cpu) +{ +struct kvmarm_stolen_time_region *reg; + + reg =per_cpu_ptr (&stolen_time_region, cpu); +if (!reg->kaddr) { +pr_warn_once ("stolen time enabled but not configured for cpu %d\n" , + cpu); +return 0 ; + } + +return le64_to_cpu (READ_ONCE (reg->kaddr->stolen_time)); +} + +static int disable_stolen_time_current_cpu (void ) +{ +struct kvmarm_stolen_time_region *reg; + + reg =this_cpu_ptr (&stolen_time_region); +if (!reg->kaddr) +return 0 ; + +memunmap (reg->kaddr); +memset (reg,0 ,sizeof (*reg)); + +return 0 ; +} + +static int stolen_time_dying_cpu (unsigned int cpu) +{ +return disable_stolen_time_current_cpu (); +} + +static int init_stolen_time_cpu (unsigned int cpu) +{ +struct kvmarm_stolen_time_region *reg; +struct arm_smccc_res res;
Quoted 8 lines
arch/arm64/kernel/kvm.c line 74
Quoted 5 lines
arch/arm64/kernel/kvm.c new file +5
-0+ + reg =this_cpu_ptr (&stolen_time_region); + +if (reg->kaddr) +return 0 ;Can this actually happen? It'd take two
CPU_UPcalls from the HP notifiers to get in that situation...
Yes, something would have to be very broken for that to happen - I'll remove this check.
Quoted 7 lines
Quoted 7 lines
arch/arm64/kernel/kvm.c new file +7
-0+ +arm_smccc_1_1_invoke (ARM_SMCCC_HV_PV_TIME_ST , &res); + +if ((long )res.a0 <0 ) +return -EINVAL ; + + reg->kaddr = memremap(res.a0,
Quoted 8 lines
arch/arm64/kernel/kvm.c line 87
Quoted 6 lines
arch/arm64/kernel/kvm.c new file +6
-0+sizeof (struct pvclock_vcpu_stolen_time_info ), + MEMREMAP_WB); + +if (reg->kaddr ==NULL ) { +pr_warn ("Failed to map stolen time data structure\n" ); +return -EINVAL ;
-ENOMEMis the expected return code.
Ok
Quoted 63 lines
Quoted 63 lines
arch/arm64/kernel/kvm.c new file +63
-0+ } + +if (le32_to_cpu (reg->kaddr->revision) !=0 || +le32_to_cpu (reg->kaddr->attributes) !=0 ) { +pr_warn ("Unexpected revision or attributes in stolen time data\n" ); +return -ENXIO ; + } + +return 0 ; +} + +static int kvm_arm_init_stolen_time (void ) +{ +int ret; + + ret =cpuhp_setup_state (CPUHP_AP_ARM_KVMPV_STARTING , +"hypervisor/kvmarm/pv:starting" , + init_stolen_time_cpu, stolen_time_dying_cpu); +if (ret <0 ) +return ret; +return 0 ; +} + +static bool has_kvm_steal_clock (void ) +{ +struct arm_smccc_res res; + +/* To detect the presence of PV time support we require SMCCC 1.1+ */ +if (psci_ops.smccc_version <SMCCC_VERSION_1_1 ) +return false; + +arm_smccc_1_1_invoke (ARM_SMCCC_ARCH_FEATURES_FUNC_ID , +ARM_SMCCC_HV_PV_FEATURES , &res); + +if (res.a0 !=SMCCC_RET_SUCCESS ) +return false; + +arm_smccc_1_1_invoke (ARM_SMCCC_HV_PV_FEATURES , +ARM_SMCCC_HV_PV_TIME_ST , &res); + +if (res.a0 !=SMCCC_RET_SUCCESS ) +return false; + +return true; +} + +static int __init kvm_guest_init (void ) +{ +int ret =0 ; + +if (!has_kvm_steal_clock ()) +return 0 ; + + ret =kvm_arm_init_stolen_time (); +if (ret) +return ret; + + pv_ops.time.steal_clock = kvm_steal_clock; + +static_key_slow_inc (¶virt_steal_enabled); +if (steal_acc) +static_key_slow_inc (¶virt_steal_rq_enabled); +
Quoted 8 lines
arch/arm64/kernel/kvm.c line 155
Quoted 5 lines
arch/arm64/kernel/kvm.c new file +5
-0+pr_info ("using stolen time PV\n" ); + +return 0 ; +} +early_initcall (kvm_guest_init);Is there any reason why we wouldn't directly call into this rather than using an initcall?
I'm not sure where the direct call would go - any pointers?
Thanks,
Steve
Quoted 18 lines
include/linux/cpuhotplug.h enum cpuhp_state { · line 142
Quoted 12 lines
include/linux/cpuhotplug.h +1
-0@@ -136,6 +136,7 @@ enum cpuhp_state {/* Must be the last timer callback */ CPUHP_AP_DUMMY_TIMER_STARTING ,CPUHP_AP_ARM_XEN_STARTING , +CPUHP_AP_ARM_KVMPV_STARTING ,CPUHP_AP_ARM_CORESIGHT_STARTING ,CPUHP_AP_ARM64_ISNDEP_STARTING , CPUHP_AP_SMPCFD_DYING,Thanks,
M.
Marc Zyngier
maintainerRe: [PATCH 9/9] arm64: Retrieve stolen time as paravirtualized guest
Quoted 19 lines · Steven Price
Quoted 18 lines · Marc Zyngier
Quoted 15 lines · On Fri, 2 Aug 2019 15:50:17 +0100 Steven Price <[email protected]> wrote:
Enable paravirtualization features when running under a hypervisor supporting the
PV_TIME_SThypercall.For each (v)CPU, we ask the hypervisor for the location of a shared page which the hypervisor will use to report stolen time to us. We set
pv_time_opsto the stolen time function which simply reads the stolen value from the shared page for a VCPU. We guarantee single-copy atomicity usingREAD_ONCEwhich means we can also read the stolen time for another VCPU than the currently running one while it is potentially being updated by the hypervisor.Signed-off-by Steven Price <[email protected]>--- arch/arm64/kernel/Makefile | 1 + arch/arm64/kernel/kvm.c | 155 +++++++++++++++++++++++++++++++++++++
[...]
Quoted 26 lines
Quoted 17 lines
Quoted 17 lines
arch/arm64/kernel/kvm.c new file +17
-0+static int __init kvm_guest_init (void ) +{ +int ret =0 ; + +if (!has_kvm_steal_clock ()) +return 0 ; + + ret =kvm_arm_init_stolen_time (); +if (ret) +return ret; + + pv_ops.time.steal_clock = kvm_steal_clock; + +static_key_slow_inc (¶virt_steal_enabled); +if (steal_acc) +static_key_slow_inc (¶virt_steal_rq_enabled); +Quoted 8 lines
arch/arm64/kernel/kvm.c line 155
Quoted 5 lines
arch/arm64/kernel/kvm.c new file +5
-0+pr_info ("using stolen time PV\n" ); + +return 0 ; +} +early_initcall (kvm_guest_init);Is there any reason why we wouldn't directly call into this rather than using an initcall?
Quoted 1 line
I'm not sure where the direct call would go - any pointers?
I'd be temped to say arch/arm64/kernel/time.c:time_init(), provided that there is no issue with the CPU hotplug lock (I remember hitting that a while ago).
M.
Signature · 1 line
Jazz is not dead, it just smells funny...
Zenghui Yu
reviewerRe: [PATCH 9/9] arm64: Retrieve stolen time as paravirtualized guest
In reply to Steven PriceQuoted 106 lines · Steven Price
Enable paravirtualization features when running under a hypervisor supporting the
PV_TIME_SThypercall.For each (v)CPU, we ask the hypervisor for the location of a shared page which the hypervisor will use to report stolen time to us. We set
pv_time_opsto the stolen time function which simply reads the stolen value from the shared page for a VCPU. We guarantee single-copy atomicity usingREAD_ONCEwhich means we can also read the stolen time for another VCPU than the currently running one while it is potentially being updated by the hypervisor.Signed-off-by Steven Price <[email protected]>--- arch/arm64/kernel/Makefile | 1 + arch/arm64/kernel/kvm.c | 155 +++++++++++++++++++++++++++++++++++++ include/linux/cpuhotplug.h | 1 + 3 files changed, 157 insertions(+)create mode 100644
arch/arm64/kernel/kvm.carch/arm64/kernel/Makefile +1
-0@@ -63,6 +63,7 @@ obj-$(CONFIG_CRASH_CORE) += crash_core.o obj-$(CONFIG_ARM_SDE_INTERFACE) += sdei.o obj-$(CONFIG_ARM64_SSBD) += ssbd.o obj-$(CONFIG_ARM64_PTR_AUTH) += pointer_auth.o +obj-$(CONFIG_PARAVIRT) += kvm.o obj-y += vdso/ probes/ obj-$(CONFIG_COMPAT_VDSO) += vdso32/arch/arm64/kernel/kvm.c new file +69
-0@@ -0,0 +1,155 @@ +// SPDX-License-Identifier: GPL-2.0 +// Copyright (C) 2019 Arm Ltd. + +#define pr_fmt (fmt) "kvmarm-pv: " fmt + +#include <linux/arm-smccc.h> +#include <linux/cpuhotplug.h> +#include <linux/io.h> +#include <linux/printk.h> +#include <linux/psci.h> +#include <linux/reboot.h> +#include <linux/slab.h> + +#include <asm/paravirt.h> +#include <asm/pvclock-abi.h> +#include <asm/smp_plat.h> + +struct kvmarm_stolen_time_region { +struct pvclock_vcpu_stolen_time_info *kaddr; +}; + +static DEFINE_PER_CPU(struct kvmarm_stolen_time_region, stolen_time_region); + +static bool steal_acc = true; +static int __init parse_no_stealacc (char *arg) +{ + steal_acc = false; +return 0 ; +} +early_param ("no-steal-acc" , parse_no_stealacc); + +/* return stolen time in ns by asking the hypervisor */ +static u64 kvm_steal_clock (int cpu) +{ +struct kvmarm_stolen_time_region *reg; + + reg =per_cpu_ptr (&stolen_time_region, cpu); +if (!reg->kaddr) { +pr_warn_once ("stolen time enabled but not configured for cpu %d\n" , + cpu); +return 0 ; + } + +return le64_to_cpu (READ_ONCE (reg->kaddr->stolen_time)); +} + +static int disable_stolen_time_current_cpu (void ) +{ +struct kvmarm_stolen_time_region *reg; + + reg =this_cpu_ptr (&stolen_time_region); +if (!reg->kaddr) +return 0 ; + +memunmap (reg->kaddr); +memset (reg,0 ,sizeof (*reg)); + +return 0 ; +} + +static int stolen_time_dying_cpu (unsigned int cpu) +{ +return disable_stolen_time_current_cpu (); +} + +static int init_stolen_time_cpu (unsigned int cpu) +{ +struct kvmarm_stolen_time_region *reg; +struct arm_smccc_res res;
arch/arm64/kernel/kvm.c line 79
Quoted 10 lines
arch/arm64/kernel/kvm.c new file +10
-0+ + reg =this_cpu_ptr (&stolen_time_region); + +if (reg->kaddr) +return 0 ; + +arm_smccc_1_1_invoke (ARM_SMCCC_HV_PV_TIME_ST , &res); + +if ((long )res.a0 <0 ) +return -EINVAL ;
Hi Steven,
Since userspace is not involved yet (right?), no one will create the PV_TIME device for guest (and no one will specify the IPA of the shared stolen time region), and I guess we will get a "not supported" error here.
So what should we do if we want to test this series now? Any userspace tools? If no, do you have any plans for userspace developing? ;-)
Thanks,
zenghui
Quoted 89 lines
arch/arm64/kernel/kvm.c new file +76
-0+ + reg->kaddr =memremap (res.a0, +sizeof (struct pvclock_vcpu_stolen_time_info ), +MEMREMAP_WB ); + +if (reg->kaddr ==NULL ) { +pr_warn ("Failed to map stolen time data structure\n" ); +return -EINVAL ; + } + +if (le32_to_cpu (reg->kaddr->revision) !=0 || +le32_to_cpu (reg->kaddr->attributes) !=0 ) { +pr_warn ("Unexpected revision or attributes in stolen time data\n" ); +return -ENXIO ; + } + +return 0 ; +} + +static int kvm_arm_init_stolen_time (void ) +{ +int ret; + + ret =cpuhp_setup_state (CPUHP_AP_ARM_KVMPV_STARTING , +"hypervisor/kvmarm/pv:starting" , + init_stolen_time_cpu, stolen_time_dying_cpu); +if (ret <0 ) +return ret; +return 0 ; +} + +static bool has_kvm_steal_clock (void ) +{ +struct arm_smccc_res res; + +/* To detect the presence of PV time support we require SMCCC 1.1+ */ +if (psci_ops.smccc_version <SMCCC_VERSION_1_1 ) +return false; + +arm_smccc_1_1_invoke (ARM_SMCCC_ARCH_FEATURES_FUNC_ID , +ARM_SMCCC_HV_PV_FEATURES , &res); + +if (res.a0 !=SMCCC_RET_SUCCESS ) +return false; + +arm_smccc_1_1_invoke (ARM_SMCCC_HV_PV_FEATURES , +ARM_SMCCC_HV_PV_TIME_ST , &res); + +if (res.a0 !=SMCCC_RET_SUCCESS ) +return false; + +return true; +} + +static int __init kvm_guest_init (void ) +{ +int ret =0 ; + +if (!has_kvm_steal_clock ()) +return 0 ; + + ret =kvm_arm_init_stolen_time (); +if (ret) +return ret; + + pv_ops.time.steal_clock = kvm_steal_clock; + +static_key_slow_inc (¶virt_steal_enabled); +if (steal_acc) +static_key_slow_inc (¶virt_steal_rq_enabled); + +pr_info ("using stolen time PV\n" ); + +return 0 ; +} +early_initcall (kvm_guest_init);include/linux/cpuhotplug.h +1
-0@@ -136,6 +136,7 @@ enum cpuhp_state {/* Must be the last timer callback */ CPUHP_AP_DUMMY_TIMER_STARTING ,CPUHP_AP_ARM_XEN_STARTING , +CPUHP_AP_ARM_KVMPV_STARTING ,CPUHP_AP_ARM_CORESIGHT_STARTING ,CPUHP_AP_ARM64_ISNDEP_STARTING , CPUHP_AP_SMPCFD_DYING,
Re: [PATCH 9/9] arm64: Retrieve stolen time as paravirtualized guest
On 09/08/2019 14:51, Zenghui Yu wrote:
[...]
Quoted 9 lines
Hi Steven,
Since userspace is not involved yet (right?), no one will create the
PV_TIMEdevice for guest (and no one will specify the IPA of the shared stolen time region), and I guess we will get a "not supported" error here.So what should we do if we want to test this series now? Any userspace tools? If no, do you have any plans for userspace developing? ;-)
At the moment I have the following patch to kvmtool which creates the PV_TIME device - this isn't in a state to go upstream, and Marc has asked that I rework the memory allocation, so this will need to change.
It's a little ugly as it simply reserves the first page of RAM to use for the PV time structures.
----8<----
Makefile +1 -1
@@ -158,7 +158,7 @@ endif # ARM OBJS_ARM_COMMON := arm/fdt.o arm/gic.o arm/gicv2m.o arm/ioport.o \ arm/kvm.o arm/kvm-cpu.o arm/pci.o arm/timer.o \ - arm/pmu.o + arm/pmu.o arm/pvtime.o HDRS_ARM_COMMON := arm/include ifeq ($(ARCH), arm) DEFINES += -DCONFIG_ARM
arm/fdt.c +1 -0
@@ -119,6 +119,7 @@ static int setup_fdt(struct kvm *kvm)/* Create new tree without a reserve map */ _FDT (fdt_create (fdt,FDT_MAX_SIZE )); +_FDT (fdt_add_reservemap_entry (fdt, kvm->arch.memory_guest_start,4096 ));_FDT (fdt_finish_reservemap (fdt));/* Header */
arm/kvm.c +6 -0
@@ -11,6 +11,8 @@#include <linux/kvm.h> #include <linux/sizes.h> +int pvtime_create (struct kvm *kvm); +struct kvm_ext kvm_req_ext[] = { {DEFINE_KVM_EXT (KVM_CAP_IRQCHIP ) }, {DEFINE_KVM_EXT (KVM_CAP_ONE_REG ) },
@@ -86,6 +88,10 @@ void kvm__arch_init(struct kvm *kvm, const char *hugetlbfs_path, u64 ram_size)/* Create the virtual GIC. */ if (gic__create (kvm, kvm->cfg.arch.irqchip))die ("Failed to create virtual GIC" ); + +/* Setup PV time */ +if (pvtime_create (kvm)) +die ("Failed to initialise PV time" ); }#define FDT_ALIGN SZ_2M
arm/pvtime.c new file +77 -0
77 lines not on this page · raw message
Zenghui Yu
reviewerRe: [PATCH 9/9] arm64: Retrieve stolen time as paravirtualized guest
Quoted 12 lines · Steven Price
On 09/08/2019 14:51, Zenghui Yu wrote:
[...]Quoted 9 lines
Hi Steven,
Since userspace is not involved yet (right?), no one will create the
PV_TIMEdevice for guest (and no one will specify the IPA of the shared stolen time region), and I guess we will get a "not supported" error here.So what should we do if we want to test this series now? Any userspace tools? If no, do you have any plans for userspace developing? ;-)
Quoted 6 lines
At the moment I have the following patch to kvmtool which creates the
PV_TIMEdevice - this isn't in a state to go upstream, and Marc has asked that I rework the memory allocation, so this will need to change.It's a little ugly as it simply reserves the first page of RAM to use for the PV time structures.
Thanks for sharing the code. It's good enough to show what is required in user-space.
(I'm not familiar with kvmtool. I will first take some time to move the steal time part to Qemu and see what will happen.)
Thanks,
zenghui
Quoted 123 lines
----8<----
Makefile +1
-1@@ -158,7 +158,7 @@ endif # ARM OBJS_ARM_COMMON := arm/fdt.o arm/gic.o arm/gicv2m.o arm/ioport.o \ arm/kvm.o arm/kvm-cpu.o arm/pci.o arm/timer.o \ - arm/pmu.o + arm/pmu.o arm/pvtime.o HDRS_ARM_COMMON := arm/include ifeq ($(ARCH), arm) DEFINES += -DCONFIG_ARMarm/fdt.c +1
-0@@ -119,6 +119,7 @@ static int setup_fdt(struct kvm *kvm)/* Create new tree without a reserve map */ _FDT (fdt_create (fdt,FDT_MAX_SIZE )); +_FDT (fdt_add_reservemap_entry (fdt, kvm->arch.memory_guest_start,4096 ));_FDT (fdt_finish_reservemap (fdt));/* Header */ arm/kvm.c +6
-0@@ -11,6 +11,8 @@#include <linux/kvm.h> #include <linux/sizes.h> +int pvtime_create (struct kvm *kvm); +struct kvm_ext kvm_req_ext[] = { {DEFINE_KVM_EXT (KVM_CAP_IRQCHIP ) }, {DEFINE_KVM_EXT (KVM_CAP_ONE_REG ) },@@ -86,6 +88,10 @@ void kvm__arch_init(struct kvm *kvm, const char *hugetlbfs_path, u64 ram_size)/* Create the virtual GIC. */ if (gic__create (kvm, kvm->cfg.arch.irqchip))die ("Failed to create virtual GIC" ); + +/* Setup PV time */ +if (pvtime_create (kvm)) +die ("Failed to initialise PV time" ); }#define FDT_ALIGN SZ_2Marm/pvtime.c new file +67
-0@@ -0,0 +1,77 @@ +#include "kvm/kvm.h" + +#define KVM_DEV_TYPE_ARM_PV_TIME (KVM_DEV_TYPE_ARM_VGIC_ITS+2) + +/* Device Control API: PV_TIME */ +#define KVM_DEV_ARM_PV_TIME_PADDR 0 +#define KVM_DEV_ARM_PV_TIME_FREQUENCY 3 + +#define KVM_DEV_ARM_PV_TIME_ST 0 +#define KVM_DEV_ARM_PV_TIME_LPT 1 + +static int pvtime_fd; + +int pvtime_create (struct kvm *kvm); + +int pvtime_create (struct kvm *kvm) +{ +int err; +u64 lpt_paddr =0x10000000 ; +u64 st_paddr = lpt_paddr +4096 ; +u32 frequency =100 *1000 *1000 ; + +printf ("lpt_paddr=%llx\n" , lpt_paddr); + +struct kvm_create_device pvtime_device = { + .type =KVM_DEV_TYPE_ARM_PV_TIME , + .flags =0 , + }; + + err =ioctl (kvm->vm_fd,KVM_CREATE_DEVICE , &pvtime_device); +if (err) { +printf ("Failed to create PV device\n" ); +return 0 ; + } + + pvtime_fd = pvtime_device.fd; + +struct kvm_device_attr lpt_base = { + .group =KVM_DEV_ARM_PV_TIME_PADDR , + .attr =KVM_DEV_ARM_PV_TIME_LPT , + .addr = (u64 )(unsigned long )&lpt_paddr + }; +struct kvm_device_attr st_base = { + .group =KVM_DEV_ARM_PV_TIME_PADDR , + .attr =KVM_DEV_ARM_PV_TIME_ST , + .addr = (u64 )(unsigned long )&st_paddr + }; + +struct kvm_device_attr lpt_freq = { + .group =KVM_DEV_ARM_PV_TIME_FREQUENCY , + .attr =KVM_DEV_ARM_PV_TIME_LPT , + .addr = (u64 )(unsigned long )&frequency + }; + + err =ioctl (pvtime_fd,KVM_SET_DEVICE_ATTR , &lpt_base); +if (err) { +perror ("ioctl lpt_base failed" ); +printf ("Ignoring LPT...\n" ); + } + err =ioctl (pvtime_fd,KVM_SET_DEVICE_ATTR , &st_base); +if (err) { +perror ("ioctl st_base failed" ); + goto out_err; + } + err =ioctl (pvtime_fd,KVM_SET_DEVICE_ATTR , &lpt_freq); +if (err) { +perror ("ioctl lpt_freq failed" );
arm/pvtime.c line 77
Quoted 10 lines
arm/pvtime.c new file +10
-0+printf ("Ignoring LPT...\n" ); + } + +printf ("PV time setup\n" ); + +return 0 ; +out_err: +close (pvtime_fd); +return err; +}
_______________________________________________
kvmarm mailing list
[email protected]
https://lists.cs.columbia.edu/mailman/listinfo/kvmarm
Quoted 39 lines · Steven Price
Quoted 24 lines · Marc Zyngier
On Fri, 2 Aug 2019 15:50:08 +0100
Steven Price <[email protected]> wrote:Hi Steven,
Quoted 5 lines
This series add support for paravirtualized time for arm64 guests and KVM hosts following the specification in Arm's document DEN 0057A:
Quoted 7 lines
It implements support for stolen time, allowing the guest to identify time when it is forcibly not executing.
It doesn't implement support for Live Physical Time (LPT) as there are some concerns about the overheads and approach in the above specification, and I expect an updated version of the specification to be released soon with just the stolen time parts.
Thanks for posting this.
My current concern with this series is around the fact that we allocate memory from the kernel on behalf of the guest. It is the first example of such thing in the ARM port, and I can't really say I'm fond of it.
Quoted 5 lines
x86 seems to get away with it by having the memory allocated from userspace, why I tend to like more. Yes,
put_useris more expensive than a straight store, but this isn't done too often either.What is the rational for your current approach?
As I see it there are 3 approaches that can be taken here:
1. Hypervisor allocates memory and adds it to the virtual machine. This means that everything to do with the 'device' is encapsulated behind the
KVM_CREATE_DEVICE/ KVM_[GS]ET_DEVICE_ATTR ioctls. But since we want the stolen time structure to be fast it cannot be a trapping region and has to be backed by real memory - in this case allocated by the host kernel.
Quoted 5 lines
2. Host user space allocates memory. Similar to above, but this time user space needs to manage the memory region as well as the usual
KVM_CREATE_DEVICEdance. I've no objection to this, but it means kvmtool/QEMU needs to be much more aware of what is going on (e.g. how to size the memory region).
You ideally want to get the host overhead for a VM to as little as you can. I'm not terribly fond of the idea of reserving a full page just because we're too afraid of having the guest donate memory.
Quoted 8 lines
3. Guest kernel "donates" the memory to the hypervisor for the structure. As far as I'm aware this is what x86 does. The problems I see this approach are:
a) kexec becomes much more tricky - there needs to be a disabling
mechanism for the guest to stop the hypervisor scribbling on memory before starting the new kernel.
I wouldn't call "quiesce a device" much more tricky. We have to do that for other devices as well today.
Quoted 4 lines
b) If there is more than one entity that is interested in the
information (e.g. firmware and kernel) then this requires some form of arbitration in the guest because the hypervisor doesn't want to have to track an arbitrary number of regions to update.
Why would FW care?
Quoted 2 lines
c) Performance can suffer if the host kernel doesn't have a suitably
aligned/sized area to use. As you say -put_user()is more expensive.
Just define the interface to always require natural alignment when donating a memory location?
Quoted 1 line
The structure is updated on every return to the VM.
If you really do suffer from put_user(), there are alternatives. You could just map the page on the registration hcall and then leave it pinned until the vcpu gets destroyed again.
Quoted 3 lines
Of course x86 does prove the third approach can work, but I'm not sure which is actually better. Avoid the kexec cancellation requirements was the main driver of the current approach. Although many of the
I really don't understand the problem with kexec cancellation. Worst case, let guest FW set it up for you and propagate only the address down via ACPI/DT. That way you can mark the respective memory as reserved too.
But even with a Linux only mechanism, just take a look at arch/x86/kernel/kvmclock.c. All they do to remove the map is to hook into machine_crash_shutdown() and machine_shutdown().
Alex
Quoted 9 lines
conversations about this were also tied up with Live Physical Time which adds its own complications.
Steve
_______________________________________________
kvmarm mailing list
[email protected]
https://lists.cs.columbia.edu/mailman/listinfo/kvmarm
Marc Zyngier
maintainerQuoted 49 lines · Alexander Graf
Quoted 39 lines · Steven Price
Quoted 24 lines · Marc Zyngier
On Fri, 2 Aug 2019 15:50:08 +0100
Steven Price <[email protected]> wrote:Hi Steven,
Quoted 5 lines
This series add support for paravirtualized time for arm64 guests and KVM hosts following the specification in Arm's document DEN 0057A:
Quoted 7 lines
It implements support for stolen time, allowing the guest to identify time when it is forcibly not executing.
It doesn't implement support for Live Physical Time (LPT) as there are some concerns about the overheads and approach in the above specification, and I expect an updated version of the specification to be released soon with just the stolen time parts.
Thanks for posting this.
My current concern with this series is around the fact that we allocate memory from the kernel on behalf of the guest. It is the first example of such thing in the ARM port, and I can't really say I'm fond of it.
Quoted 5 lines
x86 seems to get away with it by having the memory allocated from userspace, why I tend to like more. Yes,
put_useris more expensive than a straight store, but this isn't done too often either.What is the rational for your current approach?
As I see it there are 3 approaches that can be taken here:
1. Hypervisor allocates memory and adds it to the virtual machine. This means that everything to do with the 'device' is encapsulated behind the
KVM_CREATE_DEVICE/ KVM_[GS]ET_DEVICE_ATTR ioctls. But since we want the stolen time structure to be fast it cannot be a trapping region and has to be backed by real memory - in this case allocated by the host kernel.Quoted 5 lines
2. Host user space allocates memory. Similar to above, but this time user space needs to manage the memory region as well as the usual
KVM_CREATE_DEVICEdance. I've no objection to this, but it means kvmtool/QEMU needs to be much more aware of what is going on (e.g. how to size the memory region).
Quoted 3 lines
You ideally want to get the host overhead for a VM to as little as you can. I'm not terribly fond of the idea of reserving a full page just because we're too afraid of having the guest donate memory.
Well, reduce the amount of memory you give to the guest by one page, and allocate that page to the stolen time device. Problem solved!
Seriously, if you're worried about the allocation of a single page, you should first look at how many holes we have in the vcpu structure, for example (even better, with the 8.4 NV patches applied). Just fixing that would give you that page back *per vcpu*.
Quoted 10 lines
Quoted 7 lines
3. Guest kernel "donates" the memory to the hypervisor for the structure. As far as I'm aware this is what x86 does. The problems I see this approach are:
a) kexec becomes much more tricky - there needs to be a disabling
mechanism for the guest to stop the hypervisor scribbling on memory before starting the new kernel.I wouldn't call "quiesce a device" much more tricky. We have to do that for other devices as well today.
And since there is no standard way of doing it, we keep inventing weird and wonderful ways of doing so -- cue the terrible GICv3 LPI situation, and all the various hacks to keep existing IOMMU mappings around across firmware/kernel handovers as well as kexec.
Quoted 7 lines
Quoted 5 lines
b) If there is more than one entity that is interested in the
information (e.g. firmware and kernel) then this requires some form of arbitration in the guest because the hypervisor doesn't want to have to track an arbitrary number of regions to update.Why would FW care?
Exactly. It doesn't care. Not caring means it doesn't know about the page the guest has allocated for stolen time, and starts using it for its own purposes. Hello, memory corruption. Same thing goes if you reboot into a non stolen time aware kernel.
Quoted 4 lines
Quoted 3 lines
c) Performance can suffer if the host kernel doesn't have a suitably
aligned/sized area to use. As you say -put_user()is more expensive.
Quoted 8 lines
Just define the interface to always require natural alignment when donating a memory location?
Quoted 1 line
The structure is updated on every return to the VM.
If you really do suffer from
put_user(), there are alternatives. You could just map the page on the registration hcall and then leave it pinned until the vcpu gets destroyed again.
put_user() should be cheap enough. It is one of the things we tend to optimise anyway. And yes, worse case, we pin the page.
Quoted 9 lines
Quoted 4 lines
Of course x86 does prove the third approach can work, but I'm not sure which is actually better. Avoid the kexec cancellation requirements was the main driver of the current approach. Although many of the
I really don't understand the problem with kexec cancellation. Worst case, let guest FW set it up for you and propagate only the address down via ACPI/DT. That way you can mark the respective memory as reserved too.
We already went down that road with the LPI hack. I'm not going there again if we can avoid it. And it turn out that we can. Just allocate the stolen time page as a separate memblock, give it to KVM for that purpose.
Your suggestion of letting the guest firmware set something up only works if whatever you're booting after that understands it. If it doesn't, you're screwed.
Quoted 3 lines
But even with a Linux only mechanism, just take a look at
arch/x86/kernel/kvmclock.c. All they do to remove the map is to hook intomachine_crash_shutdown()andmachine_shutdown().
I'm not going to take something that is Linux specific. It has to work for all guests, at all times, whether they know about the hypervisor service or not.
M.
Quoted 13 lines
Alex
Quoted 9 lines
conversations about this were also tied up with Live Physical Time which adds its own complications.
Steve
_______________________________________________
kvmarm mailing list
[email protected]
https://lists.cs.columbia.edu/mailman/listinfo/kvmarm
Signature · 5 lines
Jazz is not dead, it just smells funny. _______________________________________________ kvmarm mailing list [email protected] https://lists.cs.columbia.edu/mailman/listinfo/kvmarm
Quoted 55 lines · Marc Zyngier
Quoted 49 lines · Alexander Graf
Quoted 39 lines · Steven Price
Quoted 24 lines · Marc Zyngier
On Fri, 2 Aug 2019 15:50:08 +0100
Steven Price <[email protected]> wrote:Hi Steven,
Quoted 5 lines
This series add support for paravirtualized time for arm64 guests and KVM hosts following the specification in Arm's document DEN 0057A:
Quoted 7 lines
It implements support for stolen time, allowing the guest to identify time when it is forcibly not executing.
It doesn't implement support for Live Physical Time (LPT) as there are some concerns about the overheads and approach in the above specification, and I expect an updated version of the specification to be released soon with just the stolen time parts.
Thanks for posting this.
My current concern with this series is around the fact that we allocate memory from the kernel on behalf of the guest. It is the first example of such thing in the ARM port, and I can't really say I'm fond of it.
Quoted 5 lines
x86 seems to get away with it by having the memory allocated from userspace, why I tend to like more. Yes,
put_useris more expensive than a straight store, but this isn't done too often either.What is the rational for your current approach?
As I see it there are 3 approaches that can be taken here:
1. Hypervisor allocates memory and adds it to the virtual machine. This means that everything to do with the 'device' is encapsulated behind the
KVM_CREATE_DEVICE/ KVM_[GS]ET_DEVICE_ATTR ioctls. But since we want the stolen time structure to be fast it cannot be a trapping region and has to be backed by real memory - in this case allocated by the host kernel.Quoted 5 lines
2. Host user space allocates memory. Similar to above, but this time user space needs to manage the memory region as well as the usual
KVM_CREATE_DEVICEdance. I've no objection to this, but it means kvmtool/QEMU needs to be much more aware of what is going on (e.g. how to size the memory region).Quoted 3 lines
You ideally want to get the host overhead for a VM to as little as you can. I'm not terribly fond of the idea of reserving a full page just because we're too afraid of having the guest donate memory.
Quoted 7 lines
Well, reduce the amount of memory you give to the guest by one page, and allocate that page to the stolen time device. Problem solved!
Seriously, if you're worried about the allocation of a single page, you should first look at how many holes we have in the vcpu structure, for example (even better, with the 8.4 NV patches applied). Just fixing that would give you that page back *per vcpu*.
I'm worried about additional memory slots, about fragmenting the cachable guest memory regions, about avoidable HV taxes.
I think we need to distinguish here between the KVM implementation and the hypervisor/guest interface. Just because in KVM we can save overhead today doesn't mean that the HV interface should be built around the assumption that "memory is free".
Quoted 9 lines
Quoted 9 lines
Quoted 8 lines
3. Guest kernel "donates" the memory to the hypervisor for the structure. As far as I'm aware this is what x86 does. The problems I see this approach are:
a) kexec becomes much more tricky - there needs to be a disabling
mechanism for the guest to stop the hypervisor scribbling on memory before starting the new kernel.
Quoted 7 lines
Quoted 2 lines
I wouldn't call "quiesce a device" much more tricky. We have to do that for other devices as well today.
And since there is no standard way of doing it, we keep inventing weird and wonderful ways of doing so -- cue the terrible GICv3 LPI situation, and all the various hacks to keep existing IOMMU mappings around across
firmware/kernelhandovers as well as kexec.
Well, the good news here is that we don't have to keep it around ;).
Quoted 7 lines
Quoted 7 lines
Quoted 6 lines
b) If there is more than one entity that is interested in the
information (e.g. firmware and kernel) then this requires some form of arbitration in the guest because the hypervisor doesn't want to have to track an arbitrary number of regions to update.
Quoted 6 lines
Quoted 1 line
Why would FW care?
Exactly. It doesn't care. Not caring means it doesn't know about the page the guest has allocated for stolen time, and starts using it for its own purposes. Hello, memory corruption. Same thing goes if you reboot into a non stolen time aware kernel.
If you reboot, you go via the vcpu reset path which clears the map, no? Same goes for FW entry. If you enter firmware that does not set up the map, you never see it.
Quoted 27 lines
Quoted 5 lines
Quoted 4 lines
c) Performance can suffer if the host kernel doesn't have a suitably
aligned/sized area to use. As you say -put_user()is more expensive.Quoted 8 lines
Just define the interface to always require natural alignment when donating a memory location?
Quoted 1 line
The structure is updated on every return to the VM.
If you really do suffer from
put_user(), there are alternatives. You could just map the page on the registration hcall and then leave it pinned until the vcpu gets destroyed again.
put_user()should be cheap enough. It is one of the things we tend to optimise anyway. And yes, worse case, we pin the page.Quoted 9 lines
Quoted 4 lines
Of course x86 does prove the third approach can work, but I'm not sure which is actually better. Avoid the kexec cancellation requirements was the main driver of the current approach. Although many of the
I really don't understand the problem with kexec cancellation. Worst case, let guest FW set it up for you and propagate only the address down via ACPI/DT. That way you can mark the respective memory as reserved too.
Quoted 8 lines
We already went down that road with the LPI hack. I'm not going there again if we can avoid it. And it turn out that we can. Just allocate the stolen time page as a separate memblock, give it to KVM for that purpose.
Your suggestion of letting the guest firmware set something up only works if whatever you're booting after that understands it. If it doesn't, you're screwed.
Why? For UEFI, mark the region as reserved in the memory map. For DT, just mark it straight on reserved.
That said, I'm not advocating for doing it in the FW. I think this can be solved really easily with a simple guest driver to enable and a vcpu reset hook to disable the map.
Quoted 8 lines
Quoted 4 lines
But even with a Linux only mechanism, just take a look at
arch/x86/kernel/kvmclock.c. All they do to remove the map is to hook intomachine_crash_shutdown()andmachine_shutdown().I'm not going to take something that is Linux specific. It has to work for all guests, at all times, whether they know about the hypervisor service or not.
If they don't know about the HV service, they don't register the writer, so they don't see corruption.
If they know about the HV service and they don't support kexec, they don't have to worry because a vcpu reset should also clear the map.
If they do support kexec, they already have a mechanism to quiesce devices.
So I don't understand how this is Linux specific? The question was Linux specific, so I answered with precedence to show that disabling on kexec is not all that hard :).
Alex
Quoted 72 lines · Alexander Graf
Quoted 61 lines · Marc Zyngier
Quoted 55 lines · Alexander Graf
Quoted 45 lines · Steven Price
Quoted 27 lines · Marc Zyngier
On Fri, 2 Aug 2019 15:50:08 +0100
Steven Price <[email protected]> wrote:Hi Steven,
Quoted 8 lines
This series add support for paravirtualized time for arm64 guests and KVM hosts following the specification in Arm's document DEN 0057A:
https://developer.arm.com/docs/den0057/a
It implements support for stolen time, allowing the guest to identify time when it is forcibly not executing.
Quoted 6 lines
It doesn't implement support for Live Physical Time (LPT) as there are
some concerns about the overheads and approach in the above specification, and I expect an updated version of the specification to
be released soon with just the stolen time parts.Thanks for posting this.
My current concern with this series is around the fact that we allocate
memory from the kernel on behalf of the guest. It is the first example of such thing in the ARM port, and I can't really say I'm fond of it.Quoted 5 lines
x86 seems to get away with it by having the memory allocated from userspace, why I tend to like more. Yes,
put_useris more expensive than a straight store, but this isn't done too often either.What is the rational for your current approach?
As I see it there are 3 approaches that can be taken here:
1. Hypervisor allocates memory and adds it to the virtual machine. This means that everything to do with the 'device' is encapsulated behind the
KVM_CREATE_DEVICE/ KVM_[GS]ET_DEVICE_ATTR ioctls. But since we want the
stolen time structure to be fast it cannot be a trapping region and has to be backed by real memory - in this case allocated by the host kernel.Quoted 5 lines
2. Host user space allocates memory. Similar to above, but this time user space needs to manage the memory region as well as the usual
KVM_CREATE_DEVICEdance. I've no objection to this, but it means kvmtool/QEMU needs to be much more aware of what is going on (e.g. how to size the memory region).Quoted 3 lines
You ideally want to get the host overhead for a VM to as little as you can. I'm not terribly fond of the idea of reserving a full page just because we're too afraid of having the guest donate memory.
Quoted 7 lines
Well, reduce the amount of memory you give to the guest by one page, and allocate that page to the stolen time device. Problem solved!
Seriously, if you're worried about the allocation of a single page, you should first look at how many holes we have in the vcpu structure, for example (even better, with the 8.4 NV patches applied). Just fixing that would give you that page back *per vcpu*.
Quoted 7 lines
I'm worried about additional memory slots, about fragmenting the cachable guest memory regions, about avoidable HV taxes.
I think we need to distinguish here between the KVM implementation and the hypervisor/guest interface. Just because in KVM we can save overhead today doesn't mean that the HV interface should be built around the assumption that "memory is free".
The HV interface just requires that the host provides some memory for the structures to live in. The memory can be adjacent (or even within) the normal memory of the guest. The only requirement is that the guest isn't told to use this memory for normal allocations (i.e. it should either be explicitly reserved or just not contained within the normal memory block).
Quoted 29 lines
Quoted 10 lines
Quoted 10 lines
Quoted 9 lines
3. Guest kernel "donates" the memory to the hypervisor for the structure. As far as I'm aware this is what x86 does. The problems I see
this approach are:a) kexec becomes much more tricky - there needs to be a disabling mechanism for the guest to stop the hypervisor scribbling on memory before starting the new kernel.
Quoted 7 lines
Quoted 2 lines
I wouldn't call "quiesce a device" much more tricky. We have to do that for other devices as well today.
And since there is no standard way of doing it, we keep inventing weird and wonderful ways of doing so -- cue the terrible GICv3 LPI situation, and all the various hacks to keep existing IOMMU mappings around across
firmware/kernelhandovers as well as kexec.Well, the good news here is that we don't have to keep it around ;).
Quoted 9 lines
Quoted 8 lines
Quoted 6 lines
b) If there is more than one entity that is interested in the information (e.g. firmware and kernel) then this requires some form of arbitration in the guest because the hypervisor doesn't want to have to track an arbitrary number of regions to update.
Why would FW care?
Quoted 8 lines
Quoted 4 lines
Exactly. It doesn't care. Not caring means it doesn't know about the page the guest has allocated for stolen time, and starts using it for its own purposes. Hello, memory corruption. Same thing goes if you reboot into a non stolen time aware kernel.
If you reboot, you go via the vcpu reset path which clears the map, no? Same goes for FW entry. If you enter firmware that does not set up the map, you never see it.
Doing this per-vcpu implies you are probably going to have to allocate an entire page per vcpu. Because it's entirely possible for a guest to reset an individual vcpu. Or at the least there's some messy reference counting going on here.
Having a region of memory provided by the host means the structures can be packed and there's nothing to be done in the reset path.
Quoted 55 lines
Quoted 27 lines
Quoted 5 lines
Quoted 4 lines
c) Performance can suffer if the host kernel doesn't have a suitably aligned/sized area to use. As you say -
put_user()is more expensive.Quoted 8 lines
Just define the interface to always require natural alignment when donating a memory location?
Quoted 1 line
The structure is updated on every return to the VM.
If you really do suffer from
put_user(), there are alternatives. You could just map the page on the registration hcall and then leave it pinned until the vcpu gets destroyed again.
put_user()should be cheap enough. It is one of the things we tend to optimise anyway. And yes, worse case, we pin the page.Quoted 9 lines
Quoted 4 lines
Of course x86 does prove the third approach can work, but I'm not sure which is actually better. Avoid the kexec cancellation requirements was the main driver of the current approach. Although many of the
I really don't understand the problem with kexec cancellation. Worst case, let guest FW set it up for you and propagate only the address down via ACPI/DT. That way you can mark the respective memory as reserved too.
Quoted 8 lines
We already went down that road with the LPI hack. I'm not going there again if we can avoid it. And it turn out that we can. Just allocate the stolen time page as a separate memblock, give it to KVM for that purpose.
Your suggestion of letting the guest firmware set something up only works if whatever you're booting after that understands it. If it doesn't, you're screwed.
Why? For UEFI, mark the region as reserved in the memory map. For DT, just mark it straight on reserved.
That said, I'm not advocating for doing it in the FW. I think this can be solved really easily with a simple guest driver to enable and a vcpu reset hook to disable the map.
Quoted 8 lines
Quoted 4 lines
But even with a Linux only mechanism, just take a look at
arch/x86/kernel/kvmclock.c. All they do to remove the map is to hook intomachine_crash_shutdown()andmachine_shutdown().I'm not going to take something that is Linux specific. It has to work for all guests, at all times, whether they know about the hypervisor service or not.
If they don't know about the HV service, they don't register the writer, so they don't see corruption.
Quoted 8 lines
If they know about the HV service and they don't support kexec, they don't have to worry because a vcpu reset should also clear the map.
If they do support kexec, they already have a mechanism to quiesce devices.
So I don't understand how this is Linux specific? The question was Linux specific, so I answered with precedence to show that disabling on kexec is not all that hard :).
My concern is more around a something like Jailhouse as a guest-hypervisor. There Linux gives up CPUs to run another OS. This hand-off of a CPU is much easier if there's just a structure in memory somewhere which doesn't move.
The kexec case like you say can be handled as a device to quiesce.
I don't think either scheme is unworkable, but I do think getting the host to provide the memory is easier for both guest and host. Marc had a good point that getting user space to allocate the memory is probably preferable to getting the host kernel to do so, so I'm reworking the code to do that.
Steve
Hi Steven,
Quoted 10 lines · Steven Price
This series add support for paravirtualized time for arm64 guests and KVM hosts following the specification in Arm's document DEN 0057A:
https://developer.arm.com/docs/den0057/a
It implements support for stolen time, allowing the guest to identify time when it is forcibly not executing.
It doesn't implement support for Live Physical Time (LPT) as there are some concerns about the overheads and approach in the above
Do you plan to pick up LPT support? As there is demand of cross-frequency migration (from older platform to newer platform).
I am not clear about the overheads and approach problem here, could you please give some detail information? Maybe we can work together to solve these concerns. :-)
Thanks,
Keqian
Quoted 56 lines
specification, and I expect an updated version of the specification to be released soon with just the stolen time parts.
I previously posted a series including LPT (as well as stolen time):
https://lore.kernel.org/kvmarm/[email protected]/Patches 2, 5, 7 and 8 are cleanup patches and could be taken separately.
Christoffer Dall (1):
KVM: arm/arm64: Factor out hypercall handling from PSCI codeSteven Price (8):
KVM: arm64: Document PV-time interface
KVM: arm64: ImplementPV_FEATUREScall
KVM: arm64: Support stolen time reporting via shared structure
KVM: Allowkvm_device_opsto be const
KVM: arm64: Provide aPV_TIMEdevice to user space arm/arm64: Provide a wrapper for SMCCC 1.1 calls arm/arm64: Make use of the SMCCC 1.1 wrapper
arm64: Retrieve stolen time as paravirtualized guestDocumentation/virtual/kvm/arm/pvtime.txt | 107 +++++++++++++ arch/arm/kvm/Makefile | 2 +- arch/arm/kvm/handle_exit.c | 2 +- arch/arm/mm/proc-v7-bugs.c | 13 +- arch/arm64/include/asm/kvm_host.h | 13 +- arch/arm64/include/asm/kvm_mmu.h | 2 + arch/arm64/include/asm/pvclock-abi.h | 20 +++ arch/arm64/include/uapi/asm/kvm.h | 6 + arch/arm64/kernel/Makefile | 1 + arch/arm64/kernel/cpu_errata.c | 80 ++++------ arch/arm64/kernel/kvm.c | 155 ++++++++++++++++++ arch/arm64/kvm/Kconfig | 1 + arch/arm64/kvm/Makefile | 2 + arch/arm64/kvm/handle_exit.c | 4 +- include/kvm/arm_hypercalls.h | 44 ++++++ include/kvm/arm_psci.h | 2 +- include/linux/arm-smccc.h | 58 +++++++ include/linux/cpuhotplug.h | 1 + include/linux/kvm_host.h | 4 +- include/linux/kvm_types.h | 2 + include/uapi/linux/kvm.h | 2 + virt/kvm/arm/arm.c | 18 +++ virt/kvm/arm/hypercalls.c | 138 ++++++++++++++++ virt/kvm/arm/mmu.c | 44 ++++++ virt/kvm/arm/psci.c | 84 +--------- virt/kvm/arm/pvtime.c | 190 +++++++++++++++++++++++ virt/kvm/kvm_main.c | 6 +- 27 files changed, 848 insertions(+), 153 deletions(-) create mode 100644 Documentation/virtual/kvm/arm/pvtime.txt create mode 100644 arch/arm64/include/asm/pvclock-abi.h create mode 100644 arch/arm64/kernel/kvm.c create mode 100644 include/kvm/arm_hypercalls.h create mode 100644 virt/kvm/arm/hypercalls.c create mode 100644 virt/kvm/arm/pvtime.c
Quoted 1 line · zhukeqian
Hi Steven,
Hi Keqian,
Quoted 6 lines · Steven Price
Quoted 5 lines
This series add support for paravirtualized time for arm64 guests and KVM hosts following the specification in Arm's document DEN 0057A:
Quoted 7 lines
Quoted 5 lines
It implements support for stolen time, allowing the guest to identify time when it is forcibly not executing.
It doesn't implement support for Live Physical Time (LPT) as there are some concerns about the overheads and approach in the above
Do you plan to pick up LPT support? As there is demand of cross-frequency migration (from older platform to newer platform).
I don't have any plans to pick up the LPT support at the moment - feel free to pick it up! ;)
Quoted 2 lines
I am not clear about the overheads and approach problem here, could you please give some detail information? Maybe we can work together to solve these concerns. :-)
Fundamentally the issue here is that LPT only solves one small part of migration between different hosts. To successfully migrate between hosts with different CPU implementations it is also necessary to be able to virtualise various ID registers (e.g. MIDR_EL1, REVIDR_EL1, AIDR_EL1) which we have no support for currently.
The problem with just virtualising the registers is how you handle errata. The guest will currently use those (and other) ID registers to decide whether to enable specific errata workarounds. But what errata should be enabled for a guest which might migrate to another host?
What we ideally need is a mechanism to communicate to the guest what workarounds are required to successfully run on any of the hosts that the guest may be migrated to. You may also have the situation where the workarounds required for two hosts are mutually incompatible - something needs to understand this and do the "right thing" (most likely just reject this situation, i.e. prevent the migration).
There are various options here: e.g. a para-virtualised interface to describe the workarounds (but this is hard to do in an OS-agnostic way), or virtual-ID registers describing an idealised environment where no workarounds are required (and only hosts that have no errata affecting a guest would be able to provide this).
Given the above complexity and the fact that Armv8.6-A standardises the frequency to 1GHz this didn't seem worth continuing with. So LPT was dropped from the spec and patches to avoid holding up the stolen time support.
However, if you have a use case which doesn't require such a generic migration (e.g. perhaps old and new platforms are based on the same IP) then it might be worth looking at bring this back. But to make the problem solvable it either needs to be restricted to platforms which are substantially the same (so the errata list will be identical), or there's work to be done in preparation to deal with migrating a guest successfully between hosts with potentially different errata requirements.
Can you share more details about the hosts that you are interested in migrating between?
Thanks,
Steve
Hi Steven,
Quoted 19 lines · Steven Price
Quoted 1 line · zhukeqian
Hi Steven,
Hi Keqian,
Quoted 6 lines · Steven Price
Quoted 5 lines
This series add support for paravirtualized time for arm64 guests and KVM hosts following the specification in Arm's document DEN 0057A:
Quoted 7 lines
Quoted 5 lines
It implements support for stolen time, allowing the guest to identify time when it is forcibly not executing.
It doesn't implement support for Live Physical Time (LPT) as there are some concerns about the overheads and approach in the above
Do you plan to pick up LPT support? As there is demand of cross-frequency migration (from older platform to newer platform).
Quoted 7 lines
I don't have any plans to pick up the LPT support at the moment - feel free to pick it up! ;)
Quoted 2 lines
I am not clear about the overheads and approach problem here, could you please give some detail information? Maybe we can work together to solve these concerns. :-)
Fundamentally the issue here is that LPT only solves one small part of migration between different hosts. To successfully migrate between hosts with different CPU implementations it is also necessary to be able to virtualise various ID registers (e.g.
MIDR_EL1,REVIDR_EL1,AIDR_EL1) which we have no support for currently.
Yeah, currently we are trying to do both timer freq virtualization and CPU feature virtualization.
Quoted 2 lines
The problem with just virtualising the registers is how you handle errata. The guest will currently use those (and other) ID registers to decide whether to enable specific errata workarounds. But what errata should be enabled for a guest which might migrate to another host?
Thanks for pointing this out.
I think the most important thing is that we should introduce a concept named CPU baseline which represents a standard platform. If we bring up a guest with a specific CPU baseline, then this guest can only run on a platform that is compatible with this CPU baseline. So "baseline" and "compatible" are the key point to promise successful cross-platform migration.
Quoted 4 lines
What we ideally need is a mechanism to communicate to the guest what workarounds are required to successfully run on any of the hosts that the guest may be migrated to. You may also have the situation where the workarounds required for two hosts are mutually incompatible - something needs to understand this and do the "right thing" (most likely just reject this situation, i.e. prevent the migration).
There are various options here: e.g. a para-virtualised interface to describe the workarounds (but this is hard to do in an OS-agnostic way), or virtual-ID registers describing an idealised environment where no workarounds are required (and only hosts that have no errata affecting a guest would be able to provide this).
My idea is similar with the "idealised environment", but errata workaround still exists.
We do not provide para-virtualised interface, and migration is restricted between platforms that are compatible with baseline.
Baseline should has two aspects: CPU feature and errata. These platforms that are compatible with a specific baseline should have the corresponding CPU feature and errata.
Quoted 5 lines
Given the above complexity and the fact that Armv8.6-A standardises the frequency to 1GHz this didn't seem worth continuing with. So LPT was dropped from the spec and patches to avoid holding up the stolen time support.
However, if you have a use case which doesn't require such a generic migration (e.g. perhaps old and new platforms are based on the same IP) then it might be worth looking at bring this back. But to make the problem solvable it either needs to be restricted to platforms which are substantially the same (so the errata list will be identical), or there's work to be done in preparation to deal with migrating a guest successfully between hosts with potentially different errata requirements.
Can you share more details about the hosts that you are interested in migrating between?
Here we have new platform with 1GHz timer, and old platform is 100MHZ, so we want to solve the cross-platform migration firstly.
Thanks,
Keqian
Quoted 6 lines
Thanks,
Steve
.