Skip to content

binder: replace Arc<Context> with Ref<Context>. #393

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
Jun 30, 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
42 changes: 22 additions & 20 deletions drivers/android/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@

extern crate alloc;

use alloc::sync::Arc;
use core::pin::Pin;
use kernel::{bindings, prelude::*, sync::Mutex, Error};
use kernel::{
bindings,
prelude::*,
sync::{Mutex, Ref},
Error,
};

use crate::{
node::NodeRef,
Expand All @@ -24,24 +27,23 @@ unsafe impl Send for Context {}
unsafe impl Sync for Context {}

impl Context {
pub(crate) fn new() -> Result<Pin<Arc<Self>>> {
let mut ctx_ref = Arc::try_new(Self {
// SAFETY: Init is called below.
manager: unsafe {
Mutex::new(Manager {
node: None,
uid: None,
})
pub(crate) fn new() -> Result<Ref<Self>> {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I notice you're replacing Pin<Arc<T>> with Ref<T> here. This is of course correct, since Ref<T> is now "intrinsically pinned" (or whatever name we want to give it).

Would you perhaps be open to one of the following (or both):

  1. replace Pin<Ref<T>> with Ref<T> everywhere
  2. rename Ref<T> to PinRef<T> or PinnedRef<T> or some such

(this should of course not hold up this PR)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would you perhaps be open to one of the following (or both):

  1. replace Pin<Ref<T>> with Ref<T> everywhere

I will consider this as I switch users of Arc to Ref. I don't anticipate any challenges but I do run into them I'll rope you in.

  1. rename Ref<T> to PinRef<T> or PinnedRef<T> or some such

I'm open to this, but I'm not convinced yet that it's a good idea. Mostly because being pinned is a property of Ref, but it's not a defining one (being ref-counted is).

Ref::try_new_and_init(
Self {
// SAFETY: Init is called below.
manager: unsafe {
Mutex::new(Manager {
node: None,
uid: None,
})
},
},
})?;
let ctx = Arc::get_mut(&mut ctx_ref).unwrap();

// SAFETY: `manager` is also pinned when `ctx` is.
let manager = unsafe { Pin::new_unchecked(&mut ctx.manager) };
kernel::mutex_init!(manager, "Context::manager");

// SAFETY: `ctx_ref` is pinned behind the `Arc` reference.
Ok(unsafe { Pin::new_unchecked(ctx_ref) })
|mut ctx| {
// SAFETY: `manager` is also pinned when `ctx` is.
let manager = unsafe { ctx.as_mut().map_unchecked_mut(|c| &mut c.manager) };
kernel::mutex_init!(manager, "Context::manager");
},
)
}

pub(crate) fn set_manager_node(&self, node_ref: NodeRef) -> Result {
Expand Down
16 changes: 7 additions & 9 deletions drivers/android/process.rs
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ impl ProcessNodeRefs {
}

pub(crate) struct Process {
ctx: Arc<Context>,
ctx: Ref<Context>,

// TODO: For now this a mutex because we have allocations in BTreeMap and RangeAllocator while
// holding the lock. We may want to split up the process state at some point to use a spin lock
Expand All @@ -291,8 +291,8 @@ unsafe impl Send for Process {}
unsafe impl Sync for Process {}

impl Process {
fn new(ctx: Arc<Context>) -> Result<Ref<Self>> {
Ref::try_new_and_init(
fn new(ctx: Ref<Context>) -> Result<Pin<Ref<Self>>> {
Ok(Ref::pinned(Ref::try_new_and_init(
Self {
ctx,
// SAFETY: `inner` is initialised in the call to `mutex_init` below.
Expand All @@ -308,7 +308,7 @@ impl Process {
let pinned = unsafe { process.as_mut().map_unchecked_mut(|p| &mut p.node_refs) };
kernel::mutex_init!(pinned, "Process::node_refs");
},
)
)?))
}

/// Attemps to fetch a work item from the process queue.
Expand Down Expand Up @@ -808,11 +808,9 @@ impl IoctlHandler for Process {
}
}

impl FileOpener<Arc<Context>> for Process {
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) })
impl FileOpener<Ref<Context>> for Process {
fn open(ctx: &Ref<Context>) -> Result<Self::Wrapper> {
Self::new(ctx.clone())
}
}

Expand Down
12 changes: 4 additions & 8 deletions drivers/android/rust_binder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ use kernel::{
linked_list::{GetLinks, GetLinksWrapped, Links},
miscdev::Registration,
prelude::*,
sync::Ref,
user_ptr::UserSlicePtrWriter,
};

Expand Down Expand Up @@ -103,18 +104,13 @@ const fn ptr_align(value: usize) -> usize {
unsafe impl Sync for BinderModule {}

struct BinderModule {
_reg: Pin<Box<Registration<Arc<Context>>>>,
_reg: Pin<Box<Registration<Ref<Context>>>>,
}

impl KernelModule for BinderModule {
fn init() -> Result<Self> {
let pinned_ctx = Context::new()?;
let ctx = unsafe { Pin::into_inner_unchecked(pinned_ctx) };
let reg = Registration::<Arc<Context>>::new_pinned::<process::Process>(
c_str!("rust_binder"),
None,
ctx,
)?;
let ctx = Context::new()?;
let reg = Registration::new_pinned::<process::Process>(c_str!("rust_binder"), None, ctx)?;
Ok(Self { _reg: reg })
}
}