Skip to contentSkip to conversation

accel/kvm: return early from kvm_irqchip_create if kvm does not support irqchip

PATCH Superseded Ani Sinha 3 participants 6 messages Apr 13 KVM

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Ani Sinha

author
#0 lore raw

PATCH v2 1/1 accel/kvm: return early from kvm_irqchip_create if kvm does not support irqchip

During refactoring of kvm_irqchip_create(), the refactored code was returning early from do_kvm_irqchip_create() function if the required capabilities were not present in KVM. This was not translating to an early return from kvm_irqchip_create() as was the case before refactoring. This is because, do_kvm_irqchip_create() did not have a means to notify the caller of the lack of kvm capabilities. Fix this by making do_notify_irqchip_create() return EOPNOTSUPP error when required capabilities are absent and then the caller can check the return code and return early.

Converted some calls to exit() to assertion checks in order to make it cleaner.

Fixes 98884e0cc1 ("accel/kvm: add changes required to support KVM VM file descriptor change")
---
 accel/kvm/kvm-all.c | 29 ++++++++++++++++-------------
 1 file changed, 16 insertions(+), 13 deletions(-)

changelog:
v2: converted some exit() calls to assertions.

accel/kvm/kvm-all.c +16 -13
index 774499d34f..c78bf6a56f 100644
@@ -2575,7 +2575,7 @@ void kvm_irqchip_set_qemuirq_gsi(KVMState *s, qemu_irq irq, int gsi)
     g_hash_table_insert(s->gsimap, irq, GINT_TO_POINTER(gsi));
 }
 
-static void do_kvm_irqchip_create(KVMState *s)
+static int do_kvm_irqchip_create(KVMState *s)
 {
     int ret;
     if (kvm_check_extension(s, KVM_CAP_IRQCHIP)) {
@@ -2587,36 +2587,38 @@ static void do_kvm_irqchip_create(KVMState *s)
             exit(1);
         }
     } else {
-        return;
+        /*
+         * neither KVM_CAP_IRQCHIP nor KVM_CAP_S390_IRQCHIP capabilities are
+         * present. Bail.
+         */
+        return -EOPNOTSUPP;
     }
 
-    if (kvm_check_extension(s, KVM_CAP_IRQFD) <= 0) {
-        fprintf(stderr, "kvm: irqfd not implemented\n");
-        exit(1);
-    }
+    assert(kvm_check_extension(s, KVM_CAP_IRQFD));
 
     /* First probe and see if there's a arch-specific hook to create the
      * in-kernel irqchip for us */
     ret = kvm_arch_irqchip_create(s);
     if (ret == 0) {
-        if (s->kernel_irqchip_split == ON_OFF_AUTO_ON) {
-            error_report("Split IRQ chip mode not supported.");
-            exit(1);
-        } else {
-            ret = kvm_vm_ioctl(s, KVM_CREATE_IRQCHIP);
-        }
+        /* assert that split IRQ chip mode is supported. */
+        assert(s->kernel_irqchip_split != ON_OFF_AUTO_ON);
+        ret = kvm_vm_ioctl(s, KVM_CREATE_IRQCHIP);
     }
     if (ret < 0) {
         fprintf(stderr, "Create kernel irqchip failed: %s\n", strerror(-ret));
         exit(1);
     }
+
+    return 0;
 }
 
 static void kvm_irqchip_create(KVMState *s)
 {
     assert(s->kernel_irqchip_split != ON_OFF_AUTO_AUTO);
 
-    do_kvm_irqchip_create(s);
+    if (do_kvm_irqchip_create(s) < 0) {
+        return;
+    }
     kvm_kernel_irqchip = true;
     /* If we have an in-kernel IRQ chip then we must have asynchronous
      * interrupt delivery (though the reverse is not necessarily true)
@@ -2835,6 +2837,7 @@ static int kvm_reset_vmfd(MachineState *ms)
     }
 
     if (s->kernel_irqchip_allowed) {
+        /* ignore return from this function */
         do_kvm_irqchip_create(s);
     }
 
Signature · 1 line
2.49.0
#1 lore raw
Quoted 44 lines · Ani Sinha

During refactoring of kvm_irqchip_create(), the refactored code was returning early from do_kvm_irqchip_create() function if the required capabilities were not present in KVM. This was not translating to an early return from kvm_irqchip_create() as was the case before refactoring. This is because, do_kvm_irqchip_create() did not have a means to notify the caller of the lack of kvm capabilities. Fix this by making do_notify_irqchip_create() return EOPNOTSUPP error when required capabilities are absent and then the caller can check the return code and return early.

Converted some calls to exit() to assertion checks in order to make it cleaner.

Fixes 98884e0cc1 ("accel/kvm: add changes required to support KVM VM file descriptor change")
---
  accel/kvm/kvm-all.c | 29 ++++++++++++++++-------------
  1 file changed, 16 insertions(+), 13 deletions(-)

changelog:
v2: converted some exit() calls to assertions.

accel/kvm/kvm-all.c +3 -2
index 774499d34f..c78bf6a56f 100644
@@ -2575,7 +2575,7 @@ void kvm_irqchip_set_qemuirq_gsi(KVMState *s, qemu_irq irq, int gsi)
      g_hash_table_insert(s->gsimap, irq, GINT_TO_POINTER(gsi));
  }
 
-static void do_kvm_irqchip_create(KVMState *s)
+static int do_kvm_irqchip_create(KVMState *s)
  {
      int ret;
      if (kvm_check_extension(s, KVM_CAP_IRQCHIP)) {
@@ -2587,36 +2587,38 @@ static void do_kvm_irqchip_create(KVMState *s)
              exit(1);
          }
      } else {
-        return;
+        /*
+         * neither KVM_CAP_IRQCHIP nor KVM_CAP_S390_IRQCHIP capabilities are

accel/kvm/kvm-all.c static void do_kvm_irqchip_create(KVMState *s) · line 2597

Quoted 10 lines
accel/kvm/kvm-all.c +4 -4
+         * present. Bail.
+         */
+        return -EOPNOTSUPP;
      }
 
-    if (kvm_check_extension(s, KVM_CAP_IRQFD) <= 0) {
-        fprintf(stderr, "kvm: irqfd not implemented\n");
-        exit(1);
-    }
+    assert(kvm_check_extension(s, KVM_CAP_IRQFD));

again changing behaviour ?
previously it was doing exit(1) on returning non-zero. Now, it will assert on returning zero ?
May be split the patch to keep the exit -> assert changes separate ?

Quoted 42 lines
accel/kvm/kvm-all.c +9 -7
      /* First probe and see if there's a arch-specific hook to create the
       * in-kernel irqchip for us */
      ret = kvm_arch_irqchip_create(s);
      if (ret == 0) {
-        if (s->kernel_irqchip_split == ON_OFF_AUTO_ON) {
-            error_report("Split IRQ chip mode not supported.");
-            exit(1);
-        } else {
-            ret = kvm_vm_ioctl(s, KVM_CREATE_IRQCHIP);
-        }
+        /* assert that split IRQ chip mode is supported. */
+        assert(s->kernel_irqchip_split != ON_OFF_AUTO_ON);
+        ret = kvm_vm_ioctl(s, KVM_CREATE_IRQCHIP);
      }
      if (ret < 0) {
          fprintf(stderr, "Create kernel irqchip failed: %s\n", strerror(-ret));
          exit(1);
      }
+
+    return 0;
  }
 
  static void kvm_irqchip_create(KVMState *s)
  {
      assert(s->kernel_irqchip_split != ON_OFF_AUTO_AUTO);
 
-    do_kvm_irqchip_create(s);
+    if (do_kvm_irqchip_create(s) < 0) {
+        return;
+    }
      kvm_kernel_irqchip = true;
      /* If we have an in-kernel IRQ chip then we must have asynchronous
       * interrupt delivery (though the reverse is not necessarily true)
@@ -2835,6 +2837,7 @@ static int kvm_reset_vmfd(MachineState *ms)
      }
 
      if (s->kernel_irqchip_allowed) {
+        /* ignore return from this function */
          do_kvm_irqchip_create(s);
      }
 
#2 lore raw
Quoted 53 lines · Harsh Prateek Bora
Quoted 49 lines · Ani Sinha

During refactoring of kvm_irqchip_create(), the refactored code was returning early from do_kvm_irqchip_create() function if the required capabilities were not present in KVM. This was not translating to an early return from kvm_irqchip_create() as was the case before refactoring. This is because, do_kvm_irqchip_create() did not have a means to notify the caller of the lack of kvm capabilities. Fix this by making do_notify_irqchip_create() return EOPNOTSUPP error when required capabilities are absent and then the caller can check the return code and return early.

Converted some calls to exit() to assertion checks in order to make it cleaner.

Fixes 98884e0cc1 ("accel/kvm: add changes required to support KVM VM file descriptor change")
---
  accel/kvm/kvm-all.c | 29 ++++++++++++++++-------------
  1 file changed, 16 insertions(+), 13 deletions(-)

changelog:
v2: converted some exit() calls to assertions.

accel/kvm/kvm-all.c +6 -2
index 774499d34f..c78bf6a56f 100644
@@ -2575,7 +2575,7 @@ void kvm_irqchip_set_qemuirq_gsi(KVMState *s, qemu_irq irq, int gsi)
      g_hash_table_insert(s->gsimap, irq, GINT_TO_POINTER(gsi));
  }
 
-static void do_kvm_irqchip_create(KVMState *s)
+static int do_kvm_irqchip_create(KVMState *s)
  {
      int ret;
      if (kvm_check_extension(s, KVM_CAP_IRQCHIP)) {
@@ -2587,36 +2587,38 @@ static void do_kvm_irqchip_create(KVMState *s)
              exit(1);
          }
      } else {
-        return;
+        /*
+         * neither KVM_CAP_IRQCHIP nor KVM_CAP_S390_IRQCHIP capabilities are
+         * present. Bail.
+         */
+        return -EOPNOTSUPP;
      }
 
Quoted 10 lines

accel/kvm/kvm-all.c static void do_kvm_irqchip_create(KVMState *s) · line 2597

Quoted 5 lines
accel/kvm/kvm-all.c +1 -4
-    if (kvm_check_extension(s, KVM_CAP_IRQFD) <= 0) {
-        fprintf(stderr, "kvm: irqfd not implemented\n");
-        exit(1);
-    }
+    assert(kvm_check_extension(s, KVM_CAP_IRQFD));

again changing behaviour ?
previously it was doing exit(1) on returning non-zero. Now, it will assert on returning zero ?
May be split the patch to keep the exit -> assert changes separate ?

I think for 11.0 it's probably best to do the minimum necessary change to fix the logic error introduced by 98884e0cc1. For that I think the v1 of this patch is OK.

There do seem to be some cleanup opportunities that we have here, but we can do those in 11.1.

-- PMM

author
#3 lore raw
Quoted 69 lines · Peter Maydell
Quoted 53 lines · Harsh Prateek Bora
Quoted 49 lines · Ani Sinha

During refactoring of kvm_irqchip_create(), the refactored code was returning early from do_kvm_irqchip_create() function if the required capabilities were not present in KVM. This was not translating to an early return from kvm_irqchip_create() as was the case before refactoring. This is because, do_kvm_irqchip_create() did not have a means to notify the caller of the lack of kvm capabilities. Fix this by making do_notify_irqchip_create() return EOPNOTSUPP error when required capabilities are absent and then the caller can check the return code and return early.

Converted some calls to exit() to assertion checks in order to make it cleaner.

Fixes 98884e0cc1 ("accel/kvm: add changes required to support KVM VM file descriptor change")
---
 accel/kvm/kvm-all.c | 29 ++++++++++++++++-------------
 1 file changed, 16 insertions(+), 13 deletions(-)

changelog:
v2: converted some exit() calls to assertions.

accel/kvm/kvm-all.c +6 -2
index 774499d34f..c78bf6a56f 100644
@@ -2575,7 +2575,7 @@ void kvm_irqchip_set_qemuirq_gsi(KVMState *s, qemu_irq irq, int gsi)
     g_hash_table_insert(s->gsimap, irq, GINT_TO_POINTER(gsi));
 }
 
-static void do_kvm_irqchip_create(KVMState *s)
+static int do_kvm_irqchip_create(KVMState *s)
 {
     int ret;
     if (kvm_check_extension(s, KVM_CAP_IRQCHIP)) {
@@ -2587,36 +2587,38 @@ static void do_kvm_irqchip_create(KVMState *s)
             exit(1);
         }
     } else {
-        return;
+        /*
+         * neither KVM_CAP_IRQCHIP nor KVM_CAP_S390_IRQCHIP capabilities are
+         * present. Bail.
+         */
+        return -EOPNOTSUPP;
     }
 
Quoted 10 lines

accel/kvm/kvm-all.c static void do_kvm_irqchip_create(KVMState *s) · line 2597

Quoted 5 lines
accel/kvm/kvm-all.c +1 -4
-    if (kvm_check_extension(s, KVM_CAP_IRQFD) <= 0) {
-        fprintf(stderr, "kvm: irqfd not implemented\n");
-        exit(1);
-    }
+    assert(kvm_check_extension(s, KVM_CAP_IRQFD));

again changing behaviour ?
previously it was doing exit(1) on returning non-zero. Now, it will assert on returning zero ?
May be split the patch to keep the exit -> assert changes separate ?

Quoted 3 lines

I think for 11.0 it's probably best to do the minimum necessary change to fix the logic error introduced by 98884e0cc1. For that I think the v1 of this patch is OK.

Agreed.

Quoted 3 lines

There do seem to be some cleanup opportunities that we have here, but we can do those in 11.1.

cool.

Quoted 2 lines

-- PMM

author
#4 lore raw
In reply to Harsh Prateek Bora
Quoted 50 lines · Harsh Prateek Bora
Quoted 43 lines · Ani Sinha

During refactoring of kvm_irqchip_create(), the refactored code was returning early from do_kvm_irqchip_create() function if the required capabilities were not present in KVM. This was not translating to an early return from kvm_irqchip_create() as was the case before refactoring. This is because, do_kvm_irqchip_create() did not have a means to notify the caller of the lack of kvm capabilities. Fix this by making do_notify_irqchip_create() return EOPNOTSUPP error when required capabilities are absent and then the caller can check the return code and return early.
Converted some calls to exit() to assertion checks in order to make it cleaner.

Fixes 98884e0cc1 ("accel/kvm: add changes required to support KVM VM file descriptor change")
---
 accel/kvm/kvm-all.c | 29 ++++++++++++++++-------------
 1 file changed, 16 insertions(+), 13 deletions(-)

changelog:
v2: converted some exit() calls to assertions.

accel/kvm/kvm-all.c +6 -1
index 774499d34f..c78bf6a56f 100644
@@ -2575,7 +2575,7 @@ void kvm_irqchip_set_qemuirq_gsi(KVMState *s, qemu_irq irq, int gsi)
     g_hash_table_insert(s->gsimap, irq, GINT_TO_POINTER(gsi));
 }
 -static void do_kvm_irqchip_create(KVMState *s)
+static int do_kvm_irqchip_create(KVMState *s)
 {
     int ret;
     if (kvm_check_extension(s, KVM_CAP_IRQCHIP)) {
@@ -2587,36 +2587,38 @@ static void do_kvm_irqchip_create(KVMState *s)
             exit(1);
         }
     } else {
-        return;
+        /*
+         * neither KVM_CAP_IRQCHIP nor KVM_CAP_S390_IRQCHIP capabilities are
+         * present. Bail.
+         */
+        return -EOPNOTSUPP;
     }
Quoted 8 lines

accel/kvm/kvm-all.c static void do_kvm_irqchip_create(KVMState *s) · line 2597

Quoted 5 lines
accel/kvm/kvm-all.c +1 -3
 -    if (kvm_check_extension(s, KVM_CAP_IRQFD) <= 0) {
-        fprintf(stderr, "kvm: irqfd not implemented\n");
-        exit(1);
-    }
+    assert(kvm_check_extension(s, KVM_CAP_IRQFD));

again changing behaviour ?
previously it was doing exit(1) on returning non-zero.

Non-zero is the success case. It was doing exit() for returning <=0 case

Quoted 45 lines

if (kvm_check_extension(s, KVM_CAP_IRQFD) <= 0)

Now, it will assert on returning zero ?
May be split the patch to keep the exit -> assert changes separate ?

Quoted 38 lines
accel/kvm/kvm-all.c +9 -6
       /* First probe and see if there's a arch-specific hook to create the
      * in-kernel irqchip for us */
     ret = kvm_arch_irqchip_create(s);
     if (ret == 0) {
-        if (s->kernel_irqchip_split == ON_OFF_AUTO_ON) {
-            error_report("Split IRQ chip mode not supported.");
-            exit(1);
-        } else {
-            ret = kvm_vm_ioctl(s, KVM_CREATE_IRQCHIP);
-        }
+        /* assert that split IRQ chip mode is supported. */
+        assert(s->kernel_irqchip_split != ON_OFF_AUTO_ON);
+        ret = kvm_vm_ioctl(s, KVM_CREATE_IRQCHIP);
     }
     if (ret < 0) {
         fprintf(stderr, "Create kernel irqchip failed: %s\n", strerror(-ret));
         exit(1);
     }
+
+    return 0;
 }
   static void kvm_irqchip_create(KVMState *s)
 {
     assert(s->kernel_irqchip_split != ON_OFF_AUTO_AUTO);
 -    do_kvm_irqchip_create(s);
+    if (do_kvm_irqchip_create(s) < 0) {
+        return;
+    }
     kvm_kernel_irqchip = true;
     /* If we have an in-kernel IRQ chip then we must have asynchronous
      * interrupt delivery (though the reverse is not necessarily true)
@@ -2835,6 +2837,7 @@ static int kvm_reset_vmfd(MachineState *ms)
     }
       if (s->kernel_irqchip_allowed) {
+        /* ignore return from this function */
         do_kvm_irqchip_create(s);
     }
 
author
#5 lore raw
Quoted 63 lines · Ani Sinha
Quoted 51 lines · Harsh Prateek Bora
Quoted 43 lines · Ani Sinha

During refactoring of kvm_irqchip_create(), the refactored code was returning early from do_kvm_irqchip_create() function if the required capabilities were not present in KVM. This was not translating to an early return from kvm_irqchip_create() as was the case before refactoring. This is because, do_kvm_irqchip_create() did not have a means to notify the caller of the lack of kvm capabilities. Fix this by making do_notify_irqchip_create() return EOPNOTSUPP error when required capabilities are absent and then the caller can check the return code and return early.
Converted some calls to exit() to assertion checks in order to make it cleaner.

Fixes 98884e0cc1 ("accel/kvm: add changes required to support KVM VM file descriptor change")

---
accel/kvm/kvm-all.c | 29 ++++++++++++++++-------------

1 file changed, 16 insertions(+), 13 deletions(-)

changelog:
v2: converted some exit() calls to assertions.

accel/kvm/kvm-all.c +6 -2
index 774499d34f..c78bf6a56f 100644
@@ -2575,7 +2575,7 @@ void kvm_irqchip_set_qemuirq_gsi(KVMState *s, qemu_irq irq, int gsi)
    g_hash_table_insert(s->gsimap, irq, GINT_TO_POINTER(gsi));
 }
-static void do_kvm_irqchip_create(KVMState *s)
+static int do_kvm_irqchip_create(KVMState *s)
 {
    int ret;
    if (kvm_check_extension(s, KVM_CAP_IRQCHIP)) {
@@ -2587,36 +2587,38 @@ static void do_kvm_irqchip_create(KVMState *s)
            exit(1);
        }
    } else {
-        return;
+        /*
+         * neither KVM_CAP_IRQCHIP nor KVM_CAP_S390_IRQCHIP capabilities are
+         * present. Bail.
+         */
+        return -EOPNOTSUPP;
    }
Quoted 8 lines

accel/kvm/kvm-all.c static void do_kvm_irqchip_create(KVMState *s) · line 2597

Quoted 5 lines
accel/kvm/kvm-all.c +1 -4
-    if (kvm_check_extension(s, KVM_CAP_IRQFD) <= 0) {
-        fprintf(stderr, "kvm: irqfd not implemented\n");
-        exit(1);
-    }
+    assert(kvm_check_extension(s, KVM_CAP_IRQFD));

again changing behaviour ?
previously it was doing exit(1) on returning non-zero.

Quoted 3 lines

Non-zero is the success case. It was doing exit() for returning <=0 case

Quoted 1 line

if (kvm_check_extension(s, KVM_CAP_IRQFD) <= 0)

Actually maybe I should make this clearer

int kvm_check_extension(KVMState *s, unsaigned int extension)

{                                                                                                                                    
    int ret;                                                                                                                         
                                                                                                                                     
    ret = kvm_ioctl(s, KVM_CHECK_EXTENSION, extension);                                                                              
    if (ret < 0) {                                                                                                                   
        ret = 0;                                                                                                                     
    }                                                                                                                                
                                                                                                                                     
    return ret;                                                                                                                      
}                       

So non-zero positive value is success. 0 is unsupported. Also see https://docs.kernel.org/virt/kvm/api.html#kvm-check-extension
So asserting on non-zero is fine.

Quoted 42 lines
Quoted 42 lines

Now, it will assert on returning zero ?
May be split the patch to keep the exit -> assert changes separate ?

Quoted 37 lines
accel/kvm/kvm-all.c +9 -7
      /* First probe and see if there's a arch-specific hook to create the
     * in-kernel irqchip for us */
    ret = kvm_arch_irqchip_create(s);
    if (ret == 0) {
-        if (s->kernel_irqchip_split == ON_OFF_AUTO_ON) {
-            error_report("Split IRQ chip mode not supported.");
-            exit(1);
-        } else {
-            ret = kvm_vm_ioctl(s, KVM_CREATE_IRQCHIP);
-        }
+        /* assert that split IRQ chip mode is supported. */
+        assert(s->kernel_irqchip_split != ON_OFF_AUTO_ON);
+        ret = kvm_vm_ioctl(s, KVM_CREATE_IRQCHIP);
    }
    if (ret < 0) {
        fprintf(stderr, "Create kernel irqchip failed: %s\n", strerror(-ret));
        exit(1);
    }
+
+    return 0;
 }
  static void kvm_irqchip_create(KVMState *s)
 {
    assert(s->kernel_irqchip_split != ON_OFF_AUTO_AUTO);
-    do_kvm_irqchip_create(s);
+    if (do_kvm_irqchip_create(s) < 0) {
+        return;
+    }
    kvm_kernel_irqchip = true;
    /* If we have an in-kernel IRQ chip then we must have asynchronous
     * interrupt delivery (though the reverse is not necessarily true)
@@ -2835,6 +2837,7 @@ static int kvm_reset_vmfd(MachineState *ms)
    }
      if (s->kernel_irqchip_allowed) {
+        /* ignore return from this function */
        do_kvm_irqchip_create(s);
    }