Skip to contentSkip to conversation

irqchip/gic-v5: Fix misc IRS and ITS host driver issues

PATCH In review Sascha Bischoff 2 participants 22 messages Nov 25 – Dec 2

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

v4 cover irqchip/gic-v5: Fix misc IRS and ITS host driver issues

This series fixes issues in the GICv5 host IRS and ITS drivers.

The ITS invalidation serialisation fix addresses Sashiko's report of a race between event and device invalidation commands [1]. The other patches fix IRS allocation and routing, ITS table selection and cache maintenance, ACPI coherency handling, and resource cleanup. The IRS synchronisation change prepares for upcoming KVM callers.

Since v2, Fuad reported missing cache maintenance and L1 invalidation when allocating an L2 device table, as well as unsafe table cleanup after a hardware timeout [2]. This revision includes his L2 device-table cache-clean patch and fixes for L1 invalidation and the host ITS timeout paths. The L2 IST timeout patch is deferred at Lorenzo's request while he reworks the approach.

The IRS changes:
* Reject allocation of an SPI without a backing IRS and warn about broken firmware.
* Only program IRS_PE_CR0.DPS when 1-of-N routing is supported.
* Serialise synchronisation requests and their completion polls in preparation for upcoming KVM callers.

The ITS changes:
* Serialise event and device cache invalidations, which share the DIDR register.
* Select linear device and interrupt translation tables when one L2 table covers the full ID space.
* Honour the ACPI noncoherent flag.
* Use firmware-node names in initialisation warnings.
* Invalidate the device cache before freeing an ITT.
* Correct cache-maintenance sizing after limiting the DeviceID width.
* Release the reserved MMIO region if DT-based ITS initialisation fails.
* Retain the ITT if device invalidation times out during registration or teardown.
* Clean a new L2 device table before publishing its valid L1 DTE.
* Invalidate the L1 DTE immediately after publishing the L2 device table, including when device registration subsequently fails.

The ITS invalidation serialisation fix, ITS table-selection fix, ACPI coherency fix, device-table cache-maintenance fixes and L1 DTE invalidation fix carry Cc: stable trailers for backporting.

The series is based on v7.4-rc4. Thanks to Lorenzo for the review and to Fuad for the additional reports and cache-clean patch.

Sascha

[1] https://sashiko.dev/#/patchset/[email protected]?part=11
[2] https://lore.kernel.org/r/[email protected]

---
Changes in v4:
- Rebased onto v7.4-rc4.
- Dropped the L2 IST timeout patch at Lorenzo's request while he
reworks the approach.
- Clarified that IRS synchronisation serialisation prepares for
upcoming KVM callers; removed its Fixes and Cc: stable tags.
- Collected Reviewed-by and Acked-by tags.
- Use FW_BUG for unimplemented SPI warnings.
- Mark retained ITTs, including L2 pointer arrays, with kmemleak_not_leak().
- Return early on device teardown invalidation failure.
- Inline L1 DTE invalidation and explain the effective EVENTID_BITS.
- Link to v3: https://patch.msgid.link/[email protected]

Changes in v3:
- Rebased onto v7.4-rc3.
- Added Fuad's patch to clean an L2 device table before publishing it.
- Retain the L2 IST and its L1 entry on a mapping timeout, and reject subsequent allocation attempts that would overwrite a pending mapping.
- Retain ITTs when device invalidation times out during registration
or teardown.
- Added L1 DTE invalidation immediately after publishing an L2 device table, keeping single-device invalidations for registration and teardown.
- Reworded the IRS synchronisation and ITS invalidation commit messages
to describe the host issues without relying on future KVM support.
- Collected Acked-by tags and reordered commit trailers as requested.
- Link to v2: https://patch.msgid.link/[email protected]

Changes in v2:
- Explained why IRS synchronisation uses a raw spinlock in the KVM
affinity path.
- Clarified the ITS invalidation race and described the KVM forwarding path.
- Reworked to calculate the device-table size after capping the
DeviceID width.
- Reordered local declarations in the ITS probe function to be in
reverse fir tree order.
- Added Acked-by tags.
- Link to v1: https://patch.msgid.link/[email protected]

---
Fuad Tabba (1):
irqchip/gic-v5: Clean a level 2 device table before making it valid

Sascha Bischoff (12):
irqchip/gic-v5: Reject SPIs not backed by an IRS irqchip/gic-v5: Only set IRS DPS when 1ofN routing is supported irqchip/gic-v5: Serialise IRS synchronisation requests irqchip/gic-v5: Serialise ITS invalidation commands irqchip/gic-v5: Use linear DT and ITT when L2 covers all IDs irqchip/gic-v5: Honour ACPI ITS noncoherent flag irqchip/gic-v5: Use firmware node name in ITS warning irqchip/gic-v5: Invalidate ITS device cache before freeing ITT irqchip/gic-v5: Clean the allocated linear device table size irqchip/gic-v5: Release ITS MMIO region on DT probe failure irqchip/gic-v5: Retain the ITT when device invalidation times out irqchip/gic-v5: Invalidate the L1 DTE when allocating an L2 device table

 drivers/irqchip/irq-gic-v5-irs.c   | 22 +++++++---
 drivers/irqchip/irq-gic-v5-its.c   | 87 +++++++++++++++++++++++++++++++-------
 drivers/irqchip/irq-gic-v5.c       |  9 ++++
 include/linux/irqchip/arm-gic-v5.h |  1 +
 4 files changed, 97 insertions(+), 22 deletions(-)

---
base-commit: b6a9fa34ce4187cb69d3c588a4bbffc31ce5db8f
change-id: 20261102-host_gicv5_fixes-1eeea29d2ea4

Best regards,

Signature · 1 line
Sascha Bischoff <[email protected]>
#1 lore raw

PATCH v4 1/13 irqchip/gic-v5: Reject SPIs not backed by an IRS

It is possible to build a multi-IRS GICv5 system where there are gaps in the global SPI range that are not backed by any IRS. In the current incarnation, this results in dereferencing a null pointer.

Reject cases where the firmware attempts to use those SPIs during IRQ allocation by catching the case where there is no chip_data associated with the irq (meaning that there is no IRS to back it). Spit out a warning about broken firmware when this is encountered as it shouldn't be using non-existent SPIs in the first place.

Fixes 5cb1b6dab2def ("irqchip/gic-v5: Add GICv5 IRS/SPI support")
---
 drivers/irqchip/irq-gic-v5.c | 9 +++++++++
 1 file changed, 9 insertions(+)
drivers/irqchip/irq-gic-v5.c +9 -0
index 8c0303af71e7..693ce1ee263d 100644
@@ -727,6 +727,15 @@ static int gicv5_irq_spi_domain_alloc(struct irq_domain *domain, unsigned int vi
 
 	irqd = irq_desc_get_irq_data(irq_to_desc(virq));
 	chip_data = gicv5_irs_lookup_by_spi_id(hwirq);
+	/*
+	 * The global SPI range may contain IDs not covered by any IRS, i.e., it
+	 * may have gaps, in which case there is no IRS to back the SPI. Catch
+	 * broken firmware trying to use any of those SPIs.
+	 */
+	if (!chip_data) {
+		pr_warn(FW_BUG "Unimplemented SPI (0x%lx)!\n", hwirq);
+		return -EINVAL;
+	}
 
 	irq_domain_set_info(domain, virq, hwirq, &gicv5_spi_irq_chip, chip_data,
 			    handle_fasteoi_irq, NULL, NULL);
Signature · 1 line
2.34.1
Reviewed-by on v4 1/13
#2 lore raw

Re: [PATCH v4 01/13] irqchip/gic-v5: Reject SPIs not backed by an IRS

Quoted 11 lines · Sascha Bischoff

It is possible to build a multi-IRS GICv5 system where there are gaps in the global SPI range that are not backed by any IRS. In the current incarnation, this results in dereferencing a null pointer.

Reject cases where the firmware attempts to use those SPIs during IRQ allocation by catching the case where there is no chip_data associated with the irq (meaning that there is no IRS to back it). Spit out a warning about broken firmware when this is encountered as it shouldn't be using non-existent SPIs in the first place.

Quoted 3 lines
Fixes 5cb1b6dab2def ("irqchip/gic-v5: Add GICv5 IRS/SPI support")

Cheers,
/fuad

#3 lore raw

PATCH v4 2/13 irqchip/gic-v5: Only set IRS DPS when 1ofN routing is supported

In reply to Sascha Bischoff

IRS_PE_CR0.DPS is RES0 when the IRS does not support 1ofN routing. Read the capability from IRS_IDR0 and skip programming DPS on systems without support for 1ofN routing, rather than blindly writing it.

Fixes 5cb1b6dab2def ("irqchip/gic-v5: Add GICv5 IRS/SPI support")
---
 drivers/irqchip/irq-gic-v5-irs.c   | 14 +++++++++-----
 include/linux/irqchip/arm-gic-v5.h |  1 +
 2 files changed, 10 insertions(+), 5 deletions(-)
drivers/irqchip/irq-gic-v5-irs.c +9 -5
index 9d60f474aa4f..4edf787f3f97 100644
@@ -541,12 +541,15 @@ int gicv5_irs_register_cpu(int cpuid)
 		return -ENXIO;
 	}
 
-	cr0 = FIELD_PREP(GICV5_IRS_PE_CR0_DPS, 0x1);
-	irs_writel_relaxed(irs_data, cr0, GICV5_IRS_PE_CR0);
+	/* IRS_PE_CR0.DPS is RES0 when 1ofN routing is unsupported. */
+	if (gicv5_global_data.one_of_n) {
+		cr0 = FIELD_PREP(GICV5_IRS_PE_CR0_DPS, 0x1);
+		irs_writel_relaxed(irs_data, cr0, GICV5_IRS_PE_CR0);
 
-	ret = gicv5_irs_wait_for_pe_cr0(irs_data);
-	if (ret)
-		return ret;
+		ret = gicv5_irs_wait_for_pe_cr0(irs_data);
+		if (ret)
+			return ret;
+	}
 
 	pr_debug("CPU %d enabled PE IAFFID 0x%x\n", cpuid, iaffid);
 
@@ -725,6 +728,7 @@ static int __init gicv5_irs_init(struct gicv5_irs_chip_data *irs_data)
 	if (list_empty(&irs_nodes)) {
 		idr = irs_readl_relaxed(irs_data, GICV5_IRS_IDR0);
 		gicv5_global_data.virt_capable = !!FIELD_GET(GICV5_IRS_IDR0_VIRT, idr);
+		gicv5_global_data.one_of_n = !!FIELD_GET(GICV5_IRS_IDR0_ONE_N, idr);
 
 		idr = irs_readl_relaxed(irs_data, GICV5_IRS_IDR1);
 		irs_setup_pri_bits(idr);
include/linux/irqchip/arm-gic-v5.h +1 -0
index 299e75bc9525..34e22e7d4a4c 100644
@@ -532,6 +532,7 @@ struct gicv5_chip_data {
 	u8			cpuif_id_bits;
 	u8			irs_pri_bits;
 	bool			virt_capable;
+	bool			one_of_n;
 	struct {
 		__le64 *l1ist_addr;
 		u32 l2_size;
Signature · 1 line
2.34.1
#4 lore raw

PATCH v4 3/13 irqchip/gic-v5: Serialise IRS synchronisation requests

In reply to Sascha Bischoff

IRS_SYNCR writes are ignored while IRS_SYNC_STATUSR.IDLE is clear. Concurrent calls to gicv5_irs_syncr() can overlap, causing a request to be dropped. A caller can then observe the earlier request completing and assume that its own events have been processed.

The existing host caller is serialised by the LPI irqdomain mutex. Upcoming KVM callers will not share that serialisation and may run in atomic context. Serialise the request and completion poll with a raw spinlock, and use atomic polling.

---
 drivers/irqchip/irq-gic-v5-irs.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)
drivers/irqchip/irq-gic-v5-irs.c +6 -2
index 4edf787f3f97..ead76560ea08 100644
@@ -10,6 +10,7 @@
 #include <linux/log2.h>
 #include <linux/of.h>
 #include <linux/of_address.h>
+#include <linux/spinlock.h>
 
 #include <linux/irqchip.h>
 #include <linux/irqchip/arm-gic-v5.h>
@@ -22,6 +23,8 @@
 #define LPI_ID_BITS_LINEAR		12
 
 static DEFINE_PER_CPU_READ_MOSTLY(struct gicv5_irs_chip_data *, per_cpu_irs_data);
+/* Synchronization requests must not overlap in an IRS Domain. */
+static DEFINE_RAW_SPINLOCK(irs_sync_lock);
 static LIST_HEAD(irs_nodes);
 
 static u32 irs_readl_relaxed(struct gicv5_irs_chip_data *irs_data,
@@ -507,10 +510,11 @@ void gicv5_irs_syncr(void)
 		return;
 
 	syncr = FIELD_PREP(GICV5_IRS_SYNCR_SYNC, 1);
+	guard(raw_spinlock_irqsave)(&irs_sync_lock);
 	irs_writel_relaxed(irs_data, syncr, GICV5_IRS_SYNCR);
 
-	gicv5_wait_for_op(irs_data->irs_base, GICV5_IRS_SYNC_STATUSR,
-			  GICV5_IRS_SYNC_STATUSR_IDLE);
+	gicv5_wait_for_op_atomic(irs_data->irs_base, GICV5_IRS_SYNC_STATUSR,
+				 GICV5_IRS_SYNC_STATUSR_IDLE, NULL);
 }
 
 int gicv5_irs_register_cpu(int cpuid)
Signature · 1 line
2.34.1
#5 lore raw

PATCH v4 4/13 irqchip/gic-v5: Serialise ITS invalidation commands

In reply to Sascha Bischoff

The ITS uses the shared DIDR and EIDR registers to select the DeviceID and EventID to invalidate. Event invalidations for different IRQs can overlap, as can an event invalidation and a device invalidation. The per-IRQ descriptor lock does not serialise these commands, so concurrent writes can cause an invalidation to use the DeviceID or EventID set by another concurrent invalidation.

Serialise the command register writes and STATUSR.IDLE polls for both invalidation paths with a per-ITS raw spinlock. Use atomic polling while holding the lock.

---
 drivers/irqchip/irq-gic-v5-its.c | 9 +++++++++
 1 file changed, 9 insertions(+)
drivers/irqchip/irq-gic-v5-its.c +9 -0
index f876dc0de1b9..88d18822a1ff 100644
@@ -16,6 +16,7 @@
 #include <linux/of_address.h>
 #include <linux/of_irq.h>
 #include <linux/slab.h>
+#include <linux/spinlock.h>
 
 #include <linux/irqchip.h>
 #include <linux/irqchip/arm-gic-v5.h>
@@ -28,6 +29,8 @@
 struct gicv5_its_chip_data {
 	struct	xarray			its_devices;
 	struct	mutex			dev_alloc_lock;
+	/* Event and device invalidation commands share DIDR. */
+	raw_spinlock_t			inv_lock;
 	struct	fwnode_handle		*fwnode;
 	struct gicv5_its_devtab_cfg	devtab_cfgr;
 	void	__iomem			*its_base;
@@ -129,6 +132,8 @@ static int gicv5_its_itt_cache_inv(struct gicv5_its_chip_data *its, u32 device_i
 	eidr = FIELD_PREP(GICV5_ITS_EIDR_EVENTID, event_id);
 	eventr = FIELD_PREP(GICV5_ITS_INV_EVENTR_I, 0x1);
 
+	guard(raw_spinlock_irqsave)(&its->inv_lock);
+
 	its_writeq_relaxed(its, didr, GICV5_ITS_DIDR);
 	its_writel_relaxed(its, eidr, GICV5_ITS_EIDR);
 	its_writel_relaxed(its, eventr, GICV5_ITS_INV_EVENTR);
@@ -371,6 +376,9 @@ static int gicv5_its_device_cache_inv(struct gicv5_its_chip_data *its,
 		  FIELD_PREP(GICV5_ITS_INV_DEVICER_EVENTID_BITS,
 			     its_dev->itt_cfg.event_id_bits)	|
 		  FIELD_PREP(GICV5_ITS_INV_DEVICER_L1, 0x0);
+
+	guard(raw_spinlock_irqsave)(&its->inv_lock);
+
 	its_writeq_relaxed(its, didr, GICV5_ITS_DIDR);
 	its_writel_relaxed(its, devicer, GICV5_ITS_INV_DEVICER);
 
@@ -1112,6 +1120,7 @@ static int __init gicv5_its_init_bases(void __iomem *its_base, struct fwnode_han
 		return -ENOMEM;
 
 	mutex_init(&its_node->dev_alloc_lock);
+	raw_spin_lock_init(&its_node->inv_lock);
 	xa_init(&its_node->its_devices);
 	its_node->fwnode = handle;
 	its_node->its_base = its_base;
Signature · 1 line
2.34.1
Reviewed-by on v4 4/13
#6 lore raw

Re: [PATCH v4 04/13] irqchip/gic-v5: Serialise ITS invalidation commands

Hi Sascha,

Quoted 7 lines · Sascha Bischoff

The ITS uses the shared DIDR and EIDR registers to select the DeviceID and EventID to invalidate. Event invalidations for different IRQs can overlap, as can an event invalidation and a device invalidation. The per-IRQ descriptor lock does not serialise these commands, so concurrent writes can cause an invalidation to use the DeviceID or EventID set by another concurrent invalidation.

Quoted 7 lines

Serialise the command register writes and STATUSR.IDLE polls for both invalidation paths with a per-ITS raw spinlock. Use atomic polling while holding the lock.

Fixes 57d72196dfc8 ("irqchip/gic-v5: Add GICv5 ITS support")

Cheers,
/fuad

#7 lore raw

PATCH v4 5/13 irqchip/gic-v5: Use linear DT and ITT when L2 covers all IDs

In reply to Sascha Bischoff

The DT and ITT level selector incorrectly chooses a two-level table when the selected L2 table has exactly enough entries to cover the full ID space. There are no L1 index bits in this case. ITT allocation correctly rejects the layout and device registration fails; the DT path instead allocates an unnecessary one-entry L1 table.

Select a linear DT or ITT when the L2 width equals the ID width.

Fixes 57d72196dfc85 ("irqchip/gic-v5: Add GICv5 ITS support")
---
 drivers/irqchip/irq-gic-v5-its.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/irqchip/irq-gic-v5-its.c +1 -1
index 88d18822a1ff..5f9fa80ea6aa 100644
@@ -337,7 +337,7 @@ static bool gicv5_its_l2sz_two_level(bool devtab, u32 its_idr1, u8 id_bits, u8 *
 
 	l2_bits = gicv5_its_l2sz_to_l2_bits(l2_sz);
 
-	if (l2_bits > id_bits)
+	if (l2_bits >= id_bits)
 		return false;
 
 	*sz = l2_sz;
Signature · 1 line
2.34.1
Reviewed-by on v4 5/13
#8 lore raw

Re: [PATCH v4 05/13] irqchip/gic-v5: Use linear DT and ITT when L2 covers all IDs

Quoted 7 lines · Sascha Bischoff

The DT and ITT level selector incorrectly chooses a two-level table when the selected L2 table has exactly enough entries to cover the full ID space. There are no L1 index bits in this case. ITT allocation correctly rejects the layout and device registration fails; the DT path instead allocates an unnecessary one-entry L1 table.

Quoted 5 lines

Select a linear DT or ITT when the L2 width equals the ID width.

Fixes 57d72196dfc85 ("irqchip/gic-v5: Add GICv5 ITS support")

Cheers,
/fuad

#9 lore raw

PATCH v4 6/13 irqchip/gic-v5: Honour ACPI ITS noncoherent flag

In reply to Sascha Bischoff

The ACPI ITS probe passes its noncoherent flag to gicv5_its_init_bases(), but the common initializer checks a DT property instead. For an ACPI ITS, this always selects coherent cache settings and skips the cache maintenance required for its tables.

Use the noncoherent argument, which both firmware paths already supply.

Fixes a97efa5ba5946 ("irqchip/gic-v5: Add ACPI ITS probing")
---
 drivers/irqchip/irq-gic-v5-its.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/irqchip/irq-gic-v5-its.c +1 -1
index 5f9fa80ea6aa..9e8077dada5e 100644
@@ -1135,7 +1135,7 @@ static int __init gicv5_its_init_bases(void __iomem *its_base, struct fwnode_han
 			goto out_free_node;
 	}
 
-	if (of_property_read_bool(np, "dma-noncoherent")) {
+	if (noncoherent) {
 		/*
 		 * A non-coherent ITS implies that some cache levels cannot be
 		 * used coherently by the cores and GIC. Our only option is to mark
Signature · 1 line
2.34.1
Reviewed-by on v4 6/13
#10 lore raw

Re: [PATCH v4 06/13] irqchip/gic-v5: Honour ACPI ITS noncoherent flag

Quoted 6 lines · Sascha Bischoff

The ACPI ITS probe passes its noncoherent flag to gicv5_its_init_bases(), but the common initializer checks a DT property instead. For an ACPI ITS, this always selects coherent cache settings and skips the cache maintenance required for its tables.

Quoted 7 lines

Use the noncoherent argument, which both firmware paths already supply.

Fixes a97efa5ba5946 ("irqchip/gic-v5: Add ACPI ITS probing")

Cheers,
/fuad

#11 lore raw

PATCH v4 7/13 irqchip/gic-v5: Use firmware node name in ITS warning

In reply to Sascha Bischoff

The common ITS initialiser can be called with an ACPI firmware node. If firmware leaves the ITS enabled, the warning dereferences the DT node pointer, which is NULL on that path.

Use the firmware node name for the warning and drop the DT-only pointer.

Fixes a97efa5ba5946 ("irqchip/gic-v5: Add ACPI ITS probing")
---
 drivers/irqchip/irq-gic-v5-its.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/irqchip/irq-gic-v5-its.c +2 -2
index 9e8077dada5e..876437017ccc 100644
@@ -1109,7 +1109,6 @@ static int gicv5_its_init_domain(struct gicv5_its_chip_data *its, struct irq_dom
 static int __init gicv5_its_init_bases(void __iomem *its_base, struct fwnode_handle *handle,
 				       struct irq_domain *parent_domain, bool noncoherent)
 {
-	struct device_node *np = to_of_node(handle);
 	struct gicv5_its_chip_data *its_node;
 	u32 cr0, cr1;
 	bool enabled;
@@ -1129,7 +1128,8 @@ static int __init gicv5_its_init_bases(void __iomem *its_base, struct fwnode_han
 
 	cr0 = its_readl_relaxed(its_node, GICV5_ITS_CR0);
 	enabled = FIELD_GET(GICV5_ITS_CR0_ITSEN, cr0);
-	if (WARN(enabled, "ITS %s enabled, disabling it before proceeding\n", np->full_name)) {
+	if (WARN(enabled, "ITS %s enabled, disabling it before proceeding\n",
+		 fwnode_get_name(handle))) {
 		ret = gicv5_its_disable(its_node);
 		if (ret)
 			goto out_free_node;
Signature · 1 line
2.34.1
Reviewed-by on v4 7/13
#12 lore raw

Re: [PATCH v4 07/13] irqchip/gic-v5: Use firmware node name in ITS warning

Quoted 11 lines · Sascha Bischoff

The common ITS initialiser can be called with an ACPI firmware node. If firmware leaves the ITS enabled, the warning dereferences the DT node pointer, which is NULL on that path.

Use the firmware node name for the warning and drop the DT-only pointer.

Fixes a97efa5ba5946 ("irqchip/gic-v5: Add ACPI ITS probing")

Cheers,
/fuad

#13 lore raw

PATCH v4 8/13 irqchip/gic-v5: Invalidate ITS device cache before freeing ITT

In reply to Sascha Bischoff

A cached device table entry can still reference the ITT after its memory is freed via kfree(), even if the DTE has been marked as invalid. Once the memory has been released, it could be reallocated, and it would be unfortunate for the hardware to interpret this as a valid ITT.

Invalidate the device cache and wait for completion before freeing the ITT during device teardown.

Fixes 57d72196dfc85 ("irqchip/gic-v5: Add GICv5 ITS support")
---
 drivers/irqchip/irq-gic-v5-its.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
drivers/irqchip/irq-gic-v5-its.c +3 -1
index 876437017ccc..dce5bfd1a78d 100644
@@ -556,6 +556,7 @@ static int gicv5_its_device_unregister(struct gicv5_its_chip_data *its,
 				       struct gicv5_its_dev *its_dev)
 {
 	__le64 *dte;
+	int ret;
 
 	dte = gicv5_its_devtab_get_dte_ref(its, its_dev->device_id, false);
 
@@ -568,9 +569,10 @@ static int gicv5_its_device_unregister(struct gicv5_its_chip_data *its,
 	/* Zero everything - make it clear that this is an invalid entry */
 	its_write_table_entry(its, dte, 0);
 
+	ret = gicv5_its_device_cache_inv(its, its_dev);
 	gicv5_its_free_itt(its_dev);
 
-	return gicv5_its_device_cache_inv(its, its_dev);
+	return ret;
 }
 
 /*
Signature · 1 line
2.34.1
Reviewed-by on v4 8/13
#14 lore raw

Re: [PATCH v4 08/13] irqchip/gic-v5: Invalidate ITS device cache before freeing ITT

Quoted 7 lines · Sascha Bischoff

A cached device table entry can still reference the ITT after its memory is freed via kfree(), even if the DTE has been marked as invalid. Once the memory has been released, it could be reallocated, and it would be unfortunate for the hardware to interpret this as a valid ITT.

Quoted 7 lines

Invalidate the device cache and wait for completion before freeing the ITT during device teardown.

Fixes 57d72196dfc85 ("irqchip/gic-v5: Add GICv5 ITS support")

Cheers,
/fuad

#15 lore raw

PATCH v4 9/13 irqchip/gic-v5: Clean the allocated linear device table size

In reply to Sascha Bischoff

When the linear device table exceeds the kmalloc limit, the driver caps the DeviceID width before allocation but keeps the original size for cache maintenance. On a noncoherent ITS, this cleans beyond the allocated table.

Recalculate the size after capping the DeviceID width.

Fixes 57d72196dfc85 ("irqchip/gic-v5: Add GICv5 ITS support")
---
 drivers/irqchip/irq-gic-v5-its.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
drivers/irqchip/irq-gic-v5-its.c +3 -3
index dce5bfd1a78d..e7db4449fe91 100644
@@ -594,14 +594,14 @@ static int gicv5_its_alloc_devtab_linear(struct gicv5_its_chip_data *its,
 	 * kmalloc limits so that the system can chug along with a linear
 	 * device table.
 	 */
-	sz = BIT_ULL(device_id_bits) * sizeof(*devtab);
-	if (sz > KMALLOC_MAX_SIZE) {
-		u8 device_id_cap = ilog2(KMALLOC_MAX_SIZE/sizeof(*devtab));
+	if (BIT_ULL(device_id_bits) * sizeof(*devtab) > KMALLOC_MAX_SIZE) {
+		u8 device_id_cap = ilog2(KMALLOC_MAX_SIZE / sizeof(*devtab));
 
 		pr_warn("Limiting device ID bits from %u to %u\n",
 			device_id_bits, device_id_cap);
 		device_id_bits = device_id_cap;
 	}
+	sz = BIT_ULL(device_id_bits) * sizeof(*devtab);
 
 	devtab = kzalloc_objs(*devtab, BIT(device_id_bits));
 	if (!devtab)
Signature · 1 line
2.34.1
Reviewed-by on v4 9/13
#16 lore raw

Re: [PATCH v4 09/13] irqchip/gic-v5: Clean the allocated linear device table size

Hi Sascha,

Quoted 11 lines · Sascha Bischoff

When the linear device table exceeds the kmalloc limit, the driver caps the DeviceID width before allocation but keeps the original size for cache maintenance. On a noncoherent ITS, this cleans beyond the allocated table.

Recalculate the size after capping the DeviceID width.

Fixes 57d72196dfc85 ("irqchip/gic-v5: Add GICv5 ITS support")

[...]

Quoted 1 line
drivers/irqchip/irq-gic-v5-its.c +0 -0

[...]

drivers/irqchip/irq-gic-v5-its.c static int gicv5_its_alloc_devtab_linear(struct gicv5_its_chip_data *its, · line 598

Quoted 9 lines
drivers/irqchip/irq-gic-v5-its.c +2 -3
@@ -594,14 +594,14 @@ static int gicv5_its_alloc_devtab_linear(struct gicv5_its_chip_data *its,
         * kmalloc limits so that the system can chug along with a linear
         * device table.
         */
-        sz = BIT_ULL(device_id_bits) * sizeof(*devtab);
-        if (sz > KMALLOC_MAX_SIZE) {
-                u8 device_id_cap = ilog2(KMALLOC_MAX_SIZE/sizeof(*devtab));
+        if (BIT_ULL(device_id_bits) * sizeof(*devtab) > KMALLOC_MAX_SIZE) {
+                u8 device_id_cap = ilog2(KMALLOC_MAX_SIZE / sizeof(*devtab));

nit: the device_id_cap line only changes whitespace. Better left out of stable fix?

Cheers,
/fuad

#17 lore raw

PATCH v4 10/13 irqchip/gic-v5: Release ITS MMIO region on DT probe failure

In reply to Sascha Bischoff

of_io_request_and_map() reserves the ITS configuration frame region. If ITS initialisation fails after mapping it, the error path only unmaps the registers and leaves the region reserved.

Keep the resource description and release the region on that path.

Fixes 57d72196dfc85 ("irqchip/gic-v5: Add GICv5 ITS support")
---
 drivers/irqchip/irq-gic-v5-its.c | 7 +++++++
 1 file changed, 7 insertions(+)
drivers/irqchip/irq-gic-v5-its.c +7 -0
index e7db4449fe91..db13011ef6d9 100644
@@ -1188,6 +1188,7 @@ static int __init gicv5_its_init_bases(void __iomem *its_base, struct fwnode_han
 static int __init gicv5_its_init(struct device_node *node)
 {
 	void __iomem *its_base;
+	struct resource res;
 	int ret, idx;
 
 	idx = of_property_match_string(node, "reg-names", "ns-config");
@@ -1196,6 +1197,11 @@ static int __init gicv5_its_init(struct device_node *node)
 		return -ENODEV;
 	}
 
+	/* Save the region to release if ITS setup fails after mapping it. */
+	ret = of_address_to_resource(node, idx, &res);
+	if (ret)
+		return ret;
+
 	its_base = of_io_request_and_map(node, idx, of_node_full_name(node));
 	if (IS_ERR(its_base)) {
 		pr_err("%pOF: unable to map GICv5 ITS_CONFIG_FRAME\n", node);
@@ -1212,6 +1218,7 @@ static int __init gicv5_its_init(struct device_node *node)
 
 out_unmap:
 	iounmap(its_base);
+	release_mem_region(res.start, resource_size(&res));
 	return ret;
 }
 
Signature · 1 line
2.34.1
Reviewed-by on v4 10/13
#18 lore raw

Re: [PATCH v4 10/13] irqchip/gic-v5: Release ITS MMIO region on DT probe failure

Hi Sascha,

Quoted 9 lines · Sascha Bischoff

of_io_request_and_map() reserves the ITS configuration frame region. If ITS initialisation fails after mapping it, the error path only unmaps the registers and leaves the region reserved.

Keep the resource description and release the region on that path.

Fixes 57d72196dfc85 ("irqchip/gic-v5: Add GICv5 ITS support")

Cheers,
/fuad

#19 lore raw

PATCH v4 11/13 irqchip/gic-v5: Retain the ITT when device invalidation times out

In reply to Sascha Bischoff

Device registration publishes a DTE pointing to the new ITT before invalidating the ITS device cache. If invalidation times out, clearing the DTE in memory does not guarantee that the ITS has discarded a cached reference to the ITT, and hence the hardware could still try and access the memory. Freeing the ITT on that path can therefore leave the ITS accessing memory that has been reallocated.

Device teardown has the same problem: it frees the ITT even when the invalidation issued after clearing the DTE fails to complete in the specified timeout.

Retain the ITT on either failure path. Continue returning the invalidation error, and free the ITT during teardown only after successful completion. This deliberately retains memory on hardware failure because a timeout does not cancel the operation or establish that cached references are gone.

---
 drivers/irqchip/irq-gic-v5-its.c | 29 +++++++++++++++++++++++++++--
 1 file changed, 27 insertions(+), 2 deletions(-)
drivers/irqchip/irq-gic-v5-its.c +27 -2
index db13011ef6d9..37835d1600fb 100644
@@ -11,6 +11,7 @@
 #include <linux/iommu.h>
 #include <linux/init.h>
 #include <linux/kernel.h>
+#include <linux/kmemleak.h>
 #include <linux/msi.h>
 #include <linux/of.h>
 #include <linux/of_address.h>
@@ -165,6 +166,23 @@ static void gicv5_its_free_itt(struct gicv5_its_dev *its_dev)
 		gicv5_its_free_itt_two_level(its_dev);
 }
 
+/* The ITS may still reference these allocations after its_dev is freed. */
+static void gicv5_its_itt_not_leak(struct gicv5_its_dev *its_dev)
+{
+	unsigned int i;
+
+	if (!its_dev->itt_cfg.l2itt) {
+		kmemleak_not_leak(its_dev->itt_cfg.linear.itt);
+		return;
+	}
+
+	for (i = 0; i < its_dev->itt_cfg.l2.num_l1_ents; i++)
+		kmemleak_not_leak(its_dev->itt_cfg.l2.l2ptrs[i]);
+
+	kmemleak_not_leak(its_dev->itt_cfg.l2.l2ptrs);
+	kmemleak_not_leak(its_dev->itt_cfg.l2.l1itt);
+}
+
 static int gicv5_its_create_itt_linear(struct gicv5_its_chip_data *its,
 				       struct gicv5_its_dev *its_dev,
 				       unsigned int event_id_bits)
@@ -540,8 +558,9 @@ static int gicv5_its_device_register(struct gicv5_its_chip_data *its,
 
 	ret = gicv5_its_device_cache_inv(its, its_dev);
 	if (ret) {
+		/* The ITS may still have a cached reference to the ITT. */
 		its_write_table_entry(its, dte, 0);
-		gicv5_its_free_itt(its_dev);
+		gicv5_its_itt_not_leak(its_dev);
 		return ret;
 	}
 
@@ -570,9 +589,15 @@ static int gicv5_its_device_unregister(struct gicv5_its_chip_data *its,
 	its_write_table_entry(its, dte, 0);
 
 	ret = gicv5_its_device_cache_inv(its, its_dev);
+	if (ret) {
+		gicv5_its_itt_not_leak(its_dev);
+		return ret;
+	}
+
+	/* Only release the ITT once the ITS has discarded its references. */
 	gicv5_its_free_itt(its_dev);
 
-	return ret;
+	return 0;
 }
 
 /*
Signature · 1 line
2.34.1
#20 lore raw

PATCH v4 12/13 irqchip/gic-v5: Clean a level 2 device table before making it valid

In reply to Sascha Bischoff

From: Fuad Tabba <[email protected]>

gicv5_its_alloc_l2_devtab() allocates a zeroed level 2 device table and writes the level 1 DTE that points to it with VALID set. Unlike the other ITS tables, the new table isn't cleaned to the PoC first. On a non-coherent ITS, the zeroes can still sit in the CPU caches when the ITS reads the table, so it can take stale memory as a valid level 2 DTE for a DeviceID that nothing has registered.

Clean the table before the level 1 DTE is written.

Fixes 57d72196dfc85 ("irqchip/gic-v5: Add GICv5 ITS support")
---
 drivers/irqchip/irq-gic-v5-its.c | 2 ++
 1 file changed, 2 insertions(+)
drivers/irqchip/irq-gic-v5-its.c +2 -0
index 37835d1600fb..959277c74b68 100644
@@ -433,6 +433,8 @@ static int gicv5_its_alloc_l2_devtab(struct gicv5_its_chip_data *its,
 	if (!l2devtab)
 		return -ENOMEM;
 
+	gicv5_its_dcache_clean(its, l2devtab, BIT(span) * sizeof(*l2devtab));
+
 	its->devtab_cfgr.l2.l2ptrs[l1_index] = l2devtab;
 
 	l1dte = FIELD_PREP(GICV5_DTL1E_SPAN, span)			|
Signature · 1 line
2.34.1
#21 lore raw

PATCH v4 13/13 irqchip/gic-v5: Invalidate the L1 DTE when allocating an L2 device table

In reply to Sascha Bischoff

Allocating an L2 device table makes its parent L1 DTE valid, but the single-device invalidation performed during device registration does not invalidate that L1 DTE. An ITS that cached the L1 DTE while it was invalid can therefore continue dropping events for DeviceIDs in its range.

Invalidate the L1 DTE and wait for completion immediately after publishing the new L2 table. This also covers a registration that subsequently fails, leaving the L2 table allocated and its parent L1 DTE valid. Keep single-device invalidations for device registration and teardown when updating individual L2 DTEs.

---
 drivers/irqchip/irq-gic-v5-its.c | 24 ++++++++++++++++++------
 1 file changed, 18 insertions(+), 6 deletions(-)
drivers/irqchip/irq-gic-v5-its.c +18 -6
index 959277c74b68..87218ff05d7b 100644
@@ -383,17 +383,17 @@ static __le64 *gicv5_its_device_get_itte_ref(struct gicv5_its_dev *its_dev,
 	return &l2_itt[l2_idx];
 }
 
-static int gicv5_its_device_cache_inv(struct gicv5_its_chip_data *its,
-				      struct gicv5_its_dev *its_dev)
+static int __gicv5_its_device_cache_inv(struct gicv5_its_chip_data *its,
+					u32 device_id, u8 event_id_bits, bool l1)
 {
 	u32 devicer;
 	u64 didr;
 
-	didr = FIELD_PREP(GICV5_ITS_DIDR_DEVICEID, its_dev->device_id);
+	didr = FIELD_PREP(GICV5_ITS_DIDR_DEVICEID, device_id);
 	devicer = FIELD_PREP(GICV5_ITS_INV_DEVICER_I, 0x1)	|
 		  FIELD_PREP(GICV5_ITS_INV_DEVICER_EVENTID_BITS,
-			     its_dev->itt_cfg.event_id_bits)	|
-		  FIELD_PREP(GICV5_ITS_INV_DEVICER_L1, 0x0);
+			     event_id_bits)			|
+		  FIELD_PREP(GICV5_ITS_INV_DEVICER_L1, l1);
 
 	guard(raw_spinlock_irqsave)(&its->inv_lock);
 
@@ -403,6 +403,13 @@ static int gicv5_its_device_cache_inv(struct gicv5_its_chip_data *its,
 	return gicv5_its_cache_sync(its);
 }
 
+static int gicv5_its_device_cache_inv(struct gicv5_its_chip_data *its,
+				      struct gicv5_its_dev *its_dev)
+{
+	return __gicv5_its_device_cache_inv(its, its_dev->device_id,
+					    its_dev->itt_cfg.event_id_bits, false);
+}
+
 /*
  * Allocate a level 2 device table entry, update L1 parent to reference it.
  * Only used for 2-level device tables, and it is called on demand.
@@ -442,7 +449,12 @@ static int gicv5_its_alloc_l2_devtab(struct gicv5_its_chip_data *its,
 		FIELD_PREP(GICV5_DTL1E_VALID, 0x1);
 	its_write_table_entry(its, &l1devtab[l1_index], l1dte);
 
-	return 0;
+	/*
+	 * Invalidate any cached L1 DTE before using the new L2 table.
+	 * With L1 set, EVENTID_BITS uses the maximum ITS_IDR2.EVENTID_BITS
+	 * value regardless of the supplied value.
+	 */
+	return __gicv5_its_device_cache_inv(its, l1_index << l2_bits, 0, true);
 }
 
 static __le64 *gicv5_its_devtab_get_dte_ref(struct gicv5_its_chip_data *its,
Signature · 1 line
2.34.1