Re: [PATCH 08/10] Move IO APIC to its own lock.

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Fri, Jul 17, 2009 at 12:19:58PM -0300, Marcelo Tosatti wrote:
> On Thu, Jul 16, 2009 at 11:49:35PM +0300, Gleb Natapov wrote:
> > On Thu, Jul 16, 2009 at 03:09:30PM -0300, Marcelo Tosatti wrote:
> > > On Thu, Jul 16, 2009 at 10:51:16AM +0300, Gleb Natapov wrote:
> > > > On Wed, Jul 15, 2009 at 06:38:48PM -0300, Marcelo Tosatti wrote:
> > > > > On Wed, Jul 15, 2009 at 11:48:17PM +0300, Gleb Natapov wrote:
> > > > > > > > +		spin_unlock(&ioapic->lock);
> > > > > > > > +		kvm_notify_acked_irq(ioapic->kvm, KVM_IRQCHIP_IOAPIC, i);
> > > > > > > > +		spin_lock(&ioapic->lock);
> > > > > > > 
> > > > > > > While traversing the IOAPIC pins you drop the lock and acquire it again,
> > > > > > > which is error prone: what if the redirection table is changed in
> > > > > > > between, for example?
> > > > > > > 
> > > > > > Yes, the ack notifiers is a problematic part. The only thing that
> > > > > > current ack notifiers do is set_irq_level(0) so this is not the problem
> > > > > > in practice. I'll see if I can eliminate this dropping of the lock here.
> > > > > 
> > > > > Currently, yes. But take into account that an ack notifier might do 
> > > > > other things than set_irq_level(0).
> > > > > 
> > > > For instance? Ack notifier can do whatever it wants if it does not call
> > > > back into ioapic. It may call to ioapic only by set_irq_level(0) (which
> > > > it does now from device assignment code) or by set_irq_level(1) and this
> > > > does not make sense for level triggered interrupts because not calling
> > > > set_irq_level(1) will have the same effect.
> > > 
> > > Checking whether a given gsi is masked on the
> > > PIC/IOAPIC from the PIC/IOAPIC mask notifiers
> > > (http://www.spinics.net/lists/kvm/msg19093.html), for example. 
> > > 
> > > Do you think that particular case should be handled in a different way?
> > I dislike ack notifiers and think that the should not be used if
> > possible. kvm_set_irq() return value provide you enough info to know it
> > interrupt was delivered or masked so the pit case can (and should) be
> > handled without using irq notifiers at all. See how RTC interrupt
> > coalescing is handled in qemu (it does not handle masking correctly, but
> > only because qemu community enjoys having their code broken).
> 
> There are a few issues that kvm_set_irq return code cannot handle,
> AFAICS (please correct me if i'm wrong, or provide useful points for
> discussion).
> 
> case 1)
> 1. kvm_set_irq, delivered OK (IOW, assume guest has acked the
> interrupt, next_timer_event += timer_period).
> 2. hlt -> kvm_vcpu_block thinks it can sleep, because next injectable
> timer event is the future.
> 3. which might not be the case, since we are not certain the guest 
> has acked the interrupt.
> 
> case 2)
> 1. kvm_set_irq, delivered OK.
> 2. guest masks irq.
> 3. here you do no want to unhalt the vcpu on the next timer event,
> because irq is masked at irqchip level.
> 
> So you need both ack notification and mask notification for proper
> emulation of UNHALT. Could check whether the interrupt is masked on
> injection and avoid UNHALT for case 2), though.
> 
> But to check that, you need to look into both PIC and IOAPIC pins, which 
> can happen like:
> 
> pic_lock
> check if pin is masked
> pic_unlock
> 
> ioapic_lock
> check if pin is masked
> ioapic_unlock
And what if interrupts are blocked by vcpu? You don't event know what
cpu will receive the interrupt (may be several?).

> 
> And only then, if interrupt can reach the processor, UNHALT the vcpu.
> This could avoid the need to check for PIC/IOAPIC state inside the PIT /
> other timers mask notifiers (regarding the current locking discussion).
> 
Timer is just a device that generates interrupt. You don't check in
a device emulation code if CPU is halted and should be unhalted and
timer code shouldn't do that either. The only check that decide if vcpu
should be unhalted is in kvm_arch_vcpu_runnable() and it goes like this:
  if there is interrupt to inject and interrupt are not masked by vcpu or
  there is nmi to inject then unhalt vcpu.
So what the timer code should do is to call kvm_set_irq(1) and if
interrupt (or nmi) will be delivered to vcpu as a result of this the
above check will unhalt vcpu.

If you want to decide in timer code what should be done to vcpu as a
result of a timer event you'll have to reimplement entire pic/ioapic/lapic
logic inside it. What if timer interrupt configured to send INIT to vcpu?

Both cases you described are problematic only because you are trying to
make interrupt code too smart.

> Those checks can race with another processor unmasking the pin for 
> the irqchips, but then its a guest problem.
Sane OSse serialize access to pic/ioapic. I think we can assume this in
our code.

> 
> You still need the mask notifier to be able to reset the reinjection
> logic on unmask (see 4780c65904f0fc4e312ee2da9383eacbe04e61ea). Is that 
> the case thats broken with RTC in QEMU?
> 
Yes, interrupt masking is broken with RTC in QEMU, but only because QEMU
wanted it like this. In KVM if kvm_set_irq() returns zero it means that
interrupt was masked so reinjection logic can be reset.

> Also, the fact that you receive an ack notification allows you to make 
> decisions of behaviour between injection and ack. You can also measure
> the delay between these two events, which is not used at the moment
> but is potentially useful information.
You can use ack notifier for this, but it doesn't require to call back
into pic/ioapic code.

> 
> You do not have that information on AEOI style interrupts (such as timer 
> delivery via PIT->LVT0 in NMI mode or PIC AEOI), but there's nothing 
> you can to help such cases.
> 
> > > (it could check whether a GSI is masked or not on both PIC/IOAPIC/LAPIC
> > > from a different context other than mask notifier).
> > > 
> > > > But I think I found the way to not drop the lock here.
> > > 
> > > See, this is adding more complexity than simplifying things (the
> > > recursive check).
> > This is not more complex than current code. (IMHO)
> 
> Maybe there is a better way to get rid of that recursion check, maybe 
> move the notifiers to be called after internal irqchip state has been
> updated, outside the lock?
> 
Impressible (IOW I don't see how it could be done). The ack notifier is
the one who drives line low, so it should be called before irr is
checked for pending interrupts. Basically the same scenario I described
for pic in my previous mail.

> > > > > Say for example an ack notifier that takes the PIC or IOAPIC lock 
> > > > > (for whatever reason).
> > > > What reason? No one should take PIC or IOAPIC lock outside of PIC or
> > > > IOAPIC code. This is layering violation. IOAPIC should be accessed only
> > > > through its public interface (set_irq/mmio_read|write/update_eoi)
> > > 
> > > See link above.
> > I can't see how the code from link above can work with current code
> > since mask notifiers are not called from PIC.
> 
> Then we're lacking mask notifier callback in i8259.c.
> > > > > 
> > > > > > > Also, irq_lock was held during ack and mask notifier callbacks, so they
> > > > > > > (the callbacks) did not have to worry about concurrency.
> > > > > > > 
> > > > > > Is it possible to have more then one ack for the same interrupt line at
> > > > > > the same time?
> > > > > 
> > > > > Why not? But maybe this is a somewhat stupid point, because you can
> > > > > require the notifiers to handle that.
> > > > If this is possible (and it looks like it may happen if IOAPIC broadcasts
> > > > an interrupt vector to more then one vcpu) then it may be better to push
> > > > complexity into an ack notifier to not penalize a common scenario.
> > > > But again, if we will not drop the lock around ack notifier call they
> > > > will be serialized.
> > > > 
> > > > > 
> > > > > > > You questioned the purpose of irq_lock earlier, and thats what it is:
> > > > > > > synchronization between pic and ioapic blur at the irq notifiers.
> > > > > > > 
> > > > > > Pic has its own locking and it fails miserably in regards ack notifiers.
> > > > > > I bet nobody tried device assignment with pic. It will not work.
> > > > > 
> > > > > It fails to take irq_lock on automatic EOI because on guest entry 
> > > > > interrupts are disabled (see d5ecfdd25c412df9c0ecf3ab8e066461fd4f69df).
> > > > This explains why the code is buggy, but does not fix the bug. There are
> > > > two bugs at least with the pic + ack notifiers. The first one is that
> > > > irq notifiers are called without irq_lock held and that will trigger
> > > > WARN_ON(!mutex_is_locked(&kvm->irq_lock)) in kvm_set_irq() in device
> > > > assignment case (besides what is the point of a lock if it is not taken
> > > > on every code path?).
> > > 
> > > This could be fixed by switching irq_lock to a spinlock.
> > Yes, but then you want to minimize the amount of code that runs under a
> > spinlock.
> > 
> > > 
> > > >  Another one is that you can't postpone call to ack notifiers in
> > > > kvm_pic_read_irq() till after pic_update_irq(s). The reason is that
> > > > pic_update_irq() will trigger acked interrupt immediately since ack
> > > > notifier is the one who lowers irq line in device assignment case
> > > > (that is the reason I haven't done the same in ioapic case BTW).
> > > 
> > > I'm not sure i understand this one, can you be more verbose please?
> > > 
> > 1. device injects level irq by calling kvm_pic_set_irq(1)
> > 2. level is recorded in irr (pic_set_irq1())
> > 3. pic_update_irq() called and interrupt is sent to vcpu
> > 4. vcpu calls kvm_pic_read_irq() which calls pic_update_irq(s) before calling 
> >    kvm_notify_acked_irq(). Ack callback is the one who calls kvm_pic_set_irq(0)
> >    to lower irq line, so when pic_update_irq(s) is called irr still
> >    indicates that irq level is 1 and it is delivered yet again.
> > 
> > But there is something strange with PIC + ack notifiers. PIC call ACK
> > notifiers not when guest acks the interrupt, but when KVM gets the
> > interrupt for delivery. Calling ack notifiers at this point is wrong.
> > Device assignment ack notifier enables host interrupts, but guest not yet
> > had a chance to clear interrupt condition in a device. Or do I miss
> > something here?
> 
> No, it looks wrong. The ack notification should be inside pic_clear_isr.
> 
So you agree with me that the current code is completely broken? The
"no" at the beginning confuses me :) Otherwise I agree with you the ack
notifiers should be inside pic_clear_isr().

> And it is not there already because kvm_notify_acked_irq/kvm_vcpu_kick
> could not be called under spinlocks.
Why kvm_notify_acked_irq() cannot be called under spinlocks?
kvm_vcpu_kick can be called under spinlocks now.

> 
> You still want the behaviour you described above for AEOI mode, but
> you should get that for free by moving kvm_notify_acked_irq/vcpu_kick
> inside pic_clear_isr.
> 
Agree.

> > > > > > irq_lock is indeed used to synchronization ioapic access, but the way
> > > > > > it is done requires calling kvm_set_irq() with lock held and this adds
> > > > > > unnecessary lock on a msi irq injection path.
> > > > > > 
> > > > > > > Using RCU to synchronize ack/mask notifier list walk versus list
> > > > > > > addition is good, but i'm not entirely sure that smaller locks like you
> > > > > > > are proposing is a benefit long term (things become much more tricky).
> > > > > > Without removing irq_lock RCU is useless since the list walking is always
> > > > > > done with irq_lock held. I see smaller locks as simplification BTW. The
> > > > > > thing is tricky now, or so I felt while trying to understand what
> > > > > > irq_lock actually protects. With smaller locks with names that clearly
> > > > > > indicates which data structure it protects I feel the code is much
> > > > > > easy to understand.
> > > > > 
> > > > > What is worrying is if you need to take both PIC and IOAPIC locks for 
> > > > > some operation (then have to worry about lock ordering etc).
> > > > > 
> > > > Lets not worry about far fetched theoretical cases especially since you
> > > > have the same problem now. 
> > > 
> > > Well the issue is you are defining what can be done in notifier context,
> > > so it does matter.
> > > 
> > Of course, just like now we have to define things that can be done in
> > notifier context. It can't sleep for instance since it may be called
> > with interrupts disabled.
> > 
> > > >  What if you'll need to take both pic lock and
> > > > irq_lock? Except that this is not so theoretical because we need to
> > > > take them both with current code we just don't do it.
> > > 
> > >     While most accesses to the i8259 are with the kvm mutex taken, the call
> > >     to kvm_pic_read_irq() is not.  We can't easily take the kvm mutex there
> > >     since the function is called with interrupts disabled.
> > > 
> > > Convert irq_lock to a spinlock.
> > Or apply my patchset :) Which basically converts irq_lock to spinlock
> > and pushes it into ioapic.
> 
> There's lots of issues here, better do one thing at a time. Please leave
> the RCU convertion to the end after getting rid of irq_lock mutex.
> 
This will cause intermediate commits that will have more locks in the
interrupt injection patch than we have now (one for irq routing, one for
notifiers and one for ioapic). May be not a big deal.

> > >     Fix by adding a spinlock to the virtual interrupt controller. Since
> > >     we can't send an IPI under the spinlock (we also take the same spinlock
> > >     in an irq disabled context), we defer the IPI until the spinlock is
> > >     released. Similarly, we defer irq ack notifications until after spinlock
> > >     release to avoid lock recursion.
> > > 
> > > Its possible to send IPI's under spinlocks now.
> > > 
> > That what I was thinking too. There is not need to all this tricks in
> > PIC now.
> > 
> > > Again, i'm not objecting to these changes, but playing devil's advocate.
> > > 
> > Just make comparison with current state of affairs fair :)
> 
> I think i'm convinced (just give some thought on the UNHALT discussion
> for timers).
> 
> > 
> > --
> > 			Gleb.

--
			Gleb.
--
To unsubscribe from this list: send the line "unsubscribe kvm" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html

[Index of Archives]     [KVM ARM]     [KVM ia64]     [KVM ppc]     [Virtualization Tools]     [Spice Development]     [Libvirt]     [Libvirt Users]     [Linux USB Devel]     [Linux Audio Users]     [Yosemite Questions]     [Linux Kernel]     [Linux SCSI]     [XFree86]
  Powered by Linux