Skip to contentSkip to conversation

PCI/VPD: Use unaligned access helper in pci_vpd_write

PATCH In review Heiner Kallweit 2 participants 5 messages Apr 18, 2021 – Apr 30, 2021 PCI

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 PCI/VPD: Use unaligned access helper in pci_vpd_write

Use helper get_unaligned_le32() to simplify the code.

---
 drivers/pci/vpd.c | 15 +++++----------
 1 file changed, 5 insertions(+), 10 deletions(-)
drivers/pci/vpd.c +5 -10
index 60573f27a..2888bb300 100644
@@ -9,6 +9,7 @@
 #include <linux/delay.h>
 #include <linux/export.h>
 #include <linux/sched/signal.h>
+#include <asm/unaligned.h>
 #include "pci.h"
 
 /* VPD access through PCI 2.2+ VPD capability */
@@ -235,10 +236,9 @@ static ssize_t pci_vpd_read(struct pci_dev *dev, loff_t pos, size_t count,
 }
 
 static ssize_t pci_vpd_write(struct pci_dev *dev, loff_t pos, size_t count,
-			     const void *arg)
+			     const void *buf)
 {
 	struct pci_vpd *vpd = dev->vpd;
-	const u8 *buf = arg;
 	loff_t end = pos + count;
 	int ret = 0;
 
@@ -264,14 +264,8 @@ static ssize_t pci_vpd_write(struct pci_dev *dev, loff_t pos, size_t count,
 		goto out;
 
 	while (pos < end) {
-		u32 val;
-
-		val = *buf++;
-		val |= *buf++ << 8;
-		val |= *buf++ << 16;
-		val |= *buf++ << 24;
-
-		ret = pci_user_write_config_dword(dev, vpd->cap + PCI_VPD_DATA, val);
+		ret = pci_user_write_config_dword(dev, vpd->cap + PCI_VPD_DATA,
+						  get_unaligned_le32(buf));
 		if (ret < 0)
 			break;
 		ret = pci_user_write_config_word(dev, vpd->cap + PCI_VPD_ADDR,
@@ -286,6 +280,7 @@ static ssize_t pci_vpd_write(struct pci_dev *dev, loff_t pos, size_t count,
 			break;
 
 		pos += sizeof(u32);
+		buf += sizeof(u32);
 	}
 out:
 	mutex_unlock(&vpd->lock);
Signature · 1 line
2.31.1
#1 lore raw
Quoted 50 lines · Heiner Kallweit

Use helper get_unaligned_le32() to simplify the code.

---
 drivers/pci/vpd.c | 15 +++++----------
 1 file changed, 5 insertions(+), 10 deletions(-)
drivers/pci/vpd.c +4 -10
index 60573f27a..2888bb300 100644
@@ -9,6 +9,7 @@
 #include <linux/delay.h>
 #include <linux/export.h>
 #include <linux/sched/signal.h>
+#include <asm/unaligned.h>
 #include "pci.h"
 
 /* VPD access through PCI 2.2+ VPD capability */
@@ -235,10 +236,9 @@ static ssize_t pci_vpd_read(struct pci_dev *dev, loff_t pos, size_t count,
 }
 
 static ssize_t pci_vpd_write(struct pci_dev *dev, loff_t pos, size_t count,
-			     const void *arg)
+			     const void *buf)
 {
 	struct pci_vpd *vpd = dev->vpd;
-	const u8 *buf = arg;
 	loff_t end = pos + count;
 	int ret = 0;
 
@@ -264,14 +264,8 @@ static ssize_t pci_vpd_write(struct pci_dev *dev, loff_t pos, size_t count,
 		goto out;
 
 	while (pos < end) {
-		u32 val;
-
-		val = *buf++;
-		val |= *buf++ << 8;
-		val |= *buf++ << 16;
-		val |= *buf++ << 24;
-
-		ret = pci_user_write_config_dword(dev, vpd->cap + PCI_VPD_DATA, val);
+		ret = pci_user_write_config_dword(dev, vpd->cap + PCI_VPD_DATA,
+						  get_unaligned_le32(buf));
 		if (ret < 0)
 			break;
 		ret = pci_user_write_config_word(dev, vpd->cap + PCI_VPD_ADDR,

drivers/pci/vpd.c static ssize_t pci_vpd_write(struct pci_dev *dev, loff_t pos, size_t count, · line 286

Quoted 7 lines
drivers/pci/vpd.c +1 -0
@@ -286,6 +280,7 @@ static ssize_t pci_vpd_write(struct pci_dev *dev, loff_t pos, size_t count,
 			break;
 
 		pos += sizeof(u32);
+		buf += sizeof(u32);
 	}
 out:
 	mutex_unlock(&vpd->lock);

Please disregard this patch. According to PCI 2.2 spec the address needs to be dword-aligned. I will address this in a separate patch.

#2 lore raw
Quoted 52 lines · Heiner Kallweit
Quoted 51 lines · Heiner Kallweit

Use helper get_unaligned_le32() to simplify the code.

---
 drivers/pci/vpd.c | 15 +++++----------
 1 file changed, 5 insertions(+), 10 deletions(-)
drivers/pci/vpd.c +4 -10
index 60573f27a..2888bb300 100644
@@ -9,6 +9,7 @@
 #include <linux/delay.h>
 #include <linux/export.h>
 #include <linux/sched/signal.h>
+#include <asm/unaligned.h>
 #include "pci.h"
 
 /* VPD access through PCI 2.2+ VPD capability */
@@ -235,10 +236,9 @@ static ssize_t pci_vpd_read(struct pci_dev *dev, loff_t pos, size_t count,
 }
 
 static ssize_t pci_vpd_write(struct pci_dev *dev, loff_t pos, size_t count,
-			     const void *arg)
+			     const void *buf)
 {
 	struct pci_vpd *vpd = dev->vpd;
-	const u8 *buf = arg;
 	loff_t end = pos + count;
 	int ret = 0;
 
@@ -264,14 +264,8 @@ static ssize_t pci_vpd_write(struct pci_dev *dev, loff_t pos, size_t count,
 		goto out;
 
 	while (pos < end) {
-		u32 val;
-
-		val = *buf++;
-		val |= *buf++ << 8;
-		val |= *buf++ << 16;
-		val |= *buf++ << 24;
-
-		ret = pci_user_write_config_dword(dev, vpd->cap + PCI_VPD_DATA, val);
+		ret = pci_user_write_config_dword(dev, vpd->cap + PCI_VPD_DATA,
+						  get_unaligned_le32(buf));
 		if (ret < 0)
 			break;
 		ret = pci_user_write_config_word(dev, vpd->cap + PCI_VPD_ADDR,
@@ -286,6 +280,7 @@ static ssize_t pci_vpd_write(struct pci_dev *dev, loff_t pos, size_t count,
 			break;
 
Quoted 10 lines

drivers/pci/vpd.c static ssize_t pci_vpd_write(struct pci_dev *dev, loff_t pos, size_t count, · line 286

Quoted 6 lines
drivers/pci/vpd.c +1 -0
 		pos += sizeof(u32);
+		buf += sizeof(u32);
 	}
 out:
 	mutex_unlock(&vpd->lock);

Please disregard this patch. According to PCI 2.2 spec the address needs to be dword-aligned. I will address this in a separate patch.

Sorry, I just confused something and wrote before finished thinking. The change is correct.

#3 lore raw
In reply to Heiner Kallweit
Quoted 35 lines · Heiner Kallweit

Use helper get_unaligned_le32() to simplify the code.

---
 drivers/pci/vpd.c | 15 +++++----------
 1 file changed, 5 insertions(+), 10 deletions(-)
drivers/pci/vpd.c +2 -2
index 60573f27a..2888bb300 100644
@@ -9,6 +9,7 @@
 #include <linux/delay.h>
 #include <linux/export.h>
 #include <linux/sched/signal.h>
+#include <asm/unaligned.h>
 #include "pci.h"
 
 /* VPD access through PCI 2.2+ VPD capability */
@@ -235,10 +236,9 @@ static ssize_t pci_vpd_read(struct pci_dev *dev, loff_t pos, size_t count,
 }
 
 static ssize_t pci_vpd_write(struct pci_dev *dev, loff_t pos, size_t count,
-			     const void *arg)
+			     const void *buf)
 {
 	struct pci_vpd *vpd = dev->vpd;
-	const u8 *buf = arg;
 	loff_t end = pos + count;
 	int ret = 0;
 
@@ -264,14 +264,8 @@ static ssize_t pci_vpd_write(struct pci_dev *dev, loff_t pos, size_t count,
 		goto out;
 
 	while (pos < end) {

drivers/pci/vpd.c static ssize_t pci_vpd_write(struct pci_dev *dev, loff_t pos, size_t count, · line 268

Quoted 10 lines
drivers/pci/vpd.c +2 -8
-		u32 val;
-
-		val = *buf++;
-		val |= *buf++ << 8;
-		val |= *buf++ << 16;
-		val |= *buf++ << 24;
-
-		ret = pci_user_write_config_dword(dev, vpd->cap + PCI_VPD_DATA, val);
+		ret = pci_user_write_config_dword(dev, vpd->cap + PCI_VPD_DATA,
+						  get_unaligned_le32(buf));

If I understand correctly, get_unaligned_le32() is equivalent to the open-coded "val = *buf++; ..." In other words, this doesn't fix a bug on any platforms, and it doesn't change the bits written to VPD. Right?

If so, this makes sense. I'd like it better if we could also make pci_vpd_read() look more symmetric, since it has similar byte order-related code. The read side is a little more complicated since the size need not be 4-byte aligned. But maybe there's a way? Seems like there should be other examples of this sort of thing, but my quick look didn't find one.

Quoted 14 lines
drivers/pci/vpd.c +1 -0
 		if (ret < 0)
 			break;
 		ret = pci_user_write_config_word(dev, vpd->cap + PCI_VPD_ADDR,
@@ -286,6 +280,7 @@ static ssize_t pci_vpd_write(struct pci_dev *dev, loff_t pos, size_t count,
 			break;
 
 		pos += sizeof(u32);
+		buf += sizeof(u32);
 	}
 out:
 	mutex_unlock(&vpd->lock);
Signature · 1 line
2.31.1
#4 lore raw
Quoted 35 lines · Bjorn Helgaas
Quoted 34 lines · Heiner Kallweit

Use helper get_unaligned_le32() to simplify the code.

---
 drivers/pci/vpd.c | 15 +++++----------
 1 file changed, 5 insertions(+), 10 deletions(-)
drivers/pci/vpd.c +2 -2
index 60573f27a..2888bb300 100644
@@ -9,6 +9,7 @@
 #include <linux/delay.h>
 #include <linux/export.h>
 #include <linux/sched/signal.h>
+#include <asm/unaligned.h>
 #include "pci.h"
 
 /* VPD access through PCI 2.2+ VPD capability */
@@ -235,10 +236,9 @@ static ssize_t pci_vpd_read(struct pci_dev *dev, loff_t pos, size_t count,
 }
 
 static ssize_t pci_vpd_write(struct pci_dev *dev, loff_t pos, size_t count,
-			     const void *arg)
+			     const void *buf)
 {
 	struct pci_vpd *vpd = dev->vpd;
-	const u8 *buf = arg;
 	loff_t end = pos + count;
 	int ret = 0;
 
@@ -264,14 +264,8 @@ static ssize_t pci_vpd_write(struct pci_dev *dev, loff_t pos, size_t count,
 		goto out;
 
Quoted 17 lines

drivers/pci/vpd.c static ssize_t pci_vpd_write(struct pci_dev *dev, loff_t pos, size_t count, · line 268

Quoted 11 lines
drivers/pci/vpd.c +2 -8
 	while (pos < end) {
-		u32 val;
-
-		val = *buf++;
-		val |= *buf++ << 8;
-		val |= *buf++ << 16;
-		val |= *buf++ << 24;
-
-		ret = pci_user_write_config_dword(dev, vpd->cap + PCI_VPD_DATA, val);
+		ret = pci_user_write_config_dword(dev, vpd->cap + PCI_VPD_DATA,
+						  get_unaligned_le32(buf));

If I understand correctly, get_unaligned_le32() is equivalent to the open-coded "val = *buf++; ..." In other words, this doesn't fix a bug on any platforms, and it doesn't change the bits written to VPD. Right?

Right.

Quoted 7 lines

If so, this makes sense. I'd like it better if we could also make pci_vpd_read() look more symmetric, since it has similar byte order-related code. The read side is a little more complicated since the size need not be 4-byte aligned. But maybe there's a way? Seems like there should be other examples of this sort of thing, but my quick look didn't find one.

I also find the read side a little bit hard to understand. Let me check whether I find some way to improve the code.

Quoted 14 lines
Quoted 14 lines
drivers/pci/vpd.c +1 -0
 		if (ret < 0)
 			break;
 		ret = pci_user_write_config_word(dev, vpd->cap + PCI_VPD_ADDR,
@@ -286,6 +280,7 @@ static ssize_t pci_vpd_write(struct pci_dev *dev, loff_t pos, size_t count,
 			break;
 
 		pos += sizeof(u32);
+		buf += sizeof(u32);
 	}
 out:
 	mutex_unlock(&vpd->lock);
Signature · 1 line
2.31.1