Re: [PATCH v2 6/6] KVM: arm64: Support the VCPU preemption check

From: yezengruan
Date: Sat Jan 11 2020 - 02:33:27 EST


Hi Steve,

On 2020/1/9 23:09, Steven Price wrote:
> On 26/12/2019 13:58, Zengruan Ye wrote:
>> Support the vcpu_is_preempted() functionality under KVM/arm64. This will
>> enhance lock performance on overcommitted hosts (more runnable VCPUs
>> than physical CPUs in the system) as doing busy waits for preempted
>> VCPUs will hurt system performance far worse than early yielding.
>>
>> unix benchmark result:
>> ÂÂ host:Â kernel 5.5.0-rc1, HiSilicon Kunpeng920, 8 CPUs
>> ÂÂ guest: kernel 5.5.0-rc1, 16 VCPUs
>>
>> ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ test-caseÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ |ÂÂÂ after-patchÂÂÂ |ÂÂ before-patch
>> ----------------------------------------+-------------------+------------------
>> Â Dhrystone 2 using register variablesÂÂ | 334600751.0 lpsÂÂ | 335319028.3 lps
>> Â Double-Precision WhetstoneÂÂÂÂÂÂÂÂÂÂÂÂ |ÂÂÂÂ 32856.1 MWIPS |ÂÂÂÂ 32849.6 MWIPS
>> Â Execl ThroughputÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ |ÂÂÂÂÂ 3662.1 lpsÂÂ |ÂÂÂÂÂ 2718.0 lps
>>  File Copy 1024 bufsize 2000 maxblocks | 432906.4 KBps | 158011.8 KBps
>>  File Copy 256 bufsize 500 maxblocks | 116023.0 KBps | 37664.0 KBps
>>  File Copy 4096 bufsize 8000 maxblocks | 1432769.8 KBps | 441108.8 KBps
>> Â Pipe ThroughputÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ |ÂÂ 6405029.6 lpsÂÂ |ÂÂ 6021457.6 lps
>> Â Pipe-based Context SwitchingÂÂÂÂÂÂÂÂÂÂ |ÂÂÂ 185872.7 lpsÂÂ |ÂÂÂ 184255.3 lps
>> Â Process CreationÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ |ÂÂÂÂÂ 4025.7 lpsÂÂ |ÂÂÂÂÂ 3706.6 lps
>> Â Shell Scripts (1 concurrent)ÂÂÂÂÂÂÂÂÂÂ |ÂÂÂÂÂ 6745.6 lpmÂÂ |ÂÂÂÂÂ 6436.1 lpm
>> Â Shell Scripts (8 concurrent)ÂÂÂÂÂÂÂÂÂÂ |ÂÂÂÂÂÂ 998.7 lpmÂÂ |ÂÂÂÂÂÂ 931.1 lpm
>> Â System Call OverheadÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ |ÂÂ 3913363.1 lpsÂÂ |ÂÂ 3883287.8 lps
>> ----------------------------------------+-------------------+------------------
>> Â System Benchmarks Index ScoreÂÂÂÂÂÂÂÂÂ |ÂÂÂÂÂ 1835.1ÂÂÂÂÂÂ |ÂÂÂÂÂ 1327.6
>>
>> Signed-off-by: Zengruan Ye <yezengruan@xxxxxxxxxx>
>> ---
>> Â arch/arm64/include/asm/paravirt.h |ÂÂ 3 +
>> Â arch/arm64/kernel/paravirt.cÂÂÂÂÂ | 117 ++++++++++++++++++++++++++++++
>> Â arch/arm64/kernel/setup.cÂÂÂÂÂÂÂÂ |ÂÂ 2 +
>> Â include/linux/cpuhotplug.hÂÂÂÂÂÂÂ |ÂÂ 1 +
>> Â 4 files changed, 123 insertions(+)
>>
>> diff --git a/arch/arm64/include/asm/paravirt.h b/arch/arm64/include/asm/paravirt.h
>> index 7b1c81b544bb..ca3a2c7881f3 100644
>> --- a/arch/arm64/include/asm/paravirt.h
>> +++ b/arch/arm64/include/asm/paravirt.h
>> @@ -29,6 +29,8 @@ static inline u64 paravirt_steal_clock(int cpu)
>> Â Â int __init pv_time_init(void);
>> Â +int __init pv_lock_init(void);
>> +
>> Â __visible bool __native_vcpu_is_preempted(int cpu);
>> Â Â static inline bool pv_vcpu_is_preempted(int cpu)
>> @@ -39,6 +41,7 @@ static inline bool pv_vcpu_is_preempted(int cpu)
>> Â #else
>> Â Â #define pv_time_init() do {} while (0)
>> +#define pv_lock_init() do {} while (0)
>> Â Â #endif // CONFIG_PARAVIRT
>> Â diff --git a/arch/arm64/kernel/paravirt.c b/arch/arm64/kernel/paravirt.c
>> index d8f1ba8c22ce..bd2ad6a17a26 100644
>> --- a/arch/arm64/kernel/paravirt.c
>> +++ b/arch/arm64/kernel/paravirt.c
>> @@ -22,6 +22,7 @@
>> Â #include <asm/paravirt.h>
>> Â #include <asm/pvclock-abi.h>
>> Â #include <asm/smp_plat.h>
>> +#include <asm/pvlock-abi.h>
>> Â Â struct static_key paravirt_steal_enabled;
>> Â struct static_key paravirt_steal_rq_enabled;
>> @@ -35,6 +36,10 @@ struct pv_time_stolen_time_region {
>> ÂÂÂÂÂ struct pvclock_vcpu_stolen_time *kaddr;
>> Â };
>> Â +struct pv_lock_state_region {
>> +ÂÂÂ struct pvlock_vcpu_state *kaddr;
>> +};
>> +
>> Â static DEFINE_PER_CPU(struct pv_time_stolen_time_region, stolen_time_region);
>> Â Â static bool steal_acc = true;
>> @@ -158,3 +163,115 @@ int __init pv_time_init(void)
>> Â ÂÂÂÂÂ return 0;
>> Â }
>> +
>> +static DEFINE_PER_CPU(struct pv_lock_state_region, lock_state_region);
>> +
>> +static bool kvm_vcpu_is_preempted(int cpu)
>> +{
>> +ÂÂÂ struct pv_lock_state_region *reg;
>> +ÂÂÂ __le64 preempted_le;
>> +
>> +ÂÂÂ reg = per_cpu_ptr(&lock_state_region, cpu);
>> +ÂÂÂ if (!reg->kaddr) {
>> +ÂÂÂÂÂÂÂ pr_warn_once("PV lock enabled but not configured for cpu %d\n",
>> +ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ cpu);
>> +ÂÂÂÂÂÂÂ return false;
>> +ÂÂÂ }
>> +
>> +ÂÂÂ preempted_le = le64_to_cpu(READ_ONCE(reg->kaddr->preempted));
>> +
>> +ÂÂÂ return !!(preempted_le & 1);
>
> According to the documentation preempted != 0 means preempted, but here you are checking the LSB. You need to be consistent about the ABI.

Thanks for posting this. I'll update the code.

>
>> +}
>> +
>> +static int pvlock_vcpu_state_dying_cpu(unsigned int cpu)
>> +{
>> +ÂÂÂ struct pv_lock_state_region *reg;
>> +
>> +ÂÂÂ reg = this_cpu_ptr(&lock_state_region);
>> +ÂÂÂ if (!reg->kaddr)
>> +ÂÂÂÂÂÂÂ return 0;
>> +
>> +ÂÂÂ memunmap(reg->kaddr);
>> +ÂÂÂ memset(reg, 0, sizeof(*reg));
>> +
>> +ÂÂÂ return 0;
>> +}
>> +
>> +static int init_pvlock_vcpu_state(unsigned int cpu)
>> +{
>> +ÂÂÂ struct pv_lock_state_region *reg;
>> +ÂÂÂ struct arm_smccc_res res;
>> +
>> +ÂÂÂ reg = this_cpu_ptr(&lock_state_region);
>> +
>> +ÂÂÂ arm_smccc_1_1_invoke(ARM_SMCCC_HV_PV_LOCK_PREEMPTED, &res);
>> +
>> +ÂÂÂ if (res.a0 == SMCCC_RET_NOT_SUPPORTED) {
>> +ÂÂÂÂÂÂÂ pr_warn("Failed to init PV lock data structure\n");
>> +ÂÂÂÂÂÂÂ return -EINVAL;
>> +ÂÂÂ }
>> +
>> +ÂÂÂ reg->kaddr = memremap(res.a0,
>> +ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ sizeof(struct pvlock_vcpu_state),
>> +ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ MEMREMAP_WB);
>> +
>> +ÂÂÂ if (!reg->kaddr) {
>> +ÂÂÂÂÂÂÂ pr_warn("Failed to map PV lock data structure\n");
>> +ÂÂÂÂÂÂÂ return -ENOMEM;
>> +ÂÂÂ }
>> +
>> +ÂÂÂ return 0;
>> +}
>> +
>> +static int kvm_arm_init_pvlock(void)
>> +{
>> +ÂÂÂ int ret;
>> +
>> +ÂÂÂ ret = cpuhp_setup_state(CPUHP_AP_ARM_KVM_PVLOCK_STARTING,
>> +ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ "hypervisor/arm/pvlock:starting",
>> +ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ init_pvlock_vcpu_state,
>> +ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ pvlock_vcpu_state_dying_cpu);
>> +ÂÂÂ if (ret < 0) {
>> +ÂÂÂÂÂÂÂ pr_warn("PV-lock init failed\n");
>> +ÂÂÂÂÂÂÂ return ret;
>> +ÂÂÂ }
>> +
>> +ÂÂÂ return 0;
>> +}
>> +
>> +static bool has_kvm_pvlock(void)
>> +{
>> +ÂÂÂ struct arm_smccc_res res;
>> +
>> +ÂÂÂ /* To detect the presence of PV lock 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_LOCK_FEATURES, &res);
>
> As mentioned previously we could do with something more robust to check that the hypervisor is actually KVM before assuming that vendor specific IDs are valid.

Will update next version.

>
> Steve
>
>> +
>> +ÂÂÂ if (res.a0 != SMCCC_RET_SUCCESS)
>> +ÂÂÂÂÂÂÂ return false;
>> +
>> +ÂÂÂ return true;
>> +}
>> +
>> +int __init pv_lock_init(void)
>> +{
>> +ÂÂÂ int ret;
>> +
>> +ÂÂÂ if (is_hyp_mode_available())
>> +ÂÂÂÂÂÂÂ return 0;
>> +
>> +ÂÂÂ if (!has_kvm_pvlock())
>> +ÂÂÂÂÂÂÂ return 0;
>> +
>> +ÂÂÂ ret = kvm_arm_init_pvlock();
>> +ÂÂÂ if (ret)
>> +ÂÂÂÂÂÂÂ return ret;
>> +
>> +ÂÂÂ pv_ops.lock.vcpu_is_preempted = kvm_vcpu_is_preempted;
>> +ÂÂÂ pr_info("using PV-lock preempted\n");
>> +
>> +ÂÂÂ return 0;
>> +}
>> diff --git a/arch/arm64/kernel/setup.c b/arch/arm64/kernel/setup.c
>> index 56f664561754..aa3a8b9e710f 100644
>> --- a/arch/arm64/kernel/setup.c
>> +++ b/arch/arm64/kernel/setup.c
>> @@ -341,6 +341,8 @@ void __init setup_arch(char **cmdline_p)
>> ÂÂÂÂÂ smp_init_cpus();
>> ÂÂÂÂÂ smp_build_mpidr_hash();
>> Â +ÂÂÂ pv_lock_init();
>> +
>> ÂÂÂÂÂ /* Init percpu seeds for random tags after cpus are set up. */
>> ÂÂÂÂÂ kasan_init_tags();
>> Â diff --git a/include/linux/cpuhotplug.h b/include/linux/cpuhotplug.h
>> index e51ee772b9f5..f72ff95ab63a 100644
>> --- a/include/linux/cpuhotplug.h
>> +++ b/include/linux/cpuhotplug.h
>> @@ -138,6 +138,7 @@ enum cpuhp_state {
>> ÂÂÂÂÂ CPUHP_AP_DUMMY_TIMER_STARTING,
>> ÂÂÂÂÂ CPUHP_AP_ARM_XEN_STARTING,
>> ÂÂÂÂÂ CPUHP_AP_ARM_KVMPV_STARTING,
>> +ÂÂÂ CPUHP_AP_ARM_KVM_PVLOCK_STARTING,
>> ÂÂÂÂÂ CPUHP_AP_ARM_CORESIGHT_STARTING,
>> ÂÂÂÂÂ CPUHP_AP_ARM64_ISNDEP_STARTING,
>> ÂÂÂÂÂ CPUHP_AP_SMPCFD_DYING,
>>
>
>
> .

Thanks,

Zengruan