From: Marcelo Tosatti <mtosatti@redhat.com>
To: "Michael S. Tsirkin" <mst@redhat.com>
Cc: x86@kernel.org, kvm@vger.kernel.org,
Ingo Molnar <mingo@redhat.com>, "H. Peter Anvin" <hpa@zytor.com>,
Avi Kivity <avi@redhat.com>,
gleb@redhat.com, Linus Torvalds <torvalds@linux-foundation.org>,
linux-kernel@vger.kernel.org,
Thomas Gleixner <tglx@linutronix.de>
Subject: Re: [PATCHv6 2/8] kvm: optimize ISR lookups
Date: Wed, 13 Jun 2012 17:21:02 -0300 [thread overview]
Message-ID: <20120613202102.GA19290@amt.cnet> (raw)
In-Reply-To: <20120613090242.GA17357@redhat.com>
On Wed, Jun 13, 2012 at 12:02:43PM +0300, Michael S. Tsirkin wrote:
> On Tue, Jun 12, 2012 at 06:08:33PM -0300, Marcelo Tosatti wrote:
> > On Sun, Jun 03, 2012 at 10:27:59AM +0300, Michael S. Tsirkin wrote:
> > > We perform ISR lookups twice: during interrupt
> > > injection and on EOI. Typical workloads only have
> > > a single bit set there. So we can avoid ISR scans by
> > > 1. counting bits as we set/clear them in ISR
> > > 2. on set, caching the injected vector number
> > > 3. on clear, invalidating the cache
> > >
> > > The real purpose of this is enabling PV EOI
> > > which needs to quickly validate the vector.
> > > But non PV guests also benefit: with this patch,
> > > and without interrupt nesting, apic_find_highest_isr
> > > will always return immediately without scanning ISR.
> > >
> > > Signed-off-by: Michael S. Tsirkin <mst@redhat.com>
> > > ---
> > >
> > > This revision is slightly reworked from the last version
> > > I sent: I don't invalidate the cache when there is more
> > > than one bit set in ISR. This makes the code simpler,
> > > makes cache valid in more cases and avoids a branch on data path.
> > >
> > > Otherwise this is basically the same as v2: this revision does not
> > > yet address Thomas's idea of reworking the APIC page handling.
> > >
> > > arch/x86/kvm/lapic.c | 48 ++++++++++++++++++++++++++++++++++++++++++++++--
> > > arch/x86/kvm/lapic.h | 4 ++++
> > > 2 files changed, 50 insertions(+), 2 deletions(-)
> > >
> > > diff --git a/arch/x86/kvm/lapic.c b/arch/x86/kvm/lapic.c
> > > index 93c1574..db54e63 100644
> > > --- a/arch/x86/kvm/lapic.c
> > > +++ b/arch/x86/kvm/lapic.c
> > > @@ -107,6 +107,16 @@ static inline void apic_clear_vector(int vec, void *bitmap)
> > > clear_bit(VEC_POS(vec), (bitmap) + REG_POS(vec));
> > > }
> > >
> > > +static inline int __apic_test_and_set_vector(int vec, void *bitmap)
> > > +{
> > > + return __test_and_set_bit(VEC_POS(vec), (bitmap) + REG_POS(vec));
> > > +}
> > > +
> > > +static inline int __apic_test_and_clear_vector(int vec, void *bitmap)
> > > +{
> > > + return __test_and_clear_bit(VEC_POS(vec), (bitmap) + REG_POS(vec));
> > > +}
> > > +
> > > static inline int apic_hw_enabled(struct kvm_lapic *apic)
> > > {
> > > return (apic)->vcpu->arch.apic_base & MSR_IA32_APICBASE_ENABLE;
> > > @@ -210,6 +220,16 @@ static int find_highest_vector(void *bitmap)
> > > return fls(word[word_offset << 2]) - 1 + (word_offset << 5);
> > > }
> > >
> > > +static u8 count_vectors(void *bitmap)
> > > +{
> > > + u32 *word = bitmap;
> > > + int word_offset;
> > > + u8 count = 0;
> > > + for (word_offset = 0; word_offset < MAX_APIC_VECTOR >> 5; ++word_offset)
> > > + count += hweight32(word[word_offset << 2]);
> > > + return count;
> > > +}
> > > +
> > > static inline int apic_test_and_set_irr(int vec, struct kvm_lapic *apic)
> > > {
> > > apic->irr_pending = true;
> > > @@ -242,6 +262,22 @@ static inline void apic_clear_irr(int vec, struct kvm_lapic *apic)
> > > apic->irr_pending = true;
> > > }
> > >
> > > +static inline void apic_set_isr(int vec, struct kvm_lapic *apic)
> > > +{
> > > + if (!__apic_test_and_set_vector(vec, apic->regs + APIC_ISR))
> > > + ++apic->isr_count;
> > > + BUG_ON(apic->isr_count > MAX_APIC_VECTOR);
> > > + apic->isr_cache = vec;
> > > +}
> > > +
> > > +static inline void apic_clear_isr(int vec, struct kvm_lapic *apic)
> > > +{
> > > + if (__apic_test_and_clear_vector(vec, apic->regs + APIC_ISR))
> > > + --apic->isr_count;
> > > + BUG_ON(apic->isr_count < 0);
> > > + apic->isr_cache = -1;
> > > +}
> > > +
> > > int kvm_lapic_find_highest_irr(struct kvm_vcpu *vcpu)
> > > {
> > > struct kvm_lapic *apic = vcpu->arch.apic;
> > > @@ -273,6 +309,10 @@ int kvm_apic_set_irq(struct kvm_vcpu *vcpu, struct kvm_lapic_irq *irq)
> > > static inline int apic_find_highest_isr(struct kvm_lapic *apic)
> > > {
> > > int result;
> > > + if (!apic->isr_count)
> > > + return -1;
> > > + if (likely(apic->isr_cache != -1))
> > > + return apic->isr_cache;
> >
> > What about TPR updates that modify highest ISR available? They are not
> > fully covered. Ex: writes to TPR register not via CR8.
>
> Sorry, I don't yet understand what you are saying.
>
> Why would TPR/PPR updates need to affect any of this:
> TPR affects the injection of interrupts but if we don't inject
> and interrupt we don't set ISR, right?
They don't, ignore that comment.
> Maybe I misunderstand, I'm going to look more at the spec, but maybe
> you could point out an example where this logic is wrong?
>
> >
> > apic_update_ppr should reset cache?
>
> I think this would kill the optimization as we call this all the time.
> With this patch applied ISR was searched in less than 1% of the cases
> in my testing, it almost always was either a cache hit or count == 0.
>
> > Instead of isr_cache what about a highest_isr field?
>
> >
> > When setting ISR:
> >
> > if (apic->highest_isr < me)
> > apic->highest_isr = me;
> >
> > To be invalidated on TPR updates properly.
> >
> > Its more meaningful.
>
> OK, I'll rename it highest_isr but we do not need the if:
>
> IIUC, ISR (in service register) bit is only set when we inject an
> interrupt. So the latest bit we set tells us exactly which isr is the
> highest: it would not be set if it was not the highest. We are simply
> caching this last bit which to me looks cleaner than adding logic here:
> if we did we'd also need to special-case the invalid value, so it
> becomes tricky.
>
> I'll add a comment here that explains the above.
>
> As I said I don't yet understand why we need to invalidate on TPR updates.
next prev parent reply other threads:[~2012-06-13 20:21 UTC|newest]
Thread overview: 23+ messages / expand[flat|nested] mbox.gz Atom feed top
2012-06-03 7:27 [PATCHv6 0/8] kvm: eoi optimization support Michael S. Tsirkin
2012-06-03 7:27 ` [PATCHv6 1/8] kvm: document lapic regs field Michael S. Tsirkin
2012-06-03 7:27 ` [PATCHv6 2/8] kvm: optimize ISR lookups Michael S. Tsirkin
2012-06-12 21:08 ` Marcelo Tosatti
2012-06-13 9:02 ` Michael S. Tsirkin
2012-06-13 9:26 ` Michael S. Tsirkin
2012-06-13 20:21 ` Marcelo Tosatti [this message]
2012-06-03 7:28 ` [PATCHv6 3/8] kvm_para: guest side for eoi avoidance Michael S. Tsirkin
2012-06-03 7:28 ` [PATCHv6 4/8] x86/bitops: note on __test_and_clear_bit atomicity Michael S. Tsirkin
2012-06-03 7:28 ` [PATCHv6 5/8] kvm: eoi msi documentation Michael S. Tsirkin
2012-06-12 22:24 ` Marcelo Tosatti
2012-06-03 7:28 ` [PATCHv6 6/8] kvm: only sync when attention bits set Michael S. Tsirkin
2012-06-12 22:27 ` Marcelo Tosatti
2012-06-13 8:19 ` Michael S. Tsirkin
2012-06-13 8:35 ` Michael S. Tsirkin
2012-06-13 20:53 ` Marcelo Tosatti
2012-06-13 21:04 ` Michael S. Tsirkin
2012-06-13 23:38 ` Marcelo Tosatti
2012-06-14 8:04 ` Michael S. Tsirkin
2012-06-03 7:28 ` [PATCHv6 7/8] kvm: rearrange injection cancelling code Michael S. Tsirkin
2012-06-03 7:28 ` [PATCHv6 8/8] kvm: host side for eoi optimization Michael S. Tsirkin
2012-06-13 21:10 ` Marcelo Tosatti
2012-06-14 8:01 ` Michael S. Tsirkin
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20120613202102.GA19290@amt.cnet \
--to=mtosatti@redhat.com \
--cc=avi@redhat.com \
--cc=gleb@redhat.com \
--cc=hpa@zytor.com \
--cc=kvm@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=mingo@redhat.com \
--cc=mst@redhat.com \
--cc=tglx@linutronix.de \
--cc=torvalds@linux-foundation.org \
--cc=x86@kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).