Skip to contentSkip to conversation

PCI: Identifying detached virtual functions

PATCH Superseded Matthew Rosato 2 participants 4 messages Aug 12, 2020

Subsystems (5)
KVM PCI s390 VFIO driver +1 more subsystems

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 PCI: Identifying detached virtual functions

As discussed previously in a qemu-devel thread:

https://www.mail-archive.com/[email protected]/msg725141.html

s390x has the notion of unlinked VFs being available at the LPAR-level (Virtual Functions where the kernel does not have access to the associated Physical Function). These devices are currently not marked as is_virtfn. There seems to be some precedent (ex: in powerpc, eeh_debugfs_break_device()) where pdev->is_virtfn && pdev->physfn == 0 is used to detect these sort of detached VFs. We toyed with the idea of doing this but it causes additional fallout as various other areas of kernel code have an expectation that is_virtfn=1 implies there is a linked PF available to the kernel.

In the s390x case, the firmware layer underneath handles the VF emulation as it still has access to the PF that the LPAR (and thus the kernel) cannot see. But one thing this firmware layer does not do is emulate the PCI_COMMAND_MEMORY bit, which was OK until vfio-pci started enforcing it via abafbc55. The vfio-pci check is waived for VFs as of ebfa440c, but vfio-pci can't actually tell that these particular devices are VFs.

The proposed patch attempts to identify these detached VFs and subsequently provide this information to vfio-pci so that it knows to also accept the lack of PCI_COMMAND_MEMORY for these sorts of devices. For now the bit is only set for s390x but other architectures could opt in to it as well if needed.

Matthew Rosato (1):
PCI: Introduce flag for detached virtual functions

 arch/s390/pci/pci.c                | 8 ++++++++
 drivers/vfio/pci/vfio_pci_config.c | 3 ++-
 include/linux/pci.h                | 1 +
 3 files changed, 11 insertions(+), 1 deletion(-)
Signature · 1 line
1.8.3.1
#1 lore raw

PATCH 1/1 PCI: Introduce flag for detached virtual functions

s390x has the notion of providing VFs to the kernel in a manner where the associated PF is inaccessible other than via firmware. These are not treated as typical VFs and access to them is emulated by underlying firmware which can still access the PF. After abafbc55 however these detached VFs were no longer able to work with vfio-pci as the firmware does not provide emulation of the PCI_COMMAND_MEMORY bit. In this case, let's explicitly recognize these detached VFs so that vfio-pci can allow memory access to them again.

---
 arch/s390/pci/pci.c                | 8 ++++++++
 drivers/vfio/pci/vfio_pci_config.c | 3 ++-
 include/linux/pci.h                | 1 +
 3 files changed, 11 insertions(+), 1 deletion(-)
arch/s390/pci/pci.c +8 -0
index 3902c9f..04ac76d 100644
@@ -581,6 +581,14 @@ int pcibios_enable_device(struct pci_dev *pdev, int mask)
 {
 	struct zpci_dev *zdev = to_zpci(pdev);
 
+	/*
+	 * If we have a VF on a non-multifunction bus, it must be a VF that is
+	 * detached from its parent PF.  We rely on firmware emulation to
+	 * provide underlying PF details.
+	 */
+	if (zdev->vfn && !zdev->zbus->multifunction)
+		pdev->detached_vf = 1;
+
 	zpci_debug_init_device(zdev, dev_name(&pdev->dev));
 	zpci_fmb_enable_device(zdev);
 
drivers/vfio/pci/vfio_pci_config.c +2 -1
index d98843f..17845fc 100644
@@ -406,7 +406,8 @@ bool __vfio_pci_memory_enabled(struct vfio_pci_device *vdev)
 	 * PF SR-IOV capability, there's therefore no need to trigger
 	 * faults based on the virtual value.
 	 */
-	return pdev->is_virtfn || (cmd & PCI_COMMAND_MEMORY);
+	return pdev->is_virtfn || pdev->detached_vf ||
+	       (cmd & PCI_COMMAND_MEMORY);
 }
 
 /*
include/linux/pci.h +1 -0
index 8355306..23a6972 100644
@@ -445,6 +445,7 @@ struct pci_dev {
 	unsigned int	is_probed:1;		/* Device probing in progress */
 	unsigned int	link_active_reporting:1;/* Device capable of reporting link active */
 	unsigned int	no_vf_scan:1;		/* Don't scan for VFs after IOV enablement */
+	unsigned int	detached_vf:1;		/* VF without local PF access */
 	pci_dev_flags_t dev_flags;
 	atomic_t	enable_cnt;	/* pci_enable_device has been called */
 
Signature · 1 line
1.8.3.1
#2 lore raw

Re: [PATCH] PCI: Introduce flag for detached virtual functions

Quoted 43 lines · On Wed, 12 Aug 2020 10:50:17 -0400 Matthew Rosato <[email protected]>…

s390x has the notion of providing VFs to the kernel in a manner where the associated PF is inaccessible other than via firmware. These are not treated as typical VFs and access to them is emulated by underlying firmware which can still access the PF. After abafbc55 however these detached VFs were no longer able to work with vfio-pci as the firmware does not provide emulation of the PCI_COMMAND_MEMORY bit. In this case, let's explicitly recognize these detached VFs so that vfio-pci can allow memory access to them again.

---
 arch/s390/pci/pci.c                | 8 ++++++++
 drivers/vfio/pci/vfio_pci_config.c | 3 ++-
 include/linux/pci.h                | 1 +
 3 files changed, 11 insertions(+), 1 deletion(-)
arch/s390/pci/pci.c +8 -0
index 3902c9f..04ac76d 100644
@@ -581,6 +581,14 @@ int pcibios_enable_device(struct pci_dev *pdev, int mask)
 {
 	struct zpci_dev *zdev = to_zpci(pdev);
 
+	/*
+	 * If we have a VF on a non-multifunction bus, it must be a VF that is
+	 * detached from its parent PF.  We rely on firmware emulation to
+	 * provide underlying PF details.
+	 */
+	if (zdev->vfn && !zdev->zbus->multifunction)
+		pdev->detached_vf = 1;
+
 	zpci_debug_init_device(zdev, dev_name(&pdev->dev));
 	zpci_fmb_enable_device(zdev);
 
drivers/vfio/pci/vfio_pci_config.c +0 -0
index d98843f..17845fc 100644

drivers/vfio/pci/vfio_pci_config.c bool __vfio_pci_memory_enabled(struct vfio_pci_device *vdev) · line 413

Quoted 9 lines
drivers/vfio/pci/vfio_pci_config.c +2 -1
@@ -406,7 +406,8 @@ bool __vfio_pci_memory_enabled(struct vfio_pci_device *vdev)
 	 * PF SR-IOV capability, there's therefore no need to trigger
 	 * faults based on the virtual value.
 	 */
-	return pdev->is_virtfn || (cmd & PCI_COMMAND_MEMORY);
+	return pdev->is_virtfn || pdev->detached_vf ||
+	       (cmd & PCI_COMMAND_MEMORY);
 }
 
 /*

Wouldn't we also want to enable the is_virtfn related code in vfio_basic_config_read() and at least the initial setting of the command register in vfio_config_init()? Otherwise we're extending the incomplete emulation out to userspace. Thanks,

Alex

Quoted 12 lines
include/linux/pci.h +1 -0
index 8355306..23a6972 100644
@@ -445,6 +445,7 @@ struct pci_dev {
 	unsigned int	is_probed:1;		/* Device probing in progress */
 	unsigned int	link_active_reporting:1;/* Device capable of reporting link active */
 	unsigned int	no_vf_scan:1;		/* Don't scan for VFs after IOV enablement */
+	unsigned int	detached_vf:1;		/* VF without local PF access */
 	pci_dev_flags_t dev_flags;
 	atomic_t	enable_cnt;	/* pci_enable_device has been called */
 
#3 lore raw

Re: [PATCH] PCI: Introduce flag for detached virtual functions

Quoted 54 lines · Alex Williamson
Quoted 51 lines · On Wed, 12 Aug 2020 10:50:17 -0400 Matthew Rosato <[email protected]>…

s390x has the notion of providing VFs to the kernel in a manner where the associated PF is inaccessible other than via firmware. These are not treated as typical VFs and access to them is emulated by underlying firmware which can still access the PF. After abafbc55 however these detached VFs were no longer able to work with vfio-pci as the firmware does not provide emulation of the PCI_COMMAND_MEMORY bit. In this case, let's explicitly recognize these detached VFs so that vfio-pci can allow memory access to them again.

---
  arch/s390/pci/pci.c                | 8 ++++++++
  drivers/vfio/pci/vfio_pci_config.c | 3 ++-
  include/linux/pci.h                | 1 +
  3 files changed, 11 insertions(+), 1 deletion(-)
arch/s390/pci/pci.c +8 -0
index 3902c9f..04ac76d 100644
@@ -581,6 +581,14 @@ int pcibios_enable_device(struct pci_dev *pdev, int mask)
  {
  	struct zpci_dev *zdev = to_zpci(pdev);
 
+	/*
+	 * If we have a VF on a non-multifunction bus, it must be a VF that is
+	 * detached from its parent PF.  We rely on firmware emulation to
+	 * provide underlying PF details.
+	 */
+	if (zdev->vfn && !zdev->zbus->multifunction)
+		pdev->detached_vf = 1;
+
  	zpci_debug_init_device(zdev, dev_name(&pdev->dev));
  	zpci_fmb_enable_device(zdev);
 
drivers/vfio/pci/vfio_pci_config.c +2 -1
index d98843f..17845fc 100644
@@ -406,7 +406,8 @@ bool __vfio_pci_memory_enabled(struct vfio_pci_device *vdev)
  	 * PF SR-IOV capability, there's therefore no need to trigger
  	 * faults based on the virtual value.
  	 */
-	return pdev->is_virtfn || (cmd & PCI_COMMAND_MEMORY);
+	return pdev->is_virtfn || pdev->detached_vf ||
+	       (cmd & PCI_COMMAND_MEMORY);
  }
 
Quoted 7 lines

drivers/vfio/pci/vfio_pci_config.c bool __vfio_pci_memory_enabled(struct vfio_pci_device *vdev) · line 413

Quoted 1 line
drivers/vfio/pci/vfio_pci_config.c +0 -0
  /*

Wouldn't we also want to enable the is_virtfn related code in vfio_basic_config_read() and at least the initial setting of the command register in vfio_config_init()? Otherwise we're extending the incomplete emulation out to userspace. Thanks,

We had discussed doing this internally and I ultimately left it out of this patch to keep the changes small... But I agree that it makes sense to fix the emulation for userspace. I can add the changes you suggest + would also need to change vfio_restore_bar() with:

- if (pdev->is_virtfn)
+ if (pdev->is_virtfn || pdev->detached_vf)

                return;

to prevent the config changes from tripping the restore code.

I'll send a V2 with this included.