diff options
author | Danilo Krummrich <dakr@kernel.org> | 2025-04-19 10:10:40 +0200 |
---|---|---|
committer | Danilo Krummrich <dakr@kernel.org> | 2025-04-19 10:10:40 +0200 |
commit | cfec9a16e6800b5489bd361f6b6ea9af65fb1050 (patch) | |
tree | 670ae3fe45014b028dc4cbc269447f1ddd327ef5 /rust/kernel/platform.rs | |
parent | f5b444361435bfc9eeaaae3dd2e9b4f18dae3888 (diff) | |
parent | 7bd1710aac0571d4722f1429a9332c57b3a8feaf (diff) |
Merge tag 'topic/device-context-2025-04-17' into nova-next
Introduce "Bound" device context
Introduce the "Bound" device context, such that it can be ensured to only
ever pass a bound device to APIs that require this precondition. [1]
This tag exists to share the patches from [1] between multiple trees.
[1] https://lore.kernel.org/lkml/20250413173758.12068-1-dakr@kernel.org/
Diffstat (limited to 'rust/kernel/platform.rs')
-rw-r--r-- | rust/kernel/platform.rs | 32 |
1 files changed, 8 insertions, 24 deletions
diff --git a/rust/kernel/platform.rs b/rust/kernel/platform.rs index 4917cb34e2fe..b1c48cd95cd6 100644 --- a/rust/kernel/platform.rs +++ b/rust/kernel/platform.rs @@ -10,13 +10,12 @@ use crate::{ of, prelude::*, str::CStr, - types::{ARef, ForeignOwnable, Opaque}, + types::{ForeignOwnable, Opaque}, ThisModule, }; use core::{ marker::PhantomData, - ops::Deref, ptr::{addr_of_mut, NonNull}, }; @@ -184,31 +183,16 @@ pub struct Device<Ctx: device::DeviceContext = device::Normal>( PhantomData<Ctx>, ); -impl Device { +impl<Ctx: device::DeviceContext> Device<Ctx> { fn as_raw(&self) -> *mut bindings::platform_device { self.0.get() } } -impl Deref for Device<device::Core> { - type Target = Device; - - fn deref(&self) -> &Self::Target { - let ptr: *const Self = self; - - // CAST: `Device<Ctx>` is a transparent wrapper of `Opaque<bindings::platform_device>`. - let ptr = ptr.cast::<Device>(); - - // SAFETY: `ptr` was derived from `&self`. - unsafe { &*ptr } - } -} - -impl From<&Device<device::Core>> for ARef<Device> { - fn from(dev: &Device<device::Core>) -> Self { - (&**dev).into() - } -} +// SAFETY: `Device` is a transparent wrapper of a type that doesn't depend on `Device`'s generic +// argument. +kernel::impl_device_context_deref!(unsafe { Device }); +kernel::impl_device_context_into_aref!(Device); // SAFETY: Instances of `Device` are always reference-counted. unsafe impl crate::types::AlwaysRefCounted for Device { @@ -223,8 +207,8 @@ unsafe impl crate::types::AlwaysRefCounted for Device { } } -impl AsRef<device::Device> for Device { - fn as_ref(&self) -> &device::Device { +impl<Ctx: device::DeviceContext> AsRef<device::Device<Ctx>> for Device<Ctx> { + fn as_ref(&self) -> &device::Device<Ctx> { // SAFETY: By the type invariant of `Self`, `self.as_raw()` is a pointer to a valid // `struct platform_device`. let dev = unsafe { addr_of_mut!((*self.as_raw()).dev) }; |