Skip to contentSkip to conversation

misc: pci_endpoint_test: Use memcpy_toio()/memcpy_fromio() for BAR tests

PATCH Superseded Niklas Cassel 3 participants 7 messages Mar 20, 2024 – Mar 22, 2024

Subsystems (3)

v3 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

PATCH v3 1/1 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

---
Changes since v2:
-Actually free the allocated memory... (thank you Kuppuswamy)

 drivers/misc/pci_endpoint_test.c | 68 ++++++++++++++++++++++++++------
 1 file changed, 55 insertions(+), 13 deletions(-)
drivers/misc/pci_endpoint_test.c +55 -13
index 705029ad8eb5..1d361589fb61 100644
@@ -272,33 +272,75 @@ static const u32 bar_test_pattern[] = {
 	0xA5A5A5A5,
 };
 
+static int pci_endpoint_test_bar_memcmp(struct pci_endpoint_test *test,
+					enum pci_barno barno, int offset,
+					void *write_buf, void *read_buf,
+					int size)
+{
+	memset(write_buf, bar_test_pattern[barno], size);
+	memcpy_toio(test->bar[barno] + offset, write_buf, size);
+
+	memcpy_fromio(read_buf, test->bar[barno] + offset, size);
+
+	return memcmp(write_buf, read_buf, size);
+}
+
 static bool pci_endpoint_test_bar(struct pci_endpoint_test *test,
 				  enum pci_barno barno)
 {
-	int j;
-	u32 val;
-	int size;
+	int j, bar_size, buf_size, iters, remain;
+	void *write_buf;
+	void *read_buf;
 	struct pci_dev *pdev = test->pdev;
+	bool ret;
 
 	if (!test->bar[barno])
 		return false;
 
-	size = pci_resource_len(pdev, barno);
+	bar_size = pci_resource_len(pdev, barno);
 
 	if (barno == test->test_reg_bar)
-		size = 0x4;
+		bar_size = 0x4;
 
-	for (j = 0; j < size; j += 4)
-		pci_endpoint_test_bar_writel(test, barno, j,
-					     bar_test_pattern[barno]);
+	buf_size = min(SZ_1M, bar_size);
 
-	for (j = 0; j < size; j += 4) {
-		val = pci_endpoint_test_bar_readl(test, barno, j);
-		if (val != bar_test_pattern[barno])
-			return false;
+	write_buf = kmalloc(buf_size, GFP_KERNEL);
+	if (!write_buf)
+		return false;
+
+	read_buf = kmalloc(buf_size, GFP_KERNEL);
+	if (!read_buf) {
+		ret = false;
+		goto err;
 	}
 
-	return true;
+	iters = bar_size / buf_size;
+	for (j = 0; j < iters; j++) {
+		if (pci_endpoint_test_bar_memcmp(test, barno, buf_size * j,
+						 write_buf, read_buf,
+						 buf_size)) {
+			ret = false;
+			goto err;
+		}
+	}
+
+	remain = bar_size % buf_size;
+	if (remain) {
+		if (pci_endpoint_test_bar_memcmp(test, barno, buf_size * iters,
+						 write_buf, read_buf,
+						 remain)) {
+			ret = false;
+			goto err;
+		}
+	}
+
+	ret = true;
+
+err:
+	kfree(write_buf);
+	kfree(read_buf);
+
+	return ret;
 }
 
 static bool pci_endpoint_test_intx_irq(struct pci_endpoint_test *test)
Signature · 1 line
2.44.0
#1 lore raw

Hi,

Quoted 63 lines · Niklas Cassel

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

---
Changes since v2:

(quoted diff) +0 -1
-Actually free the allocated memory... (thank you Kuppuswamy)
 
 drivers/misc/pci_endpoint_test.c | 68 ++++++++++++++++++++++++++------
 1 file changed, 55 insertions(+), 13 deletions(-)
drivers/misc/pci_endpoint_test.c +17 -4
index 705029ad8eb5..1d361589fb61 100644
@@ -272,33 +272,75 @@ static const u32 bar_test_pattern[] = {
 	0xA5A5A5A5,
 };
 
+static int pci_endpoint_test_bar_memcmp(struct pci_endpoint_test *test,
+					enum pci_barno barno, int offset,
+					void *write_buf, void *read_buf,
+					int size)
+{
+	memset(write_buf, bar_test_pattern[barno], size);
+	memcpy_toio(test->bar[barno] + offset, write_buf, size);
+
+	memcpy_fromio(read_buf, test->bar[barno] + offset, size);
+
+	return memcmp(write_buf, read_buf, size);
+}
+
 static bool pci_endpoint_test_bar(struct pci_endpoint_test *test,
 				  enum pci_barno barno)
 {
-	int j;
-	u32 val;
-	int size;
+	int j, bar_size, buf_size, iters, remain;
+	void *write_buf;
+	void *read_buf;
 	struct pci_dev *pdev = test->pdev;
+	bool ret;
 
 	if (!test->bar[barno])
 		return false;
 
-	size = pci_resource_len(pdev, barno);

drivers/misc/pci_endpoint_test.c static const u32 bar_test_pattern[] = { · line 305

Quoted 10 lines
drivers/misc/pci_endpoint_test.c +3 -4
+	bar_size = pci_resource_len(pdev, barno);
 
 	if (barno == test->test_reg_bar)
-		size = 0x4;
+		bar_size = 0x4;
 
-	for (j = 0; j < size; j += 4)
-		pci_endpoint_test_bar_writel(test, barno, j,
-					     bar_test_pattern[barno]);
+	buf_size = min(SZ_1M, bar_size);

Why 1MB  limit?

Quoted 46 lines
drivers/misc/pci_endpoint_test.c +35 -5
-	for (j = 0; j < size; j += 4) {
-		val = pci_endpoint_test_bar_readl(test, barno, j);
-		if (val != bar_test_pattern[barno])
-			return false;
+	write_buf = kmalloc(buf_size, GFP_KERNEL);
+	if (!write_buf)
+		return false;
+
+	read_buf = kmalloc(buf_size, GFP_KERNEL);
+	if (!read_buf) {
+		ret = false;
+		goto err;
 	}
 
-	return true;
+	iters = bar_size / buf_size;
+	for (j = 0; j < iters; j++) {
+		if (pci_endpoint_test_bar_memcmp(test, barno, buf_size * j,
+						 write_buf, read_buf,
+						 buf_size)) {
+			ret = false;
+			goto err;
+		}
+	}
+
+	remain = bar_size % buf_size;
+	if (remain) {
+		if (pci_endpoint_test_bar_memcmp(test, barno, buf_size * iters,
+						 write_buf, read_buf,
+						 remain)) {
+			ret = false;
+			goto err;
+		}
+	}
+
+	ret = true;
+
+err:
+	kfree(write_buf);
+	kfree(read_buf);
+
+	return ret;
 }
 
 static bool pci_endpoint_test_intx_irq(struct pci_endpoint_test *test)
Signature · 2 lines
Sathyanarayanan Kuppuswamy
Linux Kernel Developer
#2 lore raw

Hello Kuppuswamy,

Quoted 72 lines · Kuppuswamy Sathyanarayanan

Hi,

Quoted 69 lines · Niklas Cassel

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

---
Changes since v2:

(quoted diff) +0 -1
-Actually free the allocated memory... (thank you Kuppuswamy)
 
 drivers/misc/pci_endpoint_test.c | 68 ++++++++++++++++++++++++++------
 1 file changed, 55 insertions(+), 13 deletions(-)
drivers/misc/pci_endpoint_test.c +19 -5
index 705029ad8eb5..1d361589fb61 100644
@@ -272,33 +272,75 @@ static const u32 bar_test_pattern[] = {
 	0xA5A5A5A5,
 };
 
+static int pci_endpoint_test_bar_memcmp(struct pci_endpoint_test *test,
+					enum pci_barno barno, int offset,
+					void *write_buf, void *read_buf,
+					int size)
+{
+	memset(write_buf, bar_test_pattern[barno], size);
+	memcpy_toio(test->bar[barno] + offset, write_buf, size);
+
+	memcpy_fromio(read_buf, test->bar[barno] + offset, size);
+
+	return memcmp(write_buf, read_buf, size);
+}
+
 static bool pci_endpoint_test_bar(struct pci_endpoint_test *test,
 				  enum pci_barno barno)
 {
-	int j;
-	u32 val;
-	int size;
+	int j, bar_size, buf_size, iters, remain;
+	void *write_buf;
+	void *read_buf;
 	struct pci_dev *pdev = test->pdev;
+	bool ret;
 
 	if (!test->bar[barno])
 		return false;
 
-	size = pci_resource_len(pdev, barno);
+	bar_size = pci_resource_len(pdev, barno);
 
 	if (barno == test->test_reg_bar)
-		size = 0x4;
+		bar_size = 0x4;
 
Quoted 6 lines

drivers/misc/pci_endpoint_test.c static const u32 bar_test_pattern[] = { · line 305

Quoted 4 lines
drivers/misc/pci_endpoint_test.c +1 -3
-	for (j = 0; j < size; j += 4)
-		pci_endpoint_test_bar_writel(test, barno, j,
-					     bar_test_pattern[barno]);
+	buf_size = min(SZ_1M, bar_size);

Why 1MB  limit?

Could you please clarify your concern?

A BAR could be several GB, so it does not make sense to always kmalloc() a buffer that is of the same size of the BAR.
(Therefore we copy in to a smaller buffer, iterating over the whole BAR.)

So we have to chose a max limit that we think is likely to succeed even when the memory is fragmented, and something that will work on embedded systems, etc.

The highest BAR size used by pci-epf-test is by default 1MB, so 1MB seemed like a reasonable max limit. (Since we use min(), if the BAR is smaller than 1MB, the buffer we allocate will also be smaller than 1MB.

Since we allocate two buffers, we are in the worst case allocating 2x 1MB, so I don't think that it is reasonable to have a higher max limit.

If you are using a _very_ resource contained system as RC (and EP) to test the pci-epf-test driver, you have probably reduced the default BAR sizes defined in pci-epf-test to something smaller already, so 1MB seemed like a reasonable max limit.

Kind regards,
Niklas

Quoted 52 lines
Quoted 47 lines
drivers/misc/pci_endpoint_test.c +35 -5
-	for (j = 0; j < size; j += 4) {
-		val = pci_endpoint_test_bar_readl(test, barno, j);
-		if (val != bar_test_pattern[barno])
-			return false;
+	write_buf = kmalloc(buf_size, GFP_KERNEL);
+	if (!write_buf)
+		return false;
+
+	read_buf = kmalloc(buf_size, GFP_KERNEL);
+	if (!read_buf) {
+		ret = false;
+		goto err;
 	}
 
-	return true;
+	iters = bar_size / buf_size;
+	for (j = 0; j < iters; j++) {
+		if (pci_endpoint_test_bar_memcmp(test, barno, buf_size * j,
+						 write_buf, read_buf,
+						 buf_size)) {
+			ret = false;
+			goto err;
+		}
+	}
+
+	remain = bar_size % buf_size;
+	if (remain) {
+		if (pci_endpoint_test_bar_memcmp(test, barno, buf_size * iters,
+						 write_buf, read_buf,
+						 remain)) {
+			ret = false;
+			goto err;
+		}
+	}
+
+	ret = true;
+
+err:
+	kfree(write_buf);
+	kfree(read_buf);
+
+	return ret;
 }
 
 static bool pci_endpoint_test_intx_irq(struct pci_endpoint_test *test)
Signature · 2 lines
Sathyanarayanan Kuppuswamy
Linux Kernel Developer
#3 lore raw
Quoted 75 lines · Niklas Cassel

Hello Kuppuswamy,

Quoted 72 lines · Kuppuswamy Sathyanarayanan

Hi,

Quoted 69 lines · Niklas Cassel

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

---
Changes since v2:

(quoted diff) +0 -1
-Actually free the allocated memory... (thank you Kuppuswamy)
 
 drivers/misc/pci_endpoint_test.c | 68 ++++++++++++++++++++++++++------
 1 file changed, 55 insertions(+), 13 deletions(-)
drivers/misc/pci_endpoint_test.c +19 -5
index 705029ad8eb5..1d361589fb61 100644
@@ -272,33 +272,75 @@ static const u32 bar_test_pattern[] = {
 	0xA5A5A5A5,
 };
 
+static int pci_endpoint_test_bar_memcmp(struct pci_endpoint_test *test,
+					enum pci_barno barno, int offset,
+					void *write_buf, void *read_buf,
+					int size)
+{
+	memset(write_buf, bar_test_pattern[barno], size);
+	memcpy_toio(test->bar[barno] + offset, write_buf, size);
+
+	memcpy_fromio(read_buf, test->bar[barno] + offset, size);
+
+	return memcmp(write_buf, read_buf, size);
+}
+
 static bool pci_endpoint_test_bar(struct pci_endpoint_test *test,
 				  enum pci_barno barno)
 {
-	int j;
-	u32 val;
-	int size;
+	int j, bar_size, buf_size, iters, remain;
+	void *write_buf;
+	void *read_buf;
 	struct pci_dev *pdev = test->pdev;
+	bool ret;
 
 	if (!test->bar[barno])
 		return false;
 
-	size = pci_resource_len(pdev, barno);
+	bar_size = pci_resource_len(pdev, barno);
 
 	if (barno == test->test_reg_bar)
-		size = 0x4;
+		bar_size = 0x4;
 
Quoted 6 lines
Quoted 5 lines

drivers/misc/pci_endpoint_test.c static const u32 bar_test_pattern[] = { · line 305

Quoted 4 lines
drivers/misc/pci_endpoint_test.c +1 -3
-	for (j = 0; j < size; j += 4)
-		pci_endpoint_test_bar_writel(test, barno, j,
-					     bar_test_pattern[barno]);
+	buf_size = min(SZ_1M, bar_size);

Why 1MB  limit?

Could you please clarify your concern?

Since you are trying to optimize the number of read/write calls, I was just wondering why you chose maximum limit of 1MB per read/write call.  But your following explanation makes sense to me. I recommend adding some comments about it in commit log or code.

Code wise, your change looks fine to me.

Quoted 75 lines

A BAR could be several GB, so it does not make sense to always kmalloc() a buffer that is of the same size of the BAR.
(Therefore we copy in to a smaller buffer, iterating over the whole BAR.)

So we have to chose a max limit that we think is likely to succeed even when the memory is fragmented, and something that will work on embedded systems, etc.

The highest BAR size used by pci-epf-test is by default 1MB, so 1MB seemed like a reasonable max limit. (Since we use min(), if the BAR is smaller than 1MB, the buffer we allocate will also be smaller than 1MB.

Since we allocate two buffers, we are in the worst case allocating 2x 1MB, so I don't think that it is reasonable to have a higher max limit.

If you are using a _very_ resource contained system as RC (and EP) to test the pci-epf-test driver, you have probably reduced the default BAR sizes defined in pci-epf-test to something smaller already, so 1MB seemed like a reasonable max limit.

Kind regards,
Niklas

Quoted 50 lines
Quoted 46 lines
drivers/misc/pci_endpoint_test.c +35 -5
-	for (j = 0; j < size; j += 4) {
-		val = pci_endpoint_test_bar_readl(test, barno, j);
-		if (val != bar_test_pattern[barno])
-			return false;
+	write_buf = kmalloc(buf_size, GFP_KERNEL);
+	if (!write_buf)
+		return false;
+
+	read_buf = kmalloc(buf_size, GFP_KERNEL);
+	if (!read_buf) {
+		ret = false;
+		goto err;
 	}
 
-	return true;
+	iters = bar_size / buf_size;
+	for (j = 0; j < iters; j++) {
+		if (pci_endpoint_test_bar_memcmp(test, barno, buf_size * j,
+						 write_buf, read_buf,
+						 buf_size)) {
+			ret = false;
+			goto err;
+		}
+	}
+
+	remain = bar_size % buf_size;
+	if (remain) {
+		if (pci_endpoint_test_bar_memcmp(test, barno, buf_size * iters,
+						 write_buf, read_buf,
+						 remain)) {
+			ret = false;
+			goto err;
+		}
+	}
+
+	ret = true;
+
+err:
+	kfree(write_buf);
+	kfree(read_buf);
+
+	return ret;
 }
 
 static bool pci_endpoint_test_intx_irq(struct pci_endpoint_test *test)
Signature · 2 lines
Sathyanarayanan Kuppuswamy
Linux Kernel Developer
Signature · 2 lines
Sathyanarayanan Kuppuswamy
Linux Kernel Developer
#4 lore raw
In reply to Niklas Cassel
Quoted 76 lines · Niklas Cassel

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

---
Changes since v2:

(quoted diff) +0 -1
-Actually free the allocated memory... (thank you Kuppuswamy)
 
 drivers/misc/pci_endpoint_test.c | 68 ++++++++++++++++++++++++++------
 1 file changed, 55 insertions(+), 13 deletions(-)
drivers/misc/pci_endpoint_test.c +20 -10
index 705029ad8eb5..1d361589fb61 100644
@@ -272,33 +272,75 @@ static const u32 bar_test_pattern[] = {
 	0xA5A5A5A5,
 };
 
+static int pci_endpoint_test_bar_memcmp(struct pci_endpoint_test *test,
+					enum pci_barno barno, int offset,
+					void *write_buf, void *read_buf,
+					int size)
+{
+	memset(write_buf, bar_test_pattern[barno], size);
+	memcpy_toio(test->bar[barno] + offset, write_buf, size);
+
+	memcpy_fromio(read_buf, test->bar[barno] + offset, size);
+
+	return memcmp(write_buf, read_buf, size);
+}
+
 static bool pci_endpoint_test_bar(struct pci_endpoint_test *test,
 				  enum pci_barno barno)
 {
-	int j;
-	u32 val;
-	int size;
+	int j, bar_size, buf_size, iters, remain;
+	void *write_buf;
+	void *read_buf;
 	struct pci_dev *pdev = test->pdev;
+	bool ret;
 
 	if (!test->bar[barno])
 		return false;
 
-	size = pci_resource_len(pdev, barno);
+	bar_size = pci_resource_len(pdev, barno);
 
 	if (barno == test->test_reg_bar)
-		size = 0x4;
+		bar_size = 0x4;
 
-	for (j = 0; j < size; j += 4)
-		pci_endpoint_test_bar_writel(test, barno, j,
-					     bar_test_pattern[barno]);
+	buf_size = min(SZ_1M, bar_size);
 
-	for (j = 0; j < size; j += 4) {
-		val = pci_endpoint_test_bar_readl(test, barno, j);

drivers/misc/pci_endpoint_test.c static const u32 bar_test_pattern[] = { · line 314

Quoted 10 lines
drivers/misc/pci_endpoint_test.c +8 -2
-		if (val != bar_test_pattern[barno])
-			return false;
+	write_buf = kmalloc(buf_size, GFP_KERNEL);
+	if (!write_buf)
+		return false;
+
+	read_buf = kmalloc(buf_size, GFP_KERNEL);
+	if (!read_buf) {
+		ret = false;
+		goto err;

This frees read_buf also. Please fix that and also rename the labels to:

err_free_write_buf
err_free_read_buf

- Mani

Quoted 36 lines
drivers/misc/pci_endpoint_test.c +27 -1
 	}
 
-	return true;
+	iters = bar_size / buf_size;
+	for (j = 0; j < iters; j++) {
+		if (pci_endpoint_test_bar_memcmp(test, barno, buf_size * j,
+						 write_buf, read_buf,
+						 buf_size)) {
+			ret = false;
+			goto err;
+		}
+	}
+
+	remain = bar_size % buf_size;
+	if (remain) {
+		if (pci_endpoint_test_bar_memcmp(test, barno, buf_size * iters,
+						 write_buf, read_buf,
+						 remain)) {
+			ret = false;
+			goto err;
+		}
+	}
+
+	ret = true;
+
+err:
+	kfree(write_buf);
+	kfree(read_buf);
+
+	return ret;
 }
 
 static bool pci_endpoint_test_intx_irq(struct pci_endpoint_test *test)
Signature · 1 line
2.44.0
Signature · 1 line
மணிவண்ணன் சதாசிவம்
#5 lore raw

Hello Mani,

Quoted 90 lines · Manivannan Sadhasivam
Quoted 76 lines · Niklas Cassel

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

---
Changes since v2:

(quoted diff) +0 -1
-Actually free the allocated memory... (thank you Kuppuswamy)
 
 drivers/misc/pci_endpoint_test.c | 68 ++++++++++++++++++++++++++------
 1 file changed, 55 insertions(+), 13 deletions(-)
drivers/misc/pci_endpoint_test.c +20 -10
index 705029ad8eb5..1d361589fb61 100644
@@ -272,33 +272,75 @@ static const u32 bar_test_pattern[] = {
 	0xA5A5A5A5,
 };
 
+static int pci_endpoint_test_bar_memcmp(struct pci_endpoint_test *test,
+					enum pci_barno barno, int offset,
+					void *write_buf, void *read_buf,
+					int size)
+{
+	memset(write_buf, bar_test_pattern[barno], size);
+	memcpy_toio(test->bar[barno] + offset, write_buf, size);
+
+	memcpy_fromio(read_buf, test->bar[barno] + offset, size);
+
+	return memcmp(write_buf, read_buf, size);
+}
+
 static bool pci_endpoint_test_bar(struct pci_endpoint_test *test,
 				  enum pci_barno barno)
 {
-	int j;
-	u32 val;
-	int size;
+	int j, bar_size, buf_size, iters, remain;
+	void *write_buf;
+	void *read_buf;
 	struct pci_dev *pdev = test->pdev;
+	bool ret;
 
 	if (!test->bar[barno])
 		return false;
 
-	size = pci_resource_len(pdev, barno);
+	bar_size = pci_resource_len(pdev, barno);
 
 	if (barno == test->test_reg_bar)
-		size = 0x4;
+		bar_size = 0x4;
 
-	for (j = 0; j < size; j += 4)
-		pci_endpoint_test_bar_writel(test, barno, j,
-					     bar_test_pattern[barno]);
+	buf_size = min(SZ_1M, bar_size);
 
-	for (j = 0; j < size; j += 4) {
-		val = pci_endpoint_test_bar_readl(test, barno, j);

drivers/misc/pci_endpoint_test.c static const u32 bar_test_pattern[] = { · line 314

Quoted 10 lines
drivers/misc/pci_endpoint_test.c +8 -2
-		if (val != bar_test_pattern[barno])
-			return false;
+	write_buf = kmalloc(buf_size, GFP_KERNEL);
+	if (!write_buf)
+		return false;
+
+	read_buf = kmalloc(buf_size, GFP_KERNEL);
+	if (!read_buf) {
+		ret = false;
+		goto err;

This frees read_buf also. Please fix that and also rename the labels to:

Quoted 2 lines

err_free_write_buf
err_free_read_buf

This was intentional since kfree() handles NULL perfectly fine. (I was thinking that it would just add extra lines for no good reason.)

Do you think that there is any point in having two labels in this case?

Kind regards,
Niklas

Quoted 42 lines

- Mani

Quoted 36 lines
drivers/misc/pci_endpoint_test.c +27 -1
 	}
 
-	return true;
+	iters = bar_size / buf_size;
+	for (j = 0; j < iters; j++) {
+		if (pci_endpoint_test_bar_memcmp(test, barno, buf_size * j,
+						 write_buf, read_buf,
+						 buf_size)) {
+			ret = false;
+			goto err;
+		}
+	}
+
+	remain = bar_size % buf_size;
+	if (remain) {
+		if (pci_endpoint_test_bar_memcmp(test, barno, buf_size * iters,
+						 write_buf, read_buf,
+						 remain)) {
+			ret = false;
+			goto err;
+		}
+	}
+
+	ret = true;
+
+err:
+	kfree(write_buf);
+	kfree(read_buf);
+
+	return ret;
 }
 
 static bool pci_endpoint_test_intx_irq(struct pci_endpoint_test *test)
Signature · 1 line
2.44.0
Signature · 1 line
மணிவண்ணன் சதாசிவம்
#6 lore raw
Quoted 93 lines · Niklas Cassel

Hello Mani,

Quoted 90 lines · Manivannan Sadhasivam
Quoted 76 lines · Niklas Cassel

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

---
Changes since v2:

(quoted diff) +0 -1
-Actually free the allocated memory... (thank you Kuppuswamy)
 
 drivers/misc/pci_endpoint_test.c | 68 ++++++++++++++++++++++++++------
 1 file changed, 55 insertions(+), 13 deletions(-)
drivers/misc/pci_endpoint_test.c +20 -10
index 705029ad8eb5..1d361589fb61 100644
@@ -272,33 +272,75 @@ static const u32 bar_test_pattern[] = {
 	0xA5A5A5A5,
 };
 
+static int pci_endpoint_test_bar_memcmp(struct pci_endpoint_test *test,
+					enum pci_barno barno, int offset,
+					void *write_buf, void *read_buf,
+					int size)
+{
+	memset(write_buf, bar_test_pattern[barno], size);
+	memcpy_toio(test->bar[barno] + offset, write_buf, size);
+
+	memcpy_fromio(read_buf, test->bar[barno] + offset, size);
+
+	return memcmp(write_buf, read_buf, size);
+}
+
 static bool pci_endpoint_test_bar(struct pci_endpoint_test *test,
 				  enum pci_barno barno)
 {
-	int j;
-	u32 val;
-	int size;
+	int j, bar_size, buf_size, iters, remain;
+	void *write_buf;
+	void *read_buf;
 	struct pci_dev *pdev = test->pdev;
+	bool ret;
 
 	if (!test->bar[barno])
 		return false;
 
-	size = pci_resource_len(pdev, barno);
+	bar_size = pci_resource_len(pdev, barno);
 
 	if (barno == test->test_reg_bar)
-		size = 0x4;
+		bar_size = 0x4;
 
-	for (j = 0; j < size; j += 4)
-		pci_endpoint_test_bar_writel(test, barno, j,
-					     bar_test_pattern[barno]);
+	buf_size = min(SZ_1M, bar_size);
 
-	for (j = 0; j < size; j += 4) {
-		val = pci_endpoint_test_bar_readl(test, barno, j);

drivers/misc/pci_endpoint_test.c static const u32 bar_test_pattern[] = { · line 314

Quoted 10 lines
drivers/misc/pci_endpoint_test.c +8 -2
-		if (val != bar_test_pattern[barno])
-			return false;
+	write_buf = kmalloc(buf_size, GFP_KERNEL);
+	if (!write_buf)
+		return false;
+
+	read_buf = kmalloc(buf_size, GFP_KERNEL);
+	if (!read_buf) {
+		ret = false;
+		goto err;

This frees read_buf also. Please fix that and also rename the labels to:

Quoted 8 lines
Quoted 2 lines

err_free_write_buf
err_free_read_buf

This was intentional since kfree() handles NULL perfectly fine. (I was thinking that it would just add extra lines for no good reason.)

Do you think that there is any point in having two labels in this case?

I know that NULL is ignored, but that's not an excuse to call kfree() for non-existent memory (for code sanity). Please create separate labels.

- Mani

Quoted 46 lines

Kind regards,
Niklas

Quoted 42 lines

- Mani

Quoted 36 lines
drivers/misc/pci_endpoint_test.c +27 -1
 	}
 
-	return true;
+	iters = bar_size / buf_size;
+	for (j = 0; j < iters; j++) {
+		if (pci_endpoint_test_bar_memcmp(test, barno, buf_size * j,
+						 write_buf, read_buf,
+						 buf_size)) {
+			ret = false;
+			goto err;
+		}
+	}
+
+	remain = bar_size % buf_size;
+	if (remain) {
+		if (pci_endpoint_test_bar_memcmp(test, barno, buf_size * iters,
+						 write_buf, read_buf,
+						 remain)) {
+			ret = false;
+			goto err;
+		}
+	}
+
+	ret = true;
+
+err:
+	kfree(write_buf);
+	kfree(read_buf);
+
+	return ret;
 }
 
 static bool pci_endpoint_test_intx_irq(struct pci_endpoint_test *test)
Signature · 1 line
2.44.0
Signature · 1 line
மணிவண்ணன் சதாசிவம்
Signature · 1 line
மணிவண்ணன் சதாசிவம்