Skip to contentSkip to conversation

Add VT-d Posted-Interrupts support

PATCH Superseded Feng Wu 3 participants 32 messages Sep 16, 2015 – Sep 18, 2015

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

v8 of 14 Superseded Latest is v14 →

lore
Share & tools
lore

Feng Wu

author
#0 lore raw

v8 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

v8:
refer to the changelog in each patch

v7:
* Define two weak irq bypass callbacks:
- kvm_arch_irq_bypass_start()
- kvm_arch_irq_bypass_stop()
* Remove the x86 dummy implementation of the above two functions.
* Print some useful information instead of WARN_ON() when the irq bypass consumer unregistration fails.
* Fix an issue when calling pi_pre_block and pi_post_block.

v6:
* Rebase on 4.2.0-rc6
* Rebase on https://lkml.org/lkml/2015/8/6/526 and http://www.gossamer-threads.com/lists/linux/kernel/2235623
* Make the add_consumer and del_consumer callbacks static
* Remove pointless INIT_LIST_HEAD to 'vdev->ctx[vector].producer.node)'
* Use dev_info instead of WARN_ON() when irq_bypass_register_producer fails
* Remove optional dummy callbacks for irq producer

v4:
* For lowest-priority interrupt, only support single-CPU destination interrupts at the current stage, more common lowest priority support will be added later.
* Accoring to Marcelo's suggestion, when vCPU is blocked, we handle the posted-interrupts in the HLT emulation path.
* Some small changes (coding style, typo, add some code comments)

v3:
* Adjust the Posted-interrupts Descriptor updating logic when vCPU is preempted or blocked.
* KVM_DEV_VFIO_DEVICE_POSTING_IRQ --> KVM_DEV_VFIO_DEVICE_POST_IRQ
* __KVM_HAVE_ARCH_KVM_VFIO_POSTING --> __KVM_HAVE_ARCH_KVM_VFIO_POST
* Add KVM_DEV_VFIO_DEVICE_UNPOST_IRQ attribute for VFIO irq, which can be used to change back to remapping mode.
* Fix typo

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]>

Feng Wu (13):
KVM: Extend struct pi_desc for VT-d Posted-Interrupts
KVM: Add some helper functions for Posted-Interrupts
KVM: Define a new interface kvm_intr_is_single_vcpu()
KVM: Make struct kvm_irq_routing_table accessible
KVM: make kvm_set_msi_irq() public
vfio: Register/unregister irq_bypass_producer
KVM: x86: Update IRTE for posted-interrupts
KVM: Implement IRQ bypass consumer callbacks for x86
KVM: Add an arch specific hooks in 'struct kvm_kernel_irqfd'
KVM: Update Posted-Interrupts Descriptor when vCPU is preempted
KVM: Update Posted-Interrupts Descriptor when vCPU is blocked
KVM: Warn if 'SN' is set during posting interrupts by software iommu/vt-d: Add a command line parameter for VT-d posted-interrupts

 Documentation/kernel-parameters.txt |   1 +
 arch/x86/include/asm/kvm_host.h     |  24 +++
 arch/x86/kvm/Kconfig                |   1 +
 arch/x86/kvm/irq_comm.c             |  99 +++++++++-
 arch/x86/kvm/lapic.c                |   5 +-
 arch/x86/kvm/lapic.h                |   2 +
 arch/x86/kvm/trace.h                |  33 ++++
 arch/x86/kvm/vmx.c                  | 361 +++++++++++++++++++++++++++++++++++-
 arch/x86/kvm/x86.c                  | 106 ++++++++++-
 drivers/iommu/irq_remapping.c       |  12 +-
 drivers/vfio/pci/Kconfig            |   1 +
 drivers/vfio/pci/vfio_pci_intrs.c   |   9 +
 drivers/vfio/pci/vfio_pci_private.h |   2 +
 include/linux/kvm_host.h            |  19 ++
 virt/kvm/eventfd.c                  |  31 +++-
 virt/kvm/irqchip.c                  |  10 -
 virt/kvm/kvm_main.c                 |   3 +
 17 files changed, 687 insertions(+), 32 deletions(-)
Signature · 1 line
2.1.0
author
#1 lore raw

PATCH v8 1/13 KVM: Extend struct pi_desc for VT-d Posted-Interrupts

Extend struct pi_desc for VT-d Posted-Interrupts.

---
 arch/x86/kvm/vmx.c | 20 ++++++++++++++++++--
 1 file changed, 18 insertions(+), 2 deletions(-)
arch/x86/kvm/vmx.c +18 -2
index 83b7b5c..271dd70 100644
@@ -446,8 +446,24 @@ 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 {
+				/* bit 256 - Outstanding Notification */
+			u16	on	: 1,
+				/* bit 257 - Suppress Notification */
+				sn	: 1,
+				/* bit 271:258 - Reserved */
+				rsvd_1	: 14;
+				/* bit 279:272 - Notification Vector */
+			u8	nv;
+				/* bit 287:280 - Reserved */
+			u8	rsvd_2;
+				/* bit 319:288 - Notification Destination */
+			u32	ndst;
+		};
+		u64 control;
+	};
+	u32 rsvd[6];
 } __aligned(64);
 
 static bool pi_test_and_set_on(struct pi_desc *pi_desc)
Signature · 1 line
2.1.0
author
#2 lore raw

PATCH v8 2/13 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 file changed, 26 insertions(+)
arch/x86/kvm/vmx.c +26 -0
index 271dd70..316f9bf 100644
@@ -443,6 +443,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 */
@@ -483,6 +485,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
2.1.0
author
#3 lore raw

PATCH v8 3/13 KVM: Define a new interface kvm_intr_is_single_vcpu()

In reply to Feng Wu

This patch defines a new interface kvm_intr_is_single_vcpu(), which can returns whether the interrupt is for single-CPU or not.

It is used by VT-d PI, since now we only support single-CPU interrupts, For lowest-priority interrupts, if user configures it via /proc/irq or uses irqbalance to make it single-CPU, we can use PI to deliver the interrupts to it. Full functionality of lowest-priority support will be added later.

---
v8:
- Some optimizations in kvm_intr_is_single_vcpu().
- Expose kvm_intr_is_single_vcpu() so we can use it in vmx code.
- Add kvm_intr_is_single_vcpu_fast() as the fast path to find the target vCPU for the single-destination interrupt

 arch/x86/include/asm/kvm_host.h |  3 ++
 arch/x86/kvm/irq_comm.c         | 94 +++++++++++++++++++++++++++++++++++++++++
 arch/x86/kvm/lapic.c            |  5 +--
 arch/x86/kvm/lapic.h            |  2 +
 4 files changed, 101 insertions(+), 3 deletions(-)
arch/x86/include/asm/kvm_host.h +3 -0
index 49ec903..af11bca 100644
@@ -1204,4 +1204,7 @@ int __x86_set_memory_region(struct kvm *kvm,
 int x86_set_memory_region(struct kvm *kvm,
 			  const struct kvm_userspace_memory_region *mem);
 
+bool kvm_intr_is_single_vcpu(struct kvm *kvm, struct kvm_lapic_irq *irq,
+			     struct kvm_vcpu **dest_vcpu);
+
 #endif /* _ASM_X86_KVM_HOST_H */
arch/x86/kvm/irq_comm.c +94 -0

100 lines not on this page · raw message

arch/x86/kvm/lapic.c +2 -3
index 2a5ca97..9848cd50 100644
@@ -136,13 +136,12 @@ static inline int kvm_apic_id(struct kvm_lapic *apic)
 /* The logical map is definitely wrong if we have multiple
  * modes at the same time.  (Physical map is always right.)
  */
-static inline bool kvm_apic_logical_map_valid(struct kvm_apic_map *map)
+bool kvm_apic_logical_map_valid(struct kvm_apic_map *map)
 {
 	return !(map->mode & (map->mode - 1));
 }
 
-static inline void
-apic_logical_id(struct kvm_apic_map *map, u32 dest_id, u16 *cid, u16 *lid)
+void apic_logical_id(struct kvm_apic_map *map, u32 dest_id, u16 *cid, u16 *lid)
 {
 	unsigned lid_bits;
 
arch/x86/kvm/lapic.h +2 -0
index 7195274..6798b87 100644
@@ -169,4 +169,6 @@ bool kvm_apic_pending_eoi(struct kvm_vcpu *vcpu, int vector);
 
 void wait_lapic_expire(struct kvm_vcpu *vcpu);
 
+void apic_logical_id(struct kvm_apic_map *map, u32 dest_id, u16 *cid, u16 *lid);
+bool kvm_apic_logical_map_valid(struct kvm_apic_map *map);
 #endif
Signature · 1 line
2.1.0
maintainer
#4 lore raw

Re: [PATCH v8 03/13] KVM: Define a new interface kvm_intr_is_single_vcpu()

Quoted 40 lines · Feng Wu

This patch defines a new interface kvm_intr_is_single_vcpu(), which can returns whether the interrupt is for single-CPU or not.

It is used by VT-d PI, since now we only support single-CPU interrupts, For lowest-priority interrupts, if user configures it via /proc/irq or uses irqbalance to make it single-CPU, we can use PI to deliver the interrupts to it. Full functionality of lowest-priority support will be added later.

---
v8:
- Some optimizations in kvm_intr_is_single_vcpu().
- Expose kvm_intr_is_single_vcpu() so we can use it in vmx code.
- Add kvm_intr_is_single_vcpu_fast() as the fast path to find the target vCPU for the single-destination interrupt

 arch/x86/include/asm/kvm_host.h |  3 ++
 arch/x86/kvm/irq_comm.c         | 94 +++++++++++++++++++++++++++++++++++++++++
 arch/x86/kvm/lapic.c            |  5 +--
 arch/x86/kvm/lapic.h            |  2 +
 4 files changed, 101 insertions(+), 3 deletions(-)
arch/x86/include/asm/kvm_host.h +3 -0
index 49ec903..af11bca 100644
@@ -1204,4 +1204,7 @@ int __x86_set_memory_region(struct kvm *kvm,
 int x86_set_memory_region(struct kvm *kvm,
 			  const struct kvm_userspace_memory_region *mem);
 
+bool kvm_intr_is_single_vcpu(struct kvm *kvm, struct kvm_lapic_irq *irq,
+			     struct kvm_vcpu **dest_vcpu);
+
 #endif /* _ASM_X86_KVM_HOST_H */
arch/x86/kvm/irq_comm.c +0 -0
index 9efff9e..97ba1d6 100644

arch/x86/kvm/irq_comm.c out: · line 302

Quoted 6 lines
arch/x86/kvm/irq_comm.c +3 -0
@@ -297,6 +297,100 @@ out:
 	return r;
 }
 
+static bool kvm_intr_is_single_vcpu_fast(struct kvm *kvm,
+					 struct kvm_lapic_irq *irq,
+					 struct kvm_vcpu **dest_vcpu)

Please put this in lapic.c, similar to kvm_irq_delivery_to_apic_fast, so that you do not have to export other functions.

Quoted 9 lines
arch/x86/kvm/irq_comm.c +9 -0
+{
+	struct kvm_apic_map *map;
+	bool ret = false;
+	struct kvm_lapic *dst = NULL;
+
+	if (irq->shorthand)
+		return false;
+
+	rcu_read_lock();

arch/x86/kvm/irq_comm.c out: · line 321

Quoted 10 lines
arch/x86/kvm/irq_comm.c +10 -0
+	map = rcu_dereference(kvm->arch.apic_map);
+
+	if (!map)
+		goto out;
+
+	if (irq->dest_mode == APIC_DEST_PHYSICAL) {
+		if (irq->dest_id == 0xFF)
+			goto out;
+
+		if (irq->dest_id >= ARRAY_SIZE(map->phys_map)) {

Warning here is wrong, the guest can trigger it.

Quoted 6 lines
arch/x86/kvm/irq_comm.c +6 -0
+			WARN_ON_ONCE(1);
+			goto out;
+		}
+
+		dst = map->phys_map[irq->dest_id];
+		if (dst && kvm_apic_present(dst->vcpu))

arch/x86/kvm/irq_comm.c out: · line 337

Quoted 10 lines
arch/x86/kvm/irq_comm.c +10 -0
+			*dest_vcpu = dst->vcpu;
+		else
+			goto out;
+	} else {
+		u16 cid;
+		unsigned long bitmap = 1;
+		int i, r = 0;
+
+		if (!kvm_apic_logical_map_valid(map)) {
+			WARN_ON_ONCE(1);

Same here.

arch/x86/kvm/irq_comm.c out: · line 344

Quoted 7 lines
arch/x86/kvm/irq_comm.c +7 -0
+			goto out;
+		}
+
+		apic_logical_id(map, irq->dest_id, &cid, (u16 *)&bitmap);
+
+		if (cid >= ARRAY_SIZE(map->logical_map)) {
+			WARN_ON_ONCE(1);

Same here.

Otherwise looks good.

Paolo

Quoted 84 lines
arch/x86/kvm/irq_comm.c +49 -0
+			goto out;
+		}
+
+		for_each_set_bit(i, &bitmap, 16) {
+			dst = map->logical_map[cid][i];
+			if (++r == 2)
+				goto out;
+		}
+
+		if (dst && kvm_apic_present(dst->vcpu))
+			*dest_vcpu = dst->vcpu;
+		else
+			goto out;
+	}
+
+	ret = true;
+out:
+	rcu_read_unlock();
+	return ret;
+}
+
+
+bool kvm_intr_is_single_vcpu(struct kvm *kvm, struct kvm_lapic_irq *irq,
+			     struct kvm_vcpu **dest_vcpu)
+{
+	int i, r = 0;
+	struct kvm_vcpu *vcpu;
+
+	if (kvm_intr_is_single_vcpu_fast(kvm, irq, dest_vcpu))
+		return true;
+
+	kvm_for_each_vcpu(i, vcpu, kvm) {
+		if (!kvm_apic_present(vcpu))
+			continue;
+
+		if (!kvm_apic_match_dest(vcpu, NULL, irq->shorthand,
+					irq->dest_id, irq->dest_mode))
+			continue;
+
+		if (++r == 2)
+			return false;
+
+		*dest_vcpu = vcpu;
+	}
+
+	return r == 1;
+}
+EXPORT_SYMBOL_GPL(kvm_intr_is_single_vcpu);
+
 #define IOAPIC_ROUTING_ENTRY(irq) \
 	{ .gsi = irq, .type = KVM_IRQ_ROUTING_IRQCHIP,	\
 	  .u.irqchip = { .irqchip = KVM_IRQCHIP_IOAPIC, .pin = (irq) } }
arch/x86/kvm/lapic.c +2 -3
index 2a5ca97..9848cd50 100644
@@ -136,13 +136,12 @@ static inline int kvm_apic_id(struct kvm_lapic *apic)
 /* The logical map is definitely wrong if we have multiple
  * modes at the same time.  (Physical map is always right.)
  */
-static inline bool kvm_apic_logical_map_valid(struct kvm_apic_map *map)
+bool kvm_apic_logical_map_valid(struct kvm_apic_map *map)
 {
 	return !(map->mode & (map->mode - 1));
 }
 
-static inline void
-apic_logical_id(struct kvm_apic_map *map, u32 dest_id, u16 *cid, u16 *lid)
+void apic_logical_id(struct kvm_apic_map *map, u32 dest_id, u16 *cid, u16 *lid)
 {
 	unsigned lid_bits;
 
arch/x86/kvm/lapic.h +2 -0
index 7195274..6798b87 100644
@@ -169,4 +169,6 @@ bool kvm_apic_pending_eoi(struct kvm_vcpu *vcpu, int vector);
 
 void wait_lapic_expire(struct kvm_vcpu *vcpu);
 
+void apic_logical_id(struct kvm_apic_map *map, u32 dest_id, u16 *cid, u16 *lid);
+bool kvm_apic_logical_map_valid(struct kvm_apic_map *map);
 #endif
author
#5 lore raw

RE: [PATCH v8 03/13] KVM: Define a new interface kvm_intr_is_single_vcpu()

Quoted 80 lines

-----Original Message-----
From: Paolo Bonzini [mailto:[email protected]]
Sent: Wednesday, September 16, 2015 5:23 PM
To: Wu, Feng; [email protected]; [email protected]; [email protected]
Cc: [email protected]; [email protected]; iommu-cunTk1MwBs9QetFLy7KEm3xJsTq8ys+cHZ5vskTnxNA@public.gmane.org; [email protected]
Subject: Re: [PATCH v8 03/13] KVM: Define a new interface
kvm_intr_is_single_vcpu()

Quoted 17 lines · Feng Wu

This patch defines a new interface kvm_intr_is_single_vcpu(), which can returns whether the interrupt is for single-CPU or not.

It is used by VT-d PI, since now we only support single-CPU interrupts, For lowest-priority interrupts, if user configures it via /proc/irq or uses irqbalance to make it single-CPU, we can use PI to deliver the interrupts to it. Full functionality of lowest-priority support will be added later.

---
v8:
- Some optimizations in kvm_intr_is_single_vcpu().
- Expose kvm_intr_is_single_vcpu() so we can use it in vmx code.
- Add kvm_intr_is_single_vcpu_fast() as the fast path to find the target vCPU for the single-destination interrupt

Quoted 2 lines
 arch/x86/include/asm/kvm_host.h |  3 ++
 arch/x86/kvm/irq_comm.c         | 94

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

Review

Quoted 5 lines
 arch/x86/kvm/lapic.c            |  5 +--
 arch/x86/kvm/lapic.h            |  2 +
 4 files changed, 101 insertions(+), 3 deletions(-)
(quoted diff) +0 -0

b/arch/x86/include/asm/kvm_host.h

Quoted 16 lines

index 49ec903..af11bca 100644

arch/x86/include/asm/kvm_host.h +3 -0
@@ -1204,4 +1204,7 @@ int __x86_set_memory_region(struct kvm *kvm,
 int x86_set_memory_region(struct kvm *kvm,
 			  const struct kvm_userspace_memory_region *mem);
 
+bool kvm_intr_is_single_vcpu(struct kvm *kvm, struct kvm_lapic_irq *irq,
+			     struct kvm_vcpu **dest_vcpu);
+
 #endif /* _ASM_X86_KVM_HOST_H */
arch/x86/kvm/irq_comm.c +0 -0
index 9efff9e..97ba1d6 100644

arch/x86/kvm/irq_comm.c out: · line 302

Quoted 6 lines
arch/x86/kvm/irq_comm.c +3 -0
@@ -297,6 +297,100 @@ out:
 	return r;
 }
 
+static bool kvm_intr_is_single_vcpu_fast(struct kvm *kvm,
+					 struct kvm_lapic_irq *irq,
+					 struct kvm_vcpu **dest_vcpu)

Please put this in lapic.c, similar to kvm_irq_delivery_to_apic_fast, so that you do not have to export other functions.

Quoted 13 lines
arch/x86/kvm/irq_comm.c +13 -0
+{
+	struct kvm_apic_map *map;
+	bool ret = false;
+	struct kvm_lapic *dst = NULL;
+
+	if (irq->shorthand)
+		return false;
+
+	rcu_read_lock();
+	map = rcu_dereference(kvm->arch.apic_map);
+
+	if (!map)
+		goto out;
Quoted 8 lines

arch/x86/kvm/irq_comm.c out: · line 321

Quoted 6 lines
arch/x86/kvm/irq_comm.c +6 -0
+
+	if (irq->dest_mode == APIC_DEST_PHYSICAL) {
+		if (irq->dest_id == 0xFF)
+			goto out;
+
+		if (irq->dest_id >= ARRAY_SIZE(map->phys_map)) {

Warning here is wrong, the guest can trigger it.

Could you please share more information about how the guest triggers these conditions (including the following two), Thanks a lot!

Thanks,
Feng

Quoted 122 lines
Quoted 7 lines
arch/x86/kvm/irq_comm.c +6 -0
+			WARN_ON_ONCE(1);
+			goto out;
+		}
+
+		dst = map->phys_map[irq->dest_id];
+		if (dst && kvm_apic_present(dst->vcpu))

arch/x86/kvm/irq_comm.c out: · line 337

Quoted 10 lines
arch/x86/kvm/irq_comm.c +10 -0
+			*dest_vcpu = dst->vcpu;
+		else
+			goto out;
+	} else {
+		u16 cid;
+		unsigned long bitmap = 1;
+		int i, r = 0;
+
+		if (!kvm_apic_logical_map_valid(map)) {
+			WARN_ON_ONCE(1);

Same here.

arch/x86/kvm/irq_comm.c out: · line 344

Quoted 7 lines
arch/x86/kvm/irq_comm.c +7 -0
+			goto out;
+		}
+
+		apic_logical_id(map, irq->dest_id, &cid, (u16 *)&bitmap);
+
+		if (cid >= ARRAY_SIZE(map->logical_map)) {
+			WARN_ON_ONCE(1);

Same here.

Otherwise looks good.

Paolo

arch/x86/kvm/lapic.c static inline int kvm_apic_id(struct kvm_lapic

Quoted 57 lines
arch/x86/kvm/irq_comm.c +49 -0
+			goto out;
+		}
+
+		for_each_set_bit(i, &bitmap, 16) {
+			dst = map->logical_map[cid][i];
+			if (++r == 2)
+				goto out;
+		}
+
+		if (dst && kvm_apic_present(dst->vcpu))
+			*dest_vcpu = dst->vcpu;
+		else
+			goto out;
+	}
+
+	ret = true;
+out:
+	rcu_read_unlock();
+	return ret;
+}
+
+
+bool kvm_intr_is_single_vcpu(struct kvm *kvm, struct kvm_lapic_irq *irq,
+			     struct kvm_vcpu **dest_vcpu)
+{
+	int i, r = 0;
+	struct kvm_vcpu *vcpu;
+
+	if (kvm_intr_is_single_vcpu_fast(kvm, irq, dest_vcpu))
+		return true;
+
+	kvm_for_each_vcpu(i, vcpu, kvm) {
+		if (!kvm_apic_present(vcpu))
+			continue;
+
+		if (!kvm_apic_match_dest(vcpu, NULL, irq->shorthand,
+					irq->dest_id, irq->dest_mode))
+			continue;
+
+		if (++r == 2)
+			return false;
+
+		*dest_vcpu = vcpu;
+	}
+
+	return r == 1;
+}
+EXPORT_SYMBOL_GPL(kvm_intr_is_single_vcpu);
+
 #define IOAPIC_ROUTING_ENTRY(irq) \
 	{ .gsi = irq, .type = KVM_IRQ_ROUTING_IRQCHIP,	\
 	  .u.irqchip = { .irqchip = KVM_IRQCHIP_IOAPIC, .pin = (irq) } }
arch/x86/kvm/lapic.c +0 -0
index 2a5ca97..9848cd50 100644
@@ -136,13 +136,12 @@ static inline int kvm_apic_id(struct kvm_lapic

*apic)

arch/x86/kvm/lapic.c static inline int kvm_apic_id(struct kvm_lapic *apic)

Quoted 12 lines
arch/x86/kvm/lapic.c +2 -3
 /* The logical map is definitely wrong if we have multiple
  * modes at the same time.  (Physical map is always right.)
  */
-static inline bool kvm_apic_logical_map_valid(struct kvm_apic_map *map)
+bool kvm_apic_logical_map_valid(struct kvm_apic_map *map)
 {
 	return !(map->mode & (map->mode - 1));
 }
 
-static inline void
-apic_logical_id(struct kvm_apic_map *map, u32 dest_id, u16 *cid, u16 *lid)
+void apic_logical_id(struct kvm_apic_map *map, u32 dest_id, u16 *cid, u16

*lid)

arch/x86/kvm/lapic.h bool kvm_apic_pending_eoi(struct kvm_vcpu *vcpu,

Quoted 8 lines
arch/x86/kvm/lapic.c +0 -0
 {
 	unsigned lid_bits;
 
arch/x86/kvm/lapic.h +0 -0
index 7195274..6798b87 100644
@@ -169,4 +169,6 @@ bool kvm_apic_pending_eoi(struct kvm_vcpu *vcpu,

int vector);

arch/x86/kvm/lapic.h bool kvm_apic_pending_eoi(struct kvm_vcpu *vcpu, int vector);

Quoted 4 lines
arch/x86/kvm/lapic.h +1 -0
 void wait_lapic_expire(struct kvm_vcpu *vcpu);
 
+void apic_logical_id(struct kvm_apic_map *map, u32 dest_id, u16 *cid, u16

*lid);

Quoted 3 lines
arch/x86/kvm/lapic.h +1 -0
+bool kvm_apic_logical_map_valid(struct kvm_apic_map *map);
 #endif
 
maintainer
#6 lore raw

Re: [PATCH v8 03/13] KVM: Define a new interface kvm_intr_is_single_vcpu()

Quoted 10 lines · Feng
Quoted 7 lines
Quoted 7 lines
Quoted 5 lines

arch/x86/kvm/irq_comm.c out: · line 321

Quoted 5 lines
arch/x86/kvm/irq_comm.c +5 -0
+	if (irq->dest_mode == APIC_DEST_PHYSICAL) {
+		if (irq->dest_id == 0xFF)
+			goto out;
+
+		if (irq->dest_id >= ARRAY_SIZE(map->phys_map)) {

Warning here is wrong, the guest can trigger it.

Could you please share more information about how the guest triggers these conditions (including the following two), Thanks a lot!

irq->dest_id is a 16-bit value, so it can be > 255.

Review

Quoted 2 lines
(quoted diff) +2 -0
+		if (!kvm_apic_logical_map_valid(map)) {
+			WARN_ON_ONCE(1);

Here, the guest can trigger it by setting a few APICs in flat mode and others in cluster mode, for example.

arch/x86/kvm/irq_comm.c out: · line 344

Quoted 2 lines
arch/x86/kvm/irq_comm.c +2 -0
+		if (cid >= ARRAY_SIZE(map->logical_map)) {
+			WARN_ON_ONCE(1);

In x2apic mode irq->dest_id could have bits 12..15 set.

Paolo

author
#7 lore raw

RE: [PATCH v8 03/13] KVM: Define a new interface kvm_intr_is_single_vcpu()

Quoted 22 lines

-----Original Message-----
From: Paolo Bonzini [mailto:[email protected]]
Sent: Thursday, September 17, 2015 5:42 PM
To: Wu, Feng; [email protected]; [email protected]; [email protected]
Cc: [email protected]; [email protected]; iommu-cunTk1MwBs9QetFLy7KEm3xJsTq8ys+cHZ5vskTnxNA@public.gmane.org; [email protected]
Subject: Re: [PATCH v8 03/13] KVM: Define a new interface
kvm_intr_is_single_vcpu()

Quoted 7 lines · Feng
Quoted 7 lines
Quoted 7 lines
Quoted 5 lines

arch/x86/kvm/irq_comm.c out: · line 321

Quoted 5 lines
arch/x86/kvm/irq_comm.c +5 -0
+	if (irq->dest_mode == APIC_DEST_PHYSICAL) {
+		if (irq->dest_id == 0xFF)
+			goto out;
+
+		if (irq->dest_id >= ARRAY_SIZE(map->phys_map)) {

Warning here is wrong, the guest can trigger it.

Quoted 5 lines
Quoted 3 lines

Could you please share more information about how the guest triggers these conditions (including the following two), Thanks a lot!

irq->dest_id is a 16-bit value, so it can be > 255.

Yes, irq->dest_id is defined as u32, but by looking the current KVM code, seems desst_id is used as an u8 variable, even in x2apic mode the dest_id will not beyond 255 (except for broadcast dest in in x2apic mode). Correct me if I am wrong. Thanks a lot!

Quoted 6 lines

Review

Quoted 3 lines
(quoted diff) +2 -0
+		if (!kvm_apic_logical_map_valid(map)) {
+			WARN_ON_ONCE(1);

Here, the guest can trigger it by setting a few APICs in flat mode and others in cluster mode, for example.

Oh, right, the logical map works only when the destination mode of all the vCPUs are the same.

Quoted 5 lines

arch/x86/kvm/irq_comm.c out: · line 344

Quoted 3 lines
arch/x86/kvm/irq_comm.c +2 -0
+		if (cid >= ARRAY_SIZE(map->logical_map)) {
+			WARN_ON_ONCE(1);

In x2apic mode irq->dest_id could have bits 12..15 set.

cid is gotten from bit 16 ..31 of the ldr (in apic_logical_id()), and in x2apic mode, ldr is constructed in kvm_apic_set_x2apic_id() as below:

u32 ldr = ((id >> 4) << 16) | (1 << (id & 0xf));

So in fact, cid is (id >> 4), I cannot think of why cid can beyond 15. Do I miss something here? Thanks!

Thanks,
Feng

Quoted 2 lines

Paolo

maintainer
#8 lore raw

Re: [PATCH v8 03/13] KVM: Define a new interface kvm_intr_is_single_vcpu()

Quoted 12 lines
Quoted 12 lines · Feng
Quoted 10 lines
Quoted 7 lines
Quoted 7 lines
Quoted 5 lines

arch/x86/kvm/irq_comm.c out: · line 321

Quoted 5 lines
arch/x86/kvm/irq_comm.c +5 -0
+	if (irq->dest_mode == APIC_DEST_PHYSICAL) {
+		if (irq->dest_id == 0xFF)
+			goto out;
+
+		if (irq->dest_id >= ARRAY_SIZE(map->phys_map)) {

Warning here is wrong, the guest can trigger it.

Could you please share more information about how the guest triggers these conditions (including the following two), Thanks a lot!

Quoted 6 lines
Quoted 1 line

irq->dest_id is a 16-bit value, so it can be > 255.

Yes, irq->dest_id is defined as u32, but by looking the current KVM code, seems desst_id is used as an u8 variable, even in x2apic mode the dest_id will not beyond 255 (except for broadcast dest in in x2apic mode). Correct me if I am wrong. Thanks a lot!

Actually you're right, the MSI destination is only 8 bits. I was confused because of

#define MSI_ADDR_DEST_ID_MASK 0x00ffff0

in arch/x86/include/asm/msidef.h. But there may be a bug, see below...

Quoted 5 lines
Quoted 4 lines

arch/x86/kvm/irq_comm.c out: · line 344

Quoted 2 lines
arch/x86/kvm/irq_comm.c +2 -0
+		if (cid >= ARRAY_SIZE(map->logical_map)) {
+			WARN_ON_ONCE(1);

In x2apic mode irq->dest_id could have bits 12..15 set.

Quoted 7 lines

cid is gotten from bit 16 ..31 of the ldr (in apic_logical_id()), and in x2apic mode, ldr is constructed in kvm_apic_set_x2apic_id() as below:

u32 ldr = ((id >> 4) << 16) | (1 << (id & 0xf));

So in fact, cid is (id >> 4), I cannot think of why cid can beyond 15.

I think kvm_apic_match_logical_addr for MSI and IOAPIC interrupts is buggy in x2apic mode.

It does:

        if (apic_x2apic_mode(apic))
                return ((logical_id >> 16) == (mda >> 16))
                       && (logical_id & mda & 0xffff) != 0;

But mda is only 8-bits for MSI and IOAPIC interrupts.

Radim, should kvm_apic_mda also handle the !ipi && x2apic_mda && dest_id != APIC_BROADCAST case? It never triggers with Linux because it uses only the physical mode (that's not super-easy to see; ioapic_set_affinity looks for the RTEs in irq_data->chip_data and that is allocated with kzalloc).

Quoted 1 line

Do I miss something here? Thanks!

No, you were right.

But still I think the WARNs are unnecessary; it is conceivable that some future chipset adds support for more than 8-bits in the dest_id.

Paolo

Quoted 5 lines

Thanks,
Feng

Quoted 2 lines

Paolo

#9 lore raw

Re: [PATCH v8 03/13] KVM: Define a new interface kvm_intr_is_single_vcpu()

2015-09-17 16:24+0200, Paolo Bonzini:

Quoted 9 lines

I think kvm_apic_match_logical_addr for MSI and IOAPIC interrupts is buggy in x2apic mode.

It does:

        if (apic_x2apic_mode(apic))
                return ((logical_id >> 16) == (mda >> 16))
                       && (logical_id & mda & 0xffff) != 0;
Quoted 7 lines

But mda is only 8-bits for MSI and IOAPIC interrupts.

Radim, should kvm_apic_mda also handle the !ipi && x2apic_mda && dest_id != APIC_BROADCAST case? It never triggers with Linux because it uses only the physical mode (that's not super-easy to see; ioapic_set_affinity looks for the RTEs in irq_data->chip_data and that is allocated with kzalloc).

KVM handles that case, it's just convoluted.
(I wish we scrapped the IR-less x2APIC mode.)

For interrupts from MSI and IOxAPIC:
- Flat logical interrupts are delivered as if we had natural (CPU0<->bit0, CPU1<->bit1, ...) flat logical xAPIC for first 8 VCPUs.
- Cluster logical doesn't work much, it's interpreted like flat logical. I didn't care about xAPIC cluster because Linux, the sole user of our paravirtualized x2APIC, doesn't configure it.

I'll paste kvm_apic_mda() source for better explanation:

  static u32 kvm_apic_mda(unsigned int dest_id, struct kvm_lapic *source,
                                                struct kvm_lapic *target)
  {
  	bool ipi = source != NULL;
  	bool x2apic_mda = apic_x2apic_mode(ipi ? source : target);
  
  	if (!ipi && dest_id == APIC_BROADCAST && x2apic_mda)
  		return X2APIC_BROADCAST;
  
  	return x2apic_mda ? dest_id : SET_APIC_DEST_FIELD(dest_id);
  }

MSI/IOxAPIC interrupt means that source is NULL and if the target is in x2APIC mode, the original 'dest_id' is returned as mda => a flat logical xAPIC to 0x0f will get interpreted as (cluster) logical x2APIC 0xf in kvm_apic_match_logical_addr().
xAPIC address are only 8 bit long so they always get delivered to x2APIC cluster 0, where first 16 bits work like xAPIC flat logical mode.

maintainer
#10 lore raw

Re: [PATCH v8 03/13] KVM: Define a new interface kvm_intr_is_single_vcpu()

Quoted 21 lines · Radim Krčmář

For interrupts from MSI and IOxAPIC:
- Flat logical interrupts are delivered as if we had natural (CPU0<->bit0, CPU1<->bit1, ...) flat logical xAPIC for first 8 VCPUs.
- Cluster logical doesn't work much, it's interpreted like flat logical. I didn't care about xAPIC cluster because Linux, the sole user of our paravirtualized x2APIC, doesn't configure it.

I'll paste kvm_apic_mda() source for better explanation:

  static u32 kvm_apic_mda(unsigned int dest_id, struct kvm_lapic *source,
                                                struct kvm_lapic *target)
  {
  	bool ipi = source != NULL;
  	bool x2apic_mda = apic_x2apic_mode(ipi ? source : target);
  
  	if (!ipi && dest_id == APIC_BROADCAST && x2apic_mda)
  		return X2APIC_BROADCAST;
  
  	return x2apic_mda ? dest_id : SET_APIC_DEST_FIELD(dest_id);
  }
Quoted 6 lines

MSI/IOxAPIC interrupt means that source is NULL and if the target is in x2APIC mode, the original 'dest_id' is returned as mda => a flat logical xAPIC to 0x0f will get interpreted as (cluster) logical x2APIC 0xf in kvm_apic_match_logical_addr().
xAPIC address are only 8 bit long so they always get delivered to x2APIC cluster 0, where first 16 bits work like xAPIC flat logical mode.

Ok, I was wondering whether this was the correct interpretation. Thanks!

Paolo
_______________________________________________
iommu mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/iommu

author
#11 lore raw

RE: [PATCH v8 03/13] KVM: Define a new interface kvm_intr_is_single_vcpu()

Quoted 37 lines

-----Original Message-----
From: Paolo Bonzini [mailto:[email protected]]
Sent: Friday, September 18, 2015 12:00 AM
To: Radim Krčmář
Cc: Wu, Feng; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]
Subject: Re: [PATCH v8 03/13] KVM: Define a new interface kvm_intr_is_single_vcpu()

Quoted 11 lines · Radim Krčmář

For interrupts from MSI and IOxAPIC:
- Flat logical interrupts are delivered as if we had natural (CPU0<->bit0, CPU1<->bit1, ...) flat logical xAPIC for first 8 VCPUs.
- Cluster logical doesn't work much, it's interpreted like flat logical. I didn't care about xAPIC cluster because Linux, the sole user of our paravirtualized x2APIC, doesn't configure it.

I'll paste kvm_apic_mda() source for better explanation:

static u32 kvm_apic_mda(unsigned int dest_id, struct kvm_lapic *source,
struct kvm_lapic

*target)

Quoted 10 lines
  {
  	bool ipi = source != NULL;
  	bool x2apic_mda = apic_x2apic_mode(ipi ? source : target);

  	if (!ipi && dest_id == APIC_BROADCAST && x2apic_mda)
  		return X2APIC_BROADCAST;

  	return x2apic_mda ? dest_id : SET_APIC_DEST_FIELD(dest_id);
  }
Quoted 8 lines
Quoted 6 lines

MSI/IOxAPIC interrupt means that source is NULL and if the target is in x2APIC mode, the original 'dest_id' is returned as mda => a flat logical xAPIC to 0x0f will get interpreted as (cluster) logical x2APIC 0xf in kvm_apic_match_logical_addr().
xAPIC address are only 8 bit long so they always get delivered to x2APIC cluster 0, where first 16 bits work like xAPIC flat logical mode.

Ok, I was wondering whether this was the correct interpretation. Thanks!

Paolo, I don't think Radim clarify your concern, right? Since mda is 8-bit, it is wrong with mda >> 16, this is your concern, right?

Thanks,
Feng

Quoted 2 lines

Paolo

_______________________________________________
iommu mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/iommu

#12 lore raw

Re: [PATCH v8 03/13] KVM: Define a new interface kvm_intr_is_single_vcpu()

2015-09-17 23:18+0000, Wu, Feng:

Quoted 9 lines
Quoted 6 lines

From: Paolo Bonzini [mailto:[email protected]]

Quoted 2 lines · Radim Krčmář

xAPIC address are only 8 bit long so they always get delivered to x2APIC cluster 0, where first 16 bits work like xAPIC flat logical mode.

Ok, I was wondering whether this was the correct interpretation. Thanks!

Paolo, I don't think Radim clarify your concern, right? Since mda is 8-bit, it is wrong with mda >> 16, this is your concern, right?

In case it was: mda is u32 so the bitshift is defined by C. (xAPIC destinations in KVM's x2APIC mode are stored in lowest 8 bits of
mda, hence the cluster is always 0.)

Or am I still missing the point?
_______________________________________________
iommu mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/iommu

maintainer
#13 lore raw

Re: [PATCH v8 03/13] KVM: Define a new interface kvm_intr_is_single_vcpu()

Quoted 9 lines · Radim Krčmář
Quoted 4 lines
Quoted 4 lines
Quoted 1 line
Quoted 1 line

Ok, I was wondering whether this was the correct interpretation. Thanks!

Paolo, I don't think Radim clarify your concern, right? Since mda is 8-bit, it is wrong with mda >> 16, this is your concern, right?

In case it was: mda is u32 so the bitshift is defined by C. (xAPIC destinations in KVM's x2APIC mode are stored in lowest 8 bits of
mda, hence the cluster is always 0.)

Or am I still missing the point?

Yes, remembering that the cluster is always 0 solved my doubt.

Paolo
_______________________________________________
iommu mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/iommu

author
#14 lore raw

RE: [PATCH v8 03/13] KVM: Define a new interface kvm_intr_is_single_vcpu()

In reply to Paolo Bonzini
Quoted 69 lines

-----Original Message-----
From: Paolo Bonzini [mailto:[email protected]]
Sent: Thursday, September 17, 2015 10:25 PM
To: Wu, Feng; [email protected]; [email protected]; [email protected]
Cc: [email protected]; [email protected]; iommu-cunTk1MwBs9QetFLy7KEm3xJsTq8ys+cHZ5vskTnxNA@public.gmane.org; [email protected]; Radim Krčmář
Subject: Re: [PATCH v8 03/13] KVM: Define a new interface
kvm_intr_is_single_vcpu()

Quoted 12 lines
Quoted 12 lines · Feng
Quoted 10 lines
Quoted 7 lines
Quoted 7 lines
Quoted 5 lines

arch/x86/kvm/irq_comm.c out: · line 321

Quoted 5 lines
arch/x86/kvm/irq_comm.c +5 -0
+	if (irq->dest_mode == APIC_DEST_PHYSICAL) {
+		if (irq->dest_id == 0xFF)
+			goto out;
+
+		if (irq->dest_id >= ARRAY_SIZE(map->phys_map)) {

Warning here is wrong, the guest can trigger it.

Could you please share more information about how the guest triggers these conditions (including the following two), Thanks a lot!

Quoted 6 lines
Quoted 1 line

irq->dest_id is a 16-bit value, so it can be > 255.

Yes, irq->dest_id is defined as u32, but by looking the current KVM code, seems desst_id is used as an u8 variable, even in x2apic mode the dest_id will not beyond 255 (except for broadcast dest in in x2apic mode). Correct me if I am wrong. Thanks a lot!

Actually you're right, the MSI destination is only 8 bits. I was confused because of

#define MSI_ADDR_DEST_ID_MASK 0x00ffff0

in arch/x86/include/asm/msidef.h. But there may be a bug, see below...

Quoted 5 lines
Quoted 4 lines

arch/x86/kvm/irq_comm.c out: · line 344

Quoted 2 lines
arch/x86/kvm/irq_comm.c +2 -0
+		if (cid >= ARRAY_SIZE(map->logical_map)) {
+			WARN_ON_ONCE(1);

In x2apic mode irq->dest_id could have bits 12..15 set.

Quoted 7 lines

cid is gotten from bit 16 ..31 of the ldr (in apic_logical_id()), and in x2apic mode, ldr is constructed in kvm_apic_set_x2apic_id() as below:

u32 ldr = ((id >> 4) << 16) | (1 << (id & 0xf));

So in fact, cid is (id >> 4), I cannot think of why cid can beyond 15.

I think kvm_apic_match_logical_addr for MSI and IOAPIC interrupts is buggy in x2apic mode.

It does:

        if (apic_x2apic_mode(apic))
                return ((logical_id >> 16) == (mda >> 16))
                       && (logical_id & mda & 0xffff) != 0;

But mda is only 8-bits for MSI and IOAPIC interrupts.

Radim, should kvm_apic_mda also handle the !ipi && x2apic_mda && dest_id != APIC_BROADCAST case? It never triggers with Linux because it uses only the physical mode (that's not super-easy to see; ioapic_set_affinity looks for the RTEs in irq_data->chip_data and that is allocated with kzalloc).

Quoted 6 lines
Quoted 1 line

Do I miss something here? Thanks!

No, you were right.

But still I think the WARNs are unnecessary; it is conceivable that some future chipset adds support for more than 8-bits in the dest_id.

No problem, I agree with it. Here I just want clarify some questions, thanks for the elaboration!

Thanks,
Feng

Quoted 8 lines

Paolo

Quoted 5 lines

Thanks,
Feng

Quoted 2 lines

Paolo

author
#15 lore raw

PATCH v8 4/13 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 | 14 ++++++++++++++
 virt/kvm/irqchip.c       | 10 ----------
 2 files changed, 14 insertions(+), 10 deletions(-)
include/linux/kvm_host.h +14 -0
index 5ac8d21..5f183fb 100644
@@ -328,6 +328,20 @@ 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];
+	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];
+};
+
+#endif
+
 #ifndef KVM_PRIVATE_MEM_SLOTS
 #define KVM_PRIVATE_MEM_SLOTS 0
 #endif
virt/kvm/irqchip.c +0 -10
index 21c1424..2cf45d3 100644
@@ -31,16 +31,6 @@
 #include <trace/events/kvm.h>
 #include "irq.h"
 
-struct kvm_irq_routing_table {
-	int chip[KVM_NR_IRQCHIPS][KVM_IRQCHIP_NUM_PINS];
-	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
2.1.0
author
#16 lore raw

PATCH v8 5/13 KVM: make kvm_set_msi_irq() public

In reply to Feng Wu

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

---
v8:
- Export kvm_set_msi_irq() so we can use it in vmx code

 arch/x86/include/asm/kvm_host.h | 4 ++++
 arch/x86/kvm/irq_comm.c         | 5 +++--
 2 files changed, 7 insertions(+), 2 deletions(-)
arch/x86/include/asm/kvm_host.h +4 -0
index af11bca..daa6126 100644
@@ -175,6 +175,8 @@ enum {
  */
 #define KVM_APIC_PV_EOI_PENDING	1
 
+struct kvm_kernel_irq_routing_entry;
+
 /*
  * We don't want allocation failures within the mmu code, so we preallocate
  * enough memory for a single page fault in a cache.
@@ -1207,4 +1209,6 @@ int x86_set_memory_region(struct kvm *kvm,
 bool kvm_intr_is_single_vcpu(struct kvm *kvm, struct kvm_lapic_irq *irq,
 			     struct kvm_vcpu **dest_vcpu);
 
+void kvm_set_msi_irq(struct kvm_kernel_irq_routing_entry *e,
+		     struct kvm_lapic_irq *irq);
 #endif /* _ASM_X86_KVM_HOST_H */
arch/x86/kvm/irq_comm.c +3 -2
index 97ba1d6..add52d8 100644
@@ -91,8 +91,8 @@ 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,
-				   struct kvm_lapic_irq *irq)
+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);
 
@@ -108,6 +108,7 @@ static inline void kvm_set_msi_irq(struct kvm_kernel_irq_routing_entry *e,
 	irq->level = 1;
 	irq->shorthand = 0;
 }
+EXPORT_SYMBOL_GPL(kvm_set_msi_irq);
 
 int kvm_set_msi(struct kvm_kernel_irq_routing_entry *e,
 		struct kvm *kvm, int irq_source_id, int level, bool line_status)
Signature · 1 line
2.1.0
author
#17 lore raw

PATCH v8 6/13 vfio: Register/unregister irq_bypass_producer

In reply to Feng Wu

This patch adds the registration/unregistration of an irq_bypass_producer for MSI/MSIx on vfio pci devices.

---
v8:
- Merge "[PATCH v7 08/17] vfio: Select IRQ_BYPASS_MANAGER for vfio PCI devices" into this patch.

v6:
- Make the add_consumer and del_consumer callbacks static
- Remove pointless INIT_LIST_HEAD to 'vdev->ctx[vector].producer.node)'
- Use dev_info instead of WARN_ON() when irq_bypass_register_producer fails
- Remove optional dummy callbacks for irq producer

 drivers/vfio/pci/Kconfig            | 1 +
 drivers/vfio/pci/vfio_pci_intrs.c   | 9 +++++++++
 drivers/vfio/pci/vfio_pci_private.h | 2 ++
 3 files changed, 12 insertions(+)
drivers/vfio/pci/Kconfig +1 -0
index 579d83b..02912f1 100644
@@ -2,6 +2,7 @@ config VFIO_PCI
 	tristate "VFIO support for PCI devices"
 	depends on VFIO && PCI && EVENTFD
 	select VFIO_VIRQFD
+	select IRQ_BYPASS_MANAGER
 	help
 	  Support for the PCI VFIO bus driver.  This is required to make
 	  use of PCI drivers using the VFIO framework.
drivers/vfio/pci/vfio_pci_intrs.c +9 -0
index 1f577b4..c65299d 100644
@@ -319,6 +319,7 @@ static int vfio_msi_set_vector_signal(struct vfio_pci_device *vdev,
 
 	if (vdev->ctx[vector].trigger) {
 		free_irq(irq, vdev->ctx[vector].trigger);
+		irq_bypass_unregister_producer(&vdev->ctx[vector].producer);
 		kfree(vdev->ctx[vector].name);
 		eventfd_ctx_put(vdev->ctx[vector].trigger);
 		vdev->ctx[vector].trigger = NULL;
@@ -360,6 +361,14 @@ static int vfio_msi_set_vector_signal(struct vfio_pci_device *vdev,
 		return ret;
 	}
 
+	vdev->ctx[vector].producer.token = trigger;
+	vdev->ctx[vector].producer.irq = irq;
+	ret = irq_bypass_register_producer(&vdev->ctx[vector].producer);
+	if (unlikely(ret))
+		dev_info(&pdev->dev,
+		"irq bypass producer (token %p) registeration fails: %d\n",
+		vdev->ctx[vector].producer.token, ret);
+
 	vdev->ctx[vector].trigger = trigger;
 
 	return 0;
drivers/vfio/pci/vfio_pci_private.h +2 -0
index ae0e1b4..0e7394f 100644
@@ -13,6 +13,7 @@
 
 #include <linux/mutex.h>
 #include <linux/pci.h>
+#include <linux/irqbypass.h>
 
 #ifndef VFIO_PCI_PRIVATE_H
 #define VFIO_PCI_PRIVATE_H
@@ -29,6 +30,7 @@ struct vfio_pci_irq_ctx {
 	struct virqfd		*mask;
 	char			*name;
 	bool			masked;
+	struct irq_bypass_producer	producer;
 };
 
 struct vfio_pci_device {
Signature · 1 line
2.1.0
author
#18 lore raw

PATCH v8 7/13 KVM: x86: Update IRTE for posted-interrupts

In reply to Feng Wu

This patch adds the routine to update IRTE for posted-interrupts when guest changes the interrupt configuration.

---
v8:
- Move 'kvm_arch_update_pi_irte' to vmx.c as a callback
- Only update the PI irte when VM has assigned devices
- Add a trace point for VT-d posted-interrupts when we update or disable it for a specific irq.

 arch/x86/include/asm/kvm_host.h |  3 ++
 arch/x86/kvm/trace.h            | 33 ++++++++++++++++
 arch/x86/kvm/vmx.c              | 83 +++++++++++++++++++++++++++++++++++++++++
 arch/x86/kvm/x86.c              |  2 +
 4 files changed, 121 insertions(+)
arch/x86/include/asm/kvm_host.h +3 -0
index daa6126..8c44286 100644
@@ -862,6 +862,9 @@ struct kvm_x86_ops {
 					   gfn_t offset, unsigned long mask);
 	/* pmu operations of sub-arch */
 	const struct kvm_pmu_ops *pmu_ops;
+
+	int (*update_pi_irte)(struct kvm *kvm, unsigned int host_irq,
+			      uint32_t guest_irq, bool set);
 };
 
 struct kvm_arch_async_pf {
arch/x86/kvm/trace.h +33 -0
index 4eae7c3..539a9e4 100644
@@ -974,6 +974,39 @@ TRACE_EVENT(kvm_enter_smm,
 		  __entry->smbase)
 );
 
+/*
+ * Tracepoint for VT-d posted-interrupts.
+ */
+TRACE_EVENT(kvm_pi_irte_update,
+	TP_PROTO(unsigned int vcpu_id, unsigned int gsi,
+		 unsigned int gvec, u64 pi_desc_addr, bool set),
+	TP_ARGS(vcpu_id, gsi, gvec, pi_desc_addr, set),
+
+	TP_STRUCT__entry(
+		__field(	unsigned int,	vcpu_id		)
+		__field(	unsigned int,	gsi		)
+		__field(	unsigned int,	gvec		)
+		__field(	u64,		pi_desc_addr	)
+		__field(	bool,		set		)
+	),
+
+	TP_fast_assign(
+		__entry->vcpu_id	= vcpu_id;
+		__entry->gsi		= gsi;
+		__entry->gvec		= gvec;
+		__entry->pi_desc_addr	= pi_desc_addr;
+		__entry->set		= set;
+	),
+
+	TP_printk("VT-d PI is %s for this irq, vcpu %u, gsi: 0x%x, "
+		  "gvec: 0x%x, pi_desc_addr: 0x%llx",
+		  __entry->set ? "enabled and being updated" : "disabled",
+		  __entry->vcpu_id,
+		  __entry->gsi,
+		  __entry->gvec,
+		  __entry->pi_desc_addr)
+);
+
 #endif /* _TRACE_KVM_H */
 
 #undef TRACE_INCLUDE_PATH
arch/x86/kvm/vmx.c +83 -0

107 lines not on this page · raw message

arch/x86/kvm/x86.c +2 -0
index 5ef2560..9dcd501 100644
@@ -63,6 +63,7 @@
 #include <asm/fpu/internal.h> /* Ugh! */
 #include <asm/pvclock.h>
 #include <asm/div64.h>
+#include <asm/irq_remapping.h>
 
 #define MAX_IO_MSRS 256
 #define KVM_MAX_MCE_BANKS 32
@@ -8263,3 +8264,4 @@ EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_nested_intercepts);
 EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_write_tsc_offset);
 EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_ple_window);
 EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_pml_full);
+EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_pi_irte_update);
Signature · 1 line
2.1.0
author
#19 lore raw

PATCH v8 8/13 KVM: Implement IRQ bypass consumer callbacks for x86

In reply to Feng Wu

Implement the following callbacks for x86:

- kvm_arch_irq_bypass_add_producer
- kvm_arch_irq_bypass_del_producer
- kvm_arch_irq_bypass_stop: dummy callback
- kvm_arch_irq_bypass_resume: dummy callback

and set CONFIG_HAVE_KVM_IRQ_BYPASS for x86.

---
v8:
- Move the weak irq bypas stop and irq bypass start to this patch.
- Call kvm_x86_ops->update_pi_irte() instead of kvm_arch_update_pi_irte().

 arch/x86/include/asm/kvm_host.h |  1 +
 arch/x86/kvm/Kconfig            |  1 +
 arch/x86/kvm/x86.c              | 44 +++++++++++++++++++++++++++++++++++++++++
 virt/kvm/eventfd.c              | 12 +++++++++++
 4 files changed, 58 insertions(+)
arch/x86/include/asm/kvm_host.h +1 -0
index 8c44286..0ddd353 100644
@@ -24,6 +24,7 @@
 #include <linux/perf_event.h>
 #include <linux/pvclock_gtod.h>
 #include <linux/clocksource.h>
+#include <linux/irqbypass.h>
 
 #include <asm/pvclock-abi.h>
 #include <asm/desc.h>
arch/x86/kvm/Kconfig +1 -0
index c951d44..b90776f 100644
@@ -30,6 +30,7 @@ config KVM
 	select HAVE_KVM_IRQCHIP
 	select HAVE_KVM_IRQFD
 	select IRQ_BYPASS_MANAGER
+	select HAVE_KVM_IRQ_BYPASS
 	select HAVE_KVM_IRQ_ROUTING
 	select HAVE_KVM_EVENTFD
 	select KVM_APIC_ARCHITECTURE
arch/x86/kvm/x86.c +44 -0

56 lines not on this page · raw message

virt/kvm/eventfd.c +12 -0
index d7a230f..c0a56a1 100644
@@ -256,6 +256,18 @@ static void irqfd_update(struct kvm *kvm, struct kvm_kernel_irqfd *irqfd)
 	write_seqcount_end(&irqfd->irq_entry_sc);
 }
 
+#ifdef CONFIG_HAVE_KVM_IRQ_BYPASS
+void __attribute__((weak)) kvm_arch_irq_bypass_stop(
+				struct irq_bypass_consumer *cons)
+{
+}
+
+void __attribute__((weak)) kvm_arch_irq_bypass_start(
+				struct irq_bypass_consumer *cons)
+{
+}
+#endif
+
 static int
 kvm_irqfd_assign(struct kvm *kvm, struct kvm_irqfd *args)
 {
Signature · 1 line
2.1.0
author
#20 lore raw

PATCH v8 9/13 KVM: Add an arch specific hooks in 'struct kvm_kernel_irqfd'

In reply to Feng Wu

This patch adds an arch specific hooks 'arch_update' in 'struct kvm_kernel_irqfd'. On Intel side, it is used to update the IRTE when VT-d posted-interrupts is used.

---
v8:
- Remove callback .arch_update()
- Remove kvm_arch_irqfd_init()
- Call kvm_arch_update_irqfd_routing() instead.

 arch/x86/kvm/x86.c       |  7 +++++++
 include/linux/kvm_host.h |  2 ++
 virt/kvm/eventfd.c       | 19 ++++++++++++++++++-
 3 files changed, 27 insertions(+), 1 deletion(-)
arch/x86/kvm/x86.c +7 -0
index 79dac02..e189a94 100644
@@ -8293,6 +8293,13 @@ void kvm_arch_irq_bypass_del_producer(struct irq_bypass_consumer *cons,
 		       " fails: %d\n", irqfd->consumer.token, ret);
 }
 
+int kvm_arch_update_irqfd_routing(struct kvm *kvm, unsigned int host_irq,
+				   uint32_t guest_irq, bool set)
+{
+	return !kvm_x86_ops->update_pi_irte ? -EINVAL :
+		kvm_x86_ops->update_pi_irte(kvm, host_irq, guest_irq, set);
+}
+
 EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_exit);
 EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_inj_virq);
 EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_page_fault);
include/linux/kvm_host.h +2 -0
index 5f183fb..feba1fb 100644
@@ -1174,6 +1174,8 @@ void kvm_arch_irq_bypass_del_producer(struct irq_bypass_consumer *,
 			   struct irq_bypass_producer *);
 void kvm_arch_irq_bypass_stop(struct irq_bypass_consumer *);
 void kvm_arch_irq_bypass_start(struct irq_bypass_consumer *);
+int kvm_arch_update_irqfd_routing(struct kvm *kvm, unsigned int host_irq,
+				  uint32_t guest_irq, bool set);
 #endif /* CONFIG_HAVE_KVM_IRQ_BYPASS */
 #endif
 
virt/kvm/eventfd.c +18 -1
index c0a56a1..89c9635 100644
@@ -266,6 +266,12 @@ void __attribute__((weak)) kvm_arch_irq_bypass_start(
 				struct irq_bypass_consumer *cons)
 {
 }
+int  __attribute__((weak)) kvm_arch_update_irqfd_routing(
+				struct kvm *kvm, unsigned int host_irq,
+				uint32_t guest_irq, bool set)
+{
+	return 0;
+}
 #endif
 
 static int
@@ -582,13 +588,24 @@ kvm_irqfd_release(struct kvm *kvm)
  */
 void kvm_irq_routing_update(struct kvm *kvm)
 {
+	int ret;
 	struct kvm_kernel_irqfd *irqfd;
 
 	spin_lock_irq(&kvm->irqfds.lock);
 
-	list_for_each_entry(irqfd, &kvm->irqfds.items, list)
+	list_for_each_entry(irqfd, &kvm->irqfds.items, list) {
 		irqfd_update(kvm, irqfd);
 
+#ifdef CONFIG_HAVE_KVM_IRQ_BYPASS
+		if (irqfd->producer) {
+			ret = kvm_arch_update_irqfd_routing(
+					irqfd->kvm, irqfd->producer->irq,
+					irqfd->gsi, 1);
+			WARN_ON(ret);
+		}
+#endif
+	}
+
 	spin_unlock_irq(&kvm->irqfds.lock);
 }
 
Signature · 1 line
2.1.0
maintainer
#21 lore raw

Re: [PATCH v8 09/13] KVM: Add an arch specific hooks in 'struct kvm_kernel_irqfd'

arch/x86/kvm/x86.c void kvm_arch_irq_bypass_del_producer(struct irq_bypass_consumer *cons, · line 8302

Quoted 7 lines · Feng Wu
arch/x86/kvm/x86.c +7 -0
+int kvm_arch_update_irqfd_routing(struct kvm *kvm, unsigned int host_irq,
+				   uint32_t guest_irq, bool set)
+{
+	return !kvm_x86_ops->update_pi_irte ? -EINVAL :
+		kvm_x86_ops->update_pi_irte(kvm, host_irq, guest_irq, set);
+}
+

Just use "if" here. No need to resend if this is the only comment.

Review

Quoted 4 lines

}

(quoted diff) +2 -0
+int  __attribute__((weak)) kvm_arch_update_irqfd_routing(
+				struct kvm *kvm, unsigned

Empty line after "}".

Paolo

author
#22 lore raw

RE: [PATCH v8 09/13] KVM: Add an arch specific hooks in 'struct kvm_kernel_irqfd'

Quoted 15 lines

-----Original Message-----
From: Paolo Bonzini [mailto:[email protected]]
Sent: Wednesday, September 16, 2015 5:27 PM
To: Wu, Feng; [email protected]; [email protected]; [email protected]
Cc: [email protected]; [email protected]; iommu-cunTk1MwBs9QetFLy7KEm3xJsTq8ys+cHZ5vskTnxNA@public.gmane.org; [email protected]
Subject: Re: [PATCH v8 09/13] KVM: Add an arch specific hooks in 'struct
kvm_kernel_irqfd'

On 16/09/2015 10:50, Feng Wu wrote:

Quoted 9 lines

arch/x86/kvm/x86.c void kvm_arch_irq_bypass_del_producer(struct irq_bypass_consumer *cons, · line 8302

Quoted 7 lines
arch/x86/kvm/x86.c +7 -0
+int kvm_arch_update_irqfd_routing(struct kvm *kvm, unsigned int host_irq,
+				   uint32_t guest_irq, bool set)
+{
+	return !kvm_x86_ops->update_pi_irte ? -EINVAL :
+		kvm_x86_ops->update_pi_irte(kvm, host_irq, guest_irq, set);
+}
+

Just use "if" here. No need to resend if this is the only comment.

I am sorry, I don't quite understand. Do you mean I don't need to include this patch in v9? If so, what about other patches with your Reviewed-by?

Thanks,
Feng

Quoted 9 lines

Review

Quoted 5 lines

}

(quoted diff) +2 -0
+int  __attribute__((weak)) kvm_arch_update_irqfd_routing(
+				struct kvm *kvm, unsigned

Empty line after "}".

Paolo

maintainer
#23 lore raw

Re: [PATCH v8 09/13] KVM: Add an arch specific hooks in 'struct kvm_kernel_irqfd'

Quoted 25 lines · Feng
Quoted 15 lines

-----Original Message-----
From: Paolo Bonzini [mailto:[email protected]]
Sent: Wednesday, September 16, 2015 5:27 PM
To: Wu, Feng; [email protected]; [email protected]; [email protected]
Cc: [email protected]; [email protected]; iommu-cunTk1MwBs9QetFLy7KEm3xJsTq8ys+cHZ5vskTnxNA@public.gmane.org; [email protected]
Subject: Re: [PATCH v8 09/13] KVM: Add an arch specific hooks in 'struct
kvm_kernel_irqfd'

On 16/09/2015 10:50, Feng Wu wrote:

Quoted 9 lines

arch/x86/kvm/x86.c void kvm_arch_irq_bypass_del_producer(struct irq_bypass_consumer *cons, · line 8302

Quoted 7 lines
arch/x86/kvm/x86.c +7 -0
+int kvm_arch_update_irqfd_routing(struct kvm *kvm, unsigned int host_irq,
+				   uint32_t guest_irq, bool set)
+{
+	return !kvm_x86_ops->update_pi_irte ? -EINVAL :
+		kvm_x86_ops->update_pi_irte(kvm, host_irq, guest_irq, set);
+}
+

Just use "if" here. No need to resend if this is the only comment.

Quoted 2 lines

I am sorry, I don't quite understand. Do you mean I don't need to include this patch in v9? If so, what about other patches with your Reviewed-by?

No, I just wrote this email first. If this was the only comment, I could have fixed it up myself. But since there were a few other comments, please send v9 with all the required changes.

Thanks,

Paolo

author
#24 lore raw

PATCH v8 10/13 KVM: Update Posted-Interrupts Descriptor when vCPU is preempted

In reply to Feng Wu

This patch updates the Posted-Interrupts Descriptor when vCPU is preempted.

sched out:
- Set 'SN' to suppress furture non-urgent interrupts posted for the vCPU.

sched in:
- Clear 'SN'
- Change NDST if vCPU is scheduled to a different CPU
- Set 'NV' to POSTED_INTR_VECTOR

---
v8:
- Add two wrapper fucntion vmx_vcpu_pi_load() and vmx_vcpu_pi_put().
- Only handle VT-d PI related logic when the VM has assigned devices.

 arch/x86/kvm/vmx.c | 63 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 63 insertions(+)
arch/x86/kvm/vmx.c +63 -0
index 5a25651..5ceb280 100644
@@ -1943,6 +1943,52 @@ static void vmx_load_host_state(struct vcpu_vmx *vmx)
 	preempt_enable();
 }
 
+static void vmx_vcpu_pi_load(struct kvm_vcpu *vcpu, int cpu)
+{
+	struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
+	struct pi_desc old, new;
+	unsigned int dest;
+
+	if (!irq_remapping_cap(IRQ_POSTING_CAP) ||
+		(!kvm_arch_has_assigned_device(vcpu->kvm)))
+		return;
+
+	do {
+		old.control = new.control = pi_desc->control;
+
+		/*
+		 * If 'nv' field is POSTED_INTR_WAKEUP_VECTOR, there
+		 * are two possible cases:
+		 * 1. After running 'pi_pre_block', context switch
+		 *    happened. For this case, 'sn' was set in
+		 *    vmx_vcpu_put(), so we need to clear it here.
+		 * 2. After running 'pi_pre_block', we were blocked,
+		 *    and woken up by some other guy. For this case,
+		 *    we don't need to do anything, 'pi_post_block'
+		 *    will do everything for us. However, we cannot
+		 *    check whether it is case #1 or case #2 here
+		 *    (maybe, not needed), so we also clear sn here,
+		 *    I think it is not a big deal.
+		 */
+		if (pi_desc->nv != POSTED_INTR_WAKEUP_VECTOR) {
+			if (vcpu->cpu != cpu) {
+				dest = cpu_physical_id(cpu);
+
+				if (x2apic_enabled())
+					new.ndst = dest;
+				else
+					new.ndst = (dest << 8) & 0xFF00;
+			}
+
+			/* set 'NV' to 'notification vector' */
+			new.nv = POSTED_INTR_VECTOR;
+		}
+
+		/* Allow posting non-urgent interrupts */
+		new.sn = 0;
+	} while (cmpxchg(&pi_desc->control, old.control,
+			new.control) != old.control);
+}
 /*
  * Switches to specified vcpu, until a matching vcpu_put(), but assumes
  * vcpu mutex is already taken.
@@ -1993,10 +2039,27 @@ 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;
 	}
+
+	vmx_vcpu_pi_load(vcpu, cpu);
+}
+
+static void vmx_vcpu_pi_put(struct kvm_vcpu *vcpu)
+{
+	struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
+
+	if (!irq_remapping_cap(IRQ_POSTING_CAP) ||
+		(!kvm_arch_has_assigned_device(vcpu->kvm)))
+		return;
+
+	/* Set SN when the vCPU is preempted */
+	if (vcpu->preempted)
+		pi_set_sn(pi_desc);
 }
 
 static void vmx_vcpu_put(struct kvm_vcpu *vcpu)
 {
+	vmx_vcpu_pi_put(vcpu);
+
 	__vmx_load_host_state(to_vmx(vcpu));
 	if (!vmm_exclusive) {
 		__loaded_vmcs_clear(to_vmx(vcpu)->loaded_vmcs);
Signature · 1 line
2.1.0
maintainer
#25 lore raw

Re: [PATCH v8 10/13] KVM: Update Posted-Interrupts Descriptor when vCPU is preempted

arch/x86/kvm/vmx.c static void vmx_load_host_state(struct vcpu_vmx *vmx) · line 1955

Quoted 5 lines · Feng Wu
arch/x86/kvm/vmx.c +5 -0
+
+	if (!irq_remapping_cap(IRQ_POSTING_CAP) ||
+		(!kvm_arch_has_assigned_device(vcpu->kvm)))
+		return;
+

Better:

	if (!arch_has_assigned_device(vcpu->kvm)) ||
	    !irq_remapping_cap(IRQ_POSTING_CAP))
		return;

(In the future we might add a static_key here).

Paolo

author
#26 lore raw

PATCH v8 11/13 KVM: Update Posted-Interrupts Descriptor when vCPU is blocked

In reply to Feng Wu

This patch updates the Posted-Interrupts Descriptor when vCPU is blocked.

pre-block:
- Add the vCPU to the blocked per-CPU list
- Set 'NV' to POSTED_INTR_WAKEUP_VECTOR

post-block:
- Remove the vCPU from the per-CPU list

---
v8:
- Rename 'pi_pre_block' to 'pre_block'
- Rename 'pi_post_block' to 'post_block'
- Change some comments
- Only add the vCPU to the blocking list when the VM has assigned devices.

 arch/x86/include/asm/kvm_host.h |  13 ++++
 arch/x86/kvm/vmx.c              | 157 +++++++++++++++++++++++++++++++++++++++-
 arch/x86/kvm/x86.c              |  53 +++++++++++---
 include/linux/kvm_host.h        |   3 +
 virt/kvm/kvm_main.c             |   3 +
 5 files changed, 217 insertions(+), 12 deletions(-)
arch/x86/include/asm/kvm_host.h +13 -0
index 0ddd353..304fbb5 100644
@@ -552,6 +552,8 @@ struct kvm_vcpu_arch {
 	 */
 	bool write_fault_to_shadow_pgtable;
 
+	bool halted;
+
 	/* set at EPT violation at this point */
 	unsigned long exit_qualification;
 
@@ -864,6 +866,17 @@ struct kvm_x86_ops {
 	/* pmu operations of sub-arch */
 	const struct kvm_pmu_ops *pmu_ops;
 
+	/*
+	 * Architecture specific hooks for vCPU blocking due to
+	 * HLT instruction.
+	 * Returns for .pre_block():
+	 *    - 0 means continue to block the vCPU.
+	 *    - 1 means we cannot block the vCPU since some event
+	 *        happens during this period, such as, 'ON' bit in
+	 *        posted-interrupts descriptor is set.
+	 */
+	int (*pre_block)(struct kvm_vcpu *vcpu);
+	void (*post_block)(struct kvm_vcpu *vcpu);
 	int (*update_pi_irte)(struct kvm *kvm, unsigned int host_irq,
 			      uint32_t guest_irq, bool set);
 };
arch/x86/kvm/vmx.c +155 -2

201 lines not on this page · raw message

arch/x86/kvm/x86.c +43 -10

79 lines not on this page · raw message

include/linux/kvm_host.h +3 -0
index feba1fb..bf462e7 100644
@@ -231,6 +231,9 @@ struct kvm_vcpu {
 	unsigned long requests;
 	unsigned long guest_debug;
 
+	int pre_pcpu;
+	struct list_head blocked_vcpu_list;
+
 	struct mutex mutex;
 	struct kvm_run *run;
 
virt/kvm/kvm_main.c +3 -0
index 8b8a444..191c7eb 100644
@@ -220,6 +220,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->pre_pcpu = -1;
+	INIT_LIST_HEAD(&vcpu->blocked_vcpu_list);
+
 	page = alloc_page(GFP_KERNEL | __GFP_ZERO);
 	if (!page) {
 		r = -ENOMEM;
Signature · 1 line
2.1.0
maintainer
#27 lore raw

Re: [PATCH v8 11/13] KVM: Update Posted-Interrupts Descriptor when vCPU is blocked

arch/x86/kvm/vmx.c static void vmx_vcpu_pi_load(struct kvm_vcpu *vcpu, int cpu) · line 1969

Quoted 3 lines · Feng Wu
arch/x86/kvm/vmx.c +1 -1
 		 * are two possible cases:
-		 * 1. After running 'pi_pre_block', context switch
+		 * 1. After running 'pre_block', context switch

Please fold this in the previous patch.

arch/x86/kvm/vmx.c static void vmx_vcpu_pi_load(struct kvm_vcpu *vcpu, int cpu) · line 1973

Quoted 5 lines
arch/x86/kvm/vmx.c +1 -1
 		 *    happened. For this case, 'sn' was set in
 		 *    vmx_vcpu_put(), so we need to clear it here.
-		 * 2. After running 'pi_pre_block', we were blocked,
+		 * 2. After running 'pre_block', we were blocked,
 		 *    and woken up by some other guy. For this case,

(Same).

arch/x86/kvm/vmx.c static void update_ple_window_actual_max(void) · line 6134

Quoted 10 lines
arch/x86/kvm/vmx.c +10 -0
+	spin_lock(&per_cpu(blocked_vcpu_on_cpu_lock, cpu));
+	list_for_each_entry(vcpu, &per_cpu(blocked_vcpu_on_cpu, cpu),
+			blocked_vcpu_list) {
+		struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
+
+		if (pi_test_on(pi_desc) == 1)
+			kvm_vcpu_kick(vcpu);
+	}
+	spin_unlock(&per_cpu(blocked_vcpu_on_cpu_lock, cpu));
+}

Please document the lock in Documentation/virtual/kvm/locking.txt.

Paolo

author
#28 lore raw

PATCH v8 12/13 KVM: Warn if 'SN' is set during posting interrupts by software

In reply to Feng Wu

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

If the vcpu is in guest mode, it cannot have been scheduled out, and that's the only case when SN is set currently, warning if SN is set.

---
 arch/x86/kvm/vmx.c | 16 ++++++++++++++++
 1 file changed, 16 insertions(+)
arch/x86/kvm/vmx.c +16 -0
index 9888c43..58fbbc6 100644
@@ -4498,6 +4498,22 @@ static inline bool kvm_vcpu_trigger_posted_interrupt(struct kvm_vcpu *vcpu)
 {
 #ifdef CONFIG_SMP
 	if (vcpu->mode == IN_GUEST_MODE) {
+		struct vcpu_vmx *vmx = to_vmx(vcpu);
+
+		/*
+		 * Currently, we don't support urgent interrupt,
+		 * all interrupts are recognized as non-urgent
+		 * interrupt, so we cannot post interrupts when
+		 * 'SN' is set.
+		 *
+		 * If the vcpu is in guest mode, it means it is
+		 * running instead of being scheduled out and
+		 * waiting in the run queue, and that's the only
+		 * case when 'SN' is set currently, warning if
+		 * 'SN' is set.
+		 */
+		WARN_ON_ONCE(pi_test_sn(&vmx->pi_desc));
+
 		apic->send_IPI_mask(get_cpu_mask(vcpu->cpu),
 				POSTED_INTR_VECTOR);
 		return true;
Signature · 1 line
2.1.0
maintainer
#29 lore raw

Re: [PATCH v8 12/13] KVM: Warn if 'SN' is set during posting interrupts by software

Quoted 10 lines · Feng Wu

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

If the vcpu is in guest mode, it cannot have been scheduled out, and that's the only case when SN is set currently, warning if SN is set.

Please fold this into patch 10.

Paolo

Quoted 32 lines
---
 arch/x86/kvm/vmx.c | 16 ++++++++++++++++
 1 file changed, 16 insertions(+)
arch/x86/kvm/vmx.c +16 -0
index 9888c43..58fbbc6 100644
@@ -4498,6 +4498,22 @@ static inline bool kvm_vcpu_trigger_posted_interrupt(struct kvm_vcpu *vcpu)
 {
 #ifdef CONFIG_SMP
 	if (vcpu->mode == IN_GUEST_MODE) {
+		struct vcpu_vmx *vmx = to_vmx(vcpu);
+
+		/*
+		 * Currently, we don't support urgent interrupt,
+		 * all interrupts are recognized as non-urgent
+		 * interrupt, so we cannot post interrupts when
+		 * 'SN' is set.
+		 *
+		 * If the vcpu is in guest mode, it means it is
+		 * running instead of being scheduled out and
+		 * waiting in the run queue, and that's the only
+		 * case when 'SN' is set currently, warning if
+		 * 'SN' is set.
+		 */
+		WARN_ON_ONCE(pi_test_sn(&vmx->pi_desc));
+
 		apic->send_IPI_mask(get_cpu_mask(vcpu->cpu),
 				POSTED_INTR_VECTOR);
 		return true;
author
#30 lore raw

PATCH v8 13/13 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 1d6f045..52aca36 100644
@@ -1547,6 +1547,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 2d99930..d8c3997 100644
@@ -22,7 +22,7 @@ int irq_remap_broken;
 int disable_sourceid_checking;
 int no_x2apic_optout;
 
-int disable_irq_post = 1;
+int disable_irq_post = 0;
 
 static int disable_irq_remap;
 static struct irq_remap_ops *remap_ops;
@@ -58,14 +58,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
2.1.0
maintainer
#31 lore raw
In reply to Feng Wu
Quoted 10 lines · Feng Wu

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

v8:
refer to the changelog in each patch

Thanks, it mostly looks good.

Since we've more or less converged, could you post the whole series for v9, including the other prerequisite series?

Paolo