Skip to contentSkip to conversation

bus/pci: harmonize and document rte_pci_read_config return value

PATCH Superseded Luca Boccassi 4 participants 34 messages Aug 14, 2018 – Oct 28, 2018 Network drivers

lore
Share & tools
lore
#0 lore raw

PATCH 1/2 bus/pci: harmonize and document rte_pci_read_config return value

On Linux, rte_pci_read_config on success returns the number of read bytes, but on BSD it returns 0.
Document the return values, and have BSD behave as Linux does.

At least one case (bnx2x PMD) treats 0 as an error, so the change makes sense also for that.

---
 drivers/bus/pci/bsd/pci.c     | 4 +++-
 drivers/bus/pci/rte_bus_pci.h | 2 ++
 2 files changed, 5 insertions(+), 1 deletion(-)
drivers/bus/pci/bsd/pci.c +3 -1
index 655b34b7e4..175d83cf1b 100644
@@ -439,6 +439,8 @@ int rte_pci_read_config(const struct rte_pci_device *dev,
 {
 	int fd = -1;
 	int size;
+	/* Copy Linux implementation's behaviour */
+	const int return_len = len;
 	struct pci_io pi = {
 		.pi_sel = {
 			.pc_domain = dev->addr.domain,
@@ -469,7 +471,7 @@ int rte_pci_read_config(const struct rte_pci_device *dev,
 	}
 	close(fd);
 
-	return 0;
+	return return_len;
 
  error:
 	if (fd >= 0)
drivers/bus/pci/rte_bus_pci.h +2 -0
index 0d1955ffe0..df8f64798d 100644
@@ -219,6 +219,8 @@ void rte_pci_unregister(struct rte_pci_driver *driver);
  *   The length of the data buffer.
  * @param offset
  *   The offset into PCI config space
+ * @return
+ *  Number of bytes read on success, negative on error.
  */
 int rte_pci_read_config(const struct rte_pci_device *device,
 		void *buf, size_t len, off_t offset);
Signature · 1 line
2.18.0
#1 lore raw

PATCH 1/2 bus/pci: harmonize and document rte_pci_read_config return value

On Linux, rte_pci_read_config on success returns the number of read bytes, but on BSD it returns 0.
Document the return values, and have BSD behave as Linux does.

At least one case (bnx2x PMD) treats 0 as an error, so the change makes sense also for that.

---
 drivers/bus/pci/bsd/pci.c     | 4 +++-
 drivers/bus/pci/rte_bus_pci.h | 2 ++
 2 files changed, 5 insertions(+), 1 deletion(-)
drivers/bus/pci/bsd/pci.c +3 -1
index 655b34b7e4..175d83cf1b 100644
@@ -439,6 +439,8 @@ int rte_pci_read_config(const struct rte_pci_device *dev,
 {
 	int fd = -1;
 	int size;
+	/* Copy Linux implementation's behaviour */
+	const int return_len = len;
 	struct pci_io pi = {
 		.pi_sel = {
 			.pc_domain = dev->addr.domain,
@@ -469,7 +471,7 @@ int rte_pci_read_config(const struct rte_pci_device *dev,
 	}
 	close(fd);
 
-	return 0;
+	return return_len;
 
  error:
 	if (fd >= 0)
drivers/bus/pci/rte_bus_pci.h +2 -0
index 0d1955ffe0..df8f64798d 100644
@@ -219,6 +219,8 @@ void rte_pci_unregister(struct rte_pci_driver *driver);
  *   The length of the data buffer.
  * @param offset
  *   The offset into PCI config space
+ * @return
+ *  Number of bytes read on success, negative on error.
  */
 int rte_pci_read_config(const struct rte_pci_device *device,
 		void *buf, size_t len, off_t offset);
Signature · 1 line
2.18.0
#2 lore raw

PATCH 2/2 virtio: fix PCI config err handling

From: Brian Russell <[email protected]>

In virtio_read_caps and vtpci_msix_detect, rte_pci_read_config returns the number of bytes read from PCI config or < 0 on error. If less than the expected number of bytes are read then log the failure and return rather than carrying on with garbage.

Fixes 6ba1f63b5ab0 ("virtio: support specification 1.0")

---
v2: handle additional rte_pci_read_config incomplete reads

 drivers/net/virtio/virtio_pci.c | 35 +++++++++++++++++++++------------
 1 file changed, 22 insertions(+), 13 deletions(-)
drivers/net/virtio/virtio_pci.c +22 -13
index 6bd22e54a6..ff6f96f361 100644
@@ -560,6 +560,7 @@ virtio_read_caps(struct rte_pci_device *dev, struct virtio_hw *hw)
 	uint8_t pos;
 	struct virtio_pci_cap cap;
 	int ret;
+	uint32_t multiplier;
 
 	if (rte_pci_map_device(dev)) {
 		PMD_INIT_LOG(DEBUG, "failed to map pci device!");
@@ -567,16 +568,18 @@ virtio_read_caps(struct rte_pci_device *dev, struct virtio_hw *hw)
 	}
 
 	ret = rte_pci_read_config(dev, &pos, 1, PCI_CAPABILITY_LIST);
-	if (ret < 0) {
-		PMD_INIT_LOG(DEBUG, "failed to read pci capability list");
+	if (ret != 1) {
+		PMD_INIT_LOG(DEBUG,
+			     "failed to read pci capability list, ret %d", ret);
 		return -1;
 	}
 
 	while (pos) {
 		ret = rte_pci_read_config(dev, &cap, sizeof(cap), pos);
-		if (ret < 0) {
-			PMD_INIT_LOG(ERR,
-				"failed to read pci cap at pos: %x", pos);
+		if (ret != sizeof(cap)) {
+			PMD_INIT_LOG(DEBUG,
+				     "failed to read pci cap at pos: %x ret %d",
+				     pos, ret);
 			break;
 		}
 
@@ -610,9 +613,13 @@ virtio_read_caps(struct rte_pci_device *dev, struct virtio_hw *hw)
 			hw->common_cfg = get_cfg_addr(dev, &cap);
 			break;
 		case VIRTIO_PCI_CAP_NOTIFY_CFG:
-			rte_pci_read_config(dev, &hw->notify_off_multiplier,
-					4, pos + sizeof(cap));
-			hw->notify_base = get_cfg_addr(dev, &cap);
+			/* Avoid half-reads into hw */
+			ret = rte_pci_read_config(dev, &multiplier, 4,
+					pos + sizeof(cap));
+			if (ret == 4) {
+				hw->notify_off_multiplier = multiplier;
+				hw->notify_base = get_cfg_addr(dev, &cap);
+			}
 			break;
 		case VIRTIO_PCI_CAP_DEVICE_CFG:
 			hw->dev_cfg = get_cfg_addr(dev, &cap);
@@ -693,16 +700,18 @@ vtpci_msix_detect(struct rte_pci_device *dev)
 	int ret;
 
 	ret = rte_pci_read_config(dev, &pos, 1, PCI_CAPABILITY_LIST);
-	if (ret < 0) {
-		PMD_INIT_LOG(DEBUG, "failed to read pci capability list");
+	if (ret != 1) {
+		PMD_INIT_LOG(DEBUG,
+			     "failed to read pci capability list, ret %d", ret);
 		return VIRTIO_MSIX_NONE;
 	}
 
 	while (pos) {
 		ret = rte_pci_read_config(dev, &cap, sizeof(cap), pos);
-		if (ret < 0) {
-			PMD_INIT_LOG(ERR,
-				"failed to read pci cap at pos: %x", pos);
+		if (ret != sizeof(cap)) {
+			PMD_INIT_LOG(DEBUG,
+				     "failed to read pci cap at pos: %x ret %d",
+				     pos, ret);
 			break;
 		}
 
Signature · 1 line
2.18.0
#3 lore raw

Re: [PATCH 2/2] virtio: fix PCI config err handling

Quoted 24 lines · Luca Boccassi

From: Brian Russell <[email protected]>

In virtio_read_caps and vtpci_msix_detect, rte_pci_read_config returns
the number of bytes read from PCI config or < 0 on error. If less than the expected number of bytes are read then log the failure and return rather than carrying on with garbage.

Fixes 6ba1f63b5ab0 ("virtio: support specification 1.0")

---
v2: handle additional rte_pci_read_config incomplete reads

 drivers/net/virtio/virtio_pci.c | 35 +++++++++++++++++++++--------
----

 1 file changed, 22 insertions(+), 13 deletions(-)
drivers/net/virtio/virtio_pci.c +0 -0
index 6bd22e54a6..ff6f96f361 100644

...

Quoted 19 lines
drivers/net/virtio/virtio_pci.c +0 -0
@@ -610,9 +613,13 @@ virtio_read_caps(struct rte_pci_device *dev,
 struct virtio_hw *hw)
  			hw->common_cfg = get_cfg_addr(dev, &cap);
  			break;

  case VIRTIO_PCI_CAP_NOTIFY_CFG:

drivers/net/virtio/virtio_pci.c +0 -1
-			rte_pci_read_config(dev, &hw-
Quoted 1 line

notify_off_multiplier,

drivers/net/virtio/virtio_pci.c +2 -2
-					4, pos + sizeof(cap));
-			hw->notify_base = get_cfg_addr(dev, &cap);
+			/* Avoid half-reads into hw */
+			ret = rte_pci_read_config(dev, &multiplier,

4,

drivers/net/virtio/virtio_pci.c +3 -0
+					pos + sizeof(cap));
+			if (ret == 4) {
+				hw->notify_off_multiplier =

multiplier;

drivers/net/virtio/virtio_pci.c +1 -0
+				hw->notify_base = get_cfg_addr(dev,

&cap);

drivers/net/virtio/virtio_pci.c +1 -0
+			}
Quoted 3 lines

  break;
  case VIRTIO_PCI_CAP_DEVICE_CFG:
  hw->dev_cfg = get_cfg_addr(dev, &cap);

Tiwei: not 100% sure what's the best way to handle a failure here, this will avoid writing to *hw in case of errors. Let me know if this is OK.

Signature · 2 lines
Kind regards,
Luca Boccassi
#4 lore raw

Re: [PATCH 2/2] virtio: fix PCI config err handling

Quoted 43 lines · Luca Boccassi
Quoted 24 lines · Luca Boccassi

From: Brian Russell <[email protected]>

In virtio_read_caps and vtpci_msix_detect, rte_pci_read_config returns
the number of bytes read from PCI config or < 0 on error. If less than the expected number of bytes are read then log the failure and return rather than carrying on with garbage.

Fixes 6ba1f63b5ab0 ("virtio: support specification 1.0")

---
v2: handle additional rte_pci_read_config incomplete reads

 drivers/net/virtio/virtio_pci.c | 35 +++++++++++++++++++++--------
----

 1 file changed, 22 insertions(+), 13 deletions(-)
drivers/net/virtio/virtio_pci.c +0 -0
index 6bd22e54a6..ff6f96f361 100644

...

Quoted 17 lines
drivers/net/virtio/virtio_pci.c +0 -0
@@ -610,9 +613,13 @@ virtio_read_caps(struct rte_pci_device *dev,
 struct virtio_hw *hw)
  			hw->common_cfg = get_cfg_addr(dev, &cap);
  			break;

  case VIRTIO_PCI_CAP_NOTIFY_CFG:

drivers/net/virtio/virtio_pci.c +0 -1
-			rte_pci_read_config(dev, &hw-
Quoted 1 line

notify_off_multiplier,

drivers/net/virtio/virtio_pci.c +2 -2
-					4, pos + sizeof(cap));
-			hw->notify_base = get_cfg_addr(dev, &cap);
+			/* Avoid half-reads into hw */
+			ret = rte_pci_read_config(dev, &multiplier,

4,

drivers/net/virtio/virtio_pci.c +3 -0
+					pos + sizeof(cap));
+			if (ret == 4) {
+				hw->notify_off_multiplier =

multiplier;

drivers/net/virtio/virtio_pci.c +1 -0
+				hw->notify_base = get_cfg_addr(dev,
Quoted 8 lines
Quoted 5 lines

&cap);

drivers/net/virtio/virtio_pci.c +1 -0
+			}

  break;
  case VIRTIO_PCI_CAP_DEVICE_CFG:
  hw->dev_cfg = get_cfg_addr(dev, &cap);

Tiwei: not 100% sure what's the best way to handle a failure here, this will avoid writing to *hw in case of errors. Let me know if this is OK.

My concern is about reading the virtio capability directly. With this patch, it will give up reading other capabilities when failed to read a full virtio PCI capability structure (the returned bytes are less than the expected bytes) even though when the capability it's reading isn't a virtio vendor specific capability. I'm not quite sure whether it will bring any bad consequence. How about just reading the first two bytes first? Something like this:

https://github.com/freebsd/freebsd/blob/72445a41b3ff/sys/dev/pci/pci.c#L1491-L1497

Thanks

#5 lore raw

Re: [PATCH 2/2] virtio: fix PCI config err handling

Quoted 71 lines · Tiwei Bie
Quoted 52 lines · Luca Boccassi
Quoted 25 lines · Luca Boccassi

From: Brian Russell <[email protected]>

In virtio_read_caps and vtpci_msix_detect, rte_pci_read_config returns
the number of bytes read from PCI config or < 0 on error. If less than the expected number of bytes are read then log the failure and return rather than carrying on with garbage.

Fixes 6ba1f63b5ab0 ("virtio: support specification 1.0")

---
v2: handle additional rte_pci_read_config incomplete reads

 drivers/net/virtio/virtio_pci.c | 35 +++++++++++++++++++++----
----
----

 1 file changed, 22 insertions(+), 13 deletions(-)
drivers/net/virtio/virtio_pci.c +0 -0
index 6bd22e54a6..ff6f96f361 100644

...

Quoted 24 lines
drivers/net/virtio/virtio_pci.c +0 -0
@@ -610,9 +613,13 @@ virtio_read_caps(struct rte_pci_device *dev,
 struct virtio_hw *hw)
  			hw->common_cfg = get_cfg_addr(dev,
 &cap);
  			break;

  case VIRTIO_PCI_CAP_NOTIFY_CFG:

drivers/net/virtio/virtio_pci.c +0 -1
-			rte_pci_read_config(dev, &hw-
Quoted 1 line

notify_off_multiplier,

drivers/net/virtio/virtio_pci.c +0 -2
-					4, pos + sizeof(cap));
-			hw->notify_base = get_cfg_addr(dev,

&cap);

drivers/net/virtio/virtio_pci.c +2 -0
+			/* Avoid half-reads into hw */
+			ret = rte_pci_read_config(dev,

&multiplier,
4,

drivers/net/virtio/virtio_pci.c +3 -0
+					pos + sizeof(cap));
+			if (ret == 4) {
+				hw->notify_off_multiplier =

multiplier;

drivers/net/virtio/virtio_pci.c +1 -0
+				hw->notify_base =

get_cfg_addr(dev,
&cap);

drivers/net/virtio/virtio_pci.c +1 -0
+			}
Quoted 8 lines
Quoted 3 lines

  break;
  case VIRTIO_PCI_CAP_DEVICE_CFG:
  hw->dev_cfg = get_cfg_addr(dev, &cap);

Tiwei: not 100% sure what's the best way to handle a failure here, this
will avoid writing to *hw in case of errors. Let me know if this is OK.

My concern is about reading the virtio capability directly. With this patch, it will give up reading other capabilities when failed to read a full virtio PCI capability structure (the returned bytes are less than the expected bytes) even though when the capability it's reading isn't a virtio vendor specific capability. I'm not quite sure whether it will bring any bad consequence. How about just reading the first two bytes first? Something like this:

Quoted 2 lines

https://github.com/freebsd/freebsd/blob/72445a41b3ff/sys/dev/pci/pci.
c#L1491-L1497

I'm not sure, to be honest. That bit didn't give me trouble at all, so at this point I'd much rather leave it alone so that the maintainers can take care of it how they see fit, if necessary :-)

I've sent a v3 that removes that individual change.

Signature · 2 lines
Kind regards,
Luca Boccassi
#6 lore raw

Re: [PATCH 2/2] virtio: fix PCI config err handling

Quoted 72 lines · Luca Boccassi
Quoted 71 lines · Tiwei Bie
Quoted 52 lines · Luca Boccassi
Quoted 25 lines · Luca Boccassi

From: Brian Russell <[email protected]>

In virtio_read_caps and vtpci_msix_detect, rte_pci_read_config returns
the number of bytes read from PCI config or < 0 on error. If less than the expected number of bytes are read then log the failure and return rather than carrying on with garbage.

Fixes 6ba1f63b5ab0 ("virtio: support specification 1.0")

---
v2: handle additional rte_pci_read_config incomplete reads

 drivers/net/virtio/virtio_pci.c | 35 +++++++++++++++++++++----
----
----

 1 file changed, 22 insertions(+), 13 deletions(-)
drivers/net/virtio/virtio_pci.c +0 -0
index 6bd22e54a6..ff6f96f361 100644

...

Quoted 24 lines
drivers/net/virtio/virtio_pci.c +0 -0
@@ -610,9 +613,13 @@ virtio_read_caps(struct rte_pci_device *dev,
 struct virtio_hw *hw)
  			hw->common_cfg = get_cfg_addr(dev,
 &cap);
  			break;

  case VIRTIO_PCI_CAP_NOTIFY_CFG:

drivers/net/virtio/virtio_pci.c +0 -1
-			rte_pci_read_config(dev, &hw-
Quoted 1 line

notify_off_multiplier,

drivers/net/virtio/virtio_pci.c +0 -2
-					4, pos + sizeof(cap));
-			hw->notify_base = get_cfg_addr(dev,

&cap);

drivers/net/virtio/virtio_pci.c +2 -0
+			/* Avoid half-reads into hw */
+			ret = rte_pci_read_config(dev,

&multiplier,
4,

drivers/net/virtio/virtio_pci.c +3 -0
+					pos + sizeof(cap));
+			if (ret == 4) {
+				hw->notify_off_multiplier =

multiplier;

drivers/net/virtio/virtio_pci.c +1 -0
+				hw->notify_base =

get_cfg_addr(dev,
&cap);

drivers/net/virtio/virtio_pci.c +1 -0
+			}
Quoted 8 lines
Quoted 3 lines

  break;
  case VIRTIO_PCI_CAP_DEVICE_CFG:
  hw->dev_cfg = get_cfg_addr(dev, &cap);

Tiwei: not 100% sure what's the best way to handle a failure here, this
will avoid writing to *hw in case of errors. Let me know if this is OK.

My concern is about reading the virtio capability directly. With this patch, it will give up reading other capabilities when failed to read a full virtio PCI capability structure (the returned bytes are less than the expected bytes) even though when the capability it's reading isn't a virtio vendor specific capability. I'm not quite sure whether it will bring any bad consequence. How about just reading the first two bytes first? Something like this:

Quoted 8 lines
Quoted 2 lines

https://github.com/freebsd/freebsd/blob/72445a41b3ff/sys/dev/pci/pci.
c#L1491-L1497

I'm not sure, to be honest. That bit didn't give me trouble at all, so at this point I'd much rather leave it alone so that the maintainers can take care of it how they see fit, if necessary :-)

I've sent a v3 that removes that individual change.

My concern isn't about the above change (which handled the errors when reading multiplier). In fact, above change looks good to me! :-) I mean the below changes in this patch:

while (pos) {
ret = rte_pci_read_config(dev, &cap, sizeof(cap), pos);
- if (ret < 0) {
- PMD_INIT_LOG(ERR,
- "failed to read pci cap at pos: %x", pos);
+ if (ret != sizeof(cap)) {
+ PMD_INIT_LOG(DEBUG,
+ "failed to read pci cap at pos: %x ret %d",
+ pos, ret);
break;
}

With this change, it will give up reading other capabilities when failed to read a full virtio PCI capability structure (the returned bytes are less than the expected bytes) even though when the capability it's reading isn't a virtio vendor specific capability. Maybe it would be better to read the first two bytes first and check whether it's the capability we want to parse (i.e. vendor capability and MSIX capability). Something like this:

https://github.com/freebsd/freebsd/blob/72445a41b3ff/sys/dev/pci/pci.c#L1491-L1497

How do you think?

Thanks

#7 lore raw

Re: [PATCH 2/2] virtio: fix PCI config err handling

Quoted 121 lines · Tiwei Bie
Quoted 84 lines · Luca Boccassi
Quoted 79 lines · Tiwei Bie
Quoted 60 lines · Luca Boccassi
Quoted 28 lines · Luca Boccassi

From: Brian Russell <[email protected]>

In virtio_read_caps and vtpci_msix_detect, rte_pci_read_config
returns
the number of bytes read from PCI config or < 0 on error. If less than the expected number of bytes are read then log the
failure and return rather than carrying on with garbage.

Fixes 6ba1f63b5ab0 ("virtio: support specification 1.0")

---
v2: handle additional rte_pci_read_config incomplete reads

 drivers/net/virtio/virtio_pci.c | 35 +++++++++++++++++++++
----
----
----

 1 file changed, 22 insertions(+), 13 deletions(-)
drivers/net/virtio/virtio_pci.c +0 -0
index 6bd22e54a6..ff6f96f361 100644

...

Quoted 29 lines
drivers/net/virtio/virtio_pci.c +0 -0
@@ -610,9 +613,13 @@ virtio_read_caps(struct rte_pci_device
 *dev,
 struct virtio_hw *hw)
  			hw->common_cfg = get_cfg_addr(dev,
 &cap);
  			break;

  case VIRTIO_PCI_CAP_NOTIFY_CFG:

drivers/net/virtio/virtio_pci.c +0 -1
-			rte_pci_read_config(dev, &hw-
Quoted 1 line

notify_off_multiplier,

drivers/net/virtio/virtio_pci.c +0 -1
-					4, pos +

sizeof(cap));

drivers/net/virtio/virtio_pci.c +0 -1
-			hw->notify_base = get_cfg_addr(dev,

&cap);

drivers/net/virtio/virtio_pci.c +2 -0
+			/* Avoid half-reads into hw */
+			ret = rte_pci_read_config(dev,

&multiplier,
4,

drivers/net/virtio/virtio_pci.c +3 -0
+					pos + sizeof(cap));
+			if (ret == 4) {
+				hw->notify_off_multiplier =

multiplier;

drivers/net/virtio/virtio_pci.c +1 -0
+				hw->notify_base =

get_cfg_addr(dev,
&cap);

drivers/net/virtio/virtio_pci.c +1 -0
+			}

  break;
  case VIRTIO_PCI_CAP_DEVICE_CFG:
  hw->dev_cfg = get_cfg_addr(dev,

Quoted 8 lines
Quoted 1 line

&cap);

Tiwei: not 100% sure what's the best way to handle a failure here,
this
will avoid writing to *hw in case of errors. Let me know if this is
OK.

My concern is about reading the virtio capability directly. With this patch, it will give up reading other capabilities when failed to read a full virtio PCI capability structure (the returned bytes are less than the expected bytes) even though when the capability it's reading isn't a virtio vendor specific capability. I'm not quite sure whether it will bring any bad consequence. How about just reading the first two bytes first? Something like this:

Quoted 3 lines

https://github.com/freebsd/freebsd/blob/72445a41b3ff/sys/dev/pci/
pci.
c#L1491-L1497

Quoted 7 lines

I'm not sure, to be honest. That bit didn't give me trouble at all, so
at this point I'd much rather leave it alone so that the maintainers
can take care of it how they see fit, if necessary :-)

I've sent a v3 that removes that individual change.

My concern isn't about the above change (which handled the errors when reading multiplier). In fact, above change looks good to me! :-)  I mean the below changes in this patch:

  while (pos) {
  ret = rte_pci_read_config(dev, &cap, sizeof(cap), pos);

drivers/net/virtio/virtio_pci.c +0 -3
-		if (ret < 0) {
-			PMD_INIT_LOG(ERR,
-				"failed to read pci cap at pos: %x",

pos);

drivers/net/virtio/virtio_pci.c +3 -0
+		if (ret != sizeof(cap)) {
+			PMD_INIT_LOG(DEBUG,
+				     "failed to read pci cap at pos:

%x ret %d",

(quoted diff) +1 -0
+				     pos, ret);

  break;
  }

With this change, it will give up reading other capabilities when failed to read a full virtio PCI capability structure (the returned bytes are less than the expected bytes) even though when the capability it's reading isn't a virtio vendor specific capability. Maybe it would be better to read the first two bytes first and check whether it's the capability we want to parse (i.e. vendor capability and MSIX capability). Something like this:

Quoted 6 lines

https://github.com/freebsd/freebsd/blob/72445a41b3ff/sys/dev/pci/pci.
c#L1491-L1497

How do you think?

Thanks

Ah I see what you mean now, sorry. Would something like the following be what you are looking for:

		ret = rte_pci_read_config(dev, &cap, 2, pos);
		if (ret != 2) {
			PMD_INIT_LOG(DEBUG,
				     "failed to read pci cap at pos: %x ret %d",
				     pos, ret);
			break;
		}
		if (cap.cap_vndr != PCI_CAP_ID_MSIX &&
				cap.cap_vndr != PCI_CAP_ID_VNDR) {
			goto next;
		}

		ret = rte_pci_read_config(dev, &cap, sizeof(cap), pos);
		if (ret != sizeof(cap)) {
			PMD_INIT_LOG(DEBUG,
				     "failed to read pci cap at pos: %x ret %d",
				     pos, ret);
			break;
		}
Signature · 2 lines
Kind regards,
Luca Boccassi
#8 lore raw

Re: [PATCH 2/2] virtio: fix PCI config err handling

Quoted 144 lines · Luca Boccassi
Quoted 121 lines · Tiwei Bie
Quoted 84 lines · Luca Boccassi
Quoted 79 lines · Tiwei Bie
Quoted 60 lines · Luca Boccassi
Quoted 28 lines · Luca Boccassi

From: Brian Russell <[email protected]>

In virtio_read_caps and vtpci_msix_detect, rte_pci_read_config
returns
the number of bytes read from PCI config or < 0 on error. If less than the expected number of bytes are read then log the
failure and return rather than carrying on with garbage.

Fixes 6ba1f63b5ab0 ("virtio: support specification 1.0")

---
v2: handle additional rte_pci_read_config incomplete reads

 drivers/net/virtio/virtio_pci.c | 35 +++++++++++++++++++++
----
----
----

 1 file changed, 22 insertions(+), 13 deletions(-)
drivers/net/virtio/virtio_pci.c +0 -0
index 6bd22e54a6..ff6f96f361 100644

...

Quoted 29 lines
drivers/net/virtio/virtio_pci.c +0 -0
@@ -610,9 +613,13 @@ virtio_read_caps(struct rte_pci_device
 *dev,
 struct virtio_hw *hw)
  			hw->common_cfg = get_cfg_addr(dev,
 &cap);
  			break;

  case VIRTIO_PCI_CAP_NOTIFY_CFG:

drivers/net/virtio/virtio_pci.c +0 -1
-			rte_pci_read_config(dev, &hw-
Quoted 1 line

notify_off_multiplier,

drivers/net/virtio/virtio_pci.c +0 -1
-					4, pos +

sizeof(cap));

drivers/net/virtio/virtio_pci.c +0 -1
-			hw->notify_base = get_cfg_addr(dev,

&cap);

drivers/net/virtio/virtio_pci.c +2 -0
+			/* Avoid half-reads into hw */
+			ret = rte_pci_read_config(dev,

&multiplier,
4,

drivers/net/virtio/virtio_pci.c +3 -0
+					pos + sizeof(cap));
+			if (ret == 4) {
+				hw->notify_off_multiplier =

multiplier;

drivers/net/virtio/virtio_pci.c +1 -0
+				hw->notify_base =

get_cfg_addr(dev,
&cap);

drivers/net/virtio/virtio_pci.c +1 -0
+			}

  break;
  case VIRTIO_PCI_CAP_DEVICE_CFG:
  hw->dev_cfg = get_cfg_addr(dev,

Quoted 8 lines
Quoted 1 line

&cap);

Tiwei: not 100% sure what's the best way to handle a failure here,
this
will avoid writing to *hw in case of errors. Let me know if this is
OK.

My concern is about reading the virtio capability directly. With this patch, it will give up reading other capabilities when failed to read a full virtio PCI capability structure (the returned bytes are less than the expected bytes) even though when the capability it's reading isn't a virtio vendor specific capability. I'm not quite sure whether it will bring any bad consequence. How about just reading the first two bytes first? Something like this:

Quoted 3 lines

https://github.com/freebsd/freebsd/blob/72445a41b3ff/sys/dev/pci/
pci.
c#L1491-L1497

Quoted 7 lines

I'm not sure, to be honest. That bit didn't give me trouble at all, so
at this point I'd much rather leave it alone so that the maintainers
can take care of it how they see fit, if necessary :-)

I've sent a v3 that removes that individual change.

My concern isn't about the above change (which handled the errors when reading multiplier). In fact, above change looks good to me! :-)  I mean the below changes in this patch:

  while (pos) {
  ret = rte_pci_read_config(dev, &cap, sizeof(cap), pos);

drivers/net/virtio/virtio_pci.c +0 -3
-		if (ret < 0) {
-			PMD_INIT_LOG(ERR,
-				"failed to read pci cap at pos: %x",

pos);

drivers/net/virtio/virtio_pci.c +3 -0
+		if (ret != sizeof(cap)) {
+			PMD_INIT_LOG(DEBUG,
+				     "failed to read pci cap at pos:

%x ret %d",

(quoted diff) +1 -0
+				     pos, ret);

  break;
  }

With this change, it will give up reading other capabilities when failed to read a full virtio PCI capability structure (the returned bytes are less than the expected bytes) even though when the capability it's reading isn't a virtio vendor specific capability. Maybe it would be better to read the first two bytes first and check whether it's the capability we want to parse (i.e. vendor capability and MSIX capability). Something like this:

Quoted 6 lines

https://github.com/freebsd/freebsd/blob/72445a41b3ff/sys/dev/pci/pci.
c#L1491-L1497

How do you think?

Thanks

Ah I see what you mean now, sorry. Would something like the following be what you are looking for:

		ret = rte_pci_read_config(dev, &cap, 2, pos);
		if (ret != 2) {
			PMD_INIT_LOG(DEBUG,
				     "failed to read pci cap at pos: %x ret %d",
				     pos, ret);
			break;
		}
		if (cap.cap_vndr != PCI_CAP_ID_MSIX &&
				cap.cap_vndr != PCI_CAP_ID_VNDR) {
			goto next;
		}
Quoted 8 lines
		ret = rte_pci_read_config(dev, &cap, sizeof(cap), pos);
		if (ret != sizeof(cap)) {
			PMD_INIT_LOG(DEBUG,
				     "failed to read pci cap at pos: %x ret %d",
				     pos, ret);
			break;
		}

Yeah, that's what I want. :-)
Just one nit, we don't need to read it as a virtio cap when dealing with the MSIX cap. When dealing with the MSIX cap, we just need to read the next two bytes:

https://github.com/DPDK/dpdk/blob/7281cf520f89/drivers/net/virtio/virtio_pci.c#L584-L594
https://github.com/freebsd/freebsd/blob/59e4185a9358/sys/dev/pci/pci.c#L892

Thanks!

Quoted 3 lines
Signature · 2 lines
Kind regards,
Luca Boccassi
#9 lore raw

Re: [PATCH 2/2] virtio: fix PCI config err handling

Quoted 179 lines · Tiwei Bie
Quoted 162 lines · Luca Boccassi
Quoted 136 lines · Tiwei Bie
Quoted 95 lines · Luca Boccassi
Quoted 89 lines · Tiwei Bie
Quoted 70 lines · Luca Boccassi
Quoted 31 lines · Luca Boccassi

From: Brian Russell <[email protected]>

In virtio_read_caps and vtpci_msix_detect, rte_pci_read_config
returns
the number of bytes read from PCI config or < 0 on error. If less than the expected number of bytes are read then log
the
failure and return rather than carrying on with garbage.

Fixes 6ba1f63b5ab0 ("virtio: support specification 1.0")

---
v2: handle additional rte_pci_read_config incomplete reads

 drivers/net/virtio/virtio_pci.c | 35
+++++++++++++++++++++
----
----
----

 1 file changed, 22 insertions(+), 13 deletions(-)
drivers/net/virtio/virtio_pci.c +0 -0
index 6bd22e54a6..ff6f96f361 100644

...

Quoted 31 lines
drivers/net/virtio/virtio_pci.c +0 -0
@@ -610,9 +613,13 @@ virtio_read_caps(struct

rte_pci_device
*dev,
struct virtio_hw *hw)
  hw->common_cfg =
get_cfg_addr(dev,
&cap);
  break;
  case VIRTIO_PCI_CAP_NOTIFY_CFG:

drivers/net/virtio/virtio_pci.c +0 -1
-			rte_pci_read_config(dev, &hw-
Quoted 1 line

notify_off_multiplier,

drivers/net/virtio/virtio_pci.c +0 -1
-					4, pos +

sizeof(cap));

drivers/net/virtio/virtio_pci.c +0 -1
-			hw->notify_base =

get_cfg_addr(dev,
&cap);

drivers/net/virtio/virtio_pci.c +2 -0
+			/* Avoid half-reads into hw */
+			ret = rte_pci_read_config(dev,

&multiplier,
4,

drivers/net/virtio/virtio_pci.c +1 -0
+					pos +

sizeof(cap));

drivers/net/virtio/virtio_pci.c +2 -0
+			if (ret == 4) {
+				hw-
Quoted 1 line

notify_off_multiplier =

multiplier;

drivers/net/virtio/virtio_pci.c +1 -0
+				hw->notify_base =

get_cfg_addr(dev,
&cap);

drivers/net/virtio/virtio_pci.c +1 -0
+			}
Quoted 4 lines

  break;
  case VIRTIO_PCI_CAP_DEVICE_CFG:
  hw->dev_cfg = get_cfg_addr(dev, &cap);

Quoted 7 lines

Tiwei: not 100% sure what's the best way to handle a failure
here,
this
will avoid writing to *hw in case of errors. Let me know if this is
OK.

My concern is about reading the virtio capability directly. With this patch, it will give up reading other capabilities when failed to read a full virtio PCI capability structure (the returned bytes are less than the expected bytes) even though when the capability it's reading isn't a virtio vendor specific capability. I'm not quite sure whether it will bring any bad consequence. How about just reading the first two bytes first? Something like this:

Quoted 4 lines

https://github.com/freebsd/freebsd/blob/72445a41b3ff/sys/dev/
pci/
pci.
c#L1491-L1497

Quoted 8 lines

I'm not sure, to be honest. That bit didn't give me trouble at all,
so
at this point I'd much rather leave it alone so that the maintainers
can take care of it how they see fit, if necessary :-)

I've sent a v3 that removes that individual change.

My concern isn't about the above change (which handled the errors when reading multiplier). In fact, above change looks good to me! :-)  I mean the below changes in this patch:

  while (pos) {
  ret = rte_pci_read_config(dev, &cap, sizeof(cap),
pos);

(quoted diff) +0 -3
-		if (ret < 0) {
-			PMD_INIT_LOG(ERR,
-				"failed to read pci cap at pos:

%x",
pos);

(quoted diff) +3 -0
+		if (ret != sizeof(cap)) {
+			PMD_INIT_LOG(DEBUG,
+				     "failed to read pci cap at

pos:
%x ret %d",

(quoted diff) +1 -0
+				     pos, ret);

  break;
  }

With this change, it will give up reading other capabilities when failed to read a full virtio PCI capability structure (the returned bytes are less than the expected bytes) even though when the capability it's reading isn't a virtio vendor specific capability. Maybe it would be better to read the first two bytes first and check whether it's the capability we want to parse (i.e. vendor capability and MSIX capability). Something like this:

Quoted 7 lines

https://github.com/freebsd/freebsd/blob/72445a41b3ff/sys/dev/pci/
pci.
c#L1491-L1497

How do you think?

Thanks

Ah I see what you mean now, sorry. Would something like the following
be what you are looking for:

		ret = rte_pci_read_config(dev, &cap, 2, pos);
		if (ret != 2) {
			PMD_INIT_LOG(DEBUG,
				     "failed to read pci cap at pos: %x
ret %d",
				     pos, ret);
			break;
		}
		if (cap.cap_vndr != PCI_CAP_ID_MSIX &&
				cap.cap_vndr != PCI_CAP_ID_VNDR) {
			goto next;
		}
Quoted 9 lines
		ret = rte_pci_read_config(dev, &cap, sizeof(cap), pos);
		if (ret != sizeof(cap)) {
			PMD_INIT_LOG(DEBUG,
				     "failed to read pci cap at pos: %x
ret %d",
				     pos, ret);
			break;
		}

Yeah, that's what I want. :-)
Just one nit, we don't need to read it as a virtio cap when dealing with the MSIX cap. When dealing with the MSIX cap, we just need to read the next two bytes:

Quoted 6 lines

https://github.com/DPDK/dpdk/blob/7281cf520f89/drivers/net/virtio/vir
tio_pci.c#L584-L594
https://github.com/freebsd/freebsd/blob/59e4185a9358/sys/dev/pci/pci.
c#L892

Thanks!

Ok, done, see v4.

Signature · 2 lines
Kind regards,
Luca Boccassi
#10 lore raw

PATCH v3 1/2 bus/pci: harmonize and document rte_pci_read_config return value

In reply to Luca Boccassi

On Linux, rte_pci_read_config on success returns the number of read bytes, but on BSD it returns 0.
Document the return values, and have BSD behave as Linux does.

At least one case (bnx2x PMD) treats 0 as an error, so the change makes sense also for that.

---
 drivers/bus/pci/bsd/pci.c     | 4 +++-
 drivers/bus/pci/rte_bus_pci.h | 2 ++
 2 files changed, 5 insertions(+), 1 deletion(-)
drivers/bus/pci/bsd/pci.c +3 -1
index 655b34b7e4..175d83cf1b 100644
@@ -439,6 +439,8 @@ int rte_pci_read_config(const struct rte_pci_device *dev,
 {
 	int fd = -1;
 	int size;
+	/* Copy Linux implementation's behaviour */
+	const int return_len = len;
 	struct pci_io pi = {
 		.pi_sel = {
 			.pc_domain = dev->addr.domain,
@@ -469,7 +471,7 @@ int rte_pci_read_config(const struct rte_pci_device *dev,
 	}
 	close(fd);
 
-	return 0;
+	return return_len;
 
  error:
 	if (fd >= 0)
drivers/bus/pci/rte_bus_pci.h +2 -0
index 0d1955ffe0..df8f64798d 100644
@@ -219,6 +219,8 @@ void rte_pci_unregister(struct rte_pci_driver *driver);
  *   The length of the data buffer.
  * @param offset
  *   The offset into PCI config space
+ * @return
+ *  Number of bytes read on success, negative on error.
  */
 int rte_pci_read_config(const struct rte_pci_device *device,
 		void *buf, size_t len, off_t offset);
Signature · 1 line
2.18.0
#11 lore raw

PATCH v3 2/2 virtio: fix PCI config err handling

From: Brian Russell <[email protected]>

In virtio_read_caps and vtpci_msix_detect, rte_pci_read_config returns the number of bytes read from PCI config or < 0 on error. If less than the expected number of bytes are read then log the failure and return rather than carrying on with garbage.

Fixes 6ba1f63b5ab0 ("virtio: support specification 1.0")

---
v2: handle additional rte_pci_read_config incomplete reads
v3: do not handle rte_pci_read_config of virtio cap, added in v2,
as it's less clear what the right thing to do there is

 drivers/net/virtio/virtio_pci.c | 24 ++++++++++++++----------
 1 file changed, 14 insertions(+), 10 deletions(-)
drivers/net/virtio/virtio_pci.c +14 -10
index 6bd22e54a6..e1df2c3b4d 100644
@@ -567,16 +567,18 @@ virtio_read_caps(struct rte_pci_device *dev, struct virtio_hw *hw)
 	}
 
 	ret = rte_pci_read_config(dev, &pos, 1, PCI_CAPABILITY_LIST);
-	if (ret < 0) {
-		PMD_INIT_LOG(DEBUG, "failed to read pci capability list");
+	if (ret != 1) {
+		PMD_INIT_LOG(DEBUG,
+			     "failed to read pci capability list, ret %d", ret);
 		return -1;
 	}
 
 	while (pos) {
 		ret = rte_pci_read_config(dev, &cap, sizeof(cap), pos);
-		if (ret < 0) {
-			PMD_INIT_LOG(ERR,
-				"failed to read pci cap at pos: %x", pos);
+		if (ret != sizeof(cap)) {
+			PMD_INIT_LOG(DEBUG,
+				     "failed to read pci cap at pos: %x ret %d",
+				     pos, ret);
 			break;
 		}
 
@@ -693,16 +695,18 @@ vtpci_msix_detect(struct rte_pci_device *dev)
 	int ret;
 
 	ret = rte_pci_read_config(dev, &pos, 1, PCI_CAPABILITY_LIST);
-	if (ret < 0) {
-		PMD_INIT_LOG(DEBUG, "failed to read pci capability list");
+	if (ret != 1) {
+		PMD_INIT_LOG(DEBUG,
+			     "failed to read pci capability list, ret %d", ret);
 		return VIRTIO_MSIX_NONE;
 	}
 
 	while (pos) {
 		ret = rte_pci_read_config(dev, &cap, sizeof(cap), pos);
-		if (ret < 0) {
-			PMD_INIT_LOG(ERR,
-				"failed to read pci cap at pos: %x", pos);
+		if (ret != sizeof(cap)) {
+			PMD_INIT_LOG(DEBUG,
+				     "failed to read pci cap at pos: %x ret %d",
+				     pos, ret);
 			break;
 		}
 
Signature · 1 line
2.18.0
#12 lore raw

PATCH v4 1/2 bus/pci: harmonize and document rte_pci_read_config return value

In reply to Luca Boccassi

On Linux, rte_pci_read_config on success returns the number of read bytes, but on BSD it returns 0.
Document the return values, and have BSD behave as Linux does.

At least one case (bnx2x PMD) treats 0 as an error, so the change makes sense also for that.

---
 drivers/bus/pci/bsd/pci.c     | 4 +++-
 drivers/bus/pci/rte_bus_pci.h | 2 ++
 2 files changed, 5 insertions(+), 1 deletion(-)
drivers/bus/pci/bsd/pci.c +3 -1
index 655b34b7e4..175d83cf1b 100644
@@ -439,6 +439,8 @@ int rte_pci_read_config(const struct rte_pci_device *dev,
 {
 	int fd = -1;
 	int size;
+	/* Copy Linux implementation's behaviour */
+	const int return_len = len;
 	struct pci_io pi = {
 		.pi_sel = {
 			.pc_domain = dev->addr.domain,
@@ -469,7 +471,7 @@ int rte_pci_read_config(const struct rte_pci_device *dev,
 	}
 	close(fd);
 
-	return 0;
+	return return_len;
 
  error:
 	if (fd >= 0)
drivers/bus/pci/rte_bus_pci.h +2 -0
index 0d1955ffe0..df8f64798d 100644
@@ -219,6 +219,8 @@ void rte_pci_unregister(struct rte_pci_driver *driver);
  *   The length of the data buffer.
  * @param offset
  *   The offset into PCI config space
+ * @return
+ *  Number of bytes read on success, negative on error.
  */
 int rte_pci_read_config(const struct rte_pci_device *device,
 		void *buf, size_t len, off_t offset);
Signature · 1 line
2.18.0
#13 lore raw

PATCH v4 2/2 virtio: fix PCI config err handling

From: Brian Russell <[email protected]>

In virtio_read_caps and vtpci_msix_detect, rte_pci_read_config returns the number of bytes read from PCI config or < 0 on error. If less than the expected number of bytes are read then log the failure and return rather than carrying on with garbage.

Fixes 6ba1f63b5ab0 ("virtio: support specification 1.0")

---
v2: handle additional rte_pci_read_config incomplete reads
v3: do not handle rte_pci_read_config of virtio cap, added in v2,
as it's less clear what the right thing to do there is
v4: do a more robust check - first check what the vendor is, and
skip the cap entirely if it's not what we are looking for.

 drivers/net/virtio/virtio_pci.c | 57 ++++++++++++++++++++++++---------
 1 file changed, 42 insertions(+), 15 deletions(-)
drivers/net/virtio/virtio_pci.c +42 -15
index 6bd22e54a6..cfefa9789b 100644
@@ -567,16 +567,30 @@ virtio_read_caps(struct rte_pci_device *dev, struct virtio_hw *hw)
 	}
 
 	ret = rte_pci_read_config(dev, &pos, 1, PCI_CAPABILITY_LIST);
-	if (ret < 0) {
-		PMD_INIT_LOG(DEBUG, "failed to read pci capability list");
+	if (ret != 1) {
+		PMD_INIT_LOG(DEBUG,
+			     "failed to read pci capability list, ret %d", ret);
 		return -1;
 	}
 
 	while (pos) {
+		ret = rte_pci_read_config(dev, &cap, 2, pos);
+		if (ret != 2) {
+			PMD_INIT_LOG(DEBUG,
+				     "failed to read pci cap at pos: %x ret %d",
+				     pos, ret);
+			break;
+		}
+		if (cap.cap_vndr != PCI_CAP_ID_MSIX &&
+				cap.cap_vndr != PCI_CAP_ID_VNDR) {
+			goto next;
+		}
+
 		ret = rte_pci_read_config(dev, &cap, sizeof(cap), pos);
-		if (ret < 0) {
-			PMD_INIT_LOG(ERR,
-				"failed to read pci cap at pos: %x", pos);
+		if (ret != sizeof(cap)) {
+			PMD_INIT_LOG(DEBUG,
+				     "failed to read pci cap at pos: %x ret %d",
+				     pos, ret);
 			break;
 		}
 
@@ -689,25 +703,38 @@ enum virtio_msix_status
 vtpci_msix_detect(struct rte_pci_device *dev)
 {
 	uint8_t pos;
-	struct virtio_pci_cap cap;
 	int ret;
 
 	ret = rte_pci_read_config(dev, &pos, 1, PCI_CAPABILITY_LIST);
-	if (ret < 0) {
-		PMD_INIT_LOG(DEBUG, "failed to read pci capability list");
+	if (ret != 1) {
+		PMD_INIT_LOG(DEBUG,
+			     "failed to read pci capability list, ret %d", ret);
 		return VIRTIO_MSIX_NONE;
 	}
 
 	while (pos) {
-		ret = rte_pci_read_config(dev, &cap, sizeof(cap), pos);
-		if (ret < 0) {
-			PMD_INIT_LOG(ERR,
-				"failed to read pci cap at pos: %x", pos);
+		uint8_t cap[2];
+
+		ret = rte_pci_read_config(dev, cap, sizeof(cap), pos);
+		if (ret != sizeof(cap)) {
+			PMD_INIT_LOG(DEBUG,
+				     "failed to read pci cap at pos: %x ret %d",
+				     pos, ret);
 			break;
 		}
 
-		if (cap.cap_vndr == PCI_CAP_ID_MSIX) {
-			uint16_t flags = ((uint16_t *)&cap)[1];
+		if (cap[0] == PCI_CAP_ID_MSIX) {
+			uint16_t flags;
+
+			ret = rte_pci_read_config(dev, &flags, sizeof(flags),
+					pos + sizeof(cap));
+			if (ret != sizeof(flags)) {
+				PMD_INIT_LOG(DEBUG,
+					     "failed to read pci cap at pos:"
+					     " %x ret %d", pos + sizeof(cap),
+					     ret);
+				break;
+			}
 
 			if (flags & PCI_MSIX_ENABLE)
 				return VIRTIO_MSIX_ENABLED;
@@ -715,7 +742,7 @@ vtpci_msix_detect(struct rte_pci_device *dev)
 				return VIRTIO_MSIX_DISABLED;
 		}
 
-		pos = cap.cap_next;
+		pos = cap[1];
 	}
 
 	return VIRTIO_MSIX_NONE;
Signature · 1 line
2.18.0
#14 lore raw

Re: [PATCH v4 2/2] virtio: fix PCI config err handling

Quoted 51 lines · Luca Boccassi

From: Brian Russell <[email protected]>

In virtio_read_caps and vtpci_msix_detect, rte_pci_read_config returns the number of bytes read from PCI config or < 0 on error. If less than the expected number of bytes are read then log the failure and return rather than carrying on with garbage.

Fixes 6ba1f63b5ab0 ("virtio: support specification 1.0")

---
v2: handle additional rte_pci_read_config incomplete reads
v3: do not handle rte_pci_read_config of virtio cap, added in v2,
as it's less clear what the right thing to do there is
v4: do a more robust check - first check what the vendor is, and
skip the cap entirely if it's not what we are looking for.

 drivers/net/virtio/virtio_pci.c | 57 ++++++++++++++++++++++++---------
 1 file changed, 42 insertions(+), 15 deletions(-)
drivers/net/virtio/virtio_pci.c +15 -2
index 6bd22e54a6..cfefa9789b 100644
@@ -567,16 +567,30 @@ virtio_read_caps(struct rte_pci_device *dev, struct virtio_hw *hw)
 	}
 
 	ret = rte_pci_read_config(dev, &pos, 1, PCI_CAPABILITY_LIST);
-	if (ret < 0) {
-		PMD_INIT_LOG(DEBUG, "failed to read pci capability list");
+	if (ret != 1) {
+		PMD_INIT_LOG(DEBUG,
+			     "failed to read pci capability list, ret %d", ret);
 		return -1;
 	}
 
 	while (pos) {
+		ret = rte_pci_read_config(dev, &cap, 2, pos);
+		if (ret != 2) {
+			PMD_INIT_LOG(DEBUG,
+				     "failed to read pci cap at pos: %x ret %d",
+				     pos, ret);
+			break;
+		}
+		if (cap.cap_vndr != PCI_CAP_ID_MSIX &&
+				cap.cap_vndr != PCI_CAP_ID_VNDR) {
+			goto next;
+		}
+
 		ret = rte_pci_read_config(dev, &cap, sizeof(cap), pos);

drivers/net/virtio/virtio_pci.c virtio_read_caps(struct rte_pci_device *dev, struct virtio_hw *hw) · line 596

Quoted 10 lines
drivers/net/virtio/virtio_pci.c +4 -3
-		if (ret < 0) {
-			PMD_INIT_LOG(ERR,
-				"failed to read pci cap at pos: %x", pos);
+		if (ret != sizeof(cap)) {
+			PMD_INIT_LOG(DEBUG,
+				     "failed to read pci cap at pos: %x ret %d",
+				     pos, ret);
 			break;
 		}
 

It seems that I didn't make myself clear in my previous comments. I mean it's better to handle MSIX cap and virtio cap respectively in this function. Currently we're always reading them as virtio caps. As we are strictly requiring that _read_config() should return the required number of bytes, it's not perfect to require it to return "virtio cap size" of bytes while we're trying to read a MSIX cap. So please change the code to something similar to this:

	while (pos) {
		ret = rte_pci_read_config(dev, &cap, 2, pos);
		if (ret != 2) {
			PMD_INIT_LOG(DEBUG,
				     "failed to read pci cap at pos: %x ret %d",
				     pos, ret);
			break;
		}

		if (cap.cap_vndr == PCI_CAP_ID_MSIX) {
			/* Transitional devices would also have this capability,
			 * that's why we also check if msix is enabled.
			 * 1st byte is cap ID; 2nd byte is the position of next
			 * cap; next two bytes are the flags.
			 */
			uint16_t flags;

			ret = rte_pci_read_config(dev, &flags, sizeof(flags),
					pos + 2);
			if (ret != sizeof(flags)) {
				PMD_INIT_LOG(DEBUG,
					"failed to read pci cap at pos: %x ret %d",
					pos + 2, ret);
				break;
			}

			if (flags & PCI_MSIX_ENABLE)
				hw->use_msix = VIRTIO_MSIX_ENABLED;
			else
				hw->use_msix = VIRTIO_MSIX_DISABLED;
		}

		if (cap.cap_vndr != PCI_CAP_ID_VNDR) {
			PMD_INIT_LOG(DEBUG,
				"[%2x] skipping non VNDR cap id: %02x",
				pos, cap.cap_vndr);
			goto next;
		}

		ret = rte_pci_read_config(dev, &cap, sizeof(cap), pos);
		if (ret != sizeof(cap)) {
			PMD_INIT_LOG(DEBUG,
				     "failed to read pci cap at pos: %x ret %d",
				     pos, ret);
			break;
		}

		PMD_INIT_LOG(DEBUG,
			"[%2x] cfg type: %u, bar: %u, offset: %04x, len: %u",
			pos, cap.cfg_type, cap.bar, cap.offset, cap.length);

		switch (cap.cfg_type) {
		......
Quoted 32 lines
drivers/net/virtio/virtio_pci.c +10 -8
@@ -689,25 +703,38 @@ enum virtio_msix_status
 vtpci_msix_detect(struct rte_pci_device *dev)
 {
 	uint8_t pos;
-	struct virtio_pci_cap cap;
 	int ret;
 
 	ret = rte_pci_read_config(dev, &pos, 1, PCI_CAPABILITY_LIST);
-	if (ret < 0) {
-		PMD_INIT_LOG(DEBUG, "failed to read pci capability list");
+	if (ret != 1) {
+		PMD_INIT_LOG(DEBUG,
+			     "failed to read pci capability list, ret %d", ret);
 		return VIRTIO_MSIX_NONE;
 	}
 
 	while (pos) {
-		ret = rte_pci_read_config(dev, &cap, sizeof(cap), pos);
-		if (ret < 0) {
-			PMD_INIT_LOG(ERR,
-				"failed to read pci cap at pos: %x", pos);
+		uint8_t cap[2];
+
+		ret = rte_pci_read_config(dev, cap, sizeof(cap), pos);
+		if (ret != sizeof(cap)) {
+			PMD_INIT_LOG(DEBUG,
+				     "failed to read pci cap at pos: %x ret %d",
+				     pos, ret);
 			break;
 		}
 
-		if (cap.cap_vndr == PCI_CAP_ID_MSIX) {

drivers/net/virtio/virtio_pci.c enum virtio_msix_status · line 734

Quoted 10 lines
drivers/net/virtio/virtio_pci.c +9 -1
-			uint16_t flags = ((uint16_t *)&cap)[1];
+		if (cap[0] == PCI_CAP_ID_MSIX) {
+			uint16_t flags;
+
+			ret = rte_pci_read_config(dev, &flags, sizeof(flags),
+					pos + sizeof(cap));
+			if (ret != sizeof(flags)) {
+				PMD_INIT_LOG(DEBUG,
+					     "failed to read pci cap at pos:"
+					     " %x ret %d", pos + sizeof(cap),

There is a build error:

In file included from drivers/net/virtio/virtio_pci.c:15:
drivers/net/virtio/virtio_pci.c: In function ‘vtpci_msix_detect’:
drivers/net/virtio/virtio_logs.h:13:3: error: format ‘%x’ expects argument of type ‘unsigned int’, but argument 5 has type ‘long unsigned int’ [-Werror=format=]
"%s(): " fmt "\n", __func__, ##args)
^~~~~~~~
drivers/net/virtio/virtio_pci.c:732:5: note: in expansion of macro ‘PMD_INIT_LOG’

     PMD_INIT_LOG(DEBUG,
     ^~~~~~~~~~~~
drivers/net/virtio/virtio_pci.c:734:14: note: format string is defined here
           " %x ret %d", pos + sizeof(cap),
             ~^
             %lx
Quoted 18 lines
drivers/net/virtio/virtio_pci.c +4 -1
+					     ret);
+				break;
+			}
 
 			if (flags & PCI_MSIX_ENABLE)
 				return VIRTIO_MSIX_ENABLED;
@@ -715,7 +742,7 @@ vtpci_msix_detect(struct rte_pci_device *dev)
 				return VIRTIO_MSIX_DISABLED;
 		}
 
-		pos = cap.cap_next;
+		pos = cap[1];
 	}
 
 	return VIRTIO_MSIX_NONE;
Signature · 1 line
2.18.0
#15 lore raw

Re: [PATCH v4 2/2] virtio: fix PCI config err handling

Quoted 74 lines · Tiwei Bie
Quoted 69 lines · Luca Boccassi

From: Brian Russell <[email protected]>

In virtio_read_caps and vtpci_msix_detect, rte_pci_read_config returns
the number of bytes read from PCI config or < 0 on error. If less than the expected number of bytes are read then log the failure and return rather than carrying on with garbage.

Fixes 6ba1f63b5ab0 ("virtio: support specification 1.0")

---
v2: handle additional rte_pci_read_config incomplete reads
v3: do not handle rte_pci_read_config of virtio cap, added in v2, as it's less clear what the right thing to do there is
v4: do a more robust check - first check what the vendor is, and skip the cap entirely if it's not what we are looking for.

 drivers/net/virtio/virtio_pci.c | 57 ++++++++++++++++++++++++-----
----

 1 file changed, 42 insertions(+), 15 deletions(-)
drivers/net/virtio/virtio_pci.c +0 -0
index 6bd22e54a6..cfefa9789b 100644
@@ -567,16 +567,30 @@ virtio_read_caps(struct rte_pci_device *dev,
 struct virtio_hw *hw)
  	}


  ret = rte_pci_read_config(dev, &pos, 1,
PCI_CAPABILITY_LIST);

drivers/net/virtio/virtio_pci.c +0 -2
-	if (ret < 0) {
-		PMD_INIT_LOG(DEBUG, "failed to read pci capability

list");

drivers/net/virtio/virtio_pci.c +3 -0
+	if (ret != 1) {
+		PMD_INIT_LOG(DEBUG,
+			     "failed to read pci capability list,

ret %d", ret);
  return -1;
  }

  while (pos) {

drivers/net/virtio/virtio_pci.c +4 -0
+		ret = rte_pci_read_config(dev, &cap, 2, pos);
+		if (ret != 2) {
+			PMD_INIT_LOG(DEBUG,
+				     "failed to read pci cap at

pos: %x ret %d",

drivers/net/virtio/virtio_pci.c +8 -0
+				     pos, ret);
+			break;
+		}
+		if (cap.cap_vndr != PCI_CAP_ID_MSIX &&
+				cap.cap_vndr != PCI_CAP_ID_VNDR) {
+			goto next;
+		}
+

  ret = rte_pci_read_config(dev, &cap, sizeof(cap), pos);

drivers/net/virtio/virtio_pci.c +0 -3
-		if (ret < 0) {
-			PMD_INIT_LOG(ERR,
-				"failed to read pci cap at pos:

%x", pos);

drivers/net/virtio/virtio_pci.c +3 -0
+		if (ret != sizeof(cap)) {
+			PMD_INIT_LOG(DEBUG,
+				     "failed to read pci cap at

pos: %x ret %d",

drivers/net/virtio/virtio_pci.c +1 -0
+				     pos, ret);
Quoted 3 lines

  break;
  }

Quoted 8 lines

It seems that I didn't make myself clear in my previous comments. I mean it's better to handle MSIX cap and virtio cap respectively in this function. Currently we're always reading them as virtio caps. As we are strictly requiring that _read_config() should return the required number of bytes, it's not perfect to require it to return "virtio cap size" of bytes while we're trying to read a MSIX cap. So please change the code to something similar to this:

Sorry, though you meant in the vtpci_msix_detect function, which I changed. Fixed in v5.

Quoted 55 lines
Quoted 51 lines
drivers/net/virtio/virtio_pci.c +0 -1
@@ -689,25 +703,38 @@ enum virtio_msix_status
  vtpci_msix_detect(struct rte_pci_device *dev)
  {
  	uint8_t pos;
-	struct virtio_pci_cap cap;
  	int ret;


  ret = rte_pci_read_config(dev, &pos, 1,
PCI_CAPABILITY_LIST);

drivers/net/virtio/virtio_pci.c +0 -2
-	if (ret < 0) {
-		PMD_INIT_LOG(DEBUG, "failed to read pci capability

list");

drivers/net/virtio/virtio_pci.c +3 -0
+	if (ret != 1) {
+		PMD_INIT_LOG(DEBUG,
+			     "failed to read pci capability list,

ret %d", ret);
  return VIRTIO_MSIX_NONE;
  }

  while (pos) {

drivers/net/virtio/virtio_pci.c +0 -1
-		ret = rte_pci_read_config(dev, &cap, sizeof(cap),

pos);

drivers/net/virtio/virtio_pci.c +0 -3
-		if (ret < 0) {
-			PMD_INIT_LOG(ERR,
-				"failed to read pci cap at pos:

%x", pos);

drivers/net/virtio/virtio_pci.c +3 -0
+		uint8_t cap[2];
+
+		ret = rte_pci_read_config(dev, cap, sizeof(cap),

pos);

drivers/net/virtio/virtio_pci.c +3 -0
+		if (ret != sizeof(cap)) {
+			PMD_INIT_LOG(DEBUG,
+				     "failed to read pci cap at

pos: %x ret %d",

drivers/net/virtio/virtio_pci.c +1 -0
+				     pos, ret);

  break;
  }

drivers/net/virtio/virtio_pci.c +4 -2
-		if (cap.cap_vndr == PCI_CAP_ID_MSIX) {
-			uint16_t flags = ((uint16_t *)&cap)[1];
+		if (cap[0] == PCI_CAP_ID_MSIX) {
+			uint16_t flags;
+
+			ret = rte_pci_read_config(dev, &flags,

sizeof(flags),

drivers/net/virtio/virtio_pci.c +4 -0
+					pos + sizeof(cap));
+			if (ret != sizeof(flags)) {
+				PMD_INIT_LOG(DEBUG,
+					     "failed to read pci

cap at pos:"

drivers/net/virtio/virtio_pci.c +1 -0
+					     " %x ret %d", pos +
Quoted 1 line

sizeof(cap),

There is a build error:

Quoted 16 lines

In file included from drivers/net/virtio/virtio_pci.c:15:
drivers/net/virtio/virtio_pci.c: In function ‘vtpci_msix_detect’:
drivers/net/virtio/virtio_logs.h:13:3: error: format ‘%x’ expects argument of type ‘unsigned int’, but argument 5 has type ‘long unsigned int’ [-Werror=format=]
   "%s(): " fmt "\n", __func__, ##args)
   ^~~~~~~~
drivers/net/virtio/virtio_pci.c:732:5: note: in expansion of macro ‘PMD_INIT_LOG’
     PMD_INIT_LOG(DEBUG,
     ^~~~~~~~~~~~ drivers/net/virtio/virtio_pci.c:734:14: note: format string is defined here
           " %x ret %d", pos + sizeof(cap),
             ~^
             %lx

Ok, changed to %lx - which GCC version was that with? Didn't get any warning with 6.3 on Debian 9.

Signature · 2 lines
Kind regards,
Luca Boccassi
#16 lore raw

Re: [PATCH v4 2/2] virtio: fix PCI config err handling

Quoted 84 lines · Luca Boccassi
Quoted 74 lines · Tiwei Bie
Quoted 69 lines · Luca Boccassi

From: Brian Russell <[email protected]>

In virtio_read_caps and vtpci_msix_detect, rte_pci_read_config returns
the number of bytes read from PCI config or < 0 on error. If less than the expected number of bytes are read then log the failure and return rather than carrying on with garbage.

Fixes 6ba1f63b5ab0 ("virtio: support specification 1.0")

---
v2: handle additional rte_pci_read_config incomplete reads
v3: do not handle rte_pci_read_config of virtio cap, added in v2, as it's less clear what the right thing to do there is
v4: do a more robust check - first check what the vendor is, and skip the cap entirely if it's not what we are looking for.

 drivers/net/virtio/virtio_pci.c | 57 ++++++++++++++++++++++++-----
----

 1 file changed, 42 insertions(+), 15 deletions(-)
drivers/net/virtio/virtio_pci.c +0 -0
index 6bd22e54a6..cfefa9789b 100644
@@ -567,16 +567,30 @@ virtio_read_caps(struct rte_pci_device *dev,
 struct virtio_hw *hw)
  	}


  ret = rte_pci_read_config(dev, &pos, 1,
PCI_CAPABILITY_LIST);

drivers/net/virtio/virtio_pci.c +0 -2
-	if (ret < 0) {
-		PMD_INIT_LOG(DEBUG, "failed to read pci capability

list");

drivers/net/virtio/virtio_pci.c +3 -0
+	if (ret != 1) {
+		PMD_INIT_LOG(DEBUG,
+			     "failed to read pci capability list,

ret %d", ret);
  return -1;
  }

  while (pos) {

drivers/net/virtio/virtio_pci.c +4 -0
+		ret = rte_pci_read_config(dev, &cap, 2, pos);
+		if (ret != 2) {
+			PMD_INIT_LOG(DEBUG,
+				     "failed to read pci cap at

pos: %x ret %d",

drivers/net/virtio/virtio_pci.c +8 -0
+				     pos, ret);
+			break;
+		}
+		if (cap.cap_vndr != PCI_CAP_ID_MSIX &&
+				cap.cap_vndr != PCI_CAP_ID_VNDR) {
+			goto next;
+		}
+

  ret = rte_pci_read_config(dev, &cap, sizeof(cap), pos);

drivers/net/virtio/virtio_pci.c +0 -3
-		if (ret < 0) {
-			PMD_INIT_LOG(ERR,
-				"failed to read pci cap at pos:

%x", pos);

drivers/net/virtio/virtio_pci.c +3 -0
+		if (ret != sizeof(cap)) {
+			PMD_INIT_LOG(DEBUG,
+				     "failed to read pci cap at

pos: %x ret %d",

drivers/net/virtio/virtio_pci.c +1 -0
+				     pos, ret);
Quoted 3 lines

  break;
  }

Quoted 8 lines

It seems that I didn't make myself clear in my previous comments. I mean it's better to handle MSIX cap and virtio cap respectively in this function. Currently we're always reading them as virtio caps. As we are strictly requiring that _read_config() should return the required number of bytes, it's not perfect to require it to return "virtio cap size" of bytes while we're trying to read a MSIX cap. So please change the code to something similar to this:

Quoted 2 lines

Sorry, though you meant in the vtpci_msix_detect function, which I changed. Fixed in v5.

Thanks!

Quoted 73 lines
Quoted 56 lines
Quoted 52 lines
drivers/net/virtio/virtio_pci.c +0 -1
@@ -689,25 +703,38 @@ enum virtio_msix_status
  vtpci_msix_detect(struct rte_pci_device *dev)
  {
  	uint8_t pos;
-	struct virtio_pci_cap cap;
  	int ret;


  ret = rte_pci_read_config(dev, &pos, 1,
PCI_CAPABILITY_LIST);

drivers/net/virtio/virtio_pci.c +0 -2
-	if (ret < 0) {
-		PMD_INIT_LOG(DEBUG, "failed to read pci capability

list");

drivers/net/virtio/virtio_pci.c +3 -0
+	if (ret != 1) {
+		PMD_INIT_LOG(DEBUG,
+			     "failed to read pci capability list,

ret %d", ret);
  return VIRTIO_MSIX_NONE;
  }

  while (pos) {

drivers/net/virtio/virtio_pci.c +0 -1
-		ret = rte_pci_read_config(dev, &cap, sizeof(cap),

pos);

drivers/net/virtio/virtio_pci.c +0 -3
-		if (ret < 0) {
-			PMD_INIT_LOG(ERR,
-				"failed to read pci cap at pos:

%x", pos);

drivers/net/virtio/virtio_pci.c +3 -0
+		uint8_t cap[2];
+
+		ret = rte_pci_read_config(dev, cap, sizeof(cap),

pos);

drivers/net/virtio/virtio_pci.c +3 -0
+		if (ret != sizeof(cap)) {
+			PMD_INIT_LOG(DEBUG,
+				     "failed to read pci cap at

pos: %x ret %d",

drivers/net/virtio/virtio_pci.c +1 -0
+				     pos, ret);

  break;
  }

drivers/net/virtio/virtio_pci.c +4 -2
-		if (cap.cap_vndr == PCI_CAP_ID_MSIX) {
-			uint16_t flags = ((uint16_t *)&cap)[1];
+		if (cap[0] == PCI_CAP_ID_MSIX) {
+			uint16_t flags;
+
+			ret = rte_pci_read_config(dev, &flags,

sizeof(flags),

drivers/net/virtio/virtio_pci.c +4 -0
+					pos + sizeof(cap));
+			if (ret != sizeof(flags)) {
+				PMD_INIT_LOG(DEBUG,
+					     "failed to read pci

cap at pos:"

drivers/net/virtio/virtio_pci.c +1 -0
+					     " %x ret %d", pos +
Quoted 1 line

sizeof(cap),

There is a build error:

Quoted 16 lines

In file included from drivers/net/virtio/virtio_pci.c:15:
drivers/net/virtio/virtio_pci.c: In function ‘vtpci_msix_detect’:
drivers/net/virtio/virtio_logs.h:13:3: error: format ‘%x’ expects argument of type ‘unsigned int’, but argument 5 has type ‘long unsigned int’ [-Werror=format=]
   "%s(): " fmt "\n", __func__, ##args)
   ^~~~~~~~
drivers/net/virtio/virtio_pci.c:732:5: note: in expansion of macro ‘PMD_INIT_LOG’
     PMD_INIT_LOG(DEBUG,
     ^~~~~~~~~~~~ drivers/net/virtio/virtio_pci.c:734:14: note: format string is defined here
           " %x ret %d", pos + sizeof(cap),
             ~^
             %lx

Quoted 2 lines

Ok, changed to %lx - which GCC version was that with? Didn't get any warning with 6.3 on Debian 9.

I saw this with gcc (Debian 8.2.0-4) 8.2.0 on Debian sid. I also saw similar warnings with clang version 6.0.0 (tags/RELEASE_600/final 326565) on FreeBSD 11.2

Quoted 4 lines
Signature · 3 lines
-- 
Kind regards,
Luca Boccassi
#17 lore raw

PATCH v5 1/2 bus/pci: harmonize and document rte_pci_read_config return value

In reply to Luca Boccassi

On Linux, rte_pci_read_config on success returns the number of read bytes, but on BSD it returns 0.
Document the return values, and have BSD behave as Linux does.

At least one case (bnx2x PMD) treats 0 as an error, so the change makes sense also for that.

---
 drivers/bus/pci/bsd/pci.c     | 4 +++-
 drivers/bus/pci/rte_bus_pci.h | 2 ++
 2 files changed, 5 insertions(+), 1 deletion(-)
drivers/bus/pci/bsd/pci.c +3 -1
index 655b34b7e4..175d83cf1b 100644
@@ -439,6 +439,8 @@ int rte_pci_read_config(const struct rte_pci_device *dev,
 {
 	int fd = -1;
 	int size;
+	/* Copy Linux implementation's behaviour */
+	const int return_len = len;
 	struct pci_io pi = {
 		.pi_sel = {
 			.pc_domain = dev->addr.domain,
@@ -469,7 +471,7 @@ int rte_pci_read_config(const struct rte_pci_device *dev,
 	}
 	close(fd);
 
-	return 0;
+	return return_len;
 
  error:
 	if (fd >= 0)
drivers/bus/pci/rte_bus_pci.h +2 -0
index 0d1955ffe0..df8f64798d 100644
@@ -219,6 +219,8 @@ void rte_pci_unregister(struct rte_pci_driver *driver);
  *   The length of the data buffer.
  * @param offset
  *   The offset into PCI config space
+ * @return
+ *  Number of bytes read on success, negative on error.
  */
 int rte_pci_read_config(const struct rte_pci_device *device,
 		void *buf, size_t len, off_t offset);
Signature · 1 line
2.18.0
#18 lore raw

PATCH v5 2/2 virtio: fix PCI config err handling

From: Brian Russell <[email protected]>

In virtio_read_caps and vtpci_msix_detect, rte_pci_read_config returns the number of bytes read from PCI config or < 0 on error. If less than the expected number of bytes are read then log the failure and return rather than carrying on with garbage.

Fixes 6ba1f63b5ab0 ("virtio: support specification 1.0")

---
v2: handle additional rte_pci_read_config incomplete reads
v3: do not handle rte_pci_read_config of virtio cap, added in v2,
as it's less clear what the right thing to do there is
v4: do a more robust check - first check what the vendor is, and
skip the cap entirely if it's not what we are looking for.
v5: fetch only 2 flags bytes if the vndr is PCI_CAP_ID_MSIX

 drivers/net/virtio/virtio_pci.c | 66 ++++++++++++++++++++++++---------
 1 file changed, 49 insertions(+), 17 deletions(-)
drivers/net/virtio/virtio_pci.c +49 -17
index 6bd22e54a6..e900254a12 100644
@@ -567,16 +567,18 @@ virtio_read_caps(struct rte_pci_device *dev, struct virtio_hw *hw)
 	}
 
 	ret = rte_pci_read_config(dev, &pos, 1, PCI_CAPABILITY_LIST);
-	if (ret < 0) {
-		PMD_INIT_LOG(DEBUG, "failed to read pci capability list");
+	if (ret != 1) {
+		PMD_INIT_LOG(DEBUG,
+			     "failed to read pci capability list, ret %d", ret);
 		return -1;
 	}
 
 	while (pos) {
-		ret = rte_pci_read_config(dev, &cap, sizeof(cap), pos);
-		if (ret < 0) {
-			PMD_INIT_LOG(ERR,
-				"failed to read pci cap at pos: %x", pos);
+		ret = rte_pci_read_config(dev, &cap, 2, pos);
+		if (ret != 2) {
+			PMD_INIT_LOG(DEBUG,
+				     "failed to read pci cap at pos: %x ret %d",
+				     pos, ret);
 			break;
 		}
 
@@ -586,7 +588,16 @@ virtio_read_caps(struct rte_pci_device *dev, struct virtio_hw *hw)
 			 * 1st byte is cap ID; 2nd byte is the position of next
 			 * cap; next two bytes are the flags.
 			 */
-			uint16_t flags = ((uint16_t *)&cap)[1];
+			uint16_t flags;
+
+			ret = rte_pci_read_config(dev, &flags, sizeof(flags),
+					pos + 2);
+			if (ret != sizeof(flags)) {
+				PMD_INIT_LOG(DEBUG,
+					     "failed to read pci cap at pos:"
+					     " %x ret %d", pos + 2, ret);
+				break;
+			}
 
 			if (flags & PCI_MSIX_ENABLE)
 				hw->use_msix = VIRTIO_MSIX_ENABLED;
@@ -601,6 +612,14 @@ virtio_read_caps(struct rte_pci_device *dev, struct virtio_hw *hw)
 			goto next;
 		}
 
+		ret = rte_pci_read_config(dev, &cap, sizeof(cap), pos);
+		if (ret != sizeof(cap)) {
+			PMD_INIT_LOG(DEBUG,
+				     "failed to read pci cap at pos: %x ret %d",
+				     pos, ret);
+			break;
+		}
+
 		PMD_INIT_LOG(DEBUG,
 			"[%2x] cfg type: %u, bar: %u, offset: %04x, len: %u",
 			pos, cap.cfg_type, cap.bar, cap.offset, cap.length);
@@ -689,25 +708,38 @@ enum virtio_msix_status
 vtpci_msix_detect(struct rte_pci_device *dev)
 {
 	uint8_t pos;
-	struct virtio_pci_cap cap;
 	int ret;
 
 	ret = rte_pci_read_config(dev, &pos, 1, PCI_CAPABILITY_LIST);
-	if (ret < 0) {
-		PMD_INIT_LOG(DEBUG, "failed to read pci capability list");
+	if (ret != 1) {
+		PMD_INIT_LOG(DEBUG,
+			     "failed to read pci capability list, ret %d", ret);
 		return VIRTIO_MSIX_NONE;
 	}
 
 	while (pos) {
-		ret = rte_pci_read_config(dev, &cap, sizeof(cap), pos);
-		if (ret < 0) {
-			PMD_INIT_LOG(ERR,
-				"failed to read pci cap at pos: %x", pos);
+		uint8_t cap[2];
+
+		ret = rte_pci_read_config(dev, cap, sizeof(cap), pos);
+		if (ret != sizeof(cap)) {
+			PMD_INIT_LOG(DEBUG,
+				     "failed to read pci cap at pos: %x ret %d",
+				     pos, ret);
 			break;
 		}
 
-		if (cap.cap_vndr == PCI_CAP_ID_MSIX) {
-			uint16_t flags = ((uint16_t *)&cap)[1];
+		if (cap[0] == PCI_CAP_ID_MSIX) {
+			uint16_t flags;
+
+			ret = rte_pci_read_config(dev, &flags, sizeof(flags),
+					pos + sizeof(cap));
+			if (ret != sizeof(flags)) {
+				PMD_INIT_LOG(DEBUG,
+					     "failed to read pci cap at pos:"
+					     " %lx ret %d", pos + sizeof(cap),
+					     ret);
+				break;
+			}
 
 			if (flags & PCI_MSIX_ENABLE)
 				return VIRTIO_MSIX_ENABLED;
@@ -715,7 +747,7 @@ vtpci_msix_detect(struct rte_pci_device *dev)
 				return VIRTIO_MSIX_DISABLED;
 		}
 
-		pos = cap.cap_next;
+		pos = cap[1];
 	}
 
 	return VIRTIO_MSIX_NONE;
Signature · 1 line
2.18.0
Reviewed-by on v5 2/2
#19 lore raw

Re: [PATCH v5 2/2] virtio: fix PCI config err handling

I just noticed the title. It should be "net/virtio: xxx", instead of "virtio: xxx".

On Mon, Aug 27, 2018 at 05:52:40PM +0100, Luca Boccassi wrote:
[...]

drivers/net/virtio/virtio_pci.c enum virtio_msix_status · line 740

Quoted 7 lines
drivers/net/virtio/virtio_pci.c +7 -0
+			ret = rte_pci_read_config(dev, &flags, sizeof(flags),
+					pos + sizeof(cap));
+			if (ret != sizeof(flags)) {
+				PMD_INIT_LOG(DEBUG,
+					     "failed to read pci cap at pos:"
+					     " %lx ret %d", pos + sizeof(cap),
+					     ret);

In file included from drivers/net/virtio/virtio_pci.c:15:0:
drivers/net/virtio/virtio_pci.c: In function ‘vtpci_msix_detect’:
drivers/net/virtio/virtio_logs.h:13:3: error: format ‘%lx’ expects argument of type ‘long unsigned int’, but argument 5 has type ‘unsigned int’ [-Werror=format=]
"%s(): " fmt "\n", __func__, ##args)
^
drivers/net/virtio/virtio_pci.c:737:5: note: in expansion of macro ‘PMD_INIT_LOG’

     PMD_INIT_LOG(DEBUG,
     ^

cc1: all warnings being treated as errors

I got above build issues in 32bit build.

Apart from that,

Thanks!

#20 lore raw

Re: [PATCH v5 2/2] virtio: fix PCI config err handling

Quoted 2 lines · Tiwei Bie

I just noticed the title. It should be "net/virtio: xxx", instead of "virtio: xxx".

Fixed

Quoted 26 lines · Luca Boccassi

[...]

drivers/net/virtio/virtio_pci.c enum virtio_msix_status · line 740

Quoted 10 lines
(quoted diff) +1 -0
+			ret = rte_pci_read_config(dev, &flags,

sizeof(flags),

drivers/net/virtio/virtio_pci.c +4 -0
+					pos + sizeof(cap));
+			if (ret != sizeof(flags)) {
+				PMD_INIT_LOG(DEBUG,
+					     "failed to read pci

cap at pos:"

(quoted diff) +1 -0
+					     " %lx ret %d", pos +

sizeof(cap),

drivers/net/virtio/virtio_pci.c +1 -0
+					     ret);

In file included from drivers/net/virtio/virtio_pci.c:15:0:
drivers/net/virtio/virtio_pci.c: In function ‘vtpci_msix_detect’:
drivers/net/virtio/virtio_logs.h:13:3: error: format ‘%lx’ expects argument of type ‘long unsigned int’, but argument 5 has type ‘unsigned int’ [-Werror=format=]
   "%s(): " fmt "\n", __func__, ##args)
   ^
drivers/net/virtio/virtio_pci.c:737:5: note: in expansion of macro ‘PMD_INIT_LOG’
     PMD_INIT_LOG(DEBUG,
     ^
cc1: all warnings being treated as errors

Quoted 8 lines

I got above build issues in 32bit build.

Apart from that,

Thanks!

So the 32 and 64 bit builds want opposite things, due to the sizeof type. I changed it to avoid sizeof and just calculate it like in every other prints of these functions to avoid the issue.

Signature · 2 lines
Kind regards,
Luca Boccassi
#21 lore raw

PATCH v6 1/2 bus/pci: harmonize and document rte_pci_read_config return value

In reply to Luca Boccassi

On Linux, rte_pci_read_config on success returns the number of read bytes, but on BSD it returns 0.
Document the return values, and have BSD behave as Linux does.

At least one case (bnx2x PMD) treats 0 as an error, so the change makes sense also for that.

---
 drivers/bus/pci/bsd/pci.c     | 4 +++-
 drivers/bus/pci/rte_bus_pci.h | 2 ++
 2 files changed, 5 insertions(+), 1 deletion(-)
drivers/bus/pci/bsd/pci.c +3 -1
index 655b34b7e4..175d83cf1b 100644
@@ -439,6 +439,8 @@ int rte_pci_read_config(const struct rte_pci_device *dev,
 {
 	int fd = -1;
 	int size;
+	/* Copy Linux implementation's behaviour */
+	const int return_len = len;
 	struct pci_io pi = {
 		.pi_sel = {
 			.pc_domain = dev->addr.domain,
@@ -469,7 +471,7 @@ int rte_pci_read_config(const struct rte_pci_device *dev,
 	}
 	close(fd);
 
-	return 0;
+	return return_len;
 
  error:
 	if (fd >= 0)
drivers/bus/pci/rte_bus_pci.h +2 -0
index 0d1955ffe0..df8f64798d 100644
@@ -219,6 +219,8 @@ void rte_pci_unregister(struct rte_pci_driver *driver);
  *   The length of the data buffer.
  * @param offset
  *   The offset into PCI config space
+ * @return
+ *  Number of bytes read on success, negative on error.
  */
 int rte_pci_read_config(const struct rte_pci_device *device,
 		void *buf, size_t len, off_t offset);
Signature · 1 line
2.18.0
#22 lore raw

PATCH v6 2/2 net/virtio: fix PCI config err handling

From: Brian Russell <[email protected]>

In virtio_read_caps and vtpci_msix_detect, rte_pci_read_config returns the number of bytes read from PCI config or < 0 on error. If less than the expected number of bytes are read then log the failure and return rather than carrying on with garbage.

Fixes 6ba1f63b5ab0 ("virtio: support specification 1.0")

---
v2: handle additional rte_pci_read_config incomplete reads
v3: do not handle rte_pci_read_config of virtio cap, added in v2,
as it's less clear what the right thing to do there is
v4: do a more robust check - first check what the vendor is, and
skip the cap entirely if it's not what we are looking for.
v5: fetch only 2 flags bytes if the vndr is PCI_CAP_ID_MSIX
v6: fix 32bit build by changing the printf format specifier, fix patch title

 drivers/net/virtio/virtio_pci.c | 65 ++++++++++++++++++++++++---------
 1 file changed, 48 insertions(+), 17 deletions(-)
drivers/net/virtio/virtio_pci.c +48 -17
index 6bd22e54a6..b6a3c80b4d 100644
@@ -567,16 +567,18 @@ virtio_read_caps(struct rte_pci_device *dev, struct virtio_hw *hw)
 	}
 
 	ret = rte_pci_read_config(dev, &pos, 1, PCI_CAPABILITY_LIST);
-	if (ret < 0) {
-		PMD_INIT_LOG(DEBUG, "failed to read pci capability list");
+	if (ret != 1) {
+		PMD_INIT_LOG(DEBUG,
+			     "failed to read pci capability list, ret %d", ret);
 		return -1;
 	}
 
 	while (pos) {
-		ret = rte_pci_read_config(dev, &cap, sizeof(cap), pos);
-		if (ret < 0) {
-			PMD_INIT_LOG(ERR,
-				"failed to read pci cap at pos: %x", pos);
+		ret = rte_pci_read_config(dev, &cap, 2, pos);
+		if (ret != 2) {
+			PMD_INIT_LOG(DEBUG,
+				     "failed to read pci cap at pos: %x ret %d",
+				     pos, ret);
 			break;
 		}
 
@@ -586,7 +588,16 @@ virtio_read_caps(struct rte_pci_device *dev, struct virtio_hw *hw)
 			 * 1st byte is cap ID; 2nd byte is the position of next
 			 * cap; next two bytes are the flags.
 			 */
-			uint16_t flags = ((uint16_t *)&cap)[1];
+			uint16_t flags;
+
+			ret = rte_pci_read_config(dev, &flags, sizeof(flags),
+					pos + 2);
+			if (ret != sizeof(flags)) {
+				PMD_INIT_LOG(DEBUG,
+					     "failed to read pci cap at pos:"
+					     " %x ret %d", pos + 2, ret);
+				break;
+			}
 
 			if (flags & PCI_MSIX_ENABLE)
 				hw->use_msix = VIRTIO_MSIX_ENABLED;
@@ -601,6 +612,14 @@ virtio_read_caps(struct rte_pci_device *dev, struct virtio_hw *hw)
 			goto next;
 		}
 
+		ret = rte_pci_read_config(dev, &cap, sizeof(cap), pos);
+		if (ret != sizeof(cap)) {
+			PMD_INIT_LOG(DEBUG,
+				     "failed to read pci cap at pos: %x ret %d",
+				     pos, ret);
+			break;
+		}
+
 		PMD_INIT_LOG(DEBUG,
 			"[%2x] cfg type: %u, bar: %u, offset: %04x, len: %u",
 			pos, cap.cfg_type, cap.bar, cap.offset, cap.length);
@@ -689,25 +708,37 @@ enum virtio_msix_status
 vtpci_msix_detect(struct rte_pci_device *dev)
 {
 	uint8_t pos;
-	struct virtio_pci_cap cap;
 	int ret;
 
 	ret = rte_pci_read_config(dev, &pos, 1, PCI_CAPABILITY_LIST);
-	if (ret < 0) {
-		PMD_INIT_LOG(DEBUG, "failed to read pci capability list");
+	if (ret != 1) {
+		PMD_INIT_LOG(DEBUG,
+			     "failed to read pci capability list, ret %d", ret);
 		return VIRTIO_MSIX_NONE;
 	}
 
 	while (pos) {
-		ret = rte_pci_read_config(dev, &cap, sizeof(cap), pos);
-		if (ret < 0) {
-			PMD_INIT_LOG(ERR,
-				"failed to read pci cap at pos: %x", pos);
+		uint8_t cap[2];
+
+		ret = rte_pci_read_config(dev, cap, sizeof(cap), pos);
+		if (ret != sizeof(cap)) {
+			PMD_INIT_LOG(DEBUG,
+				     "failed to read pci cap at pos: %x ret %d",
+				     pos, ret);
 			break;
 		}
 
-		if (cap.cap_vndr == PCI_CAP_ID_MSIX) {
-			uint16_t flags = ((uint16_t *)&cap)[1];
+		if (cap[0] == PCI_CAP_ID_MSIX) {
+			uint16_t flags;
+
+			ret = rte_pci_read_config(dev, &flags, sizeof(flags),
+					pos + sizeof(cap));
+			if (ret != sizeof(flags)) {
+				PMD_INIT_LOG(DEBUG,
+					     "failed to read pci cap at pos:"
+					     " %x ret %d", pos + 2, ret);
+				break;
+			}
 
 			if (flags & PCI_MSIX_ENABLE)
 				return VIRTIO_MSIX_ENABLED;
@@ -715,7 +746,7 @@ vtpci_msix_detect(struct rte_pci_device *dev)
 				return VIRTIO_MSIX_DISABLED;
 		}
 
-		pos = cap.cap_next;
+		pos = cap[1];
 	}
 
 	return VIRTIO_MSIX_NONE;
Signature · 1 line
2.18.0
#23 lore raw

Re: [PATCH v6 2/2] net/virtio: fix PCI config err handling

28/08/2018 12:12, Luca Boccassi:

Quoted 11 lines

From: Brian Russell <[email protected]>

In virtio_read_caps and vtpci_msix_detect, rte_pci_read_config returns the number of bytes read from PCI config or < 0 on error. If less than the expected number of bytes are read then log the failure and return rather than carrying on with garbage.

Fixes 6ba1f63b5ab0 ("virtio: support specification 1.0")
Quoted 8 lines

---
v2: handle additional rte_pci_read_config incomplete reads
v3: do not handle rte_pci_read_config of virtio cap, added in v2,
as it's less clear what the right thing to do there is
v4: do a more robust check - first check what the vendor is, and
skip the cap entirely if it's not what we are looking for.
v5: fetch only 2 flags bytes if the vndr is PCI_CAP_ID_MSIX
v6: fix 32bit build by changing the printf format specifier, fix patch title

Tiwei did a comment on v5 and provided his Reviewed-by. Is it OK to apply v6 with its tag?
All is fixed?

#24 lore raw

Re: [PATCH v6 2/2] net/virtio: fix PCI config err handling

Quoted 23 lines · Thomas Monjalon

28/08/2018 12:12, Luca Boccassi:

Quoted 12 lines

From: Brian Russell <[email protected]>

In virtio_read_caps and vtpci_msix_detect, rte_pci_read_config returns
the number of bytes read from PCI config or < 0 on error. If less than the expected number of bytes are read then log the failure and return rather than carrying on with garbage.

Fixes 6ba1f63b5ab0 ("virtio: support specification 1.0")
Quoted 9 lines

---
v2: handle additional rte_pci_read_config incomplete reads
v3: do not handle rte_pci_read_config of virtio cap, added in v2, as it's less clear what the right thing to do there is
v4: do a more robust check - first check what the vendor is, and skip the cap entirely if it's not what we are looking for.
v5: fetch only 2 flags bytes if the vndr is PCI_CAP_ID_MSIX
v6: fix 32bit build by changing the printf format specifier, fix patch title

Quoted 3 lines

Tiwei did a comment on v5 and provided his Reviewed-by. Is it OK to apply v6 with its tag?
All is fixed?

Hi,

The title has been fixed (virtio -> net/virtio) and I just tried a 32bit build and it works fine after the fix from v6.

Signature · 2 lines
Kind regards,
Luca Boccassi
Reviewed-by
#25 lore raw

Re: [PATCH v6 2/2] net/virtio: fix PCI config err handling

Quoted 24 lines · Luca Boccassi
Quoted 23 lines · Thomas Monjalon

28/08/2018 12:12, Luca Boccassi:

Quoted 21 lines

From: Brian Russell <[email protected]>

In virtio_read_caps and vtpci_msix_detect, rte_pci_read_config returns
the number of bytes read from PCI config or < 0 on error. If less than the expected number of bytes are read then log the failure and return rather than carrying on with garbage.

Fixes 6ba1f63b5ab0 ("virtio: support specification 1.0")

---
v2: handle additional rte_pci_read_config incomplete reads
v3: do not handle rte_pci_read_config of virtio cap, added in v2, as it's less clear what the right thing to do there is
v4: do a more robust check - first check what the vendor is, and skip the cap entirely if it's not what we are looking for.
v5: fetch only 2 flags bytes if the vndr is PCI_CAP_ID_MSIX
v6: fix 32bit build by changing the printf format specifier, fix patch title

Quoted 8 lines
Quoted 3 lines

Tiwei did a comment on v5 and provided his Reviewed-by. Is it OK to apply v6 with its tag?
All is fixed?

Hi,

The title has been fixed (virtio -> net/virtio) and I just tried a 32bit build and it works fine after the fix from v6.

Thanks Luca!

Quoted 4 lines
Signature · 3 lines
-- 
Kind regards,
Luca Boccassi
#26 lore raw

Re: [PATCH v6 2/2] net/virtio: fix PCI config err handling

11/10/2018 15:01, Tiwei Bie:

Quoted 29 lines · Luca Boccassi
Quoted 28 lines · Thomas Monjalon
Quoted 26 lines

28/08/2018 12:12, Luca Boccassi:

Quoted 12 lines

From: Brian Russell <[email protected]>

In virtio_read_caps and vtpci_msix_detect, rte_pci_read_config returns
the number of bytes read from PCI config or < 0 on error. If less than the expected number of bytes are read then log the failure and return rather than carrying on with garbage.

Fixes 6ba1f63b5ab0 ("virtio: support specification 1.0")
Quoted 9 lines

---
v2: handle additional rte_pci_read_config incomplete reads
v3: do not handle rte_pci_read_config of virtio cap, added in v2,
as it's less clear what the right thing to do there is
v4: do a more robust check - first check what the vendor is, and
skip the cap entirely if it's not what we are looking for.
v5: fetch only 2 flags bytes if the vndr is PCI_CAP_ID_MSIX
v6: fix 32bit build by changing the printf format specifier, fix patch title

Tiwei did a comment on v5 and provided his Reviewed-by. Is it OK to apply v6 with its tag?
All is fixed?

Quoted 8 lines
Quoted 4 lines

Hi,

The title has been fixed (virtio -> net/virtio) and I just tried a 32bit build and it works fine after the fix from v6.

Thanks Luca!

Series applied, thanks

#27 lore raw
In reply to Luca Boccassi
Quoted 51 lines · Luca Boccassi

On Linux, rte_pci_read_config on success returns the number of read bytes, but on BSD it returns 0.
Document the return values, and have BSD behave as Linux does.

At least one case (bnx2x PMD) treats 0 as an error, so the change makes sense also for that.

---
 drivers/bus/pci/bsd/pci.c     | 4 +++-
 drivers/bus/pci/rte_bus_pci.h | 2 ++

 2 files changed, 5 insertions(+), 1 deletion(-)
drivers/bus/pci/bsd/pci.c +2 -0
index 655b34b7e4..175d83cf1b 100644
@@ -439,6 +439,8 @@ int rte_pci_read_config(const struct
 rte_pci_device *dev,
  {
  	int fd = -1;
  	int size;
+	/* Copy Linux implementation's behaviour */
+	const int return_len = len;
  	struct pci_io pi = {
  		.pi_sel = {

  .pc_domain = dev->addr.domain,

drivers/bus/pci/bsd/pci.c +0 -0
@@ -469,7 +471,7 @@ int rte_pci_read_config(const struct
 rte_pci_device *dev,
  	}
  	close(fd);

drivers/bus/pci/bsd/pci.c +1 -1
-	return 0;
+	return return_len;


  error:
  if (fd >= 0)

drivers/bus/pci/rte_bus_pci.h +2 -0
index 0d1955ffe0..df8f64798d 100644
@@ -219,6 +219,8 @@ void rte_pci_unregister(struct rte_pci_driver
 *driver);
   *   The length of the data buffer.
   * @param offset
   *   The offset into PCI config space
+ * @return
+ *  Number of bytes read on success, negative on error.
   */
  int rte_pci_read_config(const struct rte_pci_device *device,
Quoted 1 line

  void *buf, size_t len, off_t offset);

Hi Bruce,

Any chance you could please review the small BSD patch above when you have a sec? Thanks!

Signature · 2 lines
Kind regards,
Luca Boccassi
#29 lore raw

PATCH 2/2 virtio: fix PCI config err handling

In reply to Luca Boccassi

From: Brian Russell <[email protected]>

In virtio_read_caps, rte_pci_read_config returns the number of bytes read from PCI config or < 0 on error.
If less than the expected number of bytes are read then log the failure and return rather than carrying on with garbage.

---

Follow-up from:
http://mails.dpdk.org/archives/dev/2017-June/067278.html
https://patches.dpdk.org/patch/25056/

 drivers/net/virtio/virtio_pci.c | 12 +++++++-----
 1 file changed, 7 insertions(+), 5 deletions(-)
drivers/net/virtio/virtio_pci.c +7 -5
index 6bd22e54a6..a10698aed8 100644
@@ -567,16 +567,18 @@ virtio_read_caps(struct rte_pci_device *dev, struct virtio_hw *hw)
 	}
 
 	ret = rte_pci_read_config(dev, &pos, 1, PCI_CAPABILITY_LIST);
-	if (ret < 0) {
-		PMD_INIT_LOG(DEBUG, "failed to read pci capability list");
+	if (ret != 1) {
+		PMD_INIT_LOG(DEBUG,
+			     "failed to read pci capability list, ret %d", ret);
 		return -1;
 	}
 
 	while (pos) {
 		ret = rte_pci_read_config(dev, &cap, sizeof(cap), pos);
-		if (ret < 0) {
-			PMD_INIT_LOG(ERR,
-				"failed to read pci cap at pos: %x", pos);
+		if (ret != sizeof(cap)) {
+			PMD_INIT_LOG(DEBUG,
+				     "failed to read pci cap at pos: %x ret %d",
+				     pos, ret);
 			break;
 		}
 
Signature · 1 line
2.18.0
#30 lore raw

Re: [PATCH 2/2] virtio: fix PCI config err handling

Quoted 6 lines · Luca Boccassi

From: Brian Russell <[email protected]>

In virtio_read_caps, rte_pci_read_config returns the number of bytes read from PCI config or < 0 on error.
If less than the expected number of bytes are read then log the failure and return rather than carrying on with garbage.

Is this a fix or an improvement?
Or did you see anything broken without this patch? If so, we may need a fixes line and Cc stable.

Quoted 24 lines

---

Follow-up from:
http://mails.dpdk.org/archives/dev/2017-June/067278.html
https://patches.dpdk.org/patch/25056/

 drivers/net/virtio/virtio_pci.c | 12 +++++++-----
 1 file changed, 7 insertions(+), 5 deletions(-)
drivers/net/virtio/virtio_pci.c +3 -2
index 6bd22e54a6..a10698aed8 100644
@@ -567,16 +567,18 @@ virtio_read_caps(struct rte_pci_device *dev, struct virtio_hw *hw)
 	}
 
 	ret = rte_pci_read_config(dev, &pos, 1, PCI_CAPABILITY_LIST);
-	if (ret < 0) {
-		PMD_INIT_LOG(DEBUG, "failed to read pci capability list");
+	if (ret != 1) {
+		PMD_INIT_LOG(DEBUG,
+			     "failed to read pci capability list, ret %d", ret);

drivers/net/virtio/virtio_pci.c virtio_read_caps(struct rte_pci_device *dev, struct virtio_hw *hw) · line 579

Quoted 10 lines
drivers/net/virtio/virtio_pci.c +2 -3
 		return -1;
 	}
 
 	while (pos) {
 		ret = rte_pci_read_config(dev, &cap, sizeof(cap), pos);
-		if (ret < 0) {
-			PMD_INIT_LOG(ERR,
-				"failed to read pci cap at pos: %x", pos);
+		if (ret != sizeof(cap)) {
+			PMD_INIT_LOG(DEBUG,

Why change the log level to DEBUG?

Thanks

Quoted 8 lines
drivers/net/virtio/virtio_pci.c +2 -0
+				     "failed to read pci cap at pos: %x ret %d",
+				     pos, ret);
 			break;
 		}
 
Signature · 1 line
2.18.0
#31 lore raw

Re: [PATCH 2/2] virtio: fix PCI config err handling

Quoted 9 lines · Tiwei Bie
Quoted 7 lines · Luca Boccassi

From: Brian Russell <[email protected]>

In virtio_read_caps, rte_pci_read_config returns the number of bytes
read from PCI config or < 0 on error.
If less than the expected number of bytes are read then log the failure and return rather than carrying on with garbage.

Quoted 3 lines

Is this a fix or an improvement?
Or did you see anything broken without this patch? If so, we may need a fixes line and Cc stable.

It is a fix, as it was creating problems in production due to the constant flux of errors in the logs.
But given patch 1/2 is effectively doing a small change in the BSD bus API, and it's a requirement for 2/2, I don't think we can include it in the stable releases unfortunately.

Quoted 44 lines
Quoted 39 lines

---

Follow-up from:
http://mails.dpdk.org/archives/dev/2017-June/067278.html
https://patches.dpdk.org/patch/25056/

 drivers/net/virtio/virtio_pci.c | 12 +++++++-----

 1 file changed, 7 insertions(+), 5 deletions(-)
drivers/net/virtio/virtio_pci.c +0 -0
index 6bd22e54a6..a10698aed8 100644
@@ -567,16 +567,18 @@ virtio_read_caps(struct rte_pci_device *dev,
 struct virtio_hw *hw)
  	}


  ret = rte_pci_read_config(dev, &pos, 1,
PCI_CAPABILITY_LIST);

drivers/net/virtio/virtio_pci.c +0 -2
-	if (ret < 0) {
-		PMD_INIT_LOG(DEBUG, "failed to read pci capability

list");

drivers/net/virtio/virtio_pci.c +3 -0
+	if (ret != 1) {
+		PMD_INIT_LOG(DEBUG,
+			     "failed to read pci capability list,

ret %d", ret);
  return -1;
  }

  while (pos) {
  ret = rte_pci_read_config(dev, &cap, sizeof(cap), pos);

drivers/net/virtio/virtio_pci.c +0 -3
-		if (ret < 0) {
-			PMD_INIT_LOG(ERR,
-				"failed to read pci cap at pos:

%x", pos);

drivers/net/virtio/virtio_pci.c virtio_read_caps(struct rte_pci_device *dev, struct virtio_hw *hw) · line 579

Quoted 2 lines
drivers/net/virtio/virtio_pci.c +2 -0
+		if (ret != sizeof(cap)) {
+			PMD_INIT_LOG(DEBUG,

Why change the log level to DEBUG?

Quoted 1 line

Thanks

Beforehand reading less than the required amount of bytes caused problems in the following code, so it warranted printing errors - but now it will not go ahead without the right amount of data, so it's not critical anymore to inform the user.
Main issue is, log will get very spammy with errors, and paying customers don't like that :-)

Signature · 2 lines
Kind regards,
Luca Boccassi
#32 lore raw

Re: [PATCH 2/2] virtio: fix PCI config err handling

Quoted 10 lines · Luca Boccassi
Quoted 9 lines · Tiwei Bie
Quoted 7 lines · Luca Boccassi

From: Brian Russell <[email protected]>

In virtio_read_caps, rte_pci_read_config returns the number of bytes
read from PCI config or < 0 on error.
If less than the expected number of bytes are read then log the failure and return rather than carrying on with garbage.

Quoted 6 lines
Quoted 3 lines

Is this a fix or an improvement?
Or did you see anything broken without this patch? If so, we may need a fixes line and Cc stable.

It is a fix, as it was creating problems in production due to the constant flux of errors in the logs.

Could you be a bit more specific about which errors were logged if possible?

If my understanding is correct, you mean the errors were logged because less than the required amount of bytes were read?

Quoted 3 lines

But given patch 1/2 is effectively doing a small change in the BSD bus API, and it's a requirement for 2/2, I don't think we can include it in the stable releases unfortunately.

If it's a fix, we need a fixes line.

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

[...]

Quoted 11 lines
Quoted 11 lines
Quoted 11 lines
(quoted diff) +0 -0
@@ -567,16 +567,18 @@ virtio_read_caps(struct rte_pci_device *dev,
 struct virtio_hw *hw)
  	}


  ret = rte_pci_read_config(dev, &pos, 1, PCI_CAPABILITY_LIST);

drivers/net/virtio/virtio_pci.c +3 -2
-	if (ret < 0) {
-		PMD_INIT_LOG(DEBUG, "failed to read pci capability list");
+	if (ret != 1) {
+		PMD_INIT_LOG(DEBUG,
+			     "failed to read pci capability list, ret %d", ret);

  return -1;

Quoted 8 lines
Quoted 8 lines

drivers/net/virtio/virtio_pci.c virtio_read_caps(struct rte_pci_device *dev, struct virtio_hw *hw) · line 578

Quoted 8 lines

  }

  while (pos) {
  ret = rte_pci_read_config(dev, &cap, sizeof(cap), pos);

drivers/net/virtio/virtio_pci.c +1 -3
-		if (ret < 0) {
-			PMD_INIT_LOG(ERR,
-				"failed to read pci cap at pos: %x", pos);
+		if (ret != sizeof(cap)) {

Above code has to successfully read a full virtio PCI capability during each read, otherwise it will give up reading other capabilities and may fallback to the legacy mode. In which case it will fail to read the requested amount of bytes? Should we try to read the generic PCI fields first?

Besides, you also need to update other calls to rte_pci_read_config(), e.g.:

https://github.com/DPDK/dpdk/blob/76b9d9de5c7d/drivers/net/virtio/virtio_pci.c#L696

Thanks

Quoted 16 lines
Quoted 5 lines

Review

Quoted 1 line
(quoted diff) +1 -0
+			PMD_INIT_LOG(DEBUG,

Why change the log level to DEBUG?

Thanks

Beforehand reading less than the required amount of bytes caused problems in the following code, so it warranted printing errors - but now it will not go ahead without the right amount of data, so it's not critical anymore to inform the user.
Main issue is, log will get very spammy with errors, and paying customers don't like that :-)

Signature · 2 lines
Kind regards,
Luca Boccassi
#33 lore raw

Re: [PATCH 2/2] virtio: fix PCI config err handling

Quoted 18 lines · Tiwei Bie
Quoted 10 lines · Luca Boccassi
Quoted 9 lines · Tiwei Bie
Quoted 7 lines · Luca Boccassi

From: Brian Russell <[email protected]>

In virtio_read_caps, rte_pci_read_config returns the number of bytes
read from PCI config or < 0 on error.
If less than the expected number of bytes are read then log the failure and return rather than carrying on with garbage.

Quoted 6 lines
Quoted 3 lines

Is this a fix or an improvement?
Or did you see anything broken without this patch? If so, we may need a fixes line and Cc stable.

It is a fix, as it was creating problems in production due to the constant flux of errors in the logs.

Quoted 6 lines

Could you be a bit more specific about which errors were logged if possible?

If my understanding is correct, you mean the errors were logged because less than the required amount of bytes were read?

Yes - rte_pci_read_config on Linux will return not just 0/-1, but the actual number of bytes read. If it's less than the required amount, the code then goes on and reads garbage, which causes errors later in the execution. Checking that we actually got the amount of data we need fixes this issue.

Quoted 7 lines
Quoted 5 lines

But given patch 1/2 is effectively doing a small change in the BSD bus
API, and it's a requirement for 2/2, I don't think we can include it in
the stable releases unfortunately.

If it's a fix, we need a fixes line.

Sure, will send a v2.

Quoted 28 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

[...]

Quoted 24 lines
Quoted 24 lines
Quoted 24 lines
(quoted diff) +0 -0
@@ -567,16 +567,18 @@ virtio_read_caps(struct rte_pci_device
 *dev,
 struct virtio_hw *hw)
  	}


  ret = rte_pci_read_config(dev, &pos, 1,
PCI_CAPABILITY_LIST);

(quoted diff) +0 -2
-	if (ret < 0) {
-		PMD_INIT_LOG(DEBUG, "failed to read pci

capability list");

drivers/net/virtio/virtio_pci.c +3 -0
+	if (ret != 1) {
+		PMD_INIT_LOG(DEBUG,
+			     "failed to read pci capability

list, ret %d", ret);
  return -1;
  }

  while (pos) {
  ret = rte_pci_read_config(dev, &cap, sizeof(cap), pos);

drivers/net/virtio/virtio_pci.c +0 -3
-		if (ret < 0) {
-			PMD_INIT_LOG(ERR,
-				"failed to read pci cap at

pos: %x", pos);

Quoted 8 lines
Quoted 1 line
Quoted 1 line

drivers/net/virtio/virtio_pci.c virtio_read_caps(struct rte_pci_device *dev, struct virtio_hw *hw) · line 578

Quoted 1 line
drivers/net/virtio/virtio_pci.c +1 -0
+		if (ret != sizeof(cap)) {

Above code has to successfully read a full virtio PCI capability during each read, otherwise it will give up reading other capabilities and may fallback to the legacy mode. In which case it will fail to read the requested amount of bytes? Should we try to read the generic PCI fields first?

I do not know what exactly causes less than required bytes to be read, but we have seen it happen in production (not 100% of the times though
- so I think it's worth keeping the structure as-is). As you said in that case it falls back to legacy mode which, in our experience in production deployments, then succeeds. That's why the error level print is undesired - because the code will actually work via the fallback, but the customers will see scary errors in the logs and open escalations :-)

Quoted 7 lines

Besides, you also need to update other calls to rte_pci_read_config(), e.g.:

https://github.com/DPDK/dpdk/blob/76b9d9de5c7d/drivers/net/virtio/vir
tio_pci.c#L696

Thanks

Sure I will apply the same changes in v2.

Signature · 2 lines
Kind regards,
Luca Boccassi