[PATCH v7 1/2] rust: types: add FOREIGN_ALIGN to ForeignOwnable

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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@xxxxxxxxxx>
Signed-off-by: Maíra Canal <mcanal@xxxxxxxxxx>
---
 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 77cdbcf7bd2e..df2230f0f19b 100644
--- a/rust/kernel/sync/arc.rs
+++ b/rust/kernel/sync/arc.rs
@@ -276,6 +276,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 fdb778e65d79..5833cf04bd4c 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>;
@@ -49,6 +52,8 @@ pub trait ForeignOwnable: Sized {
 }
 
 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 {
@@ -71,6 +76,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





[Index of Archives]     [Linux Ext4 Filesystem]     [Union Filesystem]     [Filesystem Testing]     [Ceph Users]     [Ecryptfs]     [NTFS 3]     [AutoFS]     [Kernel Newbies]     [Share Photos]     [Security]     [Netfilter]     [Bugtraq]     [Yosemite News]     [MIPS Linux]     [ARM Linux]     [Linux Security]     [Linux Cachefs]     [Reiser Filesystem]     [Linux RAID]     [NTFS 3]     [Samba]     [Device Mapper]     [CEPH Development]

  Powered by Linux