All of lore.kernel.org
 help / color / mirror / Atom feed
From: Damien Hedde <damien.hedde@greensocs.com>
To: Vladimir Sementsov-Ogievskiy <vsementsov@virtuozzo.com>,
	Kevin Wolf <kwolf@redhat.com>,
	qemu-devel@nongnu.org
Cc: pkrempa@redhat.com, berrange@redhat.com, ehabkost@redhat.com,
	qemu-block@nongnu.org, libvir-list@redhat.com, armbru@redhat.com,
	its@irrelevant.dk, pbonzini@redhat.com
Subject: Re: [PATCH 06/11] qdev: Add Error parameter to qdev_set_id()
Date: Mon, 27 Sep 2021 12:33:32 +0200	[thread overview]
Message-ID: <90412958-721b-4ae8-0177-3ae6af8394af@greensocs.com> (raw)
In-Reply-To: <7b9afc35-af95-ad21-6296-7e112ada9c87@virtuozzo.com>

Hi Kevin,

I proposed a very similar patch in our rfc series because we needed some 
of the cleaning you do here.
https://lists.gnu.org/archive/html/qemu-devel/2021-09/msg05679.html
I've added a bit of doc for the function, feel free to take it if you want.

On 9/24/21 16:09, Vladimir Sementsov-Ogievskiy wrote:
> 24.09.2021 12:04, Kevin Wolf wrote:
>> object_property_add_child() fails (with &error_abort) if an object with
>> the same name already exists. As long as QemuOpts is in use for -device
>> and device_add, it catches duplicate IDs before qdev_set_id() is even
>> called. However, for enabling non-QemuOpts code paths, we need to make
>> sure that the condition doesn't cause a crash, but fails gracefully.
>>
>> Signed-off-by: Kevin Wolf <kwolf@redhat.com>
>> ---
>>   include/monitor/qdev.h      |  2 +-
>>   hw/xen/xen-legacy-backend.c |  3 ++-
>>   softmmu/qdev-monitor.c      | 16 ++++++++++------
>>   3 files changed, 13 insertions(+), 8 deletions(-)
>>
>> diff --git a/include/monitor/qdev.h b/include/monitor/qdev.h
>> index 389287eb44..7961308c75 100644
>> --- a/include/monitor/qdev.h
>> +++ b/include/monitor/qdev.h
>> @@ -9,6 +9,6 @@ void qmp_device_add(QDict *qdict, QObject **ret_data, 
>> Error **errp);
>>   int qdev_device_help(QemuOpts *opts);
>>   DeviceState *qdev_device_add(QemuOpts *opts, Error **errp);
>> -void qdev_set_id(DeviceState *dev, char *id);
>> +void qdev_set_id(DeviceState *dev, char *id, Error **errp);
>>   #endif
>> diff --git a/hw/xen/xen-legacy-backend.c b/hw/xen/xen-legacy-backend.c
>> index dd8ae1452d..17aca85ddc 100644
>> --- a/hw/xen/xen-legacy-backend.c
>> +++ b/hw/xen/xen-legacy-backend.c
>> @@ -276,7 +276,8 @@ static struct XenLegacyDevice 
>> *xen_be_get_xendev(const char *type, int dom,
>>       xendev = g_malloc0(ops->size);
>>       object_initialize(&xendev->qdev, ops->size, TYPE_XENBACKEND);
>>       OBJECT(xendev)->free = g_free;
>> -    qdev_set_id(DEVICE(xendev), g_strdup_printf("xen-%s-%d", type, 
>> dev));
>> +    qdev_set_id(DEVICE(xendev), g_strdup_printf("xen-%s-%d", type, dev),
>> +                &error_abort);
>>       qdev_realize(DEVICE(xendev), xen_sysbus, &error_fatal);
>>       object_unref(OBJECT(xendev));
>> diff --git a/softmmu/qdev-monitor.c b/softmmu/qdev-monitor.c
>> index 1207e57a46..c2af906df0 100644
>> --- a/softmmu/qdev-monitor.c
>> +++ b/softmmu/qdev-monitor.c
>> @@ -593,26 +593,27 @@ static BusState *qbus_find(const char *path, 
>> Error **errp)
>>   }
>>   /* Takes ownership of @id, will be freed when deleting the device */
>> -void qdev_set_id(DeviceState *dev, char *id)
>> +void qdev_set_id(DeviceState *dev, char *id, Error **errp)
> 
> According to recommendations in error.h, worth adding also return value 
> (for example true=success false=failure), so [..]
> 
>>   {
>>       if (id) {
>>           dev->id = id;
>>       }
> 
> Unrelated but.. What's the strange logic?
> 
> Is it intended that with passed id=NULL we don't update dev->id variable 
> but try to do following logic with old dev->id?

dev->id is expected to be NULL. The object is created just before 
calling this function so it is always the case. We could probably assert 
this.

> 
>>       if (dev->id) {
>> -        object_property_add_child(qdev_get_peripheral(), dev->id,
>> -                                  OBJECT(dev));
>> +        object_property_try_add_child(qdev_get_peripheral(), dev->id,
>> +                                      OBJECT(dev), errp);
>>       } else {
>>           static int anon_count;
>>           gchar *name = g_strdup_printf("device[%d]", anon_count++);
>> -        object_property_add_child(qdev_get_peripheral_anon(), name,
>> -                                  OBJECT(dev));
>> +        object_property_try_add_child(qdev_get_peripheral_anon(), name,
>> +                                      OBJECT(dev), errp);
>>           g_free(name);
>>       }
>>   }
>>   DeviceState *qdev_device_add(QemuOpts *opts, Error **errp)
>>   {
>> +    ERRP_GUARD();
>>       DeviceClass *dc;
>>       const char *driver, *path;
>>       DeviceState *dev = NULL;
>> @@ -691,7 +692,10 @@ DeviceState *qdev_device_add(QemuOpts *opts, 
>> Error **errp)
>>           }
>>       }
>> -    qdev_set_id(dev, g_strdup(qemu_opts_id(opts)));
>> +    qdev_set_id(dev, g_strdup(qemu_opts_id(opts)), errp);
>> +    if (*errp) {
>> +        goto err_del_dev;
>> +    }
> 
> [..] here we'll have
> 
> if (!qdev_set_id(...)) {
>    goto err_del_dev;
> }
> 
> and no need for ERRP_GUARD.
> 
>>       /* set properties */
>>       if (qemu_opt_foreach(opts, set_property, dev, errp)) {
>>
> 
> 


  reply	other threads:[~2021-09-27 10:37 UTC|newest]

Thread overview: 49+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-09-24  9:04 [PATCH 00/11] qdev: Add JSON -device and fix QMP device_add Kevin Wolf
2021-09-24  9:04 ` [PATCH 01/11] qom: Reduce use of error_propagate() Kevin Wolf
2021-09-24 13:23   ` Vladimir Sementsov-Ogievskiy
2021-09-24 14:04   ` Markus Armbruster
2021-09-24 18:14   ` Eric Blake
2021-09-24  9:04 ` [PATCH 02/11] iotests/245: Fix type for iothread property Kevin Wolf
2021-09-24 13:33   ` Vladimir Sementsov-Ogievskiy
2021-09-24  9:04 ` [PATCH 03/11] iotests/051: Fix typo Kevin Wolf
2021-09-24 13:35   ` Vladimir Sementsov-Ogievskiy
2021-09-24  9:04 ` [PATCH 04/11] qdev: Avoid using string visitor for properties Kevin Wolf
2021-09-24 18:40   ` Eric Blake
2021-09-24  9:04 ` [PATCH 05/11] qdev: Make DeviceState.id independent of QemuOpts Kevin Wolf
2021-09-24 14:02   ` Vladimir Sementsov-Ogievskiy
2021-09-24 15:10     ` Kevin Wolf
2021-09-24 15:14       ` Vladimir Sementsov-Ogievskiy
2021-09-24  9:04 ` [PATCH 06/11] qdev: Add Error parameter to qdev_set_id() Kevin Wolf
2021-09-24 14:09   ` Vladimir Sementsov-Ogievskiy
2021-09-27 10:33     ` Damien Hedde [this message]
2021-10-05 11:09       ` Kevin Wolf
2021-09-24  9:04 ` [PATCH 07/11] qemu-option: Allow deleting opts during qemu_opts_foreach() Kevin Wolf
2021-09-24 14:14   ` Vladimir Sementsov-Ogievskiy
2021-09-24  9:04 ` [PATCH 08/11] qdev: Base object creation on QDict rather than QemuOpts Kevin Wolf
2021-09-24 18:53   ` Eric Blake
2021-09-24  9:04 ` [PATCH 09/11] qdev: Avoid QemuOpts in QMP device_add Kevin Wolf
2021-09-24 18:56   ` Eric Blake
2021-09-27 11:06   ` Damien Hedde
2021-09-27 11:39     ` Kevin Wolf
2021-10-05 14:37       ` Kevin Wolf
2021-10-05 15:52         ` Damien Hedde
2021-10-05 17:33           ` Kevin Wolf
2021-10-06  8:21             ` Juan Quintela
2021-10-06  9:20               ` Laurent Vivier
2021-10-06 10:53                 ` Kevin Wolf
2021-10-06 11:09                   ` Laurent Vivier
2021-10-01 14:42   ` Peter Krempa
2021-10-04 12:18     ` Damien Hedde
2021-10-04 14:22       ` Kevin Wolf
2021-09-24  9:04 ` [PATCH 10/11] vl: Enable JSON syntax for -device Kevin Wolf
2021-09-24 19:00   ` Eric Blake
2021-09-24  9:04 ` [PATCH 11/11] Deprecate stable non-JSON -device and -object Kevin Wolf
2021-09-24 19:02   ` Eric Blake
2021-09-27  8:15   ` Paolo Bonzini
2021-09-27  8:21     ` Daniel P. Berrangé
2021-09-27 10:17       ` Kevin Wolf
2021-09-27 10:37         ` Daniel P. Berrangé
2021-09-27  9:00   ` Peter Maydell
2021-09-27 11:27     ` Kevin Wolf
2021-09-27 12:52       ` Peter Maydell
2021-09-27 16:10         ` Kevin Wolf

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=90412958-721b-4ae8-0177-3ae6af8394af@greensocs.com \
    --to=damien.hedde@greensocs.com \
    --cc=armbru@redhat.com \
    --cc=berrange@redhat.com \
    --cc=ehabkost@redhat.com \
    --cc=its@irrelevant.dk \
    --cc=kwolf@redhat.com \
    --cc=libvir-list@redhat.com \
    --cc=pbonzini@redhat.com \
    --cc=pkrempa@redhat.com \
    --cc=qemu-block@nongnu.org \
    --cc=qemu-devel@nongnu.org \
    --cc=vsementsov@virtuozzo.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.