Skip to content

Commit 269b6bd

Browse files
committed
rust: add ThisModule to the arguments of driver registration
So that the kernel is aware of any loadable modules hosting drivers and can therefore increment/decrement refcounts as appropriate to prevent unload when the driver is in use. This should be transparent to all drivers registered with macros. Those registered with direct calls will need to pass the extra argument. Signed-off-by: Wedson Almeida Filho <[email protected]>
1 parent 7739ad0 commit 269b6bd

File tree

2 files changed

+14
-6
lines changed

2 files changed

+14
-6
lines changed

rust/kernel/amba.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
77
use crate::{
88
bindings, c_types, device, driver, error::from_kernel_result, io_mem::Resource, power,
9-
str::CStr, to_result, types::PointerWrapper, Error, Result,
9+
str::CStr, to_result, types::PointerWrapper, Error, Result, ThisModule,
1010
};
1111
use core::{marker::PhantomData, ops::Deref};
1212

@@ -72,12 +72,14 @@ where
7272
unsafe fn register(
7373
reg: *mut bindings::amba_driver,
7474
name: &'static CStr,
75+
module: &'static ThisModule,
7576
id_table: *const bindings::amba_id,
7677
) -> Result {
7778
// SAFETY: By the safety requirements of this function (defined in the trait defintion),
7879
// `reg` is non-null and valid.
7980
let amba = unsafe { &mut *reg };
8081
amba.drv.name = name.as_char_ptr();
82+
amba.drv.owner = module.0;
8183
amba.id_table = id_table;
8284
amba.probe = Some(probe_callback::<T>);
8385
amba.remove = Some(remove_callback::<T>);

rust/kernel/driver.rs

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ pub trait DriverOps {
4444
unsafe fn register(
4545
reg: *mut Self::RegType,
4646
name: &'static CStr,
47+
module: &'static ThisModule,
4748
id_table: *const Self::RawIdType,
4849
) -> Result;
4950

@@ -85,17 +86,21 @@ impl<T: DriverOps> Registration<T> {
8586
/// Allocates a pinned registration object and registers it.
8687
///
8788
/// Returns a pinned heap-allocated representation of the registration.
88-
pub fn new_pinned(name: &'static CStr) -> Result<Pin<Box<Self>>> {
89+
pub fn new_pinned(name: &'static CStr, module: &'static ThisModule) -> Result<Pin<Box<Self>>> {
8990
let mut reg = Pin::from(Box::try_new(Self::new())?);
90-
reg.as_mut().register(name)?;
91+
reg.as_mut().register(name, module)?;
9192
Ok(reg)
9293
}
9394

9495
/// Registers a driver with its subsystem.
9596
///
9697
/// It must be pinned because the memory block that represents the registration is potentially
9798
/// self-referential.
98-
pub fn register(self: Pin<&mut Self>, name: &'static CStr) -> Result {
99+
pub fn register(
100+
self: Pin<&mut Self>,
101+
name: &'static CStr,
102+
module: &'static ThisModule,
103+
) -> Result {
99104
// SAFETY: We never move out of `this`.
100105
let this = unsafe { self.get_unchecked_mut() };
101106
if this.is_registered {
@@ -114,6 +119,7 @@ impl<T: DriverOps> Registration<T> {
114119
T::register(
115120
this.concrete_reg.get(),
116121
name,
122+
module,
117123
&this.id_table[0] as *const _ as *const _,
118124
)
119125
}?;
@@ -174,9 +180,9 @@ pub struct Module<T: DriverOps> {
174180
}
175181

176182
impl<T: DriverOps> KernelModule for Module<T> {
177-
fn init(name: &'static CStr, _module: &'static ThisModule) -> Result<Self> {
183+
fn init(name: &'static CStr, module: &'static ThisModule) -> Result<Self> {
178184
Ok(Self {
179-
_driver: Registration::new_pinned(name)?,
185+
_driver: Registration::new_pinned(name, module)?,
180186
})
181187
}
182188
}

0 commit comments

Comments
 (0)