From patchwork Tue Oct 20 09:17:06 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Greg Kurz X-Patchwork-Id: 532857 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [IPv6:2001:4830:134:3::11]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 388E61401E7 for ; Tue, 20 Oct 2015 20:19:46 +1100 (AEDT) Received: from localhost ([::1]:44698 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ZoT55-0003Pi-V1 for incoming@patchwork.ozlabs.org; Tue, 20 Oct 2015 05:19:43 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:49271) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ZoT2h-0002IO-6Q for qemu-devel@nongnu.org; Tue, 20 Oct 2015 05:17:18 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1ZoT2d-0005Rs-M5 for qemu-devel@nongnu.org; Tue, 20 Oct 2015 05:17:14 -0400 Received: from e06smtp13.uk.ibm.com ([195.75.94.109]:42715) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ZoT2d-0005Rl-Co for qemu-devel@nongnu.org; Tue, 20 Oct 2015 05:17:11 -0400 Received: from localhost by e06smtp13.uk.ibm.com with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted for from ; Tue, 20 Oct 2015 10:17:10 +0100 Received: from d06dlp03.portsmouth.uk.ibm.com (9.149.20.15) by e06smtp13.uk.ibm.com (192.168.101.143) with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted; Tue, 20 Oct 2015 10:17:08 +0100 X-IBM-Helo: d06dlp03.portsmouth.uk.ibm.com X-IBM-MailFrom: gkurz@linux.vnet.ibm.com X-IBM-RcptTo: qemu-devel@nongnu.org Received: from b06cxnps4076.portsmouth.uk.ibm.com (d06relay13.portsmouth.uk.ibm.com [9.149.109.198]) by d06dlp03.portsmouth.uk.ibm.com (Postfix) with ESMTP id DE2931B08067 for ; Tue, 20 Oct 2015 10:17:15 +0100 (BST) Received: from d06av01.portsmouth.uk.ibm.com (d06av01.portsmouth.uk.ibm.com [9.149.37.212]) by b06cxnps4076.portsmouth.uk.ibm.com (8.14.9/8.14.9/NCO v10.0) with ESMTP id t9K9H8JU23724158 for ; Tue, 20 Oct 2015 09:17:08 GMT Received: from d06av01.portsmouth.uk.ibm.com (localhost [127.0.0.1]) by d06av01.portsmouth.uk.ibm.com (8.14.4/8.14.4/NCO v10.0 AVout) with ESMTP id t9K9H8gQ021210 for ; Tue, 20 Oct 2015 03:17:08 -0600 Received: from smtp.lab.toulouse-stg.fr.ibm.com (srv01.lab.toulouse-stg.fr.ibm.com [9.101.4.1]) by d06av01.portsmouth.uk.ibm.com (8.14.4/8.14.4/NCO v10.0 AVin) with ESMTP id t9K9H75c021204; Tue, 20 Oct 2015 03:17:08 -0600 Received: from bahia.huguette.org (icon-9-164-138-165.megacenter.de.ibm.com [9.164.138.165]) by smtp.lab.toulouse-stg.fr.ibm.com (Postfix) with ESMTP id 2AD5F220083; Tue, 20 Oct 2015 11:17:07 +0200 (CEST) From: Greg Kurz To: qemu-devel@nongnu.org Date: Tue, 20 Oct 2015 11:17:06 +0200 Message-ID: <20151020091706.25419.50006.stgit@bahia.huguette.org> In-Reply-To: <20151020091640.25419.99985.stgit@bahia.huguette.org> References: <20151020091640.25419.99985.stgit@bahia.huguette.org> User-Agent: StGit/0.17.1-dirty MIME-Version: 1.0 X-TM-AS-MML: disable X-Content-Scanned: Fidelis XPS MAILER x-cbid: 15102009-0013-0000-0000-00000761655D X-detected-operating-system: by eggs.gnu.org: GNU/Linux 3.x X-Received-From: 195.75.94.109 Cc: Cornelia Huck , "Michael S. Tsirkin" , Alexander Graf , Andreas =?utf-8?q?F=C3=A4rber?= , aneesh.kumar@linux.vnet.ibm.com Subject: [Qemu-devel] [PATCH v3 4/5] virtio-9p: add unrealize handler 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 This patch allows to hot-unplug a quiescent virtio-9p device, and free its allocated resources. A refcount is added to the v9fs thread pool, so that its resources are freed as well when the last virtio-9p device is unplugged. Note that we have an unplug blocker which prevents this code to be reached if the 9p share is mounted in the guest. No need to bother about in-flight I/O requests in this case. This patch fixes a QEMU crash on the source node if the user device_add a virtio-9p device and then migrate. Signed-off-by: Greg Kurz --- hw/9pfs/virtio-9p-coth.c | 15 ++++++++++++++- hw/9pfs/virtio-9p-coth.h | 2 ++ hw/9pfs/virtio-9p-device.c | 12 ++++++++++++ 3 files changed, 28 insertions(+), 1 deletion(-) diff --git a/hw/9pfs/virtio-9p-coth.c b/hw/9pfs/virtio-9p-coth.c index 1d832ede1ebf..27ccc66c91d8 100644 --- a/hw/9pfs/virtio-9p-coth.c +++ b/hw/9pfs/virtio-9p-coth.c @@ -55,7 +55,7 @@ int v9fs_init_worker_threads(void) V9fsThPool *p = &v9fs_pool; sigset_t set, oldset; - if (p->pool) { + if (p->refcount++) { return 0; } @@ -81,3 +81,16 @@ err_out: pthread_sigmask(SIG_SETMASK, &oldset, NULL); return ret; } + +void v9fs_release_worker_threads(void) +{ + V9fsThPool *p = &v9fs_pool; + + if (--p->refcount) { + return; + } + + g_thread_pool_free(p->pool, TRUE, TRUE); + g_async_queue_unref(p->completed); + event_notifier_set_handler(&p->e, NULL); +} diff --git a/hw/9pfs/virtio-9p-coth.h b/hw/9pfs/virtio-9p-coth.h index 4f51b250d1d4..2a2617e670a5 100644 --- a/hw/9pfs/virtio-9p-coth.h +++ b/hw/9pfs/virtio-9p-coth.h @@ -25,6 +25,7 @@ typedef struct V9fsThPool { GThreadPool *pool; GAsyncQueue *completed; + unsigned refcount; } V9fsThPool; /* @@ -56,6 +57,7 @@ typedef struct V9fsThPool { extern void co_run_in_worker_bh(void *); extern int v9fs_init_worker_threads(void); +extern void v9fs_release_worker_threads(void); extern int v9fs_co_readlink(V9fsPDU *, V9fsPath *, V9fsString *); extern int v9fs_co_readdir_r(V9fsPDU *, V9fsFidState *, struct dirent *, struct dirent **result); diff --git a/hw/9pfs/virtio-9p-device.c b/hw/9pfs/virtio-9p-device.c index 93a407c45926..ed133c40493a 100644 --- a/hw/9pfs/virtio-9p-device.c +++ b/hw/9pfs/virtio-9p-device.c @@ -138,6 +138,17 @@ out: v9fs_path_free(&path); } +static void virtio_9p_device_unrealize(DeviceState *dev, Error **errp) +{ + VirtIODevice *vdev = VIRTIO_DEVICE(dev); + V9fsState *s = VIRTIO_9P(dev); + + v9fs_release_worker_threads(); + g_free(s->ctx.fs_root); + g_free(s->tag); + virtio_cleanup(vdev); +} + /* virtio-9p device */ static Property virtio_9p_properties[] = { @@ -154,6 +165,7 @@ static void virtio_9p_class_init(ObjectClass *klass, void *data) dc->props = virtio_9p_properties; set_bit(DEVICE_CATEGORY_STORAGE, dc->categories); vdc->realize = virtio_9p_device_realize; + vdc->unrealize = virtio_9p_device_unrealize; vdc->get_features = virtio_9p_get_features; vdc->get_config = virtio_9p_get_config; }