Skip to content

Commit 570300f

Browse files
authored
Merge pull request #160 from wedsonaf/miscdev
Make `miscdevice` a direct field so that we can use `offset_of`.
2 parents b1d2cf1 + 7b7725e commit 570300f

File tree

1 file changed

+18
-22
lines changed

1 file changed

+18
-22
lines changed

rust/kernel/miscdev.rs

Lines changed: 18 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ use core::pin::Pin;
1515

1616
/// A registration of a miscellaneous device.
1717
pub struct Registration {
18-
mdev: Option<bindings::miscdevice>,
18+
registered: bool,
19+
mdev: bindings::miscdevice,
1920
_pin: PhantomPinned,
2021
}
2122

@@ -25,7 +26,8 @@ impl Registration {
2526
/// It is allowed to move.
2627
pub fn new() -> Self {
2728
Self {
28-
mdev: None,
29+
registered: false,
30+
mdev: bindings::miscdevice::default(),
2931
_pin: PhantomPinned,
3032
}
3133
}
@@ -44,31 +46,29 @@ impl Registration {
4446

4547
/// Registers a miscellaneous device with the rest of the kernel.
4648
///
47-
/// It must be pinned because the memory block that represents the
48-
/// registration is self-referential. If a minor is not given, the kernel
49-
/// allocates a new one if possible.
49+
/// It must be pinned because the memory block that represents the registration is
50+
/// self-referential. If a minor is not given, the kernel allocates a new one if possible.
5051
pub fn register<T: FileOperations>(
5152
self: Pin<&mut Self>,
5253
name: CStr<'static>,
5354
minor: Option<i32>,
5455
) -> KernelResult {
5556
// SAFETY: We must ensure that we never move out of `this`.
5657
let this = unsafe { self.get_unchecked_mut() };
57-
if this.mdev.is_some() {
58+
if this.registered {
5859
// Already registered.
5960
return Err(Error::EINVAL);
6061
}
6162

62-
this.mdev = Some(bindings::miscdevice::default());
63-
let dev = this.mdev.as_mut().unwrap();
64-
dev.fops = FileOperationsVtable::<T>::build();
65-
dev.name = name.as_ptr() as *const c_types::c_char;
66-
dev.minor = minor.unwrap_or(bindings::MISC_DYNAMIC_MINOR as i32);
67-
let ret = unsafe { bindings::misc_register(dev) };
63+
this.mdev.fops = FileOperationsVtable::<T>::build();
64+
this.mdev.name = name.as_ptr() as *const c_types::c_char;
65+
this.mdev.minor = minor.unwrap_or(bindings::MISC_DYNAMIC_MINOR as i32);
66+
67+
let ret = unsafe { bindings::misc_register(&mut this.mdev) };
6868
if ret < 0 {
69-
this.mdev = None;
7069
return Err(Error::from_kernel_errno(ret));
7170
}
71+
this.registered = true;
7272
Ok(())
7373
}
7474
}
@@ -79,19 +79,15 @@ impl Default for Registration {
7979
}
8080
}
8181

82-
// SAFETY: The only method is `register()`, which requires a (pinned) mutable
83-
// `Registration`, so it is safe to pass `&Registration` to multiple threads
84-
// because it offers no interior mutability.
82+
// SAFETY: The only method is `register()`, which requires a (pinned) mutable `Registration`, so it
83+
// is safe to pass `&Registration` to multiple threads because it offers no interior mutability.
8584
unsafe impl Sync for Registration {}
8685

8786
impl Drop for Registration {
88-
/// Removes the registration from the kernel if it has completed
89-
/// successfully before.
87+
/// Removes the registration from the kernel if it has completed successfully before.
9088
fn drop(&mut self) {
91-
if let Some(ref mut dev) = self.mdev {
92-
unsafe {
93-
bindings::misc_deregister(dev);
94-
}
89+
if self.registered {
90+
unsafe { bindings::misc_deregister(&mut self.mdev) }
9591
}
9692
}
9793
}

0 commit comments

Comments
 (0)