Re: [PATCH 3/5] rust: add pr_*_ratelimit! macros for printing

From: Gary Guo

Date: Wed Jul 01 2026 - 12:21:00 EST


On Tue Jun 23, 2026 at 4:38 PM BST, Alice Ryhl wrote:
> Printing can be very expensive if it occurs often, so printing that can
> be triggered by userspace should be rate limited. For this purpose, add
> a Rust wrapper around `struct ratelimit_state` and use it in the new
> macros.
>
> Signed-off-by: Alice Ryhl <aliceryhl@xxxxxxxxxx>
> ---
> rust/helpers/helpers.c | 1 +
> rust/helpers/ratelimit.c | 14 ++++
> rust/kernel/lib.rs | 1 +
> rust/kernel/prelude.rs | 8 ++
> rust/kernel/ratelimit.rs | 202 +++++++++++++++++++++++++++++++++++++++++++++++
> 5 files changed, 226 insertions(+)
>
> diff --git a/rust/helpers/helpers.c b/rust/helpers/helpers.c
> index d17eaec76450..2184b11c927f 100644
> --- a/rust/helpers/helpers.c
> +++ b/rust/helpers/helpers.c
> @@ -80,6 +80,7 @@
> #include "processor.c"
> #include "property.c"
> #include "pwm.c"
> +#include "ratelimit.c"
> #include "rbtree.c"
> #include "rcu.c"
> #include "refcount.c"
> diff --git a/rust/helpers/ratelimit.c b/rust/helpers/ratelimit.c
> new file mode 100644
> index 000000000000..e5052f568b81
> --- /dev/null
> +++ b/rust/helpers/ratelimit.c
> @@ -0,0 +1,14 @@
> +// SPDX-License-Identifier: GPL-2.0
> +
> +#include <linux/ratelimit.h>
> +
> +__rust_helper void rust_helper_ratelimit_state_init(struct ratelimit_state *rs,
> + int interval, int burst)
> +{
> + ratelimit_state_init(rs, interval, burst);
> +}
> +
> +__rust_helper void rust_helper_ratelimit_state_exit(struct ratelimit_state *rs)
> +{
> + ratelimit_state_exit(rs);
> +}
> diff --git a/rust/kernel/lib.rs b/rust/kernel/lib.rs
> index b72b2fbe046d..ba65ab4f0b8c 100644
> --- a/rust/kernel/lib.rs
> +++ b/rust/kernel/lib.rs
> @@ -111,6 +111,7 @@
> pub mod ptr;
> #[cfg(CONFIG_RUST_PWM_ABSTRACTIONS)]
> pub mod pwm;
> +pub mod ratelimit;
> pub mod rbtree;
> pub mod regulator;
> pub mod revocable;
> diff --git a/rust/kernel/prelude.rs b/rust/kernel/prelude.rs
> index 44edf72a4a24..5a66028dd973 100644
> --- a/rust/kernel/prelude.rs
> +++ b/rust/kernel/prelude.rs
> @@ -89,13 +89,21 @@
> },
> init::InPlaceInit,
> pr_alert,
> + pr_alert_ratelimited,
> pr_crit,
> + pr_crit_ratelimited,
> pr_debug,
> + pr_debug_ratelimited,
> pr_emerg,
> + pr_emerg_ratelimited,
> pr_err,
> + pr_err_ratelimited,
> pr_info,
> + pr_info_ratelimited,
> pr_notice,
> + pr_notice_ratelimited,
> pr_warn,
> + pr_warn_ratelimited,
> static_assert,
> str::CStrExt as _,
> try_init,
> diff --git a/rust/kernel/ratelimit.rs b/rust/kernel/ratelimit.rs
> new file mode 100644
> index 000000000000..da0a49412023
> --- /dev/null
> +++ b/rust/kernel/ratelimit.rs
> @@ -0,0 +1,202 @@
> +// SPDX-License-Identifier: GPL-2.0
> +
> +//! Rate limiting support.
> +//!
> +//! C header: [`include/linux/ratelimit.h`](srctree/include/linux/ratelimit.h)
> +
> +use crate::{
> + bindings,
> + prelude::*,
> + types::Opaque, //
> +};
> +
> +/// Defines a `static` containing a [`Ratelimit`].
> +#[macro_export]
> +macro_rules! ratelimit_state_init {
> + ($name:ident, $interval:expr, $burst:expr $(,)?) => {
> + static $name: $crate::ratelimit::Ratelimit = {
> + let init: $crate::bindings::ratelimit_state = $crate::bindings::ratelimit_state {
> + lock: $crate::sync::lock::spinlock::raw_spin_lock_unlocked($crate::c_str!(
> + ::core::stringify!($name)
> + )),
> + interval: $interval,
> + burst: $burst,
> + // SAFETY: This type can be zeroed.
> + ..unsafe { ::core::mem::zeroed() }
> + };
> + // SAFETY: This is a repr(transparent) wrapper, and the invariants are satisfied.
> + unsafe { ::core::mem::transmute(init) }

Hmm, I don't see anything that prohibit this from being a const fn? What have I
missed?

It looks like this type is move-initializable.

Best,
Gary

> + };
> + };
> +}
> +pub use ratelimit_state_init;
> +
> +/// Rate limiter state.
> +///
> +/// # Invariants
> +///
> +/// The `inner` field contains an initialized `struct ratelimit_state`.
> +#[pin_data(PinnedDrop)]
> +#[repr(transparent)]
> +pub struct Ratelimit {
> + #[pin]
> + inner: Opaque<bindings::ratelimit_state>,
> +}
> +
> +// SAFETY: `Ratelimit` is safe to be sent to any task.
> +unsafe impl Send for Ratelimit {}
> +
> +// SAFETY: `Ratelimit` is safe to be accessed concurrently as it is protected by an internal
> +// spinlock.
> +unsafe impl Sync for Ratelimit {}
> +
> +impl Ratelimit {
> + /// Constructs a [`Ratelimit`] with the specified configuration.
> + ///
> + /// If `interval` is zero, then no rate limit is applied.
> + #[inline]
> + pub fn new(interval: i32, burst: i32) -> impl PinInit<Self> {
> + // INVARIANT: This creates a `Ratelimit` containing an initialized `struct ratelimit_state`
> + pin_init!(Self {
> + inner <- Opaque::ffi_init(|slot: *mut bindings::ratelimit_state| {
> + // SAFETY: `slot` is a valid pointer to an uninitialized `struct ratelimit_state`.
> + // The memory is pinned so it remains valid until `ratelimit_state_exit` is called.
> + unsafe { bindings::ratelimit_state_init(slot, interval, burst) };
> + }),
> + })
> + }