Skip to content

Commit 2a35703

Browse files
floatiousbjorn-helgaas
authored andcommitted
misc: pci_endpoint_test: Use memcpy_toio()/memcpy_fromio() for BAR tests
The current code uses writel()/readl(), which has an implicit memory barrier for every single readl()/writel(). Additionally, reading 4 bytes at a time over the PCI bus is not really optimal, considering that this code is running in an ioctl handler. Use memcpy_toio()/memcpy_fromio() for BAR tests. Before patch with a 4MB BAR: $ time /usr/bin/pcitest -b 1 BAR1: OKAY real 0m 1.56s After patch with a 4MB BAR: $ time /usr/bin/pcitest -b 1 BAR1: OKAY real 0m 0.54s Link: https://lore.kernel.org/linux-pci/[email protected] Signed-off-by: Niklas Cassel <[email protected]> Signed-off-by: Krzysztof Wilczyński <[email protected]> Signed-off-by: Bjorn Helgaas <[email protected]> Reviewed-by: Manivannan Sadhasivam <[email protected]> Reviewed-by: Kuppuswamy Sathyanarayanan <[email protected]>
1 parent 199b03d commit 2a35703

File tree

1 file changed

+42
-12
lines changed

1 file changed

+42
-12
lines changed

drivers/misc/pci_endpoint_test.c

Lines changed: 42 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
*/
88

99
#include <linux/crc32.h>
10+
#include <linux/cleanup.h>
1011
#include <linux/delay.h>
1112
#include <linux/fs.h>
1213
#include <linux/io.h>
@@ -275,31 +276,60 @@ static const u32 bar_test_pattern[] = {
275276
0xA5A5A5A5,
276277
};
277278

279+
static int pci_endpoint_test_bar_memcmp(struct pci_endpoint_test *test,
280+
enum pci_barno barno, int offset,
281+
void *write_buf, void *read_buf,
282+
int size)
283+
{
284+
memset(write_buf, bar_test_pattern[barno], size);
285+
memcpy_toio(test->bar[barno] + offset, write_buf, size);
286+
287+
memcpy_fromio(read_buf, test->bar[barno] + offset, size);
288+
289+
return memcmp(write_buf, read_buf, size);
290+
}
291+
278292
static bool pci_endpoint_test_bar(struct pci_endpoint_test *test,
279293
enum pci_barno barno)
280294
{
281-
int j;
282-
u32 val;
283-
int size;
295+
int j, bar_size, buf_size, iters, remain;
296+
void *write_buf __free(kfree) = NULL;
297+
void *read_buf __free(kfree) = NULL;
284298
struct pci_dev *pdev = test->pdev;
285299

286300
if (!test->bar[barno])
287301
return false;
288302

289-
size = pci_resource_len(pdev, barno);
303+
bar_size = pci_resource_len(pdev, barno);
290304

291305
if (barno == test->test_reg_bar)
292-
size = 0x4;
306+
bar_size = 0x4;
293307

294-
for (j = 0; j < size; j += 4)
295-
pci_endpoint_test_bar_writel(test, barno, j,
296-
bar_test_pattern[barno]);
308+
/*
309+
* Allocate a buffer of max size 1MB, and reuse that buffer while
310+
* iterating over the whole BAR size (which might be much larger).
311+
*/
312+
buf_size = min(SZ_1M, bar_size);
297313

298-
for (j = 0; j < size; j += 4) {
299-
val = pci_endpoint_test_bar_readl(test, barno, j);
300-
if (val != bar_test_pattern[barno])
314+
write_buf = kmalloc(buf_size, GFP_KERNEL);
315+
if (!write_buf)
316+
return false;
317+
318+
read_buf = kmalloc(buf_size, GFP_KERNEL);
319+
if (!read_buf)
320+
return false;
321+
322+
iters = bar_size / buf_size;
323+
for (j = 0; j < iters; j++)
324+
if (pci_endpoint_test_bar_memcmp(test, barno, buf_size * j,
325+
write_buf, read_buf, buf_size))
326+
return false;
327+
328+
remain = bar_size % buf_size;
329+
if (remain)
330+
if (pci_endpoint_test_bar_memcmp(test, barno, buf_size * iters,
331+
write_buf, read_buf, remain))
301332
return false;
302-
}
303333

304334
return true;
305335
}

0 commit comments

Comments
 (0)