mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: "Gary Guo" <gary@garyguo.net>
To: "Alice Ryhl" <aliceryhl@google.com>,
	"Greg Kroah-Hartman" <gregkh@linuxfoundation.org>,
	"Carlos Llamas" <cmllamas@google.com>,
	"Boqun Feng" <boqun@kernel.org>, "Gary Guo" <gary@garyguo.net>
Cc: "Onur Özkan" <work@onurozkan.dev>,
	"Andreas Hindborg" <a.hindborg@kernel.org>,
	"Benno Lossin" <lossin@kernel.org>,
	"Björn Roy Baron" <bjorn3_gh@protonmail.com>,
	"Daniel Almeida" <daniel.almeida@collabora.com>,
	"Danilo Krummrich" <dakr@kernel.org>,
	"Ingo Molnar" <mingo@redhat.com>, "Lyude Paul" <lyude@redhat.com>,
	"Miguel Ojeda" <ojeda@kernel.org>,
	"Peter Zijlstra" <peterz@infradead.org>,
	"Trevor Gross" <tmgross@umich.edu>,
	"Waiman Long" <longman@redhat.com>,
	"Will Deacon" <will@kernel.org>,
	linux-kernel@vger.kernel.org, rust-for-linux@vger.kernel.org,
	"Alvin Sun" <alvin.sun@linux.dev>
Subject: Re: [PATCH v3 3/5] rust: add pr_*_ratelimit! macros for printing
Date: Wed, 22 Jul 2026 19:15:55 +0100	[thread overview]
Message-ID: <DK5B0218LGR4.1SU1DLMQYKAQG@garyguo.net> (raw)
In-Reply-To: <20260722-pr-ratelimited-v3-3-57faa37fdfbd@google.com>

On Wed Jul 22, 2026 at 11:12 AM 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.
>
> Tested-by: Alvin Sun <alvin.sun@linux.dev>
> Reviewed-by: Carlos Llamas <cmllamas@google.com>
> Link: https://github.com/Rust-for-Linux/linux/issues/122
> Signed-off-by: Alice Ryhl <aliceryhl@google.com>
> ---
>  rust/helpers/helpers.c            |   1 +
>  rust/helpers/ratelimit.c          |  14 +++
>  rust/kernel/lib.rs                |   1 +
>  rust/kernel/prelude.rs            |   8 ++
>  rust/kernel/ratelimit.rs          | 215 ++++++++++++++++++++++++++++++++++++++
>  rust/kernel/sync/lock/spinlock.rs |   1 -
>  6 files changed, 239 insertions(+), 1 deletion(-)
>
> diff --git a/rust/kernel/ratelimit.rs b/rust/kernel/ratelimit.rs
> new file mode 100644
> index 000000000000..426992e452a2
> --- /dev/null
> +++ b/rust/kernel/ratelimit.rs
> @@ -0,0 +1,215 @@
> +// 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 name = $crate::c_str!(::core::stringify!($name));
> +            let interval = $interval;
> +            let burst = $burst;
> +            // SAFETY: This will be stored in static memory.
> +            unsafe { $crate::ratelimit::Ratelimit::new_static(name, interval, burst) }
> +        };
> +    };
> +}
> +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) };
> +            }),
> +        })
> +    }
> +
> +    /// Constructs a [`Ratelimit`] with the default configuration.
> +    #[inline]
> +    pub fn new_default() -> impl PinInit<Self> {
> +        Ratelimit::new(Ratelimit::DEFAULT_INTERVAL, Ratelimit::DEFAULT_BURST)
> +    }
> +
> +    /// Constructs a [`Ratelimit`] with the specified configuration.
> +    ///
> +    /// The name will be used for the lockdep name of the internal spinlock. See [`Self::new`] for
> +    /// the meaning of `interval` and `burst`.
> +    ///
> +    /// # Safety
> +    ///
> +    /// The resulting value must be stored in static memory.

This one is const-only so shouldn't be code-generated.

    #[inline]

Best,
Gary

> +    pub const unsafe fn new_static(name: &'static CStr, interval: i32, burst: i32) -> Self {
> +        Self {
> +            inner: Opaque::new(bindings::ratelimit_state {
> +                lock: kernel::sync::lock::spinlock::raw_spin_lock_unlocked(name),
> +                interval,
> +                burst,
> +                ..pin_init::zeroed()
> +            }),
> +        }
> +    }


  reply	other threads:[~2026-07-22 18:16 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-07-22 10:12 [PATCH v3 0/5] Rate limited printing for Rust Alice Ryhl
2026-07-22 10:12 ` [PATCH v3 1/5] rust: sync: move lockdep types to rust/kernel/sync/lockdep.rs Alice Ryhl
2026-07-22 10:12 ` [PATCH v3 2/5] rust: sync: add const constructor for raw_spinlock_t Alice Ryhl
2026-07-22 10:12 ` [PATCH v3 3/5] rust: add pr_*_ratelimit! macros for printing Alice Ryhl
2026-07-22 18:15   ` Gary Guo [this message]
2026-07-22 10:12 ` [PATCH v3 4/5] rust_binder: consolidate transaction failure prints Alice Ryhl
2026-07-22 10:12 ` [PATCH v3 5/5] rust_binder: use pr_*_ratelimited! for printing Alice Ryhl

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=DK5B0218LGR4.1SU1DLMQYKAQG@garyguo.net \
    --to=gary@garyguo.net \
    --cc=a.hindborg@kernel.org \
    --cc=aliceryhl@google.com \
    --cc=alvin.sun@linux.dev \
    --cc=bjorn3_gh@protonmail.com \
    --cc=boqun@kernel.org \
    --cc=cmllamas@google.com \
    --cc=dakr@kernel.org \
    --cc=daniel.almeida@collabora.com \
    --cc=gregkh@linuxfoundation.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=longman@redhat.com \
    --cc=lossin@kernel.org \
    --cc=lyude@redhat.com \
    --cc=mingo@redhat.com \
    --cc=ojeda@kernel.org \
    --cc=peterz@infradead.org \
    --cc=rust-for-linux@vger.kernel.org \
    --cc=tmgross@umich.edu \
    --cc=will@kernel.org \
    --cc=work@onurozkan.dev \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®