Skip to content

Commit

Permalink
Merge pull request #961 from wedsonaf/foreign-ownable-rust
Browse files Browse the repository at this point in the history
rust: rename `PointerWrapper` to `ForeignOwnable`
  • Loading branch information
ojeda authored Feb 11, 2023
2 parents 3dfc5eb + 575b1c0 commit 1a9a5d7
Show file tree
Hide file tree
Showing 15 changed files with 249 additions and 273 deletions.
12 changes: 6 additions & 6 deletions rust/kernel/amba.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
use crate::{
bindings, device, driver, error::from_kernel_result, io_mem::Resource, power, str::CStr,
to_result, types::PointerWrapper, Result, ThisModule,
to_result, types::ForeignOwnable, Result, ThisModule,
};

/// A registration of an amba driver.
Expand Down Expand Up @@ -43,7 +43,7 @@ unsafe impl const driver::RawDeviceId for DeviceId {
/// An amba driver.
pub trait Driver {
/// Data stored on device by driver.
type Data: PointerWrapper + Send + Sync + driver::DeviceRemoval = ();
type Data: ForeignOwnable + Send + Sync + driver::DeviceRemoval = ();

/// The type that implements the power-management operations.
///
Expand Down Expand Up @@ -88,7 +88,7 @@ impl<T: Driver> driver::DriverOps for Adapter<T> {
amba.id_table = t.as_ref();
}
if cfg!(CONFIG_PM) {
// SAFETY: `probe_callback` sets the driver data after calling `T::Data::into_pointer`,
// SAFETY: `probe_callback` sets the driver data after calling `T::Data::into_foreign`,
// and we guarantee that `T::Data` is the same as `T::PowerOps::Data` by a constraint
// in the type declaration.
amba.drv.pm = unsafe { power::OpsTable::<T::PowerOps>::build() };
Expand Down Expand Up @@ -131,7 +131,7 @@ unsafe extern "C" fn probe_callback<T: Driver>(
unsafe { (&*ptr).as_ref() }
};
let data = T::probe(&mut dev, info)?;
let ptr = T::Data::into_pointer(data);
let ptr = T::Data::into_foreign(data);
// SAFETY: `adev` is valid for write by the contract with the C code.
unsafe { bindings::amba_set_drvdata(adev, ptr as _) };
Ok(0)
Expand All @@ -143,8 +143,8 @@ unsafe extern "C" fn remove_callback<T: Driver>(adev: *mut bindings::amba_device
let ptr = unsafe { bindings::amba_get_drvdata(adev) };
// SAFETY: The value returned by `amba_get_drvdata` was stored by a previous call to
// `amba_set_drvdata` in `probe_callback` above; the value comes from a call to
// `T::Data::into_pointer`.
let data = unsafe { T::Data::from_pointer(ptr) };
// `T::Data::into_foreign`.
let data = unsafe { T::Data::from_foreign(ptr) };
T::remove(&data);
<T::Data as driver::DeviceRemoval>::device_remove(&data);
}
Expand Down
44 changes: 22 additions & 22 deletions rust/kernel/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use crate::{
iov_iter::IovIter,
mm,
sync::CondVar,
types::PointerWrapper,
types::ForeignOwnable,
user_ptr::{UserSlicePtr, UserSlicePtrReader, UserSlicePtrWriter},
ARef, AlwaysRefCounted,
};
Expand Down Expand Up @@ -309,7 +309,7 @@ impl<A: OpenAdapter<T::OpenData>, T: Operations> OperationsVtable<A, T> {
let fileref = unsafe { File::from_ptr(file) };
// SAFETY: `arg` was previously returned by `A::convert` and must
// be a valid non-null pointer.
let ptr = T::open(unsafe { &*arg }, fileref)?.into_pointer();
let ptr = T::open(unsafe { &*arg }, fileref)?.into_foreign();
// SAFETY: The C contract guarantees that `private_data` is available
// for implementers of the file operations (no other C code accesses
// it), so we know that there are no concurrent threads/CPUs accessing
Expand All @@ -329,7 +329,7 @@ impl<A: OpenAdapter<T::OpenData>, T: Operations> OperationsVtable<A, T> {
let mut data =
unsafe { UserSlicePtr::new(buf as *mut core::ffi::c_void, len).writer() };
// SAFETY: `private_data` was initialised by `open_callback` with a value returned by
// `T::Data::into_pointer`. `T::Data::from_pointer` is only called by the
// `T::Data::into_foreign`. `T::Data::from_foreign` is only called by the
// `release` callback, which the C API guarantees that will be called only when all
// references to `file` have been released, so we know it can't be called while this
// function is running.
Expand All @@ -356,7 +356,7 @@ impl<A: OpenAdapter<T::OpenData>, T: Operations> OperationsVtable<A, T> {
let file = unsafe { (*iocb).ki_filp };
let offset = unsafe { (*iocb).ki_pos };
// SAFETY: `private_data` was initialised by `open_callback` with a value returned by
// `T::Data::into_pointer`. `T::Data::from_pointer` is only called by the
// `T::Data::into_foreign`. `T::Data::from_foreign` is only called by the
// `release` callback, which the C API guarantees that will be called only when all
// references to `file` have been released, so we know it can't be called while this
// function is running.
Expand All @@ -382,7 +382,7 @@ impl<A: OpenAdapter<T::OpenData>, T: Operations> OperationsVtable<A, T> {
let mut data =
unsafe { UserSlicePtr::new(buf as *mut core::ffi::c_void, len).reader() };
// SAFETY: `private_data` was initialised by `open_callback` with a value returned by
// `T::Data::into_pointer`. `T::Data::from_pointer` is only called by the
// `T::Data::into_foreign`. `T::Data::from_foreign` is only called by the
// `release` callback, which the C API guarantees that will be called only when all
// references to `file` have been released, so we know it can't be called while this
// function is running.
Expand All @@ -409,7 +409,7 @@ impl<A: OpenAdapter<T::OpenData>, T: Operations> OperationsVtable<A, T> {
let file = unsafe { (*iocb).ki_filp };
let offset = unsafe { (*iocb).ki_pos };
// SAFETY: `private_data` was initialised by `open_callback` with a value returned by
// `T::Data::into_pointer`. `T::Data::from_pointer` is only called by the
// `T::Data::into_foreign`. `T::Data::from_foreign` is only called by the
// `release` callback, which the C API guarantees that will be called only when all
// references to `file` have been released, so we know it can't be called while this
// function is running.
Expand All @@ -430,7 +430,7 @@ impl<A: OpenAdapter<T::OpenData>, T: Operations> OperationsVtable<A, T> {
file: *mut bindings::file,
) -> core::ffi::c_int {
let ptr = mem::replace(unsafe { &mut (*file).private_data }, ptr::null_mut());
T::release(unsafe { T::Data::from_pointer(ptr as _) }, unsafe {
T::release(unsafe { T::Data::from_foreign(ptr as _) }, unsafe {
File::from_ptr(file)
});
0
Expand All @@ -449,7 +449,7 @@ impl<A: OpenAdapter<T::OpenData>, T: Operations> OperationsVtable<A, T> {
_ => return Err(EINVAL),
};
// SAFETY: `private_data` was initialised by `open_callback` with a value returned by
// `T::Data::into_pointer`. `T::Data::from_pointer` is only called by the
// `T::Data::into_foreign`. `T::Data::from_foreign` is only called by the
// `release` callback, which the C API guarantees that will be called only when all
// references to `file` have been released, so we know it can't be called while this
// function is running.
Expand All @@ -466,7 +466,7 @@ impl<A: OpenAdapter<T::OpenData>, T: Operations> OperationsVtable<A, T> {
) -> core::ffi::c_long {
from_kernel_result! {
// SAFETY: `private_data` was initialised by `open_callback` with a value returned by
// `T::Data::into_pointer`. `T::Data::from_pointer` is only called by the
// `T::Data::into_foreign`. `T::Data::from_foreign` is only called by the
// `release` callback, which the C API guarantees that will be called only when all
// references to `file` have been released, so we know it can't be called while this
// function is running.
Expand All @@ -484,7 +484,7 @@ impl<A: OpenAdapter<T::OpenData>, T: Operations> OperationsVtable<A, T> {
) -> core::ffi::c_long {
from_kernel_result! {
// SAFETY: `private_data` was initialised by `open_callback` with a value returned by
// `T::Data::into_pointer`. `T::Data::from_pointer` is only called by the
// `T::Data::into_foreign`. `T::Data::from_foreign` is only called by the
// `release` callback, which the C API guarantees that will be called only when all
// references to `file` have been released, so we know it can't be called while this
// function is running.
Expand All @@ -501,7 +501,7 @@ impl<A: OpenAdapter<T::OpenData>, T: Operations> OperationsVtable<A, T> {
) -> core::ffi::c_int {
from_kernel_result! {
// SAFETY: `private_data` was initialised by `open_callback` with a value returned by
// `T::Data::into_pointer`. `T::Data::from_pointer` is only called by the
// `T::Data::into_foreign`. `T::Data::from_foreign` is only called by the
// `release` callback, which the C API guarantees that will be called only when all
// references to `file` have been released, so we know it can't be called while this
// function is running.
Expand Down Expand Up @@ -529,7 +529,7 @@ impl<A: OpenAdapter<T::OpenData>, T: Operations> OperationsVtable<A, T> {
let end = end.try_into()?;
let datasync = datasync != 0;
// SAFETY: `private_data` was initialised by `open_callback` with a value returned by
// `T::Data::into_pointer`. `T::Data::from_pointer` is only called by the
// `T::Data::into_foreign`. `T::Data::from_foreign` is only called by the
// `release` callback, which the C API guarantees that will be called only when all
// references to `file` have been released, so we know it can't be called while this
// function is running.
Expand All @@ -544,7 +544,7 @@ impl<A: OpenAdapter<T::OpenData>, T: Operations> OperationsVtable<A, T> {
wait: *mut bindings::poll_table_struct,
) -> bindings::__poll_t {
// SAFETY: `private_data` was initialised by `open_callback` with a value returned by
// `T::Data::into_pointer`. `T::Data::from_pointer` is only called by the `release`
// `T::Data::into_foreign`. `T::Data::from_foreign` is only called by the `release`
// callback, which the C API guarantees that will be called only when all references to
// `file` have been released, so we know it can't be called while this function is running.
let f = unsafe { T::Data::borrow((*file).private_data) };
Expand Down Expand Up @@ -775,7 +775,7 @@ pub trait OpenAdapter<T: Sync> {
pub trait Operations {
/// The type of the context data returned by [`Operations::open`] and made available to
/// other methods.
type Data: PointerWrapper + Send + Sync = ();
type Data: ForeignOwnable + Send + Sync = ();

/// The type of the context data passed to [`Operations::open`].
type OpenData: Sync = ();
Expand All @@ -797,7 +797,7 @@ pub trait Operations {
///
/// Corresponds to the `read` and `read_iter` function pointers in `struct file_operations`.
fn read(
_data: <Self::Data as PointerWrapper>::Borrowed<'_>,
_data: <Self::Data as ForeignOwnable>::Borrowed<'_>,
_file: &File,
_writer: &mut impl IoBufferWriter,
_offset: u64,
Expand All @@ -809,7 +809,7 @@ pub trait Operations {
///
/// Corresponds to the `write` and `write_iter` function pointers in `struct file_operations`.
fn write(
_data: <Self::Data as PointerWrapper>::Borrowed<'_>,
_data: <Self::Data as ForeignOwnable>::Borrowed<'_>,
_file: &File,
_reader: &mut impl IoBufferReader,
_offset: u64,
Expand All @@ -821,7 +821,7 @@ pub trait Operations {
///
/// Corresponds to the `llseek` function pointer in `struct file_operations`.
fn seek(
_data: <Self::Data as PointerWrapper>::Borrowed<'_>,
_data: <Self::Data as ForeignOwnable>::Borrowed<'_>,
_file: &File,
_offset: SeekFrom,
) -> Result<u64> {
Expand All @@ -832,7 +832,7 @@ pub trait Operations {
///
/// Corresponds to the `unlocked_ioctl` function pointer in `struct file_operations`.
fn ioctl(
_data: <Self::Data as PointerWrapper>::Borrowed<'_>,
_data: <Self::Data as ForeignOwnable>::Borrowed<'_>,
_file: &File,
_cmd: &mut IoctlCommand,
) -> Result<i32> {
Expand All @@ -843,7 +843,7 @@ pub trait Operations {
///
/// Corresponds to the `compat_ioctl` function pointer in `struct file_operations`.
fn compat_ioctl(
_data: <Self::Data as PointerWrapper>::Borrowed<'_>,
_data: <Self::Data as ForeignOwnable>::Borrowed<'_>,
_file: &File,
_cmd: &mut IoctlCommand,
) -> Result<i32> {
Expand All @@ -854,7 +854,7 @@ pub trait Operations {
///
/// Corresponds to the `fsync` function pointer in `struct file_operations`.
fn fsync(
_data: <Self::Data as PointerWrapper>::Borrowed<'_>,
_data: <Self::Data as ForeignOwnable>::Borrowed<'_>,
_file: &File,
_start: u64,
_end: u64,
Expand All @@ -867,7 +867,7 @@ pub trait Operations {
///
/// Corresponds to the `mmap` function pointer in `struct file_operations`.
fn mmap(
_data: <Self::Data as PointerWrapper>::Borrowed<'_>,
_data: <Self::Data as ForeignOwnable>::Borrowed<'_>,
_file: &File,
_vma: &mut mm::virt::Area,
) -> Result {
Expand All @@ -879,7 +879,7 @@ pub trait Operations {
///
/// Corresponds to the `poll` function pointer in `struct file_operations`.
fn poll(
_data: <Self::Data as PointerWrapper>::Borrowed<'_>,
_data: <Self::Data as ForeignOwnable>::Borrowed<'_>,
_file: &File,
_table: &PollTable,
) -> Result<u32> {
Expand Down
Loading

0 comments on commit 1a9a5d7

Please sign in to comment.