xen: SR-IOV fixes
v5 cover xen: SR-IOV fixes
A fix for handling of stale pdevs, and a fix for a regressiong related to a locking change.
Stewart Hildebrand (3):
x86/msi: harden stale pdev handling
xen/pci: introduce PF<->VF links
x86/msi: fix locking for SR-IOV devices
xen/arch/x86/msi.c | 51 ++++++++++++++++------- xen/drivers/passthrough/pci.c | 78 +++++++++++++++++++++++++++-------- xen/include/xen/pci.h | 17 +++++++- 3 files changed, 112 insertions(+), 34 deletions(-)
base-commit: 76a54badf890f56ff72644593c0fbc72138e13aa
Signature · 1 line
2.47.0
PATCH v5 1/3 x86/msi: harden stale pdev handling
Dom0 normally informs Xen of PCI device removal via PHYSDEVOP_pci_device_remove, e.g. in response to SR-IOV disable or hot-unplug. We might find ourselves with stale pdevs if a buggy dom0 fails to report removal via PHYSDEVOP_pci_device_remove. In this case, attempts to access the config space of the stale pdevs would be invalid and return all 1s.
Some possible conditions leading to this are:
1. Dom0 disables SR-IOV without reporting VF removal to Xen.
The Linux SR-IOV subsystem normally reports VF removal when a PF driver disables SR-IOV. In case of a buggy dom0 SR-IOV subsystem, SR-IOV could become disabled with stale dangling VF pdevs in both dom0 Linux and Xen.
2. Dom0 reporting PF removal without reporting VF removal.
During SR-IOV PF removal (hot-unplug), a buggy PF driver may fail to disable SR-IOV, thus failing to remove the VFs, leaving stale dangling VFs behind in both Xen and Linux. At least Linux warns in this case:
[ 100.000000] 0000:01:00.0: driver left SR-IOV enabled after remove
In either case, Xen is left with stale VF pdevs, risking invalid PCI config space accesses.
When Xen is built with CONFIG_DEBUG=y, the following Xen crashes were observed when dom0 attempted to access the config space of a stale VF:
(XEN) Assertion 'pos' failed at arch/x86/msi.c:1274
(XEN) ----[ Xen-4.20-unstable x86_64 debug=y Tainted: C ]----
...
(XEN) Xen call trace:
(XEN) [<ffff82d040346834>] R pci_msi_conf_write_intercept+0xa2/0x1de (XEN) [<ffff82d04035d6b4>] F pci_conf_write_intercept+0x68/0x78 (XEN) [<ffff82d0403264e5>] F arch/x86/pv/emul-priv-op.c#pci_cfg_ok+0xa0/0x114 (XEN) [<ffff82d04032660e>] F arch/x86/pv/emul-priv-op.c#guest_io_write+0xb5/0x1c8 (XEN) [<ffff82d0403267bb>] F arch/x86/pv/emul-priv-op.c#write_io+0x9a/0xe0 (XEN) [<ffff82d04037c77a>] F x86_emulate+0x100e5/0x25f1e (XEN) [<ffff82d0403941a8>] F x86_emulate_wrapper+0x29/0x64 (XEN) [<ffff82d04032802b>] F pv_emulate_privileged_op+0x12e/0x217 (XEN) [<ffff82d040369f12>] F do_general_protection+0xc2/0x1b8 (XEN) [<ffff82d040201aa7>] F x86_64/entry.S#handle_exception_saved+0x2b/0x8c
(XEN) Assertion 'pos' failed at arch/x86/msi.c:1246
(XEN) ----[ Xen-4.20-unstable x86_64 debug=y Tainted: C ]----
...
(XEN) Xen call trace:
(XEN) [<ffff82d040346b0a>] R pci_reset_msix_state+0x47/0x50 (XEN) [<ffff82d040287eec>] F pdev_msix_assign+0x19/0x35 (XEN) [<ffff82d040286184>] F drivers/passthrough/pci.c#assign_device+0x181/0x471 (XEN) [<ffff82d040287c36>] F iommu_do_pci_domctl+0x248/0x2ec (XEN) [<ffff82d040284e1f>] F iommu_do_domctl+0x26/0x44 (XEN) [<ffff82d0402483b8>] F do_domctl+0x8c1/0x1660 (XEN) [<ffff82d04032977e>] F pv_hypercall+0x5ce/0x6af (XEN) [<ffff82d0402012d3>] F lstar_enter+0x143/0x150
Replace the ASSERT(s) with an error, and mark the device broken to disallow passthrough to domUs.
---v4->v5:
* new patch, independent of the rest of the series
* new approach to fixing the issue: don't rely on dom0 to report any sort of device removal; rather, fix the condition directly
---
Instructions to reproduce
Requires Xen with CONFIG_DEBUG=y
Tested with Linux 6.11
1. Dom0 disables SR-IOV without reporting VF removal to Xen.
* Hack the Linux SR-IOV subsystem to remove the call to pci_stop_and_remove_bus_device() in drivers/pci/iov.c:pci_iov_remove_virtfn().
* Enable SR-IOV, then disable SR-IOV
echo 1 > /sys/bus/pci/devices/0000\:01\:00.0/sriov_numvfs echo 0 > /sys/bus/pci/devices/0000\:01\:00.0/sriov_numvfs
* Now we have a stale VF. We can trigger the ASSERT either by unbinding the VF driver and issuing a reset...
echo 0000\:01\:10.0 > /sys/bus/pci/devices/0000\:01\:10.0/driver/unbind echo 1 > /sys/bus/pci/devices/0000\:01\:10.0/reset
... or by doing xl pci-assignable-add
xl pci-assignable-add 01:10.0
2. Dom0 reporting PF removal without reporting VF removal.
* Hack your PF driver to leave SR-IOV enabled when removing the device
* Enable SR-IOV
echo 1 > /sys/bus/pci/devices/0000\:01\:00.0/sriov_numvfs
* Unplug the PCI device
(qemu) device_del mydev
* Now we have a stale VF. We can trigger the ASSERT either by re-adding the PF device with SR-IOV disabled...
echo 0000\:01\:10.0 > /sys/bus/pci/devices/0000\:01\:10.0/driver/unbind (qemu) device_add igb,id=mydev,bus=pcie.1,netdev=net1
... or by reset / xl pci-assignable-add as above.
--- xen/arch/x86/msi.c | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-)
xen/arch/x86/msi.c +12 -2
@@ -1243,7 +1243,12 @@ int pci_reset_msix_state(struct pci_dev *pdev) {unsigned int pos =pci_find_cap_offset (pdev->sbdf,PCI_CAP_ID_MSIX ); -ASSERT (pos); +if ( !pos ) + { + pdev->broken = true; +return -EFAULT ; + } +/* * Xen expects the device state to be the after reset one, and hence * host_maskall = guest_maskall = false and all entries should have the
@@ -1271,7 +1276,12 @@ int pci_msi_conf_write_intercept(struct pci_dev *pdev, unsigned int reg, entry =find_msi_entry (pdev,-1 ,PCI_CAP_ID_MSIX ); pos = entry ? entry->msi_attrib.pos :pci_find_cap_offset (pdev->sbdf,PCI_CAP_ID_MSIX ); -ASSERT (pos); + +if ( !pos ) + { + pdev->broken = true; +return -EFAULT ; + }if ( reg >= pos && reg <msix_pba_offset_reg (pos) +4 ) {
Signature · 1 line
2.47.0
Re: [PATCH v5 1/3] x86/msi: harden stale pdev handling
Quoted 20 lines · Stewart Hildebrand
xen/arch/x86/msi.c +6
-1@@ -1243,7 +1243,12 @@ int pci_reset_msix_state(struct pci_dev *pdev) {unsigned int pos =pci_find_cap_offset (pdev->sbdf,PCI_CAP_ID_MSIX ); -ASSERT (pos); +if ( !pos ) + { + pdev->broken = true; +return -EFAULT ; + } +/* * Xen expects the device state to be the after reset one, and hence * host_maskall = guest_maskall = false and all entries should have the @@ -1271,7 +1276,12 @@ int pci_msi_conf_write_intercept(struct pci_dev *pdev, unsigned int reg, entry =find_msi_entry (pdev,-1 ,PCI_CAP_ID_MSIX ); pos = entry ? entry->msi_attrib.pos :pci_find_cap_offset (pdev->sbdf,PCI_CAP_ID_MSIX );
xen/arch/x86/msi.c int pci_msi_conf_write_intercept(struct pci_dev *pdev, unsigned int reg, · line 1287
Quoted 10 lines
xen/arch/x86/msi.c +6
-1-ASSERT (pos); + +if ( !pos ) + { + pdev->broken = true; +return -EFAULT ; + }if ( reg >= pos && reg <msix_pba_offset_reg (pos) +4 ) {
There are more instances of pci_find_cap_offset(..., PCI_CAP_ID_MSIX) which may want/need dealing with, even if there are no ASSERT()s there.
Setting ->broken is of course a perhaps desirable (side) effect. Nevertheless I wonder whether latching the capability position once during device init wouldn't be an alternative (better?) approach.
Finally I don't think -EFAULT is appropriate here. Imo it should be -ENODEV.
Jan
Re: [PATCH v5 1/3] x86/msi: harden stale pdev handling
Quoted 29 lines · Jan Beulich
Quoted 28 lines · Stewart Hildebrand
xen/arch/x86/msi.c +12
-2@@ -1243,7 +1243,12 @@ int pci_reset_msix_state(struct pci_dev *pdev) {unsigned int pos =pci_find_cap_offset (pdev->sbdf,PCI_CAP_ID_MSIX ); -ASSERT (pos); +if ( !pos ) + { + pdev->broken = true; +return -EFAULT ; + } +/* * Xen expects the device state to be the after reset one, and hence * host_maskall = guest_maskall = false and all entries should have the @@ -1271,7 +1276,12 @@ int pci_msi_conf_write_intercept(struct pci_dev *pdev, unsigned int reg, entry =find_msi_entry (pdev,-1 ,PCI_CAP_ID_MSIX ); pos = entry ? entry->msi_attrib.pos :pci_find_cap_offset (pdev->sbdf,PCI_CAP_ID_MSIX ); -ASSERT (pos); + +if ( !pos ) + { + pdev->broken = true; +return -EFAULT ; + }
Quoted 5 lines
xen/arch/x86/msi.c int pci_msi_conf_write_intercept(struct pci_dev *pdev, unsigned int reg, · line 1287
Quoted 2 lines
xen/arch/x86/msi.c +0
-0if ( reg >= pos && reg <msix_pba_offset_reg (pos) +4 ) {There are more instances of
pci_find_cap_offset(..., PCI_CAP_ID_MSIX)which may want/need dealing with, even if there are no ASSERT()s there.
Yes, and some instances of pci_find_cap_offset(..., PCI_CAP_ID_MSI) too.
Quoted 3 lines
Setting ->broken is of course a perhaps desirable (side) effect. Nevertheless I wonder whether latching the capability position once during device init wouldn't be an alternative (better?) approach.
I'll give this a try for the next rev.
Quoted 1 line
Finally I don't think
-EFAULTis appropriate here. Imo it should be-ENODEV.
OK
Quoted 2 lines
Jan
PATCH v5 2/3 xen/pci: introduce PF<->VF links
In reply to Stewart HildebrandAdd links between a VF's struct pci_dev and its associated PF struct pci_dev. Move the calls to pci_get_pdev()/pci_add_device() down to avoid dropping and re-acquiring the pcidevs_lock().
During PF removal, unlink VF from PF and mark the VF broken. As before, VFs may exist without a corresponding PF, although now only with pdev->broken = true. If the PF is removed and re-added, dom0 is expected to also remove and re-add the VFs.
---
Candidate for backport to 4.19 (the next patch depends on this one)
v4->v5:
* new patch, split from ("x86/msi: fix locking for SR-IOV devices")
* move INIT_LIST_HEAD(&pdev->vf_list); earlier
* collapse struct list_head instances
* retain error code from pci_add_device()
* unlink (and mark broken) VFs instead of removing them
* const-ify VF->PF link
--- xen/drivers/passthrough/pci.c | 70 ++++++++++++++++++++++++++++------- xen/include/xen/pci.h | 10 +++++ 2 files changed, 66 insertions(+), 14 deletions(-)
xen/drivers/passthrough/pci.c +56 -14
@@ -333,6 +333,8 @@ static struct pci_dev *alloc_pdev(struct pci_seg *pseg, u8 bus, u8 devfn) *((u8 *) &pdev->devfn) = devfn; pdev->domain =NULL ; +INIT_LIST_HEAD (&pdev->vf_list); +arch_pci_init_pdev (pdev); rc =pdev_msi_init (pdev);
@@ -449,6 +451,10 @@ static void free_pdev(struct pci_seg *pseg, struct pci_dev *pdev)list_del (&pdev->alldevs_list);pdev_msi_deinit (pdev); + +if ( pdev->info.is_virtfn && pdev->virtfn.pf_pdev ) +list_del (&pdev->vf_list); +xfree (pdev); }
@@ -656,24 +662,11 @@ int pci_add_device(u16 seg, u8 bus, u8 devfn,unsigned int slot =PCI_SLOT (devfn), func =PCI_FUNC (devfn);const char *type;int ret; -bool pf_is_extfn = false;if ( !info ) type ="device" ;else if ( info->is_virtfn ) - { -pcidevs_lock (); - pdev =pci_get_pdev (NULL , -PCI_SBDF (seg, info->physfn.bus, - info->physfn.devfn)); -if ( pdev ) - pf_is_extfn = pdev->info.is_extfn; -pcidevs_unlock (); -if ( !pdev ) -pci_add_device (seg, info->physfn.bus, info->physfn.devfn, -NULL , node); type ="virtual function" ; - }else if ( info->is_extfn ) type ="extended function" ;else
@@ -703,7 +696,44 @@ int pci_add_device(u16 seg, u8 bus, u8 devfn,* extended function. */ if ( pdev->info.is_virtfn ) - pdev->info.is_extfn = pf_is_extfn; + { +struct pci_dev *pf_pdev; + + pf_pdev =pci_get_pdev (NULL , +PCI_SBDF (seg, info->physfn.bus, + info->physfn.devfn)); + +if ( !pf_pdev ) + { + ret =pci_add_device (seg, info->physfn.bus, info->physfn.devfn, +NULL , node); +if ( ret ) + { +printk (XENLOG_WARNING "Failed to add SR-IOV device PF %pp for VF %pp\n" , + &PCI_SBDF (seg, info->physfn.bus, info->physfn.devfn), + &pdev->sbdf); +free_pdev (pseg, pdev); + goto out; + } + pf_pdev =pci_get_pdev (NULL , +PCI_SBDF (seg, info->physfn.bus, + info->physfn.devfn)); +if ( !pf_pdev ) + { +ASSERT_UNREACHABLE (); +printk (XENLOG_ERR "Failed to find SR-IOV device PF %pp for VF %pp\n" , + &PCI_SBDF (seg, info->physfn.bus, info->physfn.devfn), + &pdev->sbdf); +free_pdev (pseg, pdev); + ret = -EILSEQ ; + goto out; + } + } + + pdev->info.is_extfn = pf_pdev->info.is_extfn; + pdev->virtfn.pf_pdev = pf_pdev; +list_add (&pdev->vf_list, &pf_pdev->vf_list); + } }if ( !pdev->info.is_virtfn && !pdev->vf_rlen[0 ] )
@@ -821,6 +851,18 @@ int pci_remove_device(u16 seg, u8 bus, u8 devfn) list_for_each_entry ( pdev, &pseg->alldevs_list, alldevs_list )if ( pdev->bus == bus && pdev->devfn == devfn ) { +if ( !pdev->info.is_virtfn ) + { +struct pci_dev *vf_pdev, *tmp; + +list_for_each_entry_safe (vf_pdev, tmp, &pdev->vf_list, vf_list) + { +list_del (&vf_pdev->vf_list); + vf_pdev->virtfn.pf_pdev =NULL ; + vf_pdev->broken = true; + } + } +if ( pdev->domain ) {write_lock (&pdev->domain->pci_lock);
xen/include/xen/pci.h +10 -0
@@ -150,7 +150,17 @@ struct pci_dev {unsigned int count;#define PT_FAULT_THRESHOLD 10 } fault; + +/* +* List head if info.is_virtfn == false +* List entry if info.is_virtfn == true +*/ +struct list_head vf_list;u64 vf_rlen[6 ]; +struct { +/* Only populated for VFs (info.is_virtfn == true) */ +const struct pci_dev *pf_pdev;/* Link from VF to PF */ + } virtfn;/* Data for vPCI. */ struct vpci *vpci;
Signature · 1 line
2.47.0
Re: [PATCH v5 2/3] xen/pci: introduce PF<->VF links
Quoted 8 lines · Stewart Hildebrand
Add links between a VF's struct
pci_devand its associated PF structpci_dev. Move the calls to pci_get_pdev()/pci_add_device() down to avoid dropping and re-acquiring thepcidevs_lock().During PF removal, unlink VF from PF and mark the VF broken. As before, VFs may exist without a corresponding PF, although now only with
pdev->broken= true. If the PF is removed and re-added, dom0 is expected to also remove and re-add the VFs.
Right, or else the VF struct instance would remain orphaned the way you've implemented this. Question is whether it is a reasonable assumption that a Dom0 which failed to remove the VFs during PF removal might later "remember" that it still needs to report VFs removed. I for one doubt that.
Quoted 26 lines
xen/drivers/passthrough/pci.c +21
-1@@ -703,7 +696,44 @@ int pci_add_device(u16 seg, u8 bus, u8 devfn,* extended function. */ if ( pdev->info.is_virtfn ) - pdev->info.is_extfn = pf_is_extfn; + { +struct pci_dev *pf_pdev; + + pf_pdev =pci_get_pdev (NULL , +PCI_SBDF (seg, info->physfn.bus, + info->physfn.devfn)); + +if ( !pf_pdev ) + { + ret =pci_add_device (seg, info->physfn.bus, info->physfn.devfn, +NULL , node); +if ( ret ) + { +printk (XENLOG_WARNING "Failed to add SR-IOV device PF %pp for VF %pp\n" , + &PCI_SBDF (seg, info->physfn.bus, info->physfn.devfn), + &pdev->sbdf); +free_pdev (pseg, pdev); + goto out; + } + pf_pdev = pci_get_pdev(NULL, + PCI_SBDF(seg, info->physfn.bus,
xen/drivers/passthrough/pci.c int pci_add_device(u16 seg, u8 bus, u8 devfn, · line 729
Quoted 10 lines
xen/drivers/passthrough/pci.c +10
-0+ info->physfn.devfn)); +if ( !pf_pdev ) + { +ASSERT_UNREACHABLE (); +printk (XENLOG_ERR "Failed to find SR-IOV device PF %pp for VF %pp\n" , + &PCI_SBDF (seg, info->physfn.bus, info->physfn.devfn), + &pdev->sbdf); +free_pdev (pseg, pdev); + ret = -EILSEQ ; + goto out;
Might be helpful to have the printk() ahead of the ASSERT_UNREACHABLE(), in the unlikely event that the assertion would actually trigger. Positioning doesn't make a difference for release builds anyway.
Jan
Re: [PATCH v5 2/3] xen/pci: introduce PF<->VF links
Quoted 10 lines · Jan Beulich
Quoted 8 lines · Stewart Hildebrand
Add links between a VF's struct
pci_devand its associated PF structpci_dev. Move the calls to pci_get_pdev()/pci_add_device() down to avoid dropping and re-acquiring thepcidevs_lock().During PF removal, unlink VF from PF and mark the VF broken. As before, VFs may exist without a corresponding PF, although now only with
pdev->broken= true. If the PF is removed and re-added, dom0 is expected to also remove and re-add the VFs.
Quoted 4 lines
Right, or else the VF struct instance would remain orphaned the way you've implemented this. Question is whether it is a reasonable assumption that a Dom0 which failed to remove the VFs during PF removal might later "remember" that it still needs to report VFs removed. I for one doubt that.
This matches my observations: you're right, it most likely won't. I had just written it in a misleading way in the commit message. Re-adding should not occur until after VFs and PF have been removed in non-buggy scenarios.
A PF driver that fails to disable SR-IOV (i.e remove VFs) during PF removal is buggy, and would lead to issues inside dom0 as well due to the orphaned/stale VF left behind in Linux dom0 itself. As mentioned in patch #1, Linux warns about this:
[ 100.000000] 0000:01:00.0: driver left SR-IOV enabled after remove
With the PF<->VF links in place, we now also have the ability to detect and warn about this potentially buggy condition in Xen. I have so far only observed VF-then-PF removal order in non-buggy scenarios. My only hesitation with adding such a warning in Xen is that I don't know whether removing in PF-then-VF order is legitimate.
In the previous rev I had Xen removing the VFs during PF removal. In this rev, I chose to continue to rely on dom0 to remove VFs because:
1. This is the expected behavior as far as I can tell.
2. It more accurately mirrors the state of the VFs and PFs in dom0 and Xen.
3. No need to consider special cases when xsm has different policies for PF and VF removal operations.
Perhaps the last sentence would be better written as:
"The hardware domain is expected to remove the associated VFs before removing the PF. Print a warning in case a PF is removed with associated VFs still present."
Or, not implying any removal order:
"If the PF is removed, the hardware domain is expected to also remove the associated VFs."
I'm personally leaning toward not implying any removal order (i.e. same situation as before this patch).
Quoted 31 lines
Quoted 31 lines
xen/drivers/passthrough/pci.c +26
-1@@ -703,7 +696,44 @@ int pci_add_device(u16 seg, u8 bus, u8 devfn,* extended function. */ if ( pdev->info.is_virtfn ) - pdev->info.is_extfn = pf_is_extfn; + { +struct pci_dev *pf_pdev; + + pf_pdev =pci_get_pdev (NULL , +PCI_SBDF (seg, info->physfn.bus, + info->physfn.devfn)); + +if ( !pf_pdev ) + { + ret =pci_add_device (seg, info->physfn.bus, info->physfn.devfn, +NULL , node); +if ( ret ) + { +printk (XENLOG_WARNING "Failed to add SR-IOV device PF %pp for VF %pp\n" , + &PCI_SBDF (seg, info->physfn.bus, info->physfn.devfn), + &pdev->sbdf); +free_pdev (pseg, pdev); + goto out; + } + pf_pdev =pci_get_pdev (NULL , +PCI_SBDF (seg, info->physfn.bus, + info->physfn.devfn)); +if ( !pf_pdev ) + { +ASSERT_UNREACHABLE (); + printk(XENLOG_ERR "Failed to find SR-IOV device PF %pp for VF %pp\n",
Quoted 8 lines
xen/drivers/passthrough/pci.c int pci_add_device(u16 seg, u8 bus, u8 devfn, · line 729
Quoted 5 lines
xen/drivers/passthrough/pci.c +5
-0+ &PCI_SBDF (seg, info->physfn.bus, info->physfn.devfn), + &pdev->sbdf); +free_pdev (pseg, pdev); + ret = -EILSEQ ; + goto out;Might be helpful to have the
printk()ahead of theASSERT_UNREACHABLE(), in the unlikely event that the assertion would actually trigger.
Agreed.
Quoted 4 lines
Positioning doesn't
make a difference for release builds anyway.Jan
Re: [PATCH v5 2/3] xen/pci: introduce PF<->VF links
Quoted 26 lines · Stewart Hildebrand
Quoted 10 lines · Jan Beulich
Quoted 8 lines · Stewart Hildebrand
Add links between a VF's struct
pci_devand its associated PF structpci_dev. Move the calls to pci_get_pdev()/pci_add_device() down to avoid dropping and re-acquiring thepcidevs_lock().During PF removal, unlink VF from PF and mark the VF broken. As before, VFs may exist without a corresponding PF, although now only with
pdev->broken= true. If the PF is removed and re-added, dom0 is expected to also remove and re-add the VFs.Quoted 4 lines
Right, or else the VF struct instance would remain orphaned the way you've implemented this. Question is whether it is a reasonable assumption that a Dom0 which failed to remove the VFs during PF removal might later "remember" that it still needs to report VFs removed. I for one doubt that.
This matches my observations: you're right, it most likely won't. I had just written it in a misleading way in the commit message. Re-adding should not occur until after VFs and PF have been removed in non-buggy scenarios.
A PF driver that fails to disable SR-IOV (i.e remove VFs) during PF removal is buggy, and would lead to issues inside dom0 as well due to the orphaned/stale VF left behind in Linux dom0 itself. As mentioned in patch #1, Linux warns about this:
Quoted 7 lines
[ 100.000000] 0000:01:00.0: driver left SR-IOV enabled after removeWith the PF<->VF links in place, we now also have the ability to detect and warn about this potentially buggy condition in Xen. I have so far only observed VF-then-PF removal order in non-buggy scenarios. My only hesitation with adding such a warning in Xen is that I don't know whether removing in PF-then-VF order is legitimate.
My take here is that it's not legitimate. VFs with no PF is physically impossible, and hence also ought to be logically invalid. It's the PF that surfaces (materializes) the VFs, after all.
Jan
PATCH v5 3/3 x86/msi: fix locking for SR-IOV devices
In reply to Stewart HildebrandIn commit 4f78438b45e2 ("vpci: use per-domain PCI lock to protect vpci structure") a lock was moved from allocate_and_map_msi_pirq() to the caller and changed from pcidevs_lock() to read_lock(&d->pci_lock). However, one call path wasn't updated to reflect the change, leading to a failed assertion observed under the following conditions:
* PV dom0
* Debug build (CONFIG_DEBUG=y) of Xen
* There is an SR-IOV device in the system with one or more VFs enabled
* Dom0 has loaded the driver for the VF and enabled MSI-X
(XEN) Assertion 'd || pcidevs_locked()' failed at drivers/passthrough/pci.c:535 (XEN) ----[ Xen-4.20-unstable x86_64 debug=y Not tainted ]----
...
(XEN) Xen call trace:
(XEN) [<ffff82d040284da8>] R pci_get_pdev+0x4c/0xab (XEN) [<ffff82d040344f5c>] F arch/x86/msi.c#read_pci_mem_bar+0x58/0x272 (XEN) [<ffff82d04034530e>] F arch/x86/msi.c#msix_capability_init+0x198/0x755 (XEN) [<ffff82d040345dad>] F arch/x86/msi.c#__pci_enable_msix+0x82/0xe8 (XEN) [<ffff82d0403463e5>] F pci_enable_msi+0x3f/0x78 (XEN) [<ffff82d04034be2b>] F map_domain_pirq+0x2a4/0x6dc (XEN) [<ffff82d04034d4d5>] F allocate_and_map_msi_pirq+0x103/0x262 (XEN) [<ffff82d04035da5d>] F physdev_map_pirq+0x210/0x259 (XEN) [<ffff82d04035e798>] F do_physdev_op+0x9c3/0x1454 (XEN) [<ffff82d040329475>] F pv_hypercall+0x5ac/0x6af (XEN) [<ffff82d0402012d3>] F lstar_enter+0x143/0x150
In read_pci_mem_bar(), the VF obtains the struct pci_dev pointer for its associated PF to access the vf_rlen array. This array is initialized in pci_add_device() and is only populated in the associated PF's struct pci_dev.
Access the vf_rlen array via the link to the PF, and remove the troublesome call to pci_get_pdev().
---
Candidate for backport to 4.19
Patch #2 ("xen/pci: introduce PF<->VF links") is pre-requisite
v4->v5:
* split the PF<->VF links to a pre-requisite patch
* pass pci_sbdf_t to read_pci_mem_bar()
* use stdint.h types on changed lines
* re-add NULL check for pf_info in read_pci_mem_bar(), as pf_info could be NULL
v3->v4:
* handle case when PF is removed with VFs enabled, then re-added with VFs disabled
v2->v3:
* link from VF to PF's struct pci_dev *
v1->v2:
* remove call to pci_get_pdev()
--- xen/arch/x86/msi.c | 37 ++++++++++++++++++++++------------- xen/drivers/passthrough/pci.c | 8 +++++--- xen/include/xen/pci.h | 15 +++++++++----- 3 files changed, 38 insertions(+), 22 deletions(-)
xen/arch/x86/msi.c +23 -14
@@ -662,34 +662,35 @@ static int msi_capability_init(struct pci_dev *dev,return 0 ; } -static u64 read_pci_mem_bar (u16 seg,u8 bus,u8 slot,u8 func,u8 bir,int vf) +static uint64_t read_pci_mem_bar (pci_sbdf_t sbdf,uint8_t bir,int vf, +const struct pf_info *pf_info) { +uint16_t seg = sbdf.seg; +uint8_t bus = sbdf.bus, slot = sbdf.dev, func = sbdf.fn;u8 limit;u32 addr, base =PCI_BASE_ADDRESS_0 ;u64 disp =0 ;if ( vf >=0 ) { -struct pci_dev *pdev =pci_get_pdev (NULL , -PCI_SBDF (seg, bus, slot, func));unsigned int pos;uint16_t ctrl, num_vf, offset, stride; -if ( !pdev ) +if ( !pf_info )return 0 ; - pos =pci_find_ext_capability (pdev->sbdf,PCI_EXT_CAP_ID_SRIOV ); - ctrl =pci_conf_read16 (pdev->sbdf, pos +PCI_SRIOV_CTRL ); - num_vf =pci_conf_read16 (pdev->sbdf, pos +PCI_SRIOV_NUM_VF ); - offset =pci_conf_read16 (pdev->sbdf, pos +PCI_SRIOV_VF_OFFSET ); - stride =pci_conf_read16 (pdev->sbdf, pos +PCI_SRIOV_VF_STRIDE ); + pos =pci_find_ext_capability (sbdf,PCI_EXT_CAP_ID_SRIOV ); + ctrl =pci_conf_read16 (sbdf, pos +PCI_SRIOV_CTRL ); + num_vf =pci_conf_read16 (sbdf, pos +PCI_SRIOV_NUM_VF ); + offset =pci_conf_read16 (sbdf, pos +PCI_SRIOV_VF_OFFSET ); + stride =pci_conf_read16 (sbdf, pos +PCI_SRIOV_VF_STRIDE );if ( !pos || !(ctrl &PCI_SRIOV_CTRL_VFE ) || !(ctrl &PCI_SRIOV_CTRL_MSE ) || !num_vf || !offset || (num_vf >1 && !stride) || bir >=PCI_SRIOV_NUM_BARS || - !pdev->vf_rlen[bir] ) + !pf_info->vf_rlen[bir] )return 0 ; base = pos +PCI_SRIOV_BAR ; vf -=PCI_BDF (bus, slot, func) + offset;
@@ -703,8 +704,8 @@ static u64 read_pci_mem_bar(u16 seg, u8 bus, u8 slot, u8 func, u8 bir, int vf) }if ( vf >= num_vf )return 0 ; -BUILD_BUG_ON (ARRAY_SIZE (pdev->vf_rlen) !=PCI_SRIOV_NUM_BARS ); - disp = vf * pdev->vf_rlen[bir]; +BUILD_BUG_ON (ARRAY_SIZE (pf_info->vf_rlen) !=PCI_SRIOV_NUM_BARS ); + disp = vf * pf_info->vf_rlen[bir]; limit =PCI_SRIOV_NUM_BARS ; } else switch ( pci_conf_read8(PCI_SBDF(seg, bus, slot, func),
@@ -813,6 +814,7 @@ static int msix_capability_init(struct pci_dev *dev,int vf;paddr_t pba_paddr;unsigned int pba_offset; +const struct pf_info *pf_info;if ( !dev->info.is_virtfn ) {
@@ -820,6 +822,7 @@ static int msix_capability_init(struct pci_dev *dev, pslot = slot; pfunc = func; vf =-1 ; + pf_info =NULL ; }else {
@@ -827,9 +830,14 @@ static int msix_capability_init(struct pci_dev *dev, pslot =PCI_SLOT (dev->info.physfn.devfn); pfunc =PCI_FUNC (dev->info.physfn.devfn); vf = dev->sbdf.bdf; +if ( dev->virtfn.pf_pdev ) + pf_info = &dev->virtfn.pf_pdev->physfn; +else + pf_info =NULL ; } - table_paddr =read_pci_mem_bar (seg, pbus, pslot, pfunc, bir, vf); + table_paddr =read_pci_mem_bar (PCI_SBDF (seg, pbus, pslot, pfunc), bir, + vf, pf_info);WARN_ON (msi && msi->table_base != table_paddr);if ( !table_paddr ) {
@@ -852,7 +860,8 @@ static int msix_capability_init(struct pci_dev *dev, pba_offset =pci_conf_read32 (dev->sbdf,msix_pba_offset_reg (pos)); bir = (u8)(pba_offset &PCI_MSIX_BIRMASK ); - pba_paddr =read_pci_mem_bar (seg, pbus, pslot, pfunc, bir, vf); + pba_paddr =read_pci_mem_bar (PCI_SBDF (seg, pbus, pslot, pfunc), bir, vf, + pf_info);WARN_ON (!pba_paddr); pba_paddr += pba_offset & ~PCI_MSIX_BIRMASK ;
xen/drivers/passthrough/pci.c +5 -3
@@ -736,7 +736,7 @@ int pci_add_device(u16 seg, u8 bus, u8 devfn, } } -if ( !pdev->info.is_virtfn && !pdev->vf_rlen[0 ] ) +if ( !pdev->info.is_virtfn && !pdev->physfn.vf_rlen[0 ] ) {unsigned int pos =pci_find_ext_capability (pdev->sbdf,PCI_EXT_CAP_ID_SRIOV );
@@ -748,7 +748,9 @@ int pci_add_device(u16 seg, u8 bus, u8 devfn, {unsigned int i; -BUILD_BUG_ON (ARRAY_SIZE (pdev->vf_rlen) !=PCI_SRIOV_NUM_BARS ); +BUILD_BUG_ON (ARRAY_SIZE (pdev->physfn.vf_rlen) != +PCI_SRIOV_NUM_BARS ); +for ( i =0 ; i <PCI_SRIOV_NUM_BARS ; ) {unsigned int idx = pos +PCI_SRIOV_BAR + i *4 ;
@@ -763,7 +765,7 @@ int pci_add_device(u16 seg, u8 bus, u8 devfn,continue ; } ret =pci_size_mem_bar (pdev->sbdf, idx,NULL , - &pdev->vf_rlen[i], + &pdev->physfn.vf_rlen[i],PCI_BAR_VF | ((i ==PCI_SRIOV_NUM_BARS -1 ) ?PCI_BAR_LAST :0 ));
xen/include/xen/pci.h +10 -5
@@ -156,11 +156,16 @@ struct pci_dev {* List entry if info.is_virtfn == true */ struct list_head vf_list; -u64 vf_rlen[6 ]; -struct { -/* Only populated for VFs (info.is_virtfn == true) */ -const struct pci_dev *pf_pdev;/* Link from VF to PF */ - } virtfn; +union { +struct pf_info { +/* Only populated for PFs (info.is_virtfn == false) */ +uint64_t vf_rlen[PCI_SRIOV_NUM_BARS ]; + } physfn; +struct { +/* Only populated for VFs (info.is_virtfn == true) */ +const struct pci_dev *pf_pdev;/* Link from VF to PF */ + } virtfn; + };/* Data for vPCI. */ struct vpci *vpci;
Signature · 1 line
2.47.0
Re: [PATCH v5 3/3] x86/msi: fix locking for SR-IOV devices
Quoted 32 lines · Stewart Hildebrand
In commit
4f78438b45e2("vpci: use per-domain PCI lock to protect vpci structure") a lock was moved fromallocate_and_map_msi_pirq()to the caller and changed frompcidevs_lock()toread_lock(&d->pci_lock). However, one call path wasn't updated to reflect the change, leading to a failed assertion observed under the following conditions:* PV dom0
* Debug build (CONFIG_DEBUG=y) of Xen
* There is an SR-IOV device in the system with one or more VFs enabled
* Dom0 has loaded the driver for the VF and enabled MSI-X(XEN) Assertion 'd ||
pcidevs_locked()' failed atdrivers/passthrough/pci.c:535(XEN) ----[ Xen-4.20-unstablex86_64debug=y Not tainted ]----
...
(XEN) Xen call trace:(XEN) [<ffff82d040284da8>] R pci_get_pdev+0x4c/0xab (XEN) [<ffff82d040344f5c>] F arch/x86/msi.c#read_pci_mem_bar+0x58/0x272 (XEN) [<ffff82d04034530e>] F arch/x86/msi.c#msix_capability_init+0x198/0x755 (XEN) [<ffff82d040345dad>] F arch/x86/msi.c#__pci_enable_msix+0x82/0xe8 (XEN) [<ffff82d0403463e5>] F pci_enable_msi+0x3f/0x78 (XEN) [<ffff82d04034be2b>] F map_domain_pirq+0x2a4/0x6dc (XEN) [<ffff82d04034d4d5>] F allocate_and_map_msi_pirq+0x103/0x262 (XEN) [<ffff82d04035da5d>] F physdev_map_pirq+0x210/0x259 (XEN) [<ffff82d04035e798>] F do_physdev_op+0x9c3/0x1454 (XEN) [<ffff82d040329475>] F pv_hypercall+0x5ac/0x6af (XEN) [<ffff82d0402012d3>] F lstar_enter+0x143/0x150In
read_pci_mem_bar(), the VF obtains the structpci_devpointer for its associated PF to access thevf_rlenarray. This array is initialized inpci_add_device()and is only populated in the associated PF's structpci_dev.
Quoted 6 lines
Access the
vf_rlenarray via the link to the PF, and remove the troublesome call topci_get_pdev().Fixes 4f78438b45e2 ("vpci: use per-domain PCI lock to protect vpci structure")Reported-by Teddy Astie <[email protected]>Signed-off-by Stewart Hildebrand <[email protected]>
xen/drivers/passthrough/pci.c int pci_add_device(u16 seg, u8 bus, u8 devfn, · line 739
Quoted 8 lines
xen/drivers/passthrough/pci.c +1
-1@@ -736,7 +736,7 @@ int pci_add_device(u16 seg, u8 bus, u8 devfn, } } -if ( !pdev->info.is_virtfn && !pdev->vf_rlen[0 ] ) +if ( !pdev->info.is_virtfn && !pdev->physfn.vf_rlen[0 ] )
Unrelated to your change: Now that I look at this again, it seems slightly wrong to me to use array slot 0 as "have we populated the array already" indicator. If BAR0 was unused, we may end up doing this more than once.
Jan