Skip to contentSkip to conversation

vfio-pci: Fix the check on pci device type in vfio_pci_probe()

PATCH Superseded Wei Yang 2 participants 6 messages Dec 8, 2014 – Jan 7, 2015

Subsystems (2)
lore
Share & tools
lore

Wei Yang

author
#0 lore raw

PATCH 1/1 vfio-pci: Fix the check on pci device type in vfio_pci_probe()

Current vfio-pci just supports normal pci device, so vfio_pci_probe() will return if the pci device is not a normal device. While current code makes a mistake. PCI_HEADER_TYPE is the offset in configuration space of the device type, but we use this value to mask the type value.

This patch adds a function to check whether the pci device is a normal type and use it in vfio_pci_probe().

---
 drivers/vfio/pci/vfio_pci.c |    4 +---
 include/linux/pci.h         |   11 +++++++++++
 2 files changed, 12 insertions(+), 3 deletions(-)
drivers/vfio/pci/vfio_pci.c +1 -3
index 9558da3..f82bf62 100644
@@ -839,13 +839,11 @@ static const struct vfio_device_ops vfio_pci_ops = {
 
 static int vfio_pci_probe(struct pci_dev *pdev, const struct pci_device_id *id)
 {
-	u8 type;
 	struct vfio_pci_device *vdev;
 	struct iommu_group *group;
 	int ret;
 
-	pci_read_config_byte(pdev, PCI_HEADER_TYPE, &type);
-	if ((type & PCI_HEADER_TYPE) != PCI_HEADER_TYPE_NORMAL)
+	if (!pci_is_normal(pdev))
 		return -EINVAL;
 
 	group = iommu_group_get(&pdev->dev);
include/linux/pci.h +11 -0
index 48d1f98..2027f66 100644
@@ -519,6 +519,17 @@ static inline bool pci_is_bridge(struct pci_dev *dev)
 		dev->hdr_type == PCI_HEADER_TYPE_CARDBUS;
 }
 
+/**
+ * pci_is_normal - check if the PCI device is a normal device
+ * @dev: PCI device
+ *
+ * Return true if the PCI device is a normal device
+ */
+static inline bool pci_is_normal(struct pci_dev *dev)
+{
+	return dev->hdr_type == PCI_HEADER_TYPE_NORMAL;
+}
+
 static inline struct pci_dev *pci_upstream_bridge(struct pci_dev *dev)
 {
 	dev = pci_physfn(dev);
Signature · 1 line
1.7.9.5
author
#1 lore raw

Alex,

Does it look good to you?

Quoted 57 lines · Wei Yang

Current vfio-pci just supports normal pci device, so vfio_pci_probe() will return if the pci device is not a normal device. While current code makes a mistake. PCI_HEADER_TYPE is the offset in configuration space of the device type, but we use this value to mask the type value.

This patch adds a function to check whether the pci device is a normal type and use it in vfio_pci_probe().

---
drivers/vfio/pci/vfio_pci.c | 4 +---
include/linux/pci.h | 11 +++++++++++

2 files changed, 12 insertions(+), 3 deletions(-)
drivers/vfio/pci/vfio_pci.c +1 -3
index 9558da3..f82bf62 100644
@@ -839,13 +839,11 @@ static const struct vfio_device_ops vfio_pci_ops = {
 
 static int vfio_pci_probe(struct pci_dev *pdev, const struct pci_device_id *id)
 {
-	u8 type;
 	struct vfio_pci_device *vdev;
 	struct iommu_group *group;
 	int ret;
 
-	pci_read_config_byte(pdev, PCI_HEADER_TYPE, &type);
-	if ((type & PCI_HEADER_TYPE) != PCI_HEADER_TYPE_NORMAL)
+	if (!pci_is_normal(pdev))
 		return -EINVAL;
 
 	group = iommu_group_get(&pdev->dev);
include/linux/pci.h +11 -0
index 48d1f98..2027f66 100644
@@ -519,6 +519,17 @@ static inline bool pci_is_bridge(struct pci_dev *dev)
 		dev->hdr_type == PCI_HEADER_TYPE_CARDBUS;
 }
 
+/**
+ * pci_is_normal - check if the PCI device is a normal device
+ * @dev: PCI device
+ *
+ * Return true if the PCI device is a normal device
+ */
+static inline bool pci_is_normal(struct pci_dev *dev)
+{
+	return dev->hdr_type == PCI_HEADER_TYPE_NORMAL;
+}
+
 static inline struct pci_dev *pci_upstream_bridge(struct pci_dev *dev)
 {
 	dev = pci_physfn(dev);
Signature · 1 line
1.7.9.5
Signature · 2 lines
Richard Yang
Help you, Help me
#2 lore raw
Quoted 3 lines · Wei Yang

Alex,

Does it look good to you?

Well, mixing changes to a subsystem and a driver in a single patch is surely going to slow down acceptance because neither Bjorn or I know who is going to grab it. I'd probably want to mark the vfio-pci change for stable, which would be a lot easier to do if it was just a 4-line correction to the existing code rather than extending the pci-core interface for a single, trivial user. Thanks,

Alex

Quoted 58 lines · Wei Yang
Quoted 57 lines

Current vfio-pci just supports normal pci device, so vfio_pci_probe() will return if the pci device is not a normal device. While current code makes a mistake. PCI_HEADER_TYPE is the offset in configuration space of the device type, but we use this value to mask the type value.

This patch adds a function to check whether the pci device is a normal type and use it in vfio_pci_probe().

---
drivers/vfio/pci/vfio_pci.c | 4 +---
include/linux/pci.h | 11 +++++++++++

2 files changed, 12 insertions(+), 3 deletions(-)
drivers/vfio/pci/vfio_pci.c +1 -3
index 9558da3..f82bf62 100644
@@ -839,13 +839,11 @@ static const struct vfio_device_ops vfio_pci_ops = {
 
 static int vfio_pci_probe(struct pci_dev *pdev, const struct pci_device_id *id)
 {
-	u8 type;
 	struct vfio_pci_device *vdev;
 	struct iommu_group *group;
 	int ret;
 
-	pci_read_config_byte(pdev, PCI_HEADER_TYPE, &type);
-	if ((type & PCI_HEADER_TYPE) != PCI_HEADER_TYPE_NORMAL)
+	if (!pci_is_normal(pdev))
 		return -EINVAL;
 
 	group = iommu_group_get(&pdev->dev);
include/linux/pci.h +11 -0
index 48d1f98..2027f66 100644
@@ -519,6 +519,17 @@ static inline bool pci_is_bridge(struct pci_dev *dev)
 		dev->hdr_type == PCI_HEADER_TYPE_CARDBUS;
 }
 
+/**
+ * pci_is_normal - check if the PCI device is a normal device
+ * @dev: PCI device
+ *
+ * Return true if the PCI device is a normal device
+ */
+static inline bool pci_is_normal(struct pci_dev *dev)
+{
+	return dev->hdr_type == PCI_HEADER_TYPE_NORMAL;
+}
+
 static inline struct pci_dev *pci_upstream_bridge(struct pci_dev *dev)
 {
 	dev = pci_physfn(dev);
Signature · 1 line
1.7.9.5
author
#3 lore raw
Quoted 5 lines · Alex Williamson
Quoted 3 lines · Wei Yang

Alex,

Does it look good to you?

Quoted 8 lines

Well, mixing changes to a subsystem and a driver in a single patch is surely going to slow down acceptance because neither Bjorn or I know who is going to grab it. I'd probably want to mark the vfio-pci change for stable, which would be a lot easier to do if it was just a 4-line correction to the existing code rather than extending the pci-core interface for a single, trivial user. Thanks,

Alex

Hi,

I got your concern.

The reason I introduce a helper to check the pci device type is in my mind we will try our best to hide those pci detailed information from outside. The easiest way to fix it is to check the pci_dev->hdr_type field, but this will export some internal information from pci core to vfio-pci.

I will first make a 4-line change to fix this error. If Bjorn like the idea of the helper, I will make up another one.

Quoted 62 lines · Wei Yang
Quoted 58 lines
Quoted 57 lines

Current vfio-pci just supports normal pci device, so vfio_pci_probe() will return if the pci device is not a normal device. While current code makes a mistake. PCI_HEADER_TYPE is the offset in configuration space of the device type, but we use this value to mask the type value.

This patch adds a function to check whether the pci device is a normal type and use it in vfio_pci_probe().

---
drivers/vfio/pci/vfio_pci.c | 4 +---
include/linux/pci.h | 11 +++++++++++

2 files changed, 12 insertions(+), 3 deletions(-)
drivers/vfio/pci/vfio_pci.c +1 -3
index 9558da3..f82bf62 100644
@@ -839,13 +839,11 @@ static const struct vfio_device_ops vfio_pci_ops = {
 
 static int vfio_pci_probe(struct pci_dev *pdev, const struct pci_device_id *id)
 {
-	u8 type;
 	struct vfio_pci_device *vdev;
 	struct iommu_group *group;
 	int ret;
 
-	pci_read_config_byte(pdev, PCI_HEADER_TYPE, &type);
-	if ((type & PCI_HEADER_TYPE) != PCI_HEADER_TYPE_NORMAL)
+	if (!pci_is_normal(pdev))
 		return -EINVAL;
 
 	group = iommu_group_get(&pdev->dev);
include/linux/pci.h +11 -0
index 48d1f98..2027f66 100644
@@ -519,6 +519,17 @@ static inline bool pci_is_bridge(struct pci_dev *dev)
 		dev->hdr_type == PCI_HEADER_TYPE_CARDBUS;
 }
 
+/**
+ * pci_is_normal - check if the PCI device is a normal device
+ * @dev: PCI device
+ *
+ * Return true if the PCI device is a normal device
+ */
+static inline bool pci_is_normal(struct pci_dev *dev)
+{
+	return dev->hdr_type == PCI_HEADER_TYPE_NORMAL;
+}
+
 static inline struct pci_dev *pci_upstream_bridge(struct pci_dev *dev)
 {
 	dev = pci_physfn(dev);
Signature · 1 line
1.7.9.5
Signature · 2 lines
Richard Yang
Help you, Help me
author
#4 lore raw

PATCH v2 1/1 vfio-pci: Fix the check on pci device type in vfio_pci_probe()

Current vfio-pci just supports normal pci device, so vfio_pci_probe() will return if the pci device is not a normal device. While current code makes a mistake. PCI_HEADER_TYPE is the offset in configuration space of the device type, but we use this value to mask the type value.

This patch fixs this by do the check directly on the pci_dev->hdr_type.

V2:
* Fix the problem by checking pci_dev->hdr_type

---
 drivers/vfio/pci/vfio_pci.c |    4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)
drivers/vfio/pci/vfio_pci.c +1 -3
index 9558da3..2f8a055 100644
@@ -839,13 +839,11 @@ static const struct vfio_device_ops vfio_pci_ops = {
 
 static int vfio_pci_probe(struct pci_dev *pdev, const struct pci_device_id *id)
 {
-	u8 type;
 	struct vfio_pci_device *vdev;
 	struct iommu_group *group;
 	int ret;
 
-	pci_read_config_byte(pdev, PCI_HEADER_TYPE, &type);
-	if ((type & PCI_HEADER_TYPE) != PCI_HEADER_TYPE_NORMAL)
+	if (pdev->hdr_type != PCI_HEADER_TYPE_NORMAL)
 		return -EINVAL;
 
 	group = iommu_group_get(&pdev->dev);
Signature · 1 line
1.7.9.5
#5 lore raw
Quoted 7 lines · Wei Yang

Current vfio-pci just supports normal pci device, so vfio_pci_probe() will return if the pci device is not a normal device. While current code makes a mistake. PCI_HEADER_TYPE is the offset in configuration space of the device type, but we use this value to mask the type value.

This patch fixs this by do the check directly on the pci_dev->hdr_type.

Quoted 8 lines

V2:
* Fix the problem by checking pci_dev->hdr_type

---
 drivers/vfio/pci/vfio_pci.c |    4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)

Applied to my for-linus branch for v3.19. I've also tagged it for stable. Thanks,

Alex

Quoted 19 lines
drivers/vfio/pci/vfio_pci.c +1 -3
index 9558da3..2f8a055 100644
@@ -839,13 +839,11 @@ static const struct vfio_device_ops vfio_pci_ops = {
 
 static int vfio_pci_probe(struct pci_dev *pdev, const struct pci_device_id *id)
 {
-	u8 type;
 	struct vfio_pci_device *vdev;
 	struct iommu_group *group;
 	int ret;
 
-	pci_read_config_byte(pdev, PCI_HEADER_TYPE, &type);
-	if ((type & PCI_HEADER_TYPE) != PCI_HEADER_TYPE_NORMAL)
+	if (pdev->hdr_type != PCI_HEADER_TYPE_NORMAL)
 		return -EINVAL;
 
 	group = iommu_group_get(&pdev->dev);