Add a flag managed_sriov to the pci_driver structure that allows a
driver to opt into disabling the Single Root I/O Virtualization (SR-IOV)
capability of the device when the driver is unbound.

Add a new function pci_iov_disable() that is invoked before the remove()
callback of a PCI driver and checks for the presence of the new flag.
If the flag is set, invoke the sriov_configure() callback to allow the
driver to gracefully disable SR-IOV. Warn if the driver fails to do so
and forcibly disable SR-IOV using sriov_disable().

Since a (broken) driver may theoretically re-enable SR-IOV during its
remove() callback, extend pci_iov_remove() to forcibly disable SR-IOV
after remove() if needed and only if the flag managed_sriov is set.

Altogether the flag ensures that when a Virtual Function (VF) is bound
to a driver, the corresponding Physical Function (PF) is bound to a
driver, too, since the VF devices are destroyed when the PF driver is
unbound. This guarantee is a prerequisite for exposing a safe Rust
API that allows a VF driver to obtain the PF device for a VF device
and subsequently access the device private data of the PF device.

Suggested-by: Danilo Krummrich <[email protected]>
Signed-off-by: Peter Colberg <[email protected]>
---
Changes in v2:
- Move logic to disable SR-IOV on remove() from Rust to C.
- Add driver flag managed_sriov to opt into disabling SR-IOV on remove().
---
 drivers/pci/iov.c        | 41 ++++++++++++++++++++++++++++++++++++++++-
 drivers/pci/pci-driver.c |  3 ++-
 drivers/pci/pci.h        |  2 ++
 include/linux/pci.h      |  8 ++++++++
 4 files changed, 52 insertions(+), 2 deletions(-)

diff --git a/drivers/pci/iov.c b/drivers/pci/iov.c
index 
91ac4e37ecb9c0c5265aa40c235e84b430f43a96..da64d6ce5d30de8a52089b36fcb013937cf8b6fe
 100644
--- a/drivers/pci/iov.c
+++ b/drivers/pci/iov.c
@@ -1010,20 +1010,59 @@ void pci_iov_release(struct pci_dev *dev)
                sriov_release(dev);
 }
 
+/**
+ * pci_iov_disable - disable SR-IOV before PF driver is detached
+ * @dev: the PCI device
+ *
+ * Invoke sriov_configure() callback to allow the driver to gracefully disable
+ * SR-IOV. Warn if the driver fails to do so and forcibly disable SR-IOV.
+ */
+void pci_iov_disable(struct pci_dev *dev)
+{
+       struct pci_driver *drv = dev->driver;
+       struct pci_sriov *iov = dev->sriov;
+
+       if (WARN_ON(!drv))
+               return;
+
+       if (!dev->is_physfn || !iov->num_VFs || !drv->managed_sriov)
+               return;
+
+       if (!drv->sriov_configure) {
+               sriov_disable(dev);
+               return;
+       }
+
+       drv->sriov_configure(dev, 0);
+
+       if (WARN_ON(iov->num_VFs))
+               sriov_disable(dev);
+}
+
 /**
  * pci_iov_remove - clean up SR-IOV state after PF driver is detached
  * @dev: the PCI device
  */
 void pci_iov_remove(struct pci_dev *dev)
 {
+       struct pci_driver *drv = dev->driver;
        struct pci_sriov *iov = dev->sriov;
 
+       if (WARN_ON(!drv))
+               return;
+
        if (!dev->is_physfn)
                return;
 
        iov->driver_max_VFs = iov->total_VFs;
-       if (iov->num_VFs)
+
+       if (iov->num_VFs && !drv->managed_sriov) {
                pci_warn(dev, "driver left SR-IOV enabled after remove\n");
+               return;
+       }
+
+       if (WARN_ON(iov->num_VFs))
+               sriov_disable(dev);
 }
 
 /**
diff --git a/drivers/pci/pci-driver.c b/drivers/pci/pci-driver.c
index 
dd9075403987d84e068014b35745e8872e93fdae..3fe43711565a3eb61a06cc3700e5ca953961fbe9
 100644
--- a/drivers/pci/pci-driver.c
+++ b/drivers/pci/pci-driver.c
@@ -491,6 +491,7 @@ static void pci_device_remove(struct device *dev)
        struct pci_dev *pci_dev = to_pci_dev(dev);
        struct pci_driver *drv = pci_dev->driver;
 
+       pci_iov_disable(pci_dev);
        if (drv->remove) {
                pm_runtime_get_sync(dev);
                /*
@@ -504,8 +505,8 @@ static void pci_device_remove(struct device *dev)
                pm_runtime_put_noidle(dev);
        }
        pcibios_free_irq(pci_dev);
-       pci_dev->driver = NULL;
        pci_iov_remove(pci_dev);
+       pci_dev->driver = NULL;
 
        /* Undo the runtime PM settings in local_pci_probe() */
        pm_runtime_put_sync(dev);
diff --git a/drivers/pci/pci.h b/drivers/pci/pci.h
index 
13d998fbacce6698514d92500dfea03cc562cdc2..66308f5126ff9e4bebb537a541f1dd8717bccbfa
 100644
--- a/drivers/pci/pci.h
+++ b/drivers/pci/pci.h
@@ -943,6 +943,7 @@ static inline void pci_restore_pasid_state(struct pci_dev 
*pdev) { }
 #ifdef CONFIG_PCI_IOV
 int pci_iov_init(struct pci_dev *dev);
 void pci_iov_release(struct pci_dev *dev);
+void pci_iov_disable(struct pci_dev *dev);
 void pci_iov_remove(struct pci_dev *dev);
 void pci_iov_update_resource(struct pci_dev *dev, int resno);
 resource_size_t pci_sriov_resource_alignment(struct pci_dev *dev, int resno);
@@ -977,6 +978,7 @@ static inline int pci_iov_init(struct pci_dev *dev)
        return -ENODEV;
 }
 static inline void pci_iov_release(struct pci_dev *dev) { }
+static inline void pci_iov_disable(struct pci_dev *dev) { }
 static inline void pci_iov_remove(struct pci_dev *dev) { }
 static inline void pci_iov_update_resource(struct pci_dev *dev, int resno) { }
 static inline resource_size_t pci_sriov_resource_alignment(struct pci_dev *dev,
diff --git a/include/linux/pci.h b/include/linux/pci.h
index 
1c270f1d512301de4d462fe7e5097c32af5c6f8d..859f767b30f726bd157a6080f5977c17c4827a1d
 100644
--- a/include/linux/pci.h
+++ b/include/linux/pci.h
@@ -1015,6 +1015,13 @@ struct module;
  *             how to manage the DMA themselves and set this flag so that
  *             the IOMMU layer will allow them to setup and manage their
  *             own I/O address space.
+ * @managed_sriov: Disable SR-IOV on remove().
+ *             If set, the Single Root I/O Virtualization (SR-IOV)
+ *             capability of the device is disabled when the driver is
+ *             unbound from the device, by calling sriov_configure()
+ *             before remove(). The presence of this flag guarantees
+ *             that when a Virtual Function (VF) is bound to a driver,
+ *             the Physical Function (PF) is bound to a driver, too.
  */
 struct pci_driver {
        const char              *name;
@@ -1033,6 +1040,7 @@ struct pci_driver {
        struct device_driver    driver;
        struct pci_dynids       dynids;
        bool driver_managed_dma;
+       bool managed_sriov;
 };
 
 #define to_pci_driver(__drv)   \

-- 
2.53.0

Reply via email to