Skip to contentSkip to conversation

x86/vpt: fix a bug in pt_update_irq()

PATCH In review Chao Gao 2 participants 4 messages Oct 9, 2017 – Oct 13, 2017

lore
Share & tools
lore

Chao Gao

author
#0 lore raw

PATCH 1/1 x86/vpt: fix a bug in pt_update_irq()

pt_update_irq() is expected to return the vector number of periodic timer interrupt, which should be set in vIRR of vlapic. Otherwise it would trigger the assertion in vmx_intr_assist(), please seeing
https://lists.xenproject.org/archives/html/xen-devel/2017-10/msg00915.html.

But it fails to achieve that in the following two case:
1. hvm_isa_irq_assert() may not set the corresponding bit in vIRR for mask field of IOAPIC RTE is set. Please refer to the call tree vmx_intr_assist() -> pt_update_irq() -> hvm_isa_irq_assert() -> assert_irq() -> assert_gsi() -> vioapic_irq_positive_edge(). The patch checks whether the vector is set or not in vIRR of vlapic before returning.

2. someone changes the vector field of IOAPIC RTE between asserting the irq and getting the vector of the irq, leading to setting the old vector number but returning a different vector number. This patch holds the irq_lock when doing the two operations to prevent the case.

---
 xen/arch/x86/hvm/irq.c           | 11 ++++++----
 xen/arch/x86/hvm/vlapic.c        | 11 ++++++++++
 xen/arch/x86/hvm/vpt.c           | 43 ++++++++++++++++++++++++++++------------
 xen/include/asm-x86/hvm/irq.h    |  1 +
 xen/include/asm-x86/hvm/vlapic.h |  1 +
 5 files changed, 50 insertions(+), 17 deletions(-)
xen/arch/x86/hvm/irq.c +7 -4
index e425df9..7b0c0b1 100644
@@ -168,20 +168,23 @@ void hvm_gsi_deassert(struct domain *d, unsigned int gsi)
     spin_unlock(&d->arch.hvm_domain.irq_lock);
 }
 
-void hvm_isa_irq_assert(
-    struct domain *d, unsigned int isa_irq)
+void hvm_isa_irq_assert_locked(struct domain *d, unsigned int isa_irq)
 {
     struct hvm_irq *hvm_irq = hvm_domain_irq(d);
     unsigned int gsi = hvm_isa_irq_to_gsi(isa_irq);
 
     ASSERT(isa_irq <= 15);
-
-    spin_lock(&d->arch.hvm_domain.irq_lock);
+    ASSERT(spin_is_locked(&d->arch.hvm_domain.irq_lock));
 
     if ( !__test_and_set_bit(isa_irq, &hvm_irq->isa_irq.i) &&
          (hvm_irq->gsi_assert_count[gsi]++ == 0) )
         assert_irq(d, gsi, isa_irq);
+}
 
+void hvm_isa_irq_assert(struct domain *d, unsigned int isa_irq)
+{
+    spin_lock(&d->arch.hvm_domain.irq_lock);
+    hvm_isa_irq_assert_locked(d, isa_irq);
     spin_unlock(&d->arch.hvm_domain.irq_lock);
 }
 
xen/arch/x86/hvm/vlapic.c +11 -0
index 4bfc53e..b27b15b 100644
@@ -137,6 +137,17 @@ static void vlapic_error(struct vlapic *vlapic, unsigned int errmask)
     spin_unlock_irqrestore(&vlapic->esr_lock, flags);
 }
 
+bool vlapic_test_irq(struct vlapic *vlapic, uint8_t vec)
+{
+    if ( unlikely(vec < 16) )
+        return false;
+
+    if ( hvm_funcs.sync_pir_to_irr )
+        hvm_funcs.sync_pir_to_irr(vlapic_vcpu(vlapic));
+
+    return vlapic_test_vector(vec, &vlapic->regs->data[APIC_IRR]);
+}
+
 void vlapic_set_irq(struct vlapic *vlapic, uint8_t vec, uint8_t trig)
 {
     struct vcpu *target = vlapic_vcpu(vlapic);
xen/arch/x86/hvm/vpt.c +30 -13

62 lines not on this page · raw message

xen/include/asm-x86/hvm/irq.h +1 -0
index 3b6b4bd..d20131a 100644
@@ -189,6 +189,7 @@ void hvm_pci_intx_deassert(struct domain *d, unsigned int device,
 
 /* Modify state of an ISA device's IRQ wire. */
 void hvm_isa_irq_assert(struct domain *d, unsigned int isa_irq);
+void hvm_isa_irq_assert_locked(struct domain *d, unsigned int isa_irq);
 void hvm_isa_irq_deassert(struct domain *d, unsigned int isa_irq);
 
 /* Modify state of GSIs. */
xen/include/asm-x86/hvm/vlapic.h +1 -0
index a63fcd5..1f849c4 100644
@@ -110,6 +110,7 @@ static inline void vlapic_set_reg(
 
 bool_t is_vlapic_lvtpc_enabled(struct vlapic *vlapic);
 
+bool vlapic_test_irq(struct vlapic *vlapic, uint8_t vec);
 void vlapic_set_irq(struct vlapic *vlapic, uint8_t vec, uint8_t trig);
 
 int vlapic_has_pending_irq(struct vcpu *v);
Signature · 7 lines
1.8.3.1


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
author
#2 lore raw
In reply to Jan Beulich
Quoted 6 lines · Jan Beulich
Quoted 1 line
Quoted 1 line
Quoted 1 line · On 09.10.17 at 23:32, <[email protected]> wrote:

First of all - please use a better subject. If someone finds another bug in this function in, say, half a year's time, how will we tell apart the two patches from looking at just the list of titles several years later?

Will update.

Quoted 19 lines
Quoted 19 lines

pt_update_irq() is expected to return the vector number of periodic timer interrupt, which should be set in vIRR of vlapic. Otherwise it would trigger the assertion in vmx_intr_assist(), please seeing
https://lists.xenproject.org/archives/html/xen-devel/2017-10/msg00915.html.

But it fails to achieve that in the following two case:
1. hvm_isa_irq_assert() may not set the corresponding bit in vIRR for mask field of IOAPIC RTE is set. Please refer to the call tree vmx_intr_assist() -> pt_update_irq() -> hvm_isa_irq_assert() -> assert_irq() -> assert_gsi() -> vioapic_irq_positive_edge(). The patch checks whether the vector is set or not in vIRR of vlapic before returning.

2. someone changes the vector field of IOAPIC RTE between asserting the irq and getting the vector of the irq, leading to setting the old vector number but returning a different vector number. This patch holds the irq_lock when doing the two operations to prevent the case.

Quoted 5 lines
Quoted 1 line

Point 2 is very unlikely to be the cause of the failed assertion that osstest keeps hitting once in a while. Did your analysis yield indication that point 1 is what is happening there?

I believe it is likely to be the case. On the other hand, the assertion can be triggered in above two cases; it needs to be fixed.

Quoted 19 lines

xen/arch/x86/hvm/irq.c void hvm_gsi_deassert(struct domain *d, unsigned int gsi) · line 171

Quoted 10 lines
xen/arch/x86/hvm/irq.c +1 -2
@@ -168,20 +168,23 @@ void hvm_gsi_deassert(struct domain *d, unsigned int gsi)
     spin_unlock(&d->arch.hvm_domain.irq_lock);
 }
 
-void hvm_isa_irq_assert(
-    struct domain *d, unsigned int isa_irq)
+void hvm_isa_irq_assert_locked(struct domain *d, unsigned int isa_irq)

Please don't introduce a non-static function like this. Instead I would suggest you introduce a new helper function doing what you introduce as replacement to the call to hvm_isa_irq_assert(). That'll presumably involve passing a get_vector() callback to a wrapper of pt_irq_vector() (or to an abbreviated form of it, as "src" is hvm_intsrc_lapic), since I understand you need this called with the lock held.

Quoted 6 lines

And once you do this I don't think it'll be worthwhile breaking out hvm_isa_irq_assert_locked() at all - you'll just have a sibling to hvm_isa_irq_assert(). Or, considering the few callers the function has, simply giving that function itself an optional callback parameter might be even better (eliminating any code duplication).

Ok. I understand what you suggestion. Will give it a shot.

Quoted 13 lines

xen/arch/x86/hvm/vlapic.c static void vlapic_error(struct vlapic *vlapic, unsigned int errmask) · line 140

Quoted 8 lines
xen/arch/x86/hvm/vlapic.c +1 -0
@@ -137,6 +137,17 @@ static void vlapic_error(struct vlapic *vlapic, unsigned int errmask)
     spin_unlock_irqrestore(&vlapic->esr_lock, flags);
 }
 
+bool vlapic_test_irq(struct vlapic *vlapic, uint8_t vec)

The way the function is named, the pointer should be const qualified. However, the function does more than just testing current state:

Quoted 11 lines

xen/arch/x86/hvm/vlapic.c static void vlapic_error(struct vlapic *vlapic, unsigned int errmask) · line 146

Quoted 6 lines
xen/arch/x86/hvm/vlapic.c +6 -0
+{
+    if ( unlikely(vec < 16) )
+        return false;
+
+    if ( hvm_funcs.sync_pir_to_irr )
+        hvm_funcs.sync_pir_to_irr(vlapic_vcpu(vlapic));

Question is whether this is really necessary, of whether instead you could just return the state of the respective PIR bit here. I'd prefer that over giving the function a name no longer suggesting it leaves all state alone.

It is a good suggestion. But I incline to check the PIR bit and if the bit is set, return true and otherwise return the state of the vIRR bit in case PIR bits are already synced to vIRR.

Thanks
Chao

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel

#1 lore raw
In reply to Chao Gao
Quoted 1 line
Quoted 1 line
Quoted 1 line · On 09.10.17 at 23:32, <[email protected]> wrote:

First of all - please use a better subject. If someone finds another bug in this function in, say, half a year's time, how will we tell apart the two patches from looking at just the list of titles several years later?

Quoted 13 lines

pt_update_irq() is expected to return the vector number of periodic timer interrupt, which should be set in vIRR of vlapic. Otherwise it would trigger the assertion in vmx_intr_assist(), please seeing
https://lists.xenproject.org/archives/html/xen-devel/2017-10/msg00915.html.

But it fails to achieve that in the following two case:
1. hvm_isa_irq_assert() may not set the corresponding bit in vIRR for mask field of IOAPIC RTE is set. Please refer to the call tree vmx_intr_assist() -> pt_update_irq() -> hvm_isa_irq_assert() -> assert_irq() -> assert_gsi() -> vioapic_irq_positive_edge(). The patch checks whether the vector is set or not in vIRR of vlapic before returning.

Quoted 6 lines

2. someone changes the vector field of IOAPIC RTE between asserting the irq and getting the vector of the irq, leading to setting the old vector number but returning a different vector number. This patch holds the irq_lock when doing the two operations to prevent the case.

Point 2 is very unlikely to be the cause of the failed assertion that osstest keeps hitting once in a while. Did your analysis yield indication that point 1 is what is happening there?

xen/arch/x86/hvm/irq.c void hvm_gsi_deassert(struct domain *d, unsigned int gsi) · line 171

Quoted 9 lines
xen/arch/x86/hvm/irq.c +1 -2
@@ -168,20 +168,23 @@ void hvm_gsi_deassert(struct domain *d, unsigned int gsi)
     spin_unlock(&d->arch.hvm_domain.irq_lock);
 }
 
-void hvm_isa_irq_assert(
-    struct domain *d, unsigned int isa_irq)
+void hvm_isa_irq_assert_locked(struct domain *d, unsigned int isa_irq)

Please don't introduce a non-static function like this. Instead I would suggest you introduce a new helper function doing what you introduce as replacement to the call to hvm_isa_irq_assert(). That'll presumably involve passing a get_vector() callback to a wrapper of pt_irq_vector() (or to an abbreviated form of it, as "src" is hvm_intsrc_lapic), since I understand you need this called with the lock held.

And once you do this I don't think it'll be worthwhile breaking out hvm_isa_irq_assert_locked() at all - you'll just have a sibling to hvm_isa_irq_assert(). Or, considering the few callers the function has, simply giving that function itself an optional callback parameter might be even better (eliminating any code duplication).

xen/arch/x86/hvm/vlapic.c static void vlapic_error(struct vlapic *vlapic, unsigned int errmask) · line 140

Quoted 7 lines
xen/arch/x86/hvm/vlapic.c +1 -0
@@ -137,6 +137,17 @@ static void vlapic_error(struct vlapic *vlapic, unsigned int errmask)
     spin_unlock_irqrestore(&vlapic->esr_lock, flags);
 }
 
+bool vlapic_test_irq(struct vlapic *vlapic, uint8_t vec)

The way the function is named, the pointer should be const qualified. However, the function does more than just testing current state:

xen/arch/x86/hvm/vlapic.c static void vlapic_error(struct vlapic *vlapic, unsigned int errmask) · line 146

Quoted 6 lines
xen/arch/x86/hvm/vlapic.c +6 -0
+{
+    if ( unlikely(vec < 16) )
+        return false;
+
+    if ( hvm_funcs.sync_pir_to_irr )
+        hvm_funcs.sync_pir_to_irr(vlapic_vcpu(vlapic));

Question is whether this is really necessary, of whether instead you could just return the state of the respective PIR bit here. I'd prefer that over giving the function a name no longer suggesting it leaves all state alone.

Jan

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel

#3 lore raw
In reply to Chao Gao
Quoted 15 lines
Quoted 1 line
Quoted 1 line · On 13.10.17 at 10:14, <[email protected]> wrote:
Quoted 12 lines · Jan Beulich

xen/arch/x86/hvm/vlapic.c static void vlapic_error(struct vlapic *vlapic, unsigned int errmask) · line 146

Quoted 7 lines
Quoted 1 line
Quoted 1 line · On 09.10.17 at 23:32, <[email protected]> wrote:
xen/arch/x86/hvm/vlapic.c +6 -0
+{
+    if ( unlikely(vec < 16) )
+        return false;
+
+    if ( hvm_funcs.sync_pir_to_irr )
+        hvm_funcs.sync_pir_to_irr(vlapic_vcpu(vlapic));

Question is whether this is really necessary, of whether instead you could just return the state of the respective PIR bit here. I'd prefer that over giving the function a name no longer suggesting it leaves all state alone.

Quoted 3 lines

It is a good suggestion. But I incline to check the PIR bit and if the bit is set, return true and otherwise return the state of the vIRR bit in case PIR bits are already synced to vIRR.

Oh, right, of course you should return the effective "or" of the two bits.

Jan

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel