* [PATCH] rust: sync: lock: Add Lock::get_mut()
@ 2025-01-31 15:59 Guilherme Giacomo Simoes
2025-02-03 9:21 ` Alice Ryhl
0 siblings, 1 reply; 3+ messages in thread
From: Guilherme Giacomo Simoes @ 2025-01-31 15:59 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
At initialization where we can guarantee that we do not have multiple
threads accessing the protected resource, blocking the resource in
addition to being redundant, can cause unnecessary overhead.
Add the Lock::get_mut() function for access the data without lock.
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 | 37 +++++++++++++++++++++++++++++++++++++
1 file changed, 37 insertions(+)
diff --git a/rust/kernel/sync/lock.rs b/rust/kernel/sync/lock.rs
index eb80048e0110..f1e29820ce99 100644
--- a/rust/kernel/sync/lock.rs
+++ b/rust/kernel/sync/lock.rs
@@ -140,6 +140,43 @@ pub fn new(t: T, name: &'static CStr, key: &'static LockClassKey) -> impl PinIni
}),
})
}
+
+ /// Get a mutable reference to data
+ ///
+ /// ```
+ /// 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);
+ /// ```
+ pub fn get_mut(&mut self) -> &mut T {
+ // SAFETY: the UnsafeCell guarantee that the self.data is not null.
+ // The `&mut self` guarantees the exclusive access to the underlying data, therefore it's
+ // safe to reborrow the inner data.
+ unsafe { &mut *self.data.get() }
+ }
}
impl<B: Backend> Lock<(), B> {
--
2.34.1
^ permalink raw reply related [flat|nested] 3+ messages in thread
* Re: [PATCH] rust: sync: lock: Add Lock::get_mut()
2025-01-31 15:59 [PATCH] rust: sync: lock: Add Lock::get_mut() Guilherme Giacomo Simoes
@ 2025-02-03 9:21 ` Alice Ryhl
2025-02-03 17:55 ` Guilherme Giacomo Simoes
0 siblings, 1 reply; 3+ messages in thread
From: Alice Ryhl @ 2025-02-03 9:21 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 Fri, Jan 31, 2025 at 4:59 PM Guilherme Giacomo Simoes
<trintaeoitogc@gmail.com> wrote:
>
> At initialization where we can guarantee that we do not have multiple
> threads accessing the protected resource, blocking the resource in
> addition to being redundant, can cause unnecessary overhead.
> Add the Lock::get_mut() function for access the data without lock.
> 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 | 37 +++++++++++++++++++++++++++++++++++++
> 1 file changed, 37 insertions(+)
>
> diff --git a/rust/kernel/sync/lock.rs b/rust/kernel/sync/lock.rs
> index eb80048e0110..f1e29820ce99 100644
> --- a/rust/kernel/sync/lock.rs
> +++ b/rust/kernel/sync/lock.rs
> @@ -140,6 +140,43 @@ pub fn new(t: T, name: &'static CStr, key: &'static LockClassKey) -> impl PinIni
> }),
> })
> }
> +
> + /// Get a mutable reference to data
> + ///
> + /// ```
> + /// 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);
> + /// ```
> + pub fn get_mut(&mut self) -> &mut T {
I maintain my objection that this function cannot be correctly called.
Yes, if you use dubious unsafe code, you can call it, but we shouldn't
do that.
At best, you could change this method to take `self: Pin<&mut Self>`.
Alice
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: [PATCH] rust: sync: lock: Add Lock::get_mut()
2025-02-03 9:21 ` Alice Ryhl
@ 2025-02-03 17:55 ` Guilherme Giacomo Simoes
0 siblings, 0 replies; 3+ messages in thread
From: Guilherme Giacomo Simoes @ 2025-02-03 17:55 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:
> I maintain my objection that this function cannot be correctly called.
> Yes, if you use dubious unsafe code, you can call it, but we shouldn't
> do that.
>
> At best, you could change this method to take `self: Pin<&mut Self>`.
Yes you is right, we should avoid unsafe code. But how the
`self: Pin<&mut Self` help us here?
The unsafe code at the get_mut() call place, is because of
`get_unchecked_mut`. You probably already know, but this is unsafe because you
need guarantee that the value is not move in memory. The `get_unchecked_mut`
return a &mut T, (in this case return a &mut Example), and we need a reference
to Example for access `d` field.
since we have access for `d` , we can get get_mut() without pinned `d`.
We need a way for get `Example.d` without `get_unchecked_mut`.
Thanks,
Guilherme
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2025-02-03 17:56 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-01-31 15:59 [PATCH] rust: sync: lock: Add Lock::get_mut() Guilherme Giacomo Simoes
2025-02-03 9:21 ` Alice Ryhl
2025-02-03 17:55 ` 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).