From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 90E94C4332F for ; Thu, 3 Nov 2022 13:25:41 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231375AbiKCNZj (ORCPT ); Thu, 3 Nov 2022 09:25:39 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33128 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230165AbiKCNZe (ORCPT ); Thu, 3 Nov 2022 09:25:34 -0400 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 8E000C53; Thu, 3 Nov 2022 06:25:33 -0700 (PDT) Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id 32C751FB; Thu, 3 Nov 2022 06:25:39 -0700 (PDT) Received: from [10.1.39.27] (e122027.cambridge.arm.com [10.1.39.27]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id 654313F534; Thu, 3 Nov 2022 06:25:29 -0700 (PDT) Message-ID: <76f59579-b701-a243-2a50-72a1401d3a65@arm.com> Date: Thu, 3 Nov 2022 13:25:27 +0000 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.2.2 Subject: Re: [RFC 5/6] KVM: arm64: Support the VCPU preemption check Content-Language: en-GB To: Usama Arif , linux-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org, kvmarm@lists.cs.columbia.edu, kvm@vger.kernel.org, linux-doc@vger.kernel.org, virtualization@lists.linux-foundation.org, linux@armlinux.org.uk, yezengruan@huawei.com, catalin.marinas@arm.com, will@kernel.org, maz@kernel.org, mark.rutland@arm.com Cc: fam.zheng@bytedance.com, liangma@liangbit.com, punit.agrawal@bytedance.com References: <20221102161340.2982090-1-usama.arif@bytedance.com> <20221102161340.2982090-6-usama.arif@bytedance.com> From: Steven Price In-Reply-To: <20221102161340.2982090-6-usama.arif@bytedance.com> Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 02/11/2022 16:13, Usama Arif 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. > > Signed-off-by: Zengruan Ye > Signed-off-by: Usama Arif > --- > arch/arm64/include/asm/paravirt.h | 2 + > arch/arm64/include/asm/spinlock.h | 16 +++- > arch/arm64/kernel/paravirt.c | 126 ++++++++++++++++++++++++++++++ > arch/arm64/kernel/setup.c | 3 + > include/linux/cpuhotplug.h | 1 + > 5 files changed, 147 insertions(+), 1 deletion(-) > > diff --git a/arch/arm64/include/asm/paravirt.h b/arch/arm64/include/asm/paravirt.h > index 9aa193e0e8f2..4ccb4356c56b 100644 > --- a/arch/arm64/include/asm/paravirt.h > +++ b/arch/arm64/include/asm/paravirt.h > @@ -19,10 +19,12 @@ static inline u64 paravirt_steal_clock(int cpu) > } > > int __init pv_time_init(void); > +int __init pv_lock_init(void); > > #else > > #define pv_time_init() do {} while (0) > +#define pv_lock_init() do {} while (0) > > #endif // CONFIG_PARAVIRT > > diff --git a/arch/arm64/include/asm/spinlock.h b/arch/arm64/include/asm/spinlock.h > index 0525c0b089ed..7023efa4de96 100644 > --- a/arch/arm64/include/asm/spinlock.h > +++ b/arch/arm64/include/asm/spinlock.h > @@ -10,7 +10,20 @@ > > /* See include/linux/spinlock.h */ > #define smp_mb__after_spinlock() smp_mb() > +#define vcpu_is_preempted vcpu_is_preempted > + > +#ifdef CONFIG_PARAVIRT > +#include > + > +bool dummy_vcpu_is_preempted(int cpu); > > +DECLARE_STATIC_CALL(pv_vcpu_is_preempted, dummy_vcpu_is_preempted); > +static inline bool vcpu_is_preempted(int cpu) > +{ > + return static_call(pv_vcpu_is_preempted)(cpu); > +} > + > +#else > /* > * Changing this will break osq_lock() thanks to the call inside > * smp_cond_load_relaxed(). > @@ -18,10 +31,11 @@ > * See: > * https://lore.kernel.org/lkml/20200110100612.GC2827@hirez.programming.kicks-ass.net > */ > -#define vcpu_is_preempted vcpu_is_preempted > static inline bool vcpu_is_preempted(int cpu) > { > return false; > } > > +#endif /* CONFIG_PARAVIRT */ > + > #endif /* __ASM_SPINLOCK_H */ > diff --git a/arch/arm64/kernel/paravirt.c b/arch/arm64/kernel/paravirt.c > index 57c7c211f8c7..45bcca87bed7 100644 > --- a/arch/arm64/kernel/paravirt.c > +++ b/arch/arm64/kernel/paravirt.c > @@ -22,6 +22,7 @@ > > #include > #include > +#include > #include > > struct static_key paravirt_steal_enabled; > @@ -38,7 +39,12 @@ struct pv_time_stolen_time_region { > struct pvclock_vcpu_stolen_time __rcu *kaddr; > }; > > +struct pv_lock_state_region { > + struct pvlock_vcpu_state __rcu *kaddr; > +}; > + > static DEFINE_PER_CPU(struct pv_time_stolen_time_region, stolen_time_region); > +static DEFINE_PER_CPU(struct pv_lock_state_region, lock_state_region); > > static bool steal_acc = true; > static int __init parse_no_stealacc(char *arg) > @@ -178,3 +184,123 @@ int __init pv_time_init(void) > > return 0; > } > + > +static bool native_vcpu_is_preempted(int cpu) > +{ > + return false; > +} > + > +DEFINE_STATIC_CALL(pv_vcpu_is_preempted, native_vcpu_is_preempted); > + > +static bool para_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); > +} > + > +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 (arm_smccc_1_1_get_conduit() == SMCCC_CONDUIT_NONE) > + return false; This is unnecessary as arm_smccc_1_1_invoke() will return failure if there's no conduit (or pre-SMCCC 1.1). I suspect this was a copy/paste from has_pv_steal_clock() which also has the unnecessary check (patch welcome ;) ). > + > + arm_smccc_1_1_invoke(ARM_SMCCC_ARCH_FEATURES_FUNC_ID, > + ARM_SMCCC_HV_PV_LOCK_FEATURES, &res); Since this is a 'OWNER_VENDOR_HYP' call this should really be preceded by a check that we're running under the expected hypervisor. See e.g. kvm_init_hyp_services(). Of course for KVM we already have a (different) discovery mechanism and this could be included as a ARM_SMCCC_KVM_FUNC_xxx feature. This has_kvm_pvlock() function would then simply be: static bool has_kvm_pvlock(void) { return kvm_arm_hyp_service_available(ARM_SMCC_KVM_FUNC_PVLOCK); } Steve > + > + if (res.a0 != SMCCC_RET_SUCCESS) > + return false; > + > + arm_smccc_1_1_invoke(ARM_SMCCC_HV_PV_LOCK_FEATURES, > + ARM_SMCCC_HV_PV_LOCK_PREEMPTED, &res); > + > + return (res.a0 == SMCCC_RET_SUCCESS); > +} > + > +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; > + > + static_call_update(pv_vcpu_is_preempted, para_vcpu_is_preempted); > + pr_info("using PV-lock preempted\n"); > + > + return 0; > +} > \ No newline at end of file > diff --git a/arch/arm64/kernel/setup.c b/arch/arm64/kernel/setup.c > index fea3223704b6..05ca07ac5800 100644 > --- a/arch/arm64/kernel/setup.c > +++ b/arch/arm64/kernel/setup.c > @@ -42,6 +42,7 @@ > #include > #include > #include > +#include > #include > #include > #include > @@ -360,6 +361,8 @@ void __init __no_sanitize_address 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_sw_tags(); > > diff --git a/include/linux/cpuhotplug.h b/include/linux/cpuhotplug.h > index f61447913db9..c0ee11855c73 100644 > --- a/include/linux/cpuhotplug.h > +++ b/include/linux/cpuhotplug.h > @@ -192,6 +192,7 @@ enum cpuhp_state { > /* Must be the last timer callback */ > CPUHP_AP_DUMMY_TIMER_STARTING, > CPUHP_AP_ARM_XEN_STARTING, > + CPUHP_AP_ARM_KVM_PVLOCK_STARTING, > CPUHP_AP_ARM_CORESIGHT_STARTING, > CPUHP_AP_ARM_CORESIGHT_CTI_STARTING, > CPUHP_AP_ARM64_ISNDEP_STARTING, From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from mm01.cs.columbia.edu (mm01.cs.columbia.edu [128.59.11.253]) by smtp.lore.kernel.org (Postfix) with ESMTP id AC13BC43217 for ; Thu, 3 Nov 2022 13:25:38 +0000 (UTC) Received: from localhost (localhost [127.0.0.1]) by mm01.cs.columbia.edu (Postfix) with ESMTP id 2E2694B6B2; Thu, 3 Nov 2022 09:25:38 -0400 (EDT) X-Virus-Scanned: at lists.cs.columbia.edu Received: from mm01.cs.columbia.edu ([127.0.0.1]) by localhost (mm01.cs.columbia.edu [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id myrZI9c73o-I; Thu, 3 Nov 2022 09:25:36 -0400 (EDT) Received: from mm01.cs.columbia.edu (localhost [127.0.0.1]) by mm01.cs.columbia.edu (Postfix) with ESMTP id 921AE4B643; Thu, 3 Nov 2022 09:25:36 -0400 (EDT) Received: from localhost (localhost [127.0.0.1]) by mm01.cs.columbia.edu (Postfix) with ESMTP id DABFC4B636 for ; Thu, 3 Nov 2022 09:25:35 -0400 (EDT) X-Virus-Scanned: at lists.cs.columbia.edu Received: from mm01.cs.columbia.edu ([127.0.0.1]) by localhost (mm01.cs.columbia.edu [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id mQs44ramxcNE for ; Thu, 3 Nov 2022 09:25:33 -0400 (EDT) Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by mm01.cs.columbia.edu (Postfix) with ESMTP id CDB9C4B635 for ; Thu, 3 Nov 2022 09:25:33 -0400 (EDT) Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id 32C751FB; Thu, 3 Nov 2022 06:25:39 -0700 (PDT) Received: from [10.1.39.27] (e122027.cambridge.arm.com [10.1.39.27]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id 654313F534; Thu, 3 Nov 2022 06:25:29 -0700 (PDT) Message-ID: <76f59579-b701-a243-2a50-72a1401d3a65@arm.com> Date: Thu, 3 Nov 2022 13:25:27 +0000 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.2.2 Subject: Re: [RFC 5/6] KVM: arm64: Support the VCPU preemption check Content-Language: en-GB To: Usama Arif , linux-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org, kvmarm@lists.cs.columbia.edu, kvm@vger.kernel.org, linux-doc@vger.kernel.org, virtualization@lists.linux-foundation.org, linux@armlinux.org.uk, yezengruan@huawei.com, catalin.marinas@arm.com, will@kernel.org, maz@kernel.org, mark.rutland@arm.com References: <20221102161340.2982090-1-usama.arif@bytedance.com> <20221102161340.2982090-6-usama.arif@bytedance.com> From: Steven Price In-Reply-To: <20221102161340.2982090-6-usama.arif@bytedance.com> Cc: punit.agrawal@bytedance.com, fam.zheng@bytedance.com, liangma@liangbit.com X-BeenThere: kvmarm@lists.cs.columbia.edu X-Mailman-Version: 2.1.14 Precedence: list List-Id: Where KVM/ARM decisions are made List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Errors-To: kvmarm-bounces@lists.cs.columbia.edu Sender: kvmarm-bounces@lists.cs.columbia.edu On 02/11/2022 16:13, Usama Arif 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. > > Signed-off-by: Zengruan Ye > Signed-off-by: Usama Arif > --- > arch/arm64/include/asm/paravirt.h | 2 + > arch/arm64/include/asm/spinlock.h | 16 +++- > arch/arm64/kernel/paravirt.c | 126 ++++++++++++++++++++++++++++++ > arch/arm64/kernel/setup.c | 3 + > include/linux/cpuhotplug.h | 1 + > 5 files changed, 147 insertions(+), 1 deletion(-) > > diff --git a/arch/arm64/include/asm/paravirt.h b/arch/arm64/include/asm/paravirt.h > index 9aa193e0e8f2..4ccb4356c56b 100644 > --- a/arch/arm64/include/asm/paravirt.h > +++ b/arch/arm64/include/asm/paravirt.h > @@ -19,10 +19,12 @@ static inline u64 paravirt_steal_clock(int cpu) > } > > int __init pv_time_init(void); > +int __init pv_lock_init(void); > > #else > > #define pv_time_init() do {} while (0) > +#define pv_lock_init() do {} while (0) > > #endif // CONFIG_PARAVIRT > > diff --git a/arch/arm64/include/asm/spinlock.h b/arch/arm64/include/asm/spinlock.h > index 0525c0b089ed..7023efa4de96 100644 > --- a/arch/arm64/include/asm/spinlock.h > +++ b/arch/arm64/include/asm/spinlock.h > @@ -10,7 +10,20 @@ > > /* See include/linux/spinlock.h */ > #define smp_mb__after_spinlock() smp_mb() > +#define vcpu_is_preempted vcpu_is_preempted > + > +#ifdef CONFIG_PARAVIRT > +#include > + > +bool dummy_vcpu_is_preempted(int cpu); > > +DECLARE_STATIC_CALL(pv_vcpu_is_preempted, dummy_vcpu_is_preempted); > +static inline bool vcpu_is_preempted(int cpu) > +{ > + return static_call(pv_vcpu_is_preempted)(cpu); > +} > + > +#else > /* > * Changing this will break osq_lock() thanks to the call inside > * smp_cond_load_relaxed(). > @@ -18,10 +31,11 @@ > * See: > * https://lore.kernel.org/lkml/20200110100612.GC2827@hirez.programming.kicks-ass.net > */ > -#define vcpu_is_preempted vcpu_is_preempted > static inline bool vcpu_is_preempted(int cpu) > { > return false; > } > > +#endif /* CONFIG_PARAVIRT */ > + > #endif /* __ASM_SPINLOCK_H */ > diff --git a/arch/arm64/kernel/paravirt.c b/arch/arm64/kernel/paravirt.c > index 57c7c211f8c7..45bcca87bed7 100644 > --- a/arch/arm64/kernel/paravirt.c > +++ b/arch/arm64/kernel/paravirt.c > @@ -22,6 +22,7 @@ > > #include > #include > +#include > #include > > struct static_key paravirt_steal_enabled; > @@ -38,7 +39,12 @@ struct pv_time_stolen_time_region { > struct pvclock_vcpu_stolen_time __rcu *kaddr; > }; > > +struct pv_lock_state_region { > + struct pvlock_vcpu_state __rcu *kaddr; > +}; > + > static DEFINE_PER_CPU(struct pv_time_stolen_time_region, stolen_time_region); > +static DEFINE_PER_CPU(struct pv_lock_state_region, lock_state_region); > > static bool steal_acc = true; > static int __init parse_no_stealacc(char *arg) > @@ -178,3 +184,123 @@ int __init pv_time_init(void) > > return 0; > } > + > +static bool native_vcpu_is_preempted(int cpu) > +{ > + return false; > +} > + > +DEFINE_STATIC_CALL(pv_vcpu_is_preempted, native_vcpu_is_preempted); > + > +static bool para_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); > +} > + > +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 (arm_smccc_1_1_get_conduit() == SMCCC_CONDUIT_NONE) > + return false; This is unnecessary as arm_smccc_1_1_invoke() will return failure if there's no conduit (or pre-SMCCC 1.1). I suspect this was a copy/paste from has_pv_steal_clock() which also has the unnecessary check (patch welcome ;) ). > + > + arm_smccc_1_1_invoke(ARM_SMCCC_ARCH_FEATURES_FUNC_ID, > + ARM_SMCCC_HV_PV_LOCK_FEATURES, &res); Since this is a 'OWNER_VENDOR_HYP' call this should really be preceded by a check that we're running under the expected hypervisor. See e.g. kvm_init_hyp_services(). Of course for KVM we already have a (different) discovery mechanism and this could be included as a ARM_SMCCC_KVM_FUNC_xxx feature. This has_kvm_pvlock() function would then simply be: static bool has_kvm_pvlock(void) { return kvm_arm_hyp_service_available(ARM_SMCC_KVM_FUNC_PVLOCK); } Steve > + > + if (res.a0 != SMCCC_RET_SUCCESS) > + return false; > + > + arm_smccc_1_1_invoke(ARM_SMCCC_HV_PV_LOCK_FEATURES, > + ARM_SMCCC_HV_PV_LOCK_PREEMPTED, &res); > + > + return (res.a0 == SMCCC_RET_SUCCESS); > +} > + > +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; > + > + static_call_update(pv_vcpu_is_preempted, para_vcpu_is_preempted); > + pr_info("using PV-lock preempted\n"); > + > + return 0; > +} > \ No newline at end of file > diff --git a/arch/arm64/kernel/setup.c b/arch/arm64/kernel/setup.c > index fea3223704b6..05ca07ac5800 100644 > --- a/arch/arm64/kernel/setup.c > +++ b/arch/arm64/kernel/setup.c > @@ -42,6 +42,7 @@ > #include > #include > #include > +#include > #include > #include > #include > @@ -360,6 +361,8 @@ void __init __no_sanitize_address 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_sw_tags(); > > diff --git a/include/linux/cpuhotplug.h b/include/linux/cpuhotplug.h > index f61447913db9..c0ee11855c73 100644 > --- a/include/linux/cpuhotplug.h > +++ b/include/linux/cpuhotplug.h > @@ -192,6 +192,7 @@ enum cpuhp_state { > /* Must be the last timer callback */ > CPUHP_AP_DUMMY_TIMER_STARTING, > CPUHP_AP_ARM_XEN_STARTING, > + CPUHP_AP_ARM_KVM_PVLOCK_STARTING, > CPUHP_AP_ARM_CORESIGHT_STARTING, > CPUHP_AP_ARM_CORESIGHT_CTI_STARTING, > CPUHP_AP_ARM64_ISNDEP_STARTING, _______________________________________________ kvmarm mailing list kvmarm@lists.cs.columbia.edu https://lists.cs.columbia.edu/mailman/listinfo/kvmarm From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 199CDC433FE for ; Thu, 3 Nov 2022 13:26:58 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:In-Reply-To:From:References:Cc:To: Subject:MIME-Version:Date:Message-ID:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=PbdF8ikZmnHga0qPq/CmfuuBFiFbxzF5+1SV8r0GlZI=; b=WqjSdR/OWuhf5S md7f/Nxs514mOkhV4hkpCCLXVUAWVtjzKv1eGITnr75mCKXBZ8bJ673MYcEI2FE/0BSah4AR8xkEQ /bJOKaYcSTM3zqA9FW8/OyVSL3qOrp+TC/ZYhbMFauliqpsP9eMVf3p0JTWJNNpiTfjmxQ7PWLC/Z vGBIeFB3lbDp6yxErYpfKJAbfecHfNPZ84F5XjCJL/oh29wW2y8U45OJYsfIVU/3ueR9VTm7PuTlV qDcwRbMhOhtNWfygpvRmK4SNPG3wQmjlSB7gplxiAi6gE/6aZzrI6oCIBw5GJol9Ww+jxQfLWDKP7 2RoS3iZt3AORWE7rnbjg==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1oqaE9-00HVff-FU; Thu, 03 Nov 2022 13:25:49 +0000 Received: from foss.arm.com ([217.140.110.172]) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1oqaE1-00HVcD-89 for linux-arm-kernel@lists.infradead.org; Thu, 03 Nov 2022 13:25:47 +0000 Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id 32C751FB; Thu, 3 Nov 2022 06:25:39 -0700 (PDT) Received: from [10.1.39.27] (e122027.cambridge.arm.com [10.1.39.27]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id 654313F534; Thu, 3 Nov 2022 06:25:29 -0700 (PDT) Message-ID: <76f59579-b701-a243-2a50-72a1401d3a65@arm.com> Date: Thu, 3 Nov 2022 13:25:27 +0000 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.2.2 Subject: Re: [RFC 5/6] KVM: arm64: Support the VCPU preemption check Content-Language: en-GB To: Usama Arif , linux-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org, kvmarm@lists.cs.columbia.edu, kvm@vger.kernel.org, linux-doc@vger.kernel.org, virtualization@lists.linux-foundation.org, linux@armlinux.org.uk, yezengruan@huawei.com, catalin.marinas@arm.com, will@kernel.org, maz@kernel.org, mark.rutland@arm.com Cc: fam.zheng@bytedance.com, liangma@liangbit.com, punit.agrawal@bytedance.com References: <20221102161340.2982090-1-usama.arif@bytedance.com> <20221102161340.2982090-6-usama.arif@bytedance.com> From: Steven Price In-Reply-To: <20221102161340.2982090-6-usama.arif@bytedance.com> X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20221103_062544_820461_A8EDCC28 X-CRM114-Status: GOOD ( 29.53 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org On 02/11/2022 16:13, Usama Arif 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. > > Signed-off-by: Zengruan Ye > Signed-off-by: Usama Arif > --- > arch/arm64/include/asm/paravirt.h | 2 + > arch/arm64/include/asm/spinlock.h | 16 +++- > arch/arm64/kernel/paravirt.c | 126 ++++++++++++++++++++++++++++++ > arch/arm64/kernel/setup.c | 3 + > include/linux/cpuhotplug.h | 1 + > 5 files changed, 147 insertions(+), 1 deletion(-) > > diff --git a/arch/arm64/include/asm/paravirt.h b/arch/arm64/include/asm/paravirt.h > index 9aa193e0e8f2..4ccb4356c56b 100644 > --- a/arch/arm64/include/asm/paravirt.h > +++ b/arch/arm64/include/asm/paravirt.h > @@ -19,10 +19,12 @@ static inline u64 paravirt_steal_clock(int cpu) > } > > int __init pv_time_init(void); > +int __init pv_lock_init(void); > > #else > > #define pv_time_init() do {} while (0) > +#define pv_lock_init() do {} while (0) > > #endif // CONFIG_PARAVIRT > > diff --git a/arch/arm64/include/asm/spinlock.h b/arch/arm64/include/asm/spinlock.h > index 0525c0b089ed..7023efa4de96 100644 > --- a/arch/arm64/include/asm/spinlock.h > +++ b/arch/arm64/include/asm/spinlock.h > @@ -10,7 +10,20 @@ > > /* See include/linux/spinlock.h */ > #define smp_mb__after_spinlock() smp_mb() > +#define vcpu_is_preempted vcpu_is_preempted > + > +#ifdef CONFIG_PARAVIRT > +#include > + > +bool dummy_vcpu_is_preempted(int cpu); > > +DECLARE_STATIC_CALL(pv_vcpu_is_preempted, dummy_vcpu_is_preempted); > +static inline bool vcpu_is_preempted(int cpu) > +{ > + return static_call(pv_vcpu_is_preempted)(cpu); > +} > + > +#else > /* > * Changing this will break osq_lock() thanks to the call inside > * smp_cond_load_relaxed(). > @@ -18,10 +31,11 @@ > * See: > * https://lore.kernel.org/lkml/20200110100612.GC2827@hirez.programming.kicks-ass.net > */ > -#define vcpu_is_preempted vcpu_is_preempted > static inline bool vcpu_is_preempted(int cpu) > { > return false; > } > > +#endif /* CONFIG_PARAVIRT */ > + > #endif /* __ASM_SPINLOCK_H */ > diff --git a/arch/arm64/kernel/paravirt.c b/arch/arm64/kernel/paravirt.c > index 57c7c211f8c7..45bcca87bed7 100644 > --- a/arch/arm64/kernel/paravirt.c > +++ b/arch/arm64/kernel/paravirt.c > @@ -22,6 +22,7 @@ > > #include > #include > +#include > #include > > struct static_key paravirt_steal_enabled; > @@ -38,7 +39,12 @@ struct pv_time_stolen_time_region { > struct pvclock_vcpu_stolen_time __rcu *kaddr; > }; > > +struct pv_lock_state_region { > + struct pvlock_vcpu_state __rcu *kaddr; > +}; > + > static DEFINE_PER_CPU(struct pv_time_stolen_time_region, stolen_time_region); > +static DEFINE_PER_CPU(struct pv_lock_state_region, lock_state_region); > > static bool steal_acc = true; > static int __init parse_no_stealacc(char *arg) > @@ -178,3 +184,123 @@ int __init pv_time_init(void) > > return 0; > } > + > +static bool native_vcpu_is_preempted(int cpu) > +{ > + return false; > +} > + > +DEFINE_STATIC_CALL(pv_vcpu_is_preempted, native_vcpu_is_preempted); > + > +static bool para_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); > +} > + > +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 (arm_smccc_1_1_get_conduit() == SMCCC_CONDUIT_NONE) > + return false; This is unnecessary as arm_smccc_1_1_invoke() will return failure if there's no conduit (or pre-SMCCC 1.1). I suspect this was a copy/paste from has_pv_steal_clock() which also has the unnecessary check (patch welcome ;) ). > + > + arm_smccc_1_1_invoke(ARM_SMCCC_ARCH_FEATURES_FUNC_ID, > + ARM_SMCCC_HV_PV_LOCK_FEATURES, &res); Since this is a 'OWNER_VENDOR_HYP' call this should really be preceded by a check that we're running under the expected hypervisor. See e.g. kvm_init_hyp_services(). Of course for KVM we already have a (different) discovery mechanism and this could be included as a ARM_SMCCC_KVM_FUNC_xxx feature. This has_kvm_pvlock() function would then simply be: static bool has_kvm_pvlock(void) { return kvm_arm_hyp_service_available(ARM_SMCC_KVM_FUNC_PVLOCK); } Steve > + > + if (res.a0 != SMCCC_RET_SUCCESS) > + return false; > + > + arm_smccc_1_1_invoke(ARM_SMCCC_HV_PV_LOCK_FEATURES, > + ARM_SMCCC_HV_PV_LOCK_PREEMPTED, &res); > + > + return (res.a0 == SMCCC_RET_SUCCESS); > +} > + > +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; > + > + static_call_update(pv_vcpu_is_preempted, para_vcpu_is_preempted); > + pr_info("using PV-lock preempted\n"); > + > + return 0; > +} > \ No newline at end of file > diff --git a/arch/arm64/kernel/setup.c b/arch/arm64/kernel/setup.c > index fea3223704b6..05ca07ac5800 100644 > --- a/arch/arm64/kernel/setup.c > +++ b/arch/arm64/kernel/setup.c > @@ -42,6 +42,7 @@ > #include > #include > #include > +#include > #include > #include > #include > @@ -360,6 +361,8 @@ void __init __no_sanitize_address 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_sw_tags(); > > diff --git a/include/linux/cpuhotplug.h b/include/linux/cpuhotplug.h > index f61447913db9..c0ee11855c73 100644 > --- a/include/linux/cpuhotplug.h > +++ b/include/linux/cpuhotplug.h > @@ -192,6 +192,7 @@ enum cpuhp_state { > /* Must be the last timer callback */ > CPUHP_AP_DUMMY_TIMER_STARTING, > CPUHP_AP_ARM_XEN_STARTING, > + CPUHP_AP_ARM_KVM_PVLOCK_STARTING, > CPUHP_AP_ARM_CORESIGHT_STARTING, > CPUHP_AP_ARM_CORESIGHT_CTI_STARTING, > CPUHP_AP_ARM64_ISNDEP_STARTING, _______________________________________________ linux-arm-kernel mailing list linux-arm-kernel@lists.infradead.org http://lists.infradead.org/mailman/listinfo/linux-arm-kernel