Re: [PATCH v3 09/10] rust: pci: add SR-IOV enable and disable callbacks

From: Danilo Krummrich

Date: Wed Sep 30 2026 - 14:31:17 EST


On Wed Sep 30, 2026 at 12:18 PM CEST, Zhi Wang wrote:
> + #[cfg(CONFIG_PCI_IOV)]
> + fn sriov_enable<'bound, 'callback>(

I'd just call it 'a.

> + dev: &'bound Device<device::Core<'_>>,

This is the same lifetime, as the token stores Device<Core<'_>>, so I'd also
make it 'a.

> + this: Pin<&Self::Data<'bound>>,

Let's call this 'data', unbind() has 'this' for historical reasons.

> + token: SriovEnable<'callback>,
> + ) -> Result<SriovEnabled<'callback>> {
> + let _ = (dev, this, token);
> + build_error!(crate::error::VTABLE_DEFAULT_ERROR)
> + }

<snip>

> + #[cfg(CONFIG_PCI_IOV)]
> + fn sriov_disable<'bound>(
> + dev: &'bound Device<device::Core<'_>>,
> + this: Pin<&Self::Data<'bound>>,
> + token: SriovDisable<'_>,
> + ) -> Result {
> + let _ = (dev, this, token);
> + build_error!(crate::error::VTABLE_DEFAULT_ERROR)
> + }
> }

<snip>

> +impl<T: Driver> Adapter<T> {
> + pub(super) extern "C" fn sriov_configure_callback(
> + pdev: *mut bindings::pci_dev,
> + nr_virtfn: c_int,
> + ) -> c_int {
> + // SAFETY: The PCI bus only ever calls the sriov_configure callback with a valid pointer to
> + // a `struct pci_dev`.
> + //
> + // INVARIANT: `pdev` is valid for the duration of `sriov_configure_callback()`.
> + let pdev = unsafe { &*pdev.cast::<Device<device::CoreInternal<'_>>>() };
> +
> + // SAFETY: `sriov_configure` is called only after a successful probe and before unbind, so
> + // the stored pointer has type `T::Data<'_>` and remains valid throughout this callback.
> + let data = unsafe { pdev.as_ref().drvdata_borrow::<T::Data<'_>>() };
> +
> + from_result(|| {
> + if !pdev.is_physfn() {
> + return Err(ENODEV);
> + }

I don't think this can ever happen.

> + if nr_virtfn == 0 {
> + T::sriov_disable(pdev, data, SriovDisable { pdev })?;
> + if pdev.num_vf() != 0 {
> + return Err(EBUSY);
> + }
> + Ok(0)
> + } else {
> + let num_vfs = u16::try_from(nr_virtfn).map_err(|_| EINVAL)?;
> + let enabled = T::sriov_enable(
> + pdev,
> + data,
> + SriovEnable {
> + pdev,
> + num_vfs: u32::from(num_vfs),
> + },
> + )?;
> + Ok(enabled.disarm())
> + }
> + })
> + }
> +}