From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-wr1-f74.google.com (mail-wr1-f74.google.com [209.85.221.74]) (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 486C141E6CC for ; Thu, 16 Jul 2026 12:34:47 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.221.74 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1784205292; cv=none; b=Ag4/KjaTzaHzk1vP4g12+evXyVQxQU+z2inx+TMM6gZpDVHB/jbfgESHCILlL4VLFo7EsWmaIGJmCUVSO74eZHwtvXMM47fJYtkRQwr5UOS3n2cYiYLQv3ejNGIqhOK4I9ZnMi399ZBNvFoeuD+P0nCaEBaCoBILPBZHd3rGyRY= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1784205292; c=relaxed/simple; bh=F3XfE2qmquOsjgl5Z7SgmdoRF1G91H01/1f0xHWJi/c=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=qIOH4ufYlF50bedpaThnuLtmQAjgGYlm7t3+/9yXijRE8yPcxjtV8cUPGQftZQ7+gDecw4CloRIHXy9sDKi0dbmjTxCmvzY9jsxtQ5SdIuekhXtjHg5iMBiEAfkcoqDOUcf3Li50zJ/gAHWmKpZl6TXkg3TY2VBcxbCe4sKVZHs= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--aliceryhl.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=gTnHaGZ4; arc=none smtp.client-ip=209.85.221.74 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=flex--aliceryhl.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="gTnHaGZ4" Received: by mail-wr1-f74.google.com with SMTP id ffacd0b85a97d-47f4bff865cso1565619f8f.0 for ; Thu, 16 Jul 2026 05:34:47 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1784205286; x=1784810086; darn=vger.kernel.org; h=content-type:cc:to:from:subject:message-id:references:mime-version :in-reply-to:date:from:to:cc:subject:date:message-id:reply-to :content-type; bh=CcHRQa5oaNT52Q3NzivF4AOmmeBmFBenYIo8+2bH3+U=; b=gTnHaGZ4lbT7PsE/nDcuT33Dt/xLpTU7LktH4Wsd4g+VhwL9+gEYu4GYEPaSCFaW25 pSTYvf+jXp7t9eX7ZsISqiqAleMYJUZGbprm9HaZG4dF8rCObbkwUKREfOcw7VxAPa6b /09Gpc/N7gk2eLXmRpR0Zrahjf4LyHuCLYh1VmnEKpHzuAU7Zuqc/Ew5Bn2ExlbuMf6N osnalzWuiPiIEK6S8zqyiPj8q5tXoetD2pI2yDnAAYkrXHzuirLWBD16bZ5yG+C6d6bL vi+RZTuRM4HMSbw/nSJlx+vsSnq6cTMZrSsFwOl9SbejnuqknFj8SmPhc2Vzmfjl4kUC oo/w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1784205286; x=1784810086; h=content-type:cc:to:from:subject:message-id:references:mime-version :in-reply-to:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to:content-type; bh=CcHRQa5oaNT52Q3NzivF4AOmmeBmFBenYIo8+2bH3+U=; b=QJSSAsz5t964uq2AEbCU3yL9wOWzQNqsirmHKcCvtv1wP90ryOiiGywIjyB1jZ7plc ZgaHcKeBU2InGzT8awkVtT1DBIH3xf74CpR/VKlqiXHlV3mCwlwNiH8mgM0oxWPtq3UA EkdtEI9gqMcHRd14q9tWiisPHgSG8inOJBFXZYNKQrKj90VSZsc5aRD1OT6pPZkeo4n0 kYtWEZ6IkXKAURGr3SjOp9KuFwwwzw4bgwYOdMuQI7suDq3V+m5b+JoG0jEXhHCcDXld 2JFo58cSjFgjTPBhkuBrlQdSQ1M20SE/nJ7NJjrU1bk2yzPiauBi50eephtRz/dkUb+c UYxA== X-Forwarded-Encrypted: i=1; AHgh+Ro8sQeM3p78zioklvvmuHxBpGjMU07Rd2ArGtBDFGi5dW55rrZM8IVrCjwyZO5+LHUsVYImpWOB2xS6rDo=@vger.kernel.org X-Gm-Message-State: AOJu0YwV+vnDBaA2WEq0n47n+TiKzWmAByHfeRwvHX/5mkxQmGFfHIk1 HbHNp3xsVissp+3j6pSkYoiBdmfJaA2W896pTnj7oMOHifPP7zBkV0z4nq88b09F4/WpuY09Ywr uyhfOqK8SxpP8mwByOw== X-Received: from wrvi4.prod.google.com ([2002:a5d:5584:0:b0:47f:3eaa:11e1]) (user=aliceryhl job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6000:178b:b0:47f:2de3:b351 with SMTP id ffacd0b85a97d-47f4634dd31mr15994410f8f.26.1784205285556; Thu, 16 Jul 2026 05:34:45 -0700 (PDT) Date: Thu, 16 Jul 2026 12:34:25 +0000 In-Reply-To: <20260716-pr-ratelimited-v2-0-31c27a4543d2@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20260716-pr-ratelimited-v2-0-31c27a4543d2@google.com> X-Developer-Key: i=aliceryhl@google.com; a=openpgp; fpr=49F6C1FAA74960F43A5B86A1EE7A392FDE96209F X-Developer-Signature: v=1; a=openpgp-sha256; l=11207; i=aliceryhl@google.com; h=from:subject:message-id; bh=F3XfE2qmquOsjgl5Z7SgmdoRF1G91H01/1f0xHWJi/c=; b=owEBbQKS/ZANAwAKAQRYvu5YxjlGAcsmYgBqWM/iVDgYXKSHMFQ2++zmGF5G3oQOlRAGCOgpA M4ZMwY8jLiJAjMEAAEKAB0WIQSDkqKUTWQHCvFIvbIEWL7uWMY5RgUCaljP4gAKCRAEWL7uWMY5 RtEaD/9KW/JA2rWruHWbEO2E5cC9DljvaeFIjSkd91BzBWwESyiGAoZ/GE22uEWmYWwfbD7GLRG 81WKqtnnhjnHUTQwEkvdSEowgyX2UzB9f1u3jdIOqijVamSyr5Uqm7TDjRR1x1qlLcLAfeqPKgQ UAWNWg0LIq9LQJnhy40tqtMpXVKlm9fld9awRpIOE8XanE25h4s7QGop8cVMOk/HeND5oBDi51W vnyArAzzLdmL6Y9aPMoV65o2m2SfP71XsdEuxR4GhY4br8MUPpHSLcXAmlOjw/NTEJ2g1wKsc37 26xjLuj0Opf/eh1KvzyadXWZkKNCt7bo1lyAWB/gDiIKEtus7it/NOkcRay0z9Dl1pYWgaoIpyv d+Odb95KAXwEwOvY3uvSgLB095MS4TJ1m0iZD4gnjPTkfHzwcrdUmwmMuYenCKwqng/CdSlkLan RetTh6ZYwA/Sy09UJLWxzr/6tZLXsFs55AfJBlOvxUvE+9U5DTcnJCpBRBtroAVaDn4RImH+vqP 4gJ7fiMENglhbp5zqQhBe66Gx39PDAliO9WdbEzCmWwFkWgoACiM/xa2RL1TsmXc+ZW0nZDAoIN Qlsx3PNTzD0uJzDCuOpCPIxouCBZcGoNsdVlxpuaVGdn0kyAy44snOUFHe4ufWo6edkQxHAk1wP xaIPtuud6ga9N0w== X-Mailer: b4 0.14.3 Message-ID: <20260716-pr-ratelimited-v2-1-31c27a4543d2@google.com> Subject: [PATCH v2 1/5] rust: sync: move lockdep types to rust/kernel/sync/lockdep.rs From: Alice Ryhl To: Greg Kroah-Hartman , Carlos Llamas , Boqun Feng , Gary Guo Cc: "=?utf-8?q?Onur_=C3=96zkan?=" , Andreas Hindborg , Benno Lossin , "=?utf-8?q?Bj=C3=B6rn_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, Alice Ryhl Content-Type: text/plain; charset="utf-8" The lockdep types are currently stored directly in rust/kernel/sync.rs, but there are starting to be too many of them to keep them in that file. Thus, move them to a submodule. For commonly used lockdep logic it's useful to keep re-exports in kernel::sync, and this also avoids the need to update any users. Reviewed-by: Gary Guo Reviewed-by: Boqun Feng Reviewed-by: Carlos Llamas Signed-off-by: Alice Ryhl --- rust/kernel/sync.rs | 135 +----------------------------------------- rust/kernel/sync/lockdep.rs | 139 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 141 insertions(+), 133 deletions(-) diff --git a/rust/kernel/sync.rs b/rust/kernel/sync.rs index 993dbf2caa0e..e87a7e339994 100644 --- a/rust/kernel/sync.rs +++ b/rust/kernel/sync.rs @@ -5,10 +5,6 @@ //! This module contains the kernel APIs related to synchronisation that have been ported or //! wrapped for usage by Rust code in the kernel. -use crate::prelude::*; -use crate::types::Opaque; -use pin_init; - mod arc; pub mod aref; pub mod atomic; @@ -16,6 +12,7 @@ pub mod completion; mod condvar; pub mod lock; +pub mod lockdep; mod locked_by; pub mod poll; pub mod rcu; @@ -28,135 +25,7 @@ pub use lock::global::{global_lock, GlobalGuard, GlobalLock, GlobalLockBackend, GlobalLockedBy}; pub use lock::mutex::{new_mutex, Mutex, MutexGuard}; pub use lock::spinlock::{new_spinlock, SpinLock, SpinLockGuard}; +pub use lockdep::{static_lock_class, LockClassKey}; pub use locked_by::LockedBy; pub use refcount::Refcount; pub use set_once::SetOnce; - -/// Represents a lockdep class. -/// -/// Wraps the kernel's `struct lock_class_key`. -#[repr(transparent)] -#[pin_data(PinnedDrop)] -pub struct LockClassKey { - #[pin] - inner: Opaque, -} - -// SAFETY: Unregistering a lock class key from a different thread than where it was registered is -// allowed. -unsafe impl Send for LockClassKey {} - -// SAFETY: `bindings::lock_class_key` is designed to be used concurrently from multiple threads and -// provides its own synchronization. -unsafe impl Sync for LockClassKey {} - -impl LockClassKey { - /// Initializes a statically allocated lock class key. - /// - /// This is usually used indirectly through the [`static_lock_class!`] macro. See its - /// documentation for more information. - /// - /// # Safety - /// - /// * Before using the returned value, it must be pinned in a static memory location. - /// * The destructor must never run on the returned `LockClassKey`. - pub const unsafe fn new_static() -> Self { - LockClassKey { - inner: Opaque::uninit(), - } - } - - /// Initializes a dynamically allocated lock class key. - /// - /// In the common case of using a statically allocated lock class key, the - /// [`static_lock_class!`] macro should be used instead. - /// - /// # Examples - /// - /// ``` - /// use kernel::alloc::KBox; - /// use kernel::types::ForeignOwnable; - /// use kernel::sync::{LockClassKey, SpinLock}; - /// use pin_init::stack_pin_init; - /// - /// let key = KBox::pin_init(LockClassKey::new_dynamic(), GFP_KERNEL)?; - /// let key_ptr = key.into_foreign(); - /// - /// { - /// stack_pin_init!(let num: SpinLock = SpinLock::new( - /// 0, - /// c"my_spinlock", - /// // SAFETY: `key_ptr` is returned by the above `into_foreign()`, whose - /// // `from_foreign()` has not yet been called. - /// unsafe { > as ForeignOwnable>::borrow(key_ptr) } - /// )); - /// } - /// - /// // SAFETY: We dropped `num`, the only use of the key, so the result of the previous - /// // `borrow` has also been dropped. Thus, it's safe to use from_foreign. - /// unsafe { drop(> as ForeignOwnable>::from_foreign(key_ptr)) }; - /// # Ok::<(), Error>(()) - /// ``` - pub fn new_dynamic() -> impl PinInit { - pin_init!(Self { - // SAFETY: lockdep_register_key expects an uninitialized block of memory - inner <- Opaque::ffi_init(|slot| unsafe { bindings::lockdep_register_key(slot) }) - }) - } - - /// Returns a raw pointer to the inner C struct. - /// - /// It is up to the caller to use the raw pointer correctly. - pub fn as_ptr(&self) -> *mut bindings::lock_class_key { - self.inner.get() - } -} - -#[pinned_drop] -impl PinnedDrop for LockClassKey { - fn drop(self: Pin<&mut Self>) { - // SAFETY: `self.as_ptr()` was registered with lockdep and `self` is pinned, so the address - // hasn't changed. Thus, it's safe to pass it to unregister. - unsafe { bindings::lockdep_unregister_key(self.as_ptr()) } - } -} - -/// Defines a new static lock class and returns a pointer to it. -/// -/// # Examples -/// -/// ``` -/// use kernel::sync::{static_lock_class, Arc, SpinLock}; -/// -/// fn new_locked_int() -> Result>> { -/// Arc::pin_init(SpinLock::new( -/// 42, -/// c"new_locked_int", -/// static_lock_class!(), -/// ), GFP_KERNEL) -/// } -/// ``` -#[macro_export] -macro_rules! static_lock_class { - () => {{ - static CLASS: $crate::sync::LockClassKey = - // SAFETY: The returned `LockClassKey` is stored in static memory and we pin it. Drop - // never runs on a static global. - unsafe { $crate::sync::LockClassKey::new_static() }; - $crate::prelude::Pin::static_ref(&CLASS) - }}; -} -pub use static_lock_class; - -/// Returns the given string, if one is provided, otherwise generates one based on the source code -/// location. -#[doc(hidden)] -#[macro_export] -macro_rules! optional_name { - () => { - $crate::c_str!(::core::concat!(::core::file!(), ":", ::core::line!())) - }; - ($name:literal) => { - $crate::c_str!($name) - }; -} diff --git a/rust/kernel/sync/lockdep.rs b/rust/kernel/sync/lockdep.rs new file mode 100644 index 000000000000..784821cc2a39 --- /dev/null +++ b/rust/kernel/sync/lockdep.rs @@ -0,0 +1,139 @@ +// SPDX-License-Identifier: GPL-2.0 + +//! Utilities related to lockdep. +//! +//! C headers: [`include/linux/lockdep.h`](srctree/include/linux/lockdep.h) + +use crate::{ + prelude::*, + types::Opaque, // +}; + +/// Represents a lockdep class. +/// +/// Wraps the kernel's `struct lock_class_key`. +#[repr(transparent)] +#[pin_data(PinnedDrop)] +pub struct LockClassKey { + #[pin] + inner: Opaque, +} + +// SAFETY: Unregistering a lock class key from a different thread than where it was registered is +// allowed. +unsafe impl Send for LockClassKey {} + +// SAFETY: `bindings::lock_class_key` is designed to be used concurrently from multiple threads and +// provides its own synchronization. +unsafe impl Sync for LockClassKey {} + +impl LockClassKey { + /// Initializes a statically allocated lock class key. + /// + /// This is usually used indirectly through the [`static_lock_class!`] macro. See its + /// documentation for more information. + /// + /// # Safety + /// + /// * Before using the returned value, it must be pinned in a static memory location. + /// * The destructor must never run on the returned `LockClassKey`. + pub const unsafe fn new_static() -> Self { + LockClassKey { + inner: Opaque::uninit(), + } + } + + /// Initializes a dynamically allocated lock class key. + /// + /// In the common case of using a statically allocated lock class key, the + /// [`static_lock_class!`] macro should be used instead. + /// + /// # Examples + /// + /// ``` + /// use kernel::alloc::KBox; + /// use kernel::types::ForeignOwnable; + /// use kernel::sync::{LockClassKey, SpinLock}; + /// use pin_init::stack_pin_init; + /// + /// let key = KBox::pin_init(LockClassKey::new_dynamic(), GFP_KERNEL)?; + /// let key_ptr = key.into_foreign(); + /// + /// { + /// stack_pin_init!(let num: SpinLock = SpinLock::new( + /// 0, + /// c"my_spinlock", + /// // SAFETY: `key_ptr` is returned by the above `into_foreign()`, whose + /// // `from_foreign()` has not yet been called. + /// unsafe { > as ForeignOwnable>::borrow(key_ptr) } + /// )); + /// } + /// + /// // SAFETY: We dropped `num`, the only use of the key, so the result of the previous + /// // `borrow` has also been dropped. Thus, it's safe to use from_foreign. + /// unsafe { drop(> as ForeignOwnable>::from_foreign(key_ptr)) }; + /// # Ok::<(), Error>(()) + /// ``` + pub fn new_dynamic() -> impl PinInit { + pin_init!(Self { + // SAFETY: lockdep_register_key expects an uninitialized block of memory + inner <- Opaque::ffi_init(|slot| unsafe { bindings::lockdep_register_key(slot) }) + }) + } + + /// Returns a raw pointer to the inner C struct. + /// + /// It is up to the caller to use the raw pointer correctly. + pub fn as_ptr(&self) -> *mut bindings::lock_class_key { + self.inner.get() + } +} + +#[pinned_drop] +impl PinnedDrop for LockClassKey { + fn drop(self: Pin<&mut Self>) { + // SAFETY: `self.as_ptr()` was registered with lockdep and `self` is pinned, so the address + // hasn't changed. Thus, it's safe to pass it to unregister. + unsafe { bindings::lockdep_unregister_key(self.as_ptr()) } + } +} + +/// Defines a new static lock class and returns a pointer to it. +/// +/// # Examples +/// +/// ``` +/// use kernel::sync::{static_lock_class, Arc, SpinLock}; +/// +/// fn new_locked_int() -> Result>> { +/// Arc::pin_init(SpinLock::new( +/// 42, +/// c"new_locked_int", +/// static_lock_class!(), +/// ), GFP_KERNEL) +/// } +/// ``` +#[macro_export] +macro_rules! static_lock_class { + () => {{ + static CLASS: $crate::sync::LockClassKey = + // SAFETY: The returned `LockClassKey` is stored in static memory and we pin it. Drop + // never runs on a static global. + unsafe { $crate::sync::LockClassKey::new_static() }; + $crate::prelude::Pin::static_ref(&CLASS) + }}; +} +pub use static_lock_class; + +/// Returns the given string, if one is provided, otherwise generates one based on the source code +/// location. +#[doc(hidden)] +#[macro_export] +macro_rules! optional_name { + () => { + $crate::c_str!(::core::concat!(::core::file!(), ":", ::core::line!())) + }; + ($name:literal) => { + $crate::c_str!($name) + }; +} -- 2.55.0.229.g6434b31f56-goog