Skip to contentSkip to conversation

hw/audio/pcspk: Inline pcspk_init()

PATCH In review Philippe Mathieu-Daudé 4 participants 13 messages Oct 20, 2023 – Nov 8, 2023

lore
Share & tools
lore
#0 lore raw

v4 cover hw/audio/pcspk: Inline pcspk_init()

Unfortunately v2 was merged as commit 40f8214fcd,
so adapt v3 to clean the mess.

Philippe Mathieu-Daudé (4):
hw/i386/pc: Pass Error** argument to pc_basic_device_init() hw/i386/pc: Propagate error if HPET device creation failed hw/i386/pc: Propagate error if PC_SPEAKER device creation failed hw/isa/i82378: Propagate error if PC_SPEAKER device creation failed

 include/hw/i386/pc.h |  5 +++--
 hw/i386/pc.c         | 15 +++++++++++----
 hw/i386/pc_piix.c    |  2 +-
 hw/i386/pc_q35.c     |  2 +-
 hw/isa/i82378.c      |  4 +++-
 5 files changed, 19 insertions(+), 9 deletions(-)
Signature · 1 line
2.41.0
#1 lore raw

PATCH v4 1/4 hw/i386/pc: Pass Error** argument to pc_basic_device_init()

pc_basic_device_init() creates devices which can fail, allow to propagate error to caller.

---
 include/hw/i386/pc.h | 5 +++--
 hw/i386/pc.c         | 7 +++++--
 hw/i386/pc_piix.c    | 2 +-
 hw/i386/pc_q35.c     | 2 +-
 4 files changed, 10 insertions(+), 6 deletions(-)
include/hw/i386/pc.h +3 -2
index bec38cb92c..069c27368d 100644
@@ -174,11 +174,12 @@ void pc_memory_init(PCMachineState *pcms,
                     uint64_t pci_hole64_size);
 uint64_t pc_pci_hole64_start(void);
 DeviceState *pc_vga_init(ISABus *isa_bus, PCIBus *pci_bus);
-void pc_basic_device_init(struct PCMachineState *pcms,
+bool pc_basic_device_init(struct PCMachineState *pcms,
                           ISABus *isa_bus, qemu_irq *gsi,
                           ISADevice *rtc_state,
                           bool create_fdctrl,
-                          uint32_t hpet_irqs);
+                          uint32_t hpet_irqs,
+                          Error **errp);
 void pc_cmos_init(PCMachineState *pcms,
                   BusState *ide0, BusState *ide1,
                   ISADevice *s);
hw/i386/pc.c +5 -2
index f7ee638bec..2481c11e83 100644
@@ -1189,11 +1189,12 @@ static void pc_superio_init(ISABus *isa_bus, bool create_fdctrl,
     g_free(a20_line);
 }
 
-void pc_basic_device_init(struct PCMachineState *pcms,
+bool pc_basic_device_init(struct PCMachineState *pcms,
                           ISABus *isa_bus, qemu_irq *gsi,
                           ISADevice *rtc_state,
                           bool create_fdctrl,
-                          uint32_t hpet_irqs)
+                          uint32_t hpet_irqs,
+                          Error **errp)
 {
     int i;
     DeviceState *hpet = NULL;
@@ -1291,6 +1292,8 @@ void pc_basic_device_init(struct PCMachineState *pcms,
     /* Super I/O */
     pc_superio_init(isa_bus, create_fdctrl, pcms->i8042_enabled,
                     pcms->vmport != ON_OFF_AUTO_ON);
+
+    return true;
 }
 
 void pc_nic_init(PCMachineClass *pcmc, ISABus *isa_bus, PCIBus *pci_bus)
hw/i386/pc_piix.c +1 -1
index e36a3262b2..0d9cdf773e 100644
@@ -320,7 +320,7 @@ static void pc_init1(MachineState *machine,
 
     /* init basic PC hardware */
     pc_basic_device_init(pcms, isa_bus, x86ms->gsi, rtc_state, true,
-                         0x4);
+                         0x4, &error_fatal);
 
     pc_nic_init(pcmc, isa_bus, pci_bus);
 
hw/i386/pc_q35.c +1 -1
index a7386f2ca2..e4b05e3139 100644
@@ -301,7 +301,7 @@ static void pc_q35_init(MachineState *machine)
 
     /* init basic PC hardware */
     pc_basic_device_init(pcms, isa_bus, x86ms->gsi, rtc_state, !mc->no_floppy,
-                         0xff0104);
+                         0xff0104, &error_fatal);
 
     if (pcms->sata_enabled) {
         /* ahci and SATA device, for q35 1 ahci controller is built-in */
Signature · 1 line
2.41.0
Reviewed-by on v4 1/4
#2 lore raw

Re: [PATCH v4 1/4] hw/i386/pc: Pass Error** argument to pc_basic_device_init()

Quoted 11 lines · Philippe Mathieu-Daudé

pc_basic_device_init() creates devices which can fail, allow to propagate error to caller.

---
  include/hw/i386/pc.h | 5 +++--
  hw/i386/pc.c         | 7 +++++--
  hw/i386/pc_piix.c    | 2 +-
  hw/i386/pc_q35.c     | 2 +-
  4 files changed, 10 insertions(+), 6 deletions(-)

r~

#3 lore raw

PATCH v4 2/4 hw/i386/pc: Propagate error if HPET device creation failed

In reply to Philippe Mathieu-Daudé
---
 hw/i386/pc.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
hw/i386/pc.c +3 -1
index 2481c11e83..6750ed427a 100644
@@ -1235,7 +1235,9 @@ bool pc_basic_device_init(struct PCMachineState *pcms,
         if (!compat) {
             qdev_prop_set_uint32(hpet, HPET_INTCAP, hpet_irqs);
         }
-        sysbus_realize_and_unref(SYS_BUS_DEVICE(hpet), &error_fatal);
+        if (!sysbus_realize_and_unref(SYS_BUS_DEVICE(hpet), errp)) {
+            return false;
+        }
         sysbus_mmio_map(SYS_BUS_DEVICE(hpet), 0, HPET_BASE);
 
         for (i = 0; i < IOAPIC_NUM_PINS; i++) {
Signature · 1 line
2.41.0
#5 lore raw

PATCH v4 3/4 hw/i386/pc: Propagate error if PC_SPEAKER device creation failed

In reply to Philippe Mathieu-Daudé

In commit 40f8214fcd ("hw/audio/pcspk: Inline pcspk_init()") we neglected to give a change to the caller to handle failed device creation cleanly. Here this can not happen because pc_basic_device_init() uses &error_fatal. Anyhow, do the proper error reporting to avoid bad code example spreading.

---
 hw/i386/pc.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
hw/i386/pc.c +3 -1
index 6750ed427a..3937d88355 100644
@@ -1288,7 +1288,9 @@ bool pc_basic_device_init(struct PCMachineState *pcms,
         }
         object_property_set_link(OBJECT(pcms->pcspk), "pit",
                                  OBJECT(pit), &error_fatal);
-        isa_realize_and_unref(pcms->pcspk, isa_bus, &error_fatal);
+        if (!isa_realize_and_unref(pcms->pcspk, isa_bus, errp)) {
+            return false;
+        }
     }
 
     /* Super I/O */
Signature · 1 line
2.41.0
Reviewed-by on v4 3/4
#6 lore raw

Re: [PATCH v4 3/4] hw/i386/pc: Propagate error if PC_SPEAKER device creation failed

Quoted 6 lines · Philippe Mathieu-Daudé

In commit 40f8214fcd ("hw/audio/pcspk: Inline pcspk_init()") we neglected to give a change to the caller to handle failed device creation cleanly. Here this can not happen because pc_basic_device_init() uses &error_fatal. Anyhow, do the proper error reporting to avoid bad code example spreading.

Quoted 6 lines
---
  hw/i386/pc.c | 4 +++-
  1 file changed, 3 insertions(+), 1 deletion(-)

r~

#7 lore raw

PATCH v4 4/4 hw/isa/i82378: Propagate error if PC_SPEAKER device creation failed

In reply to Philippe Mathieu-Daudé

In commit 40f8214fcd ("hw/audio/pcspk: Inline pcspk_init()") we neglected to give a change to the caller to handle failed device creation cleanly. Respect the caller API contract and propagate the error if creating the PC_SPEAKER device ever failed. This avoid yet another bad API use to be taken as example and copy / pasted all over the code base.

---
 hw/isa/i82378.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
hw/isa/i82378.c +3 -1
index 79ffbb52a0..203b92c264 100644
@@ -105,7 +105,9 @@ static void i82378_realize(PCIDevice *pci, Error **errp)
     /* speaker */
     pcspk = isa_new(TYPE_PC_SPEAKER);
     object_property_set_link(OBJECT(pcspk), "pit", OBJECT(pit), &error_fatal);
-    isa_realize_and_unref(pcspk, isabus, &error_fatal);
+    if (!isa_realize_and_unref(pcspk, isabus, errp)) {
+        return;
+    }
 
     /* 2 82C37 (dma) */
     isa_create_simple(isabus, "i82374");
Signature · 1 line
2.41.0
Reviewed-by on v4 4/4
#8 lore raw

Re: [PATCH v4 4/4] hw/isa/i82378: Propagate error if PC_SPEAKER device creation failed

Quoted 7 lines · Philippe Mathieu-Daudé

In commit 40f8214fcd ("hw/audio/pcspk: Inline pcspk_init()") we neglected to give a change to the caller to handle failed device creation cleanly. Respect the caller API contract and propagate the error if creating the PC_SPEAKER device ever failed. This avoid yet another bad API use to be taken as example and copy / pasted all over the code base.

Quoted 6 lines
---
  hw/isa/i82378.c | 4 +++-
  1 file changed, 3 insertions(+), 1 deletion(-)

r~

Reviewed-by on v4 4/4
#9 lore raw

Re: [PATCH v4 4/4] hw/isa/i82378: Propagate error if PC_SPEAKER device creation failed

In reply to Philippe Mathieu-Daudé

Am 20. Oktober 2023 17:15:08 UTC schrieb "Philippe Mathieu-Daudé" <[email protected]>:

Quoted 10 lines

In commit 40f8214fcd ("hw/audio/pcspk: Inline pcspk_init()") we neglected to give a change to the caller to handle failed device creation cleanly. Respect the caller API contract and propagate the error if creating the PC_SPEAKER device ever failed. This avoid yet another bad API use to be taken as example and copy / pasted all over the code base.

Quoted 19 lines

---
hw/isa/i82378.c | 4 +++-

1 file changed, 3 insertions(+), 1 deletion(-)
hw/isa/i82378.c +3 -1
index 79ffbb52a0..203b92c264 100644
@@ -105,7 +105,9 @@ static void i82378_realize(PCIDevice *pci, Error **errp)
    /* speaker */
    pcspk = isa_new(TYPE_PC_SPEAKER);
    object_property_set_link(OBJECT(pcspk), "pit", OBJECT(pit), &error_fatal);
-    isa_realize_and_unref(pcspk, isabus, &error_fatal);
+    if (!isa_realize_and_unref(pcspk, isabus, errp)) {
+        return;
+    }
 
    /* 2 82C37 (dma) */
    isa_create_simple(isabus, "i82374");
#10 lore raw
In reply to Philippe Mathieu-Daudé

Am 20. Oktober 2023 17:15:04 UTC schrieb "Philippe Mathieu-Daudé" <[email protected]>:

Quoted 7 lines

Unfortunately v2 was merged as commit 40f8214fcd,
so adapt v3 to clean the mess.

Philippe Mathieu-Daudé (4):
hw/i386/pc: Pass Error** argument to pc_basic_device_init() hw/i386/pc: Propagate error if HPET device creation failed hw/i386/pc: Propagate error if PC_SPEAKER device creation failed

I'm not sure if I'd do these first three patches. The reason is that machines don't inherit from DeviceState and therefore don't have canonical methods such as realize() to propagate errors. Propagating the errors in the machine init helper methods seem a bit ad-hoc to me.

Quoted 1 line

hw/isa/i82378: Propagate error if PC_SPEAKER device creation failed

The reason I suggested use of errp here is that it is already a parameter.

Best regards,
Bernhard

Quoted 8 lines
include/hw/i386/pc.h |  5 +++--
hw/i386/pc.c         | 15 +++++++++++----
hw/i386/pc_piix.c    |  2 +-
hw/i386/pc_q35.c     |  2 +-
hw/isa/i82378.c      |  4 +++-
5 files changed, 19 insertions(+), 9 deletions(-)
#11 lore raw
Quoted 10 lines · Bernhard Beschow

Am 20. Oktober 2023 17:15:04 UTC schrieb "Philippe Mathieu-Daudé" <[email protected]>:

Quoted 7 lines

Unfortunately v2 was merged as commit 40f8214fcd,
so adapt v3 to clean the mess.

Philippe Mathieu-Daudé (4):
hw/i386/pc: Pass Error** argument to pc_basic_device_init() hw/i386/pc: Propagate error if HPET device creation failed hw/i386/pc: Propagate error if PC_SPEAKER device creation failed

I'm not sure if I'd do these first three patches. The reason is that machines don't inherit from DeviceState and therefore don't have canonical methods such as realize() to propagate errors. Propagating the errors in the machine init helper methods seem a bit ad-hoc to me.

The Error interface enables separation of error detection and error handling. On detection, we create an Error object, and handling consumes it.

A function that leaves error handling to its callers generally requires its callees to leave it, too. Use of &error_fatal is wrong then.

Even when error handling need not be left to callers, leaving it can result in simpler or more robust code.

When a function handles errors itself, say by use of &error_fatal or error_report(), it's only usable in contexts where this handling is appropriate.

Sometimes the context is obvious enough, and unlikely to change. Handling directly is fine then, and can be simpler.

When the context isn't that obvious, leaving error handling to callers liberates you from thinking about the context, and also enables safe reuse of the function in other contexts.

I think pc_basic_device_init() doesn't *need* the change, as it's context is obvious enough. But the change is fine, and if we apply it, we never have to think about the context again. Matter of taste.

Quoted 3 lines
Quoted 1 line

hw/isa/i82378: Propagate error if PC_SPEAKER device creation failed

The reason I suggested use of errp here is that it is already a parameter.

Use of &error_fatal in a function taking @errp is almost always wrong. The patch fixes an instance of "wrong".

#12 lore raw
Quoted 30 lines · Markus Armbruster
Quoted 10 lines · Bernhard Beschow

Am 20. Oktober 2023 17:15:04 UTC schrieb "Philippe Mathieu-Daudé" <[email protected]>:

Quoted 7 lines

Unfortunately v2 was merged as commit 40f8214fcd,
so adapt v3 to clean the mess.

Philippe Mathieu-Daudé (4):
hw/i386/pc: Pass Error** argument to pc_basic_device_init() hw/i386/pc: Propagate error if HPET device creation failed hw/i386/pc: Propagate error if PC_SPEAKER device creation failed

I'm not sure if I'd do these first three patches. The reason is that machines don't inherit from DeviceState and therefore don't have canonical methods such as realize() to propagate errors. Propagating the errors in the machine init helper methods seem a bit ad-hoc to me.

The Error interface enables separation of error detection and error handling. On detection, we create an Error object, and handling consumes it.

A function that leaves error handling to its callers generally requires its callees to leave it, too. Use of &error_fatal is wrong then.

Even when error handling need not be left to callers, leaving it can result in simpler or more robust code.

When a function handles errors itself, say by use of &error_fatal or error_report(), it's only usable in contexts where this handling is appropriate.

Sometimes the context is obvious enough, and unlikely to change. Handling directly is fine then, and can be simpler.

Quoted 7 lines

When the context isn't that obvious, leaving error handling to callers liberates you from thinking about the context, and also enables safe reuse of the function in other contexts.

I think pc_basic_device_init() doesn't *need* the change, as it's context is obvious enough. But the change is fine, and if we apply it, we never have to think about the context again. Matter of taste.

I disagree with Bernhard because pc_basic_device_init() could end up refactored and called elsewhere where error can be propagated -- think qdev modules --, and in its current form we'll keep ignoring the caller errp and use &error_fatal (see patch #2 and #3). Also, better to have an unified style rather that trying to "optimize" arguments on a per case basis. Anyhow, my 2 cents.

Quoted 7 lines
Quoted 4 lines
Quoted 2 lines

hw/isa/i82378: Propagate error if PC_SPEAKER device creation failed

The reason I suggested use of errp here is that it is already a parameter.

Use of &error_fatal in a function taking @errp is almost always wrong. The patch fixes an instance of "wrong".

Due to Bernhard concerns, I'm only queuing patch #4.

Regards,

Phil.