Re: [PATCH] rust: sync: export lock::do_unlocked
From: Alice Ryhl
Date: Mon Feb 16 2026 - 03:54:14 EST
On Mon, Feb 16, 2026 at 08:14:42AM +0100, Andreas Hindborg wrote:
> "Benno Lossin" <lossin@xxxxxxxxxx> writes:
>
> > On Sun Feb 15, 2026 at 9:15 PM CET, Andreas Hindborg wrote:
> >> Export lock::do_unlocked publicly. Add documentation for the method.
> >>
> >> Signed-off-by: Andreas Hindborg <a.hindborg@xxxxxxxxxx>
> >
> > Reviewed-by: Benno Lossin <lossin@xxxxxxxxxx>
> >
> >> ---
> >> rust/kernel/sync/lock.rs | 26 +++++++++++++++++++++++++-
> >> 1 file changed, 25 insertions(+), 1 deletion(-)
> >>
> >> diff --git a/rust/kernel/sync/lock.rs b/rust/kernel/sync/lock.rs
> >> index 46a57d1fc309d..f390cd7ba0762 100644
> >> --- a/rust/kernel/sync/lock.rs
> >> +++ b/rust/kernel/sync/lock.rs
> >> @@ -235,7 +235,31 @@ pub fn lock_ref(&self) -> &'a Lock<T, B> {
> >> self.lock
> >> }
> >>
> >> - pub(crate) fn do_unlocked<U>(&mut self, cb: impl FnOnce() -> U) -> U {
> >> + /// Temporarily unlock the lock to execute the given closure.
> >> + ///
> >> + /// This method unlocks the lock before calling the closure `cb`, and re-locks it afterwards.
> >> + /// This is useful when you need to perform operations that are not allowed while holding
> >> + /// certain locks, such as allocating memory (which is prohibited while holding a spinlock).
> >> + ///
> >> + /// # Examples
> >> + ///
> >> + /// ```
> >> + /// # use kernel::{new_spinlock, prelude::*};
> >> + /// # use pin_init::stack_pin_init;
> >> + ///
This will show up as a spurious newline in docs. I would just not hide
the imports, but if you do, then don't have a newline after them.
> >> + /// stack_pin_init!{
> >> + /// let lock = new_spinlock!(())
> >> + /// }
> >> + ///
> >> + /// let mut guard = lock.lock();
> >> + /// let mut buffer = KVec::new();
> >> + /// // Temporarily unlock to allocate memory, which should not be done while holding a spinlock.
> >> + /// guard.do_unlocked(|| {
> > ^
> > Spurious space.
>
> Thanks. Code formatting in comments is a problem in my setup. `rustfmt`
> is not much help.
Unfortunately there's not much helper to get there
Alice