diff mbox series

[v2,10/16] hw/mips: Use object_initialize_child for correct reference counting

Message ID 20190507163416.24647-11-philmd@redhat.com (mailing list archive)
State New, archived
Headers show
Series hw: Use object_initialize_child for correct reference counting | expand

Commit Message

Philippe Mathieu-Daudé May 7, 2019, 4:34 p.m. UTC
As explained in commit aff39be0ed97:

  Both functions, object_initialize() and object_property_add_child()
  increase the reference counter of the new object, so one of the
  references has to be dropped afterwards to get the reference
  counting right. Otherwise the child object will not be properly
  cleaned up when the parent gets destroyed.
  Thus let's use now object_initialize_child() instead to get the
  reference counting here right.

This patch was generated using the following Coccinelle script:

 @use_sysbus_init_child_obj_missing_parent@
 expression child_ptr;
 expression child_type;
 expression child_size;
 @@
 -   object_initialize(child_ptr, child_size, child_type);
     ...
 -   qdev_set_parent_bus(DEVICE(child_ptr), sysbus_get_default());
     ...
 ?-  object_unref(OBJECT(child_ptr));
 +   sysbus_init_child_obj(OBJECT(PARENT_OBJ), "CHILD_NAME", child_ptr,
 +                         child_size, child_type);

We let the Malta/Boston machines adopt the CPS child, and similarly
the CPS adopts the ITU/CPC/GIC/GCR children.

While the object_initialize() function doesn't take an
'Error *errp' argument, the object_initialize_child() does.
Since this code is used when a machine is created (and is not
yet running), we deliberately choose to use the &error_abort
argument instead of ignoring errors if an object creation failed.
This choice also matches when using sysbus_init_child_obj(),
since its code is:

  void sysbus_init_child_obj(Object *parent,
                             const char *childname, void *child,
                             size_t childsize, const char *childtype)
  {
      object_initialize_child(parent, childname, child, childsize,
                              childtype, &error_abort, NULL);

      qdev_set_parent_bus(DEVICE(child), sysbus_get_default());
  }

Suggested-by: Eduardo Habkost <ehabkost@redhat.com>
Inspired-by: Thomas Huth <thuth@redhat.com>
Signed-off-by: Philippe Mathieu-Daudé <philmd@redhat.com>
---
 hw/mips/boston.c     |  4 ++--
 hw/mips/cps.c        | 20 ++++++++------------
 hw/mips/mips_malta.c |  4 ++--
 3 files changed, 12 insertions(+), 16 deletions(-)

Comments

Paolo Bonzini May 8, 2019, 11:16 a.m. UTC | #1
On 07/05/19 11:34, Philippe Mathieu-Daudé wrote:
> As explained in commit aff39be0ed97:
> 
>   Both functions, object_initialize() and object_property_add_child()
>   increase the reference counter of the new object, so one of the
>   references has to be dropped afterwards to get the reference
>   counting right. Otherwise the child object will not be properly
>   cleaned up when the parent gets destroyed.
>   Thus let's use now object_initialize_child() instead to get the
>   reference counting here right.
> 
> This patch was generated using the following Coccinelle script:
> 
>  @use_sysbus_init_child_obj_missing_parent@
>  expression child_ptr;
>  expression child_type;
>  expression child_size;
>  @@
>  -   object_initialize(child_ptr, child_size, child_type);
>      ...
>  -   qdev_set_parent_bus(DEVICE(child_ptr), sysbus_get_default());
>      ...
>  ?-  object_unref(OBJECT(child_ptr));
>  +   sysbus_init_child_obj(OBJECT(PARENT_OBJ), "CHILD_NAME", child_ptr,
>  +                         child_size, child_type);
> 
> We let the Malta/Boston machines adopt the CPS child, and similarly
> the CPS adopts the ITU/CPC/GIC/GCR children.
> 
> While the object_initialize() function doesn't take an
> 'Error *errp' argument, the object_initialize_child() does.
> Since this code is used when a machine is created (and is not
> yet running), we deliberately choose to use the &error_abort
> argument instead of ignoring errors if an object creation failed.
> This choice also matches when using sysbus_init_child_obj(),
> since its code is:
> 
>   void sysbus_init_child_obj(Object *parent,
>                              const char *childname, void *child,
>                              size_t childsize, const char *childtype)
>   {
>       object_initialize_child(parent, childname, child, childsize,
>                               childtype, &error_abort, NULL);
> 
>       qdev_set_parent_bus(DEVICE(child), sysbus_get_default());
>   }
> 
> Suggested-by: Eduardo Habkost <ehabkost@redhat.com>
> Inspired-by: Thomas Huth <thuth@redhat.com>
> Signed-off-by: Philippe Mathieu-Daudé <philmd@redhat.com>
> ---
>  hw/mips/boston.c     |  4 ++--
>  hw/mips/cps.c        | 20 ++++++++------------
>  hw/mips/mips_malta.c |  4 ++--
>  3 files changed, 12 insertions(+), 16 deletions(-)
> 
> diff --git a/hw/mips/boston.c b/hw/mips/boston.c
> index cb3ea85fdc1..1ffccc8da92 100644
> --- a/hw/mips/boston.c
> +++ b/hw/mips/boston.c
> @@ -455,8 +455,8 @@ static void boston_mach_init(MachineState *machine)
>  
>      is_64b = cpu_supports_isa(machine->cpu_type, ISA_MIPS64);
>  
> -    object_initialize(&s->cps, sizeof(s->cps), TYPE_MIPS_CPS);
> -    qdev_set_parent_bus(DEVICE(&s->cps), sysbus_get_default());
> +    sysbus_init_child_obj(OBJECT(machine), "cps", OBJECT(&s->cps),
> +                          sizeof(s->cps), TYPE_MIPS_CPS);
>      object_property_set_str(OBJECT(&s->cps), machine->cpu_type, "cpu-type",
>                              &err);
>      object_property_set_int(OBJECT(&s->cps), smp_cpus, "num-vp", &err);
> diff --git a/hw/mips/cps.c b/hw/mips/cps.c
> index fc97f59af4c..649b35a76c5 100644
> --- a/hw/mips/cps.c
> +++ b/hw/mips/cps.c
> @@ -94,9 +94,8 @@ static void mips_cps_realize(DeviceState *dev, Error **errp)
>  
>      /* Inter-Thread Communication Unit */
>      if (itu_present) {
> -        object_initialize(&s->itu, sizeof(s->itu), TYPE_MIPS_ITU);
> -        qdev_set_parent_bus(DEVICE(&s->itu), sysbus_get_default());
> -
> +        sysbus_init_child_obj(OBJECT(dev), "itu", &s->itu, sizeof(s->itu),
> +                              TYPE_MIPS_ITU);
>          object_property_set_int(OBJECT(&s->itu), 16, "num-fifo", &err);
>          object_property_set_int(OBJECT(&s->itu), 16, "num-semaphores", &err);
>          object_property_set_bool(OBJECT(&s->itu), saar_present, "saar-present",
> @@ -115,9 +114,8 @@ static void mips_cps_realize(DeviceState *dev, Error **errp)
>      }
>  
>      /* Cluster Power Controller */
> -    object_initialize(&s->cpc, sizeof(s->cpc), TYPE_MIPS_CPC);
> -    qdev_set_parent_bus(DEVICE(&s->cpc), sysbus_get_default());
> -
> +    sysbus_init_child_obj(OBJECT(dev), "cpc", &s->cpc, sizeof(s->cpc),
> +                          TYPE_MIPS_CPC);
>      object_property_set_int(OBJECT(&s->cpc), s->num_vp, "num-vp", &err);
>      object_property_set_int(OBJECT(&s->cpc), 1, "vp-start-running", &err);
>      object_property_set_bool(OBJECT(&s->cpc), true, "realized", &err);
> @@ -130,9 +128,8 @@ static void mips_cps_realize(DeviceState *dev, Error **errp)
>                              sysbus_mmio_get_region(SYS_BUS_DEVICE(&s->cpc), 0));
>  
>      /* Global Interrupt Controller */
> -    object_initialize(&s->gic, sizeof(s->gic), TYPE_MIPS_GIC);
> -    qdev_set_parent_bus(DEVICE(&s->gic), sysbus_get_default());
> -
> +    sysbus_init_child_obj(OBJECT(dev), "gic", &s->gic, sizeof(s->gic),
> +                          TYPE_MIPS_GIC);
>      object_property_set_int(OBJECT(&s->gic), s->num_vp, "num-vp", &err);
>      object_property_set_int(OBJECT(&s->gic), 128, "num-irq", &err);
>      object_property_set_bool(OBJECT(&s->gic), true, "realized", &err);
> @@ -147,9 +144,8 @@ static void mips_cps_realize(DeviceState *dev, Error **errp)
>      /* Global Configuration Registers */
>      gcr_base = env->CP0_CMGCRBase << 4;
>  
> -    object_initialize(&s->gcr, sizeof(s->gcr), TYPE_MIPS_GCR);
> -    qdev_set_parent_bus(DEVICE(&s->gcr), sysbus_get_default());
> -
> +    sysbus_init_child_obj(OBJECT(dev), "gcr", &s->gcr, sizeof(s->gcr),
> +                          TYPE_MIPS_GCR);
>      object_property_set_int(OBJECT(&s->gcr), s->num_vp, "num-vp", &err);
>      object_property_set_int(OBJECT(&s->gcr), 0x800, "gcr-rev", &err);
>      object_property_set_int(OBJECT(&s->gcr), gcr_base, "gcr-base", &err);
> diff --git a/hw/mips/mips_malta.c b/hw/mips/mips_malta.c
> index 04f2117d71e..aff8464f2ac 100644
> --- a/hw/mips/mips_malta.c
> +++ b/hw/mips/mips_malta.c
> @@ -1151,8 +1151,8 @@ static void create_cps(MaltaState *s, const char *cpu_type,
>  {
>      Error *err = NULL;
>  
> -    object_initialize(&s->cps, sizeof(s->cps), TYPE_MIPS_CPS);
> -    qdev_set_parent_bus(DEVICE(&s->cps), sysbus_get_default());
> +    sysbus_init_child_obj(OBJECT(s), "cps", OBJECT(&s->cps), sizeof(s->cps),
> +                          TYPE_MIPS_CPS);
>      object_property_set_str(OBJECT(&s->cps), cpu_type, "cpu-type", &err);
>      object_property_set_int(OBJECT(&s->cps), smp_cpus, "num-vp", &err);
>      object_property_set_bool(OBJECT(&s->cps), true, "realized", &err);
> 

Reviewed-by: Paolo Bonzini <pbonzini@redhat.com>
diff mbox series

Patch

diff --git a/hw/mips/boston.c b/hw/mips/boston.c
index cb3ea85fdc1..1ffccc8da92 100644
--- a/hw/mips/boston.c
+++ b/hw/mips/boston.c
@@ -455,8 +455,8 @@  static void boston_mach_init(MachineState *machine)
 
     is_64b = cpu_supports_isa(machine->cpu_type, ISA_MIPS64);
 
-    object_initialize(&s->cps, sizeof(s->cps), TYPE_MIPS_CPS);
-    qdev_set_parent_bus(DEVICE(&s->cps), sysbus_get_default());
+    sysbus_init_child_obj(OBJECT(machine), "cps", OBJECT(&s->cps),
+                          sizeof(s->cps), TYPE_MIPS_CPS);
     object_property_set_str(OBJECT(&s->cps), machine->cpu_type, "cpu-type",
                             &err);
     object_property_set_int(OBJECT(&s->cps), smp_cpus, "num-vp", &err);
diff --git a/hw/mips/cps.c b/hw/mips/cps.c
index fc97f59af4c..649b35a76c5 100644
--- a/hw/mips/cps.c
+++ b/hw/mips/cps.c
@@ -94,9 +94,8 @@  static void mips_cps_realize(DeviceState *dev, Error **errp)
 
     /* Inter-Thread Communication Unit */
     if (itu_present) {
-        object_initialize(&s->itu, sizeof(s->itu), TYPE_MIPS_ITU);
-        qdev_set_parent_bus(DEVICE(&s->itu), sysbus_get_default());
-
+        sysbus_init_child_obj(OBJECT(dev), "itu", &s->itu, sizeof(s->itu),
+                              TYPE_MIPS_ITU);
         object_property_set_int(OBJECT(&s->itu), 16, "num-fifo", &err);
         object_property_set_int(OBJECT(&s->itu), 16, "num-semaphores", &err);
         object_property_set_bool(OBJECT(&s->itu), saar_present, "saar-present",
@@ -115,9 +114,8 @@  static void mips_cps_realize(DeviceState *dev, Error **errp)
     }
 
     /* Cluster Power Controller */
-    object_initialize(&s->cpc, sizeof(s->cpc), TYPE_MIPS_CPC);
-    qdev_set_parent_bus(DEVICE(&s->cpc), sysbus_get_default());
-
+    sysbus_init_child_obj(OBJECT(dev), "cpc", &s->cpc, sizeof(s->cpc),
+                          TYPE_MIPS_CPC);
     object_property_set_int(OBJECT(&s->cpc), s->num_vp, "num-vp", &err);
     object_property_set_int(OBJECT(&s->cpc), 1, "vp-start-running", &err);
     object_property_set_bool(OBJECT(&s->cpc), true, "realized", &err);
@@ -130,9 +128,8 @@  static void mips_cps_realize(DeviceState *dev, Error **errp)
                             sysbus_mmio_get_region(SYS_BUS_DEVICE(&s->cpc), 0));
 
     /* Global Interrupt Controller */
-    object_initialize(&s->gic, sizeof(s->gic), TYPE_MIPS_GIC);
-    qdev_set_parent_bus(DEVICE(&s->gic), sysbus_get_default());
-
+    sysbus_init_child_obj(OBJECT(dev), "gic", &s->gic, sizeof(s->gic),
+                          TYPE_MIPS_GIC);
     object_property_set_int(OBJECT(&s->gic), s->num_vp, "num-vp", &err);
     object_property_set_int(OBJECT(&s->gic), 128, "num-irq", &err);
     object_property_set_bool(OBJECT(&s->gic), true, "realized", &err);
@@ -147,9 +144,8 @@  static void mips_cps_realize(DeviceState *dev, Error **errp)
     /* Global Configuration Registers */
     gcr_base = env->CP0_CMGCRBase << 4;
 
-    object_initialize(&s->gcr, sizeof(s->gcr), TYPE_MIPS_GCR);
-    qdev_set_parent_bus(DEVICE(&s->gcr), sysbus_get_default());
-
+    sysbus_init_child_obj(OBJECT(dev), "gcr", &s->gcr, sizeof(s->gcr),
+                          TYPE_MIPS_GCR);
     object_property_set_int(OBJECT(&s->gcr), s->num_vp, "num-vp", &err);
     object_property_set_int(OBJECT(&s->gcr), 0x800, "gcr-rev", &err);
     object_property_set_int(OBJECT(&s->gcr), gcr_base, "gcr-base", &err);
diff --git a/hw/mips/mips_malta.c b/hw/mips/mips_malta.c
index 04f2117d71e..aff8464f2ac 100644
--- a/hw/mips/mips_malta.c
+++ b/hw/mips/mips_malta.c
@@ -1151,8 +1151,8 @@  static void create_cps(MaltaState *s, const char *cpu_type,
 {
     Error *err = NULL;
 
-    object_initialize(&s->cps, sizeof(s->cps), TYPE_MIPS_CPS);
-    qdev_set_parent_bus(DEVICE(&s->cps), sysbus_get_default());
+    sysbus_init_child_obj(OBJECT(s), "cps", OBJECT(&s->cps), sizeof(s->cps),
+                          TYPE_MIPS_CPS);
     object_property_set_str(OBJECT(&s->cps), cpu_type, "cpu-type", &err);
     object_property_set_int(OBJECT(&s->cps), smp_cpus, "num-vp", &err);
     object_property_set_bool(OBJECT(&s->cps), true, "realized", &err);