Skip to contentSkip to conversation

Use eoi to track RTC interrupt delivery status

PATCH Superseded Yang Zhang 2 participants 22 messages Apr 1, 2013 – Apr 8, 2013

Subsystems (3)

v7 of 10 Superseded Latest is v10 →

lore
Share & tools
lore

Yang Zhang

author
#0 lore raw

v7 cover Use eoi to track RTC interrupt delivery status

From: Yang Zhang <[email protected]>

Current interrupt coalescing logci which only used by RTC has conflict with Posted Interrupt.

This patch introduces a new mechinism to use eoi to track interrupt:
When delivering an interrupt to vcpu, the pending_eoi set to number of vcpu that received the interrupt. And decrease it when each vcpu writing eoi. No subsequent RTC interrupt can deliver to vcpu until all vcpus write eoi.

Changes from v6 to v7
* Only track the RTC interrupt when userspace uses *_LINE_* ioctl.
* Call rtc_irq_restore() after lapic is restored.
* Rebase on top of KVM.

Changes from v5 to v6
* Move set dest_map logic into __apic_accept_irq().
* Use RTC_GSI to distinguish different platform, and drop all CONFIG_X86.
* Rebase on top of KVM.

Changes from v4 to v5
* Calculate destination vcpu on interrupt injection not hook into ioapic modification.
* Rebase on top of KVM.

Yang Zhang (7):
KVM: Add vcpu info to ioapic_update_eoi()
KVM: Introduce struct rtc_status
KVM : Return destination vcpu on interrupt injection
KVM: Add reset/restore rtc_status support KVM : Force vmexit with virtual interrupt delivery
KVM: Let ioapic know the irq line status
KVM: Use eoi to track RTC interrupt delivery status

 arch/x86/kvm/i8254.c     |    4 +-
 arch/x86/kvm/lapic.c     |   36 ++++++++++----
 arch/x86/kvm/lapic.h     |    7 ++-
 arch/x86/kvm/x86.c       |    6 ++-
 include/linux/kvm_host.h |   11 +++--
 virt/kvm/assigned-dev.c  |   13 +++--
 virt/kvm/eventfd.c       |   15 ++++--
 virt/kvm/ioapic.c        |  115 +++++++++++++++++++++++++++++++++++++++------
 virt/kvm/ioapic.h        |   20 +++++++-
 virt/kvm/irq_comm.c      |   31 +++++++-----
 virt/kvm/kvm_main.c      |    3 +-
 11 files changed, 196 insertions(+), 65 deletions(-)
author
#1 lore raw

PATCH v7 1/7 KVM: Add vcpu info to ioapic_update_eoi()

From: Yang Zhang <[email protected]>

Add vcpu info to ioapic_update_eoi, so we can know which vcpu issued this EOI.

---
 arch/x86/kvm/lapic.c |    2 +-
 virt/kvm/ioapic.c    |   12 ++++++------
 virt/kvm/ioapic.h    |    3 ++-
 3 files changed, 9 insertions(+), 8 deletions(-)
arch/x86/kvm/lapic.c +1 -1
index a8e9369..d3e322a 100644
@@ -786,7 +786,7 @@ static void kvm_ioapic_send_eoi(struct kvm_lapic *apic, int vector)
 			trigger_mode = IOAPIC_LEVEL_TRIG;
 		else
 			trigger_mode = IOAPIC_EDGE_TRIG;
-		kvm_ioapic_update_eoi(apic->vcpu->kvm, vector, trigger_mode);
+		kvm_ioapic_update_eoi(apic->vcpu, vector, trigger_mode);
 	}
 }
 
virt/kvm/ioapic.c +6 -6
index 5ba005c..9379386 100644
@@ -267,8 +267,8 @@ void kvm_ioapic_clear_all(struct kvm_ioapic *ioapic, int irq_source_id)
 	spin_unlock(&ioapic->lock);
 }
 
-static void __kvm_ioapic_update_eoi(struct kvm_ioapic *ioapic, int vector,
-				     int trigger_mode)
+static void __kvm_ioapic_update_eoi(struct kvm_vcpu *vcpu,
+			struct kvm_ioapic *ioapic, int vector, int trigger_mode)
 {
 	int i;
 
@@ -307,12 +307,12 @@ bool kvm_ioapic_handles_vector(struct kvm *kvm, int vector)
 	return test_bit(vector, ioapic->handled_vectors);
 }
 
-void kvm_ioapic_update_eoi(struct kvm *kvm, int vector, int trigger_mode)
+void kvm_ioapic_update_eoi(struct kvm_vcpu *vcpu, int vector, int trigger_mode)
 {
-	struct kvm_ioapic *ioapic = kvm->arch.vioapic;
+	struct kvm_ioapic *ioapic = vcpu->kvm->arch.vioapic;
 
 	spin_lock(&ioapic->lock);
-	__kvm_ioapic_update_eoi(ioapic, vector, trigger_mode);
+	__kvm_ioapic_update_eoi(vcpu, ioapic, vector, trigger_mode);
 	spin_unlock(&ioapic->lock);
 }
 
@@ -410,7 +410,7 @@ static int ioapic_mmio_write(struct kvm_io_device *this, gpa_t addr, int len,
 		break;
 #ifdef	CONFIG_IA64
 	case IOAPIC_REG_EOI:
-		__kvm_ioapic_update_eoi(ioapic, data, IOAPIC_LEVEL_TRIG);
+		__kvm_ioapic_update_eoi(NULL, ioapic, data, IOAPIC_LEVEL_TRIG);
 		break;
 #endif
 
virt/kvm/ioapic.h +2 -1
index 0400a46..2fc61a5 100644
@@ -70,7 +70,8 @@ static inline struct kvm_ioapic *ioapic_irqchip(struct kvm *kvm)
 int kvm_apic_match_dest(struct kvm_vcpu *vcpu, struct kvm_lapic *source,
 		int short_hand, int dest, int dest_mode);
 int kvm_apic_compare_prio(struct kvm_vcpu *vcpu1, struct kvm_vcpu *vcpu2);
-void kvm_ioapic_update_eoi(struct kvm *kvm, int vector, int trigger_mode);
+void kvm_ioapic_update_eoi(struct kvm_vcpu *vcpu, int vector,
+			int trigger_mode);
 bool kvm_ioapic_handles_vector(struct kvm *kvm, int vector);
 int kvm_ioapic_init(struct kvm *kvm);
 void kvm_ioapic_destroy(struct kvm *kvm);
Signature · 1 line
1.7.1
author
#2 lore raw

PATCH v7 2/7 KVM: Introduce struct rtc_status

In reply to Yang Zhang

From: Yang Zhang <[email protected]>

rtc_status is used to track RTC interrupt delivery status. The pending_eoi will be increased by vcpu who received RTC interrupt and will be decreased when EOI to this interrupt.
Also, we use dest_map to record the destination vcpu to avoid the case that vcpu who didn't get the RTC interupt, but issued EOI with same vector of RTC and descreased pending_eoi by mistake.

---
 virt/kvm/ioapic.h |   12 ++++++++++++
 1 files changed, 12 insertions(+), 0 deletions(-)
virt/kvm/ioapic.h +12 -0
index 2fc61a5..87cd94b 100644
@@ -34,6 +34,17 @@ struct kvm_vcpu;
 #define	IOAPIC_INIT			0x5
 #define	IOAPIC_EXTINT			0x7
 
+#ifdef CONFIG_X86
+#define RTC_GSI 8
+#else
+#define RTC_GSI -1U
+#endif
+
+struct rtc_status {
+	int pending_eoi;
+	DECLARE_BITMAP(dest_map, KVM_MAX_VCPUS);
+};
+
 struct kvm_ioapic {
 	u64 base_address;
 	u32 ioregsel;
@@ -47,6 +58,7 @@ struct kvm_ioapic {
 	void (*ack_notifier)(void *opaque, int irq);
 	spinlock_t lock;
 	DECLARE_BITMAP(handled_vectors, 256);
+	struct rtc_status rtc_status;
 };
 
 #ifdef DEBUG
Signature · 1 line
1.7.1
author
#3 lore raw

PATCH v7 3/7 KVM : Return destination vcpu on interrupt injection

In reply to Yang Zhang

From: Yang Zhang <[email protected]>

Add a new parameter to know vcpus who received the interrupt.

---
 arch/x86/kvm/lapic.c |   25 ++++++++++++++++---------
 arch/x86/kvm/lapic.h |    5 +++--
 virt/kvm/ioapic.c    |    2 +-
 virt/kvm/ioapic.h    |    2 +-
 virt/kvm/irq_comm.c  |   12 ++++++------
 5 files changed, 27 insertions(+), 19 deletions(-)
arch/x86/kvm/lapic.c +16 -9
index d3e322a..96ab160 100644
@@ -431,14 +431,16 @@ int kvm_lapic_find_highest_irr(struct kvm_vcpu *vcpu)
 }
 
 static int __apic_accept_irq(struct kvm_lapic *apic, int delivery_mode,
-			     int vector, int level, int trig_mode);
+			     int vector, int level, int trig_mode,
+			     unsigned long *dest_map);
 
-int kvm_apic_set_irq(struct kvm_vcpu *vcpu, struct kvm_lapic_irq *irq)
+int kvm_apic_set_irq(struct kvm_vcpu *vcpu, struct kvm_lapic_irq *irq,
+		unsigned long *dest_map)
 {
 	struct kvm_lapic *apic = vcpu->arch.apic;
 
 	return __apic_accept_irq(apic, irq->delivery_mode, irq->vector,
-			irq->level, irq->trig_mode);
+			irq->level, irq->trig_mode, dest_map);
 }
 
 static int pv_eoi_put_user(struct kvm_vcpu *vcpu, u8 val)
@@ -611,7 +613,7 @@ int kvm_apic_match_dest(struct kvm_vcpu *vcpu, struct kvm_lapic *source,
 }
 
 bool kvm_irq_delivery_to_apic_fast(struct kvm *kvm, struct kvm_lapic *src,
-		struct kvm_lapic_irq *irq, int *r)
+		struct kvm_lapic_irq *irq, int *r, unsigned long *dest_map)
 {
 	struct kvm_apic_map *map;
 	unsigned long bitmap = 1;
@@ -622,7 +624,7 @@ bool kvm_irq_delivery_to_apic_fast(struct kvm *kvm, struct kvm_lapic *src,
 	*r = -1;
 
 	if (irq->shorthand == APIC_DEST_SELF) {
-		*r = kvm_apic_set_irq(src->vcpu, irq);
+		*r = kvm_apic_set_irq(src->vcpu, irq, dest_map);
 		return true;
 	}
 
@@ -667,7 +669,7 @@ bool kvm_irq_delivery_to_apic_fast(struct kvm *kvm, struct kvm_lapic *src,
 			continue;
 		if (*r < 0)
 			*r = 0;
-		*r += kvm_apic_set_irq(dst[i]->vcpu, irq);
+		*r += kvm_apic_set_irq(dst[i]->vcpu, irq, dest_map);
 	}
 
 	ret = true;
@@ -681,7 +683,8 @@ out:
  * Return 1 if successfully added and 0 if discarded.
  */
 static int __apic_accept_irq(struct kvm_lapic *apic, int delivery_mode,
-			     int vector, int level, int trig_mode)
+			     int vector, int level, int trig_mode,
+			     unsigned long *dest_map)
 {
 	int result = 0;
 	struct kvm_vcpu *vcpu = apic->vcpu;
@@ -694,6 +697,9 @@ static int __apic_accept_irq(struct kvm_lapic *apic, int delivery_mode,
 		if (unlikely(!apic_enabled(apic)))
 			break;
 
+		if (dest_map)
+			__set_bit(vcpu->vcpu_id, dest_map);
+
 		if (trig_mode) {
 			apic_debug("level trig mode for vector %d", vector);
 			apic_set_vector(vector, apic->regs + APIC_TMR);
@@ -852,7 +858,7 @@ static void apic_send_ipi(struct kvm_lapic *apic)
 		   irq.trig_mode, irq.level, irq.dest_mode, irq.delivery_mode,
 		   irq.vector);
 
-	kvm_irq_delivery_to_apic(apic->vcpu->kvm, apic, &irq);
+	kvm_irq_delivery_to_apic(apic->vcpu->kvm, apic, &irq, NULL);
 }
 
 static u32 apic_get_tmcct(struct kvm_lapic *apic)
@@ -1488,7 +1494,8 @@ int kvm_apic_local_deliver(struct kvm_lapic *apic, int lvt_type)
 		vector = reg & APIC_VECTOR_MASK;
 		mode = reg & APIC_MODE_MASK;
 		trig_mode = reg & APIC_LVT_LEVEL_TRIGGER;
-		return __apic_accept_irq(apic, mode, vector, 1, trig_mode);
+		return __apic_accept_irq(apic, mode, vector, 1, trig_mode,
+					NULL);
 	}
 	return 0;
 }
arch/x86/kvm/lapic.h +3 -2
index 2c721b9..967519c 100644
@@ -55,11 +55,12 @@ void kvm_apic_set_version(struct kvm_vcpu *vcpu);
 
 int kvm_apic_match_physical_addr(struct kvm_lapic *apic, u16 dest);
 int kvm_apic_match_logical_addr(struct kvm_lapic *apic, u8 mda);
-int kvm_apic_set_irq(struct kvm_vcpu *vcpu, struct kvm_lapic_irq *irq);
+int kvm_apic_set_irq(struct kvm_vcpu *vcpu, struct kvm_lapic_irq *irq,
+		unsigned long *dest_map);
 int kvm_apic_local_deliver(struct kvm_lapic *apic, int lvt_type);
 
 bool kvm_irq_delivery_to_apic_fast(struct kvm *kvm, struct kvm_lapic *src,
-		struct kvm_lapic_irq *irq, int *r);
+		struct kvm_lapic_irq *irq, int *r, unsigned long *dest_map);
 
 u64 kvm_get_apic_base(struct kvm_vcpu *vcpu);
 void kvm_set_apic_base(struct kvm_vcpu *vcpu, u64 data);
virt/kvm/ioapic.c +1 -1
index 9379386..8664812 100644
@@ -220,7 +220,7 @@ static int ioapic_deliver(struct kvm_ioapic *ioapic, int irq)
 	irqe.level = 1;
 	irqe.shorthand = 0;
 
-	return kvm_irq_delivery_to_apic(ioapic->kvm, NULL, &irqe);
+	return kvm_irq_delivery_to_apic(ioapic->kvm, NULL, &irqe, NULL);
 }
 
 int kvm_ioapic_set_irq(struct kvm_ioapic *ioapic, int irq, int irq_source_id,
virt/kvm/ioapic.h +1 -1
index 87cd94b..761e5b5 100644
@@ -92,7 +92,7 @@ int kvm_ioapic_set_irq(struct kvm_ioapic *ioapic, int irq, int irq_source_id,
 void kvm_ioapic_clear_all(struct kvm_ioapic *ioapic, int irq_source_id);
 void kvm_ioapic_reset(struct kvm_ioapic *ioapic);
 int kvm_irq_delivery_to_apic(struct kvm *kvm, struct kvm_lapic *src,
-		struct kvm_lapic_irq *irq);
+		struct kvm_lapic_irq *irq, unsigned long *dest_map);
 int kvm_get_ioapic(struct kvm *kvm, struct kvm_ioapic_state *state);
 int kvm_set_ioapic(struct kvm *kvm, struct kvm_ioapic_state *state);
 void kvm_ioapic_make_eoibitmap_request(struct kvm *kvm);
virt/kvm/irq_comm.c +6 -6
index e9073cf..2f07d2e 100644
@@ -63,7 +63,7 @@ inline static bool kvm_is_dm_lowest_prio(struct kvm_lapic_irq *irq)
 }
 
 int kvm_irq_delivery_to_apic(struct kvm *kvm, struct kvm_lapic *src,
-		struct kvm_lapic_irq *irq)
+		struct kvm_lapic_irq *irq, unsigned long *dest_map)
 {
 	int i, r = -1;
 	struct kvm_vcpu *vcpu, *lowest = NULL;
@@ -74,7 +74,7 @@ int kvm_irq_delivery_to_apic(struct kvm *kvm, struct kvm_lapic *src,
 		irq->delivery_mode = APIC_DM_FIXED;
 	}
 
-	if (kvm_irq_delivery_to_apic_fast(kvm, src, irq, &r))
+	if (kvm_irq_delivery_to_apic_fast(kvm, src, irq, &r, dest_map))
 		return r;
 
 	kvm_for_each_vcpu(i, vcpu, kvm) {
@@ -88,7 +88,7 @@ int kvm_irq_delivery_to_apic(struct kvm *kvm, struct kvm_lapic *src,
 		if (!kvm_is_dm_lowest_prio(irq)) {
 			if (r < 0)
 				r = 0;
-			r += kvm_apic_set_irq(vcpu, irq);
+			r += kvm_apic_set_irq(vcpu, irq, dest_map);
 		} else if (kvm_lapic_enabled(vcpu)) {
 			if (!lowest)
 				lowest = vcpu;
@@ -98,7 +98,7 @@ int kvm_irq_delivery_to_apic(struct kvm *kvm, struct kvm_lapic *src,
 	}
 
 	if (lowest)
-		r = kvm_apic_set_irq(lowest, irq);
+		r = kvm_apic_set_irq(lowest, irq, dest_map);
 
 	return r;
 }
@@ -130,7 +130,7 @@ int kvm_set_msi(struct kvm_kernel_irq_routing_entry *e,
 
 	kvm_set_msi_irq(e, &irq);
 
-	return kvm_irq_delivery_to_apic(kvm, NULL, &irq);
+	return kvm_irq_delivery_to_apic(kvm, NULL, &irq, NULL);
 }
 
 
@@ -142,7 +142,7 @@ static int kvm_set_msi_inatomic(struct kvm_kernel_irq_routing_entry *e,
 
 	kvm_set_msi_irq(e, &irq);
 
-	if (kvm_irq_delivery_to_apic_fast(kvm, NULL, &irq, &r))
+	if (kvm_irq_delivery_to_apic_fast(kvm, NULL, &irq, &r, NULL))
 		return r;
 	else
 		return -EWOULDBLOCK;
Signature · 1 line
1.7.1
author
#4 lore raw

PATCH v7 4/7 KVM: Add reset/restore rtc_status support

In reply to Yang Zhang

From: Yang Zhang <[email protected]>

---
 arch/x86/kvm/lapic.c |    9 +++++++++
 arch/x86/kvm/lapic.h |    2 ++
 virt/kvm/ioapic.c    |   43 +++++++++++++++++++++++++++++++++++++++++++
 virt/kvm/ioapic.h    |    1 +
 4 files changed, 55 insertions(+), 0 deletions(-)
arch/x86/kvm/lapic.c +9 -0
index 96ab160..9c041fa 100644
@@ -94,6 +94,14 @@ static inline int apic_test_vector(int vec, void *bitmap)
 	return test_bit(VEC_POS(vec), (bitmap) + REG_POS(vec));
 }
 
+bool kvm_apic_pending_eoi(struct kvm_vcpu *vcpu, int vector)
+{
+	struct kvm_lapic *apic = vcpu->arch.apic;
+
+	return apic_test_vector(vector, apic->regs + APIC_ISR) ||
+		apic_test_vector(vector, apic->regs + APIC_IRR);
+}
+
 static inline void apic_set_vector(int vec, void *bitmap)
 {
 	set_bit(VEC_POS(vec), (bitmap) + REG_POS(vec));
@@ -1665,6 +1673,7 @@ void kvm_apic_post_state_restore(struct kvm_vcpu *vcpu,
 	apic->highest_isr_cache = -1;
 	kvm_x86_ops->hwapic_isr_update(vcpu->kvm, apic_find_highest_isr(apic));
 	kvm_make_request(KVM_REQ_EVENT, vcpu);
+	kvm_rtc_irq_restore(vcpu);
 }
 
 void __kvm_migrate_apic_timer(struct kvm_vcpu *vcpu)
arch/x86/kvm/lapic.h +2 -0
index 967519c..004d2ad 100644
@@ -170,4 +170,6 @@ static inline bool kvm_apic_has_events(struct kvm_vcpu *vcpu)
 	return vcpu->arch.apic->pending_events;
 }
 
+bool kvm_apic_pending_eoi(struct kvm_vcpu *vcpu, int vector);
+
 #endif
virt/kvm/ioapic.c +43 -0

61 lines not on this page · raw message

virt/kvm/ioapic.h +1 -0
index 761e5b5..2692873 100644
@@ -79,6 +79,7 @@ static inline struct kvm_ioapic *ioapic_irqchip(struct kvm *kvm)
 	return kvm->arch.vioapic;
 }
 
+void kvm_rtc_irq_restore(struct kvm_vcpu *vcpu);
 int kvm_apic_match_dest(struct kvm_vcpu *vcpu, struct kvm_lapic *source,
 		int short_hand, int dest, int dest_mode);
 int kvm_apic_compare_prio(struct kvm_vcpu *vcpu1, struct kvm_vcpu *vcpu2);
Signature · 1 line
1.7.1
#5 lore raw

Re: [PATCH v7 4/7] KVM: Add reset/restore rtc_status support

Quoted 65 lines · Yang Zhang

From: Yang Zhang <[email protected]>

---
 arch/x86/kvm/lapic.c |    9 +++++++++
 arch/x86/kvm/lapic.h |    2 ++
 virt/kvm/ioapic.c    |   43 +++++++++++++++++++++++++++++++++++++++++++
 virt/kvm/ioapic.h    |    1 +
 4 files changed, 55 insertions(+), 0 deletions(-)
arch/x86/kvm/lapic.c +9 -0
index 96ab160..9c041fa 100644
@@ -94,6 +94,14 @@ static inline int apic_test_vector(int vec, void *bitmap)
 	return test_bit(VEC_POS(vec), (bitmap) + REG_POS(vec));
 }
 
+bool kvm_apic_pending_eoi(struct kvm_vcpu *vcpu, int vector)
+{
+	struct kvm_lapic *apic = vcpu->arch.apic;
+
+	return apic_test_vector(vector, apic->regs + APIC_ISR) ||
+		apic_test_vector(vector, apic->regs + APIC_IRR);
+}
+
 static inline void apic_set_vector(int vec, void *bitmap)
 {
 	set_bit(VEC_POS(vec), (bitmap) + REG_POS(vec));
@@ -1665,6 +1673,7 @@ void kvm_apic_post_state_restore(struct kvm_vcpu *vcpu,
 	apic->highest_isr_cache = -1;
 	kvm_x86_ops->hwapic_isr_update(vcpu->kvm, apic_find_highest_isr(apic));
 	kvm_make_request(KVM_REQ_EVENT, vcpu);
+	kvm_rtc_irq_restore(vcpu);
 }
 
 void __kvm_migrate_apic_timer(struct kvm_vcpu *vcpu)
arch/x86/kvm/lapic.h +2 -0
index 967519c..004d2ad 100644
@@ -170,4 +170,6 @@ static inline bool kvm_apic_has_events(struct kvm_vcpu *vcpu)
 	return vcpu->arch.apic->pending_events;
 }
 
+bool kvm_apic_pending_eoi(struct kvm_vcpu *vcpu, int vector);
+
 #endif
virt/kvm/ioapic.c +9 -0
index 8664812..0b12b17 100644
@@ -90,6 +90,47 @@ static unsigned long ioapic_read_indirect(struct kvm_ioapic *ioapic,
 	return result;
 }
 
+static void rtc_irq_reset(struct kvm_ioapic *ioapic)
+{
+	ioapic->rtc_status.pending_eoi = 0;
+	bitmap_zero(ioapic->rtc_status.dest_map, KVM_MAX_VCPUS);
+}
+
+static void rtc_irq_restore(struct kvm_ioapic *ioapic)
+{
+	struct kvm_vcpu *vcpu;

virt/kvm/ioapic.c static unsigned long ioapic_read_indirect(struct kvm_ioapic *ioapic, · line 111

Quoted 10 lines
virt/kvm/ioapic.c +10 -0
+	int vector, i, pending_eoi = 0;
+
+	if (RTC_GSI >= IOAPIC_NUM_PINS)
+		return;
+
+	vector = ioapic->redirtbl[RTC_GSI].fields.vector;
+	kvm_for_each_vcpu(i, vcpu, ioapic->kvm) {
+		if (kvm_apic_pending_eoi(vcpu, vector)) {
+			pending_eoi++;
+			__set_bit(vcpu->vcpu_id, ioapic->rtc_status.dest_map);

You should cleat dest_map at the beginning to get rid of stale bits.

virt/kvm/ioapic.c static unsigned long ioapic_read_indirect(struct kvm_ioapic *ioapic, · line 124

Quoted 13 lines
virt/kvm/ioapic.c +13 -0
+		}
+	}
+	ioapic->rtc_status.pending_eoi = pending_eoi;
+}
+
+void kvm_rtc_irq_restore(struct kvm_vcpu *vcpu)
+{
+	struct kvm_ioapic *ioapic = vcpu->kvm->arch.vioapic;
+	int vector;
+
+	if (!ioapic)
+		return;
+

Can this be called if ioapic == NULL?
Should check for if (RTC_GSI >= IOAPIC_NUM_PINS) here too.

virt/kvm/ioapic.c static unsigned long ioapic_read_indirect(struct kvm_ioapic *ioapic, · line 129

Quoted 5 lines
virt/kvm/ioapic.c +5 -0
+	spin_lock(&ioapic->lock);
+	vector = ioapic->redirtbl[RTC_GSI].fields.vector;
+	if (kvm_apic_pending_eoi(vcpu, vector)) {
+		__set_bit(vcpu->vcpu_id, ioapic->rtc_status.dest_map);
+		ioapic->rtc_status.pending_eoi++;

The bit may have been set already. The logic should be:

new_val = kvm_apic_pending_eoi(vcpu, vector)
old_val = set_bit(vcpu_id, dest_map)

if (new_val == old_val)

	return;
if (new_val) {
	__set_bit(vcpu_id, dest_map);
	pending_eoi++;
} else {
	__clear_bit(vcpu_id, dest_map);
	pending_eoi--;
}

The naming of above two functions are not good either. Call them something like kvm_rtc_eoi_tracking_restore_all() and kvm_rtc_eoi_tracking_restore_one(). And _all should call _one() for each vcpu. Make __rtc_irq_eoi_tracking_restore_one() that does not take ioapic lock and call it from kvm_rtc_eoi_tracking_restore_one() surrounded by locks.

--
Gleb.

#6 lore raw

RE: [PATCH v7 4/7] KVM: Add reset/restore rtc_status support

Gleb Natapov wrote on 2013-04-04:

Quoted 66 lines · Yang Zhang

arch/x86/kvm/lapic.c void kvm_apic_post_state_restore(struct kvm_vcpu

Quoted 27 lines

From: Yang Zhang <[email protected]>

---
 arch/x86/kvm/lapic.c |    9 +++++++++ arch/x86/kvm/lapic.h |    2 ++
 virt/kvm/ioapic.c    |   43

+++++++++++++++++++++++++++++++++++++++++++ virt/kvm/ioapic.h |
1 + 4 files changed, 55 insertions(+), 0 deletions(-)

arch/x86/kvm/lapic.c +8 -0
index 96ab160..9c041fa 100644
@@ -94,6 +94,14 @@ static inline int apic_test_vector(int vec, void *bitmap)
 	return test_bit(VEC_POS(vec), (bitmap) + REG_POS(vec));
 }
+bool kvm_apic_pending_eoi(struct kvm_vcpu *vcpu, int vector)
+{
+	struct kvm_lapic *apic = vcpu->arch.apic;
+
+	return apic_test_vector(vector, apic->regs + APIC_ISR) ||
+		apic_test_vector(vector, apic->regs + APIC_IRR);
+}
+
 static inline void apic_set_vector(int vec, void *bitmap)
 {
 	set_bit(VEC_POS(vec), (bitmap) + REG_POS(vec));
@@ -1665,6 +1673,7 @@ void kvm_apic_post_state_restore(struct kvm_vcpu

*vcpu,

arch/x86/kvm/lapic.h static inline bool kvm_apic_has_events(struct

Quoted 11 lines
arch/x86/kvm/lapic.c +0 -0
 	apic->highest_isr_cache = -1;
 	kvm_x86_ops->hwapic_isr_update(vcpu->kvm,
 apic_find_highest_isr(apic)); 	kvm_make_request(KVM_REQ_EVENT, vcpu);
 +	kvm_rtc_irq_restore(vcpu); }
 
 void __kvm_migrate_apic_timer(struct kvm_vcpu *vcpu)
arch/x86/kvm/lapic.h +0 -0
index 967519c..004d2ad 100644
@@ -170,4 +170,6 @@ static inline bool kvm_apic_has_events(struct

kvm_vcpu *vcpu)

virt/kvm/ioapic.c static unsigned long ioapic_read_indirect(struct

Quoted 10 lines
arch/x86/kvm/lapic.h +2 -0
 	return vcpu->arch.apic->pending_events;
 }
+bool kvm_apic_pending_eoi(struct kvm_vcpu *vcpu, int vector);
+
 #endif
virt/kvm/ioapic.c +0 -0
index 8664812..0b12b17 100644
@@ -90,6 +90,47 @@ static unsigned long ioapic_read_indirect(struct

kvm_ioapic *ioapic,

Quoted 14 lines
virt/kvm/ioapic.c +12 -0
 	return result;
 }
+static void rtc_irq_reset(struct kvm_ioapic *ioapic)
+{
+	ioapic->rtc_status.pending_eoi = 0;
+	bitmap_zero(ioapic->rtc_status.dest_map, KVM_MAX_VCPUS);
+}
+
+static void rtc_irq_restore(struct kvm_ioapic *ioapic)
+{
+	struct kvm_vcpu *vcpu;
+	int vector, i, pending_eoi = 0;
+
+	if (RTC_GSI >= IOAPIC_NUM_PINS)
Quoted 8 lines

virt/kvm/ioapic.c static unsigned long ioapic_read_indirect(struct kvm_ioapic *ioapic, · line 111

Quoted 7 lines
virt/kvm/ioapic.c +7 -0
+		return;
+
+	vector = ioapic->redirtbl[RTC_GSI].fields.vector;
+	kvm_for_each_vcpu(i, vcpu, ioapic->kvm) {
+		if (kvm_apic_pending_eoi(vcpu, vector)) {
+			pending_eoi++;
+			__set_bit(vcpu->vcpu_id, ioapic->rtc_status.dest_map);

You should cleat dest_map at the beginning to get rid of stale bits.

I thought kvm_set_ioapic is called only after save/restore or migration. And the ioapic should be reset successfully before call it. So the dest_map is empty before call rtc_irq_restore(). But it is possible kvm_set_ioapic is called beside save/restore or migration. Right?

Quoted 15 lines
Quoted 4 lines
virt/kvm/ioapic.c +3 -0
+		}
+	}
+	ioapic->rtc_status.pending_eoi = pending_eoi;

virt/kvm/ioapic.c static unsigned long ioapic_read_indirect(struct kvm_ioapic *ioapic, · line 124

Quoted 10 lines
virt/kvm/ioapic.c +10 -0
+}
+
+void kvm_rtc_irq_restore(struct kvm_vcpu *vcpu)
+{
+	struct kvm_ioapic *ioapic = vcpu->kvm->arch.vioapic;
+	int vector;
+
+	if (!ioapic)
+		return;
+

Can this be called if ioapic == NULL?

Yes. IIRC, unit test will test lapic function without ioapic.

Quoted 1 line

Should check for if (RTC_GSI >= IOAPIC_NUM_PINS) here too.

Not necessary. kvm_rtc_irq_restore is called from "arch/x86/" and we have the defination:
#ifdef CONFIG_X86
#define RTC_GSI 8

The check will be false always. As the logic you suggested below, this check is necessary for _all() not _one();

Quoted 7 lines

virt/kvm/ioapic.c static unsigned long ioapic_read_indirect(struct kvm_ioapic *ioapic, · line 129

Quoted 6 lines
virt/kvm/ioapic.c +5 -0
+	spin_lock(&ioapic->lock);
+	vector = ioapic->redirtbl[RTC_GSI].fields.vector;
+	if (kvm_apic_pending_eoi(vcpu, vector)) {
+		__set_bit(vcpu->vcpu_id, ioapic->rtc_status.dest_map);
+		ioapic->rtc_status.pending_eoi++;

The bit may have been set already. The logic should be:

Right.

Quoted 16 lines

new_val = kvm_apic_pending_eoi(vcpu, vector)
old_val = set_bit(vcpu_id, dest_map)

if (new_val == old_val)

	return;
if (new_val) {
	__set_bit(vcpu_id, dest_map);
	pending_eoi++;
} else {
	__clear_bit(vcpu_id, dest_map);
	pending_eoi--;
}
Quoted 6 lines

The naming of above two functions are not good either. Call them something like kvm_rtc_eoi_tracking_restore_all() and kvm_rtc_eoi_tracking_restore_one(). And _all should call _one() for each vcpu. Make __rtc_irq_eoi_tracking_restore_one() that does not take ioapic lock and call it from kvm_rtc_eoi_tracking_restore_one() surrounded by locks.

Ok. Just confirm whether I am understanding correct:

kvm_rtc_eoi_tracking_restore_all():
{
for_each_vcpu:
kvm_rtc_eoi_tracking_restore_one():
}

kvm_rtc_eoi_tracking_restore_one():
{
lock();
__rtc_irq_eoi_tracking_restore_one():
unlock();
}

kvm_set_ioapic()
{
kvm_rtc_eoi_tracking_restore_all();
}

kvm_apic_post_state_restore()
{
kvm_rtc_eoi_tracking_restore_one();
}

Quoted 7 lines

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

Best regards,
Yang

#7 lore raw

Re: [PATCH v7 4/7] KVM: Add reset/restore rtc_status support

Quoted 75 lines · Yang Z

Gleb Natapov wrote on 2013-04-04:

Quoted 66 lines · Yang Zhang

arch/x86/kvm/lapic.c void kvm_apic_post_state_restore(struct kvm_vcpu

Quoted 27 lines

From: Yang Zhang <[email protected]>

---
 arch/x86/kvm/lapic.c |    9 +++++++++ arch/x86/kvm/lapic.h |    2 ++
 virt/kvm/ioapic.c    |   43

+++++++++++++++++++++++++++++++++++++++++++ virt/kvm/ioapic.h |
1 + 4 files changed, 55 insertions(+), 0 deletions(-)

arch/x86/kvm/lapic.c +8 -0
index 96ab160..9c041fa 100644
@@ -94,6 +94,14 @@ static inline int apic_test_vector(int vec, void *bitmap)
 	return test_bit(VEC_POS(vec), (bitmap) + REG_POS(vec));
 }
+bool kvm_apic_pending_eoi(struct kvm_vcpu *vcpu, int vector)
+{
+	struct kvm_lapic *apic = vcpu->arch.apic;
+
+	return apic_test_vector(vector, apic->regs + APIC_ISR) ||
+		apic_test_vector(vector, apic->regs + APIC_IRR);
+}
+
 static inline void apic_set_vector(int vec, void *bitmap)
 {
 	set_bit(VEC_POS(vec), (bitmap) + REG_POS(vec));
@@ -1665,6 +1673,7 @@ void kvm_apic_post_state_restore(struct kvm_vcpu

*vcpu,

arch/x86/kvm/lapic.h static inline bool kvm_apic_has_events(struct

Quoted 11 lines
arch/x86/kvm/lapic.c +0 -0
 	apic->highest_isr_cache = -1;
 	kvm_x86_ops->hwapic_isr_update(vcpu->kvm,
 apic_find_highest_isr(apic)); 	kvm_make_request(KVM_REQ_EVENT, vcpu);
 +	kvm_rtc_irq_restore(vcpu); }
 
 void __kvm_migrate_apic_timer(struct kvm_vcpu *vcpu)
arch/x86/kvm/lapic.h +0 -0
index 967519c..004d2ad 100644
@@ -170,4 +170,6 @@ static inline bool kvm_apic_has_events(struct

kvm_vcpu *vcpu)

virt/kvm/ioapic.c static unsigned long ioapic_read_indirect(struct

Quoted 10 lines
arch/x86/kvm/lapic.h +2 -0
 	return vcpu->arch.apic->pending_events;
 }
+bool kvm_apic_pending_eoi(struct kvm_vcpu *vcpu, int vector);
+
 #endif
virt/kvm/ioapic.c +0 -0
index 8664812..0b12b17 100644
@@ -90,6 +90,47 @@ static unsigned long ioapic_read_indirect(struct

kvm_ioapic *ioapic,

Quoted 14 lines
virt/kvm/ioapic.c +12 -0
 	return result;
 }
+static void rtc_irq_reset(struct kvm_ioapic *ioapic)
+{
+	ioapic->rtc_status.pending_eoi = 0;
+	bitmap_zero(ioapic->rtc_status.dest_map, KVM_MAX_VCPUS);
+}
+
+static void rtc_irq_restore(struct kvm_ioapic *ioapic)
+{
+	struct kvm_vcpu *vcpu;
+	int vector, i, pending_eoi = 0;
+
+	if (RTC_GSI >= IOAPIC_NUM_PINS)
Quoted 8 lines

virt/kvm/ioapic.c static unsigned long ioapic_read_indirect(struct kvm_ioapic *ioapic, · line 111

Quoted 7 lines
virt/kvm/ioapic.c +7 -0
+		return;
+
+	vector = ioapic->redirtbl[RTC_GSI].fields.vector;
+	kvm_for_each_vcpu(i, vcpu, ioapic->kvm) {
+		if (kvm_apic_pending_eoi(vcpu, vector)) {
+			pending_eoi++;
+			__set_bit(vcpu->vcpu_id, ioapic->rtc_status.dest_map);

You should cleat dest_map at the beginning to get rid of stale bits.

Quoted 3 lines

I thought kvm_set_ioapic is called only after save/restore or migration. And the ioapic should be reset successfully before call it. So the dest_map is empty before call rtc_irq_restore(). But it is possible kvm_set_ioapic is called beside save/restore or migration. Right?

First of all userspace should not care when it calls kvm_set_ioapic() the kernel need to do the right thing. Second, believe it or not, kvm_ioapic_reset() is not called during system reset. Instead userspace reset it by calling kvm_set_ioapic() with ioapic state after reset.

Quoted 15 lines
Quoted 15 lines
Quoted 4 lines
virt/kvm/ioapic.c +3 -0
+		}
+	}
+	ioapic->rtc_status.pending_eoi = pending_eoi;

virt/kvm/ioapic.c static unsigned long ioapic_read_indirect(struct kvm_ioapic *ioapic, · line 124

Quoted 10 lines
virt/kvm/ioapic.c +10 -0
+}
+
+void kvm_rtc_irq_restore(struct kvm_vcpu *vcpu)
+{
+	struct kvm_ioapic *ioapic = vcpu->kvm->arch.vioapic;
+	int vector;
+
+	if (!ioapic)
+		return;
+

Can this be called if ioapic == NULL?

Quoted 1 line

Yes. IIRC, unit test will test lapic function without ioapic.

Unit test is a guest code. This has nothing to do with a guest code. Unit test is not the one who creates lapic.

Quoted 7 lines
Quoted 2 lines

Should check for if (RTC_GSI >= IOAPIC_NUM_PINS) here too.

Not necessary. kvm_rtc_irq_restore is called from "arch/x86/" and we have the defination:
#ifdef CONFIG_X86
#define RTC_GSI 8

The check will be false always. As the logic you suggested below, this check is necessary for _all() not _one();

OK.

Quoted 32 lines
Quoted 8 lines

virt/kvm/ioapic.c static unsigned long ioapic_read_indirect(struct kvm_ioapic *ioapic, · line 129

Quoted 7 lines
virt/kvm/ioapic.c +5 -0
+	spin_lock(&ioapic->lock);
+	vector = ioapic->redirtbl[RTC_GSI].fields.vector;
+	if (kvm_apic_pending_eoi(vcpu, vector)) {
+		__set_bit(vcpu->vcpu_id, ioapic->rtc_status.dest_map);
+		ioapic->rtc_status.pending_eoi++;

The bit may have been set already. The logic should be:

Right.

Quoted 16 lines

new_val = kvm_apic_pending_eoi(vcpu, vector)
old_val = set_bit(vcpu_id, dest_map)

if (new_val == old_val)

	return;
if (new_val) {
	__set_bit(vcpu_id, dest_map);
	pending_eoi++;
} else {
	__clear_bit(vcpu_id, dest_map);
	pending_eoi--;
}
Quoted 6 lines

The naming of above two functions are not good either. Call them something like kvm_rtc_eoi_tracking_restore_all() and kvm_rtc_eoi_tracking_restore_one(). And _all should call _one() for each vcpu. Make __rtc_irq_eoi_tracking_restore_one() that does not take ioapic lock and call it from kvm_rtc_eoi_tracking_restore_one() surrounded by locks.

Quoted 6 lines

Ok. Just confirm whether I am understanding correct:

kvm_rtc_eoi_tracking_restore_all():
{
for_each_vcpu:
kvm_rtc_eoi_tracking_restore_one():

        __rtc_irq_eoi_tracking_restore_one();

Since caller will have the lock already.

Quoted 14 lines

}

kvm_rtc_eoi_tracking_restore_one():
{
lock();
__rtc_irq_eoi_tracking_restore_one():
unlock();
}

kvm_set_ioapic()
{
kvm_rtc_eoi_tracking_restore_all();
}

Quoted 5 lines

kvm_apic_post_state_restore()
{
kvm_rtc_eoi_tracking_restore_one();
}

Yes.

--
Gleb.

#8 lore raw

RE: [PATCH v7 4/7] KVM: Add reset/restore rtc_status support

Gleb Natapov wrote on 2013-04-07:

Quoted 80 lines · Yang Z
Quoted 21 lines

Gleb Natapov wrote on 2013-04-04:

Quoted 20 lines · Yang Zhang
Quoted 19 lines

From: Yang Zhang <[email protected]>

---
 arch/x86/kvm/lapic.c |    9 +++++++++ arch/x86/kvm/lapic.h |    2 ++
 virt/kvm/ioapic.c    |   43
 +++++++++++++++++++++++++++++++++++++++++++ virt/kvm/ioapic.h | 1 +
 4 files changed, 55 insertions(+), 0 deletions(-)
arch/x86/kvm/lapic.c +4 -0
index 96ab160..9c041fa 100644
@@ -94,6 +94,14 @@ static inline int apic_test_vector(int vec, void *bitmap)
 	return test_bit(VEC_POS(vec), (bitmap) + REG_POS(vec));
 }
+bool kvm_apic_pending_eoi(struct kvm_vcpu *vcpu, int vector)
+{
+	struct kvm_lapic *apic = vcpu->arch.apic;
+
Quoted 8 lines
Quoted 8 lines

arch/x86/kvm/lapic.c void kvm_apic_post_state_restore(struct

Quoted 8 lines
arch/x86/kvm/lapic.c +4 -0
+	return apic_test_vector(vector, apic->regs + APIC_ISR) ||
+		apic_test_vector(vector, apic->regs + APIC_IRR);
+}
+
 static inline void apic_set_vector(int vec, void *bitmap)
 {
 	set_bit(VEC_POS(vec), (bitmap) + REG_POS(vec));
@@ -1665,6 +1673,7 @@ void kvm_apic_post_state_restore(struct

kvm_vcpu

Quoted 46 lines
Quoted 38 lines

*vcpu,

arch/x86/kvm/lapic.h static inline bool kvm_apic_has_events(struct

Quoted 11 lines
arch/x86/kvm/lapic.c +0 -0
 	apic->highest_isr_cache = -1;
 	kvm_x86_ops->hwapic_isr_update(vcpu->kvm,
 apic_find_highest_isr(apic)); 	kvm_make_request(KVM_REQ_EVENT,
 vcpu); +	kvm_rtc_irq_restore(vcpu); }
 
 void __kvm_migrate_apic_timer(struct kvm_vcpu *vcpu)
arch/x86/kvm/lapic.h +0 -0
index 967519c..004d2ad 100644
@@ -170,4 +170,6 @@ static inline bool kvm_apic_has_events(struct

kvm_vcpu *vcpu)

virt/kvm/ioapic.c static unsigned long ioapic_read_indirect(struct

Quoted 10 lines
arch/x86/kvm/lapic.h +2 -0
 	return vcpu->arch.apic->pending_events;
 }
+bool kvm_apic_pending_eoi(struct kvm_vcpu *vcpu, int vector);
+
 #endif
virt/kvm/ioapic.c +0 -0
index 8664812..0b12b17 100644
@@ -90,6 +90,47 @@ static unsigned long ioapic_read_indirect(struct

kvm_ioapic *ioapic,

Quoted 14 lines
virt/kvm/ioapic.c +12 -0
 	return result;
 }
+static void rtc_irq_reset(struct kvm_ioapic *ioapic)
+{
+	ioapic->rtc_status.pending_eoi = 0;
+	bitmap_zero(ioapic->rtc_status.dest_map, KVM_MAX_VCPUS);
+}
+
+static void rtc_irq_restore(struct kvm_ioapic *ioapic)
+{
+	struct kvm_vcpu *vcpu;
+	int vector, i, pending_eoi = 0;
+
+	if (RTC_GSI >= IOAPIC_NUM_PINS)
Quoted 8 lines

virt/kvm/ioapic.c static unsigned long ioapic_read_indirect(struct kvm_ioapic *ioapic, · line 111

Quoted 7 lines
virt/kvm/ioapic.c +7 -0
+		return;
+
+	vector = ioapic->redirtbl[RTC_GSI].fields.vector;
+	kvm_for_each_vcpu(i, vcpu, ioapic->kvm) {
+		if (kvm_apic_pending_eoi(vcpu, vector)) {
+			pending_eoi++;
+			__set_bit(vcpu->vcpu_id, ioapic->rtc_status.dest_map);

You should cleat dest_map at the beginning to get rid of stale bits.

Quoted 1 line

I thought kvm_set_ioapic is called only after save/restore or migration. And the

ioapic should be reset successfully before call it. So the dest_map is empty before call rtc_irq_restore().

Quoted 7 lines
Quoted 3 lines

But it is possible kvm_set_ioapic is called beside save/restore or migration. Right?

First of all userspace should not care when it calls kvm_set_ioapic() the kernel need to do the right thing. Second, believe it or not, kvm_ioapic_reset() is not called during system reset. Instead userspace reset it by calling kvm_set_ioapic() with ioapic state after reset.

Ok. I see. As the logic you suggested, it will clear dest_map if no pending eoi in vcpu, so we don't need to do it again.

Quoted 16 lines
Quoted 16 lines
Quoted 16 lines
Quoted 5 lines
virt/kvm/ioapic.c +3 -0
+		}
+	}
+	ioapic->rtc_status.pending_eoi = pending_eoi;

virt/kvm/ioapic.c static unsigned long ioapic_read_indirect(struct kvm_ioapic *ioapic, · line 124

Quoted 10 lines
virt/kvm/ioapic.c +10 -0
+}
+
+void kvm_rtc_irq_restore(struct kvm_vcpu *vcpu)
+{
+	struct kvm_ioapic *ioapic = vcpu->kvm->arch.vioapic;
+	int vector;
+
+	if (!ioapic)
+		return;
+

Can this be called if ioapic == NULL?

Quoted 3 lines
Quoted 1 line

Yes. IIRC, unit test will test lapic function without ioapic.

Unit test is a guest code. This has nothing to do with a guest code. Unit test is not the one who creates lapic.

Ok. Then !ioapic is unnecessary.

Quoted 69 lines
Quoted 6 lines
Quoted 2 lines

Should check for if (RTC_GSI >= IOAPIC_NUM_PINS) here too.

Not necessary. kvm_rtc_irq_restore is called from "arch/x86/" and we have the defination: #ifdef CONFIG_X86 #define RTC_GSI 8

The check will be false always. As the logic you suggested below, this check is

necessary for _all() not _one();
OK.

Quoted 32 lines
Quoted 8 lines

virt/kvm/ioapic.c static unsigned long ioapic_read_indirect(struct kvm_ioapic *ioapic, · line 129

Quoted 7 lines
virt/kvm/ioapic.c +5 -0
+	spin_lock(&ioapic->lock);
+	vector = ioapic->redirtbl[RTC_GSI].fields.vector;
+	if (kvm_apic_pending_eoi(vcpu, vector)) {
+		__set_bit(vcpu->vcpu_id, ioapic->rtc_status.dest_map);
+		ioapic->rtc_status.pending_eoi++;

The bit may have been set already. The logic should be:

Right.

Quoted 16 lines

new_val = kvm_apic_pending_eoi(vcpu, vector)
old_val = set_bit(vcpu_id, dest_map)

if (new_val == old_val)

	return;
if (new_val) {
	__set_bit(vcpu_id, dest_map);
	pending_eoi++;
} else {
	__clear_bit(vcpu_id, dest_map);
	pending_eoi--;
}
Quoted 6 lines

The naming of above two functions are not good either. Call them something like kvm_rtc_eoi_tracking_restore_all() and kvm_rtc_eoi_tracking_restore_one(). And _all should call _one() for each vcpu. Make __rtc_irq_eoi_tracking_restore_one() that does not take ioapic lock and call it from kvm_rtc_eoi_tracking_restore_one() surrounded by locks.

Quoted 6 lines

Ok. Just confirm whether I am understanding correct:

kvm_rtc_eoi_tracking_restore_all():
{
for_each_vcpu:
kvm_rtc_eoi_tracking_restore_one():

        __rtc_irq_eoi_tracking_restore_one();

Since caller will have the lock already.

Quoted 14 lines

}

kvm_rtc_eoi_tracking_restore_one():
{
lock();
__rtc_irq_eoi_tracking_restore_one():
unlock();
}

kvm_set_ioapic()
{
kvm_rtc_eoi_tracking_restore_all();
}

Quoted 5 lines

kvm_apic_post_state_restore()
{
kvm_rtc_eoi_tracking_restore_one();
}

Quoted 4 lines

Yes.

--
Gleb.

Best regards,
Yang

#9 lore raw

Re: [PATCH v7 4/7] KVM: Add reset/restore rtc_status support

Quoted 81 lines · Yang Z

Gleb Natapov wrote on 2013-04-07:

Quoted 80 lines · Yang Z
Quoted 21 lines

Gleb Natapov wrote on 2013-04-04:

Quoted 20 lines · Yang Zhang
Quoted 19 lines

From: Yang Zhang <[email protected]>

---
 arch/x86/kvm/lapic.c |    9 +++++++++ arch/x86/kvm/lapic.h |    2 ++
 virt/kvm/ioapic.c    |   43
 +++++++++++++++++++++++++++++++++++++++++++ virt/kvm/ioapic.h | 1 +
 4 files changed, 55 insertions(+), 0 deletions(-)
arch/x86/kvm/lapic.c +4 -0
index 96ab160..9c041fa 100644
@@ -94,6 +94,14 @@ static inline int apic_test_vector(int vec, void *bitmap)
 	return test_bit(VEC_POS(vec), (bitmap) + REG_POS(vec));
 }
+bool kvm_apic_pending_eoi(struct kvm_vcpu *vcpu, int vector)
+{
+	struct kvm_lapic *apic = vcpu->arch.apic;
+
Quoted 8 lines
Quoted 8 lines

arch/x86/kvm/lapic.c void kvm_apic_post_state_restore(struct

Quoted 8 lines
arch/x86/kvm/lapic.c +4 -0
+	return apic_test_vector(vector, apic->regs + APIC_ISR) ||
+		apic_test_vector(vector, apic->regs + APIC_IRR);
+}
+
 static inline void apic_set_vector(int vec, void *bitmap)
 {
 	set_bit(VEC_POS(vec), (bitmap) + REG_POS(vec));
@@ -1665,6 +1673,7 @@ void kvm_apic_post_state_restore(struct

kvm_vcpu

Quoted 46 lines
Quoted 38 lines

*vcpu,

arch/x86/kvm/lapic.h static inline bool kvm_apic_has_events(struct

Quoted 11 lines
arch/x86/kvm/lapic.c +0 -0
 	apic->highest_isr_cache = -1;
 	kvm_x86_ops->hwapic_isr_update(vcpu->kvm,
 apic_find_highest_isr(apic)); 	kvm_make_request(KVM_REQ_EVENT,
 vcpu); +	kvm_rtc_irq_restore(vcpu); }
 
 void __kvm_migrate_apic_timer(struct kvm_vcpu *vcpu)
arch/x86/kvm/lapic.h +0 -0
index 967519c..004d2ad 100644
@@ -170,4 +170,6 @@ static inline bool kvm_apic_has_events(struct

kvm_vcpu *vcpu)

virt/kvm/ioapic.c static unsigned long ioapic_read_indirect(struct

Quoted 10 lines
arch/x86/kvm/lapic.h +2 -0
 	return vcpu->arch.apic->pending_events;
 }
+bool kvm_apic_pending_eoi(struct kvm_vcpu *vcpu, int vector);
+
 #endif
virt/kvm/ioapic.c +0 -0
index 8664812..0b12b17 100644
@@ -90,6 +90,47 @@ static unsigned long ioapic_read_indirect(struct

kvm_ioapic *ioapic,

Quoted 14 lines
virt/kvm/ioapic.c +12 -0
 	return result;
 }
+static void rtc_irq_reset(struct kvm_ioapic *ioapic)
+{
+	ioapic->rtc_status.pending_eoi = 0;
+	bitmap_zero(ioapic->rtc_status.dest_map, KVM_MAX_VCPUS);
+}
+
+static void rtc_irq_restore(struct kvm_ioapic *ioapic)
+{
+	struct kvm_vcpu *vcpu;
+	int vector, i, pending_eoi = 0;
+
+	if (RTC_GSI >= IOAPIC_NUM_PINS)
Quoted 8 lines

virt/kvm/ioapic.c static unsigned long ioapic_read_indirect(struct kvm_ioapic *ioapic, · line 111

Quoted 7 lines
virt/kvm/ioapic.c +7 -0
+		return;
+
+	vector = ioapic->redirtbl[RTC_GSI].fields.vector;
+	kvm_for_each_vcpu(i, vcpu, ioapic->kvm) {
+		if (kvm_apic_pending_eoi(vcpu, vector)) {
+			pending_eoi++;
+			__set_bit(vcpu->vcpu_id, ioapic->rtc_status.dest_map);

You should cleat dest_map at the beginning to get rid of stale bits.

Quoted 1 line

I thought kvm_set_ioapic is called only after save/restore or migration. And the

ioapic should be reset successfully before call it. So the dest_map is empty before call rtc_irq_restore().

Quoted 9 lines
Quoted 7 lines
Quoted 3 lines

But it is possible kvm_set_ioapic is called beside save/restore or migration. Right?

First of all userspace should not care when it calls kvm_set_ioapic() the kernel need to do the right thing. Second, believe it or not, kvm_ioapic_reset() is not called during system reset. Instead userspace reset it by calling kvm_set_ioapic() with ioapic state after reset.

Ok. I see. As the logic you suggested, it will clear dest_map if no pending eoi in vcpu, so we don't need to do it again.

You again rely on userspace doing thing in certain manner. What is set_lapic() is never called? Kernel internal state have to be correct after each ioctl call.

--
Gleb.

#10 lore raw

RE: [PATCH v7 4/7] KVM: Add reset/restore rtc_status support

Gleb Natapov wrote on 2013-04-07:

Quoted 91 lines · Yang Z
Quoted 6 lines

Gleb Natapov wrote on 2013-04-07:

Quoted 5 lines · Yang Z
Quoted 4 lines

Gleb Natapov wrote on 2013-04-04:

Quoted 3 lines · Yang Zhang
Quoted 2 lines

From: Yang Zhang <[email protected]>

Quoted 11 lines
Quoted 11 lines
Quoted 11 lines
Quoted 11 lines

arch/x86/kvm/lapic.c static inline int apic_test_vector(int vec, void

Quoted 11 lines
---
 arch/x86/kvm/lapic.c |    9 +++++++++ arch/x86/kvm/lapic.h |    2
 ++ virt/kvm/ioapic.c    |   43
 +++++++++++++++++++++++++++++++++++++++++++ virt/kvm/ioapic.h | 1

+ 4 files changed, 55 insertions(+), 0 deletions(-)

arch/x86/kvm/lapic.c +0 -0
index 96ab160..9c041fa 100644
@@ -94,6 +94,14 @@ static inline int apic_test_vector(int vec, void

*bitmap)

Quoted 61 lines
Quoted 61 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
arch/x86/kvm/lapic.c +4 -0
 	return test_bit(VEC_POS(vec), (bitmap) + REG_POS(vec));
 }
+bool kvm_apic_pending_eoi(struct kvm_vcpu *vcpu, int vector)
+{
+	struct kvm_lapic *apic = vcpu->arch.apic;
+
Quoted 8 lines
Quoted 8 lines

arch/x86/kvm/lapic.c void kvm_apic_post_state_restore(struct

Quoted 8 lines
arch/x86/kvm/lapic.c +4 -0
+	return apic_test_vector(vector, apic->regs + APIC_ISR) ||
+		apic_test_vector(vector, apic->regs + APIC_IRR);
+}
+
 static inline void apic_set_vector(int vec, void *bitmap)
 {
 	set_bit(VEC_POS(vec), (bitmap) + REG_POS(vec));
@@ -1665,6 +1673,7 @@ void kvm_apic_post_state_restore(struct

kvm_vcpu

Quoted 46 lines
Quoted 46 lines

*vcpu,

arch/x86/kvm/lapic.h static inline bool kvm_apic_has_events(struct

Quoted 11 lines
arch/x86/kvm/lapic.c +0 -0
 	apic->highest_isr_cache = -1;
 	kvm_x86_ops->hwapic_isr_update(vcpu->kvm,
 apic_find_highest_isr(apic)); 	kvm_make_request(KVM_REQ_EVENT,
 vcpu); +	kvm_rtc_irq_restore(vcpu); }
 
 void __kvm_migrate_apic_timer(struct kvm_vcpu *vcpu)
arch/x86/kvm/lapic.h +0 -0
index 967519c..004d2ad 100644
@@ -170,4 +170,6 @@ static inline bool kvm_apic_has_events(struct

kvm_vcpu *vcpu)

virt/kvm/ioapic.c static unsigned long ioapic_read_indirect(struct

Quoted 10 lines
arch/x86/kvm/lapic.h +2 -0
 	return vcpu->arch.apic->pending_events;
 }
+bool kvm_apic_pending_eoi(struct kvm_vcpu *vcpu, int vector);
+
 #endif
virt/kvm/ioapic.c +0 -0
index 8664812..0b12b17 100644
@@ -90,6 +90,47 @@ static unsigned long ioapic_read_indirect(struct

kvm_ioapic *ioapic,

Quoted 11 lines
virt/kvm/ioapic.c +9 -0
 	return result;
 }
+static void rtc_irq_reset(struct kvm_ioapic *ioapic)
+{
+	ioapic->rtc_status.pending_eoi = 0;
+	bitmap_zero(ioapic->rtc_status.dest_map, KVM_MAX_VCPUS);
+}
+
+static void rtc_irq_restore(struct kvm_ioapic *ioapic)
+{
+	struct kvm_vcpu *vcpu;

virt/kvm/ioapic.c static unsigned long ioapic_read_indirect(struct kvm_ioapic *ioapic, · line 111

Quoted 10 lines
virt/kvm/ioapic.c +10 -0
+	int vector, i, pending_eoi = 0;
+
+	if (RTC_GSI >= IOAPIC_NUM_PINS)
+		return;
+
+	vector = ioapic->redirtbl[RTC_GSI].fields.vector;
+	kvm_for_each_vcpu(i, vcpu, ioapic->kvm) {
+		if (kvm_apic_pending_eoi(vcpu, vector)) {
+			pending_eoi++;
+			__set_bit(vcpu->vcpu_id, ioapic->rtc_status.dest_map);

You should cleat dest_map at the beginning to get rid of stale bits.

Quoted 1 line
Quoted 1 line
Quoted 1 line

I thought kvm_set_ioapic is called only after save/restore or migration. And

the

Quoted 9 lines
Quoted 9 lines

ioapic should be reset successfully before call it. So the dest_map is empty before call rtc_irq_restore().

Quoted 3 lines

But it is possible kvm_set_ioapic is called beside save/restore or migration. Right?

First of all userspace should not care when it calls kvm_set_ioapic() the kernel need to do the right thing. Second, believe it or not, kvm_ioapic_reset() is not called during system reset. Instead userspace reset it by calling kvm_set_ioapic() with ioapic state after reset.

Quoted 6 lines
Quoted 3 lines

Ok. I see. As the logic you suggested, it will clear dest_map if no pending eoi in vcpu, so we don't need to do it again.

You again rely on userspace doing thing in certain manner. What is set_lapic() is never called? Kernel internal state have to be correct after each ioctl call.

Sorry. I cannot figure out what's the problem if don't clear dest_map? Can you elaborate it?

Best regards,
Yang

#11 lore raw

Re: [PATCH v7 4/7] KVM: Add reset/restore rtc_status support

Quoted 92 lines · Yang Z

Gleb Natapov wrote on 2013-04-07:

Quoted 91 lines · Yang Z
Quoted 6 lines

Gleb Natapov wrote on 2013-04-07:

Quoted 5 lines · Yang Z
Quoted 4 lines

Gleb Natapov wrote on 2013-04-04:

Quoted 3 lines · Yang Zhang
Quoted 2 lines

From: Yang Zhang <[email protected]>

Quoted 11 lines
Quoted 11 lines
Quoted 11 lines
Quoted 11 lines

arch/x86/kvm/lapic.c static inline int apic_test_vector(int vec, void

Quoted 11 lines
---
 arch/x86/kvm/lapic.c |    9 +++++++++ arch/x86/kvm/lapic.h |    2
 ++ virt/kvm/ioapic.c    |   43
 +++++++++++++++++++++++++++++++++++++++++++ virt/kvm/ioapic.h | 1

+ 4 files changed, 55 insertions(+), 0 deletions(-)

arch/x86/kvm/lapic.c +0 -0
index 96ab160..9c041fa 100644
@@ -94,6 +94,14 @@ static inline int apic_test_vector(int vec, void

*bitmap)

Quoted 61 lines
Quoted 61 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
arch/x86/kvm/lapic.c +4 -0
 	return test_bit(VEC_POS(vec), (bitmap) + REG_POS(vec));
 }
+bool kvm_apic_pending_eoi(struct kvm_vcpu *vcpu, int vector)
+{
+	struct kvm_lapic *apic = vcpu->arch.apic;
+
Quoted 8 lines
Quoted 8 lines

arch/x86/kvm/lapic.c void kvm_apic_post_state_restore(struct

Quoted 8 lines
arch/x86/kvm/lapic.c +4 -0
+	return apic_test_vector(vector, apic->regs + APIC_ISR) ||
+		apic_test_vector(vector, apic->regs + APIC_IRR);
+}
+
 static inline void apic_set_vector(int vec, void *bitmap)
 {
 	set_bit(VEC_POS(vec), (bitmap) + REG_POS(vec));
@@ -1665,6 +1673,7 @@ void kvm_apic_post_state_restore(struct

kvm_vcpu

Quoted 46 lines
Quoted 46 lines

*vcpu,

arch/x86/kvm/lapic.h static inline bool kvm_apic_has_events(struct

Quoted 11 lines
arch/x86/kvm/lapic.c +0 -0
 	apic->highest_isr_cache = -1;
 	kvm_x86_ops->hwapic_isr_update(vcpu->kvm,
 apic_find_highest_isr(apic)); 	kvm_make_request(KVM_REQ_EVENT,
 vcpu); +	kvm_rtc_irq_restore(vcpu); }
 
 void __kvm_migrate_apic_timer(struct kvm_vcpu *vcpu)
arch/x86/kvm/lapic.h +0 -0
index 967519c..004d2ad 100644
@@ -170,4 +170,6 @@ static inline bool kvm_apic_has_events(struct

kvm_vcpu *vcpu)

virt/kvm/ioapic.c static unsigned long ioapic_read_indirect(struct

Quoted 10 lines
arch/x86/kvm/lapic.h +2 -0
 	return vcpu->arch.apic->pending_events;
 }
+bool kvm_apic_pending_eoi(struct kvm_vcpu *vcpu, int vector);
+
 #endif
virt/kvm/ioapic.c +0 -0
index 8664812..0b12b17 100644
@@ -90,6 +90,47 @@ static unsigned long ioapic_read_indirect(struct

kvm_ioapic *ioapic,

Quoted 11 lines
virt/kvm/ioapic.c +9 -0
 	return result;
 }
+static void rtc_irq_reset(struct kvm_ioapic *ioapic)
+{
+	ioapic->rtc_status.pending_eoi = 0;
+	bitmap_zero(ioapic->rtc_status.dest_map, KVM_MAX_VCPUS);
+}
+
+static void rtc_irq_restore(struct kvm_ioapic *ioapic)
+{
+	struct kvm_vcpu *vcpu;

virt/kvm/ioapic.c static unsigned long ioapic_read_indirect(struct · line 110

Quoted 10 lines
virt/kvm/ioapic.c +10 -0
+	int vector, i, pending_eoi = 0;
+
+	if (RTC_GSI >= IOAPIC_NUM_PINS)
+		return;
+
+	vector = ioapic->redirtbl[RTC_GSI].fields.vector;
+	kvm_for_each_vcpu(i, vcpu, ioapic->kvm) {
+		if (kvm_apic_pending_eoi(vcpu, vector)) {
+			pending_eoi++;
+			__set_bit(vcpu->vcpu_id, ioapic->rtc_status.dest_map);

You should cleat dest_map at the beginning to get rid of stale bits.

Quoted 1 line
Quoted 1 line
Quoted 1 line

I thought kvm_set_ioapic is called only after save/restore or migration. And

the

Quoted 9 lines
Quoted 9 lines

ioapic should be reset successfully before call it. So the dest_map is empty before call rtc_irq_restore().

Quoted 3 lines

But it is possible kvm_set_ioapic is called beside save/restore or migration. Right?

First of all userspace should not care when it calls kvm_set_ioapic() the kernel need to do the right thing. Second, believe it or not, kvm_ioapic_reset() is not called during system reset. Instead userspace reset it by calling kvm_set_ioapic() with ioapic state after reset.

Quoted 8 lines
Quoted 6 lines
Quoted 3 lines

Ok. I see. As the logic you suggested, it will clear dest_map if no pending eoi in vcpu, so we don't need to do it again.

You again rely on userspace doing thing in certain manner. What is set_lapic() is never called? Kernel internal state have to be correct after each ioctl call.

Sorry. I cannot figure out what's the problem if don't clear dest_map? Can you elaborate it?

What is not obvious about it? If there is a bit in dest_map that should be cleared after rtc_irq_restore() it will not.

--
Gleb.

#12 lore raw

RE: [PATCH v7 4/7] KVM: Add reset/restore rtc_status support

Gleb Natapov wrote on 2013-04-07:

Quoted 98 lines · Yang Z
Quoted 72 lines

Gleb Natapov wrote on 2013-04-07:

Quoted 71 lines · Yang Z
Quoted 6 lines

Gleb Natapov wrote on 2013-04-07:

Quoted 5 lines · Yang Z
Quoted 4 lines

Gleb Natapov wrote on 2013-04-04:

Quoted 3 lines · Yang Zhang
Quoted 2 lines

From: Yang Zhang <[email protected]>

Quoted 11 lines
Quoted 11 lines
Quoted 11 lines
Quoted 11 lines

arch/x86/kvm/lapic.c static inline int apic_test_vector(int vec, void

Quoted 11 lines
---
 arch/x86/kvm/lapic.c |    9 +++++++++ arch/x86/kvm/lapic.h | 2
 ++ virt/kvm/ioapic.c    |   43

+++++++++++++++++++++++++++++++++++++++++++ virt/kvm/ioapic.h | 1 + 4 files changed, 55 insertions(+), 0 deletions(-)

arch/x86/kvm/lapic.c +0 -0
index 96ab160..9c041fa 100644
@@ -94,6 +94,14 @@ static inline int apic_test_vector(int vec, void

*bitmap)

Quoted 52 lines
Quoted 52 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
arch/x86/kvm/lapic.c +4 -0
 	return test_bit(VEC_POS(vec), (bitmap) + REG_POS(vec));
 }
+bool kvm_apic_pending_eoi(struct kvm_vcpu *vcpu, int vector)
+{
+	struct kvm_lapic *apic = vcpu->arch.apic;
+
Quoted 8 lines
Quoted 8 lines

arch/x86/kvm/lapic.c void kvm_apic_post_state_restore(struct

Quoted 8 lines
arch/x86/kvm/lapic.c +4 -0
+	return apic_test_vector(vector, apic->regs + APIC_ISR) ||
+		apic_test_vector(vector, apic->regs + APIC_IRR);
+}
+
 static inline void apic_set_vector(int vec, void *bitmap)
 {
 	set_bit(VEC_POS(vec), (bitmap) + REG_POS(vec));
@@ -1665,6 +1673,7 @@ void kvm_apic_post_state_restore(struct

kvm_vcpu

Quoted 37 lines
Quoted 37 lines

*vcpu,

arch/x86/kvm/lapic.h static inline bool kvm_apic_has_events(struct

Quoted 11 lines
arch/x86/kvm/lapic.c +0 -0
 	apic->highest_isr_cache = -1;
 	kvm_x86_ops->hwapic_isr_update(vcpu->kvm,
 apic_find_highest_isr(apic)); 	kvm_make_request(KVM_REQ_EVENT,
 vcpu); +	kvm_rtc_irq_restore(vcpu); }
 
 void __kvm_migrate_apic_timer(struct kvm_vcpu *vcpu)
arch/x86/kvm/lapic.h +0 -0
index 967519c..004d2ad 100644
@@ -170,4 +170,6 @@ static inline bool kvm_apic_has_events(struct

kvm_vcpu *vcpu)

virt/kvm/ioapic.c static unsigned long ioapic_read_indirect(struct

Quoted 10 lines
arch/x86/kvm/lapic.h +2 -0
 	return vcpu->arch.apic->pending_events;
 }
+bool kvm_apic_pending_eoi(struct kvm_vcpu *vcpu, int vector);
+
 #endif
virt/kvm/ioapic.c +0 -0
index 8664812..0b12b17 100644
@@ -90,6 +90,47 @@ static unsigned long ioapic_read_indirect(struct

kvm_ioapic *ioapic,

Quoted 13 lines
virt/kvm/ioapic.c +11 -0
 	return result;
 }
+static void rtc_irq_reset(struct kvm_ioapic *ioapic)
+{
+	ioapic->rtc_status.pending_eoi = 0;
+	bitmap_zero(ioapic->rtc_status.dest_map, KVM_MAX_VCPUS);
+}
+
+static void rtc_irq_restore(struct kvm_ioapic *ioapic)
+{
+	struct kvm_vcpu *vcpu;
+	int vector, i, pending_eoi = 0;
+
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

virt/kvm/ioapic.c static unsigned long ioapic_read_indirect(struct · line 110

Quoted 8 lines
virt/kvm/ioapic.c +8 -0
+	if (RTC_GSI >= IOAPIC_NUM_PINS)
+		return;
+
+	vector = ioapic->redirtbl[RTC_GSI].fields.vector;
+	kvm_for_each_vcpu(i, vcpu, ioapic->kvm) {
+		if (kvm_apic_pending_eoi(vcpu, vector)) {
+			pending_eoi++;
+			__set_bit(vcpu->vcpu_id,

ioapic->rtc_status.dest_map);

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 1 line

You should cleat dest_map at the beginning to get rid of stale bits.

I thought kvm_set_ioapic is called only after save/restore or migration.

And

Quoted 13 lines
Quoted 13 lines

the

Quoted 12 lines
Quoted 9 lines

ioapic should be reset successfully before call it. So the dest_map is empty before call rtc_irq_restore().

Quoted 3 lines

But it is possible kvm_set_ioapic is called beside save/restore or migration. Right?

First of all userspace should not care when it calls kvm_set_ioapic() the kernel need to do the right thing. Second, believe it or not, kvm_ioapic_reset() is not called during system reset. Instead userspace reset it by calling kvm_set_ioapic() with ioapic state after reset.

Ok. I see. As the logic you suggested, it will clear dest_map if no pending eoi in vcpu, so we don't need to do it again.

Quoted 8 lines
Quoted 6 lines
Quoted 3 lines

You again rely on userspace doing thing in certain manner. What is set_lapic() is never called? Kernel internal state have to be correct after each ioctl call.

Sorry. I cannot figure out what's the problem if don't clear dest_map? Can you elaborate it?

What is not obvious about it? If there is a bit in dest_map that should be cleared after rtc_irq_restore() it will not.

Why it will not? If new_val is false, and the old_val is true. __clear_bit() will clear the dest_map. Am I wrong?

new_val = kvm_apic_pending_eoi(vcpu, vector);
old_val = test_bit(vcpu->vcpu_id, ioapic->rtc_status.dest_map);

if (new_val == old_val)

        return;
if (new_val) {
        __set_bit(vcpu->vcpu_id, ioapic->rtc_status.dest_map);
        ioapic->rtc_status.pending_eoi++;
} else {
        __clear_bit(vcpu->vcpu_id, ioapic->rtc_status.dest_map);
        ioapic->rtc_status.pending_eoi--;
}

Best regards,
Yang

#13 lore raw

Re: [PATCH v7 4/7] KVM: Add reset/restore rtc_status support

Quoted 102 lines · Yang Z

Gleb Natapov wrote on 2013-04-07:

Quoted 101 lines · Yang Z
Quoted 72 lines

Gleb Natapov wrote on 2013-04-07:

Quoted 71 lines · Yang Z
Quoted 6 lines

Gleb Natapov wrote on 2013-04-07:

Quoted 5 lines · Yang Z
Quoted 4 lines

Gleb Natapov wrote on 2013-04-04:

Quoted 3 lines · Yang Zhang
Quoted 2 lines

From: Yang Zhang <[email protected]>

Quoted 11 lines
Quoted 11 lines
Quoted 11 lines
Quoted 11 lines

arch/x86/kvm/lapic.c static inline int apic_test_vector(int vec, void

Quoted 11 lines
---
 arch/x86/kvm/lapic.c |    9 +++++++++ arch/x86/kvm/lapic.h | 2
 ++ virt/kvm/ioapic.c    |   43

+++++++++++++++++++++++++++++++++++++++++++ virt/kvm/ioapic.h | 1 + 4 files changed, 55 insertions(+), 0 deletions(-)

arch/x86/kvm/lapic.c +0 -0
index 96ab160..9c041fa 100644
@@ -94,6 +94,14 @@ static inline int apic_test_vector(int vec, void

*bitmap)

Quoted 52 lines
Quoted 52 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
arch/x86/kvm/lapic.c +4 -0
 	return test_bit(VEC_POS(vec), (bitmap) + REG_POS(vec));
 }
+bool kvm_apic_pending_eoi(struct kvm_vcpu *vcpu, int vector)
+{
+	struct kvm_lapic *apic = vcpu->arch.apic;
+
Quoted 8 lines
Quoted 8 lines

arch/x86/kvm/lapic.c void kvm_apic_post_state_restore(struct

Quoted 8 lines
arch/x86/kvm/lapic.c +4 -0
+	return apic_test_vector(vector, apic->regs + APIC_ISR) ||
+		apic_test_vector(vector, apic->regs + APIC_IRR);
+}
+
 static inline void apic_set_vector(int vec, void *bitmap)
 {
 	set_bit(VEC_POS(vec), (bitmap) + REG_POS(vec));
@@ -1665,6 +1673,7 @@ void kvm_apic_post_state_restore(struct

kvm_vcpu

Quoted 37 lines
Quoted 37 lines

*vcpu,

arch/x86/kvm/lapic.h static inline bool kvm_apic_has_events(struct

Quoted 11 lines
arch/x86/kvm/lapic.c +0 -0
 	apic->highest_isr_cache = -1;
 	kvm_x86_ops->hwapic_isr_update(vcpu->kvm,
 apic_find_highest_isr(apic)); 	kvm_make_request(KVM_REQ_EVENT,
 vcpu); +	kvm_rtc_irq_restore(vcpu); }
 
 void __kvm_migrate_apic_timer(struct kvm_vcpu *vcpu)
arch/x86/kvm/lapic.h +0 -0
index 967519c..004d2ad 100644
@@ -170,4 +170,6 @@ static inline bool kvm_apic_has_events(struct

kvm_vcpu *vcpu)

virt/kvm/ioapic.c static unsigned long ioapic_read_indirect(struct

Quoted 10 lines
arch/x86/kvm/lapic.h +2 -0
 	return vcpu->arch.apic->pending_events;
 }
+bool kvm_apic_pending_eoi(struct kvm_vcpu *vcpu, int vector);
+
 #endif
virt/kvm/ioapic.c +0 -0
index 8664812..0b12b17 100644
@@ -90,6 +90,47 @@ static unsigned long ioapic_read_indirect(struct

kvm_ioapic *ioapic,

Quoted 13 lines
virt/kvm/ioapic.c +11 -0
 	return result;
 }
+static void rtc_irq_reset(struct kvm_ioapic *ioapic)
+{
+	ioapic->rtc_status.pending_eoi = 0;
+	bitmap_zero(ioapic->rtc_status.dest_map, KVM_MAX_VCPUS);
+}
+
+static void rtc_irq_restore(struct kvm_ioapic *ioapic)
+{
+	struct kvm_vcpu *vcpu;
+	int vector, i, pending_eoi = 0;
+
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

virt/kvm/ioapic.c static unsigned long ioapic_read_indirect(struct · line 110

Quoted 8 lines
virt/kvm/ioapic.c +8 -0
+	if (RTC_GSI >= IOAPIC_NUM_PINS)
+		return;
+
+	vector = ioapic->redirtbl[RTC_GSI].fields.vector;
+	kvm_for_each_vcpu(i, vcpu, ioapic->kvm) {
+		if (kvm_apic_pending_eoi(vcpu, vector)) {
+			pending_eoi++;
+			__set_bit(vcpu->vcpu_id,

ioapic->rtc_status.dest_map);

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 1 line

You should cleat dest_map at the beginning to get rid of stale bits.

I thought kvm_set_ioapic is called only after save/restore or migration.

And

Quoted 16 lines
Quoted 16 lines

the

Quoted 5 lines
Quoted 5 lines

ioapic should be reset successfully before call it. So the dest_map is empty before call rtc_irq_restore().

Quoted 3 lines

But it is possible kvm_set_ioapic is called beside save/restore or migration. Right?

Quoted 7 lines
Quoted 4 lines

First of all userspace should not care when it calls kvm_set_ioapic() the kernel need to do the right thing. Second, believe it or not, kvm_ioapic_reset() is not called during system reset. Instead userspace reset it by calling kvm_set_ioapic() with ioapic state after reset.

Ok. I see. As the logic you suggested, it will clear dest_map if no pending eoi in vcpu, so we don't need to do it again.

You again rely on userspace doing thing in certain manner. What is set_lapic() is never called? Kernel internal state have to be correct after each ioctl call.

Quoted 7 lines
Quoted 5 lines
Quoted 3 lines

Sorry. I cannot figure out what's the problem if don't clear dest_map? Can you elaborate it?

What is not obvious about it? If there is a bit in dest_map that should be cleared after rtc_irq_restore() it will not.

Why it will not? If new_val is false, and the old_val is true. __clear_bit() will clear the dest_map. Am I wrong?

Ah, yes with new logic since we go over all vcpus and calculate new value for each one in theory it should be fine, but if we add cpu destruction this will be no longer true.

Quoted 1 line

new_val = kvm_apic_pending_eoi(vcpu, vector);

Which reminds me there are more bugs in the current code. It is not enough to call kvm_apic_pending_eoi() to check the new value. You need to see if the entry is masked and vcpu is the destination of the interrupt too.

Quoted 13 lines

old_val = test_bit(vcpu->vcpu_id, ioapic->rtc_status.dest_map);

if (new_val == old_val)

        return;
if (new_val) {
        __set_bit(vcpu->vcpu_id, ioapic->rtc_status.dest_map);
        ioapic->rtc_status.pending_eoi++;
} else {
        __clear_bit(vcpu->vcpu_id, ioapic->rtc_status.dest_map);
        ioapic->rtc_status.pending_eoi--;
}
Quoted 3 lines

Best regards,
Yang

--
Gleb.

#14 lore raw

RE: [PATCH v7 4/7] KVM: Add reset/restore rtc_status support

Gleb Natapov wrote on 2013-04-07:

Quoted 108 lines · Yang Z
Quoted 43 lines

Gleb Natapov wrote on 2013-04-07:

Quoted 42 lines · Yang Z
Quoted 41 lines

Gleb Natapov wrote on 2013-04-07:

Quoted 40 lines · Yang Z
Quoted 6 lines

Gleb Natapov wrote on 2013-04-07:

Quoted 5 lines · Yang Z
Quoted 4 lines

Gleb Natapov wrote on 2013-04-04:

Quoted 3 lines · Yang Zhang
Quoted 2 lines

From: Yang Zhang <[email protected]>

Quoted 11 lines
Quoted 11 lines
Quoted 11 lines
Quoted 11 lines

arch/x86/kvm/lapic.c static inline int apic_test_vector(int vec, void

Quoted 11 lines
---
 arch/x86/kvm/lapic.c |    9 +++++++++ arch/x86/kvm/lapic.h | 2
 ++ virt/kvm/ioapic.c    |   43

+++++++++++++++++++++++++++++++++++++++++++ virt/kvm/ioapic.h | 1 + 4 files changed, 55 insertions(+), 0 deletions(-)

arch/x86/kvm/lapic.c +0 -0
index 96ab160..9c041fa 100644
@@ -94,6 +94,14 @@ static inline int apic_test_vector(int vec, void

*bitmap)

Quoted 21 lines
Quoted 21 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
arch/x86/kvm/lapic.c +4 -0
 	return test_bit(VEC_POS(vec), (bitmap) + REG_POS(vec));
 }
+bool kvm_apic_pending_eoi(struct kvm_vcpu *vcpu, int vector)
+{
+	struct kvm_lapic *apic = vcpu->arch.apic;
+
Quoted 8 lines
Quoted 8 lines

arch/x86/kvm/lapic.c void kvm_apic_post_state_restore(struct

Quoted 8 lines
arch/x86/kvm/lapic.c +4 -0
+	return apic_test_vector(vector, apic->regs + APIC_ISR) ||
+		apic_test_vector(vector, apic->regs + APIC_IRR);
+}
+
 static inline void apic_set_vector(int vec, void *bitmap)
 {
 	set_bit(VEC_POS(vec), (bitmap) + REG_POS(vec));
@@ -1665,6 +1673,7 @@ void kvm_apic_post_state_restore(struct

kvm_vcpu

Quoted 6 lines
Quoted 6 lines

*vcpu,

Quoted 5 lines
arch/x86/kvm/lapic.c +0 -0
 	apic->highest_isr_cache = -1;
 	kvm_x86_ops->hwapic_isr_update(vcpu->kvm,
 apic_find_highest_isr(apic)); 	kvm_make_request(KVM_REQ_EVENT,
 vcpu); +	kvm_rtc_irq_restore(vcpu); }
 
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines

arch/x86/kvm/lapic.h static inline bool

Quoted 6 lines
arch/x86/kvm/lapic.c +0 -0
 void __kvm_migrate_apic_timer(struct kvm_vcpu *vcpu)
arch/x86/kvm/lapic.h +0 -0
index 967519c..004d2ad 100644
@@ -170,4 +170,6 @@ static inline bool

kvm_apic_has_events(struct

Quoted 11 lines
Quoted 11 lines
Quoted 11 lines
Quoted 11 lines
Quoted 11 lines
Quoted 11 lines
Quoted 11 lines
Quoted 11 lines

kvm_vcpu *vcpu)

virt/kvm/ioapic.c static unsigned long

Quoted 10 lines
arch/x86/kvm/lapic.h +2 -0
 	return vcpu->arch.apic->pending_events;
 }
+bool kvm_apic_pending_eoi(struct kvm_vcpu *vcpu, int vector);
+
 #endif
virt/kvm/ioapic.c +0 -0
index 8664812..0b12b17 100644
@@ -90,6 +90,47 @@ static unsigned long

ioapic_read_indirect(struct

Quoted 45 lines
Quoted 45 lines
Quoted 14 lines
Quoted 14 lines
Quoted 14 lines
Quoted 14 lines
Quoted 14 lines
Quoted 14 lines

kvm_ioapic *ioapic,

Quoted 13 lines
virt/kvm/ioapic.c +11 -0
 	return result;
 }
+static void rtc_irq_reset(struct kvm_ioapic *ioapic)
+{
+	ioapic->rtc_status.pending_eoi = 0;
+	bitmap_zero(ioapic->rtc_status.dest_map, KVM_MAX_VCPUS);
+}
+
+static void rtc_irq_restore(struct kvm_ioapic *ioapic)
+{
+	struct kvm_vcpu *vcpu;
+	int vector, i, pending_eoi = 0;
+
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

virt/kvm/ioapic.c static unsigned long · line 110

Quoted 8 lines
virt/kvm/ioapic.c +8 -0
+	if (RTC_GSI >= IOAPIC_NUM_PINS)
+		return;
+
+	vector = ioapic->redirtbl[RTC_GSI].fields.vector;
+	kvm_for_each_vcpu(i, vcpu, ioapic->kvm) {
+		if (kvm_apic_pending_eoi(vcpu, vector)) {
+			pending_eoi++;
+			__set_bit(vcpu->vcpu_id,

ioapic->rtc_status.dest_map);

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 1 line

You should cleat dest_map at the beginning to get rid of stale bits.

I thought kvm_set_ioapic is called only after save/restore or migration.

And

Quoted 19 lines
Quoted 10 lines

the

Quoted 9 lines
Quoted 9 lines

ioapic should be reset successfully before call it. So the dest_map is empty before call rtc_irq_restore().

Quoted 3 lines

But it is possible kvm_set_ioapic is called beside save/restore or migration. Right?

First of all userspace should not care when it calls kvm_set_ioapic() the kernel need to do the right thing. Second, believe it or not, kvm_ioapic_reset() is not called during system reset. Instead userspace reset it by calling kvm_set_ioapic() with ioapic state after reset.

Quoted 6 lines
Quoted 3 lines

Ok. I see. As the logic you suggested, it will clear dest_map if no pending eoi in vcpu, so we don't need to do it again.

You again rely on userspace doing thing in certain manner. What is set_lapic() is never called? Kernel internal state have to be correct after each ioctl call.

Sorry. I cannot figure out what's the problem if don't clear dest_map? Can you elaborate it?

Quoted 8 lines
Quoted 5 lines
Quoted 2 lines

What is not obvious about it? If there is a bit in dest_map that should be cleared after rtc_irq_restore() it will not.

Why it will not? If new_val is false, and the old_val is true. __clear_bit() will clear the dest_map. Am I wrong?

Ah, yes with new logic since we go over all vcpus and calculate new value for each one in theory it should be fine, but if we add cpu destruction this will be no longer true.

Yes. Given cpu destruction, it is wrong. Do you think we should clear dest_map now or delay it until cpu destruction is ready?

Quoted 5 lines
Quoted 2 lines

new_val = kvm_apic_pending_eoi(vcpu, vector);

Which reminds me there are more bugs in the current code. It is not enough to call kvm_apic_pending_eoi() to check the new value. You need to see if the entry is masked and vcpu is the destination of the interrupt too.

Yes, you are right.

Quoted 12 lines
Quoted 12 lines

old_val = test_bit(vcpu->vcpu_id, ioapic->rtc_status.dest_map);

if (new_val == old_val)

        return;
if (new_val) {
        __set_bit(vcpu->vcpu_id, ioapic->rtc_status.dest_map);
        ioapic->rtc_status.pending_eoi++; } else {
        __clear_bit(vcpu->vcpu_id, ioapic->rtc_status.dest_map);
        ioapic->rtc_status.pending_eoi--;
}
Quoted 6 lines
Quoted 3 lines

Best regards,
Yang

--
Gleb.

Best regards,
Yang

#15 lore raw

Re: [PATCH v7 4/7] KVM: Add reset/restore rtc_status support

Quoted 111 lines · Yang Z

Gleb Natapov wrote on 2013-04-07:

Quoted 110 lines · Yang Z
Quoted 43 lines

Gleb Natapov wrote on 2013-04-07:

Quoted 42 lines · Yang Z
Quoted 41 lines

Gleb Natapov wrote on 2013-04-07:

Quoted 40 lines · Yang Z
Quoted 6 lines

Gleb Natapov wrote on 2013-04-07:

Quoted 5 lines · Yang Z
Quoted 4 lines

Gleb Natapov wrote on 2013-04-04:

Quoted 3 lines · Yang Zhang
Quoted 2 lines

From: Yang Zhang <[email protected]>

Quoted 11 lines
Quoted 11 lines
Quoted 11 lines
Quoted 11 lines

arch/x86/kvm/lapic.c static inline int apic_test_vector(int vec, void

Quoted 11 lines
---
 arch/x86/kvm/lapic.c |    9 +++++++++ arch/x86/kvm/lapic.h | 2
 ++ virt/kvm/ioapic.c    |   43

+++++++++++++++++++++++++++++++++++++++++++ virt/kvm/ioapic.h | 1 + 4 files changed, 55 insertions(+), 0 deletions(-)

arch/x86/kvm/lapic.c +0 -0
index 96ab160..9c041fa 100644
@@ -94,6 +94,14 @@ static inline int apic_test_vector(int vec, void

*bitmap)

Quoted 21 lines
Quoted 21 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
arch/x86/kvm/lapic.c +4 -0
 	return test_bit(VEC_POS(vec), (bitmap) + REG_POS(vec));
 }
+bool kvm_apic_pending_eoi(struct kvm_vcpu *vcpu, int vector)
+{
+	struct kvm_lapic *apic = vcpu->arch.apic;
+
Quoted 8 lines
Quoted 8 lines

arch/x86/kvm/lapic.c void kvm_apic_post_state_restore(struct

Quoted 8 lines
arch/x86/kvm/lapic.c +4 -0
+	return apic_test_vector(vector, apic->regs + APIC_ISR) ||
+		apic_test_vector(vector, apic->regs + APIC_IRR);
+}
+
 static inline void apic_set_vector(int vec, void *bitmap)
 {
 	set_bit(VEC_POS(vec), (bitmap) + REG_POS(vec));
@@ -1665,6 +1673,7 @@ void kvm_apic_post_state_restore(struct

kvm_vcpu

Quoted 6 lines
Quoted 6 lines

*vcpu,

Quoted 5 lines
arch/x86/kvm/lapic.c +0 -0
 	apic->highest_isr_cache = -1;
 	kvm_x86_ops->hwapic_isr_update(vcpu->kvm,
 apic_find_highest_isr(apic)); 	kvm_make_request(KVM_REQ_EVENT,
 vcpu); +	kvm_rtc_irq_restore(vcpu); }
 
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines

arch/x86/kvm/lapic.h static inline bool

Quoted 6 lines
arch/x86/kvm/lapic.c +0 -0
 void __kvm_migrate_apic_timer(struct kvm_vcpu *vcpu)
arch/x86/kvm/lapic.h +0 -0
index 967519c..004d2ad 100644
@@ -170,4 +170,6 @@ static inline bool

kvm_apic_has_events(struct

Quoted 11 lines
Quoted 11 lines
Quoted 11 lines
Quoted 11 lines
Quoted 11 lines
Quoted 11 lines
Quoted 11 lines
Quoted 11 lines

kvm_vcpu *vcpu)

virt/kvm/ioapic.c static unsigned long

Quoted 10 lines
arch/x86/kvm/lapic.h +2 -0
 	return vcpu->arch.apic->pending_events;
 }
+bool kvm_apic_pending_eoi(struct kvm_vcpu *vcpu, int vector);
+
 #endif
virt/kvm/ioapic.c +0 -0
index 8664812..0b12b17 100644
@@ -90,6 +90,47 @@ static unsigned long

ioapic_read_indirect(struct

Quoted 47 lines
Quoted 47 lines
Quoted 14 lines
Quoted 14 lines
Quoted 14 lines
Quoted 14 lines
Quoted 14 lines
Quoted 14 lines

kvm_ioapic *ioapic,

Quoted 13 lines
virt/kvm/ioapic.c +11 -0
 	return result;
 }
+static void rtc_irq_reset(struct kvm_ioapic *ioapic)
+{
+	ioapic->rtc_status.pending_eoi = 0;
+	bitmap_zero(ioapic->rtc_status.dest_map, KVM_MAX_VCPUS);
+}
+
+static void rtc_irq_restore(struct kvm_ioapic *ioapic)
+{
+	struct kvm_vcpu *vcpu;
+	int vector, i, pending_eoi = 0;
+
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

virt/kvm/ioapic.c static unsigned long · line 110

Quoted 8 lines
virt/kvm/ioapic.c +8 -0
+	if (RTC_GSI >= IOAPIC_NUM_PINS)
+		return;
+
+	vector = ioapic->redirtbl[RTC_GSI].fields.vector;
+	kvm_for_each_vcpu(i, vcpu, ioapic->kvm) {
+		if (kvm_apic_pending_eoi(vcpu, vector)) {
+			pending_eoi++;
+			__set_bit(vcpu->vcpu_id,

ioapic->rtc_status.dest_map);

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 1 line

You should cleat dest_map at the beginning to get rid of stale bits.

I thought kvm_set_ioapic is called only after save/restore or migration.

And

Quoted 10 lines
Quoted 10 lines

the

Quoted 9 lines
Quoted 9 lines

ioapic should be reset successfully before call it. So the dest_map is empty before call rtc_irq_restore().

Quoted 3 lines

But it is possible kvm_set_ioapic is called beside save/restore or migration. Right?

First of all userspace should not care when it calls kvm_set_ioapic() the kernel need to do the right thing. Second, believe it or not, kvm_ioapic_reset() is not called during system reset. Instead userspace reset it by calling kvm_set_ioapic() with ioapic state after reset.

Quoted 9 lines
Quoted 6 lines
Quoted 3 lines

Ok. I see. As the logic you suggested, it will clear dest_map if no pending eoi in vcpu, so we don't need to do it again.

You again rely on userspace doing thing in certain manner. What is set_lapic() is never called? Kernel internal state have to be correct after each ioctl call.

Sorry. I cannot figure out what's the problem if don't clear dest_map? Can you elaborate it?

What is not obvious about it? If there is a bit in dest_map that should be cleared after rtc_irq_restore() it will not.

Quoted 8 lines
Quoted 6 lines
Quoted 3 lines

Why it will not? If new_val is false, and the old_val is true. __clear_bit() will clear the dest_map. Am I wrong?

Ah, yes with new logic since we go over all vcpus and calculate new value for each one in theory it should be fine, but if we add cpu destruction this will be no longer true.

Yes. Given cpu destruction, it is wrong. Do you think we should clear dest_map now or delay it until cpu destruction is ready?

We will surely forgot it at that point and this is slow path. Lets clear it.

Quoted 23 lines
Quoted 5 lines
Quoted 2 lines

new_val = kvm_apic_pending_eoi(vcpu, vector);

Which reminds me there are more bugs in the current code. It is not enough to call kvm_apic_pending_eoi() to check the new value. You need to see if the entry is masked and vcpu is the destination of the interrupt too.

Yes, you are right.

Quoted 16 lines
Quoted 15 lines

old_val = test_bit(vcpu->vcpu_id, ioapic->rtc_status.dest_map);

if (new_val == old_val)

        return;
if (new_val) {
        __set_bit(vcpu->vcpu_id, ioapic->rtc_status.dest_map);
        ioapic->rtc_status.pending_eoi++; } else {
        __clear_bit(vcpu->vcpu_id, ioapic->rtc_status.dest_map);
        ioapic->rtc_status.pending_eoi--;
}

Best regards,
Yang

Quoted 7 lines
Quoted 2 lines

--
Gleb.

Best regards,
Yang

--
Gleb.

#16 lore raw

RE: [PATCH v7 4/7] KVM: Add reset/restore rtc_status support

In reply to Gleb Natapov

Gleb Natapov wrote on 2013-04-07:

Quoted 113 lines · Yang Z
Quoted 43 lines

Gleb Natapov wrote on 2013-04-07:

Quoted 42 lines · Yang Z
Quoted 41 lines

Gleb Natapov wrote on 2013-04-07:

Quoted 40 lines · Yang Z
Quoted 6 lines

Gleb Natapov wrote on 2013-04-07:

Quoted 5 lines · Yang Z
Quoted 4 lines

Gleb Natapov wrote on 2013-04-04:

Quoted 3 lines · Yang Zhang
Quoted 2 lines

From: Yang Zhang <[email protected]>

Quoted 11 lines
Quoted 11 lines
Quoted 11 lines
Quoted 11 lines

arch/x86/kvm/lapic.c static inline int apic_test_vector(int vec, void

Quoted 11 lines
---
 arch/x86/kvm/lapic.c |    9 +++++++++ arch/x86/kvm/lapic.h | 2
 ++ virt/kvm/ioapic.c    |   43

+++++++++++++++++++++++++++++++++++++++++++ virt/kvm/ioapic.h | 1 + 4 files changed, 55 insertions(+), 0 deletions(-)

arch/x86/kvm/lapic.c +0 -0
index 96ab160..9c041fa 100644
@@ -94,6 +94,14 @@ static inline int apic_test_vector(int vec, void

*bitmap)

Quoted 21 lines
Quoted 21 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
arch/x86/kvm/lapic.c +4 -0
 	return test_bit(VEC_POS(vec), (bitmap) + REG_POS(vec));
 }
+bool kvm_apic_pending_eoi(struct kvm_vcpu *vcpu, int vector)
+{
+	struct kvm_lapic *apic = vcpu->arch.apic;
+
Quoted 8 lines
Quoted 8 lines

arch/x86/kvm/lapic.c void kvm_apic_post_state_restore(struct

Quoted 8 lines
arch/x86/kvm/lapic.c +4 -0
+	return apic_test_vector(vector, apic->regs + APIC_ISR) ||
+		apic_test_vector(vector, apic->regs + APIC_IRR);
+}
+
 static inline void apic_set_vector(int vec, void *bitmap)
 {
 	set_bit(VEC_POS(vec), (bitmap) + REG_POS(vec));
@@ -1665,6 +1673,7 @@ void kvm_apic_post_state_restore(struct

kvm_vcpu

Quoted 6 lines
Quoted 6 lines

*vcpu,

Quoted 5 lines
arch/x86/kvm/lapic.c +0 -0
 	apic->highest_isr_cache = -1;
 	kvm_x86_ops->hwapic_isr_update(vcpu->kvm,
 apic_find_highest_isr(apic)); 	kvm_make_request(KVM_REQ_EVENT,
 vcpu); +	kvm_rtc_irq_restore(vcpu); }
 
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines

arch/x86/kvm/lapic.h static inline bool

Quoted 6 lines
arch/x86/kvm/lapic.c +0 -0
 void __kvm_migrate_apic_timer(struct kvm_vcpu *vcpu)
arch/x86/kvm/lapic.h +0 -0
index 967519c..004d2ad 100644
@@ -170,4 +170,6 @@ static inline bool

kvm_apic_has_events(struct

Quoted 11 lines
Quoted 11 lines
Quoted 11 lines
Quoted 11 lines
Quoted 11 lines
Quoted 11 lines
Quoted 11 lines
Quoted 11 lines

kvm_vcpu *vcpu)

virt/kvm/ioapic.c static unsigned long

Quoted 10 lines
arch/x86/kvm/lapic.h +2 -0
 	return vcpu->arch.apic->pending_events;
 }
+bool kvm_apic_pending_eoi(struct kvm_vcpu *vcpu, int vector);
+
 #endif
virt/kvm/ioapic.c +0 -0
index 8664812..0b12b17 100644
@@ -90,6 +90,47 @@ static unsigned long

ioapic_read_indirect(struct

Quoted 42 lines
Quoted 42 lines
Quoted 14 lines
Quoted 14 lines
Quoted 14 lines
Quoted 14 lines
Quoted 14 lines
Quoted 14 lines

kvm_ioapic *ioapic,

Quoted 13 lines
virt/kvm/ioapic.c +11 -0
 	return result;
 }
+static void rtc_irq_reset(struct kvm_ioapic *ioapic)
+{
+	ioapic->rtc_status.pending_eoi = 0;
+	bitmap_zero(ioapic->rtc_status.dest_map, KVM_MAX_VCPUS);
+}
+
+static void rtc_irq_restore(struct kvm_ioapic *ioapic)
+{
+	struct kvm_vcpu *vcpu;
+	int vector, i, pending_eoi = 0;
+
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

virt/kvm/ioapic.c static unsigned long · line 110

Quoted 8 lines
virt/kvm/ioapic.c +8 -0
+	if (RTC_GSI >= IOAPIC_NUM_PINS)
+		return;
+
+	vector = ioapic->redirtbl[RTC_GSI].fields.vector;
+	kvm_for_each_vcpu(i, vcpu, ioapic->kvm) {
+		if (kvm_apic_pending_eoi(vcpu, vector)) {
+			pending_eoi++;
+			__set_bit(vcpu->vcpu_id,

ioapic->rtc_status.dest_map);

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 1 line

You should cleat dest_map at the beginning to get rid of stale bits.

I thought kvm_set_ioapic is called only after save/restore or migration.

And

Quoted 16 lines
Quoted 16 lines

the

Quoted 5 lines
Quoted 5 lines

ioapic should be reset successfully before call it. So the dest_map is empty before call rtc_irq_restore().

Quoted 3 lines

But it is possible kvm_set_ioapic is called beside save/restore or migration. Right?

Quoted 7 lines
Quoted 4 lines

First of all userspace should not care when it calls kvm_set_ioapic() the kernel need to do the right thing. Second, believe it or not, kvm_ioapic_reset() is not called during system reset. Instead userspace reset it by calling kvm_set_ioapic() with ioapic state after reset.

Ok. I see. As the logic you suggested, it will clear dest_map if no pending eoi in vcpu, so we don't need to do it again.

You again rely on userspace doing thing in certain manner. What is set_lapic() is never called? Kernel internal state have to be correct after each ioctl call.

Quoted 8 lines
Quoted 5 lines
Quoted 3 lines

Sorry. I cannot figure out what's the problem if don't clear dest_map? Can you elaborate it?

What is not obvious about it? If there is a bit in dest_map that should be cleared after rtc_irq_restore() it will not.

Why it will not? If new_val is false, and the old_val is true. __clear_bit() will clear the dest_map. Am I wrong?

Quoted 8 lines

Ah, yes with new logic since we go over all vcpus and calculate new value for each one in theory it should be fine, but if we add cpu destruction this will be no longer true.

Quoted 1 line

new_val = kvm_apic_pending_eoi(vcpu, vector);

Which reminds me there are more bugs in the current code. It is not enough to call kvm_apic_pending_eoi() to check the new value. You need to see if the entry is masked and vcpu is the destination of the interrupt too.

No. kvm_apic_pending_eoi() is the right way. IOAPIC entry may change after vcpu received it before issue EOI, and we should not rely on the entry. For example:
vcpu A received the interrupt, pending in IRR
mask entry
migration happened

The only problem is we may account the interrupt from non-IOAPIC(from IPI) into RTC interrupt. But it's ok, we will clear pending_eoi in EOI regardless of source.

Best regards,
Yang

#17 lore raw

Re: [PATCH v7 4/7] KVM: Add reset/restore rtc_status support

Quoted 122 lines · Yang Z

Gleb Natapov wrote on 2013-04-07:

Quoted 113 lines · Yang Z
Quoted 43 lines

Gleb Natapov wrote on 2013-04-07:

Quoted 42 lines · Yang Z
Quoted 41 lines

Gleb Natapov wrote on 2013-04-07:

Quoted 40 lines · Yang Z
Quoted 6 lines

Gleb Natapov wrote on 2013-04-07:

Quoted 5 lines · Yang Z
Quoted 4 lines

Gleb Natapov wrote on 2013-04-04:

Quoted 3 lines · Yang Zhang
Quoted 2 lines

From: Yang Zhang <[email protected]>

Quoted 11 lines
Quoted 11 lines
Quoted 11 lines
Quoted 11 lines

arch/x86/kvm/lapic.c static inline int apic_test_vector(int vec, void

Quoted 11 lines
---
 arch/x86/kvm/lapic.c |    9 +++++++++ arch/x86/kvm/lapic.h | 2
 ++ virt/kvm/ioapic.c    |   43

+++++++++++++++++++++++++++++++++++++++++++ virt/kvm/ioapic.h | 1 + 4 files changed, 55 insertions(+), 0 deletions(-)

arch/x86/kvm/lapic.c +0 -0
index 96ab160..9c041fa 100644
@@ -94,6 +94,14 @@ static inline int apic_test_vector(int vec, void

*bitmap)

Quoted 21 lines
Quoted 21 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
arch/x86/kvm/lapic.c +4 -0
 	return test_bit(VEC_POS(vec), (bitmap) + REG_POS(vec));
 }
+bool kvm_apic_pending_eoi(struct kvm_vcpu *vcpu, int vector)
+{
+	struct kvm_lapic *apic = vcpu->arch.apic;
+
Quoted 8 lines
Quoted 8 lines

arch/x86/kvm/lapic.c void kvm_apic_post_state_restore(struct

Quoted 8 lines
arch/x86/kvm/lapic.c +4 -0
+	return apic_test_vector(vector, apic->regs + APIC_ISR) ||
+		apic_test_vector(vector, apic->regs + APIC_IRR);
+}
+
 static inline void apic_set_vector(int vec, void *bitmap)
 {
 	set_bit(VEC_POS(vec), (bitmap) + REG_POS(vec));
@@ -1665,6 +1673,7 @@ void kvm_apic_post_state_restore(struct

kvm_vcpu

Quoted 6 lines
Quoted 6 lines

*vcpu,

Quoted 5 lines
arch/x86/kvm/lapic.c +0 -0
 	apic->highest_isr_cache = -1;
 	kvm_x86_ops->hwapic_isr_update(vcpu->kvm,
 apic_find_highest_isr(apic)); 	kvm_make_request(KVM_REQ_EVENT,
 vcpu); +	kvm_rtc_irq_restore(vcpu); }
 
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines

arch/x86/kvm/lapic.h static inline bool

Quoted 6 lines
arch/x86/kvm/lapic.c +0 -0
 void __kvm_migrate_apic_timer(struct kvm_vcpu *vcpu)
arch/x86/kvm/lapic.h +0 -0
index 967519c..004d2ad 100644
@@ -170,4 +170,6 @@ static inline bool

kvm_apic_has_events(struct

Quoted 11 lines
Quoted 11 lines
Quoted 11 lines
Quoted 11 lines
Quoted 11 lines
Quoted 11 lines
Quoted 11 lines
Quoted 11 lines

kvm_vcpu *vcpu)

virt/kvm/ioapic.c static unsigned long

Quoted 10 lines
arch/x86/kvm/lapic.h +2 -0
 	return vcpu->arch.apic->pending_events;
 }
+bool kvm_apic_pending_eoi(struct kvm_vcpu *vcpu, int vector);
+
 #endif
virt/kvm/ioapic.c +0 -0
index 8664812..0b12b17 100644
@@ -90,6 +90,47 @@ static unsigned long

ioapic_read_indirect(struct

Quoted 42 lines
Quoted 42 lines
Quoted 14 lines
Quoted 14 lines
Quoted 14 lines
Quoted 14 lines
Quoted 14 lines
Quoted 14 lines

kvm_ioapic *ioapic,

Quoted 13 lines
virt/kvm/ioapic.c +11 -0
 	return result;
 }
+static void rtc_irq_reset(struct kvm_ioapic *ioapic)
+{
+	ioapic->rtc_status.pending_eoi = 0;
+	bitmap_zero(ioapic->rtc_status.dest_map, KVM_MAX_VCPUS);
+}
+
+static void rtc_irq_restore(struct kvm_ioapic *ioapic)
+{
+	struct kvm_vcpu *vcpu;
+	int vector, i, pending_eoi = 0;
+
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

virt/kvm/ioapic.c static unsigned long · line 110

Quoted 8 lines
virt/kvm/ioapic.c +8 -0
+	if (RTC_GSI >= IOAPIC_NUM_PINS)
+		return;
+
+	vector = ioapic->redirtbl[RTC_GSI].fields.vector;
+	kvm_for_each_vcpu(i, vcpu, ioapic->kvm) {
+		if (kvm_apic_pending_eoi(vcpu, vector)) {
+			pending_eoi++;
+			__set_bit(vcpu->vcpu_id,

ioapic->rtc_status.dest_map);

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 1 line

You should cleat dest_map at the beginning to get rid of stale bits.

I thought kvm_set_ioapic is called only after save/restore or migration.

And

Quoted 16 lines
Quoted 16 lines

the

Quoted 5 lines
Quoted 5 lines

ioapic should be reset successfully before call it. So the dest_map is empty before call rtc_irq_restore().

Quoted 3 lines

But it is possible kvm_set_ioapic is called beside save/restore or migration. Right?

Quoted 7 lines
Quoted 4 lines

First of all userspace should not care when it calls kvm_set_ioapic() the kernel need to do the right thing. Second, believe it or not, kvm_ioapic_reset() is not called during system reset. Instead userspace reset it by calling kvm_set_ioapic() with ioapic state after reset.

Ok. I see. As the logic you suggested, it will clear dest_map if no pending eoi in vcpu, so we don't need to do it again.

You again rely on userspace doing thing in certain manner. What is set_lapic() is never called? Kernel internal state have to be correct after each ioctl call.

Quoted 8 lines
Quoted 5 lines
Quoted 3 lines

Sorry. I cannot figure out what's the problem if don't clear dest_map? Can you elaborate it?

What is not obvious about it? If there is a bit in dest_map that should be cleared after rtc_irq_restore() it will not.

Why it will not? If new_val is false, and the old_val is true. __clear_bit() will clear the dest_map. Am I wrong?

Quoted 8 lines

Ah, yes with new logic since we go over all vcpus and calculate new value for each one in theory it should be fine, but if we add cpu destruction this will be no longer true.

Quoted 1 line

new_val = kvm_apic_pending_eoi(vcpu, vector);

Which reminds me there are more bugs in the current code. It is not enough to call kvm_apic_pending_eoi() to check the new value. You need to see if the entry is masked and vcpu is the destination of the interrupt too.

Quoted 8 lines

No. kvm_apic_pending_eoi() is the right way. IOAPIC entry may change after vcpu received it before issue EOI, and we should not rely on the entry. For example:
vcpu A received the interrupt, pending in IRR
mask entry
migration happened

The only problem is we may account the interrupt from non-IOAPIC(from IPI) into RTC interrupt. But it's ok, we will clear pending_eoi in EOI regardless of source.

With apicv this EOI may never come. We have to at least check that the vcpu is a destination for the interrupt.

--
Gleb.

#18 lore raw

RE: [PATCH v7 4/7] KVM: Add reset/restore rtc_status support

Gleb Natapov wrote on 2013-04-08:

Quoted 125 lines · Yang Z
Quoted 12 lines

Gleb Natapov wrote on 2013-04-07:

Quoted 11 lines · Yang Z
Quoted 10 lines

Gleb Natapov wrote on 2013-04-07:

Quoted 9 lines · Yang Z
Quoted 8 lines

Gleb Natapov wrote on 2013-04-07:

Quoted 7 lines · Yang Z
Quoted 6 lines

Gleb Natapov wrote on 2013-04-07:

Quoted 5 lines · Yang Z
Quoted 4 lines

Gleb Natapov wrote on 2013-04-04:

Quoted 3 lines · Yang Zhang
Quoted 2 lines

From: Yang Zhang <[email protected]>

Quoted 12 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines

---
arch/x86/kvm/lapic.c | 9 +++++++++ arch/x86/kvm/lapic.h |

 2 ++ virt/kvm/ioapic.c    |   43
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

arch/x86/kvm/lapic.c static inline int apic_test_vector(int vec,

Quoted 8 lines

+++++++++++++++++++++++++++++++++++++++++++
virt/kvm/ioapic.h | 1 + 4 files changed, 55 insertions(+), 0 deletions(-)

arch/x86/kvm/lapic.c +0 -0
index 96ab160..9c041fa 100644
@@ -94,6 +94,14 @@ static inline int apic_test_vector(int vec,

void

Quoted 15 lines
Quoted 15 lines
Quoted 15 lines
Quoted 15 lines
Quoted 15 lines
Quoted 15 lines

*bitmap)

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
arch/x86/kvm/lapic.c +4 -0
 	return test_bit(VEC_POS(vec), (bitmap) + REG_POS(vec));
 }
+bool kvm_apic_pending_eoi(struct kvm_vcpu *vcpu, int vector)
+{
+	struct kvm_lapic *apic = vcpu->arch.apic;
+
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

arch/x86/kvm/lapic.c void

Quoted 8 lines
arch/x86/kvm/lapic.c +4 -0
+	return apic_test_vector(vector, apic->regs + APIC_ISR) ||
+		apic_test_vector(vector, apic->regs + APIC_IRR);
+}
+
 static inline void apic_set_vector(int vec, void *bitmap)
 {
 	set_bit(VEC_POS(vec), (bitmap) + REG_POS(vec));
@@ -1665,6 +1673,7 @@ void

kvm_apic_post_state_restore(struct

Quoted 41 lines
Quoted 41 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

kvm_vcpu

Quoted 7 lines
Quoted 7 lines

*vcpu,

Quoted 6 lines
arch/x86/kvm/lapic.c +0 -0
 	apic->highest_isr_cache = -1;
 	kvm_x86_ops->hwapic_isr_update(vcpu->kvm,
 apic_find_highest_isr(apic));
 	kvm_make_request(KVM_REQ_EVENT, vcpu);
 +	kvm_rtc_irq_restore(vcpu); }
 
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines

arch/x86/kvm/lapic.h static inline bool

Quoted 6 lines
arch/x86/kvm/lapic.c +0 -0
 void __kvm_migrate_apic_timer(struct kvm_vcpu *vcpu)
arch/x86/kvm/lapic.h +0 -0
index 967519c..004d2ad 100644
@@ -170,4 +170,6 @@ static inline bool

kvm_apic_has_events(struct

Quoted 11 lines
Quoted 11 lines
Quoted 11 lines
Quoted 11 lines
Quoted 11 lines
Quoted 11 lines
Quoted 11 lines
Quoted 11 lines

kvm_vcpu *vcpu)

virt/kvm/ioapic.c static unsigned long

Quoted 10 lines
arch/x86/kvm/lapic.h +2 -0
 	return vcpu->arch.apic->pending_events;
 }
+bool kvm_apic_pending_eoi(struct kvm_vcpu *vcpu, int vector);
+
 #endif
virt/kvm/ioapic.c +0 -0
index 8664812..0b12b17 100644
@@ -90,6 +90,47 @@ static unsigned long

ioapic_read_indirect(struct

Quoted 14 lines
Quoted 14 lines
Quoted 13 lines
Quoted 13 lines
Quoted 13 lines
Quoted 13 lines
Quoted 13 lines
Quoted 13 lines

kvm_ioapic *ioapic,

virt/kvm/ioapic.c static unsigned long · line 101

Quoted 12 lines
virt/kvm/ioapic.c +8 -0
 	return result;
 }
+static void rtc_irq_reset(struct kvm_ioapic *ioapic) +{
+	ioapic->rtc_status.pending_eoi = 0;
+	bitmap_zero(ioapic->rtc_status.dest_map, KVM_MAX_VCPUS); +}
+ +static void rtc_irq_restore(struct kvm_ioapic *ioapic) +{
+	struct kvm_vcpu *vcpu; +	int vector, i, pending_eoi = 0; +
+	if (RTC_GSI >= IOAPIC_NUM_PINS) +		return; + +	vector =
 ioapic->redirtbl[RTC_GSI].fields.vector;
+	kvm_for_each_vcpu(i, vcpu, ioapic->kvm) { +		if
 (kvm_apic_pending_eoi(vcpu, vector)) { +			pending_eoi++;
+			__set_bit(vcpu->vcpu_id,

ioapic->rtc_status.dest_map);

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 1 line

You should cleat dest_map at the beginning to get rid of stale bits.

I thought kvm_set_ioapic is called only after save/restore or

migration.

Quoted 39 lines
Quoted 26 lines
Quoted 18 lines
Quoted 18 lines

And

Quoted 17 lines
Quoted 17 lines

the

Quoted 5 lines
Quoted 5 lines

ioapic should be reset successfully before call it. So the dest_map is empty before call rtc_irq_restore().

Quoted 3 lines

But it is possible kvm_set_ioapic is called beside save/restore or migration. Right?

Quoted 8 lines
Quoted 5 lines

First of all userspace should not care when it calls kvm_set_ioapic() the kernel need to do the right thing. Second, believe it or not, kvm_ioapic_reset() is not called during system reset. Instead userspace reset it by calling kvm_set_ioapic() with ioapic state after reset.

Ok. I see. As the logic you suggested, it will clear dest_map if no pending eoi in vcpu, so we don't need to do it again.

You again rely on userspace doing thing in certain manner. What is set_lapic() is never called? Kernel internal state have to be correct after each ioctl call.

Quoted 8 lines
Quoted 5 lines
Quoted 3 lines

Sorry. I cannot figure out what's the problem if don't clear dest_map? Can you elaborate it?

What is not obvious about it? If there is a bit in dest_map that should be cleared after rtc_irq_restore() it will not.

Why it will not? If new_val is false, and the old_val is true. __clear_bit() will clear the dest_map. Am I wrong?

Quoted 8 lines

Ah, yes with new logic since we go over all vcpus and calculate new value for each one in theory it should be fine, but if we add cpu destruction this will be no longer true.

Quoted 1 line

new_val = kvm_apic_pending_eoi(vcpu, vector);

Which reminds me there are more bugs in the current code. It is not enough to call kvm_apic_pending_eoi() to check the new value. You need to see if the entry is masked and vcpu is the destination of the interrupt too.

No. kvm_apic_pending_eoi() is the right way. IOAPIC entry may change after vcpu received it before issue EOI, and we should not rely on the entry. For example: vcpu A received the interrupt, pending in IRR mask entry migration happened

Quoted 6 lines
Quoted 1 line

The only problem is we may account the interrupt from non-IOAPIC(from IPI)

into RTC interrupt. But it's ok, we will clear pending_eoi in EOI regardless of source.

Quoted 1 line

With apicv this EOI may never come. We have to at least check that the vcpu is a destination for the interrupt.

You are right. This is necessary/

Best regards,
Yang

author
#19 lore raw

PATCH v7 5/7 KVM : Force vmexit with virtual interrupt delivery

In reply to Yang Zhang

From: Yang Zhang <[email protected]>

Need the EOI to track interrupt deliver status, so force vmexit on EOI for rtc interrupt when enabling virtual interrupt delivery.

---
 virt/kvm/ioapic.c |    2 +-
 1 files changed, 1 insertions(+), 1 deletions(-)
virt/kvm/ioapic.c +1 -1
index 0b12b17..d01544a 100644
@@ -175,7 +175,7 @@ void kvm_ioapic_calculate_eoi_exitmap(struct kvm_vcpu *vcpu,
 		if (!e->fields.mask &&
 			(e->fields.trig_mode == IOAPIC_LEVEL_TRIG ||
 			 kvm_irq_has_notifier(ioapic->kvm, KVM_IRQCHIP_IOAPIC,
-				 index))) {
+				 index) || index == RTC_GSI)) {
 			irqe.dest_id = e->fields.dest_id;
 			irqe.vector = e->fields.vector;
 			irqe.dest_mode = e->fields.dest_mode;
Signature · 1 line
1.7.1
author
#20 lore raw

PATCH v7 6/7 KVM: Let ioapic know the irq line status

In reply to Yang Zhang

From: Yang Zhang <[email protected]>

Userspace may deliver RTC interrupt without query the status. So we want to track RTC EOI for this case.

---
 arch/x86/kvm/i8254.c     |    4 ++--
 arch/x86/kvm/x86.c       |    6 ++++--
 include/linux/kvm_host.h |   11 +++++++----
 virt/kvm/assigned-dev.c  |   13 +++++++------
 virt/kvm/eventfd.c       |   15 +++++++++------
 virt/kvm/ioapic.c        |   18 ++++++++++--------
 virt/kvm/ioapic.h        |    2 +-
 virt/kvm/irq_comm.c      |   19 ++++++++++++-------
 virt/kvm/kvm_main.c      |    3 ++-
 9 files changed, 54 insertions(+), 37 deletions(-)
arch/x86/kvm/i8254.c +2 -2
index c1d30b2..412a5aa 100644
@@ -290,8 +290,8 @@ static void pit_do_work(struct kthread_work *work)
 	}
 	spin_unlock(&ps->inject_lock);
 	if (inject) {
-		kvm_set_irq(kvm, kvm->arch.vpit->irq_source_id, 0, 1);
-		kvm_set_irq(kvm, kvm->arch.vpit->irq_source_id, 0, 0);
+		kvm_set_irq(kvm, kvm->arch.vpit->irq_source_id, 0, 1, false);
+		kvm_set_irq(kvm, kvm->arch.vpit->irq_source_id, 0, 0, false);
 
 		/*
 		 * Provides NMI watchdog support via Virtual Wire mode.
arch/x86/kvm/x86.c +4 -2
index 2aaba81..5e85d8d 100644
@@ -3484,13 +3484,15 @@ out:
 	return r;
 }
 
-int kvm_vm_ioctl_irq_line(struct kvm *kvm, struct kvm_irq_level *irq_event)
+int kvm_vm_ioctl_irq_line(struct kvm *kvm, struct kvm_irq_level *irq_event,
+			bool line_status)
 {
 	if (!irqchip_in_kernel(kvm))
 		return -ENXIO;
 
 	irq_event->status = kvm_set_irq(kvm, KVM_USERSPACE_IRQ_SOURCE_ID,
-					irq_event->irq, irq_event->level);
+					irq_event->irq, irq_event->level,
+					line_status);
 	return 0;
 }
 
include/linux/kvm_host.h +7 -4
index 1c0be23..7bcdb6b 100644
@@ -289,7 +289,8 @@ struct kvm_kernel_irq_routing_entry {
 	u32 gsi;
 	u32 type;
 	int (*set)(struct kvm_kernel_irq_routing_entry *e,
-		   struct kvm *kvm, int irq_source_id, int level);
+		   struct kvm *kvm, int irq_source_id, int level,
+		   bool line_status);
 	union {
 		struct {
 			unsigned irqchip;
@@ -588,7 +589,8 @@ int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm,
 
 int kvm_vm_ioctl_set_memory_region(struct kvm *kvm,
 				   struct kvm_userspace_memory_region *mem);
-int kvm_vm_ioctl_irq_line(struct kvm *kvm, struct kvm_irq_level *irq_level);
+int kvm_vm_ioctl_irq_line(struct kvm *kvm, struct kvm_irq_level *irq_level,
+			bool line_status);
 long kvm_arch_vm_ioctl(struct file *filp,
 		       unsigned int ioctl, unsigned long arg);
 
@@ -719,10 +721,11 @@ void kvm_get_intr_delivery_bitmask(struct kvm_ioapic *ioapic,
 				   union kvm_ioapic_redirect_entry *entry,
 				   unsigned long *deliver_bitmask);
 #endif
-int kvm_set_irq(struct kvm *kvm, int irq_source_id, u32 irq, int level);
+int kvm_set_irq(struct kvm *kvm, int irq_source_id, u32 irq, int level,
+		bool line_status);
 int kvm_set_irq_inatomic(struct kvm *kvm, int irq_source_id, u32 irq, int level);
 int kvm_set_msi(struct kvm_kernel_irq_routing_entry *irq_entry, struct kvm *kvm,
-		int irq_source_id, int level);
+		int irq_source_id, int level, bool line_status);
 bool kvm_irq_has_notifier(struct kvm *kvm, unsigned irqchip, unsigned pin);
 void kvm_notify_acked_irq(struct kvm *kvm, unsigned irqchip, unsigned pin);
 void kvm_register_irq_ack_notifier(struct kvm *kvm,
virt/kvm/assigned-dev.c +7 -6
index 3642239..f4c7f59 100644
@@ -80,11 +80,12 @@ kvm_assigned_dev_raise_guest_irq(struct kvm_assigned_dev_kernel *assigned_dev,
 		spin_lock(&assigned_dev->intx_mask_lock);
 		if (!(assigned_dev->flags & KVM_DEV_ASSIGN_MASK_INTX))
 			kvm_set_irq(assigned_dev->kvm,
-				    assigned_dev->irq_source_id, vector, 1);
+				    assigned_dev->irq_source_id, vector, 1,
+				    false);
 		spin_unlock(&assigned_dev->intx_mask_lock);
 	} else
 		kvm_set_irq(assigned_dev->kvm, assigned_dev->irq_source_id,
-			    vector, 1);
+			    vector, 1, false);
 }
 
 static irqreturn_t kvm_assigned_dev_thread_intx(int irq, void *dev_id)
@@ -165,7 +166,7 @@ static void kvm_assigned_dev_ack_irq(struct kvm_irq_ack_notifier *kian)
 		container_of(kian, struct kvm_assigned_dev_kernel,
 			     ack_notifier);
 
-	kvm_set_irq(dev->kvm, dev->irq_source_id, dev->guest_irq, 0);
+	kvm_set_irq(dev->kvm, dev->irq_source_id, dev->guest_irq, 0, false);
 
 	spin_lock(&dev->intx_mask_lock);
 
@@ -188,7 +189,7 @@ static void kvm_assigned_dev_ack_irq(struct kvm_irq_ack_notifier *kian)
 
 		if (reassert)
 			kvm_set_irq(dev->kvm, dev->irq_source_id,
-				    dev->guest_irq, 1);
+				    dev->guest_irq, 1, false);
 	}
 
 	spin_unlock(&dev->intx_mask_lock);
@@ -202,7 +203,7 @@ static void deassign_guest_irq(struct kvm *kvm,
 						&assigned_dev->ack_notifier);
 
 	kvm_set_irq(assigned_dev->kvm, assigned_dev->irq_source_id,
-		    assigned_dev->guest_irq, 0);
+		    assigned_dev->guest_irq, 0, false);
 
 	if (assigned_dev->irq_source_id != -1)
 		kvm_free_irq_source_id(kvm, assigned_dev->irq_source_id);
@@ -901,7 +902,7 @@ static int kvm_vm_ioctl_set_pci_irq_mask(struct kvm *kvm,
 	if (match->irq_requested_type & KVM_DEV_IRQ_GUEST_INTX) {
 		if (assigned_dev->flags & KVM_DEV_ASSIGN_MASK_INTX) {
 			kvm_set_irq(match->kvm, match->irq_source_id,
-				    match->guest_irq, 0);
+				    match->guest_irq, 0, false);
 			/*
 			 * Masking at hardware-level is performed on demand,
 			 * i.e. when an IRQ actually arrives at the host.
virt/kvm/eventfd.c +9 -6
index 020522e..36512f3 100644
@@ -100,11 +100,13 @@ irqfd_inject(struct work_struct *work)
 	struct kvm *kvm = irqfd->kvm;
 
 	if (!irqfd->resampler) {
-		kvm_set_irq(kvm, KVM_USERSPACE_IRQ_SOURCE_ID, irqfd->gsi, 1);
-		kvm_set_irq(kvm, KVM_USERSPACE_IRQ_SOURCE_ID, irqfd->gsi, 0);
+		kvm_set_irq(kvm, KVM_USERSPACE_IRQ_SOURCE_ID, irqfd->gsi, 1,
+				false);
+		kvm_set_irq(kvm, KVM_USERSPACE_IRQ_SOURCE_ID, irqfd->gsi, 0,
+				false);
 	} else
 		kvm_set_irq(kvm, KVM_IRQFD_RESAMPLE_IRQ_SOURCE_ID,
-			    irqfd->gsi, 1);
+			    irqfd->gsi, 1, false);
 }
 
 /*
@@ -121,7 +123,7 @@ irqfd_resampler_ack(struct kvm_irq_ack_notifier *kian)
 	resampler = container_of(kian, struct _irqfd_resampler, notifier);
 
 	kvm_set_irq(resampler->kvm, KVM_IRQFD_RESAMPLE_IRQ_SOURCE_ID,
-		    resampler->notifier.gsi, 0);
+		    resampler->notifier.gsi, 0, false);
 
 	rcu_read_lock();
 
@@ -146,7 +148,7 @@ irqfd_resampler_shutdown(struct _irqfd *irqfd)
 		list_del(&resampler->link);
 		kvm_unregister_irq_ack_notifier(kvm, &resampler->notifier);
 		kvm_set_irq(kvm, KVM_IRQFD_RESAMPLE_IRQ_SOURCE_ID,
-			    resampler->notifier.gsi, 0);
+			    resampler->notifier.gsi, 0, false);
 		kfree(resampler);
 	}
 
@@ -225,7 +227,8 @@ irqfd_wakeup(wait_queue_t *wait, unsigned mode, int sync, void *key)
 		irq = rcu_dereference(irqfd->irq_entry);
 		/* An event has been signaled, inject an interrupt */
 		if (irq)
-			kvm_set_msi(irq, kvm, KVM_USERSPACE_IRQ_SOURCE_ID, 1);
+			kvm_set_msi(irq, kvm, KVM_USERSPACE_IRQ_SOURCE_ID, 1,
+					false);
 		else
 			schedule_work(&irqfd->inject);
 		rcu_read_unlock();
virt/kvm/ioapic.c +10 -8
index d01544a..ea26a5a 100644
@@ -50,7 +50,8 @@
 #else
 #define ioapic_debug(fmt, arg...)
 #endif
-static int ioapic_deliver(struct kvm_ioapic *vioapic, int irq);
+static int ioapic_deliver(struct kvm_ioapic *vioapic, int irq,
+		bool line_status);
 
 static unsigned long ioapic_read_indirect(struct kvm_ioapic *ioapic,
 					  unsigned long addr,
@@ -131,7 +132,8 @@ void kvm_rtc_irq_restore(struct kvm_vcpu *vcpu)
 	spin_unlock(&ioapic->lock);
 }
 
-static int ioapic_service(struct kvm_ioapic *ioapic, unsigned int idx)
+static int ioapic_service(struct kvm_ioapic *ioapic, unsigned int idx,
+		bool line_status)
 {
 	union kvm_ioapic_redirect_entry *pent;
 	int injected = -1;
@@ -139,7 +141,7 @@ static int ioapic_service(struct kvm_ioapic *ioapic, unsigned int idx)
 	pent = &ioapic->redirtbl[idx];
 
 	if (!pent->fields.mask) {
-		injected = ioapic_deliver(ioapic, idx);
+		injected = ioapic_deliver(ioapic, idx, line_status);
 		if (injected && pent->fields.trig_mode == IOAPIC_LEVEL_TRIG)
 			pent->fields.remote_irr = 1;
 	}
@@ -236,13 +238,13 @@ static void ioapic_write_indirect(struct kvm_ioapic *ioapic, u32 val)
 			kvm_fire_mask_notifiers(ioapic->kvm, KVM_IRQCHIP_IOAPIC, index, mask_after);
 		if (e->fields.trig_mode == IOAPIC_LEVEL_TRIG
 		    && ioapic->irr & (1 << index))
-			ioapic_service(ioapic, index);
+			ioapic_service(ioapic, index, false);
 		kvm_ioapic_make_eoibitmap_request(ioapic->kvm);
 		break;
 	}
 }
 
-static int ioapic_deliver(struct kvm_ioapic *ioapic, int irq)
+static int ioapic_deliver(struct kvm_ioapic *ioapic, int irq, bool line_status)
 {
 	union kvm_ioapic_redirect_entry *entry = &ioapic->redirtbl[irq];
 	struct kvm_lapic_irq irqe;
@@ -265,7 +267,7 @@ static int ioapic_deliver(struct kvm_ioapic *ioapic, int irq)
 }
 
 int kvm_ioapic_set_irq(struct kvm_ioapic *ioapic, int irq, int irq_source_id,
-		       int level)
+		       int level, bool line_status)
 {
 	u32 old_irr;
 	u32 mask = 1 << irq;
@@ -288,7 +290,7 @@ int kvm_ioapic_set_irq(struct kvm_ioapic *ioapic, int irq, int irq_source_id,
 		ioapic->irr |= mask;
 		if ((edge && old_irr != ioapic->irr) ||
 		    (!edge && !entry.fields.remote_irr))
-			ret = ioapic_service(ioapic, irq);
+			ret = ioapic_service(ioapic, irq, line_status);
 		else
 			ret = 0; /* report coalesced interrupt */
 	}
@@ -337,7 +339,7 @@ static void __kvm_ioapic_update_eoi(struct kvm_vcpu *vcpu,
 		ASSERT(ent->fields.trig_mode == IOAPIC_LEVEL_TRIG);
 		ent->fields.remote_irr = 0;
 		if (!ent->fields.mask && (ioapic->irr & (1 << i)))
-			ioapic_service(ioapic, i);
+			ioapic_service(ioapic, i, false);
 	}
 }
 
virt/kvm/ioapic.h +1 -1
index 2692873..2f48d92 100644
@@ -89,7 +89,7 @@ bool kvm_ioapic_handles_vector(struct kvm *kvm, int vector);
 int kvm_ioapic_init(struct kvm *kvm);
 void kvm_ioapic_destroy(struct kvm *kvm);
 int kvm_ioapic_set_irq(struct kvm_ioapic *ioapic, int irq, int irq_source_id,
-		       int level);
+		       int level, bool line_status);
 void kvm_ioapic_clear_all(struct kvm_ioapic *ioapic, int irq_source_id);
 void kvm_ioapic_reset(struct kvm_ioapic *ioapic);
 int kvm_irq_delivery_to_apic(struct kvm *kvm, struct kvm_lapic *src,
virt/kvm/irq_comm.c +12 -7
index 2f07d2e..8efb580 100644
@@ -35,7 +35,8 @@
 #include "ioapic.h"
 
 static int kvm_set_pic_irq(struct kvm_kernel_irq_routing_entry *e,
-			   struct kvm *kvm, int irq_source_id, int level)
+			   struct kvm *kvm, int irq_source_id, int level,
+			   bool line_status)
 {
 #ifdef CONFIG_X86
 	struct kvm_pic *pic = pic_irqchip(kvm);
@@ -46,10 +47,12 @@ static int kvm_set_pic_irq(struct kvm_kernel_irq_routing_entry *e,
 }
 
 static int kvm_set_ioapic_irq(struct kvm_kernel_irq_routing_entry *e,
-			      struct kvm *kvm, int irq_source_id, int level)
+			      struct kvm *kvm, int irq_source_id, int level,
+			      bool line_status)
 {
 	struct kvm_ioapic *ioapic = kvm->arch.vioapic;
-	return kvm_ioapic_set_irq(ioapic, e->irqchip.pin, irq_source_id, level);
+	return kvm_ioapic_set_irq(ioapic, e->irqchip.pin, irq_source_id, level,
+				line_status);
 }
 
 inline static bool kvm_is_dm_lowest_prio(struct kvm_lapic_irq *irq)
@@ -121,7 +124,7 @@ static inline void kvm_set_msi_irq(struct kvm_kernel_irq_routing_entry *e,
 }
 
 int kvm_set_msi(struct kvm_kernel_irq_routing_entry *e,
-		struct kvm *kvm, int irq_source_id, int level)
+		struct kvm *kvm, int irq_source_id, int level, bool line_status)
 {
 	struct kvm_lapic_irq irq;
 
@@ -159,7 +162,7 @@ int kvm_send_userspace_msi(struct kvm *kvm, struct kvm_msi *msi)
 	route.msi.address_hi = msi->address_hi;
 	route.msi.data = msi->data;
 
-	return kvm_set_msi(&route, kvm, KVM_USERSPACE_IRQ_SOURCE_ID, 1);
+	return kvm_set_msi(&route, kvm, KVM_USERSPACE_IRQ_SOURCE_ID, 1, false);
 }
 
 /*
@@ -168,7 +171,8 @@ int kvm_send_userspace_msi(struct kvm *kvm, struct kvm_msi *msi)
  *  = 0   Interrupt was coalesced (previous irq is still pending)
  *  > 0   Number of CPUs interrupt was delivered to
  */
-int kvm_set_irq(struct kvm *kvm, int irq_source_id, u32 irq, int level)
+int kvm_set_irq(struct kvm *kvm, int irq_source_id, u32 irq, int level,
+		bool line_status)
 {
 	struct kvm_kernel_irq_routing_entry *e, irq_set[KVM_NR_IRQCHIPS];
 	int ret = -1, i = 0;
@@ -189,7 +193,8 @@ int kvm_set_irq(struct kvm *kvm, int irq_source_id, u32 irq, int level)
 
 	while(i--) {
 		int r;
-		r = irq_set[i].set(&irq_set[i], kvm, irq_source_id, level);
+		r = irq_set[i].set(&irq_set[i], kvm, irq_source_id, level,
+				line_status);
 		if (r < 0)
 			continue;
 
virt/kvm/kvm_main.c +2 -1
index ff71541..6c4842a 100644
@@ -2258,7 +2258,8 @@ static long kvm_vm_ioctl(struct file *filp,
 		if (copy_from_user(&irq_event, argp, sizeof irq_event))
 			goto out;
 
-		r = kvm_vm_ioctl_irq_line(kvm, &irq_event);
+		r = kvm_vm_ioctl_irq_line(kvm, &irq_event,
+					ioctl == KVM_IRQ_LINE_STATUS);
 		if (r)
 			goto out;
 
Signature · 1 line
1.7.1
author
#21 lore raw

PATCH v7 7/7 KVM: Use eoi to track RTC interrupt delivery status

In reply to Yang Zhang

From: Yang Zhang <[email protected]>

Current interrupt coalescing logci which only used by RTC has conflict with Posted Interrupt.
This patch introduces a new mechinism to use eoi to track interrupt:
When delivering an interrupt to vcpu, the pending_eoi set to number of vcpu that received the interrupt. And decrease it when each vcpu writing eoi. No subsequent RTC interrupt can deliver to vcpu until all vcpus write eoi.

---
 virt/kvm/ioapic.c |   40 +++++++++++++++++++++++++++++++++++++++-
 1 files changed, 39 insertions(+), 1 deletions(-)
virt/kvm/ioapic.c +39 -1
index ea26a5a..96a226d 100644
@@ -132,6 +132,29 @@ void kvm_rtc_irq_restore(struct kvm_vcpu *vcpu)
 	spin_unlock(&ioapic->lock);
 }
 
+static void rtc_irq_ack_eoi(struct kvm_ioapic *ioapic, struct kvm_vcpu *vcpu,
+			int irq)
+{
+	if (irq != RTC_GSI)
+		return;
+
+	if (test_and_clear_bit(vcpu->vcpu_id, ioapic->rtc_status.dest_map))
+		--ioapic->rtc_status.pending_eoi;
+
+	WARN_ON(ioapic->rtc_status.pending_eoi < 0);
+}
+
+static bool rtc_irq_check(struct kvm_ioapic *ioapic, int irq, bool line_status)
+{
+	if (irq != RTC_GSI || !line_status)
+		return false;
+
+	if (ioapic->rtc_status.pending_eoi > 0)
+		return true; /* coalesced */
+
+	return false;
+}
+
 static int ioapic_service(struct kvm_ioapic *ioapic, unsigned int idx,
 		bool line_status)
 {
@@ -248,6 +271,7 @@ static int ioapic_deliver(struct kvm_ioapic *ioapic, int irq, bool line_status)
 {
 	union kvm_ioapic_redirect_entry *entry = &ioapic->redirtbl[irq];
 	struct kvm_lapic_irq irqe;
+	int ret;
 
 	ioapic_debug("dest=%x dest_mode=%x delivery_mode=%x "
 		     "vector=%x trig_mode=%x\n",
@@ -263,7 +287,14 @@ static int ioapic_deliver(struct kvm_ioapic *ioapic, int irq, bool line_status)
 	irqe.level = 1;
 	irqe.shorthand = 0;
 
-	return kvm_irq_delivery_to_apic(ioapic->kvm, NULL, &irqe, NULL);
+	if (irq == RTC_GSI && line_status) {
+		ret = kvm_irq_delivery_to_apic(ioapic->kvm, NULL, &irqe,
+				ioapic->rtc_status.dest_map);
+		ioapic->rtc_status.pending_eoi = ret;
+	} else
+		ret = kvm_irq_delivery_to_apic(ioapic->kvm, NULL, &irqe, NULL);
+
+	return ret;
 }
 
 int kvm_ioapic_set_irq(struct kvm_ioapic *ioapic, int irq, int irq_source_id,
@@ -287,6 +318,11 @@ int kvm_ioapic_set_irq(struct kvm_ioapic *ioapic, int irq, int irq_source_id,
 		ret = 1;
 	} else {
 		int edge = (entry.fields.trig_mode == IOAPIC_EDGE_TRIG);
+
+		if (rtc_irq_check(ioapic, irq, line_status)) {
+			ret = 0; /* coalesced */
+			goto out;
+		}
 		ioapic->irr |= mask;
 		if ((edge && old_irr != ioapic->irr) ||
 		    (!edge && !entry.fields.remote_irr))
@@ -294,6 +330,7 @@ int kvm_ioapic_set_irq(struct kvm_ioapic *ioapic, int irq, int irq_source_id,
 		else
 			ret = 0; /* report coalesced interrupt */
 	}
+out:
 	trace_kvm_ioapic_set_irq(entry.bits, irq, ret == 0);
 	spin_unlock(&ioapic->lock);
 
@@ -321,6 +358,7 @@ static void __kvm_ioapic_update_eoi(struct kvm_vcpu *vcpu,
 		if (ent->fields.vector != vector)
 			continue;
 
+		rtc_irq_ack_eoi(ioapic, vcpu, i);
 		/*
 		 * We are dropping lock while calling ack notifiers because ack
 		 * notifier callbacks for assigned devices call into IOAPIC
Signature · 1 line
1.7.1