rust-for-linux.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] rust: sync: create the `get_mut()` function
@ 2025-01-30 18:51 Guilherme Giacomo Simoes
  2025-01-30 21:13 ` Boqun Feng
  2025-01-31  9:15 ` Alice Ryhl
  0 siblings, 2 replies; 5+ messages in thread
From: Guilherme Giacomo Simoes @ 2025-01-30 18:51 UTC (permalink / raw)
  To: peterz, mingo, will, boqun.feng, longman, ojeda, alex.gaynor,
	gary, bjorn3_gh, benno.lossin, a.hindborg, aliceryhl, tmgross
  Cc: Guilherme Giacomo Simoes, linux-kernel, rust-for-linux

Create a `get_mut()` function that receive a mutable instance of Lock,
and return a mutable reference to data because if the instance is
mutable, the rust compiler guarantee the access control.

Suggested-by: Björn Roy Baron <bjorn3_gh@protonmail.com>
Signed-off-by: Guilherme Giacomo Simoes <trintaeoitogc@gmail.com>
---
 rust/kernel/sync/lock.rs | 6 ++++++
 1 file changed, 6 insertions(+)

diff --git a/rust/kernel/sync/lock.rs b/rust/kernel/sync/lock.rs
index eb80048e0110..3f9d78bcb37c 100644
--- a/rust/kernel/sync/lock.rs
+++ b/rust/kernel/sync/lock.rs
@@ -140,6 +140,12 @@ pub fn new(t: T, name: &'static CStr, key: &'static LockClassKey) -> impl PinIni
             }),
         })
     }
+
+    /// Get a mutable reference to data
+    pub fn get_mut(&mut self) -> &mut T {
+        // SAFETY: the caller must guarantee that the instance is only used in one place
+        unsafe { &mut *self.data.get() }
+    }
 }
 
 impl<B: Backend> Lock<(), B> {
-- 
2.34.1


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

* Re: [PATCH] rust: sync: create the `get_mut()` function
  2025-01-30 18:51 [PATCH] rust: sync: create the `get_mut()` function Guilherme Giacomo Simoes
@ 2025-01-30 21:13 ` Boqun Feng
  2025-01-31 16:02   ` Guilherme Giacomo Simoes
  2025-01-31  9:15 ` Alice Ryhl
  1 sibling, 1 reply; 5+ messages in thread
From: Boqun Feng @ 2025-01-30 21:13 UTC (permalink / raw)
  To: Guilherme Giacomo Simoes
  Cc: peterz, mingo, will, longman, ojeda, alex.gaynor, gary, bjorn3_gh,
	benno.lossin, a.hindborg, aliceryhl, tmgross, linux-kernel,
	rust-for-linux

Hi Guilherme,

Thanks for the patch. First I would prefer the title being:

	rust: sync: lock: Add Lock::get_mut()

"create" is not a good word to use here, and the parentheses after
"get_mut" already says it's a function.

On Thu, Jan 30, 2025 at 03:51:38PM -0300, Guilherme Giacomo Simoes wrote:
> Create a `get_mut()` function that receive a mutable instance of Lock,
> and return a mutable reference to data because if the instance is
> mutable, the rust compiler guarantee the access control.
> 

This commit log doesn't include "why we need this", so please add the
reason or the usage of this function, maybe you or someone need it
because of some initialization/setup code after creating a lock
protected object? Moreover...

> Suggested-by: Björn Roy Baron <bjorn3_gh@protonmail.com>
> Signed-off-by: Guilherme Giacomo Simoes <trintaeoitogc@gmail.com>
> ---
>  rust/kernel/sync/lock.rs | 6 ++++++
>  1 file changed, 6 insertions(+)
> 
> diff --git a/rust/kernel/sync/lock.rs b/rust/kernel/sync/lock.rs
> index eb80048e0110..3f9d78bcb37c 100644
> --- a/rust/kernel/sync/lock.rs
> +++ b/rust/kernel/sync/lock.rs
> @@ -140,6 +140,12 @@ pub fn new(t: T, name: &'static CStr, key: &'static LockClassKey) -> impl PinIni
>              }),
>          })
>      }
> +
> +    /// Get a mutable reference to data

... please provide an example of the usage in the doc.

> +    pub fn get_mut(&mut self) -> &mut T {
> +        // SAFETY: the caller must guarantee that the instance is only used in one place

SAFETY comments should explain why it's safe, here it's phrased like a
requirement, maybe something like:

    // SAFETY: `&mut self` guarantees the exclusive access to the
    // underlying data, therefore it's safe to reborrow the inner data.

Regards,
Boqun

> +        unsafe { &mut *self.data.get() }
> +    }
>  }
>  
>  impl<B: Backend> Lock<(), B> {
> -- 
> 2.34.1
> 

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

* Re: [PATCH] rust: sync: create the `get_mut()` function
  2025-01-30 18:51 [PATCH] rust: sync: create the `get_mut()` function Guilherme Giacomo Simoes
  2025-01-30 21:13 ` Boqun Feng
@ 2025-01-31  9:15 ` Alice Ryhl
  2025-01-31 16:15   ` Guilherme Giacomo Simoes
  1 sibling, 1 reply; 5+ messages in thread
From: Alice Ryhl @ 2025-01-31  9:15 UTC (permalink / raw)
  To: Guilherme Giacomo Simoes
  Cc: peterz, mingo, will, boqun.feng, longman, ojeda, alex.gaynor,
	gary, bjorn3_gh, benno.lossin, a.hindborg, tmgross, linux-kernel,
	rust-for-linux

On Thu, Jan 30, 2025 at 7:52 PM Guilherme Giacomo Simoes
<trintaeoitogc@gmail.com> wrote:
>
> Create a `get_mut()` function that receive a mutable instance of Lock,
> and return a mutable reference to data because if the instance is
> mutable, the rust compiler guarantee the access control.
>
> Suggested-by: Björn Roy Baron <bjorn3_gh@protonmail.com>
> Signed-off-by: Guilherme Giacomo Simoes <trintaeoitogc@gmail.com>
> ---
>  rust/kernel/sync/lock.rs | 6 ++++++
>  1 file changed, 6 insertions(+)
>
> diff --git a/rust/kernel/sync/lock.rs b/rust/kernel/sync/lock.rs
> index eb80048e0110..3f9d78bcb37c 100644
> --- a/rust/kernel/sync/lock.rs
> +++ b/rust/kernel/sync/lock.rs
> @@ -140,6 +140,12 @@ pub fn new(t: T, name: &'static CStr, key: &'static LockClassKey) -> impl PinIni
>              }),
>          })
>      }
> +
> +    /// Get a mutable reference to data
> +    pub fn get_mut(&mut self) -> &mut T {
> +        // SAFETY: the caller must guarantee that the instance is only used in one place
> +        unsafe { &mut *self.data.get() }
> +    }

As far as I can tell, it's impossible to call this function because
you cannot obtain a bare mutable reference to a pinned value.

Alice

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

* Re: [PATCH] rust: sync: create the `get_mut()` function
  2025-01-30 21:13 ` Boqun Feng
@ 2025-01-31 16:02   ` Guilherme Giacomo Simoes
  0 siblings, 0 replies; 5+ messages in thread
From: Guilherme Giacomo Simoes @ 2025-01-31 16:02 UTC (permalink / raw)
  To: boqun.feng
  Cc: a.hindborg, alex.gaynor, aliceryhl, benno.lossin, bjorn3_gh, gary,
	linux-kernel, longman, mingo, ojeda, peterz, rust-for-linux,
	tmgross, trintaeoitogc, will

Boqun Feng <boqun.feng@gmail.com> wrotes:
> Hi Guilherme,
> 
> Thanks for the patch. First I would prefer the title being:
> 
> 	rust: sync: lock: Add Lock::get_mut()
> 
> "create" is not a good word to use here, and the parentheses after
> "get_mut" already says it's a function.
> 
> On Thu, Jan 30, 2025 at 03:51:38PM -0300, Guilherme Giacomo Simoes wrote:
> > Create a `get_mut()` function that receive a mutable instance of Lock,
> > and return a mutable reference to data because if the instance is
> > mutable, the rust compiler guarantee the access control.
> > 
> 
> This commit log doesn't include "why we need this", so please add the
> reason or the usage of this function, maybe you or someone need it
> because of some initialization/setup code after creating a lock
> protected object? Moreover...
> 
> > Suggested-by: Björn Roy Baron <bjorn3_gh@protonmail.com>
> > Signed-off-by: Guilherme Giacomo Simoes <trintaeoitogc@gmail.com>
> > ---
> >  rust/kernel/sync/lock.rs | 6 ++++++
> >  1 file changed, 6 insertions(+)
> > 
> > diff --git a/rust/kernel/sync/lock.rs b/rust/kernel/sync/lock.rs
> > index eb80048e0110..3f9d78bcb37c 100644
> > --- a/rust/kernel/sync/lock.rs
> > +++ b/rust/kernel/sync/lock.rs
> > @@ -140,6 +140,12 @@ pub fn new(t: T, name: &'static CStr, key: &'static LockClassKey) -> impl PinIni
> >              }),
> >          })
> >      }
> > +
> > +    /// Get a mutable reference to data
> 
> ... please provide an example of the usage in the doc.
> 
> > +    pub fn get_mut(&mut self) -> &mut T {
> > +        // SAFETY: the caller must guarantee that the instance is only used in one place
> 
> SAFETY comments should explain why it's safe, here it's phrased like a
> requirement, maybe something like:
> 
>     // SAFETY: `&mut self` guarantees the exclusive access to the
>     // underlying data, therefore it's safe to reborrow the inner data.
> 
> Regards,
> Boqun

Ok, I make this changes and sent a new patch:

https://lore.kernel.org/all/20250131155940.305403-1-trintaeoitogc@gmail.com

Thanks,
Guilherme

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

* Re: [PATCH] rust: sync: create the `get_mut()` function
  2025-01-31  9:15 ` Alice Ryhl
@ 2025-01-31 16:15   ` Guilherme Giacomo Simoes
  0 siblings, 0 replies; 5+ messages in thread
From: Guilherme Giacomo Simoes @ 2025-01-31 16:15 UTC (permalink / raw)
  To: aliceryhl
  Cc: a.hindborg, alex.gaynor, benno.lossin, bjorn3_gh, boqun.feng,
	gary, linux-kernel, longman, mingo, ojeda, peterz, rust-for-linux,
	tmgross, trintaeoitogc, will

Alice Ryhl <aliceryhl@google.com> wrotes:
> As far as I can tell, it's impossible to call this function because
> you cannot obtain a bare mutable reference to a pinned value.
So, I can call this function make anything like: 

```
use kernel::sync::{new_mutex, Mutex};

struct Inner {
    a: u32,
}

#[pin_data]
struct Example {
    #[pin]
    d: Mutex<Inner>,
}

impl Example {
    fn new() -> impl PinInit<Self> {
        pin_init!(Self {
            // This new_mutex! can be anothers locks like new_spinlock!()
            d <- new_mutex!(Inner { a: 20 })
        })
    }
}

let mut pin = KBox::pin_init(Example::new(), GFP_KERNEL)?;
let mut_pin = pin.as_mut();

let data = unsafe { Pin::get_unchecked_mut(mut_pin).d.get_mut() };
assert_eq!(data.a, 20);
```

Thanks,
Guilherme

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

end of thread, other threads:[~2025-01-31 16:16 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-01-30 18:51 [PATCH] rust: sync: create the `get_mut()` function Guilherme Giacomo Simoes
2025-01-30 21:13 ` Boqun Feng
2025-01-31 16:02   ` Guilherme Giacomo Simoes
2025-01-31  9:15 ` Alice Ryhl
2025-01-31 16:15   ` Guilherme Giacomo Simoes

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).