Skip to content

Commit 39f72ef

Browse files
stefanhaRHafaerber
authored andcommitted
qom: Add check() argument to object_property_add_link()
There are currently three types of object_property_add_link() callers: 1. The link property may be set at any time. 2. The link property of a DeviceState instance may only be set before realize. 3. The link property may never be set, it is read-only. Something similar can already be achieved with object_property_add_str()'s set() argument. Follow its example and add a check() argument to object_property_add_link(). Also provide default check() functions for case Torlus#1 and Torlus#2. Case Torlus#3 is covered by passing a NULL function pointer. Cc: Peter Crosthwaite <peter.crosthwaite@petalogix.com> Cc: Alexander Graf <agraf@suse.de> Cc: Anthony Liguori <aliguori@amazon.com> Cc: "Michael S. Tsirkin" <mst@redhat.com> Signed-off-by: Stefan Hajnoczi <stefanha@redhat.com> Reviewed-by: Paolo Bonzini <pbonzini@redhat.com> [AF: Tweaked documentation comment] Signed-off-by: Andreas Färber <afaerber@suse.de>
1 parent 9561fda commit 39f72ef

File tree

13 files changed

+80
-4
lines changed

13 files changed

+80
-4
lines changed

hw/core/qdev-properties.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,18 @@ void qdev_prop_set_after_realize(DeviceState *dev, const char *name,
2121
}
2222
}
2323

24+
void qdev_prop_allow_set_link_before_realize(Object *obj, const char *name,
25+
Object *val, Error **errp)
26+
{
27+
DeviceState *dev = DEVICE(obj);
28+
29+
if (dev->realized) {
30+
error_setg(errp, "Attempt to set link property '%s' on device '%s' "
31+
"(type '%s') after it was realized",
32+
name, dev->id, object_get_typename(obj));
33+
}
34+
}
35+
2436
void *qdev_get_prop_ptr(DeviceState *dev, Property *prop)
2537
{
2638
void *ptr = dev;

hw/core/qdev.c

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,10 @@ static void bus_add_child(BusState *bus, DeviceState *child)
9797
snprintf(name, sizeof(name), "child[%d]", kid->index);
9898
object_property_add_link(OBJECT(bus), name,
9999
object_get_typename(OBJECT(child)),
100-
(Object **)&kid->child, 0, NULL);
100+
(Object **)&kid->child,
101+
NULL, /* read-only property */
102+
0, /* return ownership on prop deletion */
103+
NULL);
101104
}
102105

103106
void qdev_set_parent_bus(DeviceState *dev, BusState *bus)
@@ -823,7 +826,7 @@ static void device_initfn(Object *obj)
823826
} while (class != object_class_by_name(TYPE_DEVICE));
824827

825828
object_property_add_link(OBJECT(dev), "parent_bus", TYPE_BUS,
826-
(Object **)&dev->parent_bus, 0,
829+
(Object **)&dev->parent_bus, NULL, 0,
827830
&error_abort);
828831
}
829832

@@ -945,6 +948,7 @@ static void qbus_initfn(Object *obj)
945948
object_property_add_link(obj, QDEV_HOTPLUG_HANDLER_PROPERTY,
946949
TYPE_HOTPLUG_HANDLER,
947950
(Object **)&bus->hotplug_handler,
951+
object_property_allow_set_link,
948952
OBJ_PROP_LINK_UNREF_ON_RELEASE,
949953
NULL);
950954
object_property_add_bool(obj, "realized",

hw/dma/xilinx_axidma.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -538,10 +538,12 @@ static void xilinx_axidma_realize(DeviceState *dev, Error **errp)
538538

539539
object_property_add_link(OBJECT(ds), "dma", TYPE_XILINX_AXI_DMA,
540540
(Object **)&ds->dma,
541+
object_property_allow_set_link,
541542
OBJ_PROP_LINK_UNREF_ON_RELEASE,
542543
&local_errp);
543544
object_property_add_link(OBJECT(cs), "dma", TYPE_XILINX_AXI_DMA,
544545
(Object **)&cs->dma,
546+
object_property_allow_set_link,
545547
OBJ_PROP_LINK_UNREF_ON_RELEASE,
546548
&local_errp);
547549
if (local_errp) {
@@ -576,11 +578,13 @@ static void xilinx_axidma_init(Object *obj)
576578

577579
object_property_add_link(obj, "axistream-connected", TYPE_STREAM_SLAVE,
578580
(Object **)&s->tx_data_dev,
581+
qdev_prop_allow_set_link_before_realize,
579582
OBJ_PROP_LINK_UNREF_ON_RELEASE,
580583
&error_abort);
581584
object_property_add_link(obj, "axistream-control-connected",
582585
TYPE_STREAM_SLAVE,
583586
(Object **)&s->tx_control_dev,
587+
qdev_prop_allow_set_link_before_realize,
584588
OBJ_PROP_LINK_UNREF_ON_RELEASE,
585589
&error_abort);
586590

hw/net/xilinx_axienet.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -946,10 +946,12 @@ static void xilinx_enet_realize(DeviceState *dev, Error **errp)
946946

947947
object_property_add_link(OBJECT(ds), "enet", "xlnx.axi-ethernet",
948948
(Object **) &ds->enet,
949+
object_property_allow_set_link,
949950
OBJ_PROP_LINK_UNREF_ON_RELEASE,
950951
&local_errp);
951952
object_property_add_link(OBJECT(cs), "enet", "xlnx.axi-ethernet",
952953
(Object **) &cs->enet,
954+
object_property_allow_set_link,
953955
OBJ_PROP_LINK_UNREF_ON_RELEASE,
954956
&local_errp);
955957
if (local_errp) {
@@ -987,11 +989,13 @@ static void xilinx_enet_init(Object *obj)
987989

988990
object_property_add_link(obj, "axistream-connected", TYPE_STREAM_SLAVE,
989991
(Object **) &s->tx_data_dev,
992+
qdev_prop_allow_set_link_before_realize,
990993
OBJ_PROP_LINK_UNREF_ON_RELEASE,
991994
&error_abort);
992995
object_property_add_link(obj, "axistream-control-connected",
993996
TYPE_STREAM_SLAVE,
994997
(Object **) &s->tx_control_dev,
998+
qdev_prop_allow_set_link_before_realize,
995999
OBJ_PROP_LINK_UNREF_ON_RELEASE,
9961000
&error_abort);
9971001

hw/pcmcia/pxa2xx.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,9 @@ static void pxa2xx_pcmcia_initfn(Object *obj)
198198
s->slot.irq = qemu_allocate_irqs(pxa2xx_pcmcia_set_irq, s, 1)[0];
199199

200200
object_property_add_link(obj, "card", TYPE_PCMCIA_CARD,
201-
(Object **)&s->card, 0, NULL);
201+
(Object **)&s->card,
202+
NULL, /* read-only property */
203+
0, NULL);
202204
}
203205

204206
/* Insert a new card into a slot */

hw/s390x/s390-virtio-bus.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -314,6 +314,7 @@ static void s390_virtio_rng_instance_init(Object *obj)
314314
object_property_add_child(obj, "virtio-backend", OBJECT(&dev->vdev), NULL);
315315
object_property_add_link(obj, "rng", TYPE_RNG_BACKEND,
316316
(Object **)&dev->vdev.conf.rng,
317+
qdev_prop_allow_set_link_before_realize,
317318
OBJ_PROP_LINK_UNREF_ON_RELEASE, NULL);
318319
}
319320

hw/s390x/virtio-ccw.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1273,6 +1273,7 @@ static void virtio_ccw_rng_instance_init(Object *obj)
12731273
object_property_add_child(obj, "virtio-backend", OBJECT(&dev->vdev), NULL);
12741274
object_property_add_link(obj, "rng", TYPE_RNG_BACKEND,
12751275
(Object **)&dev->vdev.conf.rng,
1276+
qdev_prop_allow_set_link_before_realize,
12761277
OBJ_PROP_LINK_UNREF_ON_RELEASE, NULL);
12771278
}
12781279

hw/virtio/virtio-pci.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1518,6 +1518,7 @@ static void virtio_rng_initfn(Object *obj)
15181518
object_property_add_child(obj, "virtio-backend", OBJECT(&dev->vdev), NULL);
15191519
object_property_add_link(obj, "rng", TYPE_RNG_BACKEND,
15201520
(Object **)&dev->vdev.conf.rng,
1521+
qdev_prop_allow_set_link_before_realize,
15211522
OBJ_PROP_LINK_UNREF_ON_RELEASE, NULL);
15221523

15231524
}

hw/virtio/virtio-rng.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,7 @@ static void virtio_rng_initfn(Object *obj)
224224

225225
object_property_add_link(obj, "rng", TYPE_RNG_BACKEND,
226226
(Object **)&vrng->conf.rng,
227+
qdev_prop_allow_set_link_before_realize,
227228
OBJ_PROP_LINK_UNREF_ON_RELEASE, NULL);
228229
}
229230

include/hw/qdev-properties.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,4 +204,15 @@ void qdev_property_add_static(DeviceState *dev, Property *prop, Error **errp);
204204
*/
205205
void qdev_prop_set_after_realize(DeviceState *dev, const char *name,
206206
Error **errp);
207+
208+
/**
209+
* qdev_prop_allow_set_link_before_realize:
210+
*
211+
* Set the #Error object if an attempt is made to set the link after realize.
212+
* This function should be used as the check() argument to
213+
* object_property_add_link().
214+
*/
215+
void qdev_prop_allow_set_link_before_realize(Object *obj, const char *name,
216+
Object *val, Error **errp);
217+
207218
#endif

0 commit comments

Comments
 (0)