Skip to content

rust/kernel: Move PointerWrapper trait to types.rs #185

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 11, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 4 additions & 65 deletions rust/kernel/file_operations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,18 @@
//! C header: [`include/linux/fs.h`](../../../../include/linux/fs.h)

use core::convert::{TryFrom, TryInto};
use core::{marker, mem, ops::Deref, pin::Pin, ptr};
use core::{marker, mem, ptr};

use alloc::boxed::Box;
use alloc::sync::Arc;

use crate::{
bindings, c_types,
error::{Error, KernelResult},
file::File,
io_buffer::{IoBufferReader, IoBufferWriter},
iov_iter::IovIter,
sync::{CondVar, Ref, RefCounted},
sync::CondVar,
types::PointerWrapper,
user_ptr::{UserSlicePtr, UserSlicePtrReader, UserSlicePtrWriter},
};

Expand Down Expand Up @@ -555,7 +555,7 @@ pub trait FileOperations: Send + Sync + Sized {
const TO_USE: ToUse;

/// The pointer type that will be used to hold ourselves.
type Wrapper: PointerWrapper<Self> = Box<Self>;
type Wrapper: PointerWrapper = Box<Self>;

/// Cleans up after the last reference to the file goes away.
///
Expand Down Expand Up @@ -633,64 +633,3 @@ pub trait FileOperations: Send + Sync + Sized {
Ok(bindings::POLLIN | bindings::POLLOUT | bindings::POLLRDNORM | bindings::POLLWRNORM)
}
}

/// Used to convert an object into a raw pointer that represents it.
///
/// It can eventually be converted back into the object. This is used to store objects as pointers
/// in kernel data structures, for example, an implementation of [`FileOperations`] in `struct
/// file::private_data`.
pub trait PointerWrapper<T> {
/// Returns the raw pointer.
fn into_pointer(self) -> *const T;

/// Returns the instance back from the raw pointer.
///
/// # Safety
///
/// The passed pointer must come from a previous call to [`PointerWrapper::into_pointer()`].
unsafe fn from_pointer(ptr: *const T) -> Self;
}

impl<T> PointerWrapper<T> for Box<T> {
fn into_pointer(self) -> *const T {
Box::into_raw(self)
}

unsafe fn from_pointer(ptr: *const T) -> Self {
Box::from_raw(ptr as _)
}
}

impl<T: RefCounted> PointerWrapper<T> for Ref<T> {
fn into_pointer(self) -> *const T {
Ref::into_raw(self)
}

unsafe fn from_pointer(ptr: *const T) -> Self {
Ref::from_raw(ptr as _)
}
}

impl<T> PointerWrapper<T> for Arc<T> {
fn into_pointer(self) -> *const T {
Arc::into_raw(self)
}

unsafe fn from_pointer(ptr: *const T) -> Self {
Arc::from_raw(ptr)
}
}

impl<T, W: PointerWrapper<T> + Deref> PointerWrapper<T> for Pin<W> {
fn into_pointer(self) -> *const T {
// 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_pointer()
}

unsafe fn from_pointer(p: *const T) -> Self {
// SAFETY: The object was originally pinned.
Pin::new_unchecked(W::from_pointer(p))
}
}
67 changes: 66 additions & 1 deletion rust/kernel/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,13 @@
//!
//! C header: [`include/linux/types.h`](../../../../include/linux/types.h)

use core::ops::Deref;
use core::{ops::Deref, pin::Pin};

use alloc::{boxed::Box, sync::Arc};

use crate::bindings;
use crate::c_types;
use crate::sync::{Ref, RefCounted};

/// Permissions.
///
Expand Down Expand Up @@ -71,3 +75,64 @@ macro_rules! cstr {
unsafe { $crate::CStr::new_unchecked(s) }
}};
}

/// Used to convert an object into a raw pointer that represents it.
///
/// It can eventually be converted back into the object. This is used to store objects as pointers
/// in kernel data structures, for example, an implementation of [`FileOperations`] in `struct
/// file::private_data`.
pub trait PointerWrapper {
/// Returns the raw pointer.
fn into_pointer(self) -> *const c_types::c_void;

/// Returns the instance back from the raw pointer.
///
/// # Safety
///
/// The passed pointer must come from a previous call to [`PointerWrapper::into_pointer()`].
unsafe fn from_pointer(ptr: *const c_types::c_void) -> Self;
}

impl<T> PointerWrapper for Box<T> {
fn into_pointer(self) -> *const c_types::c_void {
Box::into_raw(self) as _
}

unsafe fn from_pointer(ptr: *const c_types::c_void) -> Self {
Box::from_raw(ptr as _)
}
}

impl<T: RefCounted> PointerWrapper for Ref<T> {
fn into_pointer(self) -> *const c_types::c_void {
Ref::into_raw(self) as _
}

unsafe fn from_pointer(ptr: *const c_types::c_void) -> Self {
Ref::from_raw(ptr as _)
}
}

impl<T> PointerWrapper for Arc<T> {
fn into_pointer(self) -> *const c_types::c_void {
Arc::into_raw(self) as _
}

unsafe fn from_pointer(ptr: *const c_types::c_void) -> Self {
Arc::from_raw(ptr as _)
}
}

impl<T: PointerWrapper + Deref> PointerWrapper for Pin<T> {
fn into_pointer(self) -> *const c_types::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_pointer()
}

unsafe fn from_pointer(p: *const c_types::c_void) -> Self {
// SAFETY: The object was originally pinned.
Pin::new_unchecked(T::from_pointer(p))
}
}