All of lore.kernel.org
 help / color / mirror / Atom feed
From: "Daniel P. Berrangé" <berrange@redhat.com>
To: Peter Maydell <peter.maydell@linaro.org>
Cc: qemu-devel@nongnu.org, Paolo Bonzini <pbonzini@redhat.com>,
	Mark Cave-Ayland <mark.caveayland@nutanix.com>
Subject: Re: [PATCH] include/hw/core/qdev.h: Fix where device_class_set_parent_realize docs suggest putting parent_realize
Date: Fri, 16 Oct 2026 10:15:01 +0100	[thread overview]
Message-ID: <atHrFXxyIl3Nbvht@redhat.com> (raw)
In-Reply-To: <CAFEAcA-wEB15afO4Na8+8cF_Ja3v-8ewd8EdPavxJbfTmrL9GA@mail.gmail.com>

On Thu, Oct 15, 2026 at 07:16:58PM +0100, Peter Maydell wrote:
> On Thu, 15 Oct 2026 at 17:44, Daniel P. Berrangé <berrange@redhat.com> wrote:
> >
> > On Thu, Oct 15, 2026 at 05:33:52PM +0100, Peter Maydell wrote:
> > > The documentation for device_class_set_parent_realize() suggests that
> > > the parent realize function pointer is "usually in the instance
> > > structure".  This is definitely wrong, because the function pointer
> > > is a class thing, and should live in a class struct, not an instance
> > > struct.  Update the documentation to correct this, instead
> > > recommending that it should be placed in the class struct for the
> > > child class.
> > >
> > > At time of writing, of the 75 or so uses of this function, about 50
> > > put the parent_realize pointer in the child class struct, and about
> > > 25 in the parent class struct.  We recommend the former both because
> > > it is the more common choice, and also because it makes more logical
> > > sense: it's the child that knows (a) that it exists at all and that
> > > (b) it wants to have a realize method that will mean it needs to
> > > chain back to the parent, and we shouldn't need to edit the parent's
> > > class struct because we want to add a child class that chains to its
> > > realize method.
> >
> > I'm confused why we need to store a copy of the parent's
> > realize method at all, when we can just reference it from
> > the parent class struct ?
> >
> > Taking one example
> >
> >   static void pl050_kbd_realize(DeviceState *dev, Error **errp)
> >   {
> >       PL050DeviceClass *pdc = PL050_GET_CLASS(dev);
> >       PL050KbdState *s = PL050_KBD_DEVICE(dev);
> >       PL050State *ps = PL050(dev);
> >
> >       if (!sysbus_realize(SYS_BUS_DEVICE(&s->kbd), errp)) {
> >           return;
> >       }
> >
> >       ps->ps2dev = PS2_DEVICE(&s->kbd);
> >       pdc->parent_realize(dev, errp);
> >   }
> >
> > why wouldn't we rewrite that as
> >
> >   static void pl050_kbd_realize(DeviceState *dev, Error **errp)
> >   {
> >       PL050DeviceClass *pdc = PL050_GET_CLASS(dev);
> >       DeviceClass *parent = DEVICE_CLASS(object_class_get_parent(OBJECT_CLASS(pdc)));
> >       PL050KbdState *s = PL050_KBD_DEVICE(dev);
> >       PL050State *ps = PL050(dev);
> >
> >       if (!sysbus_realize(SYS_BUS_DEVICE(&s->kbd), errp)) {
> >           return;
> >       }
> >
> >       ps->ps2dev = PS2_DEVICE(&s->kbd);
> >       parent->realize(dev, errp);
> >   }
> >
> > am I missing a reason we need to store the parent's
> > realize method pointer ?
> 
> Because the parent class's struct doesn't have a pointer to
> its realize method. pl050_class_init() does this to set
> up its realize method:
> 
>     DeviceClass *dc = DEVICE_CLASS(oc);
> 
>     dc->realize = pl050_realize;
> 
> and that's the only place where pl050_realize is pointed to.
> 
> To put the child class's realize method into place we need
> to overwrite the DeviceClass::realize field, which means we
> need somewhere to put the old value.

We're overwriting the DeviceClass::realize field in the class
struct instance for "pl050_keyboard". We still have the completely
independent class struct instance for "pl050" (which is what
object_class_get_parent() returns) that contains the 'realize'
field value set by the parent class.

eg if I put a break on pl050_kbd_realize():

  (gdb) print pdc->parent_class->parent_class->parent_class->type->name
  $5 = 0x555557de40a0 "pl050_keyboard"
  (gdb) print pdc->parent_class->parent_class->realize
  $6 = (DeviceRealize) 0x55555585db67 <pl050_kbd_realize>

and separately

  (gdb) print ((DeviceClass*)(object_class_get_parent(pdc)))->parent_class->type->name
  $7 = 0x555557de3ee0 "pl050"
  (gdb) print ((DeviceClass*)(object_class_get_parent(pdc)))->realize
  $8 = (DeviceRealize) 0x55555585daf4 <pl050_realize>

> We could alternatively solve this by saying:
>  (a) every class struct must have a "realize" field,
>      and the class init sets both DeviceClass::realize and
>      MyClass::realize (perhaps restrict this to only "every
>      class that has or might need to have subclasses")
>  (b) to chain to the superclass realize, get the MyParentClass
>      class pointer and call MyParentClass::realize
> 
> The support (or lack thereof) in QOM for methods like realize,
> unrealize and reset which should pretty much always chain to
> their parent class implementation is pretty terrible, so if
> we can come up with something more ergonomic I'd be happy to
> throw out the hacks we have today.

AFAICT, we can already do everything we need with the parent class
to chain up parent virtual methods without having  to keep extra
pointers around.

With regards,
Daniel
-- 
|: https://berrange.com       ~~        https://hachyderm.io/@berrange :|
|: https://libvirt.org          ~~          https://entangle-photo.org :|
|: https://pixelfed.art/berrange   ~~    https://fstop138.berrange.com :|



  reply	other threads:[~2026-10-16  9:16 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-10-15 16:33 [PATCH] include/hw/core/qdev.h: Fix where device_class_set_parent_realize docs suggest putting parent_realize Peter Maydell
2026-10-15 16:44 ` Daniel P. Berrangé
2026-10-15 18:16   ` Peter Maydell
2026-10-16  9:15     ` Daniel P. Berrangé [this message]
2026-10-16  9:30       ` Daniel P. Berrangé
2026-10-16 10:14         ` Peter Maydell
2026-10-16 10:53           ` Daniel P. Berrangé
2026-10-16 14:09             ` Peter Maydell
2026-10-16 14:26               ` Daniel P. Berrangé
2026-10-16 14:30                 ` Peter Maydell
2026-10-16 13:11           ` Mark Cave-Ayland
2026-10-16 13:07         ` Mark Cave-Ayland
2026-10-16  9:09 ` Mark Cave-Ayland

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=atHrFXxyIl3Nbvht@redhat.com \
    --to=berrange@redhat.com \
    --cc=mark.caveayland@nutanix.com \
    --cc=pbonzini@redhat.com \
    --cc=peter.maydell@linaro.org \
    --cc=qemu-devel@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.