From patchwork Tue Sep 29 09:17:26 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jingyi Wang X-Patchwork-Id: 11805473 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id EBDE51580 for ; Tue, 29 Sep 2020 09:40:37 +0000 (UTC) Received: from merlin.infradead.org (merlin.infradead.org [205.233.59.134]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id A593F20773 for ; Tue, 29 Sep 2020 09:40:37 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=lists.infradead.org header.i=@lists.infradead.org header.b="F7fUuVEx"; dkim=fail reason="signature verification failed" (2048-bit key) header.d=infradead.org header.i=@infradead.org header.b="rDDnEBl8" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org A593F20773 Authentication-Results: mail.kernel.org; dmarc=none (p=none dis=none) header.from=huawei.com Authentication-Results: mail.kernel.org; spf=none smtp.mailfrom=linux-arm-kernel-bounces+patchwork-linux-arm=patchwork.kernel.org@lists.infradead.org DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=merlin.20170209; h=Sender:Content-Transfer-Encoding: Content-Type:Cc:List-Subscribe:List-Help:List-Post:List-Archive: List-Unsubscribe:List-Id:MIME-Version:References:In-Reply-To:Message-ID:Date: Subject:To:From:Reply-To:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=1PYyqd3Pe5m2GttOLnQWbhIn/4oecJRdSCDb2SPUTJI=; b=F7fUuVExSiWKdwice7NJ7UU+n pXUqcwBbKQv0B8M5t4okYF58o/EpU9DrK3OUjDWt0PNUTcdOyFyuJVFEoRy2sGP1JgWhCVZMrYQMt GfwO/v3ZbTHL1C9AGmHu0fMbdLafJZvK6DVZMgxcQYHxwbfHk+VxgYNQ/RyI+3dy3Zr3wZKZP4zHD 0t0V4McmFYpEl7HFg90Q3ebX2iMt51UQ5/mCd45zNDgUegOrz+CPb+SsctwFsINez6zPxINkGxcdK nUdlLh/Oxn/TZBwZ815NMYCp3Vz4WrvKhSxqkQEKPEHQP2/LmrvCIg8C7c11/SuQ2RSZKRSpJRbRS eF0QX6aig==; Received: from localhost ([::1] helo=merlin.infradead.org) by merlin.infradead.org with esmtp (Exim 4.92.3 #3 (Red Hat Linux)) id 1kNC7N-0000xg-2t; Tue, 29 Sep 2020 09:40:17 +0000 Received: from casper.infradead.org ([2001:8b0:10b:1236::1]) by merlin.infradead.org with esmtps (Exim 4.92.3 #3 (Red Hat Linux)) id 1kNC7L-0000vN-4e for linux-arm-kernel@merlin.infradead.org; Tue, 29 Sep 2020 09:40:15 +0000 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=casper.20170209; h=Content-Type:MIME-Version:References: In-Reply-To:Message-ID:Date:Subject:CC:To:From:Sender:Reply-To: Content-Transfer-Encoding:Content-ID:Content-Description; bh=jYGqDxXgrvHhfr8kUdik0WSscujBEyuUkSITSC/wv04=; b=rDDnEBl8ogE+NAMX4zC18ua5l3 admBMpWRy2WTvdrpGqcJ1Z8I7SrJHMFdyu1DMb960hA8XoQCvOAPHXNVpGLB8nBmI00wu5NUwG6jk dg4Q8q2IO4nfem1tUAm3xuQy2yPYjtkihlIbjnYNgk6AeTOx4lxmSvIGL7Di9rKphpC00duUswvGh nuYYjyfpk56SJtCqYszPDuTQw5j/CzM7hIPVtPkOy+8SpphTjkSbgKa+L0UVcibrzO3MgRDsbGTFS dtYAUOAlzVFmc5wOVOagwe/0Dq1s9AY0sr08Xa1oYcWB5WU0n53m3s0o3gwUdoPm4g1gcPEfOjh3N czDbDlvA==; Received: from szxga05-in.huawei.com ([45.249.212.191] helo=huawei.com) by casper.infradead.org with esmtps (Exim 4.92.3 #3 (Red Hat Linux)) id 1kNBmE-000658-Br for linux-arm-kernel@lists.infradead.org; Tue, 29 Sep 2020 09:18:29 +0000 Received: from DGGEMS404-HUB.china.huawei.com (unknown [172.30.72.58]) by Forcepoint Email with ESMTP id B1E9BF27A1E5B154B283; Tue, 29 Sep 2020 17:18:07 +0800 (CST) Received: from DESKTOP-FPN2511.china.huawei.com (10.174.187.69) by DGGEMS404-HUB.china.huawei.com (10.3.19.204) with Microsoft SMTP Server id 14.3.487.0; Tue, 29 Sep 2020 17:17:58 +0800 From: Jingyi Wang To: , , Subject: [RFC PATCH 3/4] KVM: arm64: Use dynamic TWE Delay value Date: Tue, 29 Sep 2020 17:17:26 +0800 Message-ID: <20200929091727.8692-4-wangjingyi11@huawei.com> X-Mailer: git-send-email 2.14.1.windows.1 In-Reply-To: <20200929091727.8692-1-wangjingyi11@huawei.com> References: <20200929091727.8692-1-wangjingyi11@huawei.com> MIME-Version: 1.0 X-Originating-IP: [10.174.187.69] X-CFilter-Loop: Reflected X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20200929_101826_825191_D9015FCB X-CRM114-Status: GOOD ( 15.34 ) X-Spam-Score: -4.2 (----) X-Spam-Report: SpamAssassin version 3.4.4 on casper.infradead.org summary: Content analysis details: (-4.2 points, 5.0 required) pts rule name description ---- ---------------------- -------------------------------------------------- -2.3 RCVD_IN_DNSWL_MED RBL: Sender listed at https://www.dnswl.org/, medium trust [45.249.212.191 listed in list.dnswl.org] 0.0 RCVD_IN_MSPIKE_H4 RBL: Very Good reputation (+4) [45.249.212.191 listed in wl.mailspike.net] -1.9 BAYES_00 BODY: Bayes spam probability is 0 to 1% [score: 0.0000] -0.0 SPF_PASS SPF: sender matches SPF record -0.0 SPF_HELO_PASS SPF: HELO matches SPF record 0.0 RCVD_IN_MSPIKE_WL Mailspike good senders X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: suzuki.poulose@arm.com, catalin.marinas@arm.com, wangjingyi11@huawei.com, shameerali.kolothum.thodi@huawei.com, yezengruan@huawei.com, fanhenglong@huawei.com, james.morse@arm.com, prime.zeng@hisilicon.com, maz@kernel.org, wanghaibin.wang@huawei.com, will@kernel.org, julien.thierry.kdev@gmail.com Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+patchwork-linux-arm=patchwork.kernel.org@lists.infradead.org We implement two new kernel parameters for changing the delay adaptively: twed_grow and twed_shrink twed_grow affects the delay on WFE trap and twed_shrink does it on sched_in; depending on their value, the delay is modifier like this: twed_shrink/ | twed_grow | WFE trap exit | sched_in --------------+---------------+------------ < 1 | = twed | = twed otherwise | = twed + 1 | = twed - 1 Signed-off-by: Zengruan Ye Signed-off-by: Jingyi Wang --- arch/arm64/include/asm/kvm_emulate.h | 6 ++++++ arch/arm64/include/asm/kvm_host.h | 6 +++++- arch/arm64/kvm/arm.c | 32 ++++++++++++++++++++++++++++ arch/arm64/kvm/handle_exit.c | 2 ++ 4 files changed, 45 insertions(+), 1 deletion(-) diff --git a/arch/arm64/include/asm/kvm_emulate.h b/arch/arm64/include/asm/kvm_emulate.h index 1a9cce836170..546d10b3b534 100644 --- a/arch/arm64/include/asm/kvm_emulate.h +++ b/arch/arm64/include/asm/kvm_emulate.h @@ -116,16 +116,22 @@ static inline void vcpu_twed_disable(struct kvm_vcpu *vcpu) static inline void vcpu_twed_init(struct kvm_vcpu *vcpu) { vcpu->arch.twed = (u64)twed; + vcpu->arch.twed_dirty = true; } static inline void vcpu_set_twed(struct kvm_vcpu *vcpu) { u64 delay = vcpu->arch.twed; + + if (!vcpu->arch.twed_dirty) + return; + if (delay > HCR_TWEDEL_MAX) delay = HCR_TWEDEL_MAX; vcpu->arch.hcr_el2 &= ~HCR_TWEDEL_MASK; vcpu->arch.hcr_el2 |= (delay << HCR_TWEDEL_SHIFT); + vcpu->arch.twed_dirty = false; } #else static inline void vcpu_twed_enable(struct kvm_vcpu *vcpu) {}; diff --git a/arch/arm64/include/asm/kvm_host.h b/arch/arm64/include/asm/kvm_host.h index 380cd9c8ad0f..35d1953d9d35 100644 --- a/arch/arm64/include/asm/kvm_host.h +++ b/arch/arm64/include/asm/kvm_host.h @@ -375,6 +375,7 @@ struct kvm_vcpu_arch { #ifdef CONFIG_ARM64_TWED /* WFE trap delay */ u64 twed; + bool twed_dirty; #endif }; @@ -595,7 +596,6 @@ void kvm_arm_vcpu_ptrauth_trap(struct kvm_vcpu *vcpu); static inline void kvm_arch_hardware_unsetup(void) {} static inline void kvm_arch_sync_events(struct kvm *kvm) {} -static inline void kvm_arch_sched_in(struct kvm_vcpu *vcpu, int cpu) {} static inline void kvm_arch_vcpu_block_finish(struct kvm_vcpu *vcpu) {} void kvm_arm_init_debug(void); @@ -697,8 +697,12 @@ bool kvm_arm_vcpu_is_finalized(struct kvm_vcpu *vcpu); #define use_twed() (has_twed() && twed_enable) extern bool twed_enable; extern unsigned int twed; +void grow_twed(struct kvm_vcpu *vcpu); +void shrink_twed(struct kvm_vcpu *vcpu); #else #define use_twed() false +static inline void grow_twed(struct kvm_vcpu *vcpu) {}; +static inline void shrink_twed(struct kvm_vcpu *vcpu) {}; #endif #endif /* __ARM64_KVM_HOST_H__ */ diff --git a/arch/arm64/kvm/arm.c b/arch/arm64/kvm/arm.c index 5b5e8b14dcd5..989bffdcb3e9 100644 --- a/arch/arm64/kvm/arm.c +++ b/arch/arm64/kvm/arm.c @@ -65,6 +65,32 @@ module_param(twed_enable, bool, S_IRUGO | S_IWUSR); unsigned int twed = 0; module_param(twed, uint, S_IRUGO | S_IWUSR); + +static unsigned int twed_grow = 0; +module_param(twed_grow, uint, S_IRUGO | S_IWUSR); + +static unsigned int twed_shrink = 0; +module_param(twed_shrink, uint, S_IRUGO | S_IWUSR); + +void grow_twed(struct kvm_vcpu *vcpu) +{ + u64 old = vcpu->arch.twed; + + if (old < HCR_TWEDEL_MAX && twed_grow) { + vcpu->arch.twed += 1; + vcpu->arch.twed_dirty = true; + } +} + +void shrink_twed(struct kvm_vcpu *vcpu) +{ + u64 old = vcpu->arch.twed; + + if (old > 0 && twed_shrink) { + vcpu->arch.twed -= 1; + vcpu->arch.twed_dirty = true; + } +} #endif int kvm_arch_vcpu_should_kick(struct kvm_vcpu *vcpu) @@ -108,6 +134,12 @@ static int kvm_arm_default_max_vcpus(void) return vgic_present ? kvm_vgic_get_max_vcpus() : KVM_MAX_VCPUS; } +void kvm_arch_sched_in(struct kvm_vcpu *vcpu, int cpu) +{ + if (use_twed()) + shrink_twed(vcpu); +} + /** * kvm_arch_init_vm - initializes a VM data structure * @kvm: pointer to the KVM struct diff --git a/arch/arm64/kvm/handle_exit.c b/arch/arm64/kvm/handle_exit.c index 5d690d60ccad..2ad72defa3c9 100644 --- a/arch/arm64/kvm/handle_exit.c +++ b/arch/arm64/kvm/handle_exit.c @@ -91,6 +91,8 @@ static int kvm_handle_wfx(struct kvm_vcpu *vcpu) { if (kvm_vcpu_get_esr(vcpu) & ESR_ELx_WFx_ISS_WFE) { trace_kvm_wfx_arm64(*vcpu_pc(vcpu), true); + if (use_twed()) + grow_twed(vcpu); vcpu->stat.wfe_exit_stat++; kvm_vcpu_on_spin(vcpu, vcpu_mode_priv(vcpu)); } else {