Skip to contentSkip to conversation

KVM: X86: Filter out the broadcast dest for IPI fastpath

PATCH Superseded Wanpeng Li 4 participants 8 messages Apr 1, 2020 – Apr 2, 2020

Subsystems (3)

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Wanpeng Li

author
#0 lore raw

PATCH v2 1/2 KVM: X86: Filter out the broadcast dest for IPI fastpath

From: Wanpeng Li <[email protected]>

Except destination shorthand, a destination value 0xffffffff is used to broadcast interrupts, let's also filter out this for single target IPI fastpath.

---
v1 -> v2:
* update subject and patch description

 arch/x86/kvm/lapic.c | 3 ---
 arch/x86/kvm/lapic.h | 3 +++
 arch/x86/kvm/x86.c   | 3 ++-
 3 files changed, 5 insertions(+), 4 deletions(-)
arch/x86/kvm/lapic.c +0 -3
index e24d405..d528bed 100644
@@ -59,9 +59,6 @@
 #define MAX_APIC_VECTOR			256
 #define APIC_VECTORS_PER_REG		32
 
-#define APIC_BROADCAST			0xFF
-#define X2APIC_BROADCAST		0xFFFFFFFFul
-
 static bool lapic_timer_advance_dynamic __read_mostly;
 #define LAPIC_TIMER_ADVANCE_ADJUST_MIN	100	/* clock cycles */
 #define LAPIC_TIMER_ADVANCE_ADJUST_MAX	10000	/* clock cycles */
arch/x86/kvm/lapic.h +3 -0
index bc76860..25b77a6 100644
@@ -17,6 +17,9 @@
 #define APIC_BUS_CYCLE_NS       1
 #define APIC_BUS_FREQUENCY      (1000000000ULL / APIC_BUS_CYCLE_NS)
 
+#define APIC_BROADCAST			0xFF
+#define X2APIC_BROADCAST		0xFFFFFFFFul
+
 enum lapic_mode {
 	LAPIC_MODE_DISABLED = 0,
 	LAPIC_MODE_INVALID = X2APIC_ENABLE,
arch/x86/kvm/x86.c +2 -1
index 5e95950..5a645df 100644
@@ -1559,7 +1559,8 @@ static int handle_fastpath_set_x2apic_icr_irqoff(struct kvm_vcpu *vcpu, u64 data
 
 	if (((data & APIC_SHORT_MASK) == APIC_DEST_NOSHORT) &&
 		((data & APIC_DEST_MASK) == APIC_DEST_PHYSICAL) &&
-		((data & APIC_MODE_MASK) == APIC_DM_FIXED)) {
+		((data & APIC_MODE_MASK) == APIC_DM_FIXED) &&
+		((u32)(data >> 32) != X2APIC_BROADCAST)) {
 
 		data &= ~(1 << 12);
 		kvm_apic_send_ipi(vcpu->arch.apic, (u32)data, (u32)(data >> 32));
Signature · 1 line
2.7.4
author
#1 lore raw

PATCH v2 2/2 KVM: LAPIC: Don't need to clear IPI delivery status in x2apic mode

From: Wanpeng Li <[email protected]>

IPI delivery status field is not present in x2apic mode, don't need to clear IPI delivery status in x2apic mode.

---
v1 -> v2:
* update code comments
* update subject and patch description

 arch/x86/kvm/lapic.c | 5 +++--
 arch/x86/kvm/x86.c   | 1 -
 2 files changed, 3 insertions(+), 3 deletions(-)
arch/x86/kvm/lapic.c +3 -2
index d528bed..5efca58 100644
@@ -1941,8 +1941,9 @@ int kvm_lapic_reg_write(struct kvm_lapic *apic, u32 reg, u32 val)
 		break;
 	}
 	case APIC_ICR:
-		/* No delay here, so we always clear the pending bit */
-		val &= ~(1 << 12);
+		/* Immediately clear Delivery Status in xAPIC mode */
+		if (!apic_x2apic_mode(apic))
+			val &= ~(1 << 12);
 		kvm_apic_send_ipi(apic, val, kvm_lapic_get_reg(apic, APIC_ICR2));
 		kvm_lapic_set_reg(apic, APIC_ICR, val);
 		break;
arch/x86/kvm/x86.c +0 -1
index 5a645df..ececc09 100644
@@ -1562,7 +1562,6 @@ static int handle_fastpath_set_x2apic_icr_irqoff(struct kvm_vcpu *vcpu, u64 data
 		((data & APIC_MODE_MASK) == APIC_DM_FIXED) &&
 		((u32)(data >> 32) != X2APIC_BROADCAST)) {
 
-		data &= ~(1 << 12);
 		kvm_apic_send_ipi(vcpu->arch.apic, (u32)data, (u32)(data >> 32));
 		kvm_lapic_set_reg(vcpu->arch.apic, APIC_ICR2, (u32)(data >> 32));
 		kvm_lapic_set_reg(vcpu->arch.apic, APIC_ICR, (u32)data);
Signature · 1 line
2.7.4
maintainer
#2 lore raw

Re: [PATCH v2 2/2] KVM: LAPIC: Don't need to clear IPI delivery status in x2apic mode

arch/x86/kvm/lapic.c int kvm_lapic_reg_write(struct kvm_lapic *apic, u32 reg, u32 val) · line 1946

Quoted 5 lines · Wanpeng Li
arch/x86/kvm/lapic.c +3 -2
-		/* No delay here, so we always clear the pending bit */
-		val &= ~(1 << 12);
+		/* Immediately clear Delivery Status in xAPIC mode */
+		if (!apic_x2apic_mode(apic))
+			val &= ~(1 << 12);

This adds a conditional, and the old behavior was valid according to the
SDM: "software should not assume the value returned by reading the ICR is the last written value".

Paolo

author
#3 lore raw

Re: [PATCH v2 2/2] KVM: LAPIC: Don't need to clear IPI delivery status in x2apic mode

Cc more people,

Quoted 11 lines · Paolo Bonzini

arch/x86/kvm/lapic.c int kvm_lapic_reg_write(struct kvm_lapic *apic, u32 reg, u32 val) · line 1946

Quoted 5 lines · Wanpeng Li
arch/x86/kvm/lapic.c +3 -2
-             /* No delay here, so we always clear the pending bit */
-             val &= ~(1 << 12);
+             /* Immediately clear Delivery Status in xAPIC mode */
+             if (!apic_x2apic_mode(apic))
+                     val &= ~(1 << 12);

This adds a conditional, and the old behavior was valid according to the
SDM: "software should not assume the value returned by reading the ICR is the last written value".

Nadav, Sean, what do you think?

Wanpeng

#4 lore raw

Re: [PATCH v2 2/2] KVM: LAPIC: Don't need to clear IPI delivery status in x2apic mode

Quoted 11 lines · Wanpeng Li

Cc more people,

Quoted 7 lines · Paolo Bonzini
Quoted 5 lines · Wanpeng Li
arch/x86/kvm/lapic.c +3 -2
-             /* No delay here, so we always clear the pending bit */
-             val &= ~(1 << 12);
+             /* Immediately clear Delivery Status in xAPIC mode */
+             if (!apic_x2apic_mode(apic))
+                     val &= ~(1 << 12);
Quoted 5 lines
Quoted 3 lines

This adds a conditional, and the old behavior was valid according to the
SDM: "software should not assume the value returned by reading the ICR is the last written value".

Nadav, Sean, what do you think?

I do not know. But if you write a KVM unit-test, I can run it on bare-metal and give you feedback about how it behaves.

#5 lore raw

Re: [PATCH v2 2/2] KVM: LAPIC: Don't need to clear IPI delivery status in x2apic mode

Quoted 11 lines · Nadav Amit
Quoted 11 lines · Wanpeng Li

Cc more people,

Quoted 7 lines · Paolo Bonzini
Quoted 5 lines · Wanpeng Li
arch/x86/kvm/lapic.c +3 -2
-             /* No delay here, so we always clear the pending bit */
-             val &= ~(1 << 12);
+             /* Immediately clear Delivery Status in xAPIC mode */
+             if (!apic_x2apic_mode(apic))
+                     val &= ~(1 << 12);
Quoted 8 lines
Quoted 5 lines
Quoted 3 lines

This adds a conditional, and the old behavior was valid according to the
SDM: "software should not assume the value returned by reading the ICR is the last written value".

Nadav, Sean, what do you think?

I do not know. But if you write a KVM unit-test, I can run it on bare-metal and give you feedback about how it behaves.

I agree with Paolo, clearing the bit doesn't violate the SDM. The conditional is just as costly as the AND, if not more so, even for x2APIC.

I would play it safe and clear the bit even in the x2APIC only path to avoid tripping up guest kernels that loop on the delivery status even when using x2APIC.

author
#6 lore raw

Re: [PATCH v2 2/2] KVM: LAPIC: Don't need to clear IPI delivery status in x2apic mode

Quoted 22 lines · Sean Christopherson
Quoted 11 lines · Nadav Amit
Quoted 11 lines · Wanpeng Li

Cc more people,

Quoted 7 lines · Paolo Bonzini
Quoted 5 lines · Wanpeng Li
arch/x86/kvm/lapic.c +3 -2
-             /* No delay here, so we always clear the pending bit */
-             val &= ~(1 << 12);
+             /* Immediately clear Delivery Status in xAPIC mode */
+             if (!apic_x2apic_mode(apic))
+                     val &= ~(1 << 12);
Quoted 8 lines
Quoted 5 lines
Quoted 3 lines

This adds a conditional, and the old behavior was valid according to the
SDM: "software should not assume the value returned by reading the ICR is the last written value".

Nadav, Sean, what do you think?

I do not know. But if you write a KVM unit-test, I can run it on bare-metal and give you feedback about how it behaves.

Quoted 6 lines

I agree with Paolo, clearing the bit doesn't violate the SDM. The conditional is just as costly as the AND, if not more so, even for x2APIC.

I would play it safe and clear the bit even in the x2APIC only path to avoid tripping up guest kernels that loop on the delivery status even when using x2APIC.

Fair enough.

Wanpeng

author
#7 lore raw

Re: [PATCH v2 2/2] KVM: LAPIC: Don't need to clear IPI delivery status in x2apic mode

In reply to Paolo Bonzini
Quoted 11 lines · Paolo Bonzini

arch/x86/kvm/lapic.c int kvm_lapic_reg_write(struct kvm_lapic *apic, u32 reg, u32 val) · line 1946

Quoted 5 lines · Wanpeng Li
arch/x86/kvm/lapic.c +3 -2
-             /* No delay here, so we always clear the pending bit */
-             val &= ~(1 << 12);
+             /* Immediately clear Delivery Status in xAPIC mode */
+             if (!apic_x2apic_mode(apic))
+                     val &= ~(1 << 12);

This adds a conditional, and the old behavior was valid according to the
SDM: "software should not assume the value returned by reading the ICR is the last written value".

We can queue patch 1/2 separately to catch the merge window. :)

Wanpeng