Rust for Linux List
 help / color / mirror / Atom feed
From: "Gary Guo" <gary@garyguo.net>
To: "Alice Ryhl" <aliceryhl@google.com>, "Gary Guo" <gary@garyguo.net>
Cc: "Greg Kroah-Hartman" <gregkh@linuxfoundation.org>,
	"Carlos Llamas" <cmllamas@google.com>,
	"Boqun Feng" <boqun@kernel.org>,
	"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
Subject: Re: [PATCH 3/5] rust: add pr_*_ratelimit! macros for printing
Date: Wed, 01 Jul 2026 18:14:45 +0100	[thread overview]
Message-ID: <DJNEJSFRKDMZ.1J9DIN32Q9FXI@garyguo.net> (raw)
In-Reply-To: <CAH5fLgjDAgwoCo3w2n0AF9NsY2mBkddG9=21Ame9qxiAY2W9iw@mail.gmail.com>

On Wed Jul 1, 2026 at 6:02 PM BST, Alice Ryhl wrote:
> On Wed, Jul 1, 2026 at 6:20 PM Gary Guo <gary@garyguo.net> wrote:
>>
>> 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@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 | 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.
>
> I believe that the lock class key this implicitly creates is only
> valid in static memory, and not for a Ratelimit stored anywhere else.

Perhaps create a

    /// # Safety
    ///
    /// The created `Ratelimit` must be stored in a static.
    pub const unsafe fn new_static() -> Self {
    }

?

Best,
Gary


  reply	other threads:[~2026-07-01 17:14 UTC|newest]

Thread overview: 26+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-06-23 15:38 [PATCH 0/5] Rate limited printing for Rust Alice Ryhl
2026-06-23 15:38 ` [PATCH 1/5] rust: sync: move lockdep types to rust/kernel/sync/lockdep.rs Alice Ryhl
2026-06-26 20:26   ` Carlos Llamas
2026-06-30  3:20   ` Boqun Feng
2026-07-01 16:10   ` Gary Guo
2026-06-23 15:38 ` [PATCH 2/5] rust: sync: add const constructor for raw_spinlock_t Alice Ryhl
2026-06-26 20:52   ` Carlos Llamas
2026-06-30  3:44   ` Boqun Feng
2026-07-01 16:15   ` Gary Guo
2026-06-23 15:38 ` [PATCH 3/5] rust: add pr_*_ratelimit! macros for printing Alice Ryhl
2026-06-23 15:55   ` Gary Guo
2026-06-23 19:11     ` Alice Ryhl
2026-06-23 19:53     ` Miguel Ojeda
2026-06-23 20:06       ` Gary Guo
2026-06-23 19:31   ` Miguel Ojeda
2026-06-23 20:05     ` Alice Ryhl
2026-06-26 23:12   ` Carlos Llamas
2026-07-01 16:20   ` Gary Guo
2026-07-01 17:02     ` Alice Ryhl
2026-07-01 17:14       ` Gary Guo [this message]
2026-07-02  7:50         ` Alice Ryhl
2026-07-02  6:15   ` Alvin Sun
2026-06-23 15:38 ` [PATCH 4/5] rust_binder: consolidate transaction failure prints Alice Ryhl
2026-06-26 23:28   ` Carlos Llamas
2026-06-23 15:38 ` [PATCH 5/5] rust_binder: use pr_*_ratelimited! for printing Alice Ryhl
2026-06-26 23:34   ` Carlos Llamas

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=DJNEJSFRKDMZ.1J9DIN32Q9FXI@garyguo.net \
    --to=gary@garyguo.net \
    --cc=a.hindborg@kernel.org \
    --cc=aliceryhl@google.com \
    --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