Skip to contentSkip to conversation

Add VT-d Posted-Interrupts support

PATCH Superseded Feng Wu 3 participants 38 messages Dec 3, 2014 – Dec 11, 2014

Subsystems (6)
Documentation x86 KVM KVM x86 +2 more subsystems

v2 of 14 Superseded Latest is v14 →

lore
Share & tools
lore

Feng Wu

author
#0 lore raw

v2 cover Add VT-d Posted-Interrupts support

VT-d Posted-Interrupts is an enhancement to CPU side Posted-Interrupt. With VT-d Posted-Interrupts enabled, external interrupts from direct-assigned devices can be delivered to guests without VMM intervention when guest is running in non-root mode.

You can find the VT-d Posted-Interrtups Spec. in the following URL:
http://www.intel.com/content/www/us/en/intelligent-systems/intel-technology/vt-directed-io-spec.html

v1->v2:
* Use VFIO framework to enable this feature, the VFIO part of this series is base on Eric's patch "[PATCH v3 0/8] KVM-VFIO IRQ forward control"
* Rebase this patchset on git://git.kernel.org/pub/scm/linux/kernel/git/tip/tip.git,
then revise some irq logic based on the new hierarchy irqdomain patches provided by Jiang Liu <jiang.liu-VuQAYsv1563Yd54FQh9/[email protected]>

This patch series is made of the following groups:
1-6: Some preparation changes in iommu and irq component, this is based on the
new hierarchy irqdomain logic.
7-9, 25: IOMMU changes for VT-d Posted-Interrupts, such as, feature detection,
command line parameter.
10-16, 21-24: Changes related to KVM itself.
17-19: Changes in VFIO component, this part was previously sent out as "[RFC PATCH v2 0/2] kvm-vfio: implement the vfio skeleton for VT-d Posted-Interrupts" 20: x86 irq related changes

Feng Wu (25):
genirq: Introduce irq_set_vcpu_affinity() to target an interrupt to a
VCPU
iommu: Add new member capability to struct irq_remap_ops iommu, x86: Define new irte structure for VT-d Posted-Interrupts iommu, x86: Implement irq_set_vcpu_affinity for intel_ir_chip x86, irq: Implement irq_set_vcpu_affinity for pci_msi_ir_controller iommu, x86: No need to migrating irq for VT-d Posted-Interrupts iommu, x86: Add cap_pi_support() to detect VT-d PI capability iommu, x86: Add intel_irq_remapping_capability() for Intel iommu, x86: define irq_remapping_cap()
KVM: change struct pi_desc for VT-d Posted-Interrupts
KVM: Add some helper functions for Posted-Interrupts
KVM: Initialize VT-d Posted-Interrupts Descriptor
KVM: Define a new interface kvm_find_dest_vcpu() for VT-d PI
KVM: Get Posted-Interrupts descriptor address from struct kvm_vcpu
KVM: Make struct kvm_irq_routing_table accessible
KVM: make kvm_set_msi_irq() public
KVM: kvm-vfio: User API for VT-d Posted-Interrupts
KVM: kvm-vfio: implement the VFIO skeleton for VT-d Posted-Interrupts
KVM: x86: kvm-vfio: VT-d posted-interrupts setup
x86, irq: Define a global vector for VT-d Posted-Interrupts
KVM: Update Posted-Interrupts descriptor during vCPU scheduling
KVM: Change NDST field after vCPU scheduling
KVM: Add the handler for Wake-up Vector
KVM: Suppress posted-interrupt when 'SN' is set iommu/vt-d: Add a command line parameter for VT-d posted-interrupts

 Documentation/kernel-parameters.txt        |    1 +
 Documentation/virtual/kvm/devices/vfio.txt |    9 +
 arch/x86/include/asm/entry_arch.h          |    2 +
 arch/x86/include/asm/hardirq.h             |    1 +
 arch/x86/include/asm/hw_irq.h              |    2 +
 arch/x86/include/asm/irq_remapping.h       |   11 ++
 arch/x86/include/asm/irq_vectors.h         |    1 +
 arch/x86/include/asm/kvm_host.h            |   14 ++
 arch/x86/kernel/apic/msi.c                 |    1 +
 arch/x86/kernel/entry_64.S                 |    2 +
 arch/x86/kernel/irq.c                      |   27 +++
 arch/x86/kernel/irqinit.c                  |    2 +
 arch/x86/kvm/Makefile                      |    2 +-
 arch/x86/kvm/kvm_vfio_x86.c                |   68 ++++++++
 arch/x86/kvm/vmx.c                         |  251 +++++++++++++++++++++++++++-
 arch/x86/kvm/x86.c                         |   38 ++++-
 drivers/iommu/intel_irq_remapping.c        |   64 +++++++
 drivers/iommu/irq_remapping.c              |   24 +++-
 drivers/iommu/irq_remapping.h              |    8 +
 include/linux/dmar.h                       |   32 ++++
 include/linux/intel-iommu.h                |    1 +
 include/linux/irq.h                        |    7 +
 include/linux/kvm_host.h                   |   43 +++++
 include/uapi/linux/kvm.h                   |   10 +
 kernel/irq/chip.c                          |   14 ++
 kernel/irq/manage.c                        |   20 +++
 virt/kvm/irq_comm.c                        |   43 +++++-
 virt/kvm/irqchip.c                         |   11 --
 virt/kvm/kvm_main.c                        |   14 ++
 virt/kvm/vfio.c                            |  103 ++++++++++++
 30 files changed, 799 insertions(+), 27 deletions(-)
 create mode 100644 arch/x86/kvm/kvm_vfio_x86.c
author
#1 lore raw

PATCH v2 1/25 genirq: Introduce irq_set_vcpu_affinity() to target an interrupt to a VCPU

From: Jiang Liu <jiang.liu-VuQAYsv1563Yd54FQh9/[email protected]>

With Posted-Interrupts support in Intel CPU and IOMMU, an external interrupt from assigned-devices could be directly delivered to a virtual CPU in a virtual machine. Instead of hacking KVM and Intel IOMMU drivers, we propose a platform independent interface to target an interrupt to a specific virtual CPU in a virtual machine, or set virtual CPU affinity for an interrupt.

By adopting this new interface and the hierarchy irqdomain, we could easily support posted-interrupts on Intel platforms, and also provide flexible enough interfaces for other platforms to support similar features.

We may also cooperate between set_affinity() and set_vcpu_affinity() in IRQ core or irq chip drivers.

Here is the usage scenario for this interface:
Guest update MSI/MSI-X interrupt configuration

	-->QEMU and KVM handle this
	-->KVM call this interface (passing posted interrupts descriptor
	   and guest vector)
	-->irq core will transfer the control to IOMMU
	-->IOMMU will do the real work of updating IRTE (IRTE has new
	   format for VT-d Posted-Interrupts)
---
 include/linux/irq.h |    4 ++++
 kernel/irq/chip.c   |   14 ++++++++++++++
 kernel/irq/manage.c |   20 ++++++++++++++++++++
 3 files changed, 38 insertions(+), 0 deletions(-)
include/linux/irq.h +4 -0
index f26e736..83abafc 100644
@@ -324,6 +324,8 @@ static inline irq_hw_number_t irqd_to_hwirq(struct irq_data *d)
  *				irq_request_resources
  * @irq_compose_msi_msg:	optional to compose message content for MSI
  * @irq_write_msi_msg:	optional to write message content for MSI
+ * @irq_set_vcpu_affinity:	optional to target a virtual CPU in a virtual
+ *				machine
  * @flags:		chip specific flags
  */
 struct irq_chip {
@@ -362,6 +364,7 @@ struct irq_chip {
 
 	void		(*irq_compose_msi_msg)(struct irq_data *data, struct msi_msg *msg);
 	void		(*irq_write_msi_msg)(struct irq_data *data, struct msi_msg *msg);
+	int		(*irq_set_vcpu_affinity)(struct irq_data *data, void *vcpu_info);
 
 	unsigned long	flags;
 };
@@ -416,6 +419,7 @@ extern void irq_cpu_online(void);
 extern void irq_cpu_offline(void);
 extern int irq_set_affinity_locked(struct irq_data *data,
 				   const struct cpumask *cpumask, bool force);
+extern int irq_set_vcpu_affinity(unsigned int irq, void *vcpu_info);
 
 #if defined(CONFIG_SMP) && defined(CONFIG_GENERIC_PENDING_IRQ)
 void irq_move_irq(struct irq_data *data);
kernel/irq/chip.c +14 -0
index 6f1c7a5..fe0908f 100644
@@ -948,6 +948,20 @@ int irq_chip_retrigger_hierarchy(struct irq_data *data)
 
 	return -ENOSYS;
 }
+
+/**
+ * irq_chip_set_vcpu_affinity_parent - Set vcpu affinity on the parent interrupt
+ * @data:	Pointer to interrupt specific data
+ * @dest:	The vcpu affinity information
+ */
+int irq_chip_set_vcpu_affinity_parent(struct irq_data *data, void *vcpu_info)
+{
+	data = data->parent_data;
+	if (data->chip->irq_set_vcpu_affinity)
+		return data->chip->irq_set_vcpu_affinity(data, vcpu_info);
+
+	return -ENOSYS;
+}
 #endif
 
 /**
kernel/irq/manage.c +20 -0
index 8069237..bd3a1ba 100644
@@ -247,6 +247,26 @@ int irq_set_affinity_hint(unsigned int irq, const struct cpumask *m)
 }
 EXPORT_SYMBOL_GPL(irq_set_affinity_hint);
 
+int irq_set_vcpu_affinity(unsigned int irq, void *vcpu_info)
+{
+	struct irq_desc *desc = irq_to_desc(irq);
+	struct irq_chip *chip;
+	unsigned long flags;
+	int ret = -ENOSYS;
+
+	if (!desc)
+		return -EINVAL;
+
+	raw_spin_lock_irqsave(&desc->lock, flags);
+	chip = desc->irq_data.chip;
+	if (chip && chip->irq_set_vcpu_affinity)
+		ret = chip->irq_set_vcpu_affinity(irq_desc_get_irq_data(desc),
+						  vcpu_info);
+	raw_spin_unlock_irqrestore(&desc->lock, flags);
+	return ret;
+}
+EXPORT_SYMBOL_GPL(irq_set_vcpu_affinity);
+
 static void irq_affinity_notify(struct work_struct *work)
 {
 	struct irq_affinity_notify *notify =
Signature · 1 line
1.7.1
author
#2 lore raw

PATCH v2 2/25 iommu: Add new member capability to struct irq_remap_ops

In reply to Feng Wu

This patch adds a new member capability to struct irq_remap_ops, this new function ops can be used to check whether some features are supported, such as VT-d Posted-Interrupts.

---
 arch/x86/include/asm/irq_remapping.h |    4 ++++
 drivers/iommu/irq_remapping.h        |    4 ++++
 2 files changed, 8 insertions(+), 0 deletions(-)
arch/x86/include/asm/irq_remapping.h +4 -0
index 6ba2431..f67ae08 100644
@@ -31,6 +31,10 @@ struct irq_alloc_info;
 
 #ifdef CONFIG_IRQ_REMAP
 
+enum irq_remap_cap {
+	IRQ_POSTING_CAP = 0,
+};
+
 extern void setup_irq_remapping_ops(void);
 extern int irq_remapping_supported(void);
 extern void set_irq_remapping_broken(void);
drivers/iommu/irq_remapping.h +4 -0
index 4bd791d..2d991b2 100644
@@ -28,6 +28,7 @@ struct irq_data;
 struct msi_msg;
 struct irq_domain;
 struct irq_alloc_info;
+enum irq_remap_cap;
 
 extern int disable_irq_remap;
 extern int irq_remap_broken;
@@ -39,6 +40,9 @@ struct irq_remap_ops {
 	/* Check whether Interrupt Remapping is supported */
 	int (*supported)(void);
 
+	/* Check some capability is supported */
+	bool (*capability)(enum irq_remap_cap);
+
 	/* Initializes hardware and makes it ready for remapping interrupts */
 	int  (*prepare)(void);
 
Signature · 1 line
1.7.1
author
#3 lore raw

PATCH v2 3/25 iommu, x86: Define new irte structure for VT-d Posted-Interrupts

In reply to Feng Wu

Add a new irte_pi structure for VT-d Posted-Interrupts.

---
 include/linux/dmar.h |   32 ++++++++++++++++++++++++++++++++
 1 files changed, 32 insertions(+), 0 deletions(-)
include/linux/dmar.h +32 -0
index 8473756..c7f9cda 100644
@@ -212,6 +212,38 @@ struct irte {
 	};
 };
 
+struct irte_pi {
+	union {
+		struct {
+			__u64   present		: 1,
+				fpd		: 1,
+				__reserved_1	: 6,
+				avail		: 4,
+				__reserved_2	: 2,
+				urg		: 1,
+				pst		: 1,
+				vector		: 8,
+				__reserved_3	: 14,
+				pda_l		: 26;
+		};
+		__u64 low;
+	};
+
+	union {
+		struct {
+			__u64	sid		: 16,
+				sq		: 2,
+				svt		: 2,
+				__reserved_4	: 12,
+				pda_h		: 32;
+		};
+		__u64 high;
+	};
+};
+
+#define PDA_LOW_BIT    26
+#define PDA_HIGH_BIT   32
+
 enum {
 	IRQ_REMAP_XAPIC_MODE,
 	IRQ_REMAP_X2APIC_MODE,
Signature · 1 line
1.7.1
author
#4 lore raw

PATCH v2 4/25 iommu, x86: Implement irq_set_vcpu_affinity for intel_ir_chip

In reply to Feng Wu

Implement irq_set_vcpu_affinity for intel_ir_chip.

---
 arch/x86/include/asm/irq_remapping.h |    5 +++++
 drivers/iommu/intel_irq_remapping.c  |   27 +++++++++++++++++++++++++++
 2 files changed, 32 insertions(+), 0 deletions(-)
arch/x86/include/asm/irq_remapping.h +5 -0
index f67ae08..f87ac70 100644
@@ -60,6 +60,11 @@ static inline struct irq_domain *arch_get_ir_parent_domain(void)
 	return x86_vector_domain;
 }
 
+struct vcpu_data {
+	u64 pi_desc_addr;	/* Physical address of PI Descriptor */
+	u32 vector;		/* Guest vector of the interrupt */
+};
+
 #else  /* CONFIG_IRQ_REMAP */
 
 static inline void setup_irq_remapping_ops(void) { }
drivers/iommu/intel_irq_remapping.c +27 -0
index f6da3b2..749cb93 100644
@@ -1010,10 +1010,37 @@ static void intel_ir_compose_msi_msg(struct irq_data *irq_data,
 	*msg = ir_data->msi_entry;
 }
 
+static int intel_ir_set_vcpu_affinity(struct irq_data *data, void *vcpu_info)
+{
+	struct intel_ir_data *ir_data = data->chip_data;
+	struct irte *irte = &ir_data->irte_entry;
+	struct irte_pi *irte_pi = (struct irte_pi *)irte;
+	struct vcpu_data *vcpu_pi_info = (struct vcpu_data *)vcpu_info;
+
+	irte_pi->urg = 0;
+	irte_pi->vector = vcpu_pi_info->vector;
+	irte_pi->pda_l = (vcpu_pi_info->pi_desc_addr >> (32 - PDA_LOW_BIT)) &
+			 ~(-1UL << PDA_LOW_BIT);
+	irte_pi->pda_h = (vcpu_pi_info->pi_desc_addr >> 32) &
+			 ~(-1UL << PDA_HIGH_BIT);
+
+	irte_pi->__reserved_1 = 0;
+	irte_pi->__reserved_2 = 0;
+	irte_pi->__reserved_3 = 0;
+	irte_pi->__reserved_4 = 0;
+
+	irte_pi->pst = 1;
+
+	modify_irte(&ir_data->irq_2_iommu, irte);
+
+	return 0;
+}
+
 static struct irq_chip intel_ir_chip = {
 	.irq_ack = ir_ack_apic_edge,
 	.irq_set_affinity = intel_ir_set_affinity,
 	.irq_compose_msi_msg = intel_ir_compose_msi_msg,
+	.irq_set_vcpu_affinity = intel_ir_set_vcpu_affinity,
 };
 
 static void intel_irq_remapping_prepare_irte(struct intel_ir_data *data,
Signature · 1 line
1.7.1
author
#5 lore raw

PATCH v2 5/25 x86, irq: Implement irq_set_vcpu_affinity for pci_msi_ir_controller

In reply to Feng Wu

Implement irq_set_vcpu_affinity for pci_msi_ir_controller.

---
 arch/x86/kernel/apic/msi.c |    1 +
 include/linux/irq.h        |    3 +++
 2 files changed, 4 insertions(+), 0 deletions(-)
arch/x86/kernel/apic/msi.c +1 -0
index da163da..b0ed073 100644
@@ -152,6 +152,7 @@ static struct irq_chip pci_msi_ir_controller = {
 	.irq_mask		= pci_msi_mask_irq,
 	.irq_ack		= irq_chip_ack_parent,
 	.irq_retrigger		= irq_chip_retrigger_hierarchy,
+	.irq_set_vcpu_affinity	= irq_chip_set_vcpu_affinity_parent,
 	.flags			= IRQCHIP_SKIP_SET_WAKE,
 };
 
include/linux/irq.h +3 -0
index 83abafc..5dcaa7f 100644
@@ -464,6 +464,9 @@ extern void irq_chip_eoi_parent(struct irq_data *data);
 extern int irq_chip_set_affinity_parent(struct irq_data *data,
 					const struct cpumask *dest,
 					bool force);
+extern int irq_chip_set_vcpu_affinity_parent(struct irq_data *data,
+					     void *vcpu_info);
+
 #endif
 
 static inline void irq_chip_write_msi_msg(struct irq_data *data,
Signature · 1 line
1.7.1
author
#6 lore raw

PATCH v2 6/25 iommu, x86: No need to migrating irq for VT-d Posted-Interrupts

In reply to Feng Wu

We don't need to migrate the irqs for VT-d Posted-Interrupts here. When 'pst' is set in IRTE, the associated irq will be posted to guests instead of interrupt remapping. The destination of the interrupt is set in Posted-Interrupts Descriptor, and the migration happens during vCPU scheduling.

---
 drivers/iommu/intel_irq_remapping.c |   10 ++++++++++
 1 files changed, 10 insertions(+), 0 deletions(-)
drivers/iommu/intel_irq_remapping.c +10 -0
index 749cb93..01786a8 100644
@@ -976,10 +976,20 @@ intel_ir_set_affinity(struct irq_data *data, const struct cpumask *mask,
 {
 	struct intel_ir_data *ir_data = data->chip_data;
 	struct irte *irte = &ir_data->irte_entry;
+	struct irte_pi *irte_pi = (struct irte_pi *)irte;
 	struct irq_cfg *cfg = irqd_cfg(data);
 	struct irq_data *parent = data->parent_data;
 	int ret;
 
+	/*
+	 * If the interrupt is for posting, it is used by guests,
+	 * we cannot set irq affinity here.
+	 */
+	if (irte_pi->pst == 1) {
+		pr_warn("cannot set irq affinity for posted-interrupts\n");
+		return -EBUSY;
+	}
+
 	ret = parent->chip->irq_set_affinity(parent, mask, force);
 	if (ret < 0 || ret == IRQ_SET_MASK_OK_DONE)
 		return ret;
Signature · 1 line
1.7.1
author
#7 lore raw

PATCH v2 7/25 iommu, x86: Add cap_pi_support() to detect VT-d PI capability

In reply to Feng Wu

Add helper function to detect VT-d Posted-Interrupts capability.

---
 include/linux/intel-iommu.h |    1 +
 1 files changed, 1 insertions(+), 0 deletions(-)
include/linux/intel-iommu.h +1 -0
index ecaf3a9..8174ae8 100644
@@ -87,6 +87,7 @@ static inline void dmar_writeq(void __iomem *addr, u64 val)
 /*
  * Decoding Capability Register
  */
+#define cap_pi_support(c)	(((c) >> 59) & 1)
 #define cap_read_drain(c)	(((c) >> 55) & 1)
 #define cap_write_drain(c)	(((c) >> 54) & 1)
 #define cap_max_amask_val(c)	(((c) >> 48) & 0x3f)
Signature · 1 line
1.7.1
author
#8 lore raw

PATCH v2 8/25 iommu, x86: Add intel_irq_remapping_capability() for Intel

In reply to Feng Wu

Add the Intel side implementation for capability in struct irq_remap_ops.

---
 drivers/iommu/intel_irq_remapping.c |   27 +++++++++++++++++++++++++++
 drivers/iommu/irq_remapping.c       |    2 ++
 drivers/iommu/irq_remapping.h       |    4 ++++
 3 files changed, 33 insertions(+), 0 deletions(-)
drivers/iommu/intel_irq_remapping.c +27 -0
index 01786a8..827aeff 100644
@@ -651,6 +651,32 @@ error:
 	return -1;
 }
 
+static bool intel_irq_remapping_capability(enum irq_remap_cap cap)
+{
+	struct dmar_drhd_unit *drhd;
+	struct intel_iommu *iommu;
+
+	switch (cap) {
+	case IRQ_POSTING_CAP:
+		/*
+		 * If 1) posted-interrupts is disabled by user
+		 * or 2) irq remapping is disabled, posted-interrupts
+		 * is not supported.
+		 */
+		if (disable_irq_post || !irq_remapping_enabled)
+			return 0;
+
+		for_each_iommu(iommu, drhd)
+			if (!cap_pi_support(iommu->cap))
+				return 0;
+
+		return 1;
+	default:
+		pr_warn("Unknown irq remapping capability.\n");
+		return 0;
+	}
+}
+
 static int ir_parse_one_hpet_scope(struct acpi_dmar_device_scope *scope,
 				   struct intel_iommu *iommu,
 				   struct acpi_dmar_hardware_unit *drhd)
@@ -947,6 +973,7 @@ static struct irq_domain *intel_get_irq_domain(struct irq_alloc_info *info)
 
 struct irq_remap_ops intel_irq_remap_ops = {
 	.supported		= intel_irq_remapping_supported,
+	.capability		= intel_irq_remapping_capability,
 	.prepare		= dmar_table_init,
 	.enable			= intel_enable_irq_remapping,
 	.disable		= disable_irq_remapping,
drivers/iommu/irq_remapping.c +2 -0
index 3c3da04..e63e969 100644
@@ -24,6 +24,8 @@ int irq_remap_broken;
 int disable_sourceid_checking;
 int no_x2apic_optout;
 
+int disable_irq_post = 1;
+
 static struct irq_remap_ops *remap_ops;
 
 static void irq_remapping_disable_io_apic(void)
drivers/iommu/irq_remapping.h +4 -0
index 2d991b2..cb1f46d 100644
@@ -36,6 +36,8 @@ extern int disable_sourceid_checking;
 extern int no_x2apic_optout;
 extern int irq_remapping_enabled;
 
+extern int disable_irq_post;
+
 struct irq_remap_ops {
 	/* Check whether Interrupt Remapping is supported */
 	int (*supported)(void);
@@ -76,6 +78,8 @@ extern void ir_ack_apic_edge(struct irq_data *data);
 #define disable_irq_remap     1
 #define irq_remap_broken      0
 
+#define disable_irq_post      1
+
 #endif /* CONFIG_IRQ_REMAP */
 
 #endif /* __IRQ_REMAPPING_H */
Signature · 1 line
1.7.1
author
#9 lore raw

PATCH v2 9/25 iommu, x86: define irq_remapping_cap()

In reply to Feng Wu

This patch adds a new interface irq_remapping_cap() to detect whether irq remapping supports new features, such as VT-d Posted-Interrupts. We export this function out, so that KVM code can check this and use this mechanism properly.

---
 arch/x86/include/asm/irq_remapping.h |    2 ++
 drivers/iommu/irq_remapping.c        |   12 ++++++++++++
 2 files changed, 14 insertions(+), 0 deletions(-)
arch/x86/include/asm/irq_remapping.h +2 -0
index f87ac70..b3ad067 100644
@@ -37,6 +37,7 @@ enum irq_remap_cap {
 
 extern void setup_irq_remapping_ops(void);
 extern int irq_remapping_supported(void);
+extern bool irq_remapping_cap(enum irq_remap_cap cap);
 extern void set_irq_remapping_broken(void);
 extern int irq_remapping_prepare(void);
 extern int irq_remapping_enable(void);
@@ -69,6 +70,7 @@ struct vcpu_data {
 
 static inline void setup_irq_remapping_ops(void) { }
 static inline int irq_remapping_supported(void) { return 0; }
+static bool irq_remapping_cap(enum irq_remap_cap cap) { return 0; }
 static inline void set_irq_remapping_broken(void) { }
 static inline int irq_remapping_prepare(void) { return -ENODEV; }
 static inline int irq_remapping_enable(void) { return -ENODEV; }
drivers/iommu/irq_remapping.c +12 -0
index e63e969..b008663 100644
@@ -103,6 +103,18 @@ int irq_remapping_supported(void)
 	return remap_ops->supported();
 }
 
+bool irq_remapping_cap(enum irq_remap_cap cap)
+{
+	if (disable_irq_post)
+		return 0;
+
+	if (!remap_ops || !remap_ops->capability)
+		return 0;
+
+	return remap_ops->capability(cap);
+}
+EXPORT_SYMBOL_GPL(irq_remapping_cap);
+
 int __init irq_remapping_prepare(void)
 {
 	if (!remap_ops || !remap_ops->prepare)
Signature · 1 line
1.7.1
author
#10 lore raw

PATCH v2 10/25 KVM: change struct pi_desc for VT-d Posted-Interrupts

In reply to Feng Wu

Change struct pi_desc for VT-d Posted-Interrupts.

---
 arch/x86/kvm/vmx.c |   15 +++++++++++++--
 1 files changed, 13 insertions(+), 2 deletions(-)
arch/x86/kvm/vmx.c +13 -2
index 3e556c6..abdb84f 100644
@@ -411,8 +411,19 @@ struct nested_vmx {
 /* Posted-Interrupt Descriptor */
 struct pi_desc {
 	u32 pir[8];     /* Posted interrupt requested */
-	u32 control;	/* bit 0 of control is outstanding notification bit */
-	u32 rsvd[7];
+	union {
+		struct {
+			u64	on	: 1,
+				sn	: 1,
+				rsvd_1	: 13,
+				ndm	: 1,
+				nv	: 8,
+				rsvd_2	: 8,
+				ndst	: 32;
+		};
+		u64 control;
+	};
+	u32 rsvd[6];
 } __aligned(64);
 
 static bool pi_test_and_set_on(struct pi_desc *pi_desc)
Signature · 1 line
1.7.1
author
#11 lore raw

PATCH v2 11/25 KVM: Add some helper functions for Posted-Interrupts

In reply to Feng Wu

This patch adds some helper functions to manipulate the Posted-Interrupts Descriptor.

---
 arch/x86/kvm/vmx.c |   26 ++++++++++++++++++++++++++
 1 files changed, 26 insertions(+), 0 deletions(-)
arch/x86/kvm/vmx.c +26 -0
index abdb84f..0b1383e 100644
@@ -408,6 +408,8 @@ struct nested_vmx {
 };
 
 #define POSTED_INTR_ON  0
+#define POSTED_INTR_SN  1
+
 /* Posted-Interrupt Descriptor */
 struct pi_desc {
 	u32 pir[8];     /* Posted interrupt requested */
@@ -443,6 +445,30 @@ static int pi_test_and_set_pir(int vector, struct pi_desc *pi_desc)
 	return test_and_set_bit(vector, (unsigned long *)pi_desc->pir);
 }
 
+static void pi_clear_sn(struct pi_desc *pi_desc)
+{
+	return clear_bit(POSTED_INTR_SN,
+			(unsigned long *)&pi_desc->control);
+}
+
+static void pi_set_sn(struct pi_desc *pi_desc)
+{
+	return set_bit(POSTED_INTR_SN,
+			(unsigned long *)&pi_desc->control);
+}
+
+static int pi_test_on(struct pi_desc *pi_desc)
+{
+	return test_bit(POSTED_INTR_ON,
+			(unsigned long *)&pi_desc->control);
+}
+
+static int pi_test_sn(struct pi_desc *pi_desc)
+{
+	return test_bit(POSTED_INTR_SN,
+			(unsigned long *)&pi_desc->control);
+}
+
 struct vcpu_vmx {
 	struct kvm_vcpu       vcpu;
 	unsigned long         host_rsp;
Signature · 1 line
1.7.1
author
#12 lore raw

PATCH v2 12/25 KVM: Initialize VT-d Posted-Interrupts Descriptor

In reply to Feng Wu

This patch initializes the VT-d Posted-Interrupts Descriptor.

---
 arch/x86/kvm/vmx.c |   27 +++++++++++++++++++++++++++
 1 files changed, 27 insertions(+), 0 deletions(-)
arch/x86/kvm/vmx.c +27 -0
index 0b1383e..66ca275 100644
@@ -45,6 +45,7 @@
 #include <asm/perf_event.h>
 #include <asm/debugreg.h>
 #include <asm/kexec.h>
+#include <asm/irq_remapping.h>
 
 #include "trace.h"
 
@@ -4433,6 +4434,30 @@ static void ept_set_mmio_spte_mask(void)
 	kvm_mmu_set_mmio_spte_mask((0x3ull << 62) | 0x6ull);
 }
 
+static void pi_desc_init(struct vcpu_vmx *vmx)
+{
+	unsigned int dest;
+
+	if (!irq_remapping_cap(IRQ_POSTING_CAP))
+		return;
+
+	/*
+	 * Initialize Posted-Interrupt Descriptor
+	 */
+
+	pi_clear_sn(&vmx->pi_desc);
+	vmx->pi_desc.nv = POSTED_INTR_VECTOR;
+
+	/* Physical mode for Notificaiton Event */
+	vmx->pi_desc.ndm = 0;
+	dest = cpu_physical_id(vmx->vcpu.cpu);
+
+	if (x2apic_enabled())
+		vmx->pi_desc.ndst = dest;
+	else
+		vmx->pi_desc.ndst = (dest << 8) & 0xFF00;
+}
+
 /*
  * Sets up the vmcs for emulated real mode.
  */
@@ -4476,6 +4501,8 @@ static int vmx_vcpu_setup(struct vcpu_vmx *vmx)
 
 		vmcs_write64(POSTED_INTR_NV, POSTED_INTR_VECTOR);
 		vmcs_write64(POSTED_INTR_DESC_ADDR, __pa((&vmx->pi_desc)));
+
+		pi_desc_init(vmx);
 	}
 
 	if (ple_gap) {
Signature · 1 line
1.7.1
author
#13 lore raw

PATCH v2 13/25 KVM: Define a new interface kvm_find_dest_vcpu() for VT-d PI

In reply to Feng Wu

This patch defines a new interface kvm_find_dest_vcpu for VT-d PI, which can returns the destination vCPU of the interrupt for guests.

Since VT-d PI cannot handle broadcast/multicast interrupt, Here we only handle Fixed and Lowest priority interrupts.

The current method of handling guest lowest priority interrtups is to use a counter 'apic_arb_prio' for each vCPU, we choose the vCPU with smallest 'apic_arb_prio' and then increase it by 1. However, for VT-d PI, we cannot re-use this, since we no longer have control to 'apic_arb_prio' with posted interrupt direct delivery by Hardware.

Here, we introduce a similar way with 'apic_arb_prio' to handle guest lowest priority interrtups when VT-d PI is used. Here is the ideas:
- Each vCPU has a counter 'round_robin_counter'.
- When guests sets an interrupts to lowest priority, we choose the vCPU with smallest 'round_robin_counter' as the destination, then increase it.

---
 arch/x86/include/asm/kvm_host.h |    4 +++
 virt/kvm/irq_comm.c             |   41 +++++++++++++++++++++++++++++++++++++++
 2 files changed, 45 insertions(+), 0 deletions(-)
arch/x86/include/asm/kvm_host.h +4 -0
index 6ed0c30..7a41808 100644
@@ -358,6 +358,7 @@ struct kvm_vcpu_arch {
 	struct kvm_lapic *apic;    /* kernel irqchip context */
 	unsigned long apic_attention;
 	int32_t apic_arb_prio;
+	int32_t round_robin_counter;
 	int mp_state;
 	u64 ia32_misc_enable_msr;
 	bool tpr_access_reporting;
@@ -1093,4 +1094,7 @@ int kvm_pmu_read_pmc(struct kvm_vcpu *vcpu, unsigned pmc, u64 *data);
 void kvm_handle_pmu_event(struct kvm_vcpu *vcpu);
 void kvm_deliver_pmi(struct kvm_vcpu *vcpu);
 
+bool kvm_find_dest_vcpu(struct kvm *kvm, struct kvm_lapic_irq *irq,
+			struct kvm_vcpu **dest_vcpu);
+
 #endif /* _ASM_X86_KVM_HOST_H */
virt/kvm/irq_comm.c +41 -0

47 lines not on this page · raw message

Signature · 1 line
1.7.1
author
#14 lore raw

PATCH v2 14/25 KVM: Get Posted-Interrupts descriptor address from struct kvm_vcpu

In reply to Feng Wu

Define a interface to get PI descriptor address from the vCPU structure.

---
 arch/x86/include/asm/kvm_host.h |    1 +
 arch/x86/kvm/vmx.c              |   12 ++++++++++++
 2 files changed, 13 insertions(+), 0 deletions(-)
arch/x86/include/asm/kvm_host.h +1 -0
index 7a41808..9b45b78 100644
@@ -772,6 +772,7 @@ struct kvm_x86_ops {
 	int (*check_nested_events)(struct kvm_vcpu *vcpu, bool external_intr);
 
 	void (*sched_in)(struct kvm_vcpu *kvm, int cpu);
+	u64 (*get_pi_desc_addr)(struct kvm_vcpu *vcpu);
 };
 
 struct kvm_arch_async_pf {
arch/x86/kvm/vmx.c +12 -0
index 66ca275..81f239b 100644
@@ -562,6 +562,11 @@ static inline struct vcpu_vmx *to_vmx(struct kvm_vcpu *vcpu)
 	return container_of(vcpu, struct vcpu_vmx, vcpu);
 }
 
+struct pi_desc *vcpu_to_pi_desc(struct kvm_vcpu *vcpu)
+{
+	return &(to_vmx(vcpu)->pi_desc);
+}
+
 #define VMCS12_OFFSET(x) offsetof(struct vmcs12, x)
 #define FIELD(number, name)	[number] = VMCS12_OFFSET(name)
 #define FIELD64(number, name)	[number] = VMCS12_OFFSET(name), \
@@ -4298,6 +4303,11 @@ static void vmx_sync_pir_to_irr_dummy(struct kvm_vcpu *vcpu)
 	return;
 }
 
+static u64 vmx_get_pi_desc_addr(struct kvm_vcpu *vcpu)
+{
+	return __pa((u64)vcpu_to_pi_desc(vcpu));
+}
+
 /*
  * Set up the vmcs's constant host-state fields, i.e., host-state fields that
  * will not change in the lifetime of the guest.
@@ -9244,6 +9254,8 @@ static struct kvm_x86_ops vmx_x86_ops = {
 	.check_nested_events = vmx_check_nested_events,
 
 	.sched_in = vmx_sched_in,
+
+	.get_pi_desc_addr = vmx_get_pi_desc_addr,
 };
 
 static int __init vmx_init(void)
Signature · 1 line
1.7.1
author
#15 lore raw

PATCH v2 15/25 KVM: Make struct kvm_irq_routing_table accessible

In reply to Feng Wu

Move struct kvm_irq_routing_table from irqchip.c to kvm_host.h, so we can use it outside of irqchip.c.

---
 include/linux/kvm_host.h |   19 +++++++++++++++++++
 virt/kvm/irqchip.c       |   11 -----------
 2 files changed, 19 insertions(+), 11 deletions(-)
include/linux/kvm_host.h +19 -0
index 0b9659d..cfa85ac 100644
@@ -335,6 +335,25 @@ struct kvm_kernel_irq_routing_entry {
 	struct hlist_node link;
 };
 
+#ifdef CONFIG_HAVE_KVM_IRQ_ROUTING
+
+struct kvm_irq_routing_table {
+	int chip[KVM_NR_IRQCHIPS][KVM_IRQCHIP_NUM_PINS];
+	struct kvm_kernel_irq_routing_entry *rt_entries;
+	u32 nr_rt_entries;
+	/*
+	 * Array indexed by gsi. Each entry contains list of irq chips
+	 * the gsi is connected to.
+	 */
+	struct hlist_head map[0];
+};
+
+#else
+
+struct kvm_irq_routing_table {};
+
+#endif
+
 #ifndef KVM_PRIVATE_MEM_SLOTS
 #define KVM_PRIVATE_MEM_SLOTS 0
 #endif
virt/kvm/irqchip.c +0 -11
index 7f256f3..cdf29a6 100644
@@ -31,17 +31,6 @@
 #include <trace/events/kvm.h>
 #include "irq.h"
 
-struct kvm_irq_routing_table {
-	int chip[KVM_NR_IRQCHIPS][KVM_IRQCHIP_NUM_PINS];
-	struct kvm_kernel_irq_routing_entry *rt_entries;
-	u32 nr_rt_entries;
-	/*
-	 * Array indexed by gsi. Each entry contains list of irq chips
-	 * the gsi is connected to.
-	 */
-	struct hlist_head map[0];
-};
-
 int kvm_irq_map_gsi(struct kvm *kvm,
 		    struct kvm_kernel_irq_routing_entry *entries, int gsi)
 {
Signature · 1 line
1.7.1
author
#16 lore raw

PATCH v2 16/25 KVM: make kvm_set_msi_irq() public

In reply to Feng Wu

Make kvm_set_msi_irq() public, we can use this function outside.

---
 include/linux/kvm_host.h |    2 ++
 virt/kvm/irq_comm.c      |    2 +-
 2 files changed, 3 insertions(+), 1 deletions(-)
include/linux/kvm_host.h +2 -0
index cfa85ac..5cd4420 100644
@@ -785,6 +785,8 @@ void kvm_unregister_irq_ack_notifier(struct kvm *kvm,
 				   struct kvm_irq_ack_notifier *kian);
 int kvm_request_irq_source_id(struct kvm *kvm);
 void kvm_free_irq_source_id(struct kvm *kvm, int irq_source_id);
+void kvm_set_msi_irq(struct kvm_kernel_irq_routing_entry *e,
+		     struct kvm_lapic_irq *irq);
 
 #ifdef CONFIG_KVM_DEVICE_ASSIGNMENT
 int kvm_iommu_map_pages(struct kvm *kvm, struct kvm_memory_slot *slot);
virt/kvm/irq_comm.c +1 -1
index f3c5d69..231671a 100644
@@ -106,7 +106,7 @@ int kvm_irq_delivery_to_apic(struct kvm *kvm, struct kvm_lapic *src,
 	return r;
 }
 
-static inline void kvm_set_msi_irq(struct kvm_kernel_irq_routing_entry *e,
+void kvm_set_msi_irq(struct kvm_kernel_irq_routing_entry *e,
 				   struct kvm_lapic_irq *irq)
 {
 	trace_kvm_msi_set_irq(e->msi.address_lo, e->msi.data);
Signature · 1 line
1.7.1
author
#17 lore raw

PATCH v2 17/25 KVM: kvm-vfio: User API for VT-d Posted-Interrupts

In reply to Feng Wu

This patch adds and documents a new attribute KVM_DEV_VFIO_DEVICE_POSTING_IRQ in KVM_DEV_VFIO_DEVICE group. This new attribute is used for VT-d Posted-Interrupts.

When guest OS changes the interrupt configuration for an assigned device, such as, MSI/MSIx data/address fields, QEMU will use this IRQ attribute to tell KVM to update the related IRTE according the VT-d Posted-Interrrupts Specification, such as, the guest vector should be updated in the related IRTE.

---
 Documentation/virtual/kvm/devices/vfio.txt |    9 +++++++++
 include/uapi/linux/kvm.h                   |   10 ++++++++++
 2 files changed, 19 insertions(+), 0 deletions(-)
Documentation/virtual/kvm/devices/vfio.txt +9 -0
index f7aff29..41e12b7 100644
@@ -42,3 +42,12 @@ activated before VFIO_DEVICE_SET_IRQS has been called to trigger the IRQ
 or associate an eventfd to it. Unforwarding can only be called while the
 signaling has been disabled with VFIO_DEVICE_SET_IRQS. If this condition is
 not satisfied, the command returns an -EBUSY.
+
+  KVM_DEV_VFIO_DEVICE_POSTING_IRQ: Use posted interrtups mechanism to post
+                                   the IRQ to guests.
+For this attribute, kvm_device_attr.addr points to a kvm_vfio_dev_irq struct.
+
+When guest OS changes the interrupt configuration for an assigned device,
+such as, MSI/MSIx data/address fields, QEMU will use this IRQ attribute
+to tell KVM to update the related IRTE according the VT-d Posted-Interrrupts
+Specification, such as, the guest vector should be updated in the related IRTE.
include/uapi/linux/kvm.h +10 -0
index a269a42..7d98650 100644
@@ -949,6 +949,7 @@ struct kvm_device_attr {
 #define  KVM_DEV_VFIO_DEVICE			2
 #define   KVM_DEV_VFIO_DEVICE_FORWARD_IRQ			1
 #define   KVM_DEV_VFIO_DEVICE_UNFORWARD_IRQ			2
+#define   KVM_DEV_VFIO_DEVICE_POSTING_IRQ			3
 
 enum kvm_device_type {
 	KVM_DEV_TYPE_FSL_MPIC_20	= 1,
@@ -973,6 +974,15 @@ struct kvm_arch_forwarded_irq {
 	__u32 gsi; /* gsi, ie. virtual IRQ number */
 };
 
+struct kvm_vfio_dev_irq {
+	__u32	argsz;
+	__u32	fd;		/* file descriptor of the VFIO device */
+	__u32	index;		/* VFIO device IRQ index */
+	__u32	start;
+	__u32	count;
+	__u32	gsi[];		/* gsi, ie. virtual IRQ number */
+};
+
 /*
  * ioctls for VM fds
  */
Signature · 1 line
1.7.1
#18 lore raw

Re: [v2 17/25] KVM: kvm-vfio: User API for VT-d Posted-Interrupts

Hi Feng,

Quoted 21 lines · Feng Wu

This patch adds and documents a new attribute KVM_DEV_VFIO_DEVICE_POSTING_IRQ in KVM_DEV_VFIO_DEVICE group. This new attribute is used for VT-d Posted-Interrupts.

When guest OS changes the interrupt configuration for an assigned device, such as, MSI/MSIx data/address fields, QEMU will use this IRQ attribute to tell KVM to update the related IRTE according the VT-d Posted-Interrrupts Specification, such as, the guest vector should be updated in the related IRTE.

---
 Documentation/virtual/kvm/devices/vfio.txt |    9 +++++++++
 include/uapi/linux/kvm.h                   |   10 ++++++++++
 2 files changed, 19 insertions(+), 0 deletions(-)
Documentation/virtual/kvm/devices/vfio.txt +0 -0
index f7aff29..41e12b7 100644

Documentation/virtual/kvm/devices/vfio.txt activated before VFIO_DEVICE_SET_IRQS has been called to trigger the IRQ · line 46

Quoted 5 lines
Documentation/virtual/kvm/devices/vfio.txt +2 -0
@@ -42,3 +42,12 @@ activated before VFIO_DEVICE_SET_IRQS has been called to trigger the IRQ
 or associate an eventfd to it. Unforwarding can only be called while the
 signaling has been disabled with VFIO_DEVICE_SET_IRQS. If this condition is
 not satisfied, the command returns an -EBUSY.
+
+  KVM_DEV_VFIO_DEVICE_POSTING_IRQ: Use posted interrtups mechanism to post

typo

Documentation/virtual/kvm/devices/vfio.txt activated before VFIO_DEVICE_SET_IRQS has been called to trigger the IRQ · line 53

Quoted 7 lines
Documentation/virtual/kvm/devices/vfio.txt +7 -0
+                                   the IRQ to guests.
+For this attribute, kvm_device_attr.addr points to a kvm_vfio_dev_irq struct.
+
+When guest OS changes the interrupt configuration for an assigned device,
+such as, MSI/MSIx data/address fields, QEMU will use this IRQ attribute
+to tell KVM to update the related IRTE according the VT-d Posted-Interrrupts
+Specification, such as, the guest vector should be updated in the related IRTE.

For my curiosity are there any restrictions about the instant at which the change can be done?
I do not get here how you deactivate the posting?

include/uapi/linux/kvm.h struct kvm_device_attr { · line 952

Quoted 9 lines
include/uapi/linux/kvm.h +1 -0
index a269a42..7d98650 100644
@@ -949,6 +949,7 @@ struct kvm_device_attr {
 #define  KVM_DEV_VFIO_DEVICE			2
 #define   KVM_DEV_VFIO_DEVICE_FORWARD_IRQ			1
 #define   KVM_DEV_VFIO_DEVICE_UNFORWARD_IRQ			2
+#define   KVM_DEV_VFIO_DEVICE_POSTING_IRQ			3

Maybe we should align our naming verb vs verbing here? Best Regards
Eric

Quoted 20 lines
include/uapi/linux/kvm.h +9 -0
 enum kvm_device_type {
 	KVM_DEV_TYPE_FSL_MPIC_20	= 1,
@@ -973,6 +974,15 @@ struct kvm_arch_forwarded_irq {
 	__u32 gsi; /* gsi, ie. virtual IRQ number */
 };
 
+struct kvm_vfio_dev_irq {
+	__u32	argsz;
+	__u32	fd;		/* file descriptor of the VFIO device */
+	__u32	index;		/* VFIO device IRQ index */
+	__u32	start;
+	__u32	count;
+	__u32	gsi[];		/* gsi, ie. virtual IRQ number */
+};
+
 /*
  * ioctls for VM fds
  */
author
#19 lore raw

RE: [v2 17/25] KVM: kvm-vfio: User API for VT-d Posted-Interrupts

Quoted 55 lines

-----Original Message-----
From: [email protected] [mailto:[email protected]] On
Behalf Of Eric Auger
Sent: Thursday, December 04, 2014 10:05 PM
To: Wu, Feng; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; jiang.liu-VuQAYsv1563Yd54FQh9/[email protected]
Cc: [email protected]; iommu-cunTk1MwBs9QetFLy7KEm3xJsTq8ys+cHZ5vskTnxNA@public.gmane.org; [email protected]
Subject: Re: [v2 17/25] KVM: kvm-vfio: User API for VT-d Posted-Interrupts

Hi Feng,

Quoted 10 lines · Feng Wu

This patch adds and documents a new attribute KVM_DEV_VFIO_DEVICE_POSTING_IRQ in KVM_DEV_VFIO_DEVICE group. This new attribute is used for VT-d Posted-Interrupts.

When guest OS changes the interrupt configuration for an assigned device, such as, MSI/MSIx data/address fields, QEMU will use this IRQ attribute to tell KVM to update the related IRTE according the VT-d Posted-Interrrupts Specification, such as, the guest vector should be updated in the related IRTE.

Review

Quoted 7 lines
---
 Documentation/virtual/kvm/devices/vfio.txt |    9 +++++++++
 include/uapi/linux/kvm.h                   |   10 ++++++++++
 2 files changed, 19 insertions(+), 0 deletions(-)
(quoted diff) +0 -0

b/Documentation/virtual/kvm/devices/vfio.txt

Documentation/virtual/kvm/devices/vfio.txt activated before VFIO_DEVICE_SET_IRQS has been

Quoted 4 lines

index f7aff29..41e12b7 100644

Documentation/virtual/kvm/devices/vfio.txt +0 -0
@@ -42,3 +42,12 @@ activated before VFIO_DEVICE_SET_IRQS has been

called to trigger the IRQ

Documentation/virtual/kvm/devices/vfio.txt activated before VFIO_DEVICE_SET_IRQS has been · line 43

Quoted 2 lines
Documentation/virtual/kvm/devices/vfio.txt +0 -0
 or associate an eventfd to it. Unforwarding can only be called while the
 signaling has been disabled with VFIO_DEVICE_SET_IRQS. If this condition

is

Documentation/virtual/kvm/devices/vfio.txt activated before VFIO_DEVICE_SET_IRQS has been called to trigger the IRQ

Quoted 3 lines
Documentation/virtual/kvm/devices/vfio.txt +2 -0
 not satisfied, the command returns an -EBUSY.
+
+  KVM_DEV_VFIO_DEVICE_POSTING_IRQ: Use posted interrtups

mechanism to post
typo

Documentation/virtual/kvm/devices/vfio.txt activated before VFIO_DEVICE_SET_IRQS has been · line 48

Quoted 2 lines
Documentation/virtual/kvm/devices/vfio.txt +2 -0
+                                   the IRQ to guests.
+For this attribute, kvm_device_attr.addr points to a kvm_vfio_dev_irq

struct.

Documentation/virtual/kvm/devices/vfio.txt activated before VFIO_DEVICE_SET_IRQS has been called to trigger the IRQ

Quoted 4 lines
Documentation/virtual/kvm/devices/vfio.txt +4 -0
+
+When guest OS changes the interrupt configuration for an assigned device,
+such as, MSI/MSIx data/address fields, QEMU will use this IRQ attribute
+to tell KVM to update the related IRTE according the VT-d

Posted-Interrrupts

Quoted 5 lines

Documentation/virtual/kvm/devices/vfio.txt activated before VFIO_DEVICE_SET_IRQS has been · line 53

Quoted 1 line
Documentation/virtual/kvm/devices/vfio.txt +1 -0
+Specification, such as, the guest vector should be updated in the related

IRTE.
For my curiosity are there any restrictions about the instant at which the change can be done?
I do not get here how you deactivate the posting?

The current method is if the hardware supports interrupts posting, we will use it instead of interrupts remapping, since it has good performance. Why do I need deactivate interrupts posting?

Here is the reply to Alex for the same question:
"In fact, I don't think we need to stop the posted-interrupts. For setting posted interrupts, we update the related IRTE according to the new format. If the guest reboots, or unload the drivers, or some other operations, the msi/msix will be disabled first, in this path, the irq will be disabled the related IRTE is not used anymore."

Quoted 13 lines

include/uapi/linux/kvm.h struct kvm_device_attr { · line 952

Quoted 10 lines
include/uapi/linux/kvm.h +1 -0
index a269a42..7d98650 100644
@@ -949,6 +949,7 @@ struct kvm_device_attr {
 #define  KVM_DEV_VFIO_DEVICE			2
 #define   KVM_DEV_VFIO_DEVICE_FORWARD_IRQ			1
 #define   KVM_DEV_VFIO_DEVICE_UNFORWARD_IRQ			2
+#define   KVM_DEV_VFIO_DEVICE_POSTING_IRQ			3

Maybe we should align our naming verb vs verbing here? Best Regards
Eric

No problem, I will align my patch in the next version. Thanks!

Thanks,
Feng

Quoted 25 lines
Quoted 10 lines
include/uapi/linux/kvm.h +2 -0
 enum kvm_device_type {
 	KVM_DEV_TYPE_FSL_MPIC_20	= 1,
@@ -973,6 +974,15 @@ struct kvm_arch_forwarded_irq {
 	__u32 gsi; /* gsi, ie. virtual IRQ number */
 };
 
+struct kvm_vfio_dev_irq {
+	__u32	argsz;

include/uapi/linux/kvm.h struct kvm_arch_forwarded_irq { · line 988

Quoted 10 lines
include/uapi/linux/kvm.h +7 -0
+	__u32	fd;		/* file descriptor of the VFIO device */
+	__u32	index;		/* VFIO device IRQ index */
+	__u32	start;
+	__u32	count;
+	__u32	gsi[];		/* gsi, ie. virtual IRQ number */
+};
+
 /*
  * ioctls for VM fds
  */

--
To unsubscribe from this list: send the line "unsubscribe kvm" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html

#20 lore raw

Re: [v2 17/25] KVM: kvm-vfio: User API for VT-d Posted-Interrupts

Quoted 64 lines · Feng
Quoted 54 lines

-----Original Message-----
From: [email protected] [mailto:[email protected]] On
Behalf Of Eric Auger
Sent: Thursday, December 04, 2014 10:05 PM
To: Wu, Feng; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; jiang.liu-VuQAYsv1563Yd54FQh9/[email protected]
Cc: [email protected]; iommu-cunTk1MwBs9QetFLy7KEm3xJsTq8ys+cHZ5vskTnxNA@public.gmane.org; [email protected]
Subject: Re: [v2 17/25] KVM: kvm-vfio: User API for VT-d Posted-Interrupts

Hi Feng,

Quoted 10 lines · Feng Wu

This patch adds and documents a new attribute KVM_DEV_VFIO_DEVICE_POSTING_IRQ in KVM_DEV_VFIO_DEVICE group. This new attribute is used for VT-d Posted-Interrupts.

When guest OS changes the interrupt configuration for an assigned device, such as, MSI/MSIx data/address fields, QEMU will use this IRQ attribute to tell KVM to update the related IRTE according the VT-d Posted-Interrrupts Specification, such as, the guest vector should be updated in the related IRTE.

Review

Quoted 7 lines
---
 Documentation/virtual/kvm/devices/vfio.txt |    9 +++++++++
 include/uapi/linux/kvm.h                   |   10 ++++++++++
 2 files changed, 19 insertions(+), 0 deletions(-)
(quoted diff) +0 -0

b/Documentation/virtual/kvm/devices/vfio.txt

Documentation/virtual/kvm/devices/vfio.txt activated before VFIO_DEVICE_SET_IRQS has been

Quoted 4 lines

index f7aff29..41e12b7 100644

Documentation/virtual/kvm/devices/vfio.txt +0 -0
@@ -42,3 +42,12 @@ activated before VFIO_DEVICE_SET_IRQS has been

called to trigger the IRQ

Documentation/virtual/kvm/devices/vfio.txt activated before VFIO_DEVICE_SET_IRQS has been · line 43

Quoted 2 lines
Documentation/virtual/kvm/devices/vfio.txt +0 -0
 or associate an eventfd to it. Unforwarding can only be called while the
 signaling has been disabled with VFIO_DEVICE_SET_IRQS. If this condition

is

Documentation/virtual/kvm/devices/vfio.txt activated before VFIO_DEVICE_SET_IRQS has been called to trigger the IRQ

Quoted 3 lines
Documentation/virtual/kvm/devices/vfio.txt +2 -0
 not satisfied, the command returns an -EBUSY.
+
+  KVM_DEV_VFIO_DEVICE_POSTING_IRQ: Use posted interrtups

mechanism to post
typo

Documentation/virtual/kvm/devices/vfio.txt activated before VFIO_DEVICE_SET_IRQS has been · line 48

Quoted 2 lines
Documentation/virtual/kvm/devices/vfio.txt +2 -0
+                                   the IRQ to guests.
+For this attribute, kvm_device_attr.addr points to a kvm_vfio_dev_irq

struct.

Documentation/virtual/kvm/devices/vfio.txt activated before VFIO_DEVICE_SET_IRQS has been called to trigger the IRQ

Quoted 4 lines
Documentation/virtual/kvm/devices/vfio.txt +4 -0
+
+When guest OS changes the interrupt configuration for an assigned device,
+such as, MSI/MSIx data/address fields, QEMU will use this IRQ attribute
+to tell KVM to update the related IRTE according the VT-d

Posted-Interrrupts

Quoted 5 lines

Documentation/virtual/kvm/devices/vfio.txt activated before VFIO_DEVICE_SET_IRQS has been · line 53

Quoted 1 line
Documentation/virtual/kvm/devices/vfio.txt +1 -0
+Specification, such as, the guest vector should be updated in the related

IRTE.
For my curiosity are there any restrictions about the instant at which the change can be done?
I do not get here how you deactivate the posting?

The current method is if the hardware supports interrupts posting, we will use it instead of interrupts remapping, since it has good performance. Why do I need deactivate interrupts posting?

Quoted 6 lines

Here is the reply to Alex for the same question:
"In fact, I don't think we need to stop the posted-interrupts. For setting posted interrupts, we update the related IRTE according to the new format. If the guest reboots, or unload the drivers, or some other operations, the msi/msix will be disabled first, in this path, the irq will be disabled the related IRTE is not used anymore."

Right, and I'm still not sure I agree with that reasoning. We need to build the kernel interface to be generic, not tailored for a specific userspace. I don't really feel comfortable having something that can't be disabled via a similar path to it being enabled. For instance, what about a dynamic debug interface where we want to enable tracing and see each interrupt injected into the guest. At that point we'd want to disabled posted interrupts and direct KVM injection and route via QEMU. Thanks,

Alex

Quoted 47 lines
Quoted 4 lines
Quoted 4 lines
include/uapi/linux/kvm.h +0 -0
index a269a42..7d98650 100644
Quoted 8 lines

include/uapi/linux/kvm.h struct kvm_device_attr { · line 952

Quoted 5 lines
include/uapi/linux/kvm.h +1 -0
@@ -949,6 +949,7 @@ struct kvm_device_attr {
 #define  KVM_DEV_VFIO_DEVICE			2
 #define   KVM_DEV_VFIO_DEVICE_FORWARD_IRQ			1
 #define   KVM_DEV_VFIO_DEVICE_UNFORWARD_IRQ			2
+#define   KVM_DEV_VFIO_DEVICE_POSTING_IRQ			3

Maybe we should align our naming verb vs verbing here? Best Regards
Eric

No problem, I will align my patch in the next version. Thanks!

Thanks,
Feng

Quoted 17 lines
Quoted 17 lines
include/uapi/linux/kvm.h +9 -0
 enum kvm_device_type {
 	KVM_DEV_TYPE_FSL_MPIC_20	= 1,
@@ -973,6 +974,15 @@ struct kvm_arch_forwarded_irq {
 	__u32 gsi; /* gsi, ie. virtual IRQ number */
 };
 
+struct kvm_vfio_dev_irq {
+	__u32	argsz;
+	__u32	fd;		/* file descriptor of the VFIO device */
+	__u32	index;		/* VFIO device IRQ index */
+	__u32	start;
+	__u32	count;
+	__u32	gsi[];		/* gsi, ie. virtual IRQ number */
+};
+
 /*
Quoted 8 lines

include/uapi/linux/kvm.h struct kvm_arch_forwarded_irq { · line 988

Quoted 3 lines
include/uapi/linux/kvm.h +0 -0
  * ioctls for VM fds
  */

--
To unsubscribe from this list: send the line "unsubscribe kvm" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html

--
To unsubscribe from this list: send the line "unsubscribe kvm" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html

author
#21 lore raw

RE: [v2 17/25] KVM: kvm-vfio: User API for VT-d Posted-Interrupts

Quoted 98 lines

-----Original Message-----
From: Alex Williamson [mailto:[email protected]]
Sent: Monday, December 08, 2014 1:21 PM
To: Wu, Feng
Cc: Eric Auger; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; jiang.liu-VuQAYsv1563Yd54FQh9/[email protected]; [email protected]; iommu-cunTk1MwBs9QetFLy7KEm3xJsTq8ys+cHZ5vskTnxNA@public.gmane.org; [email protected]
Subject: Re: [v2 17/25] KVM: kvm-vfio: User API for VT-d Posted-Interrupts

Quoted 3 lines · Feng
Quoted 3 lines

-----Original Message-----
From: [email protected] [mailto:[email protected]]

On

Quoted 10 lines
Quoted 10 lines

Behalf Of Eric Auger
Sent: Thursday, December 04, 2014 10:05 PM
To: Wu, Feng; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; jiang.liu-VuQAYsv1563Yd54FQh9/[email protected]
Cc: [email protected]; iommu-cunTk1MwBs9QetFLy7KEm3xJsTq8ys+cHZ5vskTnxNA@public.gmane.org; [email protected]
Subject: Re: [v2 17/25] KVM: kvm-vfio: User API for VT-d Posted-Interrupts

Quoted 4 lines
Quoted 4 lines

Hi Feng,

Quoted 2 lines · Feng Wu

This patch adds and documents a new attribute KVM_DEV_VFIO_DEVICE_POSTING_IRQ in KVM_DEV_VFIO_DEVICE

group.

Quoted 16 lines
Quoted 16 lines
Quoted 8 lines

This new attribute is used for VT-d Posted-Interrupts.

When guest OS changes the interrupt configuration for an assigned device, such as, MSI/MSIx data/address fields, QEMU will use this IRQ attribute to tell KVM to update the related IRTE according the VT-d Posted-Interrrupts Specification, such as, the guest vector should be updated in the related IRTE.

Review

Quoted 7 lines
---
 Documentation/virtual/kvm/devices/vfio.txt |    9 +++++++++
 include/uapi/linux/kvm.h                   |   10 ++++++++++
 2 files changed, 19 insertions(+), 0 deletions(-)
(quoted diff) +0 -0

b/Documentation/virtual/kvm/devices/vfio.txt

Quoted 7 lines
Quoted 7 lines

Documentation/virtual/kvm/devices/vfio.txt activated before VFIO_DEVICE_SET_IRQS has been

Quoted 4 lines

index f7aff29..41e12b7 100644

Documentation/virtual/kvm/devices/vfio.txt +0 -0
@@ -42,3 +42,12 @@ activated before VFIO_DEVICE_SET_IRQS has been

called to trigger the IRQ

Quoted 2 lines
Documentation/virtual/kvm/devices/vfio.txt +0 -0
 or associate an eventfd to it. Unforwarding can only be called while the
 signaling has been disabled with VFIO_DEVICE_SET_IRQS. If this

condition

Quoted 11 lines
Quoted 11 lines

is

Documentation/virtual/kvm/devices/vfio.txt activated before VFIO_DEVICE_SET_IRQS has been called to trigger the IRQ

Quoted 3 lines
Documentation/virtual/kvm/devices/vfio.txt +2 -0
 not satisfied, the command returns an -EBUSY.
+
+  KVM_DEV_VFIO_DEVICE_POSTING_IRQ: Use posted interrtups

mechanism to post
typo

Documentation/virtual/kvm/devices/vfio.txt activated before VFIO_DEVICE_SET_IRQS has been · line 48

Quoted 2 lines
Documentation/virtual/kvm/devices/vfio.txt +2 -0
+                                   the IRQ to guests.
+For this attribute, kvm_device_attr.addr points to a kvm_vfio_dev_irq

struct.

Documentation/virtual/kvm/devices/vfio.txt activated before VFIO_DEVICE_SET_IRQS has been · line 50

Quoted 2 lines
Documentation/virtual/kvm/devices/vfio.txt +2 -0
+
+When guest OS changes the interrupt configuration for an assigned

device,

Quoted 13 lines
Quoted 8 lines

Documentation/virtual/kvm/devices/vfio.txt activated before VFIO_DEVICE_SET_IRQS has been · line 52

Quoted 2 lines
Documentation/virtual/kvm/devices/vfio.txt +2 -0
+such as, MSI/MSIx data/address fields, QEMU will use this IRQ attribute
+to tell KVM to update the related IRTE according the VT-d

Posted-Interrrupts

Documentation/virtual/kvm/devices/vfio.txt activated before VFIO_DEVICE_SET_IRQS has been · line 53

Quoted 1 line
Documentation/virtual/kvm/devices/vfio.txt +1 -0
+Specification, such as, the guest vector should be updated in the related

IRTE.
For my curiosity are there any restrictions about the instant at which the change can be done?
I do not get here how you deactivate the posting?

The current method is if the hardware supports interrupts posting, we will use it instead of interrupts remapping, since it has good performance. Why do I need deactivate interrupts posting?

Quoted 6 lines

Here is the reply to Alex for the same question:
"In fact, I don't think we need to stop the posted-interrupts. For setting posted interrupts, we update the related IRTE according to the new format. If the guest reboots, or unload the drivers, or some other operations, the msi/msix will be disabled first, in this path, the irq will be disabled the related IRTE is not used anymore."

Right, and I'm still not sure I agree with that reasoning. We need to build the kernel interface to be generic, not tailored for a specific userspace. I don't really feel comfortable having something that can't be disabled via a similar path to it being enabled. For instance, what about a dynamic debug interface where we want to enable tracing and see each interrupt injected into the guest. At that point we'd want to disabled posted interrupts and direct KVM injection and route via QEMU. Thanks,

Quoted 1 line

Alex

Okay, I will think about this.

Thanks,
Feng

Quoted 50 lines
Quoted 48 lines
Quoted 13 lines

include/uapi/linux/kvm.h struct kvm_device_attr { · line 952

Quoted 10 lines
include/uapi/linux/kvm.h +1 -0
index a269a42..7d98650 100644
@@ -949,6 +949,7 @@ struct kvm_device_attr {
 #define  KVM_DEV_VFIO_DEVICE			2
 #define   KVM_DEV_VFIO_DEVICE_FORWARD_IRQ			1
 #define   KVM_DEV_VFIO_DEVICE_UNFORWARD_IRQ			2
+#define   KVM_DEV_VFIO_DEVICE_POSTING_IRQ			3

Maybe we should align our naming verb vs verbing here? Best Regards
Eric

No problem, I will align my patch in the next version. Thanks!

Thanks,
Feng

Quoted 17 lines
Quoted 17 lines
include/uapi/linux/kvm.h +9 -0
 enum kvm_device_type {
 	KVM_DEV_TYPE_FSL_MPIC_20	= 1,
@@ -973,6 +974,15 @@ struct kvm_arch_forwarded_irq {
 	__u32 gsi; /* gsi, ie. virtual IRQ number */
 };
 
+struct kvm_vfio_dev_irq {
+	__u32	argsz;
+	__u32	fd;		/* file descriptor of the VFIO device */
+	__u32	index;		/* VFIO device IRQ index */
+	__u32	start;
+	__u32	count;
+	__u32	gsi[];		/* gsi, ie. virtual IRQ number */
+};
+
 /*
Quoted 8 lines

include/uapi/linux/kvm.h struct kvm_arch_forwarded_irq { · line 988

Quoted 3 lines
include/uapi/linux/kvm.h +0 -0
  * ioctls for VM fds
  */

--
To unsubscribe from this list: send the line "unsubscribe kvm" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html

--
To unsubscribe from this list: send the line "unsubscribe kvm" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html

author
#22 lore raw

RE: [v2 17/25] KVM: kvm-vfio: User API for VT-d Posted-Interrupts

In reply to Alex Williamson
Quoted 99 lines

-----Original Message-----
From: Alex Williamson [mailto:[email protected]]
Sent: Monday, December 08, 2014 1:21 PM
To: Wu, Feng
Cc: Eric Auger; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; jiang.liu-VuQAYsv1563Yd54FQh9/[email protected]; [email protected]; iommu-cunTk1MwBs9QetFLy7KEm3xJsTq8ys+cHZ5vskTnxNA@public.gmane.org; [email protected]
Subject: Re: [v2 17/25] KVM: kvm-vfio: User API for VT-d Posted-Interrupts

Quoted 3 lines · Feng
Quoted 3 lines

-----Original Message-----
From: [email protected] [mailto:[email protected]]

On

Quoted 10 lines
Quoted 10 lines

Behalf Of Eric Auger
Sent: Thursday, December 04, 2014 10:05 PM
To: Wu, Feng; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; jiang.liu-VuQAYsv1563Yd54FQh9/[email protected]
Cc: [email protected]; iommu-cunTk1MwBs9QetFLy7KEm3xJsTq8ys+cHZ5vskTnxNA@public.gmane.org; [email protected]
Subject: Re: [v2 17/25] KVM: kvm-vfio: User API for VT-d Posted-Interrupts

Quoted 4 lines
Quoted 4 lines

Hi Feng,

Quoted 2 lines · Feng Wu

This patch adds and documents a new attribute KVM_DEV_VFIO_DEVICE_POSTING_IRQ in KVM_DEV_VFIO_DEVICE

group.

Quoted 14 lines
Quoted 14 lines
Quoted 14 lines

This new attribute is used for VT-d Posted-Interrupts.

When guest OS changes the interrupt configuration for an assigned device, such as, MSI/MSIx data/address fields, QEMU will use this IRQ attribute to tell KVM to update the related IRTE according the VT-d Posted-Interrrupts Specification, such as, the guest vector should be updated in the related IRTE.

---
 Documentation/virtual/kvm/devices/vfio.txt |    9 +++++++++
 include/uapi/linux/kvm.h                   |   10 ++++++++++
 2 files changed, 19 insertions(+), 0 deletions(-)
Quoted 6 lines
Quoted 6 lines

Review

Quoted 1 line
(quoted diff) +0 -0

b/Documentation/virtual/kvm/devices/vfio.txt

Documentation/virtual/kvm/devices/vfio.txt activated before VFIO_DEVICE_SET_IRQS has

Quoted 4 lines

index f7aff29..41e12b7 100644

Documentation/virtual/kvm/devices/vfio.txt +0 -0
@@ -42,3 +42,12 @@ activated before VFIO_DEVICE_SET_IRQS has

been

Quoted 3 lines
Quoted 3 lines

called to trigger the IRQ

Quoted 2 lines
Documentation/virtual/kvm/devices/vfio.txt +0 -0
 or associate an eventfd to it. Unforwarding can only be called while the
 signaling has been disabled with VFIO_DEVICE_SET_IRQS. If this

condition

Quoted 11 lines
Quoted 11 lines

is

Documentation/virtual/kvm/devices/vfio.txt activated before VFIO_DEVICE_SET_IRQS has been called to trigger the IRQ

Quoted 3 lines
Documentation/virtual/kvm/devices/vfio.txt +2 -0
 not satisfied, the command returns an -EBUSY.
+
+  KVM_DEV_VFIO_DEVICE_POSTING_IRQ: Use posted interrtups

mechanism to post
typo

Documentation/virtual/kvm/devices/vfio.txt activated before VFIO_DEVICE_SET_IRQS has · line 48

Quoted 2 lines
Documentation/virtual/kvm/devices/vfio.txt +2 -0
+                                   the IRQ to guests.
+For this attribute, kvm_device_attr.addr points to a kvm_vfio_dev_irq

struct.

Documentation/virtual/kvm/devices/vfio.txt activated before VFIO_DEVICE_SET_IRQS has · line 50

Quoted 2 lines
Documentation/virtual/kvm/devices/vfio.txt +2 -0
+
+When guest OS changes the interrupt configuration for an assigned

device,

Quoted 13 lines
Quoted 8 lines

Documentation/virtual/kvm/devices/vfio.txt activated before VFIO_DEVICE_SET_IRQS has · line 52

Quoted 2 lines
Documentation/virtual/kvm/devices/vfio.txt +2 -0
+such as, MSI/MSIx data/address fields, QEMU will use this IRQ attribute
+to tell KVM to update the related IRTE according the VT-d

Posted-Interrrupts

Documentation/virtual/kvm/devices/vfio.txt activated before VFIO_DEVICE_SET_IRQS has · line 53

Quoted 1 line
Documentation/virtual/kvm/devices/vfio.txt +1 -0
+Specification, such as, the guest vector should be updated in the related

IRTE.
For my curiosity are there any restrictions about the instant at which the change can be done?
I do not get here how you deactivate the posting?

The current method is if the hardware supports interrupts posting, we will use it instead of interrupts remapping, since it has good performance. Why do I need deactivate interrupts posting?

Quoted 6 lines

Here is the reply to Alex for the same question:
"In fact, I don't think we need to stop the posted-interrupts. For setting posted interrupts, we update the related IRTE according to the new format. If the guest reboots, or unload the drivers, or some other operations, the msi/msix will be disabled first, in this path, the irq will be disabled the related IRTE is not used anymore."

Right, and I'm still not sure I agree with that reasoning. We need to build the kernel interface to be generic, not tailored for a specific userspace. I don't really feel comfortable having something that can't be disabled via a similar path to it being enabled. For instance, what about a dynamic debug interface where we want to enable tracing and see each interrupt injected into the guest. At that point we'd want to disabled posted interrupts and direct KVM injection and route via QEMU. Thanks,

Quoted 1 line

Alex

I am not quite understand why we need to debug the software delivery path for interrupt when PI is used, in this case, the software injection code will have no chance to execute. If we don't want the use PI, we can disable it from kernel command line.

Thanks,
Feng

Quoted 50 lines
Quoted 48 lines
Quoted 13 lines

include/uapi/linux/kvm.h struct kvm_device_attr { · line 952

Quoted 10 lines
include/uapi/linux/kvm.h +1 -0
index a269a42..7d98650 100644
@@ -949,6 +949,7 @@ struct kvm_device_attr {
 #define  KVM_DEV_VFIO_DEVICE			2
 #define   KVM_DEV_VFIO_DEVICE_FORWARD_IRQ			1
 #define   KVM_DEV_VFIO_DEVICE_UNFORWARD_IRQ			2
+#define   KVM_DEV_VFIO_DEVICE_POSTING_IRQ			3

Maybe we should align our naming verb vs verbing here? Best Regards
Eric

No problem, I will align my patch in the next version. Thanks!

Thanks,
Feng

Quoted 17 lines
Quoted 17 lines
include/uapi/linux/kvm.h +9 -0
 enum kvm_device_type {
 	KVM_DEV_TYPE_FSL_MPIC_20	= 1,
@@ -973,6 +974,15 @@ struct kvm_arch_forwarded_irq {
 	__u32 gsi; /* gsi, ie. virtual IRQ number */
 };
 
+struct kvm_vfio_dev_irq {
+	__u32	argsz;
+	__u32	fd;		/* file descriptor of the VFIO device */
+	__u32	index;		/* VFIO device IRQ index */
+	__u32	start;
+	__u32	count;
+	__u32	gsi[];		/* gsi, ie. virtual IRQ number */
+};
+
 /*
Quoted 8 lines

include/uapi/linux/kvm.h struct kvm_arch_forwarded_irq { · line 988

Quoted 3 lines
include/uapi/linux/kvm.h +0 -0
  * ioctls for VM fds
  */

--
To unsubscribe from this list: send the line "unsubscribe kvm" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html

--
To unsubscribe from this list: send the line "unsubscribe kvm" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html

#23 lore raw

Re: [v2 17/25] KVM: kvm-vfio: User API for VT-d Posted-Interrupts

Quoted 98 lines · Feng
Quoted 98 lines

-----Original Message-----
From: Alex Williamson [mailto:[email protected]]
Sent: Monday, December 08, 2014 1:21 PM
To: Wu, Feng
Cc: Eric Auger; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; jiang.liu-VuQAYsv1563Yd54FQh9/[email protected]; [email protected]; iommu-cunTk1MwBs9QetFLy7KEm3xJsTq8ys+cHZ5vskTnxNA@public.gmane.org; [email protected]
Subject: Re: [v2 17/25] KVM: kvm-vfio: User API for VT-d Posted-Interrupts

Quoted 3 lines · Feng
Quoted 3 lines

-----Original Message-----
From: [email protected] [mailto:[email protected]]

On

Quoted 10 lines
Quoted 10 lines

Behalf Of Eric Auger
Sent: Thursday, December 04, 2014 10:05 PM
To: Wu, Feng; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; jiang.liu-VuQAYsv1563Yd54FQh9/[email protected]
Cc: [email protected]; iommu-cunTk1MwBs9QetFLy7KEm3xJsTq8ys+cHZ5vskTnxNA@public.gmane.org; [email protected]
Subject: Re: [v2 17/25] KVM: kvm-vfio: User API for VT-d Posted-Interrupts

Quoted 4 lines
Quoted 4 lines

Hi Feng,

Quoted 2 lines · Feng Wu

This patch adds and documents a new attribute KVM_DEV_VFIO_DEVICE_POSTING_IRQ in KVM_DEV_VFIO_DEVICE

group.

Quoted 14 lines
Quoted 14 lines
Quoted 14 lines

This new attribute is used for VT-d Posted-Interrupts.

When guest OS changes the interrupt configuration for an assigned device, such as, MSI/MSIx data/address fields, QEMU will use this IRQ attribute to tell KVM to update the related IRTE according the VT-d Posted-Interrrupts Specification, such as, the guest vector should be updated in the related IRTE.

---
 Documentation/virtual/kvm/devices/vfio.txt |    9 +++++++++
 include/uapi/linux/kvm.h                   |   10 ++++++++++
 2 files changed, 19 insertions(+), 0 deletions(-)
Quoted 6 lines
Quoted 6 lines

Review

Quoted 1 line
(quoted diff) +0 -0

b/Documentation/virtual/kvm/devices/vfio.txt

Documentation/virtual/kvm/devices/vfio.txt activated before VFIO_DEVICE_SET_IRQS has

Quoted 4 lines

index f7aff29..41e12b7 100644

Documentation/virtual/kvm/devices/vfio.txt +0 -0
@@ -42,3 +42,12 @@ activated before VFIO_DEVICE_SET_IRQS has

been

Quoted 3 lines
Quoted 3 lines

called to trigger the IRQ

Quoted 2 lines
Documentation/virtual/kvm/devices/vfio.txt +0 -0
 or associate an eventfd to it. Unforwarding can only be called while the
 signaling has been disabled with VFIO_DEVICE_SET_IRQS. If this

condition

Quoted 11 lines
Quoted 11 lines

is

Documentation/virtual/kvm/devices/vfio.txt activated before VFIO_DEVICE_SET_IRQS has been called to trigger the IRQ

Quoted 3 lines
Documentation/virtual/kvm/devices/vfio.txt +2 -0
 not satisfied, the command returns an -EBUSY.
+
+  KVM_DEV_VFIO_DEVICE_POSTING_IRQ: Use posted interrtups

mechanism to post
typo

Documentation/virtual/kvm/devices/vfio.txt activated before VFIO_DEVICE_SET_IRQS has · line 48

Quoted 2 lines
Documentation/virtual/kvm/devices/vfio.txt +2 -0
+                                   the IRQ to guests.
+For this attribute, kvm_device_attr.addr points to a kvm_vfio_dev_irq

struct.

Documentation/virtual/kvm/devices/vfio.txt activated before VFIO_DEVICE_SET_IRQS has · line 50

Quoted 2 lines
Documentation/virtual/kvm/devices/vfio.txt +2 -0
+
+When guest OS changes the interrupt configuration for an assigned

device,

Quoted 13 lines
Quoted 8 lines

Documentation/virtual/kvm/devices/vfio.txt activated before VFIO_DEVICE_SET_IRQS has · line 52

Quoted 2 lines
Documentation/virtual/kvm/devices/vfio.txt +2 -0
+such as, MSI/MSIx data/address fields, QEMU will use this IRQ attribute
+to tell KVM to update the related IRTE according the VT-d

Posted-Interrrupts

Documentation/virtual/kvm/devices/vfio.txt activated before VFIO_DEVICE_SET_IRQS has · line 53

Quoted 1 line
Documentation/virtual/kvm/devices/vfio.txt +1 -0
+Specification, such as, the guest vector should be updated in the related

IRTE.
For my curiosity are there any restrictions about the instant at which the change can be done?
I do not get here how you deactivate the posting?

The current method is if the hardware supports interrupts posting, we will use it instead of interrupts remapping, since it has good performance. Why do I need deactivate interrupts posting?

Quoted 6 lines

Here is the reply to Alex for the same question:
"In fact, I don't think we need to stop the posted-interrupts. For setting posted interrupts, we update the related IRTE according to the new format. If the guest reboots, or unload the drivers, or some other operations, the msi/msix will be disabled first, in this path, the irq will be disabled the related IRTE is not used anymore."

Right, and I'm still not sure I agree with that reasoning. We need to build the kernel interface to be generic, not tailored for a specific userspace. I don't really feel comfortable having something that can't be disabled via a similar path to it being enabled. For instance, what about a dynamic debug interface where we want to enable tracing and see each interrupt injected into the guest. At that point we'd want to disabled posted interrupts and direct KVM injection and route via QEMU. Thanks,

Quoted 6 lines
Quoted 1 line

Alex

I am not quite understand why we need to debug the software delivery path for interrupt when PI is used, in this case, the software injection code will have no chance to execute. If we don't want the use PI, we can disable it from kernel command line.

Well, first off, I think it's just good interface design that if we introduce the ability to enable something we also provide the ability to disable it without making assumption about how a specific userspace is expected to make use of the interface. In this specific case, you're missing the "dynamic" aspect of the switch. What if we want to attach gdb to the guest and at that point disable all KVM accelerations for an assigned device so that we can see every register access and every interrupt? I don't think the interface proposed here allows for that because it expects the guest to switch to a different interrupt mode for the device in order to disable PI. Don't design the interface tailored only for a specific use case. Thanks,

Alex

Quoted 50 lines
Quoted 49 lines
Quoted 47 lines
Quoted 4 lines
Quoted 4 lines
include/uapi/linux/kvm.h +0 -0
index a269a42..7d98650 100644
Quoted 8 lines

include/uapi/linux/kvm.h struct kvm_device_attr { · line 952

Quoted 5 lines
include/uapi/linux/kvm.h +1 -0
@@ -949,6 +949,7 @@ struct kvm_device_attr {
 #define  KVM_DEV_VFIO_DEVICE			2
 #define   KVM_DEV_VFIO_DEVICE_FORWARD_IRQ			1
 #define   KVM_DEV_VFIO_DEVICE_UNFORWARD_IRQ			2
+#define   KVM_DEV_VFIO_DEVICE_POSTING_IRQ			3

Maybe we should align our naming verb vs verbing here? Best Regards
Eric

No problem, I will align my patch in the next version. Thanks!

Thanks,
Feng

Quoted 17 lines
Quoted 17 lines
include/uapi/linux/kvm.h +9 -0
 enum kvm_device_type {
 	KVM_DEV_TYPE_FSL_MPIC_20	= 1,
@@ -973,6 +974,15 @@ struct kvm_arch_forwarded_irq {
 	__u32 gsi; /* gsi, ie. virtual IRQ number */
 };
 
+struct kvm_vfio_dev_irq {
+	__u32	argsz;
+	__u32	fd;		/* file descriptor of the VFIO device */
+	__u32	index;		/* VFIO device IRQ index */
+	__u32	start;
+	__u32	count;
+	__u32	gsi[];		/* gsi, ie. virtual IRQ number */
+};
+
 /*
Quoted 8 lines

include/uapi/linux/kvm.h struct kvm_arch_forwarded_irq { · line 988

Quoted 3 lines
include/uapi/linux/kvm.h +0 -0
  * ioctls for VM fds
  */

--
To unsubscribe from this list: send the line "unsubscribe kvm" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html

--
To unsubscribe from this list: send the line "unsubscribe kvm" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html

author
#24 lore raw

PATCH v2 18/25 KVM: kvm-vfio: implement the VFIO skeleton for VT-d Posted-Interrupts

In reply to Feng Wu

This patch adds the kvm-vfio interface for VT-d Posted-Interrrupts. When guests updates MSI/MSI-x information for an assigned-device, QEMU will use KVM_DEV_VFIO_DEVICE_POSTING_IRQ attribute to setup IRTE for VT-d PI. This patch implement this IRQ attribute.

---
 include/linux/kvm_host.h |   19 ++++++++
 virt/kvm/vfio.c          |  103 ++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 122 insertions(+), 0 deletions(-)
include/linux/kvm_host.h +19 -0
index 5cd4420..8d06678 100644
@@ -1134,6 +1134,25 @@ static inline int kvm_arch_vfio_set_forward(struct kvm_fwd_irq *fwd_irq,
 }
 #endif
 
+#ifdef __KVM_HAVE_ARCH_KVM_VFIO_POSTING
+/*
+ * kvm_arch_vfio_update_pi_irte - set IRTE for Posted-Interrupts
+ *
+ * @kvm: kvm
+ * @host_irq: host irq of the interrupt
+ * @guest_irq: gsi of the interrupt
+ * returns 0 on success, < 0 on failure
+ */
+int kvm_arch_vfio_update_pi_irte(struct kvm *kvm, unsigned int host_irq,
+				 uint32_t guest_irq);
+#else
+static int kvm_arch_vfio_update_pi_irte(struct kvm *kvm, unsigned int host_irq,
+					uint32_t guest_irq)
+{
+	return 0;
+}
+#endif
+
 #ifdef CONFIG_HAVE_KVM_CPU_RELAX_INTERCEPT
 
 static inline void kvm_vcpu_set_in_spin_loop(struct kvm_vcpu *vcpu, bool val)
virt/kvm/vfio.c +103 -0

121 lines not on this page · raw message

Signature · 1 line
1.7.1
#25 lore raw

Re: [v2 18/25] KVM: kvm-vfio: implement the VFIO skeleton for VT-d Posted-Interrupts

Hi Feng,

Quoted 2 lines · Feng Wu

This patch adds the kvm-vfio interface for VT-d Posted-Interrrupts. When guests updates MSI/MSI-x information for an assigned-device,

update

Quoted 2 lines

QEMU will use KVM_DEV_VFIO_DEVICE_POSTING_IRQ attribute to setup IRTE for VT-d PI. This patch implement this IRQ attribute.

s/implement/implements

Quoted 50 lines
---
 include/linux/kvm_host.h |   19 ++++++++
 virt/kvm/vfio.c          |  103 ++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 122 insertions(+), 0 deletions(-)
include/linux/kvm_host.h +19 -0
index 5cd4420..8d06678 100644
@@ -1134,6 +1134,25 @@ static inline int kvm_arch_vfio_set_forward(struct kvm_fwd_irq *fwd_irq,
 }
 #endif
 
+#ifdef __KVM_HAVE_ARCH_KVM_VFIO_POSTING
+/*
+ * kvm_arch_vfio_update_pi_irte - set IRTE for Posted-Interrupts
+ *
+ * @kvm: kvm
+ * @host_irq: host irq of the interrupt
+ * @guest_irq: gsi of the interrupt
+ * returns 0 on success, < 0 on failure
+ */
+int kvm_arch_vfio_update_pi_irte(struct kvm *kvm, unsigned int host_irq,
+				 uint32_t guest_irq);
+#else
+static int kvm_arch_vfio_update_pi_irte(struct kvm *kvm, unsigned int host_irq,
+					uint32_t guest_irq)
+{
+	return 0;
+}
+#endif
+
 #ifdef CONFIG_HAVE_KVM_CPU_RELAX_INTERCEPT
 
 static inline void kvm_vcpu_set_in_spin_loop(struct kvm_vcpu *vcpu, bool val)
virt/kvm/vfio.c +5 -0
index 6bc7001..5e5515f 100644
@@ -446,6 +446,99 @@ out:
 	return ret;
 }
 
+static int kvm_vfio_pci_get_irq_count(struct pci_dev *pdev, int irq_type)
+{
+	if (irq_type == VFIO_PCI_INTX_IRQ_INDEX) {
+		u8 pin;
+

virt/kvm/vfio.c out: · line 463

Quoted 10 lines
virt/kvm/vfio.c +10 -0
+		pci_read_config_byte(pdev, PCI_INTERRUPT_PIN, &pin);
+		if (pin)
+			return 1;
+	} else if (irq_type == VFIO_PCI_MSI_IRQ_INDEX)
+		return pci_msi_vec_count(pdev);
+	else if (irq_type == VFIO_PCI_MSIX_IRQ_INDEX)
+		return pci_msix_vec_count(pdev);
+
+	return 0;
+}

for platform case I was asked to move the retrieval of absolute irq number to the architecture specific part. I don't know if it should apply to PCI stuff as well? This explains why I need to pass the VFIO device (or struct device handle) to the arch specific part. Actually we do the same job, we provide a phys/virt IRQ mapping to KVM, right? So to me our architecture specific API should look quite similar?

Quoted 8 lines
virt/kvm/vfio.c +8 -0
+
+static int kvm_vfio_set_pi(struct kvm_device *kdev, int32_t __user *argp)
+{
+	struct kvm_vfio_dev_irq pi_info;
+	uint32_t *gsi;
+	unsigned long minsz;
+	struct vfio_device *vdev;
+	struct msi_desc *entry;

virt/kvm/vfio.c out: · line 481

Quoted 10 lines
virt/kvm/vfio.c +10 -0
+	struct device *dev;
+	struct pci_dev *pdev;
+	int i, max, ret;
+
+	minsz = offsetofend(struct kvm_vfio_dev_irq, count);
+
+	if (copy_from_user(&pi_info, (void __user *)argp, minsz))
+		return -EFAULT;
+
+	if (pi_info.argsz < minsz || pi_info.index >= VFIO_PCI_NUM_IRQS)

PCI specific check, same remark as above but I will let Alex further comment on this and possibly invalidate this commeny ;-)

Quoted 11 lines
virt/kvm/vfio.c +11 -0
+		return -EINVAL;
+
+	vdev = kvm_vfio_get_vfio_device(pi_info.fd);
+	if (IS_ERR(vdev))
+		return PTR_ERR(vdev);
+
+	dev = kvm_vfio_external_base_device(vdev);
+	if (!dev || !dev_is_pci(dev)) {
+		ret = -EFAULT;
+		goto put_vfio_device;
+	}

virt/kvm/vfio.c out: · line 502

Quoted 10 lines
virt/kvm/vfio.c +10 -0
+
+	pdev = to_pci_dev(dev);
+
+	max = kvm_vfio_pci_get_irq_count(pdev, pi_info.index);
+	if (max <= 0) {
+		ret = -EFAULT;
+		goto put_vfio_device;
+	}
+
+	if (pi_info.argsz - minsz < pi_info.count * sizeof(int) ||

shouldn' we use the actual datatype?

virt/kvm/vfio.c out: · line 509

Quoted 7 lines
virt/kvm/vfio.c +7 -0
+	    pi_info.start >= max || pi_info.start + pi_info.count > max) {
+		ret = -EINVAL;
+		goto put_vfio_device;
+	}
+
+	gsi = memdup_user((void __user *)((unsigned long)argp + minsz),
+			   pi_info.count * sizeof(int));

same question as above

Quoted 6 lines
virt/kvm/vfio.c +6 -0
+	if (IS_ERR(gsi)) {
+		ret = PTR_ERR(gsi);
+		goto put_vfio_device;
+	}
+
+#ifdef CONFIG_PCI_MSI

virt/kvm/vfio.c out: · line 525

Quoted 10 lines
virt/kvm/vfio.c +10 -0
+	for (i = 0; i < pi_info.count; i++) {
+		list_for_each_entry(entry, &pdev->msi_list, list) {
+			if (entry->msi_attrib.entry_nr != pi_info.start+i)
+				continue;
+
+			ret = kvm_arch_vfio_update_pi_irte(kdev->kvm,
+							   entry->irq,
+							   gsi[i]);
+			if (ret) {
+				ret = -EFAULT;

why -EFAULT? and not propagation of original error code?
you may have posting set for part of the subindexes and unset for rest. Isn't it an issue?

Best Regards

Eric

Quoted 44 lines
virt/kvm/vfio.c +26 -0
+				goto free_gsi;
+			}
+		}
+	}
+#endif
+
+	ret = 0;
+
+free_gsi:
+	kfree(gsi);
+
+put_vfio_device:
+	kvm_vfio_put_vfio_device(vdev);
+	return ret;
+}
+
 static int kvm_vfio_set_device(struct kvm_device *kdev, long attr, u64 arg)
 {
 	int32_t __user *argp = (int32_t __user *)(unsigned long)arg;
@@ -456,6 +549,11 @@ static int kvm_vfio_set_device(struct kvm_device *kdev, long attr, u64 arg)
 	case KVM_DEV_VFIO_DEVICE_UNFORWARD_IRQ:
 		ret = kvm_vfio_control_irq_forward(kdev, attr, argp);
 		break;
+#ifdef __KVM_HAVE_ARCH_KVM_VFIO_POSTING
+	case KVM_DEV_VFIO_DEVICE_POSTING_IRQ:
+		ret = kvm_vfio_set_pi(kdev, argp);
+		break;
+#endif
 	default:
 		ret = -ENXIO;
 	}
@@ -511,6 +609,11 @@ static int kvm_vfio_has_attr(struct kvm_device *dev,
 		case KVM_DEV_VFIO_DEVICE_UNFORWARD_IRQ:
 			return 0;
 #endif
+#ifdef __KVM_HAVE_ARCH_KVM_VFIO_POSTING
+		case KVM_DEV_VFIO_DEVICE_POSTING_IRQ:
+			return 0;
+#endif
+
 		}
 		break;
 	}
author
#26 lore raw

RE: [v2 18/25] KVM: kvm-vfio: implement the VFIO skeleton for VT-d Posted-Interrupts

Quoted 85 lines

-----Original Message-----
From: Eric Auger [mailto:[email protected]]
Sent: Thursday, December 04, 2014 11:36 PM
To: Wu, Feng; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; jiang.liu-VuQAYsv1563Yd54FQh9/[email protected]
Cc: [email protected]; iommu-cunTk1MwBs9QetFLy7KEm3xJsTq8ys+cHZ5vskTnxNA@public.gmane.org; [email protected]
Subject: Re: [v2 18/25] KVM: kvm-vfio: implement the VFIO skeleton for VT-d
Posted-Interrupts

Hi Feng,

Quoted 2 lines · Feng Wu

This patch adds the kvm-vfio interface for VT-d Posted-Interrrupts. When guests updates MSI/MSI-x information for an assigned-device,

update

Quoted 2 lines

QEMU will use KVM_DEV_VFIO_DEVICE_POSTING_IRQ attribute to setup IRTE for VT-d PI. This patch implement this IRQ attribute.

s/implement/implements

Quoted 5 lines
---
 include/linux/kvm_host.h |   19 ++++++++
 virt/kvm/vfio.c          |  103

++++++++++++++++++++++++++++++++++++++++++++++

include/linux/kvm_host.h static inline int

Quoted 7 lines
 2 files changed, 122 insertions(+), 0 deletions(-)
include/linux/kvm_host.h +0 -0
index 5cd4420..8d06678 100644
@@ -1134,6 +1134,25 @@ static inline int

kvm_arch_vfio_set_forward(struct kvm_fwd_irq *fwd_irq,

Quoted 6 lines
include/linux/kvm_host.h +3 -0
 }
 #endif
 
+#ifdef __KVM_HAVE_ARCH_KVM_VFIO_POSTING
+/*
+ * kvm_arch_vfio_update_pi_irte - set IRTE for Posted-Interrupts

include/linux/kvm_host.h static inline int kvm_arch_vfio_set_forward(struct kvm_fwd_irq *fwd_irq,

Quoted 10 lines
include/linux/kvm_host.h +10 -0
+ *
+ * @kvm: kvm
+ * @host_irq: host irq of the interrupt
+ * @guest_irq: gsi of the interrupt
+ * returns 0 on success, < 0 on failure
+ */
+int kvm_arch_vfio_update_pi_irte(struct kvm *kvm, unsigned int host_irq,
+				 uint32_t guest_irq);
+#else
+static int kvm_arch_vfio_update_pi_irte(struct kvm *kvm, unsigned int

host_irq,

include/linux/kvm_host.h static inline int kvm_arch_vfio_set_forward(struct kvm_fwd_irq *fwd_irq,

Quoted 9 lines
include/linux/kvm_host.h +6 -0
+					uint32_t guest_irq)
+{
+	return 0;
+}
+#endif
+
 #ifdef CONFIG_HAVE_KVM_CPU_RELAX_INTERCEPT
 
 static inline void kvm_vcpu_set_in_spin_loop(struct kvm_vcpu *vcpu, bool

val)

Quoted 21 lines
virt/kvm/vfio.c +13 -0
index 6bc7001..5e5515f 100644
@@ -446,6 +446,99 @@ out:
 	return ret;
 }
 
+static int kvm_vfio_pci_get_irq_count(struct pci_dev *pdev, int irq_type)
+{
+	if (irq_type == VFIO_PCI_INTX_IRQ_INDEX) {
+		u8 pin;
+
+		pci_read_config_byte(pdev, PCI_INTERRUPT_PIN, &pin);
+		if (pin)
+			return 1;
+	} else if (irq_type == VFIO_PCI_MSI_IRQ_INDEX)
+		return pci_msi_vec_count(pdev);
+	else if (irq_type == VFIO_PCI_MSIX_IRQ_INDEX)
+		return pci_msix_vec_count(pdev);
+
Quoted 8 lines

virt/kvm/vfio.c out: · line 463

Quoted 2 lines
virt/kvm/vfio.c +2 -0
+	return 0;
+}

for platform case I was asked to move the retrieval of absolute irq number to the architecture specific part. I don't know if it should apply to PCI stuff as well? This explains why I need to pass the VFIO device (or struct device handle) to the arch specific part. Actually we do the same job, we provide a phys/virt IRQ mapping to KVM, right? So to me our architecture specific API should look quite similar?

In my patch, QEMU passes IRQ type(MSI/MSIx in my case), VFIO device index, and sub-index via "struct kvm_vfio_dev_irq" to KVM, then KVM will find the real host irq from the VFIO device index and the IRQ type. Is this something similar with your patch?

Quoted 35 lines
Quoted 9 lines
virt/kvm/vfio.c +8 -0
+
+static int kvm_vfio_set_pi(struct kvm_device *kdev, int32_t __user *argp)
+{
+	struct kvm_vfio_dev_irq pi_info;
+	uint32_t *gsi;
+	unsigned long minsz;
+	struct vfio_device *vdev;
+	struct msi_desc *entry;

virt/kvm/vfio.c out: · line 481

Quoted 10 lines
virt/kvm/vfio.c +10 -0
+	struct device *dev;
+	struct pci_dev *pdev;
+	int i, max, ret;
+
+	minsz = offsetofend(struct kvm_vfio_dev_irq, count);
+
+	if (copy_from_user(&pi_info, (void __user *)argp, minsz))
+		return -EFAULT;
+
+	if (pi_info.argsz < minsz || pi_info.index >= VFIO_PCI_NUM_IRQS)

PCI specific check, same remark as above but I will let Alex further comment on this and possibly invalidate this commeny ;-)

Quoted 14 lines
virt/kvm/vfio.c +14 -0
+		return -EINVAL;
+
+	vdev = kvm_vfio_get_vfio_device(pi_info.fd);
+	if (IS_ERR(vdev))
+		return PTR_ERR(vdev);
+
+	dev = kvm_vfio_external_base_device(vdev);
+	if (!dev || !dev_is_pci(dev)) {
+		ret = -EFAULT;
+		goto put_vfio_device;
+	}
+
+	pdev = to_pci_dev(dev);
+
Quoted 8 lines

virt/kvm/vfio.c out: · line 502

Quoted 7 lines
virt/kvm/vfio.c +7 -0
+	max = kvm_vfio_pci_get_irq_count(pdev, pi_info.index);
+	if (max <= 0) {
+		ret = -EFAULT;
+		goto put_vfio_device;
+	}
+
+	if (pi_info.argsz - minsz < pi_info.count * sizeof(int) ||

shouldn' we use the actual datatype?

I am afraid I don't get this, could you please be more specific? Thanks a lot!

Quoted 17 lines

virt/kvm/vfio.c out: · line 509

Quoted 7 lines
virt/kvm/vfio.c +7 -0
+	    pi_info.start >= max || pi_info.start + pi_info.count > max) {
+		ret = -EINVAL;
+		goto put_vfio_device;
+	}
+
+	gsi = memdup_user((void __user *)((unsigned long)argp + minsz),
+			   pi_info.count * sizeof(int));

same question as above

Quoted 9 lines
virt/kvm/vfio.c +9 -0
+	if (IS_ERR(gsi)) {
+		ret = PTR_ERR(gsi);
+		goto put_vfio_device;
+	}
+
+#ifdef CONFIG_PCI_MSI
+	for (i = 0; i < pi_info.count; i++) {
+		list_for_each_entry(entry, &pdev->msi_list, list) {
+			if (entry->msi_attrib.entry_nr != pi_info.start+i)
Quoted 8 lines

virt/kvm/vfio.c out: · line 525

Quoted 7 lines
virt/kvm/vfio.c +7 -0
+				continue;
+
+			ret = kvm_arch_vfio_update_pi_irte(kdev->kvm,
+							   entry->irq,
+							   gsi[i]);
+			if (ret) {
+				ret = -EFAULT;

why -EFAULT? and not propagation of original error code?

Yes, you are right. Thanks for the comments!

Quoted 2 lines

you may have posting set for part of the subindexes and unset for rest. Isn't it an issue?

QEMU will always set the posting for all the sub-indexes for MSI/MSIx, once the guest updates the configuration of some sub-indexes, KVM will update it accordingly. So in which case will what you mentioned above happen?

Thanks,
Feng

Quoted 50 lines

Best Regards

Eric

virt/kvm/vfio.c static int kvm_vfio_set_device(struct kvm_device

Quoted 20 lines
virt/kvm/vfio.c +16 -0
+				goto free_gsi;
+			}
+		}
+	}
+#endif
+
+	ret = 0;
+
+free_gsi:
+	kfree(gsi);
+
+put_vfio_device:
+	kvm_vfio_put_vfio_device(vdev);
+	return ret;
+}
+
 static int kvm_vfio_set_device(struct kvm_device *kdev, long attr, u64 arg)
 {
 	int32_t __user *argp = (int32_t __user *)(unsigned long)arg;
@@ -456,6 +549,11 @@ static int kvm_vfio_set_device(struct kvm_device

*kdev, long attr, u64 arg)

virt/kvm/vfio.c static int kvm_vfio_has_attr(struct kvm_device

Quoted 12 lines
virt/kvm/vfio.c +5 -0
 	case KVM_DEV_VFIO_DEVICE_UNFORWARD_IRQ:
 		ret = kvm_vfio_control_irq_forward(kdev, attr, argp);
 		break;
+#ifdef __KVM_HAVE_ARCH_KVM_VFIO_POSTING
+	case KVM_DEV_VFIO_DEVICE_POSTING_IRQ:
+		ret = kvm_vfio_set_pi(kdev, argp);
+		break;
+#endif
 	default:
 		ret = -ENXIO;
 	}
@@ -511,6 +609,11 @@ static int kvm_vfio_has_attr(struct kvm_device

*dev,

Quoted 12 lines
virt/kvm/vfio.c +5 -0
 		case KVM_DEV_VFIO_DEVICE_UNFORWARD_IRQ:
 			return 0;
 #endif
+#ifdef __KVM_HAVE_ARCH_KVM_VFIO_POSTING
+		case KVM_DEV_VFIO_DEVICE_POSTING_IRQ:
+			return 0;
+#endif
+
 		}
 		break;
 	}
#27 lore raw

Re: [v2 18/25] KVM: kvm-vfio: implement the VFIO skeleton for VT-d Posted-Interrupts

Quoted 142 lines · Feng
Quoted 84 lines

-----Original Message-----
From: Eric Auger [mailto:[email protected]]
Sent: Thursday, December 04, 2014 11:36 PM
To: Wu, Feng; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; jiang.liu-VuQAYsv1563Yd54FQh9/[email protected]
Cc: [email protected]; iommu-cunTk1MwBs9QetFLy7KEm3xJsTq8ys+cHZ5vskTnxNA@public.gmane.org; [email protected]
Subject: Re: [v2 18/25] KVM: kvm-vfio: implement the VFIO skeleton for VT-d
Posted-Interrupts

Hi Feng,

Quoted 2 lines · Feng Wu

This patch adds the kvm-vfio interface for VT-d Posted-Interrrupts. When guests updates MSI/MSI-x information for an assigned-device,

update

Quoted 2 lines

QEMU will use KVM_DEV_VFIO_DEVICE_POSTING_IRQ attribute to setup IRTE for VT-d PI. This patch implement this IRQ attribute.

s/implement/implements

Quoted 5 lines
---
 include/linux/kvm_host.h |   19 ++++++++
 virt/kvm/vfio.c          |  103

++++++++++++++++++++++++++++++++++++++++++++++

include/linux/kvm_host.h static inline int

Quoted 7 lines
 2 files changed, 122 insertions(+), 0 deletions(-)
include/linux/kvm_host.h +0 -0
index 5cd4420..8d06678 100644
@@ -1134,6 +1134,25 @@ static inline int

kvm_arch_vfio_set_forward(struct kvm_fwd_irq *fwd_irq,

Quoted 6 lines
include/linux/kvm_host.h +3 -0
 }
 #endif
 
+#ifdef __KVM_HAVE_ARCH_KVM_VFIO_POSTING
+/*
+ * kvm_arch_vfio_update_pi_irte - set IRTE for Posted-Interrupts

include/linux/kvm_host.h static inline int kvm_arch_vfio_set_forward(struct kvm_fwd_irq *fwd_irq,

Quoted 10 lines
include/linux/kvm_host.h +10 -0
+ *
+ * @kvm: kvm
+ * @host_irq: host irq of the interrupt
+ * @guest_irq: gsi of the interrupt
+ * returns 0 on success, < 0 on failure
+ */
+int kvm_arch_vfio_update_pi_irte(struct kvm *kvm, unsigned int host_irq,
+				 uint32_t guest_irq);
+#else
+static int kvm_arch_vfio_update_pi_irte(struct kvm *kvm, unsigned int

host_irq,

include/linux/kvm_host.h static inline int kvm_arch_vfio_set_forward(struct kvm_fwd_irq *fwd_irq,

Quoted 9 lines
include/linux/kvm_host.h +6 -0
+					uint32_t guest_irq)
+{
+	return 0;
+}
+#endif
+
 #ifdef CONFIG_HAVE_KVM_CPU_RELAX_INTERCEPT
 
 static inline void kvm_vcpu_set_in_spin_loop(struct kvm_vcpu *vcpu, bool

val)

Quoted 21 lines
virt/kvm/vfio.c +13 -0
index 6bc7001..5e5515f 100644
@@ -446,6 +446,99 @@ out:
 	return ret;
 }
 
+static int kvm_vfio_pci_get_irq_count(struct pci_dev *pdev, int irq_type)
+{
+	if (irq_type == VFIO_PCI_INTX_IRQ_INDEX) {
+		u8 pin;
+
+		pci_read_config_byte(pdev, PCI_INTERRUPT_PIN, &pin);
+		if (pin)
+			return 1;
+	} else if (irq_type == VFIO_PCI_MSI_IRQ_INDEX)
+		return pci_msi_vec_count(pdev);
+	else if (irq_type == VFIO_PCI_MSIX_IRQ_INDEX)
+		return pci_msix_vec_count(pdev);
+
Quoted 8 lines

virt/kvm/vfio.c out: · line 463

Quoted 2 lines
virt/kvm/vfio.c +2 -0
+	return 0;
+}

for platform case I was asked to move the retrieval of absolute irq number to the architecture specific part. I don't know if it should apply to PCI stuff as well? This explains why I need to pass the VFIO device (or struct device handle) to the arch specific part. Actually we do the same job, we provide a phys/virt IRQ mapping to KVM, right? So to me our architecture specific API should look quite similar?

In my patch, QEMU passes IRQ type(MSI/MSIx in my case), VFIO device index, and sub-index via "struct kvm_vfio_dev_irq" to KVM, then KVM will find the real host irq from the VFIO device index and the IRQ type. Is this something similar with your patch?

Quoted 35 lines
Quoted 9 lines
virt/kvm/vfio.c +8 -0
+
+static int kvm_vfio_set_pi(struct kvm_device *kdev, int32_t __user *argp)
+{
+	struct kvm_vfio_dev_irq pi_info;
+	uint32_t *gsi;
+	unsigned long minsz;
+	struct vfio_device *vdev;
+	struct msi_desc *entry;

virt/kvm/vfio.c out: · line 481

Quoted 10 lines
virt/kvm/vfio.c +10 -0
+	struct device *dev;
+	struct pci_dev *pdev;
+	int i, max, ret;
+
+	minsz = offsetofend(struct kvm_vfio_dev_irq, count);
+
+	if (copy_from_user(&pi_info, (void __user *)argp, minsz))
+		return -EFAULT;
+
+	if (pi_info.argsz < minsz || pi_info.index >= VFIO_PCI_NUM_IRQS)

PCI specific check, same remark as above but I will let Alex further comment on this and possibly invalidate this commeny ;-)

Quoted 14 lines
virt/kvm/vfio.c +14 -0
+		return -EINVAL;
+
+	vdev = kvm_vfio_get_vfio_device(pi_info.fd);
+	if (IS_ERR(vdev))
+		return PTR_ERR(vdev);
+
+	dev = kvm_vfio_external_base_device(vdev);
+	if (!dev || !dev_is_pci(dev)) {
+		ret = -EFAULT;
+		goto put_vfio_device;
+	}
+
+	pdev = to_pci_dev(dev);
+
Quoted 8 lines

virt/kvm/vfio.c out: · line 502

Quoted 7 lines
virt/kvm/vfio.c +7 -0
+	max = kvm_vfio_pci_get_irq_count(pdev, pi_info.index);
+	if (max <= 0) {
+		ret = -EFAULT;
+		goto put_vfio_device;
+	}
+
+	if (pi_info.argsz - minsz < pi_info.count * sizeof(int) ||

shouldn' we use the actual datatype?

Quoted 1 line

I am afraid I don't get this, could you please be more specific? Thanks a lot!

We could have a platform that supports 64bit INTs.

Quoted 27 lines
Quoted 17 lines

virt/kvm/vfio.c out: · line 509

Quoted 7 lines
virt/kvm/vfio.c +7 -0
+	    pi_info.start >= max || pi_info.start + pi_info.count > max) {
+		ret = -EINVAL;
+		goto put_vfio_device;
+	}
+
+	gsi = memdup_user((void __user *)((unsigned long)argp + minsz),
+			   pi_info.count * sizeof(int));

same question as above

Quoted 9 lines
virt/kvm/vfio.c +9 -0
+	if (IS_ERR(gsi)) {
+		ret = PTR_ERR(gsi);
+		goto put_vfio_device;
+	}
+
+#ifdef CONFIG_PCI_MSI
+	for (i = 0; i < pi_info.count; i++) {
+		list_for_each_entry(entry, &pdev->msi_list, list) {
+			if (entry->msi_attrib.entry_nr != pi_info.start+i)
Quoted 8 lines

virt/kvm/vfio.c out: · line 525

Quoted 7 lines
virt/kvm/vfio.c +7 -0
+				continue;
+
+			ret = kvm_arch_vfio_update_pi_irte(kdev->kvm,
+							   entry->irq,
+							   gsi[i]);
+			if (ret) {
+				ret = -EFAULT;

why -EFAULT? and not propagation of original error code?

Yes, you are right. Thanks for the comments!

Quoted 7 lines
Quoted 2 lines

you may have posting set for part of the subindexes and unset for rest. Isn't it an issue?

QEMU will always set the posting for all the sub-indexes for MSI/MSIx, once the guest updates the configuration of some sub-indexes, KVM will update it accordingly. So in which case will what you mentioned above happen?

QEMU is just one userspace, not necessarily the only userspace. The kernel shouldn't expect a specific userspace behavior.

Quoted 51 lines
Quoted 34 lines

virt/kvm/vfio.c static int kvm_vfio_set_device(struct kvm_device

Quoted 20 lines
virt/kvm/vfio.c +16 -0
+				goto free_gsi;
+			}
+		}
+	}
+#endif
+
+	ret = 0;
+
+free_gsi:
+	kfree(gsi);
+
+put_vfio_device:
+	kvm_vfio_put_vfio_device(vdev);
+	return ret;
+}
+
 static int kvm_vfio_set_device(struct kvm_device *kdev, long attr, u64 arg)
 {
 	int32_t __user *argp = (int32_t __user *)(unsigned long)arg;
@@ -456,6 +549,11 @@ static int kvm_vfio_set_device(struct kvm_device

*kdev, long attr, u64 arg)

virt/kvm/vfio.c static int kvm_vfio_has_attr(struct kvm_device

Quoted 12 lines
virt/kvm/vfio.c +5 -0
 	case KVM_DEV_VFIO_DEVICE_UNFORWARD_IRQ:
 		ret = kvm_vfio_control_irq_forward(kdev, attr, argp);
 		break;
+#ifdef __KVM_HAVE_ARCH_KVM_VFIO_POSTING
+	case KVM_DEV_VFIO_DEVICE_POSTING_IRQ:
+		ret = kvm_vfio_set_pi(kdev, argp);
+		break;
+#endif
 	default:
 		ret = -ENXIO;
 	}
@@ -511,6 +609,11 @@ static int kvm_vfio_has_attr(struct kvm_device

*dev,

Quoted 12 lines

virt/kvm/vfio.c static int kvm_vfio_has_attr(struct kvm_device *dev, · line 619

Quoted 12 lines
virt/kvm/vfio.c +5 -0
 		case KVM_DEV_VFIO_DEVICE_UNFORWARD_IRQ:
 			return 0;
 #endif
+#ifdef __KVM_HAVE_ARCH_KVM_VFIO_POSTING
+		case KVM_DEV_VFIO_DEVICE_POSTING_IRQ:
+			return 0;
+#endif
+
 		}
 		break;
 	}

--
To unsubscribe from this list: send the line "unsubscribe kvm" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html

#28 lore raw

Re: [v2 18/25] KVM: kvm-vfio: implement the VFIO skeleton for VT-d Posted-Interrupts

Quoted 143 lines · Alex Williamson
Quoted 142 lines · Feng
Quoted 84 lines

-----Original Message-----
From: Eric Auger [mailto:[email protected]]
Sent: Thursday, December 04, 2014 11:36 PM
To: Wu, Feng; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; jiang.liu-VuQAYsv1563Yd54FQh9/[email protected]
Cc: [email protected]; iommu-cunTk1MwBs9QetFLy7KEm3xJsTq8ys+cHZ5vskTnxNA@public.gmane.org; [email protected]
Subject: Re: [v2 18/25] KVM: kvm-vfio: implement the VFIO skeleton for VT-d
Posted-Interrupts

Hi Feng,

Quoted 2 lines · Feng Wu

This patch adds the kvm-vfio interface for VT-d Posted-Interrrupts. When guests updates MSI/MSI-x information for an assigned-device,

update

Quoted 2 lines

QEMU will use KVM_DEV_VFIO_DEVICE_POSTING_IRQ attribute to setup IRTE for VT-d PI. This patch implement this IRQ attribute.

s/implement/implements

Quoted 5 lines
---
 include/linux/kvm_host.h |   19 ++++++++
 virt/kvm/vfio.c          |  103

++++++++++++++++++++++++++++++++++++++++++++++

include/linux/kvm_host.h static inline int

Quoted 7 lines
 2 files changed, 122 insertions(+), 0 deletions(-)
include/linux/kvm_host.h +0 -0
index 5cd4420..8d06678 100644
@@ -1134,6 +1134,25 @@ static inline int

kvm_arch_vfio_set_forward(struct kvm_fwd_irq *fwd_irq,

Quoted 6 lines
include/linux/kvm_host.h +3 -0
 }
 #endif
 
+#ifdef __KVM_HAVE_ARCH_KVM_VFIO_POSTING
+/*
+ * kvm_arch_vfio_update_pi_irte - set IRTE for Posted-Interrupts

include/linux/kvm_host.h static inline int kvm_arch_vfio_set_forward(struct kvm_fwd_irq *fwd_irq,

Quoted 10 lines
include/linux/kvm_host.h +10 -0
+ *
+ * @kvm: kvm
+ * @host_irq: host irq of the interrupt
+ * @guest_irq: gsi of the interrupt
+ * returns 0 on success, < 0 on failure
+ */
+int kvm_arch_vfio_update_pi_irte(struct kvm *kvm, unsigned int host_irq,
+				 uint32_t guest_irq);
+#else
+static int kvm_arch_vfio_update_pi_irte(struct kvm *kvm, unsigned int

host_irq,

include/linux/kvm_host.h static inline int kvm_arch_vfio_set_forward(struct kvm_fwd_irq *fwd_irq,

Quoted 9 lines
include/linux/kvm_host.h +6 -0
+					uint32_t guest_irq)
+{
+	return 0;
+}
+#endif
+
 #ifdef CONFIG_HAVE_KVM_CPU_RELAX_INTERCEPT
 
 static inline void kvm_vcpu_set_in_spin_loop(struct kvm_vcpu *vcpu, bool

val)

Quoted 21 lines
virt/kvm/vfio.c +13 -0
index 6bc7001..5e5515f 100644
@@ -446,6 +446,99 @@ out:
 	return ret;
 }
 
+static int kvm_vfio_pci_get_irq_count(struct pci_dev *pdev, int irq_type)
+{
+	if (irq_type == VFIO_PCI_INTX_IRQ_INDEX) {
+		u8 pin;
+
+		pci_read_config_byte(pdev, PCI_INTERRUPT_PIN, &pin);
+		if (pin)
+			return 1;
+	} else if (irq_type == VFIO_PCI_MSI_IRQ_INDEX)
+		return pci_msi_vec_count(pdev);
+	else if (irq_type == VFIO_PCI_MSIX_IRQ_INDEX)
+		return pci_msix_vec_count(pdev);
+
Quoted 8 lines

virt/kvm/vfio.c out: · line 463

Quoted 2 lines
virt/kvm/vfio.c +2 -0
+	return 0;
+}

for platform case I was asked to move the retrieval of absolute irq number to the architecture specific part. I don't know if it should apply to PCI stuff as well? This explains why I need to pass the VFIO device (or struct device handle) to the arch specific part. Actually we do the same job, we provide a phys/virt IRQ mapping to KVM, right? So to me our architecture specific API should look quite similar?

In my patch, QEMU passes IRQ type(MSI/MSIx in my case), VFIO device index, and sub-index via "struct kvm_vfio_dev_irq" to KVM, then KVM will find the real host irq from the VFIO device index and the IRQ type. Is this something similar with your patch?

Quoted 43 lines
Quoted 9 lines
virt/kvm/vfio.c +8 -0
+
+static int kvm_vfio_set_pi(struct kvm_device *kdev, int32_t __user *argp)
+{
+	struct kvm_vfio_dev_irq pi_info;
+	uint32_t *gsi;
+	unsigned long minsz;
+	struct vfio_device *vdev;
+	struct msi_desc *entry;

virt/kvm/vfio.c out: · line 481

Quoted 10 lines
virt/kvm/vfio.c +10 -0
+	struct device *dev;
+	struct pci_dev *pdev;
+	int i, max, ret;
+
+	minsz = offsetofend(struct kvm_vfio_dev_irq, count);
+
+	if (copy_from_user(&pi_info, (void __user *)argp, minsz))
+		return -EFAULT;
+
+	if (pi_info.argsz < minsz || pi_info.index >= VFIO_PCI_NUM_IRQS)

PCI specific check, same remark as above but I will let Alex further comment on this and possibly invalidate this commeny ;-)

Quoted 11 lines
virt/kvm/vfio.c +11 -0
+		return -EINVAL;
+
+	vdev = kvm_vfio_get_vfio_device(pi_info.fd);
+	if (IS_ERR(vdev))
+		return PTR_ERR(vdev);
+
+	dev = kvm_vfio_external_base_device(vdev);
+	if (!dev || !dev_is_pci(dev)) {
+		ret = -EFAULT;
+		goto put_vfio_device;
+	}

virt/kvm/vfio.c out: · line 502

Quoted 10 lines
virt/kvm/vfio.c +10 -0
+
+	pdev = to_pci_dev(dev);
+
+	max = kvm_vfio_pci_get_irq_count(pdev, pi_info.index);
+	if (max <= 0) {
+		ret = -EFAULT;
+		goto put_vfio_device;
+	}
+
+	if (pi_info.argsz - minsz < pi_info.count * sizeof(int) ||

shouldn' we use the actual datatype?

Quoted 3 lines
Quoted 1 line

I am afraid I don't get this, could you please be more specific? Thanks a lot!

We could have a platform that supports 64bit INTs.

yes this is what I meant (struct datatype is __u32).

Thanks

Eric

Quoted 28 lines
Quoted 28 lines
Quoted 18 lines

virt/kvm/vfio.c out: · line 509

Quoted 8 lines
virt/kvm/vfio.c +7 -0
+	    pi_info.start >= max || pi_info.start + pi_info.count > max) {
+		ret = -EINVAL;
+		goto put_vfio_device;
+	}
+
+	gsi = memdup_user((void __user *)((unsigned long)argp + minsz),
+			   pi_info.count * sizeof(int));

same question as above

Quoted 9 lines
virt/kvm/vfio.c +9 -0
+	if (IS_ERR(gsi)) {
+		ret = PTR_ERR(gsi);
+		goto put_vfio_device;
+	}
+
+#ifdef CONFIG_PCI_MSI
+	for (i = 0; i < pi_info.count; i++) {
+		list_for_each_entry(entry, &pdev->msi_list, list) {
+			if (entry->msi_attrib.entry_nr != pi_info.start+i)
Quoted 8 lines

virt/kvm/vfio.c out: · line 525

Quoted 7 lines
virt/kvm/vfio.c +7 -0
+				continue;
+
+			ret = kvm_arch_vfio_update_pi_irte(kdev->kvm,
+							   entry->irq,
+							   gsi[i]);
+			if (ret) {
+				ret = -EFAULT;

why -EFAULT? and not propagation of original error code?

Yes, you are right. Thanks for the comments!

Quoted 7 lines
Quoted 7 lines
Quoted 2 lines

you may have posting set for part of the subindexes and unset for rest. Isn't it an issue?

QEMU will always set the posting for all the sub-indexes for MSI/MSIx, once the guest updates the configuration of some sub-indexes, KVM will update it accordingly. So in which case will what you mentioned above happen?

Was pointing out you handle the case where kvm_arch_vfio_update_pi_irte could fail and you still continue looping thru the other indexes. So theoretically you could have a mixed of non posted IRQs and posted IRQs.

Best Regards

Eric

Quoted 58 lines

QEMU is just one userspace, not necessarily the only userspace. The kernel shouldn't expect a specific userspace behavior.

Quoted 51 lines
Quoted 34 lines

virt/kvm/vfio.c static int kvm_vfio_set_device(struct kvm_device

Quoted 20 lines
virt/kvm/vfio.c +16 -0
+				goto free_gsi;
+			}
+		}
+	}
+#endif
+
+	ret = 0;
+
+free_gsi:
+	kfree(gsi);
+
+put_vfio_device:
+	kvm_vfio_put_vfio_device(vdev);
+	return ret;
+}
+
 static int kvm_vfio_set_device(struct kvm_device *kdev, long attr, u64 arg)
 {
 	int32_t __user *argp = (int32_t __user *)(unsigned long)arg;
@@ -456,6 +549,11 @@ static int kvm_vfio_set_device(struct kvm_device

*kdev, long attr, u64 arg)

virt/kvm/vfio.c static int kvm_vfio_has_attr(struct kvm_device

Quoted 12 lines
virt/kvm/vfio.c +5 -0
 	case KVM_DEV_VFIO_DEVICE_UNFORWARD_IRQ:
 		ret = kvm_vfio_control_irq_forward(kdev, attr, argp);
 		break;
+#ifdef __KVM_HAVE_ARCH_KVM_VFIO_POSTING
+	case KVM_DEV_VFIO_DEVICE_POSTING_IRQ:
+		ret = kvm_vfio_set_pi(kdev, argp);
+		break;
+#endif
 	default:
 		ret = -ENXIO;
 	}
@@ -511,6 +609,11 @@ static int kvm_vfio_has_attr(struct kvm_device

*dev,

Quoted 12 lines

virt/kvm/vfio.c static int kvm_vfio_has_attr(struct kvm_device *dev, · line 619

Quoted 12 lines
virt/kvm/vfio.c +5 -0
 		case KVM_DEV_VFIO_DEVICE_UNFORWARD_IRQ:
 			return 0;
 #endif
+#ifdef __KVM_HAVE_ARCH_KVM_VFIO_POSTING
+		case KVM_DEV_VFIO_DEVICE_POSTING_IRQ:
+			return 0;
+#endif
+
 		}
 		break;
 	}

--
To unsubscribe from this list: send the line "unsubscribe kvm" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html

author
#29 lore raw

RE: [v2 18/25] KVM: kvm-vfio: implement the VFIO skeleton for VT-d Posted-Interrupts

Quoted 162 lines

-----Original Message-----
From: Eric Auger [mailto:[email protected]]
Sent: Monday, December 08, 2014 6:16 PM
To: Alex Williamson; Wu, Feng
Cc: [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; jiang.liu-VuQAYsv1563Yd54FQh9/[email protected]; [email protected]; iommu-cunTk1MwBs9QetFLy7KEm3xJsTq8ys+cHZ5vskTnxNA@public.gmane.org; [email protected]
Subject: Re: [v2 18/25] KVM: kvm-vfio: implement the VFIO skeleton for VT-d
Posted-Interrupts

Quoted 12 lines · Alex Williamson
Quoted 11 lines · Feng
Quoted 11 lines

-----Original Message-----
From: Eric Auger [mailto:[email protected]]
Sent: Thursday, December 04, 2014 11:36 PM
To: Wu, Feng; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; jiang.liu-VuQAYsv1563Yd54FQh9/[email protected]
Cc: [email protected]; iommu-cunTk1MwBs9QetFLy7KEm3xJsTq8ys+cHZ5vskTnxNA@public.gmane.org; [email protected]
Subject: Re: [v2 18/25] KVM: kvm-vfio: implement the VFIO skeleton for

VT-d

Quoted 28 lines
Quoted 28 lines
Quoted 28 lines

Posted-Interrupts

Hi Feng,

Quoted 2 lines · Feng Wu

This patch adds the kvm-vfio interface for VT-d Posted-Interrrupts. When guests updates MSI/MSI-x information for an assigned-device,

update

Quoted 2 lines

QEMU will use KVM_DEV_VFIO_DEVICE_POSTING_IRQ attribute to setup IRTE for VT-d PI. This patch implement this IRQ attribute.

s/implement/implements

Quoted 5 lines
---
 include/linux/kvm_host.h |   19 ++++++++
 virt/kvm/vfio.c          |  103

++++++++++++++++++++++++++++++++++++++++++++++

include/linux/kvm_host.h static inline int

Quoted 7 lines
 2 files changed, 122 insertions(+), 0 deletions(-)
include/linux/kvm_host.h +0 -0
index 5cd4420..8d06678 100644
@@ -1134,6 +1134,25 @@ static inline int

kvm_arch_vfio_set_forward(struct kvm_fwd_irq *fwd_irq,

Quoted 3 lines
include/linux/kvm_host.h +0 -0
 }
 #endif
 
Quoted 10 lines
Quoted 10 lines
Quoted 10 lines

include/linux/kvm_host.h static inline int kvm_arch_vfio_set_forward(struct kvm_fwd_irq *fwd_irq,

Quoted 10 lines
include/linux/kvm_host.h +10 -0
+#ifdef __KVM_HAVE_ARCH_KVM_VFIO_POSTING
+/*
+ * kvm_arch_vfio_update_pi_irte - set IRTE for Posted-Interrupts
+ *
+ * @kvm: kvm
+ * @host_irq: host irq of the interrupt
+ * @guest_irq: gsi of the interrupt
+ * returns 0 on success, < 0 on failure
+ */
+int kvm_arch_vfio_update_pi_irte(struct kvm *kvm, unsigned int

host_irq,

Quoted 12 lines
Quoted 12 lines
Quoted 12 lines

include/linux/kvm_host.h static inline int kvm_arch_vfio_set_forward(struct kvm_fwd_irq *fwd_irq,

Quoted 3 lines
include/linux/kvm_host.h +3 -0
+				 uint32_t guest_irq);
+#else
+static int kvm_arch_vfio_update_pi_irte(struct kvm *kvm, unsigned int

host_irq,

Quoted 8 lines
include/linux/kvm_host.h +6 -0
+					uint32_t guest_irq)
+{
+	return 0;
+}
+#endif
+
 #ifdef CONFIG_HAVE_KVM_CPU_RELAX_INTERCEPT
 
Quoted 1 line
Quoted 1 line
Quoted 1 line

include/linux/kvm_host.h static inline int · line 1158

Quoted 1 line
include/linux/kvm_host.h +0 -0
 static inline void kvm_vcpu_set_in_spin_loop(struct kvm_vcpu *vcpu,

bool

Quoted 31 lines
Quoted 31 lines
Quoted 30 lines

val)

Quoted 13 lines
virt/kvm/vfio.c +5 -0
index 6bc7001..5e5515f 100644
@@ -446,6 +446,99 @@ out:
 	return ret;
 }
 
+static int kvm_vfio_pci_get_irq_count(struct pci_dev *pdev, int irq_type)
+{
+	if (irq_type == VFIO_PCI_INTX_IRQ_INDEX) {
+		u8 pin;
+

virt/kvm/vfio.c out: · line 463

Quoted 10 lines
virt/kvm/vfio.c +10 -0
+		pci_read_config_byte(pdev, PCI_INTERRUPT_PIN, &pin);
+		if (pin)
+			return 1;
+	} else if (irq_type == VFIO_PCI_MSI_IRQ_INDEX)
+		return pci_msi_vec_count(pdev);
+	else if (irq_type == VFIO_PCI_MSIX_IRQ_INDEX)
+		return pci_msix_vec_count(pdev);
+
+	return 0;
+}

for platform case I was asked to move the retrieval of absolute irq number to the architecture specific part. I don't know if it should apply to PCI stuff as well? This explains why I need to pass the VFIO device (or struct device handle) to the arch specific part. Actually we do the same job, we provide a phys/virt IRQ mapping to KVM, right? So to me our architecture specific API should look quite similar?

Quoted 1 line
Quoted 1 line

In my patch, QEMU passes IRQ type(MSI/MSIx in my case), VFIO device

index,

Quoted 7 lines
Quoted 7 lines

and sub-index via "struct kvm_vfio_dev_irq" to KVM, then KVM will find the real host irq from the VFIO device index and the IRQ type. Is this something similar with your patch?

Quoted 3 lines

virt/kvm/vfio.c out: · line 465

Quoted 3 lines
virt/kvm/vfio.c +2 -0
+
+static int kvm_vfio_set_pi(struct kvm_device *kdev, int32_t __user

*argp)

Quoted 41 lines
Quoted 41 lines
Quoted 40 lines
Quoted 6 lines
virt/kvm/vfio.c +6 -0
+{
+	struct kvm_vfio_dev_irq pi_info;
+	uint32_t *gsi;
+	unsigned long minsz;
+	struct vfio_device *vdev;
+	struct msi_desc *entry;

virt/kvm/vfio.c out: · line 481

Quoted 10 lines
virt/kvm/vfio.c +10 -0
+	struct device *dev;
+	struct pci_dev *pdev;
+	int i, max, ret;
+
+	minsz = offsetofend(struct kvm_vfio_dev_irq, count);
+
+	if (copy_from_user(&pi_info, (void __user *)argp, minsz))
+		return -EFAULT;
+
+	if (pi_info.argsz < minsz || pi_info.index >= VFIO_PCI_NUM_IRQS)

PCI specific check, same remark as above but I will let Alex further comment on this and possibly invalidate this commeny ;-)

Quoted 11 lines
virt/kvm/vfio.c +11 -0
+		return -EINVAL;
+
+	vdev = kvm_vfio_get_vfio_device(pi_info.fd);
+	if (IS_ERR(vdev))
+		return PTR_ERR(vdev);
+
+	dev = kvm_vfio_external_base_device(vdev);
+	if (!dev || !dev_is_pci(dev)) {
+		ret = -EFAULT;
+		goto put_vfio_device;
+	}

virt/kvm/vfio.c out: · line 502

Quoted 10 lines
virt/kvm/vfio.c +10 -0
+
+	pdev = to_pci_dev(dev);
+
+	max = kvm_vfio_pci_get_irq_count(pdev, pi_info.index);
+	if (max <= 0) {
+		ret = -EFAULT;
+		goto put_vfio_device;
+	}
+
+	if (pi_info.argsz - minsz < pi_info.count * sizeof(int) ||

shouldn' we use the actual datatype?

Quoted 8 lines
Quoted 3 lines
Quoted 1 line

I am afraid I don't get this, could you please be more specific? Thanks a lot!

We could have a platform that supports 64bit INTs.

yes this is what I meant (struct datatype is __u32).

Thanks

Eric

Oh, I got it. In fact, I changed the type of gsi[] from "int" to "u32" in struct kvm_vfio_dev_irq, but I forgot to change this place. I will correct it. Thanks for the comments!

Quoted 31 lines
Quoted 31 lines
Quoted 31 lines
Quoted 18 lines

virt/kvm/vfio.c out: · line 509

Quoted 8 lines
virt/kvm/vfio.c +7 -0
+	    pi_info.start >= max || pi_info.start + pi_info.count > max) {
+		ret = -EINVAL;
+		goto put_vfio_device;
+	}
+
+	gsi = memdup_user((void __user *)((unsigned long)argp + minsz),
+			   pi_info.count * sizeof(int));

same question as above

Quoted 9 lines
virt/kvm/vfio.c +9 -0
+	if (IS_ERR(gsi)) {
+		ret = PTR_ERR(gsi);
+		goto put_vfio_device;
+	}
+
+#ifdef CONFIG_PCI_MSI
+	for (i = 0; i < pi_info.count; i++) {
+		list_for_each_entry(entry, &pdev->msi_list, list) {
+			if (entry->msi_attrib.entry_nr != pi_info.start+i)
Quoted 8 lines

virt/kvm/vfio.c out: · line 525

Quoted 7 lines
virt/kvm/vfio.c +7 -0
+				continue;
+
+			ret = kvm_arch_vfio_update_pi_irte(kdev->kvm,
+							   entry->irq,
+							   gsi[i]);
+			if (ret) {
+				ret = -EFAULT;

why -EFAULT? and not propagation of original error code?

Yes, you are right. Thanks for the comments!

Quoted 2 lines

you may have posting set for part of the subindexes and unset for rest. Isn't it an issue?

Quoted 9 lines
Quoted 4 lines
Quoted 4 lines

QEMU will always set the posting for all the sub-indexes for MSI/MSIx, once the guest updates the configuration of some sub-indexes, KVM will update it accordingly. So in which case will what you mentioned above happen?

Was pointing out you handle the case where kvm_arch_vfio_update_pi_irte could fail and you still continue looping thru the other indexes. So theoretically you could have a mixed of non posted IRQs and posted IRQs.

Okay, I got your point. In fact, remapped IRQs and Posted IRQs are independent. We can make some of the IRQ posted and the rest remapped. They are using different IRTEs in the remapped structure.

Thanks,
Feng

Quoted 63 lines

Best Regards

Eric

Quoted 13 lines

QEMU is just one userspace, not necessarily the only userspace. The kernel shouldn't expect a specific userspace behavior.

Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
virt/kvm/vfio.c +9 -0
+				goto free_gsi;
+			}
+		}
+	}
+#endif
+
+	ret = 0;
+
+free_gsi:
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

virt/kvm/vfio.c out:

Quoted 8 lines
virt/kvm/vfio.c +7 -0
+	kfree(gsi);
+
+put_vfio_device:
+	kvm_vfio_put_vfio_device(vdev);
+	return ret;
+}
+
 static int kvm_vfio_set_device(struct kvm_device *kdev, long attr, u64

arg)

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

virt/kvm/vfio.c static int kvm_vfio_set_device(struct

Quoted 3 lines
virt/kvm/vfio.c +0 -0
 {
 	int32_t __user *argp = (int32_t __user *)(unsigned long)arg;
@@ -456,6 +549,11 @@ static int kvm_vfio_set_device(struct

kvm_device

Quoted 34 lines
Quoted 31 lines
Quoted 14 lines

*kdev, long attr, u64 arg)

virt/kvm/vfio.c static int kvm_vfio_has_attr(struct kvm_device

Quoted 12 lines
virt/kvm/vfio.c +5 -0
 	case KVM_DEV_VFIO_DEVICE_UNFORWARD_IRQ:
 		ret = kvm_vfio_control_irq_forward(kdev, attr, argp);
 		break;
+#ifdef __KVM_HAVE_ARCH_KVM_VFIO_POSTING
+	case KVM_DEV_VFIO_DEVICE_POSTING_IRQ:
+		ret = kvm_vfio_set_pi(kdev, argp);
+		break;
+#endif
 	default:
 		ret = -ENXIO;
 	}
@@ -511,6 +609,11 @@ static int kvm_vfio_has_attr(struct kvm_device

*dev,

Quoted 12 lines

virt/kvm/vfio.c static int kvm_vfio_has_attr(struct kvm_device *dev, · line 619

Quoted 12 lines
virt/kvm/vfio.c +5 -0
 		case KVM_DEV_VFIO_DEVICE_UNFORWARD_IRQ:
 			return 0;
 #endif
+#ifdef __KVM_HAVE_ARCH_KVM_VFIO_POSTING
+		case KVM_DEV_VFIO_DEVICE_POSTING_IRQ:
+			return 0;
+#endif
+
 		}
 		break;
 	}

--
To unsubscribe from this list: send the line "unsubscribe kvm" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html

author
#30 lore raw

PATCH v2 19/25 KVM: x86: kvm-vfio: VT-d posted-interrupts setup

In reply to Feng Wu

This patch defines macro __KVM_HAVE_ARCH_KVM_VFIO_POSTING and implement kvm_arch_vfio_update_pi_irte for x86 architecture.

---
 arch/x86/include/asm/kvm_host.h |    2 +
 arch/x86/kvm/Makefile           |    2 +-
 arch/x86/kvm/kvm_vfio_x86.c     |   68 +++++++++++++++++++++++++++++++++++++++
 3 files changed, 71 insertions(+), 1 deletions(-)
 create mode 100644 arch/x86/kvm/kvm_vfio_x86.c
arch/x86/include/asm/kvm_host.h +2 -0
index 9b45b78..6878429 100644
@@ -82,6 +82,8 @@ static inline gfn_t gfn_to_index(gfn_t gfn, gfn_t base_gfn, int level)
 		(base_gfn >> KVM_HPAGE_GFN_SHIFT(level));
 }
 
+#define __KVM_HAVE_ARCH_KVM_VFIO_POSTING
+
 #define SELECTOR_TI_MASK (1 << 2)
 #define SELECTOR_RPL_MASK 0x03
 
arch/x86/kvm/Makefile +1 -1
index 25d22b2..8809d58 100644
@@ -14,7 +14,7 @@ kvm-$(CONFIG_KVM_DEVICE_ASSIGNMENT)	+= $(KVM)/assigned-dev.o $(KVM)/iommu.o
 kvm-$(CONFIG_KVM_ASYNC_PF)	+= $(KVM)/async_pf.o
 
 kvm-y			+= x86.o mmu.o emulate.o i8259.o irq.o lapic.o \
-			   i8254.o cpuid.o pmu.o
+			   i8254.o cpuid.o pmu.o kvm_vfio_x86.o
 kvm-intel-y		+= vmx.o
 kvm-amd-y		+= svm.o
 
arch/x86/kvm/kvm_vfio_x86.c new file +68 -0

68 lines not on this page · raw message

Signature · 1 line
1.7.1
author
#31 lore raw

PATCH v2 20/25 x86, irq: Define a global vector for VT-d Posted-Interrupts

In reply to Feng Wu

Currently, we use a global vector as the Posted-Interrupts Notification Event for all the vCPUs in the system. We need to introduce another global vector for VT-d Posted-Interrtups, which will be used to wakeup the sleep vCPU when an external interrupt from a direct-assigned device happens for that vCPU.

---
 arch/x86/include/asm/entry_arch.h  |    2 ++
 arch/x86/include/asm/hardirq.h     |    1 +
 arch/x86/include/asm/hw_irq.h      |    2 ++
 arch/x86/include/asm/irq_vectors.h |    1 +
 arch/x86/kernel/entry_64.S         |    2 ++
 arch/x86/kernel/irq.c              |   27 +++++++++++++++++++++++++++
 arch/x86/kernel/irqinit.c          |    2 ++
 7 files changed, 37 insertions(+), 0 deletions(-)
arch/x86/include/asm/entry_arch.h +2 -0
index dc5fa66..27ca0af 100644
@@ -23,6 +23,8 @@ BUILD_INTERRUPT(x86_platform_ipi, X86_PLATFORM_IPI_VECTOR)
 #ifdef CONFIG_HAVE_KVM
 BUILD_INTERRUPT3(kvm_posted_intr_ipi, POSTED_INTR_VECTOR,
 		 smp_kvm_posted_intr_ipi)
+BUILD_INTERRUPT3(kvm_posted_intr_wakeup_ipi, POSTED_INTR_WAKEUP_VECTOR,
+		 smp_kvm_posted_intr_wakeup_ipi)
 #endif
 
 /*
arch/x86/include/asm/hardirq.h +1 -0
index 0f5fb6b..9866065 100644
@@ -14,6 +14,7 @@ typedef struct {
 #endif
 #ifdef CONFIG_HAVE_KVM
 	unsigned int kvm_posted_intr_ipis;
+	unsigned int kvm_posted_intr_wakeup_ipis;
 #endif
 	unsigned int x86_platform_ipis;	/* arch dependent */
 	unsigned int apic_perf_irqs;
arch/x86/include/asm/hw_irq.h +2 -0
index e7ae6eb..38fac9b 100644
@@ -29,6 +29,7 @@
 extern asmlinkage void apic_timer_interrupt(void);
 extern asmlinkage void x86_platform_ipi(void);
 extern asmlinkage void kvm_posted_intr_ipi(void);
+extern asmlinkage void kvm_posted_intr_wakeup_ipi(void);
 extern asmlinkage void error_interrupt(void);
 extern asmlinkage void irq_work_interrupt(void);
 
@@ -92,6 +93,7 @@ extern void trace_call_function_single_interrupt(void);
 #define trace_irq_move_cleanup_interrupt  irq_move_cleanup_interrupt
 #define trace_reboot_interrupt  reboot_interrupt
 #define trace_kvm_posted_intr_ipi kvm_posted_intr_ipi
+#define trace_kvm_posted_intr_wakeup_ipi kvm_posted_intr_wakeup_ipi
 #endif /* CONFIG_TRACING */
 
 struct irq_domain;
arch/x86/include/asm/irq_vectors.h +1 -0
index b26cb12..dca94f2 100644
@@ -105,6 +105,7 @@
 /* Vector for KVM to deliver posted interrupt IPI */
 #ifdef CONFIG_HAVE_KVM
 #define POSTED_INTR_VECTOR		0xf2
+#define POSTED_INTR_WAKEUP_VECTOR	0xf1
 #endif
 
 /*
arch/x86/kernel/entry_64.S +2 -0
index e61c14a..a598447 100644
@@ -960,6 +960,8 @@ apicinterrupt X86_PLATFORM_IPI_VECTOR \
 #ifdef CONFIG_HAVE_KVM
 apicinterrupt3 POSTED_INTR_VECTOR \
 	kvm_posted_intr_ipi smp_kvm_posted_intr_ipi
+apicinterrupt3 POSTED_INTR_WAKEUP_VECTOR \
+	kvm_posted_intr_wakeup_ipi smp_kvm_posted_intr_wakeup_ipi
 #endif
 
 #ifdef CONFIG_X86_MCE_THRESHOLD
arch/x86/kernel/irq.c +27 -0
index 922d285..47408c3 100644
@@ -237,6 +237,9 @@ __visible void smp_x86_platform_ipi(struct pt_regs *regs)
 }
 
 #ifdef CONFIG_HAVE_KVM
+void (*wakeup_handler_callback)(void) = NULL;
+EXPORT_SYMBOL_GPL(wakeup_handler_callback);
+
 /*
  * Handler for POSTED_INTERRUPT_VECTOR.
  */
@@ -256,6 +259,30 @@ __visible void smp_kvm_posted_intr_ipi(struct pt_regs *regs)
 
 	set_irq_regs(old_regs);
 }
+
+/*
+ * Handler for POSTED_INTERRUPT_WAKEUP_VECTOR.
+ */
+__visible void smp_kvm_posted_intr_wakeup_ipi(struct pt_regs *regs)
+{
+	struct pt_regs *old_regs = set_irq_regs(regs);
+
+	ack_APIC_irq();
+
+	irq_enter();
+
+	exit_idle();
+
+	inc_irq_stat(kvm_posted_intr_wakeup_ipis);
+
+	if (wakeup_handler_callback)
+		wakeup_handler_callback();
+
+	irq_exit();
+
+	set_irq_regs(old_regs);
+}
+
 #endif
 
 __visible void smp_trace_x86_platform_ipi(struct pt_regs *regs)
arch/x86/kernel/irqinit.c +2 -0
index 70e181e..844673c 100644
@@ -144,6 +144,8 @@ static void __init apic_intr_init(void)
 #ifdef CONFIG_HAVE_KVM
 	/* IPI for KVM to deliver posted interrupt */
 	alloc_intr_gate(POSTED_INTR_VECTOR, kvm_posted_intr_ipi);
+	/* IPI for KVM to deliver interrupt to wake up tasks */
+	alloc_intr_gate(POSTED_INTR_WAKEUP_VECTOR, kvm_posted_intr_wakeup_ipi);
 #endif
 
 	/* IPI vectors for APIC spurious and error interrupts */
Signature · 1 line
1.7.1
author
#32 lore raw

PATCH v2 21/25 KVM: Update Posted-Interrupts descriptor during vCPU scheduling

In reply to Feng Wu

Update Posted-Interrupts descriptor according to the following rules:
- Before vCPU block, set 'NV' to POSTED_INTR_WAKEUP_VECTOR
- After vCPU block, set 'NV' back to POSTED_INTR_VECTOR

---
 arch/x86/include/asm/kvm_host.h |    5 ++
 arch/x86/kvm/vmx.c              |   83 +++++++++++++++++++++++++++++++++++++++
 arch/x86/kvm/x86.c              |   16 +++++++
 virt/kvm/kvm_main.c             |   11 +++++
 4 files changed, 115 insertions(+), 0 deletions(-)
arch/x86/include/asm/kvm_host.h +5 -0
index 6878429..2fd85a5 100644
@@ -775,6 +775,8 @@ struct kvm_x86_ops {
 
 	void (*sched_in)(struct kvm_vcpu *kvm, int cpu);
 	u64 (*get_pi_desc_addr)(struct kvm_vcpu *vcpu);
+	int (*vcpu_pre_block)(struct kvm_vcpu *vcpu);
+	void (*vcpu_post_block)(struct kvm_vcpu *vcpu);
 };
 
 struct kvm_arch_async_pf {
@@ -1100,4 +1102,7 @@ void kvm_deliver_pmi(struct kvm_vcpu *vcpu);
 bool kvm_find_dest_vcpu(struct kvm *kvm, struct kvm_lapic_irq *irq,
 			struct kvm_vcpu **dest_vcpu);
 
+int kvm_arch_vcpu_pre_block(struct kvm_vcpu *vcpu);
+void kvm_arch_vcpu_post_block(struct kvm_vcpu *vcpu);
+
 #endif /* _ASM_X86_KVM_HOST_H */
arch/x86/kvm/vmx.c +83 -0

95 lines not on this page · raw message

arch/x86/kvm/x86.c +16 -0
index 0033df3..9706984 100644
@@ -7731,6 +7731,22 @@ bool kvm_arch_has_noncoherent_dma(struct kvm *kvm)
 }
 EXPORT_SYMBOL_GPL(kvm_arch_has_noncoherent_dma);
 
+int kvm_arch_vcpu_pre_block(struct kvm_vcpu *vcpu)
+{
+	if (kvm_x86_ops->vcpu_pre_block)
+		return kvm_x86_ops->vcpu_pre_block(vcpu);
+
+	return 0;
+}
+EXPORT_SYMBOL_GPL(kvm_arch_vcpu_pre_block);
+
+void kvm_arch_vcpu_post_block(struct kvm_vcpu *vcpu)
+{
+	if (kvm_x86_ops->vcpu_post_block)
+		kvm_x86_ops->vcpu_post_block(vcpu);
+}
+EXPORT_SYMBOL_GPL(kvm_arch_vcpu_post_block);
+
 EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_exit);
 EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_inj_virq);
 EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_page_fault);
virt/kvm/kvm_main.c +11 -0
index 25ffac9..1be1a45 100644
@@ -1754,7 +1754,18 @@ void kvm_vcpu_block(struct kvm_vcpu *vcpu)
 		if (signal_pending(current))
 			break;
 
+#ifdef CONFIG_X86
+		if (kvm_arch_vcpu_pre_block(vcpu) == 1) {
+			kvm_make_request(KVM_REQ_UNHALT, vcpu);
+			break;
+		}
+#endif
+
 		schedule();
+
+#ifdef CONFIG_X86
+		kvm_arch_vcpu_post_block(vcpu);
+#endif
 	}
 
 	finish_wait(&vcpu->wq, &wait);
Signature · 1 line
1.7.1
author
#33 lore raw

PATCH v2 22/25 KVM: Change NDST field after vCPU scheduling

In reply to Feng Wu

This patch changes the NDST filed of Posted-Interrupts Descriptor after vCPU is scheduled to another physical CPU.

---
 arch/x86/kvm/vmx.c |   25 +++++++++++++++++++++++++
 1 files changed, 25 insertions(+), 0 deletions(-)
arch/x86/kvm/vmx.c +25 -0
index a1966b9..e71bf3b 100644
@@ -1906,6 +1906,31 @@ static void vmx_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
 		vmcs_writel(HOST_IA32_SYSENTER_ESP, sysenter_esp); /* 22.2.3 */
 		vmx->loaded_vmcs->cpu = cpu;
 	}
+
+	if (irq_remapping_cap(IRQ_POSTING_CAP) && (vcpu->cpu != cpu)) {
+		struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
+		struct pi_desc old, new;
+		unsigned int dest;
+
+		memset(&old, 0, sizeof(old));
+		memset(&new, 0, sizeof(new));
+
+		pi_set_sn(pi_desc);
+
+		do {
+			old.control = new.control = pi_desc->control;
+
+			dest = cpu_physical_id(cpu);
+
+			if (x2apic_enabled())
+				new.ndst = dest;
+			else
+				new.ndst = (dest << 8) & 0xFF00;
+
+		} while (cmpxchg(&pi_desc->control, old.control,
+				new.control) != old.control);
+		pi_clear_sn(pi_desc);
+	}
 }
 
 static void vmx_vcpu_put(struct kvm_vcpu *vcpu)
Signature · 1 line
1.7.1
author
#34 lore raw

PATCH v2 23/25 KVM: Add the handler for Wake-up Vector

In reply to Feng Wu

When vCPU is blocked and an external interrupts from assigned devices is delivered to it, VT-d Posted-Interrupts mechanism will deliver an interrupt to the associated physical CPU with Wake-up Vector. In its handler, we find the destination vCPU and wake up it.

---
 arch/x86/include/asm/kvm_host.h |    2 +
 arch/x86/kvm/vmx.c              |   52 +++++++++++++++++++++++++++++++++++++++
 arch/x86/kvm/x86.c              |   22 +++++++++++-----
 include/linux/kvm_host.h        |    3 ++
 virt/kvm/kvm_main.c             |    3 ++
 5 files changed, 75 insertions(+), 7 deletions(-)
arch/x86/include/asm/kvm_host.h +2 -0
index 2fd85a5..76fc32d 100644
@@ -101,6 +101,8 @@ static inline gfn_t gfn_to_index(gfn_t gfn, gfn_t base_gfn, int level)
 
 #define ASYNC_PF_PER_VCPU 64
 
+extern void (*wakeup_handler_callback)(void);
+
 enum kvm_reg {
 	VCPU_REGS_RAX = 0,
 	VCPU_REGS_RCX = 1,
arch/x86/kvm/vmx.c +52 -0

103 lines not on this page · raw message

arch/x86/kvm/x86.c +15 -7
index 9706984..37dd307 100644
@@ -6152,6 +6152,21 @@ static int vcpu_enter_guest(struct kvm_vcpu *vcpu)
 			kvm_vcpu_reload_apic_access_page(vcpu);
 	}
 
+	/*
+	 * Since posted-interrupts can be set by VT-d HW now, in this
+	 * case, KVM_REQ_EVENT is not set. We move the following
+	 * operations out of the if statement.
+	 */
+	if (kvm_lapic_enabled(vcpu)) {
+		/*
+		 * Update architecture specific hints for APIC
+		 * virtual interrupt delivery.
+		 */
+		if (kvm_x86_ops->hwapic_irr_update)
+			kvm_x86_ops->hwapic_irr_update(vcpu,
+				kvm_lapic_find_highest_irr(vcpu));
+	}
+
 	if (kvm_check_request(KVM_REQ_EVENT, vcpu) || req_int_win) {
 		kvm_apic_accept_events(vcpu);
 		if (vcpu->arch.mp_state == KVM_MP_STATE_INIT_RECEIVED) {
@@ -6168,13 +6183,6 @@ static int vcpu_enter_guest(struct kvm_vcpu *vcpu)
 			kvm_x86_ops->enable_irq_window(vcpu);
 
 		if (kvm_lapic_enabled(vcpu)) {
-			/*
-			 * Update architecture specific hints for APIC
-			 * virtual interrupt delivery.
-			 */
-			if (kvm_x86_ops->hwapic_irr_update)
-				kvm_x86_ops->hwapic_irr_update(vcpu,
-					kvm_lapic_find_highest_irr(vcpu));
 			update_cr8_intercept(vcpu);
 			kvm_lapic_sync_to_vapic(vcpu);
 		}
include/linux/kvm_host.h +3 -0
index 8d06678..a1cb764 100644
@@ -239,6 +239,9 @@ struct kvm_vcpu {
 	unsigned long requests;
 	unsigned long guest_debug;
 
+	int wakeup_cpu;
+	struct list_head blocked_vcpu_list;
+
 	struct mutex mutex;
 	struct kvm_run *run;
 
virt/kvm/kvm_main.c +3 -0
index 1be1a45..fb3e504 100644
@@ -224,6 +224,9 @@ int kvm_vcpu_init(struct kvm_vcpu *vcpu, struct kvm *kvm, unsigned id)
 	init_waitqueue_head(&vcpu->wq);
 	kvm_async_pf_vcpu_init(vcpu);
 
+	vcpu->wakeup_cpu = -1;
+	INIT_LIST_HEAD(&vcpu->blocked_vcpu_list);
+
 	page = alloc_page(GFP_KERNEL | __GFP_ZERO);
 	if (!page) {
 		r = -ENOMEM;
Signature · 1 line
1.7.1
author
#35 lore raw

PATCH v2 24/25 KVM: Suppress posted-interrupt when 'SN' is set

In reply to Feng Wu

Currently, we don't support urgent interrupt, all interrupts are recognized as non-urgent interrupt, so we cannot send posted-interrupt when 'SN' is set.

---
 arch/x86/kvm/vmx.c |   11 +++++++++--
 1 files changed, 9 insertions(+), 2 deletions(-)
arch/x86/kvm/vmx.c +9 -2
index dc6fd84..6b2f3e7 100644
@@ -4306,15 +4306,22 @@ static int vmx_vm_has_apicv(struct kvm *kvm)
 static void vmx_deliver_posted_interrupt(struct kvm_vcpu *vcpu, int vector)
 {
 	struct vcpu_vmx *vmx = to_vmx(vcpu);
-	int r;
+	int r, sn;
 
 	if (pi_test_and_set_pir(vector, &vmx->pi_desc))
 		return;
 
+	/*
+	 * Currently, we don't support urgent interrupt, all interrupts
+	 * are recognized as non-urgent interrupt, so we cannot send
+	 * posted-interrupt when 'SN' is set.
+	 */
+	sn = pi_test_sn(&vmx->pi_desc);
+
 	r = pi_test_and_set_on(&vmx->pi_desc);
 	kvm_make_request(KVM_REQ_EVENT, vcpu);
 #ifdef CONFIG_SMP
-	if (!r && (vcpu->mode == IN_GUEST_MODE))
+	if (!r && !sn && (vcpu->mode == IN_GUEST_MODE))
 		apic->send_IPI_mask(get_cpu_mask(vcpu->cpu),
 				POSTED_INTR_VECTOR);
 	else
Signature · 1 line
1.7.1
author
#36 lore raw

PATCH v2 25/25 iommu/vt-d: Add a command line parameter for VT-d posted-interrupts

In reply to Feng Wu

Enable VT-d Posted-Interrtups and add a command line parameter for it.

---
 Documentation/kernel-parameters.txt |    1 +
 drivers/iommu/irq_remapping.c       |   12 ++++++++----
 2 files changed, 9 insertions(+), 4 deletions(-)
Documentation/kernel-parameters.txt +1 -0
index 838f377..324b790 100644
@@ -1453,6 +1453,7 @@ bytes respectively. Such letter suffixes can also be entirely omitted.
 			nosid	disable Source ID checking
 			no_x2apic_optout
 				BIOS x2APIC opt-out request will be ignored
+			nopost	disable Interrupt Posting
 
 	iomem=		Disable strict checking of access to MMIO memory
 		strict	regions from userspace.
drivers/iommu/irq_remapping.c +8 -4
index b008663..aa3cd23 100644
@@ -24,7 +24,7 @@ int irq_remap_broken;
 int disable_sourceid_checking;
 int no_x2apic_optout;
 
-int disable_irq_post = 1;
+int disable_irq_post = 0;
 
 static struct irq_remap_ops *remap_ops;
 
@@ -59,14 +59,18 @@ static __init int setup_irqremap(char *str)
 		return -EINVAL;
 
 	while (*str) {
-		if (!strncmp(str, "on", 2))
+		if (!strncmp(str, "on", 2)) {
 			disable_irq_remap = 0;
-		else if (!strncmp(str, "off", 3))
+			disable_irq_post = 0;
+		} else if (!strncmp(str, "off", 3)) {
 			disable_irq_remap = 1;
-		else if (!strncmp(str, "nosid", 5))
+			disable_irq_post = 1;
+		} else if (!strncmp(str, "nosid", 5))
 			disable_sourceid_checking = 1;
 		else if (!strncmp(str, "no_x2apic_optout", 16))
 			no_x2apic_optout = 1;
+		else if (!strncmp(str, "nopost", 6))
+			disable_irq_post = 1;
 
 		str += strcspn(str, ",");
 		while (*str == ',')
Signature · 1 line
1.7.1
author
#37 lore raw
In reply to Feng Wu

Ping...

Thanks,
Feng

Quoted 101 lines

-----Original Message-----
From: Wu, Feng
Sent: Wednesday, December 03, 2014 3:39 PM
To: [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; jiang.liu-VuQAYsv1563Yd54FQh9/[email protected]
Cc: [email protected]; iommu-cunTk1MwBs9QetFLy7KEm3xJsTq8ys+cHZ5vskTnxNA@public.gmane.org; [email protected]; Wu, Feng
Subject: [v2 00/25] Add VT-d Posted-Interrupts support

VT-d Posted-Interrupts is an enhancement to CPU side Posted-Interrupt. With VT-d Posted-Interrupts enabled, external interrupts from direct-assigned devices can be delivered to guests without VMM intervention when guest is running in non-root mode.

You can find the VT-d Posted-Interrtups Spec. in the following URL:
http://www.intel.com/content/www/us/en/intelligent-systems/intel-technolog
y/vt-directed-io-spec.html

v1->v2:
* Use VFIO framework to enable this feature, the VFIO part of this series is base on Eric's patch "[PATCH v3 0/8] KVM-VFIO IRQ forward control"
* Rebase this patchset on
git://git.kernel.org/pub/scm/linux/kernel/git/tip/tip.git,
then revise some irq logic based on the new hierarchy irqdomain patches
provided
by Jiang Liu <jiang.liu-VuQAYsv1563Yd54FQh9/[email protected]>

This patch series is made of the following groups:
1-6: Some preparation changes in iommu and irq component, this is based on the
new hierarchy irqdomain logic.
7-9, 25: IOMMU changes for VT-d Posted-Interrupts, such as, feature detection,
command line parameter.
10-16, 21-24: Changes related to KVM itself.
17-19: Changes in VFIO component, this part was previously sent out as "[RFC PATCH v2 0/2] kvm-vfio: implement the vfio skeleton for VT-d Posted-Interrupts"
20: x86 irq related changes

Feng Wu (25):
genirq: Introduce irq_set_vcpu_affinity() to target an interrupt to a
VCPU
iommu: Add new member capability to struct irq_remap_ops iommu, x86: Define new irte structure for VT-d Posted-Interrupts iommu, x86: Implement irq_set_vcpu_affinity for intel_ir_chip x86, irq: Implement irq_set_vcpu_affinity for pci_msi_ir_controller iommu, x86: No need to migrating irq for VT-d Posted-Interrupts iommu, x86: Add cap_pi_support() to detect VT-d PI capability iommu, x86: Add intel_irq_remapping_capability() for Intel iommu, x86: define irq_remapping_cap()
KVM: change struct pi_desc for VT-d Posted-Interrupts
KVM: Add some helper functions for Posted-Interrupts
KVM: Initialize VT-d Posted-Interrupts Descriptor
KVM: Define a new interface kvm_find_dest_vcpu() for VT-d PI
KVM: Get Posted-Interrupts descriptor address from struct kvm_vcpu
KVM: Make struct kvm_irq_routing_table accessible
KVM: make kvm_set_msi_irq() public
KVM: kvm-vfio: User API for VT-d Posted-Interrupts
KVM: kvm-vfio: implement the VFIO skeleton for VT-d Posted-Interrupts
KVM: x86: kvm-vfio: VT-d posted-interrupts setup
x86, irq: Define a global vector for VT-d Posted-Interrupts
KVM: Update Posted-Interrupts descriptor during vCPU scheduling
KVM: Change NDST field after vCPU scheduling
KVM: Add the handler for Wake-up Vector
KVM: Suppress posted-interrupt when 'SN' is set iommu/vt-d: Add a command line parameter for VT-d posted-interrupts

 Documentation/kernel-parameters.txt        |    1 +
 Documentation/virtual/kvm/devices/vfio.txt |    9 +
 arch/x86/include/asm/entry_arch.h          |    2 +
 arch/x86/include/asm/hardirq.h             |    1 +
 arch/x86/include/asm/hw_irq.h              |    2 +
 arch/x86/include/asm/irq_remapping.h       |   11 ++
 arch/x86/include/asm/irq_vectors.h         |    1 +
 arch/x86/include/asm/kvm_host.h            |   14 ++
 arch/x86/kernel/apic/msi.c                 |    1 +
 arch/x86/kernel/entry_64.S                 |    2 +
 arch/x86/kernel/irq.c                      |   27 +++
 arch/x86/kernel/irqinit.c                  |    2 +
 arch/x86/kvm/Makefile                      |    2 +-
 arch/x86/kvm/kvm_vfio_x86.c                |   68 ++++++++
 arch/x86/kvm/vmx.c                         |  251
+++++++++++++++++++++++++++-
 arch/x86/kvm/x86.c                         |   38 ++++-
 drivers/iommu/intel_irq_remapping.c        |   64 +++++++
 drivers/iommu/irq_remapping.c              |   24 +++-
 drivers/iommu/irq_remapping.h              |    8 +
 include/linux/dmar.h                       |   32 ++++
 include/linux/intel-iommu.h                |    1 +
 include/linux/irq.h                        |    7 +
 include/linux/kvm_host.h                   |   43 +++++
 include/uapi/linux/kvm.h                   |   10 +
 kernel/irq/chip.c                          |   14 ++
 kernel/irq/manage.c                        |   20 +++
 virt/kvm/irq_comm.c                        |   43 +++++-
 virt/kvm/irqchip.c                         |   11 --
 virt/kvm/kvm_main.c                        |   14 ++
 virt/kvm/vfio.c                            |  103 ++++++++++++
 30 files changed, 799 insertions(+), 27 deletions(-)
 create mode 100644 arch/x86/kvm/kvm_vfio_x86.c