diff mbox series

[v8,1/2] rust: types: add FOREIGN_ALIGN to ForeignOwnable

Message ID 20240309235927.168915-3-mcanal@igalia.com (mailing list archive)
State New
Headers show
Series rust: xarray: Add an abstraction for XArray | expand

Commit Message

Maíra Canal March 9, 2024, 11:57 p.m. UTC
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(+)

--
2.43.0

Comments

Benno Lossin March 15, 2024, 12:19 p.m. UTC | #1
On 3/10/24 00:57, Maíra Canal wrote:
> 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;
> +

I think we need to make the trait `unsafe`, since we want `unsafe` code
to be able to rely on this value being correct.
diff mbox series

Patch

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 {