Re: [PATCH v5 7/9] rust: pci: add SR-IOV enable and disable tokens

From: Alexandre Courbot

Date: Mon Nov 09 2026 - 06:56:23 EST


On Sat Oct 31, 2026 at 2:12 AM JST, Simon Song wrote:
> From: Zhi Wang <zhiw@xxxxxxxxxx>
>
> 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 next. 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 <dakr@xxxxxxxxxx>
> Signed-off-by: Zhi Wang <zhiw@xxxxxxxxxx>
> Signed-off-by: Simon Song <xinmengs@xxxxxxxxxx>

Reviewed-by: Alexandre Courbot <acourbot@xxxxxxxxxx>

(one nit comment below)

<...>
> +/// 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<device::CoreInternal<'a>>,
> + num_vfs: u16,
> +}
> +
> +impl<'a> SriovEnable<'a> {
> + /// Returns the number of VFs requested for this callback.
> + #[inline]
> + pub fn num_vfs(&self) -> u16 {
> + 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.
> + pub fn enable(self, num_vfs: u16) -> Result<SriovEnabled<'a>> {

Do we want to return the token back in case of error, so the caller may
try again?

Also it would be nice if the returned errors could be documented, as
that would also document the conditions required for this method to run
(i.e. a VF registration must be active).