hw/intc: Handle the error of IOAPICCommonClass.realize()
PATCH 1/1 hw/intc: Handle the error of IOAPICCommonClass.realize()
From: Zhao Liu <[email protected]>
IOAPICCommonClass implements its own private realize(), and this private realize() allows error.
Therefore, return directly if IOAPICCommonClass.realize() meets error.
--- hw/intc/ioapic_common.c | 3 +++ 1 file changed, 3 insertions(+)
hw/intc/ioapic_common.c +3 -0
@@ -162,6 +162,9 @@ static void ioapic_common_realize(DeviceState *dev, Error **errp) info =IOAPIC_COMMON_GET_CLASS (s); info->realize (dev, errp); +if (*errp) { +return ; + }sysbus_init_mmio (SYS_BUS_DEVICE (s), &s->io_memory); ioapic_no++;
Signature · 1 line
2.34.1
Hi Zhao,
Quoted 17 lines · Zhao Liu
From: Zhao Liu <[email protected]>
IOAPICCommonClass implements its own private
realize(), and this privaterealize()allows error.Therefore, return directly if
IOAPICCommonClass.realize()meets error.Signed-off-by Zhao Liu <[email protected]>--- hw/intc/ioapic_common.c | 3 +++ 1 file changed, 3 insertions(+)hw/intc/ioapic_common.c +0
-0
hw/intc/ioapic_common.c static void ioapic_common_realize(DeviceState *dev, Error **errp) · line 167
Quoted 6 lines
hw/intc/ioapic_common.c +3
-0@@ -162,6 +162,9 @@ static void ioapic_common_realize(DeviceState *dev, Error **errp) info =IOAPIC_COMMON_GET_CLASS (s); info->realize (dev, errp); +if (*errp) { +return ; + }
Could be clearer to deviate from DeviceRealize and let the handler return a boolean:
-- >8 --
hw/intc/ioapic_internal.h +1 -1
@@ -92,3 +92,3 @@ struct IOAPICCommonClass { -DeviceRealize realize; +bool (*realize)(DeviceState *dev,Error **errp);DeviceUnrealize unrealize;
hw/i386/kvm/ioapic.c +3 -1
@@ -121,3 +121,3 @@ static void kvm_ioapic_set_irq(void *opaque, int irq, int level) -static void kvm_ioapic_realize (DeviceState *dev,Error **errp) +static bool kvm_ioapic_realize (DeviceState *dev,Error **errp) {
@@ -133,2 +133,4 @@ static void kvm_ioapic_realize(DeviceState *dev, Error **errp)qdev_init_gpio_in (dev, kvm_ioapic_set_irq,IOAPIC_NUM_PINS ); + +return true; }
hw/intc/ioapic_common.c +3 -1
@@ -163,3 +163,5 @@ static void ioapic_common_realize(DeviceState *dev, Error **errp) info =IOAPIC_COMMON_GET_CLASS (s); - info->realize (dev, errp); +if (!info->realize (dev, errp)) { +return ; + }
---
What do you think?
Hi Philippe,
Quoted 40 lines · Philippe Mathieu-Daudé
Date: Wed, 31 Jan 2024 17:48:24 +0100
From: Philippe Mathieu-Daudé <[email protected]>
Subject: Re: [PATCH] hw/intc: Handle the error of
IOAPICCommonClass.realize()Hi Zhao,
Quoted 17 lines · Zhao Liu
From: Zhao Liu <[email protected]>
IOAPICCommonClass implements its own private
realize(), and this privaterealize()allows error.Therefore, return directly if
IOAPICCommonClass.realize()meets error.Signed-off-by Zhao Liu <[email protected]>--- hw/intc/ioapic_common.c | 3 +++ 1 file changed, 3 insertions(+)hw/intc/ioapic_common.c +0
-0hw/intc/ioapic_common.c static void ioapic_common_realize(DeviceState *dev, Error **errp) · line 166
Quoted 5 lines
hw/intc/ioapic_common.c +3
-0@@ -162,6 +162,9 @@ static void ioapic_common_realize(DeviceState *dev, Error **errp) info =IOAPIC_COMMON_GET_CLASS (s); info->realize (dev, errp); +if (*errp) { +return ; + }Could be clearer to deviate from DeviceRealize and let the handler return a boolean:
-- >8 --
hw/intc/ioapic_internal.h +0
-0
hw/intc/ioapic_internal.h struct IOAPICCommonClass { · line 93
Quoted 3 lines
hw/intc/ioapic_internal.h +1
-1@@ -92,3 +92,3 @@ struct IOAPICCommonClass { -DeviceRealize realize; +bool (*realize)(DeviceState *dev,Error **errp);
What about I change the name of this interface?
Maybe ioapic_realize(), to distinguish it from DeviceClass.realize().
hw/intc/ioapic_internal.h struct IOAPICCommonClass { · line 94
Quoted 1 line
hw/intc/ioapic_internal.h +0
-0DeviceUnrealize unrealize;
Additionally, if I change the pattern of realize(), should I also avoid the DeviceUnrealize macro for symmetry's sake and just declare a similar function pointer as you said?
Further, do you think it's necessary to introduce InternalRealize and InternalUnrealize macros for qdev to wrap these special realize/unrealize to differentiate them from normal DeviceRealize/DeviceUnrealize?
Because I found that this pattern of realize() (i.e. registering the realize() of the child class in the parent class instead of DeviceClass, and then calling the registered realize() in parent realize()) is also widely used in many cases:
* xen_block_realize()
* virtser_port_device_realize()
* x86_iommu_realize()
* virtio_input_device_realize()
* apic_common_realize()
* pc_dimm_realize()
* virtio_device_realize()
...
I'm not quite sure if this is a generic way to use it, although it looks like it could easily be confused with DeviceClass.realize().
Quoted 30 lines
hw/i386/kvm/ioapic.c +1
-1@@ -121,3 +121,3 @@ static void kvm_ioapic_set_irq(void *opaque, int irq, int level) -static void kvm_ioapic_realize (DeviceState *dev,Error **errp) +static bool kvm_ioapic_realize (DeviceState *dev,Error **errp){
hw/i386/kvm/ioapic.c +2
-0@@ -133,2 +133,4 @@ static void kvm_ioapic_realize(DeviceState *dev, Error **errp)qdev_init_gpio_in (dev, kvm_ioapic_set_irq,IOAPIC_NUM_PINS ); + +return true;}
hw/intc/ioapic_common.c +3
-2@@ -163,3 +163,5 @@ static void ioapic_common_realize(DeviceState *dev, Error **errp) info =IOAPIC_COMMON_GET_CLASS (s); - info->realize (dev, errp); +if (!info->realize (dev, errp)) { +return ; + } ---
Quoted 1 line
What do you think?
I'm OK with the change here, but not sure if the return of private realize() should be changed elsewhere as well.
Thanks,
Zhao
Ping Philippe & Markus,
Do you have furthur comment on such private realize()? ;-)
Thanks,
Zhao
Quoted 122 lines · Zhao Liu
Date: Thu, 1 Feb 2024 11:25:56 +0800
From: Zhao Liu <[email protected]>
Subject: Re: [PATCH] hw/intc: Handle the error of
IOAPICCommonClass.realize()Hi Philippe,
Quoted 40 lines · Philippe Mathieu-Daud?
Date: Wed, 31 Jan 2024 17:48:24 +0100
From: Philippe Mathieu-Daud? <[email protected]>
Subject: Re: [PATCH] hw/intc: Handle the error of
IOAPICCommonClass.realize()Hi Zhao,
Quoted 17 lines · Zhao Liu
From: Zhao Liu <[email protected]>
IOAPICCommonClass implements its own private
realize(), and this privaterealize()allows error.Therefore, return directly if
IOAPICCommonClass.realize()meets error.Signed-off-by Zhao Liu <[email protected]>--- hw/intc/ioapic_common.c | 3 +++ 1 file changed, 3 insertions(+)hw/intc/ioapic_common.c +0
-0hw/intc/ioapic_common.c static void ioapic_common_realize(DeviceState *dev, Error **errp) · line 166
Quoted 5 lines
hw/intc/ioapic_common.c +3
-0@@ -162,6 +162,9 @@ static void ioapic_common_realize(DeviceState *dev, Error **errp) info =IOAPIC_COMMON_GET_CLASS (s); info->realize (dev, errp); +if (*errp) { +return ; + }Could be clearer to deviate from DeviceRealize and let the handler return a boolean:
-- >8 --
hw/intc/ioapic_internal.h +0
-0hw/intc/ioapic_internal.h struct IOAPICCommonClass { · line 93
Quoted 3 lines
hw/intc/ioapic_internal.h +1
-1@@ -92,3 +92,3 @@ struct IOAPICCommonClass { -DeviceRealize realize; +bool (*realize)(DeviceState *dev,Error **errp);What about I change the name of this interface?
Maybe
ioapic_realize(), to distinguish it fromDeviceClass.realize().hw/intc/ioapic_internal.h struct IOAPICCommonClass { · line 94
Quoted 1 line
hw/intc/ioapic_internal.h +0
-0DeviceUnrealize unrealize;Additionally, if I change the pattern of
realize(), should I also avoid the DeviceUnrealize macro for symmetry's sake and just declare a similar function pointer as you said?Further, do you think it's necessary to introduce InternalRealize and InternalUnrealize macros for qdev to wrap these special realize/unrealize to differentiate them from normal DeviceRealize/DeviceUnrealize?
Because I found that this pattern of
realize()(i.e. registering therealize()of the child class in the parent class instead of DeviceClass, and then calling the registeredrealize()in parentrealize()) is also widely used in many cases:*
xen_block_realize()
*virtser_port_device_realize()
*x86_iommu_realize()
*virtio_input_device_realize()
*apic_common_realize()
*pc_dimm_realize()
*virtio_device_realize()
...I'm not quite sure if this is a generic way to use it, although it looks like it could easily be confused with
DeviceClass.realize().Quoted 30 lines
hw/i386/kvm/ioapic.c +1
-1@@ -121,3 +121,3 @@ static void kvm_ioapic_set_irq(void *opaque, int irq, int level) -static void kvm_ioapic_realize (DeviceState *dev,Error **errp) +static bool kvm_ioapic_realize (DeviceState *dev,Error **errp){
hw/i386/kvm/ioapic.c +2
-0@@ -133,2 +133,4 @@ static void kvm_ioapic_realize(DeviceState *dev, Error **errp)qdev_init_gpio_in (dev, kvm_ioapic_set_irq,IOAPIC_NUM_PINS ); + +return true;}
hw/intc/ioapic_common.c +3
-2@@ -163,3 +163,5 @@ static void ioapic_common_realize(DeviceState *dev, Error **errp) info =IOAPIC_COMMON_GET_CLASS (s); - info->realize (dev, errp); +if (!info->realize (dev, errp)) { +return ; + } ---Quoted 1 line
What do you think?
I'm OK with the change here, but not sure if the return of private
realize()should be changed elsewhere as well.Thanks,
Zhao
Quoted 22 lines · Zhao Liu
Hi Philippe,
Quoted 19 lines · Philippe Mathieu-Daudé
Date: Wed, 31 Jan 2024 17:48:24 +0100
From: Philippe Mathieu-Daudé <[email protected]>
Subject: Re: [PATCH] hw/intc: Handle the error ofIOAPICCommonClass.realize()Hi Zhao,
Quoted 12 lines · Zhao Liu
From: Zhao Liu <[email protected]>
IOAPICCommonClass implements its own private
realize(), and this privaterealize()allows error.Therefore, return directly if
IOAPICCommonClass.realize()meets error.Signed-off-by Zhao Liu <[email protected]>--- hw/intc/ioapic_common.c | 3 +++ 1 file changed, 3 insertions(+)
Quoted 10 lines
Quoted 10 lines
hw/intc/ioapic_common.c static void ioapic_common_realize(DeviceState *dev, Error **errp) · line 166
Quoted 10 lines
hw/intc/ioapic_common.c +3
-0@@ -162,6 +162,9 @@ static void ioapic_common_realize(DeviceState *dev, Error **errp) info =IOAPIC_COMMON_GET_CLASS (s); info->realize (dev, errp); +if (*errp) { +return ; + }
This is wrong, although it'll work in practice.
It's wrong, because dereferencing @errp requires ERRP_GUARD(). qapi/error.h:
* = Why, when and how to use ERRP_GUARD() =
*
* Without ERRP_GUARD(), use of the @errp parameter is restricted:
* - It must not be dereferenced, because it may be null.
* - It should not be passed to error_prepend() or
* error_append_hint(), because that doesn't work with &error_fatal.
* ERRP_GUARD() lifts these restrictions.
*
* To use ERRP_GUARD(), add it right at the beginning of the function.
* @errp can then be used without worrying about the argument being
* NULL or &error_fatal.
*
* Using it when it's not needed is safe, but please avoid cluttering
* the source with useless code.
It'll work anyway, because the caller never passes null.
Obvious fix:
hw/intc/ioapic_common.c +1 -0
@@ -152,6 +152,7 @@ static int ioapic_dispatch_post_load(void *opaque, int version_id)static void ioapic_common_realize (DeviceState *dev,Error **errp) { +ERRP_GUARD ();IOAPICCommonState *s =IOAPIC_COMMON (dev);IOAPICCommonClass *info;
Quoted 10 lines
Quoted 10 lines
Could be clearer to deviate from DeviceRealize and let the handler return a boolean:
-- >8 --
hw/intc/ioapic_internal.h +0
-0@@ -92,3 +92,3 @@ struct IOAPICCommonClass {
Quoted 2 lines
hw/intc/ioapic_internal.h struct IOAPICCommonClass { · line 93
Quoted 2 lines
hw/intc/ioapic_internal.h +1
-1-DeviceRealize realize; +bool (*realize)(DeviceState *dev,Error **errp);
qapi.error.h advises:
* - Whenever practical, also return a value that indicates success /
* failure. This can make the error checking more concise, and can * avoid useless error object creation and destruction. Note that * we still have many functions returning void. We recommend * • bool-valued functions return true on success / false on failure, * • pointer-valued functions return non-null / null pointer, and * • integer-valued functions return non-negative / negative.
The patch then becomes
info = IOAPIC_COMMON_GET_CLASS(s);
- info->realize(dev, errp);
+ if (!info->realize(dev, errp) {
+ return;
+ }
DeviceClass and BusClass callbacks realize, unrealize ignore this
advice: they return void. Why?
Following the advice makes calls easier to read, but the callees have to do a tiny bit of extra work: return something. Good trade when we have at least as many callers as callees.
But these callbacks have many more callees: many devices implement them, but only a few places call. Changing them to return something looked like more trouble than it's worth, so we didn't.
Quoted 3 lines
What about I change the name of this interface?
Maybe
ioapic_realize(), to distinguish it fromDeviceClass.realize().
I wouldn't bother.
Quoted 8 lines
hw/intc/ioapic_internal.h struct IOAPICCommonClass { · line 94
Quoted 1 line
hw/intc/ioapic_internal.h +0
-0DeviceUnrealize unrealize;Additionally, if I change the pattern of
realize(), should I also avoid the DeviceUnrealize macro for symmetry's sake and just declare a similar function pointer as you said?Further, do you think it's necessary to introduce InternalRealize and InternalUnrealize macros for qdev
You mean typedefs?
Quoted 8 lines
for qdev to wrap these special realize/unrealize
to differentiate them from normal DeviceRealize/DeviceUnrealize?Because I found that this pattern of
realize()(i.e. registering therealize()of the child class in the parent class instead of DeviceClass, and then calling the registeredrealize()in parentrealize()) is also widely used in many cases:
Quoted 8 lines
*
xen_block_realize()
*virtser_port_device_realize()
*x86_iommu_realize()
*virtio_input_device_realize()
*apic_common_realize()
*pc_dimm_realize()
*virtio_device_realize()
...
Yes.
When a subtype overrides a supertype's method, it often makes sense to have the subtype's method call the supertype's method.
Quoted 2 lines
I'm not quite sure if this is a generic way to use it, although it looks like it could easily be confused with
DeviceClass.realize().
Did I answer your question? I'm not sure :)
Quoted 28 lines
Quoted 28 lines
hw/i386/kvm/ioapic.c +1
-1@@ -121,3 +121,3 @@ static void kvm_ioapic_set_irq(void *opaque, int irq, int level) -static void kvm_ioapic_realize (DeviceState *dev,Error **errp) +static bool kvm_ioapic_realize (DeviceState *dev,Error **errp){
hw/i386/kvm/ioapic.c +2
-0@@ -133,2 +133,4 @@ static void kvm_ioapic_realize(DeviceState *dev, Error **errp)qdev_init_gpio_in (dev, kvm_ioapic_set_irq,IOAPIC_NUM_PINS ); + +return true;}
hw/intc/ioapic_common.c +3
-1@@ -163,3 +163,5 @@ static void ioapic_common_realize(DeviceState *dev, Error **errp) info =IOAPIC_COMMON_GET_CLASS (s); - info->realize (dev, errp); +if (!info->realize (dev, errp)) { +return ; + }
Quoted 6 lines
Quoted 3 lines
---
What do you think?
I'm OK with the change here, but not sure if the return of private
realize()should be changed elsewhere as well.
I think I'd add the missing ERRP_GUARD() and call it a day.
Hi Markus,
Quoted 69 lines · Markus Armbruster
Date: Wed, 07 Feb 2024 07:51:52 +0100
From: Markus Armbruster <[email protected]>
Subject: Re: [PATCH] hw/intc: Handle the error of
IOAPICCommonClass.realize()Quoted 22 lines · Zhao Liu
Hi Philippe,
Quoted 19 lines · Philippe Mathieu-Daudé
Date: Wed, 31 Jan 2024 17:48:24 +0100
From: Philippe Mathieu-Daudé <[email protected]>
Subject: Re: [PATCH] hw/intc: Handle the error ofIOAPICCommonClass.realize()Hi Zhao,
Quoted 12 lines · Zhao Liu
From: Zhao Liu <[email protected]>
IOAPICCommonClass implements its own private
realize(), and this privaterealize()allows error.Therefore, return directly if
IOAPICCommonClass.realize()meets error.Signed-off-by Zhao Liu <[email protected]>--- hw/intc/ioapic_common.c | 3 +++ 1 file changed, 3 insertions(+)Quoted 10 lines
Quoted 10 lines
hw/intc/ioapic_common.c static void ioapic_common_realize(DeviceState *dev, Error **errp) · line 166
Quoted 10 lines
hw/intc/ioapic_common.c +3
-0@@ -162,6 +162,9 @@ static void ioapic_common_realize(DeviceState *dev, Error **errp) info =IOAPIC_COMMON_GET_CLASS (s); info->realize (dev, errp); +if (*errp) { +return ; + }This is wrong, although it'll work in practice.
It's wrong, because dereferencing @errp requires
ERRP_GUARD().qapi/error.h:* = Why, when and how to use
ERRP_GUARD()=
*
* WithoutERRP_GUARD(), use of the @errp parameter is restricted:
* - It must not be dereferenced, because it may be null.
* - It should not be passed toerror_prepend()or
*error_append_hint(), because that doesn't work with&error_fatal.
*ERRP_GUARD()lifts these restrictions.
*
* To useERRP_GUARD(), add it right at the beginning of the function.
* @errp can then be used without worrying about the argument being
* NULL or&error_fatal.
*
* Using it when it's not needed is safe, but please avoid cluttering
* the source with useless code.It'll work anyway, because the caller never passes null.
Obvious fix:
hw/intc/ioapic_common.c +0
-0
hw/intc/ioapic_common.c static int ioapic_dispatch_post_load(void *opaque, int version_id) · line 157
Quoted 6 lines
hw/intc/ioapic_common.c +1
-0@@ -152,6 +152,7 @@ static int ioapic_dispatch_post_load(void *opaque, int version_id)static void ioapic_common_realize (DeviceState *dev,Error **errp) { +ERRP_GUARD ();IOAPICCommonState *s =IOAPIC_COMMON (dev);IOAPICCommonClass *info;
Thanks for explaining and educating me!
Quoted 35 lines
Quoted 11 lines
Quoted 11 lines
Could be clearer to deviate from DeviceRealize and let the handler return a boolean:
-- >8 --
hw/intc/ioapic_internal.h +0
-0@@ -92,3 +92,3 @@ struct IOAPICCommonClass {Quoted 2 lines
hw/intc/ioapic_internal.h struct IOAPICCommonClass { · line 93
Quoted 2 lines
hw/intc/ioapic_internal.h +1
-1-DeviceRealize realize; +bool (*realize)(DeviceState *dev,Error **errp);
qapi.error.hadvises:* - Whenever practical, also return a value that indicates success /
* failure. This can make the error checking more concise, and can * avoid useless error object creation and destruction. Note that * we still have many functions returning void. We recommend * • bool-valued functions return true on success / false on failure, * • pointer-valued functions return non-null / null pointer, and * • integer-valued functions return non-negative / negative.The patch then becomes
info =
IOAPIC_COMMON_GET_CLASS(s);
-info->realize(dev, errp);
+ if (!info->realize(dev, errp) {
+ return;
+ }DeviceClass and BusClass callbacks realize, unrealize ignore this
advice: they return void. Why?
Quoted 7 lines
Following the advice makes calls easier to read, but the callees have to do a tiny bit of extra work: return something. Good trade when we have at least as many callers as callees.
But these callbacks have many more callees: many devices implement them, but only a few places call. Changing them to return something looked like more trouble than it's worth, so we didn't.
Thanks! Got it.
Quoted 6 lines
Quoted 4 lines
What about I change the name of this interface?
Maybe
ioapic_realize(), to distinguish it fromDeviceClass.realize().I wouldn't bother.
;-)
Quoted 11 lines
Quoted 9 lines
hw/intc/ioapic_internal.h struct IOAPICCommonClass { · line 94
Quoted 2 lines
hw/intc/ioapic_internal.h +0
-0DeviceUnrealize unrealize;Additionally, if I change the pattern of
realize(), should I also avoid the DeviceUnrealize macro for symmetry's sake and just declare a similar function pointer as you said?Further, do you think it's necessary to introduce InternalRealize and InternalUnrealize macros for qdev
You mean typedefs?
Yes!
Quoted 18 lines
Quoted 9 lines
for qdev to wrap these special realize/unrealize
to differentiate them from normal DeviceRealize/DeviceUnrealize?Because I found that this pattern of
realize()(i.e. registering therealize()of the child class in the parent class instead of DeviceClass, and then calling the registeredrealize()in parentrealize()) is also widely used in many cases:Quoted 8 lines
*
xen_block_realize()
*virtser_port_device_realize()
*x86_iommu_realize()
*virtio_input_device_realize()
*apic_common_realize()
*pc_dimm_realize()
*virtio_device_realize()
...
Quoted 4 lines
Yes.
When a subtype overrides a supertype's method, it often makes sense to have the subtype's method call the supertype's method.
Thanks! I can consider a simple RFC to introduce a new typedef.
Quoted 5 lines
Quoted 3 lines
I'm not quite sure if this is a generic way to use it, although it looks like it could easily be confused with
DeviceClass.realize().Did I answer your question? I'm not sure :)
Of course you did. :-)
Quoted 29 lines
Quoted 29 lines
Quoted 29 lines
hw/i386/kvm/ioapic.c +1
-1@@ -121,3 +121,3 @@ static void kvm_ioapic_set_irq(void *opaque, int irq, int level) -static void kvm_ioapic_realize (DeviceState *dev,Error **errp) +static bool kvm_ioapic_realize (DeviceState *dev,Error **errp){
hw/i386/kvm/ioapic.c +2
-0@@ -133,2 +133,4 @@ static void kvm_ioapic_realize(DeviceState *dev, Error **errp)qdev_init_gpio_in (dev, kvm_ioapic_set_irq,IOAPIC_NUM_PINS ); + +return true;}
hw/intc/ioapic_common.c +3
-1@@ -163,3 +163,5 @@ static void ioapic_common_realize(DeviceState *dev, Error **errp) info =IOAPIC_COMMON_GET_CLASS (s); - info->realize (dev, errp); +if (!info->realize (dev, errp)) { +return ; + }
Quoted 9 lines
Quoted 6 lines
Quoted 3 lines
---
What do you think?
I'm OK with the change here, but not sure if the return of private
realize()should be changed elsewhere as well.I think I'd add the missing
ERRP_GUARD()and call it a day.
Okay, let me add the missing ERRP_GUARD(). I'll also check and clean up other place where the ERRP_GUARD() is also missing.
Regards,
Zhao