Received: by 2002:adf:9061:0:0:0:0:0 with SMTP id h88csp3842152wrh; Tue, 7 May 2019 09:44:53 -0700 (PDT) X-Google-Smtp-Source: APXvYqw2RBJ/Jjqqh5hM7UPTDj4gVJnBnf9WP4pc6TsNoNe2e+BTSnbwGFpi5+o+umiKIcbWHmo1 X-Received: by 2002:a05:600c:1191:: with SMTP id i17mr21627216wmf.84.1557247493609; Tue, 07 May 2019 09:44:53 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1557247493; cv=none; d=google.com; s=arc-20160816; b=N5KlooWA9at2GwT5oWPJcst5C2tlX75PlCy17dLqqtnnAEsxOL5RNhC2HzyD/Ongqe 8HjtZPXrvlP3lya4O7JmNKoCq7I7Mlok/Z+pjiGP1dVXnMEgy9r8eOPnZvqsgEpFnBsr SdGNWafpNG9XrWi5+hVON2t7MvjgBz5JQqduWpsAz+w0Y5fpGCVHJ70VNlmnxHFt1FXD KU0tdfB6cA0QQ9A68eeoRgm70iQdCHxj+BoFsiiGVKCkV8oPs6nKYTmG96FOT9Ppoy48 DTDy+5IaCo2QOKc4PVk5XMqZau7vhGxeNbtq8v8SROv/HPanFvlG9hqhu0X4+q6IiZL5 JETA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:cc:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:subject :content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:to:from; bh=HYWRjrM5DiaTI1dqh7yaWNSuIP466UflLGXq6NMABkg=; b=wCpcE2AH4p4QvcRRpdqVIOKul69Yyn6253s2btiaJJqZrIzAAmooIFHWE8NUZyecPE aIk7aRqW3kyQZ1KG66FfYCUMJ749I3dJegoz7Hr0X+2if8sz/hNhNFoUN6WngMnnvZP0 uwCqlCzke7oTnEQ5MlShxhL2J42c0cSg3slYFlyEu7Z1Z9VUF4lfEJ5VbJgyVlGu/lpt yOosRmZuOipm5MgI2fezMLfR2nrPFG/7X/JJQKhrwQLobQ2Z9/SSzAue50B0k4abGDef 3P85+GqGueOk0YlDZZAmqCeaGEP9HkW1bl0PnlElSH+d2Yz+VulqT0tD65g0DFS/WAlO OwIg== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of qemu-arm-bounces+alex.bennee=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom="qemu-arm-bounces+alex.bennee=linaro.org@nongnu.org"; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from lists.gnu.org (lists.gnu.org. [209.51.188.17]) by mx.google.com with ESMTPS id h14si9907155wrv.319.2019.05.07.09.44.53 for (version=TLS1 cipher=AES128-SHA bits=128/128); Tue, 07 May 2019 09:44:53 -0700 (PDT) Received-SPF: pass (google.com: domain of qemu-arm-bounces+alex.bennee=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; Authentication-Results: mx.google.com; spf=pass (google.com: domain of qemu-arm-bounces+alex.bennee=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom="qemu-arm-bounces+alex.bennee=linaro.org@nongnu.org"; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: from localhost ([127.0.0.1]:49919 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1hO3D2-00026T-FU for alex.bennee@linaro.org; Tue, 07 May 2019 12:44:52 -0400 Received: from eggs.gnu.org ([209.51.188.92]:51236) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1hO35C-0002XK-Lf for qemu-arm@nongnu.org; Tue, 07 May 2019 12:36:48 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1hO35A-0003Nn-Oo for qemu-arm@nongnu.org; Tue, 07 May 2019 12:36:46 -0400 Received: from mx1.redhat.com ([209.132.183.28]:47420) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1hO358-0003K4-RB; Tue, 07 May 2019 12:36:44 -0400 Received: from smtp.corp.redhat.com (int-mx08.intmail.prod.int.phx2.redhat.com [10.5.11.23]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id B6B06D964B; Tue, 7 May 2019 16:36:40 +0000 (UTC) Received: from x1w.redhat.com (ovpn-204-58.brq.redhat.com [10.40.204.58]) by smtp.corp.redhat.com (Postfix) with ESMTPS id B88EA816B; Tue, 7 May 2019 16:36:01 +0000 (UTC) From: =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= To: Markus Armbruster , Thomas Huth , qemu-devel@nongnu.org, Eduardo Habkost Date: Tue, 7 May 2019 18:34:07 +0200 Message-Id: <20190507163416.24647-8-philmd@redhat.com> In-Reply-To: <20190507163416.24647-1-philmd@redhat.com> References: <20190507163416.24647-1-philmd@redhat.com> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 X-Scanned-By: MIMEDefang 2.84 on 10.5.11.23 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.38]); Tue, 07 May 2019 16:36:41 +0000 (UTC) Content-Transfer-Encoding: quoted-printable 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-arm] [PATCH v2 07/16] hw/arm/aspeed: Use object_initialize_child for correct ref. counting X-BeenThere: qemu-arm@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Peter Maydell , "Michael S. Tsirkin" , Antony Pavlov , Paul Burton , Andrew Jeffery , Alistair Francis , Mark Cave-Ayland , =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= , Andrew Baumann , Joel Stanley , Aleksandar Rikalo , qemu-arm@nongnu.org, Peter Chubb , =?UTF-8?q?C=C3=A9dric=20Le=20Goater?= , Aleksandar Markovic , qemu-ppc@nongnu.org, Jean-Christophe Dubois , =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= , Aurelien Jarno , David Gibson Errors-To: qemu-arm-bounces+alex.bennee=linaro.org@nongnu.org Sender: "Qemu-arm" X-TUID: ASUke4X+Lgm0 As explained in commit aff39be0ed97: Both functions, object_initialize() and object_property_add_child() increase the reference counter of the new object, so one of the references has to be dropped afterwards to get the reference counting right. Otherwise the child object will not be properly cleaned up when the parent gets destroyed. Thus let's use now object_initialize_child() instead to get the reference counting here right. This patch was generated using the following Coccinelle script (with a bit of manual fix-up for overly long lines): @use_object_initialize_child@ expression parent_obj; expression child_ptr; expression child_name; expression child_type; expression child_size; expression errp; @@ ( - object_initialize(child_ptr, child_size, child_type); + object_initialize_child(parent_obj, child_name, child_ptr, child_si= ze, + child_type, &error_abort, NULL); ... when !=3D parent_obj - object_property_add_child(parent_obj, child_name, OBJECT(child_ptr),= NULL); ... ?- object_unref(OBJECT(child_ptr)); | - object_initialize(child_ptr, child_size, child_type); + object_initialize_child(parent_obj, child_name, child_ptr, child_si= ze, + child_type, errp, NULL); ... when !=3D parent_obj - object_property_add_child(parent_obj, child_name, OBJECT(child_ptr),= errp); ... ?- object_unref(OBJECT(child_ptr)); ) @use_sysbus_init_child_obj@ expression parent_obj; expression dev; expression child_ptr; expression child_name; expression child_type; expression child_size; expression errp; @@ ( - object_initialize_child(parent_obj, child_name, child_ptr, child_siz= e, - child_type, errp, NULL); + sysbus_init_child_obj(parent_obj, child_name, child_ptr, child_size, + child_type); ... - qdev_set_parent_bus(DEVICE(child_ptr), sysbus_get_default()); | - object_initialize_child(parent_obj, child_name, child_ptr, child_siz= e, - child_type, errp, NULL); + sysbus_init_child_obj(parent_obj, child_name, child_ptr, child_size, + child_type); - dev =3D DEVICE(child_ptr); - qdev_set_parent_bus(dev, sysbus_get_default()); ) While the object_initialize() function doesn't take an 'Error *errp' argument, the object_initialize_child() does. Since this code is used when a machine is created (and is not yet running), we deliberately choose to use the &error_abort argument instead of ignoring errors if an object creation failed. This choice also matches when using sysbus_init_child_obj(), since its code is: void sysbus_init_child_obj(Object *parent, const char *childname, void *child, size_t childsize, const char *childtype) { object_initialize_child(parent, childname, child, childsize, childtype, &error_abort, NULL); qdev_set_parent_bus(DEVICE(child), sysbus_get_default()); } Suggested-by: Eduardo Habkost Inspired-by: Thomas Huth Signed-off-by: Philippe Mathieu-Daud=C3=A9 Signed-off-by: C=C3=A9dric Le Goater Reviewed-by: Joel Stanley --- v2: - Described new use of &error_abort (Markus) - Added C=C3=A9dric S-o-b (he sent the same 'hw/arm/aspeed_soc.c' patch) - Added Joel R-b of C=C3=A9dric patch --- hw/arm/aspeed.c | 6 +++--- hw/arm/aspeed_soc.c | 50 ++++++++++++++++++--------------------------- 2 files changed, 23 insertions(+), 33 deletions(-) diff --git a/hw/arm/aspeed.c b/hw/arm/aspeed.c index 1c23ebd9925..f700b7e4fe0 100644 --- a/hw/arm/aspeed.c +++ b/hw/arm/aspeed.c @@ -159,9 +159,9 @@ static void aspeed_board_init(MachineState *machine, ram_addr_t max_ram_size; =20 bmc =3D g_new0(AspeedBoardState, 1); - object_initialize(&bmc->soc, (sizeof(bmc->soc)), cfg->soc_name); - object_property_add_child(OBJECT(machine), "soc", OBJECT(&bmc->soc), - &error_abort); + object_initialize_child(OBJECT(machine), "soc", &bmc->soc, + (sizeof(bmc->soc)), cfg->soc_name, &error_ab= ort, + NULL); =20 sc =3D ASPEED_SOC_GET_CLASS(&bmc->soc); =20 diff --git a/hw/arm/aspeed_soc.c b/hw/arm/aspeed_soc.c index a27233d4876..faff42b84ad 100644 --- a/hw/arm/aspeed_soc.c +++ b/hw/arm/aspeed_soc.c @@ -106,12 +106,11 @@ static void aspeed_soc_init(Object *obj) AspeedSoCClass *sc =3D ASPEED_SOC_GET_CLASS(s); int i; =20 - object_initialize(&s->cpu, sizeof(s->cpu), sc->info->cpu_type); - object_property_add_child(obj, "cpu", OBJECT(&s->cpu), NULL); + object_initialize_child(obj, "cpu", OBJECT(&s->cpu), sizeof(s->cpu), + sc->info->cpu_type, &error_abort, NULL); =20 - object_initialize(&s->scu, sizeof(s->scu), TYPE_ASPEED_SCU); - object_property_add_child(obj, "scu", OBJECT(&s->scu), NULL); - qdev_set_parent_bus(DEVICE(&s->scu), sysbus_get_default()); + sysbus_init_child_obj(obj, "scu", OBJECT(&s->scu), sizeof(s->scu), + TYPE_ASPEED_SCU); qdev_prop_set_uint32(DEVICE(&s->scu), "silicon-rev", sc->info->silicon_rev); object_property_add_alias(obj, "hw-strap1", OBJECT(&s->scu), @@ -121,36 +120,29 @@ static void aspeed_soc_init(Object *obj) object_property_add_alias(obj, "hw-prot-key", OBJECT(&s->scu), "hw-prot-key", &error_abort); =20 - object_initialize(&s->vic, sizeof(s->vic), TYPE_ASPEED_VIC); - object_property_add_child(obj, "vic", OBJECT(&s->vic), NULL); - qdev_set_parent_bus(DEVICE(&s->vic), sysbus_get_default()); + sysbus_init_child_obj(obj, "vic", OBJECT(&s->vic), sizeof(s->vic), + TYPE_ASPEED_VIC); =20 - object_initialize(&s->timerctrl, sizeof(s->timerctrl), TYPE_ASPEED_T= IMER); - object_property_add_child(obj, "timerctrl", OBJECT(&s->timerctrl), N= ULL); + sysbus_init_child_obj(obj, "timerctrl", OBJECT(&s->timerctrl), + sizeof(s->timerctrl), TYPE_ASPEED_TIMER); object_property_add_const_link(OBJECT(&s->timerctrl), "scu", OBJECT(&s->scu), &error_abort); - qdev_set_parent_bus(DEVICE(&s->timerctrl), sysbus_get_default()); =20 - object_initialize(&s->i2c, sizeof(s->i2c), TYPE_ASPEED_I2C); - object_property_add_child(obj, "i2c", OBJECT(&s->i2c), NULL); - qdev_set_parent_bus(DEVICE(&s->i2c), sysbus_get_default()); + sysbus_init_child_obj(obj, "i2c", OBJECT(&s->i2c), sizeof(s->i2c), + TYPE_ASPEED_I2C); =20 - object_initialize(&s->fmc, sizeof(s->fmc), sc->info->fmc_typename); - object_property_add_child(obj, "fmc", OBJECT(&s->fmc), NULL); - qdev_set_parent_bus(DEVICE(&s->fmc), sysbus_get_default()); + sysbus_init_child_obj(obj, "fmc", OBJECT(&s->fmc), sizeof(s->fmc), + sc->info->fmc_typename); object_property_add_alias(obj, "num-cs", OBJECT(&s->fmc), "num-cs", &error_abort); =20 for (i =3D 0; i < sc->info->spis_num; i++) { - object_initialize(&s->spi[i], sizeof(s->spi[i]), - sc->info->spi_typename[i]); - object_property_add_child(obj, "spi[*]", OBJECT(&s->spi[i]), NUL= L); - qdev_set_parent_bus(DEVICE(&s->spi[i]), sysbus_get_default()); + sysbus_init_child_obj(obj, "spi[*]", OBJECT(&s->spi[i]), + sizeof(s->spi[i]), sc->info->spi_typename[= i]); } =20 - object_initialize(&s->sdmc, sizeof(s->sdmc), TYPE_ASPEED_SDMC); - object_property_add_child(obj, "sdmc", OBJECT(&s->sdmc), NULL); - qdev_set_parent_bus(DEVICE(&s->sdmc), sysbus_get_default()); + sysbus_init_child_obj(obj, "sdmc", OBJECT(&s->sdmc), sizeof(s->sdmc)= , + TYPE_ASPEED_SDMC); qdev_prop_set_uint32(DEVICE(&s->sdmc), "silicon-rev", sc->info->silicon_rev); object_property_add_alias(obj, "ram-size", OBJECT(&s->sdmc), @@ -159,16 +151,14 @@ static void aspeed_soc_init(Object *obj) "max-ram-size", &error_abort); =20 for (i =3D 0; i < sc->info->wdts_num; i++) { - object_initialize(&s->wdt[i], sizeof(s->wdt[i]), TYPE_ASPEED_WDT= ); - object_property_add_child(obj, "wdt[*]", OBJECT(&s->wdt[i]), NUL= L); - qdev_set_parent_bus(DEVICE(&s->wdt[i]), sysbus_get_default()); + sysbus_init_child_obj(obj, "wdt[*]", OBJECT(&s->wdt[i]), + sizeof(s->wdt[i]), TYPE_ASPEED_WDT); qdev_prop_set_uint32(DEVICE(&s->wdt[i]), "silicon-rev", sc->info->silicon_rev); } =20 - object_initialize(&s->ftgmac100, sizeof(s->ftgmac100), TYPE_FTGMAC10= 0); - object_property_add_child(obj, "ftgmac100", OBJECT(&s->ftgmac100), N= ULL); - qdev_set_parent_bus(DEVICE(&s->ftgmac100), sysbus_get_default()); + sysbus_init_child_obj(obj, "ftgmac100", OBJECT(&s->ftgmac100), + sizeof(s->ftgmac100), TYPE_FTGMAC100); } =20 static void aspeed_soc_realize(DeviceState *dev, Error **errp) --=20 2.20.1