rust: sync: create the `get_mut()` function
Subsystems (3)
PATCH 1/1 rust: sync: create the `get_mut()` function
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.
--- rust/kernel/sync/lock.rs | 6 ++++++ 1 file changed, 6 insertions(+)
rust/kernel/sync/lock.rs +6 -0
@@ -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 > {
Signature · 1 line
2.34.1
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.
Quoted 4 lines · Guilherme Giacomo Simoes
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...
Quoted 11 lines
Suggested-by Björn Roy Baron <[email protected]>Signed-off-by Guilherme Giacomo Simoes <[email protected]>--- rust/kernel/sync/lock.rs | 6 ++++++ 1 file changed, 6 insertions(+)rust/kernel/sync/lock.rs +0
-0
rust/kernel/sync/lock.rs pub fn new(t: T, name: &'static CStr, key: &'static LockClassKey) -> impl PinIni · line 144
Quoted 5 lines
rust/kernel/sync/lock.rs +2
-0@@ -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.
rust/kernel/sync/lock.rs pub fn new(t: T, name: &'static CStr, key: &'static LockClassKey) -> impl PinIni · line 146
Quoted 2 lines
rust/kernel/sync/lock.rs +2
-0+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
Quoted 8 lines
rust/kernel/sync/lock.rs +2
-0+unsafe { &mut *self.data.get () } + } }impl <B :Backend >Lock <(),B > {Signature · 1 line
2.34.1
Boqun Feng <[email protected]> wrotes:
Quoted 42 lines
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.Quoted 4 lines · Guilherme Giacomo Simoes
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...
Quoted 11 lines
Suggested-by Björn Roy Baron <[email protected]>Signed-off-by Guilherme Giacomo Simoes <[email protected]>--- rust/kernel/sync/lock.rs | 6 ++++++ 1 file changed, 6 insertions(+)rust/kernel/sync/lock.rs +0
-0rust/kernel/sync/lock.rs pub fn new(t: T, name: &'static CStr, key: &'static LockClassKey) -> impl PinIni · line 144
Quoted 5 lines
rust/kernel/sync/lock.rs +2
-0@@ -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.
rust/kernel/sync/lock.rs pub fn new(t: T, name: &'static CStr, key: &'static LockClassKey) -> impl PinIni · line 146
Quoted 2 lines
rust/kernel/sync/lock.rs +2
-0+pub fn get_mut (&mut self) -> &mut T { +// SAFETY: the caller must guarantee that the instance is only used in one place
Quoted 8 lines
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/[email protected]
Thanks,
Guilherme
Alice Ryhl
maintainerQuoted 16 lines · Guilherme Giacomo Simoes
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 <[email protected]>Signed-off-by Guilherme Giacomo Simoes <[email protected]>--- rust/kernel/sync/lock.rs | 6 ++++++ 1 file changed, 6 insertions(+)rust/kernel/sync/lock.rs +0
-0
rust/kernel/sync/lock.rs pub fn new(t: T, name: &'static CStr, key: &'static LockClassKey) -> impl PinIni · line 148
Quoted 9 lines
rust/kernel/sync/lock.rs +6
-0@@ -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
Alice Ryhl <[email protected]> wrotes:
Quoted 2 lines
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