From: Tomoki Sekiyama <tomoki.sekiyama.qu@hitachi.com>
To: kvm@vger.kernel.org
Cc: linux-kernel@vger.kernel.org, x86@kernel.org,
yrl.pp-manager.tt@hitachi.com,
Tomoki Sekiyama <tomoki.sekiyama.qu@hitachi.com>,
Avi Kivity <avi@redhat.com>,
Marcelo Tosatti <mtosatti@redhat.com>,
Thomas Gleixner <tglx@linutronix.de>,
Ingo Molnar <mingo@redhat.com>, "H. Peter Anvin" <hpa@zytor.com>
Subject: [RFC v2 PATCH 18/21] KVM: route assigned devices' MSI/MSI-X directly to guests on slave CPUs
Date: Thu, 06 Sep 2012 20:28:53 +0900 [thread overview]
Message-ID: <20120906112853.13320.56851.stgit@kvmdev> (raw)
In-Reply-To: <20120906112718.13320.8231.stgit@kvmdev>
When a PCI device is assigned to a guest running on slave CPUs, this
routes the device's MSI/MSI-X interrupts directly to the guest.
Because the guest uses a different interrupt vector from the host,
vector remapping is required. This is safe because slave CPUs only handles
interrupts for the assigned guest.
The slave CPU may receive interrupts for the guest while the guest is not
running. In that case, the host IRQ handler is invoked and the interrupt is
transfered as vIRQ.
If the guest receive the direct interrupt from the devices, EOI to physical
APIC is required. To handle this, if the guest issues EOI when there are no
in-service interrupts in the virtual APIC, physical EOI is issued.
Signed-off-by: Tomoki Sekiyama <tomoki.sekiyama.qu@hitachi.com>
Cc: Avi Kivity <avi@redhat.com>
Cc: Marcelo Tosatti <mtosatti@redhat.com>
Cc: Thomas Gleixner <tglx@linutronix.de>
Cc: Ingo Molnar <mingo@redhat.com>
Cc: "H. Peter Anvin" <hpa@zytor.com>
---
arch/x86/include/asm/kvm_host.h | 19 +++++
arch/x86/kvm/irq.c | 136 +++++++++++++++++++++++++++++++++++++++
arch/x86/kvm/lapic.c | 6 +-
arch/x86/kvm/x86.c | 12 +++
virt/kvm/assigned-dev.c | 8 ++
5 files changed, 179 insertions(+), 2 deletions(-)
diff --git a/arch/x86/include/asm/kvm_host.h b/arch/x86/include/asm/kvm_host.h
index 624e5ad..f43680e 100644
--- a/arch/x86/include/asm/kvm_host.h
+++ b/arch/x86/include/asm/kvm_host.h
@@ -1033,4 +1033,23 @@ void kvm_deliver_pmi(struct kvm_vcpu *vcpu);
int kvm_arch_vcpu_run_prevented(struct kvm_vcpu *vcpu);
+#ifdef CONFIG_SLAVE_CPU
+void kvm_get_slave_cpu_mask(struct kvm *kvm, struct cpumask *mask);
+
+struct kvm_assigned_dev_kernel;
+extern void assign_slave_msi(struct kvm *kvm,
+ struct kvm_assigned_dev_kernel *assigned_dev);
+extern void deassign_slave_msi(struct kvm *kvm,
+ struct kvm_assigned_dev_kernel *assigned_dev);
+extern void assign_slave_msix(struct kvm *kvm,
+ struct kvm_assigned_dev_kernel *assigned_dev);
+extern void deassign_slave_msix(struct kvm *kvm,
+ struct kvm_assigned_dev_kernel *assigned_dev);
+#else
+#define assign_slave_msi(kvm, assigned_dev)
+#define deassign_slave_msi(kvm, assigned_dev)
+#define assign_slave_msix(kvm, assigned_dev)
+#define deassign_slave_msix(kvm, assigned_dev)
+#endif
+
#endif /* _ASM_X86_KVM_HOST_H */
diff --git a/arch/x86/kvm/irq.c b/arch/x86/kvm/irq.c
index 7e06ba1..128431a 100644
--- a/arch/x86/kvm/irq.c
+++ b/arch/x86/kvm/irq.c
@@ -22,6 +22,8 @@
#include <linux/module.h>
#include <linux/kvm_host.h>
+#include <linux/pci.h>
+#include <asm/msidef.h>
#include "irq.h"
#include "i8254.h"
@@ -94,3 +96,137 @@ void __kvm_migrate_timers(struct kvm_vcpu *vcpu)
__kvm_migrate_apic_timer(vcpu);
__kvm_migrate_pit_timer(vcpu);
}
+
+
+#ifdef CONFIG_SLAVE_CPU
+
+static int kvm_lookup_msi_routing_entry(struct kvm *kvm, int irq)
+{
+ int vec = -1;
+ struct kvm_irq_routing_table *irq_rt;
+ struct kvm_kernel_irq_routing_entry *e;
+ struct hlist_node *n;
+
+ rcu_read_lock();
+ irq_rt = rcu_dereference(kvm->irq_routing);
+ if (irq < irq_rt->nr_rt_entries)
+ hlist_for_each_entry(e, n, &irq_rt->map[irq], link)
+ if (e->type == KVM_IRQ_ROUTING_MSI)
+ vec = (e->msi.data & MSI_DATA_VECTOR_MASK)
+ >> MSI_DATA_VECTOR_SHIFT;
+ rcu_read_unlock();
+
+ return vec;
+}
+
+void assign_slave_msi(struct kvm *kvm,
+ struct kvm_assigned_dev_kernel *assigned_dev)
+{
+ int irq = assigned_dev->guest_irq;
+ int host_irq = assigned_dev->host_irq;
+ struct irq_data *data = irq_get_irq_data(host_irq);
+ int vec = kvm_lookup_msi_routing_entry(kvm, irq);
+ cpumask_var_t slave_mask;
+ char buffer[16];
+
+ BUG_ON(!data);
+
+ if (!zalloc_cpumask_var(&slave_mask, GFP_KERNEL)) {
+ pr_err("assign slave MSI failed: no memory\n");
+ return;
+ }
+ kvm_get_slave_cpu_mask(kvm, slave_mask);
+
+ bitmap_scnprintf(buffer, sizeof(buffer), cpu_slave_mask->bits, 32);
+ pr_info("assigned_device slave msi: irq:%d host:%d vec:%d mask:%s\n",
+ irq, host_irq, vec, buffer);
+
+ remap_slave_vector_irq(host_irq, vec, slave_mask);
+ data->chip->irq_set_affinity(data, slave_mask, 1);
+
+ free_cpumask_var(slave_mask);
+}
+
+void deassign_slave_msi(struct kvm *kvm,
+ struct kvm_assigned_dev_kernel *assigned_dev)
+{
+ int host_irq = assigned_dev->host_irq;
+ cpumask_var_t slave_mask;
+ char buffer[16];
+
+ if (!zalloc_cpumask_var(&slave_mask, GFP_KERNEL)) {
+ pr_err("deassign slave MSI failed: no memory\n");
+ return;
+ }
+ kvm_get_slave_cpu_mask(kvm, slave_mask);
+
+ bitmap_scnprintf(buffer, sizeof(buffer), cpu_slave_mask->bits, 32);
+ pr_info("deassigned_device slave msi: host:%d mask:%s\n",
+ host_irq, buffer);
+
+ revert_slave_vector_irq(host_irq, slave_mask);
+
+ free_cpumask_var(slave_mask);
+}
+
+void assign_slave_msix(struct kvm *kvm,
+ struct kvm_assigned_dev_kernel *assigned_dev)
+{
+ int i;
+
+ for (i = 0; i < assigned_dev->entries_nr; i++) {
+ int irq = assigned_dev->guest_msix_entries[i].vector;
+ int host_irq = assigned_dev->host_msix_entries[i].vector;
+ struct irq_data *data = irq_get_irq_data(host_irq);
+ int vec = kvm_lookup_msi_routing_entry(kvm, irq);
+ cpumask_var_t slave_mask;
+ char buffer[16];
+
+ pr_info("assign_slave_msix: %d %d %x\n", irq, host_irq, vec);
+ BUG_ON(!data);
+
+ if (!zalloc_cpumask_var(&slave_mask, GFP_KERNEL)) {
+ pr_err("assign slave MSI-X failed: no memory\n");
+ return;
+ }
+ kvm_get_slave_cpu_mask(kvm, slave_mask);
+
+ bitmap_scnprintf(buffer, sizeof(buffer), cpu_slave_mask->bits,
+ 32);
+ pr_info("assigned_device slave msi-x: irq:%d host:%d vec:%d mask:%s\n",
+ irq, host_irq, vec, buffer);
+
+ remap_slave_vector_irq(host_irq, vec, slave_mask);
+ data->chip->irq_set_affinity(data, slave_mask, 1);
+
+ free_cpumask_var(slave_mask);
+ }
+}
+
+void deassign_slave_msix(struct kvm *kvm,
+ struct kvm_assigned_dev_kernel *assigned_dev)
+{
+ int i;
+
+ for (i = 0; i < assigned_dev->entries_nr; i++) {
+ int host_irq = assigned_dev->host_msix_entries[i].vector;
+ cpumask_var_t slave_mask;
+ char buffer[16];
+
+ if (!zalloc_cpumask_var(&slave_mask, GFP_KERNEL)) {
+ pr_err("deassign slave MSI failed: no memory\n");
+ return;
+ }
+ kvm_get_slave_cpu_mask(kvm, slave_mask);
+
+ bitmap_scnprintf(buffer, sizeof(buffer), cpu_slave_mask->bits, 32);
+ pr_info("deassigned_device slave msi: host:%d mask:%s\n",
+ host_irq, buffer);
+
+ revert_slave_vector_irq(host_irq, slave_mask);
+
+ free_cpumask_var(slave_mask);
+ }
+}
+
+#endif
diff --git a/arch/x86/kvm/lapic.c b/arch/x86/kvm/lapic.c
index 73f57f3..bf8e351 100644
--- a/arch/x86/kvm/lapic.c
+++ b/arch/x86/kvm/lapic.c
@@ -585,8 +585,12 @@ static int apic_set_eoi(struct kvm_lapic *apic)
* Not every write EOI will has corresponding ISR,
* one example is when Kernel check timer on setup_IO_APIC
*/
- if (vector == -1)
+ if (vector == -1) {
+ /* On slave cpu, it can be EOI for a direct interrupt */
+ if (cpu_slave(smp_processor_id()))
+ ack_APIC_irq();
return vector;
+ }
apic_clear_isr(vector, apic);
apic_update_ppr(apic);
diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c
index eb2e5c4..609ab62 100644
--- a/arch/x86/kvm/x86.c
+++ b/arch/x86/kvm/x86.c
@@ -5349,6 +5349,17 @@ static void process_nmi(struct kvm_vcpu *vcpu)
}
#ifdef CONFIG_SLAVE_CPU
+
+void kvm_get_slave_cpu_mask(struct kvm *kvm, struct cpumask *mask)
+{
+ int i;
+ struct kvm_vcpu *vcpu;
+
+ kvm_for_each_vcpu(i, vcpu, kvm)
+ if (vcpu_has_slave_cpu(vcpu))
+ cpumask_set_cpu(vcpu->arch.slave_cpu, mask);
+}
+
static int kvm_arch_kicked_by_nmi(unsigned int cmd, struct pt_regs *regs)
{
struct kvm_vcpu *vcpu;
@@ -5372,6 +5383,7 @@ static int kvm_arch_kicked_by_nmi(unsigned int cmd, struct pt_regs *regs)
kvm_arch_vcpu_prevent_run(vcpu, 1);
return NMI_HANDLED;
}
+
#endif
enum vcpu_enter_guest_slave_retval {
diff --git a/virt/kvm/assigned-dev.c b/virt/kvm/assigned-dev.c
index 23a41a9..a3acc67 100644
--- a/virt/kvm/assigned-dev.c
+++ b/virt/kvm/assigned-dev.c
@@ -225,8 +225,12 @@ static void deassign_host_irq(struct kvm *kvm,
free_irq(assigned_dev->host_irq, assigned_dev);
- if (assigned_dev->irq_requested_type & KVM_DEV_IRQ_HOST_MSI)
+ if (assigned_dev->irq_requested_type & KVM_DEV_IRQ_HOST_MSI) {
pci_disable_msi(assigned_dev->dev);
+ deassign_slave_msi(kvm, assigned_dev);
+ }
+ if (assigned_dev->irq_requested_type & KVM_DEV_IRQ_HOST_MSIX)
+ deassign_slave_msix(kvm, assigned_dev);
}
assigned_dev->irq_requested_type &= ~(KVM_DEV_IRQ_HOST_MASK);
@@ -417,6 +421,7 @@ static int assigned_device_enable_guest_msi(struct kvm *kvm,
{
dev->guest_irq = irq->guest_irq;
dev->ack_notifier.gsi = -1;
+ assign_slave_msi(kvm, dev);
return 0;
}
#endif
@@ -428,6 +433,7 @@ static int assigned_device_enable_guest_msix(struct kvm *kvm,
{
dev->guest_irq = irq->guest_irq;
dev->ack_notifier.gsi = -1;
+ assign_slave_msix(kvm, dev);
return 0;
}
#endif
next prev parent reply other threads:[~2012-09-06 11:34 UTC|newest]
Thread overview: 29+ messages / expand[flat|nested] mbox.gz Atom feed top
2012-09-06 11:27 [RFC v2 PATCH 00/21] KVM: x86: CPU isolation and direct interrupts delivery to guests Tomoki Sekiyama
2012-09-06 11:27 ` [RFC v2 PATCH 01/21] x86: Split memory hotplug function from cpu_up() as cpu_memory_up() Tomoki Sekiyama
2012-09-06 11:31 ` Avi Kivity
2012-09-06 11:32 ` Avi Kivity
2012-09-06 11:27 ` [RFC v2 PATCH 02/21] x86: Add a facility to use offlined CPUs as slave CPUs Tomoki Sekiyama
2012-09-06 11:27 ` [RFC v2 PATCH 03/21] x86: Support hrtimer on " Tomoki Sekiyama
2012-09-06 11:27 ` [RFC v2 PATCH 04/21] x86: Avoid RCU warnings " Tomoki Sekiyama
2012-09-20 17:34 ` Paul E. McKenney
2012-09-28 8:10 ` Tomoki Sekiyama
2012-09-06 11:27 ` [RFC v2 PATCH 05/21] KVM: Enable/Disable virtualization on slave CPUs are activated/dying Tomoki Sekiyama
2012-09-06 11:27 ` [RFC v2 PATCH 06/21] KVM: Add facility to run guests on slave CPUs Tomoki Sekiyama
2012-09-06 11:27 ` [RFC v2 PATCH 07/21] KVM: handle page faults of slave guests on online CPUs Tomoki Sekiyama
2012-09-06 11:28 ` [RFC v2 PATCH 08/21] KVM: Add KVM_GET_SLAVE_CPU and KVM_SET_SLAVE_CPU to vCPU ioctl Tomoki Sekiyama
2012-09-06 11:28 ` [RFC v2 PATCH 09/21] KVM: Go back to online CPU on VM exit by external interrupt Tomoki Sekiyama
2012-09-06 11:28 ` [RFC v2 PATCH 10/21] KVM: proxy slab operations for slave CPUs on online CPUs Tomoki Sekiyama
2012-09-06 11:28 ` [RFC v2 PATCH 11/21] KVM: no exiting from guest when slave CPU halted Tomoki Sekiyama
2012-09-06 11:28 ` [RFC v2 PATCH 12/21] x86/apic: Enable external interrupt routing to slave CPUs Tomoki Sekiyama
2012-09-06 11:28 ` [RFC v2 PATCH 13/21] x86/apic: IRQ vector remapping on slave for " Tomoki Sekiyama
2012-09-06 11:28 ` [RFC v2 PATCH 14/21] KVM: Directly handle interrupts by guests without VM EXIT on " Tomoki Sekiyama
2012-09-06 11:28 ` [RFC v2 PATCH 15/21] KVM: add tracepoint on enabling/disabling direct interrupt delivery Tomoki Sekiyama
2012-09-06 11:28 ` [RFC v2 PATCH 16/21] KVM: vmx: Add definitions PIN_BASED_PREEMPTION_TIMER Tomoki Sekiyama
2012-09-06 11:28 ` [RFC v2 PATCH 17/21] KVM: add kvm_arch_vcpu_prevent_run to prevent VM ENTER when NMI is received Tomoki Sekiyama
2012-09-06 11:28 ` Tomoki Sekiyama [this message]
2012-09-06 11:28 ` [RFC v2 PATCH 19/21] KVM: Enable direct EOI for directly routed interrupts to guests Tomoki Sekiyama
2012-09-06 11:29 ` [RFC v2 PATCH 20/21] KVM: Pass-through local APIC timer of on slave CPUs to guest VM Tomoki Sekiyama
2012-09-06 11:29 ` [RFC v2 PATCH 21/21] x86: request TLB flush to slave CPU using NMI Tomoki Sekiyama
2012-09-06 11:46 ` [RFC v2 PATCH 00/21] KVM: x86: CPU isolation and direct interrupts delivery to guests Avi Kivity
2012-09-07 8:26 ` Jan Kiszka
2012-09-10 11:36 ` Tomoki Sekiyama
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20120906112853.13320.56851.stgit@kvmdev \
--to=tomoki.sekiyama.qu@hitachi.com \
--cc=avi@redhat.com \
--cc=hpa@zytor.com \
--cc=kvm@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=mingo@redhat.com \
--cc=mtosatti@redhat.com \
--cc=tglx@linutronix.de \
--cc=x86@kernel.org \
--cc=yrl.pp-manager.tt@hitachi.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox