[PATCH v2 1/2] rust: pci: skip probing VFs if driver doesn't support VFs

John Hubbard
2025-10-02T02:00:17Z

Add a "supports_vf" flag to struct pci_driver to let drivers declare
Virtual Function (VF) support. If a driver does not support VFs, then
the PCI driver core will not probe() any VFs for that driver's devices.

On the Rust side, add a const "SUPPORTS_VF" Driver trait, defaulting to
false: drivers must explicitly opt into VF support.

Cc: Alexandre Courbot <acourbot@nvidia.com>
Cc: Alistair Popple <apopple@nvidia.com>
Cc: Joel Fernandes <joelagnelf@nvidia.com>
Cc: Zhi Wang <zhiw@nvidia.com>
Cc: Alex Williamson <alex.williamson@redhat.com>
Cc: Jason Gunthorpe <jgg@nvidia.com>
Suggested-by: Danilo Krummrich <dakr@kernel.org>
Signed-off-by: John Hubbard <jhubbard@nvidia.com>
---
drivers/pci/pci-driver.c | 3 +++
include/linux/pci.h | 1 +
rust/kernel/pci.rs | 4 ++++
3 files changed, 8 insertions(+)

diff --git a/drivers/pci/pci-driver.c b/drivers/pci/pci-driver.c
index 63665240ae87..588666cc7254 100644
--- a/drivers/pci/pci-driver.c
+++ b/drivers/pci/pci-driver.c
@@ -412,6 +412,9 @@ static int __pci_device_probe(struct pci_driver *drv, struct pci_dev *pci_dev)
if (drv->probe) {
error = -ENODEV;

+ if (pci_dev->is_virtfn && !drv->supports_vf)
+ return error;
+
id = pci_match_device(drv, pci_dev);
if (id)
error = pci_call_probe(drv, pci_dev, id);
diff --git a/include/linux/pci.h b/include/linux/pci.h
index 59876de13860..92510886a086 100644
--- a/include/linux/pci.h
+++ b/include/linux/pci.h
@@ -983,6 +983,7 @@ struct pci_driver {
struct device_driver driver;
struct pci_dynids dynids;
bool driver_managed_dma;
+ bool supports_vf; /* Will bind to Virtual Functions */
};

#define to_pci_driver(__drv) \
diff --git a/rust/kernel/pci.rs b/rust/kernel/pci.rs
index 7fcc5f6022c1..c5d036770e65 100644
--- a/rust/kernel/pci.rs
+++ b/rust/kernel/pci.rs
@@ -47,6 +47,7 @@ unsafe fn register(
(*pdrv.get()).probe = Some(Self::probe_callback);
(*pdrv.get()).remove = Some(Self::remove_callback);
(*pdrv.get()).id_table = T::ID_TABLE.as_ptr();
+ (*pdrv.get()).supports_vf = T::SUPPORTS_VF;
}

// SAFETY: `pdrv` is guaranteed to be a valid `RegType`.
@@ -268,6 +269,9 @@ pub trait Driver: Send {
/// The table of device ids supported by the driver.
const ID_TABLE: IdTable<Self::IdInfo>;

+ /// Whether the driver supports Virtual Functions.
+ const SUPPORTS_VF: bool = false;
+
/// PCI driver probe.
///
/// Called when a new pci device is added or discovered. Implementers should
--
2.51.0