rust: miscdevice: Add additional data to MiscDeviceRegistration
Subsystems (2)
v2 cover rust: miscdevice: Add additional data to MiscDeviceRegistration
Currently there is no good way to pass arbitrary data from the driver to a miscdevice or to share data between individual handles to a miscdevice in rust.
This series adds additional (generic) data to the MiscDeviceRegistration for this purpose.
The first patch adds the UnsafePinned (Previously Aliased) type.
The second patch implements the changes and fixes the build of the sample.
The third patch changes the rust_misc_device sample to use this to share the same data between multiple handles to the miscdevice. I have tested the sample with qemu and the C userspace example from the doc comments.
Some discussion on Zulip about the motivation and approach (Thanks a lot to everyone helping me out with this):
---
Changes in v2:
- Don't use associated_type_bounds since the MSRV does not support
that on stable yet (Kernel test robot)
- Doc changes and add intra-doc links (Miguel)
- Use container_of macro instead of pointer cast in fops_open (Greg)
- Rename Aliased to UnsafePinned (Boqun)
- Make sure Data is initialized befofre misc_register is called
- Rework the example to use an additional shared value instead of
replacing the unique one
- Expanded the c code for the example to use the new ioctls
- Link to v1: https://lore.kernel.org/r/20250119-b4-rust_miscdevice_registrationdata-v1-0-edbf18dde5fc@gmail.com
---
Christian Schrefl (3):
rust: add UnsafePinned type
rust: miscdevice: Add additional data to MiscDeviceRegistration
rust: miscdevice: adjust the rust_misc_device sample to use RegistrationData.
rust/kernel/miscdevice.rs | 75 ++++++++++++++++++------- rust/kernel/types.rs | 57 +++++++++++++++++++ samples/rust/rust_misc_device.rs | 117 ++++++++++++++++++++++++++++++++++++--- 3 files changed, 221 insertions(+), 28 deletions(-)
---
base-commit: b4b0881156fb8209bf5ef6cb63211bb0ad6e1a6b
change-id: 20250119-b4-rust_miscdevice_registrationdata-a11d88dcb284
Best regards,
Signature · 1 line
Christian Schrefl <[email protected]>
PATCH v2 1/3 rust: add UnsafePinned type
UnsafePinned<T> is useful for cases where a value might be shared with C code but not directly used by it. In particular this is added for additional data in the MiscDeviceRegistration which will be shared between fops->open and the containing struct.
Similar to Opaque but guarantees that the value is always initialized and that the inner value is dropped when UnsafePinned is dropped.
This was originally proposed for the IRQ abstractions [0] and is also useful for other where the inner data may be aliased, but is always valid and automatic Drop is desired.
--- rust/kernel/types.rs | 57 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+)
rust/kernel/types.rs +57 -0
@@ -253,6 +253,9 @@ fn drop(&mut self) {/// /// `Opaque<T>` is meant to be used with FFI objects that are never interpreted by Rust code. /// +/// In cases where the contained data is only used by Rust, is not allowed to be +/// uninitialized and automatic [`Drop`] is desired [`UnsafePinned`] should be used instead. +/// /// It is used to wrap structs from the C side, like for example `Opaque<bindings::mutex>`. /// It gets rid of all the usual assumptions that Rust has for a value: ///
@@ -573,3 +576,57 @@ pub enum Either<L, R> {/// [`NotThreadSafe`]: type@NotThreadSafe #[allow(non_upper_case_globals)] pub const NotThreadSafe :NotThreadSafe =PhantomData ; + +/// Stores a value that may be used from multiple mutable pointers. +/// +/// `UnsafePinned` gets rid of some of the usual assumptions that Rust has for a value: +/// - The value is allowed to be mutated, when a `&UnsafePinned<T>` exists on the Rust side. +/// - No uniqueness for mutable references: it is fine to have multiple `&mut UnsafePinned<T>` +/// point to the same value. +/// +/// To avoid the ability to use [`core::mem::swap`] this still needs to be used through a +/// [`core::pin::Pin`] reference. +/// +/// This is useful for cases where a value might be shared with C code +/// but not interpreted by it or in cases where it can not always be guaranteed that the +/// references are unique. +/// +/// This is similar to [`Opaque<T>`] but is guaranteed to always contain valid data and will +/// call the [`Drop`] implementation of `T` when dropped. +#[repr(transparent)] +pub struct UnsafePinned <T > { + value:UnsafeCell <T >, + _pin:PhantomPinned , +} + +impl <T >UnsafePinned <T > { +/// Creates a new [`UnsafePinned`] value. +pub const fn new (value:T ) ->Self { +Self { + value:UnsafeCell ::new (value), + _pin:PhantomPinned , + } + } + +/// Create an [`UnsafePinned`] pin-initializer from the given pin-initializer. +pub fn try_pin_init <E >(value:impl PinInit <T ,E >) ->impl PinInit <Self ,E > { +// SAFETY: +// - In case of an error in `value` the error is returned, otherwise `slot` is fully +// initialized, since `self.value` is initialized and `_pin` is a zero sized type. +// - The `Pin` invariants of `self.value` are upheld, since no moving occurs. +unsafe { init::pin_init_from_closure (move |slot| value.__pinned_init (Self ::raw_get (slot))) } + } + +/// Returns a raw pointer to the contained data. +pub const fn get (&self) -> *mut T { +UnsafeCell ::get (&self.value).cast ::<T >() + } + +/// Gets the value behind `this`. +/// +/// This function is useful to get access to the value without creating intermediate +/// references. +pub const fn raw_get (this: *const Self ) -> *mut T { +UnsafeCell ::raw_get (this.cast ::<UnsafeCell <MaybeUninit <T >>>()).cast ::<T >() + } +}
Signature · 1 line
2.48.1
Re: [PATCH v2 1/3] rust: add UnsafePinned type
rust/kernel/types.rs pub enum Either<L, R> { · line 585
Quoted 11 lines · Christian Schrefl
rust/kernel/types.rs +7
-0@@ -573,3 +576,57 @@ pub enum Either<L, R> {/// [`NotThreadSafe`]: type@NotThreadSafe #[allow(non_upper_case_globals)] pub const NotThreadSafe :NotThreadSafe =PhantomData ; + +/// Stores a value that may be used from multiple mutable pointers. +/// +/// `UnsafePinned` gets rid of some of the usual assumptions that Rust has for a value: +/// - The value is allowed to be mutated, when a `&UnsafePinned<T>` exists on the Rust side. +/// - No uniqueness for mutable references: it is fine to have multiple `&mut UnsafePinned<T>` +/// point to the same value.
We have another patch series [1] in transit that changes the wording on the Opaque<T> type. I think we should mirror the same wording here.
[1]: https://lore.kernel.org/rust-for-linux/[email protected]/
Review pub enum Either<L, R> { · line 587
Quoted 2 lines
(quoted diff) +2
-0+/// +/// To avoid the ability to use [`core::mem::swap`] this still needs to be used through a
IIRC typing out the whole path makes it also appear in the docs. I think we should just use mem::swap and omit core of course you need to add this to make it work:
/// [`mem::swap`]: core::mem::swap
rust/kernel/types.rs pub enum Either<L, R> { · line 588
Quoted 1 line
rust/kernel/types.rs +1
-0+/// [`core::pin::Pin`] reference.
Here, I would link to Pin, but say "[pinned](core::pin::Pin) pointer." instead, since eg Pin<Box<T>> also is okay to use.
Quoted 19 lines
rust/kernel/types.rs +19
-0+/// +/// This is useful for cases where a value might be shared with C code +/// but not interpreted by it or in cases where it can not always be guaranteed that the +/// references are unique. +/// +/// This is similar to [`Opaque<T>`] but is guaranteed to always contain valid data and will +/// call the [`Drop`] implementation of `T` when dropped. +#[repr(transparent)] +pub struct UnsafePinned <T > { + value:UnsafeCell <T >, + _pin:PhantomPinned , +} + +impl <T >UnsafePinned <T > { +/// Creates a new [`UnsafePinned`] value. +pub const fn new (value:T ) ->Self { +Self { + value:UnsafeCell ::new (value), + _pin:PhantomPinned ,
rust/kernel/types.rs pub enum Either<L, R> { · line 617
Quoted 10 lines
rust/kernel/types.rs +10
-0+ } + } + +/// Create an [`UnsafePinned`] pin-initializer from the given pin-initializer. +pub fn try_pin_init <E >(value:impl PinInit <T ,E >) ->impl PinInit <Self ,E > { +// SAFETY: +// - In case of an error in `value` the error is returned, otherwise `slot` is fully +// initialized, since `self.value` is initialized and `_pin` is a zero sized type. +// - The `Pin` invariants of `self.value` are upheld, since no moving occurs. +unsafe { init::pin_init_from_closure (move |slot| value.__pinned_init (Self ::raw_get (slot))) }
Ah this is a bit suboptimal, but I guess there currently isn't a better way to do this. I'll add it to my list of things to improve with pin-init.
rust/kernel/types.rs pub enum Either<L, R> { · line 630
Quoted 13 lines
rust/kernel/types.rs +13
-0+ } + +/// Returns a raw pointer to the contained data. +pub const fn get (&self) -> *mut T { +UnsafeCell ::get (&self.value).cast ::<T >() + } + +/// Gets the value behind `this`. +/// +/// This function is useful to get access to the value without creating intermediate +/// references. +pub const fn raw_get (this: *const Self ) -> *mut T { +UnsafeCell ::raw_get (this.cast ::<UnsafeCell <MaybeUninit <T >>>()).cast ::<T >()
Why the cast to MaybeUninit<T>? I think this can just be:
UnsafeCell::raw_get(&raw const this.value)
---
Cheers,
Benno
Quoted 2 lines
(quoted diff) +2
-0+ } +}
PATCH v2 2/3 rust: miscdevice: Add additional data to MiscDeviceRegistration
In reply to Christian SchreflWhen using the Rust miscdevice bindings, you generally embed the MiscDeviceRegistration within another struct:
struct MyDriverData {
data: SomeOtherData,
misc: MiscDeviceRegistration<MyMiscFile>
}
In the fops->open callback of the miscdevice, you are given a reference to the registration, which allows you to access its fields. For example, as of commit 284ae0be4dca ("rust: miscdevice: Provide accessor to pull out miscdevice::this_device") you can access the internal struct device. However, there is still no way to access the data field in the above example, because you only have a reference to the registration.
Using container_of is also not possible to do safely. For example, if the destructor of MyDriverData runs, then the destructor of data would run before the miscdevice is deregistered, so using container_of to access data from fops->open could result in a UAF. A similar problem can happen on initialization if misc is not the last field to be initialized.
To provide a safe way to access user-defined data stored next to the struct miscdevice, make MiscDeviceRegistration into a container that can store a user-provided piece of data. This way, fops->open can access that data via the registration, since the data is stored inside the registration.
The container enforces that the additional user data is initialized before the miscdevice is registered, and that the miscdevice is deregistered before the user data is destroyed. This ensures that access to the userdata is safe.
For the same reasons as in commit 88441d5c6d17 ("rust: miscdevice:
access the struct miscdevice from fops->open()"), you cannot access the user data in any other fops callback than open. This is because a miscdevice can be deregistered while there are still open files.
A situation where this user data might be required is when a platform driver acquires a resource in probe and wants to use this resource in the fops implementation of a MiscDevice.
This solution is similar to the approach used by the initial downstream Rust-for-Linux/Rust branch [0].
--- rust/kernel/miscdevice.rs | 75 +++++++++++++++++++++++++++++----------- samples/rust/rust_misc_device.rs | 4 ++- 2 files changed, 58 insertions(+), 21 deletions(-)
rust/kernel/miscdevice.rs +55 -20
@@ -9,7 +9,7 @@//! Reference: <https://www.kernel.org/doc/html/latest/driver-api/misc_devices.html> use crate ::{ - bindings, + bindings, container_of, device::Device , error::{to_result,Error ,Result ,VTABLE_DEFAULT_ERROR }, ffi::{c_int, c_long, c_uint, c_ulong},
@@ -17,7 +17,7 @@ prelude::*, seq_file::SeqFile, str::CStr, - types::{ForeignOwnable, Opaque}, + types::{ForeignOwnable, Opaque, UnsafePinned}, };use core::{marker::PhantomData , mem::MaybeUninit , pin::Pin };
@@ -45,32 +45,46 @@ pub const fn into_raw<T: MiscDevice>(self) -> bindings::miscdevice { /// # Invariants /// /// `inner` is a registered misc device. -#[repr(transparent)] +#[repr(C)] #[pin_data(PinnedDrop)] -pub struct MiscDeviceRegistration<T> { +pub struct MiscDeviceRegistration<T: MiscDevice> { #[pin] inner: Opaque<bindings::miscdevice>, + #[pin] + data: UnsafePinned<T::RegistrationData>, _t: PhantomData<T>, } -// SAFETY: It is allowed to call `misc_deregister` on a different thread from where you called -// `misc_register`. -unsafe impl<T> Send for MiscDeviceRegistration<T> {} -// SAFETY: All `&self` methods on this type are written to ensure that it is safe to call them in -// parallel. -unsafe impl<T> Sync for MiscDeviceRegistration<T> {} +// SAFETY: +// - It is allowed to call `misc_deregister` on a different thread from where you called +// `misc_register`. +// - Only implements `Send` if `MiscDevice::RegistrationData` is also `Send`. +unsafe impl<T: MiscDevice> Send for MiscDeviceRegistration<T> where T::RegistrationData: Send {} + +// SAFETY: +// - All `&self` methods on this type are written to ensure that it is safe to call them in +// parallel. +// - `MiscDevice::RegistrationData` is always `Sync`. +unsafe impl<T: MiscDevice> Sync for MiscDeviceRegistration<T> {} impl<T: MiscDevice> MiscDeviceRegistration<T> { /// Register a misc device. - pub fn register(opts: MiscDeviceOptions) -> impl PinInit<Self, Error> { + pub fn register( + opts: MiscDeviceOptions, + data: impl PinInit<T::RegistrationData, Error>, + ) -> impl PinInit<Self, Error> { try_pin_init!(Self { + data <- UnsafePinned::try_pin_init(data), inner <- Opaque::try_ffi_init(move |slot: *mut bindings::miscdevice| { // SAFETY: The initializer can write to the provided `slot`. unsafe { slot.write(opts.into_raw::<T>()) }; - // SAFETY: We just wrote the misc device options to the slot. The miscdevice will - // get unregistered before `slot` is deallocated because the memory is pinned and - // the destructor of this type deallocates the memory. + // SAFETY: + // * We just wrote the misc device options to the slot. The miscdevice will + // get unregistered before `slot` is deallocated because the memory is pinned and + // the destructor of this type deallocates the memory. + // * `data` is Initialized before `misc_register` so no race with `fops->open()` + // is possible. // INVARIANT: If this returns `Ok(())`, then the `slot` will contain a registered // misc device. to_result(unsafe { bindings::misc_register(slot) })
@@ -93,10 +107,18 @@ pub fn device(&self) -> &Device {// before the underlying `struct miscdevice` is destroyed. unsafe {Device ::as_ref ((*self.as_raw ()).this_device) } } + +/// Access the additional data stored in this registration. +pub fn data (&self) -> &T ::RegistrationData { +// SAFETY: +// * No mutable reference to the value contained by `self.data` can ever be created. +// * The value contained by `self.data` is valid for the entire lifetime of `&self`. +unsafe { &*self.data.get () } + } }#[pinned_drop] -impl <T >PinnedDrop for MiscDeviceRegistration <T > { +impl <T :MiscDevice >PinnedDrop for MiscDeviceRegistration <T > {fn drop (self:Pin <&mut Self >) {// SAFETY: We know that the device is registered by the type invariants. unsafe { bindings::misc_deregister (self.inner.get ()) };
@@ -109,6 +131,13 @@ pub trait MiscDevice: Sized {/// What kind of pointer should `Self` be wrapped in. type Ptr :ForeignOwnable +Send +Sync ; +/// The additional data carried by the [`MiscDeviceRegistration`] for this [`MiscDevice`]. +/// If no additional data is required than the unit type `()` should be used. +/// +/// This data can be accessed in [`MiscDevice::open()`] using +/// [`MiscDeviceRegistration::data()`]. +type RegistrationData :Sync ; +/// Called when the misc device is opened. /// /// The returned pointer will be stored as the private data for the file.
@@ -211,17 +240,23 @@ impl<T: MiscDevice> VtableHelper<T> {// SAFETY: The open call of a file can access the private data. let misc_ptr =unsafe { (*raw_file).private_data }; -// SAFETY: This is a miscdevice, so `misc_open()` set the private data to a pointer to the -// associated `struct miscdevice` before calling into this method. Furthermore, `misc_open()` -// ensures that the miscdevice can't be unregistered and freed during this call to `fops_open`. -let misc =unsafe { &*misc_ptr.cast ::<MiscDeviceRegistration <T >>() }; +// This is a miscdevice, so `misc_open()` sets the private data to a pointer to the +// associated `struct miscdevice` before calling into this method. +let misc_ptr = misc_ptr.cast ::<bindings::miscdevice >(); + +// SAFETY: +// * `misc_open()` ensures that the `struct miscdevice` can't be unregistered and freed +// during this call to `fops_open`. +// * The `misc_ptr` always points to the `inner` field of a `MiscDeviceRegistration<T>`. +// * The `MiscDeviceRegistration<T>` is valid until the `struct miscdevice` was unregistered. +let registration =unsafe { &*container_of! (misc_ptr,MiscDeviceRegistration <T >, inner) };// SAFETY: // * This underlying file is valid for (much longer than) the duration of `T::open`. // * There is no active fdget_pos region on the file on this thread. let file =unsafe {File ::from_raw_file (raw_file) }; -let ptr =match T ::open (file, misc) { +let ptr =match T ::open (file, registration) {Ok (ptr) => ptr,Err (err) =>return err.to_errno (), };
samples/rust/rust_misc_device.rs +3 -1
@@ -136,7 +136,7 @@ fn init(_module: &'static ThisModule) -> impl PinInit<Self, Error> { };try_pin_init! (Self { - _miscdev <-MiscDeviceRegistration ::register (options), + _miscdev <-MiscDeviceRegistration ::register (options, ()), }) } }
@@ -156,6 +156,8 @@ struct RustMiscDevice {impl MiscDevice for RustMiscDevice {type Ptr =Pin <KBox <Self >>; +type RegistrationData = (); +fn open (_file: &File , misc: &MiscDeviceRegistration <Self >) ->Result <Pin <KBox <Self >>> {let dev =ARef ::from (misc.device ());
Signature · 1 line
2.48.1
PATCH v2 3/3 rust: miscdevice: adjust the rust_misc_device sample to use RegistrationData.
In reply to Christian SchreflAdd a second mutex to the RustMiscDevice, which is shared between all instances of the device using an Arc and the RegistrationData of MiscDeviceRegistration.
This is mostly to demonstrate the capability to share data in this way.
--- samples/rust/rust_misc_device.rs | 117 ++++++++++++++++++++++++++++++++++++--- 1 file changed, 108 insertions(+), 9 deletions(-)
samples/rust/rust_misc_device.rs +108 -9
@@ -86,6 +86,62 @@/// return -1; /// } /// +/// value++; +/// +/// // Set shared value to something different +/// printf("Submitting new shared value (%d)\n", value); +/// ret = ioctl(fd, RUST_MISC_DEV_SET_SHARED_VALUE, &value); +/// if (ret < 0) { +/// perror("ioctl: Failed to submit new value"); +/// close(fd); +/// return errno; +/// } +/// +/// // Close the device file +/// printf("Closing /dev/rust-misc-device\n"); +/// close(fd); +/// +/// // Open the device file again +/// printf("Opening /dev/rust-misc-device again for reading\n"); +/// fd = open("/dev/rust-misc-device", O_RDWR); +/// if (fd < 0) { +/// perror("open"); +/// return errno; +/// } +/// +/// // Ensure new value was applied +/// printf("Fetching new value\n"); +/// ret = ioctl(fd, RUST_MISC_DEV_GET_SHARED_VALUE, &new_value); +/// if (ret < 0) { +/// perror("ioctl: Failed to fetch the new value"); +/// close(fd); +/// return errno; +/// } +/// +/// if (value != new_value) { +/// printf("Failed: Committed and retrieved values are different (%d - %d)\n", +/// value, new_value); +/// close(fd); +/// return -1; +/// } +/// +/// value = 0; +/// // Ensure non-shared value is still 0 +/// printf("Fetching new value\n"); +/// ret = ioctl(fd, RUST_MISC_DEV_GET_VALUE, &new_value); +/// if (ret < 0) { +/// perror("ioctl: Failed to fetch the new value"); +/// close(fd); +/// return errno; +/// } +/// +/// if (value != new_value) { +/// printf("Failed: Committed and retrieved values are different (%d - %d)\n", +/// value, new_value); +/// close(fd); +/// return -1; +/// } +/// /// // Close the device file /// printf("Closing /dev/rust-misc-device\n"); /// close(fd);
@@ -104,7 +160,7 @@ miscdevice::{MiscDevice, MiscDeviceOptions, MiscDeviceRegistration}, new_mutex, prelude::*, - sync::Mutex, + sync::{Arc, Mutex}, types::ARef, uaccess::{UserSlice, UserSliceReader, UserSliceWriter}, };
@@ -112,6 +168,8 @@const RUST_MISC_DEV_HELLO :u32 =_IO ('|' as u32 ,0x80 );const RUST_MISC_DEV_GET_VALUE :u32 =_IOR ::<i32 >('|' as u32 ,0x81 );const RUST_MISC_DEV_SET_VALUE :u32 =_IOW ::<i32 >('|' as u32 ,0x82 ); +const RUST_MISC_DEV_GET_SHARED_VALUE :u32 =_IOR ::<i32 >('|' as u32 ,0x83 ); +const RUST_MISC_DEV_SET_SHARED_VALUE :u32 =_IOW ::<i32 >('|' as u32 ,0x84 );module! {type :RustMiscDeviceModule ,
@@ -129,14 +187,17 @@ struct RustMiscDeviceModule {impl kernel::InPlaceModule for RustMiscDeviceModule {fn init (_module: &'staticThisModule ) ->impl PinInit <Self ,Error > { -pr_info! ("Initialising Rust Misc Device Sample\n" ); +pr_info! ("Initializing Rust Misc Device Sample\n" );let options =MiscDeviceOptions { name:c_str! ("rust-misc-device" ), };try_pin_init! (Self { - _miscdev <-MiscDeviceRegistration ::register (options, ()), + _miscdev <-MiscDeviceRegistration ::register ( + options, +Arc ::pin_init (new_mutex! (Inner { value:0_i32 }),GFP_KERNEL )? + ), }) } }
@@ -147,8 +208,9 @@ struct Inner {#[pin_data( PinnedDrop )] struct RustMiscDevice { + shared:Arc <Mutex <Inner >>,#[pin] - inner:Mutex <Inner >, + unique:Mutex <Inner >, dev:ARef <Device >, }
@@ -156,7 +218,7 @@ struct RustMiscDevice {impl MiscDevice for RustMiscDevice {type Ptr =Pin <KBox <Self >>; -type RegistrationData = (); +type RegistrationData =Arc <Mutex <Inner >>;fn open (_file: &File , misc: &MiscDeviceRegistration <Self >) ->Result <Pin <KBox <Self >>> {let dev =ARef ::from (misc.device ());
@@ -166,7 +228,8 @@ fn open(_file: &File, misc: &MiscDeviceRegistration<Self>) -> Result<Pin<KBox<SeKBox ::try_pin_init (try_pin_init! {RustMiscDevice { - inner <-new_mutex! (Inner { value:0_i32 } ), + shared: misc.data ().clone (), + unique <-new_mutex! (Inner { value:0_i32 }), dev: dev, } },
@@ -182,6 +245,12 @@ fn ioctl(me: Pin<&RustMiscDevice>, _file: &File, cmd: u32, arg: usize) -> Resultmatch cmd {RUST_MISC_DEV_GET_VALUE => me.get_value (UserSlice ::new (arg, size).writer ())?,RUST_MISC_DEV_SET_VALUE => me.set_value (UserSlice ::new (arg, size).reader ())?, +RUST_MISC_DEV_GET_SHARED_VALUE => { + me.get_shared_value (UserSlice ::new (arg, size).writer ())? + } +RUST_MISC_DEV_SET_SHARED_VALUE => { + me.set_shared_value (UserSlice ::new (arg, size).reader ())? + }RUST_MISC_DEV_HELLO => me.hello ()?, _ => {dev_err! (me.dev,"-> IOCTL not recognised: {}\n" , cmd);
@@ -192,7 +261,6 @@ fn ioctl(me: Pin<&RustMiscDevice>, _file: &File, cmd: u32, arg: usize) -> ResultOk (0 ) } } -#[pinned_drop] impl PinnedDrop for RustMiscDevice {fn drop (self:Pin <&mut Self >) {
@@ -203,7 +271,7 @@ fn drop(self: Pin<&mut Self>) {impl RustMiscDevice {fn set_value (&self,mut reader:UserSliceReader ) ->Result <isize > {let new_value = reader.read ::<i32 >()?; -let mut guard = self.inner.lock (); +let mut guard = self.unique.lock ();dev_info! ( self.dev,
@@ -216,7 +284,38 @@ fn set_value(&self, mut reader: UserSliceReader) -> Result<isize> { }fn get_value (&self,mut writer:UserSliceWriter ) ->Result <isize > { -let guard = self.inner.lock (); +let guard = self.unique.lock (); +let value = guard.value; + +// Free-up the lock and use our locally cached instance from here +drop (guard); + +dev_info! ( + self.dev, +"-> Copying data to userspace (value: {})\n" , + &value + ); + + writer.write ::<i32 >(&value)?; +Ok (0 ) + } + +fn set_shared_value (&self,mut reader:UserSliceReader ) ->Result <isize > { +let new_value = reader.read ::<i32 >()?; +let mut guard = self.shared.lock (); + +dev_info! ( + self.dev, +"-> Copying data from userspace (value: {})\n" , + new_value + ); + + guard.value = new_value; +Ok (0 ) + } + +fn get_shared_value (&self,mut writer:UserSliceWriter ) ->Result <isize > { +let guard = self.shared.lock ();let value = guard.value;// Free-up the lock and use our locally cached instance from here
Signature · 1 line
2.48.1