Skip to content

Commit

Permalink
rust: lockdep: Remove support for dynamically allocated LockClassKeys
Browse files Browse the repository at this point in the history
Currently, dynamically allocated LockCLassKeys can be used from the Rust
side without having them registered. This is a soundness issue, so
remove them.

Suggested-by: Alice Ryhl <aliceryhl@google.com>
Link: https://lore.kernel.org/rust-for-linux/20240815074519.2684107-3-nmi@metaspace.dk/
Cc: stable@vger.kernel.org
Signed-off-by: Mitchell Levy <levymitchell0@gmail.com>
  • Loading branch information
chessturo authored and intel-lab-lkp committed Feb 5, 2025
1 parent ceff075 commit 8f0664d
Showing 1 changed file with 4 additions and 12 deletions.
16 changes: 4 additions & 12 deletions rust/kernel/sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,28 +29,20 @@ pub struct LockClassKey(Opaque<bindings::lock_class_key>);
unsafe impl Sync for LockClassKey {}

impl LockClassKey {
/// Creates a new lock class key.
pub const fn new() -> Self {
Self(Opaque::uninit())
}

pub(crate) fn as_ptr(&self) -> *mut bindings::lock_class_key {
self.0.get()
}
}

impl Default for LockClassKey {
fn default() -> Self {
Self::new()
}
}

/// Defines a new static lock class and returns a pointer to it.
#[doc(hidden)]
#[macro_export]
macro_rules! static_lock_class {
() => {{
static CLASS: $crate::sync::LockClassKey = $crate::sync::LockClassKey::new();
static CLASS: $crate::sync::LockClassKey =
// SAFETY: lockdep expects uninitialized memory when it's handed a statically allocated
// lock_class_key
unsafe { ::core::mem::MaybeUninit::uninit().assume_init() };
&CLASS
}};
}
Expand Down

0 comments on commit 8f0664d

Please sign in to comment.