Skip to content

Commit 7962c82

Browse files
floatiouskwilczynski
authored andcommitted
misc: pci_endpoint_test: Handle BAR sizes larger than INT_MAX
Running 'pcitest -b 0' fails with "TEST FAILED" when the BAR0 size is e.g. 8 GB. The return value of the pci_resource_len() macro can be larger than that of a signed integer type. Thus, when using 'pcitest' with an 8 GB BAR, the bar_size of the integer type will overflow. Change bar_size from integer to resource_size_t to prevent integer overflow for large BAR sizes with 32-bit compilers. In order to handle 64-bit resource_type_t on 32-bit platforms, we would have needed to use a function like div_u64() or similar. Instead, change the code to use addition instead of division. This avoids the need for div_u64() or similar, while also simplifying the code. Fixes: cda370e ("misc: pci_endpoint_test: Avoid using hard-coded BAR sizes") Co-developed-by: Hans Zhang <[email protected]> Signed-off-by: Hans Zhang <[email protected]> Signed-off-by: Niklas Cassel <[email protected]> Reviewed-by: Manivannan Sadhasivam <[email protected]> Tested-by: Jon Hunter <[email protected]> Reviewed-by: Frank Li <[email protected]> Link: https://lore.kernel.org/r/[email protected] [mani: added fixes tag] Signed-off-by: Manivannan Sadhasivam <[email protected]> Signed-off-by: Krzysztof Wilczyński <[email protected]>
1 parent 7e80bbe commit 7962c82

File tree

1 file changed

+10
-8
lines changed

1 file changed

+10
-8
lines changed

drivers/misc/pci_endpoint_test.c

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -272,9 +272,9 @@ static const u32 bar_test_pattern[] = {
272272
};
273273

274274
static int pci_endpoint_test_bar_memcmp(struct pci_endpoint_test *test,
275-
enum pci_barno barno, int offset,
276-
void *write_buf, void *read_buf,
277-
int size)
275+
enum pci_barno barno,
276+
resource_size_t offset, void *write_buf,
277+
void *read_buf, int size)
278278
{
279279
memset(write_buf, bar_test_pattern[barno], size);
280280
memcpy_toio(test->bar[barno] + offset, write_buf, size);
@@ -287,10 +287,11 @@ static int pci_endpoint_test_bar_memcmp(struct pci_endpoint_test *test,
287287
static int pci_endpoint_test_bar(struct pci_endpoint_test *test,
288288
enum pci_barno barno)
289289
{
290-
int j, bar_size, buf_size, iters;
290+
resource_size_t bar_size, offset = 0;
291291
void *write_buf __free(kfree) = NULL;
292292
void *read_buf __free(kfree) = NULL;
293293
struct pci_dev *pdev = test->pdev;
294+
int buf_size;
294295

295296
bar_size = pci_resource_len(pdev, barno);
296297
if (!bar_size)
@@ -316,11 +317,12 @@ static int pci_endpoint_test_bar(struct pci_endpoint_test *test,
316317
if (!read_buf)
317318
return -ENOMEM;
318319

319-
iters = bar_size / buf_size;
320-
for (j = 0; j < iters; j++)
321-
if (pci_endpoint_test_bar_memcmp(test, barno, buf_size * j,
322-
write_buf, read_buf, buf_size))
320+
while (offset < bar_size) {
321+
if (pci_endpoint_test_bar_memcmp(test, barno, offset, write_buf,
322+
read_buf, buf_size))
323323
return -EIO;
324+
offset += buf_size;
325+
}
324326

325327
return 0;
326328
}

0 commit comments

Comments
 (0)