summaryrefslogtreecommitdiffstats
path: root/rust
diff options
context:
space:
mode:
authorAlice Ryhl <aliceryhl@google.com>2024-07-30 13:06:32 +0000
committerMiguel Ojeda <ojeda@kernel.org>2024-08-18 23:10:53 +0200
commit6c2d0ad53b8ff25cb1a12570191576d834e9108d (patch)
tree9017cd105a429a23cd0a85ced61e8fb87df47aaa /rust
parent08f983a55ccf0b015e4788d1a0de0da84e4a7626 (diff)
downloadlinux-6c2d0ad53b8ff25cb1a12570191576d834e9108d.tar.gz
linux-6c2d0ad53b8ff25cb1a12570191576d834e9108d.tar.bz2
linux-6c2d0ad53b8ff25cb1a12570191576d834e9108d.zip
rust: implement ForeignOwnable for Pin<Box<T>>
We already implement ForeignOwnable for Box<T>, but it may be useful to store pinned data in a ForeignOwnable container. This patch makes that possible. This will be used together with upcoming miscdev abstractions, which Binder will use when binderfs is disabled. Signed-off-by: Alice Ryhl <aliceryhl@google.com> Reviewed-by: Benno Lossin <benno.lossin@proton.me> Link: https://lore.kernel.org/r/20240730-foreign-ownable-pin-box-v1-1-b1d70cdae541@google.com Signed-off-by: Miguel Ojeda <ojeda@kernel.org>
Diffstat (limited to 'rust')
-rw-r--r--rust/kernel/types.rs27
1 files changed, 27 insertions, 0 deletions
diff --git a/rust/kernel/types.rs b/rust/kernel/types.rs
index bd189d646adb..132ca1113083 100644
--- a/rust/kernel/types.rs
+++ b/rust/kernel/types.rs
@@ -9,6 +9,7 @@ use core::{
marker::{PhantomData, PhantomPinned},
mem::MaybeUninit,
ops::{Deref, DerefMut},
+ pin::Pin,
ptr::NonNull,
};
@@ -89,6 +90,32 @@ impl<T: 'static> ForeignOwnable for Box<T> {
}
}
+impl<T: 'static> ForeignOwnable for Pin<Box<T>> {
+ type Borrowed<'a> = Pin<&'a T>;
+
+ fn into_foreign(self) -> *const core::ffi::c_void {
+ // SAFETY: We are still treating the box as pinned.
+ Box::into_raw(unsafe { Pin::into_inner_unchecked(self) }) as _
+ }
+
+ unsafe fn borrow<'a>(ptr: *const core::ffi::c_void) -> Pin<&'a T> {
+ // SAFETY: The safety requirements for this function ensure that the object is still alive,
+ // so it is safe to dereference the raw pointer.
+ // The safety requirements of `from_foreign` also ensure that the object remains alive for
+ // the lifetime of the returned value.
+ let r = unsafe { &*ptr.cast() };
+
+ // SAFETY: This pointer originates from a `Pin<Box<T>>`.
+ unsafe { Pin::new_unchecked(r) }
+ }
+
+ unsafe fn from_foreign(ptr: *const core::ffi::c_void) -> Self {
+ // SAFETY: The safety requirements of this function ensure that `ptr` comes from a previous
+ // call to `Self::into_foreign`.
+ unsafe { Pin::new_unchecked(Box::from_raw(ptr as _)) }
+ }
+}
+
impl ForeignOwnable for () {
type Borrowed<'a> = ();