From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1754505AbdCIMeO (ORCPT ); Thu, 9 Mar 2017 07:34:14 -0500 Received: from mx1.redhat.com ([209.132.183.28]:46542 "EHLO mx1.redhat.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752441AbdCIMeM (ORCPT ); Thu, 9 Mar 2017 07:34:12 -0500 Subject: Re: [PATCH] KVM: x86: optimize local IRQ disable time before vmentry To: Wanpeng Li , linux-kernel@vger.kernel.org, kvm@vger.kernel.org References: <1489061763-3832-1-git-send-email-wanpeng.li@hotmail.com> Cc: =?UTF-8?B?UmFkaW0gS3LEjW3DocWZ?= , Wanpeng Li From: Paolo Bonzini Message-ID: <7939e90b-4fe0-d07a-82b4-73cb0f4407fc@redhat.com> Date: Thu, 9 Mar 2017 13:33:26 +0100 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:45.0) Gecko/20100101 Thunderbird/45.7.0 MIME-Version: 1.0 In-Reply-To: <1489061763-3832-1-git-send-email-wanpeng.li@hotmail.com> Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.26]); Thu, 09 Mar 2017 12:33:29 +0000 (UTC) Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 09/03/2017 13:16, Wanpeng Li wrote: > From: Wanpeng Li > > Commit b95234c84 (kvm: x86: do not use KVM_REQ_EVENT for APICv interrupt > injection) disables interrupts before setting vcpu->mode to fix an race: > > | The IPI for posted interrupts may be issued between setting vcpu->mode = > | IN_GUEST_MODE and disabling interrupts. If that happens, > | kvm_trigger_posted_interrupt returns true, but smp_kvm_posted_intr_ipi > | doesn't do anything about it. The guest is entered with PIR.ON, but the > | posted interrupt IPI has not been sent and the interrupt is only delivered > | to the guest on the next vmentry (if any). > > However the race has already been fixed by the side-effects of moving the > RVI update after IN_GUEST_MODE in the commit: > > - The IPI for posted interrupts is issued after setting vcpu->mode = > IN_GUEST_MODE and disabling interrupts, the posted interrupt IPI is > delayed until the guest enters non-root mode; it is then trapped by > the processor causing the interrupt to be injected. > - The IPI for posted interrupts is issued between setting vcpu->mode = > IN_GUEST_MODE and disabling interrupts, the movement of the RVI update > after IN_GUEST_MODE in the commit will catch the new PIR, and set RVI. > > This patch tries to reduce the local IRQ disable time by restoring the > place of disable local IRQ in order to improve host kernel responsibility > to some degree. This should not make any difference; we're talking of a few dozen clock cycles, and it's quite possible that the extra chance of getting a PI interrupt in the host negates it. Paolo > Cc: Paolo Bonzini > Cc: Radim Krčmář > Signed-off-by: Wanpeng Li > --- > arch/x86/kvm/x86.c | 8 ++------ > 1 file changed, 2 insertions(+), 6 deletions(-) > > diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c > index 1faf620..9d97041 100644 > --- a/arch/x86/kvm/x86.c > +++ b/arch/x86/kvm/x86.c > @@ -6857,12 +6857,6 @@ static int vcpu_enter_guest(struct kvm_vcpu *vcpu) > kvm_x86_ops->prepare_guest_switch(vcpu); > kvm_load_guest_fpu(vcpu); > > - /* > - * Disable IRQs before setting IN_GUEST_MODE. Posted interrupt > - * IPI are then delayed after guest entry, which ensures that they > - * result in virtual interrupt delivery. > - */ > - local_irq_disable(); > vcpu->mode = IN_GUEST_MODE; > > srcu_read_unlock(&vcpu->kvm->srcu, vcpu->srcu_idx); > @@ -6881,6 +6875,8 @@ static int vcpu_enter_guest(struct kvm_vcpu *vcpu) > */ > smp_mb__after_srcu_read_unlock(); > > + local_irq_disable(); > + > /* > * This handles the case where a posted interrupt was > * notified with kvm_vcpu_kick. >