KVM: s390: Improve floating IRQ injection behavior
Janosch Frank
maintainerPATCH v2 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.
---
v2:
- Rebase onto master
- Added IRQ type check before delivering
--- arch/s390/include/asm/kvm_host.h | 1 + arch/s390/kvm/interrupt.c | 63 ++++++++++++++++++++++++++++++-- arch/s390/kvm/kvm-s390.c | 3 ++ arch/s390/kvm/kvm-s390.h | 1 + 4 files changed, 65 insertions(+), 3 deletions(-)
arch/s390/include/asm/kvm_host.h +1 -0
@@ -467,6 +467,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/interrupt.c +60 -3
95 lines not on this page · raw message
arch/s390/kvm/kvm-s390.c +3 -0
@@ -86,6 +86,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 = {
@@ -4572,6 +4573,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/kvm-s390.h +1 -0
@@ -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
[email protected]
AI reviewThank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider: - [Critical] Use-After-Free of `inti` when injecting floating interrupts. - [High] Missed vCPU kick causes…
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [Critical] Use-After-Free of inti when injecting floating interrupts.
- [High] Missed vCPU kick causes lost or delayed floating interrupts when all vCPUs have masks closed. --
--- Patch [1]: [PATCH v2] KVM: s390: Improve floating IRQ injection behavior --- commit e7cb031c2f46e8290e932cd848faad5eec943d11
Author: Janosch Frank <[email protected]>
Subject: 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.
Quoted 4 lines
arch/s390/kvm/interrupt.c +0
-0
[ ... ]
Quoted 1 line
arch/s390/kvm/interrupt.c +0
-0@@ -1915,22 +1946,48 @@ static int __inject_io(struct kvm *kvm, struct kvm_s390_interrupt_info *inti)
[ ... ]
Quoted 16 lines
/*
* Find a destination VCPU for a floating irq and kick it. */arch/s390/kvm/interrupt.c +4
-1-static void __floating_irq_kick (struct kvm *kvm,u64 type) +static void __floating_irq_kick (struct kvm *kvm,struct kvm_s390_interrupt_info *inti) {struct kvm_vcpu *dst_vcpu;int sigcpu, online_vcpus, nr_tries =0 ; +u64 type =READ_ONCE (inti->type); +u64 irq_pend_mask; online_vcpus =atomic_read (&kvm->online_vcpus);if (!online_vcpus)return ; + irq_pend_mask =inti_to_irq_pend_mask (inti);
arch/s390/kvm/interrupt.c static int __inject_io(struct kvm *kvm, struct kvm_s390_interrupt_info *inti)
Quoted 10 lines
arch/s390/kvm/interrupt.c +2
-1for (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) & irq_pend_mask)break ;/* avoid endless loops if all vcpus are stopped */ if (nr_tries++ >= online_vcpus)return ;
[Severity: High]
Does this code drop the interrupt kick entirely if all vCPUs currently have their interrupt masks closed?
If deliverable_irqs(dst_vcpu) & irq_pend_mask evaluates to 0 for all running vCPUs, this loop will exit via the nr_tries check and return early.
Because it returns early, kvm_s390_set_cpuflags() and kvm_s390_vcpu_wakeup() are never executed, meaning no vCPU drops out of SIE and set_intercept_indicators() is not called.
If the guest later unmasks the interrupt, could it remain undelivered since the hardware intercept was never set up?
[ ... ]
arch/s390/kvm/interrupt.c static int __inject_vm(struct kvm *kvm, struct kvm_s390_interrupt_info *inti) · line 2042
Quoted 6 lines
arch/s390/kvm/interrupt.c +1
-1@@ -1982,7 +2039,7 @@ static int __inject_vm(struct kvm *kvm, struct kvm_s390_interrupt_info *inti)if (rc)return rc; -__floating_irq_kick (kvm, type); +__floating_irq_kick (kvm, inti);
[Severity: Critical]
Does this cause a use-after-free when injecting GISA I/O adapter interrupts?
When __inject_io() handles a GISA I/O interrupt, it frees the interrupt info:
arch/s390/kvm/interrupt.c:__inject_io() {
...
if (gi->origin && inti->type & KVM_S390_INT_IO_AI_MASK) {
...
kfree(inti);
return 0;
}
...
}Since it returns 0, __inject_vm() will proceed to call __floating_irq_kick() with the newly freed inti pointer, which then dereferences it when calling inti_to_irq_pend_mask(inti).
Also, for non-GISA interrupts, inti is placed on a shared list where another vCPU might deliver and free it before __floating_irq_kick() runs.
Should we avoid passing inti to the kick function?
Quoted 2 lines
arch/s390/kvm/interrupt.c +0
-0return 0 ; }
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Christian Borntraeger
maintainerAm 17.08.26 um 14:28 schrieb [email protected]:
Quoted 41 lines
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [Critical] Use-After-Free ofintiwhen injecting floating interrupts.
- [High] Missed vCPU kick causes lost or delayed floating interrupts when all vCPUs have masks closed. ----- Patch [1]: [PATCH v2] KVM: s390: Improve floating IRQ injection behavior --- commit
e7cb031c2f46e8290e932cd848faad5eec943d11
Author: Janosch Frank <[email protected]>
Subject: KVM: s390: Improve floating IRQ injection behaviorFloating 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.
Quoted 4 lines
arch/s390/kvm/interrupt.c +0
-0[ ... ]
Quoted 1 line
arch/s390/kvm/interrupt.c +0
-0@@ -1915,22 +1946,48 @@ static int __inject_io(struct kvm *kvm, struct kvm_s390_interrupt_info *inti)[ ... ]
Quoted 15 lines
/*
* Find a destination VCPU for a floating irq and kick it. */arch/s390/kvm/interrupt.c +3
-1-static void __floating_irq_kick (struct kvm *kvm,u64 type) +static void __floating_irq_kick (struct kvm *kvm,struct kvm_s390_interrupt_info *inti) {struct kvm_vcpu *dst_vcpu;int sigcpu, online_vcpus, nr_tries =0 ; +u64 type =READ_ONCE (inti->type); +u64 irq_pend_mask; online_vcpus =atomic_read (&kvm->online_vcpus);if (!online_vcpus)return ;
Quoted 15 lines
arch/s390/kvm/interrupt.c static int __inject_io(struct kvm *kvm, struct kvm_s390_interrupt_info *inti)
Quoted 11 lines
arch/s390/kvm/interrupt.c +3
-1+ irq_pend_mask =inti_to_irq_pend_mask (inti);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) & irq_pend_mask)break ;/* avoid endless loops if all vcpus are stopped */ if (nr_tries++ >= online_vcpus)return ;[Severity: High]
Does this code drop the interrupt kick entirely if all vCPUs currently have their interrupt masks closed?
I think this is a corner case but still a valid finding. We can probably consider this slowpath and wakeup/set cpuflags for ALL cpus? maybe after doing 2 rounds instead of one?
Quoted 16 lines · On Mon, 17 Aug 2026 15:22:41 +0200 Christian Borntraeger…
Quoted 7 lines
Quoted 7 lines
arch/s390/kvm/interrupt.c +3
-1+ irq_pend_mask =inti_to_irq_pend_mask (inti);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) & irq_pend_mask)Quoted 8 lines
Quoted 4 lines
break;
/* avoid endless loops if all vcpus are stopped */ if (nr_tries++ >=online_vcpus)
return;[Severity: High]
Does this code drop the interrupt kick entirely if all vCPUs currently have their interrupt masks closed?
Quoted 2 lines
I think this is a corner case but still a valid finding. We can probably consider this slowpath and wakeup/set cpuflags for ALL cpus? maybe after doing 2 rounds instead of one?
With GISA, I think the FW is supposed to deliver the floating interrupts without dropping the vCPU out of SIE. I'm not 100% sure but I think we can rely on that mechanism for the CPUs that are in SIE. Without GISA, I think, it is reasonable to assume that vCPUs don't keep running for ever. It has been a while since I have looked at this code, but I think the SIE exit path would catch this. If that is true we would not really lose initiative, but just see delayed interrupt delivery.
Sleeping vCPUs on the other hand are not of interest in this context, I think.
Opinions?
Regards,
Halil
Christian Borntraeger
maintainerAm 18.08.26 um 16:32 schrieb Halil Pasic:
Quoted 30 lines
Quoted 16 lines · On Mon, 17 Aug 2026 15:22:41 +0200 Christian Borntraeger…
Quoted 7 lines
Quoted 7 lines
arch/s390/kvm/interrupt.c +3
-1+ irq_pend_mask =inti_to_irq_pend_mask (inti);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) & irq_pend_mask)Quoted 8 lines
Quoted 4 lines
break; /* avoid endless loops if all vcpus are stopped */ if (nr_tries++ >= online_vcpus) return;[Severity: High]
Does this code drop the interrupt kick entirely if all vCPUs currently have their interrupt masks closed?Quoted 2 lines
I think this is a corner case but still a valid finding. We can probably consider this slowpath and wakeup/set cpuflags for ALL cpus? maybe after doing 2 rounds instead of one?
With GISA, I think the FW is supposed to deliver the floating interrupts without dropping the vCPU out of SIE. I'm not 100% sure but I think we can rely on that mechanism for the CPUs that are in SIE. Without GISA, I think, it is reasonable to assume that vCPUs don't keep running for ever. It has been a while since I have looked at this code, but I think the SIE exit path would catch this. If that is true we would not really lose initiative, but just see delayed interrupt delivery.
Quoted 2 lines
Sleeping vCPUs on the other hand are not of interest in this context, I think.
This is all corner case handling. Imagine one CPU running with IO disabled and all other CPUs sleeping. If now the "opportunistic" wakeup fails the GISA IO interrupt will never be delivered unless there is another wakeup. In reality this is a will not happen, but see the latest kvm unit test patch from Janosch and it might also create latencies, the "pick one CPU to deliver and wake it up if normal processing does not work" obviously has a hole in specific cases.
Quoted 33 lines · On Tue, 18 Aug 2026 17:14:52 +0200 Christian Borntraeger…
Am 18.08.26 um 16:32 schrieb Halil Pasic:
Quoted 30 lines
Quoted 16 lines · On Mon, 17 Aug 2026 15:22:41 +0200 Christian Borntraeger…
Quoted 7 lines
Quoted 7 lines
arch/s390/kvm/interrupt.c +3
-1+ irq_pend_mask =inti_to_irq_pend_mask (inti);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) & irq_pend_mask)Quoted 8 lines
Quoted 4 lines
break; /* avoid endless loops if all vcpus are stopped */ if (nr_tries++ >= online_vcpus) return;[Severity: High]
Does this code drop the interrupt kick entirely if all vCPUs currently have their interrupt masks closed?Quoted 2 lines
I think this is a corner case but still a valid finding. We can probably consider this slowpath and wakeup/set cpuflags for ALL cpus? maybe after doing 2 rounds instead of one?
With GISA, I think the FW is supposed to deliver the floating interrupts without dropping the vCPU out of SIE. I'm not 100% sure but I think we can rely on that mechanism for the CPUs that are in SIE. Without GISA, I think, it is reasonable to assume that vCPUs don't keep running for ever. It has been a while since I have looked at this code, but I think the SIE exit path would catch this. If that is true we would not really lose initiative, but just see delayed interrupt delivery.
Quoted 2 lines
Sleeping vCPUs on the other hand are not of interest in this context, I think.
Quoted 7 lines
This is all corner case handling. Imagine one CPU running with IO disabled and all other CPUs sleeping. If now the "opportunistic" wakeup fails the GISA IO interrupt will never be delivered unless there is another wakeup. In reality this is a will not happen, but see the latest kvm unit test patch from Janosch and it might also create latencies, the "pick one CPU to deliver and wake it up if normal processing does not work" obviously has a hole in specific cases.
Right, but that is not the "if all vCPUs currently have their interrupt masks closed" case that Sashiko is talking about. Or did I misunderstand that?
Yes, I agree there are holes, and I was hoping to contribute to a better understanding on where the holes actually are and what are the implications of those holes.
Regards,
Halil
Janosch Frank
maintainerQuoted 44 lines · Halil Pasic
Quoted 33 lines · On Tue, 18 Aug 2026 17:14:52 +0200 Christian Borntraeger…
Am 18.08.26 um 16:32 schrieb Halil Pasic:
Quoted 30 lines
Quoted 16 lines · On Mon, 17 Aug 2026 15:22:41 +0200 Christian Borntraeger…
Quoted 7 lines
Quoted 7 lines
arch/s390/kvm/interrupt.c +3
-1+ irq_pend_mask =inti_to_irq_pend_mask (inti);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) & irq_pend_mask)Quoted 8 lines
Quoted 4 lines
break; /* avoid endless loops if all vcpus are stopped */ if (nr_tries++ >= online_vcpus) return;[Severity: High]
Does this code drop the interrupt kick entirely if all vCPUs currently have their interrupt masks closed?Quoted 2 lines
I think this is a corner case but still a valid finding. We can probably consider this slowpath and wakeup/set cpuflags for ALL cpus? maybe after doing 2 rounds instead of one?
With GISA, I think the FW is supposed to deliver the floating interrupts without dropping the vCPU out of SIE. I'm not 100% sure but I think we can rely on that mechanism for the CPUs that are in SIE. Without GISA, I think, it is reasonable to assume that vCPUs don't keep running for ever. It has been a while since I have looked at this code, but I think the SIE exit path would catch this. If that is true we would not really lose initiative, but just see delayed interrupt delivery.
Quoted 2 lines
Sleeping vCPUs on the other hand are not of interest in this context, I think.
Quoted 7 lines
This is all corner case handling. Imagine one CPU running with IO disabled and all other CPUs sleeping. If now the "opportunistic" wakeup fails the GISA IO interrupt will never be delivered unless there is another wakeup. In reality this is a will not happen, but see the latest kvm unit test patch from Janosch and it might also create latencies, the "pick one CPU to deliver and wake it up if normal processing does not work" obviously has a hole in specific cases.
Quoted 8 lines
Right, but that is not the "if all vCPUs currently have their interrupt masks closed" case that Sashiko is talking about. Or did I misunderstand that?
Yes, I agree there are holes, and I was hoping to contribute to a better understanding on where the holes actually are and what are the implications of those holes.
Yesterday I managed to find the actual problem behind this code for PV guests:
Non-ev Service IRQs are not allowed to be injected on re-entry when SCLP emulation has finished. They can only be injected when we receive the instruction notification for SCLP.
So having wakeups for service IRQs in the flic is useless. At the time they are injected (after SCLP processing and before SIE re-entry) there's no way to make one pending for a PV cpu.
I've since created a patch to kick cpus in the handling of the sclp instruction notification and that "fixes" the problems with the firq test.
For Linux we'll see delayed delivery at most since the masks are open most of the time. My guess is that the IRQ injection actually happens pretty fast. There are other OSs which might benefit though and we should generally ask ourselves if good enough for Linux is the right answer.
The stat counter did actually trigger a couple hundred times on an IPL so we're hitting the redistribution code path. Let's see what insights we can get from the performance measurements.
Christian Borntraeger
maintainerAm 19.08.26 um 14:54 schrieb Janosch Frank:
Quoted 48 lines · Halil Pasic
Quoted 47 lines
Quoted 32 lines · On Tue, 18 Aug 2026 17:14:52 +0200 Christian Borntraeger…
Am 18.08.26 um 16:32 schrieb Halil Pasic:
Quoted 29 lines
Quoted 12 lines · On Mon, 17 Aug 2026 15:22:41 +0200 Christian Borntraeger…
Quoted 12 lines
Quoted 11 lines
+
irq_pend_mask=inti_to_irq_pend_mask(inti);
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)&irq_pend_mask) break;
/* avoid endless loops if all vcpus are stopped */ if (nr_tries++ >=online_vcpus)
return;Quoted 6 lines
Quoted 3 lines
[Severity: High]
Does this code drop the interrupt kick entirely if all vCPUs currently have their interrupt masks closed?I think this is a corner case but still a valid finding. We can probably consider this slowpath and wakeup/set cpuflags for ALL cpus? maybe after doing 2 rounds instead of one?
With GISA, I think the FW is supposed to deliver the floating interrupts without dropping the vCPU out of SIE. I'm not 100% sure but I think we can rely on that mechanism for the CPUs that are in SIE. Without GISA, I think, it is reasonable to assume that vCPUs don't keep running for ever. It has been a while since I have looked at this code, but I think the SIE exit path would catch this. If that is true we would not really lose initiative, but just see delayed interrupt delivery.
Quoted 2 lines
Sleeping vCPUs on the other hand are not of interest in this context, I think.
Quoted 7 lines
This is all corner case handling. Imagine one CPU running with IO disabled and all other CPUs sleeping. If now the "opportunistic" wakeup fails the GISA IO interrupt will never be delivered unless there is another wakeup. In reality this is a will not happen, but see the latest kvm unit test patch from Janosch and it might also create latencies, the "pick one CPU to deliver and wake it up if normal processing does not work" obviously has a hole in specific cases.
Right, but that is not the "if all vCPUs currently have their interrupt masks closed" case that Sashiko is talking about. Or did I misunderstand that?
Quoted 5 lines
Quoted 4 lines
Yes, I agree there are holes, and I was hoping to contribute to a better understanding on where the holes actually are and what are the implications of those holes.
Yesterday I managed to find the actual problem behind this code for PV guests:
Thanks for digging through that.>
Quoted 11 lines
Non-ev Service IRQs are not allowed to be injected on re-entry when SCLP emulation has finished. They can only be injected when we receive the instruction notification for SCLP.
So having wakeups for service IRQs in the flic is useless. At the time they are injected (after SCLP processing and before SIE re-entry) there's no way to make one pending for a PV cpu.
I've since created a patch to kick cpus in the handling of the sclp instruction notification and that "fixes" the problems with the firq test.
For Linux we'll see delayed delivery at most since the masks are open most of the time. My guess is that the IRQ injection actually happens pretty fast. There are other OSs which might benefit though and we should generally ask ourselves if good enough for Linux is the right answer.
The stat counter did actually trigger a couple hundred times on an IPL so we're hitting the redistribution code path. Let's see what insights we can get from the performance measurements.
there are also ideas about disabling IO interrupts on some CPUs (e.g. vertical low) and other OSes like zOS have the concept of CPUs excplictely disabled for IO interrupts. So we better have a least a KVM implementation that works in those situations.