Skip to contentSkip to conversation

genirq/vfio: Introduce irq_update_devid() and optimize VFIO irq ops

PATCH Superseded Ben Luo 2 participants 9 messages Aug 22, 2019 – Aug 29, 2019

Subsystems (2)

v4 of 6 Superseded Latest is v6 →

lore
Share & tools
lore

Ben Luo

author
#0 lore raw

v4 cover genirq/vfio: Introduce irq_update_devid() and optimize VFIO irq ops

Currently, VFIO takes uses a free-then-request-irq way to do interrupt affinity setting and masking/unmasking for a VM (with device passthru via VFIO). Sometimes only change the cookie data of irqaction or even change nothing. The free-then-request-irq not only adds more latency, but also increases the risk of losing interrupt, which may lead to a VM hung forever in waiting for IO completion

This patchset solved the issue by:
Patch 2 introduces irq_update_devid() to only update dev_id of irqaction Patch 3 make use of this function and optimize irq operations in VFIO

changes from v3:
- rename the new function to irq_update_devid()
- amend commit messages and code comments
- use disbale_irq()/enable_irq() to avoid a twist for threaded interrupt
- add more comments to vfio-pci code

changes from v2:
- reformat to avoid quoted string split across lines and etc.

changes from v1:
- add Patch 1 to enhance error recovery etc. in free irq per tglx's comments
- enhance error recovery code and debugging info in irq_update_devid
- use __must_check in external referencing of this function
- use EXPORT_SYMBOL_GPL for irq_update_devid
- reformat code of patch 3 for better readability

Ben Luo (3):
genirq: enhance error recovery code in free irq
genirq: introduce irq_update_devid()
vfio/pci: make use of irq_update_devid and optimize irq ops

 drivers/vfio/pci/vfio_pci_intrs.c | 112 +++++++++++++++++++++++++-------------
 include/linux/interrupt.h         |   3 +
 kernel/irq/manage.c               | 105 +++++++++++++++++++++++++++++++----
 3 files changed, 170 insertions(+), 50 deletions(-)
Signature · 1 line
1.8.3.1
author
#1 lore raw

PATCH v4 1/3 genirq: enhance error recovery code in free irq

__free_irq()/__free_percpu_irq() need to return if called from IRQ context because the interrupt handler loop runs with desc->lock dropped and dev_id can be subject to load and store tearing. Also move WARNs out of lock region and print out dev_id to help debugging.

---
 kernel/irq/manage.c | 30 ++++++++++++++++++------------
 1 file changed, 18 insertions(+), 12 deletions(-)
kernel/irq/manage.c +18 -12
index e8f7f17..10ec3e9 100644
@@ -1690,7 +1690,10 @@ static struct irqaction *__free_irq(struct irq_desc *desc, void *dev_id)
 	struct irqaction *action, **action_ptr;
 	unsigned long flags;
 
-	WARN(in_interrupt(), "Trying to free IRQ %d from IRQ context!\n", irq);
+	if (WARN(in_interrupt(),
+		 "Trying to free IRQ %d (dev_id %p) from IRQ context!\n",
+		 irq, dev_id))
+		return NULL;
 
 	mutex_lock(&desc->request_mutex);
 	chip_bus_lock(desc);
@@ -1705,10 +1708,11 @@ static struct irqaction *__free_irq(struct irq_desc *desc, void *dev_id)
 		action = *action_ptr;
 
 		if (!action) {
-			WARN(1, "Trying to free already-free IRQ %d\n", irq);
 			raw_spin_unlock_irqrestore(&desc->lock, flags);
 			chip_bus_sync_unlock(desc);
 			mutex_unlock(&desc->request_mutex);
+			WARN(1, "Trying to free already-free IRQ %d (dev_id %p)\n",
+			     irq, dev_id);
 			return NULL;
 		}
 
@@ -2286,7 +2290,10 @@ static struct irqaction *__free_percpu_irq(unsigned int irq, void __percpu *dev_
 	struct irqaction *action;
 	unsigned long flags;
 
-	WARN(in_interrupt(), "Trying to free IRQ %d from IRQ context!\n", irq);
+	if (WARN(in_interrupt(),
+		 "Trying to free IRQ %d (dev_id %p) from IRQ context!\n",
+		 irq, dev_id))
+		return NULL;
 
 	if (!desc)
 		return NULL;
@@ -2295,14 +2302,17 @@ static struct irqaction *__free_percpu_irq(unsigned int irq, void __percpu *dev_
 
 	action = desc->action;
 	if (!action || action->percpu_dev_id != dev_id) {
-		WARN(1, "Trying to free already-free IRQ %d\n", irq);
-		goto bad;
+		raw_spin_unlock_irqrestore(&desc->lock, flags);
+		WARN(1, "Trying to free already-free IRQ (dev_id %p) %d\n",
+		     dev_id, irq);
+		return NULL;
 	}
 
 	if (!cpumask_empty(desc->percpu_enabled)) {
-		WARN(1, "percpu IRQ %d still enabled on CPU%d!\n",
-		     irq, cpumask_first(desc->percpu_enabled));
-		goto bad;
+		raw_spin_unlock_irqrestore(&desc->lock, flags);
+		WARN(1, "percpu IRQ %d (dev_id %p) still enabled on CPU%d!\n",
+		     irq, dev_id, cpumask_first(desc->percpu_enabled));
+		return NULL;
 	}
 
 	/* Found it - now remove it from the list of entries: */
@@ -2317,10 +2327,6 @@ static struct irqaction *__free_percpu_irq(unsigned int irq, void __percpu *dev_
 	irq_chip_pm_put(&desc->irq_data);
 	module_put(desc->owner);
 	return action;
-
-bad:
-	raw_spin_unlock_irqrestore(&desc->lock, flags);
-	return NULL;
 }
 
 /**
Signature · 1 line
1.8.3.1
author
#2 lore raw

PATCH v4 2/3 genirq: introduce irq_update_devid()

In reply to Ben Luo

Sometimes, only the dev_id field of irqaction needs to be changed.

E.g. KVM VM with device passthru via VFIO may switch the interrupt injection path between KVM irqfd and userspace eventfd. These two paths share the same interrupt number and handler for the same msi vector of a device, only with different 'dev_id's referencing to different fds' contexts. Set interrupt affinity in this VM is a way to trigger the path switching.

Currently, VFIO uses a free-then-request-irq way for the path switching. There is a time window between free_irq() and request_irq() where the target IRTE is invalid. So, in-flight interrupts (buffering in hardware layer and unfortunately cannot be synchronized in software) can cause DMAR faults and even worse, this VM may hang in waiting IO completion.

By using irq_update_devid(), this issue can be avoided since IRTE will not be invalidated during the whole process.

---
 include/linux/interrupt.h |  3 ++
 kernel/irq/manage.c       | 75 +++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 78 insertions(+)
include/linux/interrupt.h +3 -0
index 5b8328a..09b6a0f 100644
@@ -172,6 +172,9 @@ struct irqaction {
 request_percpu_nmi(unsigned int irq, irq_handler_t handler,
 		   const char *devname, void __percpu *dev);
 
+extern int __must_check
+irq_update_devid(unsigned int irq, void *dev_id, void *new_dev_id);
+
 extern const void *free_irq(unsigned int, void *);
 extern void free_percpu_irq(unsigned int, void __percpu *);
 
kernel/irq/manage.c +75 -0

81 lines not on this page · raw message

Signature · 1 line
1.8.3.1
author
#3 lore raw

PATCH v4 3/3 vfio/pci: make use of irq_update_devid and optimize irq ops

In reply to Ben Luo

When userspace (e.g. qemu) triggers a switch between KVM irqfd and userspace eventfd, only dev_id of irq action (i.e. the "trigger" in this patch's context) will be changed, but a free-then-request-irq action is taken in current code. And, irq affinity setting in VM will also trigger a free-then-request-irq action, which actually changes nothing, but only fires a producer re-registration to update irte in case that posted-interrupt is in use.

This patch makes use of irq_update_devid() and optimize both cases above, which reduces the risk of losing interrupt and also cuts some overhead.

---
 drivers/vfio/pci/vfio_pci_intrs.c | 112 +++++++++++++++++++++++++-------------
 1 file changed, 74 insertions(+), 38 deletions(-)
drivers/vfio/pci/vfio_pci_intrs.c +74 -38
index 3fa3f72..60d3023 100644
@@ -284,70 +284,106 @@ static int vfio_msi_enable(struct vfio_pci_device *vdev, int nvec, bool msix)
 static int vfio_msi_set_vector_signal(struct vfio_pci_device *vdev,
 				      int vector, int fd, bool msix)
 {
+	struct eventfd_ctx *trigger = NULL;
 	struct pci_dev *pdev = vdev->pdev;
-	struct eventfd_ctx *trigger;
 	int irq, ret;
 
 	if (vector < 0 || vector >= vdev->num_ctx)
 		return -EINVAL;
 
+	if (fd >= 0) {
+		trigger = eventfd_ctx_fdget(fd);
+		if (IS_ERR(trigger))
+			return PTR_ERR(trigger);
+	}
+
 	irq = pci_irq_vector(pdev, vector);
 
+	/*
+	 * For KVM-VFIO case, interrupt from passthrough device will be directly
+	 * delivered to VM after producer and consumer connected successfully.
+	 * If producer and consumer are disconnected, this interrupt process
+	 * will fall back to remap mode, where interrupt handler uses 'trigger'
+	 * to find the right way to deliver the interrupt to VM. So, it is safe
+	 * to do irq_update_devid() before irq_bypass_unregister_producer() which
+	 * switches interrupt process to remap mode. To producer and consumer,
+	 * 'trigger' is only a token used for pairing them togather.
+	 */
 	if (vdev->ctx[vector].trigger) {
-		free_irq(irq, vdev->ctx[vector].trigger);
-		irq_bypass_unregister_producer(&vdev->ctx[vector].producer);
-		kfree(vdev->ctx[vector].name);
-		eventfd_ctx_put(vdev->ctx[vector].trigger);
-		vdev->ctx[vector].trigger = NULL;
+		if (vdev->ctx[vector].trigger == trigger) {
+			/* switch back to remap mode */
+			irq_bypass_unregister_producer(&vdev->ctx[vector].producer);
+		} else if (trigger) {
+			ret = irq_update_devid(irq,
+					       vdev->ctx[vector].trigger, trigger);
+			if (unlikely(ret)) {
+				dev_info(&pdev->dev,
+					 "update devid of %d (token %p) failed: %d\n",
+					 irq, vdev->ctx[vector].trigger, ret);
+				eventfd_ctx_put(trigger);
+				return ret;
+			}
+			irq_bypass_unregister_producer(&vdev->ctx[vector].producer);
+			eventfd_ctx_put(vdev->ctx[vector].trigger);
+			vdev->ctx[vector].producer.token = trigger;
+			vdev->ctx[vector].trigger = trigger;
+		} else {
+			free_irq(irq, vdev->ctx[vector].trigger);
+			irq_bypass_unregister_producer(&vdev->ctx[vector].producer);
+			kfree(vdev->ctx[vector].name);
+			eventfd_ctx_put(vdev->ctx[vector].trigger);
+			vdev->ctx[vector].trigger = NULL;
+		}
 	}
 
 	if (fd < 0)
 		return 0;
 
-	vdev->ctx[vector].name = kasprintf(GFP_KERNEL, "vfio-msi%s[%d](%s)",
-					   msix ? "x" : "", vector,
-					   pci_name(pdev));
-	if (!vdev->ctx[vector].name)
-		return -ENOMEM;
+	if (!vdev->ctx[vector].trigger) {
+		vdev->ctx[vector].name = kasprintf(GFP_KERNEL,
+						   "vfio-msi%s[%d](%s)",
+						   msix ? "x" : "", vector,
+						   pci_name(pdev));
+		if (!vdev->ctx[vector].name) {
+			eventfd_ctx_put(trigger);
+			return -ENOMEM;
+		}
 
-	trigger = eventfd_ctx_fdget(fd);
-	if (IS_ERR(trigger)) {
-		kfree(vdev->ctx[vector].name);
-		return PTR_ERR(trigger);
-	}
+		/*
+		 * The MSIx vector table resides in device memory which may be
+		 * cleared via backdoor resets. We don't allow direct access to
+		 * the vector table so even if a userspace driver attempts to
+		 * save/restore around such a reset it would be unsuccessful.
+		 * To avoid this, restore the cached value of the message prior
+		 * to enabling.
+		 */
+		if (msix) {
+			struct msi_msg msg;
 
-	/*
-	 * The MSIx vector table resides in device memory which may be cleared
-	 * via backdoor resets. We don't allow direct access to the vector
-	 * table so even if a userspace driver attempts to save/restore around
-	 * such a reset it would be unsuccessful. To avoid this, restore the
-	 * cached value of the message prior to enabling.
-	 */
-	if (msix) {
-		struct msi_msg msg;
+			get_cached_msi_msg(irq, &msg);
+			pci_write_msi_msg(irq, &msg);
+		}
 
-		get_cached_msi_msg(irq, &msg);
-		pci_write_msi_msg(irq, &msg);
-	}
+		ret = request_irq(irq, vfio_msihandler, 0,
+				  vdev->ctx[vector].name, trigger);
+		if (ret) {
+			kfree(vdev->ctx[vector].name);
+			eventfd_ctx_put(trigger);
+			return ret;
+		}
 
-	ret = request_irq(irq, vfio_msihandler, 0,
-			  vdev->ctx[vector].name, trigger);
-	if (ret) {
-		kfree(vdev->ctx[vector].name);
-		eventfd_ctx_put(trigger);
-		return ret;
+		vdev->ctx[vector].producer.token = trigger;
+		vdev->ctx[vector].producer.irq = irq;
+		vdev->ctx[vector].trigger = trigger;
 	}
 
-	vdev->ctx[vector].producer.token = trigger;
-	vdev->ctx[vector].producer.irq = irq;
+	/* setup bypass connection and make irte updated */
 	ret = irq_bypass_register_producer(&vdev->ctx[vector].producer);
 	if (unlikely(ret))
 		dev_info(&pdev->dev,
 		"irq bypass producer (token %p) registration fails: %d\n",
 		vdev->ctx[vector].producer.token, ret);
 
-	vdev->ctx[vector].trigger = trigger;
-
 	return 0;
 }
 
Signature · 1 line
1.8.3.1
#4 lore raw

Re: [PATCH v4 3/3] vfio/pci: make use of irq_update_devid and optimize irq ops

Quoted 29 lines · On Thu, 22 Aug 2019 23:34:43 +0800 Ben Luo <[email protected]> wrote:

When userspace (e.g. qemu) triggers a switch between KVM irqfd and userspace eventfd, only dev_id of irq action (i.e. the "trigger" in this patch's context) will be changed, but a free-then-request-irq action is taken in current code. And, irq affinity setting in VM will also trigger a free-then-request-irq action, which actually changes nothing, but only fires a producer re-registration to update irte in case that posted-interrupt is in use.

This patch makes use of irq_update_devid() and optimize both cases above, which reduces the risk of losing interrupt and also cuts some overhead.

---
 drivers/vfio/pci/vfio_pci_intrs.c | 112 +++++++++++++++++++++++++-------------
 1 file changed, 74 insertions(+), 38 deletions(-)
drivers/vfio/pci/vfio_pci_intrs.c +1 -1
index 3fa3f72..60d3023 100644
@@ -284,70 +284,106 @@ static int vfio_msi_enable(struct vfio_pci_device *vdev, int nvec, bool msix)
 static int vfio_msi_set_vector_signal(struct vfio_pci_device *vdev,
 				      int vector, int fd, bool msix)
 {
+	struct eventfd_ctx *trigger = NULL;
 	struct pci_dev *pdev = vdev->pdev;
-	struct eventfd_ctx *trigger;

drivers/vfio/pci/vfio_pci_intrs.c static int vfio_msi_enable(struct vfio_pci_device *vdev, int nvec, bool msix) · line 298

Quoted 10 lines
drivers/vfio/pci/vfio_pci_intrs.c +5 -0
 	int irq, ret;
 
 	if (vector < 0 || vector >= vdev->num_ctx)
 		return -EINVAL;
 
+	if (fd >= 0) {
+		trigger = eventfd_ctx_fdget(fd);
+		if (IS_ERR(trigger))
+			return PTR_ERR(trigger);
+	}

I think this is a user visible change. Previously the vector is disabled first, then if an error occurs re-enabling, we return an errno with the vector disabled. Here we instead fail the ioctl and leave the state as if it had never happened. For instance with QEMU, if they were trying to change from KVM to userspace signaling and entered this condition, previously the interrupt would signal to neither eventfd, now it would continue signaling to KVM. If QEMU's intent was to emulate vector masking, this could induce unhandled interrupts in the guest. Maybe we need a tear-down on fault here to maintain that behavior, or do you see some justification for the change?

Quoted 12 lines
drivers/vfio/pci/vfio_pci_intrs.c +10 -0
+
 	irq = pci_irq_vector(pdev, vector);
 
+	/*
+	 * For KVM-VFIO case, interrupt from passthrough device will be directly
+	 * delivered to VM after producer and consumer connected successfully.
+	 * If producer and consumer are disconnected, this interrupt process
+	 * will fall back to remap mode, where interrupt handler uses 'trigger'
+	 * to find the right way to deliver the interrupt to VM. So, it is safe
+	 * to do irq_update_devid() before irq_bypass_unregister_producer() which
+	 * switches interrupt process to remap mode. To producer and consumer,
+	 * 'trigger' is only a token used for pairing them togather.

drivers/vfio/pci/vfio_pci_intrs.c static int vfio_msi_enable(struct vfio_pci_device *vdev, int nvec, bool msix) · line 315

Quoted 10 lines
drivers/vfio/pci/vfio_pci_intrs.c +4 -5
+	 */
 	if (vdev->ctx[vector].trigger) {
-		free_irq(irq, vdev->ctx[vector].trigger);
-		irq_bypass_unregister_producer(&vdev->ctx[vector].producer);
-		kfree(vdev->ctx[vector].name);
-		eventfd_ctx_put(vdev->ctx[vector].trigger);
-		vdev->ctx[vector].trigger = NULL;
+		if (vdev->ctx[vector].trigger == trigger) {
+			/* switch back to remap mode */
+			irq_bypass_unregister_producer(&vdev->ctx[vector].producer);

I think we leak the fd context we acquired above in this case.

Why do we do anything in this case, couldn't we just 'put' the extra ctx and return 0 here?

drivers/vfio/pci/vfio_pci_intrs.c static int vfio_msi_enable(struct vfio_pci_device *vdev, int nvec, bool msix) · line 326

Quoted 11 lines
drivers/vfio/pci/vfio_pci_intrs.c +11 -0
+		} else if (trigger) {
+			ret = irq_update_devid(irq,
+					       vdev->ctx[vector].trigger, trigger);
+			if (unlikely(ret)) {
+				dev_info(&pdev->dev,
+					 "update devid of %d (token %p) failed: %d\n",
+					 irq, vdev->ctx[vector].trigger, ret);
+				eventfd_ctx_put(trigger);
+				return ret;
+			}
+			irq_bypass_unregister_producer(&vdev->ctx[vector].producer);

Can you explain this ordering, I would have expected that we'd unregister the bypass before we updated the devid. Thanks,

Alex

Quoted 95 lines
drivers/vfio/pci/vfio_pci_intrs.c +43 -32
+			eventfd_ctx_put(vdev->ctx[vector].trigger);
+			vdev->ctx[vector].producer.token = trigger;
+			vdev->ctx[vector].trigger = trigger;
+		} else {
+			free_irq(irq, vdev->ctx[vector].trigger);
+			irq_bypass_unregister_producer(&vdev->ctx[vector].producer);
+			kfree(vdev->ctx[vector].name);
+			eventfd_ctx_put(vdev->ctx[vector].trigger);
+			vdev->ctx[vector].trigger = NULL;
+		}
 	}
 
 	if (fd < 0)
 		return 0;
 
-	vdev->ctx[vector].name = kasprintf(GFP_KERNEL, "vfio-msi%s[%d](%s)",
-					   msix ? "x" : "", vector,
-					   pci_name(pdev));
-	if (!vdev->ctx[vector].name)
-		return -ENOMEM;
+	if (!vdev->ctx[vector].trigger) {
+		vdev->ctx[vector].name = kasprintf(GFP_KERNEL,
+						   "vfio-msi%s[%d](%s)",
+						   msix ? "x" : "", vector,
+						   pci_name(pdev));
+		if (!vdev->ctx[vector].name) {
+			eventfd_ctx_put(trigger);
+			return -ENOMEM;
+		}
 
-	trigger = eventfd_ctx_fdget(fd);
-	if (IS_ERR(trigger)) {
-		kfree(vdev->ctx[vector].name);
-		return PTR_ERR(trigger);
-	}
+		/*
+		 * The MSIx vector table resides in device memory which may be
+		 * cleared via backdoor resets. We don't allow direct access to
+		 * the vector table so even if a userspace driver attempts to
+		 * save/restore around such a reset it would be unsuccessful.
+		 * To avoid this, restore the cached value of the message prior
+		 * to enabling.
+		 */
+		if (msix) {
+			struct msi_msg msg;
 
-	/*
-	 * The MSIx vector table resides in device memory which may be cleared
-	 * via backdoor resets. We don't allow direct access to the vector
-	 * table so even if a userspace driver attempts to save/restore around
-	 * such a reset it would be unsuccessful. To avoid this, restore the
-	 * cached value of the message prior to enabling.
-	 */
-	if (msix) {
-		struct msi_msg msg;
+			get_cached_msi_msg(irq, &msg);
+			pci_write_msi_msg(irq, &msg);
+		}
 
-		get_cached_msi_msg(irq, &msg);
-		pci_write_msi_msg(irq, &msg);
-	}
+		ret = request_irq(irq, vfio_msihandler, 0,
+				  vdev->ctx[vector].name, trigger);
+		if (ret) {
+			kfree(vdev->ctx[vector].name);
+			eventfd_ctx_put(trigger);
+			return ret;
+		}
 
-	ret = request_irq(irq, vfio_msihandler, 0,
-			  vdev->ctx[vector].name, trigger);
-	if (ret) {
-		kfree(vdev->ctx[vector].name);
-		eventfd_ctx_put(trigger);
-		return ret;
+		vdev->ctx[vector].producer.token = trigger;
+		vdev->ctx[vector].producer.irq = irq;
+		vdev->ctx[vector].trigger = trigger;
 	}
 
-	vdev->ctx[vector].producer.token = trigger;
-	vdev->ctx[vector].producer.irq = irq;
+	/* setup bypass connection and make irte updated */
 	ret = irq_bypass_register_producer(&vdev->ctx[vector].producer);
 	if (unlikely(ret))
 		dev_info(&pdev->dev,
 		"irq bypass producer (token %p) registration fails: %d\n",
 		vdev->ctx[vector].producer.token, ret);
 
-	vdev->ctx[vector].trigger = trigger;
-
 	return 0;
 }
 
author
#5 lore raw

Re: [PATCH v4 3/3] vfio/pci: make use of irq_update_devid and optimize irq ops

Quoted 37 lines · Alex Williamson
Quoted 34 lines · On Thu, 22 Aug 2019 23:34:43 +0800 Ben Luo <[email protected]> wrote:

When userspace (e.g. qemu) triggers a switch between KVM irqfd and userspace eventfd, only dev_id of irq action (i.e. the "trigger" in this patch's context) will be changed, but a free-then-request-irq action is taken in current code. And, irq affinity setting in VM will also trigger a free-then-request-irq action, which actually changes nothing, but only fires a producer re-registration to update irte in case that posted-interrupt is in use.

This patch makes use of irq_update_devid() and optimize both cases above, which reduces the risk of losing interrupt and also cuts some overhead.

---
  drivers/vfio/pci/vfio_pci_intrs.c | 112 +++++++++++++++++++++++++-------------
  1 file changed, 74 insertions(+), 38 deletions(-)
drivers/vfio/pci/vfio_pci_intrs.c +1 -1
index 3fa3f72..60d3023 100644
@@ -284,70 +284,106 @@ static int vfio_msi_enable(struct vfio_pci_device *vdev, int nvec, bool msix)
  static int vfio_msi_set_vector_signal(struct vfio_pci_device *vdev,
  				      int vector, int fd, bool msix)
  {
+	struct eventfd_ctx *trigger = NULL;
  	struct pci_dev *pdev = vdev->pdev;
-	struct eventfd_ctx *trigger;
  	int irq, ret;
 
  	if (vector < 0 || vector >= vdev->num_ctx)
  		return -EINVAL;
 
Quoted 15 lines

drivers/vfio/pci/vfio_pci_intrs.c static int vfio_msi_enable(struct vfio_pci_device *vdev, int nvec, bool msix) · line 298

Quoted 5 lines
drivers/vfio/pci/vfio_pci_intrs.c +5 -0
+	if (fd >= 0) {
+		trigger = eventfd_ctx_fdget(fd);
+		if (IS_ERR(trigger))
+			return PTR_ERR(trigger);
+	}

I think this is a user visible change. Previously the vector is disabled first, then if an error occurs re-enabling, we return an errno with the vector disabled. Here we instead fail the ioctl and leave the state as if it had never happened. For instance with QEMU, if they were trying to change from KVM to userspace signaling and entered this condition, previously the interrupt would signal to neither eventfd, now it would continue signaling to KVM. If QEMU's intent was to emulate vector masking, this could induce unhandled interrupts in the guest. Maybe we need a tear-down on fault here to maintain that behavior, or do you see some justification for the change?

Thanks for your comments, this reminds me to think more about the effects to users.

After I reviewed the related code in Qemu and VFIO, I think maybe there is a problem in current behavior for the signal path changing case. Qemu has neither recovery nor retry code in case that ioctl with 'VFIO_DEVICE_SET_IRQS' command fails, so if the old signal path has been disabled on fault of setting up new path, the corresponding vector may be disabled forever. Following is an example from qemu's vfio_msix_vector_do_use():

        ret = ioctl(vdev->vbasedev.fd, VFIO_DEVICE_SET_IRQS, irq_set); g_free(irq_set);
        if (ret) {
            error_report("vfio: failed to modify vector, %d", ret); }

I think the singal path before changing should be still working at this moment and the caller should keep it working if the changing fails, so that at least we still have the old path instead of no path.

For masking vector case, the 'fd' should be -1, and the interrupt will be freed as before this patch.

Quoted 16 lines
Quoted 16 lines
drivers/vfio/pci/vfio_pci_intrs.c +11 -1
+
  	irq = pci_irq_vector(pdev, vector);
 
+	/*
+	 * For KVM-VFIO case, interrupt from passthrough device will be directly
+	 * delivered to VM after producer and consumer connected successfully.
+	 * If producer and consumer are disconnected, this interrupt process
+	 * will fall back to remap mode, where interrupt handler uses 'trigger'
+	 * to find the right way to deliver the interrupt to VM. So, it is safe
+	 * to do irq_update_devid() before irq_bypass_unregister_producer() which
+	 * switches interrupt process to remap mode. To producer and consumer,
+	 * 'trigger' is only a token used for pairing them togather.
+	 */
  	if (vdev->ctx[vector].trigger) {
-		free_irq(irq, vdev->ctx[vector].trigger);
Quoted 8 lines

drivers/vfio/pci/vfio_pci_intrs.c static int vfio_msi_enable(struct vfio_pci_device *vdev, int nvec, bool msix) · line 315

Quoted 7 lines
drivers/vfio/pci/vfio_pci_intrs.c +3 -4
-		irq_bypass_unregister_producer(&vdev->ctx[vector].producer);
-		kfree(vdev->ctx[vector].name);
-		eventfd_ctx_put(vdev->ctx[vector].trigger);
-		vdev->ctx[vector].trigger = NULL;
+		if (vdev->ctx[vector].trigger == trigger) {
+			/* switch back to remap mode */
+			irq_bypass_unregister_producer(&vdev->ctx[vector].producer);

I think we leak the fd context we acquired above in this case.

Thanks for pointing it out, I will fix this in next version.

Quoted 3 lines

Why do we do anything in this case, couldn't we just 'put' the extra ctx and return 0 here?

Sorry for confusing and I do this for a reason,  I will add some more comments like this:

Unregistration here is for re-resigtraion later, which will trigger the reconnection of irq_bypass producer and consumer, which in turn calls vmx_update_pi_irte() to update IRTE if posted interrupt is in use. (vmx_update_pi_irte() will modify IRTE based on the information retrieved from KVM.) Whether producer token changed or not, irq_bypass_register_producer() is a way (seems the only way) to update IRTE by VFIO for posted interrupt. The IRTE will be used by IOMMU directly to find the target CPU for an interrupt posted to VM, since hypervisor is bypassed.

Quoted 15 lines

drivers/vfio/pci/vfio_pci_intrs.c static int vfio_msi_enable(struct vfio_pci_device *vdev, int nvec, bool msix) · line 326

Quoted 12 lines
drivers/vfio/pci/vfio_pci_intrs.c +11 -0
+		} else if (trigger) {
+			ret = irq_update_devid(irq,
+					       vdev->ctx[vector].trigger, trigger);
+			if (unlikely(ret)) {
+				dev_info(&pdev->dev,
+					 "update devid of %d (token %p) failed: %d\n",
+					 irq, vdev->ctx[vector].trigger, ret);
+				eventfd_ctx_put(trigger);
+				return ret;
+			}
+			irq_bypass_unregister_producer(&vdev->ctx[vector].producer);

Can you explain this ordering, I would have expected that we'd unregister the bypass before we updated the devid. Thanks,

Quoted 1 line

Alex

Actually, I have explained this in comments above this whole control block. I think it is safe and better to update devid before irq_bypass_unregister_producer() which switches interrupt process from posted mode to remap mode. So, if update fails, the posted interrupt can still work.

Anyway, to producer and consumer,  'trigger' is only a token used for pairing them togather.

Thanks,

    Ben

#6 lore raw

Re: [PATCH v4 3/3] vfio/pci: make use of irq_update_devid and optimize irq ops

Quoted 73 lines · On Wed, 28 Aug 2019 18:08:02 +0800 Ben Luo <[email protected]> wrote:
Quoted 37 lines · Alex Williamson
Quoted 34 lines · On Thu, 22 Aug 2019 23:34:43 +0800 Ben Luo <[email protected]> wrote:

When userspace (e.g. qemu) triggers a switch between KVM irqfd and userspace eventfd, only dev_id of irq action (i.e. the "trigger" in this patch's context) will be changed, but a free-then-request-irq action is taken in current code. And, irq affinity setting in VM will also trigger a free-then-request-irq action, which actually changes nothing, but only fires a producer re-registration to update irte in case that posted-interrupt is in use.

This patch makes use of irq_update_devid() and optimize both cases above, which reduces the risk of losing interrupt and also cuts some overhead.

---
  drivers/vfio/pci/vfio_pci_intrs.c | 112 +++++++++++++++++++++++++-------------
  1 file changed, 74 insertions(+), 38 deletions(-)
drivers/vfio/pci/vfio_pci_intrs.c +1 -1
index 3fa3f72..60d3023 100644
@@ -284,70 +284,106 @@ static int vfio_msi_enable(struct vfio_pci_device *vdev, int nvec, bool msix)
  static int vfio_msi_set_vector_signal(struct vfio_pci_device *vdev,
  				      int vector, int fd, bool msix)
  {
+	struct eventfd_ctx *trigger = NULL;
  	struct pci_dev *pdev = vdev->pdev;
-	struct eventfd_ctx *trigger;
  	int irq, ret;
 
  	if (vector < 0 || vector >= vdev->num_ctx)
  		return -EINVAL;
 
Quoted 15 lines

drivers/vfio/pci/vfio_pci_intrs.c static int vfio_msi_enable(struct vfio_pci_device *vdev, int nvec, bool msix) · line 298

Quoted 5 lines
drivers/vfio/pci/vfio_pci_intrs.c +5 -0
+	if (fd >= 0) {
+		trigger = eventfd_ctx_fdget(fd);
+		if (IS_ERR(trigger))
+			return PTR_ERR(trigger);
+	}  

I think this is a user visible change. Previously the vector is disabled first, then if an error occurs re-enabling, we return an errno with the vector disabled. Here we instead fail the ioctl and leave the state as if it had never happened. For instance with QEMU, if they were trying to change from KVM to userspace signaling and entered this condition, previously the interrupt would signal to neither eventfd, now it would continue signaling to KVM. If QEMU's intent was to emulate vector masking, this could induce unhandled interrupts in the guest. Maybe we need a tear-down on fault here to maintain that behavior, or do you see some justification for the change?

Thanks for your comments, this reminds me to think more about the effects to users.

After I reviewed the related code in Qemu and VFIO, I think maybe there is a problem in current behavior
for the signal path changing case. Qemu has neither recovery nor retry code in case that ioctl with
'VFIO_DEVICE_SET_IRQS' command fails, so if the old signal path has been disabled on fault of setting
up new path, the corresponding vector may be disabled forever. Following is an example from qemu's
vfio_msix_vector_do_use():

        ret = ioctl(vdev->vbasedev.fd, VFIO_DEVICE_SET_IRQS, irq_set); g_free(irq_set);
        if (ret) {
            error_report("vfio: failed to modify vector, %d", ret); }

Quoted 7 lines

I think the singal path before changing should be still working at this moment and the caller should keep it
working if the changing fails, so that at least we still have the old path instead of no path.

For masking vector case, the 'fd' should be -1, and the interrupt will be freed as before this patch.

QEMU doesn't really have an opportunity to signal an error to the guest, we're emulating the hardware masking of MSI and MSI-X. The guest is simply trying to write a mask bit in the vector, there's no provision in the PCI spec that setting this bit can fail. The current behavior is that the vector is disabled on error. We can argue whether that's the optimal behavior, but it's the existing behavior and changing it would require and evaluation of all existing users.

Quoted 31 lines
Quoted 15 lines
Quoted 15 lines
drivers/vfio/pci/vfio_pci_intrs.c +11 -1
+
  	irq = pci_irq_vector(pdev, vector);
 
+	/*
+	 * For KVM-VFIO case, interrupt from passthrough device will be directly
+	 * delivered to VM after producer and consumer connected successfully.
+	 * If producer and consumer are disconnected, this interrupt process
+	 * will fall back to remap mode, where interrupt handler uses 'trigger'
+	 * to find the right way to deliver the interrupt to VM. So, it is safe
+	 * to do irq_update_devid() before irq_bypass_unregister_producer() which
+	 * switches interrupt process to remap mode. To producer and consumer,
+	 * 'trigger' is only a token used for pairing them togather.
+	 */
  	if (vdev->ctx[vector].trigger) {
-		free_irq(irq, vdev->ctx[vector].trigger);
Quoted 8 lines

drivers/vfio/pci/vfio_pci_intrs.c static int vfio_msi_enable(struct vfio_pci_device *vdev, int nvec, bool msix) · line 315

Quoted 7 lines
drivers/vfio/pci/vfio_pci_intrs.c +3 -4
-		irq_bypass_unregister_producer(&vdev->ctx[vector].producer);
-		kfree(vdev->ctx[vector].name);
-		eventfd_ctx_put(vdev->ctx[vector].trigger);
-		vdev->ctx[vector].trigger = NULL;
+		if (vdev->ctx[vector].trigger == trigger) {
+			/* switch back to remap mode */
+			irq_bypass_unregister_producer(&vdev->ctx[vector].producer);  

I think we leak the fd context we acquired above in this case.

Thanks for pointing it out, I will fix this in next version.

Quoted 3 lines

Why do we do anything in this case, couldn't we just 'put' the extra ctx and return 0 here?

Sorry for confusing and I do this for a reason,  I will add some more comments like this:

Quoted 11 lines

Unregistration here is for re-resigtraion later, which will trigger the reconnection of irq_bypass producer
and consumer, which in turn calls vmx_update_pi_irte() to update IRTE if posted interrupt is in use.
(vmx_update_pi_irte() will modify IRTE based on the information retrieved from KVM.)
Whether producer token changed or not, irq_bypass_register_producer() is a way (seems the only way) to
update IRTE by VFIO for posted interrupt. The IRTE will be used by IOMMU directly to find the target CPU
for an interrupt posted to VM, since hypervisor is bypassed.

This is only explaining what the bypass de-registration and re-registration does, not why we need to perform those actions here. If the trigger is the same as that already attached to this vector, why is the IRTE changing? Seems this ought to be a no-op for this vector.

Quoted 15 lines
Quoted 14 lines

drivers/vfio/pci/vfio_pci_intrs.c static int vfio_msi_enable(struct vfio_pci_device *vdev, int nvec, bool msix) · line 326

Quoted 11 lines
drivers/vfio/pci/vfio_pci_intrs.c +11 -0
+		} else if (trigger) {
+			ret = irq_update_devid(irq,
+					       vdev->ctx[vector].trigger, trigger);
+			if (unlikely(ret)) {
+				dev_info(&pdev->dev,
+					 "update devid of %d (token %p) failed: %d\n",
+					 irq, vdev->ctx[vector].trigger, ret);
+				eventfd_ctx_put(trigger);
+				return ret;
+			}
+			irq_bypass_unregister_producer(&vdev->ctx[vector].producer);  

Can you explain this ordering, I would have expected that we'd unregister the bypass before we updated the devid. Thanks,

Quoted 1 line

Alex

Quoted 8 lines

Actually, I have explained this in comments above this whole control block. I think it is safe and better to
update devid before irq_bypass_unregister_producer() which switches interrupt process from posted mode
to remap mode. So, if update fails, the posted interrupt can still work.

Anyway, to producer and consumer,  'trigger' is only a token used for pairing them togather.

The bypass is not a guaranteed mechanism, it's an opportunistic accelerator. If the devid update fails, what state are we left with? The irq action may not work, but the bypass does; maybe; maybe not all the time? This seems to fall into the same consistency in userspace behavior issue identified above. The user ABI is that the vector is disabled if an error is returned. Thanks,

Alex

author
#7 lore raw

Re: [PATCH v4 3/3] vfio/pci: make use of irq_update_devid and optimize irq ops

Quoted 82 lines · Alex Williamson
Quoted 72 lines · On Wed, 28 Aug 2019 18:08:02 +0800 Ben Luo <[email protected]> wrote:
Quoted 37 lines · Alex Williamson
Quoted 34 lines · On Thu, 22 Aug 2019 23:34:43 +0800 Ben Luo <[email protected]> wrote:

When userspace (e.g. qemu) triggers a switch between KVM irqfd and userspace eventfd, only dev_id of irq action (i.e. the "trigger" in this patch's context) will be changed, but a free-then-request-irq action is taken in current code. And, irq affinity setting in VM will also trigger a free-then-request-irq action, which actually changes nothing, but only fires a producer re-registration to update irte in case that posted-interrupt is in use.

This patch makes use of irq_update_devid() and optimize both cases above, which reduces the risk of losing interrupt and also cuts some overhead.

---
   drivers/vfio/pci/vfio_pci_intrs.c | 112 +++++++++++++++++++++++++-------------
   1 file changed, 74 insertions(+), 38 deletions(-)
drivers/vfio/pci/vfio_pci_intrs.c +1 -1
index 3fa3f72..60d3023 100644
@@ -284,70 +284,106 @@ static int vfio_msi_enable(struct vfio_pci_device *vdev, int nvec, bool msix)
   static int vfio_msi_set_vector_signal(struct vfio_pci_device *vdev,
   				      int vector, int fd, bool msix)
   {
+	struct eventfd_ctx *trigger = NULL;
   	struct pci_dev *pdev = vdev->pdev;
-	struct eventfd_ctx *trigger;
   	int irq, ret;
 
   	if (vector < 0 || vector >= vdev->num_ctx)
   		return -EINVAL;
 
Quoted 15 lines

drivers/vfio/pci/vfio_pci_intrs.c static int vfio_msi_enable(struct vfio_pci_device *vdev, int nvec, bool msix) · line 298

Quoted 5 lines
drivers/vfio/pci/vfio_pci_intrs.c +5 -0
+	if (fd >= 0) {
+		trigger = eventfd_ctx_fdget(fd);
+		if (IS_ERR(trigger))
+			return PTR_ERR(trigger);
+	}

I think this is a user visible change. Previously the vector is disabled first, then if an error occurs re-enabling, we return an errno with the vector disabled. Here we instead fail the ioctl and leave the state as if it had never happened. For instance with QEMU, if they were trying to change from KVM to userspace signaling and entered this condition, previously the interrupt would signal to neither eventfd, now it would continue signaling to KVM. If QEMU's intent was to emulate vector masking, this could induce unhandled interrupts in the guest. Maybe we need a tear-down on fault here to maintain that behavior, or do you see some justification for the change?

Thanks for your comments, this reminds me to think more about the effects to users.

After I reviewed the related code in Qemu and VFIO, I think maybe there is a problem in current behavior
for the signal path changing case. Qemu has neither recovery nor retry code in case that ioctl with
'VFIO_DEVICE_SET_IRQS' command fails, so if the old signal path has been disabled on fault of setting
up new path, the corresponding vector may be disabled forever. Following is an example from qemu's
vfio_msix_vector_do_use():

          ret = ioctl(vdev->vbasedev.fd, VFIO_DEVICE_SET_IRQS, irq_set);
          g_free(irq_set);
          if (ret) {
              error_report("vfio: failed to modify vector, %d", ret);
          }
Quoted 7 lines

I think the singal path before changing should be still working at this moment and the caller should keep it
working if the changing fails, so that at least we still have the old path instead of no path.

For masking vector case, the 'fd' should be -1, and the interrupt will be freed as before this patch.

Quoted 7 lines

QEMU doesn't really have an opportunity to signal an error to the guest, we're emulating the hardware masking of MSI and MSI-X. The guest is simply trying to write a mask bit in the vector, there's no provision in the PCI spec that setting this bit can fail. The current behavior is that the vector is disabled on error. We can argue whether that's the optimal behavior, but it's the existing behavior and changing it would require and evaluation of all existing users.

I totally agree with you that masking of MSI and MSI-X should follow current behavior, and my code does follow this behavior when 'fd' == -1, the interrupt will surely be disabled.

There is another case that 'fd' is not -1, means userspace just want to change the singal path, this time we do have a chance of encountering error on eventfd_ctx_fdget(fd). So, I think it is better to keep the old path working in this case.

But, as you said this may break the expectation of existing users, I make it tear-down on fault in next version.

Quoted 41 lines
Quoted 30 lines
Quoted 16 lines
Quoted 16 lines
drivers/vfio/pci/vfio_pci_intrs.c +11 -1
+
   	irq = pci_irq_vector(pdev, vector);
 
+	/*
+	 * For KVM-VFIO case, interrupt from passthrough device will be directly
+	 * delivered to VM after producer and consumer connected successfully.
+	 * If producer and consumer are disconnected, this interrupt process
+	 * will fall back to remap mode, where interrupt handler uses 'trigger'
+	 * to find the right way to deliver the interrupt to VM. So, it is safe
+	 * to do irq_update_devid() before irq_bypass_unregister_producer() which
+	 * switches interrupt process to remap mode. To producer and consumer,
+	 * 'trigger' is only a token used for pairing them togather.
+	 */
   	if (vdev->ctx[vector].trigger) {
-		free_irq(irq, vdev->ctx[vector].trigger);
Quoted 8 lines

drivers/vfio/pci/vfio_pci_intrs.c static int vfio_msi_enable(struct vfio_pci_device *vdev, int nvec, bool msix) · line 315

Quoted 7 lines
drivers/vfio/pci/vfio_pci_intrs.c +3 -4
-		irq_bypass_unregister_producer(&vdev->ctx[vector].producer);
-		kfree(vdev->ctx[vector].name);
-		eventfd_ctx_put(vdev->ctx[vector].trigger);
-		vdev->ctx[vector].trigger = NULL;
+		if (vdev->ctx[vector].trigger == trigger) {
+			/* switch back to remap mode */
+			irq_bypass_unregister_producer(&vdev->ctx[vector].producer);

I think we leak the fd context we acquired above in this case.

Thanks for pointing it out, I will fix this in next version.

Quoted 2 lines

Why do we do anything in this case, couldn't we just 'put' the extra ctx and return 0 here?

Sorry for confusing and I do this for a reason,  I will add some more comments like this:

Quoted 11 lines

Unregistration here is for re-resigtraion later, which will trigger the reconnection of irq_bypass producer
and consumer, which in turn calls vmx_update_pi_irte() to update IRTE if posted interrupt is in use.
(vmx_update_pi_irte() will modify IRTE based on the information retrieved from KVM.)
Whether producer token changed or not, irq_bypass_register_producer() is a way (seems the only way) to
update IRTE by VFIO for posted interrupt. The IRTE will be used by IOMMU directly to find the target CPU
for an interrupt posted to VM, since hypervisor is bypassed.

Quoted 4 lines

This is only explaining what the bypass de-registration and re-registration does, not why we need to perform those actions here. If the trigger is the same as that already attached to this vector, why is the IRTE changing? Seems this ought to be a no-op for this vector.

Sorry, I think it cannot be a no-op here. The interrupt affinity setting in guest also triggers the calling of this function and IRTE will be modified with new affinity information retrieved from KVM's data structure by vmx_update_pi_irte() when posted interrupt is in use, not from the 'trigger'.

Quoted 24 lines
Quoted 16 lines
Quoted 15 lines

drivers/vfio/pci/vfio_pci_intrs.c static int vfio_msi_enable(struct vfio_pci_device *vdev, int nvec, bool msix) · line 326

Quoted 12 lines
drivers/vfio/pci/vfio_pci_intrs.c +11 -0
+		} else if (trigger) {
+			ret = irq_update_devid(irq,
+					       vdev->ctx[vector].trigger, trigger);
+			if (unlikely(ret)) {
+				dev_info(&pdev->dev,
+					 "update devid of %d (token %p) failed: %d\n",
+					 irq, vdev->ctx[vector].trigger, ret);
+				eventfd_ctx_put(trigger);
+				return ret;
+			}
+			irq_bypass_unregister_producer(&vdev->ctx[vector].producer);

Can you explain this ordering, I would have expected that we'd unregister the bypass before we updated the devid. Thanks,

Quoted 1 line

Alex

Quoted 8 lines

Actually, I have explained this in comments above this whole control block. I think it is safe and better to
update devid before irq_bypass_unregister_producer() which switches interrupt process from posted mode
to remap mode. So, if update fails, the posted interrupt can still work.

Anyway, to producer and consumer,  'trigger' is only a token used for pairing them togather.

Quoted 8 lines

The bypass is not a guaranteed mechanism, it's an opportunistic accelerator. If the devid update fails, what state are we left with? The irq action may not work, but the bypass does; maybe; maybe not all the time? This seems to fall into the same consistency in userspace behavior issue identified above. The user ABI is that the vector is disabled if an error is returned. Thanks,

Alex

Thanks, now I see it is better to unregister the bypass before update the devid, will change ordering in next version.

Ben

#8 lore raw

Re: [PATCH v4 3/3] vfio/pci: make use of irq_update_devid and optimize irq ops

Quoted 149 lines · On Thu, 29 Aug 2019 13:40:59 +0800 Ben Luo <[email protected]> wrote:
Quoted 82 lines · Alex Williamson
Quoted 72 lines · On Wed, 28 Aug 2019 18:08:02 +0800 Ben Luo <[email protected]> wrote:
Quoted 37 lines · Alex Williamson
Quoted 34 lines · On Thu, 22 Aug 2019 23:34:43 +0800 Ben Luo <[email protected]> wrote:

When userspace (e.g. qemu) triggers a switch between KVM irqfd and userspace eventfd, only dev_id of irq action (i.e. the "trigger" in this patch's context) will be changed, but a free-then-request-irq action is taken in current code. And, irq affinity setting in VM will also trigger a free-then-request-irq action, which actually changes nothing, but only fires a producer re-registration to update irte in case that posted-interrupt is in use.

This patch makes use of irq_update_devid() and optimize both cases above, which reduces the risk of losing interrupt and also cuts some overhead.

---
   drivers/vfio/pci/vfio_pci_intrs.c | 112 +++++++++++++++++++++++++-------------
   1 file changed, 74 insertions(+), 38 deletions(-)
drivers/vfio/pci/vfio_pci_intrs.c +1 -1
index 3fa3f72..60d3023 100644
@@ -284,70 +284,106 @@ static int vfio_msi_enable(struct vfio_pci_device *vdev, int nvec, bool msix)
   static int vfio_msi_set_vector_signal(struct vfio_pci_device *vdev,
   				      int vector, int fd, bool msix)
   {
+	struct eventfd_ctx *trigger = NULL;
   	struct pci_dev *pdev = vdev->pdev;
-	struct eventfd_ctx *trigger;
   	int irq, ret;
 
   	if (vector < 0 || vector >= vdev->num_ctx)
   		return -EINVAL;
 
Quoted 15 lines

drivers/vfio/pci/vfio_pci_intrs.c static int vfio_msi_enable(struct vfio_pci_device *vdev, int nvec, bool msix) · line 298

Quoted 5 lines
drivers/vfio/pci/vfio_pci_intrs.c +5 -0
+	if (fd >= 0) {
+		trigger = eventfd_ctx_fdget(fd);
+		if (IS_ERR(trigger))
+			return PTR_ERR(trigger);
+	}  

I think this is a user visible change. Previously the vector is disabled first, then if an error occurs re-enabling, we return an errno with the vector disabled. Here we instead fail the ioctl and leave the state as if it had never happened. For instance with QEMU, if they were trying to change from KVM to userspace signaling and entered this condition, previously the interrupt would signal to neither eventfd, now it would continue signaling to KVM. If QEMU's intent was to emulate vector masking, this could induce unhandled interrupts in the guest. Maybe we need a tear-down on fault here to maintain that behavior, or do you see some justification for the change?

Thanks for your comments, this reminds me to think more about the effects to users.

After I reviewed the related code in Qemu and VFIO, I think maybe there is a problem in current behavior
for the signal path changing case. Qemu has neither recovery nor retry code in case that ioctl with
'VFIO_DEVICE_SET_IRQS' command fails, so if the old signal path has been disabled on fault of setting
up new path, the corresponding vector may be disabled forever. Following is an example from qemu's
vfio_msix_vector_do_use():

          ret = ioctl(vdev->vbasedev.fd, VFIO_DEVICE_SET_IRQS, irq_set);
          g_free(irq_set);
          if (ret) {
              error_report("vfio: failed to modify vector, %d", ret);
          }
Quoted 7 lines

I think the singal path before changing should be still working at this moment and the caller should keep it
working if the changing fails, so that at least we still have the old path instead of no path.

For masking vector case, the 'fd' should be -1, and the interrupt will be freed as before this patch.

Quoted 7 lines

QEMU doesn't really have an opportunity to signal an error to the guest, we're emulating the hardware masking of MSI and MSI-X. The guest is simply trying to write a mask bit in the vector, there's no provision in the PCI spec that setting this bit can fail. The current behavior is that the vector is disabled on error. We can argue whether that's the optimal behavior, but it's the existing behavior and changing it would require and evaluation of all existing users.

I totally agree with you that masking of MSI and MSI-X should follow current behavior, and my code does follow this behavior when 'fd' == -1, the interrupt will surely be disabled.

There is another case that 'fd' is not -1, means userspace just want to change the singal path, this time we do have a chance of encountering error on eventfd_ctx_fdget(fd). So, I think it is better to keep the old path working in this case.

But, as you said this may break the expectation of existing users, I make it tear-down on fault in next version.

Quoted 41 lines
Quoted 30 lines
Quoted 16 lines
Quoted 16 lines
drivers/vfio/pci/vfio_pci_intrs.c +11 -1
+
   	irq = pci_irq_vector(pdev, vector);
 
+	/*
+	 * For KVM-VFIO case, interrupt from passthrough device will be directly
+	 * delivered to VM after producer and consumer connected successfully.
+	 * If producer and consumer are disconnected, this interrupt process
+	 * will fall back to remap mode, where interrupt handler uses 'trigger'
+	 * to find the right way to deliver the interrupt to VM. So, it is safe
+	 * to do irq_update_devid() before irq_bypass_unregister_producer() which
+	 * switches interrupt process to remap mode. To producer and consumer,
+	 * 'trigger' is only a token used for pairing them togather.
+	 */
   	if (vdev->ctx[vector].trigger) {
-		free_irq(irq, vdev->ctx[vector].trigger);
Quoted 8 lines

drivers/vfio/pci/vfio_pci_intrs.c static int vfio_msi_enable(struct vfio_pci_device *vdev, int nvec, bool msix) · line 315

Quoted 7 lines
drivers/vfio/pci/vfio_pci_intrs.c +3 -4
-		irq_bypass_unregister_producer(&vdev->ctx[vector].producer);
-		kfree(vdev->ctx[vector].name);
-		eventfd_ctx_put(vdev->ctx[vector].trigger);
-		vdev->ctx[vector].trigger = NULL;
+		if (vdev->ctx[vector].trigger == trigger) {
+			/* switch back to remap mode */
+			irq_bypass_unregister_producer(&vdev->ctx[vector].producer);  

I think we leak the fd context we acquired above in this case.

Thanks for pointing it out, I will fix this in next version.

Quoted 2 lines

Why do we do anything in this case, couldn't we just 'put' the extra ctx and return 0 here?

Sorry for confusing and I do this for a reason,  I will add some more comments like this:

Quoted 11 lines

Unregistration here is for re-resigtraion later, which will trigger the reconnection of irq_bypass producer
and consumer, which in turn calls vmx_update_pi_irte() to update IRTE if posted interrupt is in use.
(vmx_update_pi_irte() will modify IRTE based on the information retrieved from KVM.)
Whether producer token changed or not, irq_bypass_register_producer() is a way (seems the only way) to
update IRTE by VFIO for posted interrupt. The IRTE will be used by IOMMU directly to find the target CPU
for an interrupt posted to VM, since hypervisor is bypassed.

Quoted 4 lines

This is only explaining what the bypass de-registration and re-registration does, not why we need to perform those actions here. If the trigger is the same as that already attached to this vector, why is the IRTE changing? Seems this ought to be a no-op for this vector.

Quoted 5 lines

Sorry, I think it cannot be a no-op here. The interrupt affinity setting in guest also triggers the calling of this function and IRTE will be modified with new affinity information retrieved from KVM's data structure by vmx_update_pi_irte() when posted interrupt is in use, not from the 'trigger'.

Aha, this is the key piece of information I was missing. Please add a comment that even if the trigger is unchanged we need to bounce the bypass to allow affinity changes in the guest to be realized. Thanks,

Alex

Quoted 37 lines
Quoted 23 lines
Quoted 15 lines
Quoted 14 lines

drivers/vfio/pci/vfio_pci_intrs.c static int vfio_msi_enable(struct vfio_pci_device *vdev, int nvec, bool msix) · line 326

Quoted 11 lines
drivers/vfio/pci/vfio_pci_intrs.c +11 -0
+		} else if (trigger) {
+			ret = irq_update_devid(irq,
+					       vdev->ctx[vector].trigger, trigger);
+			if (unlikely(ret)) {
+				dev_info(&pdev->dev,
+					 "update devid of %d (token %p) failed: %d\n",
+					 irq, vdev->ctx[vector].trigger, ret);
+				eventfd_ctx_put(trigger);
+				return ret;
+			}
+			irq_bypass_unregister_producer(&vdev->ctx[vector].producer);  

Can you explain this ordering, I would have expected that we'd unregister the bypass before we updated the devid. Thanks,

Quoted 1 line

Alex

Quoted 8 lines

Actually, I have explained this in comments above this whole control block. I think it is safe and better to
update devid before irq_bypass_unregister_producer() which switches interrupt process from posted mode
to remap mode. So, if update fails, the posted interrupt can still work.

Anyway, to producer and consumer,  'trigger' is only a token used for pairing them togather.

Quoted 8 lines

The bypass is not a guaranteed mechanism, it's an opportunistic accelerator. If the devid update fails, what state are we left with? The irq action may not work, but the bypass does; maybe; maybe not all the time? This seems to fall into the same consistency in userspace behavior issue identified above. The user ABI is that the vector is disabled if an error is returned. Thanks,

Alex

Thanks, now I see it is better to unregister the bypass before update the devid, will change ordering in next version.

Ben