Skip to content

Commit da33a3e

Browse files
committed
rust: use new init API for initializing everything
Signed-off-by: Benno Lossin <[email protected]>
1 parent 00793bc commit da33a3e

24 files changed

+520
-479
lines changed

drivers/android/context.rs

+11-12
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
use kernel::{
44
bindings,
5+
macros::pin_data,
6+
mutex, pin_init,
57
prelude::*,
68
security,
79
sync::{Arc, Mutex, UniqueArc},
@@ -17,7 +19,9 @@ struct Manager {
1719
uid: Option<bindings::kuid_t>,
1820
}
1921

22+
#[pin_data]
2023
pub(crate) struct Context {
24+
#[pin]
2125
manager: Mutex<Manager>,
2226
}
2327

@@ -27,20 +31,15 @@ unsafe impl Sync for Context {}
2731

2832
impl Context {
2933
pub(crate) fn new() -> Result<Arc<Self>> {
30-
let mut ctx = Pin::from(UniqueArc::try_new(Self {
31-
// SAFETY: Init is called below.
32-
manager: unsafe {
33-
Mutex::new(Manager {
34+
let ctx = UniqueArc::pin_init::<core::convert::Infallible>(pin_init!(Self {
35+
manager: mutex!(
36+
Manager {
3437
node: None,
3538
uid: None,
36-
})
37-
},
38-
})?);
39-
40-
// SAFETY: `manager` is also pinned when `ctx` is.
41-
let manager = unsafe { ctx.as_mut().map_unchecked_mut(|c| &mut c.manager) };
42-
kernel::mutex_init!(manager, "Context::manager");
43-
39+
},
40+
"Contex::manager"
41+
),
42+
}))?;
4443
Ok(ctx.into())
4544
}
4645

drivers/android/node.rs

+15-17
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,13 @@
22

33
use core::sync::atomic::{AtomicU64, Ordering};
44
use kernel::{
5+
init::PinInit,
56
io_buffer::IoBufferWriter,
67
linked_list::{GetLinks, Links, List},
8+
macros::pin_data,
9+
pin_init,
710
prelude::*,
11+
spinlock,
812
sync::{Arc, Guard, LockedBy, Mutex, SpinLock},
913
user_ptr::UserSlicePtrWriter,
1014
};
@@ -54,6 +58,7 @@ struct NodeDeathInner {
5458
aborted: bool,
5559
}
5660

61+
#[pin_data]
5762
pub(crate) struct NodeDeath {
5863
node: Arc<Node>,
5964
process: Arc<Process>,
@@ -63,37 +68,30 @@ pub(crate) struct NodeDeath {
6368
// TODO: Add the moment we're using this for two lists, which isn't safe because we want to
6469
// remove from the list without knowing the list it's in. We need to separate this out.
6570
death_links: Links<NodeDeath>,
71+
#[pin]
6672
inner: SpinLock<NodeDeathInner>,
6773
}
6874

6975
impl NodeDeath {
7076
/// Constructs a new node death notification object.
71-
///
72-
/// # Safety
73-
///
74-
/// The caller must call `NodeDeath::init` before using the notification object.
75-
pub(crate) unsafe fn new(node: Arc<Node>, process: Arc<Process>, cookie: usize) -> Self {
76-
Self {
77+
#[allow(clippy::new_ret_no_self)]
78+
pub(crate) fn new(node: Arc<Node>, process: Arc<Process>, cookie: usize) -> impl PinInit<Self> {
79+
pin_init!(Self {
7780
node,
7881
process,
7982
cookie,
8083
work_links: Links::new(),
8184
death_links: Links::new(),
82-
inner: unsafe {
83-
SpinLock::new(NodeDeathInner {
85+
inner: spinlock!(
86+
NodeDeathInner {
8487
dead: false,
8588
cleared: false,
8689
notification_done: false,
8790
aborted: false,
88-
})
89-
},
90-
}
91-
}
92-
93-
pub(crate) fn init(self: Pin<&mut Self>) {
94-
// SAFETY: `inner` is pinned when `self` is.
95-
let inner = unsafe { self.map_unchecked_mut(|n| &mut n.inner) };
96-
kernel::spinlock_init!(inner, "NodeDeath::inner");
91+
},
92+
"NodeDeath::inner"
93+
),
94+
})
9795
}
9896

9997
/// Sets the cleared flag to `true`.

drivers/android/process.rs

+20-25
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,10 @@ use kernel::{
77
file::{self, File, IoctlCommand, IoctlHandler, PollTable},
88
io_buffer::{IoBufferReader, IoBufferWriter},
99
linked_list::List,
10-
mm,
10+
macros::pin_data,
11+
mm, mutex,
1112
pages::Pages,
13+
pin_init,
1214
prelude::*,
1315
rbtree::RBTree,
1416
sync::{Arc, ArcBorrow, Guard, Mutex, UniqueArc},
@@ -241,6 +243,7 @@ impl ProcessNodeRefs {
241243
}
242244
}
243245

246+
#[pin_data]
244247
pub(crate) struct Process {
245248
ctx: Arc<Context>,
246249

@@ -254,10 +257,12 @@ pub(crate) struct Process {
254257
// lock. We may want to split up the process state at some point to use a spin lock for the
255258
// other fields.
256259
// TODO: Make this private again.
260+
#[pin]
257261
pub(crate) inner: Mutex<ProcessInner>,
258262

259263
// References are in a different mutex to avoid recursive acquisition when
260264
// incrementing/decrementing a node in another process.
265+
#[pin]
261266
node_refs: Mutex<ProcessNodeRefs>,
262267
}
263268

@@ -267,24 +272,13 @@ unsafe impl Sync for Process {}
267272

268273
impl Process {
269274
fn new(ctx: Arc<Context>, cred: ARef<Credential>) -> Result<Arc<Self>> {
270-
let mut process = Pin::from(UniqueArc::try_new(Self {
275+
let process = UniqueArc::pin_init::<core::convert::Infallible>(pin_init!(Self {
271276
ctx,
272277
cred,
273-
task: Task::current().group_leader().into(),
274-
// SAFETY: `inner` is initialised in the call to `mutex_init` below.
275-
inner: unsafe { Mutex::new(ProcessInner::new()) },
276-
// SAFETY: `node_refs` is initialised in the call to `mutex_init` below.
277-
node_refs: unsafe { Mutex::new(ProcessNodeRefs::new()) },
278-
})?);
279-
280-
// SAFETY: `inner` is pinned when `Process` is.
281-
let pinned = unsafe { process.as_mut().map_unchecked_mut(|p| &mut p.inner) };
282-
kernel::mutex_init!(pinned, "Process::inner");
283-
284-
// SAFETY: `node_refs` is pinned when `Process` is.
285-
let pinned = unsafe { process.as_mut().map_unchecked_mut(|p| &mut p.node_refs) };
286-
kernel::mutex_init!(pinned, "Process::node_refs");
287-
278+
task: ARef::from(Task::current().group_leader()),
279+
inner: mutex!(ProcessInner::new(), "Process::inner"),
280+
node_refs: mutex!(ProcessNodeRefs::new(), "Process::node_refs"),
281+
}))?;
288282
Ok(process.into())
289283
}
290284

@@ -712,14 +706,15 @@ impl Process {
712706
return Ok(());
713707
}
714708

715-
let death = {
716-
let mut pinned = Pin::from(death.write(
717-
// SAFETY: `init` is called below.
718-
unsafe { NodeDeath::new(info.node_ref.node.clone(), self.clone(), cookie) },
719-
));
720-
pinned.as_mut().init();
721-
Arc::<NodeDeath>::from(pinned)
722-
};
709+
let death = Arc::<NodeDeath>::from(
710+
death
711+
.pin_init_now::<core::convert::Infallible>(NodeDeath::new(
712+
info.node_ref.node.clone(),
713+
self.clone(),
714+
cookie,
715+
))
716+
.map_err(|(i, _)| i)?,
717+
);
723718

724719
info.death = Some(death.clone());
725720

drivers/android/thread.rs

+11-16
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,14 @@ use core::{
66
sync::atomic::{AtomicU32, Ordering},
77
};
88
use kernel::{
9-
bindings,
9+
bindings, condvar,
1010
file::{File, PollTable},
1111
io_buffer::{IoBufferReader, IoBufferWriter},
1212
linked_list::{GetLinks, Links, List},
13+
macros::pin_data,
14+
pin_init,
1315
prelude::*,
14-
security,
16+
security, spinlock,
1517
sync::{Arc, CondVar, SpinLock, UniqueArc},
1618
user_ptr::{UserSlicePtr, UserSlicePtrWriter},
1719
Either,
@@ -229,10 +231,13 @@ impl InnerThread {
229231
}
230232
}
231233

234+
#[pin_data]
232235
pub(crate) struct Thread {
233236
pub(crate) id: i32,
234237
pub(crate) process: Arc<Process>,
238+
#[pin]
235239
inner: SpinLock<InnerThread>,
240+
#[pin]
236241
work_condvar: CondVar,
237242
links: Links<Thread>,
238243
}
@@ -241,23 +246,13 @@ impl Thread {
241246
pub(crate) fn new(id: i32, process: Arc<Process>) -> Result<Arc<Self>> {
242247
let return_work = Arc::try_new(ThreadError::new(InnerThread::set_return_work))?;
243248
let reply_work = Arc::try_new(ThreadError::new(InnerThread::set_reply_work))?;
244-
let mut thread = Pin::from(UniqueArc::try_new(Self {
249+
let thread = UniqueArc::pin_init::<core::convert::Infallible>(pin_init!(Self {
245250
id,
246251
process,
247-
// SAFETY: `inner` is initialised in the call to `spinlock_init` below.
248-
inner: unsafe { SpinLock::new(InnerThread::new()) },
249-
// SAFETY: `work_condvar` is initialised in the call to `condvar_init` below.
250-
work_condvar: unsafe { CondVar::new() },
252+
inner: spinlock!(InnerThread::new(), "Thread::inner"),
253+
work_condvar: condvar!("Thread::work_condvar"),
251254
links: Links::new(),
252-
})?);
253-
254-
// SAFETY: `inner` is pinned when `thread` is.
255-
let inner = unsafe { thread.as_mut().map_unchecked_mut(|t| &mut t.inner) };
256-
kernel::spinlock_init!(inner, "Thread::inner");
257-
258-
// SAFETY: `work_condvar` is pinned when `thread` is.
259-
let condvar = unsafe { thread.as_mut().map_unchecked_mut(|t| &mut t.work_condvar) };
260-
kernel::condvar_init!(condvar, "Thread::work_condvar");
255+
}))?;
261256

262257
{
263258
let mut inner = thread.inner.lock();

drivers/android/transaction.rs

+15-22
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,10 @@ use kernel::{
77
io_buffer::IoBufferWriter,
88
linked_list::List,
99
linked_list::{GetLinks, Links},
10+
macros::pin_data,
11+
pin_init,
1012
prelude::*,
13+
spinlock,
1114
sync::{Arc, SpinLock, UniqueArc},
1215
user_ptr::UserSlicePtrWriter,
1316
Either, ScopeGuard,
@@ -26,7 +29,9 @@ struct TransactionInner {
2629
file_list: List<Box<FileInfo>>,
2730
}
2831

32+
#[pin_data]
2933
pub(crate) struct Transaction {
34+
#[pin]
3035
inner: SpinLock<TransactionInner>,
3136
// TODO: Node should be released when the buffer is released.
3237
node_ref: Option<NodeRef>,
@@ -55,26 +60,20 @@ impl Transaction {
5560
let data_address = alloc.ptr;
5661
let file_list = alloc.take_file_list();
5762
alloc.keep_alive();
58-
let mut tr = Pin::from(UniqueArc::try_new(Self {
59-
// SAFETY: `spinlock_init` is called below.
60-
inner: unsafe { SpinLock::new(TransactionInner { file_list }) },
63+
let tr = UniqueArc::pin_init::<core::convert::Infallible>(pin_init!(Self {
64+
inner: spinlock!(TransactionInner { file_list }, "Transaction::inner"),
6165
node_ref: Some(node_ref),
6266
stack_next,
6367
from: from.clone(),
6468
to,
6569
code: tr.code,
6670
flags: tr.flags,
67-
data_size: tr.data_size as _,
71+
data_size: tr.data_size as usize,
6872
data_address,
69-
offsets_size: tr.offsets_size as _,
73+
offsets_size: tr.offsets_size as usize,
7074
links: Links::new(),
7175
free_allocation: AtomicBool::new(true),
72-
})?);
73-
74-
// SAFETY: `inner` is pinned when `tr` is.
75-
let pinned = unsafe { tr.as_mut().map_unchecked_mut(|t| &mut t.inner) };
76-
kernel::spinlock_init!(pinned, "Transaction::inner");
77-
76+
}))?;
7877
Ok(tr.into())
7978
}
8079

@@ -88,26 +87,20 @@ impl Transaction {
8887
let data_address = alloc.ptr;
8988
let file_list = alloc.take_file_list();
9089
alloc.keep_alive();
91-
let mut tr = Pin::from(UniqueArc::try_new(Self {
92-
// SAFETY: `spinlock_init` is called below.
93-
inner: unsafe { SpinLock::new(TransactionInner { file_list }) },
90+
let tr = UniqueArc::pin_init::<core::convert::Infallible>(pin_init!(Self {
91+
inner: spinlock!(TransactionInner { file_list }, "Transaction::inner"),
9492
node_ref: None,
9593
stack_next: None,
9694
from: from.clone(),
9795
to,
9896
code: tr.code,
9997
flags: tr.flags,
100-
data_size: tr.data_size as _,
98+
data_size: tr.data_size as usize,
10199
data_address,
102-
offsets_size: tr.offsets_size as _,
100+
offsets_size: tr.offsets_size as usize,
103101
links: Links::new(),
104102
free_allocation: AtomicBool::new(true),
105-
})?);
106-
107-
// SAFETY: `inner` is pinned when `tr` is.
108-
let pinned = unsafe { tr.as_mut().map_unchecked_mut(|t| &mut t.inner) };
109-
kernel::spinlock_init!(pinned, "Transaction::inner");
110-
103+
}))?;
111104
Ok(tr.into())
112105
}
113106

0 commit comments

Comments
 (0)