irqchip/gic: Fix UBSAN shift-out-of-bounds in GIC helpers
Subsystems (2)
PATCH 1/1 irqchip/gic: Fix UBSAN shift-out-of-bounds in GIC helpers
Hi,
When running a kernel built with UBSAN enabled, enabling a GPIO controller that uses a GIC interrupt as its parent triggers several shift-out-of-bounds warnings in the GIC helpers.
This patch fixes the issue by using unsigned constants so that the left shifts are well-defined and UBSAN stays quiet.
Thanks,
Zhu Ling
---
From: Zhu Ling <[email protected]>
Date: Tue, 2 Dec 2025 17:54:10 +0800
Subject: [PATCH] irqchip/gic: Fix UBSAN shift-out-of-bounds in GIC helpers
When running with UBSAN enabled, enabling a GPIO controller that uses a GIC interrupt as its parent triggers several shift-out-of-bounds warnings:
UBSAN: shift-out-of-bounds in drivers/irqchip/irq-gic-common.c:50:21 left shift of 2 by 30 places cannot be represented in type 'int'
and similar reports in gic_poke_irq() and gic_peek_irq() in drivers/irqchip/irq-gic-v3.c.
These come from shifting signed integer constants. Use unsigned constants (0x2U and 1U) so that the behavior is well-defined and the UBSAN warnings go away.
--- drivers/irqchip/irq-gic-common.c | 2 +- drivers/irqchip/irq-gic-v3.c | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-)
drivers/irqchip/irq-gic-common.c +1 -1
@@ -48,7 +48,7 @@ void gic_enable_quirks(u32 iidr, const struct gic_quirk *quirks,int gic_configure_irq (unsigned int irq,unsigned int type,void __iomem *base) { -u32 confmask =0x2 << ((irq %16 ) *2 ); +u32 confmask =0x2U << ((irq %16 ) *2 );u32 confoff = (irq /16 ) *4 ;u32 val, oldval;int ret =0 ;
drivers/irqchip/irq-gic-v3.c +2 -2
@@ -457,7 +457,7 @@ static int gic_peek_irq(struct irq_data *d, u32 offset)u32 index, mask; offset =convert_offset_index (d, offset, &index); - mask =1 << (index %32 ); + mask =1U << (index %32 );if (gic_irq_in_rdist (d)) base =gic_data_rdist_sgi_base ();
@@ -473,7 +473,7 @@ static void gic_poke_irq(struct irq_data *d, u32 offset)u32 index, mask; offset =convert_offset_index (d, offset, &index); - mask =1 << (index %32 ); + mask =1U << (index %32 );if (gic_irq_in_rdist (d)) base =gic_data_rdist_sgi_base ();
Signature · 1 line
2.17.1
From 6b46e4ce40ddd3f18a73d9c66bebf22a167bd5f3 Mon Sep 17 00:00:00 2001
From: Zhu Ling <[email protected]>
Date: Tue, 2 Dec 2025 17:54:10 +0800
Subject: [PATCH] irqchip/gic: Fix UBSAN shift-out-of-bounds in GIC helpers
When running with UBSAN enabled, enabling a GPIO controller that uses a GIC interrupt as its parent triggers several shift-out-of-bounds warnings:
UBSAN: shift-out-of-bounds in drivers/irqchip/irq-gic-common.c:50:21 left shift of 2 by 30 places cannot be represented in type 'int'
and similar reports in gic_poke_irq() and gic_peek_irq() in drivers/irqchip/irq-gic-v3.c.
These come from shifting signed integer constants. Use unsigned constants (0x2U and 1U) so that the behavior is well-defined and the UBSAN warnings go away.
--- drivers/irqchip/irq-gic-common.c | 2 +- drivers/irqchip/irq-gic-v3.c | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-)
drivers/irqchip/irq-gic-common.c +1 -1
@@ -48,7 +48,7 @@ void gic_enable_quirks(u32 iidr, const struct gic_quirk *quirks,int gic_configure_irq (unsigned int irq,unsigned int type,void __iomem *base) { -u32 confmask =0x2 << ((irq %16 ) *2 ); +u32 confmask =0x2U << ((irq %16 ) *2 );u32 confoff = (irq /16 ) *4 ;u32 val, oldval;int ret =0 ;
drivers/irqchip/irq-gic-v3.c +2 -2
@@ -457,7 +457,7 @@ static int gic_peek_irq(struct irq_data *d, u32 offset)u32 index, mask; offset =convert_offset_index (d, offset, &index); - mask =1 << (index %32 ); + mask =1U << (index %32 );if (gic_irq_in_rdist (d)) base =gic_data_rdist_sgi_base ();
@@ -473,7 +473,7 @@ static void gic_poke_irq(struct irq_data *d, u32 offset)u32 index, mask; offset =convert_offset_index (d, offset, &index); - mask =1 << (index %32 ); + mask =1U << (index %32 );if (gic_irq_in_rdist (d)) base =gic_data_rdist_sgi_base ();
Signature · 1 line
2.17.1
Marc Zyngier
maintainerQuoted 9 lines · On Tue, 02 Dec 2025 10:53:24 +0000, zhuling0805 <[email protected]> wrote:
Hi,
When running a kernel built with UBSAN enabled, enabling a GPIO controller that uses a GIC interrupt as its parent triggers several shift-out-of-bounds warnings in the GIC helpers.
This patch fixes the issue by using unsigned constants so that the left shifts are well-defined and UBSAN stays quiet.
Next time, please put additional notes *after* the commit message. In this case, you are simply repeating what is already in the commit message, so just sending the patch without anything else would have been better.
Quoted 16 lines
Thanks,
Zhu Ling---
From: Zhu Ling <[email protected]>
Date: Tue, 2 Dec 2025 17:54:10 +0800
Subject: [PATCH] irqchip/gic: Fix UBSAN shift-out-of-bounds in GIC helpersWhen running with UBSAN enabled, enabling a GPIO controller that uses a GIC interrupt as its parent triggers several shift-out-of-bounds warnings:
UBSAN: shift-out-of-bounds in drivers/irqchip/irq-gic-common.c:50:21 left shift of 2 by 30 places cannot be represented in type 'int'
Quoted 6 lines
and similar reports in
gic_poke_irq()andgic_peek_irq()indrivers/irqchip/irq-gic-v3.c.These come from shifting signed integer constants. Use unsigned constants (
0x2Uand 1U) so that the behavior is well-defined and the UBSAN warnings go away.
I think a better approach would be to convert all of this to primitives that are designed for bit mask generation, rather than reinventing the wheel. See below for some (untested) suggestions.
Quoted 12 lines
Signed-off-by Zhu Ling <[email protected]>--- drivers/irqchip/irq-gic-common.c | 2 +- drivers/irqchip/irq-gic-v3.c | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-)drivers/irqchip/irq-gic-common.c +0
-0
drivers/irqchip/irq-gic-common.c void gic_enable_quirks(u32 iidr, const struct gic_quirk *quirks, · line 51
Quoted 5 lines
drivers/irqchip/irq-gic-common.c +1
-1@@ -48,7 +48,7 @@ void gic_enable_quirks(u32 iidr, const struct gic_quirk *quirks,int gic_configure_irq (unsigned int irq,unsigned int type,void __iomem *base) { -u32 confmask =0x2 << ((irq %16 ) *2 ); +u32 confmask =0x2U << ((irq %16 ) *2 );
This really should be written as:
u32 confmask = BIT(((irq % 16) * 2) + 1);
drivers/irqchip/irq-gic-v3.c static int gic_peek_irq(struct irq_data *d, u32 offset) · line 460
Quoted 13 lines
drivers/irqchip/irq-gic-common.c +0
-0u32 confoff = (irq /16 ) *4 ;u32 val, oldval;int ret =0 ;drivers/irqchip/irq-gic-v3.c +1
-1@@ -457,7 +457,7 @@ static int gic_peek_irq(struct irq_data *d, u32 offset)u32 index, mask; offset =convert_offset_index (d, offset, &index); - mask =1 << (index %32 ); + mask =1U << (index %32 );
and this as BIT(index % 32).
drivers/irqchip/irq-gic-v3.c static void gic_poke_irq(struct irq_data *d, u32 offset) · line 476
Quoted 9 lines
drivers/irqchip/irq-gic-v3.c +1
-1if (gic_irq_in_rdist (d)) base =gic_data_rdist_sgi_base ();@@ -473,7 +473,7 @@ static void gic_poke_irq(struct irq_data *d, u32 offset)u32 index, mask; offset =convert_offset_index (d, offset, &index); - mask =1 << (index %32 ); + mask =1U << (index %32 );
Same here.
drivers/irqchip/irq-gic-v3.c static void gic_poke_irq(struct irq_data *d, u32 offset) · line 478
Quoted 3 lines
drivers/irqchip/irq-gic-v3.c +0
-0if (gic_irq_in_rdist (d)) base =gic_data_rdist_sgi_base ();
Thanks,
M.
Signature · 1 line
Without deviation from the norm, progress is not possible.
PATCH v2 1/1 irqchip/gic: Fix UBSAN shift-out-of-bounds in GIC helpers
When running with UBSAN enabled, enabling a GPIO controller that uses a GIC interrupt as its parent triggers several shift-out-of-bounds warnings:
shift-out-of-bounds in drivers/irqchip/irq-gic-common.c:50:21 left shift of 2 by 30 places cannot be represented in type 'int'
Similar reports are emitted from gic_poke_irq() and gic_peek_irq() in drivers/irqchip/irq-gic-v3.c. These masks are generated by shifting signed integer constants, which invokes undefined behavior when bit 31 is selected.
Use BIT() to generate the masks with an unsigned type and make their intent explicit.
---
Changes in v2:
- Use BIT() instead of explicit unsigned shifts, as suggested by Marc.
- Drop the redundant introductory text from the email.
- Update the author email address.
--- drivers/irqchip/irq-gic-common.c | 2 +- drivers/irqchip/irq-gic-v3.c | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-)
drivers/irqchip/irq-gic-common.c +1 -1
@@ -48,7 +48,7 @@ void gic_enable_quirks(u32 iidr, const struct gic_quirk *quirks,int gic_configure_irq (unsigned int irq,unsigned int type,void __iomem *base) { -u32 confmask =0x2 << ((irq %16 ) *2 ); +u32 confmask =BIT (((irq %16 ) *2 ) +1 );u32 confoff = (irq /16 ) *4 ;u32 val, oldval;int ret =0 ;
drivers/irqchip/irq-gic-v3.c +2 -2
@@ -457,7 +457,7 @@ static int gic_peek_irq(struct irq_data *d, u32 offset)u32 index, mask; offset =convert_offset_index (d, offset, &index); - mask =1 << (index %32 ); + mask =BIT (index %32 );if (gic_irq_in_rdist (d)) base =gic_data_rdist_sgi_base ();
@@ -473,7 +473,7 @@ static void gic_poke_irq(struct irq_data *d, u32 offset)u32 index, mask; offset =convert_offset_index (d, offset, &index); - mask =1 << (index %32 ); + mask =BIT (index %32 );if (gic_irq_in_rdist (d)) base =gic_data_rdist_sgi_base ();
Signature · 1 line
2.43.0
Quoted 54 lines · Zhu Ling
When running with UBSAN enabled, enabling a GPIO controller that uses a GIC interrupt as its parent triggers several shift-out-of-bounds warnings:
shift-out-of-bounds in
drivers/irqchip/irq-gic-common.c:50:21left shift of 2 by 30 places cannot be represented in type 'int'Similar reports are emitted from
gic_poke_irq()andgic_peek_irq()indrivers/irqchip/irq-gic-v3.c. These masks are generated by shifting signed integer constants, which invokes undefined behavior when bit 31 is selected.Use
BIT()to generate the masks with an unsigned type and make their intent explicit.Signed-off-by Zhu Ling <[email protected]>---
Changes in v2:
- UseBIT()instead of explicit unsigned shifts, as suggested by Marc.
- Drop the redundant introductory text from the email.
- Update the author email address.--- drivers/irqchip/irq-gic-common.c | 2 +- drivers/irqchip/irq-gic-v3.c | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-)drivers/irqchip/irq-gic-common.c +1
-1@@ -48,7 +48,7 @@ void gic_enable_quirks(u32 iidr, const struct gic_quirk *quirks,int gic_configure_irq (unsigned int irq,unsigned int type,void __iomem *base) { -u32 confmask =0x2 << ((irq %16 ) *2 ); +u32 confmask =BIT (((irq %16 ) *2 ) +1 );u32 confoff = (irq /16 ) *4 ;u32 val, oldval;int ret =0 ;drivers/irqchip/irq-gic-v3.c +1
-1@@ -457,7 +457,7 @@ static int gic_peek_irq(struct irq_data *d, u32 offset)u32 index, mask; offset =convert_offset_index (d, offset, &index); - mask =1 << (index %32 ); + mask =BIT (index %32 );if (gic_irq_in_rdist (d)) base =gic_data_rdist_sgi_base ();
drivers/irqchip/irq-gic-v3.c static void gic_poke_irq(struct irq_data *d, u32 offset) · line 479
Quoted 8 lines
drivers/irqchip/irq-gic-v3.c +1
-1@@ -473,7 +473,7 @@ static void gic_poke_irq(struct irq_data *d, u32 offset)u32 index, mask; offset =convert_offset_index (d, offset, &index); - mask =1 << (index %32 ); + mask =BIT (index %32 );if (gic_irq_in_rdist (d)) base =gic_data_rdist_sgi_base ();
gic_{poke,peek}_irq in drivers/irqchip/irq-gic.c have a similar pattern.
|static void gic_poke_irq(struct irq_data *d, u32 offset)
|{
| u32 mask = 1 << (irqd_to_hwirq(d) % 32);Mind fixing them as well?
Thanks,
Zenghui
Quoted 4 lines
gic_{poke,peek}_irq in
drivers/irqchip/irq-gic.chave a similar pattern.Mind fixing them as well?
Yes, thanks for pointing this out. I have fixed both helpers and will send v3 shortly.
Thanks,
Zhu Ling
信息安全声明:本邮件包含信息归发件人所在组织所有,发件人所在组织对该邮件拥有所有权利。请接收者注意保密,未经发件人书面许可,不得向任何第三方组织和个人透露本邮件所含信息。
Information Security Notice: The information contained in this mail is solely property of the sender's organization.This mail communication is confidential.Recipients named above are obligated to maintain secrecy and are not permitted to disclose the contents of this communication to others.
PATCH v3 1/1 irqchip/gic: Fix UBSAN shift-out-of-bounds in GIC helpers
In reply to Zenghui YuWhen running with UBSAN enabled, enabling a GPIO controller that uses a GIC interrupt as its parent triggers several shift-out-of-bounds warnings:
shift-out-of-bounds in drivers/irqchip/irq-gic-common.c:50:21 left shift of 2 by 30 places cannot be represented in type 'int'
Similar reports are emitted from gic_poke_irq() and gic_peek_irq() in drivers/irqchip/irq-gic-v3.c. The corresponding GICv2 helpers use the same signed-shift pattern. These masks are generated by shifting signed integer constants, which invokes undefined behavior when bit 31 is selected.
Use BIT() to generate the masks with an unsigned type and make their intent explicit.
---
Changes in v3:
- Fix the same issue in the GICv2 gic_poke_irq() and gic_peek_irq() helpers, as pointed out by Zenghui.
Changes in v2:
- Use BIT() instead of explicit unsigned shifts, as suggested by Marc.
- Drop the redundant introductory text from the email.
- Update the author email address.
--- drivers/irqchip/irq-gic-common.c | 2 +- drivers/irqchip/irq-gic-v3.c | 4 ++-- drivers/irqchip/irq-gic.c | 4 ++-- 3 files changed, 5 insertions(+), 5 deletions(-)
drivers/irqchip/irq-gic-common.c +1 -1
@@ -48,7 +48,7 @@ void gic_enable_quirks(u32 iidr, const struct gic_quirk *quirks,int gic_configure_irq (unsigned int irq,unsigned int type,void __iomem *base) { -u32 confmask =0x2 << ((irq %16 ) *2 ); +u32 confmask =BIT (((irq %16 ) *2 ) +1 );u32 confoff = (irq /16 ) *4 ;u32 val, oldval;int ret =0 ;
drivers/irqchip/irq-gic-v3.c +2 -2
@@ -457,7 +457,7 @@ static int gic_peek_irq(struct irq_data *d, u32 offset)u32 index, mask; offset =convert_offset_index (d, offset, &index); - mask =1 << (index %32 ); + mask =BIT (index %32 );if (gic_irq_in_rdist (d)) base =gic_data_rdist_sgi_base ();
@@ -473,7 +473,7 @@ static void gic_poke_irq(struct irq_data *d, u32 offset)u32 index, mask; offset =convert_offset_index (d, offset, &index); - mask =1 << (index %32 ); + mask =BIT (index %32 );if (gic_irq_in_rdist (d)) base =gic_data_rdist_sgi_base ();
drivers/irqchip/irq-gic.c +2 -2
@@ -178,14 +178,14 @@ static inline bool cascading_gic_irq(struct irq_data *d)*/ static void gic_poke_irq (struct irq_data *d,u32 offset) { -u32 mask =1 << (irqd_to_hwirq (d) %32 ); +u32 mask =BIT (irqd_to_hwirq (d) %32 );writel_relaxed (mask,gic_dist_base (d) + offset + (irqd_to_hwirq (d) /32 ) *4 ); }static int gic_peek_irq (struct irq_data *d,u32 offset) { -u32 mask =1 << (irqd_to_hwirq (d) %32 ); +u32 mask =BIT (irqd_to_hwirq (d) %32 );return !!(readl_relaxed (gic_dist_base (d) + offset + (irqd_to_hwirq (d) /32 ) *4 ) & mask); }
Signature · 1 line
2.43.0
Quoted 26 lines · Zhu Ling
When running with UBSAN enabled, enabling a GPIO controller that uses a GIC interrupt as its parent triggers several shift-out-of-bounds warnings:
shift-out-of-bounds in
drivers/irqchip/irq-gic-common.c:50:21left shift of 2 by 30 places cannot be represented in type 'int'Similar reports are emitted from
gic_poke_irq()andgic_peek_irq()indrivers/irqchip/irq-gic-v3.c. The corresponding GICv2 helpers use the same signed-shift pattern. These masks are generated by shifting signed integer constants, which invokes undefined behavior when bit 31 is selected.Use
BIT()to generate the masks with an unsigned type and make their intent explicit.Signed-off-by Zhu Ling <[email protected]>---
Changes in v3:
- Fix the same issue in the GICv2gic_poke_irq()andgic_peek_irq()helpers, as pointed out by Zenghui.Changes in v2:
- UseBIT()instead of explicit unsigned shifts, as suggested by Marc.
- Drop the redundant introductory text from the email.
- Update the author email address.
Quoted 6 lines
--- drivers/irqchip/irq-gic-common.c | 2 +- drivers/irqchip/irq-gic-v3.c | 4 ++-- drivers/irqchip/irq-gic.c | 4 ++-- 3 files changed, 5 insertions(+), 5 deletions(-)
Looks good,
Thanks,
Zenghui
Quoted 13 lines · Zhu Ling
When running with UBSAN enabled, enabling a GPIO controller that uses a GIC interrupt as its parent triggers several shift-out-of-bounds warnings:
shift-out-of-bounds in
drivers/irqchip/irq-gic-common.c:50:21left shift of 2 by 30 places cannot be represented in type 'int'Similar reports are emitted from
gic_poke_irq()andgic_peek_irq()indrivers/irqchip/irq-gic-v3.c. The corresponding GICv2 helpers use the same signed-shift pattern. These masks are generated by shifting signed integer constants, which invokes undefined behavior when bit 31 is selected.
Quoted 4 lines
Use
BIT()to generate the masks with an unsigned type and make their intent explicit.Signed-off-by Zhu Ling <[email protected]>
M.
Signature · 1 line
Without deviation from the norm, progress is not possible.
Radu Rendec
reviewerQuoted 12 lines · Zhu Ling
When running with UBSAN enabled, enabling a GPIO controller that uses a GIC interrupt as its parent triggers several shift-out-of-bounds warnings:
shift-out-of-bounds in
drivers/irqchip/irq-gic-common.c:50:21left shift of 2 by 30 places cannot be represented in type 'int'Similar reports are emitted from
gic_poke_irq()andgic_peek_irq()indrivers/irqchip/irq-gic-v3.c. The corresponding GICv2 helpers use the same signed-shift pattern. These masks are generated by shifting signed integer constants, which invokes undefined behavior when bit 31 is selected.
Quoted 4 lines
Use
BIT()to generate the masks with an unsigned type and make their intent explicit.Signed-off-by Zhu Ling <[email protected]>
The patch itself looks good to me, but I believe it deserves some Fixes tags. It's not explicitly mentioned in the documentation, so I spent a bit of time trying to understand if fixing a UBSAN warning is considered a bug fix. The answer seems to be yes.
Quoted 73 lines
---
Changes in v3:
- Fix the same issue in the GICv2gic_poke_irq()andgic_peek_irq()helpers, as pointed out by Zenghui.Changes in v2:
- UseBIT()instead of explicit unsigned shifts, as suggested by Marc.
- Drop the redundant introductory text from the email.
- Update the author email address.---
drivers/irqchip/irq-gic-common.c | 2 +-
drivers/irqchip/irq-gic-v3.c | 4 ++--
drivers/irqchip/irq-gic.c | 4 ++--3 files changed, 5 insertions(+), 5 deletions(-)drivers/irqchip/irq-gic-common.c +1
-1@@ -48,7 +48,7 @@ void gic_enable_quirks(u32 iidr, const struct gic_quirk *quirks, int gic_configure_irq(unsigned int irq, unsigned int type, void __iomem *base) { -u32 confmask =0x2 << ((irq %16 ) *2 ); +u32 confmask =BIT (((irq %16 ) *2 ) +1 ); u32 confoff = (irq / 16) * 4; u32 val, oldval; int ret = 0;drivers/irqchip/irq-gic-v3.c +0
-0@@ -457,7 +457,7 @@ static int gic_peek_irq(struct irq_data *d, u32 offset) u32 index, mask;
offset =convert_offset_index(d, offset, &index);drivers/irqchip/irq-gic-v3.c +1
-1- mask =1 << (index %32 ); + mask =BIT (index %32 );
if (gic_irq_in_rdist(d))
base =gic_data_rdist_sgi_base();drivers/irqchip/irq-gic-v3.c +0
-0@@ -473,7 +473,7 @@ static void gic_poke_irq(struct irq_data *d, u32 offset) u32 index, mask;
offset =convert_offset_index(d, offset, &index);drivers/irqchip/irq-gic-v3.c +1
-1- mask =1 << (index %32 ); + mask =BIT (index %32 );
if (gic_irq_in_rdist(d))
base =gic_data_rdist_sgi_base();drivers/irqchip/irq-gic.c +1
-1@@ -178,14 +178,14 @@ static inline bool cascading_gic_irq(struct irq_data *d)*/ static void gic_poke_irq(struct irq_data *d, u32 offset) { -u32 mask =1 << (irqd_to_hwirq (d) %32 ); +u32 mask =BIT (irqd_to_hwirq (d) %32 );
writel_relaxed(mask, gic_dist_base(d) + offset + (irqd_to_hwirq(d) / 32) * 4); }
static intgic_peek_irq(struct irq_data *d, u32 offset)
{drivers/irqchip/irq-gic.c +1
-1-u32 mask =1 << (irqd_to_hwirq (d) %32 ); +u32 mask =BIT (irqd_to_hwirq (d) %32 );
return !!(readl_relaxed(gic_dist_base(d) + offset + (irqd_to_hwirq(d)/ 32) * 4) & mask); }
PATCH v4 1/1 irqchip/gic: Fix UBSAN shift-out-of-bounds in GIC helpers
In reply to Zhu LingWhen running with UBSAN enabled, enabling a GPIO controller that uses a GIC interrupt as its parent triggers several shift-out-of-bounds warnings:
shift-out-of-bounds in drivers/irqchip/irq-gic-common.c:50:21 left shift of 2 by 30 places cannot be represented in type 'int'
Similar reports are emitted from gic_poke_irq() and gic_peek_irq() in drivers/irqchip/irq-gic-v3.c. The corresponding GICv2 helpers use the same signed-shift pattern. These masks are generated by shifting signed integer constants, which invokes undefined behavior when bit 31 is selected.
Use BIT() to generate the masks with an unsigned type and make their intent explicit.
---
Changes in v4:
- Add Fixes tags for the commits that introduced the affected masks.
Changes in v3:
- Fix the same issue in the GICv2 gic_poke_irq() and gic_peek_irq() helpers, as pointed out by Zenghui.
Changes in v2:
- Use BIT() instead of explicit unsigned shifts, as suggested by Marc.
- Drop the redundant introductory text from the email.
- Update the author email address.
--- drivers/irqchip/irq-gic-common.c | 2 +- drivers/irqchip/irq-gic-v3.c | 4 ++-- drivers/irqchip/irq-gic.c | 4 ++-- 3 files changed, 5 insertions(+), 5 deletions(-)
drivers/irqchip/irq-gic-common.c +1 -1
@@ -48,7 +48,7 @@ void gic_enable_quirks(u32 iidr, const struct gic_quirk *quirks,int gic_configure_irq (unsigned int irq,unsigned int type,void __iomem *base) { -u32 confmask =0x2 << ((irq %16 ) *2 ); +u32 confmask =BIT (((irq %16 ) *2 ) +1 );u32 confoff = (irq /16 ) *4 ;u32 val, oldval;int ret =0 ;
drivers/irqchip/irq-gic-v3.c +2 -2
@@ -457,7 +457,7 @@ static int gic_peek_irq(struct irq_data *d, u32 offset)u32 index, mask; offset =convert_offset_index (d, offset, &index); - mask =1 << (index %32 ); + mask =BIT (index %32 );if (gic_irq_in_rdist (d)) base =gic_data_rdist_sgi_base ();
@@ -473,7 +473,7 @@ static void gic_poke_irq(struct irq_data *d, u32 offset)u32 index, mask; offset =convert_offset_index (d, offset, &index); - mask =1 << (index %32 ); + mask =BIT (index %32 );if (gic_irq_in_rdist (d)) base =gic_data_rdist_sgi_base ();
drivers/irqchip/irq-gic.c +2 -2
@@ -178,14 +178,14 @@ static inline bool cascading_gic_irq(struct irq_data *d)*/ static void gic_poke_irq (struct irq_data *d,u32 offset) { -u32 mask =1 << (irqd_to_hwirq (d) %32 ); +u32 mask =BIT (irqd_to_hwirq (d) %32 );writel_relaxed (mask,gic_dist_base (d) + offset + (irqd_to_hwirq (d) /32 ) *4 ); }static int gic_peek_irq (struct irq_data *d,u32 offset) { -u32 mask =1 << (irqd_to_hwirq (d) %32 ); +u32 mask =BIT (irqd_to_hwirq (d) %32 );return !!(readl_relaxed (gic_dist_base (d) + offset + (irqd_to_hwirq (d) /32 ) *4 ) & mask); }
Signature · 1 line
2.43.0
Radu Rendec
reviewerQuoted 12 lines · Zhu Ling
When running with UBSAN enabled, enabling a GPIO controller that uses a GIC interrupt as its parent triggers several shift-out-of-bounds warnings:
shift-out-of-bounds in
drivers/irqchip/irq-gic-common.c:50:21left shift of 2 by 30 places cannot be represented in type 'int'Similar reports are emitted from
gic_poke_irq()andgic_peek_irq()indrivers/irqchip/irq-gic-v3.c. The corresponding GICv2 helpers use the same signed-shift pattern. These masks are generated by shifting signed integer constants, which invokes undefined behavior when bit 31 is selected.
Quoted 5 lines
Use
BIT()to generate the masks with an unsigned type and make their intent explicit.Fixes 5c0c1f08abf0 ("ARM: 6150/1: gic: implement set_type")Fixes 567178077c36 ("irqchip: GIC: Add support for irq_[get, set]_irqchip_state()")
Thanks for adding these and sorry for being pedantic. The above commit just moved around the offending lines, and so did the previous one that touched them. I traced them back to f27ecacc54cc ("[ARM] Add support for ARM GIC") - the commit that added the GIC driver *21 years* ago.
Quoted 1 line
Fixes b594c6e20c7f ("irqchip: GICv3: Add support for irq_[get, set]_irqchip_state()")
Same here. That commit just moved around gic_peek_irq(). The offending lines trace back to 021f653791ad ("irqchip: gic-v3: Initial support for GICv3") - the commit that added the GICv3 driver *12 years* ago.
These UB issues have been there for quite a while :) FWIW, I did some research, and gcc actually generates correct machine code for these shifts, even though they are UB from the C standard's perspective. I guess that explains why they have gone unnoticed for so long; people started to use clang only much more recently to compile the kernel.
Quoted 77 lines
Signed-off-by Zhu Ling <[email protected]>---
Changes in v4:
- Add Fixes tags for the commits that introduced the affected masks.Changes in v3:
- Fix the same issue in the GICv2gic_poke_irq()andgic_peek_irq()helpers, as pointed out by Zenghui.Changes in v2:
- UseBIT()instead of explicit unsigned shifts, as suggested by Marc.
- Drop the redundant introductory text from the email.
- Update the author email address.---
drivers/irqchip/irq-gic-common.c | 2 +-
drivers/irqchip/irq-gic-v3.c | 4 ++--
drivers/irqchip/irq-gic.c | 4 ++--3 files changed, 5 insertions(+), 5 deletions(-)drivers/irqchip/irq-gic-common.c +1
-1@@ -48,7 +48,7 @@ void gic_enable_quirks(u32 iidr, const struct gic_quirk *quirks, int gic_configure_irq(unsigned int irq, unsigned int type, void __iomem *base) { -u32 confmask =0x2 << ((irq %16 ) *2 ); +u32 confmask =BIT (((irq %16 ) *2 ) +1 ); u32 confoff = (irq / 16) * 4; u32 val, oldval; int ret = 0;drivers/irqchip/irq-gic-v3.c +0
-0@@ -457,7 +457,7 @@ static int gic_peek_irq(struct irq_data *d, u32 offset) u32 index, mask;
offset =convert_offset_index(d, offset, &index);drivers/irqchip/irq-gic-v3.c +1
-1- mask =1 << (index %32 ); + mask =BIT (index %32 );
if (gic_irq_in_rdist(d))
base =gic_data_rdist_sgi_base();drivers/irqchip/irq-gic-v3.c +0
-0@@ -473,7 +473,7 @@ static void gic_poke_irq(struct irq_data *d, u32 offset) u32 index, mask;
offset =convert_offset_index(d, offset, &index);drivers/irqchip/irq-gic-v3.c +1
-1- mask =1 << (index %32 ); + mask =BIT (index %32 );
if (gic_irq_in_rdist(d))
base =gic_data_rdist_sgi_base();drivers/irqchip/irq-gic.c +1
-1@@ -178,14 +178,14 @@ static inline bool cascading_gic_irq(struct irq_data *d)*/ static void gic_poke_irq(struct irq_data *d, u32 offset) { -u32 mask =1 << (irqd_to_hwirq (d) %32 ); +u32 mask =BIT (irqd_to_hwirq (d) %32 );
writel_relaxed(mask, gic_dist_base(d) + offset + (irqd_to_hwirq(d) / 32) * 4); }
static intgic_peek_irq(struct irq_data *d, u32 offset)
{drivers/irqchip/irq-gic.c +1
-1-u32 mask =1 << (irqd_to_hwirq (d) %32 ); +u32 mask =BIT (irqd_to_hwirq (d) %32 );
return !!(readl_relaxed(gic_dist_base(d) + offset + (irqd_to_hwirq(d)/ 32) * 4) & mask); }
PATCH v5 1/1 irqchip/gic: Fix UBSAN shift-out-of-bounds in GIC helpers
In reply to Zhu LingWhen running with UBSAN enabled, enabling a GPIO controller that uses a GIC interrupt as its parent triggers several shift-out-of-bounds warnings:
shift-out-of-bounds in drivers/irqchip/irq-gic-common.c:50:21 left shift of 2 by 30 places cannot be represented in type 'int'
Similar reports are emitted from gic_poke_irq() and gic_peek_irq() in drivers/irqchip/irq-gic-v3.c. The corresponding GICv2 helpers use the same signed-shift pattern. These masks are generated by shifting signed integer constants, which invokes undefined behavior when bit 31 is selected.
Use BIT() to generate the masks with an unsigned type and make their intent explicit.
---
Changes in v5:
- Correct the GICv2 and GICv3 helper Fixes tags to point to the commits that originally introduced the signed shifts.
- Add Zenghui's Reviewed-by and Marc's Acked-by tags.
Changes in v4:
- Add Fixes tags for the commits that introduced the affected masks.
Changes in v3:
- Fix the same issue in the GICv2 gic_poke_irq() and gic_peek_irq() helpers, as pointed out by Zenghui.
Changes in v2:
- Use BIT() instead of explicit unsigned shifts, as suggested by Marc.
- Drop the redundant introductory text from the email.
- Update the author email address.
--- drivers/irqchip/irq-gic-common.c | 2 +- drivers/irqchip/irq-gic-v3.c | 4 ++-- drivers/irqchip/irq-gic.c | 4 ++-- 3 files changed, 5 insertions(+), 5 deletions(-)
drivers/irqchip/irq-gic-common.c +1 -1
@@ -48,7 +48,7 @@ void gic_enable_quirks(u32 iidr, const struct gic_quirk *quirks,int gic_configure_irq (unsigned int irq,unsigned int type,void __iomem *base) { -u32 confmask =0x2 << ((irq %16 ) *2 ); +u32 confmask =BIT (((irq %16 ) *2 ) +1 );u32 confoff = (irq /16 ) *4 ;u32 val, oldval;int ret =0 ;
drivers/irqchip/irq-gic-v3.c +2 -2
@@ -457,7 +457,7 @@ static int gic_peek_irq(struct irq_data *d, u32 offset)u32 index, mask; offset =convert_offset_index (d, offset, &index); - mask =1 << (index %32 ); + mask =BIT (index %32 );if (gic_irq_in_rdist (d)) base =gic_data_rdist_sgi_base ();
@@ -473,7 +473,7 @@ static void gic_poke_irq(struct irq_data *d, u32 offset)u32 index, mask; offset =convert_offset_index (d, offset, &index); - mask =1 << (index %32 ); + mask =BIT (index %32 );if (gic_irq_in_rdist (d)) base =gic_data_rdist_sgi_base ();
drivers/irqchip/irq-gic.c +2 -2
@@ -178,14 +178,14 @@ static inline bool cascading_gic_irq(struct irq_data *d)*/ static void gic_poke_irq (struct irq_data *d,u32 offset) { -u32 mask =1 << (irqd_to_hwirq (d) %32 ); +u32 mask =BIT (irqd_to_hwirq (d) %32 );writel_relaxed (mask,gic_dist_base (d) + offset + (irqd_to_hwirq (d) /32 ) *4 ); }static int gic_peek_irq (struct irq_data *d,u32 offset) { -u32 mask =1 << (irqd_to_hwirq (d) %32 ); +u32 mask =BIT (irqd_to_hwirq (d) %32 );return !!(readl_relaxed (gic_dist_base (d) + offset + (irqd_to_hwirq (d) /32 ) *4 ) & mask); }
Signature · 1 line
2.43.0
Quoted 15 lines · Zhu Ling
When running with UBSAN enabled, enabling a GPIO controller that uses a GIC interrupt as its parent triggers several shift-out-of-bounds warnings:
shift-out-of-bounds in
drivers/irqchip/irq-gic-common.c:50:21left shift of 2 by 30 places cannot be represented in type 'int'Similar reports are emitted from
gic_poke_irq()andgic_peek_irq()indrivers/irqchip/irq-gic-v3.c. The corresponding GICv2 helpers use the same signed-shift pattern. These masks are generated by shifting signed integer constants, which invokes undefined behavior when bit 31 is selected.Use
BIT()to generate the masks with an unsigned type and make their intent explicit.
Quoted 6 lines
Fixes f27ecacc54cc ("[ARM] Add support for ARM GIC")Fixes 5c0c1f08abf0 ("ARM: 6150/1: gic: implement set_type")Fixes 021f653791ad ("irqchip: gic-v3: Initial support for GICv3")Reviewed-by Zenghui Yu (Huawei) <[email protected]>Acked-by Marc Zyngier <[email protected]>Signed-off-by Zhu Ling <[email protected]>
Quoted 4 lines
---
Changes in v5:
- Correct the GICv2 and GICv3 helper Fixes tags to point to the commits that originally introduced the signed shifts.
Thanks!
Quoted 77 lines
- Add Zenghui's Reviewed-by and Marc's Acked-by tags.
Changes in v4:
- Add Fixes tags for the commits that introduced the affected masks.Changes in v3:
- Fix the same issue in the GICv2gic_poke_irq()andgic_peek_irq()helpers, as pointed out by Zenghui.Changes in v2:
- UseBIT()instead of explicit unsigned shifts, as suggested by Marc.
- Drop the redundant introductory text from the email.
- Update the author email address.---
drivers/irqchip/irq-gic-common.c | 2 +-
drivers/irqchip/irq-gic-v3.c | 4 ++--
drivers/irqchip/irq-gic.c | 4 ++--3 files changed, 5 insertions(+), 5 deletions(-)drivers/irqchip/irq-gic-common.c +1
-1@@ -48,7 +48,7 @@ void gic_enable_quirks(u32 iidr, const struct gic_quirk *quirks, int gic_configure_irq(unsigned int irq, unsigned int type, void __iomem *base) { -u32 confmask =0x2 << ((irq %16 ) *2 ); +u32 confmask =BIT (((irq %16 ) *2 ) +1 ); u32 confoff = (irq / 16) * 4; u32 val, oldval; int ret = 0;drivers/irqchip/irq-gic-v3.c +0
-0@@ -457,7 +457,7 @@ static int gic_peek_irq(struct irq_data *d, u32 offset) u32 index, mask;
offset =convert_offset_index(d, offset, &index);drivers/irqchip/irq-gic-v3.c +1
-1- mask =1 << (index %32 ); + mask =BIT (index %32 );
if (gic_irq_in_rdist(d))
base =gic_data_rdist_sgi_base();drivers/irqchip/irq-gic-v3.c +0
-0@@ -473,7 +473,7 @@ static void gic_poke_irq(struct irq_data *d, u32 offset) u32 index, mask;
offset =convert_offset_index(d, offset, &index);drivers/irqchip/irq-gic-v3.c +1
-1- mask =1 << (index %32 ); + mask =BIT (index %32 );
if (gic_irq_in_rdist(d))
base =gic_data_rdist_sgi_base();drivers/irqchip/irq-gic.c +1
-1@@ -178,14 +178,14 @@ static inline bool cascading_gic_irq(struct irq_data *d)*/ static void gic_poke_irq(struct irq_data *d, u32 offset) { -u32 mask =1 << (irqd_to_hwirq (d) %32 ); +u32 mask =BIT (irqd_to_hwirq (d) %32 );
writel_relaxed(mask, gic_dist_base(d) + offset + (irqd_to_hwirq(d) / 32) * 4); }
static intgic_peek_irq(struct irq_data *d, u32 offset)
{drivers/irqchip/irq-gic.c +1
-1-u32 mask =1 << (irqd_to_hwirq (d) %32 ); +u32 mask =BIT (irqd_to_hwirq (d) %32 );
return !!(readl_relaxed(gic_dist_base(d) + offset + (irqd_to_hwirq(d)/ 32) * 4) & mask); }
The following commit has been merged into the irq/drivers branch of tip:
The following commit has been merged into the irq/drivers branch of tip:
Commit-ID: 9ed1a2c62bf21abe8cb7607d7f4fd615d5c6b53c Gitweb: https://git.kernel.org/tip/9ed1a2c62bf21abe8cb7607d7f4fd615d5c6b53c Author: Zhu Ling <[email protected]> AuthorDate: Tue, 29 Sep 2026 11:10:50 +08:00 Committer: Thomas Gleixner <[email protected]> CommitterDate: Thu, 15 Oct 2026 11:50:48 +02:00
irqchip/gic: Fix UBSAN shift-out-of-bounds in GIC helpers
When running with UBSAN enabled, enabling a GPIO controller that uses a GIC interrupt as its parent triggers several shift-out-of-bounds warnings:
shift-out-of-bounds in drivers/irqchip/irq-gic-common.c:50:21 left shift of 2 by 30 places cannot be represented in type 'int'
Similar reports are emitted from gic_poke_irq() and gic_peek_irq() in drivers/irqchip/irq-gic-v3.c. The corresponding GICv2 helpers use the same signed-shift pattern. These masks are generated by shifting signed integer constants, which invokes undefined behavior when bit 31 is selected.
Use BIT() to generate the masks with an unsigned type and make their intent explicit.
--- drivers/irqchip/irq-gic-common.c | 2 +- drivers/irqchip/irq-gic-v3.c | 4 ++-- drivers/irqchip/irq-gic.c | 4 ++-- 3 files changed, 5 insertions(+), 5 deletions(-)
drivers/irqchip/irq-gic-common.c +1 -1
@@ -48,7 +48,7 @@ void gic_enable_quirks(u32 iidr, const struct gic_quirk *quirks,int gic_configure_irq (unsigned int irq,unsigned int type,void __iomem *base) { -u32 confmask =0x2 << ((irq %16 ) *2 ); +u32 confmask =BIT (((irq %16 ) *2 ) +1 );u32 confoff = (irq /16 ) *4 ;u32 val, oldval;int ret =0 ;
drivers/irqchip/irq-gic-v3.c +2 -2
@@ -457,7 +457,7 @@ static int gic_peek_irq(struct irq_data *d, u32 offset)u32 index, mask; offset =convert_offset_index (d, offset, &index); - mask =1 << (index %32 ); + mask =BIT (index %32 );if (gic_irq_in_rdist (d)) base =gic_data_rdist_sgi_base ();
@@ -473,7 +473,7 @@ static void gic_poke_irq(struct irq_data *d, u32 offset)u32 index, mask; offset =convert_offset_index (d, offset, &index); - mask =1 << (index %32 ); + mask =BIT (index %32 );if (gic_irq_in_rdist (d)) base =gic_data_rdist_sgi_base ();
drivers/irqchip/irq-gic.c +2 -2
@@ -178,14 +178,14 @@ static inline bool cascading_gic_irq(struct irq_data *d)*/ static void gic_poke_irq (struct irq_data *d,u32 offset) { -u32 mask =1 << (irqd_to_hwirq (d) %32 ); +u32 mask =BIT (irqd_to_hwirq (d) %32 );writel_relaxed (mask,gic_dist_base (d) + offset + (irqd_to_hwirq (d) /32 ) *4 ); }static int gic_peek_irq (struct irq_data *d,u32 offset) { -u32 mask =1 << (irqd_to_hwirq (d) %32 ); +u32 mask =BIT (irqd_to_hwirq (d) %32 );return !!(readl_relaxed (gic_dist_base (d) + offset + (irqd_to_hwirq (d) /32 ) *4 ) & mask); }
The following commit has been merged into the irq/drivers branch of tip:
The following commit has been merged into the irq/drivers branch of tip:
Commit-ID: 13ce14ca41a35f5ee90b77ffa57da91ce6048a2d Gitweb: https://git.kernel.org/tip/13ce14ca41a35f5ee90b77ffa57da91ce6048a2d Author: Zhu Ling <[email protected]> AuthorDate: Tue, 29 Sep 2026 11:10:50 +08:00 Committer: Thomas Gleixner <[email protected]> CommitterDate: Thu, 15 Oct 2026 11:55:41 +02:00
irqchip/gic: Fix UBSAN shift-out-of-bounds in GIC helpers
When running with UBSAN enabled, enabling a GPIO controller that uses a GIC interrupt as its parent triggers several shift-out-of-bounds warnings:
shift-out-of-bounds in drivers/irqchip/irq-gic-common.c:50:21 left shift of 2 by 30 places cannot be represented in type 'int'
Similar reports are emitted from gic_poke_irq() and gic_peek_irq() in drivers/irqchip/irq-gic-v3.c. The corresponding GICv2 helpers use the same signed-shift pattern. These masks are generated by shifting signed integer constants, which invokes undefined behavior when bit 31 is selected.
Use BIT() to generate the masks with an unsigned type and make their intent explicit.
--- drivers/irqchip/irq-gic-common.c | 2 +- drivers/irqchip/irq-gic-v3.c | 4 ++-- drivers/irqchip/irq-gic.c | 4 ++-- 3 files changed, 5 insertions(+), 5 deletions(-)
drivers/irqchip/irq-gic-common.c +1 -1
@@ -48,7 +48,7 @@ void gic_enable_quirks(u32 iidr, const struct gic_quirk *quirks,int gic_configure_irq (unsigned int irq,unsigned int type,void __iomem *base) { -u32 confmask =0x2 << ((irq %16 ) *2 ); +u32 confmask =BIT (((irq %16 ) *2 ) +1 );u32 confoff = (irq /16 ) *4 ;u32 val, oldval;int ret =0 ;
drivers/irqchip/irq-gic-v3.c +2 -2
@@ -457,7 +457,7 @@ static int gic_peek_irq(struct irq_data *d, u32 offset)u32 index, mask; offset =convert_offset_index (d, offset, &index); - mask =1 << (index %32 ); + mask =BIT (index %32 );if (gic_irq_in_rdist (d)) base =gic_data_rdist_sgi_base ();
@@ -473,7 +473,7 @@ static void gic_poke_irq(struct irq_data *d, u32 offset)u32 index, mask; offset =convert_offset_index (d, offset, &index); - mask =1 << (index %32 ); + mask =BIT (index %32 );if (gic_irq_in_rdist (d)) base =gic_data_rdist_sgi_base ();
drivers/irqchip/irq-gic.c +2 -2
@@ -178,14 +178,14 @@ static inline bool cascading_gic_irq(struct irq_data *d)*/ static void gic_poke_irq (struct irq_data *d,u32 offset) { -u32 mask =1 << (irqd_to_hwirq (d) %32 ); +u32 mask =BIT (irqd_to_hwirq (d) %32 );writel_relaxed (mask,gic_dist_base (d) + offset + (irqd_to_hwirq (d) /32 ) *4 ); }static int gic_peek_irq (struct irq_data *d,u32 offset) { -u32 mask =1 << (irqd_to_hwirq (d) %32 ); +u32 mask =BIT (irqd_to_hwirq (d) %32 );return !!(readl_relaxed (gic_dist_base (d) + offset + (irqd_to_hwirq (d) /32 ) *4 ) & mask); }