From nobody Fri Apr 19 10:57:40 2024 Delivered-To: importer@patchew.org Received-SPF: none (zoho.com: 192.237.175.120 is neither permitted nor denied by domain of lists.xenproject.org) client-ip=192.237.175.120; envelope-from=xen-devel-bounces@lists.xenproject.org; helo=lists.xenproject.org; Authentication-Results: mx.zohomail.com; spf=none (zoho.com: 192.237.175.120 is neither permitted nor denied by domain of lists.xenproject.org) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=fail(p=none dis=none) header.from=intel.com ARC-Seal: i=1; a=rsa-sha256; t=1562133214; cv=none; d=zoho.com; s=zohoarc; b=e4bziW2C8o+2+HcvkBZ40GOum9mCvRHLpD80hBrcgqo3BeTC5Rfl9MUPir1IjLyh5PI1Ua0yUIxU06j+523scE/Qz2CM0RNmr1KjNkyTSpcFc3P1ht5Ly9z3IhAQD/sz7XIwQJ3tImNxBpy3ZugsKcFaBlaHNHmAdqv2Nxk8tSs= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zoho.com; s=zohoarc; t=1562133214; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:List-Subscribe:List-Post:List-Id:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Sender:Subject:To:ARC-Authentication-Results; bh=QAwAcQq6sMoAA/W6xC19UPvDmJ+83y/KRfsTWAtFhas=; b=QpfrMSR2SJX8jSxGuntvrl4W1+MkJJSHCB96/71/vGFhGwrNW5vw90NO5cizsRJ2cRHNMK9aKKIE1te/6wL5hNkiKpPsL8mN70QiZvwedS7DXGlH0ntNKPvQHAW5MH8LwrwCFbuukKLsRIaZ9vzyV5he2MCJZkSJT09b/gdR5Ro= ARC-Authentication-Results: i=1; mx.zoho.com; spf=none (zoho.com: 192.237.175.120 is neither permitted nor denied by domain of lists.xenproject.org) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=fail header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 1562133214059705.4488019884465; Tue, 2 Jul 2019 22:53:34 -0700 (PDT) Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.89) (envelope-from ) id 1hiYBc-0008L9-CB; Wed, 03 Jul 2019 05:52:08 +0000 Received: from us1-rack-dfw2.inumbo.com ([104.130.134.6]) by lists.xenproject.org with esmtp (Exim 4.89) (envelope-from ) id 1hiYBa-0008L4-S9 for xen-devel@lists.xenproject.org; Wed, 03 Jul 2019 05:52:06 +0000 Received: from mga11.intel.com (unknown [192.55.52.93]) by us1-rack-dfw2.inumbo.com (Halon) with ESMTPS id af9563f1-9d56-11e9-8980-bc764e045a96; Wed, 03 Jul 2019 05:52:05 +0000 (UTC) Received: from orsmga007.jf.intel.com ([10.7.209.58]) by fmsmga102.fm.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 02 Jul 2019 22:52:04 -0700 Received: from gao-cwp.sh.intel.com ([10.239.159.26]) by orsmga007.jf.intel.com with ESMTP; 02 Jul 2019 22:52:03 -0700 X-Inumbo-ID: af9563f1-9d56-11e9-8980-bc764e045a96 X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.63,446,1557212400"; d="scan'208";a="154649872" From: Chao Gao To: xen-devel@lists.xenproject.org Date: Wed, 3 Jul 2019 13:56:13 +0800 Message-Id: <1562133373-19208-1-git-send-email-chao.gao@intel.com> X-Mailer: git-send-email 1.9.1 Subject: [Xen-devel] [PATCH v2] libxl_qmp: wait for completion of device removal X-BeenThere: xen-devel@lists.xenproject.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Cc: Anthony PERARD , Ian Jackson , Wei Liu , Chao Gao MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Errors-To: xen-devel-bounces@lists.xenproject.org Sender: "Xen-devel" To remove a device from a domain, a qmp command is sent to qemu. But it is handled by qemu asychronously. Even the qmp command is claimed to be done, the actual handling in qemu side may happen later. This behavior brings two questions: 1. Attaching a device back to a domain right after detaching the device from that domain would fail with error: libxl: error: libxl_qmp.c:341:qmp_handle_error_response: Domain 1:received = an error message from QMP server: Duplicate ID 'pci-pt-60_00.0' for device 2. Accesses to PCI configuration space in Qemu may overlap with later device reset issued by 'xl' or by pciback. In order to avoid mentioned questions, wait for the completion of device removal by querying all pci devices using qmp command and ensuring the targ= et device isn't listed. Only retry 5 times to avoid 'xl' potentially being blo= cked by qemu. Signed-off-by: Chao Gao Reviewed-by: Anthony PERARD --- Changes in v2: - Break out early if we found an error during querying pci devices. - Print a message to warn admin that device removal may not be done in device model's side. --- tools/libxl/libxl_qmp.c | 61 ++++++++++++++++++++++++++++++++++++++++- 1 file changed, 60 insertions(+), 1 deletion(-) diff --git a/tools/libxl/libxl_qmp.c b/tools/libxl/libxl_qmp.c index 42c8ab8d8d..9c4480a2b1 100644 --- a/tools/libxl/libxl_qmp.c +++ b/tools/libxl/libxl_qmp.c @@ -916,6 +916,38 @@ out: return rc; } =20 +static int pci_del_callback(libxl__qmp_handler *qmp, + const libxl__json_object *response, void *opaq= ue) +{ + const char *asked_id =3D opaque; + const libxl__json_object *bus =3D NULL; + GC_INIT(qmp->ctx); + int i, j, rc =3D 0; + + for (i =3D 0; (bus =3D libxl__json_array_get(response, i)); i++) { + const libxl__json_object *devices =3D NULL; + const libxl__json_object *device =3D NULL; + const libxl__json_object *o =3D NULL; + const char *id =3D NULL; + + devices =3D libxl__json_map_get("devices", bus, JSON_ARRAY); + + for (j =3D 0; (device =3D libxl__json_array_get(devices, j)); j++)= { + o =3D libxl__json_map_get("qdev_id", device, JSON_STRING); + id =3D libxl__json_object_get_string(o); + + if (id && strcmp(asked_id, id) =3D=3D 0) { + rc =3D 1; + goto out; + } + } + } + +out: + GC_FREE; + return rc; +} + static int qmp_run_command(libxl__gc *gc, int domid, const char *cmd, libxl__json_object *args, qmp_callback_t callback, void *opaque) @@ -1000,9 +1032,36 @@ int libxl__qmp_pci_add(libxl__gc *gc, int domid, lib= xl_device_pci *pcidev) static int qmp_device_del(libxl__gc *gc, int domid, char *id) { libxl__json_object *args =3D NULL; + libxl__qmp_handler *qmp =3D NULL; + int rc =3D 0; + + qmp =3D libxl__qmp_initialize(gc, domid); + if (!qmp) + return ERROR_FAIL; =20 qmp_parameters_add_string(gc, &args, "id", id); - return qmp_run_command(gc, domid, "device_del", args, NULL, NULL); + rc =3D qmp_synchronous_send(qmp, "device_del", args, + NULL, NULL, qmp->timeout); + if (rc =3D=3D 0) { + unsigned int retry =3D 0; + + do { + rc =3D qmp_synchronous_send(qmp, "query-pci", NULL, + pci_del_callback, id, qmp->timeout); + if (rc !=3D 1) { + break; + } + sleep(1); + } while (retry++ < 5); + + if (rc !=3D 0) { + LOGD(WARN, qmp->domid, + "device model may not complete removing device %s", id); + } + } + + libxl__qmp_close(qmp); + return rc; } =20 int libxl__qmp_pci_del(libxl__gc *gc, int domid, libxl_device_pci *pcidev) --=20 2.17.1 _______________________________________________ Xen-devel mailing list Xen-devel@lists.xenproject.org https://lists.xenproject.org/mailman/listinfo/xen-devel