Skip to contentSkip to conversation

KVM: Fix GSI number space limit

PATCH Applied Alexander Graf 3 participants 8 messages Jun 6, 2014 – Jun 7, 2014 KVM

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 KVM: Fix GSI number space limit

KVM tells us the number of GSIs it can handle inside the kernel. That value is basically KVM_MAX_IRQ_ROUTES. However when we try to set the GSI mapping table, it checks for

    r = -EINVAL;
    if (routing.nr >= KVM_MAX_IRQ_ROUTES)
        goto out;

erroring out even when we're only using all of the GSIs. To make sure we never hit that limit, let's reduce the number of GSIs we get from KVM by one.

---
 kvm-all.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
kvm-all.c +1 -1
index 4e19eff..56a251b 100644
@@ -938,7 +938,7 @@ void kvm_init_irq_routing(KVMState *s)
 {
     int gsi_count, i;
 
-    gsi_count = kvm_check_extension(s, KVM_CAP_IRQ_ROUTING);
+    gsi_count = kvm_check_extension(s, KVM_CAP_IRQ_ROUTING) - 1;
     if (gsi_count > 0) {
         unsigned int gsi_bits, i;
 
Signature · 1 line
1.7.12.4
#1 lore raw
Quoted 21 lines · On Fri, 6 Jun 2014 14:46:05 +0200 Alexander Graf <[email protected]> wrote:

KVM tells us the number of GSIs it can handle inside the kernel. That value is basically KVM_MAX_IRQ_ROUTES. However when we try to set the GSI mapping table, it checks for

    r = -EINVAL;
    if (routing.nr >= KVM_MAX_IRQ_ROUTES)
        goto out;

erroring out even when we're only using all of the GSIs. To make sure we never hit that limit, let's reduce the number of GSIs we get from KVM by one.

---
 kvm-all.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
kvm-all.c +0 -0
index 4e19eff..56a251b 100644

kvm-all.c void kvm_init_irq_routing(KVMState *s) · line 944

Quoted 8 lines
kvm-all.c +1 -1
@@ -938,7 +938,7 @@ void kvm_init_irq_routing(KVMState *s)
 {
     int gsi_count, i;
 
-    gsi_count = kvm_check_extension(s, KVM_CAP_IRQ_ROUTING);
+    gsi_count = kvm_check_extension(s, KVM_CAP_IRQ_ROUTING) - 1;
     if (gsi_count > 0) {
         unsigned int gsi_bits, i;
 

But gsi_count is already marked as used further down in this function, isn't it? Confused.

#2 lore raw
Quoted 27 lines · Cornelia Huck
Quoted 24 lines · On Fri, 6 Jun 2014 14:46:05 +0200 Alexander Graf <[email protected]> wrote:

KVM tells us the number of GSIs it can handle inside the kernel. That value is basically KVM_MAX_IRQ_ROUTES. However when we try to set the GSI mapping table, it checks for

     r = -EINVAL;
     if (routing.nr >= KVM_MAX_IRQ_ROUTES)
         goto out;

erroring out even when we're only using all of the GSIs. To make sure we never hit that limit, let's reduce the number of GSIs we get from KVM by one.

---
  kvm-all.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
kvm-all.c +0 -0
index 4e19eff..56a251b 100644
@@ -938,7 +938,7 @@ void kvm_init_irq_routing(KVMState *s)
  {
      int gsi_count, i;
 
Quoted 7 lines

kvm-all.c void kvm_init_irq_routing(KVMState *s) · line 944

Quoted 5 lines
kvm-all.c +1 -1
-    gsi_count = kvm_check_extension(s, KVM_CAP_IRQ_ROUTING);
+    gsi_count = kvm_check_extension(s, KVM_CAP_IRQ_ROUTING) - 1;
      if (gsi_count > 0) {
          unsigned int gsi_bits, i;
 

But gsi_count is already marked as used further down in this function, isn't it? Confused.

gsi_bits = ALIGN(gsi_count, 32);
[...]

        for (i = gsi_count; i < gsi_bits; i++) {
            set_gsi(s, i);
        }

So if you take gsi_count = 1024, what happens?

  gsi_count = 1024;
  gsi_bits = 1024;
  for (i = 1024; i < 1024; i++) {
            set_gsi(s, i);
  }

At least in my world of C that loop never runs, no?

Alex

#3 lore raw
Quoted 45 lines · On Fri, 06 Jun 2014 15:15:54 +0200 Alexander Graf <[email protected]> wrote:
Quoted 27 lines · Cornelia Huck
Quoted 24 lines · On Fri, 6 Jun 2014 14:46:05 +0200 Alexander Graf <[email protected]> wrote:

KVM tells us the number of GSIs it can handle inside the kernel. That value is basically KVM_MAX_IRQ_ROUTES. However when we try to set the GSI mapping table, it checks for

     r = -EINVAL;
     if (routing.nr >= KVM_MAX_IRQ_ROUTES)
         goto out;

erroring out even when we're only using all of the GSIs. To make sure we never hit that limit, let's reduce the number of GSIs we get from KVM by one.

---
  kvm-all.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
kvm-all.c +0 -0
index 4e19eff..56a251b 100644
@@ -938,7 +938,7 @@ void kvm_init_irq_routing(KVMState *s)
  {
      int gsi_count, i;
 
Quoted 7 lines

kvm-all.c void kvm_init_irq_routing(KVMState *s) · line 944

Quoted 5 lines
kvm-all.c +1 -1
-    gsi_count = kvm_check_extension(s, KVM_CAP_IRQ_ROUTING);
+    gsi_count = kvm_check_extension(s, KVM_CAP_IRQ_ROUTING) - 1;
      if (gsi_count > 0) {
          unsigned int gsi_bits, i;
 

But gsi_count is already marked as used further down in this function, isn't it? Confused.

   gsi_bits = ALIGN(gsi_count, 32);
[...]
         for (i = gsi_count; i < gsi_bits; i++) {
             set_gsi(s, i);
         }

So if you take gsi_count = 1024, what happens?

Quoted 8 lines
   gsi_count = 1024;
   gsi_bits = 1024;
   for (i = 1024; i < 1024; i++) {
             set_gsi(s, i);
   }

At least in my world of C that loop never runs, no?

But then kvm_irqchip_get_virq() should never return 1024, shouldn't it?

And:

void kvm_irqchip_add_irq_route(KVMState *s, int irq, int irqchip, int pin) {
[...]

    assert(pin < s->gsi_count);

would trigger too early with your change, wouldn't it?

#4 lore raw
Quoted 52 lines · Cornelia Huck
Quoted 49 lines · On Fri, 06 Jun 2014 15:15:54 +0200 Alexander Graf <[email protected]> wrote:
Quoted 27 lines · Cornelia Huck
Quoted 24 lines · On Fri, 6 Jun 2014 14:46:05 +0200 Alexander Graf <[email protected]> wrote:

KVM tells us the number of GSIs it can handle inside the kernel. That value is basically KVM_MAX_IRQ_ROUTES. However when we try to set the GSI mapping table, it checks for

      r = -EINVAL;
      if (routing.nr >= KVM_MAX_IRQ_ROUTES)
          goto out;

erroring out even when we're only using all of the GSIs. To make sure we never hit that limit, let's reduce the number of GSIs we get from KVM by one.

---
   kvm-all.c | 2 +-
   1 file changed, 1 insertion(+), 1 deletion(-)
kvm-all.c +0 -0
index 4e19eff..56a251b 100644
@@ -938,7 +938,7 @@ void kvm_init_irq_routing(KVMState *s)
   {
       int gsi_count, i;
 
Quoted 7 lines

kvm-all.c void kvm_init_irq_routing(KVMState *s) · line 944

Quoted 5 lines
kvm-all.c +1 -1
-    gsi_count = kvm_check_extension(s, KVM_CAP_IRQ_ROUTING);
+    gsi_count = kvm_check_extension(s, KVM_CAP_IRQ_ROUTING) - 1;
       if (gsi_count > 0) {
           unsigned int gsi_bits, i;
 

But gsi_count is already marked as used further down in this function, isn't it? Confused.

    gsi_bits = ALIGN(gsi_count, 32);
[...]
          for (i = gsi_count; i < gsi_bits; i++) {
              set_gsi(s, i);
          }

So if you take gsi_count = 1024, what happens?

    gsi_count = 1024;
    gsi_bits = 1024;
    for (i = 1024; i < 1024; i++) {
              set_gsi(s, i);
    }
Quoted 3 lines
Quoted 2 lines

At least in my world of C that loop never runs, no?

But then kvm_irqchip_get_virq() should never return 1024, shouldn't it?

Right, because it returns the virq number which starts at 0. However, to describe all virqs from [0..1023] we need 1024 entries which the kernel errors out on.

Quoted 9 lines

And:

void kvm_irqchip_add_irq_route(KVMState *s, int irq, int irqchip, int pin) {
[...]

     assert(pin < s->gsi_count);

would trigger too early with your change, wouldn't it?

Not really - with my change we only support 1023 virqs. So the biggest virq number is 1022 which is < 1023 :).

Sorry for describing this with actual numbers - I find it easier to grasp when I think in concrete numbers here - this stuff is just really spinning my head :).

Alex

Acked-by
#5 lore raw
Quoted 54 lines · On Fri, 06 Jun 2014 15:28:13 +0200 Alexander Graf <[email protected]> wrote:
Quoted 52 lines · Cornelia Huck
Quoted 49 lines · On Fri, 06 Jun 2014 15:15:54 +0200 Alexander Graf <[email protected]> wrote:
Quoted 27 lines · Cornelia Huck
Quoted 24 lines · On Fri, 6 Jun 2014 14:46:05 +0200 Alexander Graf <[email protected]> wrote:

KVM tells us the number of GSIs it can handle inside the kernel. That value is basically KVM_MAX_IRQ_ROUTES. However when we try to set the GSI mapping table, it checks for

      r = -EINVAL;
      if (routing.nr >= KVM_MAX_IRQ_ROUTES)
          goto out;

erroring out even when we're only using all of the GSIs. To make sure we never hit that limit, let's reduce the number of GSIs we get from KVM by one.

---
   kvm-all.c | 2 +-
   1 file changed, 1 insertion(+), 1 deletion(-)
kvm-all.c +0 -0
index 4e19eff..56a251b 100644
@@ -938,7 +938,7 @@ void kvm_init_irq_routing(KVMState *s)
   {
       int gsi_count, i;
 
Quoted 7 lines

kvm-all.c void kvm_init_irq_routing(KVMState *s) · line 944

Quoted 5 lines
kvm-all.c +1 -1
-    gsi_count = kvm_check_extension(s, KVM_CAP_IRQ_ROUTING);
+    gsi_count = kvm_check_extension(s, KVM_CAP_IRQ_ROUTING) - 1;
       if (gsi_count > 0) {
           unsigned int gsi_bits, i;
 

But gsi_count is already marked as used further down in this function, isn't it? Confused.

    gsi_bits = ALIGN(gsi_count, 32);
[...]
          for (i = gsi_count; i < gsi_bits; i++) {
              set_gsi(s, i);
          }

So if you take gsi_count = 1024, what happens?

    gsi_count = 1024;
    gsi_bits = 1024;
    for (i = 1024; i < 1024; i++) {
              set_gsi(s, i);
    }
Quoted 7 lines
Quoted 3 lines
Quoted 2 lines

At least in my world of C that loop never runs, no?

But then kvm_irqchip_get_virq() should never return 1024, shouldn't it?

Right, because it returns the virq number which starts at 0. However, to describe all virqs from [0..1023] we need 1024 entries which the kernel errors out on.

Ah... that's kvm_irq_routing::nr and not kvm_irq_routing_entry::gsi, so it's basically a kernel misfeature we need to work around.

Quoted 11 lines
Quoted 10 lines

And:

void kvm_irqchip_add_irq_route(KVMState *s, int irq, int irqchip, int pin) {
[...]

     assert(pin < s->gsi_count);

would trigger too early with your change, wouldn't it?

Quoted 7 lines

Not really - with my change we only support 1023 virqs. So the biggest virq number is 1022 which is < 1023 :).

Sorry for describing this with actual numbers - I find it easier to grasp when I think in concrete numbers here - this stuff is just really spinning my head :).

And on top of that, it's Friday :)

But yes, makes sense now.

maintainer Applied
#6 lore raw
In reply to Alexander Graf

Il 06/06/2014 14:46, Alexander Graf ha scritto:

Quoted 22 lines

KVM tells us the number of GSIs it can handle inside the kernel. That value is basically KVM_MAX_IRQ_ROUTES. However when we try to set the GSI mapping table, it checks for

    r = -EINVAL;
    if (routing.nr >= KVM_MAX_IRQ_ROUTES)
        goto out;

erroring out even when we're only using all of the GSIs. To make sure we never hit that limit, let's reduce the number of GSIs we get from KVM by one.

---
 kvm-all.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
kvm-all.c +0 -0
index 4e19eff..56a251b 100644

kvm-all.c void kvm_init_irq_routing(KVMState *s) · line 944

Quoted 8 lines
kvm-all.c +1 -1
@@ -938,7 +938,7 @@ void kvm_init_irq_routing(KVMState *s)
 {
     int gsi_count, i;
 
-    gsi_count = kvm_check_extension(s, KVM_CAP_IRQ_ROUTING);
+    gsi_count = kvm_check_extension(s, KVM_CAP_IRQ_ROUTING) - 1;
     if (gsi_count > 0) {
         unsigned int gsi_bits, i;
 

Applied, thanks!

Paolo

#7 lore raw
Quoted 25 lines · Paolo Bonzini

Il 06/06/2014 14:46, Alexander Graf ha scritto:

Quoted 24 lines

KVM tells us the number of GSIs it can handle inside the kernel. That value is basically KVM_MAX_IRQ_ROUTES. However when we try to set the GSI mapping table, it checks for

    r = -EINVAL;
    if (routing.nr >= KVM_MAX_IRQ_ROUTES)
        goto out;

erroring out even when we're only using all of the GSIs. To make sure we never hit that limit, let's reduce the number of GSIs we get from KVM by one.

---
 kvm-all.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
kvm-all.c +0 -0
index 4e19eff..56a251b 100644
@@ -938,7 +938,7 @@ void kvm_init_irq_routing(KVMState *s)
 {
     int gsi_count, i;
 
Quoted 8 lines

kvm-all.c void kvm_init_irq_routing(KVMState *s) · line 944

Quoted 6 lines
kvm-all.c +1 -1
-    gsi_count = kvm_check_extension(s, KVM_CAP_IRQ_ROUTING);
+    gsi_count = kvm_check_extension(s, KVM_CAP_IRQ_ROUTING) - 1;
     if (gsi_count > 0) {
         unsigned int gsi_bits, i;
 

Applied, thanks!

Please CC this to qemu-stable when you send it out :).

Alex