Skip to contentSkip to conversation

gic:vgic: avoid excessive conversions

PATCH In review Andrii Anisov 3 participants 7 messages Nov 16, 2018 – Dec 19, 2018

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 gic:vgic: avoid excessive conversions

From: Andrii Anisov <[email protected]>

Avoid excessive conversions between pending_irq and irq number/priority. This simlifies functions interface and reduces under locks code size.

---
 xen/arch/arm/gic-vgic.c    | 10 +++-------
 xen/arch/arm/vgic-v3-its.c |  2 +-
 xen/arch/arm/vgic.c        |  6 +++---
 xen/include/asm-arm/gic.h  |  5 ++---
 4 files changed, 9 insertions(+), 14 deletions(-)
xen/arch/arm/gic-vgic.c +3 -7
index 990399c..e8e9136 100644
@@ -73,10 +73,8 @@ void gic_remove_from_lr_pending(struct vcpu *v, struct pending_irq *p)
     list_del_init(&p->lr_queue);
 }
 
-void gic_raise_inflight_irq(struct vcpu *v, unsigned int virtual_irq)
+void gic_raise_inflight_irq(struct vcpu *v, struct pending_irq *n)
 {
-    struct pending_irq *n = irq_to_pending(v, virtual_irq);
-
     /* If an LPI has been removed meanwhile, there is nothing left to raise. */
     if ( unlikely(!n) )
         return;
@@ -133,12 +131,10 @@ static unsigned int gic_find_unused_lr(struct vcpu *v,
     return lr;
 }
 
-void gic_raise_guest_irq(struct vcpu *v, unsigned int virtual_irq,
-        unsigned int priority)
+void gic_raise_guest_irq(struct vcpu *v, struct pending_irq *p)
 {
     int i;
     unsigned int nr_lrs = gic_get_nr_lrs();
-    struct pending_irq *p = irq_to_pending(v, virtual_irq);
 
     ASSERT(spin_is_locked(&v->arch.vgic.lock));
 
@@ -227,7 +223,7 @@ static void gic_update_one_lr(struct vcpu *v, int i)
         if ( test_bit(GIC_IRQ_GUEST_ENABLED, &p->status) &&
              test_bit(GIC_IRQ_GUEST_QUEUED, &p->status) &&
              !test_bit(GIC_IRQ_GUEST_MIGRATING, &p->status) )
-            gic_raise_guest_irq(v, irq, p->priority);
+            gic_raise_guest_irq(v, p);
         else {
             list_del_init(&p->inflight);
             /*
xen/arch/arm/vgic-v3-its.c +1 -1
index 5b73c4e..193a28f 100644
@@ -447,7 +447,7 @@ static void update_lpi_vgic_status(struct vcpu *v, struct pending_irq *p)
     {
         if ( !list_empty(&p->inflight) &&
              !test_bit(GIC_IRQ_GUEST_VISIBLE, &p->status) )
-            gic_raise_guest_irq(v, p->irq, p->lpi_priority);
+            gic_raise_guest_irq(v, p);
     }
     else
         gic_remove_from_lr_pending(v, p);
xen/arch/arm/vgic.c +3 -3
index 5a4f082..628a34f 100644
@@ -403,7 +403,7 @@ void vgic_enable_irqs(struct vcpu *v, uint32_t r, int n)
         p = irq_to_pending(v_target, irq);
         set_bit(GIC_IRQ_GUEST_ENABLED, &p->status);
         if ( !list_empty(&p->inflight) && !test_bit(GIC_IRQ_GUEST_VISIBLE, &p->status) )
-            gic_raise_guest_irq(v_target, irq, p->priority);
+            gic_raise_guest_irq(v_target, p);
         spin_unlock_irqrestore(&v_target->arch.vgic.lock, flags);
         if ( p->desc != NULL )
         {
@@ -568,7 +568,7 @@ void vgic_inject_irq(struct domain *d, struct vcpu *v, unsigned int virq,
 
     if ( !list_empty(&n->inflight) )
     {
-        gic_raise_inflight_irq(v, virq);
+        gic_raise_inflight_irq(v, n);
         goto out;
     }
 
@@ -577,7 +577,7 @@ void vgic_inject_irq(struct domain *d, struct vcpu *v, unsigned int virq,
 
     /* the irq is enabled */
     if ( test_bit(GIC_IRQ_GUEST_ENABLED, &n->status) )
-        gic_raise_guest_irq(v, virq, priority);
+        gic_raise_guest_irq(v, n);
 
     list_for_each_entry ( iter, &v->arch.vgic.inflight_irqs, inflight )
     {
xen/include/asm-arm/gic.h +2 -3
index fab02f1..1859416 100644
@@ -252,9 +252,8 @@ int gic_remove_irq_from_guest(struct domain *d, unsigned int virq,
 extern void gic_clear_pending_irqs(struct vcpu *v);
 
 extern void init_maintenance_interrupt(void);
-extern void gic_raise_guest_irq(struct vcpu *v, unsigned int irq,
-        unsigned int priority);
-extern void gic_raise_inflight_irq(struct vcpu *v, unsigned int virtual_irq);
+extern void gic_raise_guest_irq(struct vcpu *v, struct pending_irq *p);
+extern void gic_raise_inflight_irq(struct vcpu *v, struct pending_irq *n);
 
 /* Accept an interrupt from the GIC and dispatch its handler */
 extern void gic_interrupt(struct cpu_user_regs *regs, int is_fiq);
Signature · 7 lines
2.7.4


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
#1 lore raw

Hi Andrii,

Quoted 57 lines · Andrii Anisov

From: Andrii Anisov <[email protected]>

Avoid excessive conversions between pending_irq and irq number/priority. This simlifies functions interface and reduces under locks code size.

---
  xen/arch/arm/gic-vgic.c    | 10 +++-------
  xen/arch/arm/vgic-v3-its.c |  2 +-
  xen/arch/arm/vgic.c        |  6 +++---
  xen/include/asm-arm/gic.h  |  5 ++---
  4 files changed, 9 insertions(+), 14 deletions(-)
xen/arch/arm/gic-vgic.c +3 -7
index 990399c..e8e9136 100644
@@ -73,10 +73,8 @@ void gic_remove_from_lr_pending(struct vcpu *v, struct pending_irq *p)
      list_del_init(&p->lr_queue);
  }
 
-void gic_raise_inflight_irq(struct vcpu *v, unsigned int virtual_irq)
+void gic_raise_inflight_irq(struct vcpu *v, struct pending_irq *n)
  {
-    struct pending_irq *n = irq_to_pending(v, virtual_irq);
-
      /* If an LPI has been removed meanwhile, there is nothing left to raise. */
      if ( unlikely(!n) )
          return;
@@ -133,12 +131,10 @@ static unsigned int gic_find_unused_lr(struct vcpu *v,
      return lr;
  }
 
-void gic_raise_guest_irq(struct vcpu *v, unsigned int virtual_irq,
-        unsigned int priority)
+void gic_raise_guest_irq(struct vcpu *v, struct pending_irq *p)
  {
      int i;
      unsigned int nr_lrs = gic_get_nr_lrs();
-    struct pending_irq *p = irq_to_pending(v, virtual_irq);
 
      ASSERT(spin_is_locked(&v->arch.vgic.lock));
 
@@ -227,7 +223,7 @@ static void gic_update_one_lr(struct vcpu *v, int i)
          if ( test_bit(GIC_IRQ_GUEST_ENABLED, &p->status) &&
               test_bit(GIC_IRQ_GUEST_QUEUED, &p->status) &&
               !test_bit(GIC_IRQ_GUEST_MIGRATING, &p->status) )
-            gic_raise_guest_irq(v, irq, p->priority);
+            gic_raise_guest_irq(v, p);
          else {
              list_del_init(&p->inflight);
              /*
xen/arch/arm/vgic-v3-its.c +0 -0
index 5b73c4e..193a28f 100644

xen/arch/arm/vgic-v3-its.c static void update_lpi_vgic_status(struct vcpu *v, struct pending_irq *p) · line 450

Quoted 5 lines
xen/arch/arm/vgic-v3-its.c +1 -1
@@ -447,7 +447,7 @@ static void update_lpi_vgic_status(struct vcpu *v, struct pending_irq *p)
      {
          if ( !list_empty(&p->inflight) &&
               !test_bit(GIC_IRQ_GUEST_VISIBLE, &p->status) )
-            gic_raise_guest_irq(v, p->irq, p->lpi_priority);
+            gic_raise_guest_irq(v, p);

The interface is not behaving the same way now. I understand that nobody is using the 3 parameters but that's actually a bug with the ITS.

Because the LPI will not be set with expected priority.

Quoted 39 lines
xen/arch/arm/vgic-v3-its.c +0 -0
      }
      else
          gic_remove_from_lr_pending(v, p);
xen/arch/arm/vgic.c +3 -3
index 5a4f082..628a34f 100644
@@ -403,7 +403,7 @@ void vgic_enable_irqs(struct vcpu *v, uint32_t r, int n)
          p = irq_to_pending(v_target, irq);1651304100e
          set_bit(GIC_IRQ_GUEST_ENABLED, &p->status);
          if ( !list_empty(&p->inflight) && !test_bit(GIC_IRQ_GUEST_VISIBLE, &p->status) )
-            gic_raise_guest_irq(v_target, irq, p->priority);
+            gic_raise_guest_irq(v_target, p);
          spin_unlock_irqrestore(&v_target->arch.vgic.lock, flags);
          if ( p->desc != NULL )
          {
@@ -568,7 +568,7 @@ void vgic_inject_irq(struct domain *d, struct vcpu *v, unsigned int virq,
 
      if ( !list_empty(&n->inflight) )
      {
-        gic_raise_inflight_irq(v, virq);
+        gic_raise_inflight_irq(v, n);
          goto out;
      }
 
@@ -577,7 +577,7 @@ void vgic_inject_irq(struct domain *d, struct vcpu *v, unsigned int virq,
 
      /* the irq is enabled */
      if ( test_bit(GIC_IRQ_GUEST_ENABLED, &n->status) )
-        gic_raise_guest_irq(v, virq, priority);
+        gic_raise_guest_irq(v, n);
 
      list_for_each_entry ( iter, &v->arch.vgic.inflight_irqs, inflight )
      {
xen/include/asm-arm/gic.h +0 -0
index fab02f1..1859416 100644

xen/include/asm-arm/gic.h int gic_remove_irq_from_guest(struct domain *d, unsigned int virq, · line 256

Quoted 8 lines
xen/include/asm-arm/gic.h +2 -3
@@ -252,9 +252,8 @@ int gic_remove_irq_from_guest(struct domain *d, unsigned int virq,
  extern void gic_clear_pending_irqs(struct vcpu *v);
 
  extern void init_maintenance_interrupt(void);
-extern void gic_raise_guest_irq(struct vcpu *v, unsigned int irq,
-        unsigned int priority);
-extern void gic_raise_inflight_irq(struct vcpu *v, unsigned int virtual_irq);
+extern void gic_raise_guest_irq(struct vcpu *v, struct pending_irq *p);
+extern void gic_raise_inflight_irq(struct vcpu *v, struct pending_irq *n);

That code is not going to compile with the new vGIC as pending_irq only exists for the current vGIC.

xen/include/asm-arm/gic.h int gic_remove_irq_from_guest(struct domain *d, unsigned int virq,

Quoted 4 lines
xen/include/asm-arm/gic.h +0 -0
  /* Accept an interrupt from the GIC and dispatch its handler */
  extern void gic_interrupt(struct cpu_user_regs *regs, int is_fiq);
 

Cheers,

Signature · 6 lines
Julien Grall

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
#2 lore raw

Hello Julien,

Quoted 8 lines · Julien Grall
Quoted 4 lines

          if ( !list_empty(&p->inflight) && !test_bit(GIC_IRQ_GUEST_VISIBLE, &p->status) ) -            gic_raise_guest_irq(v, p->irq, p->lpi_priority); +            gic_raise_guest_irq(v, p);

The interface is not behaving the same way now. I understand that nobody is using the 3 parameters but that's actually a bug with the ITS.

Because the LPI will not be set with expected priority.

But it is not the issue of the interface, you know. Keeping in mind that both an inflight and an lr_pending queues are sorted by p->priority, the field p->lpi_priority looks odd anyway.

Quoted 9 lines
Quoted 7 lines
xen/include/asm-arm/gic.h +0 -3
-extern void gic_raise_guest_irq(struct vcpu *v, unsigned int irq,
-        unsigned int priority);
-extern void gic_raise_inflight_irq(struct vcpu *v, unsigned int 

virtual_irq);

(quoted diff) +2 -0
+extern void gic_raise_guest_irq(struct vcpu *v, struct pending_irq *p);
+extern void gic_raise_inflight_irq(struct vcpu *v, struct 

pending_irq *n);

That code is not going to compile with the new vGIC as pending_irq only exists for the current vGIC.

Yep, I did miss that.

I guess wrapping it with #ifndef CONFIG_NEW_VGIC must be enough here. Those two functions are not used by, nor defined by new-vgic.

Signature · 8 lines
Sincerely,
Andrii Anisov.


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
#3 lore raw

Sorry for the formatting.

Quoted 9 lines · On Mon, 19 Nov 2018, 12:04 Andrii Anisov, <[email protected]> wrote:

Hello Julien,

Quoted 5 lines · Julien Grall
Quoted 4 lines
          if ( !list_empty(&p->inflight) &&
               !test_bit(GIC_IRQ_GUEST_VISIBLE, &p->status) )
xen/arch/arm/vgic-v3-its.c +1 -1
-            gic_raise_guest_irq(v, p->irq, p->lpi_priority);
+            gic_raise_guest_irq(v, p);
Quoted 8 lines
Quoted 4 lines

The interface is not behaving the same way now. I understand that nobody is using the 3 parameters but that's actually a bug with the ITS.

Because the LPI will not be set with expected priority.

But it is not the issue of the interface, you know. Keeping in mind that both an inflight and an lr_pending queues are sorted by p->priority, the field p->lpi_priority looks odd anyway.

You didn't get my point. You removed a parameter without explaining why it is fine. In that context, the caller for LPI is using lpi_priority.

With your change, it is becoming issue with the interface because it may not pass the correct things.

So this not a cleanup and there must be a reason why lpi_priority was passed instead of priority.

Quoted 23 lines
Quoted 10 lines
Quoted 7 lines
xen/include/asm-arm/gic.h +0 -3
-extern void gic_raise_guest_irq(struct vcpu *v, unsigned int irq,
-        unsigned int priority);
-extern void gic_raise_inflight_irq(struct vcpu *v, unsigned int

virtual_irq);

(quoted diff) +2 -0
+extern void gic_raise_guest_irq(struct vcpu *v, struct pending_irq *p);
+extern void gic_raise_inflight_irq(struct vcpu *v, struct

pending_irq *n);

That code is not going to compile with the new vGIC as pending_irq only exists for the current vGIC.

Yep, I did miss that.

I guess wrapping it with #ifndef CONFIG_NEW_VGIC must be enough here. Those two functions are not used by, nor defined by new-vgic.

--
Sincerely,
Andrii Anisov.

Quoted 4 lines

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

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

#4 lore raw
Quoted 1 line · Julien Grall

You didn't get my point. You removed a parameter without explaining why it is fine.

It is a pure optimization. If you look through the code, you can see that callers of these functions already have a correspondent struct pending_irq pointer. But they pass irq number (and priority) to gic_raise_guest_irq/gic_raise_inflight_irq so that they should calculate the pointer again, what is suboptimal.

Quoted 1 line

In that context, the caller for LPI is using lpi_priority.

What in its turn is not taken into consideration by the current gic_raise_guest_irq function implementation. Even clearly ignored, because the priority abstraction is only needed by gic_add_to_lr_pending to insert a pending irq into lr_queue, but it does ordering by p->priority only.

Quoted 1 line

With your change, it is becoming issue with the interface because it may not pass the correct things.

Now the interface of gic_raise_guest_irq function corresponds to its implementation in addition to optimization.

Quoted 1 line

So this not a cleanup and there must be a reason why lpi_priority was passed instead of priority.

Moreover, if one would like or plan to honor lpi_priority in gic_raise_guest_irq, he can freely extract it from a pending_irq structure. It is still there.

Signature · 8 lines
Sincerely,
Andrii Anisov.


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
#5 lore raw
In reply to Andrii Anisov

Hello Andre,

I'm going to change "gic_raise_guest_irq()" function interface.

Could you please comment my understanding of vgic-v3-its.c code below? So that I could fix it alongside the function interface change.

xen/arch/arm/vgic-v3-its.c static void update_lpi_vgic_status(struct vcpu *v, struct pending_irq *p) · line 449

Quoted 8 lines · Andrii Anisov
xen/arch/arm/vgic-v3-its.c +0 -0
index 5b73c4e..193a28f 100644
@@ -447,7 +447,7 @@ static void update_lpi_vgic_status(struct vcpu *v, struct pending_irq *p)
      {
          if ( !list_empty(&p->inflight) &&
               !test_bit(GIC_IRQ_GUEST_VISIBLE, &p->status) )

As I understand, the intention here is to reinsert an irq to lr_pending queue with an updated priority, just in case the irq is not yet visible to guests. You try to pass a p->lpi_priority to the gic_raise_guest_irq as a new priority. But, with the current implementation, that parameter is clearly ignored by the function. Moreover, it just could not be honored there, because gic_raise_guest_irq touches only lr_pending queue, while both lr_pending and inflight_irqs are sorted by p->priority and you can't change it while updating only one queue.

I guess here the irq should be removed from both queues first, then p->priority updated to p->lpi_priority, then irq should be reinserted to both queues.

What do you think about that?

xen/arch/arm/vgic-v3-its.c static void update_lpi_vgic_status(struct vcpu *v, struct pending_irq *p) · line 452

Quoted 4 lines
xen/arch/arm/vgic-v3-its.c +1 -1
-            gic_raise_guest_irq(v, p->irq, p->lpi_priority);
+            gic_raise_guest_irq(v, p);
      }
      else

The call below looks excessive to me. We can reach this branch in case p->inflight is empty or it is not empty, but irq is visible to guest. In both those cases irq can not be on lr_pending queue, so calling this function is just a wasting cpu cycles.

Quoted 1 line
xen/arch/arm/vgic-v3-its.c +0 -0
          gic_remove_from_lr_pending(v, p);
Signature · 8 lines
Sincerely,
Andrii Anisov.


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
#6 lore raw

Andre,

Could you please comment on the patch and below.

Quoted 30 lines · Andrii Anisov

Hello Andre,

I'm going to change "gic_raise_guest_irq()" function interface.

Could you please comment my understanding of vgic-v3-its.c code below? So that I could fix it alongside the function interface change.

Quoted 8 lines · Andrii Anisov
xen/arch/arm/vgic-v3-its.c +0 -0
index 5b73c4e..193a28f 100644
@@ -447,7 +447,7 @@ static void update_lpi_vgic_status(struct vcpu *v, struct pending_irq *p)
       {

          if ( !list_empty(&p->inflight) && !test_bit(GIC_IRQ_GUEST_VISIBLE, &p->status) )

As I understand, the intention here is to reinsert an irq to lr_pending queue with an updated priority, just in case the irq is not yet visible to guests. You try to pass a p->lpi_priority to the gic_raise_guest_irq as a new priority. But, with the current implementation, that parameter is clearly ignored by the function. Moreover, it just could not be honored there, because gic_raise_guest_irq touches only lr_pending queue, while both lr_pending and inflight_irqs are sorted by p->priority and you can't change it while updating only one queue.

I guess here the irq should be removed from both queues first, then p->priority updated to p->lpi_priority, then irq should be reinserted to both queues.

What do you think about that?

Quoted 4 lines

-            gic_raise_guest_irq(v, p->irq, p->lpi_priority); +            gic_raise_guest_irq(v, p);
      }
      else

The call below looks excessive to me. We can reach this branch in case p->inflight is empty or it is not empty, but irq is visible to guest. In both those cases irq can not be on lr_pending queue, so calling this function is just a wasting cpu cycles.

Quoted 1 line

          gic_remove_from_lr_pending(v, p);

Signature · 7 lines
Sincerely,
Andrii Anisov.

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