Re: [PATCH] rust: sync: create the `get_mut()` function

From: Alice Ryhl
Date: Fri Jan 31 2025 - 04:16:20 EST


On Thu, Jan 30, 2025 at 7:52 PM Guilherme Giacomo Simoes
<trintaeoitogc@xxxxxxxxx> 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@xxxxxxxxxxxxxx>
> Signed-off-by: Guilherme Giacomo Simoes <trintaeoitogc@xxxxxxxxx>
> ---
> 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