Skip to contentSkip to conversation

hw/nmi: Safer vCPU delivery

PATCH In review Philippe Mathieu-Daudé 2 participants 10 messages Aug 10 – Aug 11

lore
Share & tools
lore
#0 lore raw

cover hw/nmi: Safer vCPU delivery

Hi Marc-André,

I don't remember what I suggested particularly for [email protected], but this series is what I had in mind back then.

If you find it worthy, please consider including it in your "Make HMP optional" work.

Regard,

Phil.

Philippe Mathieu-Daudé (4):
hw/nmi: Allow delivering to first available vCPU (not by index) hw/nmi: Introduce nmi_cpu_handler() for vcpu-specific delivery hw/s390x: Deliver per-vcpu NMI using nmi_cpu_handler hw/nmi: Remove unused @cpu_index and @errp arguments

 include/hw/core/nmi.h      | 10 +++++++++-
 hw/core/nmi.c              | 25 ++++++++++++++++++++++---
 hw/hppa/machine.c          |  4 ++--
 hw/i386/x86.c              |  5 ++---
 hw/intc/m68k_irqc.c        |  4 ++--
 hw/ipmi/ipmi.c             |  2 +-
 hw/m68k/q800-glue.c        |  4 ++--
 hw/misc/macio/gpio.c       |  4 ++--
 hw/ppc/pnv.c               |  7 +++----
 hw/ppc/spapr.c             |  4 ++--
 hw/s390x/s390-virtio-ccw.c |  6 ++----
 hw/watchdog/watchdog.c     |  2 +-
 12 files changed, 50 insertions(+), 27 deletions(-)
Signature · 1 line
2.53.0
#1 lore raw

PATCH 1/4 hw/nmi: Allow delivering to first available vCPU (not by index)

Allow callers to deliver on any vCPU (for example when the vCPU index is not known).

---
 include/hw/core/nmi.h  | 7 +++++++
 hw/core/nmi.c          | 5 +++++
 hw/ipmi/ipmi.c         | 2 +-
 hw/watchdog/watchdog.c | 2 +-
 4 files changed, 14 insertions(+), 2 deletions(-)
include/hw/core/nmi.h +7 -0
index fff41bebc69..6136290f4fe 100644
@@ -40,6 +40,13 @@ struct NMIClass {
     void (*nmi_monitor_handler)(NMIState *n, int cpu_index, Error **errp);
 };
 
+/**
+ * nmi_monitor_handle:
+ * @cpu_index: Index of the CPU to deliver the NMI
+ * @errp: pointer to a NULL-initialized error object
+ *
+ * Note: cpu_index = -1 can be used to deliver to the first vCPU available.
+ */
 void nmi_monitor_handle(int cpu_index, Error **errp);
 
 #endif /* NMI_H */
hw/core/nmi.c +5 -0
index 4b447e126ba..67213631c04 100644
@@ -20,6 +20,7 @@
  */
 
 #include "qemu/osdep.h"
+#include "hw/core/cpu.h"
 #include "hw/core/nmi.h"
 #include "qapi/error.h"
 #include "qemu/module.h"
@@ -42,6 +43,10 @@ static int do_nmi(Object *o, void *opaque)
         NMIClass *nc = NMI_GET_CLASS(n);
 
         ns->handled = true;
+        if (ns->cpu_index == -1) {
+            /* Any vCPU is OK, take the first one */
+            ns->cpu_index = first_cpu->cpu_index;
+        }
         nc->nmi_monitor_handler(n, ns->cpu_index, &ns->err);
         if (ns->err) {
             return -1;
hw/ipmi/ipmi.c +1 -1
index 74818ff3cea..8c492271f90 100644
@@ -60,7 +60,7 @@ static int ipmi_do_hw_op(IPMIInterface *s, enum ipmi_op op, int checkonly)
             return 0;
         }
         /* We don't care what CPU we use. */
-        nmi_monitor_handle(0, NULL);
+        nmi_monitor_handle(-1, NULL);
         return 0;
 
     case IPMI_SHUTDOWN_VIA_ACPI_OVERTEMP:
hw/watchdog/watchdog.c +1 -1
index 0842fe373ae..0c1b94a62f3 100644
@@ -81,7 +81,7 @@ void watchdog_perform_action(void)
 
     case WATCHDOG_ACTION_INJECT_NMI:
         qapi_event_send_watchdog(WATCHDOG_ACTION_INJECT_NMI);
-        nmi_monitor_handle(0, NULL);
+        nmi_monitor_handle(-1, NULL);
         break;
 
     default:
Signature · 1 line
2.53.0
#2 lore raw

PATCH 2/4 hw/nmi: Introduce nmi_cpu_handler() for vcpu-specific delivery

In reply to Philippe Mathieu-Daudé

No need to have handlers check for existing CPU index, lookup the CPUState once in the core do_nmi() code and pass along.

---
 include/hw/core/nmi.h |  1 +
 hw/core/nmi.c         | 29 +++++++++++++++++++++++------
 2 files changed, 24 insertions(+), 6 deletions(-)
include/hw/core/nmi.h +1 -0
index 6136290f4fe..2f554d58b7f 100644
@@ -38,6 +38,7 @@ struct NMIClass {
     InterfaceClass parent_class;
 
     void (*nmi_monitor_handler)(NMIState *n, int cpu_index, Error **errp);
+    void (*nmi_cpu_handler)(NMIState *ns, CPUState *cs);
 };
 
 /**
hw/core/nmi.c +23 -6
index 67213631c04..fb623fd00be 100644
@@ -43,13 +43,30 @@ static int do_nmi(Object *o, void *opaque)
         NMIClass *nc = NMI_GET_CLASS(n);
 
         ns->handled = true;
-        if (ns->cpu_index == -1) {
-            /* Any vCPU is OK, take the first one */
-            ns->cpu_index = first_cpu->cpu_index;
+
+        /* Generic handler */
+        if (nc->nmi_monitor_handler) {
+            nc->nmi_monitor_handler(n, ns->cpu_index, &ns->err);
+            if (ns->err) {
+                return -1;
+            }
         }
-        nc->nmi_monitor_handler(n, ns->cpu_index, &ns->err);
-        if (ns->err) {
-            return -1;
+        /* Per vCPU handler */
+        if (nc->nmi_cpu_handler) {
+            CPUState *cs;
+
+            if (ns->cpu_index >= 0) {
+                /* Specific vCPU requested */
+                cs = qemu_get_cpu(ns->cpu_index);
+            } else {
+                /* Any vCPU is OK, take the first one */
+                cs = first_cpu;
+            }
+            if (!cs) {
+                error_setg(&ns->err, "CPU %d does not exist", ns->cpu_index);
+                return -1;
+            }
+            nc->nmi_cpu_handler(n, cs);
         }
     }
     nmi_children(o, ns);
Signature · 1 line
2.53.0
#3 lore raw

PATCH 3/4 hw/s390x: Deliver per-vcpu NMI using nmi_cpu_handler

In reply to Philippe Mathieu-Daudé

NMIClass::nmi_cpu_handler() handler is safer because the CPUState argument has been checked to be valid.

---
 hw/s390x/s390-virtio-ccw.c | 6 ++----
 1 file changed, 2 insertions(+), 4 deletions(-)
hw/s390x/s390-virtio-ccw.c +2 -4
index 25a9fa49554..b71086c254a 100644
@@ -725,10 +725,8 @@ static HotplugHandler *s390_get_hotplug_handler(MachineState *machine,
     return NULL;
 }
 
-static void s390_nmi(NMIState *n, int cpu_index, Error **errp)
+static void s390_nmi(NMIState *ns, CPUState *cs)
 {
-    CPUState *cs = qemu_get_cpu(cpu_index);
-
     s390_cpu_restart(S390_CPU(cs));
 }
 
@@ -832,7 +830,7 @@ static void ccw_machine_class_init(ObjectClass *oc, const void *data)
     hc->plug = s390_machine_device_plug;
     hc->unplug_request = s390_machine_device_unplug_request;
     hc->unplug = s390_machine_device_unplug;
-    nc->nmi_monitor_handler = s390_nmi;
+    nc->nmi_cpu_handler = s390_nmi;
     mc->default_ram_id = "s390.ram";
     mc->default_nic = "virtio-net-ccw";
     dsi->qmp_dump_skeys = s390_qmp_dump_skeys;
Signature · 1 line
2.53.0
#4 lore raw

PATCH 4/4 hw/nmi: Remove unused @cpu_index and @errp arguments

In reply to Philippe Mathieu-Daudé

NMI delivery never fails: remove the unused @errp argument. nmi_handler() neither use the @cpu_index argument, remove it.

---
 include/hw/core/nmi.h | 2 +-
 hw/core/nmi.c         | 7 ++-----
 hw/hppa/machine.c     | 4 ++--
 hw/i386/x86.c         | 5 ++---
 hw/intc/m68k_irqc.c   | 4 ++--
 hw/m68k/q800-glue.c   | 4 ++--
 hw/misc/macio/gpio.c  | 4 ++--
 hw/ppc/pnv.c          | 7 +++----
 hw/ppc/spapr.c        | 4 ++--
 9 files changed, 18 insertions(+), 23 deletions(-)
include/hw/core/nmi.h +1 -1
index 2f554d58b7f..476b5d02fa1 100644
@@ -37,7 +37,7 @@ typedef struct NMIState NMIState;
 struct NMIClass {
     InterfaceClass parent_class;
 
-    void (*nmi_monitor_handler)(NMIState *n, int cpu_index, Error **errp);
+    void (*nmi_handler)(NMIState *ns);
     void (*nmi_cpu_handler)(NMIState *ns, CPUState *cs);
 };
 
hw/core/nmi.c +2 -5
index fb623fd00be..3f4ef5b2094 100644
@@ -45,11 +45,8 @@ static int do_nmi(Object *o, void *opaque)
         ns->handled = true;
 
         /* Generic handler */
-        if (nc->nmi_monitor_handler) {
-            nc->nmi_monitor_handler(n, ns->cpu_index, &ns->err);
-            if (ns->err) {
-                return -1;
-            }
+        if (nc->nmi_handler) {
+            nc->nmi_handler(n);
         }
         /* Per vCPU handler */
         if (nc->nmi_cpu_handler) {
hw/hppa/machine.c +2 -2
index d762163ddf5..93874b5d5c9 100644
@@ -829,7 +829,7 @@ static void hppa_machine_reset(MachineState *ms, ResetType type)
     cpu[0]->env.cmdline_or_bootorder = 'c';
 }
 
-static void hppa_nmi(NMIState *n, int cpu_index, Error **errp)
+static void hppa_nmi(NMIState *ns)
 {
     CPUState *cs;
 
@@ -851,7 +851,7 @@ static void hppa_machine_common_class_init(ObjectClass *oc, const void *data)
     mc->default_ram_id = "hppa.ram";
     mc->default_nic = "tulip";
 
-    nc->nmi_monitor_handler = hppa_nmi;
+    nc->nmi_handler = hppa_nmi;
 }
 
 static void HP_B160L_machine_init_class_init(ObjectClass *oc, const void *data)
hw/i386/x86.c +2 -3
index dc7f0d56b01..201746945d5 100644
@@ -147,9 +147,8 @@ static const CPUArchIdList *x86_possible_cpu_arch_ids(MachineState *ms)
     return ms->possible_cpus;
 }
 
-static void x86_nmi(NMIState *n, int cpu_index, Error **errp)
+static void x86_nmi(NMIState *ns)
 {
-    /* cpu index isn't used */
     CPUState *cs;
 
     CPU_FOREACH(cs) {
@@ -389,7 +388,7 @@ static void x86_machine_class_init(ObjectClass *oc, const void *data)
     mc->get_default_cpu_node_id = x86_get_default_cpu_node_id;
     mc->possible_cpu_arch_ids = x86_possible_cpu_arch_ids;
     mc->kvm_type = x86_kvm_type;
-    nc->nmi_monitor_handler = x86_nmi;
+    nc->nmi_handler = x86_nmi;
 
     object_class_property_add(oc, X86_MACHINE_SMM, "OnOffAuto",
         x86_machine_get_smm, x86_machine_set_smm,
hw/intc/m68k_irqc.c +2 -2
index 68ddb5351be..5d4e5a10ab8 100644
@@ -70,7 +70,7 @@ static void m68k_irqc_instance_init(Object *obj)
     qdev_init_gpio_in(DEVICE(obj), m68k_set_irq, M68K_IRQC_LEVEL_NUM);
 }
 
-static void m68k_nmi(NMIState *n, int cpu_index, Error **errp)
+static void m68k_nmi(NMIState *n)
 {
     m68k_set_irq(n, M68K_IRQC_LEVEL_7, 1);
 }
@@ -97,7 +97,7 @@ static void m68k_irqc_class_init(ObjectClass *oc, const void *data)
     InterruptStatsProviderClass *ic = INTERRUPT_STATS_PROVIDER_CLASS(oc);
 
     device_class_set_props(dc, m68k_irqc_properties);
-    nc->nmi_monitor_handler = m68k_nmi;
+    nc->nmi_handler = m68k_nmi;
     device_class_set_legacy_reset(dc, m68k_irqc_reset);
     dc->vmsd = &vmstate_m68k_irqc;
     ic->get_statistics = m68k_irqc_get_statistics;
hw/m68k/q800-glue.c +2 -2
index ac9062c6488..6a9592182a4 100644
@@ -159,7 +159,7 @@ static void glue_auxmode_set_irq(void *opaque, int irq, int level)
     s->auxmode = level;
 }
 
-static void glue_nmi(NMIState *n, int cpu_index, Error **errp)
+static void glue_nmi(NMIState *n)
 {
     GLUEState *s = GLUE(n);
 
@@ -237,7 +237,7 @@ static void glue_class_init(ObjectClass *klass, const void *data)
     dc->vmsd = &vmstate_glue;
     device_class_set_props(dc, glue_properties);
     rc->phases.hold = glue_reset_hold;
-    nc->nmi_monitor_handler = glue_nmi;
+    nc->nmi_handler = glue_nmi;
 }
 
 static const TypeInfo glue_info_types[] = {
hw/misc/macio/gpio.c +2 -2
index 1a7c534d652..f6fce5b3532 100644
@@ -188,7 +188,7 @@ static void macio_gpio_reset(DeviceState *dev)
     macio_set_gpio(s, 1, true);
 }
 
-static void macio_gpio_nmi(NMIState *n, int cpu_index, Error **errp)
+static void macio_gpio_nmi(NMIState *n)
 {
     macio_set_gpio(MACIO_GPIO(n), 9, true);
     macio_set_gpio(MACIO_GPIO(n), 9, false);
@@ -201,7 +201,7 @@ static void macio_gpio_class_init(ObjectClass *oc, const void *data)
 
     device_class_set_legacy_reset(dc, macio_gpio_reset);
     dc->vmsd = &vmstate_macio_gpio;
-    nc->nmi_monitor_handler = macio_gpio_nmi;
+    nc->nmi_handler = macio_gpio_nmi;
 }
 
 static const TypeInfo macio_gpio_init_info = {
hw/ppc/pnv.c +3 -4
index c0cb45dbfbc..817f2ed7ae4 100644
@@ -3552,12 +3552,11 @@ static void pnv_cpu_do_nmi(PnvChip *chip, PowerPCCPU *cpu, void *opaque)
     async_run_on_cpu(CPU(cpu), pnv_cpu_do_nmi_on_cpu, RUN_ON_CPU_HOST_INT(0));
 }
 
-static void pnv_nmi(NMIState *n, int cpu_index, Error **errp)
+static void pnv_nmi(NMIState *ns)
 {
     PnvMachineState *pnv = PNV_MACHINE(qdev_get_machine());
-    int i;
 
-    for (i = 0; i < pnv->num_chips; i++) {
+    for (int i = 0; i < pnv->num_chips; i++) {
         pnv_chip_foreach_cpu(pnv->chips[i], pnv_cpu_do_nmi, NULL);
     }
 }
@@ -3583,7 +3582,7 @@ static void pnv_machine_class_init(ObjectClass *oc, const void *data)
     mc->default_ram_size = 1 * GiB;
     mc->default_ram_id = "pnv.ram";
     ispc->print_info = pnv_pic_print_info;
-    nc->nmi_monitor_handler = pnv_nmi;
+    nc->nmi_handler = pnv_nmi;
 
     object_class_property_add_bool(oc, "hb-mode",
                                    pnv_machine_get_hb, pnv_machine_set_hb);
hw/ppc/spapr.c +2 -2
index b79828b4e90..a2195796efe 100644
@@ -3550,7 +3550,7 @@ void spapr_do_system_reset_on_cpu(CPUState *cs, run_on_cpu_data arg)
     }
 }
 
-static void spapr_nmi(NMIState *n, int cpu_index, Error **errp)
+static void spapr_nmi(NMIState *ns)
 {
     CPUState *cs;
 
@@ -4652,7 +4652,7 @@ static void spapr_machine_class_init(ObjectClass *oc, const void *data)
     mc->nvdimm_supported = true;
     smc->resize_hpt_default = SPAPR_RESIZE_HPT_ENABLED;
     fwc->get_dev_path = spapr_get_fw_dev_path;
-    nc->nmi_monitor_handler = spapr_nmi;
+    nc->nmi_handler = spapr_nmi;
     vhc->cpu_in_nested = spapr_cpu_in_nested;
     vhc->deliver_hv_excp = spapr_exit_nested;
     vhc->hypercall = emulate_spapr_hypercall;
Signature · 1 line
2.53.0
#5 lore raw
In reply to Philippe Mathieu-Daudé
Quoted 8 lines · Philippe Mathieu-Daudé

Hi Marc-André,

I don't remember what I suggested particularly for [email protected], but this series is what I had in mind back then.

If you find it worthy, please consider including it in your "Make HMP optional" work.

Not well explained, this series disentangle HMP from the generic NMI class.

Quoted 25 lines

Regard,

Phil.

Philippe Mathieu-Daudé (4):
hw/nmi: Allow delivering to first available vCPU (not by index) hw/nmi: Introduce nmi_cpu_handler() for vcpu-specific delivery hw/s390x: Deliver per-vcpu NMI using nmi_cpu_handler hw/nmi: Remove unused @cpu_index and @errp arguments

  include/hw/core/nmi.h      | 10 +++++++++-
  hw/core/nmi.c              | 25 ++++++++++++++++++++++---
  hw/hppa/machine.c          |  4 ++--
  hw/i386/x86.c              |  5 ++---
  hw/intc/m68k_irqc.c        |  4 ++--
  hw/ipmi/ipmi.c             |  2 +-
  hw/m68k/q800-glue.c        |  4 ++--
  hw/misc/macio/gpio.c       |  4 ++--
  hw/ppc/pnv.c               |  7 +++----
  hw/ppc/spapr.c             |  4 ++--
  hw/s390x/s390-virtio-ccw.c |  6 ++----
  hw/watchdog/watchdog.c     |  2 +-
  12 files changed, 50 insertions(+), 27 deletions(-)
#6 lore raw
Quoted 7 lines · Philippe Mathieu-Daudé
Quoted 6 lines · Philippe Mathieu-Daudé

Hi Marc-André,

I don't remember what I suggested particularly for [email protected], but this series is what I had in mind back then.

Quoted 5 lines
Quoted 2 lines

If you find it worthy, please consider including it in your "Make HMP optional" work.

Not well explained, this series disentangle HMP from the generic NMI class.

Oh no, one patch is missing!

Quoted 28 lines
Quoted 26 lines

Regard,

Phil.

Philippe Mathieu-Daudé (4):
   hw/nmi: Allow delivering to first available vCPU (not by index) hw/nmi: Introduce nmi_cpu_handler() for vcpu-specific delivery hw/s390x: Deliver per-vcpu NMI using nmi_cpu_handler hw/nmi: Remove unused @cpu_index and @errp arguments

  include/hw/core/nmi.h      | 10 +++++++++- hw/core/nmi.c              | 25 ++++++++++++++++++++++--- hw/hppa/machine.c          |  4 ++-- hw/i386/x86.c              |  5 ++--- hw/intc/m68k_irqc.c        |  4 ++-- hw/ipmi/ipmi.c             |  2 +- hw/m68k/q800-glue.c        |  4 ++-- hw/misc/macio/gpio.c       |  4 ++-- hw/ppc/pnv.c               |  7 +++---- hw/ppc/spapr.c             |  4 ++-- hw/s390x/s390-virtio-ccw.c |  6 ++---- hw/watchdog/watchdog.c     |  2 +-

  12 files changed, 50 insertions(+), 27 deletions(-)
#7 lore raw
Quoted 8 lines · Philippe Mathieu-Daudé
Quoted 7 lines · Philippe Mathieu-Daudé
Quoted 6 lines · Philippe Mathieu-Daudé

Hi Marc-André,

I don't remember what I suggested particularly for [email protected], but this series is what I had in mind back then.

Quoted 5 lines
Quoted 5 lines
Quoted 2 lines

If you find it worthy, please consider including it in your "Make HMP optional" work.

Not well explained, this series disentangle HMP from the generic NMI class.

Actually previous version is better:
https://lore.kernel.org/qemu-devel/[email protected]/

Quoted 33 lines

Oh no, one patch is missing!

Quoted 28 lines
Quoted 26 lines

Regard,

Phil.

Philippe Mathieu-Daudé (4):
   hw/nmi: Allow delivering to first available vCPU (not by index) hw/nmi: Introduce nmi_cpu_handler() for vcpu-specific delivery hw/s390x: Deliver per-vcpu NMI using nmi_cpu_handler hw/nmi: Remove unused @cpu_index and @errp arguments

  include/hw/core/nmi.h      | 10 +++++++++- hw/core/nmi.c              | 25 ++++++++++++++++++++++--- hw/hppa/machine.c          |  4 ++-- hw/i386/x86.c              |  5 ++--- hw/intc/m68k_irqc.c        |  4 ++-- hw/ipmi/ipmi.c             |  2 +- hw/m68k/q800-glue.c        |  4 ++-- hw/misc/macio/gpio.c       |  4 ++-- hw/ppc/pnv.c               |  7 +++---- hw/ppc/spapr.c             |  4 ++-- hw/s390x/s390-virtio-ccw.c |  6 ++---- hw/watchdog/watchdog.c     |  2 +-

  12 files changed, 50 insertions(+), 27 deletions(-)
#8 lore raw
Quoted 9 lines · Philippe Mathieu-Daudé
Quoted 8 lines · Philippe Mathieu-Daudé
Quoted 7 lines · Philippe Mathieu-Daudé
Quoted 6 lines · Philippe Mathieu-Daudé

Hi Marc-André,

I don't remember what I suggested particularly for [email protected], but this series is what I had in mind back then.

Quoted 8 lines
Quoted 5 lines
Quoted 5 lines
Quoted 2 lines

If you find it worthy, please consider including it in your "Make HMP optional" work.

Not well explained, this series disentangle HMP from the generic NMI class.

Actually previous version is better:
https://lore.kernel.org/qemu-devel/[email protected]/

Are you going to rebase & resend that ? The old thread has a lot
of commentary that I don't think it is worth reviewing the old code again until updated.

With regards,
Daniel

Signature · 3 lines
|: https://berrange.com       ~~        https://hachyderm.io/@berrange :|
|: https://libvirt.org          ~~          https://entangle-photo.org :|
|: https://pixelfed.art/berrange   ~~    https://fstop138.berrange.com :|
#9 lore raw
Quoted 16 lines · Daniel P. Berrangé
Quoted 15 lines · Philippe Mathieu-Daudé
Quoted 13 lines · Philippe Mathieu-Daudé
Quoted 12 lines · Philippe Mathieu-Daudé
Quoted 8 lines · Philippe Mathieu-Daudé

Hi Marc-André,

I don't remember what I suggested particularly for [email protected], but this series is what I had in mind back then.

If you find it worthy, please consider including it in your "Make HMP optional" work.

Not well explained, this series disentangle HMP from the generic NMI class.

Quoted 6 lines
Quoted 2 lines

Actually previous version is better:
https://lore.kernel.org/qemu-devel/[email protected]/

Are you going to rebase & resend that ? The old thread has a lot
of commentary that I don't think it is worth reviewing the old code again until updated.

Please discard this series.

Old series rebased and reposted (addressing comments) as v3 here:
https://lore.kernel.org/qemu-devel/[email protected]/