arm64: Stolen time support
Subsystems (8)
v3 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.
NOTE: Patches 8 and 9 will conflict with Mark Rutland's series[1] cleaning up the SMCCC conduit. I do feel that the addition of an _invoke() call makes a number of call sites cleaner and it should be possible to integrate both this and Mark's other cleanups.
[1] https://lore.kernel.org/linux-arm-kernel/[email protected]/
Also available as a git tree:git://linux-arm.org/linux-sp.git stolen_time/v3
Changes from v2:
https://lore.kernel.org/lkml/[email protected]/
* Switched from using gfn_to_hva_cache to a new macro kvm_put_guest() that can provide the single-copy atomicity required (on arm64). This macro is added in patch 4.
* Tidied up the locking for kvm_update_stolen_time(). pagefault_disable() was unnecessary and the caller didn't need to take kvm->srcu as the function does it itself.
* Removed struct kvm_arch_pvtime from the arm implementation, replaced instead with inline static functions which are empty for arm.
* Fixed a few checkpatch --strict warnings.
Changes from v1:
https://lore.kernel.org/lkml/[email protected]/
* Host kernel no longer allocates the stolen time structure, instead it is allocated by user space. This means the save/restore functionality can be removed.
* Refactored the code so arm has stub implementations and to avoid initcall
* Rebased to pick up Documentation/{virt->virtual} change
* Bunch of typo fixes
Christoffer Dall (1):
KVM: arm/arm64: Factor out hypercall handling from PSCI code
Steven Price (9):
KVM: arm64: Document PV-time interface
KVM: arm64: Implement PV_FEATURES call
KVM: Implement kvm_put_guest()
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/virt/kvm/arm/pvtime.txt | 100 ++++++++++++++ arch/arm/include/asm/kvm_host.h | 30 +++++ 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 | 28 +++- arch/arm64/include/asm/paravirt.h | 9 +- arch/arm64/include/asm/pvclock-abi.h | 17 +++ arch/arm64/include/uapi/asm/kvm.h | 8 ++ arch/arm64/kernel/cpu_errata.c | 80 ++++------- arch/arm64/kernel/paravirt.c | 148 +++++++++++++++++++++ arch/arm64/kernel/time.c | 3 + arch/arm64/kvm/Kconfig | 1 + arch/arm64/kvm/Makefile | 2 + arch/arm64/kvm/handle_exit.c | 4 +- include/kvm/arm_hypercalls.h | 43 ++++++ include/kvm/arm_psci.h | 2 +- include/linux/arm-smccc.h | 58 ++++++++ include/linux/cpuhotplug.h | 1 + include/linux/kvm_host.h | 28 +++- include/linux/kvm_types.h | 2 + include/uapi/linux/kvm.h | 2 + virt/kvm/arm/arm.c | 11 ++ virt/kvm/arm/hypercalls.c | 68 ++++++++++ virt/kvm/arm/psci.c | 84 +----------- virt/kvm/arm/pvtime.c | 182 ++++++++++++++++++++++++++ virt/kvm/kvm_main.c | 6 +- 27 files changed, 780 insertions(+), 154 deletions(-) create mode 100644 Documentation/virt/kvm/arm/pvtime.txt create mode 100644 arch/arm64/include/asm/pvclock-abi.h 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 · 1 line
2.20.1
PATCH v3 1/10 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/virt/kvm/arm/pvtime.txt | 100 ++++++++++++++++++++++++++ 1 file changed, 100 insertions(+) create mode 100644 Documentation/virt/kvm/arm/pvtime.txt
Documentation/virt/kvm/arm/pvtime.txt new file +100 -0
@@ -0,0 +1,100 @@ +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. The existence of +the PV_FEATURES hypercall should be probed using the SMCCC 1.1 ARCH_FEATURES +mechanism before calling it. + +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) + +The IPA returned by PV_TIME_ST should be mapped by the guest as normal memory +with inner and outer write back caching attributes, in the inner shareable +domain. A total of 16 bytes from the IPA returned are guaranteed to be +meaningfully filled by the hypervisor (see structure below). + +PV_TIME_ST returns the structure for the calling VCPU. + +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 prior to scheduling a 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 per 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); + +Creation of the device should be done after creating the vCPUs of the virtual +machine. + +The 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). The base address +must be page aligned. The size must be at least 64 * number of VCPUs and be a +multiple of PAGE_SIZE. + +The memory for these structures should be added to the guest in the usual +manner (e.g. using KVM_SET_USER_MEMORY_REGION). + +For example: + + struct kvm_dev_arm_st_region region = { + .gpa = <IPA of guest base address>, + .size = <size in bytes> + }; + + struct kvm_device_attr st_base = { + .group = KVM_DEV_ARM_PV_TIME_PADDR, + .attr = KVM_DEV_ARM_PV_TIME_ST, + .addr = (u64)®ion + }; + + ioctl(pvtime_fd, KVM_SET_DEVICE_ATTR, &st_base);
Signature · 1 line
2.20.1
Re: [PATCH v3 01/10] KVM: arm64: Document PV-time interface
Quoted 83 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.
Signed-off-by Steven Price <[email protected]>--- Documentation/virt/kvm/arm/pvtime.txt | 100 ++++++++++++++++++++++++++ 1 file changed, 100 insertions(+) create mode 100644 Documentation/virt/kvm/arm/pvtime.txtDocumentation/virt/kvm/arm/pvtime.txt new file +57
-0@@ -0,0 +1,100 @@ +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. The existence of +the PV_FEATURES hypercall should be probed using the SMCCC 1.1 ARCH_FEATURES +mechanism before calling it. + +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) + +The IPA returned by PV_TIME_ST should be mapped by the guest as normal memory +with inner and outer write back caching attributes, in the inner shareable +domain. A total of 16 bytes from the IPA returned are guaranteed to be +meaningfully filled by the hypervisor (see structure below). + +PV_TIME_ST returns the structure for the calling VCPU. + +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 prior to scheduling a VCPU. It
Documentation/virt/kvm/arm/pvtime.txt line 67
Quoted 10 lines
Documentation/virt/kvm/arm/pvtime.txt new file +10
-0+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 per 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: +
I feel it would be more consistent to have the details of this in Documentation/virt/kvm/devices/arm-pv-time.txt and refer to this document from here.
Quoted 36 lines
Documentation/virt/kvm/arm/pvtime.txt new file +33
-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); + +Creation of the device should be done after creating the vCPUs of the virtual +machine. + +The 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). The base address +must be page aligned. The size must be at least 64 * number of VCPUs and be a +multiple of PAGE_SIZE. + +The memory for these structures should be added to the guest in the usual +manner (e.g. using KVM_SET_USER_MEMORY_REGION). + +For example: + + struct kvm_dev_arm_st_region region = { + .gpa = <IPA of guest base address>, + .size = <size in bytes> + }; + + struct kvm_device_attr st_base = { + .group = KVM_DEV_ARM_PV_TIME_PADDR, + .attr = KVM_DEV_ARM_PV_TIME_ST, + .addr = (u64)®ion + }; + + ioctl(pvtime_fd, KVM_SET_DEVICE_ATTR, &st_base);Signature · 1 line
2.20.1
Thanks,
Christoffer
Re: [PATCH v3 01/10] KVM: arm64: Document PV-time interface
Quoted 90 lines · Christoffer Dall
Quoted 89 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.
Signed-off-by Steven Price <[email protected]>--- Documentation/virt/kvm/arm/pvtime.txt | 100 ++++++++++++++++++++++++++ 1 file changed, 100 insertions(+) create mode 100644 Documentation/virt/kvm/arm/pvtime.txtDocumentation/virt/kvm/arm/pvtime.txt new file +63
-0@@ -0,0 +1,100 @@ +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. The existence of +the PV_FEATURES hypercall should be probed using the SMCCC 1.1 ARCH_FEATURES +mechanism before calling it. + +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) + +The IPA returned by PV_TIME_ST should be mapped by the guest as normal memory +with inner and outer write back caching attributes, in the inner shareable +domain. A total of 16 bytes from the IPA returned are guaranteed to be +meaningfully filled by the hypervisor (see structure below). + +PV_TIME_ST returns the structure for the calling VCPU. + +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 prior to scheduling a 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 per VCPU of the guest. + +User space interface +====================
Quoted 8 lines
Documentation/virt/kvm/arm/pvtime.txt line 67
Quoted 4 lines
Documentation/virt/kvm/arm/pvtime.txt new file +4
-0+ +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: +I feel it would be more consistent to have the details of this in
Documentation/virt/kvm/devices/arm-pv-time.txtand refer to this document from here.
Fair point - I'll move this lower part of the document and add a reference.
Thanks,
Steve
Quoted 41 lines
Quoted 36 lines
Documentation/virt/kvm/arm/pvtime.txt new file +33
-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); + +Creation of the device should be done after creating the vCPUs of the virtual +machine. + +The 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). The base address +must be page aligned. The size must be at least 64 * number of VCPUs and be a +multiple of PAGE_SIZE. + +The memory for these structures should be added to the guest in the usual +manner (e.g. using KVM_SET_USER_MEMORY_REGION). + +For example: + + struct kvm_dev_arm_st_region region = { + .gpa = <IPA of guest base address>, + .size = <size in bytes> + }; + + struct kvm_device_attr st_base = { + .group = KVM_DEV_ARM_PV_TIME_PADDR, + .attr = KVM_DEV_ARM_PV_TIME_ST, + .addr = (u64)®ion + }; + + ioctl(pvtime_fd, KVM_SET_DEVICE_ATTR, &st_base);Signature · 1 line
2.20.1Thanks,
Christoffer
Re: [PATCH v3 01/10] KVM: arm64: Document PV-time interface
In reply to Steven PriceQuoted 108 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.
Signed-off-by Steven Price <[email protected]>--- Documentation/virt/kvm/arm/pvtime.txt | 100 ++++++++++++++++++++++++++ 1 file changed, 100 insertions(+) create mode 100644 Documentation/virt/kvm/arm/pvtime.txtDocumentation/virt/kvm/arm/pvtime.txt new file +82
-0@@ -0,0 +1,100 @@ +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. The existence of +the PV_FEATURES hypercall should be probed using the SMCCC 1.1 ARCH_FEATURES +mechanism before calling it. + +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) + +The IPA returned by PV_TIME_ST should be mapped by the guest as normal memory +with inner and outer write back caching attributes, in the inner shareable +domain. A total of 16 bytes from the IPA returned are guaranteed to be +meaningfully filled by the hypervisor (see structure below). + +PV_TIME_ST returns the structure for the calling VCPU. + +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 prior to scheduling a 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 per 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); + +Creation of the device should be done after creating the vCPUs of the virtual +machine. + +The 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). The base address +must be page aligned. The size must be at least 64 * number of VCPUs and be a +multiple of PAGE_SIZE.
Documentation/virt/kvm/arm/pvtime.txt line 92
Quoted 10 lines
Documentation/virt/kvm/arm/pvtime.txt new file +10
-0+ +The memory for these structures should be added to the guest in the usual +manner (e.g. using KVM_SET_USER_MEMORY_REGION). + +For example: + + struct kvm_dev_arm_st_region region = { + .gpa = <IPA of guest base address>, + .size = <size in bytes> + };
This feel fragile; how are you handling userspace creating VCPUs after setting this up, the GPA overlapping guest memory, etc. Is the philosophy here that the VMM can mess up the VM if it wants, but that this should never lead attacks on the host (we better hope not) and so we don't care?
It seems to me setting the IPA per vcpu throught the VCPU device would avoid a lot of these issues. See Documentation/virt/kvm/devices/vcpu.txt.
Thanks,
Christoffer
Quoted 15 lines
Documentation/virt/kvm/arm/pvtime.txt new file +8
-0+ + struct kvm_device_attr st_base = { + .group = KVM_DEV_ARM_PV_TIME_PADDR, + .attr = KVM_DEV_ARM_PV_TIME_ST, + .addr = (u64)®ion + }; + + ioctl(pvtime_fd, KVM_SET_DEVICE_ATTR, &st_base);Signature · 6 lines
2.20.1 _______________________________________________ kvmarm mailing list [email protected] https://lists.cs.columbia.edu/mailman/listinfo/kvmarm
Re: [PATCH v3 01/10] KVM: arm64: Document PV-time interface
Quoted 114 lines · Christoffer Dall
Quoted 113 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.
Signed-off-by Steven Price <[email protected]>--- Documentation/virt/kvm/arm/pvtime.txt | 100 ++++++++++++++++++++++++++ 1 file changed, 100 insertions(+) create mode 100644 Documentation/virt/kvm/arm/pvtime.txtDocumentation/virt/kvm/arm/pvtime.txt new file +87
-0@@ -0,0 +1,100 @@ +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. The existence of +the PV_FEATURES hypercall should be probed using the SMCCC 1.1 ARCH_FEATURES +mechanism before calling it. + +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) + +The IPA returned by PV_TIME_ST should be mapped by the guest as normal memory +with inner and outer write back caching attributes, in the inner shareable +domain. A total of 16 bytes from the IPA returned are guaranteed to be +meaningfully filled by the hypervisor (see structure below). + +PV_TIME_ST returns the structure for the calling VCPU. + +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 prior to scheduling a 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 per 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); + +Creation of the device should be done after creating the vCPUs of the virtual +machine. + +The 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). The base address +must be page aligned. The size must be at least 64 * number of VCPUs and be a +multiple of PAGE_SIZE. + +The memory for these structures should be added to the guest in the usual +manner (e.g. using KVM_SET_USER_MEMORY_REGION). + +For example:
Quoted 8 lines
Documentation/virt/kvm/arm/pvtime.txt line 92
Quoted 5 lines
Documentation/virt/kvm/arm/pvtime.txt new file +5
-0+ + struct kvm_dev_arm_st_region region = { + .gpa = <IPA of guest base address>, + .size = <size in bytes> + };This feel fragile; how are you handling userspace creating VCPUs after setting this up,
In this case as long as the structures all fit within the region created VCPUs can be created/destroyed at will. If the VCPU index is too high then the kernel will bail out in kvm_update_stolen_time() so the structure will not be written. I consider this case as user space messing up, so beyond protecting the host from the mess, user space gets to keep the pieces.
Quoted 1 line
the GPA overlapping guest memory, etc.
Again, the (host) kernel is protected against this, but clearly this will end badly for the guest.
Quoted 4 lines
Is the
philosophy here that the VMM can mess up the VM if it wants, but that this should never lead attacks on the host (we better hope not) and so we don't care?
Yes. For things like GPA overlapping guest memory it's not really the host's position to work out what is "guest memory". It's quite possible that user space could decide to place the stolen time structures right in the middle of guest memory - it's just up to user space to inform the guest what memory is usable. Obviously the expectation is that the shared structures would be positioned "out of the way" in GPA space in any normal arrangement.
Quoted 3 lines
It seems to me setting the IPA per vcpu throught the VCPU device would avoid a lot of these issues. See
Documentation/virt/kvm/devices/vcpu.txt.
That is certainly a possibility, I'm not really sure what the benefit is though? It would still lead to corner cases:
* What if only some VCPUs had stolen time setup on them?
* What if multiple VCPUs pointed to the same location?
* The structures can still overlap with guest memory
It's also more work to setup in user space with the only "benefit" being that user space could choose to organise the structures however it sees fit (e.g. no need for them to be contiguous in memory). But I'm not sure I see a use case for that flexibility.
Perhaps there's some benefit I'm not seeing?
Steve
Re: [PATCH v3 01/10] KVM: arm64: Document PV-time interface
Quoted 160 lines · Steven Price
Quoted 114 lines · Christoffer Dall
Quoted 113 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.
Signed-off-by Steven Price <[email protected]>--- Documentation/virt/kvm/arm/pvtime.txt | 100 ++++++++++++++++++++++++++ 1 file changed, 100 insertions(+) create mode 100644 Documentation/virt/kvm/arm/pvtime.txtDocumentation/virt/kvm/arm/pvtime.txt new file +87
-0@@ -0,0 +1,100 @@ +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. The existence of +the PV_FEATURES hypercall should be probed using the SMCCC 1.1 ARCH_FEATURES +mechanism before calling it. + +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) + +The IPA returned by PV_TIME_ST should be mapped by the guest as normal memory +with inner and outer write back caching attributes, in the inner shareable +domain. A total of 16 bytes from the IPA returned are guaranteed to be +meaningfully filled by the hypervisor (see structure below). + +PV_TIME_ST returns the structure for the calling VCPU. + +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 prior to scheduling a 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 per 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); + +Creation of the device should be done after creating the vCPUs of the virtual +machine. + +The 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). The base address +must be page aligned. The size must be at least 64 * number of VCPUs and be a +multiple of PAGE_SIZE. + +The memory for these structures should be added to the guest in the usual +manner (e.g. using KVM_SET_USER_MEMORY_REGION). + +For example:Quoted 8 lines
Documentation/virt/kvm/arm/pvtime.txt line 92
Quoted 5 lines
Documentation/virt/kvm/arm/pvtime.txt new file +5
-0+ + struct kvm_dev_arm_st_region region = { + .gpa = <IPA of guest base address>, + .size = <size in bytes> + };This feel fragile; how are you handling userspace creating VCPUs after setting this up,
In this case as long as the structures all fit within the region created VCPUs can be created/destroyed at will. If the VCPU index is too high then the kernel will bail out in
kvm_update_stolen_time()so the structure will not be written. I consider this case as user space messing up, so beyond protecting the host from the mess, user space gets to keep the pieces.Quoted 1 line
the GPA overlapping guest memory, etc.
Again, the (host) kernel is protected against this, but clearly this will end badly for the guest.
Quoted 4 lines
Is the
philosophy here that the VMM can mess up the VM if it wants, but that this should never lead attacks on the host (we better hope not) and so we don't care?Yes. For things like GPA overlapping guest memory it's not really the host's position to work out what is "guest memory". It's quite possible that user space could decide to place the stolen time structures right in the middle of guest memory - it's just up to user space to inform the guest what memory is usable. Obviously the expectation is that the shared structures would be positioned "out of the way" in GPA space in any normal arrangement.
Quoted 3 lines
It seems to me setting the IPA per vcpu throught the VCPU device would avoid a lot of these issues. See
Documentation/virt/kvm/devices/vcpu.txt.That is certainly a possibility, I'm not really sure what the benefit is though? It would still lead to corner cases:
* What if only some VCPUs had stolen time setup on them?
* What if multiple VCPUs pointed to the same location?
* The structures can still overlap with guest memory
Quoted 7 lines
It's also more work to setup in user space with the only "benefit" being that user space could choose to organise the structures however it sees fit (e.g. no need for them to be contiguous in memory). But I'm not sure I see a use case for that flexibility.
Perhaps there's some benefit I'm not seeing?
So this is now mostly moot as you said you were going to change this, but overall I think the assumption that it's trivial to maintain an interface such the one proposed here to never be an attack vector for the host is not as easy as it may sound. In the past, we've had numerous bugs related to things like calculating an offset into some region based on some index and a size, because suddenly someone changed how an index is calculated, and it had unintended side-effects etc.
So if an API is used to specify something which is effectively per-CPU, it's better to use the per-CPU handles (VCPU fds) to directly describe this relationship.
Thanks,
Christoffer
Re: [PATCH v3 01/10] KVM: arm64: Document PV-time interface
In reply to Christoffer DallQuoted 126 lines · Christoffer Dall
Quoted 108 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.
Signed-off-by Steven Price <[email protected]>--- Documentation/virt/kvm/arm/pvtime.txt | 100 ++++++++++++++++++++++++++ 1 file changed, 100 insertions(+) create mode 100644 Documentation/virt/kvm/arm/pvtime.txtDocumentation/virt/kvm/arm/pvtime.txt new file +82
-0@@ -0,0 +1,100 @@ +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. The existence of +the PV_FEATURES hypercall should be probed using the SMCCC 1.1 ARCH_FEATURES +mechanism before calling it. + +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) + +The IPA returned by PV_TIME_ST should be mapped by the guest as normal memory +with inner and outer write back caching attributes, in the inner shareable +domain. A total of 16 bytes from the IPA returned are guaranteed to be +meaningfully filled by the hypervisor (see structure below). + +PV_TIME_ST returns the structure for the calling VCPU. + +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 prior to scheduling a 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 per 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); + +Creation of the device should be done after creating the vCPUs of the virtual +machine. + +The 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). The base address +must be page aligned. The size must be at least 64 * number of VCPUs and be a +multiple of PAGE_SIZE.Documentation/virt/kvm/arm/pvtime.txt line 92
Quoted 10 lines
Documentation/virt/kvm/arm/pvtime.txt new file +10
-0+ +The memory for these structures should be added to the guest in the usual +manner (e.g. using KVM_SET_USER_MEMORY_REGION). + +For example: + + struct kvm_dev_arm_st_region region = { + .gpa = <IPA of guest base address>, + .size = <size in bytes> + };This feel fragile; how are you handling userspace creating VCPUs after setting this up, the GPA overlapping guest memory, etc. Is the philosophy here that the VMM can mess up the VM if it wants, but that this should never lead attacks on the host (we better hope not) and so we don't care?
Quoted 5 lines
It seems to me setting the IPA per vcpu throught the VCPU device would avoid a lot of these issues. See
Documentation/virt/kvm/devices/vcpu.txt.
I discussed this with Marc the other day, and we realized that if we make the configuration of the IPA per-PE, then a VMM can construct a VM where these data structures are distributed within the IPA space of a VM, which could lead to a lower TLB pressure for some configurations/workloads.
Thanks,
Christoffer
Re: [PATCH v3 01/10] KVM: arm64: Document PV-time interface
Quoted 132 lines · Christoffer Dall
Quoted 126 lines · Christoffer Dall
Quoted 108 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.
Signed-off-by Steven Price <[email protected]>--- Documentation/virt/kvm/arm/pvtime.txt | 100 ++++++++++++++++++++++++++ 1 file changed, 100 insertions(+) create mode 100644 Documentation/virt/kvm/arm/pvtime.txtDocumentation/virt/kvm/arm/pvtime.txt new file +82
-0@@ -0,0 +1,100 @@ +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. The existence of +the PV_FEATURES hypercall should be probed using the SMCCC 1.1 ARCH_FEATURES +mechanism before calling it. + +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) + +The IPA returned by PV_TIME_ST should be mapped by the guest as normal memory +with inner and outer write back caching attributes, in the inner shareable +domain. A total of 16 bytes from the IPA returned are guaranteed to be +meaningfully filled by the hypervisor (see structure below). + +PV_TIME_ST returns the structure for the calling VCPU. + +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 prior to scheduling a 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 per 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); + +Creation of the device should be done after creating the vCPUs of the virtual +machine. + +The 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). The base address +must be page aligned. The size must be at least 64 * number of VCPUs and be a +multiple of PAGE_SIZE.Documentation/virt/kvm/arm/pvtime.txt line 92
Quoted 10 lines
Documentation/virt/kvm/arm/pvtime.txt new file +10
-0+ +The memory for these structures should be added to the guest in the usual +manner (e.g. using KVM_SET_USER_MEMORY_REGION). + +For example: + + struct kvm_dev_arm_st_region region = { + .gpa = <IPA of guest base address>, + .size = <size in bytes> + };This feel fragile; how are you handling userspace creating VCPUs after setting this up, the GPA overlapping guest memory, etc. Is the philosophy here that the VMM can mess up the VM if it wants, but that this should never lead attacks on the host (we better hope not) and so we don't care?
Quoted 5 lines
It seems to me setting the IPA per vcpu throught the VCPU device would avoid a lot of these issues. See
Documentation/virt/kvm/devices/vcpu.txt.
Quoted 5 lines
I discussed this with Marc the other day, and we realized that if we make the configuration of the IPA per-PE, then a VMM can construct a VM where these data structures are distributed within the IPA space of a VM, which could lead to a lower TLB pressure for some configurations/workloads.
Ok, I'm dubious it will make much difference in terms of TLB pressure, but I've done the refactoring and I think it actually simplifies the code. So I'll post a new version where the base address is set via the VCPU device.
Thanks for the review,
Steve
Re: [PATCH v3 01/10] KVM: arm64: Document PV-time interface
In reply to Steven PriceQuoted 48 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.
Signed-off-by Steven Price <[email protected]>--- Documentation/virt/kvm/arm/pvtime.txt | 100 ++++++++++++++++++++++++++ 1 file changed, 100 insertions(+) create mode 100644 Documentation/virt/kvm/arm/pvtime.txtDocumentation/virt/kvm/arm/pvtime.txt new file +22
-0@@ -0,0 +1,100 @@ +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. The existence of +the PV_FEATURES hypercall should be probed using the SMCCC 1.1 ARCH_FEATURES +mechanism before calling it. +
Documentation/virt/kvm/arm/pvtime.txt line 32
Quoted 10 lines
Documentation/virt/kvm/arm/pvtime.txt new file +10
-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. + +PV_TIME_ST + Function ID: (uint32) : 0xC5000022 + Return value: (int64) : IPA of the stolen time data structure for this + (V)CPU. On failure:
Why the () around the V in VCPU?
Documentation/virt/kvm/arm/pvtime.txt line 40
Quoted 8 lines
Documentation/virt/kvm/arm/pvtime.txt new file +8
-0+ NOT_SUPPORTED (-1) + +The IPA returned by PV_TIME_ST should be mapped by the guest as normal memory +with inner and outer write back caching attributes, in the inner shareable +domain. A total of 16 bytes from the IPA returned are guaranteed to be +meaningfully filled by the hypervisor (see structure below). + +PV_TIME_ST returns the structure for the calling VCPU.
The above sentence seems redundant here.
Quoted 24 lines
Documentation/virt/kvm/arm/pvtime.txt new file +24
-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 + | | | much time this VCPU thread + | | | was involuntarily not + | | | running on a physical CPU. + +The structure will be updated by the hypervisor prior to scheduling a 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 per VCPU of the guest. + +User space interface +==================== +
Documentation/virt/kvm/arm/pvtime.txt line 74
Quoted 10 lines
Documentation/virt/kvm/arm/pvtime.txt new file +10
-0+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 ioctl doesn't return the fd. If the ioctl returns zero the fd will be in pvtime_device.fd.
Documentation/virt/kvm/arm/pvtime.txt line 77
Quoted 3 lines
Documentation/virt/kvm/arm/pvtime.txt new file +3
-0+ +Creation of the device should be done after creating the vCPUs of the virtual +machine.
Or else what? Will an error be reported in that case?
Documentation/virt/kvm/arm/pvtime.txt line 85
Quoted 8 lines
Documentation/virt/kvm/arm/pvtime.txt new file +8
-0+ +The 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). The base address +must be page aligned. The size must be at least 64 * number of VCPUs and be a +multiple of PAGE_SIZE. + +The memory for these structures should be added to the guest in the usual +manner (e.g. using KVM_SET_USER_MEMORY_REGION).
Above it says the guest shouldn't attempt to write the memory. Should KVM_MEM_READONLY be used with KVM_SET_USER_MEMORY_REGION for it?
Documentation/virt/kvm/arm/pvtime.txt line 95
Quoted 10 lines
Documentation/virt/kvm/arm/pvtime.txt new file +10
-0+ +For example: + + struct kvm_dev_arm_st_region region = { + .gpa = <IPA of guest base address>, + .size = <size in bytes> + }; + + struct kvm_device_attr st_base = { + .group = KVM_DEV_ARM_PV_TIME_PADDR,
This is KVM_DEV_ARM_PV_TIME_REGION in the code.
Quoted 8 lines
Documentation/virt/kvm/arm/pvtime.txt new file +5
-0+ .attr = KVM_DEV_ARM_PV_TIME_ST, + .addr = (u64)®ion + }; + + ioctl(pvtime_fd, KVM_SET_DEVICE_ATTR, &st_base);Signature · 1 line
2.20.1
Thanks,
drew
Re: [PATCH v3 01/10] KVM: arm64: Document PV-time interface
Quoted 53 lines · Andrew Jones
Quoted 52 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.
Signed-off-by Steven Price <[email protected]>--- Documentation/virt/kvm/arm/pvtime.txt | 100 ++++++++++++++++++++++++++ 1 file changed, 100 insertions(+) create mode 100644 Documentation/virt/kvm/arm/pvtime.txtDocumentation/virt/kvm/arm/pvtime.txt new file +26
-0@@ -0,0 +1,100 @@ +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. The existence of +the PV_FEATURES hypercall should be probed using the SMCCC 1.1 ARCH_FEATURES +mechanism before calling it. + +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
Quoted 8 lines
Documentation/virt/kvm/arm/pvtime.txt line 32
Quoted 6 lines
Documentation/virt/kvm/arm/pvtime.txt new file +6
-0+ 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:Why the () around the V in VCPU?
There's nothing preventing the same mechanism being used without the virtualisation of CPUs. For example a hypervisor like Jailhouse could implement this interface even though there the CPU isn't being virtualised but is being handed over to the guest. Equally it is possible for firmware to provide the same mechanism (using the SMC64 calling convention).
Admittedly that's a little confusing here because the rest of this document is talking about KVM's implementation and normal hypervisors. So I'll drop the brackets.
Quoted 10 lines
Documentation/virt/kvm/arm/pvtime.txt line 40
Quoted 8 lines
Documentation/virt/kvm/arm/pvtime.txt new file +8
-0+ NOT_SUPPORTED (-1) + +The IPA returned by PV_TIME_ST should be mapped by the guest as normal memory +with inner and outer write back caching attributes, in the inner shareable +domain. A total of 16 bytes from the IPA returned are guaranteed to be +meaningfully filled by the hypervisor (see structure below). + +PV_TIME_ST returns the structure for the calling VCPU.The above sentence seems redundant here.
It is an important detail that each VCPU must use PV_TIME_ST to fetch the address of the structure for that VCPU. E.g. It could have been implemented so that the hypercall took a VCPU number. So while redundant I do feel it's worth pointing this out explicitly.
Quoted 29 lines
Quoted 29 lines
Documentation/virt/kvm/arm/pvtime.txt new file +29
-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 + | | | much time this VCPU thread + | | | was involuntarily not + | | | running on a physical CPU. + +The structure will be updated by the hypervisor prior to scheduling a 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 per 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,
Quoted 8 lines
Documentation/virt/kvm/arm/pvtime.txt line 74
Quoted 5 lines
Documentation/virt/kvm/arm/pvtime.txt new file +5
-0+ .attr = 0, + .flags = 0, + }; + + pvtime_fd = ioctl(vm_fd, KVM_CREATE_DEVICE, &pvtime_device);The ioctl doesn't return the fd. If the ioctl returns zero the fd will be in pvtime_device.fd.
Good catch - I'm not sure quite why I wrote that. Anyway I've agreed to change the interface to operate on the VCPU device instead so this text is rewritten completely.
Quoted 5 lines
Documentation/virt/kvm/arm/pvtime.txt line 77
Quoted 3 lines
Documentation/virt/kvm/arm/pvtime.txt new file +3
-0+ +Creation of the device should be done after creating the vCPUs of the virtual +machine.Or else what? Will an error be reported in that case?
This is now enforced by calling the ioctl on the VCPU device, so it's impossible to do in the wrong order.
Quoted 11 lines
Documentation/virt/kvm/arm/pvtime.txt line 85
Quoted 8 lines
Documentation/virt/kvm/arm/pvtime.txt new file +8
-0+ +The 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). The base address +must be page aligned. The size must be at least 64 * number of VCPUs and be a +multiple of PAGE_SIZE. + +The memory for these structures should be added to the guest in the usual +manner (e.g. using KVM_SET_USER_MEMORY_REGION).Above it says the guest shouldn't attempt to write the memory. Should
KVM_MEM_READONLYbe used withKVM_SET_USER_MEMORY_REGIONfor it?
That is optional - the specification states the guest must not attempt to write to it - so marking it read-only for the guest should work fine with a conforming guest. But it's not required.
Quoted 4 lines
Quoted 4 lines
Documentation/virt/kvm/arm/pvtime.txt new file +4
-0+ +For example: + + struct kvm_dev_arm_st_region region = {
Quoted 8 lines
Documentation/virt/kvm/arm/pvtime.txt line 95
Quoted 6 lines
Documentation/virt/kvm/arm/pvtime.txt new file +6
-0+ .gpa = <IPA of guest base address>, + .size = <size in bytes> + }; + + struct kvm_device_attr st_base = { + .group = KVM_DEV_ARM_PV_TIME_PADDR,This is
KVM_DEV_ARM_PV_TIME_REGIONin the code.
Gah - I obviously missed that when I refactored to define the region rather than just the base address. Anyway this has all changed (again) because each VCPU has its own base address so the size is no longer necessary.
Thanks for the review,
Steve
Quoted 12 lines
Quoted 8 lines
Documentation/virt/kvm/arm/pvtime.txt new file +5
-0+ .attr = KVM_DEV_ARM_PV_TIME_ST, + .addr = (u64)®ion + }; + + ioctl(pvtime_fd, KVM_SET_DEVICE_ATTR, &st_base);Signature · 1 line
2.20.1Thanks,
drew
PATCH v3 2/10 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 v3 3/10 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/arm/include/asm/kvm_host.h | 6 ++++++ arch/arm64/include/asm/kvm_host.h | 2 ++ arch/arm64/include/asm/pvclock-abi.h | 17 +++++++++++++++++ arch/arm64/kvm/Makefile | 1 + include/linux/arm-smccc.h | 14 ++++++++++++++ virt/kvm/arm/hypercalls.c | 6 ++++++ virt/kvm/arm/pvtime.c | 21 +++++++++++++++++++++ 7 files changed, 67 insertions(+) create mode 100644 arch/arm64/include/asm/pvclock-abi.h create mode 100644 virt/kvm/arm/pvtime.c
arch/arm/include/asm/kvm_host.h +6 -0
@@ -7,6 +7,7 @@#ifndef __ARM_KVM_HOST_H__#define __ARM_KVM_HOST_H__ +#include <linux/arm-smccc.h> #include <linux/errno.h> #include <linux/types.h> #include <linux/kvm_types.h>
@@ -323,6 +324,11 @@ static inline int kvm_arch_vm_ioctl_check_extension(struct kvm *kvm, long ext)int kvm_perf_init (void );int kvm_perf_teardown (void ); +static inline int kvm_hypercall_pv_features (struct kvm_vcpu *vcpu) +{ +return SMCCC_RET_NOT_SUPPORTED ; +} +void kvm_mmu_wp_memory_region (struct kvm *kvm,int slot);struct kvm_vcpu *kvm_mpidr_to_vcpu (struct kvm *kvm,unsigned long mpidr);
arch/arm64/include/asm/kvm_host.h +2 -0
@@ -478,6 +478,8 @@ void handle_exit_early(struct kvm_vcpu *vcpu, struct kvm_run *run,int kvm_perf_init (void );int kvm_perf_teardown (void ); +int kvm_hypercall_pv_features (struct kvm_vcpu *vcpu); +void kvm_set_sei_esr (struct kvm_vcpu *vcpu,u64 syndrome);struct kvm_vcpu *kvm_mpidr_to_vcpu (struct kvm *kvm,unsigned long mpidr);
arch/arm64/include/asm/pvclock-abi.h new file +17 -0
@@ -0,0 +1,17 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* Copyright (C) 2019 Arm Ltd. */ + +#ifndef __ASM_PVCLOCK_ABI_H +#define __ASM_PVCLOCK_ABI_H + +/* The below structure is defined in ARM DEN0057A */ + +struct pvclock_vcpu_stolen_time { +__le32 revision; +__le32 attributes; +__le64 stolen_time; +/* Structure must be 64 byte aligned, pad to that size */ +u8 padding[48 ]; +} __packed; + +#endif
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/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 +6 -0
@@ -48,8 +48,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 : + val =kvm_hypercall_pv_features (vcpu); +break ;default :return kvm_psci_call (vcpu); }
virt/kvm/arm/pvtime.c new file +21 -0
@@ -0,0 +1,21 @@ +// SPDX-License-Identifier: GPL-2.0 +// Copyright (C) 2019 Arm Ltd. + +#include <linux/arm-smccc.h> + +#include <kvm/arm_hypercalls.h> + +int kvm_hypercall_pv_features (struct kvm_vcpu *vcpu) +{ +u32 feature =smccc_get_arg1 (vcpu); +u32 val =SMCCC_RET_NOT_SUPPORTED ; + +switch (feature) { +case ARM_SMCCC_HV_PV_FEATURES : + val =SMCCC_RET_SUCCESS ; +break ; + } + +return val; +} +
Signature · 1 line
2.20.1
PATCH v3 4/10 KVM: Implement kvm_put_guest()
In reply to Steven Pricekvm_put_guest() is analogous to put_user() - it writes a single value to the guest physical address. The implementation is built upon put_user() and so it has the same single copy atomic properties.
--- include/linux/kvm_host.h | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+)
include/linux/kvm_host.h +24 -0
@@ -746,6 +746,30 @@ int kvm_write_guest_offset_cached(struct kvm *kvm, struct gfn_to_hva_cache *ghc, unsigned long len);int kvm_gfn_to_hva_cache_init (struct kvm *kvm,struct gfn_to_hva_cache *ghc,gpa_t gpa,unsigned long len); + +#define __kvm_put_guest (kvm, gfn, offset, value, type) \ +({ \ + unsigned long __addr = gfn_to_hva(kvm, gfn); \ + type __user *__uaddr = (type __user *)(__addr + offset); \ + int __ret = 0; \ + \ + if (kvm_is_error_hva(__addr)) \ + __ret = -EFAULT; \ + else \ + __ret = put_user(value, __uaddr); \ + if (!__ret) \ + mark_page_dirty(kvm, gfn); \ + __ret; \ +}) + +#define kvm_put_guest (kvm, gpa, value, type) \ +({ \ + gpa_t __gpa = gpa; \ + struct kvm *__kvm = kvm; \ + __kvm_put_guest(__kvm, __gpa >> PAGE_SHIFT, \ + offset_in_page(__gpa), (value), type); \ +}) +int kvm_clear_guest_page (struct kvm *kvm,gfn_t gfn,int offset,int len);int kvm_clear_guest (struct kvm *kvm,gpa_t gpa,unsigned long len);struct kvm_memory_slot *gfn_to_memslot (struct kvm *kvm,gfn_t gfn);
Signature · 1 line
2.20.1
Re: [PATCH v3 04/10] KVM: Implement kvm_put_guest()
Quoted 14 lines · On Wed, 21 Aug 2019 16:36:50 +0100 Steven Price <[email protected]> wrote:
kvm_put_guest()is analogous toput_user()- it writes a single value to the guest physical address. The implementation is built uponput_user()and so it has the same single copy atomic properties.Signed-off-by Steven Price <[email protected]>--- include/linux/kvm_host.h | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+)include/linux/kvm_host.h +0
-0
include/linux/kvm_host.h int kvm_write_guest_offset_cached(struct kvm *kvm, struct gfn_to_hva_cache *ghc, · line 754
Quoted 9 lines
include/linux/kvm_host.h +6
-0@@ -746,6 +746,30 @@ int kvm_write_guest_offset_cached(struct kvm *kvm, struct gfn_to_hva_cache *ghc, unsigned long len);int kvm_gfn_to_hva_cache_init (struct kvm *kvm,struct gfn_to_hva_cache *ghc,gpa_t gpa,unsigned long len); + +#define __kvm_put_guest (kvm, gfn, offset, value, type) \ +({ \ + unsigned long __addr = gfn_to_hva(kvm, gfn); \ + type __user *__uaddr = (type __user *)(__addr + offset); \ + int __ret = 0; \
Why initialize __ret?
Quoted 21 lines
include/linux/kvm_host.h +18
-0+ \ +if (kvm_is_error_hva (__addr)) \ + __ret = -EFAULT ; \ +else \ + __ret =put_user (value, __uaddr); \ +if (!__ret) \ +mark_page_dirty (kvm, gfn); \ + __ret; \ +}) + +#define kvm_put_guest (kvm, gpa, value, type) \ +({ \ + gpa_t __gpa = gpa; \ + struct kvm *__kvm = kvm; \ + __kvm_put_guest(__kvm, __gpa >> PAGE_SHIFT, \ + offset_in_page(__gpa), (value), type); \ +}) +int kvm_clear_guest_page (struct kvm *kvm,gfn_t gfn,int offset,int len);int kvm_clear_guest (struct kvm *kvm,gpa_t gpa,unsigned long len);struct kvm_memory_slot *gfn_to_memslot (struct kvm *kvm,gfn_t gfn);
Re: [PATCH v3 04/10] KVM: Implement kvm_put_guest()
Quoted 12 lines · Jonathan Cameron
Quoted 9 lines · On Wed, 21 Aug 2019 16:36:50 +0100 Steven Price <[email protected]> wrote:
kvm_put_guest()is analogous toput_user()- it writes a single value to the guest physical address. The implementation is built uponput_user()and so it has the same single copy atomic properties.Signed-off-by Steven Price <[email protected]>--- include/linux/kvm_host.h | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+)
Quoted 16 lines
Quoted 5 lines
include/linux/kvm_host.h +0
-0include/linux/kvm_host.h int kvm_write_guest_offset_cached(struct kvm *kvm, struct gfn_to_hva_cache *ghc, · line 754
Quoted 9 lines
include/linux/kvm_host.h +6
-0@@ -746,6 +746,30 @@ int kvm_write_guest_offset_cached(struct kvm *kvm, struct gfn_to_hva_cache *ghc, unsigned long len);int kvm_gfn_to_hva_cache_init (struct kvm *kvm,struct gfn_to_hva_cache *ghc,gpa_t gpa,unsigned long len); + +#define __kvm_put_guest (kvm, gfn, offset, value, type) \ +({ \ + unsigned long __addr = gfn_to_hva(kvm, gfn); \ + type __user *__uaddr = (type __user *)(__addr + offset); \ + int __ret = 0; \Why initialize
__ret?
Good question. Actually looking at this again if I reorder this to be pessimistic I can make it shorter:
int __ret = -EFAULT; if (!kvm_is_error_hva(__addr)) __ret = put_user(value, __uaddr); if (!__ret) mark_page_dirty(kvm, gfn); __ret;
Thanks for taking a look.
Steve
Quoted 29 lines
Quoted 11 lines
include/linux/kvm_host.h +11
-0+ \ +if (kvm_is_error_hva (__addr)) \ + __ret = -EFAULT ; \ +else \ + __ret =put_user (value, __uaddr); \ +if (!__ret) \ +mark_page_dirty (kvm, gfn); \ + __ret; \ +}) + +#define kvm_put_guest (kvm, gpa, value, type) \include/linux/kvm_host.h int kvm_write_guest_offset_cached(struct kvm *kvm, struct gfn_to_hva_cache *ghc, · line 775
Quoted 10 lines
include/linux/kvm_host.h +7
-0+({ \ +gpa_t __gpa = gpa; \ +struct kvm *__kvm = kvm; \ +__kvm_put_guest (__kvm, __gpa >>PAGE_SHIFT , \ +offset_in_page (__gpa), (value), type); \ +}) +int kvm_clear_guest_page (struct kvm *kvm,gfn_t gfn,int offset,int len);int kvm_clear_guest (struct kvm *kvm,gpa_t gpa,unsigned long len);struct kvm_memory_slot *gfn_to_memslot (struct kvm *kvm,gfn_t gfn);_______________________________________________
linux-arm-kernel mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel
Re: [PATCH v3 04/10] KVM: Implement kvm_put_guest()
In reply to Steven PriceQuoted 3 lines · Steven Price
kvm_put_guest()is analogous toput_user()- it writes a single value to the guest physical address. The implementation is built uponput_user()and so it has the same single copy atomic properties.
What you mean by "single copy atomic"? I.e. what guarantees does put_user() provide that __copy_to_user() does not?
Quoted 44 lines
Signed-off-by Steven Price <[email protected]>--- include/linux/kvm_host.h | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+)include/linux/kvm_host.h +24
-0@@ -746,6 +746,30 @@ int kvm_write_guest_offset_cached(struct kvm *kvm, struct gfn_to_hva_cache *ghc, unsigned long len);int kvm_gfn_to_hva_cache_init (struct kvm *kvm,struct gfn_to_hva_cache *ghc,gpa_t gpa,unsigned long len); + +#define __kvm_put_guest (kvm, gfn, offset, value, type) \ +({ \ + unsigned long __addr = gfn_to_hva(kvm, gfn); \ + type __user *__uaddr = (type __user *)(__addr + offset); \ + int __ret = 0; \ + \ + if (kvm_is_error_hva(__addr)) \ + __ret = -EFAULT; \ + else \ + __ret = put_user(value, __uaddr); \ + if (!__ret) \ + mark_page_dirty(kvm, gfn); \ + __ret; \ +}) + +#define kvm_put_guest (kvm, gpa, value, type) \ +({ \ + gpa_t __gpa = gpa; \ + struct kvm *__kvm = kvm; \ + __kvm_put_guest(__kvm, __gpa >> PAGE_SHIFT, \ + offset_in_page(__gpa), (value), type); \ +}) +int kvm_clear_guest_page (struct kvm *kvm,gfn_t gfn,int offset,int len);int kvm_clear_guest (struct kvm *kvm,gpa_t gpa,unsigned long len);struct kvm_memory_slot *gfn_to_memslot (struct kvm *kvm,gfn_t gfn);Signature · 1 line
2.20.1
Re: [PATCH v3 04/10] KVM: Implement kvm_put_guest()
Quoted 7 lines · Sean Christopherson
Quoted 3 lines · Steven Price
kvm_put_guest()is analogous toput_user()- it writes a single value to the guest physical address. The implementation is built uponput_user()and so it has the same single copy atomic properties.What you mean by "single copy atomic"? I.e. what guarantees does
put_user()provide that__copy_to_user()does not?
Single-copy atomicity is defined by the Arm architecture[1] and I'm not going to try to go into the full details here, so this is a summary.
For the sake of this feature what we care about is that the value written/read cannot be "torn". In other words if there is a read (in this case from another VCPU) that is racing with the write then the read will either get the old value or the new value. It cannot return a mixture. (This is of course assuming that the read is using a single-copy atomic safe method).
__copy_to_user() is implemented as a memcpy() and as such cannot provide single-copy atomicity in the general case (the buffer could easily be bigger than the architecture can guarantee).
put_user() on the other hand is implemented (on arm64) as an explicit store instruction and therefore is guaranteed by the architecture to be single-copy atomic (i.e. another CPU cannot see a half-written value).
Steve
[1] https://static.docs.arm.com/ddi0487/ea/DDI0487E_a_armv8_arm.pdf#page=110
Re: [PATCH v3 04/10] KVM: Implement kvm_put_guest()
Quoted 12 lines · Steven Price
Quoted 7 lines · Sean Christopherson
Quoted 3 lines · Steven Price
kvm_put_guest()is analogous toput_user()- it writes a single value to the guest physical address. The implementation is built uponput_user()and so it has the same single copy atomic properties.What you mean by "single copy atomic"? I.e. what guarantees does
put_user()provide that__copy_to_user()does not?Single-copy atomicity is defined by the Arm architecture[1] and I'm not going to try to go into the full details here, so this is a summary.
Quoted 6 lines
For the sake of this feature what we care about is that the value written/read cannot be "torn". In other words if there is a read (in this case from another VCPU) that is racing with the write then the read will either get the old value or the new value. It cannot return a mixture. (This is of course assuming that the read is using a single-copy atomic safe method).
Thanks for the explanation. I assumed that's what you were referring to, but wanted to double check.
Quoted 7 lines
__copy_to_user()is implemented as amemcpy()and as such cannot provide single-copy atomicity in the general case (the buffer could easily be bigger than the architecture can guarantee).
put_user()on the other hand is implemented (on arm64) as an explicit store instruction and therefore is guaranteed by the architecture to be single-copy atomic (i.e. another CPU cannot see a half-written value).
I don't think kvm_put_guest() belongs in generic code, at least not with the current changelog explanation about it providing single-copy atomic semantics. AFAICT, the single-copy thing is very much an arm64 implementation detail, e.g. the vast majority of 32-bit architectures, including x86, do not provide any guarantees, and x86-64 generates more or less the same code for put_user() and __copy_to_user() for 8-byte and smaller accesses.
As an alternative to kvm_put_guest() entirely, is it an option to change arm64's raw_copy_to_user() to redirect to __put_user() for sizes that are constant at compile time and can be handled by __put_user()? That would allow using kvm_write_guest() to update stolen time, albeit with arguably an even bigger dependency on the uaccess implementation details.
Re: [PATCH v3 04/10] KVM: Implement kvm_put_guest()
Quoted 39 lines · Sean Christopherson
Quoted 12 lines · Steven Price
Quoted 7 lines · Sean Christopherson
Quoted 3 lines · Steven Price
kvm_put_guest()is analogous toput_user()- it writes a single value to the guest physical address. The implementation is built uponput_user()and so it has the same single copy atomic properties.What you mean by "single copy atomic"? I.e. what guarantees does
put_user()provide that__copy_to_user()does not?Single-copy atomicity is defined by the Arm architecture[1] and I'm not going to try to go into the full details here, so this is a summary.
Quoted 6 lines
For the sake of this feature what we care about is that the value written/read cannot be "torn". In other words if there is a read (in this case from another VCPU) that is racing with the write then the read will either get the old value or the new value. It cannot return a mixture. (This is of course assuming that the read is using a single-copy atomic safe method).
Thanks for the explanation. I assumed that's what you were referring to, but wanted to double check.
Quoted 7 lines
__copy_to_user()is implemented as amemcpy()and as such cannot provide single-copy atomicity in the general case (the buffer could easily be bigger than the architecture can guarantee).
put_user()on the other hand is implemented (on arm64) as an explicit store instruction and therefore is guaranteed by the architecture to be single-copy atomic (i.e. another CPU cannot see a half-written value).I don't think
kvm_put_guest()belongs in generic code, at least not with the current changelog explanation about it providing single-copy atomic semantics. AFAICT, the single-copy thing is very much an arm64 implementation detail, e.g. the vast majority of 32-bit architectures, including x86, do not provide any guarantees, and x86-64 generates more or less the same code forput_user()and__copy_to_user()for 8-byte and smaller accesses.
Quoted 5 lines
As an alternative to
kvm_put_guest()entirely, is it an option to change arm64'sraw_copy_to_user()to redirect to__put_user()for sizes that are constant at compile time and can be handled by__put_user()? That would allow usingkvm_write_guest()to update stolen time, albeit with arguably an even bigger dependency on the uaccess implementation details.
I think it's important to in some way ensure that the desire that this is a single write is shown. copy_to_user() is effectively "setup();memcpy();finish();" and while a good memcpy() implementation would be identical to put_user() there's a lot more room for this being broken in the future by changes to the memcpy() implementation. (And I don't want to require that memcpy() has to detect this case).
One suggestion is to call it something like kvm_put_guest_atomic() to reflect the atomicity requirement. Presumably that would be based on a new put_user_atomic() which architectures could override as necessary if put_user() doesn't provide the necessary guarantees.
Steve
PATCH v3 5/10 KVM: arm64: Support stolen time reporting via shared structure
In reply to Steven PriceImplement the service call for configuring a shared structure 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 updates the shared structure using kvm_put_guest() to ensure single copy atomicity of the 64-bit value reporting the 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/arm/include/asm/kvm_host.h | 20 +++++++++ arch/arm64/include/asm/kvm_host.h | 25 +++++++++++- arch/arm64/kvm/Kconfig | 1 + include/linux/kvm_types.h | 2 + virt/kvm/arm/arm.c | 10 +++++ virt/kvm/arm/hypercalls.c | 3 ++ virt/kvm/arm/pvtime.c | 67 +++++++++++++++++++++++++++++++ 7 files changed, 127 insertions(+), 1 deletion(-)
arch/arm/include/asm/kvm_host.h +20 -0
@@ -39,6 +39,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);
@@ -329,6 +330,25 @@ static inline int kvm_hypercall_pv_features(struct kvm_vcpu *vcpu)return SMCCC_RET_NOT_SUPPORTED ; } +static inline int kvm_hypercall_stolen_time (struct kvm_vcpu *vcpu) +{ +return SMCCC_RET_NOT_SUPPORTED ; +} + +static inline int kvm_update_stolen_time (struct kvm_vcpu *vcpu,bool init) +{ +return -ENOTSUPP ; +} + +static inline void kvm_pvtime_init_vm (struct kvm_arch *kvm_arch) +{ +} + +static inline bool kvm_is_pvtime_enabled (struct kvm_arch *kvm_arch) +{ +return false; +} +void kvm_mmu_wp_memory_region (struct kvm *kvm,int slot);struct kvm_vcpu *kvm_mpidr_to_vcpu (struct kvm *kvm,unsigned long mpidr);
arch/arm64/include/asm/kvm_host.h +24 -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 { +gpa_t st_base; +u64 st_size; + } 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)))
@@ -479,6 +490,18 @@ int kvm_perf_init(void);int kvm_perf_teardown (void );int kvm_hypercall_pv_features (struct kvm_vcpu *vcpu); +int kvm_hypercall_stolen_time (struct kvm_vcpu *vcpu); +int kvm_update_stolen_time (struct kvm_vcpu *vcpu,bool init); + +static inline void kvm_pvtime_init_vm (struct kvm_arch *kvm_arch) +{ + kvm_arch->pvtime.st_base =GPA_INVALID ; +} + +static inline bool kvm_is_pvtime_enabled (struct kvm_arch *kvm_arch) +{ +return (kvm_arch->pvtime.st_base !=GPA_INVALID ); +}void kvm_set_sei_esr (struct kvm_vcpu *vcpu,u64 syndrome);
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/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 +10 -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_pvtime_init_vm (&kvm->arch);return ret; out_free_stage2_pgd:kvm_free_stage2_pgd (kvm);
@@ -379,6 +384,8 @@ 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); +if (kvm_is_pvtime_enabled (&vcpu->kvm->arch)) +kvm_make_request (KVM_REQ_RECORD_STEAL , vcpu);if (single_task_running ())vcpu_clear_wfe_traps (vcpu);
@@ -644,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)) +kvm_update_stolen_time (vcpu, false); } }
virt/kvm/arm/hypercalls.c +3 -0
@@ -56,6 +56,9 @@ int kvm_hvc_call_handler(struct kvm_vcpu *vcpu)case ARM_SMCCC_HV_PV_FEATURES : val =kvm_hypercall_pv_features (vcpu);break ; +case ARM_SMCCC_HV_PV_TIME_ST : + val =kvm_hypercall_stolen_time (vcpu); +break ;default :return kvm_psci_call (vcpu); }
virt/kvm/arm/pvtime.c +67 -0
84 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
Re: [PATCH v3 05/10] KVM: arm64: Support stolen time reporting via shared structure
Quoted 9 lines · On Wed, 21 Aug 2019 16:36:51 +0100 Steven Price <[email protected]> wrote:
Implement the service call for configuring a shared structure 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 updates the shared structure using
kvm_put_guest()to ensure single copy atomicity of the 64-bit value reporting the stolen time in nanoseconds.
Quoted 8 lines
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]>
One totally trivial comment inline... Feel free to ignore :)
Quoted 261 lines
--- arch/arm/include/asm/kvm_host.h | 20 +++++++++ arch/arm64/include/asm/kvm_host.h | 25 +++++++++++- arch/arm64/kvm/Kconfig | 1 + include/linux/kvm_types.h | 2 + virt/kvm/arm/arm.c | 10 +++++ virt/kvm/arm/hypercalls.c | 3 ++ virt/kvm/arm/pvtime.c | 67 +++++++++++++++++++++++++++++++ 7 files changed, 127 insertions(+), 1 deletion(-)arch/arm/include/asm/kvm_host.h +20
-0@@ -39,6 +39,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);@@ -329,6 +330,25 @@ static inline int kvm_hypercall_pv_features(struct kvm_vcpu *vcpu)return SMCCC_RET_NOT_SUPPORTED ; } +static inline int kvm_hypercall_stolen_time (struct kvm_vcpu *vcpu) +{ +return SMCCC_RET_NOT_SUPPORTED ; +} + +static inline int kvm_update_stolen_time (struct kvm_vcpu *vcpu,bool init) +{ +return -ENOTSUPP ; +} + +static inline void kvm_pvtime_init_vm (struct kvm_arch *kvm_arch) +{ +} + +static inline bool kvm_is_pvtime_enabled (struct kvm_arch *kvm_arch) +{ +return false; +} +void kvm_mmu_wp_memory_region (struct kvm *kvm,int slot);struct kvm_vcpu *kvm_mpidr_to_vcpu (struct kvm *kvm,unsigned long mpidr);arch/arm64/include/asm/kvm_host.h +24
-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 { +gpa_t st_base; +u64 st_size; + } 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)))@@ -479,6 +490,18 @@ int kvm_perf_init(void);int kvm_perf_teardown (void );int kvm_hypercall_pv_features (struct kvm_vcpu *vcpu); +int kvm_hypercall_stolen_time (struct kvm_vcpu *vcpu); +int kvm_update_stolen_time (struct kvm_vcpu *vcpu,bool init); + +static inline void kvm_pvtime_init_vm (struct kvm_arch *kvm_arch) +{ + kvm_arch->pvtime.st_base =GPA_INVALID ; +} + +static inline bool kvm_is_pvtime_enabled (struct kvm_arch *kvm_arch) +{ +return (kvm_arch->pvtime.st_base !=GPA_INVALID ); +}void kvm_set_sei_esr (struct kvm_vcpu *vcpu,u64 syndrome);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/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 +10
-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_pvtime_init_vm (&kvm->arch);return ret; out_free_stage2_pgd:kvm_free_stage2_pgd (kvm);@@ -379,6 +384,8 @@ 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); +if (kvm_is_pvtime_enabled (&vcpu->kvm->arch)) +kvm_make_request (KVM_REQ_RECORD_STEAL , vcpu);if (single_task_running ())vcpu_clear_wfe_traps (vcpu);@@ -644,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)) +kvm_update_stolen_time (vcpu, false); } }virt/kvm/arm/hypercalls.c +3
-0@@ -56,6 +56,9 @@ int kvm_hvc_call_handler(struct kvm_vcpu *vcpu)case ARM_SMCCC_HV_PV_FEATURES : val =kvm_hypercall_pv_features (vcpu);break ; +case ARM_SMCCC_HV_PV_TIME_ST : + val =kvm_hypercall_stolen_time (vcpu); +break ;default :return kvm_psci_call (vcpu); }virt/kvm/arm/pvtime.c +50
-0@@ -3,8 +3,51 @@#include <linux/arm-smccc.h> +#include <asm/pvclock-abi.h> +#include <kvm/arm_hypercalls.h> +int kvm_update_stolen_time (struct kvm_vcpu *vcpu,bool init) +{ +struct kvm *kvm = vcpu->kvm; +struct kvm_arch_pvtime *pvtime = &kvm->arch.pvtime; +u64 steal; +u64 steal_le; +u64 offset; +int idx; +const int stride =sizeof (struct pvclock_vcpu_stolen_time ); + +if (pvtime->st_base ==GPA_INVALID ) +return -ENOTSUPP ; + +/* 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; + + offset = stride *kvm_vcpu_get_idx (vcpu); + +if (unlikely (offset + stride > pvtime->st_size)) +return -EINVAL ; + + steal_le =cpu_to_le64 (steal); + idx =srcu_read_lock (&kvm->srcu); +if (init) { +struct pvclock_vcpu_stolen_time init_values = { + .revision =0 , + .attributes =0 + }; +kvm_write_guest (kvm, pvtime->st_base + offset, &init_values, +sizeof (init_values)); + } + offset += offsetof(struct pvclock_vcpu_stolen_time , stolen_time); +kvm_put_guest (kvm, pvtime->st_base + offset, steal_le, u64); +srcu_read_unlock (&kvm->srcu, idx); + +return 0 ; +} +int kvm_hypercall_pv_features (struct kvm_vcpu *vcpu) {u32 feature =smccc_get_arg1 (vcpu);@@ -12,6 +55,7 @@ int kvm_hypercall_pv_features(struct kvm_vcpu *vcpu)switch (feature) {case ARM_SMCCC_HV_PV_FEATURES : +case ARM_SMCCC_HV_PV_TIME_ST : val =SMCCC_RET_SUCCESS ;break ; }@@ -19,3 +63,26 @@ int kvm_hypercall_pv_features(struct kvm_vcpu *vcpu)return val; } +int kvm_hypercall_stolen_time (struct kvm_vcpu *vcpu) +{ +u64 ret; +int err; + +/*
virt/kvm/arm/pvtime.c int kvm_hypercall_pv_features(struct kvm_vcpu *vcpu) · line 81
Quoted 10 lines
virt/kvm/arm/pvtime.c +10
-0+* 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, true); + +if (err) + ret =SMCCC_RET_NOT_SUPPORTED ;
Trivial by why not
return SMCCC_RET_NOT_SUPPORTED; return vcpu->kvm->arch.pvtime.st_base +
...
Drops the indentation a bit and puts the error handling out of line which is slightly nicer to read (to my eyes).
Quoted 7 lines
virt/kvm/arm/pvtime.c +7
-0+else + ret = vcpu->kvm->arch.pvtime.st_base + + (sizeof (struct pvclock_vcpu_stolen_time ) * +kvm_vcpu_get_idx (vcpu)); + +return ret; +}
Re: [PATCH v3 05/10] KVM: arm64: Support stolen time reporting via shared structure
Quoted 23 lines · Jonathan Cameron
Quoted 9 lines · On Wed, 21 Aug 2019 16:36:51 +0100 Steven Price <[email protected]> wrote:
Implement the service call for configuring a shared structure 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 updates the shared structure using
kvm_put_guest()to ensure single copy atomicity of the 64-bit value reporting the stolen time in nanoseconds.Quoted 8 lines
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]>One totally trivial comment inline... Feel free to ignore :)
[...]
Quoted 18 lines
Quoted 6 lines
virt/kvm/arm/pvtime.c +6
-0+int kvm_hypercall_stolen_time (struct kvm_vcpu *vcpu) +{ +u64 ret; +int err; + +/* virt/kvm/arm/pvtime.c int kvm_hypercall_pv_features(struct kvm_vcpu *vcpu) · line 81
Quoted 10 lines
virt/kvm/arm/pvtime.c +10
-0+* 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, true); + +if (err) + ret =SMCCC_RET_NOT_SUPPORTED ;Trivial by why not
Quoted 6 lines
return SMCCC_RET_NOT_SUPPORTED; return vcpu->kvm->arch.pvtime.st_base +...
Drops the indentation a bit and puts the error handling out of line which is slightly nicer to read (to my eyes).
Yes that's a nice change - drops the extra "ret" variable too.
Thanks,
Steve
Zenghui Yu
reviewerRe: [PATCH v3 05/10] KVM: arm64: Support stolen time reporting via shared structure
In reply to Steven PriceHi Steven,
Only one comment, at the bottom.
Quoted 284 lines · Steven Price
Implement the service call for configuring a shared structure 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 updates the shared structure using
kvm_put_guest()to ensure single copy atomicity of the 64-bit value reporting the 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/arm/include/asm/kvm_host.h | 20 +++++++++ arch/arm64/include/asm/kvm_host.h | 25 +++++++++++- arch/arm64/kvm/Kconfig | 1 + include/linux/kvm_types.h | 2 + virt/kvm/arm/arm.c | 10 +++++ virt/kvm/arm/hypercalls.c | 3 ++ virt/kvm/arm/pvtime.c | 67 +++++++++++++++++++++++++++++++ 7 files changed, 127 insertions(+), 1 deletion(-)arch/arm/include/asm/kvm_host.h +20
-0@@ -39,6 +39,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);@@ -329,6 +330,25 @@ static inline int kvm_hypercall_pv_features(struct kvm_vcpu *vcpu)return SMCCC_RET_NOT_SUPPORTED ; } +static inline int kvm_hypercall_stolen_time (struct kvm_vcpu *vcpu) +{ +return SMCCC_RET_NOT_SUPPORTED ; +} + +static inline int kvm_update_stolen_time (struct kvm_vcpu *vcpu,bool init) +{ +return -ENOTSUPP ; +} + +static inline void kvm_pvtime_init_vm (struct kvm_arch *kvm_arch) +{ +} + +static inline bool kvm_is_pvtime_enabled (struct kvm_arch *kvm_arch) +{ +return false; +} +void kvm_mmu_wp_memory_region (struct kvm *kvm,int slot);struct kvm_vcpu *kvm_mpidr_to_vcpu (struct kvm *kvm,unsigned long mpidr);arch/arm64/include/asm/kvm_host.h +24
-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 { +gpa_t st_base; +u64 st_size; + } 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)))@@ -479,6 +490,18 @@ int kvm_perf_init(void);int kvm_perf_teardown (void );int kvm_hypercall_pv_features (struct kvm_vcpu *vcpu); +int kvm_hypercall_stolen_time (struct kvm_vcpu *vcpu); +int kvm_update_stolen_time (struct kvm_vcpu *vcpu,bool init); + +static inline void kvm_pvtime_init_vm (struct kvm_arch *kvm_arch) +{ + kvm_arch->pvtime.st_base =GPA_INVALID ; +} + +static inline bool kvm_is_pvtime_enabled (struct kvm_arch *kvm_arch) +{ +return (kvm_arch->pvtime.st_base !=GPA_INVALID ); +}void kvm_set_sei_esr (struct kvm_vcpu *vcpu,u64 syndrome);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/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 +10
-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_pvtime_init_vm (&kvm->arch);return ret; out_free_stage2_pgd:kvm_free_stage2_pgd (kvm);@@ -379,6 +384,8 @@ 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); +if (kvm_is_pvtime_enabled (&vcpu->kvm->arch)) +kvm_make_request (KVM_REQ_RECORD_STEAL , vcpu);if (single_task_running ())vcpu_clear_wfe_traps (vcpu);@@ -644,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)) +kvm_update_stolen_time (vcpu, false); } }virt/kvm/arm/hypercalls.c +3
-0@@ -56,6 +56,9 @@ int kvm_hvc_call_handler(struct kvm_vcpu *vcpu)case ARM_SMCCC_HV_PV_FEATURES : val =kvm_hypercall_pv_features (vcpu);break ; +case ARM_SMCCC_HV_PV_TIME_ST : + val =kvm_hypercall_stolen_time (vcpu); +break ;default :return kvm_psci_call (vcpu); }virt/kvm/arm/pvtime.c +56
-0@@ -3,8 +3,51 @@#include <linux/arm-smccc.h> +#include <asm/pvclock-abi.h> +#include <kvm/arm_hypercalls.h> +int kvm_update_stolen_time (struct kvm_vcpu *vcpu,bool init) +{ +struct kvm *kvm = vcpu->kvm; +struct kvm_arch_pvtime *pvtime = &kvm->arch.pvtime; +u64 steal; +u64 steal_le; +u64 offset; +int idx; +const int stride =sizeof (struct pvclock_vcpu_stolen_time ); + +if (pvtime->st_base ==GPA_INVALID ) +return -ENOTSUPP ; + +/* 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; + + offset = stride *kvm_vcpu_get_idx (vcpu); + +if (unlikely (offset + stride > pvtime->st_size)) +return -EINVAL ; + + steal_le =cpu_to_le64 (steal); + idx =srcu_read_lock (&kvm->srcu); +if (init) { +struct pvclock_vcpu_stolen_time init_values = { + .revision =0 , + .attributes =0 + }; +kvm_write_guest (kvm, pvtime->st_base + offset, &init_values, +sizeof (init_values)); + } + offset += offsetof(struct pvclock_vcpu_stolen_time , stolen_time); +kvm_put_guest (kvm, pvtime->st_base + offset, steal_le, u64); +srcu_read_unlock (&kvm->srcu, idx); + +return 0 ; +} +int kvm_hypercall_pv_features (struct kvm_vcpu *vcpu) {u32 feature =smccc_get_arg1 (vcpu);@@ -12,6 +55,7 @@ int kvm_hypercall_pv_features(struct kvm_vcpu *vcpu)switch (feature) {case ARM_SMCCC_HV_PV_FEATURES : +case ARM_SMCCC_HV_PV_TIME_ST : val =SMCCC_RET_SUCCESS ;break ; }@@ -19,3 +63,26 @@ int kvm_hypercall_pv_features(struct kvm_vcpu *vcpu)return val; } +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; +
virt/kvm/arm/pvtime.c int kvm_hypercall_pv_features(struct kvm_vcpu *vcpu) · line 87
Quoted 10 lines
virt/kvm/arm/pvtime.c +10
-0+ err =kvm_update_stolen_time (vcpu, true); + +if (err) + ret =SMCCC_RET_NOT_SUPPORTED ; +else + ret = vcpu->kvm->arch.pvtime.st_base + + (sizeof (struct pvclock_vcpu_stolen_time ) * +kvm_vcpu_get_idx (vcpu)); + +return ret;
The *type* of the 'ret' here looks a bit messy to me:
(1)u64 -> (2)int -> (3)u32 -> (4)unsigned long
(1)->(2): just inside kvm_hypercall_stolen_time() (2)->(3): inside kvm_hvc_call_handler(), assign 'ret' to 'val' (3)->(4): through smccc_set_retval()
I really have seen an issue caused by (2)->(3).
When the PV guest running without PV_TIME device supporting, the result of the ARM_SMCCC_HV_PV_TIME_ST hypercall is expected to be -1 (which means "not supported"), but the actual result I got is 4294967295. Guest continues to run blindly, bad things would happen then...
I think this needs a fix?
Thanks,
zenghui
_______________________________________________
linux-arm-kernel mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel
Re: [PATCH v3 05/10] KVM: arm64: Support stolen time reporting via shared structure
Quoted 296 lines · Zenghui Yu
Hi Steven,
Only one comment, at the bottom.
Quoted 273 lines · Steven Price
Implement the service call for configuring a shared structure 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 updates the shared structure using
kvm_put_guest()to ensure single copy atomicity of the 64-bit value reporting the 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/arm/include/asm/kvm_host.h | 20 +++++++++arch/arm64/include/asm/kvm_host.h| 25 +++++++++++- arch/arm64/kvm/Kconfig | 1 + include/linux/kvm_types.h | 2 + virt/kvm/arm/arm.c | 10 +++++ virt/kvm/arm/hypercalls.c | 3 ++ virt/kvm/arm/pvtime.c | 67 +++++++++++++++++++++++++++++++7 files changed, 127 insertions(+), 1 deletion(-)arch/arm/include/asm/kvm_host.h +1
-0@@ -39,6 +39,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);@@ -329,6 +330,25 @@ static inline int
kvm_hypercall_pv_features(struct kvm_vcpu *vcpu)
returnSMCCC_RET_NOT_SUPPORTED;
}
+static inline intkvm_hypercall_stolen_time(struct kvm_vcpu *vcpu)arch/arm/include/asm/kvm_host.h +5
-0+{ + return SMCCC_RET_NOT_SUPPORTED; +} + +static inline int kvm_update_stolen_time(struct kvm_vcpu *vcpu, boolinit)
arch/arm/include/asm/kvm_host.h +13
-0+{ + return -ENOTSUPP; +} + +static inline void kvm_pvtime_init_vm (struct kvm_arch *kvm_arch) +{ +} + +static inline bool kvm_is_pvtime_enabled (struct kvm_arch *kvm_arch) +{ + return false; +} +void
kvm_mmu_wp_memory_region(struct kvm *kvm, int slot); structkvm_vcpu*kvm_mpidr_to_vcpu(struct kvm *kvm, unsigned long mpidr);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 { /* Mandated version of PSCI */u32
psci_version;
+
+ structkvm_arch_pvtime{
+gpa_tst_base;
+ u64st_size;
+ } pvtime;
};
#define KVM_NR_MEM_OBJS 40arch/arm64/include/asm/kvm_host.h +0
-1@@ -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;
+ u64last_steal;
+ } steal;
+};
/* Pointer to the vcpu's SVE FFR for sve_{save,load}_state() */ #definevcpu_sve_pffr(vcpu)((void *)((char *)((vcpu)->arch.sve_state) + \
sve_ffr_offset((vcpu)->arch.sve_max_vl)))arch/arm64/include/asm/kvm_host.h +12
-0@@ -479,6 +490,18 @@ int kvm_perf_init(void); int kvm_perf_teardown(void); int kvm_hypercall_pv_features(struct kvm_vcpu *vcpu); +int kvm_hypercall_stolen_time (struct kvm_vcpu *vcpu); +int kvm_update_stolen_time (struct kvm_vcpu *vcpu,bool init); + +static inline void kvm_pvtime_init_vm (struct kvm_arch *kvm_arch) +{ + kvm_arch->pvtime.st_base = GPA_INVALID; +} + +static inline bool kvm_is_pvtime_enabled (struct kvm_arch *kvm_arch) +{ + return (kvm_arch->pvtime.st_base != GPA_INVALID); +} void kvm_set_sei_esr(struct kvm_vcpu *vcpu, u64 syndrome);diff --git a/arch/arm64/kvm/Kconfig b/arch/arm64/kvm/Kconfig index
a67121d419a2..d8b88e40d223100644arch/arm64/kvm/Kconfig +0
-0@@ -39,6 +39,7 @@ config KVMselect
IRQ_BYPASS_MANAGER
selectHAVE_KVM_IRQ_BYPASS
selectHAVE_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/linux/kvm_types.h +1
-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 +0
-0@@ -40,6 +40,10 @@ #include <asm/kvm_coproc.h> #include <asm/sections.h>+#include <
kvm/arm_hypercalls.h>virt/kvm/arm/arm.c +3
-0+#include <kvm/arm_pmu.h> +#include <kvm/arm_psci.h> +#ifdef
REQUIRES_VIRT
__asm__(".arch_extension virt");
#endifvirt/kvm/arm/arm.c +0
-0@@ -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_pvtime_init_vm(&kvm->arch);
return ret;
out_free_stage2_pgd:
kvm_free_stage2_pgd(kvm);virt/kvm/arm/arm.c +5
-0@@ -379,6 +384,8 @@ 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); + if (kvm_is_pvtime_enabled(&vcpu->kvm->arch)) + kvm_make_request(KVM_REQ_RECORD_STEAL, vcpu); if (single_task_running()) vcpu_clear_wfe_traps(vcpu);@@ -644,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)) + kvm_update_stolen_time(vcpu, false); } }diff --git
a/virt/kvm/arm/hypercalls.cb/virt/kvm/arm/hypercalls.cindex63ae629c466a..ac678eabf15f100644virt/kvm/arm/hypercalls.c +0
-0@@ -56,6 +56,9 @@ int kvm_hvc_call_handler(struct kvm_vcpu *vcpu)case
ARM_SMCCC_HV_PV_FEATURES:
val =kvm_hypercall_pv_features(vcpu);
break;
+ caseARM_SMCCC_HV_PV_TIME_ST:
+ val =kvm_hypercall_stolen_time(vcpu);
+ break;
default:
returnkvm_psci_call(vcpu);
}virt/kvm/arm/pvtime.c +0
-0@@ -3,8 +3,51 @@ #include <linux/arm-smccc.h>+#include <
asm/pvclock-abi.h>
+
#include <kvm/arm_hypercalls.h>
+intkvm_update_stolen_time(struct kvm_vcpu *vcpu, bool init)virt/kvm/arm/pvtime.c +15
-0+{ + struct kvm *kvm = vcpu->kvm; + struct kvm_arch_pvtime *pvtime = &kvm->arch.pvtime; + u64 steal; + u64 steal_le; + u64 offset; + int idx; + const int stride = sizeof(struct pvclock_vcpu_stolen_time); + + if (pvtime->st_base == GPA_INVALID) + return -ENOTSUPP; + + /* 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;virt/kvm/arm/pvtime.c +23
-0+ + offset = stride * kvm_vcpu_get_idx(vcpu); + + if (unlikely(offset + stride > pvtime->st_size)) + return -EINVAL; + + steal_le = cpu_to_le64(steal); + idx = srcu_read_lock(&kvm->srcu); + if (init) { + struct pvclock_vcpu_stolen_time init_values = { + .revision = 0, + .attributes = 0 + }; + kvm_write_guest(kvm, pvtime->st_base + offset, &init_values, + sizeof(init_values)); + } + offset += offsetof(struct pvclock_vcpu_stolen_time, stolen_time); + kvm_put_guest(kvm, pvtime->st_base + offset, steal_le, u64); + srcu_read_unlock(&kvm->srcu, idx); + + return 0; +} +int
kvm_hypercall_pv_features(struct kvm_vcpu *vcpu){
u32 feature =smccc_get_arg1(vcpu);virt/kvm/arm/pvtime.c +0
-0@@ -12,6 +55,7 @@ int kvm_hypercall_pv_features(struct kvm_vcpu *vcpu) switch (feature) {case
ARM_SMCCC_HV_PV_FEATURES:
+ caseARM_SMCCC_HV_PV_TIME_ST:
val =SMCCC_RET_SUCCESS;
break;
}virt/kvm/arm/pvtime.c +11
-0@@ -19,3 +63,26 @@ int kvm_hypercall_pv_features(struct kvm_vcpu *vcpu) return val; } +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; +virt/kvm/arm/pvtime.c int kvm_hypercall_pv_features(struct kvm_vcpu *vcpu) · line 86
Quoted 10 lines
virt/kvm/arm/pvtime.c +10
-0+ err = kvm_update_stolen_time(vcpu, true); + + if (err) + ret = SMCCC_RET_NOT_SUPPORTED; + else + ret = vcpu->kvm->arch.pvtime.st_base + + (sizeof(struct pvclock_vcpu_stolen_time) * + kvm_vcpu_get_idx(vcpu)); + + return ret;The *type* of the 'ret' here looks a bit messy to me:
(1)u64 -> (2)int -> (3)u32 -> (4)unsigned long(1)->(2): just inside
kvm_hypercall_stolen_time()(2)->(3): insidekvm_hvc_call_handler(), assign 'ret' to 'val' (3)->(4): throughsmccc_set_retval()
Quoted 8 lines
I really have seen an issue caused by (2)->(3).
When the PV guest running without
PV_TIMEdevice supporting, the result of theARM_SMCCC_HV_PV_TIME_SThypercall is expected to be -1 (which means "not supported"), but the actual result I got is 4294967295. Guest continues to run blindly, bad things would happen then...I think this needs a fix?
Yes you are entirely right. I'm afraid this happened because I refactored the functions and apparently forgot to update the return type. In a previous version the functions themselves did smccc_set_retval() themselves and the return value was always "1" (the same as kvm_hvc_call_handler()).
The function should really return a "long" and "val" in kvm_hvc_call_handler() should be upgraded to a "long" too - the SMC64/HVC64 calling convention requires error codes to be 64-bit signed integers.
Thanks for spotting this!
Steve
PATCH v3 6/10 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
@@ -1262,7 +1262,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;
@@ -1315,7 +1315,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
@@ -3046,14 +3046,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 ;
@@ -3074,7 +3074,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 v3 7/10 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 page aligned. The guest will discover the address via a hypercall.
--- arch/arm/include/asm/kvm_host.h | 4 ++ arch/arm64/include/asm/kvm_host.h | 1 + arch/arm64/include/uapi/asm/kvm.h | 8 +++ include/uapi/linux/kvm.h | 2 + virt/kvm/arm/arm.c | 1 + virt/kvm/arm/pvtime.c | 94 +++++++++++++++++++++++++++++++ 6 files changed, 110 insertions(+)
arch/arm/include/asm/kvm_host.h +4 -0
@@ -325,6 +325,10 @@ static inline int kvm_arch_vm_ioctl_check_extension(struct kvm *kvm, long ext)int kvm_perf_init (void );int kvm_perf_teardown (void ); +static inline void kvm_pvtime_init (void ) +{ +} +static inline int kvm_hypercall_pv_features (struct kvm_vcpu *vcpu) {return SMCCC_RET_NOT_SUPPORTED ;
arch/arm64/include/asm/kvm_host.h +1 -0
@@ -489,6 +489,7 @@ void handle_exit_early(struct kvm_vcpu *vcpu, struct kvm_run *run,int kvm_perf_init (void );int kvm_perf_teardown (void ); +void kvm_pvtime_init (void );int kvm_hypercall_pv_features (struct kvm_vcpu *vcpu);int kvm_hypercall_stolen_time (struct kvm_vcpu *vcpu);int kvm_update_stolen_time (struct kvm_vcpu *vcpu,bool init);
arch/arm64/include/uapi/asm/kvm.h +8 -0
@@ -367,6 +367,14 @@ 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_REGION 0 +#define KVM_DEV_ARM_PV_TIME_ST 0 +struct kvm_dev_arm_st_region { +__u64 gpa; +__u64 size; +}; +#endif #endif /* __ARM_KVM_H__ */
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/arm.c +1 -0
@@ -1494,6 +1494,7 @@ static int init_subsystems(void)kvm_perf_init ();kvm_coproc_table_init (); +kvm_pvtime_init (); out:on_each_cpu (_kvm_arch_hardware_disable,NULL ,1 );
virt/kvm/arm/pvtime.c +94 -0
104 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
Re: [PATCH v3 07/10] KVM: arm64: Provide a PV_TIME device to user space
Quoted 7 lines · On Wed, 21 Aug 2019 16:36:53 +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 5 lines
The address is given in terms of the physical address visible to the guest and must be page aligned. The guest will discover the address via a hypercall.
Signed-off-by Steven Price <[email protected]>
Hi Steven,
One general question inline. I'm not particularly familiar with this area of the kernel, so maybe I'm missing something obvious, but having .destroy free the kvm_device which wasn't created in .create seems 'unusual'.
Otherwise, FWIW looks good to me.
Jonathan
Quoted 100 lines
--- arch/arm/include/asm/kvm_host.h | 4 ++ arch/arm64/include/asm/kvm_host.h | 1 + arch/arm64/include/uapi/asm/kvm.h | 8 +++ include/uapi/linux/kvm.h | 2 + virt/kvm/arm/arm.c | 1 + virt/kvm/arm/pvtime.c | 94 +++++++++++++++++++++++++++++++ 6 files changed, 110 insertions(+)arch/arm/include/asm/kvm_host.h +4
-0@@ -325,6 +325,10 @@ static inline int kvm_arch_vm_ioctl_check_extension(struct kvm *kvm, long ext)int kvm_perf_init (void );int kvm_perf_teardown (void ); +static inline void kvm_pvtime_init (void ) +{ +} +static inline int kvm_hypercall_pv_features (struct kvm_vcpu *vcpu) {return SMCCC_RET_NOT_SUPPORTED ;arch/arm64/include/asm/kvm_host.h +1
-0@@ -489,6 +489,7 @@ void handle_exit_early(struct kvm_vcpu *vcpu, struct kvm_run *run,int kvm_perf_init (void );int kvm_perf_teardown (void ); +void kvm_pvtime_init (void );int kvm_hypercall_pv_features (struct kvm_vcpu *vcpu);int kvm_hypercall_stolen_time (struct kvm_vcpu *vcpu);int kvm_update_stolen_time (struct kvm_vcpu *vcpu,bool init);arch/arm64/include/uapi/asm/kvm.h +8
-0@@ -367,6 +367,14 @@ 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_REGION 0 +#define KVM_DEV_ARM_PV_TIME_ST 0 +struct kvm_dev_arm_st_region { +__u64 gpa; +__u64 size; +}; +#endif #endif /* __ARM_KVM_H__ */ 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/arm.c +1
-0@@ -1494,6 +1494,7 @@ static int init_subsystems(void)kvm_perf_init ();kvm_coproc_table_init (); +kvm_pvtime_init (); out:on_each_cpu (_kvm_arch_hardware_disable,NULL ,1 );virt/kvm/arm/pvtime.c +4
-0@@ -2,7 +2,9 @@// Copyright (C) 2019 Arm Ltd. #include <linux/arm-smccc.h> +#include <linux/kvm_host.h> +#include <asm/kvm_mmu.h> #include <asm/pvclock-abi.h> #include <kvm/arm_hypercalls.h> @@ -86,3 +88,95 @@ int kvm_hypercall_stolen_time(struct kvm_vcpu *vcpu)return ret; } + +static int kvm_arm_pvtime_create (struct kvm_device *dev,u32 type)
virt/kvm/arm/pvtime.c int kvm_hypercall_stolen_time(struct kvm_vcpu *vcpu) · line 102
Quoted 10 lines
virt/kvm/arm/pvtime.c +10
-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 ; +kfree (dev);
Nothing to do with your patch as such... All users do the same.
This seems miss balanced. Why do we need to free the device by hand when we didn't create it in the create function? I appreciate the comments say this is needed, but as far as I can see every single callback does kfree(dev) at the end which seems an odd thing to do.
Quoted 80 lines
virt/kvm/arm/pvtime.c +80
-0+} + +static int kvm_arm_pvtime_set_attr (struct kvm_device *dev, +struct kvm_device_attr *attr) +{ +struct kvm *kvm = dev->kvm; +struct kvm_arch_pvtime *pvtime = &kvm->arch.pvtime; +u64 __user *user = (u64 __user *)attr->addr; +struct kvm_dev_arm_st_region region; + +switch (attr->group) { +case KVM_DEV_ARM_PV_TIME_REGION : +if (copy_from_user (®ion, user,sizeof (region))) +return -EFAULT ; +if (region.gpa & ~PAGE_MASK ) +return -EINVAL ; +if (region.size & ~PAGE_MASK ) +return -EINVAL ; +switch (attr->attr) { +case KVM_DEV_ARM_PV_TIME_ST : +if (pvtime->st_base !=GPA_INVALID ) +return -EEXIST ; + pvtime->st_base = region.gpa; + pvtime->st_size = region.size; +return 0 ; + } +break ; + } +return -ENXIO ; +} + +static int kvm_arm_pvtime_get_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; +struct kvm_dev_arm_st_region region; + +switch (attr->group) { +case KVM_DEV_ARM_PV_TIME_REGION : +switch (attr->attr) { +case KVM_DEV_ARM_PV_TIME_ST : + region.gpa = pvtime->st_base; + region.size = pvtime->st_size; +if (copy_to_user (user, ®ion,sizeof (region))) +return -EFAULT ; +return 0 ; + } +break ; + } +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_REGION : +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 +}; + +void kvm_pvtime_init (void ) +{ +kvm_register_device_ops (&pvtime_ops,KVM_DEV_TYPE_ARM_PV_TIME ); +}
Re: [PATCH v3 07/10] KVM: arm64: Provide a PV_TIME device to user space
Quoted 27 lines · Jonathan Cameron
Quoted 7 lines · On Wed, 21 Aug 2019 16:36:53 +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 5 lines
The address is given in terms of the physical address visible to the guest and must be page aligned. The guest will discover the address via a hypercall.
Signed-off-by Steven Price <[email protected]>Hi Steven,
One general question inline. I'm not particularly familiar with this area of the kernel, so maybe I'm missing something obvious, but having .destroy free the
kvm_devicewhich wasn't created in .create seems 'unusual'.Otherwise, FWIW looks good to me.
Jonathan
[...]
Quoted 9 lines
virt/kvm/arm/pvtime.c int kvm_hypercall_stolen_time(struct kvm_vcpu *vcpu) · line 102
Quoted 6 lines
virt/kvm/arm/pvtime.c +6
-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 ; +kfree (dev);Nothing to do with your patch as such... All users do the same.
Quoted 5 lines
This seems miss balanced. Why do we need to free the device by hand when we didn't create it in the create function? I appreciate the comments say this is needed, but as far as I can see every single callback does kfree(dev) at the end which seems an odd thing to do.
Yes I think this is odd too - indeed when I initially wrote this I missed off the kfree() call and had to track down the memory leak.
When I looked into potentially tiding this up I found some other oddities, e.g. "kvm-xive" (arch/powerpc/kvm/book3s_xive.c) doesn't have a destroy callback. But I can't see anything in the common code which deals with that case. So I decided to just "go with the flow" at the moment, since I don't understand how some of these existing devices work (perhaps they are already broken?).
Steve
Quoted 88 lines
Quoted 70 lines
virt/kvm/arm/pvtime.c +70
-0+} + +static int kvm_arm_pvtime_set_attr (struct kvm_device *dev, +struct kvm_device_attr *attr) +{ +struct kvm *kvm = dev->kvm; +struct kvm_arch_pvtime *pvtime = &kvm->arch.pvtime; +u64 __user *user = (u64 __user *)attr->addr; +struct kvm_dev_arm_st_region region; + +switch (attr->group) { +case KVM_DEV_ARM_PV_TIME_REGION : +if (copy_from_user (®ion, user,sizeof (region))) +return -EFAULT ; +if (region.gpa & ~PAGE_MASK ) +return -EINVAL ; +if (region.size & ~PAGE_MASK ) +return -EINVAL ; +switch (attr->attr) { +case KVM_DEV_ARM_PV_TIME_ST : +if (pvtime->st_base !=GPA_INVALID ) +return -EEXIST ; + pvtime->st_base = region.gpa; + pvtime->st_size = region.size; +return 0 ; + } +break ; + } +return -ENXIO ; +} + +static int kvm_arm_pvtime_get_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; +struct kvm_dev_arm_st_region region; + +switch (attr->group) { +case KVM_DEV_ARM_PV_TIME_REGION : +switch (attr->attr) { +case KVM_DEV_ARM_PV_TIME_ST : + region.gpa = pvtime->st_base; + region.size = pvtime->st_size; +if (copy_to_user (user, ®ion,sizeof (region))) +return -EFAULT ; +return 0 ; + } +break ; + } +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_REGION : +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,virt/kvm/arm/pvtime.c int kvm_hypercall_stolen_time(struct kvm_vcpu *vcpu) · line 182
Quoted 10 lines
virt/kvm/arm/pvtime.c +10
-0+ .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 +}; + +void kvm_pvtime_init (void ) +{ +kvm_register_device_ops (&pvtime_ops,KVM_DEV_TYPE_ARM_PV_TIME ); +}_______________________________________________
linux-arm-kernel mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel
Re: [PATCH v3 07/10] KVM: arm64: Provide a PV_TIME device to user space
Quoted 47 lines · On Thu, 22 Aug 2019 12:11:55 +0100 Steven Price <[email protected]> wrote:
Quoted 27 lines · Jonathan Cameron
Quoted 7 lines · On Wed, 21 Aug 2019 16:36:53 +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 5 lines
The address is given in terms of the physical address visible to the guest and must be page aligned. The guest will discover the address via a hypercall.
Signed-off-by Steven Price <[email protected]>Hi Steven,
One general question inline. I'm not particularly familiar with this area of the kernel, so maybe I'm missing something obvious, but having .destroy free the
kvm_devicewhich wasn't created in .create seems 'unusual'.Otherwise, FWIW looks good to me.
Jonathan
[...]
Quoted 9 lines
virt/kvm/arm/pvtime.c int kvm_hypercall_stolen_time(struct kvm_vcpu *vcpu) · line 102
Quoted 6 lines
virt/kvm/arm/pvtime.c +6
-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 ; +kfree (dev);Nothing to do with your patch as such... All users do the same.
Quoted 5 lines
This seems miss balanced. Why do we need to free the device by hand when we didn't create it in the create function? I appreciate the comments say this is needed, but as far as I can see every single callback does kfree(dev) at the end which seems an odd thing to do.
Yes I think this is odd too - indeed when I initially wrote this I missed off the
kfree()call and had to track down the memory leak.
Quoted 6 lines
When I looked into potentially tiding this up I found some other oddities, e.g. "kvm-xive" (
arch/powerpc/kvm/book3s_xive.c) doesn't have a destroy callback. But I can't see anything in the common code which deals with that case. So I decided to just "go with the flow" at the moment, since I don't understand how some of these existing devices work (perhaps they are already broken?).
It has a release however and kvm_device_release also removes the device from the list that would then be cleared by kvm_destroy_devices.
kvm_device_release is a release callback for the file operations so it 'might' be called in all paths.
Fun though, in kvm_ioctl_create_device the error handling for the anon_inode_getfd calls ops->destroy without checking it exists. Boom.
Possibly never happens in reality but looks like a bug to me.
Jonathan
Quoted 92 lines
Steve
Quoted 88 lines
Quoted 70 lines
virt/kvm/arm/pvtime.c +70
-0+} + +static int kvm_arm_pvtime_set_attr (struct kvm_device *dev, +struct kvm_device_attr *attr) +{ +struct kvm *kvm = dev->kvm; +struct kvm_arch_pvtime *pvtime = &kvm->arch.pvtime; +u64 __user *user = (u64 __user *)attr->addr; +struct kvm_dev_arm_st_region region; + +switch (attr->group) { +case KVM_DEV_ARM_PV_TIME_REGION : +if (copy_from_user (®ion, user,sizeof (region))) +return -EFAULT ; +if (region.gpa & ~PAGE_MASK ) +return -EINVAL ; +if (region.size & ~PAGE_MASK ) +return -EINVAL ; +switch (attr->attr) { +case KVM_DEV_ARM_PV_TIME_ST : +if (pvtime->st_base !=GPA_INVALID ) +return -EEXIST ; + pvtime->st_base = region.gpa; + pvtime->st_size = region.size; +return 0 ; + } +break ; + } +return -ENXIO ; +} + +static int kvm_arm_pvtime_get_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; +struct kvm_dev_arm_st_region region; + +switch (attr->group) { +case KVM_DEV_ARM_PV_TIME_REGION : +switch (attr->attr) { +case KVM_DEV_ARM_PV_TIME_ST : + region.gpa = pvtime->st_base; + region.size = pvtime->st_size; +if (copy_to_user (user, ®ion,sizeof (region))) +return -EFAULT ; +return 0 ; + } +break ; + } +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_REGION : +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,virt/kvm/arm/pvtime.c int kvm_hypercall_stolen_time(struct kvm_vcpu *vcpu) · line 182
Quoted 10 lines
virt/kvm/arm/pvtime.c +10
-0+ .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 +}; + +void kvm_pvtime_init (void ) +{ +kvm_register_device_ops (&pvtime_ops,KVM_DEV_TYPE_ARM_PV_TIME ); +}_______________________________________________
linux-arm-kernel mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel
PATCH v3 8/10 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 v3 9/10 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 · 1 line
2.20.1
PATCH v3 10/10 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/include/asm/paravirt.h | 9 +- arch/arm64/kernel/paravirt.c | 148 ++++++++++++++++++++++++++++++ arch/arm64/kernel/time.c | 3 + include/linux/cpuhotplug.h | 1 + 4 files changed, 160 insertions(+), 1 deletion(-)
arch/arm64/include/asm/paravirt.h +8 -1
@@ -21,6 +21,13 @@ static inline u64 paravirt_steal_clock(int cpu) {return pv_ops.time.steal_clock (cpu); } -#endif + +int __init kvm_guest_init (void ); + +#else + +#define kvm_guest_init () + +#endif // CONFIG_PARAVIRT #endif
arch/arm64/kernel/paravirt.c +148 -0
161 lines not on this page · raw message
arch/arm64/kernel/time.c +3 -0
@@ -30,6 +30,7 @@#include <asm/thread_info.h> #include <asm/stacktrace.h> +#include <asm/paravirt.h> unsigned long profile_pc (struct pt_regs *regs) {
@@ -65,4 +66,6 @@ void __init time_init(void)/* Calibrate the delay loop directly */ lpj_fine = arch_timer_rate /HZ ; + +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,
Signature · 6 lines
2.20.1 _______________________________________________ kvmarm mailing list [email protected] https://lists.cs.columbia.edu/mailman/listinfo/kvmarm
Zenghui Yu
reviewerRe: [PATCH v3 10/10] arm64: Retrieve stolen time as paravirtualized guest
Hi Steven,
Quoted 123 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/include/asm/paravirt.h | 9 +- arch/arm64/kernel/paravirt.c | 148 ++++++++++++++++++++++++++++++ arch/arm64/kernel/time.c | 3 + include/linux/cpuhotplug.h | 1 + 4 files changed, 160 insertions(+), 1 deletion(-)arch/arm64/include/asm/paravirt.h +8
-1@@ -21,6 +21,13 @@ static inline u64 paravirt_steal_clock(int cpu) {return pv_ops.time.steal_clock (cpu); } -#endif + +int __init kvm_guest_init (void ); + +#else + +#define kvm_guest_init () + +#endif // CONFIG_PARAVIRT #endif arch/arm64/kernel/paravirt.c +67
-0@@ -6,13 +6,161 @@* Author: Stefano Stabellini <[email protected]> */ +#define pr_fmt (fmt) "kvmarm-pv: " fmt + +#include <linux/arm-smccc.h> +#include <linux/cpuhotplug.h> #include <linux/export.h> +#include <linux/io.h> #include <linux/jump_label.h> +#include <linux/printk.h> +#include <linux/psci.h> +#include <linux/reboot.h> +#include <linux/slab.h> #include <linux/types.h> +#include <asm/paravirt.h> +#include <asm/pvclock-abi.h> +#include <asm/smp_plat.h> struct static_key paravirt_steal_enabled;struct static_key paravirt_steal_rq_enabled;struct paravirt_patch_template pv_ops;EXPORT_SYMBOL_GPL (pv_ops); + +struct kvmarm_stolen_time_region { +struct pvclock_vcpu_stolen_time *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/paravirt.c line 95
Quoted 10 lines
arch/arm64/kernel/paravirt.c +10
-0+ reg =this_cpu_ptr (&stolen_time_region); + +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 ), +MEMREMAP_WB );
cpuhp callbacks can be invoked in atomic context (see:
secondary_start_kernel -> notify_cpu_starting -> invoke callbacks),
but memremap might sleep...
Try to run a DEBUG_ATOMIC_SLEEP enabled PV guest, I guess we will be greeted by the Sleep-in-Atomic-Context BUG. We need an alternative here?
Quoted 43 lines
arch/arm64/kernel/paravirt.c +43
-0+ +if (!reg->kaddr) { +pr_warn ("Failed to map stolen time data structure\n" ); +return -ENOMEM ; + } + +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);
arch/arm64/kernel/paravirt.c line 148
Quoted 10 lines
arch/arm64/kernel/paravirt.c +10
-0+ +if (res.a0 !=SMCCC_RET_SUCCESS ) +return false; + +return true; +} + +int __init kvm_guest_init (void ) +{ +int ret =0 ;
And this look like a redundant initialization?
Thanks,
zenghui
Quoted 50 lines
arch/arm64/kernel/paravirt.c +18
-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 ; +}arch/arm64/kernel/time.c +3
-0@@ -30,6 +30,7 @@#include <asm/thread_info.h> #include <asm/stacktrace.h> +#include <asm/paravirt.h> unsigned long profile_pc (struct pt_regs *regs) {@@ -65,4 +66,6 @@ void __init time_init(void)/* Calibrate the delay loop directly */ lpj_fine = arch_timer_rate /HZ ; + +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 v3 10/10] arm64: Retrieve stolen time as paravirtualized guest
Quoted 142 lines · Zenghui Yu
Hi Steven,
Quoted 122 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/include/asm/paravirt.h| 9 +- arch/arm64/kernel/paravirt.c | 148 ++++++++++++++++++++++++++++++ arch/arm64/kernel/time.c | 3 + include/linux/cpuhotplug.h | 1 +4 files changed, 160 insertions(+), 1 deletion(-)arch/arm64/include/asm/paravirt.h +8
-1@@ -21,6 +21,13 @@ static inline u64 paravirt_steal_clock(int cpu) { return pv_ops.time.steal_clock(cpu); } -#endif + +int __init kvm_guest_init (void ); + +#else + +#define kvm_guest_init () + +#endif // CONFIG_PARAVIRT #endifarch/arm64/kernel/paravirt.c +0
-0@@ -6,13 +6,161 @@* Author: Stefano Stabellini <[email protected]> */ +#define
pr_fmt(fmt)"kvmarm-pv: " fmtarch/arm64/kernel/paravirt.c +3
-0+ +#include <linux/arm-smccc.h> +#include <linux/cpuhotplug.h> #include <
linux/export.h>
+#include <linux/io.h>
#include <linux/jump_label.h>arch/arm64/kernel/paravirt.c +4
-0+#include <linux/printk.h> +#include <linux/psci.h> +#include <linux/reboot.h> +#include <linux/slab.h> #include <
linux/types.h>
+
#include <asm/paravirt.h>arch/arm64/kernel/paravirt.c +2
-0+#include <asm/pvclock-abi.h> +#include <asm/smp_plat.h> struct
static_keyparavirt_steal_enabled;
structstatic_keyparavirt_steal_rq_enabled;
structparavirt_patch_templatepv_ops;
EXPORT_SYMBOL_GPL(pv_ops);arch/arm64/kernel/paravirt.c +6
-0+ +struct kvmarm_stolen_time_region { + struct pvclock_vcpu_stolen_time *kaddr; +}; + +static DEFINE_PER_CPU(struct kvmarm_stolen_time_region,
stolen_time_region);arch/arm64/kernel/paravirt.c +18
-0+ +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;
+ }arch/arm64/kernel/paravirt.c +28
-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/paravirt.c line 95
Quoted 10 lines
arch/arm64/kernel/paravirt.c +10
-0+ reg = this_cpu_ptr(&stolen_time_region); + + 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), + MEMREMAP_WB);cpuhp callbacks can be invoked in atomic context (see:
secondary_start_kernel ->notify_cpu_starting->
invoke callbacks),
but memremap might sleep...
Quoted 3 lines
Try to run a
DEBUG_ATOMIC_SLEEPenabled PV guest, I guess we will be greeted by the Sleep-in-Atomic-Context BUG. We need an alternative here?
Actually I had run DEBUG_ATOMIC_SLEEP and not seen any issue. But I think that's because of the way I've configured the region in my kvmtool changes. I'm hitting the path where the memory region is in the linear map of the kernel and so no actual remapping is needed and hence memremap doesn't sleep (the shared structure is in a reserved region of RAM).
But even changing the memory layout of the guest so the call goes into ioremap_page_range() (which contains a might_sleep()) I'm not seeing any problems.
Am I missing something? I have to admit I don't entirely follow the early start up - perhaps it's a simple as DEBUG_ATOMIC_SLEEP doesn't work this early in boot?
Quoted 49 lines
Quoted 49 lines
arch/arm64/kernel/paravirt.c +9
-0+ + if (!reg->kaddr) { + pr_warn("Failed to map stolen time data structure\n"); + return -ENOMEM; + } + + if (le32_to_cpu(reg->kaddr->revision) != 0 || + le32_to_cpu(reg->kaddr->attributes) != 0) { + pr_warn("Unexpected revision or attributes in stolen timedata\n");
+ return-ENXIO;
+ }arch/arm64/kernel/paravirt.c +21
-0+ + 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 SMCCC1.1+ */
+ if (psci_ops.smccc_version <SMCCC_VERSION_1_1) + return false;arch/arm64/kernel/paravirt.c +13
-0+ + 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; +
Quoted 8 lines
arch/arm64/kernel/paravirt.c line 148
Quoted 6 lines
+ return true;
arch/arm64/kernel/paravirt.c +5
-0+} + +int __init kvm_guest_init (void ) +{ + int ret = 0;And this look like a redundant initialization?
Yes - that should go, thanks for spotting it.
Steve
Quoted 53 lines
Thanks,
zenghuiQuoted 47 lines
arch/arm64/kernel/paravirt.c +18
-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; +}arch/arm64/kernel/time.c +3
-0@@ -30,6 +30,7 @@ #include <asm/thread_info.h> #include <asm/stacktrace.h> +#include <asm/paravirt.h> unsigned long profile_pc(struct pt_regs *regs) {@@ -65,4 +66,6 @@ void __init time_init(void) /* Calibrate the delay loop directly */ lpj_fine = arch_timer_rate / HZ; + + 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,
Zenghui Yu
reviewerRe: [PATCH v3 10/10] arm64: Retrieve stolen time as paravirtualized guest
Quoted 154 lines · Steven Price
Quoted 142 lines · Zenghui Yu
Hi Steven,
Quoted 122 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/include/asm/paravirt.h | 9 +- arch/arm64/kernel/paravirt.c | 148 ++++++++++++++++++++++++++++++ arch/arm64/kernel/time.c | 3 + include/linux/cpuhotplug.h | 1 + 4 files changed, 160 insertions(+), 1 deletion(-)arch/arm64/include/asm/paravirt.h +8
-1@@ -21,6 +21,13 @@ static inline u64 paravirt_steal_clock(int cpu) { return pv_ops.time.steal_clock(cpu); } -#endif + +int __init kvm_guest_init (void ); + +#else + +#define kvm_guest_init () + +#endif // CONFIG_PARAVIRT #endifarch/arm64/kernel/paravirt.c +66
-0@@ -6,13 +6,161 @@* Author: Stefano Stabellini <[email protected]> */ +#define pr_fmt(fmt) "kvmarm-pv: " fmt + +#include <linux/arm-smccc.h> +#include <linux/cpuhotplug.h> #include <linux/export.h> +#include <linux/io.h> #include <linux/jump_label.h> +#include <linux/printk.h> +#include <linux/psci.h> +#include <linux/reboot.h> +#include <linux/slab.h> #include <linux/types.h> + #include <asm/paravirt.h> +#include <asm/pvclock-abi.h> +#include <asm/smp_plat.h> struct static_key paravirt_steal_enabled; struct static_key paravirt_steal_rq_enabled; struct paravirt_patch_template pv_ops; EXPORT_SYMBOL_GPL(pv_ops); + +struct kvmarm_stolen_time_region { + struct pvclock_vcpu_stolen_time *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/paravirt.c line 94
Quoted 10 lines
arch/arm64/kernel/paravirt.c +10
-0+ reg = this_cpu_ptr(&stolen_time_region); + + 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), + MEMREMAP_WB);cpuhp callbacks can be invoked in atomic context (see:
secondary_start_kernel ->notify_cpu_starting->
invoke callbacks),
but memremap might sleep...Quoted 3 lines
Try to run a
DEBUG_ATOMIC_SLEEPenabled PV guest, I guess we will be greeted by the Sleep-in-Atomic-Context BUG. We need an alternative here?Actually I had run
DEBUG_ATOMIC_SLEEPand not seen any issue. But I think that's because of the way I've configured the region in my kvmtool changes. I'm hitting the path where the memory region is in the linear map of the kernel and so no actual remapping is needed and hence memremap doesn't sleep (the shared structure is in a reserved region of RAM).
Quoted 3 lines
But even changing the memory layout of the guest so the call goes into
ioremap_page_range()(which contains amight_sleep()) I'm not seeing any problems.
Emm, I hit this SAC BUG when testing your V1 with the kvmtool changes you've provided, but forgot to report it at that time. I go back to V1 and get the following call trace:
[ 0.120113] BUG: sleeping function called from invalid context at mm/slab.h:501 [ 0.120118] in_atomic(): 1, irqs_disabled(): 128, pid: 0, name: swapper/1 [ 0.120122] no locks held by swapper/1/0. [ 0.120126] irq event stamp: 0 [ 0.120135] hardirqs last enabled at (0): [<0000000000000000>] 0x0 [ 0.120145] hardirqs last disabled at (0): [<ffff200010113b40>] copy_process+0x870/0x2878 [ 0.120152] softirqs last enabled at (0): [<ffff200010113b40>] copy_process+0x870/0x2878 [ 0.120157] softirqs last disabled at (0): [<0000000000000000>] 0x0 [ 0.120164] CPU: 1 PID: 0 Comm: swapper/1 Not tainted 5.3.0-rc6+ #2 [ 0.120168] Hardware name: linux,dummy-virt (DT) [ 0.120173] Call trace: [ 0.120179] dump_backtrace+0x0/0x250 [ 0.120184] show_stack+0x24/0x30 [ 0.120192] dump_stack+0x120/0x174 [ 0.120198] ___might_sleep+0x1b0/0x280 [ 0.120203] __might_sleep+0x80/0xf0 [ 0.120209] kmem_cache_alloc_node_trace+0x30c/0x3c8 [ 0.120216] __get_vm_area_node+0x9c/0x208 [ 0.120221] get_vm_area_caller+0x58/0x68 [ 0.120227] __ioremap_caller+0x78/0x120 [ 0.120233] ioremap_cache+0xf0/0x1a8 [ 0.120240] memremap+0x154/0x3b8 [ 0.120245] init_stolen_time_cpu+0x94/0x150 [ 0.120251] cpuhp_invoke_callback+0x12c/0x12f8 [ 0.120257] notify_cpu_starting+0xa0/0xc0 [ 0.120263] secondary_start_kernel+0x1d4/0x328
But things may have changed because we're talking about V3 now... I will dig it a bit deeper.
Quoted 3 lines
Am I missing something? I have to admit I don't entirely follow the early start up - perhaps it's a simple as
DEBUG_ATOMIC_SLEEPdoesn't work this early in boot?
I think it should work.
Thanks,
zenghui