virtio-serial-bus: do cleanup on the error path in realize() to avoid memleaks
PATCH 1/1 virtio-serial-bus: do cleanup on the error path in realize() to avoid memleaks
port->bh forgot to delete on the error path, this patch add it to fix memleaks. It's easy to reproduce as follow(add a same nr port):
{'execute': 'device_add', 'arguments': {'id': 'virtio_serial_pci0', 'driver': 'virtio-serial-pci', 'bus': 'pci.0', 'addr': '0x5'}, 'id': 'yVkZcGgV'}
{'execute': 'device_add', 'arguments': {'id': 'port1', 'driver': 'virtserialport', 'name': 'port1', 'chardev': 'channel1', 'bus': 'virtio_serial_pci0.0', 'nr': 1}, 'id': '3dXdUgJA'}
{'execute': 'device_add', 'arguments': {'id': 'port2', 'driver': 'virtserialport', 'name': 'port2', 'chardev': 'channel2', 'bus': 'virtio_serial_pci0.0', 'nr': 1}, 'id': 'qLzcCkob'}
{'execute': 'device_add', 'arguments': {'id': 'port2', 'driver': 'virtserialport', 'name': 'port2', 'chardev': 'channel2', 'bus': 'virtio_serial_pci0.0', 'nr': 2}, 'id': 'qLzcCkob'}The leak stack:
Direct leak of 40 byte(s) in 1 object(s) allocated from:
#0 0x7f04a8008ae8 in __interceptor_malloc (/lib64/libasan.so.5+0xefae8)
#1 0x7f04a73cf1d5 in g_malloc (/lib64/libglib-2.0.so.0+0x531d5)
#2 0x56273eaee484 in aio_bh_new /mnt/sdb/backup/qemu/util/async.c:125
#3 0x56273eafe9a8 in qemu_bh_new /mnt/sdb/backup/qemu/util/main-loop.c:532
#4 0x56273d52e62e in virtser_port_device_realize /mnt/sdb/backup/qemu/hw/char/virtio-serial-bus.c:946
#5 0x56273dcc5040 in device_set_realized /mnt/sdb/backup/qemu/hw/core/qdev.c:891
#6 0x56273e5ebbce in property_set_bool /mnt/sdb/backup/qemu/qom/object.c:2238
#7 0x56273e5e5a9c in object_property_set /mnt/sdb/backup/qemu/qom/object.c:1324
#8 0x56273e5ef5f8 in object_property_set_qobject /mnt/sdb/backup/qemu/qom/qom-qobject.c:26
#9 0x56273e5e5e6a in object_property_set_bool /mnt/sdb/backup/qemu/qom/object.c:1390
#10 0x56273daa40de in qdev_device_add /mnt/sdb/backup/qemu/qdev-monitor.c:680
#11 0x56273daa53e9 in qmp_device_add /mnt/sdb/backup/qemu/qdev-monitor.c:805--- hw/char/virtio-serial-bus.c | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-)
hw/char/virtio-serial-bus.c +9 -5
@@ -957,13 +957,13 @@ static void virtser_port_device_realize(DeviceState *dev, Error **errp)if (find_port_by_id (port->vser, port->id)) {error_setg (errp,"virtio-serial-bus: A port already exists at id %u" , port->id); -return ; + goto fail; }if (port->name !=NULL &&find_port_by_name (port->name)) {error_setg (errp,"virtio-serial-bus: A port already exists by name %s" , port->name); -return ; + goto fail; }if (port->id ==VIRTIO_CONSOLE_BAD_ID ) {
@@ -974,7 +974,7 @@ static void virtser_port_device_realize(DeviceState *dev, Error **errp)if (port->id ==VIRTIO_CONSOLE_BAD_ID ) {error_setg (errp,"virtio-serial-bus: Maximum port limit for " "this device reached" ); -return ; + goto fail; } } }
@@ -983,16 +983,20 @@ static void virtser_port_device_realize(DeviceState *dev, Error **errp)if (port->id >= max_nr_ports) {error_setg (errp,"virtio-serial-bus: Out-of-range port id specified, " "max. allowed: %u" , max_nr_ports -1 ); -return ; + goto fail; } vsc->realize (dev, &err);if (err !=NULL ) {error_propagate (errp, err); -return ; + goto fail; } port->elem =NULL ; +return ; + +fail: +qemu_bh_delete (port->bh); } static void virtser_port_device_plug(HotplugHandler *hotplug_dev,
Signature · 1 line
2.18.2
Quoted 21 lines · Pan Nengyuan
port->bhforgot to delete on the error path, this patch add it to fix memleaks. It's easy to reproduce as follow(add a same nr port):{'execute': 'device_add', 'arguments': {'id': 'virtio_serial_pci0', 'driver': 'virtio-serial-pci', 'bus': 'pci.0', 'addr': '0x5'}, 'id': 'yVkZcGgV'} {'execute': 'device_add', 'arguments': {'id': 'port1', 'driver': 'virtserialport', 'name': 'port1', 'chardev': 'channel1', 'bus': 'virtio_serial_pci0.0', 'nr': 1}, 'id': '3dXdUgJA'} {'execute': 'device_add', 'arguments': {'id': 'port2', 'driver': 'virtserialport', 'name': 'port2', 'chardev': 'channel2', 'bus': 'virtio_serial_pci0.0', 'nr': 1}, 'id': 'qLzcCkob'} {'execute': 'device_add', 'arguments': {'id': 'port2', 'driver': 'virtserialport', 'name': 'port2', 'chardev': 'channel2', 'bus': 'virtio_serial_pci0.0', 'nr': 2}, 'id': 'qLzcCkob'}The leak stack:
Direct leak of 40 byte(s) in 1 object(s) allocated from:#0 0x7f04a8008ae8 in __interceptor_malloc (/lib64/libasan.so.5+0xefae8) #1 0x7f04a73cf1d5 in g_malloc (/lib64/libglib-2.0.so.0+0x531d5) #2 0x56273eaee484 in aio_bh_new /mnt/sdb/backup/qemu/util/async.c:125 #3 0x56273eafe9a8 in qemu_bh_new /mnt/sdb/backup/qemu/util/main-loop.c:532 #4 0x56273d52e62e in virtser_port_device_realize /mnt/sdb/backup/qemu/hw/char/virtio-serial-bus.c:946 #5 0x56273dcc5040 in device_set_realized /mnt/sdb/backup/qemu/hw/core/qdev.c:891 #6 0x56273e5ebbce in property_set_bool /mnt/sdb/backup/qemu/qom/object.c:2238 #7 0x56273e5e5a9c in object_property_set /mnt/sdb/backup/qemu/qom/object.c:1324 #8 0x56273e5ef5f8 in object_property_set_qobject /mnt/sdb/backup/qemu/qom/qom-qobject.c:26 #9 0x56273e5e5e6a in object_property_set_bool /mnt/sdb/backup/qemu/qom/object.c:1390 #10 0x56273daa40de in qdev_device_add /mnt/sdb/backup/qemu/qdev-monitor.c:680 #11 0x56273daa53e9 in qmp_device_add /mnt/sdb/backup/qemu/qdev-monitor.c:805
Quoted 2 lines
Reported-by Euler Robot <[email protected]>Signed-off-by Pan Nengyuan <[email protected]>
Quoted 46 lines
--- hw/char/virtio-serial-bus.c | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-)hw/char/virtio-serial-bus.c +5
-5@@ -957,13 +957,13 @@ static void virtser_port_device_realize(DeviceState *dev, Error **errp)if (find_port_by_id (port->vser, port->id)) {error_setg (errp,"virtio-serial-bus: A port already exists at id %u" , port->id); -return ; + goto fail; }if (port->name !=NULL &&find_port_by_name (port->name)) {error_setg (errp,"virtio-serial-bus: A port already exists by name %s" , port->name); -return ; + goto fail; }if (port->id ==VIRTIO_CONSOLE_BAD_ID ) {@@ -974,7 +974,7 @@ static void virtser_port_device_realize(DeviceState *dev, Error **errp)if (port->id ==VIRTIO_CONSOLE_BAD_ID ) {error_setg (errp,"virtio-serial-bus: Maximum port limit for " "this device reached" ); -return ; + goto fail; } } }@@ -983,16 +983,20 @@ static void virtser_port_device_realize(DeviceState *dev, Error **errp)if (port->id >= max_nr_ports) {error_setg (errp,"virtio-serial-bus: Out-of-range port id specified, " "max. allowed: %u" , max_nr_ports -1 ); -return ; + goto fail; } vsc->realize (dev, &err);if (err !=NULL ) {error_propagate (errp, err); -return ; + goto fail;
hw/char/virtio-serial-bus.c static void virtser_port_device_realize(DeviceState *dev, Error **errp) · line 1002
Quoted 10 lines
hw/char/virtio-serial-bus.c +4
-0} port->elem =NULL ; +return ; + +fail: +qemu_bh_delete (port->bh); } static void virtser_port_device_plug(HotplugHandler *hotplug_dev,
Looks correct to me.
However, I wonder whether we could simply create port->bh later. It's for use by virtio_serial_throttle_port(), called on incoming migration via virtio_serial_load_device(), virtio_load(), virtio_device_get(). It runs flush_queued_data(), which does nothing unless virtio_queue_ready(port->ovq).
Note that virtio_queue_ready() dereferences its argument. It's safe only after virtser_port_device_plug() set port->ovq. I'd expect that to be possible only while the device is realized. If that's correct, we could simply create port->bh last in virtser_port_device_realize().
Quoted 93 lines · Markus Armbruster
Quoted 21 lines · Pan Nengyuan
port->bhforgot to delete on the error path, this patch add it to fix memleaks. It's easy to reproduce as follow(add a same nr port):{'execute': 'device_add', 'arguments': {'id': 'virtio_serial_pci0', 'driver': 'virtio-serial-pci', 'bus': 'pci.0', 'addr': '0x5'}, 'id': 'yVkZcGgV'} {'execute': 'device_add', 'arguments': {'id': 'port1', 'driver': 'virtserialport', 'name': 'port1', 'chardev': 'channel1', 'bus': 'virtio_serial_pci0.0', 'nr': 1}, 'id': '3dXdUgJA'} {'execute': 'device_add', 'arguments': {'id': 'port2', 'driver': 'virtserialport', 'name': 'port2', 'chardev': 'channel2', 'bus': 'virtio_serial_pci0.0', 'nr': 1}, 'id': 'qLzcCkob'} {'execute': 'device_add', 'arguments': {'id': 'port2', 'driver': 'virtserialport', 'name': 'port2', 'chardev': 'channel2', 'bus': 'virtio_serial_pci0.0', 'nr': 2}, 'id': 'qLzcCkob'}The leak stack:
Direct leak of 40 byte(s) in 1 object(s) allocated from:#0 0x7f04a8008ae8 in __interceptor_malloc (/lib64/libasan.so.5+0xefae8) #1 0x7f04a73cf1d5 in g_malloc (/lib64/libglib-2.0.so.0+0x531d5) #2 0x56273eaee484 in aio_bh_new /mnt/sdb/backup/qemu/util/async.c:125 #3 0x56273eafe9a8 in qemu_bh_new /mnt/sdb/backup/qemu/util/main-loop.c:532 #4 0x56273d52e62e in virtser_port_device_realize /mnt/sdb/backup/qemu/hw/char/virtio-serial-bus.c:946 #5 0x56273dcc5040 in device_set_realized /mnt/sdb/backup/qemu/hw/core/qdev.c:891 #6 0x56273e5ebbce in property_set_bool /mnt/sdb/backup/qemu/qom/object.c:2238 #7 0x56273e5e5a9c in object_property_set /mnt/sdb/backup/qemu/qom/object.c:1324 #8 0x56273e5ef5f8 in object_property_set_qobject /mnt/sdb/backup/qemu/qom/qom-qobject.c:26 #9 0x56273e5e5e6a in object_property_set_bool /mnt/sdb/backup/qemu/qom/object.c:1390 #10 0x56273daa40de in qdev_device_add /mnt/sdb/backup/qemu/qdev-monitor.c:680 #11 0x56273daa53e9 in qmp_device_add /mnt/sdb/backup/qemu/qdev-monitor.c:805Quoted 2 lines
Reported-by Euler Robot <[email protected]>Signed-off-by Pan Nengyuan <[email protected]>Fixes 199646d81522509ac2dba6d28c31e8c7d807bc93Quoted 46 lines
--- hw/char/virtio-serial-bus.c | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-)hw/char/virtio-serial-bus.c +5
-5@@ -957,13 +957,13 @@ static void virtser_port_device_realize(DeviceState *dev, Error **errp)if (find_port_by_id (port->vser, port->id)) {error_setg (errp,"virtio-serial-bus: A port already exists at id %u" , port->id); -return ; + goto fail; }if (port->name !=NULL &&find_port_by_name (port->name)) {error_setg (errp,"virtio-serial-bus: A port already exists by name %s" , port->name); -return ; + goto fail; }if (port->id ==VIRTIO_CONSOLE_BAD_ID ) {@@ -974,7 +974,7 @@ static void virtser_port_device_realize(DeviceState *dev, Error **errp)if (port->id ==VIRTIO_CONSOLE_BAD_ID ) {error_setg (errp,"virtio-serial-bus: Maximum port limit for " "this device reached" ); -return ; + goto fail; } } }@@ -983,16 +983,20 @@ static void virtser_port_device_realize(DeviceState *dev, Error **errp)if (port->id >= max_nr_ports) {error_setg (errp,"virtio-serial-bus: Out-of-range port id specified, " "max. allowed: %u" , max_nr_ports -1 ); -return ; + goto fail; } vsc->realize (dev, &err);if (err !=NULL ) {error_propagate (errp, err); -return ; + goto fail;hw/char/virtio-serial-bus.c static void virtser_port_device_realize(DeviceState *dev, Error **errp) · line 1002
Quoted 10 lines
hw/char/virtio-serial-bus.c +4
-0} port->elem =NULL ; +return ; + +fail: +qemu_bh_delete (port->bh); } static void virtser_port_device_plug(HotplugHandler *hotplug_dev,Looks correct to me.
However, I wonder whether we could simply create
port->bhlater. It's for use byvirtio_serial_throttle_port(), called on incoming migration viavirtio_serial_load_device(),virtio_load(),virtio_device_get(). It runsflush_queued_data(), which does nothing unlessvirtio_queue_ready(port->ovq).
Quoted 4 lines
Note that
virtio_queue_ready()dereferences its argument. It's safe only aftervirtser_port_device_plug()setport->ovq. I'd expect that to be possible only while the device is realized. If that's correct, we could simply createport->bhlast invirtser_port_device_realize().
Yes, it looks correct. If there is no other points, I will change it.
Thanks.
Quoted 3 lines
.