File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff 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) ) ) ]
You can’t perform that action at this time.
0 commit comments