Skip to contentSkip to conversation

kvm: s390: Improve floating IRQ injection behavior

PATCH Superseded Janosch Frank 2 participants 3 messages Aug 13 – Aug 14

Subsystems (3)

v1 of 5 Superseded Latest is v5 →

lore
Share & tools
lore

Janosch Frank

author maintainer
#0 lore raw

PATCH 1/1 kvm: s390: Improve floating IRQ injection behavior

Floating IRQs can be handled by any VCPU that opened its masks. The current design does not check if the mask is open when a floating IRQ is injected via the FLIC. It will wakeup the last VCPU that went sleeping hoping it's the correct one.

Improve this by at least checking if the VCPU has pending IRQs and if not try to find another VCPU which can take the IRQ.

Also add a function to distribute floating IRQs which can be called on VCPU enter or exit. It will check for pending floating IRQs and wakeup sleeping VCPUs which have pending IRQs.

This is not the final fix around this topic but we'll eventually inject a pending IRQ. The current code can easily deadlock a VM and with this fix we work around that.

This will fix this test's hangs as well as another hang that's already been present before I added the new test code. See:
https://lore.kernel.org/kvm/[email protected]/T/#u

Fixes 352ccf890a3e ("KVM: s390: improve interrupt cpu for wakeup")

---

Currently this is in the entry path and therefore might affect latency. I chose to do it this way as we inject IRQs into the current cpu a couple of lines above the distribute_float_irqs() call which should mean we have a higher chance that no other floating IRQs are pending since we already handled an IRQ.

A simple boot of a smp guest resulted in 275 injects via the new code.

I haven't done any performance tests yet.

---
 arch/s390/include/asm/kvm_host_s390.h |  1 +
 arch/s390/kvm/s390/interrupt.c        | 34 ++++++++++++++++++++++++++-
 arch/s390/kvm/s390/s390.c             |  3 +++
 arch/s390/kvm/s390/s390.h             |  1 +
 4 files changed, 38 insertions(+), 1 deletion(-)
arch/s390/include/asm/kvm_host_s390.h +1 -0
index cd692f8fb764..a86863e621b7 100644
@@ -412,6 +412,7 @@ struct kvm_vm_stat {
 	u64 gmap_shadow_r3_entry;
 	u64 gmap_shadow_sg_entry;
 	u64 gmap_shadow_pg_entry;
+	u64 inject_redist;
 };
 
 struct kvm_arch_memory_slot {
arch/s390/kvm/s390/interrupt.c +33 -1
index 0381ae981703..663fc2e3eca5 100644
@@ -372,6 +372,37 @@ static unsigned long deliverable_irqs(struct kvm_vcpu *vcpu)
 	return active_mask;
 }
 
+void distribute_float_irqs(struct kvm *kvm)
+{
+	struct kvm_vcpu *dst_vcpu;
+	int sigcpu, online_vcpus;
+
+	if (!READ_ONCE(kvm->arch.float_int.pending_irqs))
+		return;
+
+	online_vcpus = atomic_read(&kvm->online_vcpus);
+
+	/*
+	 * Not too worried about synchronization for idle_mask. We
+	 * might burn too many cycles but apart from that waking a
+	 * vcpu is not harmful.
+	 */
+	sigcpu = find_first_bit(kvm->arch.idle_mask, online_vcpus);
+	/* Well nobody's sleeping so someone will likely take the IRQ soon */
+	if (sigcpu == online_vcpus)
+		return;
+
+	do {
+		dst_vcpu = kvm_get_vcpu(kvm, sigcpu);
+		if (deliverable_irqs(dst_vcpu)) {
+			kvm->stat.inject_redist++;
+			kvm_s390_vcpu_wakeup(dst_vcpu);
+			break;
+		}
+		sigcpu = find_next_bit(kvm->arch.idle_mask, online_vcpus, ++sigcpu);
+	} while (sigcpu < online_vcpus);
+}
+
 static void __set_cpu_idle(struct kvm_vcpu *vcpu)
 {
 	kvm_s390_set_cpuflags(vcpu, CPUSTAT_WAIT);
@@ -1933,7 +1964,8 @@ static void __floating_irq_kick(struct kvm *kvm, u64 type)
 	for (sigcpu = kvm->arch.float_int.last_sleep_cpu; ; sigcpu++) {
 		sigcpu %= online_vcpus;
 		dst_vcpu = kvm_get_vcpu(kvm, sigcpu);
-		if (!is_vcpu_stopped(dst_vcpu))
+		if (!is_vcpu_stopped(dst_vcpu) &&
+		    deliverable_irqs(dst_vcpu))
 			break;
 		/* avoid endless loops if all vcpus are stopped */
 		if (nr_tries++ >= online_vcpus)
arch/s390/kvm/s390/s390.c +3 -0
index b0839e887221..2ee0ab6b926a 100644
@@ -87,6 +87,7 @@ const struct kvm_stats_desc kvm_vm_stats_desc[] = {
 	STATS_DESC_COUNTER(VM, gmap_shadow_r3_entry),
 	STATS_DESC_COUNTER(VM, gmap_shadow_sg_entry),
 	STATS_DESC_COUNTER(VM, gmap_shadow_pg_entry),
+	STATS_DESC_COUNTER(VM, inject_redist),
 };
 
 const struct kvm_stats_header kvm_vm_stats_header = {
@@ -4549,6 +4550,8 @@ static int vcpu_pre_run(struct kvm_vcpu *vcpu)
 		rc = kvm_s390_deliver_pending_interrupts(vcpu);
 		if (rc || guestdbg_exit_pending(vcpu))
 			return rc;
+
+		distribute_float_irqs(vcpu->kvm);
 	}
 
 	rc = kvm_s390_handle_requests(vcpu);
arch/s390/kvm/s390/s390.h +1 -0
index d284a263ba70..e518339fb290 100644
@@ -375,6 +375,7 @@ enum hrtimer_restart kvm_s390_idle_wakeup(struct hrtimer *timer);
 int __must_check kvm_s390_deliver_pending_interrupts(struct kvm_vcpu *vcpu);
 void kvm_s390_clear_local_irqs(struct kvm_vcpu *vcpu);
 void kvm_s390_clear_float_irqs(struct kvm *kvm);
+void distribute_float_irqs(struct kvm *kvm);
 int __must_check kvm_s390_inject_vm(struct kvm *kvm,
 				    struct kvm_s390_interrupt *s390int,
 				    struct kvm_s390_interrupt_info *inti);
Signature · 1 line
2.53.0
#1 lore raw

Am 13.08.26 um 16:56 schrieb Janosch Frank:
kvm_s390_set_cpuflags(vcpu, CPUSTAT_WAIT);

arch/s390/kvm/s390/interrupt.c static void __floating_irq_kick(struct kvm *kvm, u64 type) · line 1968

Quoted 7 lines
arch/s390/kvm/s390/interrupt.c +2 -1
@@ -1933,7 +1964,8 @@ static void __floating_irq_kick(struct kvm *kvm, u64 type)
  	for (sigcpu = kvm->arch.float_int.last_sleep_cpu; ; sigcpu++) {
  		sigcpu %= online_vcpus;
  		dst_vcpu = kvm_get_vcpu(kvm, sigcpu);
-		if (!is_vcpu_stopped(dst_vcpu))
+		if (!is_vcpu_stopped(dst_vcpu) &&
+		    deliverable_irqs(dst_vcpu))

We have the type. Wouldnt it be better to actually check if type is deliverable?

author maintainer
#2 lore raw
Quoted 4 lines · Christian Borntraeger

Am 13.08.26 um 16:56 schrieb Janosch Frank:

   	kvm_s390_set_cpuflags(vcpu, CPUSTAT_WAIT);
Quoted 10 lines

arch/s390/kvm/s390/interrupt.c static void __floating_irq_kick(struct kvm *kvm, u64 type) · line 1968

Quoted 7 lines
arch/s390/kvm/s390/interrupt.c +2 -1
@@ -1933,7 +1964,8 @@ static void __floating_irq_kick(struct kvm *kvm, u64 type)
   	for (sigcpu = kvm->arch.float_int.last_sleep_cpu; ; sigcpu++) {
   		sigcpu %= online_vcpus;
   		dst_vcpu = kvm_get_vcpu(kvm, sigcpu);
-		if (!is_vcpu_stopped(dst_vcpu))
+		if (!is_vcpu_stopped(dst_vcpu) &&
+		    deliverable_irqs(dst_vcpu))

We have the type. Wouldnt it be better to actually check if type is deliverable?

Better yes, easier no.

I need to do the translation from the IRQ types that we have in the function to the pending types that deliverable_irqs() returns. I'll send a v2 next week once I had some time to test the change.