Skip to contentSkip to conversation

vfio/ims: Back guest interrupts from Interrupt Message Store (IMS)

RFC PATCH Superseded Reinette Chatre 3 participants 10 messages Aug 24, 2023 – Aug 25, 2023

Subsystems (2)

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

cover vfio/ims: Back guest interrupts from Interrupt Message Store (IMS)

Hi Everybody,

With Interrupt Message Store (IMS) support introduced in commit 0194425af0c8 ("PCI/MSI: Provide IMS (Interrupt Message Store) support") a device can create a secondary interrupt domain that works side by side with MSI-X on the same device. IMS allows for implementation-specific interrupt storage that is managed by the implementation specific interrupt chip associated with the IMS domain at the time it (the IMS domain) is created for the device via pci_create_ims_domain().

An example usage of IMS is for devices that can have their resources assigned to guests with varying granularity. For example, an accelerator device may support many workqueues and a single workqueue can be composed into a virtual device for use by a guest. Using IMS interrupts for the guest preserves MSI-X for host usage while allowing a significantly larger number of interrupt vectors than allowed by MSI-X. All while enabling usage of the same device driver within the host and guest.

This series introduces a VFIO library (VFIO PCI IMS) for use by virtual devices that support MSI-X interrupts that are backed by IMS interrupts on the host. Specifically, that means that when the virtual device's VFIO_DEVICE_SET_IRQS ioctl() receives a "trigger interrupt" (VFIO_IRQ_SET_ACTION_TRIGGER) for a MSI-X index then VFIO PCI IMS allocates/frees an IMS interrupt on the host.

An IMS interrupt is allocated via pci_ims_alloc_irq() that requires an implementation specific cookie that is opaque to VFIO PCI IMS. This can be a PASID, queue ID, pointer etc. During initialization VFIO PCI IMS learns which PCI device to operate on and what the default cookie should be for any new interrupt allocation. VFIO PCI IMS can also associate a unique cookie with each vector and to maintain this association the library maintains interrupt contexts for the virtual device's lifetime.

Guests may access a virtual device via both 'direct-path', where the guest interacts directly with the underlying hardware, and 'intercepted path', where the virtual device emulates operations. VFIO PCI IMS supports emulated interrupts (better naming suggestions are welcome) to handle 'intercepted path' operations where completion interrupts are signaled from the virtual device, not the underlying hardware.

This library has been tested with a yet to be published VFIO driver for the Intel Data Accelerators (IDXD) present in Intel Xeon CPUs.

While this series contains a working implementation it is presented as an RFC with the goal to obtain feedback on whether VFIO PCI IMS is appropriate for inclusion into VFIO and whether it is (or could be adapted to be) appropriate for support of other planned IMS usages you may be aware of.

Any feedback will be greatly appreciated.

Reinette

Reinette Chatre (3):
vfio/pci: Introduce library allocating from Interrupt Message Store

    (IMS)

vfio/ims: Support emulated interrupts
vfio/ims: Add helper that returns IMS index

 drivers/vfio/pci/Kconfig        |  12 +
 drivers/vfio/pci/Makefile       |   2 +
 drivers/vfio/pci/vfio_pci_ims.c | 499 ++++++++++++++++++++++++++++++++
 include/linux/vfio.h            |  81 ++++++
 4 files changed, 594 insertions(+)
 create mode 100644 drivers/vfio/pci/vfio_pci_ims.c

base-commit: a5e505a99ca748583dbe558b691be1b26f05d678

Signature · 1 line
2.34.1
#1 lore raw

RFC PATCH 1/3 vfio/pci: Introduce library allocating from Interrupt Message Store (IMS)

With Interrupt Message Store (IMS) support introduced in commit 0194425af0c8 ("PCI/MSI: Provide IMS (Interrupt Message Store) support") a device can create a secondary interrupt domain that works side by side with MSI-X on the same device. IMS allows for implementation-specific interrupt storage that is managed by the implementation specific interrupt chip associated with the IMS domain at the time it (the IMS domain) is created for the device via pci_create_ims_domain().

An example usage of IMS is for devices that can have their resources assigned to guests with varying granularity. For example, an accelerator device may support many workqueues and a single workqueue can be composed into a virtual device for use by a guest. Using IMS interrupts for the guest preserves MSI-X for host usage while allowing a significantly larger number of interrupt vectors than allowed by MSI-X. All while enabling usage of the same device driver in the host and guest.

VFIO PCI IMS is a library that supports virtual devices with MSI-X interrupts that are backed by IMS interrupts on the host.

Introduce core functionality of VFIO PCI IMS consisting of IMS interrupt allocation and free, and ability to associate domain specific information with each interrupt. With the generic term of "cookie" (the same name used in the IMS core) this value is opaque to VFIO PCI IMS but important to the virtual device, for example a PASID.

The core VFIO PCI IMS API is:

vfio_pci_ims_init(): Initialize the IMS interrupt context. VFIO PCI
IMS needs to allocate from a device specific IMS domain. The PCI device owning the IMS domain as well as the default cookie are provided using vfio_pci_ims_init(). vfio_pci_ims_init() is intended to be called from the virtual device's vfio_device_ops->open_device().

vfio_pci_ims_free(): Free the IMS interrupt context. vfio_pci_ims_free()
is intended to be called from the virtual device's vfio_device_ops->close_device() after the index as a whole has been disabled via vfio_pci_set_ims_trigger().

vfio_pci_ims_set_cookie(): Set a unique cookie for a particular vector.
Must be called after vfio_pci_ims_init(). For example, the virtual device driver may intercept the guest's MMIO write that configures a new PASID. Calling vfio_pci_ims_set_cookie() with the new PASID value enables subsequent interrupts to be allocated with accurate data.

vfio_pci_set_ims_trigger(): Runtime helper intended to be called
via the VFIO_DEVICE_SET_IRQS ioctl() and more directly on device close to disable the index as whole. vfio_pci_set_ims_trigger() allocates and frees IMS interrupts on the host as MSI-X interrupts in the guest change state. Interrupts are always dynamically allocated, enabling a user of this helper to clear the VFIO_IRQ_INFO_NORESIZE flag in response to user space's request for interrupt information.

While interrupts are dynamically allocated and freed the interrupt contexts are maintained from initial allocation to virtual device shutdown. This is done to maintain the vector specific cookies.

The interrupt context can be modified via separate flows. For example, vfio_pci_set_ims_trigger() can modify the context while handling an ioctl(), while vfio_pci_ims_set_cookie() can modify the interrupt context as part of handling an intercepted MMIO write. Concurrent changes to the interrupt context are protected by a mutex (ctx_mutex).

VFIO PCI IMS manages IMS interrupts of a PCI device. This prompts it to be located in drivers/vfio/pci, enabled with a new config option CONFIG_VFIO_PCI_IMS. The library does not depend on the PCI VFIO bus driver so there is not a dependency on CONFIG_VFIO_PCI.

---
 drivers/vfio/pci/Kconfig        |  12 +
 drivers/vfio/pci/Makefile       |   2 +
 drivers/vfio/pci/vfio_pci_ims.c | 389 ++++++++++++++++++++++++++++++++
 include/linux/vfio.h            |  60 +++++
 4 files changed, 463 insertions(+)
 create mode 100644 drivers/vfio/pci/vfio_pci_ims.c
drivers/vfio/pci/Kconfig +12 -0
index 86bb7835cf3c..cf9e4ee14574 100644
@@ -24,6 +24,18 @@ config VFIO_PCI
 
 	  If you don't know what to do here, say N.
 
+config VFIO_PCI_IMS
+	tristate "VFIO library supporting Interrupt Message Store (IMS)"
+	depends on PCI_MSI
+	select EVENTFD
+	select IRQ_BYPASS_MANAGER
+	default n
+	help
+	  VFIO library used by virtual devices with MSI-X interrupts
+	  that are backed by IMS interrupts on the host.
+
+	  If you don't know what to do here, say N.
+
 if VFIO_PCI
 config VFIO_PCI_VGA
 	bool "Generic VFIO PCI support for VGA devices"
drivers/vfio/pci/Makefile +2 -0
index 24c524224da5..860e1ff5ef60 100644
@@ -8,6 +8,8 @@ vfio-pci-y := vfio_pci.o
 vfio-pci-$(CONFIG_VFIO_PCI_IGD) += vfio_pci_igd.o
 obj-$(CONFIG_VFIO_PCI) += vfio-pci.o
 
+obj-$(CONFIG_VFIO_PCI_IMS) += vfio_pci_ims.o
+
 obj-$(CONFIG_MLX5_VFIO_PCI)           += mlx5/
 
 obj-$(CONFIG_HISI_ACC_VFIO_PCI) += hisilicon/
drivers/vfio/pci/vfio_pci_ims.c new file +389 -0

389 lines not on this page · raw message

include/linux/vfio.h +60 -0

82 lines not on this page · raw message

Signature · 1 line
2.34.1
#2 lore raw

RFC PATCH 2/3 vfio/ims: Support emulated interrupts

In reply to Reinette Chatre

Access from a guest to a virtual device may be either 'direct-path', where the guest interacts directly with the underlying hardware, or 'intercepted path' where the virtual device emulates operations.

Support emulated interrupts that can be used to handle 'intercepted path' operations. For example, a virtual device may use 'intercepted path' for configuration. Doing so, configuration requests intercepted by the virtual device driver are handled within the virtual device driver with completion signaled to the guest without interacting with the underlying hardware.

Add vfio_pci_ims_set_emulated() and vfio_pci_ims_send_signal() to the VFIO PCI IMS API. vfio_pci_ims_set_emulated() configures a range of interrupts that are emulated. Any range of interrupts can be configured as emulated as long as no IMS interrupt has previously been allocated at that vector. The virtual device uses vfio_pci_ims_send_signal() to trigger interrupts in the guest.

---
 drivers/vfio/pci/vfio_pci_ims.c | 117 ++++++++++++++++++++++++++++----
 include/linux/vfio.h            |  14 ++++
 2 files changed, 117 insertions(+), 14 deletions(-)
drivers/vfio/pci/vfio_pci_ims.c +103 -14
index 0926eb921351..fe5b3484ad34 100644
@@ -17,16 +17,18 @@
 #include <linux/xarray.h>
 
 /*
- * IMS interrupt context.
- * @name:	Name of device associated with interrupt.
+ * Interrupt context. Used for emulated as well as IMS interrupts.
+ * @emulated:	(IMS and emulated) true if context belongs to emulated interrupt.
+ * @name:	(IMS and emulated) Name of device associated with interrupt.
  *		Provided to request_irq().
- * @trigger:	eventfd associated with interrupt.
- * @producer:	Interrupt's registered IRQ bypass producer.
- * @ims_id:	Interrupt index associated with IMS interrupt.
- * @virq:	Linux IRQ number associated with IMS interrupt.
- * @icookie:	Cookie used by irqchip driver.
+ * @trigger:	(IMS and emulated) eventfd associated with interrupt.
+ * @producer:	(IMS only) Interrupt's registered IRQ bypass producer.
+ * @ims_id:	(IMS only) Interrupt index associated with IMS interrupt.
+ * @virq:	(IMS only) Linux IRQ number associated with IMS interrupt.
+ * @icookie:	(IMS only) Cookie used by irqchip driver.
  */
 struct vfio_pci_ims_ctx {
+	bool				emulated;
 	char				*name;
 	struct eventfd_ctx		*trigger;
 	struct irq_bypass_producer	producer;
@@ -35,6 +37,31 @@ struct vfio_pci_ims_ctx {
 	union msi_instance_cookie	icookie;
 };
 
+/*
+ * Send signal to the eventfd.
+ * @vdev:	VFIO device
+ * @vector:	MSI-X vector of @vdev for which interrupt will be signaled
+ *
+ * Intended for use to send signal for emulated interrupts.
+ */
+void vfio_pci_ims_send_signal(struct vfio_device *vdev, unsigned int vector)
+{
+	struct vfio_pci_ims *ims = &vdev->ims;
+	struct vfio_pci_ims_ctx *ctx;
+
+	mutex_lock(&ims->ctx_mutex);
+	ctx = xa_load(&ims->ctx, vector);
+
+	if (WARN_ON_ONCE(!ctx || !ctx->emulated || !ctx->trigger)) {
+		mutex_unlock(&ims->ctx_mutex);
+		return;
+	}
+
+	eventfd_signal(ctx->trigger, 1);
+	mutex_unlock(&ims->ctx_mutex);
+}
+EXPORT_SYMBOL_GPL(vfio_pci_ims_send_signal);
+
 static irqreturn_t vfio_pci_ims_irq_handler(int irq, void *arg)
 {
 	struct eventfd_ctx *trigger = arg;
@@ -46,7 +73,8 @@ static irqreturn_t vfio_pci_ims_irq_handler(int irq, void *arg)
 /*
  * Free the interrupt associated with @ctx.
  *
- * Free interrupt from the underlying PCI device's IMS domain.
+ * For an emulated interrupt there is nothing to do. For an IMS interrupt
+ * the interrupt is freed from the underlying PCI device's IMS domain.
  */
 static void vfio_pci_ims_irq_free(struct vfio_pci_ims *ims,
 				  struct vfio_pci_ims_ctx *ctx)
@@ -55,6 +83,9 @@ static void vfio_pci_ims_irq_free(struct vfio_pci_ims *ims,
 
 	lockdep_assert_held(&ims->ctx_mutex);
 
+	if (ctx->emulated)
+		return;
+
 	irq_map.index = ctx->ims_id;
 	irq_map.virq = ctx->virq;
 	pci_ims_free_irq(ims->pdev, irq_map);
@@ -65,7 +96,8 @@ static void vfio_pci_ims_irq_free(struct vfio_pci_ims *ims,
 /*
  * Allocate an interrupt for @ctx.
  *
- * Allocate interrupt from the underlying PCI device's IMS domain.
+ * For an emulated interrupt there is nothing to do. For an IMS interrupt
+ * the interrupt is allocated from the underlying PCI device's IMS domain.
  */
 static int vfio_pci_ims_irq_alloc(struct vfio_pci_ims *ims,
 				  struct vfio_pci_ims_ctx *ctx)
@@ -74,6 +106,9 @@ static int vfio_pci_ims_irq_alloc(struct vfio_pci_ims *ims,
 
 	lockdep_assert_held(&ims->ctx_mutex);
 
+	if (ctx->emulated)
+		return -EINVAL;
+
 	irq_map = pci_ims_alloc_irq(ims->pdev, &ctx->icookie, NULL);
 	if (irq_map.index < 0)
 		return irq_map.index;
@@ -133,9 +168,11 @@ static int vfio_pci_ims_set_vector_signal(struct vfio_device *vdev,
 	ctx = xa_load(&ims->ctx, vector);
 
 	if (ctx && ctx->trigger) {
-		irq_bypass_unregister_producer(&ctx->producer);
-		free_irq(ctx->virq, ctx->trigger);
-		vfio_pci_ims_irq_free(ims, ctx);
+		if (!ctx->emulated) {
+			irq_bypass_unregister_producer(&ctx->producer);
+			free_irq(ctx->virq, ctx->trigger);
+			vfio_pci_ims_irq_free(ims, ctx);
+		}
 		kfree(ctx->name);
 		ctx->name = NULL;
 		eventfd_ctx_put(ctx->trigger);
@@ -163,6 +200,9 @@ static int vfio_pci_ims_set_vector_signal(struct vfio_device *vdev,
 
 	ctx->trigger = trigger;
 
+	if (ctx->emulated)
+		return 0;
+
 	ret = vfio_pci_ims_irq_alloc(ims, ctx);
 	if (ret < 0)
 		goto out_put_eventfd_ctx;
@@ -219,8 +259,8 @@ static int vfio_pci_ims_set_block(struct vfio_device *vdev, unsigned int start,
 }
 
 /*
- * Manage Interrupt Message Store (IMS) interrupts on the host that are
- * backing guest MSI-X vectors.
+ * Manage Interrupt Message Store (IMS) or emulated interrupts on the
+ * host that are backing guest MSI-X vectors.
  *
  * @vdev:	 VFIO device
  * @index:	 Type of guest vectors to set up.  Must be
@@ -360,6 +400,10 @@ int vfio_pci_ims_set_cookie(struct vfio_device *vdev, unsigned int vector,
 	mutex_lock(&ims->ctx_mutex);
 	ctx = xa_load(&ims->ctx, vector);
 	if (ctx) {
+		if (WARN_ON_ONCE(ctx->emulated)) {
+			ret = -EINVAL;
+			goto out_unlock;
+		}
 		ctx->icookie = *icookie;
 		goto out_unlock;
 	}
@@ -385,5 +429,50 @@ int vfio_pci_ims_set_cookie(struct vfio_device *vdev, unsigned int vector,
 }
 EXPORT_SYMBOL_GPL(vfio_pci_ims_set_cookie);
 
+/*
+ * Set range of interrupts that will be emulated instead of backed by IMS.
+ *
+ * Return: error code on failure (-EBUSY if the vector is not available,
+ * -ENOMEM on allocation failure), 0 on success
+ */
+int vfio_pci_ims_set_emulated(struct vfio_device *vdev, unsigned int start,
+			      unsigned int count)
+{
+	struct vfio_pci_ims *ims = &vdev->ims;
+	struct vfio_pci_ims_ctx *ctx;
+	unsigned long i, j;
+	int ret = 0;
+
+	mutex_lock(&ims->ctx_mutex);
+
+	for (i = start; i < start + count; i++) {
+		ctx = kzalloc(sizeof(*ctx), GFP_KERNEL_ACCOUNT);
+		if (!ctx) {
+			ret = -ENOMEM;
+			goto out_err;
+		}
+		ctx->emulated = true;
+		ret = xa_insert(&ims->ctx, i, ctx, GFP_KERNEL_ACCOUNT);
+		if (ret) {
+			kfree(ctx);
+			goto out_err;
+		}
+	}
+
+	mutex_unlock(&ims->ctx_mutex);
+	return 0;
+
+out_err:
+	for (j = start; j < i; j++) {
+		ctx = xa_load(&ims->ctx, j);
+		xa_erase(&ims->ctx, j);
+		kfree(ctx);
+	}
+	mutex_unlock(&ims->ctx_mutex);
+
+	return ret;
+}
+EXPORT_SYMBOL_GPL(vfio_pci_ims_set_emulated);
+
 MODULE_LICENSE("GPL");
 MODULE_AUTHOR("Intel Corporation");
include/linux/vfio.h +14 -0
index aa54239bff4d..906220002ff4 100644
@@ -334,6 +334,9 @@ int vfio_pci_set_ims_trigger(struct vfio_device *vdev, unsigned int index,
 void vfio_pci_ims_init(struct vfio_device *vdev, struct pci_dev *pdev,
 		       union msi_instance_cookie *default_cookie);
 void vfio_pci_ims_free(struct vfio_device *vdev);
+int vfio_pci_ims_set_emulated(struct vfio_device *vdev, unsigned int start,
+			      unsigned int count);
+void vfio_pci_ims_send_signal(struct vfio_device *vdev, unsigned int vector);
 int vfio_pci_ims_set_cookie(struct vfio_device *vdev, unsigned int vector,
 			    union msi_instance_cookie *icookie);
 #else
@@ -353,6 +356,17 @@ static inline void vfio_pci_ims_init(struct vfio_device *vdev,
 
 static inline void vfio_pci_ims_free(struct vfio_device *vdev) {}
 
+static inline int vfio_pci_ims_set_emulated(struct vfio_device *vdev,
+					    unsigned int start,
+					    unsigned int count)
+{
+	return -EOPNOTSUPP;
+}
+
+static inline void vfio_pci_ims_send_signal(struct vfio_device *vdev,
+					    unsigned int vector)
+{}
+
 static inline int vfio_pci_ims_set_cookie(struct vfio_device *vdev,
 					  unsigned int vector,
 					  union msi_instance_cookie *icookie)
Signature · 1 line
2.34.1
#3 lore raw

Re: [RFC PATCH 2/3] vfio/ims: Support emulated interrupts

Quoted 10 lines · Reinette Chatre

Access from a guest to a virtual device may be either 'direct-path', where the guest interacts directly with the underlying hardware, or 'intercepted path' where the virtual device emulates operations.

Support emulated interrupts that can be used to handle 'intercepted path' operations. For example, a virtual device may use 'intercepted path' for configuration. Doing so, configuration requests intercepted by the virtual device driver are handled within the virtual device driver with completion signaled to the guest without interacting with the underlying hardware.

Why does this have anything to do with IMS? I thought the point here was that IMS was some back end to the MSI-X emulation - should a purely emulated interrupt logically be part of the MSI code, not IMS?

Jason

#4 lore raw

Re: [RFC PATCH 2/3] vfio/ims: Support emulated interrupts

Hi Jason,

Quoted 12 lines · Jason Gunthorpe
Quoted 10 lines · Reinette Chatre

Access from a guest to a virtual device may be either 'direct-path', where the guest interacts directly with the underlying hardware, or 'intercepted path' where the virtual device emulates operations.

Support emulated interrupts that can be used to handle 'intercepted path' operations. For example, a virtual device may use 'intercepted path' for configuration. Doing so, configuration requests intercepted by the virtual device driver are handled within the virtual device driver with completion signaled to the guest without interacting with the underlying hardware.

Quoted 3 lines

Why does this have anything to do with IMS? I thought the point here was that IMS was some back end to the MSI-X emulation - should a purely emulated interrupt logically be part of the MSI code, not IMS?

You are correct, an emulated interrupt is not unique to IMS.

The target usage of this library is by pure(?) VFIO devices (struct vfio_device). These are virtual devices that are composed by separate VFIO drivers. For example, a single resource of an accelerator device can be composed into a stand-alone virtual device for use by a guest.

Through its API and implementation the current VFIO MSI code expects to work with actual PCI devices (struct vfio_pci_core_device). With the target usage not being an actual PCI device the VFIO MSI code was not found to be a good fit and thus this implementation does not build on current MSI support.

Reinette

#5 lore raw

RE: [RFC PATCH 2/3] vfio/ims: Support emulated interrupts

Quoted 29 lines

From: Chatre, Reinette <[email protected]>
Sent: Friday, August 25, 2023 1:19 AM

Hi Jason,

Quoted 12 lines · Jason Gunthorpe
Quoted 10 lines · Reinette Chatre

Access from a guest to a virtual device may be either 'direct-path', where the guest interacts directly with the underlying hardware, or 'intercepted path' where the virtual device emulates operations.

Support emulated interrupts that can be used to handle 'intercepted path' operations. For example, a virtual device may use 'intercepted path' for configuration. Doing so, configuration requests intercepted by the virtual device driver are handled within the virtual device driver with completion signaled to the guest without interacting with the underlying hardware.

Quoted 3 lines

Why does this have anything to do with IMS? I thought the point here was that IMS was some back end to the MSI-X emulation - should a purely emulated interrupt logically be part of the MSI code, not IMS?

You are correct, an emulated interrupt is not unique to IMS.

The target usage of this library is by pure(?) VFIO devices (struct vfio_device). These are virtual devices that are composed by separate VFIO drivers. For example, a single resource of an accelerator device can be composed into a stand-alone virtual device for use by a guest.

Quoted 7 lines

Through its API and implementation the current VFIO MSI code expects to work with actual PCI devices (struct vfio_pci_core_device). With the target usage not being an actual PCI device the VFIO MSI code was not found to be a good fit and thus this implementation does not build on current MSI support.

This might be achieved by creating a structure vfio_pci_intr_ctx included by vfio_pci_core_device and other vfio device types. Then move vfio_pci_intr.c to operate on vfio_pci_intr_ctx instead of vfio_pci_core_device to make MSI frontend code sharable by both PCI devices or virtual devices (mdev or SIOV).

Then there is only one irq_ctx. Within the ctx we can abstract backend ops, e.g. enable/disble_msi(), alloc/free_ctx(), alloc/free_irq(), etc. to accommodate pci MSI/MSI-X, IMS, or emulation.

The unknown risk is whether a clear abstraction can be defined. If in the end the common library contains many if-else to handle subtle backend differences then it might not be a good choice...

#6 lore raw

Re: [RFC PATCH 2/3] vfio/ims: Support emulated interrupts

Hi Kevin,

Quoted 37 lines · Kevin
Quoted 29 lines

From: Chatre, Reinette <[email protected]>
Sent: Friday, August 25, 2023 1:19 AM

Hi Jason,

Quoted 12 lines · Jason Gunthorpe
Quoted 10 lines · Reinette Chatre

Access from a guest to a virtual device may be either 'direct-path', where the guest interacts directly with the underlying hardware, or 'intercepted path' where the virtual device emulates operations.

Support emulated interrupts that can be used to handle 'intercepted path' operations. For example, a virtual device may use 'intercepted path' for configuration. Doing so, configuration requests intercepted by the virtual device driver are handled within the virtual device driver with completion signaled to the guest without interacting with the underlying hardware.

Quoted 3 lines

Why does this have anything to do with IMS? I thought the point here was that IMS was some back end to the MSI-X emulation - should a purely emulated interrupt logically be part of the MSI code, not IMS?

You are correct, an emulated interrupt is not unique to IMS.

The target usage of this library is by pure(?) VFIO devices (struct vfio_device). These are virtual devices that are composed by separate VFIO drivers. For example, a single resource of an accelerator device can be composed into a stand-alone virtual device for use by a guest.

Quoted 7 lines

Through its API and implementation the current VFIO MSI code expects to work with actual PCI devices (struct vfio_pci_core_device). With the target usage not being an actual PCI device the VFIO MSI code was not found to be a good fit and thus this implementation does not build on current MSI support.

Quoted 5 lines

This might be achieved by creating a structure vfio_pci_intr_ctx included by vfio_pci_core_device and other vfio device types. Then move vfio_pci_intr.c to operate on vfio_pci_intr_ctx instead of vfio_pci_core_device to make MSI frontend code sharable by both PCI devices or virtual devices (mdev or SIOV).

Thank you very much Kevin.

For data there is the per-device context related to interrupts as well as the per-interrupt context. These contexts are different between the different device types and interrupt types and if I understand correctly both context types (per-device as well as per-interrupt) should be made opaque within the new vfio_pci_intr.c. Additionally, with different mechanisms (for example, the locking required) to interact with the different device types the code is also device (PCI or virtual) specific. Considering how both the data and code would be opaque to this new library it looks to me as though in some aspects this library may thus appear as a skeleton with interrupt and device specific logic contained in its users.

It is not obvious to me where the MSI frontend and backend boundary is. If I understand correctly majority of the code in vfio_pci_intrs.c would move to callbacks. Potentially leaving little to be shared, vfio_pci_set_irqs_ioctl() and vfio_msihandler() seems like candidates.

To clarify it sounds to me as though this design is motivated by the requirement to bring emulated interrupt support to the current INTx/MSI/MSI-X support on PCI devices? It is not clear to me how this feature will be used since interrupts need to be signaled from the driver instead of the hardware. I am missing something.

Quoted 7 lines

Then there is only one irq_ctx. Within the ctx we can abstract backend ops, e.g. enable/disble_msi(), alloc/free_ctx(), alloc/free_irq(), etc. to accommodate pci MSI/MSI-X, IMS, or emulation.

The unknown risk is whether a clear abstraction can be defined. If in the end the common library contains many if-else to handle subtle backend differences then it might not be a good choice...

Thank you very much for your guidance. Instead of Jason's expectation that IMS would be a backend of MSI-X this will change to IMS and MSI-X both being a backend to a new interface. It is difficult for me to envision the end result so I will work on an implementation based on my understanding of your proposal that we can use for further discussion.

Reinette

#7 lore raw

Re: [RFC PATCH 2/3] vfio/ims: Support emulated interrupts

Quoted 5 lines · Reinette Chatre

Thank you very much for your guidance. Instead of Jason's expectation that IMS would be a backend of MSI-X this will change to IMS and MSI-X both being a backend to a new interface. It is difficult for me to envision the end result so I will work on an implementation based on my understanding of your proposal that we can use for further discussion.

I think the point is that emulating MSI-X is kind of tricky and should be common VFIO code, regardless of what path it takes.

So I would expect some library code to do this, entry points the vfio_device can hook into its callbacks (eg config space and rw of a page)

Then the other side would connect to the physical implementation, sw,ims,msi-x,future

Jason

#8 lore raw

Re: [RFC PATCH 2/3] vfio/ims: Support emulated interrupts

Quoted 11 lines · Jason Gunthorpe
Quoted 5 lines · Reinette Chatre

Thank you very much for your guidance. Instead of Jason's expectation that IMS would be a backend of MSI-X this will change to IMS and MSI-X both being a backend to a new interface. It is difficult for me to envision the end result so I will work on an implementation based on my understanding of your proposal that we can use for further discussion.

I think the point is that emulating MSI-X is kind of tricky and should be common VFIO code, regardless of what path it takes.

Quoted 7 lines

So I would expect some library code to do this, entry points the vfio_device can hook into its callbacks (eg config space and rw of a page)

Then the other side would connect to the physical implementation, sw,ims,msi-x,future

Thank you very much Jason. I am thus hearing the same from you and Kevin. I'll work on this.

Reinette

#9 lore raw

RFC PATCH 3/3 vfio/ims: Add helper that returns IMS index

In reply to Reinette Chatre

A virtual device driver needs to facilitate translation between the guest's MSI-X interrupt and the backing IMS interrupt with which the physical device is programmed. For example, the guest may need to obtain the IMS index from the virtual device driver that it needs to program into descriptors submitted to the device to ensure that the completion interrupts are generated correctly.

Introduce vfio_pci_ims_hwirq() to VFIO PCI IMS as a helper that returns the IMS interrupt index backing a provided MSI-X interrupt index belonging to a guest.

---
 drivers/vfio/pci/vfio_pci_ims.c | 21 +++++++++++++++++++++
 include/linux/vfio.h            |  7 +++++++
 2 files changed, 28 insertions(+)
drivers/vfio/pci/vfio_pci_ims.c +21 -0
index fe5b3484ad34..27711bc7a6c2 100644
@@ -37,6 +37,27 @@ struct vfio_pci_ims_ctx {
 	union msi_instance_cookie	icookie;
 };
 
+/*
+ * Return IMS index of IMS interrupt backing MSI-X interrupt @vector
+ */
+int vfio_pci_ims_hwirq(struct vfio_device *vdev, unsigned int vector)
+{
+	struct vfio_pci_ims *ims = &vdev->ims;
+	struct vfio_pci_ims_ctx *ctx;
+	int id;
+
+	mutex_lock(&ims->ctx_mutex);
+	ctx = xa_load(&ims->ctx, vector);
+	if (!ctx || ctx->emulated)
+		id = -EINVAL;
+	else
+		id = ctx->ims_id;
+	mutex_unlock(&ims->ctx_mutex);
+
+	return id;
+}
+EXPORT_SYMBOL_GPL(vfio_pci_ims_hwirq);
+
 /*
  * Send signal to the eventfd.
  * @vdev:	VFIO device
include/linux/vfio.h +7 -0
index 906220002ff4..1c0e1bddd95b 100644
@@ -339,6 +339,7 @@ int vfio_pci_ims_set_emulated(struct vfio_device *vdev, unsigned int start,
 void vfio_pci_ims_send_signal(struct vfio_device *vdev, unsigned int vector);
 int vfio_pci_ims_set_cookie(struct vfio_device *vdev, unsigned int vector,
 			    union msi_instance_cookie *icookie);
+int vfio_pci_ims_hwirq(struct vfio_device *vdev, unsigned int vector);
 #else
 static inline int vfio_pci_set_ims_trigger(struct vfio_device *vdev,
 					   unsigned int index,
@@ -374,6 +375,12 @@ static inline int vfio_pci_ims_set_cookie(struct vfio_device *vdev,
 	return -EOPNOTSUPP;
 }
 
+static inline int vfio_pci_ims_hwirq(struct vfio_device *vdev,
+				     unsigned int index)
+{
+	return -EOPNOTSUPP;
+}
+
 #endif /* CONFIG_VFIO_PCI_IMS */
 
 #endif /* VFIO_H */
Signature · 1 line
2.34.1