From: "Jan Beulich" Subject: [PATCH] x86/HPET: deal with event having expired while interrupt was masked Date: Wed, 20 Mar 2013 14:45:04 +0000 Message-ID: <5149D98002000078000C736E@nat28.tlf.novell.com> Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="=__PartDBEBCE60.1__=" Return-path: List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Sender: xen-devel-bounces@lists.xen.org Errors-To: xen-devel-bounces@lists.xen.org To: xen-devel Cc: Sander Eikelenboom , Keir Fraser List-Id: xen-devel@lists.xenproject.org This is a MIME message. If you are reading this text, you may want to consider changing to a mail reader or gateway that understands how to properly handle MIME multipart messages. --=__PartDBEBCE60.1__= Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: quoted-printable Content-Disposition: inline Commit 2d8a282 ("x86/HPET: fix FSB interrupt masking") may cause the HPET event to occur while its interrupt is masked. In that case we need to "manually" deliver the event. Unfortunately this requires the locking to be changed: For one, it was always bogus for handle_hpet_broadcast() to use spin_unlock_irq() - the function is being called from an interrupt handler, and hence shouldn't blindly re-enable interrupts (this should be left up to the generic interrupt handling code). And with the event handler wanting to acquire the lock for two of its code regions, we must not enter it with the lock already held. Hence move the locking into hpet_{attach,detach}_channel(), permitting the lock to be dropped by set_channel_irq_affinity() (which is a tail call of those functions). Reported-by: Sander Eikelenboom Signed-off-by: Jan Beulich Tested-by: Sander Eikelenboom --- a/xen/arch/x86/hpet.c +++ b/xen/arch/x86/hpet.c @@ -171,13 +171,14 @@ static void handle_hpet_broadcast(struct cpumask_t mask; s_time_t now, next_event; unsigned int cpu; + unsigned long flags; =20 - spin_lock_irq(&ch->lock); + spin_lock_irqsave(&ch->lock, flags); =20 again: ch->next_event =3D STIME_MAX; =20 - spin_unlock_irq(&ch->lock); + spin_unlock_irqrestore(&ch->lock, flags); =20 next_event =3D STIME_MAX; cpumask_clear(&mask); @@ -205,13 +206,13 @@ again: =20 if ( next_event !=3D STIME_MAX ) { - spin_lock_irq(&ch->lock); + spin_lock_irqsave(&ch->lock, flags); =20 if ( next_event < ch->next_event && reprogram_hpet_evt_channel(ch, next_event, now, 0) ) goto again; =20 - spin_unlock_irq(&ch->lock); + spin_unlock_irqrestore(&ch->lock, flags); } } =20 @@ -460,7 +461,7 @@ static struct hpet_event_channel *hpet_g return ch; } =20 -static void set_channel_irq_affinity(const struct hpet_event_channel *ch) +static void set_channel_irq_affinity(struct hpet_event_channel *ch) { struct irq_desc *desc =3D irq_to_desc(ch->msi.irq); =20 @@ -470,12 +471,19 @@ static void set_channel_irq_affinity(con hpet_msi_set_affinity(desc, cpumask_of(ch->cpu)); hpet_msi_unmask(desc); spin_unlock(&desc->lock); + + spin_unlock(&ch->lock); + + /* We may have missed an interrupt due to the temporary masking. */ + if ( ch->event_handler && ch->next_event < NOW() ) + ch->event_handler(ch); } =20 static void hpet_attach_channel(unsigned int cpu, struct hpet_event_channel *ch) { - ASSERT(spin_is_locked(&ch->lock)); + ASSERT(!local_irq_is_enabled()); + spin_lock(&ch->lock); =20 per_cpu(cpu_bc_channel, cpu) =3D ch; =20 @@ -484,31 +492,34 @@ static void hpet_attach_channel(unsigned ch->cpu =3D cpu; =20 if ( ch->cpu !=3D cpu ) - return; - - set_channel_irq_affinity(ch); + spin_unlock(&ch->lock); + else + set_channel_irq_affinity(ch); } =20 static void hpet_detach_channel(unsigned int cpu, struct hpet_event_channel *ch) { - ASSERT(spin_is_locked(&ch->lock)); + spin_lock_irq(&ch->lock); + ASSERT(ch =3D=3D per_cpu(cpu_bc_channel, cpu)); =20 per_cpu(cpu_bc_channel, cpu) =3D NULL; =20 if ( cpu !=3D ch->cpu ) - return; - - if ( cpumask_empty(ch->cpumask) ) + spin_unlock_irq(&ch->lock); + else if ( cpumask_empty(ch->cpumask) ) { ch->cpu =3D -1; clear_bit(HPET_EVT_USED_BIT, &ch->flags); - return; + spin_unlock_irq(&ch->lock); + } + else + { + ch->cpu =3D cpumask_first(ch->cpumask); + set_channel_irq_affinity(ch); + local_irq_enable(); } - - ch->cpu =3D cpumask_first(ch->cpumask); - set_channel_irq_affinity(ch); } =20 #include @@ -686,11 +697,7 @@ void hpet_broadcast_enter(void) ASSERT(!local_irq_is_enabled()); =20 if ( !(ch->flags & HPET_EVT_LEGACY) ) - { - spin_lock(&ch->lock); hpet_attach_channel(cpu, ch); - spin_unlock(&ch->lock); - } =20 /* Disable LAPIC timer interrupts. */ disable_APIC_timer(); @@ -722,11 +729,7 @@ void hpet_broadcast_exit(void) cpumask_clear_cpu(cpu, ch->cpumask); =20 if ( !(ch->flags & HPET_EVT_LEGACY) ) - { - spin_lock_irq(&ch->lock); hpet_detach_channel(cpu, ch); - spin_unlock_irq(&ch->lock); - } } =20 int hpet_broadcast_is_available(void) --=__PartDBEBCE60.1__= Content-Type: text/plain; name="x86-HPET-affinity-masked-deliver.patch" Content-Transfer-Encoding: quoted-printable Content-Disposition: attachment; filename="x86-HPET-affinity-masked-deliver.patch" x86/HPET: deal with event having expired while interrupt was masked=0A=0ACo= mmit 2d8a282 ("x86/HPET: fix FSB interrupt masking") may cause the=0AHPET = event to occur while its interrupt is masked. In that case we need=0Ato = "manually" deliver the event.=0A=0AUnfortunately this requires the locking = to be changed: For one, it was=0Aalways bogus for handle_hpet_broadcast() = to use spin_unlock_irq() - the=0Afunction is being called from an = interrupt handler, and hence shouldn't=0Ablindly re-enable interrupts = (this should be left up to the generic=0Ainterrupt handling code). And = with the event handler wanting to acquire=0Athe lock for two of its code = regions, we must not enter it with the=0Alock already held. Hence move the = locking into=0Ahpet_{attach,detach}_channel(), permitting the lock to be = dropped by=0Aset_channel_irq_affinity() (which is a tail call of those = functions).=0A=0AReported-by: Sander Eikelenboom =0AS= igned-off-by: Jan Beulich =0ATested-by: Sander = Eikelenboom =0A=0A--- a/xen/arch/x86/hpet.c=0A+++ = b/xen/arch/x86/hpet.c=0A@@ -171,13 +171,14 @@ static void handle_hpet_broad= cast(struct=0A cpumask_t mask;=0A s_time_t now, next_event;=0A = unsigned int cpu;=0A+ unsigned long flags;=0A =0A- spin_lock_irq(&ch-= >lock);=0A+ spin_lock_irqsave(&ch->lock, flags);=0A =0A again:=0A = ch->next_event =3D STIME_MAX;=0A =0A- spin_unlock_irq(&ch->lock);=0A+ = spin_unlock_irqrestore(&ch->lock, flags);=0A =0A next_event =3D = STIME_MAX;=0A cpumask_clear(&mask);=0A@@ -205,13 +206,13 @@ again:=0A = =0A if ( next_event !=3D STIME_MAX )=0A {=0A- spin_lock_irq(= &ch->lock);=0A+ spin_lock_irqsave(&ch->lock, flags);=0A =0A = if ( next_event < ch->next_event &&=0A reprogram_hpet_evt_chan= nel(ch, next_event, now, 0) )=0A goto again;=0A =0A- = spin_unlock_irq(&ch->lock);=0A+ spin_unlock_irqrestore(&ch->lock, = flags);=0A }=0A }=0A =0A@@ -460,7 +461,7 @@ static struct hpet_event_ch= annel *hpet_g=0A return ch;=0A }=0A =0A-static void set_channel_irq_aff= inity(const struct hpet_event_channel *ch)=0A+static void set_channel_irq_a= ffinity(struct hpet_event_channel *ch)=0A {=0A struct irq_desc *desc = =3D irq_to_desc(ch->msi.irq);=0A =0A@@ -470,12 +471,19 @@ static void = set_channel_irq_affinity(con=0A hpet_msi_set_affinity(desc, cpumask_of(= ch->cpu));=0A hpet_msi_unmask(desc);=0A spin_unlock(&desc->lock);= =0A+=0A+ spin_unlock(&ch->lock);=0A+=0A+ /* We may have missed an = interrupt due to the temporary masking. */=0A+ if ( ch->event_handler = && ch->next_event < NOW() )=0A+ ch->event_handler(ch);=0A }=0A =0A = static void hpet_attach_channel(unsigned int cpu,=0A = struct hpet_event_channel *ch)=0A {=0A- ASSERT(spin_is_locked(= &ch->lock));=0A+ ASSERT(!local_irq_is_enabled());=0A+ spin_lock(&ch->= lock);=0A =0A per_cpu(cpu_bc_channel, cpu) =3D ch;=0A =0A@@ -484,31 = +492,34 @@ static void hpet_attach_channel(unsigned=0A ch->cpu =3D = cpu;=0A =0A if ( ch->cpu !=3D cpu )=0A- return;=0A-=0A- = set_channel_irq_affinity(ch);=0A+ spin_unlock(&ch->lock);=0A+ = else=0A+ set_channel_irq_affinity(ch);=0A }=0A =0A static void = hpet_detach_channel(unsigned int cpu,=0A = struct hpet_event_channel *ch)=0A {=0A- ASSERT(spin_is_locked(&ch->lock)= );=0A+ spin_lock_irq(&ch->lock);=0A+=0A ASSERT(ch =3D=3D per_cpu(cpu= _bc_channel, cpu));=0A =0A per_cpu(cpu_bc_channel, cpu) =3D NULL;=0A = =0A if ( cpu !=3D ch->cpu )=0A- return;=0A-=0A- if ( = cpumask_empty(ch->cpumask) )=0A+ spin_unlock_irq(&ch->lock);=0A+ = else if ( cpumask_empty(ch->cpumask) )=0A {=0A ch->cpu =3D = -1;=0A clear_bit(HPET_EVT_USED_BIT, &ch->flags);=0A- = return;=0A+ spin_unlock_irq(&ch->lock);=0A+ }=0A+ else=0A+ = {=0A+ ch->cpu =3D cpumask_first(ch->cpumask);=0A+ set_channel= _irq_affinity(ch);=0A+ local_irq_enable();=0A }=0A-=0A- = ch->cpu =3D cpumask_first(ch->cpumask);=0A- set_channel_irq_affinity(ch)= ;=0A }=0A =0A #include =0A@@ -686,11 +697,7 @@ void = hpet_broadcast_enter(void)=0A ASSERT(!local_irq_is_enabled());=0A =0A = if ( !(ch->flags & HPET_EVT_LEGACY) )=0A- {=0A- spin_lock(&ch-= >lock);=0A hpet_attach_channel(cpu, ch);=0A- spin_unlock(&ch= ->lock);=0A- }=0A =0A /* Disable LAPIC timer interrupts. */=0A = disable_APIC_timer();=0A@@ -722,11 +729,7 @@ void hpet_broadcast_exit(void)= =0A cpumask_clear_cpu(cpu, ch->cpumask);=0A =0A if ( !(ch->flags & = HPET_EVT_LEGACY) )=0A- {=0A- spin_lock_irq(&ch->lock);=0A = hpet_detach_channel(cpu, ch);=0A- spin_unlock_irq(&ch->lock);=0A- = }=0A }=0A =0A int hpet_broadcast_is_available(void)=0A --=__PartDBEBCE60.1__= Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Disposition: inline _______________________________________________ Xen-devel mailing list Xen-devel@lists.xen.org http://lists.xen.org/xen-devel --=__PartDBEBCE60.1__=--