All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] arm/gicv3: Fix deadlock in gicv3_set_affinity()
@ 2026-11-26 16:38 Alejandro Vallejo
  2026-11-30 15:01 ` Orzel, Michal
  0 siblings, 1 reply; 3+ messages in thread
From: Alejandro Vallejo @ 2026-11-26 16:38 UTC (permalink / raw)
  To: xen-devel
  Cc: Alejandro Vallejo, Stefano Stabellini, Julien Grall,
	Bertrand Marquis, Michal Orzel, Volodymyr Babchuk

gicv3_irq_set_affinity() acquires the gicv3 lock with a bare spin_lock()
with IRQs on from arch_move_irqs(). This happens when a vCPU with
physical IRQs routed to it changes pCPU. The same function is called
with IRQs off from vgic_migrate_irq().

If the wrong interrupt arrives while moving IRQs, Xen would re-lock
the mutex, but it'll never succeed because it's already locked in the
interrupted thread of that very pCPU.

Have gicv3_irq_set_affinity() use spin_lock_irqsave() instead to avoid
the deadly embrace.

Fixes: bc183a0235e0 ("xen/arm: Add support for GIC v3")
Signed-off-by: Alejandro Vallejo <alejandro.garciavallejo@amd.com>
---
 xen/arch/arm/gic-v3.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)

diff --git a/xen/arch/arm/gic-v3.c b/xen/arch/arm/gic-v3.c
index cca49d2a2a..ec1e341f2e 100644
--- a/xen/arch/arm/gic-v3.c
+++ b/xen/arch/arm/gic-v3.c
@@ -1463,6 +1463,7 @@ static void gicv3_irq_set_affinity(struct irq_desc *desc, const cpumask_t *mask)
     unsigned int cpu;
     uint64_t affinity;
     void __iomem *addr;
+    unsigned long flags;
 
     if ( desc->irq < NR_GIC_LOCAL_IRQS )
         return;
@@ -1473,7 +1474,7 @@ static void gicv3_irq_set_affinity(struct irq_desc *desc, const cpumask_t *mask)
 
     ASSERT(!cpumask_empty(mask));
 
-    spin_lock(&gicv3.lock);
+    spin_lock_irqsave(&gicv3.lock, flags);
 
     cpu = gicv3_get_cpu_from_mask(mask);
     affinity = gicv3_mpidr_to_affinity(cpu);
@@ -1481,7 +1482,7 @@ static void gicv3_irq_set_affinity(struct irq_desc *desc, const cpumask_t *mask)
     affinity &= ~GICD_IROUTER_SPI_MODE_ANY;
     writeq_relaxed_non_atomic(affinity, addr);
 
-    spin_unlock(&gicv3.lock);
+    spin_unlock_irqrestore(&gicv3.lock, flags);
 }
 
 static int gicv3_make_hwdom_dt_node(const struct domain *d,

base-commit: 1208df803530bb885419627ab36a43313b98ce5c
-- 
2.43.0



^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [PATCH] arm/gicv3: Fix deadlock in gicv3_set_affinity()
  2026-11-26 16:38 [PATCH] arm/gicv3: Fix deadlock in gicv3_set_affinity() Alejandro Vallejo
@ 2026-11-30 15:01 ` Orzel, Michal
  2026-11-30 18:56   ` Alejandro Vallejo
  0 siblings, 1 reply; 3+ messages in thread
From: Orzel, Michal @ 2026-11-30 15:01 UTC (permalink / raw)
  To: Alejandro Vallejo, xen-devel
  Cc: Stefano Stabellini, Julien Grall, Bertrand Marquis,
	Volodymyr Babchuk



On 26-Nov-26 17:38, Alejandro Vallejo wrote:
> gicv3_irq_set_affinity() acquires the gicv3 lock with a bare spin_lock()
> with IRQs on from arch_move_irqs(). This happens when a vCPU with
> physical IRQs routed to it changes pCPU. The same function is called
> with IRQs off from vgic_migrate_irq().
> 
> If the wrong interrupt arrives while moving IRQs, Xen would re-lock
> the mutex, but it'll never succeed because it's already locked in the
> interrupted thread of that very pCPU.
> 
> Have gicv3_irq_set_affinity() use spin_lock_irqsave() instead to avoid
> the deadly embrace.
> 
> Fixes: bc183a0235e0 ("xen/arm: Add support for GIC v3")
> Signed-off-by: Alejandro Vallejo <alejandro.garciavallejo@amd.com>
> ---
>  xen/arch/arm/gic-v3.c | 5 +++--
>  1 file changed, 3 insertions(+), 2 deletions(-)
> 
> diff --git a/xen/arch/arm/gic-v3.c b/xen/arch/arm/gic-v3.c
> index cca49d2a2a..ec1e341f2e 100644
> --- a/xen/arch/arm/gic-v3.c
> +++ b/xen/arch/arm/gic-v3.c
> @@ -1463,6 +1463,7 @@ static void gicv3_irq_set_affinity(struct irq_desc *desc, const cpumask_t *mask)
>      unsigned int cpu;
>      uint64_t affinity;
>      void __iomem *addr;
> +    unsigned long flags;
>  
>      if ( desc->irq < NR_GIC_LOCAL_IRQS )
>          return;
> @@ -1473,7 +1474,7 @@ static void gicv3_irq_set_affinity(struct irq_desc *desc, const cpumask_t *mask)
>  
>      ASSERT(!cpumask_empty(mask));
>  
> -    spin_lock(&gicv3.lock);
> +    spin_lock_irqsave(&gicv3.lock, flags);
>  
>      cpu = gicv3_get_cpu_from_mask(mask);
>      affinity = gicv3_mpidr_to_affinity(cpu);
> @@ -1481,7 +1482,7 @@ static void gicv3_irq_set_affinity(struct irq_desc *desc, const cpumask_t *mask)
>      affinity &= ~GICD_IROUTER_SPI_MODE_ANY;
>      writeq_relaxed_non_atomic(affinity, addr);
>  
> -    spin_unlock(&gicv3.lock);
> +    spin_unlock_irqrestore(&gicv3.lock, flags);
>  }
>  
>  static int gicv3_make_hwdom_dt_node(const struct domain *d,
> 
> base-commit: 1208df803530bb885419627ab36a43313b98ce5c

gicv2_irq_set_affinity() has the same problem.

Shouldn't arch_move_irqs() be fixed instead? It is the only
irq_set_affinity() caller running with IRQs on. All the others hold a lock
taken with irqsave. Fixing the caller would cover both GICs.

~Michal


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH] arm/gicv3: Fix deadlock in gicv3_set_affinity()
  2026-11-30 15:01 ` Orzel, Michal
@ 2026-11-30 18:56   ` Alejandro Vallejo
  0 siblings, 0 replies; 3+ messages in thread
From: Alejandro Vallejo @ 2026-11-30 18:56 UTC (permalink / raw)
  To: Orzel, Michal, xen-devel
  Cc: Stefano Stabellini, Julien Grall, Bertrand Marquis,
	Volodymyr Babchuk

On Mon Nov 30, 2026 at 4:01 PM CET, Orzel, Michal wrote:
>
>
> On 26-Nov-26 17:38, Alejandro Vallejo wrote:
> > gicv3_irq_set_affinity() acquires the gicv3 lock with a bare spin_lock()
> > with IRQs on from arch_move_irqs(). This happens when a vCPU with
> > physical IRQs routed to it changes pCPU. The same function is called
> > with IRQs off from vgic_migrate_irq().
> > 
> > If the wrong interrupt arrives while moving IRQs, Xen would re-lock
> > the mutex, but it'll never succeed because it's already locked in the
> > interrupted thread of that very pCPU.
> > 
> > Have gicv3_irq_set_affinity() use spin_lock_irqsave() instead to avoid
> > the deadly embrace.
> > 
> > Fixes: bc183a0235e0 ("xen/arm: Add support for GIC v3")
> > Signed-off-by: Alejandro Vallejo <alejandro.garciavallejo@amd.com>
> > ---
> >  xen/arch/arm/gic-v3.c | 5 +++--
> >  1 file changed, 3 insertions(+), 2 deletions(-)
> > 
> > diff --git a/xen/arch/arm/gic-v3.c b/xen/arch/arm/gic-v3.c
> > index cca49d2a2a..ec1e341f2e 100644
> > --- a/xen/arch/arm/gic-v3.c
> > +++ b/xen/arch/arm/gic-v3.c
> > @@ -1463,6 +1463,7 @@ static void gicv3_irq_set_affinity(struct irq_desc *desc, const cpumask_t *mask)
> >      unsigned int cpu;
> >      uint64_t affinity;
> >      void __iomem *addr;
> > +    unsigned long flags;
> >  
> >      if ( desc->irq < NR_GIC_LOCAL_IRQS )
> >          return;
> > @@ -1473,7 +1474,7 @@ static void gicv3_irq_set_affinity(struct irq_desc *desc, const cpumask_t *mask)
> >  
> >      ASSERT(!cpumask_empty(mask));
> >  
> > -    spin_lock(&gicv3.lock);
> > +    spin_lock_irqsave(&gicv3.lock, flags);
> >  
> >      cpu = gicv3_get_cpu_from_mask(mask);
> >      affinity = gicv3_mpidr_to_affinity(cpu);
> > @@ -1481,7 +1482,7 @@ static void gicv3_irq_set_affinity(struct irq_desc *desc, const cpumask_t *mask)
> >      affinity &= ~GICD_IROUTER_SPI_MODE_ANY;
> >      writeq_relaxed_non_atomic(affinity, addr);
> >  
> > -    spin_unlock(&gicv3.lock);
> > +    spin_unlock_irqrestore(&gicv3.lock, flags);
> >  }
> >  
> >  static int gicv3_make_hwdom_dt_node(const struct domain *d,
> > 
> > base-commit: 1208df803530bb885419627ab36a43313b98ce5c
>
> gicv2_irq_set_affinity() has the same problem.

True, I only looked at v3.

>
> Shouldn't arch_move_irqs() be fixed instead? It is the only
> irq_set_affinity() caller running with IRQs on. All the others hold a lock
> taken with irqsave. Fixing the caller would cover both GICs.

Works for me. Let me send a v2.

Cheers,
Alejandro


^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2026-11-30 18:57 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-26 16:38 [PATCH] arm/gicv3: Fix deadlock in gicv3_set_affinity() Alejandro Vallejo
2026-11-30 15:01 ` Orzel, Michal
2026-11-30 18:56   ` Alejandro Vallejo

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.