Skip to content

Commit 31b295a

Browse files
asahilinaFabo
authored andcommitted
rust: sync: Add dummy LockClassKey implementation for !CONFIG_LOCKDEP
Lock classes aren't used without lockdep. The C side declares the key as an empty struct in that case, but let's make it an explicit ZST in Rust, implemented in a separate module. This allows us to more easily guarantee that the lockdep code will be trivially optimized out without CONFIG_LOCKDEP, including LockClassKey arguments that are passed around. Depending on whether CONFIG_LOCKDEP is enabled or not, we then import the real lockdep implementation or the dummy one. Signed-off-by: Asahi Lina <[email protected]> (cherry picked from commit 3d8fea8) Signed-off-by: Fabien Parent <[email protected]>
1 parent c69fc2d commit 31b295a

File tree

3 files changed

+54
-21
lines changed

3 files changed

+54
-21
lines changed

rust/kernel/sync.rs

Lines changed: 8 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -5,39 +5,26 @@
55
//! This module contains the kernel APIs related to synchronisation that have been ported or
66
//! wrapped for usage by Rust code in the kernel.
77
8-
use crate::types::Opaque;
9-
108
mod arc;
119
mod condvar;
1210
pub mod lock;
1311
mod locked_by;
1412
pub mod poll;
1513

14+
#[cfg(CONFIG_LOCKDEP)]
15+
mod lockdep;
16+
#[cfg(not(CONFIG_LOCKDEP))]
17+
mod no_lockdep;
18+
#[cfg(not(CONFIG_LOCKDEP))]
19+
use no_lockdep as lockdep;
20+
1621
pub use arc::{Arc, ArcBorrow, UniqueArc};
1722
pub use condvar::{new_condvar, CondVar, CondVarTimeoutResult};
1823
pub use lock::mutex::{new_mutex, Mutex};
1924
pub use lock::spinlock::{new_spinlock, SpinLock};
25+
pub use lockdep::LockClassKey;
2026
pub use locked_by::LockedBy;
2127

22-
/// Represents a lockdep class. It's a wrapper around C's `lock_class_key`.
23-
#[repr(transparent)]
24-
pub struct LockClassKey(Opaque<bindings::lock_class_key>);
25-
26-
// SAFETY: `bindings::lock_class_key` is designed to be used concurrently from multiple threads and
27-
// provides its own synchronization.
28-
unsafe impl Sync for LockClassKey {}
29-
30-
impl LockClassKey {
31-
/// Creates a new lock class key.
32-
pub const fn new() -> Self {
33-
Self(Opaque::uninit())
34-
}
35-
36-
pub(crate) fn as_ptr(&self) -> *mut bindings::lock_class_key {
37-
self.0.get()
38-
}
39-
}
40-
4128
/// Defines a new static lock class and returns a pointer to it.
4229
#[doc(hidden)]
4330
#[macro_export]

rust/kernel/sync/lockdep.rs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
3+
//! Lockdep utilities.
4+
//!
5+
//! This module abstracts the parts of the kernel lockdep API relevant to Rust
6+
//! modules, including lock classes.
7+
8+
use crate::types::Opaque;
9+
10+
/// Represents a lockdep class. It's a wrapper around C's `lock_class_key`.
11+
#[repr(transparent)]
12+
pub struct LockClassKey(Opaque<bindings::lock_class_key>);
13+
14+
impl LockClassKey {
15+
/// Creates a new lock class key.
16+
pub const fn new() -> Self {
17+
Self(Opaque::uninit())
18+
}
19+
20+
pub(crate) fn as_ptr(&self) -> *mut bindings::lock_class_key {
21+
self.0.get()
22+
}
23+
}
24+
25+
// SAFETY: `bindings::lock_class_key` is designed to be used concurrently from multiple threads and
26+
// provides its own synchronization.
27+
unsafe impl Sync for LockClassKey {}

rust/kernel/sync/no_lockdep.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
3+
//! Dummy lockdep utilities.
4+
//!
5+
//! Takes the place of the `lockdep` module when lockdep is disabled.
6+
7+
/// A dummy, zero-sized lock class.
8+
pub struct LockClassKey();
9+
10+
impl LockClassKey {
11+
/// Creates a new dummy lock class key.
12+
pub const fn new() -> Self {
13+
Self()
14+
}
15+
16+
pub(crate) fn as_ptr(&self) -> *mut bindings::lock_class_key {
17+
core::ptr::null_mut()
18+
}
19+
}

0 commit comments

Comments
 (0)