Re: [PATCH v2 1/3] rust: add runtime PM support

Alice Ryhl <[email protected]>
Newsgroups dev.linux.lists.driver-core,org.kernel.vger.linux-kernel,org.kernel.vger.linux-pm,org.kernel.vger.rust-for-linux
Message-ID <[email protected]>
On Tue, Jul 21, 2026 at 05:34:02PM +0200, Beata Michalska wrote:
> +/// Runtime PM callbacks implemented by a driver.
> +///
> +/// Defines the [`PMOps`] trait and its corresponding [`bindings::dev_pm_ops`].
> +///
> +/// Each generated C callback recovers the Rust bus device from the raw
> +/// `struct device *`, borrows the registered runtime PM payload, and delegates
> +/// to the matching [`PMOps`] trait method.
> +///
> +/// # Safety
> +///
> +/// `PMContext::<T>::PM_OPS` must only be installed for devices whose concrete
> +/// bus type is `T::DeviceType`, and whose runtime PM registration data was
> +/// installed by [`Registration::new`] for the same `T`.
> +///
> +macro_rules! define_pm_ops {
> +    ($($name:ident $( : $desc:tt )? ),+ $(,)?) => {
> +        $( define_pm_callback!( @parse_desc $name $( $desc )?); )+
> +        define_pm_ops!(@common $( $name ), +);
> +    };
> +
> +    (@common $( $name:ident),+ ) => {
> +        /// Runtime PM callbacks implemented by a driver
> +        #[vtable]
> +        pub trait PMOps: Sized
> +        {
> +            /// Type of a bus device
> +            type DeviceType: AsBusDevice<device::Bound>;
> +            /// Type of the data associated with a PM transitions:
> +            type RuntimePayloadType: Send;
> +
> +            $(
> +                #[allow(missing_docs)]
> +                // Callback-specific docs are provided by the generated `dev_pm_ops`
> +                // contract on `PMOps`.
> +
> +                fn $name<'a>(
> +                    _dev:  &'a Self::DeviceType,
> +                    _payload: Option<Self::RuntimePayloadType>,
> +                ) -> Result<Option<Self::RuntimePayloadType>, (Option<Self::RuntimePayloadType>, Error)> {
> +                    build_error!(VTABLE_DEFAULT_ERROR)
> +                }
> +            )+
> +        }
> +        paste!(
> +            impl<'a, T:PMOps> PMContext<'a, T> {
> +                /// Driver-provided runtime PM operations.
> +                ///
> +                /// A driver implements this trait to handle runtime PM
> +                /// transitions for its device type.
> +                ///
> +                /// Each callback receives the device and the current payload.
> +                /// On success, it returns the payload to keep for the next
> +                /// transition. On failure, it returns the payload together
> +                /// with the error so the previous, or otherwise sane state
> +                /// can be preserved.
> +                pub const PM_OPS: bindings::dev_pm_ops = bindings::dev_pm_ops {
> +                    $( [<$name>]: if T::[<HAS_ $name:upper>] {
> +                        Some([<$name _callback>]::<T>)
> +                    } else {
> +                        None
> +                    }, )+
> +                    ..PMOPS_NONE
> +                };
> +            }
> +        );
> +    }
> +}

> +
> +define_pm_ops!(
> +    // PM state change
> +    runtime_suspend,
> +    runtime_resume,
> +);

I think using a macro to define this trait is overkill. Just write it
out:

pub trait PMOps: Sized {
    /// Type of a bus device
    type DeviceType: AsBusDevice<device::Bound>;
    /// Type of the data associated with a PM transitions:
    type RuntimePayloadType: Send;

    fn runtime_suspend<'a>(
        _dev:  &'a Self::DeviceType,
        _payload: Option<Self::RuntimePayloadType>,
    ) -> Result<Option<Self::RuntimePayloadType>, (Option<Self::RuntimePayloadType>, Error)> {
        build_error!(VTABLE_DEFAULT_ERROR)
    }

    fn runtime_resume<'a>(
        _dev:  &'a Self::DeviceType,
        _payload: Option<Self::RuntimePayloadType>,
    ) -> Result<Option<Self::RuntimePayloadType>, (Option<Self::RuntimePayloadType>, Error)> {
        build_error!(VTABLE_DEFAULT_ERROR)
    }
}

This is a lot easier to read.

Alice
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.