Skip to contentSkip to conversation

hw/arm/cubieboard: make sure SOC object isn't leaked

PATCH Applied Igor Mammedov 4 participants 7 messages Mar 3, 2020 – Mar 12, 2020

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 hw/arm/cubieboard: make sure SOC object isn't leaked

SOC object returned by object_new() is leaked in current code. Set SOC parent explicitly to board and then unref to SOC object to make sure that refererence returned by object_new() is taken care of.

The SOC object will be kept alive by its parent (machine) and will be automatically freed when MachineState is destroyed.

---
 hw/arm/cubieboard.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
hw/arm/cubieboard.c +6 -1
index 089f9a30c1..12f8ac798d 100644
@@ -30,9 +30,14 @@ static struct arm_boot_info cubieboard_binfo = {
 
 static void cubieboard_init(MachineState *machine)
 {
-    AwA10State *a10 = AW_A10(object_new(TYPE_AW_A10));
+    AwA10State *a10;
     Error *err = NULL;
 
+    a10 = AW_A10(object_new(TYPE_AW_A10));
+    object_property_add_child(OBJECT(machine), "soc", OBJECT(a10),
+                              &error_abort);
+    object_unref(OBJECT(a10));
+
     object_property_set_int(OBJECT(&a10->emac), 1, "phy-addr", &err);
     if (err != NULL) {
         error_reportf_err(err, "Couldn't set phy address: ");
Signature · 1 line
2.18.1
#1 lore raw

Hi Igor,

Quoted 8 lines · Igor Mammedov

SOC object returned by object_new() is leaked in current code. Set SOC parent explicitly to board and then unref to SOC object to make sure that refererence returned by object_new() is taken care of.

The SOC object will be kept alive by its parent (machine) and will be automatically freed when MachineState is destroyed.

Thanks for reporting this issue. In the Allwinner H3 series, I actually inherited this pattern as well,
so I'll include this fix as well in the next version of that series.

Quoted 14 lines
---
 hw/arm/cubieboard.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
hw/arm/cubieboard.c +0 -0
index 089f9a30c1..12f8ac798d 100644
@@ -30,9 +30,14 @@ static struct arm_boot_info cubieboard_binfo = {
 
 static void cubieboard_init(MachineState *machine)

hw/arm/cubieboard.c static struct arm_boot_info cubieboard_binfo = { · line 40

Quoted 10 lines
hw/arm/cubieboard.c +5 -1
 {
-    AwA10State *a10 = AW_A10(object_new(TYPE_AW_A10));
+    AwA10State *a10;
     Error *err = NULL;
 
+    a10 = AW_A10(object_new(TYPE_AW_A10));
+    object_property_add_child(OBJECT(machine), "soc", OBJECT(a10),
+                              &error_abort);
+    object_unref(OBJECT(a10));
 

I see that there are a few machines which also do object_property_add_child() to add its
SoC object to the machine, but they do not do the object_unref(). Can you explain why it is needed here?
Or do the other machines still have a leak due to the missing object_unref()? Examples are:
hw/arm/sabrelite.c
hw/arm/mcimx7d-sabre.c
hw/arm/mcimx6ul-evk.c

Regards,
Niek

Quoted 9 lines
hw/arm/cubieboard.c +1 -0
+
     object_property_set_int(OBJECT(&a10->emac), 1, "phy-addr", &err);
     if (err != NULL) {
         error_reportf_err(err, "Couldn't set phy address: ");

--
2.18.1

Signature · 1 line
Niek Linnenbank
#2 lore raw
Quoted 2 lines · On Thu, 5 Mar 2020 21:07:08 +0100 Niek Linnenbank <[email protected]>…

Hi Igor,

[...]

Quoted 10 lines

hw/arm/cubieboard.c static struct arm_boot_info cubieboard_binfo = { · line 39

Quoted 5 lines
hw/arm/cubieboard.c +4 -0
+    a10 = AW_A10(object_new(TYPE_AW_A10));
+    object_property_add_child(OBJECT(machine), "soc", OBJECT(a10),
+                              &error_abort);
+    object_unref(OBJECT(a10));

I see that there are a few machines which also do object_property_add_child() to add its
SoC object to the machine, but they do not do the object_unref(). Can you explain why it is needed here?

object_new() returns object with ref == 1, and it's responsibility of the caller to take care of it.
If caller won't track and call object_unref() eventually on returned pointer, the object will leak.

(object_unref() destroys object when 'ref' reaches 0 )

Quoted 20 lines

Or do the other machines still have a leak due to the missing object_unref()? Examples are:
hw/arm/sabrelite.c
hw/arm/mcimx7d-sabre.c
hw/arm/mcimx6ul-evk.c

Regards,
Niek

Quoted 9 lines

+

     object_property_set_int(OBJECT(&a10->emac), 1, "phy-addr", &err);
     if (err != NULL) {
         error_reportf_err(err, "Couldn't set phy address: ");

--
2.18.1

#3 lore raw
In reply to Igor Mammedov
Quoted 19 lines · Igor Mammedov

SOC object returned by object_new() is leaked in current code. Set SOC parent explicitly to board and then unref to SOC object to make sure that refererence returned by object_new() is taken care of.

The SOC object will be kept alive by its parent (machine) and will be automatically freed when MachineState is destroyed.

---
  hw/arm/cubieboard.c | 7 ++++++-
  1 file changed, 6 insertions(+), 1 deletion(-)
hw/arm/cubieboard.c +0 -0
index 089f9a30c1..12f8ac798d 100644

hw/arm/cubieboard.c static struct arm_boot_info cubieboard_binfo = { · line 35

Quoted 7 lines
hw/arm/cubieboard.c +1 -1
@@ -30,9 +30,14 @@ static struct arm_boot_info cubieboard_binfo = {
 
  static void cubieboard_init(MachineState *machine)
  {
-    AwA10State *a10 = AW_A10(object_new(TYPE_AW_A10));
+    AwA10State *a10;
      Error *err = NULL;
 

What about using object_new_with_props similarly to commit e4c81e3a451f?

    a10 = AW_A10(object_new_with_props(TYPE_AW_A10, OBJECT(s), "soc",
                                       &err, NULL));
    if (err != NULL) {
        error_propagate(errp, err);
        return;
    }
Quoted 9 lines
hw/arm/cubieboard.c +5 -0
+    a10 = AW_A10(object_new(TYPE_AW_A10));
+    object_property_add_child(OBJECT(machine), "soc", OBJECT(a10),
+                              &error_abort);
+    object_unref(OBJECT(a10));
+
      object_property_set_int(OBJECT(&a10->emac), 1, "phy-addr", &err);
      if (err != NULL) {
          error_reportf_err(err, "Couldn't set phy address: ");
#4 lore raw
Quoted 21 lines · On Fri, 6 Mar 2020 11:33:07 +0100 Philippe Mathieu-Daudé <[email protected]>…
Quoted 20 lines · Igor Mammedov

SOC object returned by object_new() is leaked in current code. Set SOC parent explicitly to board and then unref to SOC object to make sure that refererence returned by object_new() is taken care of.

The SOC object will be kept alive by its parent (machine) and will be automatically freed when MachineState is destroyed.

---
  hw/arm/cubieboard.c | 7 ++++++-
  1 file changed, 6 insertions(+), 1 deletion(-)
hw/arm/cubieboard.c +0 -0
index 089f9a30c1..12f8ac798d 100644
@@ -30,9 +30,14 @@ static struct arm_boot_info cubieboard_binfo = {
 
Quoted 8 lines

hw/arm/cubieboard.c static struct arm_boot_info cubieboard_binfo = { · line 35

Quoted 6 lines
hw/arm/cubieboard.c +1 -1
  static void cubieboard_init(MachineState *machine)
  {
-    AwA10State *a10 = AW_A10(object_new(TYPE_AW_A10));
+    AwA10State *a10;
      Error *err = NULL;
 

What about using object_new_with_props similarly to commit e4c81e3a451f?

I prefer explicit object_property_add_child() and would use above only if there are list of properties to feed to it.

Quoted 17 lines
     a10 = AW_A10(object_new_with_props(TYPE_AW_A10, OBJECT(s), "soc",
                                        &err, NULL));
     if (err != NULL) {
         error_propagate(errp, err);
         return;
     }
Quoted 9 lines
hw/arm/cubieboard.c +5 -0
+    a10 = AW_A10(object_new(TYPE_AW_A10));
+    object_property_add_child(OBJECT(machine), "soc", OBJECT(a10),
+                              &error_abort);
+    object_unref(OBJECT(a10));
+
      object_property_set_int(OBJECT(&a10->emac), 1, "phy-addr", &err);
      if (err != NULL) {
          error_reportf_err(err, "Couldn't set phy address: ");
Tested-by
#5 lore raw

Hi Igor,

Quoted 32 lines · Igor Mammedov
Quoted 21 lines · On Fri, 6 Mar 2020 11:33:07 +0100 Philippe Mathieu-Daudé <[email protected]>…
Quoted 20 lines · Igor Mammedov

SOC object returned by object_new() is leaked in current code. Set SOC parent explicitly to board and then unref to SOC object to make sure that refererence returned by object_new() is taken care of.

The SOC object will be kept alive by its parent (machine) and will be automatically freed when MachineState is destroyed.

---
  hw/arm/cubieboard.c | 7 ++++++-
  1 file changed, 6 insertions(+), 1 deletion(-)
hw/arm/cubieboard.c +0 -0
index 089f9a30c1..12f8ac798d 100644
@@ -30,9 +30,14 @@ static struct arm_boot_info cubieboard_binfo = {
 
Quoted 8 lines

hw/arm/cubieboard.c static struct arm_boot_info cubieboard_binfo = { · line 35

Quoted 6 lines
hw/arm/cubieboard.c +1 -1
  static void cubieboard_init(MachineState *machine)
  {
-    AwA10State *a10 = AW_A10(object_new(TYPE_AW_A10));
+    AwA10State *a10;
      Error *err = NULL;
 

What about using object_new_with_props similarly to commit e4c81e3a451f?

Quoted 16 lines

I prefer explicit object_property_add_child() and would use above only if there are list of properties to feed to it.

Quoted 11 lines
     a10 = AW_A10(object_new_with_props(TYPE_AW_A10, OBJECT(s), "soc",
                                        &err, NULL));
     if (err != NULL) {
         error_propagate(errp, err);
         return;
     }
Quoted 4 lines
hw/arm/cubieboard.c +4 -0
+    a10 = AW_A10(object_new(TYPE_AW_A10));
+    object_property_add_child(OBJECT(machine), "soc", OBJECT(a10),
+                              &error_abort);
+    object_unref(OBJECT(a10));

Thanks, I added this same solution to the Allwinner H3 series, and its working fine there.

Quoted 9 lines
Quoted 6 lines
Quoted 5 lines
hw/arm/cubieboard.c +1 -0
+
      object_property_set_int(OBJECT(&a10->emac), 1, "phy-addr", &err);
      if (err != NULL) {
          error_reportf_err(err, "Couldn't set phy address: ");
Signature · 1 line
Niek Linnenbank
#6 lore raw
In reply to Igor Mammedov
Quoted 6 lines · Igor Mammedov

SOC object returned by object_new() is leaked in current code. Set SOC parent explicitly to board and then unref to SOC object to make sure that refererence returned by object_new() is taken care of.

Quoted 9 lines

The SOC object will be kept alive by its parent (machine) and will be automatically freed when MachineState is destroyed.

---
 hw/arm/cubieboard.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)

Applied to target-arm.next, thanks.

-- PMM