From patchwork Wed May 16 09:08:45 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tangnianyao X-Patchwork-Id: 10403125 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 61EDE601D2 for ; Wed, 16 May 2018 09:11:24 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 4829E28870 for ; Wed, 16 May 2018 09:11:24 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 3850128905; Wed, 16 May 2018 09:11:24 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-2.9 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,MAILING_LIST_MULTI autolearn=unavailable version=3.3.1 Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id 2726628870 for ; Wed, 16 May 2018 09:11:07 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20170209; h=Sender: Content-Transfer-Encoding:Content-Type:Cc:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:MIME-Version:In-Reply-To:References: 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=Fi0KR1tf7XbeXLSQbC08PmIxctBmR1so8FFTknD+1is=; b=QeGJCtV0up3g8i 3Gl+3qYGyjY192hgVcbJdaTX3Nn3D/Fe1x4J+mm0GheEMeNwYenibAMu9/Dq+Kwx8XuTE10mqytIX GOw4931eoL/fnIaeUrv5ZhSyjCHtuQ6khsWoy+AS0a2pvDeKjBOQZW6v+g7X8xmK3I/DONGscAyww S3ZX3kU8tjTJiylWe48zNUYbk3YLVPvAonBC71ZADsw0ZayaIqI/rtfOuMcY+GgRqK8fKEPrzKvyw Hh+qrJLqoN9QjakahOKo+IgQ+1TeBNn2LGGXgwz2yV6Qb7dZmjIX+LnlhJfsZ0D6kVo72WHEH4wxA Jxta46oGc0P9D56DxGOQ==; Received: from localhost ([127.0.0.1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.90_1 #2 (Red Hat Linux)) id 1fIsSa-0001rB-CP; Wed, 16 May 2018 09:11:00 +0000 Received: from szxga02-in.huawei.com ([45.249.212.188] helo=huawei.com) by bombadil.infradead.org with esmtps (Exim 4.90_1 #2 (Red Hat Linux)) id 1fIsSW-0001c1-Qd for linux-arm-kernel@lists.infradead.org; Wed, 16 May 2018 09:10:58 +0000 Received: from dggemi405-hub.china.huawei.com (unknown [172.30.72.57]) by Forcepoint Email with ESMTP id 1B855AE299B28; Wed, 16 May 2018 17:10:35 +0800 (CST) Received: from DGGEMI509-MBS.china.huawei.com ([169.254.2.151]) by dggemi405-hub.china.huawei.com ([10.3.17.143]) with mapi id 14.03.0382.000; Wed, 16 May 2018 17:08:45 +0800 From: "Tangnianyao (ICT)" To: Marc Zyngier , Christoffer Dall , "linux-arm-kernel@lists.infradead.org" , "kvmarm@lists.cs.columbia.edu" , "kvm@vger.kernel.org" Subject: [PATCH] arm64: KVM: reduce guest fpsimd trap Thread-Topic: [PATCH] arm64: KVM: reduce guest fpsimd trap Thread-Index: AQHT7PNQq/NQnnOp40+SiQXPXr6SHqQyDw+A Date: Wed, 16 May 2018 09:08:45 +0000 Message-ID: References: <1526460738-11157-1-git-send-email-zhangshaokun@hisilicon.com> In-Reply-To: <1526460738-11157-1-git-send-email-zhangshaokun@hisilicon.com> Accept-Language: en-US Content-Language: zh-CN X-MS-Has-Attach: X-MS-TNEF-Correlator: x-originating-ip: [10.57.56.75] MIME-Version: 1.0 X-CFilter-Loop: Reflected X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20180516_021057_081592_CCE5E339 X-CRM114-Status: GOOD ( 12.90 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Zhangshaokun Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+patchwork-linux-arm=patchwork.kernel.org@lists.infradead.org X-Virus-Scanned: ClamAV using ClamSMTP Add last_fpsimd_trap to notify if guest last exit reason is handling fpsimd. If guest is using fpsimd frequently, save host's fpsimd state and restore guest's fpsimd state and deactive fpsimd trap before returning to guest. It can avoid guest fpsimd trap soon to improve performance. Signed-off-by: Nianyao Tang --- arch/arm64/kernel/asm-offsets.c | 1 + arch/arm64/kvm/hyp/entry.S | 5 +++++ arch/arm64/kvm/hyp/switch.c | 38 ++++++++++++++++++++++++++++++++++++-- include/linux/kvm_host.h | 1 + 4 files changed, 43 insertions(+), 2 deletions(-) -- 2.7.4 diff --git a/arch/arm64/kernel/asm-offsets.c b/arch/arm64/kernel/asm-offsets.c index 5bdda65..35a9c5c 100644 --- a/arch/arm64/kernel/asm-offsets.c +++ b/arch/arm64/kernel/asm-offsets.c @@ -136,6 +136,7 @@ int main(void) #ifdef CONFIG_KVM_ARM_HOST DEFINE(VCPU_CONTEXT, offsetof(struct kvm_vcpu, arch.ctxt)); DEFINE(VCPU_FAULT_DISR, offsetof(struct kvm_vcpu, arch.fault.disr_el1)); + DEFINE(VCPU_LAST_FPSIMD_TRAP, offsetof(struct kvm_vcpu, + last_fpsimd_trap)); DEFINE(CPU_GP_REGS, offsetof(struct kvm_cpu_context, gp_regs)); DEFINE(CPU_USER_PT_REGS, offsetof(struct kvm_regs, regs)); DEFINE(CPU_FP_REGS, offsetof(struct kvm_regs, fp_regs)); diff --git a/arch/arm64/kvm/hyp/entry.S b/arch/arm64/kvm/hyp/entry.S index e41a161..956e042 100644 --- a/arch/arm64/kvm/hyp/entry.S +++ b/arch/arm64/kvm/hyp/entry.S @@ -197,6 +197,11 @@ alternative_endif add x0, x2, #CPU_GP_REG_OFFSET(CPU_FP_REGS) bl __fpsimd_restore_state + // Mark guest using fpsimd now + ldr x0, [x3, #VCPU_LAST_FPSIMD_TRAP] + add x0, x0, #1 + str x0, [x3, #VCPU_LAST_FPSIMD_TRAP] + // Skip restoring fpexc32 for AArch64 guests mrs x1, hcr_el2 tbnz x1, #HCR_RW_SHIFT, 1f diff --git a/arch/arm64/kvm/hyp/switch.c b/arch/arm64/kvm/hyp/switch.c index d964523..86eea1b 100644 --- a/arch/arm64/kvm/hyp/switch.c +++ b/arch/arm64/kvm/hyp/switch.c @@ -92,7 +92,13 @@ static void activate_traps_vhe(struct kvm_vcpu *vcpu) val = read_sysreg(cpacr_el1); val |= CPACR_EL1_TTA; - val &= ~(CPACR_EL1_FPEN | CPACR_EL1_ZEN); + val &= ~CPACR_EL1_ZEN; + + if (vcpu->last_fpsimd_trap) + val |= CPACR_EL1_FPEN; + else + val &= ~CPACR_EL1_FPEN; + write_sysreg(val, cpacr_el1); write_sysreg(kvm_get_hyp_vector(), vbar_el1); @@ -105,7 +111,13 @@ static void __hyp_text __activate_traps_nvhe(struct kvm_vcpu *vcpu) __activate_traps_common(vcpu); val = CPTR_EL2_DEFAULT; - val |= CPTR_EL2_TTA | CPTR_EL2_TFP | CPTR_EL2_TZ; + val |= CPTR_EL2_TTA | CPTR_EL2_TZ; + + if (vcpu->last_fpsimd_trap) + val &= ~CPTR_EL2_TFP; + else + val |= CPTR_EL2_TFP; + write_sysreg(val, cptr_el2); } @@ -406,6 +418,17 @@ int kvm_vcpu_run_vhe(struct kvm_vcpu *vcpu) __activate_traps(vcpu); __activate_vm(vcpu->kvm); + /* + * If guest last trap to host for handling fpsimd, last_fpsimd_trap + * is set. Restore guest's fpsimd state and deactivate fpsimd trap + * to avoid guest traping soon. + */ + if (vcpu->last_fpsimd_trap) { + __fpsimd_save_state(&host_ctxt->gp_regs.fp_regs); + __fpsimd_restore_state(&guest_ctxt->gp_regs.fp_regs); + vcpu->last_fpsimd_trap = 0; + } + sysreg_restore_guest_state_vhe(guest_ctxt); __debug_switch_to_guest(vcpu); @@ -454,6 +477,17 @@ int __hyp_text __kvm_vcpu_run_nvhe(struct kvm_vcpu *vcpu) __activate_traps(vcpu); __activate_vm(kern_hyp_va(vcpu->kvm)); + /* + * If guest last trap to host for handling fpsimd, last_fpsimd_trap + * is set. Restore guest's fpsimd state and deactivate fpsimd trap + * to avoid guest traping soon. + */ + if (vcpu->last_fpsimd_trap) { + __fpsimd_save_state(&host_ctxt->gp_regs.fp_regs); + __fpsimd_restore_state(&guest_ctxt->gp_regs.fp_regs); + vcpu->last_fpsimd_trap = 0; + } + __hyp_vgic_restore_state(vcpu); __timer_enable_traps(vcpu); diff --git a/include/linux/kvm_host.h b/include/linux/kvm_host.h index 6930c63..46bdf0d 100644 --- a/include/linux/kvm_host.h +++ b/include/linux/kvm_host.h @@ -274,6 +274,7 @@ struct kvm_vcpu { bool preempted; struct kvm_vcpu_arch arch; struct dentry *debugfs_dentry; + unsigned int last_fpsimd_trap; }; static inline int kvm_vcpu_exiting_guest_mode(struct kvm_vcpu *vcpu)