summaryrefslogtreecommitdiff
path: root/rust/kernel/platform.rs
diff options
context:
space:
mode:
authorDanilo Krummrich <dakr@kernel.org>2025-01-03 17:46:03 +0100
committerGreg Kroah-Hartman <gregkh@linuxfoundation.org>2025-01-07 11:31:45 +0100
commite1a51c2bf4b3b20868a0e6e9520b11639bd363f1 (patch)
tree2a1be8a3b07303bd8432fabb28e6e0ee09fd8661 /rust/kernel/platform.rs
parent9b880189327b9727640147253f3236ec5b3f704f (diff)
rust: driver: address soundness issue in `RegistrationOps`
The `RegistrationOps` trait holds some obligations to the caller and implementers. While being documented, the trait and the corresponding functions haven't been marked as unsafe. Hence, markt the trait and functions unsafe and add the corresponding safety comments. This patch does not include any fuctional changes. Reported-by: Gary Guo <gary@garyguo.net> Closes: https://lore.kernel.org/rust-for-linux/20241224195821.3b43302b.gary@garyguo.net/ Signed-off-by: Danilo Krummrich <dakr@kernel.org> Reviewed-by: Gary Guo <gary@garyguo.net> Link: https://lore.kernel.org/r/20250103164655.96590-4-dakr@kernel.org Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Diffstat (limited to 'rust/kernel/platform.rs')
-rw-r--r--rust/kernel/platform.rs8
1 files changed, 5 insertions, 3 deletions
diff --git a/rust/kernel/platform.rs b/rust/kernel/platform.rs
index 03287794f9d0..50e6b0421813 100644
--- a/rust/kernel/platform.rs
+++ b/rust/kernel/platform.rs
@@ -19,10 +19,12 @@ use core::ptr::addr_of_mut;
/// An adapter for the registration of platform drivers.
pub struct Adapter<T: Driver>(T);
-impl<T: Driver + 'static> driver::RegistrationOps for Adapter<T> {
+// SAFETY: A call to `unregister` for a given instance of `RegType` is guaranteed to be valid if
+// a preceding call to `register` has been successful.
+unsafe impl<T: Driver + 'static> driver::RegistrationOps for Adapter<T> {
type RegType = bindings::platform_driver;
- fn register(
+ unsafe fn register(
pdrv: &Opaque<Self::RegType>,
name: &'static CStr,
module: &'static ThisModule,
@@ -44,7 +46,7 @@ impl<T: Driver + 'static> driver::RegistrationOps for Adapter<T> {
to_result(unsafe { bindings::__platform_driver_register(pdrv.get(), module.0) })
}
- fn unregister(pdrv: &Opaque<Self::RegType>) {
+ unsafe fn unregister(pdrv: &Opaque<Self::RegType>) {
// SAFETY: `pdrv` is guaranteed to be a valid `RegType`.
unsafe { bindings::platform_driver_unregister(pdrv.get()) };
}