Re: [RFC PATCH 1/3] rust: Add runtime PM support

From: Onur Özkan

Date: Sun May 17 2026 - 03:24:54 EST


Hi Beata,

Thank you for working on this. This will eventually be needed for the
tyr GPU reset implementation.

On Thu, 14 May 2026 17:09:03 +0200
Beata Michalska <beata.michalska@xxxxxxx> wrote:

> Introduce a Rust abstraction for Linux runtime PM, centered around a
> `PMContext` ownign a device reference and modeling the runtime PM
> lifecycle. The context is initialised inactive and transitions into
> active once the runtime PM gets enabled. The drivers might opt in to
> initialize the devices power state prior to enabling the context, which
> is advised at probe, when the actual PM callbacks cannot be fully served.
> Once active, the context provides access to runtime PM operations and
> request modes used to manage device power state transitions.
>
> Runtime PM callbacks are provided via a PMOps trait. The generated
> dev_pm_ops callbacks retrieve the driver's PMContext from drvdata,
> validate the expected transition, and transfer ownership of the
> driver-defined runtime PM payload to the relevant callback. Once
> the callback completes, ownership of the returned payload is
> transferred back to the PMContext and stored for the next transition.
> The payload returned by the callback is expected to reflect the
> current runtime PM state of the device. On a successful transition,
> the returned payload should represent the new state reached by
> the device. If the transition fails, it is the responsibility of
> the driver to return a payload either rolled back to the previous
> state or left in a state that remains valid for subsequent runtime PM
> transitions. At present, only runtime_suspend and runtime_resume
> callbacks are supported.
>
> Profile and scoped runtime PM helpers are provided for common usage
> patterns. ResumeScope resumes the device for the lifetime of the scope,
> AwakeScope resumes the device while holding a runtime PM usage reference,
> and RetainScope only increments the runtime PM usage count. References
> are released on drop, and request behaviour is controlled through
> driver-defined Profile values covering the common runtime PM request
> modes.
>
> Additionally, PMConfig provides common runtime PM configuration options.
>
> Signed-off-by: Beata Michalska <beata.michalska@xxxxxxx>
> ---
> rust/bindings/bindings_helper.h | 1 +
> rust/helpers/helpers.c | 1 +
> rust/helpers/pm_runtime.c | 38 ++
> rust/kernel/lib.rs | 1 +
> rust/kernel/pm.rs | 924 ++++++++++++++++++++++++++++++++
> 5 files changed, 965 insertions(+)
> create mode 100644 rust/helpers/pm_runtime.c
> create mode 100644 rust/kernel/pm.rs
>
> diff --git a/rust/bindings/bindings_helper.h b/rust/bindings/bindings_helper.h
> index 446dbeaf0866..3a38db22681f 100644
> --- a/rust/bindings/bindings_helper.h
> +++ b/rust/bindings/bindings_helper.h
> @@ -76,6 +76,7 @@
> #include <linux/pid_namespace.h>
> #include <linux/platform_device.h>
> #include <linux/pm_opp.h>
> +#include <linux/pm_runtime.h>
> #include <linux/poll.h>
> #include <linux/property.h>
> #include <linux/pwm.h>
> diff --git a/rust/helpers/helpers.c b/rust/helpers/helpers.c
> index 625921e27dfb..d67c69ebd33e 100644
> --- a/rust/helpers/helpers.c
> +++ b/rust/helpers/helpers.c
> @@ -75,6 +75,7 @@
> #include "pci.c"
> #include "pid_namespace.c"
> #include "platform.c"
> +#include "pm_runtime.c"
> #include "poll.c"
> #include "processor.c"
> #include "property.c"
> diff --git a/rust/helpers/pm_runtime.c b/rust/helpers/pm_runtime.c
> new file mode 100644
> index 000000000000..9b2056110199
> --- /dev/null
> +++ b/rust/helpers/pm_runtime.c
> @@ -0,0 +1,38 @@
> +// SPDX-License-Identifier: GPL-2.0
> +
> +#include <linux/pm_runtime.h>
> +
> +void rust_helper_pm_runtime_get_noresume(struct device *dev)
> +{
> + pm_runtime_get_noresume(dev);
> +}
> +
> +void rust_helper_pm_runtime_put_noidle(struct device *dev)
> +{
> + pm_runtime_put_noidle(dev);
> +}
> +
> +void rust_helper_pm_runtime_mark_last_busy(struct device *dev)
> +{
> + pm_runtime_mark_last_busy(dev) ;
> +}
> +
> +bool rust_helper_pm_runtime_active(struct device *dev)
> +{
> + return pm_runtime_active(dev);
> +}
> +
> +void rust_helper_pm_suspend_ignore_children(struct device *dev, bool enable)
> +{
> + pm_suspend_ignore_children(dev, enable);
> +}
> +
> +int rust_helper_pm_runtime_set_active(struct device *dev)
> +{
> + return pm_runtime_set_active(dev);
> +}
> +
> +int rust_helper_pm_runtime_set_suspended(struct device *dev)
> +{
> + return pm_runtime_set_suspended(dev);
> +}

You should add __rust_helper prefixes to helper functions.

> diff --git a/rust/kernel/lib.rs b/rust/kernel/lib.rs
> index b72b2fbe046d..7e51e497e2f0 100644
> --- a/rust/kernel/lib.rs
> +++ b/rust/kernel/lib.rs
> @@ -105,6 +105,7 @@
> pub mod pci;
> pub mod pid_namespace;
> pub mod platform;
> +pub mod pm;
> pub mod prelude;
> pub mod print;
> pub mod processor;
> diff --git a/rust/kernel/pm.rs b/rust/kernel/pm.rs
> new file mode 100644
> index 000000000000..7bdffa727c71
> --- /dev/null
> +++ b/rust/kernel/pm.rs
> @@ -0,0 +1,924 @@
> +// SPDX-License-Identifier: GPL-2.0
> +
> +//! Rust Runtime Power Management abstraction.
> +//!
> +//! C header: [`include/linux/pm_runtime.h`](srctree/include/linux/pm_runtime.h)
> +
> +use crate::{
> + bindings, device,
> + error::{to_result, VTABLE_DEFAULT_ERROR},
> + macros::paste,
> + prelude::*,
> + sync::aref::ARef,
> + sync::atomic::{
> + ordering::{Acquire, Full, Release},
> + Atomic,
> + },
> + sync::Arc,
> + sync::{new_mutex, Mutex},
> +};

We started using vertical format on imports like:

use crate::{
bindings,
device,
error::{
to_result,
VTABLE_DEFAULT_ERROR,
//
},
...

to avoid conflict issues.

> +
> +use core::marker::PhantomData;
> +
> +/// Runtime Power Management modes that determine how a particular PM
> +/// transition is to be carried out.
> +/// Corresponds to C Runtime PM flag argument bits:
> +/// - `RPM_ASYNC`
> +/// - `RPM_NOWAIT`
> +/// - `RPM_GET_PUT`
> +/// - `RPM_AUTO`
> +#[derive(Clone, Copy, PartialEq, Eq, Debug)]
> +struct Mode(u32);
> +
> +impl Mode {
> + /// Synchronous PM operations - default.
> + #[allow(unused)]
> + const SYNC: Mode = Mode(0);

This seems to be used in Profile::new, why do we need `#[allow(unused)]` here?

> + /// Allow asynchronous PM operations.
> + const ASYNC: Mode = Mode(bindings::RPM_ASYNC);
> + /// Do not wait for any pending rquests to finish.
> + const NOWAIT: Mode = Mode(bindings::RPM_NOWAIT);
> + /// Acquire a runtime-PM usage reference.
> + const ACQUIRE: Mode = Mode(bindings::RPM_GET_PUT);
> + /// Use autosuspend.
> + const AUTO: Mode = Mode(bindings::RPM_AUTO);
> + /// Additional mode for devices supporting idle states.
> + const IDLE: Mode = Mode(1 << 16);
> +
> + const fn join(self, other: Self) -> Self {
> + Self(self.0 | other.0)
> + }
> +
> + fn contains(self, other: Self) -> bool {
> + (self.0 & other.0) != 0
> + }
> +}
> +
> +impl core::ops::BitOr for Mode {
> + type Output = Self;
> + fn bitor(self, rhs: Self) -> Self::Output {
> + Self(self.0 | rhs.0)
> + }
> +}
> +
> +impl core::ops::BitAnd for Mode {
> + type Output = Self;
> + fn bitand(self, rhs: Self) -> Self::Output {
> + Self(self.0 & rhs.0)
> + }
> +}
> +
> +impl core::ops::Not for Mode {
> + type Output = Self;
> + fn not(self) -> Self::Output {
> + Self(!self.0)
> + }
> +}
> +
> +impl From<Mode> for core::ffi::c_int {
> + #[inline]
> + fn from(mode: Mode) -> core::ffi::c_int {
> + mode.0 as core::ffi::c_int
> + }
> +}
> +
> +/// Utility macro for combining multiple request modes
> +macro_rules! mode {
> + ($mode:expr $(, $args:expr)+ $(,)?) => {{
> + let mut new_mode = $mode;
> + $( new_mode = new_mode.join($args); )+
> + new_mode
> + }};
> +}
> +
> +/// Runtime power transition scope.
> +pub struct Scope<Tag> {
> + dev: ARef<device::Device>,
> + mode: Mode,
> + _tag: PhantomData<Tag>,
> +}
> +
> +/// Device resumed without incrementing the device's usage count
> +pub struct Resume;
> +/// Device resumed with the devices'a usage count being incremented
> +pub struct Awake;
> +/// Device with increased usage reference
> +pub struct Retain;
> +
> +/// Resumes the device without acquiring the usage reference.
> +/// Note: This does not guarantee the device will be kept active
> +/// for the lifetime of the scope due to potentil pending suspend
> +/// requests.
> +///
> +/// On drop:
> +/// - If `Mode::IDLE`, calls `__pm_runtime_idle()`:
> +/// triggers idle notification before attempting to suspend
> +/// - If `Mode::AUTO`, marks last busy then calls `__pm_runtime_suspend()`.
> +/// - Otherwise calls `__pm_runtime_suspend()`.
> +///
> +/// The guard must be dropped from a context matching the requested transition
> +/// mode: sync vs async.
> +#[must_use = "dropping this guard issues the matching runtime PM release request"]
> +pub struct ResumeScope(Scope<Resume>);
> +
> +/// Acquires a runtime-PM usage reference and keeps the device powered.
> +///
> +/// Requires `Mode::ACQUIRE`. Drop behavior matches `ResumeScope`.
> +/// The guard must be dropped from a context matching the requested transition
> +/// mode: sync vs async.
> +#[must_use = "dropping this guard releases its runtime PM hold"]
> +pub struct AwakeScope(Scope<Awake>);
> +
> +/// Prevents the device from getting suspended by holding the usage reference
> +/// count.
> +///
> +/// On drop, calls `pm_runtime_put_noidle()`.
> +#[must_use = "dropping this guard releases its runtime PM hold"]
> +pub struct RetainScope(Scope<Retain>);
> +
> +impl ResumeScope {
> + fn new(dev: ARef<device::Device>, mode: Mode) -> Result<Self> {
> + if mode.contains(Mode::ACQUIRE) {
> + // Mode::ACQUIRE is intended to be used with Awake scope
> + // Avoid mixing the modes.
> + return Err(EINVAL);
> + }
> +
> + // Mode::IDLE is internal so strip it of before passing further
> + Request::resume(&dev, mode & !Mode::IDLE).map(|()| {
> + Self(Scope::<Resume> {
> + dev: dev.clone(),
> + mode,
> + _tag: PhantomData,
> + })
> + })
> + }
> +}
> +
> +impl Drop for ResumeScope {
> + fn drop(&mut self) {
> + let scope_mode = self.0.mode & !Mode::IDLE;
> +
> + match self.0.mode {
> + mode if mode.contains(Mode::IDLE) => {
> + Request::idle(&self.0.dev, scope_mode & Mode::ASYNC);
> + }
> + mode if mode.contains(Mode::AUTO) => {
> + Request::mark_last_busy(&self.0.dev);
> + let _ = Request::suspend(&self.0.dev, scope_mode).inspect_err(|_| {
> + dev_err!(self.0.dev.as_ref(), "Failed to suspend device\n");
> + });
> + }
> + _ => {
> + let _ = Request::suspend(&self.0.dev, scope_mode).inspect_err(|_| {
> + dev_err!(self.0.dev.as_ref(), "Failed to suspend device\n");
> + });
> + }
> + }
> + }
> +}
> +
> +impl AwakeScope {
> + fn new(dev: ARef<device::Device>, mode: Mode) -> Result<Self> {
> + if !mode.contains(Mode::ACQUIRE) {
> + return Err(EINVAL);
> + }
> + // Mode::IDLE is internal so strip it of before passing further
> + Request::resume(&dev, mode & !Mode::IDLE)
> + .inspect_err(|_| Request::put_noidle(&dev))
> + .map(|()| {
> + Self(Scope::<Awake> {
> + dev: dev.clone(),
> + mode,
> + _tag: PhantomData,
> + })
> + })
> + }
> +}
> +
> +impl Drop for AwakeScope {
> + fn drop(&mut self) {
> + let scope_mode = self.0.mode & !Mode::IDLE;
> + match self.0.mode {
> + mode if mode.contains(Mode::IDLE) => {
> + Request::idle(&self.0.dev, scope_mode);
> + }
> + mode if mode.contains(Mode::AUTO) => {
> + Request::mark_last_busy(&self.0.dev);
> + let _ = Request::suspend(&self.0.dev, scope_mode).inspect_err(|_| {
> + dev_err!(self.0.dev.as_ref(), "Failed to suspend device\n");
> + });
> + }
> + _ => {
> + let _ = Request::suspend(&self.0.dev, scope_mode).inspect_err(|_| {
> + dev_err!(self.0.dev.as_ref(), "Failed to suspend device\n");
> + });
> + }
> + }
> + }
> +}
> +
> +impl RetainScope {
> + fn new(dev: ARef<device::Device>) -> Result<Self> {
> + Request::get_noresume(&dev);
> + Ok(Self(Scope::<Retain> {
> + dev,
> + mode: Mode(0),

This is Mode::SYNC.

Regards,
Onur

[...]

> +
> +impl<T: PMOps, State> PMContext<T, State> {
> + /// Applies runtime PM configuration options.
> + ///
> + /// Options are applied in the order provided. The currently supported
> + /// options do not report per-option failures.
> + ///
> + /// This may be called before or after runtime PM is enabled, depending on
> + /// the option and the driver's setup sequence.
> + pub fn apply_config(&self, opts: &[PMConfig]) -> Result {
> + #[cfg(not(CONFIG_PM))]
> + let _ = opts;
> + #[cfg(CONFIG_PM)]
> + for opt in opts {
> + match opt {
> + // SAFETY: `self.dev` is a valid `&ARef<Device>`, so the underlying `Device` is
> + // guaranteed to be alive and `as_raw()` yields a valid pointer for the
> + // duration of this call.
> + PMConfig::IgnoreChildren(v) => unsafe {
> + bindings::pm_suspend_ignore_children(self.dev.as_raw(), *v)
> + },
> + // SAFETY: `self.dev` is a valid `&ARef<Device>`, so the underlying `Device` is
> + // guaranteed to be alive and `as_raw()` yields a valid pointer for the
> + // duration of this call.
> + PMConfig::NoCallbacks => unsafe {
> + bindings::pm_runtime_no_callbacks(self.dev.as_raw())
> + },
> + // SAFETY: `self.dev` is a valid `&ARef<Device>`, so the underlying `Device` is
> + // guaranteed to be alive and `as_raw()` yields a valid pointer for the
> + // duration of this call.
> + PMConfig::IrqSafe => unsafe { bindings::pm_runtime_irq_safe(self.dev.as_raw()) },
> + // SAFETY: `self.dev` is a valid `&ARef<Device>`, so the underlying `Device` is
> + // guaranteed to be alive and `as_raw()` yields a valid pointer for the
> + // duration of this call.
> + PMConfig::AutoSuspend(v) => unsafe {
> + bindings::__pm_runtime_use_autosuspend(self.dev.as_raw(), *v);
> + },
> + // SAFETY: `self.dev` is a valid `&ARef<Device>`, so the underlying `Device` is
> + // guaranteed to be alive and `as_raw()` yields a valid pointer for the
> + // duration of this call.
> + PMConfig::AutoSuspendDelay(v) => unsafe {
> + bindings::pm_runtime_set_autosuspend_delay(self.dev.as_raw(), *v as i32);
> + bindings::__pm_runtime_use_autosuspend(self.dev.as_raw(), true);
> + },
> + }
> + }
> + Ok(())
> + }
> +}
> --
> 2.43.0
>