Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

rust: sync: Refactor static_lock_class!() macro

By introducing a new_static() constructor, the macro does not need to go
through MaybeUninit::uninit().assume_init(), which is a pattern that is
best avoided when possible.

The safety comment not only requires that the value is leaked, but also
that it is stored in the right portion of memory. This is so that the
lockdep static_obj() check will succeed when using this constructor. One
could argue that lockdep detects this scenario, so that safety
requirement isn't needed. However, it simplifies matters to require that
static_obj() will succeed and it's not a burdensome requirement on the
caller.

Suggested-by: Benno Lossin <lossin@kernel.org>
Reviewed-by: Daniel Almeida <daniel.almeida@collabora.com>
Reviewed-by: Benno Lossin <lossin@kernel.org>
Signed-off-by: Alice Ryhl <aliceryhl@google.com>
Signed-off-by: Boqun Feng <boqun.feng@gmail.com>
Link: https://patch.msgid.link/20250811-lock-class-key-cleanup-v3-1-b12967ee1ca2@google.com

authored by

Alice Ryhl and committed by
Boqun Feng
86f4a271 a45026ce

+18 -6
+18 -6
rust/kernel/sync.rs
··· 45 45 unsafe impl Sync for LockClassKey {} 46 46 47 47 impl LockClassKey { 48 + /// Initializes a statically allocated lock class key. 49 + /// 50 + /// This is usually used indirectly through the [`static_lock_class!`] macro. 51 + /// 52 + /// # Safety 53 + /// 54 + /// * Before using the returned value, it must be pinned in a static memory location. 55 + /// * The destructor must never run on the returned `LockClassKey`. 56 + #[doc(hidden)] 57 + pub const unsafe fn new_static() -> Self { 58 + LockClassKey { 59 + inner: Opaque::uninit(), 60 + } 61 + } 62 + 48 63 /// Initializes a dynamically allocated lock class key. In the common case of using a 49 64 /// statically allocated lock class key, the static_lock_class! macro should be used instead. 50 65 /// ··· 116 101 macro_rules! static_lock_class { 117 102 () => {{ 118 103 static CLASS: $crate::sync::LockClassKey = 119 - // Lockdep expects uninitialized memory when it's handed a statically allocated `struct 120 - // lock_class_key`. 121 - // 122 - // SAFETY: `LockClassKey` transparently wraps `Opaque` which permits uninitialized 123 - // memory. 124 - unsafe { ::core::mem::MaybeUninit::uninit().assume_init() }; 104 + // SAFETY: The returned `LockClassKey` is stored in static memory and we pin it. Drop 105 + // never runs on a static global. 106 + unsafe { $crate::sync::LockClassKey::new_static() }; 125 107 $crate::prelude::Pin::static_ref(&CLASS) 126 108 }}; 127 109 }