x86/pass-through: avoid double IRQ unbind during domain cleanup
PATCH 1/1 x86/pass-through: avoid double IRQ unbind during domain cleanup
XEN_DOMCTL_destroydomain creates a continuation if domain_kill -ERESTARTs. In that scenario, it is possible to receive multiple _pirq_guest_unbind calls for the same pirq from domain_kill, if the pirq has not yet been removed from the domain's pirq_tree, as:
domain_kill()
-> domain_relinquish_resources()
-> pci_release_devices()
-> pci_clean_dpci_irq()
-> pirq_guest_unbind()
-> __pirq_guest_unbind()Avoid recurring invocations of pirq_guest_unbind() by removing the pIRQ from the tree being iterated after the first call there. In case such a removed entry still has a softirq outstanding, record it and re-check upon re-invocation.
xen/arch/x86/irq.c +1 -1
@@ -1323,7 +1323,7 @@ void (pirq_cleanup_check)(struct pirq *p }if (radix_tree_delete (&d->pirq_tree, pirq->pirq) != pirq ) -BUG (); +BUG_ON (!d->is_dying); }/* Flush all ready EOIs from the top of this CPU's pending-EOI stack. */
xen/drivers/passthrough/pci.c +19 -2
@@ -873,7 +873,14 @@ static int pci_clean_dpci_irq(struct domxfree (digl); } -return pt_pirq_softirq_active (pirq_dpci) ? -ERESTART :0 ; +radix_tree_delete (&d->pirq_tree,dpci_pirq (pirq_dpci)->pirq); + +if ( !pt_pirq_softirq_active (pirq_dpci) ) +return 0 ; + +domain_get_irq_dpci (d)->pending_pirq_dpci = pirq_dpci; + +return -ERESTART ; }static int pci_clean_dpci_irqs (struct domain *d)
@@ -890,8 +897,18 @@ static int pci_clean_dpci_irqs(struct do hvm_irq_dpci =domain_get_irq_dpci (d);if ( hvm_irq_dpci !=NULL ) { -int ret =pt_pirq_iterate (d, pci_clean_dpci_irq,NULL ); +int ret =0 ; + +if ( hvm_irq_dpci->pending_pirq_dpci ) + { +if (pt_pirq_softirq_active (hvm_irq_dpci->pending_pirq_dpci) ) + ret = -ERESTART ; +else + hvm_irq_dpci->pending_pirq_dpci =NULL ; + } +if ( !ret ) + ret =pt_pirq_iterate (d, pci_clean_dpci_irq,NULL );if ( ret ) {spin_unlock (&d->event_lock);
xen/include/asm-x86/hvm/irq.h +2 -0
@@ -158,6 +158,8 @@ struct hvm_irq_dpci {DECLARE_BITMAP (isairq_map,NR_ISAIRQS );/* Record of mapped Links */ uint8_t link_cnt[NR_LINK ]; +/* Clean up: Entry with a softirq invocation pending / in progress. */ +struct hvm_pirq_dpci *pending_pirq_dpci; };/* Machine IRQ to guest device/intx mapping. */
Quoted 19 lines
-----Original Message-----
From: Jan Beulich <[email protected]>
Sent: 28 April 2020 13:22
To: [email protected]
Cc: Paul Durrant <[email protected]>; Varad Gautam <[email protected]>; Andrew Cooper <[email protected]>; Roger Pau Monné <[email protected]>; Wei Liu <[email protected]>
Subject: [PATCH] x86/pass-through: avoid double IRQ unbind during domain cleanup
XEN_DOMCTL_destroydomaincreates a continuation ifdomain_kill-ERESTARTs. In that scenario, it is possible to receive multiple_pirq_guest_unbindcalls for the same pirq fromdomain_kill, if the pirq has not yet been removed from the domain'spirq_tree, as:
domain_kill()-> domain_relinquish_resources() -> pci_release_devices() -> pci_clean_dpci_irq() -> pirq_guest_unbind() -> __pirq_guest_unbind()
Quoted 8 lines
Avoid recurring invocations of
pirq_guest_unbind()by removing the pIRQ from the tree being iterated after the first call there. In case such a removed entry still has a softirq outstanding, record it and re-check upon re-invocation.Reported-by Varad Gautam <[email protected]>Signed-off-by Jan Beulich <[email protected]>Tested-by Varad Gautam <[email protected]>
Quoted 23 lines · Jan Beulich
XEN_DOMCTL_destroydomaincreates a continuation ifdomain_kill-ERESTARTs. In that scenario, it is possible to receive multiple_pirq_guest_unbindcalls for the same pirq fromdomain_kill, if the pirq has not yet been removed from the domain'spirq_tree, as:
domain_kill()-> domain_relinquish_resources() -> pci_release_devices() -> pci_clean_dpci_irq() -> pirq_guest_unbind() -> __pirq_guest_unbind()Avoid recurring invocations of
pirq_guest_unbind()by removing the pIRQ from the tree being iterated after the first call there. In case such a removed entry still has a softirq outstanding, record it and re-check upon re-invocation.Reported-by Varad Gautam <[email protected]>Signed-off-by Jan Beulich <[email protected]>Tested-by Varad Gautam <[email protected]>
xen/arch/x86/irq.c void (pirq_cleanup_check)(struct pirq *p · line 1326
Quoted 5 lines
xen/arch/x86/irq.c +1
-1@@ -1323,7 +1323,7 @@ void (pirq_cleanup_check)(struct pirq *p }if (radix_tree_delete (&d->pirq_tree, pirq->pirq) != pirq ) -BUG (); +BUG_ON (!d->is_dying);
I think to keep the previous behavior this should be:
BUG_ON(!is_hvm_domain(d) || !d->is_dying);
Since the pirqs will only be removed elsewhere if the domain is HVM?
Thanks, Roger.
Quoted 32 lines · Roger Pau Monné
Quoted 23 lines · Jan Beulich
XEN_DOMCTL_destroydomaincreates a continuation ifdomain_kill-ERESTARTs. In that scenario, it is possible to receive multiple_pirq_guest_unbindcalls for the same pirq fromdomain_kill, if the pirq has not yet been removed from the domain'spirq_tree, as:
domain_kill()-> domain_relinquish_resources() -> pci_release_devices() -> pci_clean_dpci_irq() -> pirq_guest_unbind() -> __pirq_guest_unbind()Avoid recurring invocations of
pirq_guest_unbind()by removing the pIRQ from the tree being iterated after the first call there. In case such a removed entry still has a softirq outstanding, record it and re-check upon re-invocation.Reported-by Varad Gautam <[email protected]>Signed-off-by Jan Beulich <[email protected]>Tested-by Varad Gautam <[email protected]>xen/arch/x86/irq.c void (pirq_cleanup_check)(struct pirq *p · line 1326
Quoted 5 lines
xen/arch/x86/irq.c +1
-1@@ -1323,7 +1323,7 @@ void (pirq_cleanup_check)(struct pirq *p }if (radix_tree_delete (&d->pirq_tree, pirq->pirq) != pirq ) -BUG (); +BUG_ON (!d->is_dying);I think to keep the previous behavior this should be:
Quoted 3 lines
BUG_ON(!is_hvm_domain(d) || !d->is_dying);Since the pirqs will only be removed elsewhere if the domain is HVM?
pirq_cleanup_check() is a generic hook, and hence I consider it more correct to not have it behave differently in this regard for different types of guests. IOW while it _may_ (didn't check) not be the case today that this can be called multiple times even for PV guests, I'd view this as legitimate behavior.
Jan
Quoted 35 lines · Jan Beulich
Quoted 34 lines · Roger Pau Monné
Quoted 23 lines · Jan Beulich
XEN_DOMCTL_destroydomaincreates a continuation ifdomain_kill-ERESTARTs. In that scenario, it is possible to receive multiple_pirq_guest_unbindcalls for the same pirq fromdomain_kill, if the pirq has not yet been removed from the domain'spirq_tree, as:
domain_kill()-> domain_relinquish_resources() -> pci_release_devices() -> pci_clean_dpci_irq() -> pirq_guest_unbind() -> __pirq_guest_unbind()Avoid recurring invocations of
pirq_guest_unbind()by removing the pIRQ from the tree being iterated after the first call there. In case such a removed entry still has a softirq outstanding, record it and re-check upon re-invocation.Reported-by Varad Gautam <[email protected]>Signed-off-by Jan Beulich <[email protected]>Tested-by Varad Gautam <[email protected]>xen/arch/x86/irq.c void (pirq_cleanup_check)(struct pirq *p · line 1326
Quoted 5 lines
xen/arch/x86/irq.c +1
-1@@ -1323,7 +1323,7 @@ void (pirq_cleanup_check)(struct pirq *p }if (radix_tree_delete (&d->pirq_tree, pirq->pirq) != pirq ) -BUG (); +BUG_ON (!d->is_dying);I think to keep the previous behavior this should be:
BUG_ON(!is_hvm_domain(d) || !d->is_dying);
Quoted 7 lines
Quoted 1 line
Since the pirqs will only be removed elsewhere if the domain is HVM?
pirq_cleanup_check()is a generic hook, and hence I consider it more correct to not have it behave differently in this regard for different types of guests. IOW while it _may_ (didn't check) not be the case today that this can be called multiple times even for PV guests, I'd view this as legitimate behavior.
Previous to this patch pirq_cleanup_check couldn't be called multiple times, as it would result in the BUG triggering, that was true for both PV and HVM. Now that the removal of PIRQs from the tree is done elsewhere for HVM when the domain is dying the check needs to be relaxed for HVM at least. I would prefer if it was kept as-is for PV (since there's been no change in behavior for PV that could allow for multiple calls to pirq_cleanup_check), or else a small comment in the commit message would help clarify this is done on purpose.
Thanks, Roger.
Quoted 44 lines · Roger Pau Monné
Quoted 35 lines · Jan Beulich
Quoted 34 lines · Roger Pau Monné
Quoted 23 lines · Jan Beulich
XEN_DOMCTL_destroydomaincreates a continuation ifdomain_kill-ERESTARTs. In that scenario, it is possible to receive multiple_pirq_guest_unbindcalls for the same pirq fromdomain_kill, if the pirq has not yet been removed from the domain'spirq_tree, as:
domain_kill()-> domain_relinquish_resources() -> pci_release_devices() -> pci_clean_dpci_irq() -> pirq_guest_unbind() -> __pirq_guest_unbind()Avoid recurring invocations of
pirq_guest_unbind()by removing the pIRQ from the tree being iterated after the first call there. In case such a removed entry still has a softirq outstanding, record it and re-check upon re-invocation.Reported-by Varad Gautam <[email protected]>Signed-off-by Jan Beulich <[email protected]>Tested-by Varad Gautam <[email protected]>xen/arch/x86/irq.c void (pirq_cleanup_check)(struct pirq *p · line 1326
Quoted 5 lines
xen/arch/x86/irq.c +1
-1@@ -1323,7 +1323,7 @@ void (pirq_cleanup_check)(struct pirq *p }if (radix_tree_delete (&d->pirq_tree, pirq->pirq) != pirq ) -BUG (); +BUG_ON (!d->is_dying);I think to keep the previous behavior this should be:
BUG_ON(!is_hvm_domain(d) || !d->is_dying);Quoted 7 lines
Quoted 1 line
Since the pirqs will only be removed elsewhere if the domain is HVM?
pirq_cleanup_check()is a generic hook, and hence I consider it more correct to not have it behave differently in this regard for different types of guests. IOW while it _may_ (didn't check) not be the case today that this can be called multiple times even for PV guests, I'd view this as legitimate behavior.
Quoted 8 lines
Previous to this patch
pirq_cleanup_checkcouldn't be called multiple times, as it would result in the BUG triggering, that was true for both PV and HVM. Now that the removal of PIRQs from the tree is done elsewhere for HVM when the domain is dying the check needs to be relaxed for HVM at least. I would prefer if it was kept as-is for PV (since there's been no change in behavior for PV that could allow for multiple calls topirq_cleanup_check), or else a small comment in the commit message would help clarify this is done on purpose.
I've added
"Note that pirq_cleanup_check() gets relaxed beyond what's strictly
needed here, to avoid introducing an asymmetry there between HVM and PV guests."
Does this sound suitable to you?
Jan
Roger Pau Monné
Reviewed-byQuoted 54 lines · Jan Beulich
Quoted 44 lines · Roger Pau Monné
Quoted 35 lines · Jan Beulich
Quoted 34 lines · Roger Pau Monné
Quoted 23 lines · Jan Beulich
XEN_DOMCTL_destroydomaincreates a continuation ifdomain_kill-ERESTARTs. In that scenario, it is possible to receive multiple_pirq_guest_unbindcalls for the same pirq fromdomain_kill, if the pirq has not yet been removed from the domain'spirq_tree, as:
domain_kill()-> domain_relinquish_resources() -> pci_release_devices() -> pci_clean_dpci_irq() -> pirq_guest_unbind() -> __pirq_guest_unbind()Avoid recurring invocations of
pirq_guest_unbind()by removing the pIRQ from the tree being iterated after the first call there. In case such a removed entry still has a softirq outstanding, record it and re-check upon re-invocation.Reported-by Varad Gautam <[email protected]>Signed-off-by Jan Beulich <[email protected]>Tested-by Varad Gautam <[email protected]>xen/arch/x86/irq.c void (pirq_cleanup_check)(struct pirq *p · line 1326
Quoted 5 lines
xen/arch/x86/irq.c +1
-1@@ -1323,7 +1323,7 @@ void (pirq_cleanup_check)(struct pirq *p }if (radix_tree_delete (&d->pirq_tree, pirq->pirq) != pirq ) -BUG (); +BUG_ON (!d->is_dying);I think to keep the previous behavior this should be:
BUG_ON(!is_hvm_domain(d) || !d->is_dying);Quoted 7 lines
Quoted 1 line
Since the pirqs will only be removed elsewhere if the domain is HVM?
pirq_cleanup_check()is a generic hook, and hence I consider it more correct to not have it behave differently in this regard for different types of guests. IOW while it _may_ (didn't check) not be the case today that this can be called multiple times even for PV guests, I'd view this as legitimate behavior.Quoted 8 lines
Previous to this patch
pirq_cleanup_checkcouldn't be called multiple times, as it would result in the BUG triggering, that was true for both PV and HVM. Now that the removal of PIRQs from the tree is done elsewhere for HVM when the domain is dying the check needs to be relaxed for HVM at least. I would prefer if it was kept as-is for PV (since there's been no change in behavior for PV that could allow for multiple calls topirq_cleanup_check), or else a small comment in the commit message would help clarify this is done on purpose.
Quoted 7 lines
I've added
"Note that
pirq_cleanup_check()gets relaxed beyond what's strictly
needed here, to avoid introducing an asymmetry there between HVM and PV guests."Does this sound suitable to you?
Yes, thanks. With that:
Roger.