[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-Id: <20251119-rust-pci-sriov-v1-7-883a94599a97@redhat.com>
Date: Wed, 19 Nov 2025 17:19:11 -0500
From: Peter Colberg <pcolberg@...hat.com>
To: Danilo Krummrich <dakr@...nel.org>, Bjorn Helgaas <bhelgaas@...gle.com>,
Krzysztof Wilczyński <kwilczynski@...nel.org>,
Miguel Ojeda <ojeda@...nel.org>, Alex Gaynor <alex.gaynor@...il.com>,
Boqun Feng <boqun.feng@...il.com>, Gary Guo <gary@...yguo.net>,
Björn Roy Baron <bjorn3_gh@...tonmail.com>,
Benno Lossin <lossin@...nel.org>, Andreas Hindborg <a.hindborg@...nel.org>,
Alice Ryhl <aliceryhl@...gle.com>, Trevor Gross <tmgross@...ch.edu>,
Abdiel Janulgue <abdiel.janulgue@...il.com>,
Daniel Almeida <daniel.almeida@...labora.com>,
Robin Murphy <robin.murphy@....com>,
Greg Kroah-Hartman <gregkh@...uxfoundation.org>,
Dave Ertman <david.m.ertman@...el.com>, Ira Weiny <ira.weiny@...el.com>,
Leon Romanovsky <leon@...nel.org>
Cc: linux-pci@...r.kernel.org, rust-for-linux@...r.kernel.org,
linux-kernel@...r.kernel.org, Alexandre Courbot <acourbot@...dia.com>,
Alistair Popple <apopple@...dia.com>,
Joel Fernandes <joelagnelf@...dia.com>, John Hubbard <jhubbard@...dia.com>,
Zhi Wang <zhiw@...dia.com>, Peter Colberg <pcolberg@...hat.com>,
Jason Gunthorpe <jgg@...pe.ca>
Subject: [PATCH 7/8] rust: pci: add physfn(), to return PF device for VF
device
Add a method to return the Physical Function (PF) device for a Virtual
Function (VF) device in the bound device context.
Unlike for a PCI driver written in C, guarantee that when a VF device is
bound to a driver, the underlying PF device is bound to a driver, too.
When a device with enabled VFs is unbound from a driver, invoke the
sriov_configure() callback to disable SR-IOV before the unbind()
callback. To ensure the guarantee is upheld, call disable_sriov()
to remove all VF devices if the driver has not done so already.
Suggested-by: Danilo Krummrich <dakr@...nel.org>
Signed-off-by: Peter Colberg <pcolberg@...hat.com>
---
rust/kernel/pci.rs | 47 +++++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 47 insertions(+)
diff --git a/rust/kernel/pci.rs b/rust/kernel/pci.rs
index f9054c52a3bdff2c42273366a4058d943ee5fd76..d6cc5d7e7cd7a3b6e451c0ff5aea044b89c6774a 100644
--- a/rust/kernel/pci.rs
+++ b/rust/kernel/pci.rs
@@ -120,6 +120,19 @@ extern "C" fn remove_callback(pdev: *mut bindings::pci_dev) {
// and stored a `Pin<KBox<T>>`.
let data = unsafe { pdev.as_ref().drvdata_obtain::<T>() };
+ // Always disable SR-IOV before `unbind()` to guarantee that when a VF device is bound
+ // to a driver, the underlying PF device is bound to a driver, too.
+ #[cfg(CONFIG_PCI_IOV)]
+ {
+ // First, allow the driver to gracefully disable SR-IOV by itself.
+ if T::HAS_SRIOV_CONFIGURE && pdev.num_vf() != 0 {
+ let _ = T::sriov_configure(pdev, 0);
+ }
+ // Then, forcibly disable SR-IOV if the driver has not done so already,
+ // to uphold the guarantee with regard to driver binding described above.
+ pdev.disable_sriov();
+ }
+
T::unbind(pdev, data.as_ref());
}
@@ -332,6 +345,11 @@ fn unbind(dev: &Device<device::Core>, this: Pin<&Self>) {
/// [`Device`] by writing the number of Virtual Functions (VF), `nr_virtfn` or zero to the
/// sysfs file `sriov_numvfs` for this device. Implementing this callback is optional.
///
+ /// Further, and unlike for a PCI driver written in C, when a PF device with enabled VFs is
+ /// unbound from its bound [`Driver`], the `sriov_configure()` callback is invoked to disable
+ /// SR-IOV before the `unbind()` callback. This guarantees that when a VF device is bound to a
+ /// driver, the underlying PF device is bound to a driver, too.
+ ///
/// Upon success, this callback must return the number of VFs that were enabled, or zero if
/// SR-IOV was disabled.
///
@@ -500,6 +518,35 @@ pub fn pci_class(&self) -> Class {
}
}
+impl Device<device::Bound> {
+ /// Returns the Physical Function (PF) device for a Virtual Function (VF) device.
+ ///
+ /// # Examples
+ ///
+ /// The following example illustrates how to obtain the private driver data of the PF device,
+ /// where `vf_pdev` is the VF device of reference type `&Device<Core>` or `&Device<Bound>`.
+ ///
+ /// ```
+ /// let pf_pdev = vf_pdev.physfn()?;
+ /// let pf_drvdata = pf_pdev.as_ref().drvdata::<MyDriver>()?;
+ /// ```
+ #[cfg(CONFIG_PCI_IOV)]
+ pub fn physfn(&self) -> Result<&Device<device::Bound>> {
+ if !self.is_virtfn() {
+ return Err(EINVAL);
+ }
+ // SAFETY:
+ // `self.as_raw` returns a valid pointer to a `struct pci_dev`.
+ //
+ // `physfn` is a valid pointer to a `struct pci_dev` since self.is_virtfn() is `true`.
+ //
+ // `physfn` may be cast to a `Device<device::Bound>` since `pci::Driver::remove()` calls
+ // `disable_sriov()` to remove all VF devices, which guarantees that the underlying
+ // PF device is always bound to a driver when the VF device is bound to a driver.
+ Ok(unsafe { &*(*self.as_raw()).__bindgen_anon_1.physfn.cast() })
+ }
+}
+
impl Device<device::Core> {
/// Enable memory resources for this device.
pub fn enable_device_mem(&self) -> Result {
--
2.51.1
Powered by blists - more mailing lists