rust: misc: add OWNER field to the `MiscDevice` trait
Subsystems (2)
PATCH 1/1 rust: misc: add OWNER field to the `MiscDevice` trait
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.
---
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(-)
rust/kernel/miscdevice.rs +12 -1
@@ -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 <&'staticThisModule > =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 () };
samples/rust/rust_misc_device.rs +4 -0
@@ -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 <&'staticThisModule > =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,
Signature · 1 line
Alexandru Radovici <[email protected]>
Alice Ryhl
reviewerQuoted 6 lines · Alexandru Radovici
Add
OWNERconstant to theMiscDevicetrait to allow modules to prevent unloading while the device is in use.Set the
OWNERfield in therust_misc_deviceexample.Signed-off-by Alexandru Radovici <[email protected]>
This is already being solved by:
https://lore.kernel.org/all/[email protected]/
Quoted 82 lines
This patch exposes the
ownerfield offile_operationsto the Rust APIMiscDevicetrait. It allows users to prevent the unloading of their loadable modules written in Rust while theirMiscDeviceis 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
OWNERfield to therust_misc_deviceexample.--- rust/kernel/miscdevice.rs | 13 ++++++++++++- samples/rust/rust_misc_device.rs | 4 ++++ 2 files changed, 16 insertions(+), 1 deletion(-)rust/kernel/miscdevice.rs +12
-1@@ -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 <&'staticThisModule > =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 () };samples/rust/rust_misc_device.rs +4
-1@@ -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 <&'staticThisModule > =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-095994661956Best regards,
Signature · 1 line
Alexandru Radovici <[email protected]>