Skip to contentSkip to conversation

PCI/VPD: Further improvements

PATCH Applied Heiner Kallweit 3 participants 33 messages May 13, 2021 – Aug 5, 2021 PCI

lore
Share & tools
lore
#0 lore raw

cover PCI/VPD: Further improvements

Series with further improvements to PCI VPD handling.

Heiner Kallweit (5):
PCI/VPD: Refactor pci_vpd_size
PCI: Clean up VPD constants and functions in pci.h PCI/VPD: Remove old_size argument from pci_vpd_size PCI/VPD: Make pci_vpd_wait uninterruptible PCI/VPD: Remove pci_vpd member flag

 drivers/pci/vpd.c   | 106 ++++++++++++++++----------------------------
 include/linux/pci.h |  43 ------------------
 2 files changed, 37 insertions(+), 112 deletions(-)
Signature · 1 line
2.31.1
#1 lore raw

PATCH 1/5 PCI/VPD: Refactor pci_vpd_size

The only Short Resource Data Type tag is the end tag. This allows to remove the generic SRDT tag handling and the code be significantly simplified.

---
 drivers/pci/vpd.c | 46 ++++++++++++----------------------------------
 1 file changed, 12 insertions(+), 34 deletions(-)
drivers/pci/vpd.c +12 -34
index 26bf7c877..ecdce170f 100644
@@ -73,50 +73,28 @@ EXPORT_SYMBOL(pci_write_vpd);
 static size_t pci_vpd_size(struct pci_dev *dev, size_t old_size)
 {
 	size_t off = 0;
-	unsigned char header[1+2];	/* 1 byte tag, 2 bytes length */
+	u8 header[3];	/* 1 byte tag, 2 bytes length */
 
 	while (off < old_size && pci_read_vpd(dev, off, 1, header) == 1) {
-		unsigned char tag;
-
 		if (!header[0] && !off) {
 			pci_info(dev, "Invalid VPD tag 00, assume missing optional VPD EPROM\n");
 			return 0;
 		}
 
-		if (header[0] & PCI_VPD_LRDT) {
-			/* Large Resource Data Type Tag */
-			tag = pci_vpd_lrdt_tag(header);
-			/* Only read length from known tag items */
-			if ((tag == PCI_VPD_LTIN_ID_STRING) ||
-			    (tag == PCI_VPD_LTIN_RO_DATA) ||
-			    (tag == PCI_VPD_LTIN_RW_DATA)) {
-				if (pci_read_vpd(dev, off+1, 2,
-						 &header[1]) != 2) {
-					pci_warn(dev, "invalid large VPD tag %02x size at offset %zu",
-						 tag, off + 1);
-					return 0;
-				}
-				off += PCI_VPD_LRDT_TAG_SIZE +
-					pci_vpd_lrdt_size(header);
-			}
-		} else {
-			/* Short Resource Data Type Tag */
-			off += PCI_VPD_SRDT_TAG_SIZE +
-				pci_vpd_srdt_size(header);
-			tag = pci_vpd_srdt_tag(header);
-		}
-
-		if (tag == PCI_VPD_STIN_END)	/* End tag descriptor */
-			return off;
+		if (header[0] == PCI_VPD_SRDT_END)
+			return off + PCI_VPD_SRDT_TAG_SIZE;
 
-		if ((tag != PCI_VPD_LTIN_ID_STRING) &&
-		    (tag != PCI_VPD_LTIN_RO_DATA) &&
-		    (tag != PCI_VPD_LTIN_RW_DATA)) {
-			pci_warn(dev, "invalid %s VPD tag %02x at offset %zu",
-				 (header[0] & PCI_VPD_LRDT) ? "large" : "short",
-				 tag, off);
+		if (header[0] != PCI_VPD_LRDT_ID_STRING &&
+		    header[0] != PCI_VPD_LRDT_RO_DATA &&
+		    header[0] != PCI_VPD_LRDT_RW_DATA) {
+			pci_warn(dev, "invalid VPD tag %02x at offset %zu", header[0], off);
 			return 0;
 		}
+
+		if (pci_read_vpd(dev, off + 1, 2, header + 1) != 2)
+			return 0;
+
+		off += PCI_VPD_LRDT_TAG_SIZE + pci_vpd_lrdt_size(header);
 	}
 	return 0;
 }
Signature · 1 line
2.31.1
#2 lore raw

Re: [PATCH 1/5] PCI/VPD: Refactor pci_vpd_size

[+cc Hannes]

Quoted 45 lines · Heiner Kallweit

The only Short Resource Data Type tag is the end tag. This allows to remove the generic SRDT tag handling and the code be significantly simplified.

---
 drivers/pci/vpd.c | 46 ++++++++++++----------------------------------
 1 file changed, 12 insertions(+), 34 deletions(-)
drivers/pci/vpd.c +1 -20
index 26bf7c877..ecdce170f 100644
@@ -73,50 +73,28 @@ EXPORT_SYMBOL(pci_write_vpd);
 static size_t pci_vpd_size(struct pci_dev *dev, size_t old_size)
 {
 	size_t off = 0;
-	unsigned char header[1+2];	/* 1 byte tag, 2 bytes length */
+	u8 header[3];	/* 1 byte tag, 2 bytes length */
 
 	while (off < old_size && pci_read_vpd(dev, off, 1, header) == 1) {
-		unsigned char tag;
-
 		if (!header[0] && !off) {
 			pci_info(dev, "Invalid VPD tag 00, assume missing optional VPD EPROM\n");
 			return 0;
 		}
 
-		if (header[0] & PCI_VPD_LRDT) {
-			/* Large Resource Data Type Tag */
-			tag = pci_vpd_lrdt_tag(header);
-			/* Only read length from known tag items */
-			if ((tag == PCI_VPD_LTIN_ID_STRING) ||
-			    (tag == PCI_VPD_LTIN_RO_DATA) ||
-			    (tag == PCI_VPD_LTIN_RW_DATA)) {
-				if (pci_read_vpd(dev, off+1, 2,
-						 &header[1]) != 2) {
-					pci_warn(dev, "invalid large VPD tag %02x size at offset %zu",
-						 tag, off + 1);
-					return 0;
-				}
-				off += PCI_VPD_LRDT_TAG_SIZE +
-					pci_vpd_lrdt_size(header);
-			}
-		} else {

drivers/pci/vpd.c EXPORT_SYMBOL(pci_write_vpd); · line 85

Quoted 10 lines
drivers/pci/vpd.c +2 -8
-			/* Short Resource Data Type Tag */
-			off += PCI_VPD_SRDT_TAG_SIZE +
-				pci_vpd_srdt_size(header);
-			tag = pci_vpd_srdt_tag(header);
-		}
-
-		if (tag == PCI_VPD_STIN_END)	/* End tag descriptor */
-			return off;
+		if (header[0] == PCI_VPD_SRDT_END)
+			return off + PCI_VPD_SRDT_TAG_SIZE;

This makes the code beautiful. But I think pci_vpd_size() is too picky even now, and this patch makes it more so.

I don't know why pci_vpd_size() currently checks the tags for ID_STRING, RO_DATA, and RW_DATA. That seems too aggressive to me.

I think these data items originally came from PNP ISA, and it defines several other tags. Of course, that wasn't for PCI devices, but a Google search for '"invalid" "vpd tag" "at offset"' does find several cases where VPD contains things that look like PNP ISA data items.

I think we should compute the VPD size by iterating through it looking only at the type (small or large) and the data item length until we find the End Tag.

This code originally came from 104daa71b396 ("PCI: Determine actual VPD size on first access"), so I added Hannes in case there was some reason we do the extra validation.

Quoted 24 lines
drivers/pci/vpd.c +9 -6
-		if ((tag != PCI_VPD_LTIN_ID_STRING) &&
-		    (tag != PCI_VPD_LTIN_RO_DATA) &&
-		    (tag != PCI_VPD_LTIN_RW_DATA)) {
-			pci_warn(dev, "invalid %s VPD tag %02x at offset %zu",
-				 (header[0] & PCI_VPD_LRDT) ? "large" : "short",
-				 tag, off);
+		if (header[0] != PCI_VPD_LRDT_ID_STRING &&
+		    header[0] != PCI_VPD_LRDT_RO_DATA &&
+		    header[0] != PCI_VPD_LRDT_RW_DATA) {
+			pci_warn(dev, "invalid VPD tag %02x at offset %zu", header[0], off);
 			return 0;
 		}
+
+		if (pci_read_vpd(dev, off + 1, 2, header + 1) != 2)
+			return 0;
+
+		off += PCI_VPD_LRDT_TAG_SIZE + pci_vpd_lrdt_size(header);
 	}
 	return 0;
 }
Signature · 1 line
2.31.1
#3 lore raw

Re: [PATCH 1/5] PCI/VPD: Refactor pci_vpd_size

Quoted 62 lines · Bjorn Helgaas

[+cc Hannes]

Quoted 45 lines · Heiner Kallweit

The only Short Resource Data Type tag is the end tag. This allows to remove the generic SRDT tag handling and the code be significantly simplified.

---
 drivers/pci/vpd.c | 46 ++++++++++++----------------------------------
 1 file changed, 12 insertions(+), 34 deletions(-)
drivers/pci/vpd.c +1 -20
index 26bf7c877..ecdce170f 100644
@@ -73,50 +73,28 @@ EXPORT_SYMBOL(pci_write_vpd);
 static size_t pci_vpd_size(struct pci_dev *dev, size_t old_size)
 {
 	size_t off = 0;
-	unsigned char header[1+2];	/* 1 byte tag, 2 bytes length */
+	u8 header[3];	/* 1 byte tag, 2 bytes length */
 
 	while (off < old_size && pci_read_vpd(dev, off, 1, header) == 1) {
-		unsigned char tag;
-
 		if (!header[0] && !off) {
 			pci_info(dev, "Invalid VPD tag 00, assume missing optional VPD EPROM\n");
 			return 0;
 		}
 
-		if (header[0] & PCI_VPD_LRDT) {
-			/* Large Resource Data Type Tag */
-			tag = pci_vpd_lrdt_tag(header);
-			/* Only read length from known tag items */
-			if ((tag == PCI_VPD_LTIN_ID_STRING) ||
-			    (tag == PCI_VPD_LTIN_RO_DATA) ||
-			    (tag == PCI_VPD_LTIN_RW_DATA)) {
-				if (pci_read_vpd(dev, off+1, 2,
-						 &header[1]) != 2) {
-					pci_warn(dev, "invalid large VPD tag %02x size at offset %zu",
-						 tag, off + 1);
-					return 0;
-				}
-				off += PCI_VPD_LRDT_TAG_SIZE +
-					pci_vpd_lrdt_size(header);
-			}
-		} else {

drivers/pci/vpd.c EXPORT_SYMBOL(pci_write_vpd); · line 85

Quoted 10 lines
drivers/pci/vpd.c +2 -8
-			/* Short Resource Data Type Tag */
-			off += PCI_VPD_SRDT_TAG_SIZE +
-				pci_vpd_srdt_size(header);
-			tag = pci_vpd_srdt_tag(header);
-		}
-
-		if (tag == PCI_VPD_STIN_END)	/* End tag descriptor */
-			return off;
+		if (header[0] == PCI_VPD_SRDT_END)
+			return off + PCI_VPD_SRDT_TAG_SIZE;

This makes the code beautiful. But I think pci_vpd_size() is too picky even now, and this patch makes it more so.

Quoted 3 lines

I don't know why pci_vpd_size() currently checks the tags for ID_STRING, RO_DATA, and RW_DATA. That seems too aggressive to me.

It checks for these tags (+ the end tag) because it's the only ones defined for VPD by the PCI spec.

Quoted 5 lines

I think these data items originally came from PNP ISA, and it defines several other tags. Of course, that wasn't for PCI devices, but a Google search for '"invalid" "vpd tag" "at offset"' does find several cases where VPD contains things that look like PNP ISA data items.

Right, the tag format is based on PNP ISA. But PCI spec explicitly lists the supported tags.
I tried to do the same search and found:
- "invalid short vpd tag 00" and "invalid large tag 7f" Both are symptom of a missing optional VPD EEPROM.
- "ixgbe 0000:0b:00.0: invalid short VPD tag 06 at offset 4" and a similar message for igb
I didn't see any response explaining what causes this issue. My personal guess: Some OEM provided invalid VPD EEPROM content. Offset 4 is the first character of the ID string. The message indicates that the ID tag declares an empty ID. That would be weird.

Quoted 4 lines

I think we should compute the VPD size by iterating through it looking only at the type (small or large) and the data item length until we find the End Tag.

Still I didn't see any example of a rejected valid VPD image. Not checking for supported tags increases he risk that we interpret a random byte as tag and read beyond the VPD end, what is known to cause a freeze on some devices.

Quoted 28 lines

This code originally came from 104daa71b396 ("PCI: Determine actual VPD size on first access"), so I added Hannes in case there was some reason we do the extra validation.

Quoted 24 lines
drivers/pci/vpd.c +9 -6
-		if ((tag != PCI_VPD_LTIN_ID_STRING) &&
-		    (tag != PCI_VPD_LTIN_RO_DATA) &&
-		    (tag != PCI_VPD_LTIN_RW_DATA)) {
-			pci_warn(dev, "invalid %s VPD tag %02x at offset %zu",
-				 (header[0] & PCI_VPD_LRDT) ? "large" : "short",
-				 tag, off);
+		if (header[0] != PCI_VPD_LRDT_ID_STRING &&
+		    header[0] != PCI_VPD_LRDT_RO_DATA &&
+		    header[0] != PCI_VPD_LRDT_RW_DATA) {
+			pci_warn(dev, "invalid VPD tag %02x at offset %zu", header[0], off);
 			return 0;
 		}
+
+		if (pci_read_vpd(dev, off + 1, 2, header + 1) != 2)
+			return 0;
+
+		off += PCI_VPD_LRDT_TAG_SIZE + pci_vpd_lrdt_size(header);
 	}
 	return 0;
 }
Signature · 1 line
2.31.1
#4 lore raw

Re: [PATCH 1/5] PCI/VPD: Refactor pci_vpd_size

Quoted 69 lines · Heiner Kallweit
Quoted 62 lines · Bjorn Helgaas

[+cc Hannes]

Quoted 45 lines · Heiner Kallweit

The only Short Resource Data Type tag is the end tag. This allows to remove the generic SRDT tag handling and the code be significantly simplified.

---
 drivers/pci/vpd.c | 46 ++++++++++++----------------------------------
 1 file changed, 12 insertions(+), 34 deletions(-)
drivers/pci/vpd.c +1 -20
index 26bf7c877..ecdce170f 100644
@@ -73,50 +73,28 @@ EXPORT_SYMBOL(pci_write_vpd);
 static size_t pci_vpd_size(struct pci_dev *dev, size_t old_size)
 {
 	size_t off = 0;
-	unsigned char header[1+2];	/* 1 byte tag, 2 bytes length */
+	u8 header[3];	/* 1 byte tag, 2 bytes length */
 
 	while (off < old_size && pci_read_vpd(dev, off, 1, header) == 1) {
-		unsigned char tag;
-
 		if (!header[0] && !off) {
 			pci_info(dev, "Invalid VPD tag 00, assume missing optional VPD EPROM\n");
 			return 0;
 		}
 
-		if (header[0] & PCI_VPD_LRDT) {
-			/* Large Resource Data Type Tag */
-			tag = pci_vpd_lrdt_tag(header);
-			/* Only read length from known tag items */
-			if ((tag == PCI_VPD_LTIN_ID_STRING) ||
-			    (tag == PCI_VPD_LTIN_RO_DATA) ||
-			    (tag == PCI_VPD_LTIN_RW_DATA)) {
-				if (pci_read_vpd(dev, off+1, 2,
-						 &header[1]) != 2) {
-					pci_warn(dev, "invalid large VPD tag %02x size at offset %zu",
-						 tag, off + 1);
-					return 0;
-				}
-				off += PCI_VPD_LRDT_TAG_SIZE +
-					pci_vpd_lrdt_size(header);
-			}
-		} else {

drivers/pci/vpd.c EXPORT_SYMBOL(pci_write_vpd); · line 85

Quoted 10 lines
drivers/pci/vpd.c +2 -8
-			/* Short Resource Data Type Tag */
-			off += PCI_VPD_SRDT_TAG_SIZE +
-				pci_vpd_srdt_size(header);
-			tag = pci_vpd_srdt_tag(header);
-		}
-
-		if (tag == PCI_VPD_STIN_END)	/* End tag descriptor */
-			return off;
+		if (header[0] == PCI_VPD_SRDT_END)
+			return off + PCI_VPD_SRDT_TAG_SIZE;

This makes the code beautiful. But I think pci_vpd_size() is too picky even now, and this patch makes it more so.

Quoted 2 lines

I don't know why pci_vpd_size() currently checks the tags for ID_STRING, RO_DATA, and RW_DATA. That seems too aggressive to me.

It checks for these tags (+ the end tag) because it's the only ones defined for VPD by the PCI spec.

Quoted 7 lines
Quoted 4 lines

I think these data items originally came from PNP ISA, and it defines several other tags. Of course, that wasn't for PCI devices, but a Google search for '"invalid" "vpd tag" "at offset"' does find several cases where VPD contains things that look like PNP ISA data items.

Right, the tag format is based on PNP ISA. But PCI spec explicitly lists the supported tags.

Yes. I guess my point is that I think we should make the minimum assumptions required by the spec, and we shouldn't make things fail because of minor spec violations that don't get in the way of what we're trying to accomplish. Sort of the "be liberal in what you accept" idea.

In this case, we're trying to determine the VPD size. We don't need to know what data types are in the VPD; we only need to know the size of each data item, which is determined by the small/large bit and the data item length. The "name" (ID_STRING, RO_DATA, RW_DATA) isn't involved at all except that we need to recognize the END tag.

The value of rejecting unrecognized data types is a bit fuzzy. Yes, it may keep us from reading beyond the VPD end. But it's no guarantee; we can still read beyond the end if the VPD is ill-structured (missing END tag, too-large item length, etc).

The kernel itself doesn't depend on any data from VPD, so I don't think it should be in the business of validating that VPD only uses the approved data item types.

Quoted 3 lines

I tried to do the same search and found:
- "invalid short vpd tag 00" and "invalid large tag 7f" Both are symptom of a missing optional VPD EEPROM.

If we read a tag that is inconsistent, we *have* to stop because we can no longer compute the size. Neither of the trivial cases of a missing VPD EEPROM looking like all zeroes or all 0xff can be interpreted as valid tags, so it's easy to discard those cases.

Incidentally, I think we may be able to improve our diagnostic messages a bit. The "Invalid VPD tag 00, assume missing optional VPD EPROM" message is good, but the 0xff case isn't quite as clear.

Quoted 45 lines

- "ixgbe 0000:0b:00.0: invalid short VPD tag 06 at offset 4" and a similar message for igb
I didn't see any response explaining what causes this issue. My personal guess: Some OEM provided invalid VPD EEPROM content. Offset 4 is the first character of the ID string. The message indicates that the ID tag declares an empty ID. That would be weird.

Quoted 3 lines

I think we should compute the VPD size by iterating through it looking only at the type (small or large) and the data item length until we find the End Tag.

Still I didn't see any example of a rejected valid VPD image. Not checking for supported tags increases the risk that we interpret a random byte as tag and read beyond the VPD end, what is known to cause a freeze on some devices.

Quoted 28 lines

This code originally came from 104daa71b396 ("PCI: Determine actual VPD size on first access"), so I added Hannes in case there was some reason we do the extra validation.

Quoted 24 lines
drivers/pci/vpd.c +9 -6
-		if ((tag != PCI_VPD_LTIN_ID_STRING) &&
-		    (tag != PCI_VPD_LTIN_RO_DATA) &&
-		    (tag != PCI_VPD_LTIN_RW_DATA)) {
-			pci_warn(dev, "invalid %s VPD tag %02x at offset %zu",
-				 (header[0] & PCI_VPD_LRDT) ? "large" : "short",
-				 tag, off);
+		if (header[0] != PCI_VPD_LRDT_ID_STRING &&
+		    header[0] != PCI_VPD_LRDT_RO_DATA &&
+		    header[0] != PCI_VPD_LRDT_RW_DATA) {
+			pci_warn(dev, "invalid VPD tag %02x at offset %zu", header[0], off);
 			return 0;
 		}
+
+		if (pci_read_vpd(dev, off + 1, 2, header + 1) != 2)
+			return 0;
+
+		off += PCI_VPD_LRDT_TAG_SIZE + pci_vpd_lrdt_size(header);
 	}
 	return 0;
 }
Signature · 1 line
2.31.1
#5 lore raw

Re: [PATCH 1/5] PCI/VPD: Refactor pci_vpd_size

Quoted 103 lines · Bjorn Helgaas
Quoted 69 lines · Heiner Kallweit
Quoted 62 lines · Bjorn Helgaas

[+cc Hannes]

Quoted 45 lines · Heiner Kallweit

The only Short Resource Data Type tag is the end tag. This allows to remove the generic SRDT tag handling and the code be significantly simplified.

---
 drivers/pci/vpd.c | 46 ++++++++++++----------------------------------
 1 file changed, 12 insertions(+), 34 deletions(-)
drivers/pci/vpd.c +1 -20
index 26bf7c877..ecdce170f 100644
@@ -73,50 +73,28 @@ EXPORT_SYMBOL(pci_write_vpd);
 static size_t pci_vpd_size(struct pci_dev *dev, size_t old_size)
 {
 	size_t off = 0;
-	unsigned char header[1+2];	/* 1 byte tag, 2 bytes length */
+	u8 header[3];	/* 1 byte tag, 2 bytes length */
 
 	while (off < old_size && pci_read_vpd(dev, off, 1, header) == 1) {
-		unsigned char tag;
-
 		if (!header[0] && !off) {
 			pci_info(dev, "Invalid VPD tag 00, assume missing optional VPD EPROM\n");
 			return 0;
 		}
 
-		if (header[0] & PCI_VPD_LRDT) {
-			/* Large Resource Data Type Tag */
-			tag = pci_vpd_lrdt_tag(header);
-			/* Only read length from known tag items */
-			if ((tag == PCI_VPD_LTIN_ID_STRING) ||
-			    (tag == PCI_VPD_LTIN_RO_DATA) ||
-			    (tag == PCI_VPD_LTIN_RW_DATA)) {
-				if (pci_read_vpd(dev, off+1, 2,
-						 &header[1]) != 2) {
-					pci_warn(dev, "invalid large VPD tag %02x size at offset %zu",
-						 tag, off + 1);
-					return 0;
-				}
-				off += PCI_VPD_LRDT_TAG_SIZE +
-					pci_vpd_lrdt_size(header);
-			}
-		} else {

drivers/pci/vpd.c EXPORT_SYMBOL(pci_write_vpd); · line 85

Quoted 10 lines
drivers/pci/vpd.c +2 -8
-			/* Short Resource Data Type Tag */
-			off += PCI_VPD_SRDT_TAG_SIZE +
-				pci_vpd_srdt_size(header);
-			tag = pci_vpd_srdt_tag(header);
-		}
-
-		if (tag == PCI_VPD_STIN_END)	/* End tag descriptor */
-			return off;
+		if (header[0] == PCI_VPD_SRDT_END)
+			return off + PCI_VPD_SRDT_TAG_SIZE;

This makes the code beautiful. But I think pci_vpd_size() is too picky even now, and this patch makes it more so.

Quoted 2 lines

I don't know why pci_vpd_size() currently checks the tags for ID_STRING, RO_DATA, and RW_DATA. That seems too aggressive to me.

It checks for these tags (+ the end tag) because it's the only ones defined for VPD by the PCI spec.

Quoted 7 lines
Quoted 4 lines

I think these data items originally came from PNP ISA, and it defines several other tags. Of course, that wasn't for PCI devices, but a Google search for '"invalid" "vpd tag" "at offset"' does find several cases where VPD contains things that look like PNP ISA data items.

Right, the tag format is based on PNP ISA. But PCI spec explicitly lists the supported tags.

Yes. I guess my point is that I think we should make the minimum assumptions required by the spec, and we shouldn't make things fail because of minor spec violations that don't get in the way of what we're trying to accomplish. Sort of the "be liberal in what you accept" idea.

In this case, we're trying to determine the VPD size. We don't need to know what data types are in the VPD; we only need to know the size of each data item, which is determined by the small/large bit and the data item length. The "name" (ID_STRING, RO_DATA, RW_DATA) isn't involved at all except that we need to recognize the END tag.

The value of rejecting unrecognized data types is a bit fuzzy. Yes, it may keep us from reading beyond the VPD end. But it's no guarantee; we can still read beyond the end if the VPD is ill-structured (missing END tag, too-large item length, etc).

The kernel itself doesn't depend on any data from VPD, so I don't think it should be in the business of validating that VPD only uses the approved data item types.

Quoted 3 lines

I tried to do the same search and found:
- "invalid short vpd tag 00" and "invalid large tag 7f" Both are symptom of a missing optional VPD EEPROM.

Quoted 9 lines

If we read a tag that is inconsistent, we *have* to stop because we can no longer compute the size. Neither of the trivial cases of a missing VPD EEPROM looking like all zeroes or all 0xff can be interpreted as valid tags, so it's easy to discard those cases.

Incidentally, I think we may be able to improve our diagnostic messages a bit. The "Invalid VPD tag 00, assume missing optional VPD EPROM" message is good, but the 0xff case isn't quite as clear.

Right, we should handle 0xff together with 0x00. I'll submit a patch for it.

Quoted 45 lines
Quoted 45 lines

- "ixgbe 0000:0b:00.0: invalid short VPD tag 06 at offset 4" and a similar message for igb
I didn't see any response explaining what causes this issue. My personal guess: Some OEM provided invalid VPD EEPROM content. Offset 4 is the first character of the ID string. The message indicates that the ID tag declares an empty ID. That would be weird.

Quoted 3 lines

I think we should compute the VPD size by iterating through it looking only at the type (small or large) and the data item length until we find the End Tag.

Still I didn't see any example of a rejected valid VPD image. Not checking for supported tags increases the risk that we interpret a random byte as tag and read beyond the VPD end, what is known to cause a freeze on some devices.

Quoted 28 lines

This code originally came from 104daa71b396 ("PCI: Determine actual VPD size on first access"), so I added Hannes in case there was some reason we do the extra validation.

Quoted 24 lines
drivers/pci/vpd.c +9 -6
-		if ((tag != PCI_VPD_LTIN_ID_STRING) &&
-		    (tag != PCI_VPD_LTIN_RO_DATA) &&
-		    (tag != PCI_VPD_LTIN_RW_DATA)) {
-			pci_warn(dev, "invalid %s VPD tag %02x at offset %zu",
-				 (header[0] & PCI_VPD_LRDT) ? "large" : "short",
-				 tag, off);
+		if (header[0] != PCI_VPD_LRDT_ID_STRING &&
+		    header[0] != PCI_VPD_LRDT_RO_DATA &&
+		    header[0] != PCI_VPD_LRDT_RW_DATA) {
+			pci_warn(dev, "invalid VPD tag %02x at offset %zu", header[0], off);
 			return 0;
 		}
+
+		if (pci_read_vpd(dev, off + 1, 2, header + 1) != 2)
+			return 0;
+
+		off += PCI_VPD_LRDT_TAG_SIZE + pci_vpd_lrdt_size(header);
 	}
 	return 0;
 }
Signature · 1 line
2.31.1
#6 lore raw

PATCH 2/5 PCI: Clean up VPD constants and functions in pci.h

In reply to Heiner Kallweit

Move some constants that are used by vpd.c only from include/linux/pci.h to drivers/pci/vpd.c. In addition remove some unused VPD inline functions.

---
 drivers/pci/vpd.c   |  7 +++++++
 include/linux/pci.h | 43 -------------------------------------------
 2 files changed, 7 insertions(+), 43 deletions(-)
drivers/pci/vpd.c +7 -0
index ecdce170f..ff537371c 100644
@@ -13,6 +13,13 @@
 
 /* VPD access through PCI 2.2+ VPD capability */
 
+/* Small Resource Data Type */
+#define PCI_VPD_SRDT_TAG_SIZE	1
+#define PCI_VPD_SRDT_END	(0x0f << 3) /* end tag */
+
+/* Large Resource Data Type */
+#define PCI_VPD_LRDT_TIN_MASK	0x7f
+
 struct pci_vpd_ops {
 	ssize_t (*read)(struct pci_dev *dev, loff_t pos, size_t count, void *buf);
 	ssize_t (*write)(struct pci_dev *dev, loff_t pos, size_t count, const void *buf);
include/linux/pci.h +0 -43

56 lines not on this page · raw message

Signature · 1 line
2.31.1
#7 lore raw

Re: [PATCH 2/5] PCI: Clean up VPD constants and functions in pci.h

Quoted 75 lines · Heiner Kallweit

Move some constants that are used by vpd.c only from include/linux/pci.h to drivers/pci/vpd.c. In addition remove some unused VPD inline functions.

---
 drivers/pci/vpd.c   |  7 +++++++
 include/linux/pci.h | 43 -------------------------------------------
 2 files changed, 7 insertions(+), 43 deletions(-)
drivers/pci/vpd.c +7 -0
index ecdce170f..ff537371c 100644
@@ -13,6 +13,13 @@
 
 /* VPD access through PCI 2.2+ VPD capability */
 
+/* Small Resource Data Type */
+#define PCI_VPD_SRDT_TAG_SIZE	1
+#define PCI_VPD_SRDT_END	(0x0f << 3) /* end tag */
+
+/* Large Resource Data Type */
+#define PCI_VPD_LRDT_TIN_MASK	0x7f
+
 struct pci_vpd_ops {
 	ssize_t (*read)(struct pci_dev *dev, loff_t pos, size_t count, void *buf);
 	ssize_t (*write)(struct pci_dev *dev, loff_t pos, size_t count, const void *buf);
include/linux/pci.h +0 -32
index c20211e59..c21558821 100644
@@ -2240,17 +2240,7 @@ int pci_enable_atomic_ops_to_root(struct pci_dev *dev, u32 cap_mask);
 #define PCI_VPD_LRDT_RO_DATA		PCI_VPD_LRDT_ID(PCI_VPD_LTIN_RO_DATA)
 #define PCI_VPD_LRDT_RW_DATA		PCI_VPD_LRDT_ID(PCI_VPD_LTIN_RW_DATA)
 
-/* Small Resource Data Type Tag Item Names */
-#define PCI_VPD_STIN_END		0x0f	/* End */
-
-#define PCI_VPD_SRDT_END		(PCI_VPD_STIN_END << 3)
-
-#define PCI_VPD_SRDT_TIN_MASK		0x78
-#define PCI_VPD_SRDT_LEN_MASK		0x07
-#define PCI_VPD_LRDT_TIN_MASK		0x7f
-
 #define PCI_VPD_LRDT_TAG_SIZE		3
-#define PCI_VPD_SRDT_TAG_SIZE		1
 
 #define PCI_VPD_INFO_FLD_HDR_SIZE	3
 
@@ -2271,39 +2261,6 @@ static inline u16 pci_vpd_lrdt_size(const u8 *lrdt)
 	return (u16)lrdt[1] + ((u16)lrdt[2] << 8);
 }
 
-/**
- * pci_vpd_lrdt_tag - Extracts the Large Resource Data Type Tag Item
- * @lrdt: Pointer to the beginning of the Large Resource Data Type tag
- *
- * Returns the extracted Large Resource Data Type Tag item.
- */
-static inline u16 pci_vpd_lrdt_tag(const u8 *lrdt)
-{
-	return (u16)(lrdt[0] & PCI_VPD_LRDT_TIN_MASK);
-}
-
-/**
- * pci_vpd_srdt_size - Extracts the Small Resource Data Type length
- * @srdt: Pointer to the beginning of the Small Resource Data Type tag
- *
- * Returns the extracted Small Resource Data Type length.
- */
-static inline u8 pci_vpd_srdt_size(const u8 *srdt)
-{
-	return (*srdt) & PCI_VPD_SRDT_LEN_MASK;
-}
-

include/linux/pci.h static inline u16 pci_vpd_lrdt_size(const u8 *lrdt) · line 2305

Quoted 10 lines
include/linux/pci.h +0 -10
-/**
- * pci_vpd_srdt_tag - Extracts the Small Resource Data Type Tag Item
- * @srdt: Pointer to the beginning of the Small Resource Data Type tag
- *
- * Returns the extracted Small Resource Data Type Tag Item.
- */
-static inline u8 pci_vpd_srdt_tag(const u8 *srdt)
-{
-	return ((*srdt) & PCI_VPD_SRDT_TIN_MASK) >> 3;
-}

I think the last uses of these were removed by the previous patch. Can you remove these definitions in the same patch that removed the last use? Then we don't have the unused things dangling between patches.

Quoted 7 lines
include/linux/pci.h +0 -0
 /**
  * pci_vpd_info_field_size - Extracts the information field length
  * @info_field: Pointer to the beginning of an information field header
Signature · 1 line
2.31.1
#8 lore raw

PATCH 3/5 PCI/VPD: Remove old_size argument from pci_vpd_size

In reply to Heiner Kallweit

vpd->len is initialized to PCI_VPD_MAX_SIZE, and if a quirk is used to set a specific VPD size, then pci_vpd_set_size() sets vpd->valid, resulting in pci_vpd_size() not being called. Therefore we can remove the old_size argument. Note that we don't have to check
off < PCI_VPD_MAX_SIZE because that's implicitly done by pci_read_vpd().

---
 drivers/pci/vpd.c | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)
drivers/pci/vpd.c +5 -5
index ff537371c..e73a3a55f 100644
@@ -75,14 +75,13 @@ EXPORT_SYMBOL(pci_write_vpd);
 /**
  * pci_vpd_size - determine actual size of Vital Product Data
  * @dev:	pci device struct
- * @old_size:	current assumed size, also maximum allowed size
  */
-static size_t pci_vpd_size(struct pci_dev *dev, size_t old_size)
+static size_t pci_vpd_size(struct pci_dev *dev)
 {
 	size_t off = 0;
 	u8 header[3];	/* 1 byte tag, 2 bytes length */
 
-	while (off < old_size && pci_read_vpd(dev, off, 1, header) == 1) {
+	while (pci_read_vpd(dev, off, 1, header) == 1) {
 		if (!header[0] && !off) {
 			pci_info(dev, "Invalid VPD tag 00, assume missing optional VPD EPROM\n");
 			return 0;
@@ -164,7 +163,7 @@ static ssize_t pci_vpd_read(struct pci_dev *dev, loff_t pos, size_t count,
 
 	if (!vpd->valid) {
 		vpd->valid = 1;
-		vpd->len = pci_vpd_size(dev, vpd->len);
+		vpd->len = pci_vpd_size(dev);
 	}
 
 	if (vpd->len == 0)
@@ -231,7 +230,7 @@ static ssize_t pci_vpd_write(struct pci_dev *dev, loff_t pos, size_t count,
 
 	if (!vpd->valid) {
 		vpd->valid = 1;
-		vpd->len = pci_vpd_size(dev, vpd->len);
+		vpd->len = pci_vpd_size(dev);
 	}
 
 	if (vpd->len == 0)
@@ -455,6 +454,7 @@ static void quirk_blacklist_vpd(struct pci_dev *dev)
 {
 	if (dev->vpd) {
 		dev->vpd->len = 0;
+		dev->vpd->valid = 1;
 		pci_warn(dev, FW_BUG "disabling VPD access (can't determine size of non-standard VPD format)\n");
 	}
 }
Signature · 1 line
2.31.1
#9 lore raw

PATCH 4/5 PCI/VPD: Make pci_vpd_wait uninterruptible

In reply to Heiner Kallweit

Reading/writing 4 bytes should be fast enough even on a slow bus, therefore pci_vpd_wait() doesn't have to be interruptible. Making it uninterruptible allows to simplify the code. In addition make VPD writes uninterruptible in general. It's about vital data, and allowing writes to be interruptible may leave the VPD in an inconsistent state.

---
 drivers/pci/vpd.c | 33 +++++++++------------------------
 1 file changed, 9 insertions(+), 24 deletions(-)
drivers/pci/vpd.c +9 -24
index e73a3a55f..76b20a13a 100644
@@ -31,7 +31,6 @@ struct pci_vpd {
 	unsigned int	len;
 	u16		flag;
 	u8		cap;
-	unsigned int	busy:1;
 	unsigned int	valid:1;
 };
 
@@ -121,22 +120,14 @@ static int pci_vpd_wait(struct pci_dev *dev)
 	u16 status;
 	int ret;
 
-	if (!vpd->busy)
-		return 0;
-
 	do {
 		ret = pci_user_read_config_word(dev, vpd->cap + PCI_VPD_ADDR,
 						&status);
 		if (ret < 0)
 			return ret;
 
-		if ((status & PCI_VPD_ADDR_F) == vpd->flag) {
-			vpd->busy = 0;
+		if ((status & PCI_VPD_ADDR_F) == vpd->flag)
 			return 0;
-		}
-
-		if (fatal_signal_pending(current))
-			return -EINTR;
 
 		if (time_after(jiffies, timeout))
 			break;
@@ -154,7 +145,7 @@ static ssize_t pci_vpd_read(struct pci_dev *dev, loff_t pos, size_t count,
 			    void *arg)
 {
 	struct pci_vpd *vpd = dev->vpd;
-	int ret;
+	int ret = 0;
 	loff_t end = pos + count;
 	u8 *buf = arg;
 
@@ -180,19 +171,19 @@ static ssize_t pci_vpd_read(struct pci_dev *dev, loff_t pos, size_t count,
 	if (mutex_lock_killable(&vpd->lock))
 		return -EINTR;
 
-	ret = pci_vpd_wait(dev);
-	if (ret < 0)
-		goto out;
-
 	while (pos < end) {
 		u32 val;
 		unsigned int i, skip;
 
+		if (fatal_signal_pending(current)) {
+			ret = -EINTR;
+			break;
+		}
+
 		ret = pci_user_write_config_word(dev, vpd->cap + PCI_VPD_ADDR,
 						 pos & ~3);
 		if (ret < 0)
 			break;
-		vpd->busy = 1;
 		vpd->flag = PCI_VPD_ADDR_F;
 		ret = pci_vpd_wait(dev);
 		if (ret < 0)
@@ -212,7 +203,7 @@ static ssize_t pci_vpd_read(struct pci_dev *dev, loff_t pos, size_t count,
 			val >>= 8;
 		}
 	}
-out:
+
 	mutex_unlock(&vpd->lock);
 	return ret ? ret : count;
 }
@@ -242,10 +233,6 @@ static ssize_t pci_vpd_write(struct pci_dev *dev, loff_t pos, size_t count,
 	if (mutex_lock_killable(&vpd->lock))
 		return -EINTR;
 
-	ret = pci_vpd_wait(dev);
-	if (ret < 0)
-		goto out;
-
 	while (pos < end) {
 		u32 val;
 
@@ -262,7 +249,6 @@ static ssize_t pci_vpd_write(struct pci_dev *dev, loff_t pos, size_t count,
 		if (ret < 0)
 			break;
 
-		vpd->busy = 1;
 		vpd->flag = 0;
 		ret = pci_vpd_wait(dev);
 		if (ret < 0)
@@ -270,7 +256,7 @@ static ssize_t pci_vpd_write(struct pci_dev *dev, loff_t pos, size_t count,
 
 		pos += sizeof(u32);
 	}
-out:
+
 	mutex_unlock(&vpd->lock);
 	return ret ? ret : count;
 }
@@ -333,7 +319,6 @@ void pci_vpd_init(struct pci_dev *dev)
 		vpd->ops = &pci_vpd_ops;
 	mutex_init(&vpd->lock);
 	vpd->cap = cap;
-	vpd->busy = 0;
 	vpd->valid = 0;
 	dev->vpd = vpd;
 }
Signature · 1 line
2.31.1
#10 lore raw

PATCH 5/5 PCI/VPD: Remove pci_vpd member flag

In reply to Heiner Kallweit

Remove the flag member and simplify the code.

---
 drivers/pci/vpd.c | 12 +++++-------
 1 file changed, 5 insertions(+), 7 deletions(-)
drivers/pci/vpd.c +5 -7
index 76b20a13a..df252f906 100644
@@ -29,7 +29,6 @@ struct pci_vpd {
 	const struct pci_vpd_ops *ops;
 	struct mutex	lock;
 	unsigned int	len;
-	u16		flag;
 	u8		cap;
 	unsigned int	valid:1;
 };
@@ -109,10 +108,11 @@ static size_t pci_vpd_size(struct pci_dev *dev)
  * This code has to spin since there is no other notification from the PCI
  * hardware. Since the VPD is often implemented by serial attachment to an
  * EEPROM, it may take many milliseconds to complete.
+ * @set: if true wait for flag to be set, else wait for it to be cleared
  *
  * Returns 0 on success, negative values indicate error.
  */
-static int pci_vpd_wait(struct pci_dev *dev)
+static int pci_vpd_wait(struct pci_dev *dev, bool set)
 {
 	struct pci_vpd *vpd = dev->vpd;
 	unsigned long timeout = jiffies + msecs_to_jiffies(125);
@@ -126,7 +126,7 @@ static int pci_vpd_wait(struct pci_dev *dev)
 		if (ret < 0)
 			return ret;
 
-		if ((status & PCI_VPD_ADDR_F) == vpd->flag)
+		if (!!(status & PCI_VPD_ADDR_F) == set)
 			return 0;
 
 		if (time_after(jiffies, timeout))
@@ -184,8 +184,7 @@ static ssize_t pci_vpd_read(struct pci_dev *dev, loff_t pos, size_t count,
 						 pos & ~3);
 		if (ret < 0)
 			break;
-		vpd->flag = PCI_VPD_ADDR_F;
-		ret = pci_vpd_wait(dev);
+		ret = pci_vpd_wait(dev, true);
 		if (ret < 0)
 			break;
 
@@ -249,8 +248,7 @@ static ssize_t pci_vpd_write(struct pci_dev *dev, loff_t pos, size_t count,
 		if (ret < 0)
 			break;
 
-		vpd->flag = 0;
-		ret = pci_vpd_wait(dev);
+		ret = pci_vpd_wait(dev, false);
 		if (ret < 0)
 			break;
 
Signature · 1 line
2.31.1
#11 lore raw
In reply to Heiner Kallweit
Quoted 9 lines · Heiner Kallweit

Series with further improvements to PCI VPD handling.

Heiner Kallweit (5):
PCI/VPD: Refactor pci_vpd_size
PCI: Clean up VPD constants and functions in pci.h PCI/VPD: Remove old_size argument from pci_vpd_size PCI/VPD: Make pci_vpd_wait uninterruptible PCI/VPD: Remove pci_vpd member flag

Quoted 4 lines
 drivers/pci/vpd.c   | 106 ++++++++++++++++----------------------------
 include/linux/pci.h |  43 ------------------
 2 files changed, 37 insertions(+), 112 deletions(-)

This series is still sitting as new in patchwork. Is it simply waiting for free review capacity or .. ?

#12 lore raw
Quoted 10 lines · Heiner Kallweit
Quoted 9 lines · Heiner Kallweit

Series with further improvements to PCI VPD handling.

Heiner Kallweit (5):
PCI/VPD: Refactor pci_vpd_size
PCI: Clean up VPD constants and functions in pci.h PCI/VPD: Remove old_size argument from pci_vpd_size PCI/VPD: Make pci_vpd_wait uninterruptible PCI/VPD: Remove pci_vpd member flag

Quoted 6 lines
Quoted 4 lines
 drivers/pci/vpd.c   | 106 ++++++++++++++++----------------------------
 include/linux/pci.h |  43 ------------------
 2 files changed, 37 insertions(+), 112 deletions(-)

This series is still sitting as new in patchwork. Is it simply waiting for free review capacity or .. ?

Yes, sorry, Heiner, I'm hopelessly behind. Trying to finish up the v5.14 pull request today, and then next week I'll start on the v5.15 material.

If this series applies cleanly to v5.14-rc1, you don't need to do anything. If it needs any rebasing for v5.14-rc1, it would save me some time if you could post an update.

Bjorn

#13 lore raw

cover PCI/VPD: pci_vpd_size() cleanups

In reply to Heiner Kallweit

From: Bjorn Helgaas <[email protected]>

This is a follow-up to the conversation here:
https://lore.kernel.org/r/[email protected]
about how and whether we should validate the contents of VPD.

My proposal is to basically do minimal validation since the kernel really doesn't care about the actual *contents* of VPD and mainly provides an access mechanism (although there a few drivers that get part numbers and maybe even clocking information from VPD).

I welcome any feedback!

Bjorn Helgaas (5):
PCI/VPD: Correct diagnostic for VPD read failure PCI/VPD: Check Resource tags against those valid for type PCI/VPD: Consolidate missing EEPROM checks
PCI/VPD: Don't check Large Resource types for validity PCI/VPD: Allow access to valid parts of VPD if some is invalid

 drivers/pci/vpd.c | 63 +++++++++++++++++++++++++----------------------
 1 file changed, 33 insertions(+), 30 deletions(-)
Signature · 1 line
2.25.1
#14 lore raw

PATCH 1/5 PCI/VPD: Correct diagnostic for VPD read failure

From: Bjorn Helgaas <[email protected]>

Previously, when a VPD read failed, we warned about an "invalid large VPD tag". Warn about the VPD read failure instead.

---
 drivers/pci/vpd.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/pci/vpd.c +2 -2
index 26bf7c877de5..7bfb8fc4251b 100644
@@ -92,8 +92,8 @@ static size_t pci_vpd_size(struct pci_dev *dev, size_t old_size)
 			    (tag == PCI_VPD_LTIN_RW_DATA)) {
 				if (pci_read_vpd(dev, off+1, 2,
 						 &header[1]) != 2) {
-					pci_warn(dev, "invalid large VPD tag %02x size at offset %zu",
-						 tag, off + 1);
+					pci_warn(dev, "failed VPD read at offset %zu",
+						 off + 1);
 					return 0;
 				}
 				off += PCI_VPD_LRDT_TAG_SIZE +
Signature · 1 line
2.25.1
#15 lore raw

Re: [PATCH 1/5] PCI/VPD: Correct diagnostic for VPD read failure

Quoted 5 lines · Bjorn Helgaas

From: Bjorn Helgaas <[email protected]>

Previously, when a VPD read failed, we warned about an "invalid large VPD tag". Warn about the VPD read failure instead.

LGTM. Seems no VPD has been broken enough yet to trigger this warning (at that place), so there's not much need to argue.

Quoted 21 lines
---
 drivers/pci/vpd.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/pci/vpd.c +2 -2
index 26bf7c877de5..7bfb8fc4251b 100644
@@ -92,8 +92,8 @@ static size_t pci_vpd_size(struct pci_dev *dev, size_t old_size)
 			    (tag == PCI_VPD_LTIN_RW_DATA)) {
 				if (pci_read_vpd(dev, off+1, 2,
 						 &header[1]) != 2) {
-					pci_warn(dev, "invalid large VPD tag %02x size at offset %zu",
-						 tag, off + 1);
+					pci_warn(dev, "failed VPD read at offset %zu",
+						 off + 1);
 					return 0;
 				}
 				off += PCI_VPD_LRDT_TAG_SIZE +
Reviewed-by on v1 1/5
#16 lore raw

Re: [PATCH 1/5] PCI/VPD: Correct diagnostic for VPD read failure

In reply to Bjorn Helgaas
Quoted 16 lines · Bjorn Helgaas

From: Bjorn Helgaas <[email protected]>

Previously, when a VPD read failed, we warned about an "invalid large VPD tag". Warn about the VPD read failure instead.

---
  drivers/pci/vpd.c | 4 ++--
  1 file changed, 2 insertions(+), 2 deletions(-)
drivers/pci/vpd.c +0 -0
index 26bf7c877de5..7bfb8fc4251b 100644

drivers/pci/vpd.c static size_t pci_vpd_size(struct pci_dev *dev, size_t old_size) · line 99

Quoted 10 lines
drivers/pci/vpd.c +2 -2
@@ -92,8 +92,8 @@ static size_t pci_vpd_size(struct pci_dev *dev, size_t old_size)
  			    (tag == PCI_VPD_LTIN_RW_DATA)) {
  				if (pci_read_vpd(dev, off+1, 2,
  						 &header[1]) != 2) {
-					pci_warn(dev, "invalid large VPD tag %02x size at offset %zu",
-						 tag, off + 1);
+					pci_warn(dev, "failed VPD read at offset %zu",
+						 off + 1);
  					return 0;
  				}
  				off += PCI_VPD_LRDT_TAG_SIZE +

Cheers,

Hannes

Signature · 4 lines
Dr. Hannes Reinecke                Kernel Storage Architect
[email protected]                              +49 911 74053 688
SUSE Software Solutions GmbH, Maxfeldstr. 5, 90409 Nürnberg
HRB 36809 (AG Nürnberg), Geschäftsführer: Felix Imendörffer
#17 lore raw

PATCH 2/5 PCI/VPD: Check Resource tags against those valid for type

In reply to Bjorn Helgaas

From: Bjorn Helgaas <[email protected]>

Previously, we checked for PCI_VPD_STIN_END, PCI_VPD_LTIN_ID_STRING, etc., outside the Large and Small Resource cases, so we checked Large Resource tags against a Small Resource name and vice versa.

Move these tests into the Large and Small Resource cases, so we only check PCI_VPD_STIN_END for Small Resources and PCI_VPD_LTIN_* for Large Resources.

---
 drivers/pci/vpd.c | 18 ++++++------------
 1 file changed, 6 insertions(+), 12 deletions(-)
drivers/pci/vpd.c +6 -12
index 7bfb8fc4251b..9b54dd95e42c 100644
@@ -98,24 +98,18 @@ static size_t pci_vpd_size(struct pci_dev *dev, size_t old_size)
 				}
 				off += PCI_VPD_LRDT_TAG_SIZE +
 					pci_vpd_lrdt_size(header);
+			} else {
+				pci_warn(dev, "invalid large VPD tag %02x at offset %zu",
+					 tag, off);
+				return 0;
 			}
 		} else {
 			/* Short Resource Data Type Tag */
 			off += PCI_VPD_SRDT_TAG_SIZE +
 				pci_vpd_srdt_size(header);
 			tag = pci_vpd_srdt_tag(header);
-		}
-
-		if (tag == PCI_VPD_STIN_END)	/* End tag descriptor */
-			return off;
-
-		if ((tag != PCI_VPD_LTIN_ID_STRING) &&
-		    (tag != PCI_VPD_LTIN_RO_DATA) &&
-		    (tag != PCI_VPD_LTIN_RW_DATA)) {
-			pci_warn(dev, "invalid %s VPD tag %02x at offset %zu",
-				 (header[0] & PCI_VPD_LRDT) ? "large" : "short",
-				 tag, off);
-			return 0;
+			if (tag == PCI_VPD_STIN_END)	/* End tag descriptor */
+				return off;
 		}
 	}
 	return 0;
Signature · 1 line
2.25.1
Reviewed-by on v1 2/5
#18 lore raw

Re: [PATCH 2/5] PCI/VPD: Check Resource tags against those valid for type

Quoted 6 lines · Bjorn Helgaas

From: Bjorn Helgaas <[email protected]>

Previously, we checked for PCI_VPD_STIN_END, PCI_VPD_LTIN_ID_STRING, etc., outside the Large and Small Resource cases, so we checked Large Resource tags against a Small Resource name and vice versa.

Quoted 9 lines

Move these tests into the Large and Small Resource cases, so we only check PCI_VPD_STIN_END for Small Resources and PCI_VPD_LTIN_* for Large Resources.

---
  drivers/pci/vpd.c | 18 ++++++------------
  1 file changed, 6 insertions(+), 12 deletions(-)

Cheers,

Hannes

Signature · 4 lines
Dr. Hannes Reinecke                Kernel Storage Architect
[email protected]                              +49 911 74053 688
SUSE Software Solutions GmbH, Maxfeldstr. 5, 90409 Nürnberg
HRB 36809 (AG Nürnberg), Geschäftsführer: Felix Imendörffer
#19 lore raw

PATCH 3/5 PCI/VPD: Consolidate missing EEPROM checks

In reply to Bjorn Helgaas

From: Bjorn Helgaas <[email protected]>

A missing VPD EEPROM typically reads as either all 0xff or all zeroes. Both cases lead to invalid VPD resource items. A 0xff tag would be a Large Resource with length 0xffff (65535). That's invalid because VPD can only be 32768 bytes, limited by the size of the address register in the VPD Capability.

A VPD that reads as all zeroes is also invalid because a 0x00 tag is a Small Resource with length 0, which would result in an item of length 1. This isn't explicitly illegal in PCIe r5.0, sec 6.28, but the format is derived from PNP ISA, which *does* say "a small resource data type may be 2-8 bytes in size" (Plug and Play ISA v1.0a, sec 6.2.2.

Check for these invalid tags and return VPD size of zero if we find them. If they occur at the beginning of VPD, assume it's the result of a missing EEPROM.

---
 drivers/pci/vpd.c | 36 +++++++++++++++++++++++++++---------
 1 file changed, 27 insertions(+), 9 deletions(-)
drivers/pci/vpd.c +27 -9
index 9b54dd95e42c..9c2744d79b53 100644
@@ -77,11 +77,7 @@ static size_t pci_vpd_size(struct pci_dev *dev, size_t old_size)
 
 	while (off < old_size && pci_read_vpd(dev, off, 1, header) == 1) {
 		unsigned char tag;
-
-		if (!header[0] && !off) {
-			pci_info(dev, "Invalid VPD tag 00, assume missing optional VPD EPROM\n");
-			return 0;
-		}
+		size_t size;
 
 		if (header[0] & PCI_VPD_LRDT) {
 			/* Large Resource Data Type Tag */
@@ -96,8 +92,16 @@ static size_t pci_vpd_size(struct pci_dev *dev, size_t old_size)
 						 off + 1);
 					return 0;
 				}
-				off += PCI_VPD_LRDT_TAG_SIZE +
-					pci_vpd_lrdt_size(header);
+				size = pci_vpd_lrdt_size(header);
+
+				/*
+				 * Missing EEPROM may read as 0xff.
+				 * Length of 0xffff (65535) cannot be valid
+				 * because VPD can't be that large.
+				 */
+				if (size > PCI_VPD_MAX_SIZE)
+					goto error;
+				off += PCI_VPD_LRDT_TAG_SIZE + size;
 			} else {
 				pci_warn(dev, "invalid large VPD tag %02x at offset %zu",
 					 tag, off);
@@ -105,14 +109,28 @@ static size_t pci_vpd_size(struct pci_dev *dev, size_t old_size)
 			}
 		} else {
 			/* Short Resource Data Type Tag */
-			off += PCI_VPD_SRDT_TAG_SIZE +
-				pci_vpd_srdt_size(header);
 			tag = pci_vpd_srdt_tag(header);
+			size = pci_vpd_srdt_size(header);
+
+			/*
+			 * Missing EEPROM may read as 0x00.  A small item
+			 * must be at least 2 bytes.
+			 */
+			if (size == 0)
+				goto error;
+
+			off += PCI_VPD_SRDT_TAG_SIZE + size;
 			if (tag == PCI_VPD_STIN_END)	/* End tag descriptor */
 				return off;
 		}
 	}
 	return 0;
+
+error:
+	pci_info(dev, "invalid VPD tag %#04x at offset %zu%s\n",
+		 header[0], off, off == 0 ?
+		 "; assume missing optional EEPROM" : "");
+	return 0;
 }
 
 /*
Signature · 1 line
2.25.1
#20 lore raw

Re: [PATCH 3/5] PCI/VPD: Consolidate missing EEPROM checks

Quoted 43 lines · Bjorn Helgaas

From: Bjorn Helgaas <[email protected]>

A missing VPD EEPROM typically reads as either all 0xff or all zeroes. Both cases lead to invalid VPD resource items. A 0xff tag would be a Large Resource with length 0xffff (65535). That's invalid because VPD can only be 32768 bytes, limited by the size of the address register in the VPD Capability.

A VPD that reads as all zeroes is also invalid because a 0x00 tag is a Small Resource with length 0, which would result in an item of length 1. This isn't explicitly illegal in PCIe r5.0, sec 6.28, but the format is derived from PNP ISA, which *does* say "a small resource data type may be 2-8 bytes in size" (Plug and Play ISA v1.0a, sec 6.2.2.

Check for these invalid tags and return VPD size of zero if we find them. If they occur at the beginning of VPD, assume it's the result of a missing EEPROM.

---
 drivers/pci/vpd.c | 36 +++++++++++++++++++++++++++---------
 1 file changed, 27 insertions(+), 9 deletions(-)
drivers/pci/vpd.c +1 -5
index 9b54dd95e42c..9c2744d79b53 100644
@@ -77,11 +77,7 @@ static size_t pci_vpd_size(struct pci_dev *dev, size_t old_size)
 
 	while (off < old_size && pci_read_vpd(dev, off, 1, header) == 1) {
 		unsigned char tag;
-
-		if (!header[0] && !off) {
-			pci_info(dev, "Invalid VPD tag 00, assume missing optional VPD EPROM\n");
-			return 0;
-		}
+		size_t size;
 
 		if (header[0] & PCI_VPD_LRDT) {
 			/* Large Resource Data Type Tag */
@@ -96,8 +92,16 @@ static size_t pci_vpd_size(struct pci_dev *dev, size_t old_size)
 						 off + 1);
 					return 0;

drivers/pci/vpd.c static size_t pci_vpd_size(struct pci_dev *dev, size_t old_size) · line 101

Quoted 10 lines
drivers/pci/vpd.c +7 -2
 				}
-				off += PCI_VPD_LRDT_TAG_SIZE +
-					pci_vpd_lrdt_size(header);
+				size = pci_vpd_lrdt_size(header);
+
+				/*
+				 * Missing EEPROM may read as 0xff.
+				 * Length of 0xffff (65535) cannot be valid
+				 * because VPD can't be that large.
+				 */

I'm not fully convinced. Instead of checking for a "no VPD EPROM" read (00/ff) directly, we now do it indirectly based on the internal tag structure. We have pci_vpd_lrdt_size() et al to encapsulate the internal structure. IMO the code is harder to understand now.

Quoted 38 lines
drivers/pci/vpd.c +19 -2
+				if (size > PCI_VPD_MAX_SIZE)
+					goto error;
+				off += PCI_VPD_LRDT_TAG_SIZE + size;
 			} else {
 				pci_warn(dev, "invalid large VPD tag %02x at offset %zu",
 					 tag, off);
@@ -105,14 +109,28 @@ static size_t pci_vpd_size(struct pci_dev *dev, size_t old_size)
 			}
 		} else {
 			/* Short Resource Data Type Tag */
-			off += PCI_VPD_SRDT_TAG_SIZE +
-				pci_vpd_srdt_size(header);
 			tag = pci_vpd_srdt_tag(header);
+			size = pci_vpd_srdt_size(header);
+
+			/*
+			 * Missing EEPROM may read as 0x00.  A small item
+			 * must be at least 2 bytes.
+			 */
+			if (size == 0)
+				goto error;
+
+			off += PCI_VPD_SRDT_TAG_SIZE + size;
 			if (tag == PCI_VPD_STIN_END)	/* End tag descriptor */
 				return off;
 		}
 	}
 	return 0;
+
+error:
+	pci_info(dev, "invalid VPD tag %#04x at offset %zu%s\n",
+		 header[0], off, off == 0 ?
+		 "; assume missing optional EEPROM" : "");
+	return 0;
 }
 
 /*
#21 lore raw

Re: [PATCH 3/5] PCI/VPD: Consolidate missing EEPROM checks

Quoted 51 lines · Heiner Kallweit
Quoted 50 lines · Bjorn Helgaas

From: Bjorn Helgaas <[email protected]>

A missing VPD EEPROM typically reads as either all 0xff or all zeroes. Both cases lead to invalid VPD resource items. A 0xff tag would be a Large Resource with length 0xffff (65535). That's invalid because VPD can only be 32768 bytes, limited by the size of the address register in the VPD Capability.

A VPD that reads as all zeroes is also invalid because a 0x00 tag is a Small Resource with length 0, which would result in an item of length 1. This isn't explicitly illegal in PCIe r5.0, sec 6.28, but the format is derived from PNP ISA, which *does* say "a small resource data type may be 2-8 bytes in size" (Plug and Play ISA v1.0a, sec 6.2.2.

Check for these invalid tags and return VPD size of zero if we find them. If they occur at the beginning of VPD, assume it's the result of a missing EEPROM.

---
 drivers/pci/vpd.c | 36 +++++++++++++++++++++++++++---------
 1 file changed, 27 insertions(+), 9 deletions(-)
drivers/pci/vpd.c +5 -7
index 9b54dd95e42c..9c2744d79b53 100644
@@ -77,11 +77,7 @@ static size_t pci_vpd_size(struct pci_dev *dev, size_t old_size)
 
 	while (off < old_size && pci_read_vpd(dev, off, 1, header) == 1) {
 		unsigned char tag;
-
-		if (!header[0] && !off) {
-			pci_info(dev, "Invalid VPD tag 00, assume missing optional VPD EPROM\n");
-			return 0;
-		}
+		size_t size;
 
 		if (header[0] & PCI_VPD_LRDT) {
 			/* Large Resource Data Type Tag */
@@ -96,8 +92,16 @@ static size_t pci_vpd_size(struct pci_dev *dev, size_t old_size)
 						 off + 1);
 					return 0;
 				}
-				off += PCI_VPD_LRDT_TAG_SIZE +
-					pci_vpd_lrdt_size(header);
+				size = pci_vpd_lrdt_size(header);
+
+				/*
+				 * Missing EEPROM may read as 0xff.
Quoted 8 lines

drivers/pci/vpd.c static size_t pci_vpd_size(struct pci_dev *dev, size_t old_size) · line 101

Quoted 3 lines
drivers/pci/vpd.c +3 -0
+				 * Length of 0xffff (65535) cannot be valid
+				 * because VPD can't be that large.
+				 */

I'm not fully convinced. Instead of checking for a "no VPD EPROM" read (00/ff) directly, we now do it indirectly based on the internal tag structure. We have pci_vpd_lrdt_size() et al to encapsulate the internal structure. IMO the code is harder to understand now.

I don't quite follow. Previously we checked for 0x00 data ("if (!header[0] && !off)"), but we didn't check directly for 0xff.

If we read 0xff, we took the PCI_VPD_LRDT case, but it wouldn't match ID_STRING, RO_DATA, or RW_DATA, so we'd fall out and check again against ID_STRING, RO_DATA, and RW_DATA, and take the "invalid %s VPD tag" error path because it doesn't match any.

This results in failure for any large resource except ID_STRING, RO_DATA, and RW_DATA, regardless of the size.

My proposed code catches a different set of invalid things. "size > PCI_VPD_MAX_SIZE" will catch any large resource headers with length 0x8001 through 0xffff.

Possibly it should actually check for "off + size > PCI_VPD_MAX_SIZE" so e.g., it would catch a 0x20 byte resource starting at 0x7ff0.

Quoted 39 lines
Quoted 38 lines
drivers/pci/vpd.c +19 -2
+				if (size > PCI_VPD_MAX_SIZE)
+					goto error;
+				off += PCI_VPD_LRDT_TAG_SIZE + size;
 			} else {
 				pci_warn(dev, "invalid large VPD tag %02x at offset %zu",
 					 tag, off);
@@ -105,14 +109,28 @@ static size_t pci_vpd_size(struct pci_dev *dev, size_t old_size)
 			}
 		} else {
 			/* Short Resource Data Type Tag */
-			off += PCI_VPD_SRDT_TAG_SIZE +
-				pci_vpd_srdt_size(header);
 			tag = pci_vpd_srdt_tag(header);
+			size = pci_vpd_srdt_size(header);
+
+			/*
+			 * Missing EEPROM may read as 0x00.  A small item
+			 * must be at least 2 bytes.
+			 */
+			if (size == 0)
+				goto error;
+
+			off += PCI_VPD_SRDT_TAG_SIZE + size;
 			if (tag == PCI_VPD_STIN_END)	/* End tag descriptor */
 				return off;
 		}
 	}
 	return 0;
+
+error:
+	pci_info(dev, "invalid VPD tag %#04x at offset %zu%s\n",
+		 header[0], off, off == 0 ?
+		 "; assume missing optional EEPROM" : "");
+	return 0;
 }
 
 /*
#22 lore raw

Re: [PATCH 3/5] PCI/VPD: Consolidate missing EEPROM checks

Quoted 72 lines · Bjorn Helgaas
Quoted 51 lines · Heiner Kallweit
Quoted 50 lines · Bjorn Helgaas

From: Bjorn Helgaas <[email protected]>

A missing VPD EEPROM typically reads as either all 0xff or all zeroes. Both cases lead to invalid VPD resource items. A 0xff tag would be a Large Resource with length 0xffff (65535). That's invalid because VPD can only be 32768 bytes, limited by the size of the address register in the VPD Capability.

A VPD that reads as all zeroes is also invalid because a 0x00 tag is a Small Resource with length 0, which would result in an item of length 1. This isn't explicitly illegal in PCIe r5.0, sec 6.28, but the format is derived from PNP ISA, which *does* say "a small resource data type may be 2-8 bytes in size" (Plug and Play ISA v1.0a, sec 6.2.2.

Check for these invalid tags and return VPD size of zero if we find them. If they occur at the beginning of VPD, assume it's the result of a missing EEPROM.

---
 drivers/pci/vpd.c | 36 +++++++++++++++++++++++++++---------
 1 file changed, 27 insertions(+), 9 deletions(-)
drivers/pci/vpd.c +5 -7
index 9b54dd95e42c..9c2744d79b53 100644
@@ -77,11 +77,7 @@ static size_t pci_vpd_size(struct pci_dev *dev, size_t old_size)
 
 	while (off < old_size && pci_read_vpd(dev, off, 1, header) == 1) {
 		unsigned char tag;
-
-		if (!header[0] && !off) {
-			pci_info(dev, "Invalid VPD tag 00, assume missing optional VPD EPROM\n");
-			return 0;
-		}
+		size_t size;
 
 		if (header[0] & PCI_VPD_LRDT) {
 			/* Large Resource Data Type Tag */
@@ -96,8 +92,16 @@ static size_t pci_vpd_size(struct pci_dev *dev, size_t old_size)
 						 off + 1);
 					return 0;
 				}
-				off += PCI_VPD_LRDT_TAG_SIZE +
-					pci_vpd_lrdt_size(header);
+				size = pci_vpd_lrdt_size(header);
+
+				/*
+				 * Missing EEPROM may read as 0xff.
Quoted 8 lines

drivers/pci/vpd.c static size_t pci_vpd_size(struct pci_dev *dev, size_t old_size) · line 101

Quoted 3 lines
drivers/pci/vpd.c +3 -0
+				 * Length of 0xffff (65535) cannot be valid
+				 * because VPD can't be that large.
+				 */

I'm not fully convinced. Instead of checking for a "no VPD EPROM" read (00/ff) directly, we now do it indirectly based on the internal tag structure. We have pci_vpd_lrdt_size() et al to encapsulate the internal structure. IMO the code is harder to understand now.

I don't quite follow. Previously we checked for 0x00 data ("if (!header[0] && !off)"), but we didn't check directly for 0xff.

If we read 0xff, we took the PCI_VPD_LRDT case, but it wouldn't match ID_STRING, RO_DATA, or RW_DATA, so we'd fall out and check again against ID_STRING, RO_DATA, and RW_DATA, and take the "invalid %s VPD tag" error path because it doesn't match any.

This results in failure for any large resource except ID_STRING, RO_DATA, and RW_DATA, regardless of the size.

Quoted 7 lines

My proposed code catches a different set of invalid things. "size > PCI_VPD_MAX_SIZE" will catch any large resource headers with length 0x8001 through 0xffff.

Possibly it should actually check for "off + size > PCI_VPD_MAX_SIZE" so e.g., it would catch a 0x20 byte resource starting at 0x7ff0.

For handling the 0xff case w/o additional overhead the following is pending:
https://patchwork.kernel.org/project/linux-pci/patch/[email protected]/

As far as I can see the VPD structure hasn't changed since it was introduced in PCI 2.2. This was how many years ago? Instead of adding code at least my personal objective would be to make support for such legacy features as simple and maintainable as possible.

Quoted 39 lines
Quoted 39 lines
Quoted 38 lines
drivers/pci/vpd.c +19 -2
+				if (size > PCI_VPD_MAX_SIZE)
+					goto error;
+				off += PCI_VPD_LRDT_TAG_SIZE + size;
 			} else {
 				pci_warn(dev, "invalid large VPD tag %02x at offset %zu",
 					 tag, off);
@@ -105,14 +109,28 @@ static size_t pci_vpd_size(struct pci_dev *dev, size_t old_size)
 			}
 		} else {
 			/* Short Resource Data Type Tag */
-			off += PCI_VPD_SRDT_TAG_SIZE +
-				pci_vpd_srdt_size(header);
 			tag = pci_vpd_srdt_tag(header);
+			size = pci_vpd_srdt_size(header);
+
+			/*
+			 * Missing EEPROM may read as 0x00.  A small item
+			 * must be at least 2 bytes.
+			 */
+			if (size == 0)
+				goto error;
+
+			off += PCI_VPD_SRDT_TAG_SIZE + size;
 			if (tag == PCI_VPD_STIN_END)	/* End tag descriptor */
 				return off;
 		}
 	}
 	return 0;
+
+error:
+	pci_info(dev, "invalid VPD tag %#04x at offset %zu%s\n",
+		 header[0], off, off == 0 ?
+		 "; assume missing optional EEPROM" : "");
+	return 0;
 }
 
 /*
#23 lore raw

Re: [PATCH 3/5] PCI/VPD: Consolidate missing EEPROM checks

Quoted 77 lines · Heiner Kallweit
Quoted 76 lines · Bjorn Helgaas
Quoted 51 lines · Heiner Kallweit
Quoted 50 lines · Bjorn Helgaas

From: Bjorn Helgaas <[email protected]>

A missing VPD EEPROM typically reads as either all 0xff or all zeroes. Both cases lead to invalid VPD resource items. A 0xff tag would be a Large Resource with length 0xffff (65535). That's invalid because VPD can only be 32768 bytes, limited by the size of the address register in the VPD Capability.

A VPD that reads as all zeroes is also invalid because a 0x00 tag is a Small Resource with length 0, which would result in an item of length 1. This isn't explicitly illegal in PCIe r5.0, sec 6.28, but the format is derived from PNP ISA, which *does* say "a small resource data type may be 2-8 bytes in size" (Plug and Play ISA v1.0a, sec 6.2.2.

Check for these invalid tags and return VPD size of zero if we find them. If they occur at the beginning of VPD, assume it's the result of a missing EEPROM.

---
 drivers/pci/vpd.c | 36 +++++++++++++++++++++++++++---------
 1 file changed, 27 insertions(+), 9 deletions(-)
drivers/pci/vpd.c +5 -7
index 9b54dd95e42c..9c2744d79b53 100644
@@ -77,11 +77,7 @@ static size_t pci_vpd_size(struct pci_dev *dev, size_t old_size)
 
 	while (off < old_size && pci_read_vpd(dev, off, 1, header) == 1) {
 		unsigned char tag;
-
-		if (!header[0] && !off) {
-			pci_info(dev, "Invalid VPD tag 00, assume missing optional VPD EPROM\n");
-			return 0;
-		}
+		size_t size;
 
 		if (header[0] & PCI_VPD_LRDT) {
 			/* Large Resource Data Type Tag */
@@ -96,8 +92,16 @@ static size_t pci_vpd_size(struct pci_dev *dev, size_t old_size)
 						 off + 1);
 					return 0;
 				}
-				off += PCI_VPD_LRDT_TAG_SIZE +
-					pci_vpd_lrdt_size(header);
+				size = pci_vpd_lrdt_size(header);
+
+				/*
+				 * Missing EEPROM may read as 0xff.
Quoted 8 lines

drivers/pci/vpd.c static size_t pci_vpd_size(struct pci_dev *dev, size_t old_size) · line 101

Quoted 3 lines
drivers/pci/vpd.c +3 -0
+				 * Length of 0xffff (65535) cannot be valid
+				 * because VPD can't be that large.
+				 */

I'm not fully convinced. Instead of checking for a "no VPD EPROM" read (00/ff) directly, we now do it indirectly based on the internal tag structure. We have pci_vpd_lrdt_size() et al to encapsulate the internal structure. IMO the code is harder to understand now.

I don't quite follow. Previously we checked for 0x00 data ("if (!header[0] && !off)"), but we didn't check directly for 0xff.

If we read 0xff, we took the PCI_VPD_LRDT case, but it wouldn't match ID_STRING, RO_DATA, or RW_DATA, so we'd fall out and check again against ID_STRING, RO_DATA, and RW_DATA, and take the "invalid %s VPD tag" error path because it doesn't match any.

This results in failure for any large resource except ID_STRING, RO_DATA, and RW_DATA, regardless of the size.

My proposed code catches a different set of invalid things. "size > PCI_VPD_MAX_SIZE" will catch any large resource headers with length 0x8001 through 0xffff.

Quoted 6 lines
Quoted 3 lines

Possibly it should actually check for "off + size > PCI_VPD_MAX_SIZE" so e.g., it would catch a 0x20 byte resource starting at 0x7ff0.

For handling the 0xff case w/o additional overhead the following is pending:
https://patchwork.kernel.org/project/linux-pci/patch/[email protected]/

Makes sense, I'll add that in the middle and post a v2 so you can see what you think.

Quoted 46 lines

As far as I can see the VPD structure hasn't changed since it was introduced in PCI 2.2. This was how many years ago? Instead of adding code at least my personal objective would be to make support for such legacy features as simple and maintainable as possible.

Quoted 39 lines
Quoted 39 lines
Quoted 38 lines
drivers/pci/vpd.c +19 -2
+				if (size > PCI_VPD_MAX_SIZE)
+					goto error;
+				off += PCI_VPD_LRDT_TAG_SIZE + size;
 			} else {
 				pci_warn(dev, "invalid large VPD tag %02x at offset %zu",
 					 tag, off);
@@ -105,14 +109,28 @@ static size_t pci_vpd_size(struct pci_dev *dev, size_t old_size)
 			}
 		} else {
 			/* Short Resource Data Type Tag */
-			off += PCI_VPD_SRDT_TAG_SIZE +
-				pci_vpd_srdt_size(header);
 			tag = pci_vpd_srdt_tag(header);
+			size = pci_vpd_srdt_size(header);
+
+			/*
+			 * Missing EEPROM may read as 0x00.  A small item
+			 * must be at least 2 bytes.
+			 */
+			if (size == 0)
+				goto error;
+
+			off += PCI_VPD_SRDT_TAG_SIZE + size;
 			if (tag == PCI_VPD_STIN_END)	/* End tag descriptor */
 				return off;
 		}
 	}
 	return 0;
+
+error:
+	pci_info(dev, "invalid VPD tag %#04x at offset %zu%s\n",
+		 header[0], off, off == 0 ?
+		 "; assume missing optional EEPROM" : "");
+	return 0;
 }
 
 /*
Reviewed-by on v1 3/5
#24 lore raw

Re: [PATCH 3/5] PCI/VPD: Consolidate missing EEPROM checks

In reply to Bjorn Helgaas
Quoted 14 lines · Bjorn Helgaas

From: Bjorn Helgaas <[email protected]>

A missing VPD EEPROM typically reads as either all 0xff or all zeroes. Both cases lead to invalid VPD resource items. A 0xff tag would be a Large Resource with length 0xffff (65535). That's invalid because VPD can only be 32768 bytes, limited by the size of the address register in the VPD Capability.

A VPD that reads as all zeroes is also invalid because a 0x00 tag is a Small Resource with length 0, which would result in an item of length 1. This isn't explicitly illegal in PCIe r5.0, sec 6.28, but the format is derived from PNP ISA, which *does* say "a small resource data type may be 2-8 bytes in size" (Plug and Play ISA v1.0a, sec 6.2.2.

Quoted 9 lines

Check for these invalid tags and return VPD size of zero if we find them. If they occur at the beginning of VPD, assume it's the result of a missing EEPROM.

---
  drivers/pci/vpd.c | 36 +++++++++++++++++++++++++++---------
  1 file changed, 27 insertions(+), 9 deletions(-)

Very good idea.

Cheers,

Hannes

Signature · 4 lines
Dr. Hannes Reinecke                Kernel Storage Architect
[email protected]                              +49 911 74053 688
SUSE Software Solutions GmbH, Maxfeldstr. 5, 90409 Nürnberg
HRB 36809 (AG Nürnberg), Geschäftsführer: Felix Imendörffer
#25 lore raw

PATCH 4/5 PCI/VPD: Don't check Large Resource types for validity

In reply to Bjorn Helgaas

From: Bjorn Helgaas <[email protected]>

VPD consists of a series of Small and Large Resources. Computing the size of VPD requires only the length of each, which is specified in the generic tag of each resource. We only expect to see ID_STRING, RO_DATA, and RW_DATA in VPD, but it's not a problem if it contains other resource types.

Drop the validity checking of Large Resource items.

---
 drivers/pci/vpd.c | 37 ++++++++++++++-----------------------
 1 file changed, 14 insertions(+), 23 deletions(-)
drivers/pci/vpd.c +14 -23
index 9c2744d79b53..d7a4a9f05bd6 100644
@@ -82,31 +82,22 @@ static size_t pci_vpd_size(struct pci_dev *dev, size_t old_size)
 		if (header[0] & PCI_VPD_LRDT) {
 			/* Large Resource Data Type Tag */
 			tag = pci_vpd_lrdt_tag(header);
-			/* Only read length from known tag items */
-			if ((tag == PCI_VPD_LTIN_ID_STRING) ||
-			    (tag == PCI_VPD_LTIN_RO_DATA) ||
-			    (tag == PCI_VPD_LTIN_RW_DATA)) {
-				if (pci_read_vpd(dev, off+1, 2,
-						 &header[1]) != 2) {
-					pci_warn(dev, "failed VPD read at offset %zu",
-						 off + 1);
-					return 0;
-				}
-				size = pci_vpd_lrdt_size(header);
-
-				/*
-				 * Missing EEPROM may read as 0xff.
-				 * Length of 0xffff (65535) cannot be valid
-				 * because VPD can't be that large.
-				 */
-				if (size > PCI_VPD_MAX_SIZE)
-					goto error;
-				off += PCI_VPD_LRDT_TAG_SIZE + size;
-			} else {
-				pci_warn(dev, "invalid large VPD tag %02x at offset %zu",
-					 tag, off);
+			if (pci_read_vpd(dev, off + 1, 2, &header[1]) != 2) {
+				pci_warn(dev, "failed VPD read at offset %zu",
+					 off + 1);
 				return 0;
 			}
+			size = pci_vpd_lrdt_size(header);
+
+			/*
+			 * Missing EEPROM may read as 0xff.  Length of
+			 * 0xffff (65535) cannot be valid because VPD can't
+			 * be that large.
+			 */
+			if (size > PCI_VPD_MAX_SIZE)
+				goto error;
+
+			off += PCI_VPD_LRDT_TAG_SIZE + size;
 		} else {
 			/* Short Resource Data Type Tag */
 			tag = pci_vpd_srdt_tag(header);
Signature · 1 line
2.25.1
Reviewed-by on v1 4/5
#26 lore raw

Re: [PATCH 4/5] PCI/VPD: Don't check Large Resource types for validity

Quoted 55 lines · Bjorn Helgaas

From: Bjorn Helgaas <[email protected]>

VPD consists of a series of Small and Large Resources. Computing the size of VPD requires only the length of each, which is specified in the generic tag of each resource. We only expect to see ID_STRING, RO_DATA, and RW_DATA in VPD, but it's not a problem if it contains other resource types.

Drop the validity checking of Large Resource items.

---
  drivers/pci/vpd.c | 37 ++++++++++++++-----------------------
  1 file changed, 14 insertions(+), 23 deletions(-)
drivers/pci/vpd.c +7 -23
index 9c2744d79b53..d7a4a9f05bd6 100644
@@ -82,31 +82,22 @@ static size_t pci_vpd_size(struct pci_dev *dev, size_t old_size)
  		if (header[0] & PCI_VPD_LRDT) {
  			/* Large Resource Data Type Tag */
  			tag = pci_vpd_lrdt_tag(header);
-			/* Only read length from known tag items */
-			if ((tag == PCI_VPD_LTIN_ID_STRING) ||
-			    (tag == PCI_VPD_LTIN_RO_DATA) ||
-			    (tag == PCI_VPD_LTIN_RW_DATA)) {
-				if (pci_read_vpd(dev, off+1, 2,
-						 &header[1]) != 2) {
-					pci_warn(dev, "failed VPD read at offset %zu",
-						 off + 1);
-					return 0;
-				}
-				size = pci_vpd_lrdt_size(header);
-
-				/*
-				 * Missing EEPROM may read as 0xff.
-				 * Length of 0xffff (65535) cannot be valid
-				 * because VPD can't be that large.
-				 */
-				if (size > PCI_VPD_MAX_SIZE)
-					goto error;
-				off += PCI_VPD_LRDT_TAG_SIZE + size;
-			} else {
-				pci_warn(dev, "invalid large VPD tag %02x at offset %zu",
-					 tag, off);
+			if (pci_read_vpd(dev, off + 1, 2, &header[1]) != 2) {
+				pci_warn(dev, "failed VPD read at offset %zu",
+					 off + 1);
  				return 0;
  			}
+			size = pci_vpd_lrdt_size(header);
+
+			/*
+			 * Missing EEPROM may read as 0xff.  Length of

drivers/pci/vpd.c static size_t pci_vpd_size(struct pci_dev *dev, size_t old_size) · line 103

Quoted 10 lines
drivers/pci/vpd.c +7 -0
+			 * 0xffff (65535) cannot be valid because VPD can't
+			 * be that large.
+			 */
+			if (size > PCI_VPD_MAX_SIZE)
+				goto error;
+
+			off += PCI_VPD_LRDT_TAG_SIZE + size;
  		} else {
  			/* Short Resource Data Type Tag */
  			tag = pci_vpd_srdt_tag(header);

I'm not entirely happy with this; we really have to rely on well-formed VPD tags for the protocol to work correctly, and that's why I took the cautious approach. But with the check for missing EEPROM I hope we've covered the most common causes for invalid tags. Let's see how it goes.

Cheers,

Hannes

Signature · 4 lines
Dr. Hannes Reinecke                Kernel Storage Architect
[email protected]                              +49 911 74053 688
SUSE Software Solutions GmbH, Maxfeldstr. 5, 90409 Nürnberg
HRB 36809 (AG Nürnberg), Geschäftsführer: Felix Imendörffer
#27 lore raw

Re: [PATCH 4/5] PCI/VPD: Don't check Large Resource types for validity

Quoted 62 lines · Hannes Reinecke
Quoted 61 lines · Bjorn Helgaas

From: Bjorn Helgaas <[email protected]>

VPD consists of a series of Small and Large Resources. Computing the size of VPD requires only the length of each, which is specified in the generic tag of each resource. We only expect to see ID_STRING, RO_DATA, and RW_DATA in VPD, but it's not a problem if it contains other resource types.

Drop the validity checking of Large Resource items.

---
  drivers/pci/vpd.c | 37 ++++++++++++++-----------------------
  1 file changed, 14 insertions(+), 23 deletions(-)
drivers/pci/vpd.c +14 -23
index 9c2744d79b53..d7a4a9f05bd6 100644
@@ -82,31 +82,22 @@ static size_t pci_vpd_size(struct pci_dev *dev, size_t old_size)
  		if (header[0] & PCI_VPD_LRDT) {
  			/* Large Resource Data Type Tag */
  			tag = pci_vpd_lrdt_tag(header);
-			/* Only read length from known tag items */
-			if ((tag == PCI_VPD_LTIN_ID_STRING) ||
-			    (tag == PCI_VPD_LTIN_RO_DATA) ||
-			    (tag == PCI_VPD_LTIN_RW_DATA)) {
-				if (pci_read_vpd(dev, off+1, 2,
-						 &header[1]) != 2) {
-					pci_warn(dev, "failed VPD read at offset %zu",
-						 off + 1);
-					return 0;
-				}
-				size = pci_vpd_lrdt_size(header);
-
-				/*
-				 * Missing EEPROM may read as 0xff.
-				 * Length of 0xffff (65535) cannot be valid
-				 * because VPD can't be that large.
-				 */
-				if (size > PCI_VPD_MAX_SIZE)
-					goto error;
-				off += PCI_VPD_LRDT_TAG_SIZE + size;
-			} else {
-				pci_warn(dev, "invalid large VPD tag %02x at offset %zu",
-					 tag, off);
+			if (pci_read_vpd(dev, off + 1, 2, &header[1]) != 2) {
+				pci_warn(dev, "failed VPD read at offset %zu",
+					 off + 1);
  				return 0;
  			}
+			size = pci_vpd_lrdt_size(header);
+
+			/*
+			 * Missing EEPROM may read as 0xff.  Length of
+			 * 0xffff (65535) cannot be valid because VPD can't
+			 * be that large.
+			 */
+			if (size > PCI_VPD_MAX_SIZE)
+				goto error;
+
+			off += PCI_VPD_LRDT_TAG_SIZE + size;
Quoted 8 lines

drivers/pci/vpd.c static size_t pci_vpd_size(struct pci_dev *dev, size_t old_size) · line 103

Quoted 4 lines
drivers/pci/vpd.c +0 -0
  		} else {
  			/* Short Resource Data Type Tag */
  			tag = pci_vpd_srdt_tag(header);

I'm not entirely happy with this; we really have to rely on well-formed VPD tags for the protocol to work correctly, and that's why I took the cautious approach. But with the check for missing EEPROM I hope we've covered the most common causes for invalid tags. Let's see how it goes.

True. The tags need to be well-formed in the sense of having intelligible lengths so we can identify the beginning and end of each resource. But we don't actually depend on the resource name (ID_STRING, etc) or the content.

Quoted 10 lines

Cheers,

Hannes

Signature · 4 lines
Dr. Hannes Reinecke                Kernel Storage Architect
[email protected]                              +49 911 74053 688
SUSE Software Solutions GmbH, Maxfeldstr. 5, 90409 Nürnberg
HRB 36809 (AG Nürnberg), Geschäftsführer: Felix Imendörffer
#28 lore raw

PATCH 5/5 PCI/VPD: Allow access to valid parts of VPD if some is invalid

In reply to Bjorn Helgaas

From: Bjorn Helgaas <[email protected]>

Previously, if we found any error in the VPD, we returned size 0, which prevents access to all of VPD. But there may be valid resources in VPD before the error, and there's no reason to prevent access to those.

"off" covers only VPD resources known to have valid header tags. In case of error, return "off" (which may be zero if we haven't found any valid header tags at all).

---
 drivers/pci/vpd.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
drivers/pci/vpd.c +3 -3
index d7a4a9f05bd6..92acbbcc8059 100644
@@ -85,7 +85,7 @@ static size_t pci_vpd_size(struct pci_dev *dev, size_t old_size)
 			if (pci_read_vpd(dev, off + 1, 2, &header[1]) != 2) {
 				pci_warn(dev, "failed VPD read at offset %zu",
 					 off + 1);
-				return 0;
+				return off;
 			}
 			size = pci_vpd_lrdt_size(header);
 
@@ -115,13 +115,13 @@ static size_t pci_vpd_size(struct pci_dev *dev, size_t old_size)
 				return off;
 		}
 	}
-	return 0;
+	return off;
 
 error:
 	pci_info(dev, "invalid VPD tag %#04x at offset %zu%s\n",
 		 header[0], off, off == 0 ?
 		 "; assume missing optional EEPROM" : "");
-	return 0;
+	return off;
 }
 
 /*
Signature · 1 line
2.25.1
Reviewed-by on v1 5/5
#29 lore raw

Re: [PATCH 5/5] PCI/VPD: Allow access to valid parts of VPD if some is invalid

Quoted 6 lines · Bjorn Helgaas

From: Bjorn Helgaas <[email protected]>

Previously, if we found any error in the VPD, we returned size 0, which prevents access to all of VPD. But there may be valid resources in VPD before the error, and there's no reason to prevent access to those.

Quoted 9 lines

"off" covers only VPD resources known to have valid header tags. In case of error, return "off" (which may be zero if we haven't found any valid header tags at all).

---
  drivers/pci/vpd.c | 6 +++---
  1 file changed, 3 insertions(+), 3 deletions(-)

D'oh. Of course.

Cheers,

Hannes

Signature · 4 lines
Dr. Hannes Reinecke                Kernel Storage Architect
[email protected]                              +49 911 74053 688
SUSE Software Solutions GmbH, Maxfeldstr. 5, 90409 Nürnberg
HRB 36809 (AG Nürnberg), Geschäftsführer: Felix Imendörffer
#30 lore raw
In reply to Heiner Kallweit
Quoted 9 lines · Heiner Kallweit

Series with further improvements to PCI VPD handling.

Heiner Kallweit (5):
PCI/VPD: Refactor pci_vpd_size
PCI: Clean up VPD constants and functions in pci.h PCI/VPD: Remove old_size argument from pci_vpd_size PCI/VPD: Make pci_vpd_wait uninterruptible PCI/VPD: Remove pci_vpd member flag

Quoted 3 lines
 drivers/pci/vpd.c   | 106 ++++++++++++++++----------------------------
 include/linux/pci.h |  43 ------------------
 2 files changed, 37 insertions(+), 112 deletions(-)

Applied to pci/vpd for v5.15, thanks!

#31 lore raw
Quoted 10 lines · Bjorn Helgaas
Quoted 9 lines · Heiner Kallweit

Series with further improvements to PCI VPD handling.

Heiner Kallweit (5):
PCI/VPD: Refactor pci_vpd_size
PCI: Clean up VPD constants and functions in pci.h PCI/VPD: Remove old_size argument from pci_vpd_size PCI/VPD: Make pci_vpd_wait uninterruptible PCI/VPD: Remove pci_vpd member flag

Quoted 6 lines
Quoted 3 lines
 drivers/pci/vpd.c   | 106 ++++++++++++++++----------------------------
 include/linux/pci.h |  43 ------------------
 2 files changed, 37 insertions(+), 112 deletions(-)

Applied to pci/vpd for v5.15, thanks!

pci/vpd hasn't been merged yet into next, is this intentional?

#32 lore raw
Quoted 11 lines · Heiner Kallweit
Quoted 10 lines · Bjorn Helgaas
Quoted 9 lines · Heiner Kallweit

Series with further improvements to PCI VPD handling.

Heiner Kallweit (5):
PCI/VPD: Refactor pci_vpd_size
PCI: Clean up VPD constants and functions in pci.h PCI/VPD: Remove old_size argument from pci_vpd_size PCI/VPD: Make pci_vpd_wait uninterruptible PCI/VPD: Remove pci_vpd member flag

Quoted 7 lines
Quoted 6 lines
Quoted 3 lines
 drivers/pci/vpd.c   | 106 ++++++++++++++++----------------------------
 include/linux/pci.h |  43 ------------------
 2 files changed, 37 insertions(+), 112 deletions(-)

Applied to pci/vpd for v5.15, thanks!

pci/vpd hasn't been merged yet into next, is this intentional?

I let the 0-day bot build things before merging into next. But thanks for the reminder, there are a couple branches I should merge.

Bjorn