Re: [PATCH RFC 05/11] rust: sync: Add dummy LockClassKey implementation for !CONFIG_LOCKDEP

From: Martin Rodriguez Reboredo
Date: Fri Jul 14 2023 - 16:38:08 EST


On 7/14/23 06:13, Asahi Lina wrote:
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@xxxxxxxxxxxxx>
---
rust/kernel/sync.rs | 29 ++++++++---------------------
rust/kernel/sync/lockdep.rs | 27 +++++++++++++++++++++++++++
rust/kernel/sync/no_lockdep.rs | 19 +++++++++++++++++++
3 files changed, 54 insertions(+), 21 deletions(-)

diff --git a/rust/kernel/sync.rs b/rust/kernel/sync.rs
index d219ee518eff..352472c6b77a 100644
--- a/rust/kernel/sync.rs
+++ b/rust/kernel/sync.rs
@@ -5,37 +5,24 @@
//! 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::types::Opaque;
-
mod arc;
mod condvar;
pub mod lock;
mod locked_by;
+#[cfg(CONFIG_LOCKDEP)]
+mod lockdep;
+#[cfg(not(CONFIG_LOCKDEP))]
+mod no_lockdep;

Some care must be taken wrt how is it going to appear in the resulting
documentation, I think it can lead to missing details. `#[cfg(doc)]`
should be considered.

+#[cfg(not(CONFIG_LOCKDEP))]
+use no_lockdep as lockdep;
[...]