Skip to contentSkip to conversation

VT-d: fix VF of RC integrated PF matched to wrong VT-d unit

PATCH Superseded Chao Gao 4 participants 4 messages Jul 6, 2017 – Jul 7, 2017

v5 of 6 Superseded Latest is v6 →

lore
Share & tools
lore

Chao Gao

author
#0 lore raw

PATCH v5 1/1 VT-d: fix VF of RC integrated PF matched to wrong VT-d unit

The problem is for a VF of RC integrated PF (e.g. PF's BDF is 00:02.0), we would wrongly use 00:00.0 to search VT-d unit.

If a PF is an extended function, the BDF of a traditional function within the same device should be used to search VT-d unit. Otherwise, the real BDF of PF should be used. According PCI-e spec, an extended function is a function within an ARI device and Function Number is greater than 7. The original code tried to tell apart them through checking PCI_SLOT(), missing counterpart of pci_ari_enabled() (this function exists in linux kernel) compared to linux kernel. Without checking whether ARI is enabled, it incurs a RC integrated PF with PCI_SLOT() >0 is wrongly classified to an extended function. Note that a RC integrated function isn't within an ARI device and thus cannot be extended function and in this case the real BDF should be used.

Considering 'is_extfn' field of struct pci_dev has been passed down from Domain0 to indicate whether the function is an extended function, this patch just looks up the 'is_extfn' field of PF's struct pci_dev and set 'devfn' to 0 when 'is_extfn' is true.

---
v5:
- Commit description change.
v4:
- access pf's struct pci_pdev between pcidevs_lock() and pcidevs_unlock()

---
 xen/drivers/passthrough/vtd/dmar.c | 11 ++++++++++-
 1 file changed, 10 insertions(+), 1 deletion(-)
xen/drivers/passthrough/vtd/dmar.c +10 -1
index 82040dd..8724f0a 100644
@@ -218,8 +218,17 @@ struct acpi_drhd_unit *acpi_find_matched_drhd_unit(const struct pci_dev *pdev)
     }
     else if ( pdev->info.is_virtfn )
     {
+        struct pci_dev *physfn;
+
         bus = pdev->info.physfn.bus;
-        devfn = PCI_SLOT(pdev->info.physfn.devfn) ? 0 : pdev->info.physfn.devfn;
+        /*
+         * Use 0 as 'devfn' to search VT-d unit when the physical function
+         * is an Extended Function.
+         */
+        pcidevs_lock();
+        physfn = pci_get_pdev(pdev->seg, bus, pdev->info.physfn.devfn);
+        devfn = (physfn && physfn->info.is_extfn) ? 0 : pdev->info.physfn.devfn;
+        pcidevs_unlock();
     }
     else
     {
Signature · 7 lines
1.8.3.1


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
Acked-by on v5 1/1
#1 lore raw
Quoted 20 lines

From: Gao, Chao
Sent: Thursday, July 6, 2017 3:58 PM

The problem is for a VF of RC integrated PF (e.g. PF's BDF is 00:02.0), we would wrongly use 00:00.0 to search VT-d unit.

If a PF is an extended function, the BDF of a traditional function within the same device should be used to search VT-d unit. Otherwise, the real BDF of PF
should be used. According PCI-e spec, an extended function is a function within an ARI device and Function Number is greater than 7. The original code
tried to tell apart them through checking PCI_SLOT(), missing counterpart of pci_ari_enabled() (this function exists in linux kernel) compared to linux kernel. Without checking whether ARI is enabled, it incurs a RC integrated PF with PCI_SLOT() >0 is wrongly classified to an extended function. Note that a RC integrated function isn't within an ARI device and thus cannot be extended
function and in this case the real BDF should be used.

Quoted 7 lines

Considering 'is_extfn' field of struct pci_dev has been passed down from Domain0 to indicate whether the function is an extended function, this patch just looks up the 'is_extfn' field of PF's struct pci_dev and set 'devfn' to 0 when 'is_extfn' is true.

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel

#2 lore raw
In reply to Chao Gao
Quoted 41 lines · Chao Gao

The problem is for a VF of RC integrated PF (e.g. PF's BDF is 00:02.0), we would wrongly use 00:00.0 to search VT-d unit.

If a PF is an extended function, the BDF of a traditional function within the same device should be used to search VT-d unit. Otherwise, the real BDF of PF should be used. According PCI-e spec, an extended function is a function within an ARI device and Function Number is greater than 7. The original code tried to tell apart them through checking PCI_SLOT(), missing counterpart of pci_ari_enabled() (this function exists in linux kernel) compared to linux kernel. Without checking whether ARI is enabled, it incurs a RC integrated PF with PCI_SLOT() >0 is wrongly classified to an extended function. Note that a RC integrated function isn't within an ARI device and thus cannot be extended function and in this case the real BDF should be used.

Considering 'is_extfn' field of struct pci_dev has been passed down from Domain0 to indicate whether the function is an extended function, this patch just looks up the 'is_extfn' field of PF's struct pci_dev and set 'devfn' to 0 when 'is_extfn' is true.

---
v5:
- Commit description change.
v4:
- access pf's struct pci_pdev between pcidevs_lock() and pcidevs_unlock()

---
 xen/drivers/passthrough/vtd/dmar.c | 11 ++++++++++-
 1 file changed, 10 insertions(+), 1 deletion(-)
xen/drivers/passthrough/vtd/dmar.c +2 -0
index 82040dd..8724f0a 100644
@@ -218,8 +218,17 @@ struct acpi_drhd_unit *acpi_find_matched_drhd_unit(const struct pci_dev *pdev)
     }
     else if ( pdev->info.is_virtfn )
     {
+        struct pci_dev *physfn;
+

xen/drivers/passthrough/vtd/dmar.c struct acpi_drhd_unit *acpi_find_matched_drhd_unit(const struct pci_dev *pdev) · line 231

Quoted 10 lines
xen/drivers/passthrough/vtd/dmar.c +8 -1
         bus = pdev->info.physfn.bus;
-        devfn = PCI_SLOT(pdev->info.physfn.devfn) ? 0 : pdev->info.physfn.devfn;
+        /*
+         * Use 0 as 'devfn' to search VT-d unit when the physical function
+         * is an Extended Function.
+         */
+        pcidevs_lock();
+        physfn = pci_get_pdev(pdev->seg, bus, pdev->info.physfn.devfn);
+        devfn = (physfn && physfn->info.is_extfn) ? 0 : pdev->info.physfn.devfn;
+        pcidevs_unlock();

Just as a note (not that I intend you to fix this), but AFAICT this function should be called holding the pcidevs lock, or else there's the risk that the pdev argument is freed while poking at it.

Roger.

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel

#3 lore raw
Quoted 16 lines
Quoted 1 line
Quoted 1 line · On 07.07.17 at 10:13, <[email protected]> wrote:
Quoted 14 lines · Chao Gao
xen/drivers/passthrough/vtd/dmar.c +6 -1
@@ -218,8 +218,17 @@ struct acpi_drhd_unit *acpi_find_matched_drhd_unit(const struct pci_dev *pdev)
     }
     else if ( pdev->info.is_virtfn )
     {
+        struct pci_dev *physfn;
+
         bus = pdev->info.physfn.bus;
-        devfn = PCI_SLOT(pdev->info.physfn.devfn) ? 0 : pdev->info.physfn.devfn;
+        /*
+         * Use 0 as 'devfn' to search VT-d unit when the physical function
+         * is an Extended Function.
+         */
Quoted 8 lines

xen/drivers/passthrough/vtd/dmar.c struct acpi_drhd_unit *acpi_find_matched_drhd_unit(const struct pci_dev *pdev) · line 231

Quoted 4 lines
xen/drivers/passthrough/vtd/dmar.c +4 -0
+        pcidevs_lock();
+        physfn = pci_get_pdev(pdev->seg, bus, pdev->info.physfn.devfn);
+        devfn = (physfn && physfn->info.is_extfn) ? 0 : pdev->info.physfn.devfn;
+        pcidevs_unlock();

Just as a note (not that I intend you to fix this), but AFAICT this function should be called holding the pcidevs lock, or else there's the risk that the pdev argument is freed while poking at it.

As pointed out in discussion on (I think) one of your recent patch series, it is well known that we don't take that lock in all the places we should, and we really should ref-count struct pci_dev instances. I don't think dealing with the issue in individual places would be very useful - if anything, we'd have to audit the entire code base.

Jan

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel