From nobody Mon May 6 05:05:04 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1685630770; cv=none; d=zohomail.com; s=zohoarc; b=hTQrcd3YcaR2rYTPyI3BoI+MMLg+8MnYDrLZEXJOzWCpL7cKkfSHubrRXsCv1Abu6X+ONn0EpDJG54Vc7Ns3o/acluUgE32lvmIJlskTIjlpVQViimAg0N9AQFTkljHW0EHEGXtUcO+pXUnqa9TccI0mZVCVjhnlMltfHqTQSDw= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1685630770; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Sender:Subject:To; bh=NITtXpTE7BCLkawwlIXFp9Ajmlqyypv78zAXUrrsDVA=; b=JNhqwTtbxgp8L/cq6zir6z0F1gDVdO4XI3cFxcP7eo+/gSZCkuHJV9TSqApp6P2fDH/zF4jx+BlBcv3b92mI7th+QX5e+uMSSF/vxGac+dzK1KCiJXI5H5Hy1GYnX35H+IZSx9m8l68+jIE30TScHwLqVy2BCiqlxI0OvVxOcTM= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1685630770021852.5870371612139; Thu, 1 Jun 2023 07:46:10 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1q4jYV-0005JE-Cu; Thu, 01 Jun 2023 10:45:35 -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 1q4jYI-0005Is-PM for qemu-devel@nongnu.org; Thu, 01 Jun 2023 10:45:24 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1q4jYG-0003PJ-Su for qemu-devel@nongnu.org; Thu, 01 Jun 2023 10:45:22 -0400 Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-74-TmurjrJHN-GkX43qwaXWcA-1; Thu, 01 Jun 2023 10:45:16 -0400 Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.rdu2.redhat.com [10.11.54.6]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 02D7B85A5BA; Thu, 1 Jun 2023 14:45:16 +0000 (UTC) Received: from omen.home.shazbot.org (unknown [10.22.17.157]) by smtp.corp.redhat.com (Postfix) with ESMTP id 3F4F82166B25; Thu, 1 Jun 2023 14:45:15 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1685630719; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=NITtXpTE7BCLkawwlIXFp9Ajmlqyypv78zAXUrrsDVA=; b=EZ5kkVCB0mAqai3UHnXkLbskg3LCg+BhBCCJJL0eEDQWcMrElw4TZ2uOzF87wR8W601ET2 M4mrD/uuPjWAR6Mspo2dsexuV0bCheTNwgQOI1bSQ/1oYkW1iJ2z2gtGYcrfCbqZLUZFyG ny1+f3s/BvMQRfwWGCx9ivAHMHPN7eU= X-MC-Unique: TmurjrJHN-GkX43qwaXWcA-1 From: Alex Williamson To: qemu-devel@nongnu.org Cc: Alex Williamson , clg@redhat.com, robin@streamhpc.com, philmd@linaro.org Subject: [PATCH] vfio: Implement a common device info helper Date: Thu, 1 Jun 2023 08:45:06 -0600 Message-Id: <20230601144506.2105353-1-alex.williamson@redhat.com> MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.1 on 10.11.54.6 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+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=170.10.133.124; envelope-from=alex.williamson@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -22 X-Spam_score: -2.3 X-Spam_bar: -- X-Spam_report: (-2.3 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.166, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=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+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @redhat.com) X-ZM-MESSAGEID: 1685630771641100003 A common helper implementing the realloc algorithm for handling capabilities. Reviewed-by: Philippe Mathieu-Daud=C3=A9 Reviewed-by: C=C3=A9dric Le Goater Signed-off-by: Alex Williamson Reviewed-by: Robin Voetter --- RFCv2 -> v1: Remove spurious semicolon hw/s390x/s390-pci-vfio.c | 37 ++++------------------------ hw/vfio/common.c | 46 ++++++++++++++++++++++++++--------- include/hw/vfio/vfio-common.h | 1 + 3 files changed, 41 insertions(+), 43 deletions(-) diff --git a/hw/s390x/s390-pci-vfio.c b/hw/s390x/s390-pci-vfio.c index f51190d4662f..59a2e03873bd 100644 --- a/hw/s390x/s390-pci-vfio.c +++ b/hw/s390x/s390-pci-vfio.c @@ -289,38 +289,11 @@ static void s390_pci_read_pfip(S390PCIBusDevice *pbde= v, memcpy(pbdev->zpci_fn.pfip, cap->pfip, CLP_PFIP_NR_SEGMENTS); } =20 -static struct vfio_device_info *get_device_info(S390PCIBusDevice *pbdev, - uint32_t argsz) +static struct vfio_device_info *get_device_info(S390PCIBusDevice *pbdev) { - struct vfio_device_info *info =3D g_malloc0(argsz); - VFIOPCIDevice *vfio_pci; - int fd; + VFIOPCIDevice *vfio_pci =3D container_of(pbdev->pdev, VFIOPCIDevice, p= dev); =20 - vfio_pci =3D container_of(pbdev->pdev, VFIOPCIDevice, pdev); - fd =3D vfio_pci->vbasedev.fd; - - /* - * If the specified argsz is not large enough to contain all capabilit= ies - * it will be updated upon return from the ioctl. Retry until we have - * a big enough buffer to hold the entire capability chain. On error, - * just exit and rely on CLP defaults. - */ -retry: - info->argsz =3D argsz; - - if (ioctl(fd, VFIO_DEVICE_GET_INFO, info)) { - trace_s390_pci_clp_dev_info(vfio_pci->vbasedev.name); - g_free(info); - return NULL; - } - - if (info->argsz > argsz) { - argsz =3D info->argsz; - info =3D g_realloc(info, argsz); - goto retry; - } - - return info; + return vfio_get_device_info(vfio_pci->vbasedev.fd); } =20 /* @@ -335,7 +308,7 @@ bool s390_pci_get_host_fh(S390PCIBusDevice *pbdev, uint= 32_t *fh) =20 assert(fh); =20 - info =3D get_device_info(pbdev, sizeof(*info)); + info =3D get_device_info(pbdev); if (!info) { return false; } @@ -356,7 +329,7 @@ void s390_pci_get_clp_info(S390PCIBusDevice *pbdev) { g_autofree struct vfio_device_info *info =3D NULL; =20 - info =3D get_device_info(pbdev, sizeof(*info)); + info =3D get_device_info(pbdev); if (!info) { return; } diff --git a/hw/vfio/common.c b/hw/vfio/common.c index 78358ede2764..ed142296e9fe 100644 --- a/hw/vfio/common.c +++ b/hw/vfio/common.c @@ -2843,11 +2843,35 @@ void vfio_put_group(VFIOGroup *group) } } =20 +struct vfio_device_info *vfio_get_device_info(int fd) +{ + struct vfio_device_info *info; + uint32_t argsz =3D sizeof(*info); + + info =3D g_malloc0(argsz); + +retry: + info->argsz =3D argsz; + + if (ioctl(fd, VFIO_DEVICE_GET_INFO, info)) { + g_free(info); + return NULL; + } + + if (info->argsz > argsz) { + argsz =3D info->argsz; + info =3D g_realloc(info, argsz); + goto retry; + } + + return info; +} + int vfio_get_device(VFIOGroup *group, const char *name, VFIODevice *vbasedev, Error **errp) { - struct vfio_device_info dev_info =3D { .argsz =3D sizeof(dev_info) }; - int ret, fd; + g_autofree struct vfio_device_info *info =3D NULL; + int fd; =20 fd =3D ioctl(group->fd, VFIO_GROUP_GET_DEVICE_FD, name); if (fd < 0) { @@ -2859,11 +2883,11 @@ int vfio_get_device(VFIOGroup *group, const char *n= ame, return fd; } =20 - ret =3D ioctl(fd, VFIO_DEVICE_GET_INFO, &dev_info); - if (ret) { + info =3D vfio_get_device_info(fd); + if (!info) { error_setg_errno(errp, errno, "error getting device info"); close(fd); - return ret; + return -1; } =20 /* @@ -2891,14 +2915,14 @@ int vfio_get_device(VFIOGroup *group, const char *n= ame, vbasedev->group =3D group; QLIST_INSERT_HEAD(&group->device_list, vbasedev, next); =20 - vbasedev->num_irqs =3D dev_info.num_irqs; - vbasedev->num_regions =3D dev_info.num_regions; - vbasedev->flags =3D dev_info.flags; + vbasedev->num_irqs =3D info->num_irqs; + vbasedev->num_regions =3D info->num_regions; + vbasedev->flags =3D info->flags; + + trace_vfio_get_device(name, info->flags, info->num_regions, info->num_= irqs); =20 - trace_vfio_get_device(name, dev_info.flags, dev_info.num_regions, - dev_info.num_irqs); + vbasedev->reset_works =3D !!(info->flags & VFIO_DEVICE_FLAGS_RESET); =20 - vbasedev->reset_works =3D !!(dev_info.flags & VFIO_DEVICE_FLAGS_RESET); return 0; } =20 diff --git a/include/hw/vfio/vfio-common.h b/include/hw/vfio/vfio-common.h index eed244f25f34..a8dcda592c08 100644 --- a/include/hw/vfio/vfio-common.h +++ b/include/hw/vfio/vfio-common.h @@ -212,6 +212,7 @@ void vfio_region_finalize(VFIORegion *region); void vfio_reset_handler(void *opaque); VFIOGroup *vfio_get_group(int groupid, AddressSpace *as, Error **errp); void vfio_put_group(VFIOGroup *group); +struct vfio_device_info *vfio_get_device_info(int fd); int vfio_get_device(VFIOGroup *group, const char *name, VFIODevice *vbasedev, Error **errp); =20 --=20 2.39.2