Skip to contentSkip to conversation

kvm: Remove KVM_CREATE_IRQCHIP support assumption

PATCH In review Andrew Jones 5 participants 7 messages Jul 25, 2023 – Aug 10, 2023 KVM

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 kvm: Remove KVM_CREATE_IRQCHIP support assumption

Since Linux commit 00f918f61c56 ("RISC-V: KVM: Skeletal in-kernel AIA irqchip support") checking KVM_CAP_IRQCHIP returns non-zero when the RISC-V platform has AIA. The cap indicates KVM supports at least one of the following ioctls:

KVM_CREATE_IRQCHIP
KVM_IRQ_LINE
KVM_GET_IRQCHIP
KVM_SET_IRQCHIP
KVM_GET_LAPIC
KVM_SET_LAPIC

but the cap doesn't imply that KVM must support any of those ioctls in particular. However, QEMU was assuming the KVM_CREATE_IRQCHIP ioctl was supported. Stop making that assumption by introducing a KVM parameter that each architecture which supports KVM_CREATE_IRQCHIP sets. Adding parameters isn't awesome, but given how the KVM_CAP_IRQCHIP isn't very helpful on its own, we don't have a lot of options.

---

While this fixes booting guests on riscv KVM with AIA it's unlikely to get merged before the QEMU support for KVM AIA[1] lands, which would also fix the issue. I think this patch is still worth considering though since QEMU's assumption is wrong.

[1] https://lore.kernel.org/all/[email protected]/

v2:
- Move the s390x code to an s390x file. [Thomas]
- Drop the KVM_CAP_IRQCHIP check from the top of kvm_irqchip_create(), as it's no longer necessary.

 accel/kvm/kvm-all.c    | 16 ++++------------
 include/sysemu/kvm.h   |  1 +
 target/arm/kvm.c       |  3 +++
 target/i386/kvm/kvm.c  |  2 ++
 target/s390x/kvm/kvm.c | 11 +++++++++++
 5 files changed, 21 insertions(+), 12 deletions(-)
accel/kvm/kvm-all.c +4 -12
index 373d876c0580..cddcb6eca641 100644
@@ -86,6 +86,7 @@ struct KVMParkedVcpu {
 };
 
 KVMState *kvm_state;
+bool kvm_has_create_irqchip;
 bool kvm_kernel_irqchip;
 bool kvm_split_irqchip;
 bool kvm_async_interrupts_allowed;
@@ -2358,17 +2359,6 @@ static void kvm_irqchip_create(KVMState *s)
     int ret;
 
     assert(s->kernel_irqchip_split != ON_OFF_AUTO_AUTO);
-    if (kvm_check_extension(s, KVM_CAP_IRQCHIP)) {
-        ;
-    } else if (kvm_check_extension(s, KVM_CAP_S390_IRQCHIP)) {
-        ret = kvm_vm_enable_cap(s, KVM_CAP_S390_IRQCHIP, 0);
-        if (ret < 0) {
-            fprintf(stderr, "Enable kernel irqchip failed: %s\n", strerror(-ret));
-            exit(1);
-        }
-    } else {
-        return;
-    }
 
     /* First probe and see if there's a arch-specific hook to create the
      * in-kernel irqchip for us */
@@ -2377,8 +2367,10 @@ static void kvm_irqchip_create(KVMState *s)
         if (s->kernel_irqchip_split == ON_OFF_AUTO_ON) {
             error_report("Split IRQ chip mode not supported.");
             exit(1);
-        } else {
+        } else if (kvm_has_create_irqchip) {
             ret = kvm_vm_ioctl(s, KVM_CREATE_IRQCHIP);
+        } else {
+            return;
         }
     }
     if (ret < 0) {
include/sysemu/kvm.h +1 -0
index 115f0cca79d1..84b1bb3dc91e 100644
@@ -32,6 +32,7 @@
 #ifdef CONFIG_KVM_IS_POSSIBLE
 
 extern bool kvm_allowed;
+extern bool kvm_has_create_irqchip;
 extern bool kvm_kernel_irqchip;
 extern bool kvm_split_irqchip;
 extern bool kvm_async_interrupts_allowed;
target/arm/kvm.c +3 -0
index b4c7654f4980..2fa87b495d68 100644
@@ -250,6 +250,9 @@ int kvm_arm_get_max_vm_ipa_size(MachineState *ms, bool *fixed_ipa)
 int kvm_arch_init(MachineState *ms, KVMState *s)
 {
     int ret = 0;
+
+    kvm_has_create_irqchip = kvm_check_extension(s, KVM_CAP_IRQCHIP);
+
     /* For ARM interrupt delivery is always asynchronous,
      * whether we are using an in-kernel VGIC or not.
      */
target/i386/kvm/kvm.c +2 -0
index ebfaf3d24c79..6363e67f092d 100644
@@ -2771,6 +2771,8 @@ int kvm_arch_init(MachineState *ms, KVMState *s)
         }
     }
 
+    kvm_has_create_irqchip = kvm_check_extension(s, KVM_CAP_IRQCHIP);
+
     return 0;
 }
 
target/s390x/kvm/kvm.c +11 -0
index a9e5880349d9..bcc735227f7d 100644
@@ -391,6 +391,17 @@ int kvm_arch_init(MachineState *ms, KVMState *s)
     }
 
     kvm_set_max_memslot_size(KVM_SLOT_MAX_BYTES);
+
+    kvm_has_create_irqchip = kvm_check_extension(s, KVM_CAP_S390_IRQCHIP);
+    if (kvm_has_create_irqchip) {
+        int ret = kvm_vm_enable_cap(s, KVM_CAP_S390_IRQCHIP, 0);
+
+        if (ret < 0) {
+            fprintf(stderr, "Enable kernel irqchip failed: %s\n", strerror(-ret));
+            exit(1);
+        }
+    }
+
     return 0;
 }
 
Signature · 1 line
2.41.0
Reviewed-by on v2 1/1
#1 lore raw
Quoted 35 lines · Andrew Jones

Since Linux commit 00f918f61c56 ("RISC-V: KVM: Skeletal in-kernel AIA irqchip support") checking KVM_CAP_IRQCHIP returns non-zero when the RISC-V platform has AIA. The cap indicates KVM supports at least one of the following ioctls:

KVM_CREATE_IRQCHIP
KVM_IRQ_LINE
KVM_GET_IRQCHIP
KVM_SET_IRQCHIP
KVM_GET_LAPIC
KVM_SET_LAPIC

but the cap doesn't imply that KVM must support any of those ioctls in particular. However, QEMU was assuming the KVM_CREATE_IRQCHIP ioctl was supported. Stop making that assumption by introducing a KVM parameter that each architecture which supports KVM_CREATE_IRQCHIP sets. Adding parameters isn't awesome, but given how the KVM_CAP_IRQCHIP isn't very helpful on its own, we don't have a lot of options.

---

While this fixes booting guests on riscv KVM with AIA it's unlikely to get merged before the QEMU support for KVM AIA[1] lands, which would also fix the issue. I think this patch is still worth considering though since QEMU's assumption is wrong.

[1] https://lore.kernel.org/all/[email protected]/

v2:
- Move the s390x code to an s390x file. [Thomas]
- Drop the KVM_CAP_IRQCHIP check from the top of kvm_irqchip_create(), as it's no longer necessary.

Quoted 6 lines
  accel/kvm/kvm-all.c    | 16 ++++------------
  include/sysemu/kvm.h   |  1 +
  target/arm/kvm.c       |  3 +++
  target/i386/kvm/kvm.c  |  2 ++
  target/s390x/kvm/kvm.c | 11 +++++++++++
  5 files changed, 21 insertions(+), 12 deletions(-)
Reviewed-by on v2 1/1
#2 lore raw
In reply to Andrew Jones
Quoted 28 lines · Andrew Jones

Since Linux commit 00f918f61c56 ("RISC-V: KVM: Skeletal in-kernel AIA irqchip support") checking KVM_CAP_IRQCHIP returns non-zero when the RISC-V platform has AIA. The cap indicates KVM supports at least one of the following ioctls:

KVM_CREATE_IRQCHIP
KVM_IRQ_LINE
KVM_GET_IRQCHIP
KVM_SET_IRQCHIP
KVM_GET_LAPIC
KVM_SET_LAPIC

but the cap doesn't imply that KVM must support any of those ioctls in particular. However, QEMU was assuming the KVM_CREATE_IRQCHIP ioctl was supported. Stop making that assumption by introducing a KVM parameter that each architecture which supports KVM_CREATE_IRQCHIP sets. Adding parameters isn't awesome, but given how the KVM_CAP_IRQCHIP isn't very helpful on its own, we don't have a lot of options.

---

While this fixes booting guests on riscv KVM with AIA it's unlikely to get merged before the QEMU support for KVM AIA[1] lands, which would also fix the issue. I think this patch is still worth considering though since QEMU's assumption is wrong.

Quoted 6 lines

[1] https://lore.kernel.org/all/[email protected]/

v2:
- Move the s390x code to an s390x file. [Thomas]
- Drop the KVM_CAP_IRQCHIP check from the top of kvm_irqchip_create(), as it's no longer necessary.

Looks good now!

Reviewed-by on v2 1/1
#3 lore raw
In reply to Andrew Jones
Quoted 20 lines · Andrew Jones

Since Linux commit 00f918f61c56 ("RISC-V: KVM: Skeletal in-kernel AIA irqchip support") checking KVM_CAP_IRQCHIP returns non-zero when the RISC-V platform has AIA. The cap indicates KVM supports at least one of the following ioctls:

KVM_CREATE_IRQCHIP
KVM_IRQ_LINE
KVM_GET_IRQCHIP
KVM_SET_IRQCHIP
KVM_GET_LAPIC
KVM_SET_LAPIC

but the cap doesn't imply that KVM must support any of those ioctls in particular. However, QEMU was assuming the KVM_CREATE_IRQCHIP ioctl was supported. Stop making that assumption by introducing a KVM parameter that each architecture which supports KVM_CREATE_IRQCHIP sets. Adding parameters isn't awesome, but given how the KVM_CAP_IRQCHIP isn't very helpful on its own, we don't have a lot of options.

Quoted 2 lines

---

Quoted 123 lines

While this fixes booting guests on riscv KVM with AIA it's unlikely to get merged before the QEMU support for KVM AIA[1] lands, which would also fix the issue. I think this patch is still worth considering though since QEMU's assumption is wrong.

[1] https://lore.kernel.org/all/[email protected]/

v2:
- Move the s390x code to an s390x file. [Thomas]
- Drop the KVM_CAP_IRQCHIP check from the top of kvm_irqchip_create(), as it's no longer necessary.

  accel/kvm/kvm-all.c    | 16 ++++------------
  include/sysemu/kvm.h   |  1 +
  target/arm/kvm.c       |  3 +++
  target/i386/kvm/kvm.c  |  2 ++
  target/s390x/kvm/kvm.c | 11 +++++++++++
  5 files changed, 21 insertions(+), 12 deletions(-)
accel/kvm/kvm-all.c +4 -12
index 373d876c0580..cddcb6eca641 100644
@@ -86,6 +86,7 @@ struct KVMParkedVcpu {
  };
 
  KVMState *kvm_state;
+bool kvm_has_create_irqchip;
  bool kvm_kernel_irqchip;
  bool kvm_split_irqchip;
  bool kvm_async_interrupts_allowed;
@@ -2358,17 +2359,6 @@ static void kvm_irqchip_create(KVMState *s)
      int ret;
 
      assert(s->kernel_irqchip_split != ON_OFF_AUTO_AUTO);
-    if (kvm_check_extension(s, KVM_CAP_IRQCHIP)) {
-        ;
-    } else if (kvm_check_extension(s, KVM_CAP_S390_IRQCHIP)) {
-        ret = kvm_vm_enable_cap(s, KVM_CAP_S390_IRQCHIP, 0);
-        if (ret < 0) {
-            fprintf(stderr, "Enable kernel irqchip failed: %s\n", strerror(-ret));
-            exit(1);
-        }
-    } else {
-        return;
-    }
 
      /* First probe and see if there's a arch-specific hook to create the
       * in-kernel irqchip for us */
@@ -2377,8 +2367,10 @@ static void kvm_irqchip_create(KVMState *s)
          if (s->kernel_irqchip_split == ON_OFF_AUTO_ON) {
              error_report("Split IRQ chip mode not supported.");
              exit(1);
-        } else {
+        } else if (kvm_has_create_irqchip) {
              ret = kvm_vm_ioctl(s, KVM_CREATE_IRQCHIP);
+        } else {
+            return;
          }
      }
      if (ret < 0) {
include/sysemu/kvm.h +1 -0
index 115f0cca79d1..84b1bb3dc91e 100644
@@ -32,6 +32,7 @@
  #ifdef CONFIG_KVM_IS_POSSIBLE
 
  extern bool kvm_allowed;
+extern bool kvm_has_create_irqchip;
  extern bool kvm_kernel_irqchip;
  extern bool kvm_split_irqchip;
  extern bool kvm_async_interrupts_allowed;
target/arm/kvm.c +3 -0
index b4c7654f4980..2fa87b495d68 100644
@@ -250,6 +250,9 @@ int kvm_arm_get_max_vm_ipa_size(MachineState *ms, bool *fixed_ipa)
  int kvm_arch_init(MachineState *ms, KVMState *s)
  {
      int ret = 0;
+
+    kvm_has_create_irqchip = kvm_check_extension(s, KVM_CAP_IRQCHIP);
+
      /* For ARM interrupt delivery is always asynchronous,
       * whether we are using an in-kernel VGIC or not.
       */
target/i386/kvm/kvm.c +2 -0
index ebfaf3d24c79..6363e67f092d 100644
@@ -2771,6 +2771,8 @@ int kvm_arch_init(MachineState *ms, KVMState *s)
          }
      }
 
+    kvm_has_create_irqchip = kvm_check_extension(s, KVM_CAP_IRQCHIP);
+
      return 0;
  }
 
target/s390x/kvm/kvm.c +11 -0
index a9e5880349d9..bcc735227f7d 100644
@@ -391,6 +391,17 @@ int kvm_arch_init(MachineState *ms, KVMState *s)
      }
 
      kvm_set_max_memslot_size(KVM_SLOT_MAX_BYTES);
+
+    kvm_has_create_irqchip = kvm_check_extension(s, KVM_CAP_S390_IRQCHIP);
+    if (kvm_has_create_irqchip) {
+        int ret = kvm_vm_enable_cap(s, KVM_CAP_S390_IRQCHIP, 0);
+
+        if (ret < 0) {
+            fprintf(stderr, "Enable kernel irqchip failed: %s\n", strerror(-ret));
+            exit(1);
+        }
+    }
+
      return 0;
  }
 
#4 lore raw
In reply to Andrew Jones

Hi Paolo,

Is this good for 8.1?

Thanks,
drew

Quoted 148 lines · Andrew Jones

Since Linux commit 00f918f61c56 ("RISC-V: KVM: Skeletal in-kernel AIA irqchip support") checking KVM_CAP_IRQCHIP returns non-zero when the RISC-V platform has AIA. The cap indicates KVM supports at least one of the following ioctls:

KVM_CREATE_IRQCHIP
KVM_IRQ_LINE
KVM_GET_IRQCHIP
KVM_SET_IRQCHIP
KVM_GET_LAPIC
KVM_SET_LAPIC

but the cap doesn't imply that KVM must support any of those ioctls in particular. However, QEMU was assuming the KVM_CREATE_IRQCHIP ioctl was supported. Stop making that assumption by introducing a KVM parameter that each architecture which supports KVM_CREATE_IRQCHIP sets. Adding parameters isn't awesome, but given how the KVM_CAP_IRQCHIP isn't very helpful on its own, we don't have a lot of options.

---

While this fixes booting guests on riscv KVM with AIA it's unlikely to get merged before the QEMU support for KVM AIA[1] lands, which would also fix the issue. I think this patch is still worth considering though since QEMU's assumption is wrong.

[1] https://lore.kernel.org/all/[email protected]/

v2:
- Move the s390x code to an s390x file. [Thomas]
- Drop the KVM_CAP_IRQCHIP check from the top of kvm_irqchip_create(), as it's no longer necessary.

 accel/kvm/kvm-all.c    | 16 ++++------------
 include/sysemu/kvm.h   |  1 +
 target/arm/kvm.c       |  3 +++
 target/i386/kvm/kvm.c  |  2 ++
 target/s390x/kvm/kvm.c | 11 +++++++++++
 5 files changed, 21 insertions(+), 12 deletions(-)
accel/kvm/kvm-all.c +4 -12
index 373d876c0580..cddcb6eca641 100644
@@ -86,6 +86,7 @@ struct KVMParkedVcpu {
 };
 
 KVMState *kvm_state;
+bool kvm_has_create_irqchip;
 bool kvm_kernel_irqchip;
 bool kvm_split_irqchip;
 bool kvm_async_interrupts_allowed;
@@ -2358,17 +2359,6 @@ static void kvm_irqchip_create(KVMState *s)
     int ret;
 
     assert(s->kernel_irqchip_split != ON_OFF_AUTO_AUTO);
-    if (kvm_check_extension(s, KVM_CAP_IRQCHIP)) {
-        ;
-    } else if (kvm_check_extension(s, KVM_CAP_S390_IRQCHIP)) {
-        ret = kvm_vm_enable_cap(s, KVM_CAP_S390_IRQCHIP, 0);
-        if (ret < 0) {
-            fprintf(stderr, "Enable kernel irqchip failed: %s\n", strerror(-ret));
-            exit(1);
-        }
-    } else {
-        return;
-    }
 
     /* First probe and see if there's a arch-specific hook to create the
      * in-kernel irqchip for us */
@@ -2377,8 +2367,10 @@ static void kvm_irqchip_create(KVMState *s)
         if (s->kernel_irqchip_split == ON_OFF_AUTO_ON) {
             error_report("Split IRQ chip mode not supported.");
             exit(1);
-        } else {
+        } else if (kvm_has_create_irqchip) {
             ret = kvm_vm_ioctl(s, KVM_CREATE_IRQCHIP);
+        } else {
+            return;
         }
     }
     if (ret < 0) {
include/sysemu/kvm.h +1 -0
index 115f0cca79d1..84b1bb3dc91e 100644
@@ -32,6 +32,7 @@
 #ifdef CONFIG_KVM_IS_POSSIBLE
 
 extern bool kvm_allowed;
+extern bool kvm_has_create_irqchip;
 extern bool kvm_kernel_irqchip;
 extern bool kvm_split_irqchip;
 extern bool kvm_async_interrupts_allowed;
target/arm/kvm.c +3 -0
index b4c7654f4980..2fa87b495d68 100644
@@ -250,6 +250,9 @@ int kvm_arm_get_max_vm_ipa_size(MachineState *ms, bool *fixed_ipa)
 int kvm_arch_init(MachineState *ms, KVMState *s)
 {
     int ret = 0;
+
+    kvm_has_create_irqchip = kvm_check_extension(s, KVM_CAP_IRQCHIP);
+
     /* For ARM interrupt delivery is always asynchronous,
      * whether we are using an in-kernel VGIC or not.
      */
target/i386/kvm/kvm.c +2 -0
index ebfaf3d24c79..6363e67f092d 100644
@@ -2771,6 +2771,8 @@ int kvm_arch_init(MachineState *ms, KVMState *s)
         }
     }
 
+    kvm_has_create_irqchip = kvm_check_extension(s, KVM_CAP_IRQCHIP);
+
     return 0;
 }
 
target/s390x/kvm/kvm.c +11 -0
index a9e5880349d9..bcc735227f7d 100644
@@ -391,6 +391,17 @@ int kvm_arch_init(MachineState *ms, KVMState *s)
     }
 
     kvm_set_max_memslot_size(KVM_SLOT_MAX_BYTES);
+
+    kvm_has_create_irqchip = kvm_check_extension(s, KVM_CAP_S390_IRQCHIP);
+    if (kvm_has_create_irqchip) {
+        int ret = kvm_vm_enable_cap(s, KVM_CAP_S390_IRQCHIP, 0);
+
+        if (ret < 0) {
+            fprintf(stderr, "Enable kernel irqchip failed: %s\n", strerror(-ret));
+            exit(1);
+        }
+    }
+
     return 0;
 }
 
Signature · 1 line
2.41.0
#5 lore raw
Quoted 5 lines · Andrew Jones

Hi Paolo,

Is this good for 8.1?

Is it fixing a regression since 8.0 ?

thanks
-- PMM

Quoted 154 lines

Thanks,
drew

Quoted 148 lines · Andrew Jones

Since Linux commit 00f918f61c56 ("RISC-V: KVM: Skeletal in-kernel AIA irqchip support") checking KVM_CAP_IRQCHIP returns non-zero when the RISC-V platform has AIA. The cap indicates KVM supports at least one of the following ioctls:

KVM_CREATE_IRQCHIP
KVM_IRQ_LINE
KVM_GET_IRQCHIP
KVM_SET_IRQCHIP
KVM_GET_LAPIC
KVM_SET_LAPIC

but the cap doesn't imply that KVM must support any of those ioctls in particular. However, QEMU was assuming the KVM_CREATE_IRQCHIP ioctl was supported. Stop making that assumption by introducing a KVM parameter that each architecture which supports KVM_CREATE_IRQCHIP sets. Adding parameters isn't awesome, but given how the KVM_CAP_IRQCHIP isn't very helpful on its own, we don't have a lot of options.

---

While this fixes booting guests on riscv KVM with AIA it's unlikely to get merged before the QEMU support for KVM AIA[1] lands, which would also fix the issue. I think this patch is still worth considering though since QEMU's assumption is wrong.

[1] https://lore.kernel.org/all/[email protected]/

v2:
- Move the s390x code to an s390x file. [Thomas]
- Drop the KVM_CAP_IRQCHIP check from the top of kvm_irqchip_create(), as it's no longer necessary.

 accel/kvm/kvm-all.c    | 16 ++++------------
 include/sysemu/kvm.h   |  1 +
 target/arm/kvm.c       |  3 +++
 target/i386/kvm/kvm.c  |  2 ++
 target/s390x/kvm/kvm.c | 11 +++++++++++
 5 files changed, 21 insertions(+), 12 deletions(-)
accel/kvm/kvm-all.c +4 -12
index 373d876c0580..cddcb6eca641 100644
@@ -86,6 +86,7 @@ struct KVMParkedVcpu {
 };
 
 KVMState *kvm_state;
+bool kvm_has_create_irqchip;
 bool kvm_kernel_irqchip;
 bool kvm_split_irqchip;
 bool kvm_async_interrupts_allowed;
@@ -2358,17 +2359,6 @@ static void kvm_irqchip_create(KVMState *s)
     int ret;
 
     assert(s->kernel_irqchip_split != ON_OFF_AUTO_AUTO);
-    if (kvm_check_extension(s, KVM_CAP_IRQCHIP)) {
-        ;
-    } else if (kvm_check_extension(s, KVM_CAP_S390_IRQCHIP)) {
-        ret = kvm_vm_enable_cap(s, KVM_CAP_S390_IRQCHIP, 0);
-        if (ret < 0) {
-            fprintf(stderr, "Enable kernel irqchip failed: %s\n", strerror(-ret));
-            exit(1);
-        }
-    } else {
-        return;
-    }
 
     /* First probe and see if there's a arch-specific hook to create the
      * in-kernel irqchip for us */
@@ -2377,8 +2367,10 @@ static void kvm_irqchip_create(KVMState *s)
         if (s->kernel_irqchip_split == ON_OFF_AUTO_ON) {
             error_report("Split IRQ chip mode not supported.");
             exit(1);
-        } else {
+        } else if (kvm_has_create_irqchip) {
             ret = kvm_vm_ioctl(s, KVM_CREATE_IRQCHIP);
+        } else {
+            return;
         }
     }
     if (ret < 0) {
include/sysemu/kvm.h +1 -0
index 115f0cca79d1..84b1bb3dc91e 100644
@@ -32,6 +32,7 @@
 #ifdef CONFIG_KVM_IS_POSSIBLE
 
 extern bool kvm_allowed;
+extern bool kvm_has_create_irqchip;
 extern bool kvm_kernel_irqchip;
 extern bool kvm_split_irqchip;
 extern bool kvm_async_interrupts_allowed;
target/arm/kvm.c +3 -0
index b4c7654f4980..2fa87b495d68 100644
@@ -250,6 +250,9 @@ int kvm_arm_get_max_vm_ipa_size(MachineState *ms, bool *fixed_ipa)
 int kvm_arch_init(MachineState *ms, KVMState *s)
 {
     int ret = 0;
+
+    kvm_has_create_irqchip = kvm_check_extension(s, KVM_CAP_IRQCHIP);
+
     /* For ARM interrupt delivery is always asynchronous,
      * whether we are using an in-kernel VGIC or not.
      */
target/i386/kvm/kvm.c +2 -0
index ebfaf3d24c79..6363e67f092d 100644
@@ -2771,6 +2771,8 @@ int kvm_arch_init(MachineState *ms, KVMState *s)
         }
     }
 
+    kvm_has_create_irqchip = kvm_check_extension(s, KVM_CAP_IRQCHIP);
+
     return 0;
 }
 
target/s390x/kvm/kvm.c +11 -1
index a9e5880349d9..bcc735227f7d 100644
@@ -391,6 +391,17 @@ int kvm_arch_init(MachineState *ms, KVMState *s)
     }
 
     kvm_set_max_memslot_size(KVM_SLOT_MAX_BYTES);
+
+    kvm_has_create_irqchip = kvm_check_extension(s, KVM_CAP_S390_IRQCHIP);
+    if (kvm_has_create_irqchip) {
+        int ret = kvm_vm_enable_cap(s, KVM_CAP_S390_IRQCHIP, 0);
+
+        if (ret < 0) {
+            fprintf(stderr, "Enable kernel irqchip failed: %s\n", strerror(-ret));
+            exit(1);
+        }
+    }
+
     return 0;
 }
 
--

2.41.0

#6 lore raw
Quoted 8 lines · Peter Maydell
Quoted 5 lines · Andrew Jones

Hi Paolo,

Is this good for 8.1?

Is it fixing a regression since 8.0 ?

The fix is for running with KVM which includes 00f918f61c56, which is v6.5-rc1 and later. All QEMU versions will fail to start, so this fix isn't fixing a QEMU regression, but until this fix, or the KVM AIA support series, is merged, then we won't have any QEMU version we can use with later KVM.

Thanks,
drew

Quoted 158 lines

thanks
-- PMM

Quoted 154 lines

Thanks,
drew

Quoted 148 lines · Andrew Jones

Since Linux commit 00f918f61c56 ("RISC-V: KVM: Skeletal in-kernel AIA irqchip support") checking KVM_CAP_IRQCHIP returns non-zero when the RISC-V platform has AIA. The cap indicates KVM supports at least one of the following ioctls:

KVM_CREATE_IRQCHIP
KVM_IRQ_LINE
KVM_GET_IRQCHIP
KVM_SET_IRQCHIP
KVM_GET_LAPIC
KVM_SET_LAPIC

but the cap doesn't imply that KVM must support any of those ioctls in particular. However, QEMU was assuming the KVM_CREATE_IRQCHIP ioctl was supported. Stop making that assumption by introducing a KVM parameter that each architecture which supports KVM_CREATE_IRQCHIP sets. Adding parameters isn't awesome, but given how the KVM_CAP_IRQCHIP isn't very helpful on its own, we don't have a lot of options.

---

While this fixes booting guests on riscv KVM with AIA it's unlikely to get merged before the QEMU support for KVM AIA[1] lands, which would also fix the issue. I think this patch is still worth considering though since QEMU's assumption is wrong.

[1] https://lore.kernel.org/all/[email protected]/

v2:
- Move the s390x code to an s390x file. [Thomas]
- Drop the KVM_CAP_IRQCHIP check from the top of kvm_irqchip_create(), as it's no longer necessary.

 accel/kvm/kvm-all.c    | 16 ++++------------
 include/sysemu/kvm.h   |  1 +
 target/arm/kvm.c       |  3 +++
 target/i386/kvm/kvm.c  |  2 ++
 target/s390x/kvm/kvm.c | 11 +++++++++++
 5 files changed, 21 insertions(+), 12 deletions(-)
accel/kvm/kvm-all.c +4 -12
index 373d876c0580..cddcb6eca641 100644
@@ -86,6 +86,7 @@ struct KVMParkedVcpu {
 };
 
 KVMState *kvm_state;
+bool kvm_has_create_irqchip;
 bool kvm_kernel_irqchip;
 bool kvm_split_irqchip;
 bool kvm_async_interrupts_allowed;
@@ -2358,17 +2359,6 @@ static void kvm_irqchip_create(KVMState *s)
     int ret;
 
     assert(s->kernel_irqchip_split != ON_OFF_AUTO_AUTO);
-    if (kvm_check_extension(s, KVM_CAP_IRQCHIP)) {
-        ;
-    } else if (kvm_check_extension(s, KVM_CAP_S390_IRQCHIP)) {
-        ret = kvm_vm_enable_cap(s, KVM_CAP_S390_IRQCHIP, 0);
-        if (ret < 0) {
-            fprintf(stderr, "Enable kernel irqchip failed: %s\n", strerror(-ret));
-            exit(1);
-        }
-    } else {
-        return;
-    }
 
     /* First probe and see if there's a arch-specific hook to create the
      * in-kernel irqchip for us */
@@ -2377,8 +2367,10 @@ static void kvm_irqchip_create(KVMState *s)
         if (s->kernel_irqchip_split == ON_OFF_AUTO_ON) {
             error_report("Split IRQ chip mode not supported.");
             exit(1);
-        } else {
+        } else if (kvm_has_create_irqchip) {
             ret = kvm_vm_ioctl(s, KVM_CREATE_IRQCHIP);
+        } else {
+            return;
         }
     }
     if (ret < 0) {
include/sysemu/kvm.h +1 -0
index 115f0cca79d1..84b1bb3dc91e 100644
@@ -32,6 +32,7 @@
 #ifdef CONFIG_KVM_IS_POSSIBLE
 
 extern bool kvm_allowed;
+extern bool kvm_has_create_irqchip;
 extern bool kvm_kernel_irqchip;
 extern bool kvm_split_irqchip;
 extern bool kvm_async_interrupts_allowed;
target/arm/kvm.c +3 -0
index b4c7654f4980..2fa87b495d68 100644
@@ -250,6 +250,9 @@ int kvm_arm_get_max_vm_ipa_size(MachineState *ms, bool *fixed_ipa)
 int kvm_arch_init(MachineState *ms, KVMState *s)
 {
     int ret = 0;
+
+    kvm_has_create_irqchip = kvm_check_extension(s, KVM_CAP_IRQCHIP);
+
     /* For ARM interrupt delivery is always asynchronous,
      * whether we are using an in-kernel VGIC or not.
      */
target/i386/kvm/kvm.c +2 -0
index ebfaf3d24c79..6363e67f092d 100644
@@ -2771,6 +2771,8 @@ int kvm_arch_init(MachineState *ms, KVMState *s)
         }
     }
 
+    kvm_has_create_irqchip = kvm_check_extension(s, KVM_CAP_IRQCHIP);
+
     return 0;
 }
 
target/s390x/kvm/kvm.c +11 -1
index a9e5880349d9..bcc735227f7d 100644
@@ -391,6 +391,17 @@ int kvm_arch_init(MachineState *ms, KVMState *s)
     }
 
     kvm_set_max_memslot_size(KVM_SLOT_MAX_BYTES);
+
+    kvm_has_create_irqchip = kvm_check_extension(s, KVM_CAP_S390_IRQCHIP);
+    if (kvm_has_create_irqchip) {
+        int ret = kvm_vm_enable_cap(s, KVM_CAP_S390_IRQCHIP, 0);
+
+        if (ret < 0) {
+            fprintf(stderr, "Enable kernel irqchip failed: %s\n", strerror(-ret));
+            exit(1);
+        }
+    }
+
     return 0;
 }
 
--

2.41.0