From patchwork Wed Aug 8 06:25:53 2012 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: pingfan liu X-Patchwork-Id: 175864 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (Client did not present a certificate) by ozlabs.org (Postfix) with ESMTPS id 4D90B2C00A6 for ; Wed, 8 Aug 2012 16:51:59 +1000 (EST) Received: from localhost ([::1]:47908 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Sz07Z-00005C-Gh for incoming@patchwork.ozlabs.org; Wed, 08 Aug 2012 02:51:57 -0400 Received: from eggs.gnu.org ([208.118.235.92]:44040) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Syzjb-0001eo-LH for qemu-devel@nongnu.org; Wed, 08 Aug 2012 02:27:13 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1Syzja-0002SG-ED for qemu-devel@nongnu.org; Wed, 08 Aug 2012 02:27:11 -0400 Received: from mail-ob0-f173.google.com ([209.85.214.173]:62109) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Syzja-0001sm-9s for qemu-devel@nongnu.org; Wed, 08 Aug 2012 02:27:10 -0400 Received: by mail-ob0-f173.google.com with SMTP id ta14so635077obb.4 for ; Tue, 07 Aug 2012 23:27:10 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=from:to:cc:subject:date:message-id:x-mailer:in-reply-to:references; bh=c4SKJMCIwT3W9xLFvGwg6OxtOA+Ly3f3sJ7GZPAP8QU=; b=dtzZkan/fi6tWayL1nvDpQNfFAsADQ0oI2pFxylNYxfj3yP8sXrm3OoAJHF0Wqf32x 7E3BCr98qFSRHxY6khS7FktuV3eioX45qZ0yJsoaAufhzMclmzeXgI6KtCBtnJCuD9Py gbR+wpqs5me4NCukkFYZxOayu8E2GbXgFbGfvu8UoBfK54VY6ZItVdIdSl723beTmYD2 8b8BecF0y66o4wVEIPrCjsJ8BKEoalss7JMae7ZV53JN6ZzCP6RAkXf6eRA9T8W/OkXF YeBokH9QDvZbvwKqTtVFWwKJZNQLEYBUgNKh5jSyLIfOdlyps4feFWE8kSZ4nFeb+LJl Jo6w== Received: by 10.182.167.101 with SMTP id zn5mr28726539obb.60.1344407230062; Tue, 07 Aug 2012 23:27:10 -0700 (PDT) Received: from localhost ([202.108.130.138]) by mx.google.com with ESMTPS id ig3sm21894579obb.0.2012.08.07.23.27.08 (version=TLSv1/SSLv3 cipher=OTHER); Tue, 07 Aug 2012 23:27:09 -0700 (PDT) From: Liu Ping Fan To: qemu-devel@nongnu.org Date: Wed, 8 Aug 2012 14:25:53 +0800 Message-Id: <1344407156-25562-13-git-send-email-qemulist@gmail.com> X-Mailer: git-send-email 1.7.4.4 In-Reply-To: <1344407156-25562-1-git-send-email-qemulist@gmail.com> References: <1344407156-25562-1-git-send-email-qemulist@gmail.com> X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 209.85.214.173 Cc: kvm@vger.kernel.org, Stefan Hajnoczi , Marcelo Tosatti , qemulist@gmail.com, Blue Swirl , Avi Kivity , Anthony Liguori , Jan Kiszka , Paolo Bonzini , =?UTF-8?q?Andreas=20F=C3=A4rber?= Subject: [Qemu-devel] [PATCH 12/15] qdev: using devtree lock to protect device's accessing X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org From: Liu Ping Fan lock: qemu_device_tree_mutex competitors: --device_del(destruction of device will be postphoned until unplug ack from guest), --pci hot-unplug --iteration (qdev_reset_all) --device_add Signed-off-by: Liu Ping Fan --- hw/pci-hotplug.c | 4 ++++ hw/qdev-monitor.c | 17 ++++++++++++++++- hw/qdev.c | 2 ++ 3 files changed, 22 insertions(+), 1 deletions(-) diff --git a/hw/pci-hotplug.c b/hw/pci-hotplug.c index e7fb780..33a9dfe 100644 --- a/hw/pci-hotplug.c +++ b/hw/pci-hotplug.c @@ -265,9 +265,11 @@ static int pci_device_hot_remove(Monitor *mon, const char *pci_addr) return -1; } + qemu_lock_devtree(); d = pci_find_device(pci_find_root_bus(dom), bus, PCI_DEVFN(slot, 0)); if (!d) { monitor_printf(mon, "slot %d empty\n", slot); + qemu_unlock_devtree(); return -1; } @@ -275,9 +277,11 @@ static int pci_device_hot_remove(Monitor *mon, const char *pci_addr) if (error_is_set(&local_err)) { monitor_printf(mon, "%s\n", error_get_pretty(local_err)); error_free(local_err); + qemu_unlock_devtree(); return -1; } + qemu_unlock_devtree(); return 0; } diff --git a/hw/qdev-monitor.c b/hw/qdev-monitor.c index 7915b45..2d47fe0 100644 --- a/hw/qdev-monitor.c +++ b/hw/qdev-monitor.c @@ -429,14 +429,18 @@ DeviceState *qdev_device_add(QemuOpts *opts) /* find bus */ path = qemu_opt_get(opts, "bus"); + + qemu_lock_devtree(); if (path != NULL) { bus = qbus_find(path); if (!bus) { + qemu_unlock_devtree(); return NULL; } if (strcmp(object_get_typename(OBJECT(bus)), k->bus_type) != 0) { qerror_report(QERR_BAD_BUS_FOR_DEVICE, driver, object_get_typename(OBJECT(bus))); + qemu_unlock_devtree(); return NULL; } } else { @@ -444,11 +448,13 @@ DeviceState *qdev_device_add(QemuOpts *opts) if (!bus) { qerror_report(QERR_NO_BUS_FOR_DEVICE, driver, k->bus_type); + qemu_unlock_devtree(); return NULL; } } if (qdev_hotplug && !bus->allow_hotplug) { qerror_report(QERR_BUS_NO_HOTPLUG, bus->name); + qemu_unlock_devtree(); return NULL; } @@ -466,6 +472,7 @@ DeviceState *qdev_device_add(QemuOpts *opts) } if (qemu_opt_foreach(opts, set_property, qdev, 1) != 0) { qdev_free(qdev); + qemu_unlock_devtree(); return NULL; } if (qdev->id) { @@ -478,6 +485,8 @@ DeviceState *qdev_device_add(QemuOpts *opts) OBJECT(qdev), NULL); g_free(name); } + qemu_unlock_devtree(); + if (qdev_init(qdev) < 0) { qerror_report(QERR_DEVICE_INIT_FAILED, driver); return NULL; @@ -600,13 +609,19 @@ void qmp_device_del(const char *id, Error **errp) { DeviceState *dev; + /* protect against unplug ack from guest, where we really remove device + * from system + */ + qemu_lock_devtree(); dev = qdev_find_recursive(sysbus_get_default(), id); if (NULL == dev) { error_set(errp, QERR_DEVICE_NOT_FOUND, id); + qemu_unlock_devtree(); return; } - + /* Just remove from system, and drop refcnt there*/ qdev_unplug(dev, errp); + qemu_unlock_devtree(); } void qdev_machine_init(void) diff --git a/hw/qdev.c b/hw/qdev.c index af54467..17525fe 100644 --- a/hw/qdev.c +++ b/hw/qdev.c @@ -230,7 +230,9 @@ static int qbus_reset_one(BusState *bus, void *opaque) void qdev_reset_all(DeviceState *dev) { + qemu_lock_devtree(); qdev_walk_children(dev, qdev_reset_one, qbus_reset_one, NULL); + qemu_unlock_devtree(); } void qbus_reset_all_fn(void *opaque)