On Fri, Oct 25, 2024 at 11:01:56AM -0400, Peter Xu wrote:
> On Fri, Oct 25, 2024 at 09:38:31AM +0200, Markus Armbruster wrote:
> > Peter Xu <pet...@redhat.com> writes:
> > 
> > > This patchset introduces the singleton interface for QOM.
> > >
> > > The singleton interface is as simple as "this class can only create one
> > > instance".
> > >
> > > We used to have similar demand when working on all kinds of vIOMMUs,
> > > because in most cases that I am aware of, vIOMMU must be a singleton as
> > > it's closely bound to the machine and also the root PCIe systems.  We used
> > > to have a bunch of special code guarding those, for example, X86 has
> > > pc_machine_device_pre_plug_cb() just to detect when vIOMMU is created more
> > > than once.
> > >
> > > There is a similar demand raising recently (even if the problem existed
> > > over years) when we were looking at a migration bug, that part of it was
> > > involved with the current_migration global pointer being referenced
> > > anywhere, even after the migration object was finalize()ed.  So far 
> > > without
> > > singleton support, there's no way to reset the variable properly.
> > > Declaring migration object to be a singleton also just makes sense, e.g.,
> > > dynamically creating migration objects on the fly with QMP commands 
> > > doesn't
> > > sound right..
> > >
> > > The idea behind is pretty simple: any object that can only be created once
> > > can now declare the TYPE_SINGLETON interface, then QOM facilities will 
> > > make
> > > sure it won't be created more than once.  For example, 
> > > qom-list-properties,
> > > device-list-properties, etc., will be smart enough to not try to create
> > > temporary singleton objects now.
> > 
> > QOM design assumption: object_new() followed by object_unref() is always
> > possible and has no side effect.
> 
> I see.
> 
> > 
> > QOM introspection relies on this.  Your PATCH 1 makes non-class
> > properties of singletons invisible in introspection.  Making something
> > with such properties a singleton would be a regression.
> > 
> > Anything that violates the design assumption must be delayed to a
> > suitable state transition.  For devices (subtypes of TYPE_DEVICE), this
> > is ->realize().  For user-creatable objects (provide interface
> > TYPE_USER_CREATABLE), this is ->complete().  For anything else, it's
> > something else that probably doesn't even exist, yet.  See "Problem 5:
> > QOM lacks a clear life cycle" in
> > 
> >     Subject: Dynamic & heterogeneous machines, initial configuration: 
> > problems
> >     Date: Wed, 31 Jan 2024 21:14:21 +0100
> >     Message-ID: <87o7d1i7ky....@pond.sub.org>
> >     https://lore.kernel.org/qemu-devel/87o7d1i7ky....@pond.sub.org/
> 
> The major challenge here might be that, in migration's use case we want to
> do something after the last refcount is released.

This is rather a strange idea, and feels back to front. An object's last
refcount must never be released until code has entirely finished using
the object.

> IOW, I don't yet see an easy way to explicit do qdev_unrealize() (even if
> migration object will use realize(), while it doesn't yet..), because there
> can be more than one thread holding refcount of the object, and we don't
> know where to invoke it, and we don't want to do the cleanup if the other
> one still holds a refcount.

This sounds like the code is missing some synchronization mechanism
beween the threads, which need to communicate with each other when
they are "done", so that the "primary" thread can then finally
release any resources.

> Maybe I can also try do that with a "magic property" with its set/get, as
> that's indeed the other hook (basically, object_property_del_all()) that is
> only invoked after the final refcount is released.  However I think we
> still need the singleton idea somehow..

Based on the description above it feels like the problem is outside
of QOM, around the lack of synchronization across threads.

With regards,
Daniel
-- 
|: https://berrange.com      -o-    https://www.flickr.com/photos/dberrange :|
|: https://libvirt.org         -o-            https://fstop138.berrange.com :|
|: https://entangle-photo.org    -o-    https://www.instagram.com/dberrange :|


Reply via email to