From: Zhi Wang Enabling VFs can succeed before the driver's remaining setup fails. Use a callback-scoped token to return an enabled guard that disables SR-IOV when dropped, rolling back those VFs on a later error. Add SriovEnable, SriovEnabled and SriovDisable for the split callbacks introduced later in this series. Tie each token to its PF and callback lifetime, and limit the enabled count to the user's request. The PCI adapter disarms the guard when accepting a successful enable callback. Require a VfRegistration to own final VF teardown after that handoff. Drivers that do not share data with VFs can register (). A disable token permits explicit shutdown without forcing it on a rejected request. Suggested-by: Danilo Krummrich Signed-off-by: Zhi Wang Reviewed-by: Alexandre Courbot Signed-off-by: Simon Song --- rust/kernel/pci.rs | 7 ++- rust/kernel/pci/iov.rs | 102 +++++++++++++++++++++++++++++++++++++++-- 2 files changed, 105 insertions(+), 4 deletions(-) diff --git a/rust/kernel/pci.rs b/rust/kernel/pci.rs index 033c49363d1e..077fa80e7425 100644 --- a/rust/kernel/pci.rs +++ b/rust/kernel/pci.rs @@ -59,7 +59,12 @@ Extended, Normal, // }; -pub use self::iov::VfRegistration; +pub use self::iov::{ + SriovDisable, + SriovEnable, + SriovEnabled, + VfRegistration, // +}; pub use self::irq::{ IrqType, IrqTypes, diff --git a/rust/kernel/pci/iov.rs b/rust/kernel/pci/iov.rs index 3f4414aa7d36..bda2bffffcda 100644 --- a/rust/kernel/pci/iov.rs +++ b/rust/kernel/pci/iov.rs @@ -16,7 +16,9 @@ }; use core::{ any::TypeId, - marker::PhantomPinned, // + marker::PhantomPinned, + mem::ManuallyDrop, + num::NonZero, // }; /// Returns the PF's registration pointer, or null when `CONFIG_PCI_IOV` is disabled. @@ -71,7 +73,6 @@ impl Device> { /// Enable the Single Root I/O Virtualization (SR-IOV) capability for this device, /// where `nr_virtfn` is number of Virtual Functions (VF) to enable. #[inline] - #[expect(dead_code)] pub(crate) fn enable_sriov(&self, nr_virtfn: u16) -> Result { // SAFETY: // - `self.as_raw` returns a valid pointer to a `struct pci_dev`. @@ -88,7 +89,6 @@ pub(crate) fn enable_sriov(&self, nr_virtfn: u16) -> Result { /// Disable the Single Root I/O Virtualization (SR-IOV) capability for this device. #[inline] - #[expect(dead_code)] pub(crate) fn disable_sriov(&self) { // SAFETY: // - `self.as_raw` returns a valid pointer to a `struct pci_dev`. @@ -103,6 +103,102 @@ pub(crate) fn disable_sriov(&self) { } } +/// Permission to enable VFs during a driver's `sriov_enable()` callback. +/// +/// The PCI adapter creates this token for the PF being configured. It cannot be cloned or sent +/// to another thread, and its lifetime is restricted to the callback. Enabling VFs consumes it. +/// +/// The callback lifetime cannot be extended: +/// +/// ```ignore,compile_fail +/// use kernel::pci::SriovEnable; +/// +/// fn escape(token: SriovEnable<'_>) -> SriovEnable<'static> { +/// token +/// } +/// ``` +pub struct SriovEnable<'a> { + pdev: &'a Device>, + num_vfs: NonZero, +} + +impl<'a> SriovEnable<'a> { + /// Returns the number of VFs requested for this callback. + #[inline] + pub fn num_vfs(&self) -> NonZero { + self.num_vfs + } + + /// Enables between one and the requested number of VFs. + /// + /// VF drivers can probe before this method returns, so the PF resources they access must + /// already be initialized. The returned guard disables the VFs if subsequent setup fails. + /// Return it from `sriov_enable()` to leave the VFs enabled on callback success. + /// + /// # Errors + /// + /// - [`EINVAL`]: `num_vfs` is greater than [`Self::num_vfs()`]. + /// - [`ENODEV`]: the PF driver has not registered a [`VfRegistration`]. + /// - Any error returned by `pci_enable_sriov()`. + pub fn enable(self, num_vfs: NonZero) -> Result> { + if num_vfs > self.num_vfs { + return Err(EINVAL); + } + let num_vfs = num_vfs.get(); + + // SAFETY: The PF's driver data and registration remain installed throughout this callback. + // The registration owns final VF teardown after the enable guard is disarmed. + if unsafe { vf_registration_data_ptr(self.pdev.as_raw()) }.is_null() { + return Err(ENODEV); + } + + self.pdev.enable_sriov(num_vfs)?; + Ok(SriovEnabled { + pdev: self.pdev, + num_vfs, + }) + } +} + +/// Enabled VFs awaiting successful completion of `sriov_enable()`. +pub struct SriovEnabled<'a> { + pdev: &'a Device>, + num_vfs: u16, +} + +impl SriovEnabled<'_> { + #[inline] + #[expect(dead_code)] + fn disarm(self) -> u16 { + ManuallyDrop::new(self).num_vfs + } +} + +impl Drop for SriovEnabled<'_> { + fn drop(&mut self) { + self.pdev.disable_sriov(); + } +} + +/// Permission to disable VFs during a driver's `sriov_disable()` callback. +/// +/// The PCI adapter creates this token for the PF being configured. It cannot be cloned or sent +/// to another thread, and its lifetime is restricted to the callback. Dropping the token does +/// not disable VFs, allowing the callback to reject a disable request. +pub struct SriovDisable<'a> { + pdev: &'a Device>, +} + +impl SriovDisable<'_> { + /// Disables all VFs and waits for their drivers to unbind. + /// + /// The PF resources used by VF drivers must remain available until this method returns. + #[inline] + pub fn disable(self) { + self.pdev.disable_sriov(); + } +} + /// Wrapper for VF registration data stored inside a [`VfRegistration`]. /// /// Stores a [`TypeId`] header (derived from `F`) followed by the pinned data, -- 2.43.7