Skip to content

Commit 33ef1b8

Browse files
committed
Added ffi_initX used to create initializers from C functions
1 parent c8764f3 commit 33ef1b8

File tree

7 files changed

+90
-81
lines changed

7 files changed

+90
-81
lines changed

rust/kernel/init.rs

+1
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ use core::{
117117

118118
#[doc(hidden)]
119119
pub mod __private;
120+
pub mod common;
120121
mod pin_project;
121122
mod pinned_drop;
122123

rust/kernel/init/common.rs

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
3+
//! Module containing common kernel initializer functions.
4+
5+
use crate::{
6+
init::{self, PinInit},
7+
Opaque,
8+
};
9+
10+
macro_rules! create_func {
11+
($name:ident $(, $arg_name:ident: $arg_typ:ident)*) => {
12+
/// Create an initializer using the given initalizer function from C.
13+
///
14+
/// # Safety
15+
/// The given function **must** under all circumstances initialize the memory location to a valid
16+
/// `T`. If it fails to do so it results in UB.
17+
///
18+
/// If any parameters are given, those need to be valid for the function. Valid means that
19+
/// calling the function with those parameters complies with the above requirement **and** every
20+
/// other requirement on the function itself.
21+
pub unsafe fn $name<T $(, $arg_typ)*>(init_func: unsafe extern "C" fn(*mut T $(, $arg_name: $arg_typ)*) $(, $arg_name: $arg_typ)*) -> impl PinInit<Opaque<T>> {
22+
unsafe {
23+
init::pin_init_from_closure(move |slot| {
24+
init_func(Opaque::raw_get(slot) $(, $arg_name)*);
25+
Ok(())
26+
})
27+
}
28+
}
29+
}
30+
}
31+
32+
create_func!(ffi_init);
33+
create_func!(ffi_init1, arg1: A1);
34+
create_func!(ffi_init2, arg1: A1, arg2: A2);
35+
create_func!(ffi_init3, arg1: A1, arg2: A2, arg3: A3);
36+
create_func!(ffi_init4, arg1: A1, arg2: A2, arg3: A3, arg4: A4);

rust/kernel/sync/condvar.rs

+8-15
Original file line numberDiff line numberDiff line change
@@ -60,23 +60,16 @@ impl CondVar {
6060
/// Constructs a new conditional variable.
6161
#[allow(clippy::new_ret_no_self)]
6262
pub const fn new(name: &'static CStr, key: &'static LockClassKey) -> impl PinInit<Self> {
63-
fn init_wait_list(
64-
name: &'static CStr,
65-
key: &'static LockClassKey,
66-
) -> impl PinInit<Opaque<bindings::wait_queue_head>> {
67-
let init = move |place: *mut Opaque<bindings::wait_queue_head>| unsafe {
68-
bindings::__init_waitqueue_head(
69-
Opaque::raw_get(place),
63+
pin_init!(Self {
64+
// SAFETY: __init_waitqueue_head is an init function and name and key are valid
65+
// parameters
66+
wait_list: unsafe {
67+
init::common::ffi_init2(
68+
bindings::__init_waitqueue_head,
7069
name.as_char_ptr(),
7170
key.get(),
72-
);
73-
Ok(())
74-
};
75-
// SAFETY: waitqueue has been initialized
76-
unsafe { init::pin_init_from_closure(init) }
77-
}
78-
pin_init!(Self {
79-
wait_list: init_wait_list(name, key),
71+
)
72+
},
8073
_pin: PhantomPinned,
8174
})
8275
}

rust/kernel/sync/mutex.rs

+9-12
Original file line numberDiff line numberDiff line change
@@ -81,19 +81,16 @@ unsafe impl<T> PinInit<Mutex<T>> for MutexInit<T> {
8181
self,
8282
slot: *mut Mutex<T>,
8383
) -> core::result::Result<(), core::convert::Infallible> {
84-
fn init_mutex(
85-
name: &'static CStr,
86-
key: &'static LockClassKey,
87-
) -> impl PinInit<Opaque<bindings::mutex>> {
88-
let init = move |slot: *mut Opaque<bindings::mutex>| unsafe {
89-
bindings::__mutex_init(Opaque::raw_get(slot), name.as_char_ptr(), key.get());
90-
Ok(())
91-
};
92-
// SAFETY: mutex has been initialized
93-
unsafe { init::pin_init_from_closure(init) }
94-
}
9584
let init = pin_init!(Mutex<T> {
96-
mutex: init_mutex(self.name, self.key),
85+
// SAFETY: __mutex_init is an initializer function and name and key are valid
86+
// parameters.
87+
mutex: unsafe {
88+
init::common::ffi_init2(
89+
bindings::__mutex_init,
90+
self.name.as_char_ptr(),
91+
self.key.get(),
92+
)
93+
},
9794
data: UnsafeCell::new(self.data),
9895
_pin: PhantomPinned,
9996
});

rust/kernel/sync/rwsem.rs

+9-11
Original file line numberDiff line numberDiff line change
@@ -84,18 +84,16 @@ unsafe impl<T> PinInit<RwSemaphore<T>> for Init<T> {
8484
self,
8585
slot: *mut RwSemaphore<T>,
8686
) -> core::result::Result<(), core::convert::Infallible> {
87-
fn init_rw_semaphore(
88-
name: &'static CStr,
89-
key: &'static LockClassKey,
90-
) -> impl PinInit<Opaque<bindings::rw_semaphore>> {
91-
let init = move |slot: *mut Opaque<bindings::rw_semaphore>| unsafe {
92-
bindings::__init_rwsem(Opaque::raw_get(slot), name.as_char_ptr(), key.get());
93-
Ok(())
94-
};
95-
unsafe { init::pin_init_from_closure(init) }
96-
}
9787
let init = pin_init!(RwSemaphore<T> {
98-
rwsem: init_rw_semaphore(self.name, self.key),
88+
// SAFETY: __init_rwsem is an initializer function and name and key are valid
89+
// parameters.
90+
rwsem: unsafe {
91+
init::common::ffi_init2(
92+
bindings::__init_rwsem,
93+
self.name.as_char_ptr(),
94+
self.key.get(),
95+
)
96+
},
9997
data: UnsafeCell::new(self.data),
10098
_pin: PhantomPinned,
10199
});

rust/kernel/sync/seqlock.rs

+9-17
Original file line numberDiff line numberDiff line change
@@ -94,25 +94,17 @@ impl<L: Lock> SeqLock<L> {
9494
L::Inner: Sized,
9595
<L as LockFactory>::Error: From<core::convert::Infallible>,
9696
{
97-
fn init_count(
98-
name: &'static CStr,
99-
key2: &'static LockClassKey,
100-
) -> impl PinInit<Opaque<bindings::seqcount>> {
101-
let init = move |place: *mut Opaque<bindings::seqcount>| {
102-
unsafe {
103-
bindings::__seqcount_init(
104-
Opaque::raw_get(place),
105-
name.as_char_ptr(),
106-
key2.get(),
107-
)
108-
};
109-
Ok(())
110-
};
111-
unsafe { init::pin_init_from_closure(init) }
112-
}
11397
try_pin_init!(Self {
11498
_p: PhantomPinned,
115-
count: init_count(name, key2),
99+
// SAFETY: __seqcount_init is an initializer function and name and key are valid
100+
// parameters.
101+
count: unsafe {
102+
init::common::ffi_init2(
103+
bindings::__seqcount_init,
104+
name.as_char_ptr(),
105+
key2.get()
106+
)
107+
},
116108
write_lock: L::new_lock(data, name, key1),
117109
}? L::Error)
118110
}

rust/kernel/sync/spinlock.rs

+18-26
Original file line numberDiff line numberDiff line change
@@ -126,18 +126,16 @@ unsafe impl<T> PinInit<SpinLock<T>> for Init<T> {
126126
self,
127127
slot: *mut SpinLock<T>,
128128
) -> core::result::Result<(), core::convert::Infallible> {
129-
fn init_spinlock(
130-
name: &'static CStr,
131-
key: &'static LockClassKey,
132-
) -> impl PinInit<Opaque<bindings::spinlock>> {
133-
let init = move |slot: *mut Opaque<bindings::spinlock>| unsafe {
134-
bindings::__spin_lock_init(Opaque::raw_get(slot), name.as_char_ptr(), key.get());
135-
Ok(())
136-
};
137-
unsafe { init::pin_init_from_closure(init) }
138-
}
139129
let init = pin_init!(SpinLock<T> {
140-
spin_lock: init_spinlock(self.name, self.key),
130+
// SAFETY: __spin_lock_init is an initializer function and name and key are valid
131+
// parameters.
132+
spin_lock: unsafe {
133+
init::common::ffi_init2(
134+
bindings::__spin_lock_init,
135+
self.name.as_char_ptr(),
136+
self.key.get(),
137+
)
138+
},
141139
data: UnsafeCell::new(self.data),
142140
_pin: PhantomPinned,
143141
});
@@ -317,22 +315,16 @@ unsafe impl<T> PinInit<RawSpinLock<T>> for RInit<T> {
317315
self,
318316
slot: *mut RawSpinLock<T>,
319317
) -> core::result::Result<(), core::convert::Infallible> {
320-
fn init_spinlock(
321-
name: &'static CStr,
322-
key: &'static LockClassKey,
323-
) -> impl PinInit<Opaque<bindings::raw_spinlock>> {
324-
let init = move |place: *mut Opaque<bindings::raw_spinlock>| unsafe {
325-
bindings::_raw_spin_lock_init(
326-
Opaque::raw_get(place),
327-
name.as_char_ptr(),
328-
key.get(),
329-
);
330-
Ok(())
331-
};
332-
unsafe { init::pin_init_from_closure(init) }
333-
}
334318
let init = pin_init!(RawSpinLock<T> {
335-
spin_lock: init_spinlock(self.name, self.key),
319+
// SAFETY: _raw_spin_lock_init is an initializer function and name and key are valid
320+
// parameters.
321+
spin_lock: unsafe {
322+
init::common::ffi_init2(
323+
bindings::_raw_spin_lock_init,
324+
self.name.as_char_ptr(),
325+
self.key.get(),
326+
)
327+
},
336328
data: UnsafeCell::new(self.data),
337329
_pin: PhantomPinned,
338330
});

0 commit comments

Comments
 (0)