Skip to content

Commit c782b7d

Browse files
hoshinolinajannau
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 <lina@asahilina.net>
1 parent 00d5b9d commit c782b7d

3 files changed

Lines changed: 56 additions & 23 deletions

File tree

rust/kernel/sync.rs

Lines changed: 10 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -5,41 +5,28 @@
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
pub mod rcu;
1614

15+
#[cfg(CONFIG_LOCKDEP)]
16+
mod lockdep;
17+
#[cfg(not(CONFIG_LOCKDEP))]
18+
mod no_lockdep;
19+
#[cfg(not(CONFIG_LOCKDEP))]
20+
use no_lockdep as lockdep;
21+
1722
pub use arc::{Arc, ArcBorrow, UniqueArc};
1823
pub use condvar::{new_condvar, CondVar, CondVarTimeoutResult};
1924
pub use lock::global::{global_lock, GlobalGuard, GlobalLock, GlobalLockBackend, GlobalLockedBy};
20-
pub use lock::mutex::{new_mutex, Mutex, MutexGuard};
21-
pub use lock::spinlock::{new_spinlock, SpinLock, SpinLockGuard};
25+
pub use lock::mutex::{new_mutex, Mutex};
26+
pub use lock::spinlock::{new_spinlock, SpinLock};
27+
pub use lockdep::LockClassKey;
2228
pub use locked_by::LockedBy;
2329

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

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)