{"id":833271,"url":"http://patchwork.ozlabs.org/api/1.2/patches/833271/?format=json","web_url":"http://patchwork.ozlabs.org/project/qemu-devel/patch/1509617407-21191-3-git-send-email-thuth@redhat.com/","project":{"id":14,"url":"http://patchwork.ozlabs.org/api/1.2/projects/14/?format=json","name":"QEMU Development","link_name":"qemu-devel","list_id":"qemu-devel.nongnu.org","list_email":"qemu-devel@nongnu.org","web_url":"","scm_url":"","webscm_url":"","list_archive_url":"","list_archive_url_format":"","commit_url_format":""},"msgid":"<1509617407-21191-3-git-send-email-thuth@redhat.com>","list_archive_url":null,"date":"2017-11-02T10:10:06","name":"[v3,2/2] qdev: Check for the availability of a hotplug controller before adding a device","commit_ref":null,"pull_url":null,"state":"new","archived":false,"hash":"205db30361e8db458621f790f2ef10e9eba12a2a","submitter":{"id":66152,"url":"http://patchwork.ozlabs.org/api/1.2/people/66152/?format=json","name":"Thomas Huth","email":"thuth@redhat.com"},"delegate":null,"mbox":"http://patchwork.ozlabs.org/project/qemu-devel/patch/1509617407-21191-3-git-send-email-thuth@redhat.com/mbox/","series":[{"id":11466,"url":"http://patchwork.ozlabs.org/api/1.2/series/11466/?format=json","web_url":"http://patchwork.ozlabs.org/project/qemu-devel/list/?series=11466","date":"2017-11-02T10:10:06","name":null,"version":3,"mbox":"http://patchwork.ozlabs.org/series/11466/mbox/"}],"comments":"http://patchwork.ozlabs.org/api/patches/833271/comments/","check":"pending","checks":"http://patchwork.ozlabs.org/api/patches/833271/checks/","tags":{},"related":[],"headers":{"Return-Path":"<qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org>","X-Original-To":"incoming@patchwork.ozlabs.org","Delivered-To":"patchwork-incoming@bilbo.ozlabs.org","Authentication-Results":["ozlabs.org;\n\tspf=pass (mailfrom) smtp.mailfrom=nongnu.org\n\t(client-ip=2001:4830:134:3::11; helo=lists.gnu.org;\n\tenvelope-from=qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org;\n\treceiver=<UNKNOWN>)","ext-mx10.extmail.prod.ext.phx2.redhat.com;\n\tdmarc=none (p=none dis=none) header.from=redhat.com","ext-mx10.extmail.prod.ext.phx2.redhat.com;\n\tspf=fail smtp.mailfrom=thuth@redhat.com"],"Received":["from lists.gnu.org (lists.gnu.org [IPv6:2001:4830:134:3::11])\n\t(using TLSv1 with cipher AES256-SHA (256/256 bits))\n\t(No client certificate requested)\n\tby ozlabs.org (Postfix) with ESMTPS id 3ySLT03LNKz9t30\n\tfor <incoming@patchwork.ozlabs.org>;\n\tThu,  2 Nov 2017 21:10:57 +1100 (AEDT)","from localhost ([::1]:59480 helo=lists.gnu.org)\n\tby lists.gnu.org with esmtp (Exim 4.71) (envelope-from\n\t<qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org>)\n\tid 1eACSd-0000R0-TJ\n\tfor incoming@patchwork.ozlabs.org; Thu, 02 Nov 2017 06:10:55 -0400","from eggs.gnu.org ([2001:4830:134:3::10]:41550)\n\tby lists.gnu.org with esmtp (Exim 4.71)\n\t(envelope-from <thuth@redhat.com>) id 1eACS4-0000Pa-Gd\n\tfor qemu-devel@nongnu.org; Thu, 02 Nov 2017 06:10:21 -0400","from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71)\n\t(envelope-from <thuth@redhat.com>) id 1eACS2-0002hO-NY\n\tfor qemu-devel@nongnu.org; Thu, 02 Nov 2017 06:10:20 -0400","from mx1.redhat.com ([209.132.183.28]:36758)\n\tby eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32)\n\t(Exim 4.71) (envelope-from <thuth@redhat.com>) id 1eACS2-0002gI-2C\n\tfor qemu-devel@nongnu.org; Thu, 02 Nov 2017 06:10:18 -0400","from smtp.corp.redhat.com\n\t(int-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.11])\n\t(using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits))\n\t(No client certificate requested)\n\tby mx1.redhat.com (Postfix) with ESMTPS id 389105D686\n\tfor <qemu-devel@nongnu.org>; Thu,  2 Nov 2017 10:10:17 +0000 (UTC)","from thh440s.redhat.com (ovpn-116-111.ams2.redhat.com\n\t[10.36.116.111])\n\tby smtp.corp.redhat.com (Postfix) with ESMTP id 3A14F600C0;\n\tThu,  2 Nov 2017 10:10:15 +0000 (UTC)"],"DMARC-Filter":"OpenDMARC Filter v1.3.2 mx1.redhat.com 389105D686","From":"Thomas Huth <thuth@redhat.com>","To":"qemu-devel@nongnu.org, Eduardo Habkost <ehabkost@redhat.com>,\n\tIgor Mammedov <imammedo@redhat.com>,\n\t\"Dr. David Alan Gilbert\" <dgilbert@redhat.com>","Date":"Thu,  2 Nov 2017 11:10:06 +0100","Message-Id":"<1509617407-21191-3-git-send-email-thuth@redhat.com>","In-Reply-To":"<1509617407-21191-1-git-send-email-thuth@redhat.com>","References":"<1509617407-21191-1-git-send-email-thuth@redhat.com>","X-Scanned-By":"MIMEDefang 2.79 on 10.5.11.11","X-Greylist":"Sender IP whitelisted, not delayed by milter-greylist-4.5.16\n\t(mx1.redhat.com [10.5.110.39]);\n\tThu, 02 Nov 2017 10:10:17 +0000 (UTC)","X-detected-operating-system":"by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic]\n\t[fuzzy]","X-Received-From":"209.132.183.28","Subject":"[Qemu-devel] [PATCH v3 2/2] qdev: Check for the availability of a\n\thotplug controller before adding a device","X-BeenThere":"qemu-devel@nongnu.org","X-Mailman-Version":"2.1.21","Precedence":"list","List-Id":"<qemu-devel.nongnu.org>","List-Unsubscribe":"<https://lists.nongnu.org/mailman/options/qemu-devel>,\n\t<mailto:qemu-devel-request@nongnu.org?subject=unsubscribe>","List-Archive":"<http://lists.nongnu.org/archive/html/qemu-devel/>","List-Post":"<mailto:qemu-devel@nongnu.org>","List-Help":"<mailto:qemu-devel-request@nongnu.org?subject=help>","List-Subscribe":"<https://lists.nongnu.org/mailman/listinfo/qemu-devel>,\n\t<mailto:qemu-devel-request@nongnu.org?subject=subscribe>","Cc":"Paolo Bonzini <pbonzini@redhat.com>,\n\tMarkus Armbruster <armbru@redhat.com>, david@redhat.com","Errors-To":"qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org","Sender":"\"Qemu-devel\"\n\t<qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org>"},"content":"The qdev_unplug() function contains a g_assert(hotplug_ctrl) statement,\nso QEMU crashes when the user tries to device_add + device_del a device\nthat does not have a corresponding hotplug controller. This could be\nprovoked for a couple of devices in the past (see commit 4c93950659487c7ad\nor 84ebd3e8c7d4fe955 for example), and can currently for example also be\ntriggered like this:\n\n$ s390x-softmmu/qemu-system-s390x -M none -nographic \nQEMU 2.10.50 monitor - type 'help' for more information\n(qemu) device_add qemu-s390x-cpu,id=x\n(qemu) device_del x\n**\nERROR:qemu/qdev-monitor.c:872:qdev_unplug: assertion failed: (hotplug_ctrl)\nAborted (core dumped)\n\nSo devices clearly need a hotplug controller when they should be usable\nwith device_add.\nThe code in qdev_device_add() already checks whether the bus has a proper\nhotplug controller, but for devices that do not have a corresponding bus,\nthere is no appropriate check available yet. In that case we should check\nwhether the machine itself provides a suitable hotplug controller and\nrefuse to plug the device if none is available.\n\nReviewed-by: Igor Mammedov <imammedo@redhat.com>\nSigned-off-by: Thomas Huth <thuth@redhat.com>\n---\n hw/core/qdev.c         | 28 ++++++++++++++++++++--------\n include/hw/qdev-core.h |  1 +\n qdev-monitor.c         |  5 +++++\n 3 files changed, 26 insertions(+), 8 deletions(-)","diff":"diff --git a/hw/core/qdev.c b/hw/core/qdev.c\nindex 1111295..f739753 100644\n--- a/hw/core/qdev.c\n+++ b/hw/core/qdev.c\n@@ -253,19 +253,31 @@ void qdev_set_legacy_instance_id(DeviceState *dev, int alias_id,\n     dev->alias_required_for_version = required_for_version;\n }\n \n+HotplugHandler *qdev_get_machine_hotplug_handler(DeviceState *dev)\n+{\n+    MachineState *machine;\n+    MachineClass *mc;\n+    Object *m_obj = qdev_get_machine();\n+\n+    if (object_dynamic_cast(m_obj, TYPE_MACHINE)) {\n+        machine = MACHINE(m_obj);\n+        mc = MACHINE_GET_CLASS(machine);\n+        if (mc->get_hotplug_handler) {\n+            return mc->get_hotplug_handler(machine, dev);\n+        }\n+    }\n+\n+    return NULL;\n+}\n+\n HotplugHandler *qdev_get_hotplug_handler(DeviceState *dev)\n {\n-    HotplugHandler *hotplug_ctrl = NULL;\n+    HotplugHandler *hotplug_ctrl;\n \n     if (dev->parent_bus && dev->parent_bus->hotplug_handler) {\n         hotplug_ctrl = dev->parent_bus->hotplug_handler;\n-    } else if (object_dynamic_cast(qdev_get_machine(), TYPE_MACHINE)) {\n-        MachineState *machine = MACHINE(qdev_get_machine());\n-        MachineClass *mc = MACHINE_GET_CLASS(machine);\n-\n-        if (mc->get_hotplug_handler) {\n-            hotplug_ctrl = mc->get_hotplug_handler(machine, dev);\n-        }\n+    } else {\n+        hotplug_ctrl = qdev_get_machine_hotplug_handler(dev);\n     }\n     return hotplug_ctrl;\n }\ndiff --git a/include/hw/qdev-core.h b/include/hw/qdev-core.h\nindex 0a71bf8..51473ee 100644\n--- a/include/hw/qdev-core.h\n+++ b/include/hw/qdev-core.h\n@@ -286,6 +286,7 @@ DeviceState *qdev_try_create(BusState *bus, const char *name);\n void qdev_init_nofail(DeviceState *dev);\n void qdev_set_legacy_instance_id(DeviceState *dev, int alias_id,\n                                  int required_for_version);\n+HotplugHandler *qdev_get_machine_hotplug_handler(DeviceState *dev);\n HotplugHandler *qdev_get_hotplug_handler(DeviceState *dev);\n void qdev_unplug(DeviceState *dev, Error **errp);\n void qdev_simple_device_unplug_cb(HotplugHandler *hotplug_dev,\ndiff --git a/qdev-monitor.c b/qdev-monitor.c\nindex 9188d20..38c0fc2 100644\n--- a/qdev-monitor.c\n+++ b/qdev-monitor.c\n@@ -614,6 +614,11 @@ DeviceState *qdev_device_add(QemuOpts *opts, Error **errp)\n \n     if (bus) {\n         qdev_set_parent_bus(dev, bus);\n+    } else if (qdev_hotplug && !qdev_get_machine_hotplug_handler(dev)) {\n+        /* No bus, no machine hotplug handler --> device is not hotpluggable */\n+        error_setg(&err, \"Device '%s' can not be hotplugged on this machine\",\n+                   driver);\n+        goto err_del_dev;\n     }\n \n     qdev_set_id(dev, qemu_opts_id(opts));\n","prefixes":["v3","2/2"]}