Skip to contentSkip to conversation

PCI: Cadence: Avoid possible signed 64-bit truncation

PATCH Applied Ian Rogers 2 participants 2 messages Dec 9, 2025 – Dec 23, 2025

Subsystems (3)
lore
Share & tools
lore

Ian Rogers

author
#0 lore raw

PATCH 1/1 PCI: Cadence: Avoid possible signed 64-bit truncation

64-bit truncation to 32-bit can result in the sign of the truncated value changing. The cdns_pcie_host_dma_ranges_cmp is used in list_sort and so the truncation could result in an invalid sort order. This would only happen were the resource_size values large.

---
 drivers/pci/controller/cadence/pcie-cadence-host.c | 14 +++++++++++---
 1 file changed, 11 insertions(+), 3 deletions(-)
drivers/pci/controller/cadence/pcie-cadence-host.c +11 -3
index fffd63d6665e..e5fd02305ab6 100644
@@ -414,11 +414,19 @@ static int cdns_pcie_host_dma_ranges_cmp(void *priv, const struct list_head *a,
 					 const struct list_head *b)
 {
 	struct resource_entry *entry1, *entry2;
+	u64 size1, size2;
 
-        entry1 = container_of(a, struct resource_entry, node);
-        entry2 = container_of(b, struct resource_entry, node);
+	entry1 = container_of(a, struct resource_entry, node);
+	entry2 = container_of(b, struct resource_entry, node);
 
-        return resource_size(entry2->res) - resource_size(entry1->res);
+	size1 = resource_size(entry1->res);
+	size2 = resource_size(entry2->res);
+
+	if (size1 > size2)
+		return -1;
+	if (size1 < size2)
+		return 1;
+	return 0;
 }
 
 static void cdns_pcie_host_unmap_dma_ranges(struct cdns_pcie_rc *rc)
Signature · 1 line
2.52.0.223.gf5cc29aaa4-goog
maintainer Applied
#1 lore raw
Quoted 6 lines · Ian Rogers

64-bit truncation to 32-bit can result in the sign of the truncated value changing. The cdns_pcie_host_dma_ranges_cmp is used in list_sort and so the truncation could result in an invalid sort order. This would only happen were the resource_size values large.

Applied to pci/controller/cadence!

- Mani

Quoted 34 lines
---
 drivers/pci/controller/cadence/pcie-cadence-host.c | 14 +++++++++++---
 1 file changed, 11 insertions(+), 3 deletions(-)
drivers/pci/controller/cadence/pcie-cadence-host.c +11 -3
index fffd63d6665e..e5fd02305ab6 100644
@@ -414,11 +414,19 @@ static int cdns_pcie_host_dma_ranges_cmp(void *priv, const struct list_head *a,
 					 const struct list_head *b)
 {
 	struct resource_entry *entry1, *entry2;
+	u64 size1, size2;
 
-        entry1 = container_of(a, struct resource_entry, node);
-        entry2 = container_of(b, struct resource_entry, node);
+	entry1 = container_of(a, struct resource_entry, node);
+	entry2 = container_of(b, struct resource_entry, node);
 
-        return resource_size(entry2->res) - resource_size(entry1->res);
+	size1 = resource_size(entry1->res);
+	size2 = resource_size(entry2->res);
+
+	if (size1 > size2)
+		return -1;
+	if (size1 < size2)
+		return 1;
+	return 0;
 }
 
 static void cdns_pcie_host_unmap_dma_ranges(struct cdns_pcie_rc *rc)
Signature · 1 line
2.52.0.223.gf5cc29aaa4-goog
Signature · 1 line
மணிவண்ணன் சதாசிவம்