From: "Maíra Canal" <mcanal@igalia.com>
To: "Asahi Lina" <lina@asahilina.net>,
"Miguel Ojeda" <ojeda@kernel.org>,
"Alex Gaynor" <alex.gaynor@gmail.com>,
"Wedson Almeida Filho" <wedsonaf@gmail.com>,
"Boqun Feng" <boqun.feng@gmail.com>,
"Gary Guo" <gary@garyguo.net>,
"Björn Roy Baron" <bjorn3_gh@protonmail.com>,
"Benno Lossin" <benno.lossin@proton.me>,
"Andreas Hindborg" <a.hindborg@samsung.com>,
"Alice Ryhl" <aliceryhl@google.com>,
"Matthew Wilcox" <willy@infradead.org>
Cc: rust-for-linux@vger.kernel.org, linux-fsdevel@vger.kernel.org,
kernel-dev@igalia.com, "Maíra Canal" <mcanal@igalia.com>
Subject: [PATCH v8 1/2] rust: types: add FOREIGN_ALIGN to ForeignOwnable
Date: Sat, 9 Mar 2024 20:57:51 -0300 [thread overview]
Message-ID: <20240309235927.168915-3-mcanal@igalia.com> (raw)
In-Reply-To: <20240309235927.168915-2-mcanal@igalia.com>
There are cases where we need to check the alignment of the pointers
returned by `into_foreign`. Currently, this is not possible to be done
at build time. Therefore, add a property to the trait ForeignOwnable,
which specifies the alignment of the pointers returned by
`into_foreign`.
Suggested-by: Alice Ryhl <aliceryhl@google.com>
Signed-off-by: Maíra Canal <mcanal@igalia.com>
Reviewed-by: Andreas Hindborg <a.hindborg@samsung.com>
Reviewed-by: Alice Ryhl <aliceryhl@google.com>
---
rust/kernel/sync/arc.rs | 2 ++
rust/kernel/types.rs | 7 +++++++
2 files changed, 9 insertions(+)
diff --git a/rust/kernel/sync/arc.rs b/rust/kernel/sync/arc.rs
index 7d4c4bf58388..da5c8cc325b6 100644
--- a/rust/kernel/sync/arc.rs
+++ b/rust/kernel/sync/arc.rs
@@ -274,6 +274,8 @@ pub fn ptr_eq(this: &Self, other: &Self) -> bool {
}
impl<T: 'static> ForeignOwnable for Arc<T> {
+ const FOREIGN_ALIGN: usize = core::mem::align_of::<ArcInner<T>>();
+
type Borrowed<'a> = ArcBorrow<'a, T>;
fn into_foreign(self) -> *const core::ffi::c_void {
diff --git a/rust/kernel/types.rs b/rust/kernel/types.rs
index aa77bad9bce4..76cd4226dd35 100644
--- a/rust/kernel/types.rs
+++ b/rust/kernel/types.rs
@@ -20,6 +20,9 @@
/// This trait is meant to be used in cases when Rust objects are stored in C objects and
/// eventually "freed" back to Rust.
pub trait ForeignOwnable: Sized {
+ /// The alignment of pointers returned by `into_foreign`.
+ const FOREIGN_ALIGN: usize;
+
/// Type of values borrowed between calls to [`ForeignOwnable::into_foreign`] and
/// [`ForeignOwnable::from_foreign`].
type Borrowed<'a>;
@@ -68,6 +71,8 @@ unsafe fn try_from_foreign(ptr: *const core::ffi::c_void) -> Option<Self> {
}
impl<T: 'static> ForeignOwnable for Box<T> {
+ const FOREIGN_ALIGN: usize = core::mem::align_of::<T>();
+
type Borrowed<'a> = &'a T;
fn into_foreign(self) -> *const core::ffi::c_void {
@@ -90,6 +95,8 @@ unsafe fn from_foreign(ptr: *const core::ffi::c_void) -> Self {
}
impl ForeignOwnable for () {
+ const FOREIGN_ALIGN: usize = core::mem::align_of::<()>();
+
type Borrowed<'a> = ();
fn into_foreign(self) -> *const core::ffi::c_void {
--
2.43.0
next prev parent reply other threads:[~2024-03-10 0:00 UTC|newest]
Thread overview: 15+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-03-09 23:57 [PATCH v8 0/2] rust: xarray: Add an abstraction for XArray Maíra Canal
2024-03-09 23:57 ` Maíra Canal [this message]
2024-03-15 12:19 ` [PATCH v8 1/2] rust: types: add FOREIGN_ALIGN to ForeignOwnable Benno Lossin
2024-03-09 23:57 ` [PATCH v8 2/2] rust: xarray: Add an abstraction for XArray Maíra Canal
2024-03-18 12:10 ` Alice Ryhl
2024-03-19 9:32 ` Philipp Stanner
2024-03-22 0:22 ` John Hubbard
2024-03-22 1:20 ` Boqun Feng
2024-03-22 1:34 ` John Hubbard
2024-03-26 7:41 ` Philipp Stanner
2024-03-26 12:23 ` Miguel Ojeda
2024-03-26 13:53 ` Philipp Stanner
2024-03-26 17:03 ` Miguel Ojeda
2024-03-26 12:16 ` Miguel Ojeda
2024-03-22 11:12 ` Benno Lossin
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20240309235927.168915-3-mcanal@igalia.com \
--to=mcanal@igalia.com \
--cc=a.hindborg@samsung.com \
--cc=alex.gaynor@gmail.com \
--cc=aliceryhl@google.com \
--cc=benno.lossin@proton.me \
--cc=bjorn3_gh@protonmail.com \
--cc=boqun.feng@gmail.com \
--cc=gary@garyguo.net \
--cc=kernel-dev@igalia.com \
--cc=lina@asahilina.net \
--cc=linux-fsdevel@vger.kernel.org \
--cc=ojeda@kernel.org \
--cc=rust-for-linux@vger.kernel.org \
--cc=wedsonaf@gmail.com \
--cc=willy@infradead.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).