From patchwork Mon Oct 12 09:03:11 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Greg Kurz X-Patchwork-Id: 528986 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 C77171402B2 for ; Mon, 12 Oct 2015 20:03:46 +1100 (AEDT) Received: from localhost ([::1]:54002 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ZlZ1F-0003OZ-06 for incoming@patchwork.ozlabs.org; Mon, 12 Oct 2015 05:03:45 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:54075) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ZlZ0r-0002um-RX for qemu-devel@nongnu.org; Mon, 12 Oct 2015 05:03:22 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1ZlZ0o-00085i-LZ for qemu-devel@nongnu.org; Mon, 12 Oct 2015 05:03:21 -0400 Received: from e06smtp15.uk.ibm.com ([195.75.94.111]:58396) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ZlZ0o-00085M-DH for qemu-devel@nongnu.org; Mon, 12 Oct 2015 05:03:18 -0400 Received: from localhost by e06smtp15.uk.ibm.com with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted for from ; Mon, 12 Oct 2015 10:03:17 +0100 Received: from d06dlp03.portsmouth.uk.ibm.com (9.149.20.15) by e06smtp15.uk.ibm.com (192.168.101.145) with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted; Mon, 12 Oct 2015 10:03:14 +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 b06cxnps3074.portsmouth.uk.ibm.com (d06relay09.portsmouth.uk.ibm.com [9.149.109.194]) by d06dlp03.portsmouth.uk.ibm.com (Postfix) with ESMTP id 6485B1B08061 for ; Mon, 12 Oct 2015 10:03:18 +0100 (BST) Received: from d06av07.portsmouth.uk.ibm.com (d06av07.portsmouth.uk.ibm.com [9.149.37.248]) by b06cxnps3074.portsmouth.uk.ibm.com (8.14.9/8.14.9/NCO v10.0) with ESMTP id t9C93Ekl34930918 for ; Mon, 12 Oct 2015 09:03:14 GMT Received: from d06av07.portsmouth.uk.ibm.com (localhost [127.0.0.1]) by d06av07.portsmouth.uk.ibm.com (8.14.4/8.14.4/NCO v10.0 AVout) with ESMTP id t9C93EeT023858 for ; Mon, 12 Oct 2015 03:03:14 -0600 Received: from smtp.lab.toulouse-stg.fr.ibm.com (srv01.lab.toulouse-stg.fr.ibm.com [9.101.4.1]) by d06av07.portsmouth.uk.ibm.com (8.14.4/8.14.4/NCO v10.0 AVin) with ESMTP id t9C93DpO023854; Mon, 12 Oct 2015 03:03:13 -0600 Received: from bahia.huguette.org (icon-9-164-169-147.megacenter.de.ibm.com [9.164.169.147]) by smtp.lab.toulouse-stg.fr.ibm.com (Postfix) with ESMTP id 5333F220192; Mon, 12 Oct 2015 11:03:12 +0200 (CEST) From: Greg Kurz To: qemu-devel@nongnu.org Date: Mon, 12 Oct 2015 11:03:11 +0200 Message-ID: <20151012090048.10877.87543.stgit@bahia.huguette.org> In-Reply-To: <20151012085708.10877.51489.stgit@bahia.huguette.org> References: <20151012085708.10877.51489.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: 15101209-0021-0000-0000-00000786608C X-detected-operating-system: by eggs.gnu.org: GNU/Linux 3.x X-Received-From: 195.75.94.111 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 v2 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 --- V2: - now introduces v9fs_release_worker_threads() (was in patch 1) - adds a refcount to the v9fs thread pool --- 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; }