public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH] rust: sync: export lock::do_unlocked
@ 2026-02-15 20:15 Andreas Hindborg
  2026-02-15 23:24 ` Benno Lossin
  0 siblings, 1 reply; 4+ messages in thread
From: Andreas Hindborg @ 2026-02-15 20:15 UTC (permalink / raw)
  To: Peter Zijlstra, Ingo Molnar, Will Deacon, Boqun Feng, Waiman Long,
	Miguel Ojeda, Gary Guo, Björn Roy Baron, Benno Lossin,
	Alice Ryhl, Trevor Gross, Danilo Krummrich
  Cc: linux-kernel, rust-for-linux, Andreas Hindborg

Export lock::do_unlocked publicly. Add documentation for the method.

Signed-off-by: Andreas Hindborg <a.hindborg@kernel.org>
---
 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;
+    ///
+    /// 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(|| {
+    ///     buffer.push(5u32, GFP_KERNEL)
+    /// })?;
+    /// # Ok::<(), Error>(())
+    /// ```
+    pub fn do_unlocked<U>(&mut self, cb: impl FnOnce() -> U) -> U {
         // SAFETY: The caller owns the lock, so it is safe to unlock it.
         unsafe { B::unlock(self.lock.state.get(), &self.state) };
 

---
base-commit: 05f7e89ab9731565d8a62e3b5d1ec206485eeb0b
change-id: 20260215-export-do-unlocked-00a6ac9373d4

Best regards,
-- 
Andreas Hindborg <a.hindborg@kernel.org>



^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH] rust: sync: export lock::do_unlocked
  2026-02-15 20:15 [PATCH] rust: sync: export lock::do_unlocked Andreas Hindborg
@ 2026-02-15 23:24 ` Benno Lossin
  2026-02-16  7:14   ` Andreas Hindborg
  0 siblings, 1 reply; 4+ messages in thread
From: Benno Lossin @ 2026-02-15 23:24 UTC (permalink / raw)
  To: Andreas Hindborg, Peter Zijlstra, Ingo Molnar, Will Deacon,
	Boqun Feng, Waiman Long, Miguel Ojeda, Gary Guo,
	Björn Roy Baron, Alice Ryhl, Trevor Gross, Danilo Krummrich
  Cc: linux-kernel, rust-for-linux

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@kernel.org>

Reviewed-by: Benno Lossin <lossin@kernel.org>

> ---
>  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;
> +    ///
> +    /// 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.

Cheers,
Benno

> +    ///     buffer.push(5u32, GFP_KERNEL)
> +    /// })?;
> +    /// # Ok::<(), Error>(())
> +    /// ```
> +    pub fn do_unlocked<U>(&mut self, cb: impl FnOnce() -> U) -> U {
>          // SAFETY: The caller owns the lock, so it is safe to unlock it.
>          unsafe { B::unlock(self.lock.state.get(), &self.state) };
>  
>
> ---
> base-commit: 05f7e89ab9731565d8a62e3b5d1ec206485eeb0b
> change-id: 20260215-export-do-unlocked-00a6ac9373d4
>
> Best regards,


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] rust: sync: export lock::do_unlocked
  2026-02-15 23:24 ` Benno Lossin
@ 2026-02-16  7:14   ` Andreas Hindborg
  2026-02-16  8:54     ` Alice Ryhl
  0 siblings, 1 reply; 4+ messages in thread
From: Andreas Hindborg @ 2026-02-16  7:14 UTC (permalink / raw)
  To: Benno Lossin, Peter Zijlstra, Ingo Molnar, Will Deacon,
	Boqun Feng, Waiman Long, Miguel Ojeda, Gary Guo,
	Björn Roy Baron, Alice Ryhl, Trevor Gross, Danilo Krummrich
  Cc: linux-kernel, rust-for-linux

"Benno Lossin" <lossin@kernel.org> 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@kernel.org>
>
> Reviewed-by: Benno Lossin <lossin@kernel.org>
>
>> ---
>>  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;
>> +    ///
>> +    /// 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.


Best regards,
Andreas Hindborg




^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] rust: sync: export lock::do_unlocked
  2026-02-16  7:14   ` Andreas Hindborg
@ 2026-02-16  8:54     ` Alice Ryhl
  0 siblings, 0 replies; 4+ messages in thread
From: Alice Ryhl @ 2026-02-16  8:54 UTC (permalink / raw)
  To: Andreas Hindborg
  Cc: Benno Lossin, Peter Zijlstra, Ingo Molnar, Will Deacon,
	Boqun Feng, Waiman Long, Miguel Ojeda, Gary Guo,
	Björn Roy Baron, Trevor Gross, Danilo Krummrich,
	linux-kernel, rust-for-linux

On Mon, Feb 16, 2026 at 08:14:42AM +0100, Andreas Hindborg wrote:
> "Benno Lossin" <lossin@kernel.org> 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@kernel.org>
> >
> > Reviewed-by: Benno Lossin <lossin@kernel.org>
> >
> >> ---
> >>  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

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2026-02-16  8:54 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-02-15 20:15 [PATCH] rust: sync: export lock::do_unlocked Andreas Hindborg
2026-02-15 23:24 ` Benno Lossin
2026-02-16  7:14   ` Andreas Hindborg
2026-02-16  8:54     ` Alice Ryhl

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox