Patchwork [04/11] qdev: add reference count to a device for the BusChild

login
register
mail settings
Submitter Paolo Bonzini
Date Dec. 5, 2012, 8:44 p.m.
Message ID <1354740282-20679-5-git-send-email-pbonzini@redhat.com>
Download mbox | patch
Permalink /patch/203940/
State New
Headers show

Comments

Paolo Bonzini - Dec. 5, 2012, 8:44 p.m.
Each device has a reference through the BusChild.  This reference
was not accounted for, add it now.

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
---
 hw/qdev.c | 5 +++++
 1 file changed, 5 insertions(+)
Anthony Liguori - Jan. 7, 2013, 8:26 p.m.
Paolo Bonzini <pbonzini@redhat.com> writes:

> Each device has a reference through the BusChild.  This reference
> was not accounted for, add it now.
>
> Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
> ---
>  hw/qdev.c | 5 +++++
>  1 file changed, 5 insertions(+)
>
> diff --git a/hw/qdev.c b/hw/qdev.c
> index e758131..87dfcb5 100644
> --- a/hw/qdev.c
> +++ b/hw/qdev.c
> @@ -65,7 +65,10 @@ static void bus_remove_child(BusState *bus, DeviceState *child)
>  
>              snprintf(name, sizeof(name), "child[%d]", kid->index);
>              QTAILQ_REMOVE(&bus->children, kid, sibling);
> +
> +            /* This gives back ownership of kid->child back to us.  */
>              object_property_del(OBJECT(bus), name, NULL);
> +            object_unref(OBJECT(kid->child));
>              g_free(kid);
>              return;
>          }
> @@ -83,9 +86,11 @@ static void bus_add_child(BusState *bus, DeviceState *child)
>  
>      kid->index = bus->max_index++;
>      kid->child = child;
> +    object_ref(OBJECT(kid->child));
>  
>      QTAILQ_INSERT_HEAD(&bus->children, kid, sibling);
>  
> +    /* This transfers ownership of kid->child to the property.  */
>      snprintf(name, sizeof(name), "child[%d]", kid->index);
>      object_property_add_link(OBJECT(bus), name,
>                               object_get_typename(OBJECT(child)),


The link property itself holds a reference no?  Or is this not the case
because we aren't checking a link when it's added and taking a reference
if it's !NULL?

Regards,

Anthony Liguori

> -- 
> 1.8.0.1
Paolo Bonzini - Jan. 9, 2013, 1:23 p.m.
Il 07/01/2013 21:26, Anthony Liguori ha scritto:
>> > +    object_ref(OBJECT(kid->child));
>> >  
>> >      QTAILQ_INSERT_HEAD(&bus->children, kid, sibling);
>> >  
>> > +    /* This transfers ownership of kid->child to the property.  */
>> >      snprintf(name, sizeof(name), "child[%d]", kid->index);
>> >      object_property_add_link(OBJECT(bus), name,
>> >                               object_get_typename(OBJECT(child)),
> 
> The link property itself holds a reference no?  Or is this not the case
> because we aren't checking a link when it's added and taking a reference
> if it's !NULL?

Yes.  I think it's okay this way though, and anyway a separate series
that would touch all object_property_add_link callers.

Paolo

Patch

diff --git a/hw/qdev.c b/hw/qdev.c
index e758131..87dfcb5 100644
--- a/hw/qdev.c
+++ b/hw/qdev.c
@@ -65,7 +65,10 @@  static void bus_remove_child(BusState *bus, DeviceState *child)
 
             snprintf(name, sizeof(name), "child[%d]", kid->index);
             QTAILQ_REMOVE(&bus->children, kid, sibling);
+
+            /* This gives back ownership of kid->child back to us.  */
             object_property_del(OBJECT(bus), name, NULL);
+            object_unref(OBJECT(kid->child));
             g_free(kid);
             return;
         }
@@ -83,9 +86,11 @@  static void bus_add_child(BusState *bus, DeviceState *child)
 
     kid->index = bus->max_index++;
     kid->child = child;
+    object_ref(OBJECT(kid->child));
 
     QTAILQ_INSERT_HEAD(&bus->children, kid, sibling);
 
+    /* This transfers ownership of kid->child to the property.  */
     snprintf(name, sizeof(name), "child[%d]", kid->index);
     object_property_add_link(OBJECT(bus), name,
                              object_get_typename(OBJECT(child)),