From nobody Sat Oct 4 17:29:45 2025 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1601988321; cv=none; d=zohomail.com; s=zohoarc; b=lbnzpX+KwytEX+k7M0k5eq64N59y5Ivw7U6eB9NgzWZlCusZqbFt2EL8rYp0DZdqmThgBqderYhSvx3EFRAOaJOYOPkybWwlTnjbvL3fg0VDnuKb5aYCm7cu/7CEu7XkuLoX8l8WS3huRg8P7tKe/FjiRDgMZptba24YMfqTj7o= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1601988321; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=LUacnfjmsfZENYi2/nUXJJfEyX9gS6a5ILJs/13HHIU=; b=e1VJ3kB1rrSP6iBcSw+NoTgl2A4LnvqdNhOLB3gaxT4zarvIJBQqSubzh9YtW6affIg6WUtFfofVEZRPBjsHS8uK2vFVJIF24VTTfON5zaE6IA8FTGptiLTJnyBjebuF5vC3vyzvseJNY1kutMwv+vbSgvd1Kv6FJCLfzae71VM= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1601988321395733.494448698149; Tue, 6 Oct 2020 05:45:21 -0700 (PDT) Received: from localhost ([::1]:56870 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1kPmLH-0006g1-58 for importer@patchew.org; Tue, 06 Oct 2020 08:45:19 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:56050) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1kPmFq-0001Eh-3I for qemu-devel@nongnu.org; Tue, 06 Oct 2020 08:39:43 -0400 Received: from us-smtp-delivery-124.mimecast.com ([216.205.24.124]:31553) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.90_1) (envelope-from ) id 1kPmFn-0007ux-PQ for qemu-devel@nongnu.org; Tue, 06 Oct 2020 08:39:41 -0400 Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-253-8EBCAX8LN9aEE9lldLY8EQ-1; Tue, 06 Oct 2020 08:39:34 -0400 Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.phx2.redhat.com [10.5.11.15]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 910A01015ECB; Tue, 6 Oct 2020 12:39:33 +0000 (UTC) Received: from localhost.localdomain (unknown [10.35.206.84]) by smtp.corp.redhat.com (Postfix) with ESMTP id D02442C31E; Tue, 6 Oct 2020 12:39:26 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1601987979; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=LUacnfjmsfZENYi2/nUXJJfEyX9gS6a5ILJs/13HHIU=; b=HTcP4tWAvnlt4cQ88+KNEhfXa8VsGM8MsBBT9R9y0oowErC6DySwinExHJH1BV7al7HQS6 9Olmu5tpRZT6+72cNGnTZv+Vd4oPLr+62m+KN2zKcX8woAPl0+c5vy77ze7kWa+0X9Ddgm fJepdbimq5SVxdxMHGaYRB9XVQdFn9E= X-MC-Unique: 8EBCAX8LN9aEE9lldLY8EQ-1 From: Maxim Levitsky To: qemu-devel@nongnu.org Subject: [PATCH v7 04/13] qdev: add "check if address free" callback for buses Date: Tue, 6 Oct 2020 15:38:55 +0300 Message-Id: <20201006123904.610658-5-mlevitsk@redhat.com> In-Reply-To: <20201006123904.610658-1-mlevitsk@redhat.com> References: <20201006123904.610658-1-mlevitsk@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.15 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=mlevitsk@redhat.com X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=216.205.24.124; envelope-from=mlevitsk@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-detected-operating-system: by eggs.gnu.org: First seen = 2020/10/06 00:55:20 X-ACL-Warn: Detected OS = Linux 2.2.x-3.x [generic] [fuzzy] X-Spam_score_int: -27 X-Spam_score: -2.8 X-Spam_bar: -- X-Spam_report: (-2.8 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.733, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H4=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=unavailable autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Fam Zheng , Laurent Vivier , Thomas Huth , =?UTF-8?q?Daniel=20P=2E=20Berrang=C3=A9?= , Eduardo Habkost , qemu-block@nongnu.org, "Michael S. Tsirkin" , Jason Wang , Markus Armbruster , =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= , Paolo Bonzini , Maxim Levitsky , John Snow , Stefan Berger Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Type: text/plain; charset="utf-8" From: Paolo Bonzini Check if an address is free on the bus before plugging in the device. This makes it possible to do the check without any side effects, and to detect the problem early without having to do it in the realize callback. Signed-off-by: Paolo Bonzini --- hw/core/qdev.c | 17 +++++++++++++++-- hw/net/virtio-net.c | 2 +- hw/sd/core.c | 3 ++- include/hw/qdev-core.h | 13 ++++++++++++- 4 files changed, 30 insertions(+), 5 deletions(-) diff --git a/hw/core/qdev.c b/hw/core/qdev.c index 96772a15bd..74db78df36 100644 --- a/hw/core/qdev.c +++ b/hw/core/qdev.c @@ -94,13 +94,23 @@ static void bus_add_child(BusState *bus, DeviceState *c= hild) 0); } =20 -void qdev_set_parent_bus(DeviceState *dev, BusState *bus) +static bool bus_check_address(BusState *bus, DeviceState *child, Error **e= rrp) +{ + BusClass *bc =3D BUS_GET_CLASS(bus); + return !bc->check_address || bc->check_address(bus, child, errp); +} + +bool qdev_set_parent_bus(DeviceState *dev, BusState *bus, Error **errp) { BusState *old_parent_bus =3D dev->parent_bus; DeviceClass *dc =3D DEVICE_GET_CLASS(dev); =20 assert(dc->bus_type && object_dynamic_cast(OBJECT(bus), dc->bus_type)); =20 + if (!bus_check_address(bus, dev, errp)) { + return false; + } + if (old_parent_bus) { trace_qdev_update_parent_bus(dev, object_get_typename(OBJECT(dev)), old_parent_bus, object_get_typename(OBJECT(old_parent_bus)), @@ -126,6 +136,7 @@ void qdev_set_parent_bus(DeviceState *dev, BusState *bu= s) object_unref(OBJECT(old_parent_bus)); object_unref(OBJECT(dev)); } + return true; } =20 DeviceState *qdev_new(const char *name) @@ -371,7 +382,9 @@ bool qdev_realize(DeviceState *dev, BusState *bus, Erro= r **errp) assert(!dev->realized && !dev->parent_bus); =20 if (bus) { - qdev_set_parent_bus(dev, bus); + if (!qdev_set_parent_bus(dev, bus, errp)) { + return false; + } } else { assert(!DEVICE_GET_CLASS(dev)->bus_type); } diff --git a/hw/net/virtio-net.c b/hw/net/virtio-net.c index 7bf27b9db7..268cecc498 100644 --- a/hw/net/virtio-net.c +++ b/hw/net/virtio-net.c @@ -3142,7 +3142,7 @@ static bool failover_replug_primary(VirtIONet *n, Err= or **errp) error_setg(errp, "virtio_net: couldn't find primary bus"); return false; } - qdev_set_parent_bus(n->primary_dev, n->primary_bus); + qdev_set_parent_bus(n->primary_dev, n->primary_bus, &error_abort); n->primary_should_be_hidden =3D false; if (!qemu_opt_set_bool(n->primary_device_opts, "partially_hotplugged", true, errp)) { diff --git a/hw/sd/core.c b/hw/sd/core.c index 957d116f1a..08c93b5903 100644 --- a/hw/sd/core.c +++ b/hw/sd/core.c @@ -23,6 +23,7 @@ #include "hw/qdev-core.h" #include "hw/sd/sd.h" #include "qemu/module.h" +#include "qapi/error.h" #include "trace.h" =20 static inline const char *sdbus_name(SDBus *sdbus) @@ -240,7 +241,7 @@ void sdbus_reparent_card(SDBus *from, SDBus *to) readonly =3D sc->get_readonly(card); =20 sdbus_set_inserted(from, false); - qdev_set_parent_bus(DEVICE(card), &to->qbus); + qdev_set_parent_bus(DEVICE(card), &to->qbus, &error_abort); sdbus_set_inserted(to, true); sdbus_set_readonly(to, readonly); } diff --git a/include/hw/qdev-core.h b/include/hw/qdev-core.h index 72064f4dd4..14d476c587 100644 --- a/include/hw/qdev-core.h +++ b/include/hw/qdev-core.h @@ -210,13 +210,24 @@ struct BusClass { /* FIXME first arg should be BusState */ void (*print_dev)(Monitor *mon, DeviceState *dev, int indent); char *(*get_dev_path)(DeviceState *dev); + /* * This callback is used to create Open Firmware device path in accord= ance * with OF spec http://forthworks.com/standards/of1275.pdf. Individual= bus * bindings can be found at http://playground.sun.com/1275/bindings/. */ char *(*get_fw_dev_path)(DeviceState *dev); + void (*reset)(BusState *bus); + + /* + * Return whether the device can be added to @bus, + * based on the address that was set (via device properties) + * before realize. If not, on return @errp contains the + * human-readable error message. + */ + bool (*check_address)(BusState *bus, DeviceState *dev, Error **errp); + BusRealize realize; BusUnrealize unrealize; =20 @@ -788,7 +799,7 @@ const char *qdev_fw_name(DeviceState *dev); Object *qdev_get_machine(void); =20 /* FIXME: make this a link<> */ -void qdev_set_parent_bus(DeviceState *dev, BusState *bus); +bool qdev_set_parent_bus(DeviceState *dev, BusState *bus, Error **errp); =20 extern bool qdev_hotplug; extern bool qdev_hot_removed; --=20 2.26.2