From a8553d87d15bdc58c5d5599cda5d9a1fb2a2a486 Mon Sep 17 00:00:00 2001 From: Sven Van Asbroeck Date: Fri, 4 Jun 2021 12:54:48 -0400 Subject: [PATCH] rust/kernel: remove config `#ifdef` in mutex.rs file The use of `#ifdef CONFIG_` statements in .c/.rs files should be avoided: it makes the code much more unmaintainable over time. See: https://lore.kernel.org/lkml/YLjWKwhp7akqyR1S@kroah.com/ Use a Rust-C helper instead to leverage automatic `CONFIG` selection in C kernel headers. Signed-off-by: Sven Van Asbroeck --- rust/helpers.c | 7 +++++++ rust/kernel/sync/mutex.rs | 15 +++++++-------- 2 files changed, 14 insertions(+), 8 deletions(-) diff --git a/rust/helpers.c b/rust/helpers.c index 3526129f41c79a..cf4ddd705f222c 100644 --- a/rust/helpers.c +++ b/rust/helpers.c @@ -8,6 +8,7 @@ #include #include #include +#include void rust_helper_BUG(void) { @@ -123,6 +124,12 @@ const char *rust_helper_errname(int err) return errname(err); } +void rust_helper_mutex_lock(struct mutex *lock) +{ + mutex_lock(lock); +} +EXPORT_SYMBOL_GPL(rust_helper_mutex_lock); + /* We use bindgen's --size_t-is-usize option to bind the C size_t type * as the Rust usize type, so we can use it in contexts where Rust * expects a usize like slice (array) indices. usize is defined to be diff --git a/rust/kernel/sync/mutex.rs b/rust/kernel/sync/mutex.rs index 0a4371a7db6c74..74654631c8ce1b 100644 --- a/rust/kernel/sync/mutex.rs +++ b/rust/kernel/sync/mutex.rs @@ -77,19 +77,18 @@ impl NeedsLockClass for Mutex { } } +extern "C" { + fn rust_helper_mutex_lock(mutex: *mut bindings::mutex); +} + impl Lock for Mutex { type Inner = T; - #[cfg(not(CONFIG_DEBUG_LOCK_ALLOC))] fn lock_noguard(&self) { // SAFETY: `mutex` points to valid memory. - unsafe { bindings::mutex_lock(self.mutex.get()) }; - } - - #[cfg(CONFIG_DEBUG_LOCK_ALLOC)] - fn lock_noguard(&self) { - // SAFETY: `mutex` points to valid memory. - unsafe { bindings::mutex_lock_nested(self.mutex.get(), 0) }; + unsafe { + rust_helper_mutex_lock(self.mutex.get()); + } } unsafe fn unlock(&self) {