From nobody Thu Apr 25 08:30:09 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 ARC-Seal: i=1; a=rsa-sha256; t=1561556334; cv=none; d=zoho.com; s=zohoarc; b=SfLC9+PAjqoDvgsPiB01/GbmTmpBW68ia6AzeQiXU+E9eq0lUzGT/o0igjbS2c7QAdpLl+bnD+hwIvxoXjC5dTV/xJAor+HGTFOgXLElkVD/sHn/Ok75dDEq4yRFSJgDhM82bQ9o4PaEKkhmDj6jN3jm0Jszy7eOCrvVXTMZT1M= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zoho.com; s=zohoarc; t=1561556334; 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=99s4b1jVwvLU7qOP4/JNQ+hpKcMJ6CNoEMXlpt2ByQ4=; b=Lr5ZGZOOCvK7bDHBgmf2oD2tmGdsNZTwWtwSbVz8Q10bV1giEInmTcpqbtgsgyuOuLVXHDXva0gHJepZRhovcMo6lMp/778lBnyvK8X1oXsrSAQYXI42gNSdKXU2VQ1msjrr+IVfgeRiyxQkwPA/hsEFzsIBTFIDcB4B/ozEFC0= 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 Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 1561556334733945.5488738610543; Wed, 26 Jun 2019 06:38:54 -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 1hg87A-0005ah-ND; Wed, 26 Jun 2019 13:37:32 +0000 Received: from all-amaz-eas1.inumbo.com ([34.197.232.57] helo=us1-amaz-eas2.inumbo.com) by lists.xenproject.org with esmtp (Exim 4.89) (envelope-from ) id 1hg878-0005aZ-PL for xen-devel@lists.xenproject.org; Wed, 26 Jun 2019 13:37:30 +0000 Received: from mx1.suse.de (unknown [195.135.220.15]) by us1-amaz-eas2.inumbo.com (Halon) with ESMTPS id 8ac20c52-9817-11e9-a981-2bca2498b7ef; Wed, 26 Jun 2019 13:37:29 +0000 (UTC) Received: from relay2.suse.de (unknown [195.135.220.254]) by mx1.suse.de (Postfix) with ESMTP id D8478AFB2; Wed, 26 Jun 2019 13:37:28 +0000 (UTC) X-Inumbo-ID: 8ac20c52-9817-11e9-a981-2bca2498b7ef X-Virus-Scanned: by amavisd-new at test-mx.suse.de From: Juergen Gross To: xen-devel@lists.xenproject.org Date: Wed, 26 Jun 2019 15:37:26 +0200 Message-Id: <20190626133726.29896-1-jgross@suse.com> X-Mailer: git-send-email 2.16.4 Subject: [Xen-devel] [PATCH] libxl: fix pci device re-assigning after domain reboot 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: Juergen Gross , Ian Jackson , Wei Liu 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" After a reboot of a guest only the first pci device configuration will be retrieved from Xenstore resulting in loss of any further assigned passed through pci devices. The main reason is that all passed through pci devices reside under a common root device "0" in Xenstore. So when the device list is rebuilt from Xenstore after a reboot the sub-devices below that root device need to be selected instead of using the root device number as a selector. Fix that by adding a new member to struct libxl_device_type which when set is used to get the number of devices. Add such a member for pci to get the correct number of pci devices instead of implying it from the number of pci root devices (which will always be 1). While at it fix the type of libxl__device_pci_from_xs_be() to match the one of the .from_xenstore member of struct libxl_device_type. This fixes a latent bug checking the return value of a function returning void. Signed-off-by: Juergen Gross Tested-by: Chao Gao --- tools/libxl/libxl_device.c | 24 +++++++++++++++++++----- tools/libxl/libxl_internal.h | 2 ++ tools/libxl/libxl_pci.c | 35 ++++++++++++++++++++++++++--------- 3 files changed, 47 insertions(+), 14 deletions(-) diff --git a/tools/libxl/libxl_device.c b/tools/libxl/libxl_device.c index db6c0203b7..a2569102ee 100644 --- a/tools/libxl/libxl_device.c +++ b/tools/libxl/libxl_device.c @@ -2026,6 +2026,7 @@ void *libxl__device_list(libxl__gc *gc, const struct = libxl_device_type *dt, char *libxl_path; char **dir =3D NULL; unsigned int ndirs =3D 0; + unsigned int ndevs =3D 0; int rc; =20 *num =3D 0; @@ -2037,21 +2038,34 @@ void *libxl__device_list(libxl__gc *gc, const struc= t libxl_device_type *dt, dir =3D libxl__xs_directory(gc, XBT_NULL, libxl_path, &ndirs); =20 if (dir && ndirs) { - list =3D libxl__malloc(NOGC, dt->dev_elem_size * ndirs); + if (dt->get_num) { + if (ndirs !=3D 1) { + LOGD(ERROR, domid, "multiple entries in %s\n", libxl_path); + rc =3D ERROR_FAIL; + goto out; + } + rc =3D dt->get_num(gc, GCSPRINTF("%s/%s", libxl_path, *dir), &= ndevs); + if (rc) goto out; + } else { + ndevs =3D ndirs; + } + list =3D libxl__malloc(NOGC, dt->dev_elem_size * ndevs); item =3D list; =20 - while (*num < ndirs) { + while (*num < ndevs) { dt->init(item); - ++(*num); =20 if (dt->from_xenstore) { + int nr =3D dt->get_num ? *num : atoi(*dir); char *device_libxl_path =3D GCSPRINTF("%s/%s", libxl_path,= *dir); - rc =3D dt->from_xenstore(gc, device_libxl_path, atoi(*dir)= , item); + rc =3D dt->from_xenstore(gc, device_libxl_path, nr, item); if (rc) goto out; } =20 item =3D (uint8_t *)item + dt->dev_elem_size; - ++dir; + ++(*num); + if (!dt->get_num) + ++dir; } } =20 diff --git a/tools/libxl/libxl_internal.h b/tools/libxl/libxl_internal.h index 3be5c644c1..a3102871f3 100644 --- a/tools/libxl/libxl_internal.h +++ b/tools/libxl/libxl_internal.h @@ -3707,6 +3707,7 @@ typedef void (*device_merge_fn_t)(libxl_ctx *, void *= , void *); typedef int (*device_dm_needed_fn_t)(void *, unsigned); typedef void (*device_update_config_fn_t)(libxl__gc *, void *, void *); typedef int (*device_update_devid_fn_t)(libxl__gc *, uint32_t, void *); +typedef int (*device_get_num_fn_t)(libxl__gc *, const char *, unsigned int= *); typedef int (*device_from_xenstore_fn_t)(libxl__gc *, const char *, libxl_devid, void *); typedef int (*device_set_xenstore_config_fn_t)(libxl__gc *, uint32_t, void= *, @@ -3730,6 +3731,7 @@ struct libxl_device_type { device_dm_needed_fn_t dm_needed; device_update_config_fn_t update_config; device_update_devid_fn_t update_devid; + device_get_num_fn_t get_num; device_from_xenstore_fn_t from_xenstore; device_set_xenstore_config_fn_t set_xenstore_config; }; diff --git a/tools/libxl/libxl_pci.c b/tools/libxl/libxl_pci.c index 4ec6872798..03beb865d9 100644 --- a/tools/libxl/libxl_pci.c +++ b/tools/libxl/libxl_pci.c @@ -1547,12 +1547,13 @@ int libxl_device_pci_destroy(libxl_ctx *ctx, uint32= _t domid, return AO_INPROGRESS; } =20 -static void libxl__device_pci_from_xs_be(libxl__gc *gc, - const char *be_path, - int nr, libxl_device_pci *pci) +static int libxl__device_pci_from_xs_be(libxl__gc *gc, + const char *be_path, + libxl_devid nr, void *data) { char *s; unsigned int domain =3D 0, bus =3D 0, dev =3D 0, func =3D 0, vdevfn = =3D 0; + libxl_device_pci *pci =3D data; =20 s =3D libxl__xs_read(gc, XBT_NULL, GCSPRINTF("%s/dev-%d", be_path, nr)= ); sscanf(s, PCI_BDF, &domain, &bus, &dev, &func); @@ -1582,24 +1583,39 @@ static void libxl__device_pci_from_xs_be(libxl__gc = *gc, } } while ((p =3D strtok_r(NULL, ",=3D", &saveptr)) !=3D NULL); } + + return 0; +} + +static int libxl__device_pci_get_num(libxl__gc *gc, const char *be_path, + unsigned int *num) +{ + char *num_devs; + int rc =3D 0; + + num_devs =3D libxl__xs_read(gc, XBT_NULL, GCSPRINTF("%s/num_devs", be_= path)); + if (!num_devs) + rc =3D ERROR_FAIL; + else + *num =3D atoi(num_devs); + + return rc; } =20 libxl_device_pci *libxl_device_pci_list(libxl_ctx *ctx, uint32_t domid, in= t *num) { GC_INIT(ctx); - char *be_path, *num_devs; - int n, i; + char *be_path; + unsigned int n, i; libxl_device_pci *pcidevs =3D NULL; =20 *num =3D 0; =20 be_path =3D libxl__domain_device_backend_path(gc, 0, domid, 0, LIBXL__DEVICE_KIND_PCI); - num_devs =3D libxl__xs_read(gc, XBT_NULL, GCSPRINTF("%s/num_devs", be_= path)); - if (!num_devs) + if (libxl__device_pci_get_num(gc, be_path, &n)) goto out; =20 - n =3D atoi(num_devs); pcidevs =3D calloc(n, sizeof(libxl_device_pci)); =20 for (i =3D 0; i < n; i++) @@ -1688,7 +1704,8 @@ static int libxl_device_pci_compare(const libxl_devic= e_pci *d1, #define libxl__device_pci_update_devid NULL =20 DEFINE_DEVICE_TYPE_STRUCT_X(pcidev, pci, PCI, - .from_xenstore =3D (device_from_xenstore_fn_t)libxl__device_pci_from_x= s_be, + .get_num =3D libxl__device_pci_get_num, + .from_xenstore =3D libxl__device_pci_from_xs_be, ); =20 /* --=20 2.16.4 _______________________________________________ Xen-devel mailing list Xen-devel@lists.xenproject.org https://lists.xenproject.org/mailman/listinfo/xen-devel