Skip to contentSkip to conversation

hw/core/qdev-clock: add a reference on aliased clocks

PATCH Superseded Luc Michel 2 participants 3 messages Oct 20, 2020

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Luc Michel

author
#0 lore raw

PATCH 1/1 hw/core/qdev-clock: add a reference on aliased clocks

When aliasing a clock with the qdev_alias_clock() function, a new link property is created on the device aliasing the clock. The link points to the aliased clock and use the OBJ_PROP_LINK_STRONG flag. This property is read only since it does not provide a check callback for modifications.

The object_property_add_link() documentation stats that with OBJ_PROP_LINK_STRONG properties, the linked object reference count get decremented when the property is deleted. But it is _not_ incremented on creation (object_property_add_link() does not actually know the link).

This commit increments the reference count on the aliased clock to ensure the aliased clock stays alive during the property lifetime, and to avoid a double-free memory error when the property get deleted.

---
 hw/core/qdev-clock.c | 1 +
 1 file changed, 1 insertion(+)
hw/core/qdev-clock.c +1 -0
index 6a9a340d0f..5f5e143702 100644
@@ -59,10 +59,11 @@ static NamedClockList *qdev_init_clocklist(DeviceState *dev, const char *name,
     } else {
         object_property_add_link(OBJECT(dev), name,
                                  object_get_typename(OBJECT(clk)),
                                  (Object **) &ncl->clock,
                                  NULL, OBJ_PROP_LINK_STRONG);
+        object_ref(OBJECT(clk));
     }
 
     ncl->clock = clk;
 
     QLIST_INSERT_HEAD(&dev->clocks, ncl, node);
Signature · 1 line
2.28.0
#1 lore raw

Cc'ing Markus/Marc-André too.

Quoted 37 lines · Luc Michel

When aliasing a clock with the qdev_alias_clock() function, a new link property is created on the device aliasing the clock. The link points to the aliased clock and use the OBJ_PROP_LINK_STRONG flag. This property is read only since it does not provide a check callback for modifications.

The object_property_add_link() documentation stats that with OBJ_PROP_LINK_STRONG properties, the linked object reference count get decremented when the property is deleted. But it is _not_ incremented on creation (object_property_add_link() does not actually know the link).

This commit increments the reference count on the aliased clock to ensure the aliased clock stays alive during the property lifetime, and to avoid a double-free memory error when the property get deleted.

---
  hw/core/qdev-clock.c | 1 +
  1 file changed, 1 insertion(+)
hw/core/qdev-clock.c +1 -0
index 6a9a340d0f..5f5e143702 100644
@@ -59,10 +59,11 @@ static NamedClockList *qdev_init_clocklist(DeviceState *dev, const char *name,
      } else {
          object_property_add_link(OBJECT(dev), name,
                                   object_get_typename(OBJECT(clk)),
                                   (Object **) &ncl->clock,
                                   NULL, OBJ_PROP_LINK_STRONG);
+        object_ref(OBJECT(clk));
      }
 
      ncl->clock = clk;
 
      QLIST_INSERT_HEAD(&dev->clocks, ncl, node);
#2 lore raw
Quoted 23 lines · Philippe Mathieu-Daudé

Cc'ing Markus/Marc-André too.

Quoted 20 lines · Luc Michel

When aliasing a clock with the qdev_alias_clock() function, a new link property is created on the device aliasing the clock. The link points to the aliased clock and use the OBJ_PROP_LINK_STRONG flag. This property is read only since it does not provide a check callback for modifications.

The object_property_add_link() documentation stats that with OBJ_PROP_LINK_STRONG properties, the linked object reference count get decremented when the property is deleted. But it is _not_ incremented on creation (object_property_add_link() does not actually know the link).

This commit increments the reference count on the aliased clock to ensure the aliased clock stays alive during the property lifetime, and to avoid a double-free memory error when the property get deleted.

---
  hw/core/qdev-clock.c | 1 +

  1 file changed, 1 insertion(+)
Quoted 12 lines

hw/core/qdev-clock.c static NamedClockList · line 65

Quoted 12 lines
hw/core/qdev-clock.c +1 -0
index 6a9a340d0f..5f5e143702 100644
@@ -59,10 +59,11 @@ static NamedClockList 
 *qdev_init_clocklist(DeviceState *dev, const char *name,
       } else {
           object_property_add_link(OBJECT(dev), name,
                                    object_get_typename(OBJECT(clk)),
                                    (Object **) &ncl->clock,
                                    NULL, OBJ_PROP_LINK_STRONG);
+        object_ref(OBJECT(clk));

OK, this is particular to this model because device_finalize() garbage-collector and calls qdev_finalize_clocklist().

With a comment explaining why we need this call:

Quoted 5 lines
Quoted 4 lines
hw/core/qdev-clock.c +0 -0
       }
       ncl->clock = clk;
       QLIST_INSERT_HEAD(&dev->clocks, ncl, node);