mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Pengpeng Hou <hppiscas@163.com>
To: Anup Patel <anup@brainfault.org>, Thomas Gleixner <tglx@kernel.org>
Cc: Pengpeng Hou <hppiscas@163.com>, Radu Rendec <radu@rendec.net>,
	Paul Walmsley <pjw@kernel.org>,
	Palmer Dabbelt <palmer@dabbelt.com>,
	Albert Ou <aou@eecs.berkeley.edu>,
	Alexandre Ghiti <alex@ghiti.fr>,
	linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org,
	Andrew Jones <andrew.jones@oss.qualcomm.com>,
	Bjorn Topel <bjorn@kernel.org>
Subject: [PATCH 1/2] irqchip/riscv-imsic: Ignore unused group index shifts
Date: Sun,  6 Sep 2026 16:08:44 +0800	[thread overview]
Message-ID: <caplitmus-rv.4882de641c0d41f9bef843fe41b3084f.imsic-zero@163.com> (raw)
In-Reply-To: <caplitmus-rv.4f722288a2f1483090d75bcd991d5b90.imsic-cover@163.com>

The IMSIC parser validates and uses group_index_shift even when the
group index has zero width. The binding permits a zero-width group with
shift zero, but bits + shift - 1 underflows and rejects that layout.
APLIC MSI setup also validates HHXS although HHXW is zero.

Only validate and clear group-address bits when the group width is
nonzero. Use zero for APLIC HHXS when there is no group field. This also
avoids shifting an empty mask by an otherwise unused shift value.

The issue was found by our static-analysis tool and manually reviewed.

Fixes: 21a8f8a0eb35 ("irqchip: Add RISC-V incoming MSI controller early driver")
Assisted-by: GPT-5
Signed-off-by: Pengpeng Hou <hppiscas@163.com>
---

diff --git a/drivers/irqchip/irq-riscv-aplic-msi.c b/drivers/irqchip/irq-riscv-aplic-msi.c
index fb8d1838..569b3ff4 100644
--- a/drivers/irqchip/irq-riscv-aplic-msi.c
+++ b/drivers/irqchip/irq-riscv-aplic-msi.c
@@ -223,17 +223,21 @@ int aplic_msi_setup(struct device *dev, void __iomem *regs)
 		return -EINVAL;
 	}
 
-	/* Find first bit position of group index (HHXS) */
-	mc->hhxs = imsic_global->group_index_shift;
-	if (mc->hhxs < (2 * APLIC_xMSICFGADDR_PPN_SHIFT)) {
-		dev_err(dev, "IMSIC group index shift should be >= %d\n",
-			(2 * APLIC_xMSICFGADDR_PPN_SHIFT));
-		return -EINVAL;
-	}
-	mc->hhxs -= (2 * APLIC_xMSICFGADDR_PPN_SHIFT);
-	if (APLIC_xMSICFGADDRH_HHXS_MASK < mc->hhxs) {
-		dev_err(dev, "IMSIC group index shift big for APLIC HHXS\n");
-		return -EINVAL;
+	if (mc->hhxw) {
+		/* Find first bit position of group index (HHXS) */
+		mc->hhxs = imsic_global->group_index_shift;
+		if (mc->hhxs < (2 * APLIC_xMSICFGADDR_PPN_SHIFT)) {
+			dev_err(dev, "IMSIC group index shift should be >= %d\n",
+				(2 * APLIC_xMSICFGADDR_PPN_SHIFT));
+			return -EINVAL;
+		}
+		mc->hhxs -= (2 * APLIC_xMSICFGADDR_PPN_SHIFT);
+		if (APLIC_xMSICFGADDRH_HHXS_MASK < mc->hhxs) {
+			dev_err(dev, "IMSIC group index shift big for APLIC HHXS\n");
+			return -EINVAL;
+		}
+	} else {
+		mc->hhxs = 0;
 	}
 
 	/* Compute PPN base */
diff --git a/drivers/irqchip/irq-riscv-imsic-state.c b/drivers/irqchip/irq-riscv-imsic-state.c
index 9505ddbd..44b9c34c 100644
--- a/drivers/irqchip/irq-riscv-imsic-state.c
+++ b/drivers/irqchip/irq-riscv-imsic-state.c
@@ -741,10 +741,12 @@ static int __init imsic_parse_fwnode(struct fwnode_handle *fwnode,
 	}
 
 	/* Sanity check group index shift */
-	i = global->group_index_bits + global->group_index_shift - 1;
-	if (i >= BITS_PER_LONG) {
-		pr_err("%pfwP: group index shift too big\n", fwnode);
-		return -EINVAL;
+	if (global->group_index_bits) {
+		i = global->group_index_bits + global->group_index_shift - 1;
+		if (i >= BITS_PER_LONG) {
+			pr_err("%pfwP: group index shift too big\n", fwnode);
+			return -EINVAL;
+		}
 	}
 
 	/* Sanity check number of interrupt identities */
@@ -773,8 +775,9 @@ static int __init imsic_parse_fwnode(struct fwnode_handle *fwnode,
 	global->base_addr &= ~GENMASK(global->guest_index_bits +
 				       global->hart_index_bits +
 				       IMSIC_MMIO_PAGE_SHIFT - 1, 0);
-	global->base_addr &= ~((BIT(global->group_index_bits) - 1) <<
-			       global->group_index_shift);
+	if (global->group_index_bits)
+		global->base_addr &= ~((BIT(global->group_index_bits) - 1) <<
+				       global->group_index_shift);
 
 	/* Find number of MMIO register sets */
 	while (!imsic_get_mmio_resource(fwnode, *nr_mmios, &res))
@@ -854,8 +857,9 @@ int __init imsic_setup_state(struct fwnode_handle *fwnode, void *opaque)
 		base_addr &= ~GENMASK(global->guest_index_bits +
 				      global->hart_index_bits +
 				      IMSIC_MMIO_PAGE_SHIFT - 1, 0);
-		base_addr &= ~((BIT(global->group_index_bits) - 1) <<
-			       global->group_index_shift);
+		if (global->group_index_bits)
+			base_addr &= ~((BIT(global->group_index_bits) - 1) <<
+				       global->group_index_shift);
 		if (base_addr != global->base_addr) {
 			rc = -EINVAL;
 			pr_err("%pfwP: address mismatch for regset %d\n", fwnode, i);

base-commit: 13ca1c0b7d8a3ab1e59cb3e780c1dd7441e22515


  reply	other threads:[~2026-09-06  8:09 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-09-06  8:07 [PATCH 0/2] irqchip/riscv-imsic: Validate group index geometry Pengpeng Hou
2026-09-06  8:08 ` Pengpeng Hou [this message]
2026-09-06  9:26   ` [PATCH 1/2] irqchip/riscv-imsic: Ignore unused group index shifts Anup Patel
2026-09-06  8:09 ` [PATCH 2/2] irqchip/riscv-imsic: Reject overlapping group index fields Pengpeng Hou
2026-09-06 15:12   ` Anup Patel

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=caplitmus-rv.4882de641c0d41f9bef843fe41b3084f.imsic-zero@163.com \
    --to=hppiscas@163.com \
    --cc=alex@ghiti.fr \
    --cc=andrew.jones@oss.qualcomm.com \
    --cc=anup@brainfault.org \
    --cc=aou@eecs.berkeley.edu \
    --cc=bjorn@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-riscv@lists.infradead.org \
    --cc=palmer@dabbelt.com \
    --cc=pjw@kernel.org \
    --cc=radu@rendec.net \
    --cc=tglx@kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®