Received: by 2002:a25:1985:0:0:0:0:0 with SMTP id 127csp3195913ybz; Mon, 27 Apr 2020 11:40:50 -0700 (PDT) X-Google-Smtp-Source: APiQypK2vOb1UsmII/GZHWtImGaz8phHgwm32BDjVsAiquXMCU0yTm8CrzF8D0Cpp3dxaG7EcB6M X-Received: by 2002:a17:906:2584:: with SMTP id m4mr21225172ejb.200.1588012850107; Mon, 27 Apr 2020 11:40:50 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1588012850; cv=none; d=google.com; s=arc-20160816; b=rgjSMdji8BzgQVp36aGwxyvlzIGBbpzSG73JkblRCX8VBpmqBXEtoVFTEwea7G5myv uzIR9k1MQbHQClt5q8ShxgEm+7frnFdgwOncfXARQEmlTA2GxVAkexcPPB6k8iNOCi+8 o6aSH7zt9uXlTgmSRahBQknVBLcdYyCcjfgEpe5okuLmqw3Nncw9DXvisDp7QJDa8C7E 6bhw2z67VkIJyTiekrkEmZkmMqWDCn85T69EBmqe7ekWGoBUY76Zi7tuGCMo0Yu3ZQp9 5KRsR3rvkcm6OOsD9i161JUZMGSFbc3qAumIbkJNU2v1nBOfX43RGcC8Cs6ukzZG8wdD 4D3w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:user-agent:in-reply-to :content-disposition:mime-version:references:message-id:subject:cc :to:from:date:ironport-sdr:ironport-sdr; bh=uPWbF0TMaO9AU/MV8rIfiZ82JpA21NVgCW2X6ncOToo=; b=eqKLpQIV0+ZbrphAo6/ryIla+nhhvRD5UJZt3QPyQQgDmTmC/Cpi/+KkaFGBtJ1R4L gWQ7crhyUsD+DL8sy7X7t1Fle2WMCq4N+IPkbj7NP01jdoqs+og0UkWngxe3/qX1CrRi x2I4h4mqRMo9Ixg/8d3IrwSG1/tu26aaLUDEG5TRPupythTkUpIJjtYYzmQVNN5Y0YXP IfXooX13eDtFZJb/4ayXbLuKkKpLD80Jx8IWmjkwVRb53MLYdjqUb8fwTuvsDJkN/LGc DhnNzQWJBekryJOlJ0OunapLsC6W0SrDvqLN0Kx+8BM5/H/Bs2C6Nj5cAaHTt+bIgH9T +1HA== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=intel.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id ce16si250197ejc.526.2020.04.27.11.40.27; Mon, 27 Apr 2020 11:40:50 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726557AbgD0Sg5 (ORCPT + 99 others); Mon, 27 Apr 2020 14:36:57 -0400 Received: from mga17.intel.com ([192.55.52.151]:35122 "EHLO mga17.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725995AbgD0Sg5 (ORCPT ); Mon, 27 Apr 2020 14:36:57 -0400 IronPort-SDR: naqbU4v4yGjOrqXAUFq2F6zB1J1Cn4jWaQrjcAyK54Uln/5rwwjPlNc4UX1qMnSWue73sejTjK yRmMmXovqsOg== X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from orsmga004.jf.intel.com ([10.7.209.38]) by fmsmga107.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 27 Apr 2020 11:36:56 -0700 IronPort-SDR: lhNRKftxecrArb0ifevrmQZSglEPvEDWlICFbxaFvVzcaAQDgs9+HU9KlSIDPL7K1NkjhI77fH 7nED5OMry3Cg== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.73,325,1583222400"; d="scan'208";a="404401595" Received: from sjchrist-coffee.jf.intel.com (HELO linux.intel.com) ([10.54.74.202]) by orsmga004.jf.intel.com with ESMTP; 27 Apr 2020 11:36:56 -0700 Date: Mon, 27 Apr 2020 11:36:56 -0700 From: Sean Christopherson To: Wanpeng Li Cc: LKML , kvm , Paolo Bonzini , Vitaly Kuznetsov , Wanpeng Li , Jim Mattson , Joerg Roedel , Haiwei Li Subject: Re: [PATCH v3 2/5] KVM: X86: Introduce need_cancel_enter_guest helper Message-ID: <20200427183656.GO14870@linux.intel.com> References: <1587709364-19090-1-git-send-email-wanpengli@tencent.com> <1587709364-19090-3-git-send-email-wanpengli@tencent.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: User-Agent: Mutt/1.5.24 (2015-08-30) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Sun, Apr 26, 2020 at 10:05:00AM +0800, Wanpeng Li wrote: > On Fri, 24 Apr 2020 at 14:23, Wanpeng Li wrote: > > > > From: Wanpeng Li > > > > Introduce need_cancel_enter_guest() helper, we need to check some > > conditions before doing CONT_RUN, in addition, it can also catch > > the case vmexit occurred while another event was being delivered > > to guest software since vmx_complete_interrupts() adds the request > > bit. > > > > Tested-by: Haiwei Li > > Cc: Haiwei Li > > Signed-off-by: Wanpeng Li > > --- > > arch/x86/kvm/vmx/vmx.c | 12 +++++++----- > > arch/x86/kvm/x86.c | 10 ++++++++-- > > arch/x86/kvm/x86.h | 1 + > > 3 files changed, 16 insertions(+), 7 deletions(-) > > > > diff --git a/arch/x86/kvm/vmx/vmx.c b/arch/x86/kvm/vmx/vmx.c > > index f1f6638..5c21027 100644 > > --- a/arch/x86/kvm/vmx/vmx.c > > +++ b/arch/x86/kvm/vmx/vmx.c > > @@ -6577,7 +6577,7 @@ bool __vmx_vcpu_run(struct vcpu_vmx *vmx, unsigned long *regs, bool launched); > > > > static enum exit_fastpath_completion vmx_vcpu_run(struct kvm_vcpu *vcpu) > > { > > - enum exit_fastpath_completion exit_fastpath; > > + enum exit_fastpath_completion exit_fastpath = EXIT_FASTPATH_NONE; > > struct vcpu_vmx *vmx = to_vmx(vcpu); > > unsigned long cr3, cr4; > > > > @@ -6754,10 +6754,12 @@ static enum exit_fastpath_completion vmx_vcpu_run(struct kvm_vcpu *vcpu) > > vmx_recover_nmi_blocking(vmx); > > vmx_complete_interrupts(vmx); > > > > - exit_fastpath = vmx_exit_handlers_fastpath(vcpu); > > - /* static call is better with retpolines */ > > - if (exit_fastpath == EXIT_FASTPATH_CONT_RUN) > > - goto cont_run; > > + if (!kvm_need_cancel_enter_guest(vcpu)) { > > + exit_fastpath = vmx_exit_handlers_fastpath(vcpu); > > + /* static call is better with retpolines */ > > + if (exit_fastpath == EXIT_FASTPATH_CONT_RUN) > > + goto cont_run; > > + } > > The kvm_need_cancel_enter_guest() should not before > vmx_exit_handlers_fastpath() which will break IPI fastpath. How about > applying something like below, otherwise, maybe introduce another > EXIT_FASTPATH_CONT_FAIL to indicate fails due to > kvm_need_cancel_enter_guest() if checking it after > vmx_exit_handlers_fastpath(), then we return 1 in vmx_handle_exit() > directly instead of kvm_skip_emulated_instruction(). VMX-preemption > timer exit doesn't need to skip emulated instruction but wrmsr > TSCDEADLINE MSR exit does which results in a little complex here. > > Paolo, what do you think? > > diff --git a/arch/x86/kvm/vmx/vmx.c b/arch/x86/kvm/vmx/vmx.c > index 853d3af..9317924 100644 > --- a/arch/x86/kvm/vmx/vmx.c > +++ b/arch/x86/kvm/vmx/vmx.c > @@ -6564,6 +6564,9 @@ static enum exit_fastpath_completion > handle_fastpath_preemption_timer(struct kvm > { > struct vcpu_vmx *vmx = to_vmx(vcpu); > > + if (kvm_need_cancel_enter_guest(vcpu)) > + return EXIT_FASTPATH_NONE; > + > if (!vmx->req_immediate_exit && > !unlikely(vmx->loaded_vmcs->hv_timer_soft_disabled)) { > kvm_lapic_expired_hv_timer(vcpu); > @@ -6771,12 +6774,10 @@ static enum exit_fastpath_completion > vmx_vcpu_run(struct kvm_vcpu *vcpu) > vmx_recover_nmi_blocking(vmx); > vmx_complete_interrupts(vmx); > > - if (!(kvm_need_cancel_enter_guest(vcpu))) { > - exit_fastpath = vmx_exit_handlers_fastpath(vcpu); > - if (exit_fastpath == EXIT_FASTPATH_CONT_RUN) { > - vmx_sync_pir_to_irr(vcpu); > - goto cont_run; > - } > + exit_fastpath = vmx_exit_handlers_fastpath(vcpu); > + if (exit_fastpath == EXIT_FASTPATH_CONT_RUN) { Relying on the handlers to check kvm_need_cancel_enter_guest() will be error prone and costly to maintain. I also don't like that it buries the logic. What about adding another flavor, e.g.: exit_fastpath = vmx_exit_handlers_fastpath(vcpu); if (exit_fastpath == EXIT_FASTPATH_CONT_RUN && kvm_need_cancel_enter_guest(vcpu)) exit_fastpath = EXIT_FASTPATH_NOP; That would also allow you to enable preemption timer without first having to add CONT_RUN, which would be a very good thing for bisection. > + vmx_sync_pir_to_irr(vcpu); > + goto cont_run; > } > > return exit_fastpath; > diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c > index 99061ba..11b309c 100644 > --- a/arch/x86/kvm/x86.c > +++ b/arch/x86/kvm/x86.c > @@ -1618,6 +1618,9 @@ static int > handle_fastpath_set_x2apic_icr_irqoff(struct kvm_vcpu *vcpu, u64 data > > static int handle_fastpath_set_tscdeadline(struct kvm_vcpu *vcpu, u64 data) > { > + if (kvm_need_cancel_enter_guest(vcpu)) > + return 1; > + > if (!kvm_x86_ops.set_hv_timer || > kvm_mwait_in_guest(vcpu->kvm) || > kvm_can_post_timer_interrupt(vcpu))