Linux-ARM-Kernel Archive on lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] irqchip/gic-v5: Replace the dead linear IST ID cap with a build-time check
@ 2026-12-04  9:20 Zenghui Yu
  2026-12-04 11:44 ` Lorenzo Pieralisi
  0 siblings, 1 reply; 2+ messages in thread
From: Zenghui Yu @ 2026-12-04  9:20 UTC (permalink / raw)
  To: linux-arm-kernel, linux-kernel
  Cc: maz, lpieralisi, tglx, radu, wangzhigang17, Zenghui Yu

gicv5_irs_init_ist_linear() has a cap intended to keep the linear IST
within the kmalloc() limit. It is broken in two ways:

(a) It adds @istsz instead of subtracting it when computing the ID bits;

(b) and it only updates its local lpi_id_bits, so the caller keeps using
    the original, uncapped value when publishing the LPI ID space
    (gicv5_init_lpis()).

The branch is dead code, though. With the current LPI_ID_BITS_LINEAR (12),
a linear IST is used with at most 14 LPI ID bits. On 1-level-only HW,
lpi_id_bits is at most max(LPI_ID_BITS_LINEAR, MIN_LPI_ID_BITS), and
MIN_LPI_ID_BITS is at most 14 as per the specification. On 2-level-capable
HW, a linear table is used only when lpi_id_bits fits in
((10-ISTSZ) + (2*L2SZ)), which is at most 14 as well. A table with 14 LPI
ID bits and 16-byte ISTEs is 256KB, while KMALLOC_MAX_SIZE is 4MB on arm64
with 4K pages and larger with 16K/64K pages.

Rather than fixing the dead code, remove the cap and add a build-time
assertion: the maximum possible linear table, derived from
GICV5_IRS_LINEAR_MAX_ID_BITS and the largest ISTE, must fit in
KMALLOC_MAX_SIZE. This catches a future increase of LPI_ID_BITS_LINEAR or a
page/MAX_ORDER configuration that would make the table too large.

Signed-off-by: Zenghui Yu <yuzenghui@huawei.com>
---
 drivers/irqchip/irq-gic-v5-irs.c   | 46 +++++++++++++++++-------------
 include/linux/irqchip/arm-gic-v5.h |  2 ++
 2 files changed, 28 insertions(+), 20 deletions(-)

diff --git a/drivers/irqchip/irq-gic-v5-irs.c b/drivers/irqchip/irq-gic-v5-irs.c
index 9d60f474aa4f..79176a9a7c2f 100644
--- a/drivers/irqchip/irq-gic-v5-irs.c
+++ b/drivers/irqchip/irq-gic-v5-irs.c
@@ -7,7 +7,6 @@
 
 #include <linux/acpi.h>
 #include <linux/kmemleak.h>
-#include <linux/log2.h>
 #include <linux/of.h>
 #include <linux/of_address.h>
 
@@ -16,11 +15,32 @@
 
 /*
  * Hardcoded ID_BITS limit for systems supporting only a 1-level IST
- * table. Systems supporting only a 1-level IST table aren't expected
- * to require more than 2^12 LPIs. Tweak as required.
+ * table. Such systems aren't expected to require more than 2^12 LPIs.
+ * Tweak as required; raising it above 14 also raises the size bound
+ * checked below.
  */
 #define LPI_ID_BITS_LINEAR		12
 
+/*
+ * Upper bound of LPI ID bits used by a linear IST.
+ *
+ * On hardware that only supports a 1-level IST, gicv5_irs_init_ist()
+ * asks for at most max(LPI_ID_BITS_LINEAR, MIN_LPI_ID_BITS) LPI ID
+ * bits; MIN_LPI_ID_BITS is at most 14 as per the architecture. On
+ * hardware with 2-level IST support, it uses a linear table only when
+ * ((10-ISTSZ) + (2*L2SZ)) covers the ID space, which is at most 14 as
+ * well.
+ *
+ * LPI_ID_BITS_LINEAR may ask for more than 14; the assertion makes sure
+ * the maximum possible table fits in one kmalloc() chunk.
+ */
+#define GICV5_IRS_LINEAR_MAX_ID_BITS	\
+	((LPI_ID_BITS_LINEAR) > 14 ? (LPI_ID_BITS_LINEAR) : 14)
+
+static_assert(BIT(GICV5_IRS_LINEAR_MAX_ID_BITS + 2 +
+		  GICV5_IRS_IST_CFGR_ISTSZ_MAX) <= KMALLOC_MAX_SIZE,
+	      "GICv5 linear IST does not fit in KMALLOC_MAX_SIZE");
+
 static DEFINE_PER_CPU_READ_MOSTLY(struct gicv5_irs_chip_data *, per_cpu_irs_data);
 static LIST_HEAD(irs_nodes);
 
@@ -78,20 +98,6 @@ static int __init gicv5_irs_init_ist_linear(struct gicv5_irs_chip_data *irs_data
 	n = max(5, lpi_id_bits + 1 + istsz);
 
 	l2istsz = BIT(n + 1);
-	/*
-	 * Check memory requirements. For a linear IST we cap the
-	 * number of ID bits to a value that should never exceed
-	 * kmalloc interface memory allocation limits, so this
-	 * check is really belt and braces.
-	 */
-	if (l2istsz > KMALLOC_MAX_SIZE) {
-		u8 lpi_id_cap = ilog2(KMALLOC_MAX_SIZE) - 2 + istsz;
-
-		pr_warn("Limiting LPI ID bits from %u to %u\n",
-			lpi_id_bits, lpi_id_cap);
-		lpi_id_bits = lpi_id_cap;
-		l2istsz = KMALLOC_MAX_SIZE;
-	}
 
 	ist = kzalloc(l2istsz, GFP_KERNEL);
 	if (!ist)
@@ -317,9 +323,9 @@ static int __init gicv5_irs_init_ist(struct gicv5_irs_chip_data *irs_data)
 	 * number of IDs.
 	 *
 	 * For 1-level tables, we should support a number of bits that
-	 * is >= min_lpi_id_bits but cap it to LPI_ID_BITS_LINEAR lest
-	 * the level 1-table gets too large and its memory allocation
-	 * may fail.
+	 * is >= min_lpi_id_bits but cap it to LPI_ID_BITS_LINEAR to keep
+	 * the table small; the worst case is checked at build time, see
+	 * GICV5_IRS_LINEAR_MAX_ID_BITS.
 	 */
 	if (two_levels) {
 		lpi_id_bits = idr2_id_bits;
diff --git a/include/linux/irqchip/arm-gic-v5.h b/include/linux/irqchip/arm-gic-v5.h
index 299e75bc9525..1210c6de8452 100644
--- a/include/linux/irqchip/arm-gic-v5.h
+++ b/include/linux/irqchip/arm-gic-v5.h
@@ -252,6 +252,8 @@
 #define GICV5_IRS_IST_CFGR_ISTSZ_8	0b01
 #define GICV5_IRS_IST_CFGR_ISTSZ_16	0b10
 
+#define GICV5_IRS_IST_CFGR_ISTSZ_MAX	GICV5_IRS_IST_CFGR_ISTSZ_16
+
 #define GICV5_IRS_IST_CFGR_L2SZ_4K	0b00
 #define GICV5_IRS_IST_CFGR_L2SZ_16K	0b01
 #define GICV5_IRS_IST_CFGR_L2SZ_64K	0b10
-- 
2.33.0



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

* Re: [PATCH] irqchip/gic-v5: Replace the dead linear IST ID cap with a build-time check
  2026-12-04  9:20 [PATCH] irqchip/gic-v5: Replace the dead linear IST ID cap with a build-time check Zenghui Yu
@ 2026-12-04 11:44 ` Lorenzo Pieralisi
  0 siblings, 0 replies; 2+ messages in thread
From: Lorenzo Pieralisi @ 2026-12-04 11:44 UTC (permalink / raw)
  To: Zenghui Yu; +Cc: linux-arm-kernel, linux-kernel, maz, tglx, radu, wangzhigang17

On Fri, Dec 04, 2026 at 05:20:42PM +0800, Zenghui Yu wrote:
> gicv5_irs_init_ist_linear() has a cap intended to keep the linear IST
> within the kmalloc() limit. It is broken in two ways:
> 
> (a) It adds @istsz instead of subtracting it when computing the ID bits;
> 
> (b) and it only updates its local lpi_id_bits, so the caller keeps using
>     the original, uncapped value when publishing the LPI ID space
>     (gicv5_init_lpis()).

Bah. See below. Thank you for spotting it and fixing it.

> The branch is dead code, though. With the current LPI_ID_BITS_LINEAR (12),

Yep, that's a symptom of how useless the current check is, (a) and (b) are
the obvious consequences.

> a linear IST is used with at most 14 LPI ID bits. On 1-level-only HW,
> lpi_id_bits is at most max(LPI_ID_BITS_LINEAR, MIN_LPI_ID_BITS), and
> MIN_LPI_ID_BITS is at most 14 as per the specification. On 2-level-capable
> HW, a linear table is used only when lpi_id_bits fits in
> ((10-ISTSZ) + (2*L2SZ)), which is at most 14 as well. A table with 14 LPI
> ID bits and 16-byte ISTEs is 256KB, while KMALLOC_MAX_SIZE is 4MB on arm64
> with 4K pages and larger with 16K/64K pages.
> 
> Rather than fixing the dead code, remove the cap and add a build-time
> assertion: the maximum possible linear table, derived from
> GICV5_IRS_LINEAR_MAX_ID_BITS and the largest ISTE, must fit in
> KMALLOC_MAX_SIZE. This catches a future increase of LPI_ID_BITS_LINEAR or a
> page/MAX_ORDER configuration that would make the table too large.
> 
> Signed-off-by: Zenghui Yu <yuzenghui@huawei.com>
> ---
>  drivers/irqchip/irq-gic-v5-irs.c   | 46 +++++++++++++++++-------------
>  include/linux/irqchip/arm-gic-v5.h |  2 ++
>  2 files changed, 28 insertions(+), 20 deletions(-)
> 
> diff --git a/drivers/irqchip/irq-gic-v5-irs.c b/drivers/irqchip/irq-gic-v5-irs.c
> index 9d60f474aa4f..79176a9a7c2f 100644
> --- a/drivers/irqchip/irq-gic-v5-irs.c
> +++ b/drivers/irqchip/irq-gic-v5-irs.c
> @@ -7,7 +7,6 @@
>  
>  #include <linux/acpi.h>
>  #include <linux/kmemleak.h>
> -#include <linux/log2.h>
>  #include <linux/of.h>
>  #include <linux/of_address.h>
>  
> @@ -16,11 +15,32 @@
>  
>  /*
>   * Hardcoded ID_BITS limit for systems supporting only a 1-level IST
> - * table. Systems supporting only a 1-level IST table aren't expected
> - * to require more than 2^12 LPIs. Tweak as required.
> + * table. Such systems aren't expected to require more than 2^12 LPIs.
> + * Tweak as required; raising it above 14 also raises the size bound
> + * checked below.

Nit: 14 seems to come out of thin air. Maybe we can move the two defines
below together with the single comment (very clear) that you wrote below
explaining the reasoning as a whole.

>   */
>  #define LPI_ID_BITS_LINEAR		12
>  
> +/*
> + * Upper bound of LPI ID bits used by a linear IST.
> + *
> + * On hardware that only supports a 1-level IST, gicv5_irs_init_ist()
> + * asks for at most max(LPI_ID_BITS_LINEAR, MIN_LPI_ID_BITS) LPI ID
> + * bits; MIN_LPI_ID_BITS is at most 14 as per the architecture. On
> + * hardware with 2-level IST support, it uses a linear table only when
> + * ((10-ISTSZ) + (2*L2SZ)) covers the ID space, which is at most 14 as
> + * well.
> + *
> + * LPI_ID_BITS_LINEAR may ask for more than 14; the assertion makes sure
> + * the maximum possible table fits in one kmalloc() chunk.
> + */
> +#define GICV5_IRS_LINEAR_MAX_ID_BITS	\
> +	((LPI_ID_BITS_LINEAR) > 14 ? (LPI_ID_BITS_LINEAR) : 14)
> +
> +static_assert(BIT(GICV5_IRS_LINEAR_MAX_ID_BITS + 2 +
> +		  GICV5_IRS_IST_CFGR_ISTSZ_MAX) <= KMALLOC_MAX_SIZE,
> +	      "GICv5 linear IST does not fit in KMALLOC_MAX_SIZE");

Nit: if I really had to be picky this caters for the worst case where
MIN_LPI_ID_BITS is at its max level and so is ISTSZ, which I don't necessarily
expect but this patch makes sense to me, certainly more than current code.

Thanks,
Lorenzo


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

end of thread, other threads:[~2026-12-04 11:53 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-04  9:20 [PATCH] irqchip/gic-v5: Replace the dead linear IST ID cap with a build-time check Zenghui Yu
2026-12-04 11:44 ` Lorenzo Pieralisi

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox