Skip to content

Commit

Permalink
Merge pull request #259 from wedsonaf/result
Browse files Browse the repository at this point in the history
Rename `KernelResult` to just `Result`.
  • Loading branch information
alex authored May 11, 2021
2 parents 1efbbe6 + 28a2643 commit 56e7e87
Show file tree
Hide file tree
Showing 31 changed files with 148 additions and 219 deletions.
16 changes: 8 additions & 8 deletions drivers/android/allocation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,9 @@ impl<'a> Allocation<'a> {
}
}

fn iterate<T>(&self, mut offset: usize, mut size: usize, mut cb: T) -> KernelResult
fn iterate<T>(&self, mut offset: usize, mut size: usize, mut cb: T) -> Result
where
T: FnMut(&Pages<0>, usize, usize) -> KernelResult,
T: FnMut(&Pages<0>, usize, usize) -> Result,
{
// Check that the request is within the buffer.
if offset.checked_add(size).ok_or(Error::EINVAL)? > self.size {
Expand All @@ -65,13 +65,13 @@ impl<'a> Allocation<'a> {
reader: &mut UserSlicePtrReader,
offset: usize,
size: usize,
) -> KernelResult {
) -> Result {
self.iterate(offset, size, |page, offset, to_copy| {
page.copy_into_page(reader, offset, to_copy)
})
}

pub(crate) fn read<T>(&self, offset: usize) -> KernelResult<T> {
pub(crate) fn read<T>(&self, offset: usize) -> Result<T> {
let mut out = MaybeUninit::<T>::uninit();
let mut out_offset = 0;
self.iterate(offset, size_of::<T>(), |page, offset, to_copy| {
Expand All @@ -90,7 +90,7 @@ impl<'a> Allocation<'a> {
Ok(unsafe { out.assume_init() })
}

pub(crate) fn write<T>(&self, offset: usize, obj: &T) -> KernelResult {
pub(crate) fn write<T>(&self, offset: usize, obj: &T) -> Result {
let mut obj_offset = 0;
self.iterate(offset, size_of::<T>(), |page, offset, to_copy| {
// SAFETY: The sum of `offset` and `to_copy` is bounded by the size of T.
Expand All @@ -112,7 +112,7 @@ impl<'a> Allocation<'a> {
self.allocation_info = Some(info);
}

fn cleanup_object(&self, index_offset: usize, view: &AllocationView) -> KernelResult {
fn cleanup_object(&self, index_offset: usize, view: &AllocationView) -> Result {
let offset = self.read(index_offset)?;
let header = view.read::<bindings::binder_object_header>(offset)?;
// TODO: Handle other types.
Expand Down Expand Up @@ -169,14 +169,14 @@ impl<'a> AllocationView<'a> {
AllocationView { alloc, limit }
}

pub fn read<T>(&self, offset: usize) -> KernelResult<T> {
pub fn read<T>(&self, offset: usize) -> Result<T> {
if offset.checked_add(size_of::<T>()).ok_or(Error::EINVAL)? > self.limit {
return Err(Error::EINVAL);
}
self.alloc.read(offset)
}

pub fn write<T>(&self, offset: usize, obj: &T) -> KernelResult {
pub fn write<T>(&self, offset: usize, obj: &T) -> Result {
if offset.checked_add(size_of::<T>()).ok_or(Error::EINVAL)? > self.limit {
return Err(Error::EINVAL);
}
Expand Down
4 changes: 2 additions & 2 deletions drivers/android/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ unsafe impl Send for Context {}
unsafe impl Sync for Context {}

impl Context {
pub(crate) fn new() -> KernelResult<Pin<Arc<Self>>> {
pub(crate) fn new() -> Result<Pin<Arc<Self>>> {
let mut ctx_ref = Arc::try_new(Self {
// SAFETY: Init is called below.
manager: unsafe {
Expand All @@ -44,7 +44,7 @@ impl Context {
Ok(unsafe { Pin::new_unchecked(ctx_ref) })
}

pub(crate) fn set_manager_node(&self, node_ref: NodeRef) -> KernelResult {
pub(crate) fn set_manager_node(&self, node_ref: NodeRef) -> Result {
let mut manager = self.manager.lock();
if manager.node.is_some() {
return Err(Error::EBUSY);
Expand Down
14 changes: 3 additions & 11 deletions drivers/android/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -170,11 +170,7 @@ impl GetLinks for NodeDeath {
}

impl DeliverToRead for NodeDeath {
fn do_work(
self: Arc<Self>,
_thread: &Thread,
writer: &mut UserSlicePtrWriter,
) -> KernelResult<bool> {
fn do_work(self: Arc<Self>, _thread: &Thread, writer: &mut UserSlicePtrWriter) -> Result<bool> {
let done = {
let inner = self.inner.lock();
if inner.aborted {
Expand Down Expand Up @@ -335,7 +331,7 @@ impl Node {
inner.weak.has_count = true;
}

fn write(&self, writer: &mut UserSlicePtrWriter, code: u32) -> KernelResult {
fn write(&self, writer: &mut UserSlicePtrWriter, code: u32) -> Result {
writer.write(&code)?;
writer.write(&self.ptr)?;
writer.write(&self.cookie)?;
Expand All @@ -344,11 +340,7 @@ impl Node {
}

impl DeliverToRead for Node {
fn do_work(
self: Arc<Self>,
_thread: &Thread,
writer: &mut UserSlicePtrWriter,
) -> KernelResult<bool> {
fn do_work(self: Arc<Self>, _thread: &Thread, writer: &mut UserSlicePtrWriter) -> Result<bool> {
let mut owner_inner = self.owner.inner.lock();
let inner = self.inner.access_mut(&mut owner_inner);
let strong = inner.strong.count > 0;
Expand Down
62 changes: 25 additions & 37 deletions drivers/android/process.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ struct Mapping {
}

impl Mapping {
fn new(address: usize, size: usize, pages: Arc<[Pages<0>]>) -> KernelResult<Self> {
fn new(address: usize, size: usize, pages: Arc<[Pages<0>]>) -> Result<Self> {
let alloc = RangeAllocator::new(size)?;
Ok(Self {
address,
Expand Down Expand Up @@ -162,7 +162,7 @@ impl ProcessInner {
/// Returns an existing node with the given pointer and cookie, if one exists.
///
/// Returns an error if a node with the given pointer but a different cookie exists.
fn get_existing_node(&self, ptr: usize, cookie: usize) -> KernelResult<Option<Arc<Node>>> {
fn get_existing_node(&self, ptr: usize, cookie: usize) -> Result<Option<Arc<Node>>> {
match self.nodes.get(&ptr) {
None => Ok(None),
Some(node) => {
Expand All @@ -185,7 +185,7 @@ impl ProcessInner {
cookie: usize,
strong: bool,
thread: Option<&Thread>,
) -> KernelResult<Option<NodeRef>> {
) -> Result<Option<NodeRef>> {
Ok(match self.get_existing_node(ptr, cookie)? {
None => None,
Some(node) => Some(self.new_node_ref(node, strong, thread)),
Expand Down Expand Up @@ -225,7 +225,7 @@ struct ArcReservation<T> {
}

impl<T> ArcReservation<T> {
fn new() -> KernelResult<Self> {
fn new() -> Result<Self> {
Ok(Self {
mem: Arc::try_new(MaybeUninit::<T>::uninit())?,
})
Expand Down Expand Up @@ -293,7 +293,7 @@ unsafe impl Send for Process {}
unsafe impl Sync for Process {}

impl Process {
fn new(ctx: Arc<Context>) -> KernelResult<Ref<Self>> {
fn new(ctx: Arc<Context>) -> Result<Ref<Self>> {
let mut proc_ref = Ref::try_new(Self {
ref_count: RefCount::new(),
ctx,
Expand Down Expand Up @@ -338,7 +338,7 @@ impl Process {
Either::Right(Registration::new(self, thread, &mut inner))
}

fn get_thread(&self, id: i32) -> KernelResult<Arc<Thread>> {
fn get_thread(&self, id: i32) -> Result<Arc<Thread>> {
// TODO: Consider using read/write locks here instead.
{
let inner = self.inner.lock();
Expand Down Expand Up @@ -367,7 +367,7 @@ impl Process {
self.inner.lock().push_work(work)
}

fn set_as_manager(&self, info: Option<FlatBinderObject>, thread: &Thread) -> KernelResult {
fn set_as_manager(&self, info: Option<FlatBinderObject>, thread: &Thread) -> Result {
let (ptr, cookie) = if let Some(obj) = info {
(unsafe { obj.__bindgen_anon_1.binder }, obj.cookie)
} else {
Expand All @@ -390,7 +390,7 @@ impl Process {
cookie: usize,
strong: bool,
thread: Option<&Thread>,
) -> KernelResult<NodeRef> {
) -> Result<NodeRef> {
// Try to find an existing node.
{
let mut inner = self.inner.lock();
Expand All @@ -416,7 +416,7 @@ impl Process {
&self,
node_ref: NodeRef,
is_mananger: bool,
) -> KernelResult<u32> {
) -> Result<u32> {
let mut refs = self.node_refs.lock();

// Do a lookup before inserting.
Expand Down Expand Up @@ -475,7 +475,7 @@ impl Process {
drop(removed);
}

pub(crate) fn update_ref(&self, handle: u32, inc: bool, strong: bool) -> KernelResult {
pub(crate) fn update_ref(&self, handle: u32, inc: bool, strong: bool) -> Result {
if inc && handle == 0 {
if let Ok(node_ref) = self.ctx.get_manager_node(strong) {
if core::ptr::eq(self, &*node_ref.node.owner) {
Expand Down Expand Up @@ -514,11 +514,7 @@ impl Process {
}
}

pub(crate) fn inc_ref_done(
&self,
reader: &mut UserSlicePtrReader,
strong: bool,
) -> KernelResult {
pub(crate) fn inc_ref_done(&self, reader: &mut UserSlicePtrReader, strong: bool) -> Result {
let ptr = reader.read::<usize>()?;
let cookie = reader.read::<usize>()?;
self.update_node(ptr, cookie, strong, true);
Expand All @@ -539,7 +535,7 @@ impl Process {
))
}

// TODO: Review if we want an Option or a KernelResult.
// TODO: Review if we want an Option or a Result.
pub(crate) fn buffer_get(&self, ptr: usize) -> Option<Allocation> {
let mut inner = self.inner.lock();
let mapping = inner.mapping.as_mut()?;
Expand Down Expand Up @@ -574,7 +570,7 @@ impl Process {
}
}

fn create_mapping(&self, vma: &mut bindings::vm_area_struct) -> KernelResult {
fn create_mapping(&self, vma: &mut bindings::vm_area_struct) -> Result {
let size = core::cmp::min(
(vma.vm_end - vma.vm_start) as usize,
bindings::SZ_4M as usize,
Expand Down Expand Up @@ -606,7 +602,7 @@ impl Process {
Ok(())
}

fn version(&self, data: UserSlicePtr) -> KernelResult {
fn version(&self, data: UserSlicePtr) -> Result {
data.writer().write(&BinderVersion::current())
}

Expand All @@ -623,7 +619,7 @@ impl Process {
self.inner.lock().max_threads = max;
}

fn get_node_debug_info(&self, data: UserSlicePtr) -> KernelResult {
fn get_node_debug_info(&self, data: UserSlicePtr) -> Result {
let (mut reader, mut writer) = data.reader_writer();

// Read the starting point.
Expand All @@ -643,7 +639,7 @@ impl Process {
writer.write(&out)
}

fn get_node_info_from_ref(&self, data: UserSlicePtr) -> KernelResult {
fn get_node_info_from_ref(&self, data: UserSlicePtr) -> Result {
let (mut reader, mut writer) = data.reader_writer();
let mut out = reader.read::<BinderNodeInfoForRef>()?;

Expand Down Expand Up @@ -686,11 +682,7 @@ impl Process {
ret
}

pub(crate) fn request_death(
&self,
reader: &mut UserSlicePtrReader,
thread: &Thread,
) -> KernelResult {
pub(crate) fn request_death(&self, reader: &mut UserSlicePtrReader, thread: &Thread) -> Result {
let handle: u32 = reader.read()?;
let cookie: usize = reader.read()?;

Expand Down Expand Up @@ -733,11 +725,7 @@ impl Process {
Ok(())
}

pub(crate) fn clear_death(
&self,
reader: &mut UserSlicePtrReader,
thread: &Thread,
) -> KernelResult {
pub(crate) fn clear_death(&self, reader: &mut UserSlicePtrReader, thread: &Thread) -> Result {
let handle: u32 = reader.read()?;
let cookie: usize = reader.read()?;

Expand Down Expand Up @@ -767,7 +755,7 @@ impl Process {
}

impl IoctlHandler for Process {
fn write(&self, _file: &File, cmd: u32, reader: &mut UserSlicePtrReader) -> KernelResult<i32> {
fn write(&self, _file: &File, cmd: u32, reader: &mut UserSlicePtrReader) -> Result<i32> {
let thread = self.get_thread(unsafe { rust_helper_current_pid() })?;
match cmd {
bindings::BINDER_SET_MAX_THREADS => self.set_max_threads(reader.read()?),
Expand All @@ -781,7 +769,7 @@ impl IoctlHandler for Process {
Ok(0)
}

fn read_write(&self, file: &File, cmd: u32, data: UserSlicePtr) -> KernelResult<i32> {
fn read_write(&self, file: &File, cmd: u32, data: UserSlicePtr) -> Result<i32> {
let thread = self.get_thread(unsafe { rust_helper_current_pid() })?;
match cmd {
bindings::BINDER_WRITE_READ => thread.write_read(data, file.is_blocking())?,
Expand All @@ -801,7 +789,7 @@ unsafe impl RefCounted for Process {
}

impl FileOpener<Arc<Context>> for Process {
fn open(ctx: &Arc<Context>) -> KernelResult<Self::Wrapper> {
fn open(ctx: &Arc<Context>) -> Result<Self::Wrapper> {
let process = Self::new(ctx.clone())?;
// SAFETY: Pointer is pinned behind `Ref`.
Ok(unsafe { Pin::new_unchecked(process) })
Expand Down Expand Up @@ -892,15 +880,15 @@ impl FileOperations for Process {
}
}

fn ioctl(&self, file: &File, cmd: &mut IoctlCommand) -> KernelResult<i32> {
fn ioctl(&self, file: &File, cmd: &mut IoctlCommand) -> Result<i32> {
cmd.dispatch(self, file)
}

fn compat_ioctl(&self, file: &File, cmd: &mut IoctlCommand) -> KernelResult<i32> {
fn compat_ioctl(&self, file: &File, cmd: &mut IoctlCommand) -> Result<i32> {
cmd.dispatch(self, file)
}

fn mmap(&self, _file: &File, vma: &mut bindings::vm_area_struct) -> KernelResult {
fn mmap(&self, _file: &File, vma: &mut bindings::vm_area_struct) -> Result {
// TODO: Only group leader is allowed to create mappings.

if vma.vm_start == 0 {
Expand All @@ -918,7 +906,7 @@ impl FileOperations for Process {
self.create_mapping(vma)
}

fn poll(&self, file: &File, table: &PollTable) -> KernelResult<u32> {
fn poll(&self, file: &File, table: &PollTable) -> Result<u32> {
let thread = self.get_thread(unsafe { rust_helper_current_pid() })?;
let (from_proc, mut mask) = thread.poll(file, table);
if mask == 0 && from_proc && !self.inner.lock().work.is_empty() {
Expand Down
12 changes: 6 additions & 6 deletions drivers/android/range_alloc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ enum DescriptorState {
}

impl<T> RangeAllocator<T> {
pub(crate) fn new(size: usize) -> KernelResult<Self> {
pub(crate) fn new(size: usize) -> Result<Self> {
let desc = Box::try_new(Descriptor::new(0, size))?;
let mut list = List::new();
list.push_back(desc);
Expand Down Expand Up @@ -49,7 +49,7 @@ impl<T> RangeAllocator<T> {
best
}

pub(crate) fn reserve_new(&mut self, size: usize) -> KernelResult<usize> {
pub(crate) fn reserve_new(&mut self, size: usize) -> Result<usize> {
let desc_ptr = match self.find_best_match(size) {
None => return Err(Error::ENOMEM),
Some(found) => found,
Expand All @@ -70,7 +70,7 @@ impl<T> RangeAllocator<T> {
Ok(desc.offset)
}

fn free_with_cursor(cursor: &mut CursorMut<Box<Descriptor<T>>>) -> KernelResult {
fn free_with_cursor(cursor: &mut CursorMut<Box<Descriptor<T>>>) -> Result {
let mut size = match cursor.current() {
None => return Err(Error::EINVAL),
Some(ref mut entry) => {
Expand Down Expand Up @@ -121,13 +121,13 @@ impl<T> RangeAllocator<T> {
None
}

pub(crate) fn reservation_abort(&mut self, offset: usize) -> KernelResult {
pub(crate) fn reservation_abort(&mut self, offset: usize) -> Result {
// TODO: The force case is currently O(n), but could be made O(1) with unsafe.
let mut cursor = self.find_at_offset(offset).ok_or(Error::EINVAL)?;
Self::free_with_cursor(&mut cursor)
}

pub(crate) fn reservation_commit(&mut self, offset: usize, data: Option<T>) -> KernelResult {
pub(crate) fn reservation_commit(&mut self, offset: usize, data: Option<T>) -> Result {
// TODO: This is currently O(n), make it O(1).
let mut cursor = self.find_at_offset(offset).ok_or(Error::ENOENT)?;
let desc = cursor.current().unwrap();
Expand All @@ -140,7 +140,7 @@ impl<T> RangeAllocator<T> {
/// [`DescriptorState::Reserved`].
///
/// Returns the size of the existing entry and the data associated with it.
pub(crate) fn reserve_existing(&mut self, offset: usize) -> KernelResult<(usize, Option<T>)> {
pub(crate) fn reserve_existing(&mut self, offset: usize) -> Result<(usize, Option<T>)> {
// TODO: This is currently O(n), make it O(log n).
let mut cursor = self.find_at_offset(offset).ok_or(Error::ENOENT)?;
let desc = cursor.current().unwrap();
Expand Down
Loading

0 comments on commit 56e7e87

Please sign in to comment.