Skip to contentSkip to conversation

vfio/common: Introduce vfio_set_irq_signaling helper

PATCH Superseded Eric Auger 3 participants 5 messages Apr 9, 2019 – May 20, 2019

v1 of 5 Superseded Latest is v5 →

lore
Share & tools
lore

Eric Auger

author
#0 lore raw

PATCH 1/1 vfio/common: Introduce vfio_set_irq_signaling helper

The code used to assign an interrupt index/subindex to an eventfd is duplicated many times. Let's introduce an helper that allows to set/unset the signaling for an ACTION_TRIGGER or ACTION_UNMASK action.

---

This is a follow-up to
[PATCH v2 0/2] vfio-pci: Introduce vfio_set_event_handler(). It looks to me that introducing vfio_set_irq_signaling() has more benefits in term of code reduction and the helper abstraction looks cleaner.

---
 hw/vfio/common.c              |  61 +++++++++
 hw/vfio/pci.c                 | 224 ++++++++--------------------------
 hw/vfio/platform.c            |  55 +++------
 include/hw/vfio/vfio-common.h |   2 +
 4 files changed, 134 insertions(+), 208 deletions(-)
hw/vfio/common.c +61 -0
index 4374cc6176..f88fd10ca3 100644
@@ -95,6 +95,67 @@ void vfio_mask_single_irqindex(VFIODevice *vbasedev, int index)
     ioctl(vbasedev->fd, VFIO_DEVICE_SET_IRQS, &irq_set);
 }
 
+static inline const char *action_to_str(int action)
+{
+    switch (action) {
+    case VFIO_IRQ_SET_ACTION_MASK:
+        return "MASK";
+    case VFIO_IRQ_SET_ACTION_UNMASK:
+        return "UNMASK";
+    case VFIO_IRQ_SET_ACTION_TRIGGER:
+        return "TRIGGER";
+    default:
+        return "UNKNOWN ACTION";
+    }
+}
+
+int vfio_set_irq_signaling(VFIODevice *vbasedev, int index, int subindex,
+                           int action, int fd, Error **errp)
+{
+    struct vfio_irq_info irq_info = { .argsz = sizeof(irq_info),
+                                      .index = index };
+    struct vfio_irq_set *irq_set;
+    int argsz, ret = 0;
+    int32_t *pfd;
+
+    ret = ioctl(vbasedev->fd, VFIO_DEVICE_GET_IRQ_INFO, &irq_info);
+    if (ret < 0) {
+        error_setg_errno(errp, errno, "index %d does not exist", index);
+        goto error;
+    }
+    if (irq_info.count < subindex + 1) {
+        error_setg_errno(errp, errno, "subindex %d does not exist", subindex);
+        goto error;
+    }
+
+    argsz = sizeof(*irq_set) + sizeof(*pfd);
+
+    irq_set = g_malloc0(argsz);
+    irq_set->argsz = argsz;
+    irq_set->flags = VFIO_IRQ_SET_DATA_EVENTFD | action;
+    irq_set->index = index;
+    irq_set->start = subindex;
+    irq_set->count = 1;
+    pfd = (int32_t *)&irq_set->data;
+    *pfd = fd;
+
+    ret = ioctl(vbasedev->fd, VFIO_DEVICE_SET_IRQS, irq_set);
+
+    g_free(irq_set);
+
+    if (ret) {
+        error_setg_errno(errp, -ret, "VFIO_DEVICE_SET_IRQS failure");
+        goto error;
+    }
+    return 0;
+error:
+    error_prepend(errp,
+                  "Failed to %s %s eventfd signaling for interrupt [%d,%d]: ",
+                  fd < 0 ? "tear down" : "set up", action_to_str(action),
+                  index, subindex);
+    return ret;
+}
+
 /*
  * IO Port/MMIO - Beware of the endians, VFIO is always little endian
  */
hw/vfio/pci.c +51 -173

332 lines not on this page · raw message

hw/vfio/platform.c +20 -35

72 lines not on this page · raw message

include/hw/vfio/vfio-common.h +2 -0
index 1155b79678..686d99ff8c 100644
@@ -167,6 +167,8 @@ void vfio_put_base_device(VFIODevice *vbasedev);
 void vfio_disable_irqindex(VFIODevice *vbasedev, int index);
 void vfio_unmask_single_irqindex(VFIODevice *vbasedev, int index);
 void vfio_mask_single_irqindex(VFIODevice *vbasedev, int index);
+int vfio_set_irq_signaling(VFIODevice *vbasedev, int index, int subindex,
+                           int action, int fd, Error **errp);
 void vfio_region_write(void *opaque, hwaddr addr,
                            uint64_t data, unsigned size);
 uint64_t vfio_region_read(void *opaque,
Signature · 1 line
2.20.1
Reviewed-by on v1 1/1
#1 lore raw
Quoted 4 lines · On Tue, 9 Apr 2019 17:58:31 +0200 Eric Auger <[email protected]> wrote:

The code used to assign an interrupt index/subindex to an eventfd is duplicated many times. Let's introduce an helper that allows to set/unset the signaling for an ACTION_TRIGGER or ACTION_UNMASK action.

I like that, and it looks like ccw can use the new function as well. (I can do a patch on top.)

Quoted 10 lines

---

This is a follow-up to
[PATCH v2 0/2] vfio-pci: Introduce vfio_set_event_handler(). It looks to me that introducing vfio_set_irq_signaling() has more benefits in term of code reduction and the helper abstraction looks cleaner.

Quoted 6 lines
---
 hw/vfio/common.c              |  61 +++++++++
 hw/vfio/pci.c                 | 224 ++++++++--------------------------
 hw/vfio/platform.c            |  55 +++------
 include/hw/vfio/vfio-common.h |   2 +
 4 files changed, 134 insertions(+), 208 deletions(-)
author
#2 lore raw

Hi Cornelia

Quoted 10 lines · Cornelia Huck
Quoted 4 lines · On Tue, 9 Apr 2019 17:58:31 +0200 Eric Auger <[email protected]> wrote:

The code used to assign an interrupt index/subindex to an eventfd is duplicated many times. Let's introduce an helper that allows to set/unset the signaling for an ACTION_TRIGGER or ACTION_UNMASK action.

I like that, and it looks like ccw can use the new function as well. (I can do a patch on top.)

Thanks! Yes, feel free to proceed with the ccw patch once this gets merged.

Quoted 18 lines
Quoted 11 lines

---

This is a follow-up to
[PATCH v2 0/2] vfio-pci: Introduce vfio_set_event_handler(). It looks to me that introducing vfio_set_irq_signaling() has more benefits in term of code reduction and the helper abstraction looks cleaner.

Quoted 6 lines
---
 hw/vfio/common.c              |  61 +++++++++
 hw/vfio/pci.c                 | 224 ++++++++--------------------------
 hw/vfio/platform.c            |  55 +++------
 include/hw/vfio/vfio-common.h |   2 +
 4 files changed, 134 insertions(+), 208 deletions(-)
Quoted 1 line

Thanks

Eric

Quoted 1 line
#3 lore raw
In reply to Eric Auger
Quoted 64 lines · On Tue, 9 Apr 2019 17:58:31 +0200 Eric Auger <[email protected]> wrote:

The code used to assign an interrupt index/subindex to an eventfd is duplicated many times. Let's introduce an helper that allows to set/unset the signaling for an ACTION_TRIGGER or ACTION_UNMASK action.

---

This is a follow-up to
[PATCH v2 0/2] vfio-pci: Introduce vfio_set_event_handler(). It looks to me that introducing vfio_set_irq_signaling() has more benefits in term of code reduction and the helper abstraction looks cleaner.

---
 hw/vfio/common.c              |  61 +++++++++
 hw/vfio/pci.c                 | 224 ++++++++--------------------------
 hw/vfio/platform.c            |  55 +++------
 include/hw/vfio/vfio-common.h |   2 +
 4 files changed, 134 insertions(+), 208 deletions(-)
hw/vfio/common.c +35 -0
index 4374cc6176..f88fd10ca3 100644
@@ -95,6 +95,67 @@ void vfio_mask_single_irqindex(VFIODevice *vbasedev, int index)
     ioctl(vbasedev->fd, VFIO_DEVICE_SET_IRQS, &irq_set);
 }
 
+static inline const char *action_to_str(int action)
+{
+    switch (action) {
+    case VFIO_IRQ_SET_ACTION_MASK:
+        return "MASK";
+    case VFIO_IRQ_SET_ACTION_UNMASK:
+        return "UNMASK";
+    case VFIO_IRQ_SET_ACTION_TRIGGER:
+        return "TRIGGER";
+    default:
+        return "UNKNOWN ACTION";
+    }
+}
+
+int vfio_set_irq_signaling(VFIODevice *vbasedev, int index, int subindex,
+                           int action, int fd, Error **errp)
+{
+    struct vfio_irq_info irq_info = { .argsz = sizeof(irq_info),
+                                      .index = index };
+    struct vfio_irq_set *irq_set;
+    int argsz, ret = 0;
+    int32_t *pfd;
+
+    ret = ioctl(vbasedev->fd, VFIO_DEVICE_GET_IRQ_INFO, &irq_info);
+    if (ret < 0) {
+        error_setg_errno(errp, errno, "index %d does not exist", index);
+        goto error;
+    }
+    if (irq_info.count < subindex + 1) {
+        error_setg_errno(errp, errno, "subindex %d does not exist", subindex);
+        goto error;
+    }
+
+    argsz = sizeof(*irq_set) + sizeof(*pfd);
+

hw/vfio/common.c void vfio_mask_single_irqindex(VFIODevice *vbasedev, int index) · line 142

Quoted 10 lines
hw/vfio/common.c +10 -0
+    irq_set = g_malloc0(argsz);
+    irq_set->argsz = argsz;
+    irq_set->flags = VFIO_IRQ_SET_DATA_EVENTFD | action;
+    irq_set->index = index;
+    irq_set->start = subindex;
+    irq_set->count = 1;
+    pfd = (int32_t *)&irq_set->data;
+    *pfd = fd;
+
+    ret = ioctl(vbasedev->fd, VFIO_DEVICE_SET_IRQS, irq_set);

Hi Eric,

Sorry for the long delay. While I like the code reduction and simplification, is it really acceptable that every SET_IRQS ioctl is now a GET_IRQ_INFO + SET_IRQS? Are we trying to protect against devices dynamically changing their interrupt support? Do we not trust the callers?

hw/vfio/common.c void vfio_mask_single_irqindex(VFIODevice *vbasedev, int index) · line 155

Quoted 13 lines
hw/vfio/common.c +13 -0
+
+    g_free(irq_set);
+
+    if (ret) {
+        error_setg_errno(errp, -ret, "VFIO_DEVICE_SET_IRQS failure");
+        goto error;
+    }
+    return 0;
+error:
+    error_prepend(errp,
+                  "Failed to %s %s eventfd signaling for interrupt [%d,%d]: ",
+                  fd < 0 ? "tear down" : "set up", action_to_str(action),
+                  index, subindex);

Maybe icing on the cake, but this leaves me wishing it printed "MSIX-3" rather than "[2,3]" for a PCI device ;)

Quoted 10 lines
hw/vfio/common.c +3 -0
+    return ret;
+}
+
 /*
  * IO Port/MMIO - Beware of the endians, VFIO is always little endian
  */
hw/vfio/pci.c +0 -0
index 504019c458..cd93ff6fa3 100644

[snip]

Quoted 10 lines
hw/vfio/pci.c +1 -5
@@ -2718,77 +2630,43 @@ static void vfio_req_notifier_handler(void *opaque)
 
 static void vfio_register_req_notifier(VFIOPCIDevice *vdev)
 {
-    struct vfio_irq_info irq_info = { .argsz = sizeof(irq_info),
-                                      .index = VFIO_PCI_REQ_IRQ_INDEX };
-    int argsz;
-    struct vfio_irq_set *irq_set;
-    int32_t *pfd;
+    Error *err = NULL;

hw/vfio/pci.c static void vfio_req_notifier_handler(void *opaque) · line 2734

Quoted 10 lines
hw/vfio/pci.c +1 -4
+    int32_t fd;
 
     if (!(vdev->features & VFIO_FEATURE_ENABLE_REQ)) {
         return;
     }
 
-    if (ioctl(vdev->vbasedev.fd,
-              VFIO_DEVICE_GET_IRQ_INFO, &irq_info) < 0 || irq_info.count < 1) {
-        return;
-    }

Here we used GET_IRQ_INFO to quietly only enable the request notifier when it's available, now it looks like this is no longer quiet if that support is unavailable. Is that intentional? Thanks,

Alex

Quoted 36 lines
hw/vfio/pci.c +6 -19
-
     if (event_notifier_init(&vdev->req_notifier, 0)) {
         error_report("vfio: Unable to init event notifier for device request");
         return;
     }
 
-    argsz = sizeof(*irq_set) + sizeof(*pfd);
+    fd = event_notifier_get_fd(&vdev->req_notifier);
+    qemu_set_fd_handler(fd, vfio_req_notifier_handler, NULL, vdev);
 
-    irq_set = g_malloc0(argsz);
-    irq_set->argsz = argsz;
-    irq_set->flags = VFIO_IRQ_SET_DATA_EVENTFD |
-                     VFIO_IRQ_SET_ACTION_TRIGGER;
-    irq_set->index = VFIO_PCI_REQ_IRQ_INDEX;
-    irq_set->start = 0;
-    irq_set->count = 1;
-    pfd = (int32_t *)&irq_set->data;
-
-    *pfd = event_notifier_get_fd(&vdev->req_notifier);
-    qemu_set_fd_handler(*pfd, vfio_req_notifier_handler, NULL, vdev);
-
-    if (ioctl(vdev->vbasedev.fd, VFIO_DEVICE_SET_IRQS, irq_set)) {
-        error_report("vfio: Failed to set up device request notification");
-        qemu_set_fd_handler(*pfd, NULL, NULL, vdev);
+    if (vfio_set_irq_signaling(&vdev->vbasedev, VFIO_PCI_REQ_IRQ_INDEX, 0,
+                           VFIO_IRQ_SET_ACTION_TRIGGER, fd, &err)) {
+        error_reportf_err(err, VFIO_MSG_PREFIX, vdev->vbasedev.name);
+        qemu_set_fd_handler(fd, NULL, NULL, vdev);
         event_notifier_cleanup(&vdev->req_notifier);
     } else {
         vdev->req_enabled = true;
     }
-
-    g_free(irq_set);
 }
author
#4 lore raw

Hi Alex,

Quoted 80 lines · Alex Williamson
Quoted 64 lines · On Tue, 9 Apr 2019 17:58:31 +0200 Eric Auger <[email protected]> wrote:

The code used to assign an interrupt index/subindex to an eventfd is duplicated many times. Let's introduce an helper that allows to set/unset the signaling for an ACTION_TRIGGER or ACTION_UNMASK action.

---

This is a follow-up to
[PATCH v2 0/2] vfio-pci: Introduce vfio_set_event_handler(). It looks to me that introducing vfio_set_irq_signaling() has more benefits in term of code reduction and the helper abstraction looks cleaner.

---
 hw/vfio/common.c              |  61 +++++++++
 hw/vfio/pci.c                 | 224 ++++++++--------------------------
 hw/vfio/platform.c            |  55 +++------
 include/hw/vfio/vfio-common.h |   2 +
 4 files changed, 134 insertions(+), 208 deletions(-)
hw/vfio/common.c +35 -0
index 4374cc6176..f88fd10ca3 100644
@@ -95,6 +95,67 @@ void vfio_mask_single_irqindex(VFIODevice *vbasedev, int index)
     ioctl(vbasedev->fd, VFIO_DEVICE_SET_IRQS, &irq_set);
 }
 
+static inline const char *action_to_str(int action)
+{
+    switch (action) {
+    case VFIO_IRQ_SET_ACTION_MASK:
+        return "MASK";
+    case VFIO_IRQ_SET_ACTION_UNMASK:
+        return "UNMASK";
+    case VFIO_IRQ_SET_ACTION_TRIGGER:
+        return "TRIGGER";
+    default:
+        return "UNKNOWN ACTION";
+    }
+}
+
+int vfio_set_irq_signaling(VFIODevice *vbasedev, int index, int subindex,
+                           int action, int fd, Error **errp)
+{
+    struct vfio_irq_info irq_info = { .argsz = sizeof(irq_info),
+                                      .index = index };
+    struct vfio_irq_set *irq_set;
+    int argsz, ret = 0;
+    int32_t *pfd;
+
+    ret = ioctl(vbasedev->fd, VFIO_DEVICE_GET_IRQ_INFO, &irq_info);
+    if (ret < 0) {
+        error_setg_errno(errp, errno, "index %d does not exist", index);
+        goto error;
+    }
+    if (irq_info.count < subindex + 1) {
+        error_setg_errno(errp, errno, "subindex %d does not exist", subindex);
+        goto error;
+    }
+
+    argsz = sizeof(*irq_set) + sizeof(*pfd);
+

hw/vfio/common.c void vfio_mask_single_irqindex(VFIODevice *vbasedev, int index) · line 142

Quoted 10 lines
hw/vfio/common.c +10 -0
+    irq_set = g_malloc0(argsz);
+    irq_set->argsz = argsz;
+    irq_set->flags = VFIO_IRQ_SET_DATA_EVENTFD | action;
+    irq_set->index = index;
+    irq_set->start = subindex;
+    irq_set->count = 1;
+    pfd = (int32_t *)&irq_set->data;
+    *pfd = fd;
+
+    ret = ioctl(vbasedev->fd, VFIO_DEVICE_SET_IRQS, irq_set);

Hi Eric,

Quoted 5 lines

Sorry for the long delay. While I like the code reduction and simplification, is it really acceptable that every SET_IRQS ioctl is now a GET_IRQ_INFO + SET_IRQS? Are we trying to protect against devices dynamically changing their interrupt support? Do we not trust the callers?

I agree this is generally not needed. I will remove the check.

Quoted 10 lines
Quoted 10 lines
hw/vfio/common.c +9 -0
+
+    g_free(irq_set);
+
+    if (ret) {
+        error_setg_errno(errp, -ret, "VFIO_DEVICE_SET_IRQS failure");
+        goto error;
+    }
+    return 0;
+error:
Quoted 8 lines

hw/vfio/common.c void vfio_mask_single_irqindex(VFIODevice *vbasedev, int index) · line 155

Quoted 4 lines
hw/vfio/common.c +4 -0
+    error_prepend(errp,
+                  "Failed to %s %s eventfd signaling for interrupt [%d,%d]: ",
+                  fd < 0 ? "tear down" : "set up", action_to_str(action),
+                  index, subindex);

Maybe icing on the cake, but this leaves me wishing it printed "MSIX-3" rather than "[2,3]" for a PCI device ;)

OK

Quoted 29 lines
Quoted 12 lines
hw/vfio/common.c +3 -0
+    return ret;
+}
+
 /*
  * IO Port/MMIO - Beware of the endians, VFIO is always little endian
  */
hw/vfio/pci.c +0 -0
index 504019c458..cd93ff6fa3 100644

[snip]

Quoted 16 lines
hw/vfio/pci.c +2 -5
@@ -2718,77 +2630,43 @@ static void vfio_req_notifier_handler(void *opaque)
 
 static void vfio_register_req_notifier(VFIOPCIDevice *vdev)
 {
-    struct vfio_irq_info irq_info = { .argsz = sizeof(irq_info),
-                                      .index = VFIO_PCI_REQ_IRQ_INDEX };
-    int argsz;
-    struct vfio_irq_set *irq_set;
-    int32_t *pfd;
+    Error *err = NULL;
+    int32_t fd;
 
     if (!(vdev->features & VFIO_FEATURE_ENABLE_REQ)) {
         return;
     }
 
Quoted 8 lines

hw/vfio/pci.c static void vfio_req_notifier_handler(void *opaque) · line 2734

Quoted 4 lines
hw/vfio/pci.c +0 -4
-    if (ioctl(vdev->vbasedev.fd,
-              VFIO_DEVICE_GET_IRQ_INFO, &irq_info) < 0 || irq_info.count < 1) {
-        return;
-    }

Here we used GET_IRQ_INFO to quietly only enable the request notifier when it's available, now it looks like this is no longer quiet if that support is unavailable. Is that intentional? Thanks,

not really I acknowledge ;-) I will restore that quiet check here.

Thanks

Eric

Quoted 40 lines

Alex

Quoted 36 lines
hw/vfio/pci.c +6 -19
-
     if (event_notifier_init(&vdev->req_notifier, 0)) {
         error_report("vfio: Unable to init event notifier for device request");
         return;
     }
 
-    argsz = sizeof(*irq_set) + sizeof(*pfd);
+    fd = event_notifier_get_fd(&vdev->req_notifier);
+    qemu_set_fd_handler(fd, vfio_req_notifier_handler, NULL, vdev);
 
-    irq_set = g_malloc0(argsz);
-    irq_set->argsz = argsz;
-    irq_set->flags = VFIO_IRQ_SET_DATA_EVENTFD |
-                     VFIO_IRQ_SET_ACTION_TRIGGER;
-    irq_set->index = VFIO_PCI_REQ_IRQ_INDEX;
-    irq_set->start = 0;
-    irq_set->count = 1;
-    pfd = (int32_t *)&irq_set->data;
-
-    *pfd = event_notifier_get_fd(&vdev->req_notifier);
-    qemu_set_fd_handler(*pfd, vfio_req_notifier_handler, NULL, vdev);
-
-    if (ioctl(vdev->vbasedev.fd, VFIO_DEVICE_SET_IRQS, irq_set)) {
-        error_report("vfio: Failed to set up device request notification");
-        qemu_set_fd_handler(*pfd, NULL, NULL, vdev);
+    if (vfio_set_irq_signaling(&vdev->vbasedev, VFIO_PCI_REQ_IRQ_INDEX, 0,
+                           VFIO_IRQ_SET_ACTION_TRIGGER, fd, &err)) {
+        error_reportf_err(err, VFIO_MSG_PREFIX, vdev->vbasedev.name);
+        qemu_set_fd_handler(fd, NULL, NULL, vdev);
         event_notifier_cleanup(&vdev->req_notifier);
     } else {
         vdev->req_enabled = true;
     }
-
-    g_free(irq_set);
 }