diff mbox series

[16/20] spapr, xics, xive: Better use of assert()s on irq claim/free paths

Message ID 20190925064534.19155-17-david@gibson.dropbear.id.au (mailing list archive)
State New, archived
Headers show
Series spapr: IRQ subsystem cleanups | expand

Commit Message

David Gibson Sept. 25, 2019, 6:45 a.m. UTC
The irq claim and free paths for both XICS and XIVE check for some
validity conditions.  Some of these represent genuine runtime failures,
however others - particularly checking that the basic irq number is in a
sane range - could only fail in the case of bugs in the callin code.
Therefore use assert()s instead of runtime failures for those.

In addition the non backend-specific part of the claim/free paths should
only be used for PAPR external irqs, that is in the range SPAPR_XIRQ_BASE
to the maximum irq number.  Put assert()s for that into the top level
dispatchers as well.

Signed-off-by: David Gibson <david@gibson.dropbear.id.au>
---
 hw/intc/spapr_xive.c |  8 ++------
 hw/ppc/spapr_irq.c   | 18 ++++++++++--------
 2 files changed, 12 insertions(+), 14 deletions(-)

Comments

Cédric Le Goater Sept. 25, 2019, 7:22 a.m. UTC | #1
On 25/09/2019 08:45, David Gibson wrote:
> The irq claim and free paths for both XICS and XIVE check for some
> validity conditions.  Some of these represent genuine runtime failures,
> however others - particularly checking that the basic irq number is in a
> sane range - could only fail in the case of bugs in the callin code.
> Therefore use assert()s instead of runtime failures for those.
> 
> In addition the non backend-specific part of the claim/free paths should
> only be used for PAPR external irqs, that is in the range SPAPR_XIRQ_BASE
> to the maximum irq number.  Put assert()s for that into the top level
> dispatchers as well.
> 
> Signed-off-by: David Gibson <david@gibson.dropbear.id.au>

Reviewed-by: Cédric Le Goater <clg@kaod.org>

> ---
>  hw/intc/spapr_xive.c |  8 ++------
>  hw/ppc/spapr_irq.c   | 18 ++++++++++--------
>  2 files changed, 12 insertions(+), 14 deletions(-)
> 
> diff --git a/hw/intc/spapr_xive.c b/hw/intc/spapr_xive.c
> index c1c97192a7..47b5ec0b56 100644
> --- a/hw/intc/spapr_xive.c
> +++ b/hw/intc/spapr_xive.c
> @@ -532,9 +532,7 @@ bool spapr_xive_irq_claim(SpaprXive *xive, uint32_t lisn, bool lsi)
>  {
>      XiveSource *xsrc = &xive->source;
>  
> -    if (lisn >= xive->nr_irqs) {
> -        return false;
> -    }
> +    assert(lisn < xive->nr_irqs);
>  
>      /*
>       * Set default values when allocating an IRQ number
> @@ -559,9 +557,7 @@ bool spapr_xive_irq_claim(SpaprXive *xive, uint32_t lisn, bool lsi)
>  
>  bool spapr_xive_irq_free(SpaprXive *xive, uint32_t lisn)
>  {
> -    if (lisn >= xive->nr_irqs) {
> -        return false;
> -    }
> +    assert(lisn < xive->nr_irqs);
>  
>      xive->eat[lisn].w &= cpu_to_be64(~EAS_VALID);
>      return true;
> diff --git a/hw/ppc/spapr_irq.c b/hw/ppc/spapr_irq.c
> index c40357a985..261d66ba17 100644
> --- a/hw/ppc/spapr_irq.c
> +++ b/hw/ppc/spapr_irq.c
> @@ -118,11 +118,7 @@ static int spapr_irq_claim_xics(SpaprMachineState *spapr, int irq, bool lsi,
>      ICSState *ics = spapr->ics;
>  
>      assert(ics);
> -
> -    if (!ics_valid_irq(ics, irq)) {
> -        error_setg(errp, "IRQ %d is invalid", irq);
> -        return -1;
> -    }
> +    assert(ics_valid_irq(ics, irq));
>  
>      if (!ics_irq_free(ics, irq - ics->offset)) {
>          error_setg(errp, "IRQ %d is not free", irq);
> @@ -138,9 +134,9 @@ static void spapr_irq_free_xics(SpaprMachineState *spapr, int irq)
>      ICSState *ics = spapr->ics;
>      uint32_t srcno = irq - ics->offset;
>  
> -    if (ics_valid_irq(ics, irq)) {
> -        memset(&ics->irqs[srcno], 0, sizeof(ICSIRQState));
> -    }
> +    assert(ics_valid_irq(ics, irq));
> +
> +    memset(&ics->irqs[srcno], 0, sizeof(ICSIRQState));
>  }
>  
>  static void spapr_irq_print_info_xics(SpaprMachineState *spapr, Monitor *mon)
> @@ -628,6 +624,9 @@ void spapr_irq_init(SpaprMachineState *spapr, Error **errp)
>  
>  int spapr_irq_claim(SpaprMachineState *spapr, int irq, bool lsi, Error **errp)
>  {
> +    assert(irq >= SPAPR_XIRQ_BASE);
> +    assert(irq < (spapr->irq->nr_xirqs + SPAPR_XIRQ_BASE));
> +
>      return spapr->irq->claim(spapr, irq, lsi, errp);
>  }
>  
> @@ -635,6 +634,9 @@ void spapr_irq_free(SpaprMachineState *spapr, int irq, int num)
>  {
>      int i;
>  
> +    assert(irq >= SPAPR_XIRQ_BASE);
> +    assert((irq+num) <= (spapr->irq->nr_xirqs + SPAPR_XIRQ_BASE));
> +
>      for (i = irq; i < (irq + num); i++) {
>          spapr->irq->free(spapr, irq);
>      }
>
Greg Kurz Sept. 26, 2019, 8:08 a.m. UTC | #2
On Wed, 25 Sep 2019 16:45:30 +1000
David Gibson <david@gibson.dropbear.id.au> wrote:

> The irq claim and free paths for both XICS and XIVE check for some
> validity conditions.  Some of these represent genuine runtime failures,
> however others - particularly checking that the basic irq number is in a
> sane range - could only fail in the case of bugs in the callin code.
> Therefore use assert()s instead of runtime failures for those.
> 
> In addition the non backend-specific part of the claim/free paths should
> only be used for PAPR external irqs, that is in the range SPAPR_XIRQ_BASE
> to the maximum irq number.  Put assert()s for that into the top level
> dispatchers as well.
> 
> Signed-off-by: David Gibson <david@gibson.dropbear.id.au>
> ---
>  hw/intc/spapr_xive.c |  8 ++------
>  hw/ppc/spapr_irq.c   | 18 ++++++++++--------
>  2 files changed, 12 insertions(+), 14 deletions(-)
> 
> diff --git a/hw/intc/spapr_xive.c b/hw/intc/spapr_xive.c
> index c1c97192a7..47b5ec0b56 100644
> --- a/hw/intc/spapr_xive.c
> +++ b/hw/intc/spapr_xive.c
> @@ -532,9 +532,7 @@ bool spapr_xive_irq_claim(SpaprXive *xive, uint32_t lisn, bool lsi)
>  {
>      XiveSource *xsrc = &xive->source;
>  
> -    if (lisn >= xive->nr_irqs) {
> -        return false;
> -    }
> +    assert(lisn < xive->nr_irqs);
>  
>      /*
>       * Set default values when allocating an IRQ number
> @@ -559,9 +557,7 @@ bool spapr_xive_irq_claim(SpaprXive *xive, uint32_t lisn, bool lsi)
>  
>  bool spapr_xive_irq_free(SpaprXive *xive, uint32_t lisn)
>  {
> -    if (lisn >= xive->nr_irqs) {
> -        return false;
> -    }
> +    assert(lisn < xive->nr_irqs);
>  
>      xive->eat[lisn].w &= cpu_to_be64(~EAS_VALID);
>      return true;
> diff --git a/hw/ppc/spapr_irq.c b/hw/ppc/spapr_irq.c
> index c40357a985..261d66ba17 100644
> --- a/hw/ppc/spapr_irq.c
> +++ b/hw/ppc/spapr_irq.c
> @@ -118,11 +118,7 @@ static int spapr_irq_claim_xics(SpaprMachineState *spapr, int irq, bool lsi,
>      ICSState *ics = spapr->ics;
>  
>      assert(ics);
> -
> -    if (!ics_valid_irq(ics, irq)) {
> -        error_setg(errp, "IRQ %d is invalid", irq);
> -        return -1;
> -    }
> +    assert(ics_valid_irq(ics, irq));
>  
>      if (!ics_irq_free(ics, irq - ics->offset)) {
>          error_setg(errp, "IRQ %d is not free", irq);
> @@ -138,9 +134,9 @@ static void spapr_irq_free_xics(SpaprMachineState *spapr, int irq)
>      ICSState *ics = spapr->ics;
>      uint32_t srcno = irq - ics->offset;
>  
> -    if (ics_valid_irq(ics, irq)) {
> -        memset(&ics->irqs[srcno], 0, sizeof(ICSIRQState));
> -    }
> +    assert(ics_valid_irq(ics, irq));
> +
> +    memset(&ics->irqs[srcno], 0, sizeof(ICSIRQState));
>  }
>  
>  static void spapr_irq_print_info_xics(SpaprMachineState *spapr, Monitor *mon)
> @@ -628,6 +624,9 @@ void spapr_irq_init(SpaprMachineState *spapr, Error **errp)
>  
>  int spapr_irq_claim(SpaprMachineState *spapr, int irq, bool lsi, Error **errp)
>  {
> +    assert(irq >= SPAPR_XIRQ_BASE);
> +    assert(irq < (spapr->irq->nr_xirqs + SPAPR_XIRQ_BASE));
> +
>      return spapr->irq->claim(spapr, irq, lsi, errp);
>  }
>  
> @@ -635,6 +634,9 @@ void spapr_irq_free(SpaprMachineState *spapr, int irq, int num)
>  {
>      int i;
>  
> +    assert(irq >= SPAPR_XIRQ_BASE);
> +    assert((irq+num) <= (spapr->irq->nr_xirqs + SPAPR_XIRQ_BASE));

Non surprisingly this makes checkpatch unhappy:

ERROR: spaces required around that '+' (ctx:VxV)
#91: FILE: hw/ppc/spapr_irq.c:638:
+    assert((irq+num) <= (spapr->irq->nr_xirqs + SPAPR_XIRQ_BASE));

With that fixed,

Reviewed-by: Greg Kurz <groug@kaod.org>

> +
>      for (i = irq; i < (irq + num); i++) {
>          spapr->irq->free(spapr, irq);
>      }
David Gibson Sept. 26, 2019, 11:39 a.m. UTC | #3
On Thu, Sep 26, 2019 at 10:08:41AM +0200, Greg Kurz wrote:
> On Wed, 25 Sep 2019 16:45:30 +1000
> David Gibson <david@gibson.dropbear.id.au> wrote:
> 
> > The irq claim and free paths for both XICS and XIVE check for some
> > validity conditions.  Some of these represent genuine runtime failures,
> > however others - particularly checking that the basic irq number is in a
> > sane range - could only fail in the case of bugs in the callin code.
> > Therefore use assert()s instead of runtime failures for those.
> > 
> > In addition the non backend-specific part of the claim/free paths should
> > only be used for PAPR external irqs, that is in the range SPAPR_XIRQ_BASE
> > to the maximum irq number.  Put assert()s for that into the top level
> > dispatchers as well.
> > 
> > Signed-off-by: David Gibson <david@gibson.dropbear.id.au>
> > ---
> >  hw/intc/spapr_xive.c |  8 ++------
> >  hw/ppc/spapr_irq.c   | 18 ++++++++++--------
> >  2 files changed, 12 insertions(+), 14 deletions(-)
> > 
> > diff --git a/hw/intc/spapr_xive.c b/hw/intc/spapr_xive.c
> > index c1c97192a7..47b5ec0b56 100644
> > --- a/hw/intc/spapr_xive.c
> > +++ b/hw/intc/spapr_xive.c
> > @@ -532,9 +532,7 @@ bool spapr_xive_irq_claim(SpaprXive *xive, uint32_t lisn, bool lsi)
> >  {
> >      XiveSource *xsrc = &xive->source;
> >  
> > -    if (lisn >= xive->nr_irqs) {
> > -        return false;
> > -    }
> > +    assert(lisn < xive->nr_irqs);
> >  
> >      /*
> >       * Set default values when allocating an IRQ number
> > @@ -559,9 +557,7 @@ bool spapr_xive_irq_claim(SpaprXive *xive, uint32_t lisn, bool lsi)
> >  
> >  bool spapr_xive_irq_free(SpaprXive *xive, uint32_t lisn)
> >  {
> > -    if (lisn >= xive->nr_irqs) {
> > -        return false;
> > -    }
> > +    assert(lisn < xive->nr_irqs);
> >  
> >      xive->eat[lisn].w &= cpu_to_be64(~EAS_VALID);
> >      return true;
> > diff --git a/hw/ppc/spapr_irq.c b/hw/ppc/spapr_irq.c
> > index c40357a985..261d66ba17 100644
> > --- a/hw/ppc/spapr_irq.c
> > +++ b/hw/ppc/spapr_irq.c
> > @@ -118,11 +118,7 @@ static int spapr_irq_claim_xics(SpaprMachineState *spapr, int irq, bool lsi,
> >      ICSState *ics = spapr->ics;
> >  
> >      assert(ics);
> > -
> > -    if (!ics_valid_irq(ics, irq)) {
> > -        error_setg(errp, "IRQ %d is invalid", irq);
> > -        return -1;
> > -    }
> > +    assert(ics_valid_irq(ics, irq));
> >  
> >      if (!ics_irq_free(ics, irq - ics->offset)) {
> >          error_setg(errp, "IRQ %d is not free", irq);
> > @@ -138,9 +134,9 @@ static void spapr_irq_free_xics(SpaprMachineState *spapr, int irq)
> >      ICSState *ics = spapr->ics;
> >      uint32_t srcno = irq - ics->offset;
> >  
> > -    if (ics_valid_irq(ics, irq)) {
> > -        memset(&ics->irqs[srcno], 0, sizeof(ICSIRQState));
> > -    }
> > +    assert(ics_valid_irq(ics, irq));
> > +
> > +    memset(&ics->irqs[srcno], 0, sizeof(ICSIRQState));
> >  }
> >  
> >  static void spapr_irq_print_info_xics(SpaprMachineState *spapr, Monitor *mon)
> > @@ -628,6 +624,9 @@ void spapr_irq_init(SpaprMachineState *spapr, Error **errp)
> >  
> >  int spapr_irq_claim(SpaprMachineState *spapr, int irq, bool lsi, Error **errp)
> >  {
> > +    assert(irq >= SPAPR_XIRQ_BASE);
> > +    assert(irq < (spapr->irq->nr_xirqs + SPAPR_XIRQ_BASE));
> > +
> >      return spapr->irq->claim(spapr, irq, lsi, errp);
> >  }
> >  
> > @@ -635,6 +634,9 @@ void spapr_irq_free(SpaprMachineState *spapr, int irq, int num)
> >  {
> >      int i;
> >  
> > +    assert(irq >= SPAPR_XIRQ_BASE);
> > +    assert((irq+num) <= (spapr->irq->nr_xirqs + SPAPR_XIRQ_BASE));
> 
> Non surprisingly this makes checkpatch unhappy:
> 
> ERROR: spaces required around that '+' (ctx:VxV)
> #91: FILE: hw/ppc/spapr_irq.c:638:
> +    assert((irq+num) <= (spapr->irq->nr_xirqs + SPAPR_XIRQ_BASE));

Oops, fixed.  I hadn't done a checkpatch run yet, since I'm still
working actively on the series.

> 
> With that fixed,
> 
> Reviewed-by: Greg Kurz <groug@kaod.org>
> 
> > +
> >      for (i = irq; i < (irq + num); i++) {
> >          spapr->irq->free(spapr, irq);
> >      }
>
diff mbox series

Patch

diff --git a/hw/intc/spapr_xive.c b/hw/intc/spapr_xive.c
index c1c97192a7..47b5ec0b56 100644
--- a/hw/intc/spapr_xive.c
+++ b/hw/intc/spapr_xive.c
@@ -532,9 +532,7 @@  bool spapr_xive_irq_claim(SpaprXive *xive, uint32_t lisn, bool lsi)
 {
     XiveSource *xsrc = &xive->source;
 
-    if (lisn >= xive->nr_irqs) {
-        return false;
-    }
+    assert(lisn < xive->nr_irqs);
 
     /*
      * Set default values when allocating an IRQ number
@@ -559,9 +557,7 @@  bool spapr_xive_irq_claim(SpaprXive *xive, uint32_t lisn, bool lsi)
 
 bool spapr_xive_irq_free(SpaprXive *xive, uint32_t lisn)
 {
-    if (lisn >= xive->nr_irqs) {
-        return false;
-    }
+    assert(lisn < xive->nr_irqs);
 
     xive->eat[lisn].w &= cpu_to_be64(~EAS_VALID);
     return true;
diff --git a/hw/ppc/spapr_irq.c b/hw/ppc/spapr_irq.c
index c40357a985..261d66ba17 100644
--- a/hw/ppc/spapr_irq.c
+++ b/hw/ppc/spapr_irq.c
@@ -118,11 +118,7 @@  static int spapr_irq_claim_xics(SpaprMachineState *spapr, int irq, bool lsi,
     ICSState *ics = spapr->ics;
 
     assert(ics);
-
-    if (!ics_valid_irq(ics, irq)) {
-        error_setg(errp, "IRQ %d is invalid", irq);
-        return -1;
-    }
+    assert(ics_valid_irq(ics, irq));
 
     if (!ics_irq_free(ics, irq - ics->offset)) {
         error_setg(errp, "IRQ %d is not free", irq);
@@ -138,9 +134,9 @@  static void spapr_irq_free_xics(SpaprMachineState *spapr, int irq)
     ICSState *ics = spapr->ics;
     uint32_t srcno = irq - ics->offset;
 
-    if (ics_valid_irq(ics, irq)) {
-        memset(&ics->irqs[srcno], 0, sizeof(ICSIRQState));
-    }
+    assert(ics_valid_irq(ics, irq));
+
+    memset(&ics->irqs[srcno], 0, sizeof(ICSIRQState));
 }
 
 static void spapr_irq_print_info_xics(SpaprMachineState *spapr, Monitor *mon)
@@ -628,6 +624,9 @@  void spapr_irq_init(SpaprMachineState *spapr, Error **errp)
 
 int spapr_irq_claim(SpaprMachineState *spapr, int irq, bool lsi, Error **errp)
 {
+    assert(irq >= SPAPR_XIRQ_BASE);
+    assert(irq < (spapr->irq->nr_xirqs + SPAPR_XIRQ_BASE));
+
     return spapr->irq->claim(spapr, irq, lsi, errp);
 }
 
@@ -635,6 +634,9 @@  void spapr_irq_free(SpaprMachineState *spapr, int irq, int num)
 {
     int i;
 
+    assert(irq >= SPAPR_XIRQ_BASE);
+    assert((irq+num) <= (spapr->irq->nr_xirqs + SPAPR_XIRQ_BASE));
+
     for (i = irq; i < (irq + num); i++) {
         spapr->irq->free(spapr, irq);
     }