[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-Id: <20230503090708.2524310-10-nmi@metaspace.dk>
Date: Wed, 3 May 2023 11:07:06 +0200
From: Andreas Hindborg <nmi@...aspace.dk>
To: Jens Axboe <axboe@...nel.dk>, Christoph Hellwig <hch@....de>,
Keith Busch <kbusch@...nel.org>,
Damien Le Moal <Damien.LeMoal@....com>,
Hannes Reinecke <hare@...e.de>,
lsf-pc@...ts.linux-foundation.org, rust-for-linux@...r.kernel.org,
linux-block@...r.kernel.org
Cc: Andreas Hindborg <a.hindborg@...sung.com>,
Matthew Wilcox <willy@...radead.org>,
Miguel Ojeda <ojeda@...nel.org>,
Alex Gaynor <alex.gaynor@...il.com>,
Wedson Almeida Filho <wedsonaf@...il.com>,
Boqun Feng <boqun.feng@...il.com>, Gary Guo <gary@...yguo.net>,
Björn Roy Baron <bjorn3_gh@...tonmail.com>,
Benno Lossin <benno.lossin@...ton.me>,
Andreas Hindborg <nmi@...aspace.dk>,
linux-kernel@...r.kernel.org (open list), gost.dev@...sung.com
Subject: [RFC PATCH 09/11] RUST: implement `ForeignOwnable` for `Pin`
From: Andreas Hindborg <a.hindborg@...sung.com>
Implement `ForeignOwnable for Pin<T> where T: ForeignOwnable + Deref`.
Imported from rust tree [1]
[1] https://github.com/Rust-for-Linux/linux/tree/bc22545f38d74473cfef3e9fd65432733435b79f
Cc: Wedson Almeida Filho <wedsonaf@...il.com>
---
rust/kernel/types.rs | 24 ++++++++++++++++++++++++
1 file changed, 24 insertions(+)
diff --git a/rust/kernel/types.rs b/rust/kernel/types.rs
index 29db59d6119a..98e71e96a7fc 100644
--- a/rust/kernel/types.rs
+++ b/rust/kernel/types.rs
@@ -9,6 +9,7 @@ use core::{
marker::PhantomData,
mem::MaybeUninit,
ops::{Deref, DerefMut},
+ pin::Pin,
ptr::NonNull,
};
@@ -100,6 +101,29 @@ impl ForeignOwnable for () {
unsafe fn from_foreign(_: *const core::ffi::c_void) -> Self {}
}
+impl<T: ForeignOwnable + Deref> ForeignOwnable for Pin<T> {
+ type Borrowed<'a> = T::Borrowed<'a>;
+
+ fn into_foreign(self) -> *const core::ffi::c_void {
+ // SAFETY: We continue to treat the pointer as pinned by returning just a pointer to it to
+ // the caller.
+ let inner = unsafe { Pin::into_inner_unchecked(self) };
+ inner.into_foreign()
+ }
+
+ unsafe fn borrow<'a>(ptr: *const core::ffi::c_void) -> Self::Borrowed<'a> {
+ // SAFETY: The safety requirements for this function are the same as the ones for
+ // `T::borrow`.
+ unsafe { T::borrow(ptr) }
+ }
+
+ unsafe fn from_foreign(p: *const core::ffi::c_void) -> Self {
+ // SAFETY: The object was originally pinned.
+ // The passed pointer comes from a previous call to `T::into_foreign`.
+ unsafe { Pin::new_unchecked(T::from_foreign(p)) }
+ }
+}
+
/// Runs a cleanup function/closure when dropped.
///
/// The [`ScopeGuard::dismiss`] function prevents the cleanup function from running.
--
2.40.0
Powered by blists - more mailing lists