These types live forever and do not require cleanup, so they can
serve as `ForeignOwnable`.
Tested-by: Dirk Behme <dirk.behme@de.bosch.com>
Signed-off-by: Matthew Maurer <mmaurer@google.com>
---
rust/kernel/types.rs | 58 ++++++++++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 58 insertions(+)
diff --git a/rust/kernel/types.rs b/rust/kernel/types.rs
index 22985b6f69820d6df8ff3aae0bf815fad36a9d92..0a2b15cd05f91c69ef9c678555b845a23c19b82c 100644
--- a/rust/kernel/types.rs
+++ b/rust/kernel/types.rs
@@ -142,6 +142,64 @@ unsafe fn borrow<'a>(_: *mut Self::PointedTo) -> Self::Borrowed<'a> {}
unsafe fn borrow_mut<'a>(_: *mut Self::PointedTo) -> Self::BorrowedMut<'a> {}
}
+// SAFETY: The `into_foreign` function derives its pointer from a reference, so it is correctly
+// aligned.
+unsafe impl<T: 'static> ForeignOwnable for &'static T {
+ type PointedTo = T;
+ type Borrowed<'a> = &'a T;
+ type BorrowedMut<'a> = &'a T;
+
+ fn into_foreign(self) -> *mut Self::PointedTo {
+ core::ptr::from_ref(self).cast_mut()
+ }
+
+ unsafe fn from_foreign(foreign: *mut Self::PointedTo) -> Self {
+ // SAFETY: from_foreign has stricter restrictions than borrow
+ unsafe { Self::borrow(foreign) }
+ }
+
+ unsafe fn borrow<'a>(foreign: *mut Self::PointedTo) -> Self::Borrowed<'a> {
+ // SAFETY: We know the original reference lived forever, so we can convert it back
+ unsafe { &*foreign }
+ }
+
+ unsafe fn borrow_mut<'a>(foreign: *mut Self::PointedTo) -> Self::BorrowedMut<'a> {
+ // SAFETY: borrow_mut has stricter restrictions than borrow
+ unsafe { Self::borrow(foreign) }
+ }
+}
+
+// SAFETY: The `into_foreign` function derives its pointer from a reference, so it is correctly
+// aligned.
+unsafe impl<T: 'static> ForeignOwnable for &'static mut T {
+ type PointedTo = T;
+ type Borrowed<'a> = &'a T;
+ type BorrowedMut<'a> = &'a mut T;
+
+ fn into_foreign(self) -> *mut Self::PointedTo {
+ core::ptr::from_mut(self)
+ }
+
+ unsafe fn from_foreign(foreign: *mut Self::PointedTo) -> Self {
+ // SAFETY: from_foreign has stricter restrictions than `borrow_mut`
+ unsafe { Self::borrow_mut(foreign) }
+ }
+
+ unsafe fn borrow<'a>(foreign: *mut Self::PointedTo) -> Self::Borrowed<'a> {
+ // SAFETY: We know the original reference lived forever, and the requirements on the
+ // function indicate that `from_foreign` and `borrow_mut` will not happen concurrently, so
+ // we can do a shared borrow.
+ unsafe { &*foreign }
+ }
+
+ unsafe fn borrow_mut<'a>(foreign: *mut Self::PointedTo) -> Self::BorrowedMut<'a> {
+ // SAFETY: We know the original reference lived forever, and the requirements on the
+ // function indicate that no other borrows will happen concurrently, so we can do a
+ // unique borrow.
+ unsafe { &mut *foreign }
+ }
+}
+
/// Runs a cleanup function/closure when dropped.
///
/// The [`ScopeGuard::dismiss`] function prevents the cleanup function from running.
--
2.50.0.727.gbf7dc18ff4-goog
On 28/06/2025 01:18, Matthew Maurer wrote: > These types live forever and do not require cleanup, so they can > serve as `ForeignOwnable`. > > Tested-by: Dirk Behme <dirk.behme@de.bosch.com> > Signed-off-by: Matthew Maurer <mmaurer@google.com> > --- > rust/kernel/types.rs | 58 ++++++++++++++++++++++++++++++++++++++++++++++++++++ > 1 file changed, 58 insertions(+) > > diff --git a/rust/kernel/types.rs b/rust/kernel/types.rs > index 22985b6f69820d6df8ff3aae0bf815fad36a9d92..0a2b15cd05f91c69ef9c678555b845a23c19b82c 100644 > --- a/rust/kernel/types.rs > +++ b/rust/kernel/types.rs > @@ -142,6 +142,64 @@ unsafe fn borrow<'a>(_: *mut Self::PointedTo) -> Self::Borrowed<'a> {} > unsafe fn borrow_mut<'a>(_: *mut Self::PointedTo) -> Self::BorrowedMut<'a> {} > } > > +// SAFETY: The `into_foreign` function derives its pointer from a reference, so it is correctly > +// aligned. > +unsafe impl<T: 'static> ForeignOwnable for &'static T { > + type PointedTo = T; > + type Borrowed<'a> = &'a T; > + type BorrowedMut<'a> = &'a T; > + ... > +// SAFETY: The `into_foreign` function derives its pointer from a reference, so it is correctly > +// aligned. > +unsafe impl<T: 'static> ForeignOwnable for &'static mut T { > + type PointedTo = T; > + type Borrowed<'a> = &'a T; > + type BorrowedMut<'a> = &'a mut T; Just fyi, depending on what hits mainline first, this patch series or https://lore.kernel.org/rust-for-linux/20250626200054.243480-5-dakr@kernel.org/ the latter might need something like [1]. Dirk [1] diff --git a/rust/kernel/debugfs/display_file.rs b/rust/kernel/debugfs/display_file.rs index b38675a90e1b..9cea3bd633dc 100644 --- a/rust/kernel/debugfs/display_file.rs +++ b/rust/kernel/debugfs/display_file.rs @@ -102,6 +102,7 @@ pub(crate) struct BorrowedAdapter<'a, D: ForeignOwnable, F> { // SAFETY: We delegate to D's implementation of `ForeignOwnable`, so `into_foreign` produced aligned // pointers. unsafe impl<D: ForeignOwnable, F> ForeignOwnable for FormatAdapter<D, F> { + type Target = D; type PointedTo = D::PointedTo; type Borrowed<'a> = BorrowedAdapter<'a, D, F>; type BorrowedMut<'a> = Self::Borrowed<'a>; diff --git a/rust/kernel/types.rs b/rust/kernel/types.rs index ffbd750e1eda..c2d87e077c8f 100644 --- a/rust/kernel/types.rs +++ b/rust/kernel/types.rs @@ -149,6 +149,7 @@ unsafe fn borrow_mut<'a>(_: *mut Self::PointedTo) -> Self::BorrowedMut<'a> {} // SAFETY: The `into_foreign` function derives its pointer from a reference, so it is correctly // aligned. unsafe impl<T: 'static> ForeignOwnable for &'static T { + type Target = T; type PointedTo = T; type Borrowed<'a> = &'a T; type BorrowedMut<'a> = &'a T; @@ -176,6 +177,7 @@ unsafe fn borrow_mut<'a>(foreign: *mut Self::PointedTo) -> Self::BorrowedMut<'a> // SAFETY: The `into_foreign` function derives its pointer from a reference, so it is correctly // aligned. unsafe impl<T: 'static> ForeignOwnable for &'static mut T { + type Target = T; type PointedTo = T; type Borrowed<'a> = &'a T; type BorrowedMut<'a> = &'a mut T;
On Tue, Jul 01, 2025 at 01:41:44PM +0200, Dirk Behme wrote: > Just fyi, depending on what hits mainline first, this patch series or > > https://lore.kernel.org/rust-for-linux/20250626200054.243480-5-dakr@kernel.org/ Thanks for pointing this out, but I dropped the linked patch for now. - Danilo
© 2016 - 2025 Red Hat, Inc.