From patchwork Thu Jul 21 15:54:35 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Igor Mammedov X-Patchwork-Id: 9241893 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 9DF2A602F0 for ; Thu, 21 Jul 2016 16:01:58 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 8EFE527D64 for ; Thu, 21 Jul 2016 16:01:58 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 8328F27EED; Thu, 21 Jul 2016 16:01:58 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-6.9 required=2.0 tests=BAYES_00,RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id 0DEF427D64 for ; Thu, 21 Jul 2016 16:01:58 +0000 (UTC) Received: from localhost ([::1]:41698 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1bQGQ9-00081Z-3D for patchwork-qemu-devel@patchwork.kernel.org; Thu, 21 Jul 2016 12:01:57 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:36003) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1bQGJM-0002Yc-GV for qemu-devel@nongnu.org; Thu, 21 Jul 2016 11:55:02 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1bQGJL-00083p-9M for qemu-devel@nongnu.org; Thu, 21 Jul 2016 11:54:56 -0400 Received: from mx1.redhat.com ([209.132.183.28]:52886) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1bQGJL-00083h-42; Thu, 21 Jul 2016 11:54:55 -0400 Received: from int-mx13.intmail.prod.int.phx2.redhat.com (int-mx13.intmail.prod.int.phx2.redhat.com [10.5.11.26]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 9A51612B28; Thu, 21 Jul 2016 15:54:54 +0000 (UTC) Received: from dell-r430-03.lab.eng.brq.redhat.com (dell-r430-03.lab.eng.brq.redhat.com [10.34.112.60]) by int-mx13.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id u6LFsf1n011051; Thu, 21 Jul 2016 11:54:52 -0400 From: Igor Mammedov To: qemu-devel@nongnu.org Date: Thu, 21 Jul 2016 17:54:35 +0200 Message-Id: <1469116479-233280-5-git-send-email-imammedo@redhat.com> In-Reply-To: <1469116479-233280-1-git-send-email-imammedo@redhat.com> References: <1469116479-233280-1-git-send-email-imammedo@redhat.com> X-Scanned-By: MIMEDefang 2.68 on 10.5.11.26 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.30]); Thu, 21 Jul 2016 15:54:54 +0000 (UTC) X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 209.132.183.28 Subject: [Qemu-devel] [PATCH 4/8] qdev: fix object reference leak in case device.realize() fails X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Riku Voipio , Eduardo Habkost , "Michael S. Tsirkin" , Peter Crosthwaite , Alexander Graf , qemu-ppc@nongnu.org, Bharata B Rao , Paolo Bonzini , David Gibson , Richard Henderson Errors-To: qemu-devel-bounces+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org Sender: "Qemu-devel" X-Virus-Scanned: ClamAV using ClamSMTP If device doesn't have parent assined before its realize is called, device_set_realized() will implicitly set parent to '/machine/unattached'. However device_set_realized() may fail after that point at several other points leaving not realized object dangling in '/machine/unattached' and as result caller of obj = object_new() obj->ref == 1 object_property_set_bool(obj,..., true, "realized",...) obj->ref == 2 if (fail) object_unref(obj); obj->ref == 1 will get object leak instead of expected object destruction. Fix it by making device_set_realized() to cleanup after itself in case of failure. Signed-off-by: Igor Mammedov Reviewed-by: David Gibson --- hw/core/qdev.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/hw/core/qdev.c b/hw/core/qdev.c index 6680089..ee4a083 100644 --- a/hw/core/qdev.c +++ b/hw/core/qdev.c @@ -885,6 +885,8 @@ static void device_set_realized(Object *obj, bool value, Error **errp) HotplugHandler *hotplug_ctrl; BusState *bus; Error *local_err = NULL; + bool unattached_parent = false; + static int unattached_count; if (dev->hotplugged && !dc->hotpluggable) { error_setg(errp, QERR_DEVICE_NO_HOTPLUG, object_get_typename(obj)); @@ -893,12 +895,12 @@ static void device_set_realized(Object *obj, bool value, Error **errp) if (value && !dev->realized) { if (!obj->parent) { - static int unattached_count; gchar *name = g_strdup_printf("device[%d]", unattached_count++); object_property_add_child(container_get(qdev_get_machine(), "/unattached"), name, obj, &error_abort); + unattached_parent = true; g_free(name); } @@ -987,6 +989,10 @@ post_realize_fail: fail: error_propagate(errp, local_err); + if (unattached_parent) { + object_unparent(OBJECT(dev)); + unattached_count--; + } } static bool device_get_hotpluggable(Object *obj, Error **errp)