The Linux Kernel Mailing List
 help / color / mirror / Atom feed
* [PATCH] rust: misc: add OWNER field to the `MiscDevice` trait
@ 2026-08-05 12:20 Alexandru Radovici
  2026-08-06  8:28 ` Alice Ryhl
  0 siblings, 1 reply; 2+ messages in thread
From: Alexandru Radovici @ 2026-08-05 12:20 UTC (permalink / raw)
  To: Arnd Bergmann, Greg Kroah-Hartman, Miguel Ojeda, Boqun Feng,
	Gary Guo, Björn Roy Baron, Benno Lossin, Andreas Hindborg,
	Alice Ryhl, Trevor Gross, Danilo Krummrich, Daniel Almeida,
	Tamir Duberstein, Alexandre Courbot, Onur Özkan
  Cc: rust-for-linux, linux-kernel, Alexandru Radovici

Add `OWNER` constant to the `MiscDevice` trait to allow modules
to prevent unloading while the device is in use.

Set the `OWNER` field in the `rust_misc_device` example.

Signed-off-by: Alexandru Radovici <alexandru.radovici@wyliodrin.com>
---
This patch exposes the `owner` field of `file_operations` to the
Rust API `MiscDevice` trait. It allows users to prevent the
unloading of their loadable modules written in Rust
while their `MiscDevice` is still in use.

Without this ability, writing a misc loadable module and running
the following commands will Oops the kernel.

   # insmod misc_module.ko
   # sleep 10 > /dev/misc_device
   # rmmod misc_module

   ... after the sleep, the kernel Oops

I added the `OWNER` field to the `rust_misc_device` example.
---
 rust/kernel/miscdevice.rs        | 13 ++++++++++++-
 samples/rust/rust_misc_device.rs |  4 ++++
 2 files changed, 16 insertions(+), 1 deletion(-)

diff --git a/rust/kernel/miscdevice.rs b/rust/kernel/miscdevice.rs
index 83ce50def5ac..21c5d88c5dc4 100644
--- a/rust/kernel/miscdevice.rs
+++ b/rust/kernel/miscdevice.rs
@@ -31,7 +31,7 @@
         Opaque, //
     },
 };
-use core::marker::PhantomData;
+use core::{marker::PhantomData, ptr};
 
 /// Options for creating a misc device.
 #[derive(Copy, Clone)]
@@ -126,6 +126,12 @@ pub trait MiscDevice: Sized {
     /// What kind of pointer should `Self` be wrapped in.
     type Ptr: ForeignOwnable + Send + Sync;
 
+    /// Set the owner of this `MiscDevice` instance.
+    ///
+    /// In case this is compiled as a module, setting this prevents
+    /// the module unloading while the device is still in use.
+    const OWNER: Option<&'static ThisModule> = None;
+
     /// Called when the misc device is opened.
     ///
     /// The returned pointer will be stored as the private data for the file.
@@ -430,6 +436,11 @@ impl<T: MiscDevice> MiscdeviceVTable<T> {
         } else {
             None
         },
+        #[cfg(CONFIG_MODULES)]
+        owner: match T::OWNER {
+            Some(module) => module.as_ptr(),
+            None => ptr::null_mut(),
+        },
         ..pin_init::zeroed()
     };
 
diff --git a/samples/rust/rust_misc_device.rs b/samples/rust/rust_misc_device.rs
index 41e26c825060..bf8801ea3cb2 100644
--- a/samples/rust/rust_misc_device.rs
+++ b/samples/rust/rust_misc_device.rs
@@ -177,6 +177,10 @@ struct RustMiscDevice {
 impl MiscDevice for RustMiscDevice {
     type Ptr = Pin<KBox<Self>>;
 
+    // Set the OWNER constant to the current module to prevent the unloading
+    // of this module while this device is in use.
+    const OWNER: Option<&'static ThisModule> = Some(&crate::THIS_MODULE);
+
     fn open(_file: &File, misc: &MiscDeviceRegistration<Self>) -> Result<Pin<KBox<Self>>> {
         let dev = ARef::from(misc.device());
 

---
base-commit: c21bb4193868a8de71fc4693fa741e195fdf5d86
change-id: 20260805-rust_tmp-095994661956

Best regards,
-- 
Alexandru Radovici <alexandru.radovici@wyliodrin.com>


^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [PATCH] rust: misc: add OWNER field to the `MiscDevice` trait
  2026-08-05 12:20 [PATCH] rust: misc: add OWNER field to the `MiscDevice` trait Alexandru Radovici
@ 2026-08-06  8:28 ` Alice Ryhl
  0 siblings, 0 replies; 2+ messages in thread
From: Alice Ryhl @ 2026-08-06  8:28 UTC (permalink / raw)
  To: Alexandru Radovici
  Cc: Arnd Bergmann, Greg Kroah-Hartman, Miguel Ojeda, Boqun Feng,
	Gary Guo, Björn Roy Baron, Benno Lossin, Andreas Hindborg,
	Trevor Gross, Danilo Krummrich, Daniel Almeida, Tamir Duberstein,
	Alexandre Courbot, Onur Özkan, rust-for-linux, linux-kernel

On Wed, Aug 05, 2026 at 03:20:07PM +0300, Alexandru Radovici wrote:
> Add `OWNER` constant to the `MiscDevice` trait to allow modules
> to prevent unloading while the device is in use.
> 
> Set the `OWNER` field in the `rust_misc_device` example.
> 
> Signed-off-by: Alexandru Radovici <alexandru.radovici@wyliodrin.com>

This is already being solved by:
https://lore.kernel.org/all/20260723-fix-fops-owner-v9-0-c1c3af7f7bcb@linux.dev/

> This patch exposes the `owner` field of `file_operations` to the
> Rust API `MiscDevice` trait. It allows users to prevent the
> unloading of their loadable modules written in Rust
> while their `MiscDevice` is still in use.
> 
> Without this ability, writing a misc loadable module and running
> the following commands will Oops the kernel.
> 
>    # insmod misc_module.ko
>    # sleep 10 > /dev/misc_device
>    # rmmod misc_module
> 
>    ... after the sleep, the kernel Oops
> 
> I added the `OWNER` field to the `rust_misc_device` example.
> ---
>  rust/kernel/miscdevice.rs        | 13 ++++++++++++-
>  samples/rust/rust_misc_device.rs |  4 ++++
>  2 files changed, 16 insertions(+), 1 deletion(-)
> 
> diff --git a/rust/kernel/miscdevice.rs b/rust/kernel/miscdevice.rs
> index 83ce50def5ac..21c5d88c5dc4 100644
> --- a/rust/kernel/miscdevice.rs
> +++ b/rust/kernel/miscdevice.rs
> @@ -31,7 +31,7 @@
>          Opaque, //
>      },
>  };
> -use core::marker::PhantomData;
> +use core::{marker::PhantomData, ptr};
>  
>  /// Options for creating a misc device.
>  #[derive(Copy, Clone)]
> @@ -126,6 +126,12 @@ pub trait MiscDevice: Sized {
>      /// What kind of pointer should `Self` be wrapped in.
>      type Ptr: ForeignOwnable + Send + Sync;
>  
> +    /// Set the owner of this `MiscDevice` instance.
> +    ///
> +    /// In case this is compiled as a module, setting this prevents
> +    /// the module unloading while the device is still in use.
> +    const OWNER: Option<&'static ThisModule> = None;
> +
>      /// Called when the misc device is opened.
>      ///
>      /// The returned pointer will be stored as the private data for the file.
> @@ -430,6 +436,11 @@ impl<T: MiscDevice> MiscdeviceVTable<T> {
>          } else {
>              None
>          },
> +        #[cfg(CONFIG_MODULES)]
> +        owner: match T::OWNER {
> +            Some(module) => module.as_ptr(),
> +            None => ptr::null_mut(),
> +        },
>          ..pin_init::zeroed()
>      };
>  
> diff --git a/samples/rust/rust_misc_device.rs b/samples/rust/rust_misc_device.rs
> index 41e26c825060..bf8801ea3cb2 100644
> --- a/samples/rust/rust_misc_device.rs
> +++ b/samples/rust/rust_misc_device.rs
> @@ -177,6 +177,10 @@ struct RustMiscDevice {
>  impl MiscDevice for RustMiscDevice {
>      type Ptr = Pin<KBox<Self>>;
>  
> +    // Set the OWNER constant to the current module to prevent the unloading
> +    // of this module while this device is in use.
> +    const OWNER: Option<&'static ThisModule> = Some(&crate::THIS_MODULE);
> +
>      fn open(_file: &File, misc: &MiscDeviceRegistration<Self>) -> Result<Pin<KBox<Self>>> {
>          let dev = ARef::from(misc.device());
>  
> 
> ---
> base-commit: c21bb4193868a8de71fc4693fa741e195fdf5d86
> change-id: 20260805-rust_tmp-095994661956
> 
> Best regards,
> -- 
> Alexandru Radovici <alexandru.radovici@wyliodrin.com>
> 

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2026-08-06  8:28 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-08-05 12:20 [PATCH] rust: misc: add OWNER field to the `MiscDevice` trait Alexandru Radovici
2026-08-06  8:28 ` Alice Ryhl

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox