Skip to content

Commit 0c48c83

Browse files
committed
rust_binder: Implement BINDER_DEBUG_DEAD_TRANSACTION
This adds dynamic debug logs for: - Releasing active transactions during thread stack unwinding. - Discarded transaction error codes when a thread exits. - Undelivered transaction acknowledgments (TRANSACTION_COMPLETE) upon thread exit. - Undelivered process death and freeze notifications when processes exit or die. - Undelivered transactions canceled due to target process death. We now store the process PID in `ThreadError`, `DeliverCode`, and `FreezeMessage` to ensure the correct PID is logged on cancellation. This is necessary because `cancel()` runs from background `kworkers`, which would otherwise print the wrong PID. Change-Id: Iad322cb89497087dfc9e1d52850cf74afb55b6b5 Signed-off-by: Jahnavi MN <jahnavimn@google.com>
1 parent 8d97147 commit 0c48c83

5 files changed

Lines changed: 69 additions & 20 deletions

File tree

drivers/android/binder/freeze.rs

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ type UninitFM = UniqueArc<core::mem::MaybeUninit<DTRWrap<FreezeMessage>>>;
6060
/// Represents a notification that the freeze state has changed.
6161
pub(crate) struct FreezeMessage {
6262
cookie: FreezeCookie,
63+
pid: i32,
6364
}
6465

6566
kernel::list::impl_list_arc_safe! {
@@ -73,8 +74,8 @@ impl FreezeMessage {
7374
UniqueArc::new_uninit(flags)
7475
}
7576

76-
fn init(ua: UninitFM, cookie: FreezeCookie) -> DLArc<FreezeMessage> {
77-
match ua.pin_init_with(DTRWrap::new(FreezeMessage { cookie })) {
77+
fn init(ua: UninitFM, cookie: FreezeCookie, pid: i32) -> DLArc<FreezeMessage> {
78+
match ua.pin_init_with(DTRWrap::new(FreezeMessage { cookie, pid })) {
7879
Ok(msg) => ListArc::from(msg),
7980
Err(err) => match err {},
8081
}
@@ -140,7 +141,14 @@ impl DeliverToRead for FreezeMessage {
140141
}
141142
}
142143

143-
fn cancel(self: DArc<Self>) {}
144+
fn cancel(self: DArc<Self>) {
145+
binder_debug!(
146+
pid=self.pid,
147+
DeadTransaction,
148+
"undelivered freeze notification, {:016x}",
149+
self.cookie.0
150+
);
151+
}
144152

145153
fn should_sync_wakeup(&self) -> bool {
146154
false
@@ -264,7 +272,7 @@ impl Process {
264272
}
265273

266274
*info.freeze() = Some(cookie);
267-
let msg = FreezeMessage::init(msg, cookie);
275+
let msg = FreezeMessage::init(msg, cookie, self.task.pid());
268276
drop(node_refs_guard);
269277
let _ = self.push_work(msg);
270278
Ok(())
@@ -285,7 +293,7 @@ impl Process {
285293
};
286294
let mut clear_msg = None;
287295
if freeze.num_pending_duplicates > 0 {
288-
clear_msg = Some(FreezeMessage::init(alloc, cookie));
296+
clear_msg = Some(FreezeMessage::init(alloc, cookie, self.task.pid()));
289297
freeze.num_pending_duplicates -= 1;
290298
freeze.num_cleared_duplicates += 1;
291299
} else {
@@ -300,7 +308,7 @@ impl Process {
300308
let is_frozen = freeze.node.owner.inner.lock().is_frozen.is_fully_frozen();
301309
if freeze.is_clearing || freeze.last_is_frozen != Some(is_frozen) {
302310
// Immediately send another FreezeMessage.
303-
clear_msg = Some(FreezeMessage::init(alloc, cookie));
311+
clear_msg = Some(FreezeMessage::init(alloc, cookie, self.task.pid()));
304312
}
305313
freeze.is_pending = false;
306314
}
@@ -353,7 +361,7 @@ impl Process {
353361
*info.freeze() = None;
354362
let mut msg = None;
355363
if !listener.is_pending {
356-
msg = Some(FreezeMessage::init(alloc, cookie));
364+
msg = Some(FreezeMessage::init(alloc, cookie, self.task.pid()));
357365
}
358366
drop(node_refs_guard);
359367

@@ -433,7 +441,7 @@ impl Process {
433441
continue;
434442
};
435443
let msg_alloc = FreezeMessage::new(GFP_KERNEL)?;
436-
let msg = FreezeMessage::init(msg_alloc, cookie);
444+
let msg = FreezeMessage::init(msg_alloc, cookie, proc.task.pid());
437445
batch.push((proc, msg), GFP_KERNEL)?;
438446
}
439447

drivers/android/binder/node.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1128,7 +1128,14 @@ impl DeliverToRead for NodeDeath {
11281128
Ok(cmd != BR_DEAD_BINDER)
11291129
}
11301130

1131-
fn cancel(self: DArc<Self>) {}
1131+
fn cancel(self: DArc<Self>) {
1132+
binder_debug!(
1133+
pid=self.process.task.pid(),
1134+
DeadTransaction,
1135+
"undelivered death notification, {:016x}",
1136+
self.cookie
1137+
);
1138+
}
11321139

11331140
fn should_sync_wakeup(&self) -> bool {
11341141
false

drivers/android/binder/rust_binder_main.rs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -222,17 +222,19 @@ impl<T: ListArcSafe> DTRWrap<T> {
222222
struct DeliverCode {
223223
code: u32,
224224
skip: Atomic<bool>,
225+
pid: i32,
225226
}
226227

227228
kernel::list::impl_list_arc_safe! {
228229
impl ListArcSafe<0> for DeliverCode { untracked; }
229230
}
230231

231232
impl DeliverCode {
232-
fn new(code: u32) -> Self {
233+
fn new(code: u32, pid: i32) -> Self {
233234
Self {
234235
code,
235236
skip: Atomic::new(false),
237+
pid,
236238
}
237239
}
238240

@@ -257,7 +259,15 @@ impl DeliverToRead for DeliverCode {
257259
Ok(true)
258260
}
259261

260-
fn cancel(self: DArc<Self>) {}
262+
fn cancel(self: DArc<Self>) {
263+
if !self.skip.load(Relaxed) {
264+
binder_debug!(
265+
pid=self.pid,
266+
DeadTransaction,
267+
"undelivered TRANSACTION_COMPLETE"
268+
);
269+
}
270+
}
261271

262272
fn should_sync_wakeup(&self) -> bool {
263273
false

drivers/android/binder/thread.rs

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,7 @@ const LOOPER_WAITING_PROC: u32 = 0x20;
281281
const LOOPER_POLL: u32 = 0x40;
282282

283283
impl InnerThread {
284-
fn new() -> Result<Self> {
284+
fn new(pid: i32) -> Result<Self> {
285285
fn next_err_id() -> u32 {
286286
static EE_ID: Atomic<u32> = Atomic::new(0);
287287
EE_ID.fetch_add(1, Relaxed)
@@ -292,8 +292,8 @@ impl InnerThread {
292292
looper_need_return: false,
293293
is_dead: false,
294294
process_work_list: false,
295-
reply_work: ThreadError::try_new()?,
296-
return_work: ThreadError::try_new()?,
295+
reply_work: ThreadError::try_new(pid)?,
296+
return_work: ThreadError::try_new(pid)?,
297297
work_list: List::new(),
298298
current_transaction: None,
299299
extended_error: ExtendedError::new(next_err_id(), BR_OK, 0),
@@ -452,7 +452,7 @@ kernel::list::impl_list_item! {
452452

453453
impl Thread {
454454
pub(crate) fn new(id: i32, process: Arc<Process>) -> Result<Arc<Self>> {
455-
let inner = InnerThread::new()?;
455+
let inner = InnerThread::new(process.task.pid())?;
456456

457457
Arc::pin_init(
458458
try_pin_init!(Thread {
@@ -1154,6 +1154,12 @@ impl Thread {
11541154
let mut inner = thread.inner.lock();
11551155
inner.pop_transaction_to_reply(thread.as_ref())
11561156
} {
1157+
binder_debug!(
1158+
DeadTransaction,
1159+
"release transaction {} in, still active",
1160+
transaction.debug_id
1161+
);
1162+
11571163
let reply = Err(BR_DEAD_REPLY);
11581164
if !transaction
11591165
.from
@@ -1354,7 +1360,7 @@ impl Thread {
13541360
// TODO: We need to ensure that there isn't a pending transaction in the work queue. How
13551361
// could this happen?
13561362
let top = self.top_of_transaction_stack()?;
1357-
let list_completion = DTRWrap::arc_try_new(DeliverCode::new(BR_TRANSACTION_COMPLETE))?;
1363+
let list_completion = DTRWrap::arc_try_new(DeliverCode::new(BR_TRANSACTION_COMPLETE, self.process.task.pid()))?;
13581364
let completion = list_completion.clone_arc();
13591365
let transaction = Transaction::new(node_ref, top, self, info)?;
13601366

@@ -1412,7 +1418,7 @@ impl Thread {
14121418

14131419
// We need to complete the transaction even if we cannot complete building the reply.
14141420
let out = (|| -> BinderResult<_> {
1415-
let completion = DTRWrap::arc_try_new(DeliverCode::new(BR_TRANSACTION_COMPLETE))?;
1421+
let completion = DTRWrap::arc_try_new(DeliverCode::new(BR_TRANSACTION_COMPLETE, self.process.task.pid()))?;
14161422
let process = orig.from.process.clone();
14171423
let allow_fds = orig.flags & TF_ACCEPT_FDS != 0;
14181424
let reply = Transaction::new_reply(self, process, info, allow_fds)?;
@@ -1453,7 +1459,7 @@ impl Thread {
14531459
} else {
14541460
BR_TRANSACTION_COMPLETE
14551461
};
1456-
let list_completion = DTRWrap::arc_try_new(DeliverCode::new(code))?;
1462+
let list_completion = DTRWrap::arc_try_new(DeliverCode::new(code, self.process.task.pid()))?;
14571463
let completion = list_completion.clone_arc();
14581464
// Not notifying: Reply to current thread.
14591465
let _ = self.inner.lock().push_work(list_completion);
@@ -1692,14 +1698,16 @@ impl Thread {
16921698
#[pin_data]
16931699
struct ThreadError {
16941700
error_code: Atomic<u32>,
1701+
pid: i32,
16951702
#[pin]
16961703
links_track: AtomicTracker,
16971704
}
16981705

16991706
impl ThreadError {
1700-
fn try_new() -> Result<DArc<Self>> {
1707+
fn try_new(pid: i32) -> Result<DArc<Self>> {
17011708
DTRWrap::arc_pin_init(pin_init!(Self {
17021709
error_code: Atomic::new(BR_OK),
1710+
pid,
17031711
links_track <- AtomicTracker::new(),
17041712
}))
17051713
.map(ListArc::into_arc)
@@ -1726,7 +1734,16 @@ impl DeliverToRead for ThreadError {
17261734
Ok(true)
17271735
}
17281736

1729-
fn cancel(self: DArc<Self>) {}
1737+
fn cancel(self: DArc<Self>) {
1738+
let code = self.error_code.load(Relaxed);
1739+
if code != BR_OK {
1740+
binder_debug!(
1741+
pid=self.pid,
1742+
DeadTransaction,
1743+
"undelivered TRANSACTION_ERROR: {code}"
1744+
);
1745+
}
1746+
}
17301747

17311748
fn should_sync_wakeup(&self) -> bool {
17321749
false

drivers/android/binder/transaction.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -532,6 +532,13 @@ impl DeliverToRead for Transaction {
532532
if self.target_node.is_some() && self.flags & TF_ONE_WAY == 0 {
533533
let reply = Err(BR_DEAD_REPLY);
534534
self.from.deliver_reply(reply, &self, None);
535+
} else {
536+
binder_debug!(
537+
pid=self.to.task.pid(),
538+
DeadTransaction,
539+
"undelivered transaction {}, process died",
540+
self.debug_id
541+
);
535542
}
536543

537544
self.drop_outstanding_txn();

0 commit comments

Comments
 (0)