Re: [PATCH v2] KVM: s390: fix gisa destroy operation might lead to cpu stalls

Matthew Rosato
2023-08-25T02:57:17Z

On 8/24/23 4:36 PM, Michael Mueller wrote:
>
>
> On 24.08.23 21:17, Matthew Rosato wrote:
>> On 8/24/23 9:09 AM, Michael Mueller wrote:
>>> A GISA cannot be destroyed as long it is linked in the GIB alert list
>>> as this would breake the alert list. Just waiting for its removal from
>>
>> Hi Michael,
>>
>> Nit: s/breake/break/
>>
>>> the list triggered by another vm is not sufficient as it might be the
>>> only vm. The below shown cpu stall situation might occur when GIB alerts
>>> are delayed and is fixed by calling process_gib_alert_list() instead of
>>> waiting.
>>>
>>> At this time the vcpus of the vm are already destroyed and thus
>>> no vcpu can be kicked to enter the SIE again if for some reason an
>>> interrupt is pending for that vm.
>>>
>>> Additianally the IAM restore value ist set to 0x00 if that was not the
>>
>> Nits: s/Additianally/Additionally/  as well as s/ist/is/
>>
>
> Thanks a lot, Matt. I will address of course all these typos ;)
>
>>> case. That would be a bug introduced by incomplete device de-registration,
>>> i.e. missing kvm_s390_gisc_unregister() call.
>> If this implies a bug, maybe it should be a WARN_ON instead of a KVM_EVENT?  Because if we missed a call to kvm_s390_gisc_unregister() then we're also leaking refcounts (one for each gisc that we didn't unregister).
>
> I was thinking of a WARN_ON() as well and will most probaly add it because it is much better visible.
>
>>
>>>
>>> Setting this value guarantees that late interrupts don't bring the GISA
>>> back into the alert list.
>>
>> Just to make sure I understand -- The idea is that once you set the alert mask to 0x00 then it should be impossible for millicode to deliver further alerts associated with this gisa right?  Thus making it OK to do one last process_gib_alert_list() after that point in time.
>>
>> But I guess my question is: will millicode actually see this gi->alert.mask change soon enough to prevent further alerts?  Don't you need to also cmpxchg the mask update into the contents of kvm_s390_gisa (via gisa_set_iam?)
>
> It is not the IAM directly that I set to 0x00 but gi->alert.mask. It is used the restore the IAM in the gisa by means of gisa_get_ipm_or_restore_iam() under cmpxchg() conditions which is called by process_gib_alert_list() and the hr_timer function gisa_vcpu_kicker() that it triggers. When the gisa is in the alert list, the IAM is always 0x00. It's set by millicode. I just need to ensure that it is not changed to anything else.

Besides zeroing it while on the alert list and restoring the IAM to re-enable millicode alerts, we also change the IAM to enable a gisc (kvm_s390_gisc_register) and disable a gisc (kvm_s390_gisc_register) for alerts via a call to gisa_set_iam(). AFAIU the IAM is telling millicode what giscs host alerts should happen for, and the point of the gisa_set_iam() call during gisc_unregister is to tell millicode to stop alerts from being delivered for that gisc at that point.

Now for this patch, my understanding is that you are basically cleaning up after a driver that did not handle their gisc refcounts properly, right? Otherwise by the time you reach gisa_destroy the alert.mask would already be 0x00. Then in that case, wouldn't you want to force the unregistration of any gisc still in the IAM at gisa_destroy time -- meaning shouldn't we do the equivalent gisa_set_iam that would have previously been done during gisc_unregister, had it been called properly?

For example, rather than just setting gi->alert.mask = 0x00 in kvm_s390_gisa_destroy(), what if you instead did:
1) issue the warning that gi->alert.mask was nonzero upon entry to gisa_destroy
2) perform the equivalent of calling kvm_s390_gisc_unregister() for every bit that is still on in the gi->alert.mask, performing the same actions as though the refcount were reaching 0 for each gisc (remove the bit from the alert mask, call gisa_set_iam).
3) Finally, process the alert list one more time if gisa_in_alert_list(gi->origin). At this point, since we already set IAM to 0x00, millicode would have no further reason to deliver more alerts, so doing one last check should be safe.

That would be the same chain of events (minus the warning) that would occur if a driver actually called kvm_s390_gisc_unregister() the correct number of times. Of course you could also just collapse step #2 -- doing that gets us _very_ close to this patch; you could just set gi->alert.mask directly to 0x00 like you do here but then you would also need a gisa_set_iam call to tell millicode to stop sending alerts for all of the giscs you just removed from the alert.mask. In either approach, the -EBUSY return from gisa_set_iam would be an issue that needs to be handled.

Overall I guess until the IAM visible to millicode is set to 0x00 I'm not sure I understand what would prevent millicode from delivering another alert to any gisc still enabled in the IAM. You say above it will be cmpxchg()'d during process_gib_alert_list() via gisa_get_ipm_or_restore_iam() but if we first check gisa_in_alert_list(gi->origin) with this new patch and the gisa is not yet in the alert list then we would skip the call to process_gib_alert_list() and instead just cancel the timer -- I could very well be misunderstanding something, but my concern is that you are shrinking but not eliminating the window here. Let me try an example -- With this patch, isn't the following chain of events still possible:

1) enter kvm_s390_gisa_destroy. Let's say gi->alert.mask = 0x80.
2) set gi->alert.mask = 0x00
3) check if(gisa_in_alert_list(gi->origin)) -- it returns false
4) Since the IAM still had a bit on at this point, millicode now delivers an alert for the gisc associated with bit 0x80 and sets IAM to 0x00 to indicate the gisa in the alert list
5) call hrtimer_cancel (since we already checked gisa_in_alert_list, we don't notice that last alert delivered)
6) set gi->origin = NULL, return from kvm_s390_gisa_destroy

Assuming that series of events is possible, wouldn't a solution be to replace step #3 above with something along the lines of this (untested diff on top of this patch):

diff --git a/arch/s390/kvm/interrupt.c b/arch/s390/kvm/interrupt.c
index 06890a58d001..ab99c9ec1282 100644
--- a/arch/s390/kvm/interrupt.c
+++ b/arch/s390/kvm/interrupt.c
@@ -3220,6 +3220,10 @@ void kvm_s390_gisa_destroy(struct kvm *kvm)
KVM_EVENT(3, "vm 0x%pK has unexpected restore iam 0x%02x",
kvm, gi->alert.mask);
gi->alert.mask = 0x00;
+ while (gisa_set_iam(gi->origin, gi->alert.mask)) {
+ KVM_EVENT(3, "vm 0x%pK alert while clearing iam", kvm);
+ process_gib_alert_list();
+ }
}
if (gisa_in_alert_list(gi->origin)) {
KVM_EVENT(3, "vm 0x%pK gisa in alert list during destroy", kvm);

>
> in order to ensure an alert can't still be delivered some time after you check gisa_in_alert_list(gi->origin)?  That matches up with what is done per-gisc in kvm_s390_gisc_unregister() today.
>
> right
>
>>
>> ...  That said, now that I'm looking closer at kvm_s390_gisc_unregister() and gisa_set_iam():  it seems strange that nobody checks the return code from gisa_set_iam today.  AFAICT, even if the device driver(s) call kvm_s390_gisc_unregister correctly for all associated gisc, if gisa_set_iam manages to return -EBUSY because the gisa is already in the alert list then wouldn't the gisc refcounts be decremented but the relevant alert bit left enabled for that gisc until the next time we call gisa_set_iam or gisa_get_ipm_or_restore_iam?
>
> you are right, that should retried in kvm_s390_gisc_register() and kvm_s390_gisc_unregister() until the rc is 0 but that would lead to a CPU stall as well under the condition where GAL interrupts are not delivered in the host.
>
>>
>> Similar strangeness for kvm_s390_gisc_register() - AFAICT if gisa_set_iam returns -EBUSY then we would increment the gisc refcounts but never actually enable the alert bit for that gisc until the next time we call gisa_set_iam or gisa_get_ipm_or_restore_iam.
>
> I have to think and play around with process_gib_alert_list() being called as well in these situations.
>
> BTW the pci and the vfip_ap device drivers currently also ignore the return codes of kvm_s390_gisc_unregister().
>

Hmm, good point. You're right, we should probably do something there. I think the 3 reasons kvm_s390_gisc_unregister() could give a nonzero RC today would all be strange, likely implementation bugs...

-ENODEV we also would have never been able to register, or something odd happened to gisa after registration
-ERANGE we also would have never been able to register, or the gisc got clobbered sometime after registration
-EINVAL either we never registered, unregistered too many times or gisa was destroyed on us somehow

I think for these cases the best pci/ap can do would be to WARN_ON(_ONCE) and then proceed just assuming that the gisc was unregistered or never properly registered.

Thanks,
Matt