From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-pl1-f180.google.com (mail-pl1-f180.google.com [209.85.214.180]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 178C03BF69D for ; Fri, 26 Jun 2026 20:53:00 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.180 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1782507183; cv=none; b=JalrXiSTKNvWo0tbskUJvdcv64B1AGBbxmvbm+Ucd+VNARp9qJIms2Bjf1aCG3MeQTzMjdGDg2cSmcw56h4GqsKvEowRc5WXeJFsM6OojiwThllBb2IZ78+h5kH+gJqdH2MfH2vKYm2t5BjT20Cz8wwXufAcr/J2fbJc2Y5nkr4= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1782507183; c=relaxed/simple; bh=gXW3dCe4WC3+rTR/lWPp+8k43M2LpCggBbClN6estak=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=VgvxpnF68B3BqjV0Scr4kshtcyeDlju7YZtXDnpMvkRGtPfuWkNOBzT52NVVpr9um/Zhf1LtTcySwYFXt8hQUshaNOANstX/agcPypKpZ8ReZgwhchZALInIf7syvtOf5DEfsiyKlxMIWH9zC4S86/Ki32ljAY4Zkb/Uho19M2E= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=UH/IrBm2; arc=none smtp.client-ip=209.85.214.180 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="UH/IrBm2" Received: by mail-pl1-f180.google.com with SMTP id d9443c01a7336-2c813a45a65so3585ad.1 for ; Fri, 26 Jun 2026 13:53:00 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1782507180; x=1783111980; darn=vger.kernel.org; h=in-reply-to:content-disposition:content-type:mime-version :references:message-id:subject:cc:to:from:date:from:to:cc:subject :date:message-id:reply-to:content-type; bh=dS/M2HRldoM7WFjsowGr4rjNFpNg1xpcvyBkf4LjNgo=; b=UH/IrBm2vfd8OYeKRXL99VXP94ysFVLpUY1uWJzuHw42MLYkeMfaA908PaKk75SKxM 98tzaRxUn9xiKrQ0P5IA85o+CbOr27Ihjbei37uX6lEa0EYxZznhkjNBmT78GeCKBBxY UL3/5ICgq45Zhz4FjJxsD0zJSvT5qUxILloDU6id4Qmo4AfEXQLmowVWl0+XvELKyaTP sjUuRnat7aPq46yuI/AnJvOGpVFRIeKYznVFphZPpGufr61HD4jF04wNpA2xe7N+Imng vaw6ON1jEmYhAOR1nY8bK3TCd7PHPgF2s6N7OVbwsNiVmQjSC1D1iVeI689RJQeQNpvY z+Xw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1782507180; x=1783111980; h=in-reply-to:content-disposition:content-type:mime-version :references:message-id:subject:cc:to:from:date:x-gm-gg :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to :content-type; bh=dS/M2HRldoM7WFjsowGr4rjNFpNg1xpcvyBkf4LjNgo=; b=Gx27GBPgqiOqn5QvxToQAkdBwtDeUcaopkwsrcNxvGlSbMwgMCvCxK0cl/YTgeqzu8 BcV48oiLziNesHUCjQBsBui1lLzqBSHJe2FE3QWRbQLjdjtFFiQ0HXaOos7quVbkIQHv m9AOYhFD59w7Gi79e+PAly9wmHQgUsskRiJphRSz4zOsY0Ru0KuF8iONqZ3/17d/08pe SK8qrwVU3k556LyV5hnhlyp3jgA0T1aowP3CpSBmN0h9YpzkR5IorZ6rrPx91JwC8pQS XAJc/obERpMWhouwfO48A4REQwz7a5GTbfVpj2EqbG6hdleTHZK733ce7KSLWF1Prp8J FwIg== X-Forwarded-Encrypted: i=1; AHgh+Rr2uN4vXrUDWSOUygdTXSnLLyX++gA20E/6jKd4rS5IgePQ1X9fMtyvpWfSHlEnrltvjUuMNiZyQNczftU=@vger.kernel.org X-Gm-Message-State: AOJu0YwU9o/qwuoCszMAk4PJdqegydtT1bEfFy6Gk1iej7mfDPDmueph tts6+aWdr+GEpXiHUX0GKfFM98jOcPE2YEBphLM48+5wztz8EI7GYVwq01z7TvNtjg== X-Gm-Gg: AfdE7cnRShTs7cR7Gz+cTTNo1TgEIOB/h7N/rfjTrLRH4mf4VBtIcwpxzbVcw/LGKBP TRT565R+8QsIOavGzmXCyPyHAVAld0X1pLtKpSLMylpsm6HGpllrsJ7U/xmiTKMXzC9P23Z13wc h1/+gAfjyJxN6i4/010TNEHzJei5VoyFMLypIbyMx7sQbzTA1qkO0XfB2wrxnpTrNtkeFE4UB2N sY1VOyHSMi5x9GmeVEkng9Paj9fBQ3fzWa57uxZVVZxSSet1K3tgLgOrnrFNYDw+skO3ALD+Bi8 dmuJnvXPlYi/vlR9jNYQ7UJxZrP3tmFScNBQWBBcDhqi+eR2KpkDUFltnQOVQ0jtVzrwkgA696J btH15aFeNLdQ6NpRf36wXsrmtXEnlLjyLXNViG07LICkdbg8Uo7eH6X2WIKtBcypoprTQFUoNM2 q+GJ9a26PU2DEKE7J0LoRDP5qo46mTUXmDOvhSd2whEs/NJD8bZUMmB9uOMSTZvR5s0knloa1uK zC5FSPKm2uypyBoyYD3JOw7CqU1cLG0uKE= X-Received: by 2002:a17:903:2ad0:b0:2bd:6dad:3dfb with SMTP id d9443c01a7336-2c9ae984803mr129675ad.25.1782507179731; Fri, 26 Jun 2026 13:52:59 -0700 (PDT) Received: from google.com (112.174.16.34.bc.googleusercontent.com. [34.16.174.112]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-2c7f5afb1e0sm46815795ad.29.2026.06.26.13.52.58 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 26 Jun 2026 13:52:58 -0700 (PDT) Date: Fri, 26 Jun 2026 20:52:54 +0000 From: Carlos Llamas To: Alice Ryhl Cc: Greg Kroah-Hartman , Boqun Feng , Gary Guo , Onur =?iso-8859-1?Q?=D6zkan?= , Andreas Hindborg , Benno Lossin , =?iso-8859-1?Q?Bj=F6rn?= Roy Baron , Daniel Almeida , Danilo Krummrich , Ingo Molnar , Lyude Paul , Miguel Ojeda , Peter Zijlstra , Trevor Gross , Waiman Long , Will Deacon , 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 Message-ID: References: <20260623-pr-ratelimited-v1-0-cc922f544dc0@google.com> <20260623-pr-ratelimited-v1-2-cc922f544dc0@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline 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 > --- > 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 > > +/* > + * 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