Re: [PATCH v3 10/10] arm64: Retrieve stolen time as paravirtualized guest

From: Steven Price
Date: Fri Aug 23 2019 - 10:22:49 EST


On 23/08/2019 12:45, Zenghui Yu wrote:
> Hi Steven,
>
> On 2019/8/21 23:36, Steven Price wrote:
>> Enable 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.
>>
>> Signed-off-by: Steven Price <steven.price@xxxxxxx>
>> ---
>> Â 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(-)
>>
>> diff --git a/arch/arm64/include/asm/paravirt.h
>> b/arch/arm64/include/asm/paravirt.h
>> index 799d9dd6f7cc..125c26c42902 100644
>> --- a/arch/arm64/include/asm/paravirt.h
>> +++ b/arch/arm64/include/asm/paravirt.h
>> @@ -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
>> diff --git a/arch/arm64/kernel/paravirt.c b/arch/arm64/kernel/paravirt.c
>> index 4cfed91fe256..ea8dbbbd3293 100644
>> --- a/arch/arm64/kernel/paravirt.c
>> +++ b/arch/arm64/kernel/paravirt.c
>> @@ -6,13 +6,161 @@
>> ÂÂ * Author: Stefano Stabellini <stefano.stabellini@xxxxxxxxxxxxx>
>> ÂÂ */
>> Â +#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;
>> +
>> +ÂÂÂ 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?

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?

>> +
>> +ÂÂÂ 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);
>> +
>> +ÂÂÂ 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?

Yes - that should go, thanks for spotting it.

Steve

>
>
> Thanks,
> zenghui
>
>> +
>> +ÂÂÂ 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(&paravirt_steal_enabled);
>> +ÂÂÂ if (steal_acc)
>> +ÂÂÂÂÂÂÂ static_key_slow_inc(&paravirt_steal_rq_enabled);
>> +
>> +ÂÂÂ pr_info("using stolen time PV\n");
>> +
>> +ÂÂÂ return 0;
>> +}
>> diff --git a/arch/arm64/kernel/time.c b/arch/arm64/kernel/time.c
>> index 0b2946414dc9..a52aea14c6ec 100644
>> --- a/arch/arm64/kernel/time.c
>> +++ b/arch/arm64/kernel/time.c
>> @@ -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();
>> Â }
>> diff --git a/include/linux/cpuhotplug.h b/include/linux/cpuhotplug.h
>> index 068793a619ca..89d75edb5750 100644
>> --- a/include/linux/cpuhotplug.h
>> +++ b/include/linux/cpuhotplug.h
>> @@ -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,
>>
>