Skip to content

Commit 1468657

Browse files
tamirdojeda
authored andcommitted
rust: kernel: change ForeignOwnable pointer to mut
It is slightly more convenient to operate on mut pointers, and this also properly conveys the desired ownership semantics of the trait. Reviewed-by: Alice Ryhl <[email protected]> Reviewed-by: Andreas Hindborg <[email protected]> Signed-off-by: Tamir Duberstein <[email protected]> Acked-by: Danilo Krummrich <[email protected]> Link: https://lore.kernel.org/r/[email protected] [ Reworded title slightly. - Miguel ] Signed-off-by: Miguel Ojeda <[email protected]>
1 parent 5d385a3 commit 1468657

File tree

4 files changed

+21
-21
lines changed

4 files changed

+21
-21
lines changed

rust/kernel/alloc/kbox.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -355,17 +355,17 @@ where
355355
{
356356
type Borrowed<'a> = &'a T;
357357

358-
fn into_foreign(self) -> *const crate::ffi::c_void {
358+
fn into_foreign(self) -> *mut crate::ffi::c_void {
359359
Box::into_raw(self).cast()
360360
}
361361

362-
unsafe fn from_foreign(ptr: *const crate::ffi::c_void) -> Self {
362+
unsafe fn from_foreign(ptr: *mut crate::ffi::c_void) -> Self {
363363
// SAFETY: The safety requirements of this function ensure that `ptr` comes from a previous
364364
// call to `Self::into_foreign`.
365-
unsafe { Box::from_raw(ptr.cast_mut().cast()) }
365+
unsafe { Box::from_raw(ptr.cast()) }
366366
}
367367

368-
unsafe fn borrow<'a>(ptr: *const crate::ffi::c_void) -> &'a T {
368+
unsafe fn borrow<'a>(ptr: *mut crate::ffi::c_void) -> &'a T {
369369
// SAFETY: The safety requirements of this method ensure that the object remains alive and
370370
// immutable for the duration of 'a.
371371
unsafe { &*ptr.cast() }
@@ -378,18 +378,18 @@ where
378378
{
379379
type Borrowed<'a> = Pin<&'a T>;
380380

381-
fn into_foreign(self) -> *const crate::ffi::c_void {
381+
fn into_foreign(self) -> *mut crate::ffi::c_void {
382382
// SAFETY: We are still treating the box as pinned.
383383
Box::into_raw(unsafe { Pin::into_inner_unchecked(self) }).cast()
384384
}
385385

386-
unsafe fn from_foreign(ptr: *const crate::ffi::c_void) -> Self {
386+
unsafe fn from_foreign(ptr: *mut crate::ffi::c_void) -> Self {
387387
// SAFETY: The safety requirements of this function ensure that `ptr` comes from a previous
388388
// call to `Self::into_foreign`.
389-
unsafe { Pin::new_unchecked(Box::from_raw(ptr.cast_mut().cast())) }
389+
unsafe { Pin::new_unchecked(Box::from_raw(ptr.cast())) }
390390
}
391391

392-
unsafe fn borrow<'a>(ptr: *const crate::ffi::c_void) -> Pin<&'a T> {
392+
unsafe fn borrow<'a>(ptr: *mut crate::ffi::c_void) -> Pin<&'a T> {
393393
// SAFETY: The safety requirements for this function ensure that the object is still alive,
394394
// so it is safe to dereference the raw pointer.
395395
// The safety requirements of `from_foreign` also ensure that the object remains alive for

rust/kernel/miscdevice.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ unsafe extern "C" fn fops_open<T: MiscDevice>(
189189
};
190190

191191
// SAFETY: The open call of a file owns the private data.
192-
unsafe { (*file).private_data = ptr.into_foreign().cast_mut() };
192+
unsafe { (*file).private_data = ptr.into_foreign() };
193193

194194
0
195195
}

rust/kernel/sync/arc.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -345,24 +345,24 @@ impl<T: ?Sized> Arc<T> {
345345
impl<T: 'static> ForeignOwnable for Arc<T> {
346346
type Borrowed<'a> = ArcBorrow<'a, T>;
347347

348-
fn into_foreign(self) -> *const crate::ffi::c_void {
348+
fn into_foreign(self) -> *mut crate::ffi::c_void {
349349
ManuallyDrop::new(self).ptr.as_ptr().cast()
350350
}
351351

352-
unsafe fn borrow<'a>(ptr: *const crate::ffi::c_void) -> ArcBorrow<'a, T> {
352+
unsafe fn borrow<'a>(ptr: *mut crate::ffi::c_void) -> ArcBorrow<'a, T> {
353353
// SAFETY: The safety requirements of this function ensure that `ptr` comes from a previous
354354
// call to `Self::into_foreign`.
355-
let inner = unsafe { NonNull::new_unchecked(ptr.cast_mut().cast::<ArcInner<T>>()) };
355+
let inner = unsafe { NonNull::new_unchecked(ptr.cast::<ArcInner<T>>()) };
356356

357357
// SAFETY: The safety requirements of `from_foreign` ensure that the object remains alive
358358
// for the lifetime of the returned value.
359359
unsafe { ArcBorrow::new(inner) }
360360
}
361361

362-
unsafe fn from_foreign(ptr: *const crate::ffi::c_void) -> Self {
362+
unsafe fn from_foreign(ptr: *mut crate::ffi::c_void) -> Self {
363363
// SAFETY: The safety requirements of this function ensure that `ptr` comes from a previous
364364
// call to `Self::into_foreign`.
365-
let inner = unsafe { NonNull::new_unchecked(ptr.cast_mut().cast::<ArcInner<T>>()) };
365+
let inner = unsafe { NonNull::new_unchecked(ptr.cast::<ArcInner<T>>()) };
366366

367367
// SAFETY: By the safety requirement of this function, we know that `ptr` came from
368368
// a previous call to `Arc::into_foreign`, which guarantees that `ptr` is valid and

rust/kernel/types.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,15 +29,15 @@ pub trait ForeignOwnable: Sized {
2929
/// For example, it might be invalid, dangling or pointing to uninitialized memory. Using it in
3030
/// any way except for [`ForeignOwnable::from_foreign`], [`ForeignOwnable::borrow`],
3131
/// [`ForeignOwnable::try_from_foreign`] can result in undefined behavior.
32-
fn into_foreign(self) -> *const crate::ffi::c_void;
32+
fn into_foreign(self) -> *mut crate::ffi::c_void;
3333

3434
/// Borrows a foreign-owned object.
3535
///
3636
/// # Safety
3737
///
3838
/// `ptr` must have been returned by a previous call to [`ForeignOwnable::into_foreign`] for
3939
/// which a previous matching [`ForeignOwnable::from_foreign`] hasn't been called yet.
40-
unsafe fn borrow<'a>(ptr: *const crate::ffi::c_void) -> Self::Borrowed<'a>;
40+
unsafe fn borrow<'a>(ptr: *mut crate::ffi::c_void) -> Self::Borrowed<'a>;
4141

4242
/// Converts a foreign-owned object back to a Rust-owned one.
4343
///
@@ -47,7 +47,7 @@ pub trait ForeignOwnable: Sized {
4747
/// which a previous matching [`ForeignOwnable::from_foreign`] hasn't been called yet.
4848
/// Additionally, all instances (if any) of values returned by [`ForeignOwnable::borrow`] for
4949
/// this object must have been dropped.
50-
unsafe fn from_foreign(ptr: *const crate::ffi::c_void) -> Self;
50+
unsafe fn from_foreign(ptr: *mut crate::ffi::c_void) -> Self;
5151

5252
/// Tries to convert a foreign-owned object back to a Rust-owned one.
5353
///
@@ -58,7 +58,7 @@ pub trait ForeignOwnable: Sized {
5858
///
5959
/// `ptr` must either be null or satisfy the safety requirements for
6060
/// [`ForeignOwnable::from_foreign`].
61-
unsafe fn try_from_foreign(ptr: *const crate::ffi::c_void) -> Option<Self> {
61+
unsafe fn try_from_foreign(ptr: *mut crate::ffi::c_void) -> Option<Self> {
6262
if ptr.is_null() {
6363
None
6464
} else {
@@ -72,13 +72,13 @@ pub trait ForeignOwnable: Sized {
7272
impl ForeignOwnable for () {
7373
type Borrowed<'a> = ();
7474

75-
fn into_foreign(self) -> *const crate::ffi::c_void {
75+
fn into_foreign(self) -> *mut crate::ffi::c_void {
7676
core::ptr::NonNull::dangling().as_ptr()
7777
}
7878

79-
unsafe fn borrow<'a>(_: *const crate::ffi::c_void) -> Self::Borrowed<'a> {}
79+
unsafe fn borrow<'a>(_: *mut crate::ffi::c_void) -> Self::Borrowed<'a> {}
8080

81-
unsafe fn from_foreign(_: *const crate::ffi::c_void) -> Self {}
81+
unsafe fn from_foreign(_: *mut crate::ffi::c_void) -> Self {}
8282
}
8383

8484
/// Runs a cleanup function/closure when dropped.

0 commit comments

Comments
 (0)