qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: "Daniel P. Berrange" <berrange@redhat.com>
To: Igor Mammedov <imammedo@redhat.com>
Cc: pbonzini@redhat.com, qemu-devel@nongnu.org, armbru@redhat.com,
	afaerber@suse.de
Subject: Re: [Qemu-devel] [PATCH 2/2] qdev: safely fail device_add if unable to allocate device
Date: Fri, 18 Dec 2015 16:48:23 +0000	[thread overview]
Message-ID: <20151218164823.GH7228@redhat.com> (raw)
In-Reply-To: <1450452647-118105-3-git-send-email-imammedo@redhat.com>

On Fri, Dec 18, 2015 at 04:30:47PM +0100, Igor Mammedov wrote:
> qdev_device_add() currently uses object_new() which
> will abort if there memory allocation for device instance
> fails. While it's fine it startup, it is not desirable
> diring hotplug.
> 
> Try to allocate memory for object first and fail safely
> if allocation fails.
> 
> Signed-off-by: Igor Mammedov <imammedo@redhat.com>
> ---
> It's just a step in making hotplug safer wrt object allocation.
> To make it more safer, hotplugged class constructor
> shouldn't allocate memory either, but that should be
> addressed on per device basis providing we fix QOM
> internals to avoid dynamic allocations.
> ---
>  qdev-monitor.c | 9 ++++++++-
>  1 file changed, 8 insertions(+), 1 deletion(-)
> 
> diff --git a/qdev-monitor.c b/qdev-monitor.c
> index a35098f..a70262e 100644
> --- a/qdev-monitor.c
> +++ b/qdev-monitor.c
> @@ -514,6 +514,7 @@ DeviceState *qdev_device_add(QemuOpts *opts, Error **errp)
>      DeviceClass *dc;
>      const char *driver, *path, *id;
>      DeviceState *dev;
> +    size_t obj_size;
>      BusState *bus = NULL;
>      Error *err = NULL;
>  
> @@ -555,7 +556,13 @@ DeviceState *qdev_device_add(QemuOpts *opts, Error **errp)
>      }
>  
>      /* create device */
> -    dev = DEVICE(object_new(driver));
> +    obj_size = object_class_get_instance_size(OBJECT_CLASS(dc));
> +    dev = g_try_malloc0(obj_size);
> +    if (dev == NULL) {
> +        error_setg(errp, "Not enough memory for Device '%s'", driver);
> +        return NULL;
> +    }

This just avoids one small malloc failure.

> +    object_initialize(dev, obj_size, driver);

This is going to call g_new many more times, so you'll
still hit OOM almost immediately. eg the call to
g_hash_table_new_full() in object_initialize_with_type
will abort on OOM, not to mention anything run in a
instance constructor function registered against the
class. There's no way to avoid this given that we have
chosen to use GLib in QEMU, so I don't really see any
point in replacing the 'object_new' call with g_try_malloc

Regards,
Daniel
-- 
|: http://berrange.com      -o-    http://www.flickr.com/photos/dberrange/ :|
|: http://libvirt.org              -o-             http://virt-manager.org :|
|: http://autobuild.org       -o-         http://search.cpan.org/~danberr/ :|
|: http://entangle-photo.org       -o-       http://live.gnome.org/gtk-vnc :|

  reply	other threads:[~2015-12-18 16:48 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-12-18 15:30 [Qemu-devel] [PATCH 0/2] qdev: fail safely if can't allocate device in device_add() Igor Mammedov
2015-12-18 15:30 ` [Qemu-devel] [PATCH 1/2] qom: add object_class_get_instance_size() Igor Mammedov
2016-01-11 15:37   ` Andreas Färber
2015-12-18 15:30 ` [Qemu-devel] [PATCH 2/2] qdev: safely fail device_add if unable to allocate device Igor Mammedov
2015-12-18 16:48   ` Daniel P. Berrange [this message]
2015-12-18 17:26     ` Eric Blake
2015-12-18 21:15       ` Markus Armbruster
2016-01-11 16:04         ` Andreas Färber
2016-01-12 15:43           ` Daniel P. Berrange

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=20151218164823.GH7228@redhat.com \
    --to=berrange@redhat.com \
    --cc=afaerber@suse.de \
    --cc=armbru@redhat.com \
    --cc=imammedo@redhat.com \
    --cc=pbonzini@redhat.com \
    --cc=qemu-devel@nongnu.org \
    /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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).