qdev and blockdev refcount leak fixes
cover qdev and blockdev refcount leak fixes
It started with a bug report along these lines:
(qemu) device_add virtio-blk-pci,drive=drive0,x-data-plane=on device is incompatible with x-data-plane, use scsi=off Device initialization failed.
Device initialization failed.
Device 'virtio-blk-pci' could not be initialized (qemu) drive_del drive0
(qemu) drive_add 0 if=none,id=drive0
Duplicate ID 'drive0' for drive
The drive_add command should succeed since the old "drive0" was deleted.
With the help of Andreas and Paolo we figured out that the problem is not virtio-blk or dataplane. There are actually two problems:
1. qdev_device_add() must release its DeviceState reference if qdev_init() failed.
2. blockdev_init() must release its QemuOpts on failure or early return when no file= option was specified.
This series fixes these problems and then qtest test cases for both bugs. In order to do this we need to add QMP response objects to the libqtest API, which currently discards QMP responses.
Patches 1 & 2 fix the leaks.
Patches 2 & 3 add QMP response objects to libqtest. Patches 5 & 6 add qtest test cases for the bugs.
Stefan Hajnoczi (6):
blockdev: fix drive_init() opts and bs_opts leaks
qdev: unref qdev when device_add fails
libqtest: rename qmp() to qmp_discard_response()
libqtest: add qmp(fmt, ...) -> QDict* function
blockdev-test: add test case for drive_add duplicate IDs
qdev-monitor-test: add device_add leak test cases
blockdev.c | 51 +++++++++++++++-------------- qdev-monitor.c | 4 ++- tests/Makefile | 4 +++ tests/blockdev-test.c | 59 ++++++++++++++++++++++++++++++++++ tests/boot-order-test.c | 4 +-- tests/fdc-test.c | 15 +++++---- tests/ide-test.c | 10 +++--- tests/libqtest.c | 72 +++++++++++++++++++++++++++++++---------- tests/libqtest.h | 51 +++++++++++++++++++++++++---- tests/qdev-monitor-test.c | 81 +++++++++++++++++++++++++++++++++++++++++++++++ 10 files changed, 290 insertions(+), 61 deletions(-) create mode 100644 tests/blockdev-test.c create mode 100644 tests/qdev-monitor-test.c
Signature · 1 line
1.8.3.1
PATCH 1/6 blockdev: fix drive_init() opts and bs_opts leaks
These memory leaks also make drive_add if=none,id=drive0 without a file= option leak the options list. This keeps ID "drive0" around forever.
--- blockdev.c | 51 +++++++++++++++++++++++++++------------------------ 1 file changed, 27 insertions(+), 24 deletions(-)
blockdev.c +27 -24
@@ -360,7 +360,7 @@ static DriveInfo *blockdev_init(QemuOpts *all_opts,if (error_is_set (&error)) {qerror_report_err (error);error_free (error); -return NULL ; + goto early_err; }if (id) {
@@ -390,7 +390,7 @@ static DriveInfo *blockdev_init(QemuOpts *all_opts, ;if (type ==IF_COUNT ) {error_report ("unsupported bus type '%s'" , buf); -return NULL ; + goto early_err; } }else { type = block_default_type;
@@ -401,15 +401,15 @@ static DriveInfo *blockdev_init(QemuOpts *all_opts,if (cyls || heads || secs) {if (cyls <1 ) {error_report ("invalid physical cyls number" ); -return NULL ; + goto early_err; }if (heads <1 ) {error_report ("invalid physical heads number" ); -return NULL ; + goto early_err; }if (secs <1 ) {error_report ("invalid physical secs number" ); -return NULL ; + goto early_err; } }
@@ -417,7 +417,7 @@ static DriveInfo *blockdev_init(QemuOpts *all_opts,if (!cyls) {error_report ("'%s' trans must be used with cyls, heads and secs" , buf); -return NULL ; + goto early_err; }if (!strcmp (buf,"none" )) translation =BIOS_ATA_TRANSLATION_NONE ;
@@ -427,7 +427,7 @@ static DriveInfo *blockdev_init(QemuOpts *all_opts, translation =BIOS_ATA_TRANSLATION_AUTO ; else {error_report ("'%s' invalid translation type" , buf); -return NULL ; + goto early_err; } }
@@ -437,19 +437,19 @@ static DriveInfo *blockdev_init(QemuOpts *all_opts, }else if (!strcmp (buf,"cdrom" )) {if (cyls || secs || heads) {error_report ("CHS can't be set with media=%s" , buf); -return NULL ; + goto early_err; } media =MEDIA_CDROM ; }else {error_report ("'%s' invalid media" , buf); -return NULL ; + goto early_err; } }if ((buf =qemu_opt_get (opts,"discard" )) !=NULL ) {if (bdrv_parse_discard_flags (buf, &bdrv_flags) !=0 ) {error_report ("invalid discard option" ); -return NULL ; + goto early_err; } }
@@ -471,7 +471,7 @@ static DriveInfo *blockdev_init(QemuOpts *all_opts, /* this is the default */ }else {error_report ("invalid aio option" ); -return NULL ; + goto early_err; } }#endif
@@ -481,7 +481,7 @@ static DriveInfo *blockdev_init(QemuOpts *all_opts,error_printf ("Supported formats:" );bdrv_iterate_format (bdrv_format_print,NULL );error_printf ("\n" ); -return NULL ; + goto early_err; } drv =bdrv_find_whitelisted_format (buf, ro);
@@ -491,7 +491,7 @@ static DriveInfo *blockdev_init(QemuOpts *all_opts, }else {error_report ("'%s' invalid format" , buf); } -return NULL ; + goto early_err; } }
@@ -512,7 +512,7 @@ static DriveInfo *blockdev_init(QemuOpts *all_opts,if (!do_check_io_limits (&io_limits, &error)) {error_report ("%s" ,error_get_pretty (error));error_free (error); -return NULL ; + goto early_err; }if (qemu_opt_get (opts,"boot" ) !=NULL ) {
@@ -525,12 +525,12 @@ static DriveInfo *blockdev_init(QemuOpts *all_opts,if ((buf =qemu_opt_get (opts,"werror" )) !=NULL ) {if (type !=IF_IDE && type !=IF_SCSI && type !=IF_VIRTIO && type !=IF_NONE ) {error_report ("werror is not supported by this bus type" ); -return NULL ; + goto early_err; } on_write_error =parse_block_error_action (buf,0 );if (on_write_error <0 ) { -return NULL ; + goto early_err; } }
@@ -538,19 +538,19 @@ static DriveInfo *blockdev_init(QemuOpts *all_opts,if ((buf =qemu_opt_get (opts,"rerror" )) !=NULL ) {if (type !=IF_IDE && type !=IF_VIRTIO && type !=IF_SCSI && type !=IF_NONE ) {error_report ("rerror is not supported by this bus type" ); -return NULL ; + goto early_err; } on_read_error =parse_block_error_action (buf,1 );if (on_read_error <0 ) { -return NULL ; + goto early_err; } }if ((devaddr =qemu_opt_get (opts,"addr" )) !=NULL ) {if (type !=IF_VIRTIO ) {error_report ("addr is not supported by this bus type" ); -return NULL ; + goto early_err; } }
@@ -559,7 +559,7 @@ static DriveInfo *blockdev_init(QemuOpts *all_opts,if (index !=-1 ) {if (bus_id !=0 || unit_id !=-1 ) {error_report ("index cannot be used with bus and unit" ); -return NULL ; + goto early_err; } bus_id =drive_index_to_bus_id (type, index); unit_id =drive_index_to_unit_id (type, index);
@@ -585,7 +585,7 @@ static DriveInfo *blockdev_init(QemuOpts *all_opts,if (max_devs && unit_id >= max_devs) {error_report ("unit %d too big (max is %d)" , unit_id, max_devs -1 ); -return NULL ; + goto early_err; }/*
@@ -595,7 +595,7 @@ static DriveInfo *blockdev_init(QemuOpts *all_opts,if (drive_get (type, bus_id, unit_id) !=NULL ) {error_report ("drive with bus=%d, unit=%d (index=%d) exists" , bus_id, unit_id, index); -return NULL ; + goto early_err; }/* init */
@@ -672,6 +672,8 @@ static DriveInfo *blockdev_init(QemuOpts *all_opts,if (has_driver_specific_opts) { file =NULL ; }else { +QDECREF (bs_opts); +qemu_opts_del (opts);return dinfo; } }
@@ -730,12 +732,13 @@ static DriveInfo *blockdev_init(QemuOpts *all_opts,return dinfo; err: -qemu_opts_del (opts); -QDECREF (bs_opts);bdrv_delete (dinfo->bdrv);g_free (dinfo->id);QTAILQ_REMOVE (&drives, dinfo, next);g_free (dinfo); +early_err: +QDECREF (bs_opts); +qemu_opts_del (opts);return NULL ; }
Signature · 1 line
1.8.3.1
PATCH 2/6 qdev: unref qdev when device_add fails
In reply to Stefan Hajnocziqdev_device_add() leaks the created qdev upon failure. I suspect this problem crept in because qdev_free() unparents the qdev but does not drop a reference - confusing name.
Also drop trailing whitespace after curly bracket.
--- qdev-monitor.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)
qdev-monitor.c +3 -1
@@ -512,6 +512,7 @@ DeviceState *qdev_device_add(QemuOpts *opts) }if (qemu_opt_foreach (opts, set_property, qdev,1 ) !=0 ) {qdev_free (qdev); +object_unref (OBJECT (qdev));return NULL ; }if (qdev->id) {
@@ -523,8 +524,9 @@ DeviceState *qdev_device_add(QemuOpts *opts)object_property_add_child (qdev_get_peripheral_anon (), name,OBJECT (qdev),NULL );g_free (name); - } + }if (qdev_init (qdev) <0 ) { +object_unref (OBJECT (qdev));qerror_report (QERR_DEVICE_INIT_FAILED , driver);return NULL ; }
Signature · 1 line
1.8.3.1
PATCH 3/6 libqtest: rename qmp() to qmp_discard_response()
In reply to Stefan HajnocziExisting qmp() callers do not expect a response object. In order to implement real QMP test cases it will be necessary to inspect the response object.
Rename qmp() to qmp_discard_response(). Later patches will introduce a qmp() function that returns the response object and tests that use it.
--- tests/boot-order-test.c | 4 ++-- tests/fdc-test.c | 15 +++++++++------ tests/ide-test.c | 10 ++++++---- tests/libqtest.c | 8 ++++---- tests/libqtest.h | 20 ++++++++++---------- 5 files changed, 31 insertions(+), 26 deletions(-)
tests/boot-order-test.c +2 -2
@@ -41,12 +41,12 @@ static void test_a_boot_order(const char *machine,qtest_start (args); actual =read_boot_order (); g_assert_cmphex(actual, ==, expected_boot); -qmp ("{ 'execute': 'system_reset' }" ); +qmp_discard_response ("{ 'execute': 'system_reset' }" );/* * system_reset only requests reset. We get a RESET event after * the actual reset completes. Need to wait for that. */ -qmp ("" );/* HACK: wait for event */ +qmp_discard_response ("" );/* HACK: wait for event */ actual =read_boot_order (); g_assert_cmphex(actual, ==, expected_reboot);qtest_quit (global_qtest);
tests/fdc-test.c +9 -6
@@ -290,10 +290,12 @@ static void test_media_insert(void)/* Insert media in drive. DSKCHK should not be reset until a step pulse * is sent. */ -qmp ("{'execute':'change', 'arguments':{ 'device':'floppy0', " -"'target': '%s' }}" , test_image); -qmp ("" );/* ignore event (FIXME open -> open transition?!) */ -qmp ("" );/* ignore event */ +qmp_discard_response ("{'execute':'change', 'arguments':{" +" 'device':'floppy0', 'target': '%s' }}" , + test_image); +qmp_discard_response ("" );/* ignore event +(FIXME open -> open transition?!) */ +qmp_discard_response ("" );/* ignore event */ dir =inb (FLOPPY_BASE + reg_dir);assert_bit_set (dir,DSKCHG );
@@ -322,8 +324,9 @@ static void test_media_change(void)/* Eject the floppy and check that DSKCHG is set. Reading it out doesn't * reset the bit. */ -qmp ("{'execute':'eject', 'arguments':{ 'device':'floppy0' }}" ); -qmp ("" );/* ignore event */ +qmp_discard_response ("{'execute':'eject', 'arguments':{" +" 'device':'floppy0' }}" ); +qmp_discard_response ("" );/* ignore event */ dir =inb (FLOPPY_BASE + reg_dir);assert_bit_set (dir,DSKCHG );
tests/ide-test.c +6 -4
@@ -435,8 +435,9 @@ static void test_flush(void) tmp_path);/* Delay the completion of the flush request until we explicitly do it */ -qmp ("{'execute':'human-monitor-command', 'arguments': { " -"'command-line': 'qemu-io ide0-hd0 \"break flush_to_os A\"'} }" ); +qmp_discard_response ("{'execute':'human-monitor-command', 'arguments': {" +" 'command-line':" +" 'qemu-io ide0-hd0 \"break flush_to_os A\"'} }" );/* FLUSH CACHE command on device 0*/ outb (IDE_BASE + reg_device,0 );
@@ -448,8 +449,9 @@ static void test_flush(void)assert_bit_clear (data,DF |ERR |DRQ );/* Complete the command */ -qmp ("{'execute':'human-monitor-command', 'arguments': { " -"'command-line': 'qemu-io ide0-hd0 \"resume A\"'} }" ); +qmp_discard_response ("{'execute':'human-monitor-command', 'arguments': {" +" 'command-line':" +" 'qemu-io ide0-hd0 \"resume A\"'} }" );/* Check registers */ data =inb (IDE_BASE + reg_device);
tests/libqtest.c +4 -4
@@ -151,8 +151,8 @@ QTestState *qtest_init(const char *extra_args) }/* Read the QMP greeting and then do the handshake */ -qtest_qmp (s,"" ); -qtest_qmp (s,"{ 'execute': 'qmp_capabilities' }" ); +qtest_qmp_discard_response (s,"" ); +qtest_qmp_discard_response (s,"{ 'execute': 'qmp_capabilities' }" );if (getenv ("QTEST_STOP" )) {kill (qtest_qemu_pid (s),SIGSTOP );
@@ -291,7 +291,7 @@ redo:return words; } -void qtest_qmpv (QTestState *s,const char *fmt,va_list ap) +void qtest_qmpv_discard_response (QTestState *s,const char *fmt,va_list ap) {bool has_reply = false;int nesting =0 ;
@@ -326,7 +326,7 @@ void qtest_qmpv(QTestState *s, const char *fmt, va_list ap) } } -void qtest_qmp (QTestState *s,const char *fmt, ...) +void qtest_qmp_discard_response (QTestState *s,const char *fmt, ...) {va_list ap;
tests/libqtest.h +10 -10
@@ -44,23 +44,23 @@ QTestState *qtest_init(const char *extra_args);void qtest_quit (QTestState *s);/** -* qtest_qmp: +* qtest_qmp_discard_response: * @s: #QTestState instance to operate on. * @fmt...: QMP message to send to qemu * -* Sends a QMP message to QEMU +* Sends a QMP message to QEMU and consumes the response. */ -void qtest_qmp (QTestState *s,const char *fmt, ...); +void qtest_qmp_discard_response (QTestState *s,const char *fmt, ...);/** -* qtest_qmpv: +* qtest_qmpv_discard_response: * @s: #QTestState instance to operate on. * @fmt: QMP message to send to QEMU * @ap: QMP message arguments * -* Sends a QMP message to QEMU. +* Sends a QMP message to QEMU and consumes the response. */ -void qtest_qmpv (QTestState *s,const char *fmt,va_list ap); +void qtest_qmpv_discard_response (QTestState *s,const char *fmt,va_list ap);/** * qtest_get_irq:
@@ -331,17 +331,17 @@ static inline void qtest_end(void) }/** -* qmp: +* qmp_discard_response: * @fmt...: QMP message to send to qemu * -* Sends a QMP message to QEMU +* Sends a QMP message to QEMU and consumes the response. */ -static inline void qmp (const char *fmt, ...) +static inline void qmp_discard_response (const char *fmt, ...) {va_list ap;va_start (ap, fmt); -qtest_qmpv (global_qtest, fmt, ap); +qtest_qmpv_discard_response (global_qtest, fmt, ap);va_end (ap); }
Signature · 1 line
1.8.3.1
PATCH 4/6 libqtest: add qmp(fmt, ...) -> QDict* function
In reply to Stefan HajnocziAdd a qtest qmp() function that returns the response object. This allows test cases to verify the result or to check for error responses. It also allows waiting for QMP events.
--- tests/libqtest.c | 66 ++++++++++++++++++++++++++++++++++++++++++++------------ tests/libqtest.h | 37 +++++++++++++++++++++++++++++++ 2 files changed, 89 insertions(+), 14 deletions(-)
tests/libqtest.c +52 -14
@@ -30,6 +30,8 @@#include "qemu/compiler.h" #include "qemu/osdep.h" +#include "qapi/qmp/json-streamer.h" +#include "qapi/qmp/json-parser.h" #define MAX_IRQ 256
@@ -291,16 +293,38 @@ redo:return words; } -void qtest_qmpv_discard_response (QTestState *s,const char *fmt,va_list ap) +typedef struct { +JSONMessageParser parser; +QDict *response; +}QMPResponseParser ; + +static void qmp_response (JSONMessageParser *parser,QList *tokens) { -bool has_reply = false; -int nesting =0 ; +QMPResponseParser *qmp =container_of (parser, QMPResponseParser, parser); +QObject *obj; + + obj =json_parser_parse (tokens,NULL ); +if (!obj) { +fprintf (stderr,"QMP JSON response parsing failed\n" ); +exit (1 ); + } + +g_assert (qobject_type (obj) ==QTYPE_QDICT ); +g_assert (!qmp->response); + qmp->response = (QDict *)obj; +} + +QDict *qtest_qmpv (QTestState *s,const char *fmt,va_list ap) +{ +QMPResponseParser qmp;/* Send QMP request */ socket_sendf (s->qmp_fd, fmt, ap);/* Receive reply */ -while (!has_reply || nesting >0 ) { + qmp.response =NULL ; +json_message_parser_init (&qmp.parser, qmp_response); +while (!qmp.response) {ssize_t len;char c;
@@ -314,25 +338,39 @@ void qtest_qmpv_discard_response(QTestState *s, const char *fmt, va_list ap)exit (1 ); } -switch (c) { -case '{' : - nesting++; - has_reply = true; -break ; -case '}' : - nesting--; -break ; - } +json_message_parser_feed (&qmp.parser, &c,1 ); } +json_message_parser_destroy (&qmp.parser); + +return qmp.response; +} + +QDict *qtest_qmp (QTestState *s,const char *fmt, ...) +{ +va_list ap; +QDict *response; + +va_start (ap, fmt); + response =qtest_qmpv (s, fmt, ap); +va_end (ap); +return response; +} + +void qtest_qmpv_discard_response (QTestState *s,const char *fmt,va_list ap) +{ +QDict *response =qtest_qmpv (s, fmt, ap); +QDECREF (response); }void qtest_qmp_discard_response (QTestState *s,const char *fmt, ...) {va_list ap; +QDict *response;va_start (ap, fmt); -qtest_qmpv (s, fmt, ap); + response =qtest_qmpv (s, fmt, ap);va_end (ap); +QDECREF (response); }const char *qtest_get_arch (void )
tests/libqtest.h +37 -0
@@ -22,6 +22,7 @@#include <stdbool.h> #include <stdarg.h> #include <sys/types.h> +#include "qapi/qmp/qdict.h" typedef struct QTestState QTestState ;
@@ -53,6 +54,15 @@ void qtest_quit(QTestState *s);void qtest_qmp_discard_response (QTestState *s,const char *fmt, ...);/** +* qtest_qmp: +* @s: #QTestState instance to operate on. +* @fmt...: QMP message to send to qemu +* +* Sends a QMP message to QEMU and returns the response. +*/ +QDict *qtest_qmp (QTestState *s,const char *fmt, ...); + +/** * qtest_qmpv_discard_response: * @s: #QTestState instance to operate on. * @fmt: QMP message to send to QEMU
@@ -63,6 +73,16 @@ void qtest_qmp_discard_response(QTestState *s, const char *fmt, ...);void qtest_qmpv_discard_response (QTestState *s,const char *fmt,va_list ap);/** +* qtest_qmpv: +* @s: #QTestState instance to operate on. +* @fmt: QMP message to send to QEMU +* @ap: QMP message arguments +* +* Sends a QMP message to QEMU and returns the response. +*/ +QDict *qtest_qmpv (QTestState *s,const char *fmt,va_list ap); + +/** * qtest_get_irq: * @s: #QTestState instance to operate on. * @num: Interrupt to observe.
@@ -331,6 +351,23 @@ static inline void qtest_end(void) }/** +* qmp: +* @fmt...: QMP message to send to qemu +* +* Sends a QMP message to QEMU and returns the response. +*/ +static inline QDict *qmp (const char *fmt, ...) +{ +va_list ap; +QDict *response; + +va_start (ap, fmt); + response =qtest_qmpv (global_qtest, fmt, ap); +va_end (ap); +return response; +} + +/** * qmp_discard_response: * @fmt...: QMP message to send to qemu *
Signature · 1 line
1.8.3.1
PATCH 5/6 blockdev-test: add test case for drive_add duplicate IDs
In reply to Stefan HajnocziThe following should work:
(qemu) drive_add if=none,id=drive0
(qemu) drive_del drive0
(qemu) drive_add if=none,id=drive0
Previous versions of QEMU produced a duplicate ID error because drive_add leaked the options.
--- tests/Makefile | 2 ++ tests/blockdev-test.c | 59 +++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 61 insertions(+) create mode 100644 tests/blockdev-test.c
tests/Makefile +2 -0
@@ -66,6 +66,7 @@ check-qtest-i386-y += tests/boot-order-test$(EXESUF) check-qtest-i386-y += tests/rtc-test$(EXESUF) check-qtest-i386-y += tests/i440fx-test$(EXESUF) check-qtest-i386-y += tests/fw_cfg-test$(EXESUF) +check-qtest-i386-y += tests/blockdev-test$(EXESUF) check-qtest-x86_64-y = $(check-qtest-i386-y) gcov-files-i386-y += i386-softmmu/hw/mc146818rtc.c gcov-files-x86_64-y = $(subst i386-softmmu/,x86_64-softmmu/,$(gcov-files-i386-y))
@@ -172,6 +173,7 @@ tests/boot-order-test$(EXESUF): tests/boot-order-test.o $(libqos-obj-y) tests/tmp105-test$(EXESUF): tests/tmp105-test.o $(libqos-omap-obj-y) tests/i440fx-test$(EXESUF): tests/i440fx-test.o $(libqos-pc-obj-y) tests/fw_cfg-test$(EXESUF): tests/fw_cfg-test.o $(libqos-pc-obj-y) +tests/blockdev-test$(EXESUF): tests/blockdev-test.o $(libqos-pc-obj-y) # QTest rules
tests/blockdev-test.c new file +59 -0
59 lines not on this page · raw message
Signature · 1 line
1.8.3.1
PATCH 6/6 qdev-monitor-test: add device_add leak test cases
In reply to Stefan HajnocziEnsure that the device_add error code path deletes device objects. Failure to do so not only leaks the objects but can also keep other objects (like drive or netdev) alive due to qdev properties holding references.
--- tests/Makefile | 2 ++ tests/qdev-monitor-test.c | 81 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 83 insertions(+) create mode 100644 tests/qdev-monitor-test.c
tests/Makefile +2 -0
@@ -67,6 +67,7 @@ check-qtest-i386-y += tests/rtc-test$(EXESUF) check-qtest-i386-y += tests/i440fx-test$(EXESUF) check-qtest-i386-y += tests/fw_cfg-test$(EXESUF) check-qtest-i386-y += tests/blockdev-test$(EXESUF) +check-qtest-i386-y += tests/qdev-monitor-test$(EXESUF) check-qtest-x86_64-y = $(check-qtest-i386-y) gcov-files-i386-y += i386-softmmu/hw/mc146818rtc.c gcov-files-x86_64-y = $(subst i386-softmmu/,x86_64-softmmu/,$(gcov-files-i386-y))
@@ -174,6 +175,7 @@ tests/tmp105-test$(EXESUF): tests/tmp105-test.o $(libqos-omap-obj-y) tests/i440fx-test$(EXESUF): tests/i440fx-test.o $(libqos-pc-obj-y) tests/fw_cfg-test$(EXESUF): tests/fw_cfg-test.o $(libqos-pc-obj-y) tests/blockdev-test$(EXESUF): tests/blockdev-test.o $(libqos-pc-obj-y) +tests/qdev-monitor-test$(EXESUF): tests/qdev-monitor-test.o $(libqos-pc-obj-y) # QTest rules
tests/qdev-monitor-test.c new file +81 -0
81 lines not on this page · raw message
Signature · 1 line
1.8.3.1
Re: [Qemu-devel] [PATCH 2/6] qdev: unref qdev when device_add fails
In reply to Stefan HajnocziIl 10/09/2013 18:21, Stefan Hajnoczi ha scritto:
Quoted 3 lines
qdev_device_add()leaks the created qdev upon failure. I suspect this problem crept in becauseqdev_free()unparents the qdev but does not drop a reference - confusing name.
Right, the name a leftover from pre-refcounting days.
BTW, not dropping a reference is the right thing to do because the reference is dropped much earlier, typically as soon as qdev_device_add returns. The QOM object tree then will still provide means to access devices, until they are unparented.
In this case, however, qdev_device_add's caller does not have a reference to free; doing that is the responsibility of qdev_device_add, since it returns NULL.
Quoted 21 lines
Also drop trailing whitespace after curly bracket.
Signed-off-by Stefan Hajnoczi <[email protected]>--- qdev-monitor.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)qdev-monitor.c +1
-0@@ -512,6 +512,7 @@ DeviceState *qdev_device_add(QemuOpts *opts) }if (qemu_opt_foreach (opts, set_property, qdev,1 ) !=0 ) {qdev_free (qdev); +object_unref (OBJECT (qdev));return NULL ; }if (qdev->id) {
qdev-monitor.c DeviceState *qdev_device_add(QemuOpts *opts) · line 532
Quoted 10 lines
qdev-monitor.c +2
-1@@ -523,8 +524,9 @@ DeviceState *qdev_device_add(QemuOpts *opts)object_property_add_child (qdev_get_peripheral_anon (), name,OBJECT (qdev),NULL );g_free (name); - } + }if (qdev_init (qdev) <0 ) { +object_unref (OBJECT (qdev));qerror_report (QERR_DEVICE_INIT_FAILED , driver);return NULL ; }
Re: [Qemu-devel] [PATCH 2/6] qdev: unref qdev when device_add fails
Am 10.09.2013 18:49, schrieb Paolo Bonzini:
Quoted 23 lines
Il 10/09/2013 18:21, Stefan Hajnoczi ha scritto:
Quoted 3 lines
qdev_device_add()leaks the created qdev upon failure. I suspect this problem crept in becauseqdev_free()unparents the qdev but does not drop a reference - confusing name.Right, the name a leftover from pre-refcounting days.
BTW, not dropping a reference is the right thing to do because the reference is dropped much earlier, typically as soon as
qdev_device_addreturns. The QOM object tree then will still provide means to access devices, until they are unparented.In this case, however, qdev_device_add's caller does not have a reference to free; doing that is the responsibility of
qdev_device_add, since it returns NULL.Quoted 7 lines
Also drop trailing whitespace after curly bracket.
Signed-off-by Stefan Hajnoczi <[email protected]>--- qdev-monitor.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)
Quoted 12 lines
qdev-monitor.c DeviceState *qdev_device_add(QemuOpts *opts) · line 518
Quoted 12 lines
qdev-monitor.c +1
-0@@ -512,6 +512,7 @@ DeviceState *qdev_device_add(QemuOpts *opts) }if (qemu_opt_foreach (opts, set_property, qdev,1 ) !=0 ) {qdev_free (qdev); +object_unref (OBJECT (qdev));return NULL ; }if (qdev->id) {
Given that qdev_free() doesn't do what one might expect, I would suggest to s/qdev_free/object_unparent/g above.
Quoted 6 lines
Quoted 6 lines
qdev-monitor.c +1
-1@@ -523,8 +524,9 @@ DeviceState *qdev_device_add(QemuOpts *opts)object_property_add_child (qdev_get_peripheral_anon (), name,OBJECT (qdev),NULL );g_free (name); - } + }
Quoted 8 lines
qdev-monitor.c DeviceState *qdev_device_add(QemuOpts *opts) · line 532
Quoted 6 lines
qdev-monitor.c +1
-0if (qdev_init (qdev) <0 ) { +object_unref (OBJECT (qdev));qerror_report (QERR_DEVICE_INIT_FAILED , driver);return NULL ; }Reviewed-by Paolo Bonzini <[email protected]>
I would like to take this through qom-next tree since I have pending variable cleanups there ("qdev" being touched here). Not sure how to handle that wrt block changes in this series?
Andreas
Signature · 2 lines
SUSE LINUX Products GmbH, Maxfeldstr. 5, 90409 Nürnberg, Germany GF: Jeff Hawn, Jennifer Guild, Felix Imendörffer; HRB 16746 AG Nürnberg
Re: [Qemu-devel] [PATCH 2/6] qdev: unref qdev when device_add fails
Il 10/09/2013 18:59, Andreas Färber ha scritto:
Quoted 24 lines
Am 10.09.2013 18:49, schrieb Paolo Bonzini:
Quoted 23 lines
Il 10/09/2013 18:21, Stefan Hajnoczi ha scritto:
Quoted 3 lines
qdev_device_add()leaks the created qdev upon failure. I suspect this problem crept in becauseqdev_free()unparents the qdev but does not drop a reference - confusing name.Right, the name a leftover from pre-refcounting days.
BTW, not dropping a reference is the right thing to do because the reference is dropped much earlier, typically as soon as
qdev_device_addreturns. The QOM object tree then will still provide means to access devices, until they are unparented.In this case, however, qdev_device_add's caller does not have a reference to free; doing that is the responsibility of
qdev_device_add, since it returns NULL.Quoted 7 lines
Also drop trailing whitespace after curly bracket.
Signed-off-by Stefan Hajnoczi <[email protected]>--- qdev-monitor.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)
Quoted 15 lines
Quoted 4 lines
Quoted 4 lines
qdev-monitor.c +0
-0Quoted 8 lines
qdev-monitor.c DeviceState *qdev_device_add(QemuOpts *opts) · line 518
Quoted 8 lines
qdev-monitor.c +1
-0@@ -512,6 +512,7 @@ DeviceState *qdev_device_add(QemuOpts *opts) }if (qemu_opt_foreach (opts, set_property, qdev,1 ) !=0 ) {qdev_free (qdev); +object_unref (OBJECT (qdev));return NULL ; }if (qdev->id) {Given that
qdev_free()doesn't do what one might expect, I would suggest to s/qdev_free/object_unparent/g above.
Then do it everywhere...
Paolo
Quoted 21 lines
Quoted 6 lines
Quoted 6 lines
qdev-monitor.c +1
-1@@ -523,8 +524,9 @@ DeviceState *qdev_device_add(QemuOpts *opts)object_property_add_child (qdev_get_peripheral_anon (), name,OBJECT (qdev),NULL );g_free (name); - } + }Quoted 8 lines
qdev-monitor.c DeviceState *qdev_device_add(QemuOpts *opts) · line 532
Quoted 6 lines
qdev-monitor.c +1
-0if (qdev_init (qdev) <0 ) { +object_unref (OBJECT (qdev));qerror_report (QERR_DEVICE_INIT_FAILED , driver);return NULL ; }Reviewed-by Paolo Bonzini <[email protected]>I would like to take this through qom-next tree since I have pending variable cleanups there ("qdev" being touched here). Not sure how to handle that wrt block changes in this series?
Andreas
Re: [Qemu-devel] [PATCH 2/6] qdev: unref qdev when device_add fails
In reply to Andreas FärberQuoted 24 lines · Andreas Färber
Am 10.09.2013 18:49, schrieb Paolo Bonzini:
Quoted 23 lines
Il 10/09/2013 18:21, Stefan Hajnoczi ha scritto:
Quoted 3 lines
qdev_device_add()leaks the created qdev upon failure. I suspect this problem crept in becauseqdev_free()unparents the qdev but does not drop a reference - confusing name.Right, the name a leftover from pre-refcounting days.
BTW, not dropping a reference is the right thing to do because the reference is dropped much earlier, typically as soon as
qdev_device_addreturns. The QOM object tree then will still provide means to access devices, until they are unparented.In this case, however, qdev_device_add's caller does not have a reference to free; doing that is the responsibility of
qdev_device_add, since it returns NULL.Quoted 7 lines
Also drop trailing whitespace after curly bracket.
Signed-off-by Stefan Hajnoczi <[email protected]>--- qdev-monitor.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)
Quoted 15 lines
Quoted 4 lines
Quoted 4 lines
qdev-monitor.c +0
-0Quoted 8 lines
qdev-monitor.c DeviceState *qdev_device_add(QemuOpts *opts) · line 518
Quoted 8 lines
qdev-monitor.c +1
-0@@ -512,6 +512,7 @@ DeviceState *qdev_device_add(QemuOpts *opts) }if (qemu_opt_foreach (opts, set_property, qdev,1 ) !=0 ) {qdev_free (qdev); +object_unref (OBJECT (qdev));return NULL ; }if (qdev->id) {Given that
qdev_free()doesn't do what one might expect, I would suggest to s/qdev_free/object_unparent/g above.
I'll send a follow-up patch to do this across the tree.
Quoted 15 lines
Quoted 6 lines
Quoted 6 lines
qdev-monitor.c +1
-1@@ -523,8 +524,9 @@ DeviceState *qdev_device_add(QemuOpts *opts)object_property_add_child (qdev_get_peripheral_anon (), name,OBJECT (qdev),NULL );g_free (name); - } + }Quoted 8 lines
qdev-monitor.c DeviceState *qdev_device_add(QemuOpts *opts) · line 532
Quoted 6 lines
qdev-monitor.c +1
-0if (qdev_init (qdev) <0 ) { +object_unref (OBJECT (qdev));qerror_report (QERR_DEVICE_INIT_FAILED , driver);return NULL ; }Reviewed-by Paolo Bonzini <[email protected]>
Quoted 3 lines
I would like to take this through qom-next tree since I have pending variable cleanups there ("qdev" being touched here). Not sure how to handle that wrt block changes in this series?
It would be simplest to take the whole series. Splitting it is hard because the qdev device_add test case requires the blockdev.c fix (otherwise the drive ID is held due to the leaked options list).
Or you can cherry-pick just this patch and the rest of the series can go via the block tree?
Stefan