Share the mutex stored in RustMiscDevice between all instances using an Arc
and the RegistrationData of MiscDeviceRegistration.
This is mostly to Demonstrate the capability to share data in this way.
Signed-off-by: Christian Schrefl <chrisi.schrefl@gmail.com>
---
samples/rust/rust_misc_device.rs | 22 +++++++++++-----------
1 file changed, 11 insertions(+), 11 deletions(-)
diff --git a/samples/rust/rust_misc_device.rs b/samples/rust/rust_misc_device.rs
index 779fcfd64119bdd5b4f8be740f7e8336c652b4d3..956b3b2bd8ef7455fcfe283dd140690ac325c0fb 100644
--- a/samples/rust/rust_misc_device.rs
+++ b/samples/rust/rust_misc_device.rs
@@ -104,7 +104,7 @@
miscdevice::{MiscDevice, MiscDeviceOptions, MiscDeviceRegistration},
new_mutex,
prelude::*,
- sync::Mutex,
+ sync::{Arc, Mutex},
types::ARef,
uaccess::{UserSlice, UserSliceReader, UserSliceWriter},
};
@@ -136,7 +136,10 @@ fn init(_module: &'static ThisModule) -> impl PinInit<Self, Error> {
};
try_pin_init!(Self {
- _miscdev <- MiscDeviceRegistration::register(options, ()),
+ _miscdev <- MiscDeviceRegistration::register(
+ options,
+ Arc::pin_init(new_mutex!(Inner { value: 0_i32 }), GFP_KERNEL)?
+ ),
})
}
}
@@ -145,10 +148,8 @@ struct Inner {
value: i32,
}
-#[pin_data(PinnedDrop)]
struct RustMiscDevice {
- #[pin]
- inner: Mutex<Inner>,
+ inner: Arc<Mutex<Inner>>,
dev: ARef<Device>,
}
@@ -156,7 +157,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());
@@ -164,9 +165,9 @@ fn open(_file: &File, misc: &MiscDeviceRegistration<Self>) -> Result<Pin<KBox<Se
dev_info!(dev, "Opening Rust Misc Device Sample\n");
KBox::try_pin_init(
- try_pin_init! {
+ try_init! {
RustMiscDevice {
- inner <- new_mutex!( Inner{ value: 0_i32 } ),
+ inner: misc.data().clone(),
dev: dev,
}
},
@@ -193,9 +194,8 @@ fn ioctl(me: Pin<&RustMiscDevice>, _file: &File, cmd: u32, arg: usize) -> Result
}
}
-#[pinned_drop]
-impl PinnedDrop for RustMiscDevice {
- fn drop(self: Pin<&mut Self>) {
+impl Drop for RustMiscDevice {
+ fn drop(&mut self) {
dev_info!(self.dev, "Exiting the Rust Misc Device Sample\n");
}
}
--
2.48.1
On Sun, Jan 19, 2025 at 11:11 PM Christian Schrefl <chrisi.schrefl@gmail.com> wrote: > > Share the mutex stored in RustMiscDevice between all instances using an Arc > and the RegistrationData of MiscDeviceRegistration. > > This is mostly to Demonstrate the capability to share data in this way. > > Signed-off-by: Christian Schrefl <chrisi.schrefl@gmail.com> This change causes all open files to share the same value, instead of it being per-fd. Alice
Hi Alice On 21.01.25 4:40 PM, Alice Ryhl wrote: > On Sun, Jan 19, 2025 at 11:11 PM Christian Schrefl > <chrisi.schrefl@gmail.com> wrote: >> >> Share the mutex stored in RustMiscDevice between all instances using an Arc >> and the RegistrationData of MiscDeviceRegistration. >> >> This is mostly to Demonstrate the capability to share data in this way. >> >> Signed-off-by: Christian Schrefl <chrisi.schrefl@gmail.com> > > This change causes all open files to share the same value, instead of > it being per-fd. I know, if that is unwanted I'm fine with dropping this patch, it is mostly here to show how patch 2 can be used. > > Alice Cheers Christian
On Thu, Jan 23, 2025 at 6:57 PM Christian Schrefl <chrisi.schrefl@gmail.com> wrote: > > Hi Alice > > On 21.01.25 4:40 PM, Alice Ryhl wrote: > > On Sun, Jan 19, 2025 at 11:11 PM Christian Schrefl > > <chrisi.schrefl@gmail.com> wrote: > >> > >> Share the mutex stored in RustMiscDevice between all instances using an Arc > >> and the RegistrationData of MiscDeviceRegistration. > >> > >> This is mostly to Demonstrate the capability to share data in this way. > >> > >> Signed-off-by: Christian Schrefl <chrisi.schrefl@gmail.com> > > > > This change causes all open files to share the same value, instead of > > it being per-fd. > > I know, if that is unwanted I'm fine with dropping this patch, > it is mostly here to show how patch 2 can be used. Perhaps instead of changing the per-fd value, we could add a new shared value? E.g., it could have a counter for the number of open files. Alice
On Fri, Jan 24, 2025 at 08:29:38AM +0100, Alice Ryhl wrote: > On Thu, Jan 23, 2025 at 6:57 PM Christian Schrefl > <chrisi.schrefl@gmail.com> wrote: > > > > Hi Alice > > > > On 21.01.25 4:40 PM, Alice Ryhl wrote: > > > On Sun, Jan 19, 2025 at 11:11 PM Christian Schrefl > > > <chrisi.schrefl@gmail.com> wrote: > > >> > > >> Share the mutex stored in RustMiscDevice between all instances using an Arc > > >> and the RegistrationData of MiscDeviceRegistration. > > >> > > >> This is mostly to Demonstrate the capability to share data in this way. > > >> > > >> Signed-off-by: Christian Schrefl <chrisi.schrefl@gmail.com> > > > > > > This change causes all open files to share the same value, instead of > > > it being per-fd. > > > > I know, if that is unwanted I'm fine with dropping this patch, > > it is mostly here to show how patch 2 can be used. > > Perhaps instead of changing the per-fd value, we could add a new > shared value? E.g., it could have a counter for the number of open > files. Counters don't work, sorry (think about dup() for file handles), please, either make it per-file handle, or a "global" thing for the specific object, don't attempt to count open/release calls, the vfs does this for us already. thanks, greg k-h
On Fri, Jan 24, 2025 at 9:06 AM Greg Kroah-Hartman <gregkh@linuxfoundation.org> wrote: > > On Fri, Jan 24, 2025 at 08:29:38AM +0100, Alice Ryhl wrote: > > On Thu, Jan 23, 2025 at 6:57 PM Christian Schrefl > > <chrisi.schrefl@gmail.com> wrote: > > > > > > Hi Alice > > > > > > On 21.01.25 4:40 PM, Alice Ryhl wrote: > > > > On Sun, Jan 19, 2025 at 11:11 PM Christian Schrefl > > > > <chrisi.schrefl@gmail.com> wrote: > > > >> > > > >> Share the mutex stored in RustMiscDevice between all instances using an Arc > > > >> and the RegistrationData of MiscDeviceRegistration. > > > >> > > > >> This is mostly to Demonstrate the capability to share data in this way. > > > >> > > > >> Signed-off-by: Christian Schrefl <chrisi.schrefl@gmail.com> > > > > > > > > This change causes all open files to share the same value, instead of > > > > it being per-fd. > > > > > > I know, if that is unwanted I'm fine with dropping this patch, > > > it is mostly here to show how patch 2 can be used. > > > > Perhaps instead of changing the per-fd value, we could add a new > > shared value? E.g., it could have a counter for the number of open > > files. > > Counters don't work, sorry (think about dup() for file handles), please, > either make it per-file handle, or a "global" thing for the specific > object, don't attempt to count open/release calls, the vfs does this for > us already. I mean, it's just for an example, shrug. It could also be another ioctl that updates the shared value. Alice
On Fri, Jan 24, 2025 at 10:42:53AM +0100, Alice Ryhl wrote: > On Fri, Jan 24, 2025 at 9:06 AM Greg Kroah-Hartman > <gregkh@linuxfoundation.org> wrote: > > > > On Fri, Jan 24, 2025 at 08:29:38AM +0100, Alice Ryhl wrote: > > > On Thu, Jan 23, 2025 at 6:57 PM Christian Schrefl > > > <chrisi.schrefl@gmail.com> wrote: > > > > > > > > Hi Alice > > > > > > > > On 21.01.25 4:40 PM, Alice Ryhl wrote: > > > > > On Sun, Jan 19, 2025 at 11:11 PM Christian Schrefl > > > > > <chrisi.schrefl@gmail.com> wrote: > > > > >> > > > > >> Share the mutex stored in RustMiscDevice between all instances using an Arc > > > > >> and the RegistrationData of MiscDeviceRegistration. > > > > >> > > > > >> This is mostly to Demonstrate the capability to share data in this way. > > > > >> > > > > >> Signed-off-by: Christian Schrefl <chrisi.schrefl@gmail.com> > > > > > > > > > > This change causes all open files to share the same value, instead of > > > > > it being per-fd. > > > > > > > > I know, if that is unwanted I'm fine with dropping this patch, > > > > it is mostly here to show how patch 2 can be used. > > > > > > Perhaps instead of changing the per-fd value, we could add a new > > > shared value? E.g., it could have a counter for the number of open > > > files. > > > > Counters don't work, sorry (think about dup() for file handles), please, > > either make it per-file handle, or a "global" thing for the specific > > object, don't attempt to count open/release calls, the vfs does this for > > us already. > > I mean, it's just for an example, shrug. It could also be another > ioctl that updates the shared value. Sure, but the number of times I've seen sample code copied into "real" code is way too high. Also, getting people to stop thinking they even can count the number of open file handles is a good idea as that's an extremely common "anti-pattern" in way too many drivers even today (i.e. if you try to count open calls, or even restrict the number of open calls to attempt some sort of control, you're doing it totally wrong.) thanks, greg k-h
On Fri, Jan 24, 2025 at 11:34 AM Greg Kroah-Hartman <gregkh@linuxfoundation.org> wrote: > > On Fri, Jan 24, 2025 at 10:42:53AM +0100, Alice Ryhl wrote: > > On Fri, Jan 24, 2025 at 9:06 AM Greg Kroah-Hartman > > <gregkh@linuxfoundation.org> wrote: > > > > > > On Fri, Jan 24, 2025 at 08:29:38AM +0100, Alice Ryhl wrote: > > > > On Thu, Jan 23, 2025 at 6:57 PM Christian Schrefl > > > > <chrisi.schrefl@gmail.com> wrote: > > > > > > > > > > Hi Alice > > > > > > > > > > On 21.01.25 4:40 PM, Alice Ryhl wrote: > > > > > > On Sun, Jan 19, 2025 at 11:11 PM Christian Schrefl > > > > > > <chrisi.schrefl@gmail.com> wrote: > > > > > >> > > > > > >> Share the mutex stored in RustMiscDevice between all instances using an Arc > > > > > >> and the RegistrationData of MiscDeviceRegistration. > > > > > >> > > > > > >> This is mostly to Demonstrate the capability to share data in this way. > > > > > >> > > > > > >> Signed-off-by: Christian Schrefl <chrisi.schrefl@gmail.com> > > > > > > > > > > > > This change causes all open files to share the same value, instead of > > > > > > it being per-fd. > > > > > > > > > > I know, if that is unwanted I'm fine with dropping this patch, > > > > > it is mostly here to show how patch 2 can be used. > > > > > > > > Perhaps instead of changing the per-fd value, we could add a new > > > > shared value? E.g., it could have a counter for the number of open > > > > files. > > > > > > Counters don't work, sorry (think about dup() for file handles), please, > > > either make it per-file handle, or a "global" thing for the specific > > > object, don't attempt to count open/release calls, the vfs does this for > > > us already. > > > > I mean, it's just for an example, shrug. It could also be another > > ioctl that updates the shared value. > > Sure, but the number of times I've seen sample code copied into "real" > code is way too high. Also, getting people to stop thinking they even > can count the number of open file handles is a good idea as that's an > extremely common "anti-pattern" in way too many drivers even today (i.e. > if you try to count open calls, or even restrict the number of open > calls to attempt some sort of control, you're doing it totally wrong.) Fair point. Do you have good ideas for some data we could put in the data shared by all instances of the file? Alice
On Fri, Jan 24, 2025 at 11:39:23AM +0100, Alice Ryhl wrote: > On Fri, Jan 24, 2025 at 11:34 AM Greg Kroah-Hartman > <gregkh@linuxfoundation.org> wrote: > > > > On Fri, Jan 24, 2025 at 10:42:53AM +0100, Alice Ryhl wrote: > > > On Fri, Jan 24, 2025 at 9:06 AM Greg Kroah-Hartman > > > <gregkh@linuxfoundation.org> wrote: > > > > > > > > On Fri, Jan 24, 2025 at 08:29:38AM +0100, Alice Ryhl wrote: > > > > > On Thu, Jan 23, 2025 at 6:57 PM Christian Schrefl > > > > > <chrisi.schrefl@gmail.com> wrote: > > > > > > > > > > > > Hi Alice > > > > > > > > > > > > On 21.01.25 4:40 PM, Alice Ryhl wrote: > > > > > > > On Sun, Jan 19, 2025 at 11:11 PM Christian Schrefl > > > > > > > <chrisi.schrefl@gmail.com> wrote: > > > > > > >> > > > > > > >> Share the mutex stored in RustMiscDevice between all instances using an Arc > > > > > > >> and the RegistrationData of MiscDeviceRegistration. > > > > > > >> > > > > > > >> This is mostly to Demonstrate the capability to share data in this way. > > > > > > >> > > > > > > >> Signed-off-by: Christian Schrefl <chrisi.schrefl@gmail.com> > > > > > > > > > > > > > > This change causes all open files to share the same value, instead of > > > > > > > it being per-fd. > > > > > > > > > > > > I know, if that is unwanted I'm fine with dropping this patch, > > > > > > it is mostly here to show how patch 2 can be used. > > > > > > > > > > Perhaps instead of changing the per-fd value, we could add a new > > > > > shared value? E.g., it could have a counter for the number of open > > > > > files. > > > > > > > > Counters don't work, sorry (think about dup() for file handles), please, > > > > either make it per-file handle, or a "global" thing for the specific > > > > object, don't attempt to count open/release calls, the vfs does this for > > > > us already. > > > > > > I mean, it's just for an example, shrug. It could also be another > > > ioctl that updates the shared value. > > > > Sure, but the number of times I've seen sample code copied into "real" > > code is way too high. Also, getting people to stop thinking they even > > can count the number of open file handles is a good idea as that's an > > extremely common "anti-pattern" in way too many drivers even today (i.e. > > if you try to count open calls, or even restrict the number of open > > calls to attempt some sort of control, you're doing it totally wrong.) > > Fair point. Do you have good ideas for some data we could put in the > data shared by all instances of the file? "shared_cookie"? Read/write and let readers and writers access it. But I thought that was what the misc example did already, but I might be getting confused here, it's been a while since I looked at the code, sorry. greg k-h
On Fri, Jan 24, 2025 at 12:22 PM Greg Kroah-Hartman <gregkh@linuxfoundation.org> wrote: > > On Fri, Jan 24, 2025 at 11:39:23AM +0100, Alice Ryhl wrote: > > On Fri, Jan 24, 2025 at 11:34 AM Greg Kroah-Hartman > > <gregkh@linuxfoundation.org> wrote: > > > > > > On Fri, Jan 24, 2025 at 10:42:53AM +0100, Alice Ryhl wrote: > > > > On Fri, Jan 24, 2025 at 9:06 AM Greg Kroah-Hartman > > > > <gregkh@linuxfoundation.org> wrote: > > > > > > > > > > On Fri, Jan 24, 2025 at 08:29:38AM +0100, Alice Ryhl wrote: > > > > > > On Thu, Jan 23, 2025 at 6:57 PM Christian Schrefl > > > > > > <chrisi.schrefl@gmail.com> wrote: > > > > > > > > > > > > > > Hi Alice > > > > > > > > > > > > > > On 21.01.25 4:40 PM, Alice Ryhl wrote: > > > > > > > > On Sun, Jan 19, 2025 at 11:11 PM Christian Schrefl > > > > > > > > <chrisi.schrefl@gmail.com> wrote: > > > > > > > >> > > > > > > > >> Share the mutex stored in RustMiscDevice between all instances using an Arc > > > > > > > >> and the RegistrationData of MiscDeviceRegistration. > > > > > > > >> > > > > > > > >> This is mostly to Demonstrate the capability to share data in this way. > > > > > > > >> > > > > > > > >> Signed-off-by: Christian Schrefl <chrisi.schrefl@gmail.com> > > > > > > > > > > > > > > > > This change causes all open files to share the same value, instead of > > > > > > > > it being per-fd. > > > > > > > > > > > > > > I know, if that is unwanted I'm fine with dropping this patch, > > > > > > > it is mostly here to show how patch 2 can be used. > > > > > > > > > > > > Perhaps instead of changing the per-fd value, we could add a new > > > > > > shared value? E.g., it could have a counter for the number of open > > > > > > files. > > > > > > > > > > Counters don't work, sorry (think about dup() for file handles), please, > > > > > either make it per-file handle, or a "global" thing for the specific > > > > > object, don't attempt to count open/release calls, the vfs does this for > > > > > us already. > > > > > > > > I mean, it's just for an example, shrug. It could also be another > > > > ioctl that updates the shared value. > > > > > > Sure, but the number of times I've seen sample code copied into "real" > > > code is way too high. Also, getting people to stop thinking they even > > > can count the number of open file handles is a good idea as that's an > > > extremely common "anti-pattern" in way too many drivers even today (i.e. > > > if you try to count open calls, or even restrict the number of open > > > calls to attempt some sort of control, you're doing it totally wrong.) > > > > Fair point. Do you have good ideas for some data we could put in the > > data shared by all instances of the file? > > "shared_cookie"? Read/write and let readers and writers access it. But > I thought that was what the misc example did already, but I might be > getting confused here, it's been a while since I looked at the code, > sorry. Makes sense to me. The existing cookie is per `struct file`, but this series is about making it possible to have data shared by all instances of a miscdevice. But having two cookies, one that is per `struct file` and one that is global makes sense to me. Alice
On 24.01.25 12:37 PM, Alice Ryhl wrote: > On Fri, Jan 24, 2025 at 12:22 PM Greg Kroah-Hartman > <gregkh@linuxfoundation.org> wrote: >> >> On Fri, Jan 24, 2025 at 11:39:23AM +0100, Alice Ryhl wrote: >>> On Fri, Jan 24, 2025 at 11:34 AM Greg Kroah-Hartman >>> <gregkh@linuxfoundation.org> wrote: >>>> >>>> On Fri, Jan 24, 2025 at 10:42:53AM +0100, Alice Ryhl wrote: >>>>> On Fri, Jan 24, 2025 at 9:06 AM Greg Kroah-Hartman >>>>> <gregkh@linuxfoundation.org> wrote: >>>>>> >>>>>> On Fri, Jan 24, 2025 at 08:29:38AM +0100, Alice Ryhl wrote: >>>>>>> On Thu, Jan 23, 2025 at 6:57 PM Christian Schrefl >>>>>>> <chrisi.schrefl@gmail.com> wrote: >>>>>>>> >>>>>>>> Hi Alice >>>>>>>> >>>>>>>> On 21.01.25 4:40 PM, Alice Ryhl wrote: >>>>>>>>> On Sun, Jan 19, 2025 at 11:11 PM Christian Schrefl >>>>>>>>> <chrisi.schrefl@gmail.com> wrote: >>>>>>>>>> >>>>>>>>>> Share the mutex stored in RustMiscDevice between all instances using an Arc >>>>>>>>>> and the RegistrationData of MiscDeviceRegistration. >>>>>>>>>> >>>>>>>>>> This is mostly to Demonstrate the capability to share data in this way. >>>>>>>>>> >>>>>>>>>> Signed-off-by: Christian Schrefl <chrisi.schrefl@gmail.com> >>>>>>>>> >>>>>>>>> This change causes all open files to share the same value, instead of >>>>>>>>> it being per-fd. >>>>>>>> >>>>>>>> I know, if that is unwanted I'm fine with dropping this patch, >>>>>>>> it is mostly here to show how patch 2 can be used. >>>>>>> >>>>>>> Perhaps instead of changing the per-fd value, we could add a new >>>>>>> shared value? E.g., it could have a counter for the number of open >>>>>>> files. >>>>>> >>>>>> Counters don't work, sorry (think about dup() for file handles), please, >>>>>> either make it per-file handle, or a "global" thing for the specific >>>>>> object, don't attempt to count open/release calls, the vfs does this for >>>>>> us already. >>>>> >>>>> I mean, it's just for an example, shrug. It could also be another >>>>> ioctl that updates the shared value. >>>> >>>> Sure, but the number of times I've seen sample code copied into "real" >>>> code is way too high. Also, getting people to stop thinking they even >>>> can count the number of open file handles is a good idea as that's an >>>> extremely common "anti-pattern" in way too many drivers even today (i.e. >>>> if you try to count open calls, or even restrict the number of open >>>> calls to attempt some sort of control, you're doing it totally wrong.) >>> >>> Fair point. Do you have good ideas for some data we could put in the >>> data shared by all instances of the file? >> >> "shared_cookie"? Read/write and let readers and writers access it. But >> I thought that was what the misc example did already, but I might be >> getting confused here, it's been a while since I looked at the code, >> sorry. > > Makes sense to me. > > The existing cookie is per `struct file`, but this series is about > making it possible to have data shared by all instances of a > miscdevice. But having two cookies, one that is per `struct file` and > one that is global makes sense to me. I've Implemented one shared (Every instance) and one unique(One for every FD) data. Christian
© 2016 - 2026 Red Hat, Inc.