Skip to content

Commit f880ea1

Browse files
hoshinolinajannau
authored andcommitted
*RFL import: kernel::KParamGuard & friends
Commit reference: 3dfc5eb
1 parent 03d6fef commit f880ea1

1 file changed

Lines changed: 37 additions & 0 deletions

File tree

rust/kernel/lib.rs

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,43 @@ impl ThisModule {
177177
pub const fn as_ptr(&self) -> *mut bindings::module {
178178
self.0
179179
}
180+
181+
/// Locks the module parameters to access them.
182+
///
183+
/// Returns a [`KParamGuard`] that will release the lock when dropped.
184+
pub fn kernel_param_lock(&self) -> KParamGuard<'_> {
185+
// SAFETY: `kernel_param_lock` will check if the pointer is null and
186+
// use the built-in mutex in that case.
187+
#[cfg(CONFIG_SYSFS)]
188+
unsafe {
189+
bindings::kernel_param_lock(self.0)
190+
}
191+
192+
KParamGuard {
193+
#[cfg(CONFIG_SYSFS)]
194+
this_module: self,
195+
phantom: core::marker::PhantomData,
196+
}
197+
}
198+
}
199+
200+
/// Scoped lock on the kernel parameters of [`ThisModule`].
201+
///
202+
/// Lock will be released when this struct is dropped.
203+
pub struct KParamGuard<'a> {
204+
#[cfg(CONFIG_SYSFS)]
205+
this_module: &'a ThisModule,
206+
phantom: core::marker::PhantomData<&'a ()>,
207+
}
208+
209+
#[cfg(CONFIG_SYSFS)]
210+
impl<'a> Drop for KParamGuard<'a> {
211+
fn drop(&mut self) {
212+
// SAFETY: `kernel_param_lock` will check if the pointer is null and
213+
// use the built-in mutex in that case. The existence of `self`
214+
// guarantees that the lock is held.
215+
unsafe { bindings::kernel_param_unlock(self.this_module.0) }
216+
}
180217
}
181218

182219
#[cfg(not(any(testlib, test)))]

0 commit comments

Comments
 (0)