xen/events: Fix Global and Domain VIRQ tracking
Subsystems (2)
v3 cover xen/events: Fix Global and Domain VIRQ tracking
v2 and v3 are now series first changing find_virq() to return -EEXIST in some cases, and then tracking cpu movement of VIRQs. This keeps ensures the cpu is correct when calling unbind_from_irq().
I'm adding Cc stable to all patches. Patch 2 doesn't have a Fixes, but it goes together with patch 3.
Jason Andryuk (3):
xen/events: Cleanup find_virq() return codes
xen/events: Return -EEXIST for bound VIRQs
xen/events: Update virq_to_irq on migration
drivers/xen/events/events_base.c | 37 +++++++++++++++++++++++++------- 1 file changed, 29 insertions(+), 8 deletions(-)
Signature · 1 line
2.34.1
PATCH v3 1/3 xen/events: Cleanup find_virq() return codes
rc is overwritten by the evtchn_status hypercall in each iteration, so the return value will be whatever the last iteration is. This could incorrectly return success even if the event channel was not found. Change to an explicit -ENOENT for an un-found virq and return 0 on a successful match.
---
v3:
Reduce rc's scope
Add R-b: Jan
Mention false success in commit message
Add Fixes
Cc: stable
Add R-b: Juergen
v2:
New
--- drivers/xen/events/events_base.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-)
drivers/xen/events/events_base.c +4 -3
@@ -1318,10 +1318,11 @@ static int find_virq(unsigned int virq, unsigned int cpu, evtchn_port_t *evtchn) {struct evtchn_status status;evtchn_port_t port; -int rc = -ENOENT ;memset (&status,0 ,sizeof (status));for (port =0 ; port <xen_evtchn_max_channels (); port++) { +int rc; + status.dom =DOMID_SELF ; status.port = port; rc =HYPERVISOR_event_channel_op (EVTCHNOP_status, &status);
@@ -1331,10 +1332,10 @@ static int find_virq(unsigned int virq, unsigned int cpu, evtchn_port_t *evtchn)continue ;if (status.u.virq == virq && status.vcpu ==xen_vcpu_nr (cpu)) { *evtchn = port; -break ; +return 0 ; } } -return rc; +return -ENOENT ; }/**
Signature · 1 line
2.34.1
PATCH v3 2/3 xen/events: Return -EEXIST for bound VIRQs
In reply to Jason AndryukChange find_virq() to return -EEXIST when a VIRQ is bound to a different CPU than the one passed in. With that, remove the BUG_ON() from bind_virq_to_irq() to propogate the error upwards.
Some VIRQs are per-cpu, but others are per-domain or global. Those must be bound to CPU0 and can then migrate elsewhere. The lookup for per-domain and global will probably fail when migrated off CPU 0, especially when the current CPU is tracked. This now returns -EEXIST instead of BUG_ON().
A second call to bind a per-domain or global VIRQ is not expected, but make it non-fatal to avoid trying to look up the irq, since we don't know which per_cpu(virq_to_irq) it will be in.
---
v3:
Cc: stable as a pre-req for the subsequent virg tracking change Call __unbind_from_irq() on error ro avoid leaking info
v2:
New
--- drivers/xen/events/events_base.c | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-)
drivers/xen/events/events_base.c +14 -5
@@ -1314,10 +1314,12 @@ int bind_interdomain_evtchn_to_irq_lateeoi(struct xenbus_device *dev, }EXPORT_SYMBOL_GPL (bind_interdomain_evtchn_to_irq_lateeoi); -static int find_virq (unsigned int virq,unsigned int cpu,evtchn_port_t *evtchn) +static int find_virq (unsigned int virq,unsigned int cpu,evtchn_port_t *evtchn, +bool percpu) {struct evtchn_status status;evtchn_port_t port; +bool exists = false;memset (&status,0 ,sizeof (status));for (port =0 ; port <xen_evtchn_max_channels (); port++) {
@@ -1330,12 +1332,16 @@ static int find_virq(unsigned int virq, unsigned int cpu, evtchn_port_t *evtchn)continue ;if (status.status != EVTCHNSTAT_virq)continue ; -if (status.u.virq == virq && status.vcpu ==xen_vcpu_nr (cpu)) { +if (status.u.virq != virq) +continue ; +if (status.vcpu ==xen_vcpu_nr (cpu)) { *evtchn = port;return 0 ; + }else if (!percpu) { + exists = true; } } -return -ENOENT ; +return exists ? -EEXIST : -ENOENT ; }/**
@@ -1382,8 +1388,11 @@ int bind_virq_to_irq(unsigned int virq, unsigned int cpu, bool percpu) evtchn = bind_virq.port; else {if (ret == -EEXIST ) - ret =find_virq (virq, cpu, &evtchn); -BUG_ON (ret <0 ); + ret =find_virq (virq, cpu, &evtchn, percpu); +if (ret) { +__unbind_from_irq (info, info->irq); + goto out; + } } ret =xen_irq_info_virq_setup (info, cpu, evtchn, virq);
Signature · 1 line
2.34.1
Re: [PATCH v3 2/3] xen/events: Return -EEXIST for bound VIRQs
Quoted 10 lines · Jason Andryuk
Change
find_virq()to return-EEXISTwhen a VIRQ is bound to a different CPU than the one passed in. With that, remove theBUG_ON()frombind_virq_to_irq()to propogate the error upwards.Some VIRQs are per-cpu, but others are per-domain or global. Those must be bound to CPU0 and can then migrate elsewhere. The lookup for per-domain and global will probably fail when migrated off CPU 0, especially when the current CPU is tracked. This now returns
-EEXISTinstead ofBUG_ON().
Quoted 6 lines
A second call to bind a per-domain or global VIRQ is not expected, but make it non-fatal to avoid trying to look up the irq, since we don't know which
per_cpu(virq_to_irq)it will be in.Signed-off-by Jason Andryuk <[email protected]>
Juergen
PATCH v3 3/3 xen/events: Update virq_to_irq on migration
In reply to Jason AndryukVIRQs come in 3 flavors, per-VPU, per-domain, and global, and the VIRQs are tracked in per-cpu virq_to_irq arrays.
Per-domain and global VIRQs must be bound on CPU 0, and bind_virq_to_irq() sets the per_cpu virq_to_irq at registration time Later, the interrupt can migrate, and info->cpu is updated. When calling __unbind_from_irq(), the per-cpu virq_to_irq is cleared for a different cpu. If bind_virq_to_irq() is called again with CPU 0, the stale irq is returned. There won't be any irq_info for the irq, so things break.
Make xen_rebind_evtchn_to_cpu() update the per_cpu virq_to_irq mappings to keep them update to date with the current cpu. This ensures the correct virq_to_irq is cleared in __unbind_from_irq().
---
v3:
Kernel style brace placement
Delay setting old_cpu and tighten scope of variable
v2:
Different approach changing virq_to_irq
--- drivers/xen/events/events_base.c | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-)
drivers/xen/events/events_base.c +12 -1
@@ -1797,9 +1797,20 @@ static int xen_rebind_evtchn_to_cpu(struct irq_info *info, unsigned int tcpu)* virq or IPI channel, which don't actually need to be rebound. Ignore * it, but don't do the xenlinux-level rebind in that case. */ -if (HYPERVISOR_event_channel_op (EVTCHNOP_bind_vcpu, &bind_vcpu) >=0 ) +if (HYPERVISOR_event_channel_op (EVTCHNOP_bind_vcpu, &bind_vcpu) >=0 ) { +int old_cpu = info->cpu; +bind_evtchn_to_cpu (info, tcpu, false); +if (info->type ==IRQT_VIRQ ) { +int virq = info->u.virq; +int irq =per_cpu (virq_to_irq, old_cpu)[virq]; + +per_cpu (virq_to_irq, old_cpu)[virq] =-1 ; +per_cpu (virq_to_irq, tcpu)[virq] = irq; + } + } +do_unmask (info,EVT_MASK_REASON_TEMPORARY );return 0 ;
Signature · 1 line
2.34.1
Re: [PATCH v3 3/3] xen/events: Update virq_to_irq on migration
Quoted 11 lines · Jason Andryuk
VIRQs come in 3 flavors, per-VPU, per-domain, and global, and the VIRQs are tracked in per-cpu
virq_to_irqarrays.Per-domain and global VIRQs must be bound on CPU 0, and
bind_virq_to_irq()sets theper_cpuvirq_to_irqat registration time Later, the interrupt can migrate, andinfo->cpuis updated. When calling__unbind_from_irq(), the per-cpuvirq_to_irqis cleared for a different cpu. Ifbind_virq_to_irq()is called again with CPU 0, the stale irq is returned. There won't be anyirq_infofor the irq, so things break.
Quoted 7 lines
Make
xen_rebind_evtchn_to_cpu()update theper_cpuvirq_to_irqmappings to keep them update to date with the current cpu. This ensures the correctvirq_to_irqis cleared in__unbind_from_irq().Fixes e46cdb66c8fc ("xen: event channels")Signed-off-by Jason Andryuk <[email protected]>
Juergen