Some places do not define owner and we have null pointer dereference
bugs which we can fix with this. I have tested miscdevice that it
actually works.
---
drivers/android/binder/rust_binder_main.rs | 2 +-
drivers/block/rnull/rnull.rs | 1 +
drivers/gpu/drm/nova/driver.rs | 1 +
drivers/gpu/drm/tyr/driver.rs | 1 +
lib/find_bit_benchmark_rust.rs | 2 +-
rust/kernel/block/mq/gen_disk.rs | 31 ++++--------------------------
rust/kernel/block/mq/operations.rs | 12 ++++++++++++
rust/kernel/drm/device.rs | 2 +-
rust/kernel/drm/driver.rs | 3 +++
rust/kernel/miscdevice.rs | 4 ++++
samples/rust/rust_misc_device.rs | 1 +
11 files changed, 30 insertions(+), 30 deletions(-)
diff --git a/drivers/android/binder/rust_binder_main.rs
b/drivers/android/binder/rust_binder_main.rs
index 169fe552e32a..7877503c639e 100644
--- a/drivers/android/binder/rust_binder_main.rs
+++ b/drivers/android/binder/rust_binder_main.rs
@@ -311,7 +311,7 @@ unsafe impl<T> Sync for AssertSync<T> {}
let zeroed_ops = unsafe { core::mem::MaybeUninit::zeroed().assume_init() };
let ops = kernel::bindings::file_operations {
- owner: THIS_MODULE.as_ptr(),
+ owner: <THIS_MODULE as ThisModule>::OWNER.as_ptr(),
poll: Some(rust_binder_poll),
unlocked_ioctl: Some(rust_binder_ioctl),
compat_ioctl: Some(bindings::compat_ptr_ioctl),
diff --git a/drivers/block/rnull/rnull.rs b/drivers/block/rnull/rnull.rs
index c9dff74489c1..3360a0f50fc6 100644
--- a/drivers/block/rnull/rnull.rs
+++ b/drivers/block/rnull/rnull.rs
@@ -75,6 +75,7 @@ struct QueueData {
#[vtable]
impl Operations for NullBlkDevice {
type QueueData = KBox<QueueData>;
+ type ThisModule = THIS_MODULE;
#[inline(always)]
fn queue_rq(queue_data: &QueueData, rq: ARef<mq::Request<Self>>, _is_last:
bool) -> Result {
diff --git a/drivers/gpu/drm/nova/driver.rs b/drivers/gpu/drm/nova/driver.rs
index 2246d8e104e0..a49c9848ce2e 100644
--- a/drivers/gpu/drm/nova/driver.rs
+++ b/drivers/gpu/drm/nova/driver.rs
@@ -57,6 +57,7 @@ fn probe(adev: &auxiliary::Device<Core>, _info:
&Self::IdInfo) -> impl PinInit<S
#[vtable]
impl drm::Driver for NovaDriver {
+ type ThisModule = crate::THIS_MODULE;
type Data = NovaData;
type File = File;
type Object = gem::Object<NovaObject>;
diff --git a/drivers/gpu/drm/tyr/driver.rs b/drivers/gpu/drm/tyr/driver.rs
index 264c2362237a..a84825dbd008 100644
--- a/drivers/gpu/drm/tyr/driver.rs
+++ b/drivers/gpu/drm/tyr/driver.rs
@@ -180,6 +180,7 @@ fn drop(self: Pin<&mut Self>) {
#[vtable]
impl drm::Driver for TyrDriver {
+ type ThisModule = crate::THIS_MODULE;
type Data = TyrData;
type File = File;
type Object = drm::gem::Object<TyrObject>;
diff --git a/lib/find_bit_benchmark_rust.rs b/lib/find_bit_benchmark_rust.rs
index 6bdc51de2f30..420a1855b08a 100644
--- a/lib/find_bit_benchmark_rust.rs
+++ b/lib/find_bit_benchmark_rust.rs
@@ -88,7 +88,7 @@ fn find_bit_test() {
}
impl kernel::Module for Benchmark {
- fn init(_module: &'static ThisModule) -> Result<Self> {
+ fn init<M: ThisModule>() -> Result<Self> {
find_bit_test();
// Return error so test module can be inserted again without rmmod.
Err(code::EINVAL)
diff --git a/rust/kernel/block/mq/gen_disk.rs b/rust/kernel/block/mq/gen_disk.rs
index 1ce815c8cdab..f5839829d0b7 100644
--- a/rust/kernel/block/mq/gen_disk.rs
+++ b/rust/kernel/block/mq/gen_disk.rs
@@ -7,7 +7,7 @@
use crate::{
bindings,
- block::mq::{Operations, TagSet},
+ block::mq::{operations::OperationsVTable, Operations, TagSet},
error::{self, from_err_ptr, Result},
fmt::{self, Write},
prelude::*,
@@ -126,32 +126,9 @@ pub fn build<T: Operations>(
)
})?;
- const TABLE: bindings::block_device_operations =
bindings::block_device_operations {
- submit_bio: None,
- open: None,
- release: None,
- ioctl: None,
- compat_ioctl: None,
- check_events: None,
- unlock_native_capacity: None,
- getgeo: None,
- set_read_only: None,
- swap_slot_free_notify: None,
- report_zones: None,
- devnode: None,
- alternative_gpt_sector: None,
- get_unique_id: None,
- // TODO: Set to THIS_MODULE. Waiting for const_refs_to_static
feature to
- // be merged (unstable in rustc 1.78 which is staged for linux
6.10)
- // <https://github.com/rust-lang/rust/issues/119618>
- owner: core::ptr::null_mut(),
- pr_ops: core::ptr::null_mut(),
- free_disk: None,
- poll_bio: None,
- };
-
- // SAFETY: `gendisk` is a valid pointer as we initialized it above
- unsafe { (*gendisk).fops = &TABLE };
+ unsafe {
+ (*gendisk).fops =
OperationsVTable::<T>::build_block_device_operations();
+ }
let mut writer = NullTerminatedFormatter::new(
// SAFETY: `gendisk` points to a valid and initialized instance. We
diff --git a/rust/kernel/block/mq/operations.rs
b/rust/kernel/block/mq/operations.rs
index 8ad46129a52c..3e0e8baa7d07 100644
--- a/rust/kernel/block/mq/operations.rs
+++ b/rust/kernel/block/mq/operations.rs
@@ -31,6 +31,8 @@ pub trait Operations: Sized {
/// Data associated with the `struct request_queue` that is allocated for
/// the `GenDisk` associated with this `Operations` implementation.
type QueueData: ForeignOwnable;
+ /// TODO Doc
+ type ThisModule: ThisModule;
/// Called by the kernel to queue a request with the driver. If `is_last`
is
/// `false`, the driver is allowed to defer committing the request.
@@ -283,4 +285,14 @@ impl<T: Operations> OperationsVTable<T> {
pub(crate) const fn build() -> &'static bindings::blk_mq_ops {
&Self::VTABLE
}
+
+ const BLOCK_OPS: bindings::block_device_operations =
bindings::block_device_operations {
+ owner: T::ThisModule::OWNER.as_ptr(),
+ ..pin_init::zeroed()
+ };
+
+ pub(crate) const fn build_block_device_operations() -> &'static
bindings::block_device_operations
+ {
+ &Self::BLOCK_OPS
+ }
}
diff --git a/rust/kernel/drm/device.rs b/rust/kernel/drm/device.rs
index 3ce8f62a0056..a740c87933d0 100644
--- a/rust/kernel/drm/device.rs
+++ b/rust/kernel/drm/device.rs
@@ -92,7 +92,7 @@ impl<T: drm::Driver> Device<T> {
fops: &Self::GEM_FOPS,
};
- const GEM_FOPS: bindings::file_operations = drm::gem::create_fops();
+ const GEM_FOPS: bindings::file_operations =
drm::gem::create_fops::<T::ThisModule>();
/// Create a new `drm::Device` for a `drm::Driver`.
pub fn new(dev: &device::Device, data: impl PinInit<T::Data, Error>) ->
Result<ARef<Self>> {
diff --git a/rust/kernel/drm/driver.rs b/rust/kernel/drm/driver.rs
index f30ee4c6245c..ae8f7d3b9156 100644
--- a/rust/kernel/drm/driver.rs
+++ b/rust/kernel/drm/driver.rs
@@ -99,6 +99,9 @@ pub trait AllocImpl: super::private::Sealed +
drm::gem::IntoGEMObject {
/// drm_driver` to be registered in the DRM subsystem.
#[vtable]
pub trait Driver {
+ /// TODO Doc
+ type ThisModule: ThisModule;
+
/// Context data associated with the DRM driver
type Data: Sync + Send;
diff --git a/rust/kernel/miscdevice.rs b/rust/kernel/miscdevice.rs
index d698cddcb4a5..08346c52d574 100644
--- a/rust/kernel/miscdevice.rs
+++ b/rust/kernel/miscdevice.rs
@@ -116,6 +116,9 @@ pub trait MiscDevice: Sized {
/// What kind of pointer should `Self` be wrapped in.
type Ptr: ForeignOwnable + Send + Sync;
+ /// TODO Docs
+ type ThisModule: ThisModule;
+
/// Called when the misc device is opened.
///
/// The returned pointer will be stored as the private data for the file.
@@ -389,6 +392,7 @@ impl<T: MiscDevice> MiscdeviceVTable<T> {
}
const VTABLE: bindings::file_operations = bindings::file_operations {
+ owner: T::ThisModule::OWNER.as_ptr(),
open: Some(Self::open),
release: Some(Self::release),
mmap: if T::HAS_MMAP { Some(Self::mmap) } else { None },
diff --git a/samples/rust/rust_misc_device.rs b/samples/rust/rust_misc_device.rs
index 3f1acb6818a5..78d239b26dcc 100644
--- a/samples/rust/rust_misc_device.rs
+++ b/samples/rust/rust_misc_device.rs
@@ -157,6 +157,7 @@ struct RustMiscDevice {
#[vtable]
impl MiscDevice for RustMiscDevice {
type Ptr = Pin<KBox<Self>>;
+ type ThisModule = THIS_MODULE;
fn open(_file: &File, misc: &MiscDeviceRegistration<Self>) ->
Result<Pin<KBox<Self>>> {
let dev = ARef::from(misc.device());
--
2.43.0