xen/arm: Fix eSPI IRQ handling
v4 cover xen/arm: Fix eSPI IRQ handling
This series fixes sparse eSPI INTID handling and checks errors returned by irq_set_type().
Patch 1 makes is_espi() check the architectural INTID range regardless of CONFIG_GICV3_ESPI. If the GIC reports an eSPI without compiled-in support, Xen stops with BUG_ON(). Virtual eSPI pending lookups return NULL when eSPI support is disabled.
Xen has IRQ descriptors for INTIDs below NR_IRQS and, with eSPI support, for eSPIs starting at 4096. It has no descriptors for INTIDs 1024 through 4095. Patch 2 checks INTIDs in setup_irq() and irq_set_spi_type() before these functions look up a descriptor. irq_set_spi_type() checks descriptor ranges because it can run before the GIC line counts are known. setup_irq() uses the line counts once they are available.
Patch 3 fixes the vGIC allocation bitmap. Reserving an eSPI used a compact bitmap index, but freeing it used the raw virtual INTID. This could write past the bitmap and leave the eSPI reserved.
Patch 4, introduced in v2, checks errors from irq_set_type() in the GTDT, MADT, SPCR, and FF-A paths. GTDT and MADT could keep a rejected timer or maintenance INTID and later use it in a direct descriptor lookup. This patch also fixes MISRA C Rule 17.7 violations.
Additional testing during v4 review, before the final edits:
- Patch 1: QEMU and FVP tests with CONFIG_GICV3_ESPI, CONFIG_HAS_ITS, and CONFIG_DEBUG enabled and disabled.
- Patch 1: pending lookup checks for all eSPI INTIDs, including NULL results without eSPI support.
- Patch 1: physical and virtual eSPI delivery on FVP, including delivery to both vCPUs and retriggering an active eSPI.
- Patch 1: injected an unsupported physical eSPI and confirmed BUG_ON() in both debug and release builds.
- Patch 2: Arm64 builds with CONFIG_GICV3_ESPI and CONFIG_DEBUG enabled and disabled.
Testing on v3:
- Arm64 debug builds with CONFIG_ACPI=y and CONFIG_FFA=y, both with and without CONFIG_GICV3_ESPI.
- FVP Device Tree boot with 64 eSPIs; Linux dom0 started.
- QEMU virt UEFI/ACPI boot to a dom0 initramfs shell; this covered the GTDT, GICv3 MADT, and PL011 SPCR paths.
Changes in v4:
- Use BUG_ON() when the GIC reports an eSPI without compiled-in support.
- Return NULL for virtual eSPI pending lookups when support is disabled.
- Remove the redundant CONFIG_GICV3_ESPI check in IRQ dispatch.
- Share irq_has_desc() with the assertion in __irq_to_desc().
- Log invalid IRQs rejected by setup_irq().
- Document the compressed vIRQ allocation bitmap above the conversion helpers, with an ASCII diagram and a reference to struct vgic_dist.
- Clarify the is_espi() commit message and drop unrelated blank-line removals.
- Add Reviewed-by tags.
Changes in v3:
- Add a preparatory patch making is_espi() a pure range predicate and move configuration policy and debug checks to callers.
- Add the requested assertion before regular descriptor lookup.
- Avoid partial MADT and UART state updates after irq_set_type() failures.
- Apply cosmetic cleanups from review.
Changes in v2:
- Check descriptor ranges in irq_set_spi_type() and implemented GIC lines in setup_irq().
- Keep the is_espi() debug check when CONFIG_GICV3_ESPI is disabled.
- Remove a redundant CONFIG_GICV3_ESPI guard from the vGIC code.
- Add patch 3 to check irq_set_type() errors in the GTDT, MADT, SPCR, and FF-A paths.
- Target master instead of the 4.22 release.
Mykola Kvach (4):
xen/arm: make is_espi() a pure range predicate xen/arm: validate IRQs before descriptor lookup xen/arm: vgic: free eSPIs using the bitmap index xen/arm: handle irq_set_type() failures
xen/arch/arm/gic-v2.c | 15 ++++--- xen/arch/arm/gic-v3.c | 15 ++++--- xen/arch/arm/gic.c | 2 + xen/arch/arm/include/asm/irq.h | 11 ------ xen/arch/arm/irq.c | 28 +++++++++++-- xen/arch/arm/tee/ffa_notif.c | 11 +++++- xen/arch/arm/time.c | 18 +++++++-- xen/arch/arm/vgic.c | 72 ++++++++++++++++++++++------------ xen/drivers/char/ns16550.c | 8 +++- xen/drivers/char/pl011.c | 4 +- 10 files changed, 125 insertions(+), 59 deletions(-)
Signature · 1 line
2.53.0
PATCH v4 1/4 xen/arm: make is_espi() a pure range predicate
is_espi() currently changes its result according to CONFIG_GICV3_ESPI. Without eSPI support, it returns false, and its assertion fails if an eSPI INTID is passed. Callers therefore use it both to identify eSPIs and to exclude eSPI handling when support is disabled.
Make is_espi() report only whether an INTID is in the architectural eSPI range. Check for eSPI support at the call sites.
Use BUG_ON() if the GIC reports an eSPI without compiled-in support, matching the handling of unsupported LPIs. Return NULL from spi_to_pending() for an eSPI when support is disabled, using an espi_to_pending() stub.
---
Changes in v4:
- Clarify the existing is_espi() behavior in the commit message.
- Drop the unrelated blank-line removal in vgic.c.
- Use BUG_ON() if the GIC reports an eSPI without eSPI support.
- Drop the redundant CONFIG_GICV3_ESPI check in IRQ dispatch.
- Return NULL for virtual eSPI lookup when eSPI support is disabled.
Changes in v3:
- New preparatory cleanup requested during review.
--- xen/arch/arm/gic.c | 2 ++ xen/arch/arm/include/asm/irq.h | 11 ----------- xen/arch/arm/vgic.c | 30 ++++++++++++++++-------------- 3 files changed, 18 insertions(+), 25 deletions(-)
xen/arch/arm/gic.c +2 -0
@@ -348,6 +348,8 @@ void gic_interrupt(struct cpu_user_regs *regs, int is_fiq)/* Reading IRQ will ACK it */ irq = gic_hw_ops->read_irq (); +BUG_ON (!IS_ENABLED (CONFIG_GICV3_ESPI ) &&is_espi (irq)); +if (likely (irq >=GIC_SGI_STATIC_MAX && irq <1020 ) ||is_espi (irq) ) {isb ();
xen/arch/arm/include/asm/irq.h +0 -11
@@ -66,18 +66,7 @@ static inline bool is_lpi(unsigned int irq)static inline bool is_espi (unsigned int irq) { -#ifdef CONFIG_GICV3_ESPI return irq >=ESPI_BASE_INTID && irq <=ESPI_MAX_INTID ; -#else -/* -* The function should not be called for eSPIs when CONFIG_GICV3_ESPI is -* disabled. Returning false allows the compiler to optimize the code -* when the config is disabled, while the assert ensures that out-of-range -* array resources are not accessed. -*/ -ASSERT (!(irq >=ESPI_BASE_INTID && irq <=ESPI_MAX_INTID )); -return false; -#endif }static inline unsigned int espi_intid_to_idx (unsigned int intid)
xen/arch/arm/vgic.c +16 -14
@@ -62,6 +62,13 @@ static inline struct vgic_irq_rank *vgic_get_espi_rank(struct vcpu *v, return &v->domain->arch.vgic.ext_shared_irqs[EXT_RANK_NUM2IDX(rank)]; } +static struct pending_irq *espi_to_pending (struct domain *d,unsigned int irq) +{ +unsigned int idx =espi_intid_to_idx (irq) + d->arch.vgic.nr_spis; + +return &d->arch.vgic.pending_irqs[idx]; +} +#else static inline bool is_valid_espi_rank (struct domain *d,unsigned int rank) {
@@ -78,6 +85,11 @@ static inline struct vgic_irq_rank *vgic_get_espi_rank(struct vcpu *v,ASSERT_UNREACHABLE ();return NULL ; } + +static struct pending_irq *espi_to_pending (struct domain *d,unsigned int irq) +{ +return NULL ; +}#endif static inline struct vgic_irq_rank *vgic_get_rank(struct vcpu *v,
@@ -696,8 +708,8 @@ bool vgic_to_sgi(struct vcpu *v, register_t sgir, enum gic_sgi_mode irqmode,/* * Returns the pointer to the struct pending_irq belonging to the given * interrupt. -* This can return NULL if called for an LPI which has been unmapped -* meanwhile. +* This can return NULL for an eSPI when support is disabled, or for an +* LPI which has been unmapped meanwhile. */ struct pending_irq *irq_to_pending (struct vcpu *v,unsigned int irq) {
@@ -715,22 +727,12 @@ struct pending_irq *irq_to_pending(struct vcpu *v, unsigned int irq)struct pending_irq *spi_to_pending (struct domain *d,unsigned int irq) { -unsigned int idx; -ASSERT (irq >=NR_LOCAL_IRQS );if (is_espi (irq) ) - { -unsigned int nr_spis = d->arch.vgic.nr_spis; - - idx =espi_intid_to_idx (irq) + nr_spis; - } -else - { - idx = irq -NR_LOCAL_IRQS ; - } +return espi_to_pending (d, irq); -return &d->arch.vgic.pending_irqs[idx]; +return &d->arch.vgic.pending_irqs[irq -NR_LOCAL_IRQS ]; }void vgic_clear_pending_irqs (struct vcpu *v)
Signature · 1 line
2.53.0
Re: [PATCH v4 1/4] xen/arm: make is_espi() a pure range predicate
Quoted 35 lines · Mykola Kvach
is_espi()currently changes its result according toCONFIG_GICV3_ESPI. Without eSPI support, it returns false, and its assertion fails if an eSPI INTID is passed. Callers therefore use it both to identify eSPIs and to exclude eSPI handling when support is disabled.Make
is_espi()report only whether an INTID is in the architectural eSPI range. Check for eSPI support at the call sites.Use
BUG_ON()if the GIC reports an eSPI without compiled-in support, matching the handling of unsupported LPIs. Return NULL fromspi_to_pending()for an eSPI when support is disabled, using anespi_to_pending()stub.Signed-off-by Mykola Kvach <[email protected]>---
Changes in v4:
- Clarify the existingis_espi()behavior in the commit message.
- Drop the unrelated blank-line removal invgic.c.
- UseBUG_ON()if the GIC reports an eSPI without eSPI support.
- Drop the redundantCONFIG_GICV3_ESPIcheck in IRQ dispatch.
- Return NULL for virtual eSPI lookup when eSPI support is disabled.Changes in v3:
- New preparatory cleanup requested during review.--- xen/arch/arm/gic.c | 2 ++ xen/arch/arm/include/asm/irq.h | 11 ----------- xen/arch/arm/vgic.c | 30 ++++++++++++++++-------------- 3 files changed, 18 insertions(+), 25 deletions(-)xen/arch/arm/gic.c +0
-0
xen/arch/arm/gic.c void gic_interrupt(struct cpu_user_regs *regs, int is_fiq) · line 351
Quoted 4 lines
xen/arch/arm/gic.c +1
-0@@ -348,6 +348,8 @@ void gic_interrupt(struct cpu_user_regs *regs, int is_fiq)/* Reading IRQ will ACK it */ irq = gic_hw_ops->read_irq (); +BUG_ON (!IS_ENABLED (CONFIG_GICV3_ESPI ) &&is_espi (irq));
Usually BUG() should come with a comment explaining why we cannot continue. Here it's that without config enabled, there's no descriptor and no pending_irq storage for it. Please add a comment.
Quoted 32 lines
xen/arch/arm/gic.c +1
-0+if (likely (irq >=GIC_SGI_STATIC_MAX && irq <1020 ) ||is_espi (irq) ) {isb ();xen/arch/arm/include/asm/irq.h +0
-11@@ -66,18 +66,7 @@ static inline bool is_lpi(unsigned int irq)static inline bool is_espi (unsigned int irq) { -#ifdef CONFIG_GICV3_ESPI return irq >=ESPI_BASE_INTID && irq <=ESPI_MAX_INTID ; -#else -/* -* The function should not be called for eSPIs when CONFIG_GICV3_ESPI is -* disabled. Returning false allows the compiler to optimize the code -* when the config is disabled, while the assert ensures that out-of-range -* array resources are not accessed. -*/ -ASSERT (!(irq >=ESPI_BASE_INTID && irq <=ESPI_MAX_INTID )); -return false; -#endif }static inline unsigned int espi_intid_to_idx (unsigned int intid)xen/arch/arm/vgic.c +0
-0
xen/arch/arm/vgic.c static inline struct vgic_irq_rank *vgic_get_espi_rank(struct vcpu *v, · line 65
Quoted 4 lines
xen/arch/arm/vgic.c +1
-0@@ -62,6 +62,13 @@ static inline struct vgic_irq_rank *vgic_get_espi_rank(struct vcpu *v,return &v->domain->arch.vgic.ext_shared_irqs[EXT_RANK_NUM2IDX (rank)]; } +static struct pending_irq *espi_to_pending (struct domain *d,unsigned int irq)
Constify d, please.
Also, use static inline like the surrounding helpers.
Quoted 10 lines
xen/arch/arm/vgic.c +6
-0+{ +unsigned int idx =espi_intid_to_idx (irq) + d->arch.vgic.nr_spis; + +return &d->arch.vgic.pending_irqs[idx]; +} +#else static inline bool is_valid_espi_rank (struct domain *d,unsigned int rank) {
xen/arch/arm/vgic.c static inline struct vgic_irq_rank *vgic_get_espi_rank(struct vcpu *v, · line 90
Quoted 6 lines
xen/arch/arm/vgic.c +3
-0@@ -78,6 +85,11 @@ static inline struct vgic_irq_rank *vgic_get_espi_rank(struct vcpu *v,ASSERT_UNREACHABLE ();return NULL ; } + +static struct pending_irq *espi_to_pending (struct domain *d,unsigned int irq) +{
Add ASSERT_UNREACHABLE() here like for vgic_get_espi_rank().
xen/arch/arm/vgic.c bool vgic_to_sgi(struct vcpu *v, register_t sgir, enum gic_sgi_mode irqmode, · line 712
Quoted 13 lines
xen/arch/arm/vgic.c +4
-2+return NULL ; +}#endif static inline struct vgic_irq_rank *vgic_get_rank(struct vcpu *v,@@ -696,8 +708,8 @@ bool vgic_to_sgi(struct vcpu *v, register_t sgir, enum gic_sgi_mode irqmode,/* * Returns the pointer to the struct pending_irq belonging to the given * interrupt. -* This can return NULL if called for an LPI which has been unmapped -* meanwhile. +* This can return NULL for an eSPI when support is disabled, or for an +* LPI which has been unmapped meanwhile.
This puts LPI and eSPIs on exactly the same level which is not correct. While unmapped LPI can happen, eSPI with support disabled must not. Leave the LPI comment as is and clarify eSPI in one sentence.
Otherwsie, LGTM.
~Michal
Re: [PATCH v4 1/4] xen/arm: make is_espi() a pure range predicate
Hi Michal,
Thank you for the review.
Quoted 41 lines · Michal
Quoted 38 lines · Mykola Kvach
is_espi()currently changes its result according toCONFIG_GICV3_ESPI. Without eSPI support, it returns false, and its assertion fails if an eSPI INTID is passed. Callers therefore use it both to identify eSPIs and to exclude eSPI handling when support is disabled.Make
is_espi()report only whether an INTID is in the architectural eSPI range. Check for eSPI support at the call sites.Use
BUG_ON()if the GIC reports an eSPI without compiled-in support, matching the handling of unsupported LPIs. Return NULL fromspi_to_pending()for an eSPI when support is disabled, using anespi_to_pending()stub.Signed-off-by Mykola Kvach <[email protected]>---
Changes in v4:
- Clarify the existingis_espi()behavior in the commit message.
- Drop the unrelated blank-line removal invgic.c.
- UseBUG_ON()if the GIC reports an eSPI without eSPI support.
- Drop the redundantCONFIG_GICV3_ESPIcheck in IRQ dispatch.
- Return NULL for virtual eSPI lookup when eSPI support is disabled.Changes in v3:
- New preparatory cleanup requested during review.--- xen/arch/arm/gic.c | 2 ++ xen/arch/arm/include/asm/irq.h | 11 ----------- xen/arch/arm/vgic.c | 30 ++++++++++++++++-------------- 3 files changed, 18 insertions(+), 25 deletions(-)xen/arch/arm/gic.c +0
-0@@ -348,6 +348,8 @@ void gic_interrupt(struct cpu_user_regs *regs, int is_fiq)/* Reading IRQ will ACK it */ irq = gic_hw_ops->read_irq ();
Quoted 4 lines
xen/arch/arm/gic.c void gic_interrupt(struct cpu_user_regs *regs, int is_fiq) · line 351
Quoted 1 line
xen/arch/arm/gic.c +1
-0+BUG_ON (!IS_ENABLED (CONFIG_GICV3_ESPI ) &&is_espi (irq));Usually
BUG()should come with a comment explaining why we cannot continue. Here it's that without config enabled, there's no descriptor and nopending_irqstorage for it. Please add a comment.
Ack.
Quoted 36 lines
Quoted 36 lines
xen/arch/arm/gic.c +1
-0+if (likely (irq >=GIC_SGI_STATIC_MAX && irq <1020 ) ||is_espi (irq) ) {isb ();xen/arch/arm/include/asm/irq.h +0
-11@@ -66,18 +66,7 @@ static inline bool is_lpi(unsigned int irq)static inline bool is_espi (unsigned int irq) { -#ifdef CONFIG_GICV3_ESPI return irq >=ESPI_BASE_INTID && irq <=ESPI_MAX_INTID ; -#else -/* -* The function should not be called for eSPIs when CONFIG_GICV3_ESPI is -* disabled. Returning false allows the compiler to optimize the code -* when the config is disabled, while the assert ensures that out-of-range -* array resources are not accessed. -*/ -ASSERT (!(irq >=ESPI_BASE_INTID && irq <=ESPI_MAX_INTID )); -return false; -#endif }static inline unsigned int espi_intid_to_idx (unsigned int intid)xen/arch/arm/vgic.c +0
-0@@ -62,6 +62,13 @@ static inline struct vgic_irq_rank *vgic_get_espi_rank(struct vcpu *v,return &v->domain->arch.vgic.ext_shared_irqs[EXT_RANK_NUM2IDX (rank)]; }
Quoted 3 lines
xen/arch/arm/vgic.c static inline struct vgic_irq_rank *vgic_get_espi_rank(struct vcpu *v, · line 65
Quoted 1 line
xen/arch/arm/vgic.c +1
-0+static struct pending_irq *espi_to_pending (struct domain *d,unsigned int irq)Constify d, please.
Also, use static inline like the surrounding helpers.
Ack.
Quoted 10 lines
Quoted 10 lines
xen/arch/arm/vgic.c +6
-0+{ +unsigned int idx =espi_intid_to_idx (irq) + d->arch.vgic.nr_spis; + +return &d->arch.vgic.pending_irqs[idx]; +} +#else static inline bool is_valid_espi_rank (struct domain *d,unsigned int rank) {
Quoted 8 lines
xen/arch/arm/vgic.c static inline struct vgic_irq_rank *vgic_get_espi_rank(struct vcpu *v, · line 90
Quoted 7 lines
xen/arch/arm/vgic.c +3
-0@@ -78,6 +85,11 @@ static inline struct vgic_irq_rank *vgic_get_espi_rank(struct vcpu *v,ASSERT_UNREACHABLE ();return NULL ; } + +static struct pending_irq *espi_to_pending (struct domain *d,unsigned int irq) +{Add
ASSERT_UNREACHABLE()here like forvgic_get_espi_rank().
Ack.
Quoted 5 lines
Quoted 5 lines
xen/arch/arm/vgic.c +2
-0+ return NULL; +}#endif
Quoted 12 lines
xen/arch/arm/vgic.c bool vgic_to_sgi(struct vcpu *v, register_t sgir, enum gic_sgi_mode irqmode, · line 712
Quoted 9 lines
xen/arch/arm/vgic.c +2
-2static inline struct vgic_irq_rank *vgic_get_rank(struct vcpu *v,@@ -696,8 +708,8 @@ bool vgic_to_sgi(struct vcpu *v, register_t sgir, enum gic_sgi_mode irqmode,/* * Returns the pointer to the struct pending_irq belonging to the given * interrupt. -* This can return NULL if called for an LPI which has been unmapped -* meanwhile. +* This can return NULL for an eSPI when support is disabled, or for an +* LPI which has been unmapped meanwhile. This puts LPI and eSPIs on exactly the same level which is not correct. While unmapped LPI can happen, eSPI with support disabled must not. Leave the LPI comment as is and clarify eSPI in one sentence.
Ack.
Best regards,
Mykola
PATCH v4 2/4 xen/arm: validate IRQs before descriptor lookup
In reply to Mykola KvachGICv3 eSPI support makes nr_irqs span the architectural INTID namespace through ESPI_MAX_INTID, but descriptor storage is sparse. local_irq_desc[] and irq_desc[] cover INTIDs below NR_IRQS, while espi_desc[] covers eSPIs. INTIDs 1024 through 4095 have no backing descriptors.
Validation based only on nr_irqs accepts an INTID in this gap. __irq_to_desc() then indexes beyond irq_desc[], and callers may lock or update unrelated Xen memory.
Reject INTIDs that the GIC reports as unimplemented in setup_irq() before looking up a descriptor. irq_set_spi_type() can run before the implemented GIC line counts are available, so validate descriptor-backed ranges there before looking up a descriptor.
Use the same descriptor range check in irq_set_spi_type() and the assertion in __irq_to_desc() to keep them in sync. Log the IRQ number when setup_irq() rejects an invalid line.
---
Changes in v4:
- Share irq_has_desc() with the assertion in __irq_to_desc().
- Log invalid IRQs rejected by setup_irq().
- Drop the unrelated blank line removal.
Changes in v3:
- Add the requested bound assertion and retain the SPI-only comment.
Changes in v2:
- Validate descriptor-backed ranges in irq_set_spi_type().
- Validate implemented GIC lines in setup_irq().
- Preserve is_espi() validation with CONFIG_GICV3_ESPI disabled.
--- xen/arch/arm/irq.c | 28 +++++++++++++++++++++++++--- 1 file changed, 25 insertions(+), 3 deletions(-)
xen/arch/arm/irq.c +25 -3
@@ -85,6 +85,12 @@ static int __init init_espi_data(void) static DEFINE_PER_CPU(irq_desc_t[NR_LOCAL_IRQS], local_irq_desc); +static bool irq_has_desc (unsigned int irq) +{ +return irq <NR_IRQS || + (IS_ENABLED (CONFIG_GICV3_ESPI ) &&is_espi (irq)); +} +struct irq_desc *__irq_to_desc (unsigned int irq) {if ( irq <NR_LOCAL_IRQS )
@@ -95,6 +101,8 @@ struct irq_desc *__irq_to_desc(unsigned int irq)return espi_to_desc (irq);#endif +ASSERT (irq_has_desc (irq)); +return &irq_desc[irq-NR_LOCAL_IRQS ]; }
@@ -416,6 +424,12 @@ int setup_irq(unsigned int irq, unsigned int irqflags, struct irqaction *new)struct irq_desc *desc;bool disabled; +if ( !gic_is_valid_line (irq) ) + { +printk (XENLOG_ERR "Cannot set up IRQ %u: invalid GIC interrupt\n" , irq); +return -EINVAL ; + } + desc =irq_to_desc (irq);spin_lock_irqsave (&desc->lock, flags);
@@ -647,13 +661,21 @@ static bool irq_validate_new_type(unsigned int curr, unsigned int new)int irq_set_spi_type (unsigned int spi,unsigned int type) {unsigned long flags; -struct irq_desc *desc =irq_to_desc (spi); +struct irq_desc *desc;int ret = -EBUSY ; -/* This function should not be used for other than SPIs */ -if ( spi <NR_LOCAL_IRQS ) +/* +* This function should not be used for other than SPIs. +* +* The implemented GIC line counts are not available when early +* callers configure IRQ types. Check descriptor storage here; setup_irq() +* validates the implemented line before the interrupt is used. +*/ +if ( spi <NR_LOCAL_IRQS || !irq_has_desc (spi) )return -EINVAL ; + desc =irq_to_desc (spi); +spin_lock_irqsave (&desc->lock, flags);if ( !irq_validate_new_type (desc->arch.type, type) )
Signature · 1 line
2.53.0
Re: [PATCH v4 2/4] xen/arm: validate IRQs before descriptor lookup
Quoted 14 lines · Mykola Kvach
GICv3 eSPI support makes
nr_irqsspan the architectural INTID namespace throughESPI_MAX_INTID, but descriptor storage is sparse. local_irq_desc[] and irq_desc[] cover INTIDs belowNR_IRQS, while espi_desc[] covers eSPIs. INTIDs 1024 through 4095 have no backing descriptors.Validation based only on
nr_irqsaccepts an INTID in this gap.__irq_to_desc()then indexes beyond irq_desc[], and callers may lock or update unrelated Xen memory.Reject INTIDs that the GIC reports as unimplemented in
setup_irq()before looking up a descriptor.irq_set_spi_type()can run before the implemented GIC line counts are available, so validate descriptor-backed ranges there before looking up a descriptor.
Quoted 6 lines
Use the same descriptor range check in
irq_set_spi_type()and the assertion in__irq_to_desc()to keep them in sync. Log the IRQ number whensetup_irq()rejects an invalid line.Fixes 98f7060b9ed5 ("xen/arm/irq: add handling for IRQs in the eSPI range")Signed-off-by Mykola Kvach <[email protected]>
~Michal
PATCH v4 3/4 xen/arm: vgic: free eSPIs using the bitmap index
In reply to Mykola KvachThe allocated_irqs bitmap in the existing vGIC implementation stores eSPI allocation bits immediately after the regular vIRQ bits. vgic_reserve_virq() converts an eSPI INTID to this compressed bitmap index, but vgic_free_virq() used the raw INTID.
Freeing INTID 4096 therefore clears bit 4096 instead of the first eSPI bit. This writes beyond allocated_irqs and leaves the intended eSPI bit set. Valid eSPIs reach this path during DOMCTL bind failure cleanup and unbind, and during vPL011 teardown.
Add virq_to_idx(), the inverse of idx_to_virq(), and use it when reserving and freeing vIRQs. Validate a vIRQ before clearing its allocation bit.
---
Changes in v4:
- Document the compressed bitmap layout with an ASCII diagram above the conversion helpers and refer to struct vgic_dist for the full layout.
Changes in v3:
- Adapt virq_to_idx() to the configuration-neutral is_espi() helper.
Changes in v2:
- Call is_espi() without a configuration guard.
--- xen/arch/arm/vgic.c | 42 +++++++++++++++++++++++++++++++----------- 1 file changed, 31 insertions(+), 11 deletions(-)
xen/arch/arm/vgic.c +31 -11
@@ -25,6 +25,21 @@#include <asm/vgic.h> +/* +* The allocated_irqs bitmap is compressed: eSPI bits immediately follow +* regular IRQ bits, skipping the gap in the INTID space. +* +* +-----------+-----------+-------------------+-------------------+ +* | SGIs | PPIs | SPIs | eSPIs | +* +-----------+-----------+-------------------+-------------------+ +* 0 16 32 vgic_num_irqs(d) +* +* INTID ESPI_BASE_INTID maps to bitmap index vgic_num_irqs(d). +* The following idx_to_virq() and virq_to_idx() convert between INTIDs +* and bitmap indexes. +* +* See also the allocated_irqs comment in struct vgic_dist. +*/ static inline unsigned int idx_to_virq (struct domain *d,unsigned int idx) {if ( idx >=vgic_num_irqs (d) )
@@ -33,6 +48,16 @@ static inline unsigned int idx_to_virq(struct domain *d, unsigned int idx)return idx; } +static inline unsigned int virq_to_idx (struct domain *d,unsigned int virq) +{ +ASSERT (IS_ENABLED (CONFIG_GICV3_ESPI ) || !is_espi (virq)); + +if (IS_ENABLED (CONFIG_GICV3_ESPI ) &&is_espi (virq) ) +return espi_intid_to_idx (virq) +vgic_num_irqs (d); + +return virq; +} +bool vgic_is_valid_line (struct domain *d,unsigned int virq) {#ifdef CONFIG_GICV3_ESPI
@@ -850,19 +875,11 @@ bool vgic_emulate(struct cpu_user_regs *regs, union hsr hsr)bool vgic_reserve_virq (struct domain *d,unsigned int virq) { -unsigned int idx = virq; -if ( !vgic_is_valid_line (d, virq) )return false; -if (is_espi (virq) ) - { -unsigned int num_regular_irqs =vgic_num_irqs (d); - - idx =espi_intid_to_idx (virq) + num_regular_irqs; - } - -return !test_and_set_bit (idx, d->arch.vgic.allocated_irqs); +return !test_and_set_bit (virq_to_idx (d, virq), + d->arch.vgic.allocated_irqs); }int vgic_allocate_virq (struct domain *d,bool spi)
@@ -899,7 +916,10 @@ int vgic_allocate_virq(struct domain *d, bool spi)void vgic_free_virq (struct domain *d,unsigned int virq) { -clear_bit (virq, d->arch.vgic.allocated_irqs); +if ( !vgic_is_valid_line (d, virq) ) +return ; + +clear_bit (virq_to_idx (d, virq), d->arch.vgic.allocated_irqs); }unsigned int vgic_max_vcpus (unsigned int domctl_vgic_version)
Signature · 1 line
2.53.0
Re: [PATCH v4 3/4] xen/arm: vgic: free eSPIs using the bitmap index
Quoted 5 lines · Mykola Kvach
The
allocated_irqsbitmap in the existing vGIC implementation stores eSPI allocation bits immediately after the regular vIRQ bits.vgic_reserve_virq()converts an eSPI INTID to this compressed bitmap index, butvgic_free_virq()used the raw INTID.
Quoted 7 lines
Freeing INTID 4096 therefore clears bit 4096 instead of the first eSPI bit. This writes beyond
allocated_irqsand leaves the intended eSPI bit set. Valid eSPIs reach this path during DOMCTL bind failure cleanup and unbind, and during vPL011 teardown.Add
virq_to_idx(), the inverse ofidx_to_virq(), and use it when reserving and freeing vIRQs. Validate a vIRQ before clearing its allocation bit.
Please also add this validation (just vgic_is_valid_line() guard) to a new VGIC vgic.c. It's eSPI agnostic and we should keep them in sync if possible.
~Michal
Re: [PATCH v4 3/4] xen/arm: vgic: free eSPIs using the bitmap index
Quoted 13 lines · Michal
Quoted 10 lines · Mykola Kvach
The
allocated_irqsbitmap in the existing vGIC implementation stores eSPI allocation bits immediately after the regular vIRQ bits.vgic_reserve_virq()converts an eSPI INTID to this compressed bitmap index, butvgic_free_virq()used the raw INTID.Freeing INTID 4096 therefore clears bit 4096 instead of the first eSPI bit. This writes beyond
allocated_irqsand leaves the intended eSPI bit set. Valid eSPIs reach this path during DOMCTL bind failure cleanup and unbind, and during vPL011 teardown.
Quoted 4 lines
Quoted 2 lines
Add
virq_to_idx(), the inverse ofidx_to_virq(), and use it when reserving and freeing vIRQs. Validate a vIRQ before clearing its allocation bit.Please also add this validation (just
vgic_is_valid_line()guard) to a new VGICvgic.c. It's eSPI agnostic and we should keep them in sync if possible.
Sure, I can add the validation there as well.
Just to clarify, NEW_VGIC currently selects GICV2 and GICV3_ESPI depends on GICV3 && !NEW_VGIC, so the eSPI bitmap issue itself cannot affect the new VGIC. But I agree that the validity check is generic and it makes sense to keep vgic_free_virq() consistent between the two implementations.
Best regards,
Mykola
PATCH v4 4/4 xen/arm: handle irq_set_type() failures
In reply to Mykola KvachSeveral Arm firmware initialization paths discard irq_set_type()'s return value, violating MISRA C Rule 17.7. If trigger configuration fails, initialization continues with an IRQ that was not configured as requested.
GTDT and MADT retain rejected timer and maintenance INTIDs. check_timer_irq_cfg() and release_irq() later perform unconditional descriptor lookups on those values. Xen has no backing descriptors for INTIDs 1024 through 4095, so retaining one can cause an out-of-bounds access.
Check the return value in the GTDT, MADT, SPCR, and FF-A paths. Store timer INTIDs only after successful trigger configuration, make GTDT parsing failure fatal, and stop UART or notification setup when trigger configuration fails.
---
Changes in v3:
- Avoid partial state updates and simplify maintenance IRQ setup.
Changes in v2:
- New patch.
--- xen/arch/arm/gic-v2.c | 15 +++++++++------ xen/arch/arm/gic-v3.c | 15 +++++++++------ xen/arch/arm/tee/ffa_notif.c | 11 ++++++++++- xen/arch/arm/time.c | 18 ++++++++++++++---- xen/drivers/char/ns16550.c | 8 ++++++-- xen/drivers/char/pl011.c | 4 +++- 6 files changed, 51 insertions(+), 20 deletions(-)
xen/arch/arm/gic-v2.c +9 -6
@@ -1166,17 +1166,20 @@ gic_acpi_parse_madt_cpu(struct acpi_subtable_header *header,/* Read from APIC table and fill up the GIC variables */ if ( cpu_base_assigned ==0 ) { +int rc; + + rc =irq_set_type (processor->vgic_interrupt, + processor->flags &ACPI_MADT_VGIC_IRQ_MODE ? +IRQ_TYPE_EDGE_BOTH :IRQ_TYPE_LEVEL_MASK ); + +if ( rc ) +return rc; + cbase = processor->base_address; csize =SZ_8K ; hbase = processor->gich_base_address; vbase = processor->gicv_base_address; gicv2_info.maintenance_irq = processor->vgic_interrupt; - -if ( processor->flags &ACPI_MADT_VGIC_IRQ_MODE ) -irq_set_type (gicv2_info.maintenance_irq,IRQ_TYPE_EDGE_BOTH ); -else -irq_set_type (gicv2_info.maintenance_irq,IRQ_TYPE_LEVEL_MASK ); - cpu_base_assigned =1 ; }else
xen/arch/arm/gic-v3.c +9 -6
@@ -1743,15 +1743,18 @@ gic_acpi_parse_madt_cpu(struct acpi_subtable_header *header,/* Read from APIC table and fill up the GIC variables */ if ( !cpu_base_assigned ) { +int rc; + + rc =irq_set_type (processor->vgic_interrupt, + processor->flags &ACPI_MADT_VGIC_IRQ_MODE ? +IRQ_TYPE_EDGE_BOTH :IRQ_TYPE_LEVEL_MASK ); + +if ( rc ) +return rc; + cbase = processor->base_address; vbase = processor->gicv_base_address; gicv3_info.maintenance_irq = processor->vgic_interrupt; - -if ( processor->flags &ACPI_MADT_VGIC_IRQ_MODE ) -irq_set_type (gicv3_info.maintenance_irq,IRQ_TYPE_EDGE_BOTH ); -else -irq_set_type (gicv3_info.maintenance_irq,IRQ_TYPE_LEVEL_MASK ); - cpu_base_assigned =1 ; }else
xen/arch/arm/tee/ffa_notif.c +10 -1
@@ -407,7 +407,16 @@ void ffa_notif_init(void) irq = resp.a2; notif_sri_irq = irq;if ( irq >=NR_GIC_SGI ) -irq_set_type (irq,IRQ_TYPE_EDGE_RISING ); + { + ret =irq_set_type (irq,IRQ_TYPE_EDGE_RISING ); +if ( ret ) + { +printk (XENLOG_ERR +"ffa: irq_set_type irq %u failed: error %d\n" , + irq, ret); +return ; + } + } ret =request_irq (irq,0 , notif_irq_handler,"FF-A notif" ,NULL );if ( ret ) {
xen/arch/arm/time.c +14 -4
@@ -60,20 +60,27 @@ static int __init arch_timer_acpi_init(struct acpi_table_header *header) {u32 irq_type;struct acpi_table_gtdt *gtdt; +int rc; gtdt = container_of(header, struct acpi_table_gtdt, header);/* Initialize all the generic timer IRQ variable from GTDT table */ irq_type =acpi_get_timer_irq_type (gtdt->non_secure_el1_flags); -irq_set_type (gtdt->non_secure_el1_interrupt, irq_type); + rc =irq_set_type (gtdt->non_secure_el1_interrupt, irq_type); +if ( rc ) +return rc; timer_irq[TIMER_PHYS_NONSECURE_PPI ] = gtdt->non_secure_el1_interrupt; irq_type =acpi_get_timer_irq_type (gtdt->virtual_timer_flags); -irq_set_type (gtdt->virtual_timer_interrupt, irq_type); + rc =irq_set_type (gtdt->virtual_timer_interrupt, irq_type); +if ( rc ) +return rc; timer_irq[TIMER_VIRT_PPI ] = gtdt->virtual_timer_interrupt; irq_type =acpi_get_timer_irq_type (gtdt->non_secure_el2_flags); -irq_set_type (gtdt->non_secure_el2_interrupt, irq_type); + rc =irq_set_type (gtdt->non_secure_el2_interrupt, irq_type); +if ( rc ) +return rc; timer_irq[TIMER_HYP_PPI ] = gtdt->non_secure_el2_interrupt;return 0 ;
@@ -81,7 +88,10 @@ static int __init arch_timer_acpi_init(struct acpi_table_header *header)static void __init preinit_acpi_xen_time (void ) { -acpi_table_parse (ACPI_SIG_GTDT , arch_timer_acpi_init); +int rc =acpi_table_parse (ACPI_SIG_GTDT , arch_timer_acpi_init); + +if ( rc ) +panic ("Timer: Failed to configure interrupts from GTDT: %d\n" , rc); }#else static void __init preinit_acpi_xen_time (void ) { }
xen/drivers/char/ns16550.c +6 -2
@@ -1928,6 +1928,7 @@ static int __init ns16550_acpi_uart_init(const void *data)struct acpi_table_header *table;struct acpi_table_spcr *spcr;acpi_status status; +int rc;/* * Same as the DT part. * Only support one UART on ARM which happen to be ns16550_com[0].
@@ -1959,6 +1960,11 @@ static int __init ns16550_acpi_uart_init(const void *data)return -EINVAL ; } +/* The trigger/polarity information is not available in spcr. */ + rc =irq_set_type (spcr->interrupt,IRQ_TYPE_LEVEL_HIGH ); +if ( rc ) +return rc; +ns16550_init_common (uart);/*
@@ -1975,8 +1981,6 @@ static int __init ns16550_acpi_uart_init(const void *data) uart->reg_shift = spcr->serial_port.bit_offset; uart->reg_width = spcr->serial_port.access_width; -/* The trigger/polarity information is not available in spcr. */ -irq_set_type (spcr->interrupt,IRQ_TYPE_LEVEL_HIGH ); uart->irq = spcr->interrupt; uart->vuart.base_addr = uart->io_base;
xen/drivers/char/pl011.c +3 -1
@@ -363,7 +363,9 @@ static int __init pl011_acpi_uart_init(const void *data) spcr->interface_type == ACPI_DBG2_SBSA_32);/* trigger/polarity information is not available in spcr */ -irq_set_type (spcr->interrupt,IRQ_TYPE_LEVEL_HIGH ); + res =irq_set_type (spcr->interrupt,IRQ_TYPE_LEVEL_HIGH ); +if ( res ) +return res;/* TODO - mmio32 proper handling (for now set to true) */ res = pl011_uart_init(spcr->interrupt, spcr->serial_port.address,
Signature · 1 line
2.53.0
Re: [PATCH v4 4/4] xen/arm: handle irq_set_type() failures
Quoted 10 lines · Mykola Kvach
Several Arm firmware initialization paths discard irq_set_type()'s return value, violating MISRA C Rule 17.7. If trigger configuration fails, initialization continues with an IRQ that was not configured as requested.
GTDT and MADT retain rejected timer and maintenance INTIDs.
check_timer_irq_cfg()andrelease_irq()later perform unconditional descriptor lookups on those values. Xen has no backing descriptors for INTIDs 1024 through 4095, so retaining one can cause an out-of-bounds access.
Quoted 8 lines
Check the return value in the GTDT, MADT, SPCR, and FF-A paths. Store timer INTIDs only after successful trigger configuration, make GTDT parsing failure fatal, and stop UART or notification setup when trigger configuration fails.
Signed-off-by Mykola Kvach <[email protected]>Reviewed-by Volodymyr Babchuk <[email protected]>Reviewed-by Michal Orzel <[email protected]>
I asked you on v3 to reorder the patches, so that this one comes first (usually when sending the series, fixes, improvements should come first so that they can be immediately taken).
~Michal
Re: [PATCH v4 4/4] xen/arm: handle irq_set_type() failures
Quoted 18 lines · Michal
Quoted 15 lines · Mykola Kvach
Several Arm firmware initialization paths discard irq_set_type()'s return value, violating MISRA C Rule 17.7. If trigger configuration fails, initialization continues with an IRQ that was not configured as requested.
GTDT and MADT retain rejected timer and maintenance INTIDs.
check_timer_irq_cfg()andrelease_irq()later perform unconditional descriptor lookups on those values. Xen has no backing descriptors for INTIDs 1024 through 4095, so retaining one can cause an out-of-bounds access.Check the return value in the GTDT, MADT, SPCR, and FF-A paths. Store timer INTIDs only after successful trigger configuration, make GTDT parsing failure fatal, and stop UART or notification setup when trigger configuration fails.
Quoted 6 lines
Quoted 3 lines
Signed-off-by Mykola Kvach <[email protected]>Reviewed-by Volodymyr Babchuk <[email protected]>Reviewed-by Michal Orzel <[email protected]>I asked you on v3 to reorder the patches, so that this one comes first (usually when sending the series, fixes, improvements should come first so that they can be immediately taken).
Sure, I'll reorder the series in v5 so that this patch comes first. Thanks for the reminder.
Best regards,
Mykola