Skip to content

Commit 0297dce

Browse files
captain5050bjorn-helgaas
authored andcommitted
PCI: cadence: Avoid signed 64-bit truncation and invalid sort
The cdns_pcie_host_dma_ranges_cmp() element comparison function used by list_sort() is of type list_cmp_func_t, so it returns a 32-bit int. cdns_pcie_host_dma_ranges_cmp() computes a resource_size_t difference that may be a 64-bit value, and truncating that difference to a 32-bit return value may change the sign and result in an invalid sort order. Avoid the truncation and invalid sort order by returning -1, 0, or 1. Signed-off-by: Ian Rogers <irogers@google.com> Signed-off-by: Manivannan Sadhasivam <mani@kernel.org> [bhelgaas: commit log] Signed-off-by: Bjorn Helgaas <bhelgaas@google.com> Link: https://patch.msgid.link/20251209223756.2321578-1-irogers@google.com
1 parent 8f0b4cc commit 0297dce

1 file changed

Lines changed: 11 additions & 1 deletion

File tree

drivers/pci/controller/cadence/pcie-cadence-host-common.c

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,11 +173,21 @@ int cdns_pcie_host_dma_ranges_cmp(void *priv, const struct list_head *a,
173173
const struct list_head *b)
174174
{
175175
struct resource_entry *entry1, *entry2;
176+
u64 size1, size2;
176177

177178
entry1 = container_of(a, struct resource_entry, node);
178179
entry2 = container_of(b, struct resource_entry, node);
179180

180-
return resource_size(entry2->res) - resource_size(entry1->res);
181+
size1 = resource_size(entry1->res);
182+
size2 = resource_size(entry2->res);
183+
184+
if (size1 > size2)
185+
return -1;
186+
187+
if (size1 < size2)
188+
return 1;
189+
190+
return 0;
181191
}
182192
EXPORT_SYMBOL_GPL(cdns_pcie_host_dma_ranges_cmp);
183193

0 commit comments

Comments
 (0)