Skip to contentSkip to conversation

qom/object properties cleanups

PATCH In review [email protected] 3 participants 15 messages Apr 29, 2025 Arm/Amlogic Meson SoC support

lore
Share & tools
lore
#0 lore raw

cover qom/object properties cleanups

From: Marc-André Lureau <[email protected]>

Hi,

While reviewing some related code, I noticed array properties are leaking.

I also wonder why I refactored object_property_del_all() with a HashTable, and whether it's really necessary. Here is also a RFC to simplify that code.

Marc-André Lureau (6):
qdev: make release_string() idempotent
qdev: make release_drive() idempotent
qdev: make release_tpm() idempotent
qdev: make release_prop_array() free array and idempotent
tests: add /qdev/free-properties test
RFC: qom/object: simplify object_property_del_all()

 backends/tpm/tpm_util.c          |  1 +
 hw/core/qdev-properties-system.c |  1 +
 hw/core/qdev-properties.c        | 18 +++---
 qom/object.c                     | 18 ++----
 tests/unit/test-qdev.c           | 96 ++++++++++++++++++++++++++++++++
 tests/unit/meson.build           |  1 +
 6 files changed, 114 insertions(+), 21 deletions(-)
 create mode 100644 tests/unit/test-qdev.c
Signature · 1 line
2.49.0
#1 lore raw

PATCH 1/6 qdev: make release_string() idempotent

From: Marc-André Lureau <[email protected]>

So it can eventually be called multiple times safely.

---
 hw/core/qdev-properties.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
hw/core/qdev-properties.c +2 -1
index 147b3ffd16..bad111833c 100644
@@ -484,7 +484,8 @@ const PropertyInfo qdev_prop_usize = {
 static void release_string(Object *obj, const char *name, void *opaque)
 {
     const Property *prop = opaque;
-    g_free(*(char **)object_field_prop_ptr(obj, prop));
+
+    g_clear_pointer((char **)object_field_prop_ptr(obj, prop), g_free);
 }
 
 static void get_string(Object *obj, Visitor *v, const char *name,
Signature · 1 line
2.49.0
Reviewed-by on v1 1/6
#2 lore raw

Re: [PATCH 1/6] qdev: make release_string() idempotent

Quoted 14 lines · On 4/29/25 10:03 AM, [email protected] wrote:

From: Marc-André Lureau <[email protected]>

So it can eventually be called multiple times safely.

---
  hw/core/qdev-properties.c | 3 ++-
  1 file changed, 2 insertions(+), 1 deletion(-)
hw/core/qdev-properties.c +0 -0
index 147b3ffd16..bad111833c 100644

hw/core/qdev-properties.c const PropertyInfo qdev_prop_usize = { · line 491

Quoted 9 lines
hw/core/qdev-properties.c +2 -1
@@ -484,7 +484,8 @@ const PropertyInfo qdev_prop_usize = {
  static void release_string(Object *obj, const char *name, void *opaque)
  {
      const Property *prop = opaque;
-    g_free(*(char **)object_field_prop_ptr(obj, prop));
+
+    g_clear_pointer((char **)object_field_prop_ptr(obj, prop), g_free);
  }
 
  static void get_string(Object *obj, Visitor *v, const char *name,
#3 lore raw

PATCH 2/6 qdev: make release_drive() idempotent

In reply to [email protected]

From: Marc-André Lureau <[email protected]>

So it can eventually be called multiple times safely.

---
 hw/core/qdev-properties-system.c | 1 +
 1 file changed, 1 insertion(+)
hw/core/qdev-properties-system.c +1 -0
index 8e11e6388b..b10835185c 100644
@@ -231,6 +231,7 @@ static void release_drive(Object *obj, const char *name, void *opaque)
     if (*ptr) {
         blockdev_auto_del(*ptr);
         blk_detach_dev(*ptr, dev);
+        *ptr = NULL;
     }
 }
 
Signature · 1 line
2.49.0
Reviewed-by on v1 2/6
#4 lore raw

Re: [PATCH 2/6] qdev: make release_drive() idempotent

Quoted 14 lines · On 4/29/25 10:03 AM, [email protected] wrote:

From: Marc-André Lureau <[email protected]>

So it can eventually be called multiple times safely.

---
  hw/core/qdev-properties-system.c | 1 +
  1 file changed, 1 insertion(+)
hw/core/qdev-properties-system.c +0 -0
index 8e11e6388b..b10835185c 100644

hw/core/qdev-properties-system.c static void release_drive(Object *obj, const char *name, void *opaque) · line 237

Quoted 7 lines
hw/core/qdev-properties-system.c +1 -0
@@ -231,6 +231,7 @@ static void release_drive(Object *obj, const char *name, void *opaque)
      if (*ptr) {
          blockdev_auto_del(*ptr);
          blk_detach_dev(*ptr, dev);
+        *ptr = NULL;
      }
  }
 
#5 lore raw

PATCH 3/6 qdev: make release_tpm() idempotent

In reply to [email protected]

From: Marc-André Lureau <[email protected]>

So it can eventually be called multiple times safely.

---
 backends/tpm/tpm_util.c | 1 +
 1 file changed, 1 insertion(+)
backends/tpm/tpm_util.c +1 -0
index f2d1739e33..16eab82cd2 100644
@@ -72,6 +72,7 @@ static void release_tpm(Object *obj, const char *name, void *opaque)
 
     if (*be) {
         tpm_backend_reset(*be);
+        *be = NULL;
     }
 }
 
Signature · 1 line
2.49.0
Reviewed-by on v1 3/6
#6 lore raw

Re: [PATCH 3/6] qdev: make release_tpm() idempotent

Quoted 14 lines · On 4/29/25 10:03 AM, [email protected] wrote:

From: Marc-André Lureau <[email protected]>

So it can eventually be called multiple times safely.

---
  backends/tpm/tpm_util.c | 1 +
  1 file changed, 1 insertion(+)
backends/tpm/tpm_util.c +0 -0
index f2d1739e33..16eab82cd2 100644

backends/tpm/tpm_util.c static void release_tpm(Object *obj, const char *name, void *opaque) · line 78

Quoted 7 lines
backends/tpm/tpm_util.c +1 -0
@@ -72,6 +72,7 @@ static void release_tpm(Object *obj, const char *name, void *opaque)
 
      if (*be) {
          tpm_backend_reset(*be);
+        *be = NULL;
      }
  }
 
#7 lore raw

PATCH 4/6 qdev: make release_prop_array() free array and idempotent

In reply to [email protected]

From: Marc-André Lureau <[email protected]>

Fix a memory leak, and allow it to be called multiple times safely.

---
 hw/core/qdev-properties.c | 15 ++++++++-------
 1 file changed, 8 insertions(+), 7 deletions(-)
hw/core/qdev-properties.c +8 -7
index bad111833c..7a53c85887 100644
@@ -620,15 +620,16 @@ static void release_prop_array(Object *obj, const char *name, void *opaque)
     char *elem = *arrayptr;
     int i;
 
-    if (!prop->arrayinfo->release) {
-        return;
+    if (prop->arrayinfo->release) {
+        for (i = 0; i < *alenptr; i++) {
+            Property elem_prop = array_elem_prop(obj, prop, name, elem);
+            prop->arrayinfo->release(obj, NULL, &elem_prop);
+            elem += prop->arrayfieldsize;
+        }
     }
 
-    for (i = 0; i < *alenptr; i++) {
-        Property elem_prop = array_elem_prop(obj, prop, name, elem);
-        prop->arrayinfo->release(obj, NULL, &elem_prop);
-        elem += prop->arrayfieldsize;
-    }
+    *alenptr = 0;
+    g_clear_pointer(arrayptr, g_free);
 }
 
 /*
Signature · 1 line
2.49.0
Reviewed-by on v1 4/6
#8 lore raw

Re: [PATCH 4/6] qdev: make release_prop_array() free array and idempotent

Quoted 27 lines · On 4/29/25 10:03 AM, [email protected] wrote:

From: Marc-André Lureau <[email protected]>

Fix a memory leak, and allow it to be called multiple times safely.

---
  hw/core/qdev-properties.c | 15 ++++++++-------
  1 file changed, 8 insertions(+), 7 deletions(-)
hw/core/qdev-properties.c +6 -2
index bad111833c..7a53c85887 100644
@@ -620,15 +620,16 @@ static void release_prop_array(Object *obj, const char *name, void *opaque)
      char *elem = *arrayptr;
      int i;
 
-    if (!prop->arrayinfo->release) {
-        return;
+    if (prop->arrayinfo->release) {
+        for (i = 0; i < *alenptr; i++) {
+            Property elem_prop = array_elem_prop(obj, prop, name, elem);
+            prop->arrayinfo->release(obj, NULL, &elem_prop);
+            elem += prop->arrayfieldsize;
+        }
      }
 

hw/core/qdev-properties.c static void release_prop_array(Object *obj, const char *name, void *opaque) · line 635

Quoted 10 lines
hw/core/qdev-properties.c +2 -5
-    for (i = 0; i < *alenptr; i++) {
-        Property elem_prop = array_elem_prop(obj, prop, name, elem);
-        prop->arrayinfo->release(obj, NULL, &elem_prop);
-        elem += prop->arrayfieldsize;
-    }
+    *alenptr = 0;
+    g_clear_pointer(arrayptr, g_free);
  }
 
  /*
#9 lore raw

PATCH 5/6 tests: add /qdev/free-properties test

In reply to [email protected]

From: Marc-André Lureau <[email protected]>

Add a simple qdev test to check that allocated properties get free with the object. This test exhibited array leaks before the fixes.

---
 tests/unit/test-qdev.c | 96 ++++++++++++++++++++++++++++++++++++++++++
 tests/unit/meson.build |  1 +
 2 files changed, 97 insertions(+)
 create mode 100644 tests/unit/test-qdev.c
tests/unit/test-qdev.c new file +96 -0
new file mode 100644
index 0000000000..2b4c9dd643
@@ -0,0 +1,96 @@
+#include "qemu/osdep.h"
+
+#include "hw/qdev-properties.h"
+#include "qom/object.h"
+#include "qapi/error.h"
+#include "qapi/visitor.h"
+
+
+#define TYPE_MY_DEV "my-dev"
+typedef struct MyDev MyDev;
+DECLARE_INSTANCE_CHECKER(MyDev, STATIC_TYPE,
+                         TYPE_MY_DEV)
+
+struct MyDev {
+    DeviceState parent_obj;
+
+    uint32_t prop_u32;
+    char *prop_string;
+    uint32_t *prop_array_u32;
+    uint32_t prop_array_u32_nb;
+};
+
+static const Property my_dev_props[] = {
+    DEFINE_PROP_UINT32("u32", MyDev, prop_u32, 100),
+    DEFINE_PROP_STRING("string", MyDev, prop_string),
+    DEFINE_PROP_ARRAY("array-u32", MyDev, prop_array_u32_nb, prop_array_u32, qdev_prop_uint32, uint32_t),
+};
+
+static void my_dev_class_init(ObjectClass *oc, const void *data)
+{
+    DeviceClass *dc = DEVICE_CLASS(oc);
+
+    dc->realize = NULL;
+    device_class_set_props(dc, my_dev_props);
+}
+
+static const TypeInfo my_dev_type_info = {
+    .name = TYPE_MY_DEV,
+    .parent = TYPE_DEVICE,
+    .instance_size = sizeof(MyDev),
+    .class_init = my_dev_class_init,
+};
+
+/*
+ * Initialize a fake machine, being prepared for future tests.
+ *
+ * Realization of anonymous qdev (with no parent object) requires both
+ * the machine object and its "unattached" container to be at least present.
+ */
+static void test_init_machine(void)
+{
+    /* This is a fake machine - it doesn't need to be a machine object */
+    Object *machine = object_property_add_new_container(
+        object_get_root(), "machine");
+
+    /* This container must exist for anonymous qdevs to realize() */
+    object_property_add_new_container(machine, "unattached");
+}
+
+static void test_qdev_free_properties(void)
+{
+    MyDev *mt;
+
+    mt = STATIC_TYPE(object_new(TYPE_MY_DEV));
+    object_set_props(OBJECT(mt), &error_fatal,
+                     "string", "something",
+                     "array-u32", "12,13",
+                     NULL);
+    qdev_realize(DEVICE(mt), NULL, &error_fatal);
+
+    g_assert_cmpuint(mt->prop_u32, ==, 100);
+    g_assert_cmpstr(mt->prop_string, ==, "something");
+    g_assert_cmpuint(mt->prop_array_u32_nb, ==, 2);
+    g_assert_cmpuint(mt->prop_array_u32[0], ==, 12);
+    g_assert_cmpuint(mt->prop_array_u32[1], ==, 13);
+
+    object_unparent(OBJECT(mt));
+    object_unref(mt);
+}
+
+
+int main(int argc, char **argv)
+{
+    g_test_init(&argc, &argv, NULL);
+
+    module_call_init(MODULE_INIT_QOM);
+    type_register_static(&my_dev_type_info);
+    test_init_machine();
+
+    g_test_add_func("/qdev/free-properties",
+                    test_qdev_free_properties);
+
+    g_test_run();
+
+    return 0;
+}
tests/unit/meson.build +1 -0
index d5248ae51d..99f59f4ceb 100644
@@ -159,6 +159,7 @@ if have_system
       'test-qdev-global-props': [qom, hwcore]
     }
   endif
+  tests += {'test-qdev': [qom, hwcore]}
 endif
 
 if have_ga and host_os == 'linux'
Signature · 1 line
2.49.0
Reviewed-by on v1 5/6
#10 lore raw

Re: [PATCH 5/6] tests: add /qdev/free-properties test

Quoted 3 lines · On 4/29/25 10:03 AM, [email protected] wrote:

From: Marc-André Lureau <[email protected]>

Add a simple qdev test to check that allocated properties get free with

get freed

Quoted 116 lines

the object. This test exhibited array leaks before the fixes.

---
  tests/unit/test-qdev.c | 96 ++++++++++++++++++++++++++++++++++++++++++
  tests/unit/meson.build |  1 +
  2 files changed, 97 insertions(+)

create mode 100644 tests/unit/test-qdev.c

tests/unit/test-qdev.c new file +96 -0
new file mode 100644
index 0000000000..2b4c9dd643
@@ -0,0 +1,96 @@
+#include "qemu/osdep.h"
+
+#include "hw/qdev-properties.h"
+#include "qom/object.h"
+#include "qapi/error.h"
+#include "qapi/visitor.h"
+
+
+#define TYPE_MY_DEV "my-dev"
+typedef struct MyDev MyDev;
+DECLARE_INSTANCE_CHECKER(MyDev, STATIC_TYPE,
+                         TYPE_MY_DEV)
+
+struct MyDev {
+    DeviceState parent_obj;
+
+    uint32_t prop_u32;
+    char *prop_string;
+    uint32_t *prop_array_u32;
+    uint32_t prop_array_u32_nb;
+};
+
+static const Property my_dev_props[] = {
+    DEFINE_PROP_UINT32("u32", MyDev, prop_u32, 100),
+    DEFINE_PROP_STRING("string", MyDev, prop_string),
+    DEFINE_PROP_ARRAY("array-u32", MyDev, prop_array_u32_nb, prop_array_u32, qdev_prop_uint32, uint32_t),
+};
+
+static void my_dev_class_init(ObjectClass *oc, const void *data)
+{
+    DeviceClass *dc = DEVICE_CLASS(oc);
+
+    dc->realize = NULL;
+    device_class_set_props(dc, my_dev_props);
+}
+
+static const TypeInfo my_dev_type_info = {
+    .name = TYPE_MY_DEV,
+    .parent = TYPE_DEVICE,
+    .instance_size = sizeof(MyDev),
+    .class_init = my_dev_class_init,
+};
+
+/*
+ * Initialize a fake machine, being prepared for future tests.
+ *
+ * Realization of anonymous qdev (with no parent object) requires both
+ * the machine object and its "unattached" container to be at least present.
+ */
+static void test_init_machine(void)
+{
+    /* This is a fake machine - it doesn't need to be a machine object */
+    Object *machine = object_property_add_new_container(
+        object_get_root(), "machine");
+
+    /* This container must exist for anonymous qdevs to realize() */
+    object_property_add_new_container(machine, "unattached");
+}
+
+static void test_qdev_free_properties(void)
+{
+    MyDev *mt;
+
+    mt = STATIC_TYPE(object_new(TYPE_MY_DEV));
+    object_set_props(OBJECT(mt), &error_fatal,
+                     "string", "something",
+                     "array-u32", "12,13",
+                     NULL);
+    qdev_realize(DEVICE(mt), NULL, &error_fatal);
+
+    g_assert_cmpuint(mt->prop_u32, ==, 100);
+    g_assert_cmpstr(mt->prop_string, ==, "something");
+    g_assert_cmpuint(mt->prop_array_u32_nb, ==, 2);
+    g_assert_cmpuint(mt->prop_array_u32[0], ==, 12);
+    g_assert_cmpuint(mt->prop_array_u32[1], ==, 13);
+
+    object_unparent(OBJECT(mt));
+    object_unref(mt);
+}
+
+
+int main(int argc, char **argv)
+{
+    g_test_init(&argc, &argv, NULL);
+
+    module_call_init(MODULE_INIT_QOM);
+    type_register_static(&my_dev_type_info);
+    test_init_machine();
+
+    g_test_add_func("/qdev/free-properties",
+                    test_qdev_free_properties);
+
+    g_test_run();
+
+    return 0;
+}
tests/unit/meson.build +0 -0
index d5248ae51d..99f59f4ceb 100644

tests/unit/meson.build if have_system · line 165

Quoted 7 lines
tests/unit/meson.build +1 -0
@@ -159,6 +159,7 @@ if have_system
        'test-qdev-global-props': [qom, hwcore]
      }
    endif
+  tests += {'test-qdev': [qom, hwcore]}
  endif
 
  if have_ga and host_os == 'linux'
#11 lore raw

PATCH 6/6 RFC: qom/object: simplify object_property_del_all()

In reply to [email protected]

From: Marc-André Lureau <[email protected]>

Since commit 9859fac ("object: release all props"), the code was changed to tracks the already released properties in a hash table. I am not sure why this was done, perhaps to prevent from potential crashes if properties are being added dynamically during release. I am not sure if it's a valid concern though.

---
 qom/object.c | 18 +++++-------------
 1 file changed, 5 insertions(+), 13 deletions(-)
qom/object.c +5 -13
index 664f0f24ae..a2ccebca6d 100644
@@ -656,21 +656,13 @@ static void object_property_del_all(Object *obj)
     g_autoptr(GHashTable) done = g_hash_table_new(NULL, NULL);
     ObjectProperty *prop;
     ObjectPropertyIterator iter;
-    bool released;
 
-    do {
-        released = false;
-        object_property_iter_init(&iter, obj);
-        while ((prop = object_property_iter_next(&iter)) != NULL) {
-            if (g_hash_table_add(done, prop)) {
-                if (prop->release) {
-                    prop->release(obj, prop->name, prop->opaque);
-                    released = true;
-                    break;
-                }
-            }
+    object_property_iter_init(&iter, obj);
+    while ((prop = object_property_iter_next(&iter)) != NULL) {
+        if (prop->release) {
+            prop->release(obj, prop->name, prop->opaque);
         }
-    } while (released);
+    }
 
     g_hash_table_unref(obj->properties);
 }
Signature · 1 line
2.49.0
#12 lore raw

Re: [PATCH 6/6] RFC: qom/object: simplify object_property_del_all()

Il mar 29 apr 2025, 16:03 <[email protected]> ha scritto:

Quoted 8 lines

From: Marc-André Lureau <[email protected]>

Since commit 9859fac ("object: release all props"), the code was changed to tracks the already released properties in a hash table. I am not sure why this was done, perhaps to prevent from potential crashes if properties are being added dynamically during release. I am not sure if it's a valid concern though.

You always need object_property_iter_init in case prop->release deletes a property, thus invalidating the GHashTable iterator. The hash table instead is needed in case prop->release does *not* delete a property, because then the property reappears on subsequent recreations of the iterator.

Paolo

Quoted 27 lines
qom/object.c +5 -14
-    bool released;
 
-    do {
-        released = false;
-        object_property_iter_init(&iter, obj);
-        while ((prop = object_property_iter_next(&iter)) != NULL) {
-            if (g_hash_table_add(done, prop)) {
-                if (prop->release) {
-                    prop->release(obj, prop->name, prop->opaque);
-                    released = true;
-                    break;
-                }
-            }
+    object_property_iter_init(&iter, obj);
+    while ((prop = object_property_iter_next(&iter)) != NULL) {
+        if (prop->release) {
+            prop->release(obj, prop->name, prop->opaque);
         }
-    } while (released);
+    }
 
     g_hash_table_unref(obj->properties);
 }
--

2.49.0

#13 lore raw

Re: [PATCH 6/6] RFC: qom/object: simplify object_property_del_all()

Hi

Quoted 7 lines · Paolo Bonzini

Il mar 29 apr 2025, 16:03 <[email protected]> ha scritto:

Quoted 3 lines

From: Marc-André Lureau <[email protected]>

Quoted 8 lines
Quoted 5 lines

Since commit 9859fac ("object: release all props"), the code was changed to tracks the already released properties in a hash table. I am not sure why this was done, perhaps to prevent from potential crashes if properties are being added dynamically during release. I am not sure if it's a valid concern though.

You always need object_property_iter_init in case prop->release deletes a property, thus invalidating the GHashTable iterator. The hash table instead is needed in case prop->release does *not* delete a property, because then the property reappears on subsequent recreations of the iterator.

Yes, changing the properties while they are being removed is I think the reason I added the HashTable/Set. But is this a valid concern? (the penalty is a bit sad)

#14 lore raw

Re: [PATCH 6/6] RFC: qom/object: simplify object_property_del_all()

Il mar 29 apr 2025, 16:37 Marc-André Lureau <[email protected]> ha scritto:

Quoted 18 lines

Hi

Quoted 7 lines · Paolo Bonzini

Il mar 29 apr 2025, 16:03 <[email protected]> ha scritto:

Quoted 3 lines

From: Marc-André Lureau <[email protected]>

Quoted 8 lines
Quoted 5 lines

Since commit 9859fac ("object: release all props"), the code was changed to tracks the already released properties in a hash table. I am not sure why this was done, perhaps to prevent from potential crashes if properties are being added dynamically during release. I am not sure if it's a valid concern though.

You always need object_property_iter_init in case prop->release deletes

Quoted 9 lines

a property, thus invalidating the GHashTable iterator. The hash table instead is needed in case prop->release does *not* delete a property, because then the property reappears on subsequent recreations of the iterator.

Yes, changing the properties while they are being removed is I think the reason I added the HashTable/Set. But is this a valid concern? (the penalty is a bit sad)

I think deleting is a possibility... For example releasing a property could trigger an object_unparent(). Since there is no better way to notice I think it's better to keep it.

Maybe we could add a generation count to the Object struct?

Paolo

Quoted 1 line