* [PATCH] vfio: Implement a common device info helper
@ 2023-06-01 14:45 Alex Williamson
2023-06-01 15:01 ` Robin Voetter
0 siblings, 1 reply; 2+ messages in thread
From: Alex Williamson @ 2023-06-01 14:45 UTC (permalink / raw)
To: qemu-devel; +Cc: Alex Williamson, clg, robin, philmd
A common helper implementing the realloc algorithm for handling
capabilities.
Reviewed-by: Philippe Mathieu-Daudé <philmd@linaro.org>
Reviewed-by: Cédric Le Goater <clg@redhat.com>
Signed-off-by: Alex Williamson <alex.williamson@redhat.com>
---
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 *pbdev,
memcpy(pbdev->zpci_fn.pfip, cap->pfip, CLP_PFIP_NR_SEGMENTS);
}
-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 = g_malloc0(argsz);
- VFIOPCIDevice *vfio_pci;
- int fd;
+ VFIOPCIDevice *vfio_pci = container_of(pbdev->pdev, VFIOPCIDevice, pdev);
- vfio_pci = container_of(pbdev->pdev, VFIOPCIDevice, pdev);
- fd = vfio_pci->vbasedev.fd;
-
- /*
- * If the specified argsz is not large enough to contain all capabilities
- * 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 = 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 = info->argsz;
- info = g_realloc(info, argsz);
- goto retry;
- }
-
- return info;
+ return vfio_get_device_info(vfio_pci->vbasedev.fd);
}
/*
@@ -335,7 +308,7 @@ bool s390_pci_get_host_fh(S390PCIBusDevice *pbdev, uint32_t *fh)
assert(fh);
- info = get_device_info(pbdev, sizeof(*info));
+ info = 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 = NULL;
- info = get_device_info(pbdev, sizeof(*info));
+ info = 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)
}
}
+struct vfio_device_info *vfio_get_device_info(int fd)
+{
+ struct vfio_device_info *info;
+ uint32_t argsz = sizeof(*info);
+
+ info = g_malloc0(argsz);
+
+retry:
+ info->argsz = argsz;
+
+ if (ioctl(fd, VFIO_DEVICE_GET_INFO, info)) {
+ g_free(info);
+ return NULL;
+ }
+
+ if (info->argsz > argsz) {
+ argsz = info->argsz;
+ info = 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 = { .argsz = sizeof(dev_info) };
- int ret, fd;
+ g_autofree struct vfio_device_info *info = NULL;
+ int fd;
fd = ioctl(group->fd, VFIO_GROUP_GET_DEVICE_FD, name);
if (fd < 0) {
@@ -2859,11 +2883,11 @@ int vfio_get_device(VFIOGroup *group, const char *name,
return fd;
}
- ret = ioctl(fd, VFIO_DEVICE_GET_INFO, &dev_info);
- if (ret) {
+ info = vfio_get_device_info(fd);
+ if (!info) {
error_setg_errno(errp, errno, "error getting device info");
close(fd);
- return ret;
+ return -1;
}
/*
@@ -2891,14 +2915,14 @@ int vfio_get_device(VFIOGroup *group, const char *name,
vbasedev->group = group;
QLIST_INSERT_HEAD(&group->device_list, vbasedev, next);
- vbasedev->num_irqs = dev_info.num_irqs;
- vbasedev->num_regions = dev_info.num_regions;
- vbasedev->flags = dev_info.flags;
+ vbasedev->num_irqs = info->num_irqs;
+ vbasedev->num_regions = info->num_regions;
+ vbasedev->flags = info->flags;
+
+ trace_vfio_get_device(name, info->flags, info->num_regions, info->num_irqs);
- trace_vfio_get_device(name, dev_info.flags, dev_info.num_regions,
- dev_info.num_irqs);
+ vbasedev->reset_works = !!(info->flags & VFIO_DEVICE_FLAGS_RESET);
- vbasedev->reset_works = !!(dev_info.flags & VFIO_DEVICE_FLAGS_RESET);
return 0;
}
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);
--
2.39.2
^ permalink raw reply related [flat|nested] 2+ messages in thread
* Re: [PATCH] vfio: Implement a common device info helper
2023-06-01 14:45 [PATCH] vfio: Implement a common device info helper Alex Williamson
@ 2023-06-01 15:01 ` Robin Voetter
0 siblings, 0 replies; 2+ messages in thread
From: Robin Voetter @ 2023-06-01 15:01 UTC (permalink / raw)
To: Alex Williamson, qemu-devel; +Cc: clg, philmd
On 6/1/23 16:45, Alex Williamson wrote:
> A common helper implementing the realloc algorithm for handling
> capabilities.
>
> Reviewed-by: Philippe Mathieu-Daudé <philmd@linaro.org>
> Reviewed-by: Cédric Le Goater <clg@redhat.com>
> Signed-off-by: Alex Williamson <alex.williamson@redhat.com>
Reviewed-by: Robin Voetter <robin@streamhpc.com>
> ---
>
> 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 *pbdev,
> memcpy(pbdev->zpci_fn.pfip, cap->pfip, CLP_PFIP_NR_SEGMENTS);
> }
>
> -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 = g_malloc0(argsz);
> - VFIOPCIDevice *vfio_pci;
> - int fd;
> + VFIOPCIDevice *vfio_pci = container_of(pbdev->pdev, VFIOPCIDevice, pdev);
>
> - vfio_pci = container_of(pbdev->pdev, VFIOPCIDevice, pdev);
> - fd = vfio_pci->vbasedev.fd;
> -
> - /*
> - * If the specified argsz is not large enough to contain all capabilities
> - * 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 = 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 = info->argsz;
> - info = g_realloc(info, argsz);
> - goto retry;
> - }
> -
> - return info;
> + return vfio_get_device_info(vfio_pci->vbasedev.fd);
> }
>
> /*
> @@ -335,7 +308,7 @@ bool s390_pci_get_host_fh(S390PCIBusDevice *pbdev, uint32_t *fh)
>
> assert(fh);
>
> - info = get_device_info(pbdev, sizeof(*info));
> + info = 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 = NULL;
>
> - info = get_device_info(pbdev, sizeof(*info));
> + info = 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)
> }
> }
>
> +struct vfio_device_info *vfio_get_device_info(int fd)
> +{
> + struct vfio_device_info *info;
> + uint32_t argsz = sizeof(*info);
> +
> + info = g_malloc0(argsz);
> +
> +retry:
> + info->argsz = argsz;
> +
> + if (ioctl(fd, VFIO_DEVICE_GET_INFO, info)) {
> + g_free(info);
> + return NULL;
> + }
> +
> + if (info->argsz > argsz) {
> + argsz = info->argsz;
> + info = 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 = { .argsz = sizeof(dev_info) };
> - int ret, fd;
> + g_autofree struct vfio_device_info *info = NULL;
> + int fd;
>
> fd = ioctl(group->fd, VFIO_GROUP_GET_DEVICE_FD, name);
> if (fd < 0) {
> @@ -2859,11 +2883,11 @@ int vfio_get_device(VFIOGroup *group, const char *name,
> return fd;
> }
>
> - ret = ioctl(fd, VFIO_DEVICE_GET_INFO, &dev_info);
> - if (ret) {
> + info = vfio_get_device_info(fd);
> + if (!info) {
> error_setg_errno(errp, errno, "error getting device info");
> close(fd);
> - return ret;
> + return -1;
> }
>
> /*
> @@ -2891,14 +2915,14 @@ int vfio_get_device(VFIOGroup *group, const char *name,
> vbasedev->group = group;
> QLIST_INSERT_HEAD(&group->device_list, vbasedev, next);
>
> - vbasedev->num_irqs = dev_info.num_irqs;
> - vbasedev->num_regions = dev_info.num_regions;
> - vbasedev->flags = dev_info.flags;
> + vbasedev->num_irqs = info->num_irqs;
> + vbasedev->num_regions = info->num_regions;
> + vbasedev->flags = info->flags;
> +
> + trace_vfio_get_device(name, info->flags, info->num_regions, info->num_irqs);
>
> - trace_vfio_get_device(name, dev_info.flags, dev_info.num_regions,
> - dev_info.num_irqs);
> + vbasedev->reset_works = !!(info->flags & VFIO_DEVICE_FLAGS_RESET);
>
> - vbasedev->reset_works = !!(dev_info.flags & VFIO_DEVICE_FLAGS_RESET);
> return 0;
> }
>
> 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);
>
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2023-06-01 15:01 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2023-06-01 14:45 [PATCH] vfio: Implement a common device info helper Alex Williamson
2023-06-01 15:01 ` Robin Voetter
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).