From patchwork Thu Apr 16 10:15:07 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Xiaoyao Li X-Patchwork-Id: 11492835 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 EC4BC112C for ; Thu, 16 Apr 2020 11:45:57 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id DE07A21D7F for ; Thu, 16 Apr 2020 11:45:57 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2505206AbgDPLpy (ORCPT ); Thu, 16 Apr 2020 07:45:54 -0400 Received: from mga01.intel.com ([192.55.52.88]:46619 "EHLO mga01.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2504914AbgDPKdi (ORCPT ); Thu, 16 Apr 2020 06:33:38 -0400 IronPort-SDR: CBFc0+RD/rxjCamx/CRPEvlO6bTrJVUX4OIwq8jHiIvt3Rvp6XRL3YygupM8qsk+0JJ4zR+pQB qT1GHhZyJ7JA== X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from fmsmga004.fm.intel.com ([10.253.24.48]) by fmsmga101.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 16 Apr 2020 03:33:27 -0700 IronPort-SDR: GM7QgTRxLZxNtY5oDGeuldEujPWqCiRNQBU2Z1aqO/DnlR2z2n87sj4SRohvLHQLhs+KMo2hax VASDiE9UKVsA== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.72,390,1580803200"; d="scan'208";a="277947896" Received: from lxy-clx-4s.sh.intel.com ([10.239.43.132]) by fmsmga004.fm.intel.com with ESMTP; 16 Apr 2020 03:33:26 -0700 From: Xiaoyao Li To: Paolo Bonzini , Sean Christopherson , kvm@vger.kernel.org Cc: Nadav Amit , Vitaly Kuznetsov , linux-kernel@vger.kernel.org, Xiaoyao Li Subject: [RFC PATCH 1/3] kvm: x86: Rename KVM_DEBUGREG_RELOAD to KVM_DEBUGREG_NEED_RELOAD Date: Thu, 16 Apr 2020 18:15:07 +0800 Message-Id: <20200416101509.73526-2-xiaoyao.li@intel.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20200416101509.73526-1-xiaoyao.li@intel.com> References: <20200416101509.73526-1-xiaoyao.li@intel.com> MIME-Version: 1.0 Sender: kvm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org To make it more clear that the flag means DRn (except DR7) need to be reloaded before vm entry. Signed-off-by: Xiaoyao Li --- arch/x86/include/asm/kvm_host.h | 2 +- arch/x86/kvm/x86.c | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/arch/x86/include/asm/kvm_host.h b/arch/x86/include/asm/kvm_host.h index c7da23aed79a..f465c76e6e5a 100644 --- a/arch/x86/include/asm/kvm_host.h +++ b/arch/x86/include/asm/kvm_host.h @@ -511,7 +511,7 @@ struct kvm_pmu_ops; enum { KVM_DEBUGREG_BP_ENABLED = 1, KVM_DEBUGREG_WONT_EXIT = 2, - KVM_DEBUGREG_RELOAD = 4, + KVM_DEBUGREG_NEED_RELOAD = 4, }; struct kvm_mtrr_range { diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index de77bc9bd0d7..cce926658d10 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -1067,7 +1067,7 @@ static void kvm_update_dr0123(struct kvm_vcpu *vcpu) if (!(vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP)) { for (i = 0; i < KVM_NR_DB_REGS; i++) vcpu->arch.eff_db[i] = vcpu->arch.db[i]; - vcpu->arch.switch_db_regs |= KVM_DEBUGREG_RELOAD; + vcpu->arch.switch_db_regs |= KVM_DEBUGREG_NEED_RELOAD; } } @@ -8407,7 +8407,7 @@ static int vcpu_enter_guest(struct kvm_vcpu *vcpu) set_debugreg(vcpu->arch.eff_db[2], 2); set_debugreg(vcpu->arch.eff_db[3], 3); set_debugreg(vcpu->arch.dr6, 6); - vcpu->arch.switch_db_regs &= ~KVM_DEBUGREG_RELOAD; + vcpu->arch.switch_db_regs &= ~KVM_DEBUGREG_NEED_RELOAD; } kvm_x86_ops.run(vcpu); @@ -8424,7 +8424,7 @@ static int vcpu_enter_guest(struct kvm_vcpu *vcpu) kvm_update_dr0123(vcpu); kvm_update_dr6(vcpu); kvm_update_dr7(vcpu); - vcpu->arch.switch_db_regs &= ~KVM_DEBUGREG_RELOAD; + vcpu->arch.switch_db_regs &= ~KVM_DEBUGREG_NEED_RELOAD; } /* From patchwork Thu Apr 16 10:15:08 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Xiaoyao Li X-Patchwork-Id: 11492627 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 5060014DD for ; Thu, 16 Apr 2020 10:34:35 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 3803821D92 for ; Thu, 16 Apr 2020 10:34:35 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2505105AbgDPKeO (ORCPT ); Thu, 16 Apr 2020 06:34:14 -0400 Received: from mga01.intel.com ([192.55.52.88]:46605 "EHLO mga01.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2505097AbgDPKeC (ORCPT ); Thu, 16 Apr 2020 06:34:02 -0400 IronPort-SDR: tKTQGJ1hQI7xoal+PAS6ZiNdOXg5na9yp4sV3S/Vit4KwF2E3RMt/RB2+7NrsdaGCF3THanvF/ KdtHa8tuQqnw== X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from fmsmga004.fm.intel.com ([10.253.24.48]) by fmsmga101.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 16 Apr 2020 03:33:29 -0700 IronPort-SDR: JvzddcKGvjliYl2cvOvoOesY9Om7KNGV4b/0is+8tvoi2stVDHbVm1Dz10URsWVpRAGtuhOGmr EJcN4MFuyU3A== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.72,390,1580803200"; d="scan'208";a="277947904" Received: from lxy-clx-4s.sh.intel.com ([10.239.43.132]) by fmsmga004.fm.intel.com with ESMTP; 16 Apr 2020 03:33:27 -0700 From: Xiaoyao Li To: Paolo Bonzini , Sean Christopherson , kvm@vger.kernel.org Cc: Nadav Amit , Vitaly Kuznetsov , linux-kernel@vger.kernel.org, Xiaoyao Li Subject: [RFC PATCH 2/3] kvm: x86: Use KVM_DEBUGREG_NEED_RELOAD instead of KVM_DEBUGREG_BP_ENABLED Date: Thu, 16 Apr 2020 18:15:08 +0800 Message-Id: <20200416101509.73526-3-xiaoyao.li@intel.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20200416101509.73526-1-xiaoyao.li@intel.com> References: <20200416101509.73526-1-xiaoyao.li@intel.com> MIME-Version: 1.0 Sender: kvm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org Once any #BP enabled in DR7, it will set KVM_DEBUGREG_BP_ENABLED, which leads to reload DRn before every VM entry even if none of DRn changed. Drop KVM_DEBUGREG_BP_ENABLED flag and set KVM_DEBUGREG_NEED_RELOAD flag for the cases that DRn need to be reloaded instead, to avoid unnecessary DRn reload. Signed-off-by: Xiaoyao Li --- arch/x86/include/asm/kvm_host.h | 3 +-- arch/x86/kvm/x86.c | 4 ++-- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/arch/x86/include/asm/kvm_host.h b/arch/x86/include/asm/kvm_host.h index f465c76e6e5a..87e2d020351e 100644 --- a/arch/x86/include/asm/kvm_host.h +++ b/arch/x86/include/asm/kvm_host.h @@ -509,9 +509,8 @@ struct kvm_pmu { struct kvm_pmu_ops; enum { - KVM_DEBUGREG_BP_ENABLED = 1, + KVM_DEBUGREG_NEED_RELOAD = 1, KVM_DEBUGREG_WONT_EXIT = 2, - KVM_DEBUGREG_NEED_RELOAD = 4, }; struct kvm_mtrr_range { diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index cce926658d10..71264df64001 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -1086,9 +1086,8 @@ static void kvm_update_dr7(struct kvm_vcpu *vcpu) else dr7 = vcpu->arch.dr7; kvm_x86_ops.set_dr7(vcpu, dr7); - vcpu->arch.switch_db_regs &= ~KVM_DEBUGREG_BP_ENABLED; if (dr7 & DR7_BP_EN_MASK) - vcpu->arch.switch_db_regs |= KVM_DEBUGREG_BP_ENABLED; + vcpu->arch.switch_db_regs |= KVM_DEBUGREG_NEED_RELOAD; } static u64 kvm_dr6_fixed(struct kvm_vcpu *vcpu) @@ -1128,6 +1127,7 @@ static int __kvm_set_dr(struct kvm_vcpu *vcpu, int dr, unsigned long val) break; } + vcpu->arch.switch_db_regs |= KVM_DEBUGREG_NEED_RELOAD; return 0; } From patchwork Thu Apr 16 10:15:09 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Xiaoyao Li X-Patchwork-Id: 11492625 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 4545E913 for ; Thu, 16 Apr 2020 10:34:29 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 370FF206D6 for ; Thu, 16 Apr 2020 10:34:29 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2504984AbgDPKeV (ORCPT ); Thu, 16 Apr 2020 06:34:21 -0400 Received: from mga01.intel.com ([192.55.52.88]:46619 "EHLO mga01.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2505096AbgDPKeC (ORCPT ); Thu, 16 Apr 2020 06:34:02 -0400 IronPort-SDR: CDrhlRQysprIsZyFirOrkx3MXsSk0l3AKyFwcJB+VWntV/yDAzSVifTGoi2pZfwJKjC3hCrNt5 zko0gkyxmeDA== X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from fmsmga004.fm.intel.com ([10.253.24.48]) by fmsmga101.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 16 Apr 2020 03:33:31 -0700 IronPort-SDR: uaGMqjC1D3LJTSHhxkWzZqyX/QcbfjO69rPnA6+ZfklqV8oJiikirHHkpPssHofAXpOL6lPM8f LVq4Ne569yow== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.72,390,1580803200"; d="scan'208";a="277947912" Received: from lxy-clx-4s.sh.intel.com ([10.239.43.132]) by fmsmga004.fm.intel.com with ESMTP; 16 Apr 2020 03:33:29 -0700 From: Xiaoyao Li To: Paolo Bonzini , Sean Christopherson , kvm@vger.kernel.org Cc: Nadav Amit , Vitaly Kuznetsov , linux-kernel@vger.kernel.org, Xiaoyao Li Subject: [RFC PATCH 3/3] kvm: x86: skip DRn reload if previous VM exit is DR access VM exit Date: Thu, 16 Apr 2020 18:15:09 +0800 Message-Id: <20200416101509.73526-4-xiaoyao.li@intel.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20200416101509.73526-1-xiaoyao.li@intel.com> References: <20200416101509.73526-1-xiaoyao.li@intel.com> MIME-Version: 1.0 Sender: kvm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org When DR access vm exit, there is no DRn change throughout VM exit to next VM enter. Skip the DRn reload in this case and fix the comments. Signed-off-by: Xiaoyao Li --- arch/x86/kvm/svm/svm.c | 8 +++++--- arch/x86/kvm/vmx/vmx.c | 8 +++++--- arch/x86/kvm/x86.c | 2 +- 3 files changed, 11 insertions(+), 7 deletions(-) diff --git a/arch/x86/kvm/svm/svm.c b/arch/x86/kvm/svm/svm.c index 66123848448d..c6883a0bf8c3 100644 --- a/arch/x86/kvm/svm/svm.c +++ b/arch/x86/kvm/svm/svm.c @@ -2287,9 +2287,11 @@ static int dr_interception(struct vcpu_svm *svm) if (svm->vcpu.guest_debug == 0) { /* - * No more DR vmexits; force a reload of the debug registers - * and reenter on this instruction. The next vmexit will - * retrieve the full state of the debug registers. + * No more DR vmexits and reenter on this instruction. + * The next vmexit will retrieve the full state of the debug + * registers and re-enable DR vmexits. + * No need to set KVM_DEBUGREG_NEED_RELOAD because no DRn change + * since this DR vmexit. */ clr_dr_intercepts(svm); svm->vcpu.arch.switch_db_regs |= KVM_DEBUGREG_WONT_EXIT; diff --git a/arch/x86/kvm/vmx/vmx.c b/arch/x86/kvm/vmx/vmx.c index aa1b8cf7c915..22eff8503048 100644 --- a/arch/x86/kvm/vmx/vmx.c +++ b/arch/x86/kvm/vmx/vmx.c @@ -4967,9 +4967,11 @@ static int handle_dr(struct kvm_vcpu *vcpu) exec_controls_clearbit(to_vmx(vcpu), CPU_BASED_MOV_DR_EXITING); /* - * No more DR vmexits; force a reload of the debug registers - * and reenter on this instruction. The next vmexit will - * retrieve the full state of the debug registers. + * No more DR vmexits and reenter on this instruction. + * The next vmexit will retrieve the full state of the debug + * registers and re-enable DR vmexits. + * No need to set KVM_DEBUGREG_NEED_RELOAD because no DRn change + * since this DR vmexit. */ vcpu->arch.switch_db_regs |= KVM_DEBUGREG_WONT_EXIT; return 1; diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index 71264df64001..8983848cbf45 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -8400,7 +8400,7 @@ static int vcpu_enter_guest(struct kvm_vcpu *vcpu) if (test_thread_flag(TIF_NEED_FPU_LOAD)) switch_fpu_return(); - if (unlikely(vcpu->arch.switch_db_regs)) { + if (unlikely(vcpu->arch.switch_db_regs & KVM_DEBUGREG_NEED_RELOAD)) { set_debugreg(0, 7); set_debugreg(vcpu->arch.eff_db[0], 0); set_debugreg(vcpu->arch.eff_db[1], 1);