linux/rust
Dirk Behme d065cc7605 rust: mutex: fix __mutex_init() usage in case of PREEMPT_RT
In case CONFIG_PREEMPT_RT is enabled __mutex_init() becomes a macro
instead of an extern function (simplified from
include/linux/mutex.h):

    #ifndef CONFIG_PREEMPT_RT
    extern void __mutex_init(struct mutex *lock, const char *name,
    			 struct lock_class_key *key);
    #else
    #define __mutex_init(mutex, name, key)		\
    do {						\
	rt_mutex_base_init(&(mutex)->rtmutex);		\
    	__mutex_rt_init((mutex), name, key);		\
    } while (0)
    #endif

The macro isn't resolved by bindgen, then. What results in a build
error:

error[E0425]: cannot find function `__mutex_init` in crate `bindings`
     --> rust/kernel/sync/lock/mutex.rs:104:28
      |
104   |           unsafe { bindings::__mutex_init(ptr, name, key) }
      |                              ^^^^^^^^^^^^ help: a function with a similar name exists: `__mutex_rt_init`
      |
     ::: rust/bindings/bindings_generated.rs:23722:5
      |
23722 | /     pub fn __mutex_rt_init(
23723 | |         lock: *mut mutex,
23724 | |         name: *const core::ffi::c_char,
23725 | |         key: *mut lock_class_key,
23726 | |     );
      | |_____- similarly named function `__mutex_rt_init` defined here

Fix this by adding a helper.

As explained by Gary Guo in [1] no #ifdef CONFIG_PREEMPT_RT
is needed here as rust/bindings/lib.rs prefers externed function to
helpers if an externed function exists.

Reported-by: Conor Dooley <conor@kernel.org>
Link: https://lore.kernel.org/rust-for-linux/20240913-shack-estate-b376a65921b1@spud/
Link: https://lore.kernel.org/rust-for-linux/20240915123626.1a170103.gary@garyguo.net/ [1]
Fixes: 6d20d629c6 ("rust: lock: introduce `Mutex`")
Signed-off-by: Dirk Behme <dirk.behme@de.bosch.com>
Tested-by: Conor Dooley <conor.dooley@microchip.com>
Reviewed-by: Gary Guo <gary@garyguo.net>
Link: https://lore.kernel.org/r/20240916073752.3123484-1-dirk.behme@de.bosch.com
[ Reworded to include the proper example by Dirk. - Miguel ]
Signed-off-by: Miguel Ojeda <ojeda@kernel.org>
2024-09-26 21:22:15 +02:00
..
bindings rust: sort blk includes in bindings_helper.h 2024-08-21 00:37:02 +02:00
helpers rust: mutex: fix __mutex_init() usage in case of PREEMPT_RT 2024-09-26 21:22:15 +02:00
kernel rust: sync: require T: Sync for LockedBy::access 2024-09-26 17:51:49 +02:00
macros Rust changes for v6.12 2024-09-25 10:25:40 -07:00
uapi rust: net::phy unified read/write API for C22 and C45 registers 2024-08-30 10:27:35 +01:00
.gitignore rust: support running Rust documentation tests as KUnit ones 2023-07-19 09:32:53 -06:00
bindgen_parameters rust: fix ARCH_SLAB_MINALIGN multiple definition error 2024-09-26 17:52:35 +02:00
build_error.rs rust: add build_error crate 2022-12-04 01:59:16 +01:00
compiler_builtins.rs rust: add intrinsics to fix -Os builds 2024-08-10 00:05:10 +02:00
exports.c rust: kbuild: auto generate helper exports 2024-08-19 11:09:02 +02:00
Makefile Rust changes for v6.12 2024-09-25 10:25:40 -07:00