From: "Cédric Le Goater" <clg@redhat.com>
To: John Levon <john.levon@nutanix.com>, qemu-devel@nongnu.org
Cc: "Peter Xu" <peterx@redhat.com>,
qemu-s390x@nongnu.org, "Jason Herne" <jjherne@linux.ibm.com>,
"Tomita Moeko" <tomitamoeko@gmail.com>,
"Markus Armbruster" <armbru@redhat.com>,
"Matthew Rosato" <mjrosato@linux.ibm.com>,
"Eric Farman" <farman@linux.ibm.com>,
"David Hildenbrand" <david@redhat.com>,
"Philippe Mathieu-Daudé" <philmd@linaro.org>,
"Michael S. Tsirkin" <mst@redhat.com>,
"Tony Krowiak" <akrowiak@linux.ibm.com>,
"Alex Williamson" <alex.williamson@redhat.com>,
"Stefano Garzarella" <sgarzare@redhat.com>,
"Thomas Huth" <thuth@redhat.com>,
"Paolo Bonzini" <pbonzini@redhat.com>,
"Halil Pasic" <pasic@linux.ibm.com>
Subject: Re: [PATCH v2 12/15] vfio: add read/write to device IO ops vector
Date: Mon, 5 May 2025 14:39:19 +0200 [thread overview]
Message-ID: <b573f499-4083-4aac-9ee0-04e6028e4cf3@redhat.com> (raw)
In-Reply-To: <20250430194003.2793823-13-john.levon@nutanix.com>
On 4/30/25 21:40, John Levon wrote:
> Now we have the region info cache, add ->region_read/write device I/O
> operations instead of explicit pread()/pwrite() system calls.
> ---
> hw/vfio/device.c | 38 +++++++++++++++++++++++++++++++++++
> hw/vfio/pci.c | 28 +++++++++++++-------------
> hw/vfio/region.c | 17 ++++++++++------
> include/hw/vfio/vfio-device.h | 18 +++++++++++++++++
> 4 files changed, 81 insertions(+), 20 deletions(-)
>
> diff --git a/hw/vfio/device.c b/hw/vfio/device.c
> index d08c0ab536..ceb7bbebda 100644
> --- a/hw/vfio/device.c
> +++ b/hw/vfio/device.c
> @@ -510,9 +510,47 @@ static int vfio_device_io_set_irqs(VFIODevice *vbasedev,
> return ret < 0 ? -errno : ret;
> }
>
> +static int vfio_device_io_region_read(VFIODevice *vbasedev, uint8_t index,
> + off_t off, uint32_t size, void *data)
> +{
> + struct vfio_region_info *info = vbasedev->reginfo[index];
Why not rely on vfio_device_get_region_info() to fill the cache and
return the cached struct vfio_region_info ?
> + int ret;
> +
> + if (info == NULL) {
> + ret = vfio_device_get_region_info(vbasedev, index, &info);
> + if (ret != 0) {
> + return ret;
> + }
> + }
> +
> + ret = pread(vbasedev->fd, data, size, info->offset + off);
> +
> + return ret < 0 ? -errno : ret;
> +}
> +
> +static int vfio_device_io_region_write(VFIODevice *vbasedev, uint8_t index,
> + off_t off, uint32_t size, void *data)
> +{
> + struct vfio_region_info *info = vbasedev->reginfo[index];
same here.
The rest looks good.
Thanks,
C.
> + int ret;
> +
> + if (info == NULL) {
> + ret = vfio_device_get_region_info(vbasedev, index, &info);
> + if (ret != 0) {
> + return ret;
> + }
> + }
> +
> + ret = pwrite(vbasedev->fd, data, size, info->offset + off);
> +
> + return ret < 0 ? -errno : ret;
> +}
> +
> static VFIODeviceIOOps vfio_device_io_ops_ioctl = {
> .device_feature = vfio_device_io_device_feature,
> .get_region_info = vfio_device_io_get_region_info,
> .get_irq_info = vfio_device_io_get_irq_info,
> .set_irqs = vfio_device_io_set_irqs,
> + .region_read = vfio_device_io_region_read,
> + .region_write = vfio_device_io_region_write,
> };
> diff --git a/hw/vfio/pci.c b/hw/vfio/pci.c
> index 1aeb4d91d2..5e811d5d6a 100644
> --- a/hw/vfio/pci.c
> +++ b/hw/vfio/pci.c
> @@ -918,18 +918,22 @@ static void vfio_pci_load_rom(VFIOPCIDevice *vdev)
> memset(vdev->rom, 0xff, size);
>
> while (size) {
> - bytes = pread(vbasedev->fd, vdev->rom + off,
> - size, vdev->rom_offset + off);
> + bytes = vbasedev->io_ops->region_read(vbasedev,
> + VFIO_PCI_ROM_REGION_INDEX,
> + off, size, vdev->rom + off);
> +
> if (bytes == 0) {
> break;
> } else if (bytes > 0) {
> off += bytes;
> size -= bytes;
> } else {
> - if (errno == EINTR || errno == EAGAIN) {
> + if (bytes == -EINTR || bytes == -EAGAIN) {
> continue;
> }
> - error_report("vfio: Error reading device ROM: %m");
> + error_report("vfio: Error reading device ROM: %s",
> + strreaderror(bytes));
> +
> break;
> }
> }
> @@ -969,22 +973,18 @@ static void vfio_pci_load_rom(VFIOPCIDevice *vdev)
> static int vfio_pci_config_space_read(VFIOPCIDevice *vdev, off_t offset,
> uint32_t size, void *data)
> {
> - ssize_t ret;
> -
> - ret = pread(vdev->vbasedev.fd, data, size, vdev->config_offset + offset);
> -
> - return ret < 0 ? -errno : (int)ret;
> + return vdev->vbasedev.io_ops->region_read(&vdev->vbasedev,
> + VFIO_PCI_CONFIG_REGION_INDEX,
> + offset, size, data);
> }
>
> /* "Raw" write of underlying config space. */
> static int vfio_pci_config_space_write(VFIOPCIDevice *vdev, off_t offset,
> uint32_t size, void *data)
> {
> - ssize_t ret;
> -
> - ret = pwrite(vdev->vbasedev.fd, data, size, vdev->config_offset + offset);
> -
> - return ret < 0 ? -errno : (int)ret;
> + return vdev->vbasedev.io_ops->region_write(&vdev->vbasedev,
> + VFIO_PCI_CONFIG_REGION_INDEX,
> + offset, size, data);
> }
>
> static uint64_t vfio_rom_read(void *opaque, hwaddr addr, unsigned size)
> diff --git a/hw/vfio/region.c b/hw/vfio/region.c
> index ef2630cac3..34752c3f65 100644
> --- a/hw/vfio/region.c
> +++ b/hw/vfio/region.c
> @@ -45,6 +45,7 @@ void vfio_region_write(void *opaque, hwaddr addr,
> uint32_t dword;
> uint64_t qword;
> } buf;
> + int ret;
>
> switch (size) {
> case 1:
> @@ -64,11 +65,13 @@ void vfio_region_write(void *opaque, hwaddr addr,
> break;
> }
>
> - if (pwrite(vbasedev->fd, &buf, size, region->fd_offset + addr) != size) {
> + ret = vbasedev->io_ops->region_write(vbasedev, region->nr,
> + addr, size, &buf);
> + if (ret != size) {
> error_report("%s(%s:region%d+0x%"HWADDR_PRIx", 0x%"PRIx64
> - ",%d) failed: %m",
> + ",%d) failed: %s",
> __func__, vbasedev->name, region->nr,
> - addr, data, size);
> + addr, data, size, strwriteerror(ret));
> }
>
> trace_vfio_region_write(vbasedev->name, region->nr, addr, data, size);
> @@ -96,11 +99,13 @@ uint64_t vfio_region_read(void *opaque,
> uint64_t qword;
> } buf;
> uint64_t data = 0;
> + int ret;
>
> - if (pread(vbasedev->fd, &buf, size, region->fd_offset + addr) != size) {
> - error_report("%s(%s:region%d+0x%"HWADDR_PRIx", %d) failed: %m",
> + ret = vbasedev->io_ops->region_read(vbasedev, region->nr, addr, size, &buf);
> + if (ret != size) {
> + error_report("%s(%s:region%d+0x%"HWADDR_PRIx", %d) failed: %s",
> __func__, vbasedev->name, region->nr,
> - addr, size);
> + addr, size, strreaderror(ret));
> return (uint64_t)-1;
> }
> switch (size) {
> diff --git a/include/hw/vfio/vfio-device.h b/include/hw/vfio/vfio-device.h
> index b4a28c2a54..d3ab13ca6a 100644
> --- a/include/hw/vfio/vfio-device.h
> +++ b/include/hw/vfio/vfio-device.h
> @@ -178,6 +178,24 @@ struct VFIODeviceIOOps {
> * Configure IRQs as defined by @irqs.
> */
> int (*set_irqs)(VFIODevice *vdev, struct vfio_irq_set *irqs);
> +
> + /**
> + * @region_read
> + *
> + * Read @size bytes from the region @nr at offset @off into the buffer
> + * @data.
> + */
> + int (*region_read)(VFIODevice *vdev, uint8_t nr, off_t off, uint32_t size,
> + void *data);
> +
> + /**
> + * @region_write
> + *
> + * Write @size bytes to the region @nr at offset @off from the buffer
> + * @data.
> + */
> + int (*region_write)(VFIODevice *vdev, uint8_t nr, off_t off, uint32_t size,
> + void *data);
> };
>
> int vfio_device_get_region_info(VFIODevice *vbasedev, int index,
next prev parent reply other threads:[~2025-05-05 12:40 UTC|newest]
Thread overview: 41+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-04-30 19:39 [PATCH v2 00/15] vfio: preparation for vfio-user John Levon
2025-04-30 19:39 ` [PATCH v2 01/15] vfio: add vfio_prepare_device() John Levon
2025-05-05 8:35 ` Cédric Le Goater
2025-04-30 19:39 ` [PATCH v2 02/15] vfio: add vfio_unprepare_device() John Levon
2025-05-05 9:18 ` Cédric Le Goater
2025-04-30 19:39 ` [PATCH v2 03/15] vfio: add vfio_attach_device_by_iommu_type() John Levon
2025-04-30 19:39 ` [PATCH v2 04/15] vfio: add vfio_device_get_irq_info() helper John Levon
2025-05-01 11:53 ` Anthony Krowiak
2025-05-05 9:19 ` Cédric Le Goater
2025-05-06 11:38 ` John Levon
2025-05-06 12:27 ` Cédric Le Goater
2025-05-06 12:35 ` John Levon
2025-04-30 19:39 ` [PATCH v2 05/15] vfio: consistently handle return value for helpers John Levon
2025-05-05 9:32 ` Cédric Le Goater
2025-04-30 19:39 ` [PATCH v2 06/15] include/qemu: add strread/writeerror() John Levon
2025-05-05 9:37 ` Cédric Le Goater
2025-04-30 19:39 ` [PATCH v2 07/15] vfio: add vfio_pci_config_space_read/write() John Levon
2025-05-05 9:45 ` Cédric Le Goater
2025-04-30 19:39 ` [PATCH v2 08/15] vfio: add unmap_all flag to DMA unmap callback John Levon
2025-05-05 12:06 ` Cédric Le Goater
2025-05-05 13:26 ` John Levon
2025-05-05 21:05 ` Cédric Le Goater
2025-04-30 19:39 ` [PATCH v2 09/15] vfio: implement unmap all for DMA unmap callbacks John Levon
2025-05-05 11:28 ` Cédric Le Goater
2025-05-07 11:47 ` John Levon
2025-04-30 19:39 ` [PATCH v2 10/15] vfio: add device IO ops vector John Levon
2025-05-05 12:21 ` Cédric Le Goater
2025-05-06 10:01 ` Cédric Le Goater
2025-04-30 19:39 ` [PATCH v2 11/15] vfio: add region info cache John Levon
2025-05-05 12:26 ` Cédric Le Goater
2025-04-30 19:40 ` [PATCH v2 12/15] vfio: add read/write to device IO ops vector John Levon
2025-05-05 12:39 ` Cédric Le Goater [this message]
2025-04-30 19:40 ` [PATCH v2 13/15] vfio: add vfio-pci-base class John Levon
2025-05-05 12:42 ` Cédric Le Goater
2025-04-30 19:40 ` [PATCH v2 14/15] vfio/container: pass listener_begin/commit callbacks John Levon
2025-05-05 12:43 ` Cédric Le Goater
2025-04-30 19:40 ` [PATCH v2 15/15] vfio/container: pass MemoryRegion to DMA operations John Levon
2025-05-05 12:46 ` Cédric Le Goater
2025-05-07 13:58 ` John Levon
2025-05-09 10:28 ` Cédric Le Goater
2025-05-05 12:51 ` [PATCH v2 00/15] vfio: preparation for vfio-user Cédric Le Goater
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=b573f499-4083-4aac-9ee0-04e6028e4cf3@redhat.com \
--to=clg@redhat.com \
--cc=akrowiak@linux.ibm.com \
--cc=alex.williamson@redhat.com \
--cc=armbru@redhat.com \
--cc=david@redhat.com \
--cc=farman@linux.ibm.com \
--cc=jjherne@linux.ibm.com \
--cc=john.levon@nutanix.com \
--cc=mjrosato@linux.ibm.com \
--cc=mst@redhat.com \
--cc=pasic@linux.ibm.com \
--cc=pbonzini@redhat.com \
--cc=peterx@redhat.com \
--cc=philmd@linaro.org \
--cc=qemu-devel@nongnu.org \
--cc=qemu-s390x@nongnu.org \
--cc=sgarzare@redhat.com \
--cc=thuth@redhat.com \
--cc=tomitamoeko@gmail.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
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).