From: Tomita Moeko <tomitamoeko@gmail.com>
To: John Levon <john.levon@nutanix.com>, qemu-devel@nongnu.org
Cc: "Tony Krowiak" <akrowiak@linux.ibm.com>,
"Stefano Garzarella" <sgarzare@redhat.com>,
"Cédric Le Goater" <clg@redhat.com>,
"Peter Xu" <peterx@redhat.com>, "Thomas Huth" <thuth@redhat.com>,
"Matthew Rosato" <mjrosato@linux.ibm.com>,
"David Hildenbrand" <david@redhat.com>,
"Michael S. Tsirkin" <mst@redhat.com>,
"Alex Williamson" <alex.williamson@redhat.com>,
qemu-s390x@nongnu.org,
"Philippe Mathieu-Daudé" <philmd@linaro.org>,
"Halil Pasic" <pasic@linux.ibm.com>,
"Jason Herne" <jjherne@linux.ibm.com>,
"Paolo Bonzini" <pbonzini@redhat.com>,
"Eric Farman" <farman@linux.ibm.com>
Subject: Re: [PATCH 11/14] vfio: add vfio_pci_config_space_read/write()
Date: Wed, 9 Apr 2025 23:51:09 +0800 [thread overview]
Message-ID: <ed4d5a1e-61dc-4041-a24d-c1a0bd49fa3e@gmail.com> (raw)
In-Reply-To: <20250409134814.478903-12-john.levon@nutanix.com>
On 4/9/25 21:48, John Levon wrote:
> Add these helpers that access config space and return an -errno style
> return.
>
> Signed-off-by: John Levon <john.levon@nutanix.com>
> ---
> hw/vfio/pci.c | 134 ++++++++++++++++++++++++++++++++++----------------
> 1 file changed, 91 insertions(+), 43 deletions(-)
>
> diff --git a/hw/vfio/pci.c b/hw/vfio/pci.c
> index ddeee33aa9..c3842d2f8d 100644
> --- a/hw/vfio/pci.c
> +++ b/hw/vfio/pci.c
> @@ -964,6 +964,28 @@ static void vfio_pci_load_rom(VFIOPCIDevice *vdev)
> }
> }
>
> +/* "Raw" read of underlying config space. */
> +static int vfio_pci_config_space_read(VFIOPCIDevice *vdev, off_t offset,
> + uint32_t size, void *data)
Returning ssize_t here might be better here to avoid casting issues,
though we would never read/write something exceeds INT32_MAX.
Thanks,
Moeko
> +{
> + ssize_t ret;
> +
> + ret = pread(vdev->vbasedev.fd, data, size, vdev->config_offset + offset);
> +
> + return ret < 0 ? -errno : (int)ret;
> +}
> +
> +/* "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;
> +}
> +
> static uint64_t vfio_rom_read(void *opaque, hwaddr addr, unsigned size)
> {
> VFIOPCIDevice *vdev = opaque;
> @@ -1016,10 +1038,9 @@ static const MemoryRegionOps vfio_rom_ops = {
>
> static void vfio_pci_size_rom(VFIOPCIDevice *vdev)
> {
> + VFIODevice *vbasedev = &vdev->vbasedev;
> uint32_t orig, size = cpu_to_le32((uint32_t)PCI_ROM_ADDRESS_MASK);
> - off_t offset = vdev->config_offset + PCI_ROM_ADDRESS;
> char *name;
> - int fd = vdev->vbasedev.fd;
>
> if (vdev->pdev.romfile || !vdev->pdev.rom_bar) {
> /* Since pci handles romfile, just print a message and return */
> @@ -1036,11 +1057,12 @@ static void vfio_pci_size_rom(VFIOPCIDevice *vdev)
> * Use the same size ROM BAR as the physical device. The contents
> * will get filled in later when the guest tries to read it.
> */
> - if (pread(fd, &orig, 4, offset) != 4 ||
> - pwrite(fd, &size, 4, offset) != 4 ||
> - pread(fd, &size, 4, offset) != 4 ||
> - pwrite(fd, &orig, 4, offset) != 4) {
> - error_report("%s(%s) failed: %m", __func__, vdev->vbasedev.name);
> + if (vfio_pci_config_space_read(vdev, PCI_ROM_ADDRESS, 4, &orig) != 4 ||
> + vfio_pci_config_space_write(vdev, PCI_ROM_ADDRESS, 4, &size) != 4 ||
> + vfio_pci_config_space_read(vdev, PCI_ROM_ADDRESS, 4, &size) != 4 ||
> + vfio_pci_config_space_write(vdev, PCI_ROM_ADDRESS, 4, &orig) != 4) {
> +
> + error_report("%s(%s) ROM access failed", __func__, vbasedev->name);
> return;
> }
>
> @@ -1220,6 +1242,7 @@ static void vfio_sub_page_bar_update_mapping(PCIDevice *pdev, int bar)
> uint32_t vfio_pci_read_config(PCIDevice *pdev, uint32_t addr, int len)
> {
> VFIOPCIDevice *vdev = VFIO_PCI_BASE(pdev);
> + VFIODevice *vbasedev = &vdev->vbasedev;
> uint32_t emu_bits = 0, emu_val = 0, phys_val = 0, val;
>
> memcpy(&emu_bits, vdev->emulated_config_bits + addr, len);
> @@ -1232,12 +1255,13 @@ uint32_t vfio_pci_read_config(PCIDevice *pdev, uint32_t addr, int len)
> if (~emu_bits & (0xffffffffU >> (32 - len * 8))) {
> ssize_t ret;
>
> - ret = pread(vdev->vbasedev.fd, &phys_val, len,
> - vdev->config_offset + addr);
> + ret = vfio_pci_config_space_read(vdev, addr, len, &phys_val);
> if (ret != len) {
> - error_report("%s(%s, 0x%x, 0x%x) failed: %m",
> - __func__, vdev->vbasedev.name, addr, len);
> - return -errno;
> + const char *err = ret < 0 ? strerror(-ret) : "short read";
> +
> + error_report("%s(%s, 0x%x, 0x%x) failed: %s",
> + __func__, vbasedev->name, addr, len, err);
> + return -1;
> }
> phys_val = le32_to_cpu(phys_val);
> }
> @@ -1253,15 +1277,19 @@ void vfio_pci_write_config(PCIDevice *pdev,
> uint32_t addr, uint32_t val, int len)
> {
> VFIOPCIDevice *vdev = VFIO_PCI_BASE(pdev);
> + VFIODevice *vbasedev = &vdev->vbasedev;
> uint32_t val_le = cpu_to_le32(val);
> + int ret;
>
> trace_vfio_pci_write_config(vdev->vbasedev.name, addr, val, len);
>
> /* Write everything to VFIO, let it filter out what we can't write */
> - if (pwrite(vdev->vbasedev.fd, &val_le, len, vdev->config_offset + addr)
> - != len) {
> - error_report("%s(%s, 0x%x, 0x%x, 0x%x) failed: %m",
> - __func__, vdev->vbasedev.name, addr, val, len);
> + ret = vfio_pci_config_space_write(vdev, addr, len, &val_le);
> + if (ret != len) {
> + const char *err = ret < 0 ? strerror(-ret) : "short write";
> +
> + error_report("%s(%s, 0x%x, 0x%x, 0x%x) failed: %s",
> + __func__, vbasedev->name, addr, val, len, err);
> }
>
> /* MSI/MSI-X Enabling/Disabling */
> @@ -1349,9 +1377,12 @@ static bool vfio_msi_setup(VFIOPCIDevice *vdev, int pos, Error **errp)
> int ret, entries;
> Error *err = NULL;
>
> - if (pread(vdev->vbasedev.fd, &ctrl, sizeof(ctrl),
> - vdev->config_offset + pos + PCI_CAP_FLAGS) != sizeof(ctrl)) {
> - error_setg_errno(errp, errno, "failed reading MSI PCI_CAP_FLAGS");
> + ret = vfio_pci_config_space_read(vdev, pos + PCI_CAP_FLAGS,
> + sizeof(ctrl), &ctrl);
> + if (ret != sizeof(ctrl)) {
> + const char *errmsg = ret < 0 ? strerror(-ret) : "short read";
> +
> + error_setg(errp, "failed reading MSI PCI_CAP_FLAGS: %s", errmsg);
> return false;
> }
> ctrl = le16_to_cpu(ctrl);
> @@ -1558,30 +1589,39 @@ static bool vfio_msix_early_setup(VFIOPCIDevice *vdev, Error **errp)
> uint8_t pos;
> uint16_t ctrl;
> uint32_t table, pba;
> - int ret, fd = vdev->vbasedev.fd;
> struct vfio_irq_info irq_info;
> VFIOMSIXInfo *msix;
> + int ret;
>
> pos = pci_find_capability(&vdev->pdev, PCI_CAP_ID_MSIX);
> if (!pos) {
> return true;
> }
>
> - if (pread(fd, &ctrl, sizeof(ctrl),
> - vdev->config_offset + pos + PCI_MSIX_FLAGS) != sizeof(ctrl)) {
> - error_setg_errno(errp, errno, "failed to read PCI MSIX FLAGS");
> + ret = vfio_pci_config_space_read(vdev, pos + PCI_MSIX_FLAGS,
> + sizeof(ctrl), &ctrl);
> + if (ret != sizeof(ctrl)) {
> + const char *err = ret < 0 ? strerror(-ret) : "short read";
> +
> + error_setg(errp, "failed to read PCI MSIX FLAGS: %s", err);
> return false;
> }
>
> - if (pread(fd, &table, sizeof(table),
> - vdev->config_offset + pos + PCI_MSIX_TABLE) != sizeof(table)) {
> - error_setg_errno(errp, errno, "failed to read PCI MSIX TABLE");
> + ret = vfio_pci_config_space_read(vdev, pos + PCI_MSIX_TABLE,
> + sizeof(table), &table);
> + if (ret != sizeof(table)) {
> + const char *err = ret < 0 ? strerror(-ret) : "short read";
> +
> + error_setg(errp, "failed to read PCI MSIX TABLE: %s", err);
> return false;
> }
>
> - if (pread(fd, &pba, sizeof(pba),
> - vdev->config_offset + pos + PCI_MSIX_PBA) != sizeof(pba)) {
> - error_setg_errno(errp, errno, "failed to read PCI MSIX PBA");
> + ret = vfio_pci_config_space_read(vdev, pos + PCI_MSIX_PBA,
> + sizeof(pba), &pba);
> + if (ret != sizeof(pba)) {
> + const char *err = ret < 0 ? strerror(-ret) : "short read";
> +
> + error_setg(errp, "failed to read PCI MSIX PBA: %s", err);
> return false;
> }
>
> @@ -1741,10 +1781,12 @@ static void vfio_bar_prepare(VFIOPCIDevice *vdev, int nr)
> }
>
> /* Determine what type of BAR this is for registration */
> - ret = pread(vdev->vbasedev.fd, &pci_bar, sizeof(pci_bar),
> - vdev->config_offset + PCI_BASE_ADDRESS_0 + (4 * nr));
> + ret = vfio_pci_config_space_read(vdev, PCI_BASE_ADDRESS_0 + (4 * nr),
> + sizeof(pci_bar), &pci_bar);
> if (ret != sizeof(pci_bar)) {
> - error_report("vfio: Failed to read BAR %d (%m)", nr);
> + const char *err = ret < 0 ? strerror(-ret) : "short read";
> +
> + error_report("vfio: Failed to read BAR %d: %s", nr, err);
> return;
> }
>
> @@ -2448,21 +2490,25 @@ void vfio_pci_pre_reset(VFIOPCIDevice *vdev)
>
> void vfio_pci_post_reset(VFIOPCIDevice *vdev)
> {
> + VFIODevice *vbasedev = &vdev->vbasedev;
> Error *err = NULL;
> - int nr;
> + int ret, nr;
>
> if (!vfio_intx_enable(vdev, &err)) {
> error_reportf_err(err, VFIO_MSG_PREFIX, vdev->vbasedev.name);
> }
>
> for (nr = 0; nr < PCI_NUM_REGIONS - 1; ++nr) {
> - off_t addr = vdev->config_offset + PCI_BASE_ADDRESS_0 + (4 * nr);
> + off_t addr = PCI_BASE_ADDRESS_0 + (4 * nr);
> uint32_t val = 0;
> uint32_t len = sizeof(val);
>
> - if (pwrite(vdev->vbasedev.fd, &val, len, addr) != len) {
> - error_report("%s(%s) reset bar %d failed: %m", __func__,
> - vdev->vbasedev.name, nr);
> + ret = vfio_pci_config_space_write(vdev, addr, len, &val);
> + if (ret != len) {
> + const char *errmsg = ret < 0 ? strerror(-ret) : "short write";
> +
> + error_report("%s(%s) reset bar %d failed: %s", __func__,
> + vbasedev->name, nr, errmsg);
> }
> }
>
> @@ -3099,6 +3145,7 @@ static void vfio_realize(PCIDevice *pdev, Error **errp)
> int i, ret;
> char uuid[UUID_STR_LEN];
> g_autofree char *name = NULL;
> + size_t config_space_size;
>
> if (vbasedev->fd < 0 && !vbasedev->sysfsdev) {
> if (!(~vdev->host.domain || ~vdev->host.bus ||
> @@ -3153,13 +3200,14 @@ static void vfio_realize(PCIDevice *pdev, Error **errp)
> goto error;
> }
>
> + config_space_size = MIN(pci_config_size(&vdev->pdev), vdev->config_size);
> +
> /* Get a copy of config space */
> - ret = pread(vbasedev->fd, vdev->pdev.config,
> - MIN(pci_config_size(&vdev->pdev), vdev->config_size),
> - vdev->config_offset);
> - if (ret < (int)MIN(pci_config_size(&vdev->pdev), vdev->config_size)) {
> - ret = ret < 0 ? -errno : -EFAULT;
> - error_setg_errno(errp, -ret, "failed to read device config space");
> + ret = vfio_pci_config_space_read(vdev, 0, config_space_size,
> + vdev->pdev.config);
> + if (ret < (int)config_space_size) {
> + ret = ret < 0 ? -ret : EFAULT;
> + error_setg_errno(errp, ret, "failed to read device config space");
> goto error;
> }
>
next prev parent reply other threads:[~2025-04-09 15:52 UTC|newest]
Thread overview: 53+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-04-09 13:48 [PATCH 00/14] vfio: preparation for vfio-user John Levon
2025-04-09 13:48 ` [PATCH 01/14] vfio: refactor out vfio_interrupt_setup() John Levon
2025-04-23 12:20 ` Cédric Le Goater
2025-04-09 13:48 ` [PATCH 02/14] vfio: refactor out vfio_pci_config_setup() John Levon
2025-04-09 15:38 ` Tomita Moeko
2025-04-09 15:41 ` John Levon
2025-04-09 13:48 ` [PATCH 03/14] vfio: add vfio_prepare_device() John Levon
2025-04-23 12:45 ` Cédric Le Goater
2025-04-23 13:19 ` John Levon
2025-04-09 13:48 ` [PATCH 04/14] vfio: add vfio_attach_device_by_iommu_type() John Levon
2025-04-23 13:25 ` Cédric Le Goater
2025-04-09 13:48 ` [PATCH 05/14] vfio/container: pass listener_begin/commit callbacks John Levon
2025-04-23 13:45 ` Cédric Le Goater
2025-04-24 16:24 ` Cédric Le Goater
2025-04-24 16:28 ` John Levon
2025-04-24 16:35 ` Cédric Le Goater
2025-04-09 13:48 ` [PATCH 06/14] vfio: add flags parameter to DMA unmap callback John Levon
2025-04-09 13:48 ` [PATCH 07/14] vfio: specify VFIO_DMA_UNMAP_FLAG_ALL to callback John Levon
2025-04-23 17:01 ` Cédric Le Goater
2025-04-23 17:17 ` John Levon
2025-04-24 17:16 ` Cédric Le Goater
2025-04-24 19:35 ` John Levon
2025-04-28 11:41 ` Cédric Le Goater
2025-04-09 13:48 ` [PATCH 08/14] vfio: add vfio-pci-base class John Levon
2025-04-24 15:17 ` Cédric Le Goater
2025-04-24 21:52 ` John Levon
2025-04-25 12:46 ` Cédric Le Goater
2025-04-25 13:01 ` John Levon
2025-04-28 12:53 ` Cédric Le Goater
2025-04-09 13:48 ` [PATCH 09/14] vfio: add vfio_device_get_irq_info() helper John Levon
2025-04-23 17:16 ` Cédric Le Goater
2025-04-09 13:48 ` [PATCH 10/14] vfio: consistently handle return value for helpers John Levon
2025-04-24 15:19 ` Cédric Le Goater
2025-04-09 13:48 ` [PATCH 11/14] vfio: add vfio_pci_config_space_read/write() John Levon
2025-04-09 15:51 ` Tomita Moeko [this message]
2025-04-09 15:54 ` John Levon
2025-04-09 16:30 ` Tomita Moeko
2025-04-24 15:59 ` Cédric Le Goater
2025-04-24 16:06 ` Cédric Le Goater
2025-04-09 13:48 ` [PATCH 12/14] vfio: add region info cache John Levon
2025-04-24 16:08 ` Cédric Le Goater
2025-04-24 16:26 ` John Levon
2025-04-28 15:16 ` Cédric Le Goater
2025-04-28 15:26 ` John Levon
2025-04-28 15:39 ` Cédric Le Goater
2025-04-28 16:09 ` John Levon
2025-04-29 22:41 ` John Levon
2025-04-09 13:48 ` [PATCH 13/14] vfio: add device IO ops vector John Levon
2025-04-24 16:18 ` Cédric Le Goater
2025-04-09 13:48 ` [PATCH 14/14] vfio/container: pass MemoryRegion to DMA operations John Levon
2025-04-24 16:32 ` Cédric Le Goater
2025-04-24 17:49 ` John Levon
2025-04-25 7:59 ` [PATCH 00/14] 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=ed4d5a1e-61dc-4041-a24d-c1a0bd49fa3e@gmail.com \
--to=tomitamoeko@gmail.com \
--cc=akrowiak@linux.ibm.com \
--cc=alex.williamson@redhat.com \
--cc=clg@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 \
/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).