linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: "Radim Krčmář" <rkrcmar@redhat.com>
To: linux-kernel@vger.kernel.org, kvm@vger.kernel.org
Cc: Paolo Bonzini <pbonzini@redhat.com>,
	"Lan, Tianyu" <tianyu.lan@intel.com>,
	Igor Mammedov <imammedo@redhat.com>,
	Jan Kiszka <jan.kiszka@web.de>, Peter Xu <peterx@redhat.com>,
	Yang Zhang <yang.zhang.wz@gmail.com>
Subject: [PATCH v3 12/14] KVM: x86: add a flag to disable KVM x2apic broadcast quirk
Date: Tue, 12 Jul 2016 22:09:28 +0200	[thread overview]
Message-ID: <20160712200930.32143-13-rkrcmar@redhat.com> (raw)
In-Reply-To: <20160712200930.32143-1-rkrcmar@redhat.com>

Add KVM_X2APIC_API_DISABLE_BROADCAST_QUIRK as a feature flag to
KVM_CAP_X2APIC_API.

The quirk made KVM interpret 0xff as a broadcast even in x2APIC mode.
The enableable capability is needed in order to support standard x2APIC and
remain backward compatible.

Signed-off-by: Radim Krčmář <rkrcmar@redhat.com>
---
 v3: new

 Documentation/virtual/kvm/api.txt |  5 +++++
 arch/x86/include/asm/kvm_host.h   |  1 +
 arch/x86/kvm/lapic.c              | 40 +++++++++++++++++++++++++++++----------
 arch/x86/kvm/x86.c                |  5 ++++-
 include/uapi/linux/kvm.h          |  1 +
 5 files changed, 41 insertions(+), 11 deletions(-)

diff --git a/Documentation/virtual/kvm/api.txt b/Documentation/virtual/kvm/api.txt
index 10e2bf903e57..c2753ff4a499 100644
--- a/Documentation/virtual/kvm/api.txt
+++ b/Documentation/virtual/kvm/api.txt
@@ -3843,12 +3843,17 @@ Returns: 0 on success, -EINVAL when args[0] contains invalid features
 Valid feature flags in args[0] are
 
 #define KVM_X2APIC_API_USE_32BIT_FORMAT         (1ULL << 0)
+#define KVM_X2APIC_API_DISABLE_BROADCAST_QUIRK  (1ULL << 1)
 
 Enabling KVM_X2APIC_API_USE_32BIT_FORMAT changes the behavior of
 KVM_SET_GSI_ROUTING, KVM_SIGNAL_MSI, KVM_SET_LAPIC, and KVM_GET_LAPIC, allowing
 the use of 32-bit APIC IDs.  See KVM_CAP_X2APIC_API in their respective
 sections.
 
+KVM_X2APIC_API_DISABLE_BROADCAST_QUIRK must be enabled for x2APIC to work in
+logical mode or with more than 255 VCPUs.  A KVM quirk treats 0xff as a
+broadcast even in x2APIC mode (a consequence of allowing physical x2APIC
+without interrupt remapping).
 
 
 8. Other capabilities.
diff --git a/arch/x86/include/asm/kvm_host.h b/arch/x86/include/asm/kvm_host.h
index f13522f85a1d..25bf9d627c15 100644
--- a/arch/x86/include/asm/kvm_host.h
+++ b/arch/x86/include/asm/kvm_host.h
@@ -784,6 +784,7 @@ struct kvm_arch {
 	struct page *avic_physical_id_table_page;
 
 	bool x2apic_format;
+	bool x2apic_broadcast_quirk_disabled;
 };
 
 struct kvm_vm_stat {
diff --git a/arch/x86/kvm/lapic.c b/arch/x86/kvm/lapic.c
index d27a7829a4ce..46f1d1c93420 100644
--- a/arch/x86/kvm/lapic.c
+++ b/arch/x86/kvm/lapic.c
@@ -619,14 +619,17 @@ static bool kvm_apic_match_logical_addr(struct kvm_lapic *apic, u32 mda)
 /* KVM APIC implementation has two quirks
  *  - dest always begins at 0 while xAPIC MDA has offset 24,
  *  - IOxAPIC messages have to be delivered (directly) to x2APIC.
+ *
+ * The broadcast quirk can be disabled with KVM_CAP_X2APIC_API.
  */
-static u32 kvm_apic_mda(unsigned int dest_id, struct kvm_lapic *source,
-                                              struct kvm_lapic *target)
+static u32 kvm_apic_mda(struct kvm_vcpu *vcpu, 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)
+	if (!vcpu->kvm->arch.x2apic_broadcast_quirk_disabled &&
+	    !ipi && dest_id == APIC_BROADCAST && x2apic_mda)
 		return X2APIC_BROADCAST;
 
 	return x2apic_mda ? dest_id : SET_APIC_DEST_FIELD(dest_id);
@@ -636,7 +639,7 @@ bool kvm_apic_match_dest(struct kvm_vcpu *vcpu, struct kvm_lapic *source,
 			   int short_hand, unsigned int dest, int dest_mode)
 {
 	struct kvm_lapic *target = vcpu->arch.apic;
-	u32 mda = kvm_apic_mda(dest, source, target);
+	u32 mda = kvm_apic_mda(vcpu, dest, source, target);
 
 	apic_debug("target %p, source %p, dest 0x%x, "
 		   "dest_mode 0x%x, short_hand 0x%x\n",
@@ -688,6 +691,28 @@ static void kvm_apic_disabled_lapic_found(struct kvm *kvm)
 	}
 }
 
+static bool kvm_apic_map_need_slowpath(struct kvm *kvm, struct kvm_lapic **src,
+		struct kvm_lapic_irq *irq, struct kvm_apic_map *map)
+{
+	if (!map)
+		return true;
+
+	if (kvm->arch.x2apic_broadcast_quirk_disabled) {
+		if ((irq->dest_id == APIC_BROADCAST &&
+				map->mode != KVM_APIC_MODE_X2APIC))
+			return true;
+		if (irq->dest_id == X2APIC_BROADCAST)
+			return true;
+	} else {
+		bool x2apic_ipi = src && *src && apic_x2apic_mode(*src);
+		if (irq->dest_id == (x2apic_ipi ?
+		                     X2APIC_BROADCAST : APIC_BROADCAST))
+			return true;
+	}
+
+	return false;
+}
+
 /* Return true if the interrupt can be handled by using *bitmap as index mask
  * for valid destinations in *dst array.
  * Return false if kvm_apic_map_get_dest_lapic did nothing useful.
@@ -701,7 +726,6 @@ static inline bool kvm_apic_map_get_dest_lapic(struct kvm *kvm,
 		unsigned long *bitmap)
 {
 	int i, lowest;
-	bool x2apic_ipi;
 
 	if (irq->shorthand == APIC_DEST_SELF && src) {
 		*dst = src;
@@ -710,11 +734,7 @@ static inline bool kvm_apic_map_get_dest_lapic(struct kvm *kvm,
 	} else if (irq->shorthand)
 		return false;
 
-	x2apic_ipi = src && *src && apic_x2apic_mode(*src);
-	if (irq->dest_id == (x2apic_ipi ? X2APIC_BROADCAST : APIC_BROADCAST))
-		return false;
-
-	if (!map)
+	if (kvm_apic_map_need_slowpath(kvm, src, irq, map))
 		return false;
 
 	if (irq->dest_mode == APIC_DEST_PHYSICAL) {
diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c
index 330276315daa..4bab85f82a96 100644
--- a/arch/x86/kvm/x86.c
+++ b/arch/x86/kvm/x86.c
@@ -90,7 +90,8 @@ static u64 __read_mostly efer_reserved_bits = ~((u64)EFER_SCE);
 #define VM_STAT(x) offsetof(struct kvm, stat.x), KVM_STAT_VM
 #define VCPU_STAT(x) offsetof(struct kvm_vcpu, stat.x), KVM_STAT_VCPU
 
-#define KVM_X2APIC_API_VALID_FLAGS (KVM_X2APIC_API_USE_32BIT_FORMAT)
+#define KVM_X2APIC_API_VALID_FLAGS (KVM_X2APIC_API_USE_32BIT_FORMAT | \
+                                    KVM_X2APIC_API_DISABLE_BROADCAST_QUIRK)
 
 static void update_cr8_intercept(struct kvm_vcpu *vcpu);
 static void process_nmi(struct kvm_vcpu *vcpu);
@@ -3811,6 +3812,8 @@ split_irqchip_unlock:
 
 		if (cap->args[0] & KVM_X2APIC_API_USE_32BIT_FORMAT)
 			kvm->arch.x2apic_format = true;
+		if (cap->args[0] & KVM_X2APIC_API_DISABLE_BROADCAST_QUIRK)
+			kvm->arch.x2apic_broadcast_quirk_disabled = true;
 
 		r = 0;
 		break;
diff --git a/include/uapi/linux/kvm.h b/include/uapi/linux/kvm.h
index b6de3febabaa..13b30dfd93b2 100644
--- a/include/uapi/linux/kvm.h
+++ b/include/uapi/linux/kvm.h
@@ -1315,5 +1315,6 @@ struct kvm_assigned_msix_entry {
 };
 
 #define KVM_X2APIC_API_USE_32BIT_FORMAT         (1ULL << 0)
+#define KVM_X2APIC_API_DISABLE_BROADCAST_QUIRK  (1ULL << 1)
 
 #endif /* __LINUX_KVM_H */
-- 
2.9.0

  parent reply	other threads:[~2016-07-12 20:11 UTC|newest]

Thread overview: 32+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-07-12 20:09 [PATCH v3 00/14] KVM: x86: break the xAPIC barrier Radim Krčmář
2016-07-12 20:09 ` [PATCH v3 01/14] KVM: x86: bump KVM_SOFT_MAX_VCPUS to 240 Radim Krčmář
2016-07-12 20:09 ` [PATCH v3 02/14] KVM: x86: add kvm_apic_map_get_dest_lapic Radim Krčmář
2016-07-12 20:09 ` [PATCH v3 03/14] KVM: x86: use physical LAPIC array for logical x2APIC Radim Krčmář
2016-08-02 13:46   ` Wanpeng Li
2016-08-02 14:22     ` Radim Krčmář
2016-08-02 16:40       ` Nadav Amit
2016-07-12 20:09 ` [PATCH v3 04/14] KVM: x86: dynamic kvm_apic_map Radim Krčmář
2016-07-13  8:29   ` Paolo Bonzini
2016-07-13 14:37     ` Radim Krčmář
2016-08-02 11:39   ` Wanpeng Li
2016-08-02 12:22     ` Radim Krčmář
2016-08-02 13:15       ` Wanpeng Li
2016-07-12 20:09 ` [PATCH v3 05/14] KVM: x86: use generic function for MSI parsing Radim Krčmář
2016-07-12 20:09 ` [PATCH v3 06/14] KVM: x86: use hardware-compatible format for APIC ID register Radim Krčmář
2016-07-12 20:09 ` [PATCH v3 07/14] KVM: x86: reset APIC ID when enabling LAPIC Radim Krčmář
2016-08-02 13:26   ` Wanpeng Li
2016-08-02 13:52     ` Radim Krčmář
2016-07-12 20:09 ` [PATCH v3 08/14] KVM: VMX: optimize APIC ID read with APICv Radim Krčmář
2016-07-12 20:09 ` [PATCH v3 09/14] KVM: x86: reset lapic base in kvm_lapic_reset Radim Krčmář
2016-07-12 20:09 ` [PATCH v3 10/14] KVM: pass struct kvm to kvm_set_routing_entry Radim Krčmář
2016-07-12 20:09 ` [PATCH v3 11/14] KVM: x86: add KVM_CAP_X2APIC_API Radim Krčmář
2016-07-13  8:41   ` Paolo Bonzini
2016-07-13 14:40     ` Radim Krčmář
2016-07-12 20:09 ` Radim Krčmář [this message]
2016-07-13  8:38   ` [PATCH v3 12/14] KVM: x86: add a flag to disable KVM x2apic broadcast quirk Paolo Bonzini
2016-07-13 15:14     ` Radim Krčmář
2016-07-13 15:30       ` Paolo Bonzini
2016-07-13 10:15   ` Paolo Bonzini
2016-07-13 14:52     ` Radim Krčmář
2016-07-12 20:09 ` [PATCH v3 13/14] KVM: x86: bump MAX_VCPUS to 288 Radim Krčmář
2016-07-12 20:09 ` [PATCH v3 14/14] KVM: x86: bump KVM_MAX_VCPU_ID to 1023 Radim Krčmář

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=20160712200930.32143-13-rkrcmar@redhat.com \
    --to=rkrcmar@redhat.com \
    --cc=imammedo@redhat.com \
    --cc=jan.kiszka@web.de \
    --cc=kvm@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=pbonzini@redhat.com \
    --cc=peterx@redhat.com \
    --cc=tianyu.lan@intel.com \
    --cc=yang.zhang.wz@gmail.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;
as well as URLs for NNTP newsgroup(s).