From nobody Fri Oct 18 06:16:17 2024 Delivered-To: importer2@patchew.org Authentication-Results: mx.zohomail.com; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer2=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=movementarian.org Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1717000374236488.89656445075207; Wed, 29 May 2024 09:32:54 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1sCM6r-00015d-F2; Wed, 29 May 2024 12:25:05 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1sCM6n-00013e-1N for qemu-devel@nongnu.org; Wed, 29 May 2024 12:25:01 -0400 Received: from ssh.movementarian.org ([139.162.205.133] helo=movementarian.org) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1sCM6l-0006KY-45 for qemu-devel@nongnu.org; Wed, 29 May 2024 12:25:00 -0400 Received: from movement by movementarian.org with local (Exim 4.95) (envelope-from ) id 1sCM6i-006COn-W9; Wed, 29 May 2024 17:24:56 +0100 From: John Levon To: qemu-devel@nongnu.org Cc: alex.williamson@redhat.com, clg@redhat.com, jag.raman@oracle.com, thanos.makatos@nutanix.com, John Johnson , Elena Ufimtseva , John Levon Subject: [PATCH 06/26] vfio: add region cache Date: Wed, 29 May 2024 17:22:59 +0100 Message-Id: <20240529162319.1476680-7-levon@movementarian.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240529162319.1476680-1-levon@movementarian.org> References: <20240529162319.1476680-1-levon@movementarian.org> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer2=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=139.162.205.133; envelope-from=movement@movementarian.org; helo=movementarian.org X-Spam_score_int: -18 X-Spam_score: -1.9 X-Spam_bar: - X-Spam_report: (-1.9 / 5.0 requ) BAYES_00=-1.9, SPF_HELO_PASS=-0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer2=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer2=patchew.org@nongnu.org X-ZM-MESSAGEID: 1717000376089100005 Content-Type: text/plain; charset="utf-8" From: Jagannathan Raman cache VFIO_DEVICE_GET_REGION_INFO results to reduce memory alloc/free cycles and as prep work for vfio-user Originally-by: John Johnson Signed-off-by: Elena Ufimtseva Signed-off-by: Jagannathan Raman Signed-off-by: John Levon --- hw/vfio/ccw.c | 5 ----- hw/vfio/common.c | 12 ++++++++++++ hw/vfio/container.c | 10 ++++++++++ hw/vfio/helpers.c | 21 ++++++++++++++++----- hw/vfio/igd.c | 8 ++++---- hw/vfio/pci.c | 8 ++++---- include/hw/vfio/vfio-common.h | 1 + 7 files changed, 47 insertions(+), 18 deletions(-) diff --git a/hw/vfio/ccw.c b/hw/vfio/ccw.c index 2600e62e37..315449c1b1 100644 --- a/hw/vfio/ccw.c +++ b/hw/vfio/ccw.c @@ -510,7 +510,6 @@ static bool vfio_ccw_get_region(VFIOCCWDevice *vcdev, E= rror **errp) =20 vcdev->io_region_offset =3D info->offset; vcdev->io_region =3D g_malloc0(info->size); - g_free(info); =20 /* check for the optional async command region */ ret =3D vfio_get_dev_region_info(vdev, VFIO_REGION_TYPE_CCW, @@ -523,7 +522,6 @@ static bool vfio_ccw_get_region(VFIOCCWDevice *vcdev, E= rror **errp) } vcdev->async_cmd_region_offset =3D info->offset; vcdev->async_cmd_region =3D g_malloc0(info->size); - g_free(info); } =20 ret =3D vfio_get_dev_region_info(vdev, VFIO_REGION_TYPE_CCW, @@ -536,7 +534,6 @@ static bool vfio_ccw_get_region(VFIOCCWDevice *vcdev, E= rror **errp) } vcdev->schib_region_offset =3D info->offset; vcdev->schib_region =3D g_malloc(info->size); - g_free(info); } =20 ret =3D vfio_get_dev_region_info(vdev, VFIO_REGION_TYPE_CCW, @@ -550,7 +547,6 @@ static bool vfio_ccw_get_region(VFIOCCWDevice *vcdev, E= rror **errp) } vcdev->crw_region_offset =3D info->offset; vcdev->crw_region =3D g_malloc(info->size); - g_free(info); } =20 return true; @@ -560,7 +556,6 @@ out_err: g_free(vcdev->schib_region); g_free(vcdev->async_cmd_region); g_free(vcdev->io_region); - g_free(info); return false; } =20 diff --git a/hw/vfio/common.c b/hw/vfio/common.c index 81f4c88f2d..1a71c3be05 100644 --- a/hw/vfio/common.c +++ b/hw/vfio/common.c @@ -1542,6 +1542,16 @@ retry: return info; } =20 +static void vfio_get_all_regions(VFIODevice *vbasedev) +{ + struct vfio_region_info *info; + int i; + + for (i =3D 0; i < vbasedev->num_regions; i++) { + vfio_get_region_info(vbasedev, i, &info); + } +} + void vfio_prepare_device(VFIODevice *vbasedev, VFIOContainerBase *bcontain= er, VFIOGroup *group, struct vfio_device_info *info) { @@ -1559,6 +1569,8 @@ void vfio_prepare_device(VFIODevice *vbasedev, VFIOCo= ntainerBase *bcontainer, } =20 QLIST_INSERT_HEAD(&vfio_device_list, vbasedev, global_next); + + vfio_get_all_regions(vbasedev); } =20 bool vfio_attach_device_by_iommu_type(const char *iommu_type, char *name, diff --git a/hw/vfio/container.c b/hw/vfio/container.c index 1bbcbaa874..6c70f95f04 100644 --- a/hw/vfio/container.c +++ b/hw/vfio/container.c @@ -891,6 +891,16 @@ static bool vfio_get_device(VFIOGroup *group, const ch= ar *name, =20 static void vfio_put_base_device(VFIODevice *vbasedev) { + if (vbasedev->regions !=3D NULL) { + int i; + + for (i =3D 0; i < vbasedev->num_regions; i++) { + g_free(vbasedev->regions[i]); + } + g_free(vbasedev->regions); + vbasedev->regions =3D NULL; + } + if (!vbasedev->group) { return; } diff --git a/hw/vfio/helpers.c b/hw/vfio/helpers.c index 27ea26aa48..cdc7eb5bd5 100644 --- a/hw/vfio/helpers.c +++ b/hw/vfio/helpers.c @@ -343,7 +343,7 @@ static int vfio_setup_region_sparse_mmaps(VFIORegion *r= egion, int vfio_region_setup(Object *obj, VFIODevice *vbasedev, VFIORegion *regio= n, int index, const char *name) { - g_autofree struct vfio_region_info *info =3D NULL; + struct vfio_region_info *info =3D NULL; int ret; =20 ret =3D vfio_get_region_info(vbasedev, index, &info); @@ -533,6 +533,17 @@ int vfio_get_region_info(VFIODevice *vbasedev, int ind= ex, { size_t argsz =3D sizeof(struct vfio_region_info); =20 + /* create region cache */ + if (vbasedev->regions =3D=3D NULL) { + vbasedev->regions =3D g_new0(struct vfio_region_info *, + vbasedev->num_regions); + } + /* check cache */ + if (vbasedev->regions[index] !=3D NULL) { + *info =3D vbasedev->regions[index]; + return 0; + } + *info =3D g_malloc0(argsz); =20 (*info)->index =3D index; @@ -552,6 +563,9 @@ retry: goto retry; } =20 + /* fill cache */ + vbasedev->regions[index] =3D *info; + return 0; } =20 @@ -570,7 +584,6 @@ int vfio_get_dev_region_info(VFIODevice *vbasedev, uint= 32_t type, =20 hdr =3D vfio_get_region_info_cap(*info, VFIO_REGION_INFO_CAP_TYPE); if (!hdr) { - g_free(*info); continue; } =20 @@ -582,8 +595,6 @@ int vfio_get_dev_region_info(VFIODevice *vbasedev, uint= 32_t type, if (cap_type->type =3D=3D type && cap_type->subtype =3D=3D subtype= ) { return 0; } - - g_free(*info); } =20 *info =3D NULL; @@ -592,7 +603,7 @@ int vfio_get_dev_region_info(VFIODevice *vbasedev, uint= 32_t type, =20 bool vfio_has_region_cap(VFIODevice *vbasedev, int region, uint16_t cap_ty= pe) { - g_autofree struct vfio_region_info *info =3D NULL; + struct vfio_region_info *info =3D NULL; bool ret =3D false; =20 if (!vfio_get_region_info(vbasedev, region, &info)) { diff --git a/hw/vfio/igd.c b/hw/vfio/igd.c index d320d032a7..34d8a41dd3 100644 --- a/hw/vfio/igd.c +++ b/hw/vfio/igd.c @@ -367,10 +367,10 @@ static const MemoryRegionOps vfio_igd_index_quirk =3D= { =20 void vfio_probe_igd_bar4_quirk(VFIOPCIDevice *vdev, int nr) { - g_autofree struct vfio_region_info *rom =3D NULL; - g_autofree struct vfio_region_info *opregion =3D NULL; - g_autofree struct vfio_region_info *host =3D NULL; - g_autofree struct vfio_region_info *lpc =3D NULL; + struct vfio_region_info *rom =3D NULL; + struct vfio_region_info *opregion =3D NULL; + struct vfio_region_info *host =3D NULL; + struct vfio_region_info *lpc =3D NULL; VFIOQuirk *quirk; VFIOIGDQuirk *igd; PCIDevice *lpc_bridge; diff --git a/hw/vfio/pci.c b/hw/vfio/pci.c index 74a79bdf61..470fb3502a 100644 --- a/hw/vfio/pci.c +++ b/hw/vfio/pci.c @@ -879,7 +879,7 @@ static void vfio_update_msi(VFIOPCIDevice *vdev) =20 static void vfio_pci_load_rom(VFIOPCIDevice *vdev) { - g_autofree struct vfio_region_info *reg_info =3D NULL; + struct vfio_region_info *reg_info =3D NULL; uint64_t size; off_t off =3D 0; ssize_t bytes; @@ -2666,7 +2666,7 @@ static VFIODeviceOps vfio_pci_ops =3D { bool vfio_populate_vga(VFIOPCIDevice *vdev, Error **errp) { VFIODevice *vbasedev =3D &vdev->vbasedev; - g_autofree struct vfio_region_info *reg_info =3D NULL; + struct vfio_region_info *reg_info =3D NULL; int ret; =20 ret =3D vfio_get_region_info(vbasedev, VFIO_PCI_VGA_REGION_INDEX, ®= _info); @@ -2731,7 +2731,7 @@ bool vfio_populate_vga(VFIOPCIDevice *vdev, Error **e= rrp) static bool vfio_populate_device(VFIOPCIDevice *vdev, Error **errp) { VFIODevice *vbasedev =3D &vdev->vbasedev; - g_autofree struct vfio_region_info *reg_info =3D NULL; + struct vfio_region_info *reg_info =3D NULL; struct vfio_irq_info irq_info =3D { .argsz =3D sizeof(irq_info) }; int i, ret =3D -1; =20 @@ -3135,7 +3135,7 @@ static void vfio_realize(PCIDevice *pdev, Error **err= p) =20 if (!vdev->igd_opregion && vdev->features & VFIO_FEATURE_ENABLE_IGD_OPREGION) { - g_autofree struct vfio_region_info *opregion =3D NULL; + struct vfio_region_info *opregion =3D NULL; =20 if (vdev->pdev.qdev.hotplugged) { error_setg(errp, diff --git a/include/hw/vfio/vfio-common.h b/include/hw/vfio/vfio-common.h index ee022c9cbd..2428c7d80c 100644 --- a/include/hw/vfio/vfio-common.h +++ b/include/hw/vfio/vfio-common.h @@ -128,6 +128,7 @@ typedef struct VFIODevice { bool dirty_tracking; int devid; IOMMUFDBackend *iommufd; + struct vfio_region_info **regions; } VFIODevice; =20 struct VFIODeviceOps { --=20 2.34.1