From patchwork Thu Jun 11 08:06:51 2009 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Gleb Natapov X-Patchwork-Id: 29493 Received: from vger.kernel.org (vger.kernel.org [209.132.176.167]) by demeter.kernel.org (8.14.2/8.14.2) with ESMTP id n5B8721O025337 for ; Thu, 11 Jun 2009 08:07:02 GMT Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1756897AbZFKIG4 (ORCPT ); Thu, 11 Jun 2009 04:06:56 -0400 Received: (majordomo@vger.kernel.org) by vger.kernel.org id S1755931AbZFKIGz (ORCPT ); Thu, 11 Jun 2009 04:06:55 -0400 Received: from mx2.redhat.com ([66.187.237.31]:58404 "EHLO mx2.redhat.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1755389AbZFKIGv (ORCPT ); Thu, 11 Jun 2009 04:06:51 -0400 Received: from int-mx2.corp.redhat.com (int-mx2.corp.redhat.com [172.16.27.26]) by mx2.redhat.com (8.13.8/8.13.8) with ESMTP id n5B86s1u003800 for ; Thu, 11 Jun 2009 04:06:54 -0400 Received: from ns3.rdu.redhat.com (ns3.rdu.redhat.com [10.11.255.199]) by int-mx2.corp.redhat.com (8.13.1/8.13.1) with ESMTP id n5B86rGV003372; Thu, 11 Jun 2009 04:06:53 -0400 Received: from dhcp-1-237.tlv.redhat.com (dhcp-1-237.tlv.redhat.com [10.35.1.237]) by ns3.rdu.redhat.com (8.13.8/8.13.8) with ESMTP id n5B86pw8000516; Thu, 11 Jun 2009 04:06:52 -0400 Received: by dhcp-1-237.tlv.redhat.com (Postfix, from userid 13519) id 9DB3E18D479; Thu, 11 Jun 2009 11:06:51 +0300 (IDT) Date: Thu, 11 Jun 2009 11:06:51 +0300 From: Gleb Natapov To: avi@redhat.com Cc: kvm@vger.kernel.org, mtosatti@redhat.com Subject: [PATCHv2] [APIC] Optimize searching for highest IRR Message-ID: <20090611080651.GP15949@redhat.com> MIME-Version: 1.0 Content-Disposition: inline X-Scanned-By: MIMEDefang 2.58 on 172.16.27.26 Sender: kvm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org Most of the time IRR is empty, so instead of scanning the whole IRR on each VM entry keep a variable that tells us if IRR is not empty. IRR will have to be scanned twice on each IRQ delivery, but this is much more rare than VM entry. v2: The only difference from v1 is the comment describing possible race and how it is solved. The race is not created by the patch BTW. Signed-off-by: Gleb Natapov --- Gleb. -- To unsubscribe from this list: send the line "unsubscribe kvm" in the body of a message to majordomo@vger.kernel.org More majordomo info at http://vger.kernel.org/majordomo-info.html diff --git a/arch/x86/kvm/lapic.c b/arch/x86/kvm/lapic.c index 44f20cd..38a7fa0 100644 --- a/arch/x86/kvm/lapic.c +++ b/arch/x86/kvm/lapic.c @@ -165,29 +165,45 @@ static int find_highest_vector(void *bitmap) static inline int apic_test_and_set_irr(int vec, struct kvm_lapic *apic) { + apic->irr_pending = true; return apic_test_and_set_vector(vec, apic->regs + APIC_IRR); } -static inline void apic_clear_irr(int vec, struct kvm_lapic *apic) +static inline int apic_search_irr(struct kvm_lapic *apic) { - apic_clear_vector(vec, apic->regs + APIC_IRR); + return find_highest_vector(apic->regs + APIC_IRR); } static inline int apic_find_highest_irr(struct kvm_lapic *apic) { int result; - result = find_highest_vector(apic->regs + APIC_IRR); + if (!apic->irr_pending) + return -1; + + result = apic_search_irr(apic); ASSERT(result == -1 || result >= 16); return result; } +static inline void apic_clear_irr(int vec, struct kvm_lapic *apic) +{ + apic->irr_pending = false; + apic_clear_vector(vec, apic->regs + APIC_IRR); + if (apic_search_irr(apic) != -1) + apic->irr_pending = true; +} + int kvm_lapic_find_highest_irr(struct kvm_vcpu *vcpu) { struct kvm_lapic *apic = vcpu->arch.apic; int highest_irr; + /* This may race with setting of irr in __apic_accept_irq() and + value returned may be wrong, but kvm_vcpu_kick() in __apic_accept_irq + will cause vmexit immediately and the value will be recalculated + on the next vmentry. */ if (!apic) return 0; highest_irr = apic_find_highest_irr(apic); @@ -842,6 +858,7 @@ void kvm_lapic_reset(struct kvm_vcpu *vcpu) apic_set_reg(apic, APIC_ISR + 0x10 * i, 0); apic_set_reg(apic, APIC_TMR + 0x10 * i, 0); } + apic->irr_pending = false; update_divide_count(apic); atomic_set(&apic->lapic_timer.pending, 0); if (vcpu->vcpu_id == 0) diff --git a/arch/x86/kvm/lapic.h b/arch/x86/kvm/lapic.h index a587f83..3f3ecc6 100644 --- a/arch/x86/kvm/lapic.h +++ b/arch/x86/kvm/lapic.h @@ -12,6 +12,7 @@ struct kvm_lapic { struct kvm_timer lapic_timer; u32 divide_count; struct kvm_vcpu *vcpu; + bool irr_pending; struct page *regs_page; void *regs; gpa_t vapic_addr;