Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1757385AbcJNHcp (ORCPT ); Fri, 14 Oct 2016 03:32:45 -0400 Received: from mail-oi0-f68.google.com ([209.85.218.68]:34127 "EHLO mail-oi0-f68.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1756864AbcJNHcm (ORCPT ); Fri, 14 Oct 2016 03:32:42 -0400 Subject: Re: [PATCH 3/3] KVM: x86: do not scan IRR twice on APICv vmentry To: Paolo Bonzini , linux-kernel@vger.kernel.org, kvm@vger.kernel.org References: <1475011213-34225-1-git-send-email-pbonzini@redhat.com> <1475011213-34225-4-git-send-email-pbonzini@redhat.com> Cc: feng.wu@intel.com, mst@redhat.com, rkrcmar@redhat.com From: Yang Zhang Message-ID: <30768946-c4f6-f9bd-5495-2a3adefffc69@gmail.com> Date: Fri, 14 Oct 2016 15:32:34 +0800 User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:45.0) Gecko/20100101 Thunderbird/45.4.0 MIME-Version: 1.0 In-Reply-To: <1475011213-34225-4-git-send-email-pbonzini@redhat.com> Content-Type: text/plain; charset=windows-1252; format=flowed Content-Transfer-Encoding: 7bit Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Length: 7497 Lines: 213 On 2016/9/28 5:20, Paolo Bonzini wrote: > Calling apic_find_highest_irr results in IRR being scanned twice, > once in vmx_sync_pir_from_irr and once in apic_search_irr. Change > vcpu_enter_guest to use sync_pir_from_irr (with a new argument to > trigger the RVI write), and let sync_pir_from_irr get the new RVI > directly from kvm_apic_update_irr. > > Signed-off-by: Paolo Bonzini > --- > arch/x86/include/asm/kvm_host.h | 2 +- > arch/x86/kvm/lapic.c | 25 ++++++++++++++++--------- > arch/x86/kvm/lapic.h | 4 ++-- > arch/x86/kvm/svm.c | 2 +- > arch/x86/kvm/vmx.c | 25 ++++++++++++++----------- > arch/x86/kvm/x86.c | 7 +++---- > 6 files changed, 37 insertions(+), 28 deletions(-) > > diff --git a/arch/x86/include/asm/kvm_host.h b/arch/x86/include/asm/kvm_host.h > index 4b20f7304b9c..f73eea243567 100644 > --- a/arch/x86/include/asm/kvm_host.h > +++ b/arch/x86/include/asm/kvm_host.h > @@ -941,7 +941,7 @@ struct kvm_x86_ops { > void (*set_virtual_x2apic_mode)(struct kvm_vcpu *vcpu, bool set); > void (*set_apic_access_page_addr)(struct kvm_vcpu *vcpu, hpa_t hpa); > void (*deliver_posted_interrupt)(struct kvm_vcpu *vcpu, int vector); > - void (*sync_pir_to_irr)(struct kvm_vcpu *vcpu); > + void (*sync_pir_to_irr)(struct kvm_vcpu *vcpu, bool sync_rvi); > int (*set_tss_addr)(struct kvm *kvm, unsigned int addr); > int (*get_tdp_level)(void); > u64 (*get_mt_mask)(struct kvm_vcpu *vcpu, gfn_t gfn, bool is_mmio); > diff --git a/arch/x86/kvm/lapic.c b/arch/x86/kvm/lapic.c > index be8b7ad56dd1..85b79b90e3d0 100644 > --- a/arch/x86/kvm/lapic.c > +++ b/arch/x86/kvm/lapic.c > @@ -317,7 +317,7 @@ static int find_highest_vector(void *bitmap) > vec >= 0; vec -= APIC_VECTORS_PER_REG) { > reg = bitmap + REG_POS(vec); > if (*reg) > - return fls(*reg) - 1 + vec; > + return __fls(*reg) + vec; > } > > return -1; > @@ -337,25 +337,32 @@ static u8 count_vectors(void *bitmap) > return count; > } > > -void __kvm_apic_update_irr(u32 *pir, void *regs) > +int __kvm_apic_update_irr(u32 *pir, void *regs) > { > - u32 i, pir_val; > + u32 i, vec; > + u32 pir_val, irr_val; > + int max_irr = -1; > > - for (i = 0; i <= 7; i++) { > + for (i = vec = 0; i <= 7; i++, vec += 32) { how about ignore the first 32 vectors since they cannot be used as normal interrupt except nmi interrupt which is special handled. > pir_val = READ_ONCE(pir[i]); > + irr_val = *((u32 *)(regs + APIC_IRR + i * 0x10)); > if (pir_val) { > - pir_val = xchg(&pir[i], 0); > - *((u32 *)(regs + APIC_IRR + i * 0x10)) |= pir_val; > + irr_val |= xchg(&pir[i], 0); > + *((u32 *)(regs + APIC_IRR + i * 0x10)) = irr_val; > } > + if (irr_val) > + max_irr = __fls(irr_val) + vec; > } > + > + return max_irr; > } > EXPORT_SYMBOL_GPL(__kvm_apic_update_irr); > > -void kvm_apic_update_irr(struct kvm_vcpu *vcpu, u32 *pir) > +int kvm_apic_update_irr(struct kvm_vcpu *vcpu, u32 *pir) > { > struct kvm_lapic *apic = vcpu->arch.apic; > > - __kvm_apic_update_irr(pir, apic->regs); > + return __kvm_apic_update_irr(pir, apic->regs); > } > EXPORT_SYMBOL_GPL(kvm_apic_update_irr); > > @@ -376,7 +383,7 @@ static inline int apic_find_highest_irr(struct kvm_lapic *apic) > return -1; > > if (apic->vcpu->arch.apicv_active) > - kvm_x86_ops->sync_pir_to_irr(apic->vcpu); > + kvm_x86_ops->sync_pir_to_irr(apic->vcpu, false); > result = apic_search_irr(apic); You have got the max_irr in sync_pir_to_irr. It seems the apic_search_irr() is redundant here. > ASSERT(result == -1 || result >= 16); I think the result must >= 32. But it seems this code exists when kvm first merging into kernel. > > diff --git a/arch/x86/kvm/lapic.h b/arch/x86/kvm/lapic.h > index f60d01c29d51..fc72828c3782 100644 > --- a/arch/x86/kvm/lapic.h > +++ b/arch/x86/kvm/lapic.h > @@ -70,8 +70,8 @@ int kvm_lapic_reg_read(struct kvm_lapic *apic, u32 offset, int len, > bool kvm_apic_match_dest(struct kvm_vcpu *vcpu, struct kvm_lapic *source, > int short_hand, unsigned int dest, int dest_mode); > > -void __kvm_apic_update_irr(u32 *pir, void *regs); > -void kvm_apic_update_irr(struct kvm_vcpu *vcpu, u32 *pir); > +int __kvm_apic_update_irr(u32 *pir, void *regs); > +int kvm_apic_update_irr(struct kvm_vcpu *vcpu, u32 *pir); > int kvm_apic_set_irq(struct kvm_vcpu *vcpu, struct kvm_lapic_irq *irq, > struct dest_map *dest_map); > int kvm_apic_local_deliver(struct kvm_lapic *apic, int lvt_type); > diff --git a/arch/x86/kvm/svm.c b/arch/x86/kvm/svm.c > index 9b4221471e3d..60e53fa2b554 100644 > --- a/arch/x86/kvm/svm.c > +++ b/arch/x86/kvm/svm.c > @@ -4383,7 +4383,7 @@ static void svm_load_eoi_exitmap(struct kvm_vcpu *vcpu, u64 *eoi_exit_bitmap) > return; > } > > -static void svm_sync_pir_to_irr(struct kvm_vcpu *vcpu) > +static void svm_sync_pir_to_irr(struct kvm_vcpu *vcpu, bool sync_rvi) > { > return; > } > diff --git a/arch/x86/kvm/vmx.c b/arch/x86/kvm/vmx.c > index 207b9aa32915..1edefab54d01 100644 > --- a/arch/x86/kvm/vmx.c > +++ b/arch/x86/kvm/vmx.c > @@ -4852,17 +4852,6 @@ static void vmx_deliver_posted_interrupt(struct kvm_vcpu *vcpu, int vector) > kvm_vcpu_kick(vcpu); > } > > -static void vmx_sync_pir_to_irr(struct kvm_vcpu *vcpu) > -{ > - struct vcpu_vmx *vmx = to_vmx(vcpu); > - > - if (!pi_test_on(&vmx->pi_desc) || > - !pi_test_and_clear_on(&vmx->pi_desc)) > - return; > - > - kvm_apic_update_irr(vcpu, vmx->pi_desc.pir); > -} > - > /* > * Set up the vmcs's constant host-state fields, i.e., host-state fields that > * will not change in the lifetime of the guest. > @@ -8609,6 +8598,20 @@ static void vmx_hwapic_irr_update(struct kvm_vcpu *vcpu, int max_irr) > } > } > > +static void vmx_sync_pir_to_irr(struct kvm_vcpu *vcpu, bool sync_rvi) > +{ > + struct vcpu_vmx *vmx = to_vmx(vcpu); > + int max_irr; > + > + if (!pi_test_on(&vmx->pi_desc) || > + !pi_test_and_clear_on(&vmx->pi_desc)) > + return; > + > + max_irr = kvm_apic_update_irr(vcpu, vmx->pi_desc.pir); > + if (sync_rvi) > + vmx_hwapic_irr_update(vcpu, max_irr); > +} > + > static void vmx_load_eoi_exitmap(struct kvm_vcpu *vcpu, u64 *eoi_exit_bitmap) > { > if (!kvm_vcpu_apicv_active(vcpu)) > diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c > index 604cfbfc5bee..148e14fdc55d 100644 > --- a/arch/x86/kvm/x86.c > +++ b/arch/x86/kvm/x86.c > @@ -2821,7 +2821,7 @@ static int kvm_vcpu_ioctl_get_lapic(struct kvm_vcpu *vcpu, > struct kvm_lapic_state *s) > { > if (vcpu->arch.apicv_active) > - kvm_x86_ops->sync_pir_to_irr(vcpu); > + kvm_x86_ops->sync_pir_to_irr(vcpu, false); > > return kvm_apic_get_state(vcpu, s); > } > @@ -6448,7 +6448,7 @@ static void vcpu_scan_ioapic(struct kvm_vcpu *vcpu) > kvm_scan_ioapic_routes(vcpu, vcpu->arch.ioapic_handled_vectors); > else { > if (vcpu->arch.apicv_active) > - kvm_x86_ops->sync_pir_to_irr(vcpu); > + kvm_x86_ops->sync_pir_to_irr(vcpu, false); > kvm_ioapic_scan_entry(vcpu, vcpu->arch.ioapic_handled_vectors); > } > bitmap_or((ulong *)eoi_exit_bitmap, vcpu->arch.ioapic_handled_vectors, > @@ -6611,8 +6611,7 @@ static int vcpu_enter_guest(struct kvm_vcpu *vcpu) > * virtual interrupt delivery. > */ > if (vcpu->arch.apicv_active) > - kvm_x86_ops->hwapic_irr_update(vcpu, > - kvm_lapic_find_highest_irr(vcpu)); > + kvm_x86_ops->sync_pir_to_irr(vcpu, true); > } > > if (kvm_check_request(KVM_REQ_EVENT, vcpu) || req_int_win) { > -- Yang Alibaba Cloud Computing