Rust for Linux List
 help / color / mirror / Atom feed
From: Carlos Llamas <cmllamas@google.com>
To: Alice Ryhl <aliceryhl@google.com>
Cc: "Greg Kroah-Hartman" <gregkh@linuxfoundation.org>,
	"Boqun Feng" <boqun@kernel.org>, "Gary Guo" <gary@garyguo.net>,
	"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 2/5] rust: sync: add const constructor for raw_spinlock_t
Date: Fri, 26 Jun 2026 20:52:54 +0000	[thread overview]
Message-ID: <aj7mpkvobbQVLLcx@google.com> (raw)
In-Reply-To: <20260623-pr-ratelimited-v1-2-cc922f544dc0@google.com>

On Tue, Jun 23, 2026 at 03:38:05PM +0000, Alice Ryhl wrote:
> The abstractions for pr_*_ratelimited! need to construct a global
> `struct ratelimit_state`, which contains a `raw_spinlock_t` field. Thus,
> add a const constructor for the `raw_spinlock_t` type.
> 
> The SPINLOCK_OWNER_INIT constant isn't mirrored via a const helper
> because bindgen generates a 'static mut' instead of a constant from the
> pointer constant.,
> 
> The __ARCH_SPIN_LOCK_UNLOCKED constant cannot be translated by bindgen
> because it's a define for a struct without type annotations, so it's
> explicitly declared in Rust.
> 
> Signed-off-by: Alice Ryhl <aliceryhl@google.com>
> ---
>  include/linux/spinlock_types_raw.h |  4 ++++
>  rust/bindings/lib.rs               | 24 ++++++++++++++++++++++++
>  rust/kernel/sync/lock/spinlock.rs  | 29 +++++++++++++++++++++++++++++
>  rust/kernel/sync/lockdep.rs        | 22 ++++++++++++++++++++++
>  4 files changed, 79 insertions(+)
> 
> diff --git a/include/linux/spinlock_types_raw.h b/include/linux/spinlock_types_raw.h
> index e5644ab2161f..942c229c90bb 100644
> --- a/include/linux/spinlock_types_raw.h
> +++ b/include/linux/spinlock_types_raw.h
> @@ -11,6 +11,10 @@
>  
>  #include <linux/lockdep_types.h>
>  
> +/*
> + * Keep in sync with rust/kernel/sync/lock/spinlock.rs
> + */
> +
>  context_lock_struct(raw_spinlock) {
>  	arch_spinlock_t raw_lock;
>  #ifdef CONFIG_DEBUG_SPINLOCK
> diff --git a/rust/bindings/lib.rs b/rust/bindings/lib.rs
> index 854e7c471434..adde41e41edc 100644
> --- a/rust/bindings/lib.rs
> +++ b/rust/bindings/lib.rs
> @@ -77,3 +77,27 @@ mod bindings_helper {
>          None
>      }
>  };
> +
> +// Explicitly list architectures where this logic is checked correct.
> +#[cfg(any(
> +    CONFIG_ARM,
> +    CONFIG_ARM64,
> +    CONFIG_LOONGARCH,
> +    CONFIG_PPC,
> +    CONFIG_RISCV,
> +    CONFIG_S390,
> +    CONFIG_X86,
> +))]

I guess this is done explicitly because we can't test if
__ARCH_SPIN_LOCK_UNLOCKED is defined by the arch?

> +pub const __ARCH_SPIN_LOCK_UNLOCKED: arch_spinlock_t = {
> +    // SAFETY: The `arch_spinlock_t` type can be zeroed.
> +    #[allow(unused_mut)]
> +    let mut lock: arch_spinlock_t = unsafe { core::mem::zeroed() };
> +
> +    #[cfg(not(CONFIG_SMP))]
> +    #[cfg(CONFIG_DEBUG_SPINLOCK)]
> +    {
> +        lock.slock = 1;
> +    }
> +
> +    lock
> +};
> diff --git a/rust/kernel/sync/lock/spinlock.rs b/rust/kernel/sync/lock/spinlock.rs
> index ef76fa07ca3a..8babfb79098f 100644
> --- a/rust/kernel/sync/lock/spinlock.rs
> +++ b/rust/kernel/sync/lock/spinlock.rs
> @@ -4,6 +4,8 @@
>  //!
>  //! This module allows Rust code to use the kernel's `spinlock_t`.
>  
> +use kernel::prelude::*;
> +
>  /// Creates a [`SpinLock`] initialiser with the given name and a newly-created lock class.
>  ///
>  /// It uses the name if one is given, otherwise it generates one based on the file name and line
> @@ -144,3 +146,30 @@ unsafe fn assert_is_held(ptr: *mut Self::State) {
>          unsafe { bindings::spin_assert_is_held(ptr) }
>      }
>  }
> +
> +/// Helper for creating a raw unlocked `bindings::raw_spinlock_t`.
> +///
> +/// For use in statics containing raw spinlocks.
> +pub const fn raw_spin_lock_unlocked(name: &'static CStr) -> bindings::raw_spinlock_t {
> +    // Silence unused variable warnings.
> +    #[cfg(not(CONFIG_DEBUG_LOCK_ALLOC))]
> +    let _ = name;
> +
> +    bindings::raw_spinlock_t {
> +        raw_lock: bindings::__ARCH_SPIN_LOCK_UNLOCKED,
> +
> +        #[cfg(CONFIG_DEBUG_SPINLOCK)]
> +        magic: bindings::SPINLOCK_MAGIC,
> +        #[cfg(CONFIG_DEBUG_SPINLOCK)]
> +        owner_cpu: u32::MAX,
> +        #[cfg(CONFIG_DEBUG_SPINLOCK)]
> +        owner: usize::MAX as *mut c_void,
> +
> +        #[cfg(CONFIG_DEBUG_LOCK_ALLOC)]
> +        dep_map: kernel::sync::lockdep::raw_lockdep_map(
> +            name,
> +            kernel::sync::lockdep::LD_WAIT_SPIN,
> +            kernel::sync::lockdep::LD_WAIT_INV,
> +        ),
> +    }
> +}

This seems identical to the __RAW_SPIN_LOCK_UNLOCKED equivalent.

> diff --git a/rust/kernel/sync/lockdep.rs b/rust/kernel/sync/lockdep.rs
> index 784821cc2a39..c0f8b196c084 100644
> --- a/rust/kernel/sync/lockdep.rs
> +++ b/rust/kernel/sync/lockdep.rs
> @@ -137,3 +137,25 @@ macro_rules! optional_name {
>          $crate::c_str!($name)
>      };
>  }
> +
> +/// Not checked, catch all.
> +pub const LD_WAIT_INV: u8 = bindings::lockdep_wait_type_LD_WAIT_INV as u8;
> +/// Spin loops, `raw_spinlock_t` etc
> +pub const LD_WAIT_SPIN: u8 = bindings::lockdep_wait_type_LD_WAIT_SPIN as u8;
> +
> +/// Helper for declaring a raw `struct lockdep_map` for locks in statics.
> +///
> +/// It's up to the caller to use the returned `struct lockdep_map` correctly.
> +#[cfg(CONFIG_LOCKDEP)]
> +pub const fn raw_lockdep_map(
> +    name: &'static CStr,
> +    wait_type_inner: u8,
> +    wait_type_outer: u8,
> +) -> bindings::lockdep_map {
> +    // SAFETY: All zeros is valid for this type.
> +    let mut map: bindings::lockdep_map = unsafe { core::mem::zeroed() };
> +    map.name = kernel::str::as_char_ptr_in_const_context(name);
> +    map.wait_type_inner = wait_type_inner;
> +    map.wait_type_outer = wait_type_outer;
> +    map
> +}
> 
> -- 
> 2.55.0.rc0.799.gd6f94ed593-goog
> 

Looks good to me,

Reviewed-by: Carlos Llamas <cmllamas@google.com>

  reply	other threads:[~2026-06-26 20:53 UTC|newest]

Thread overview: 17+ 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-23 15:38 ` [PATCH 2/5] rust: sync: add const constructor for raw_spinlock_t Alice Ryhl
2026-06-26 20:52   ` Carlos Llamas [this message]
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-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=aj7mpkvobbQVLLcx@google.com \
    --to=cmllamas@google.com \
    --cc=a.hindborg@kernel.org \
    --cc=aliceryhl@google.com \
    --cc=bjorn3_gh@protonmail.com \
    --cc=boqun@kernel.org \
    --cc=dakr@kernel.org \
    --cc=daniel.almeida@collabora.com \
    --cc=gary@garyguo.net \
    --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