From patchwork Tue May 16 07:59:25 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Roger_Pau_Monn=C3=A9?= X-Patchwork-Id: 9728511 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 96FBC602B4 for ; Tue, 16 May 2017 08:02:26 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 8672827F95 for ; Tue, 16 May 2017 08:02:26 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 7B253284ED; Tue, 16 May 2017 08:02:26 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-4.2 required=2.0 tests=BAYES_00, RCVD_IN_DNSWL_MED autolearn=ham version=3.3.1 Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) (using TLSv1.2 with cipher AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id 19EC227F95 for ; Tue, 16 May 2017 08:02:26 +0000 (UTC) Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.84_2) (envelope-from ) id 1dAXOc-0007oX-3R; Tue, 16 May 2017 07:59:54 +0000 Received: from mail6.bemta6.messagelabs.com ([193.109.254.103]) by lists.xenproject.org with esmtp (Exim 4.84_2) (envelope-from ) id 1dAXOa-0007nd-5o for xen-devel@lists.xenproject.org; Tue, 16 May 2017 07:59:52 +0000 Received: from [193.109.254.147] by server-8.bemta-6.messagelabs.com id BF/E6-03696-771BA195; Tue, 16 May 2017 07:59:51 +0000 X-Brightmail-Tracker: H4sIAAAAAAAAA+NgFprLIsWRWlGSWpSXmKPExsXitHSDvW7ZRql IgxWTDSy+b5nM5MDocfjDFZYAxijWzLyk/IoE1oxZxz6zFxxRrDjXvoS1gfGxRBcjJ4eEgL/E l+/vWEBsNgEdiYtzd7J1MXJwiAioSNzea9DFyMXBLDCTUaK1oY0VpEZYIFjiyKudjCA2i4Cqx IFdLewgNq+ApcTrRx+YIGbqSbyd+AKshlPASmLjz8tgcSGgmo3vDzJD1AtKnJz5BGwvs4CmRO v23+wQtrxE89bZzBD1ihL98x6wTWDkm4WkZRaSlllIWhYwMq9i1ChOLSpLLdI1NtZLKspMzyj JTczM0TU0MNPLTS0uTkxPzUlMKtZLzs/dxAgMNgYg2MG4c33gIUZJDiYlUd60aqlIIb6k/JTK jMTijPii0pzU4kOMMhwcShK839YD5QSLUtNTK9Iyc4BhD5OW4OBREuE9CJLmLS5IzC3OTIdIn WJUlBLn/Q+SEABJZJTmwbXBYu0So6yUMC8j0CFCPAWpRbmZJajyrxjFORiVhHnPg0zhycwrgZ v+CmgxE9DisJfiIItLEhFSUg2MFt4RMysnGc4p4218Zsj3Inf53eX+y+aIOuxmUV/IOHf7adO 66KeCuy3Ll+5v/Lv6d9v12hrzBXP1g+P8GtZ070jKfPPNPNp/60bZ7fNm1Ij+zLuxLfpYK4uZ 5O836uWcro8P/xPoK4t+8XbBhCVTGl5fmHDi4KlXD/qWlss/8maP2G8cmi5SpcRSnJFoqMVcV JwIADktAM6wAgAA X-Env-Sender: prvs=302023821=roger.pau@citrix.com X-Msg-Ref: server-16.tower-27.messagelabs.com!1494921588!102148991!2 X-Originating-IP: [66.165.176.63] X-SpamReason: No, hits=0.0 required=7.0 tests=sa_preprocessor: VHJ1c3RlZCBJUDogNjYuMTY1LjE3Ni42MyA9PiAzMDYwNDg=\n, received_headers: No Received headers X-StarScan-Received: X-StarScan-Version: 9.4.12; banners=-,-,- X-VirusChecked: Checked Received: (qmail 60286 invoked from network); 16 May 2017 07:59:50 -0000 Received: from smtp02.citrix.com (HELO SMTP02.CITRIX.COM) (66.165.176.63) by server-16.tower-27.messagelabs.com with RC4-SHA encrypted SMTP; 16 May 2017 07:59:50 -0000 X-IronPort-AV: E=Sophos;i="5.38,348,1491264000"; d="scan'208";a="432571115" From: Roger Pau Monne To: Date: Tue, 16 May 2017 08:59:25 +0100 Message-ID: <20170516075925.46047-4-roger.pau@citrix.com> X-Mailer: git-send-email 2.11.0 (Apple Git-81) In-Reply-To: <20170516075925.46047-1-roger.pau@citrix.com> References: <20170516075925.46047-1-roger.pau@citrix.com> MIME-Version: 1.0 Cc: Wei Liu , Julien Grall , Ian Jackson , Roger Pau Monne Subject: [Xen-devel] [PATCH v3 for-4.9 3/3] libxl/devd: move the device allocation/removal code X-BeenThere: xen-devel@lists.xen.org X-Mailman-Version: 2.1.18 Precedence: list List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Errors-To: xen-devel-bounces@lists.xen.org Sender: "Xen-devel" X-Virus-Scanned: ClamAV using ClamSMTP Move the device addition/removal code to the {add/remove}_device functions. Signed-off-by: Roger Pau Monné Acked-by: Wei Liu Acked-by: Ian Jackson --- Cc: Ian Jackson Cc: Wei Liu Cc: Julien Grall --- tools/libxl/libxl_device.c | 61 +++++++++++++++++++++++----------------------- 1 file changed, 30 insertions(+), 31 deletions(-) diff --git a/tools/libxl/libxl_device.c b/tools/libxl/libxl_device.c index c82ac3cace..00356afea3 100644 --- a/tools/libxl/libxl_device.c +++ b/tools/libxl/libxl_device.c @@ -1520,14 +1520,25 @@ static void check_and_maybe_remove_guest(libxl__gc *gc, */ static int add_device(libxl__egc *egc, libxl__ao *ao, libxl__ddomain_guest *dguest, - libxl__ddomain_device *ddev) + libxl__device *dev) { AO_GC; - libxl__device *dev = ddev->dev; libxl__ao_device *aodev; + libxl__ddomain_device *ddev; libxl__dm_spawn_state *dmss; int rc = 0; + /* + * New device addition, allocate a struct to hold it and add it + * to the list of active devices for a given guest. + */ + ddev = libxl__zalloc(NOGC, sizeof(*ddev)); + ddev->dev = libxl__zalloc(NOGC, sizeof(*ddev->dev)); + *ddev->dev = *dev; + LIBXL_SLIST_INSERT_HEAD(&dguest->devices, ddev, next); + LOGD(DEBUG, dev->domid, "Added device %s to the list of active devices", + libxl__device_backend_path(gc, dev)); + switch(dev->backend_kind) { case LIBXL__DEVICE_KIND_VBD: case LIBXL__DEVICE_KIND_VIF: @@ -1607,6 +1618,22 @@ static int remove_device(libxl__egc *egc, libxl__ao *ao, break; } + /* + * Removal of an active device, remove it from the list and + * free it's data structures if they are no longer needed. + * + * NB: the freeing is safe because all the async ops launched + * above or from add_device make a copy of the data they use, so + * there's no risk of dereferencing. + */ + LIBXL_SLIST_REMOVE(&dguest->devices, ddev, libxl__ddomain_device, + next); + LOGD(DEBUG, dev->domid, "Removed device %s from the list of active devices", + libxl__device_backend_path(gc, dev)); + + free(ddev->dev); + free(ddev); + out: return rc; } @@ -1678,38 +1705,13 @@ static void backend_watch_callback(libxl__egc *egc, libxl__ev_xswatch *watch, */ goto skip; } else if (ddev == NULL) { - /* - * New device addition, allocate a struct to hold it and add it - * to the list of active devices for a given guest. - */ - ddev = libxl__zalloc(NOGC, sizeof(*ddev)); - ddev->dev = libxl__zalloc(NOGC, sizeof(*ddev->dev)); - *ddev->dev = *dev; - LIBXL_SLIST_INSERT_HEAD(&dguest->devices, ddev, next); - LOGD(DEBUG, dev->domid, "Added device %s to the list of active devices", - path); - rc = add_device(egc, nested_ao, dguest, ddev); + rc = add_device(egc, nested_ao, dguest, dev); if (rc > 0) free_ao = true; } else if (state == XenbusStateClosed && online == 0) { - /* - * Removal of an active device, remove it from the list and - * free it's data structures if they are no longer needed. - * - * NB: the freeing is safe because all the async ops launched from - * backend_watch_callback make a copy of the data they use, so - * there's no risk of dereferencing. - */ - LIBXL_SLIST_REMOVE(&dguest->devices, ddev, libxl__ddomain_device, - next); - LOGD(DEBUG, dev->domid, "Removed device %s from the list of active devices", - path); rc = remove_device(egc, nested_ao, dguest, ddev); if (rc > 0) free_ao = true; - - free(ddev->dev); - free(ddev); check_and_maybe_remove_guest(gc, ddomain, dguest); } @@ -1720,9 +1722,6 @@ static void backend_watch_callback(libxl__egc *egc, libxl__ev_xswatch *watch, skip: libxl__nested_ao_free(nested_ao); - if (ddev) - free(ddev->dev); - free(ddev); check_and_maybe_remove_guest(gc, ddomain, dguest); return; }