All of lore.kernel.org
 help / color / mirror / Atom feed
* [Qemu-devel] [PATCH v2 1/2] spapr: introduce SPAPR_MACHINE_NOASSERT()
@ 2017-10-08 17:17 Greg Kurz
  2017-10-08 17:17 ` [Qemu-devel] [PATCH v2 2/2] spapr_pci: fail gracefully with non-pseries machine types Greg Kurz
  2017-10-08 20:05 ` [Qemu-devel] [PATCH v2 1/2] spapr: introduce SPAPR_MACHINE_NOASSERT() Peter Maydell
  0 siblings, 2 replies; 5+ messages in thread
From: Greg Kurz @ 2017-10-08 17:17 UTC (permalink / raw)
  To: qemu-devel; +Cc: qemu-ppc, David Gibson

A spapr-cpu-core device can only be plugged into a pseries machine. This
is checked in spapr_cpu_core_realize() with object_dynamic_cast() instead
of the usual SPAPR_MACHINE() macro because we don't want QEMU to abort.

This patch moves the gory details to a SPAPR_MACHINE_NOASSERT() macro
that acts like the SPAPR_MACHINE() one, except it returns NULL instead
of aborting if its argument doesn't point to a pseries machine type.

This is done for two reasons:
- it makes the code nicer
- it may be used by other pseries-specific devices like PHBs for example

Signed-off-by: Greg Kurz <groug@kaod.org>
---
 hw/ppc/spapr_cpu_core.c |    7 +++----
 include/hw/ppc/spapr.h  |    3 +++
 2 files changed, 6 insertions(+), 4 deletions(-)

diff --git a/hw/ppc/spapr_cpu_core.c b/hw/ppc/spapr_cpu_core.c
index 37beb56e8b18..5fde07614218 100644
--- a/hw/ppc/spapr_cpu_core.c
+++ b/hw/ppc/spapr_cpu_core.c
@@ -199,7 +199,7 @@ error:
 
 static void spapr_cpu_core_realize(DeviceState *dev, Error **errp)
 {
-    sPAPRMachineState *spapr;
+    sPAPRMachineState *spapr = SPAPR_MACHINE_NOASSERT(qdev_get_machine());
     sPAPRCPUCore *sc = SPAPR_CPU_CORE(OBJECT(dev));
     sPAPRCPUCoreClass *scc = SPAPR_CPU_CORE_GET_CLASS(OBJECT(dev));
     CPUCore *cc = CPU_CORE(OBJECT(dev));
@@ -209,9 +209,8 @@ static void spapr_cpu_core_realize(DeviceState *dev, Error **errp)
     void *obj;
     int i, j;
 
-    spapr = (sPAPRMachineState *) qdev_get_machine();
-    if (!object_dynamic_cast((Object *) spapr, TYPE_SPAPR_MACHINE)) {
-        error_setg(errp, "spapr-cpu-core needs a pseries machine");
+    if (!spapr) {
+        error_setg(errp, TYPE_SPAPR_CPU_CORE " needs a pseries machine");
         return;
     }
 
diff --git a/include/hw/ppc/spapr.h b/include/hw/ppc/spapr.h
index c1b365f56431..4933da8083df 100644
--- a/include/hw/ppc/spapr.h
+++ b/include/hw/ppc/spapr.h
@@ -43,6 +43,9 @@ typedef struct sPAPRMachineClass sPAPRMachineClass;
 #define SPAPR_MACHINE_CLASS(klass) \
     OBJECT_CLASS_CHECK(sPAPRMachineClass, klass, TYPE_SPAPR_MACHINE)
 
+#define SPAPR_MACHINE_NOASSERT(obj) \
+    ((sPAPRMachineState *) object_dynamic_cast(obj, TYPE_SPAPR_MACHINE))
+
 typedef enum {
     SPAPR_RESIZE_HPT_DEFAULT = 0,
     SPAPR_RESIZE_HPT_DISABLED,

^ permalink raw reply related	[flat|nested] 5+ messages in thread

* [Qemu-devel] [PATCH v2 2/2] spapr_pci: fail gracefully with non-pseries machine types
  2017-10-08 17:17 [Qemu-devel] [PATCH v2 1/2] spapr: introduce SPAPR_MACHINE_NOASSERT() Greg Kurz
@ 2017-10-08 17:17 ` Greg Kurz
  2017-10-08 20:05 ` [Qemu-devel] [PATCH v2 1/2] spapr: introduce SPAPR_MACHINE_NOASSERT() Peter Maydell
  1 sibling, 0 replies; 5+ messages in thread
From: Greg Kurz @ 2017-10-08 17:17 UTC (permalink / raw)
  To: qemu-devel; +Cc: qemu-ppc, David Gibson

QEMU currently crashes when the user tries to add a spapr-pci-host-bridge
on a non-pseries machine:

$ qemu-system-ppc64 -M ppce500 -device spapr-pci-host-bridge,index=1
hw/ppc/spapr_pci.c:1535:spapr_phb_realize:
Object 0x1003dacae60 is not an instance of type spapr-machine
Aborted (core dumped)

The same thing happens with the deprecated but still available child type
spapr-pci-vfio-host-bridge.

Fix both by using the SPAPR_MACHINE_NOASSERT() macro in papr_phb_realize().

Reviewed-by: Daniel Henrique Barboza <danielhb@linux.vnet.ibm.com>
Signed-off-by: Greg Kurz <groug@kaod.org>
---
v2: - use new SPAPR_MACHINE_NOASSERT() macro
---
 hw/ppc/spapr_pci.c |    7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)

diff --git a/hw/ppc/spapr_pci.c b/hw/ppc/spapr_pci.c
index 5049ced4e8b4..4d6a91ebb9c2 100644
--- a/hw/ppc/spapr_pci.c
+++ b/hw/ppc/spapr_pci.c
@@ -1507,7 +1507,7 @@ static void spapr_pci_unplug_request(HotplugHandler *plug_handler,
 
 static void spapr_phb_realize(DeviceState *dev, Error **errp)
 {
-    sPAPRMachineState *spapr = SPAPR_MACHINE(qdev_get_machine());
+    sPAPRMachineState *spapr = SPAPR_MACHINE_NOASSERT(qdev_get_machine());
     SysBusDevice *s = SYS_BUS_DEVICE(dev);
     sPAPRPHBState *sphb = SPAPR_PCI_HOST_BRIDGE(s);
     PCIHostState *phb = PCI_HOST_BRIDGE(s);
@@ -1519,6 +1519,11 @@ static void spapr_phb_realize(DeviceState *dev, Error **errp)
     const unsigned windows_supported =
         sphb->ddw_enabled ? SPAPR_PCI_DMA_MAX_WINDOWS : 1;
 
+    if (!spapr) {
+        error_setg(errp, TYPE_SPAPR_PCI_HOST_BRIDGE " needs a pseries machine");
+        return;
+    }
+
     if (sphb->index != (uint32_t)-1) {
         sPAPRMachineClass *smc = SPAPR_MACHINE_GET_CLASS(spapr);
         Error *local_err = NULL;

^ permalink raw reply related	[flat|nested] 5+ messages in thread

* Re: [Qemu-devel] [PATCH v2 1/2] spapr: introduce SPAPR_MACHINE_NOASSERT()
  2017-10-08 17:17 [Qemu-devel] [PATCH v2 1/2] spapr: introduce SPAPR_MACHINE_NOASSERT() Greg Kurz
  2017-10-08 17:17 ` [Qemu-devel] [PATCH v2 2/2] spapr_pci: fail gracefully with non-pseries machine types Greg Kurz
@ 2017-10-08 20:05 ` Peter Maydell
  2017-10-08 23:26   ` David Gibson
  1 sibling, 1 reply; 5+ messages in thread
From: Peter Maydell @ 2017-10-08 20:05 UTC (permalink / raw)
  To: Greg Kurz; +Cc: QEMU Developers, qemu-ppc, David Gibson

On 8 October 2017 at 18:17, Greg Kurz <groug@kaod.org> wrote:
> A spapr-cpu-core device can only be plugged into a pseries machine. This
> is checked in spapr_cpu_core_realize() with object_dynamic_cast() instead
> of the usual SPAPR_MACHINE() macro because we don't want QEMU to abort.
>
> This patch moves the gory details to a SPAPR_MACHINE_NOASSERT() macro
> that acts like the SPAPR_MACHINE() one, except it returns NULL instead
> of aborting if its argument doesn't point to a pseries machine type.
>
> This is done for two reasons:
> - it makes the code nicer
> - it may be used by other pseries-specific devices like PHBs for example
>
> Signed-off-by: Greg Kurz <groug@kaod.org>
> ---
>  hw/ppc/spapr_cpu_core.c |    7 +++----
>  include/hw/ppc/spapr.h  |    3 +++
>  2 files changed, 6 insertions(+), 4 deletions(-)
>
> diff --git a/hw/ppc/spapr_cpu_core.c b/hw/ppc/spapr_cpu_core.c
> index 37beb56e8b18..5fde07614218 100644
> --- a/hw/ppc/spapr_cpu_core.c
> +++ b/hw/ppc/spapr_cpu_core.c
> @@ -199,7 +199,7 @@ error:
>
>  static void spapr_cpu_core_realize(DeviceState *dev, Error **errp)
>  {
> -    sPAPRMachineState *spapr;
> +    sPAPRMachineState *spapr = SPAPR_MACHINE_NOASSERT(qdev_get_machine());
>      sPAPRCPUCore *sc = SPAPR_CPU_CORE(OBJECT(dev));
>      sPAPRCPUCoreClass *scc = SPAPR_CPU_CORE_GET_CLASS(OBJECT(dev));
>      CPUCore *cc = CPU_CORE(OBJECT(dev));
> @@ -209,9 +209,8 @@ static void spapr_cpu_core_realize(DeviceState *dev, Error **errp)
>      void *obj;
>      int i, j;
>
> -    spapr = (sPAPRMachineState *) qdev_get_machine();
> -    if (!object_dynamic_cast((Object *) spapr, TYPE_SPAPR_MACHINE)) {
> -        error_setg(errp, "spapr-cpu-core needs a pseries machine");
> +    if (!spapr) {
> +        error_setg(errp, TYPE_SPAPR_CPU_CORE " needs a pseries machine");
>          return;
>      }
>
> diff --git a/include/hw/ppc/spapr.h b/include/hw/ppc/spapr.h
> index c1b365f56431..4933da8083df 100644
> --- a/include/hw/ppc/spapr.h
> +++ b/include/hw/ppc/spapr.h
> @@ -43,6 +43,9 @@ typedef struct sPAPRMachineClass sPAPRMachineClass;
>  #define SPAPR_MACHINE_CLASS(klass) \
>      OBJECT_CLASS_CHECK(sPAPRMachineClass, klass, TYPE_SPAPR_MACHINE)
>
> +#define SPAPR_MACHINE_NOASSERT(obj) \
> +    ((sPAPRMachineState *) object_dynamic_cast(obj, TYPE_SPAPR_MACHINE))
> +

I don't think this is a great idea. Doing things with pointers
that might not be of the right type should be obvious, not hidden
under macros. An opencoded call to object_dynamic_cast is how the
rest of the codebase does this; it's a bit of a weird way
to write "isinstance()" but there you go. If we want to
improve the way we write this sort of thing we should do
so as a general improvement to the QOM APIs and conventions,
not just a single thing in SPAPR code.

thanks
-- PMM

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [Qemu-devel] [PATCH v2 1/2] spapr: introduce SPAPR_MACHINE_NOASSERT()
  2017-10-08 20:05 ` [Qemu-devel] [PATCH v2 1/2] spapr: introduce SPAPR_MACHINE_NOASSERT() Peter Maydell
@ 2017-10-08 23:26   ` David Gibson
  2017-10-09  5:50     ` Greg Kurz
  0 siblings, 1 reply; 5+ messages in thread
From: David Gibson @ 2017-10-08 23:26 UTC (permalink / raw)
  To: Peter Maydell; +Cc: Greg Kurz, QEMU Developers, qemu-ppc

[-- Attachment #1: Type: text/plain, Size: 3388 bytes --]

On Sun, Oct 08, 2017 at 09:05:37PM +0100, Peter Maydell wrote:
> On 8 October 2017 at 18:17, Greg Kurz <groug@kaod.org> wrote:
> > A spapr-cpu-core device can only be plugged into a pseries machine. This
> > is checked in spapr_cpu_core_realize() with object_dynamic_cast() instead
> > of the usual SPAPR_MACHINE() macro because we don't want QEMU to abort.
> >
> > This patch moves the gory details to a SPAPR_MACHINE_NOASSERT() macro
> > that acts like the SPAPR_MACHINE() one, except it returns NULL instead
> > of aborting if its argument doesn't point to a pseries machine type.
> >
> > This is done for two reasons:
> > - it makes the code nicer
> > - it may be used by other pseries-specific devices like PHBs for example
> >
> > Signed-off-by: Greg Kurz <groug@kaod.org>
> > ---
> >  hw/ppc/spapr_cpu_core.c |    7 +++----
> >  include/hw/ppc/spapr.h  |    3 +++
> >  2 files changed, 6 insertions(+), 4 deletions(-)
> >
> > diff --git a/hw/ppc/spapr_cpu_core.c b/hw/ppc/spapr_cpu_core.c
> > index 37beb56e8b18..5fde07614218 100644
> > --- a/hw/ppc/spapr_cpu_core.c
> > +++ b/hw/ppc/spapr_cpu_core.c
> > @@ -199,7 +199,7 @@ error:
> >
> >  static void spapr_cpu_core_realize(DeviceState *dev, Error **errp)
> >  {
> > -    sPAPRMachineState *spapr;
> > +    sPAPRMachineState *spapr = SPAPR_MACHINE_NOASSERT(qdev_get_machine());
> >      sPAPRCPUCore *sc = SPAPR_CPU_CORE(OBJECT(dev));
> >      sPAPRCPUCoreClass *scc = SPAPR_CPU_CORE_GET_CLASS(OBJECT(dev));
> >      CPUCore *cc = CPU_CORE(OBJECT(dev));
> > @@ -209,9 +209,8 @@ static void spapr_cpu_core_realize(DeviceState *dev, Error **errp)
> >      void *obj;
> >      int i, j;
> >
> > -    spapr = (sPAPRMachineState *) qdev_get_machine();
> > -    if (!object_dynamic_cast((Object *) spapr, TYPE_SPAPR_MACHINE)) {
> > -        error_setg(errp, "spapr-cpu-core needs a pseries machine");
> > +    if (!spapr) {
> > +        error_setg(errp, TYPE_SPAPR_CPU_CORE " needs a pseries machine");
> >          return;
> >      }
> >
> > diff --git a/include/hw/ppc/spapr.h b/include/hw/ppc/spapr.h
> > index c1b365f56431..4933da8083df 100644
> > --- a/include/hw/ppc/spapr.h
> > +++ b/include/hw/ppc/spapr.h
> > @@ -43,6 +43,9 @@ typedef struct sPAPRMachineClass sPAPRMachineClass;
> >  #define SPAPR_MACHINE_CLASS(klass) \
> >      OBJECT_CLASS_CHECK(sPAPRMachineClass, klass, TYPE_SPAPR_MACHINE)
> >
> > +#define SPAPR_MACHINE_NOASSERT(obj) \
> > +    ((sPAPRMachineState *) object_dynamic_cast(obj, TYPE_SPAPR_MACHINE))
> > +
> 
> I don't think this is a great idea. Doing things with pointers
> that might not be of the right type should be obvious, not hidden
> under macros. An opencoded call to object_dynamic_cast is how the
> rest of the codebase does this; it's a bit of a weird way
> to write "isinstance()" but there you go. If we want to
> improve the way we write this sort of thing we should do
> so as a general improvement to the QOM APIs and conventions,
> not just a single thing in SPAPR code.

Yeah, I tend to agree.  Sorry, the original advice I gave on not using
object_dynamic_cast() directly was bogus - I didn't think it through
clearly.

-- 
David Gibson			| I'll have my music baroque, and my code
david AT gibson.dropbear.id.au	| minimalist, thank you.  NOT _the_ _other_
				| _way_ _around_!
http://www.ozlabs.org/~dgibson

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 833 bytes --]

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [Qemu-devel] [PATCH v2 1/2] spapr: introduce SPAPR_MACHINE_NOASSERT()
  2017-10-08 23:26   ` David Gibson
@ 2017-10-09  5:50     ` Greg Kurz
  0 siblings, 0 replies; 5+ messages in thread
From: Greg Kurz @ 2017-10-09  5:50 UTC (permalink / raw)
  To: David Gibson; +Cc: Peter Maydell, QEMU Developers, qemu-ppc

[-- Attachment #1: Type: text/plain, Size: 3532 bytes --]

On Mon, 9 Oct 2017 10:26:38 +1100
David Gibson <david@gibson.dropbear.id.au> wrote:

> On Sun, Oct 08, 2017 at 09:05:37PM +0100, Peter Maydell wrote:
> > On 8 October 2017 at 18:17, Greg Kurz <groug@kaod.org> wrote:  
> > > A spapr-cpu-core device can only be plugged into a pseries machine. This
> > > is checked in spapr_cpu_core_realize() with object_dynamic_cast() instead
> > > of the usual SPAPR_MACHINE() macro because we don't want QEMU to abort.
> > >
> > > This patch moves the gory details to a SPAPR_MACHINE_NOASSERT() macro
> > > that acts like the SPAPR_MACHINE() one, except it returns NULL instead
> > > of aborting if its argument doesn't point to a pseries machine type.
> > >
> > > This is done for two reasons:
> > > - it makes the code nicer
> > > - it may be used by other pseries-specific devices like PHBs for example
> > >
> > > Signed-off-by: Greg Kurz <groug@kaod.org>
> > > ---
> > >  hw/ppc/spapr_cpu_core.c |    7 +++----
> > >  include/hw/ppc/spapr.h  |    3 +++
> > >  2 files changed, 6 insertions(+), 4 deletions(-)
> > >
> > > diff --git a/hw/ppc/spapr_cpu_core.c b/hw/ppc/spapr_cpu_core.c
> > > index 37beb56e8b18..5fde07614218 100644
> > > --- a/hw/ppc/spapr_cpu_core.c
> > > +++ b/hw/ppc/spapr_cpu_core.c
> > > @@ -199,7 +199,7 @@ error:
> > >
> > >  static void spapr_cpu_core_realize(DeviceState *dev, Error **errp)
> > >  {
> > > -    sPAPRMachineState *spapr;
> > > +    sPAPRMachineState *spapr = SPAPR_MACHINE_NOASSERT(qdev_get_machine());
> > >      sPAPRCPUCore *sc = SPAPR_CPU_CORE(OBJECT(dev));
> > >      sPAPRCPUCoreClass *scc = SPAPR_CPU_CORE_GET_CLASS(OBJECT(dev));
> > >      CPUCore *cc = CPU_CORE(OBJECT(dev));
> > > @@ -209,9 +209,8 @@ static void spapr_cpu_core_realize(DeviceState *dev, Error **errp)
> > >      void *obj;
> > >      int i, j;
> > >
> > > -    spapr = (sPAPRMachineState *) qdev_get_machine();
> > > -    if (!object_dynamic_cast((Object *) spapr, TYPE_SPAPR_MACHINE)) {
> > > -        error_setg(errp, "spapr-cpu-core needs a pseries machine");
> > > +    if (!spapr) {
> > > +        error_setg(errp, TYPE_SPAPR_CPU_CORE " needs a pseries machine");
> > >          return;
> > >      }
> > >
> > > diff --git a/include/hw/ppc/spapr.h b/include/hw/ppc/spapr.h
> > > index c1b365f56431..4933da8083df 100644
> > > --- a/include/hw/ppc/spapr.h
> > > +++ b/include/hw/ppc/spapr.h
> > > @@ -43,6 +43,9 @@ typedef struct sPAPRMachineClass sPAPRMachineClass;
> > >  #define SPAPR_MACHINE_CLASS(klass) \
> > >      OBJECT_CLASS_CHECK(sPAPRMachineClass, klass, TYPE_SPAPR_MACHINE)
> > >
> > > +#define SPAPR_MACHINE_NOASSERT(obj) \
> > > +    ((sPAPRMachineState *) object_dynamic_cast(obj, TYPE_SPAPR_MACHINE))
> > > +  
> > 
> > I don't think this is a great idea. Doing things with pointers
> > that might not be of the right type should be obvious, not hidden
> > under macros. An opencoded call to object_dynamic_cast is how the
> > rest of the codebase does this; it's a bit of a weird way
> > to write "isinstance()" but there you go. If we want to
> > improve the way we write this sort of thing we should do
> > so as a general improvement to the QOM APIs and conventions,
> > not just a single thing in SPAPR code.  
> 

Ok, I agree.

> Yeah, I tend to agree.  Sorry, the original advice I gave on not using
> object_dynamic_cast() directly was bogus - I didn't think it through
> clearly.
> 

Then maybe you can apply the previous version or do you have another
suggestion ?

[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 195 bytes --]

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2017-10-09  5:50 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2017-10-08 17:17 [Qemu-devel] [PATCH v2 1/2] spapr: introduce SPAPR_MACHINE_NOASSERT() Greg Kurz
2017-10-08 17:17 ` [Qemu-devel] [PATCH v2 2/2] spapr_pci: fail gracefully with non-pseries machine types Greg Kurz
2017-10-08 20:05 ` [Qemu-devel] [PATCH v2 1/2] spapr: introduce SPAPR_MACHINE_NOASSERT() Peter Maydell
2017-10-08 23:26   ` David Gibson
2017-10-09  5:50     ` Greg Kurz

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.