All of lore.kernel.org
 help / color / mirror / Atom feed
From: Greg Kurz <groug@kaod.org>
To: David Gibson <david@gibson.dropbear.id.au>
Cc: Peter Maydell <peter.maydell@linaro.org>,
	QEMU Developers <qemu-devel@nongnu.org>,
	"qemu-ppc@nongnu.org" <qemu-ppc@nongnu.org>
Subject: Re: [Qemu-devel] [PATCH v2 1/2] spapr: introduce SPAPR_MACHINE_NOASSERT()
Date: Mon, 9 Oct 2017 07:50:33 +0200	[thread overview]
Message-ID: <20171009075033.75c152aa@bahia.lan> (raw)
In-Reply-To: <20171008232638.GN10050@umbus.fritz.box>

[-- 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 --]

      reply	other threads:[~2017-10-09  5:50 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
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 message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20171009075033.75c152aa@bahia.lan \
    --to=groug@kaod.org \
    --cc=david@gibson.dropbear.id.au \
    --cc=peter.maydell@linaro.org \
    --cc=qemu-devel@nongnu.org \
    --cc=qemu-ppc@nongnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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.