Skip to content

Commit 3602828

Browse files
committed
rust_binder: Update transaction flags to use kernel::impl_flags!
- Define `TransactionFlags(u32)` and `TransactionFlag` with 4 variants. - Change flags field type to `TransactionFlags` in structs. - Add `is_oneway` helper on `TransactionFlags` to simplify checks. - Update `can_replace` logic to use type-safe combined flag checks. - Convert `flags` to `u32` for FFI boundaries and logging.
1 parent a208636 commit 3602828

2 files changed

Lines changed: 45 additions & 17 deletions

File tree

drivers/android/binder/thread.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ use crate::{
3131
process::{GetWorkOrRegister, Process},
3232
ptr_align,
3333
stats::GLOBAL_STATS,
34-
transaction::{Transaction, TransactionInfo},
34+
transaction::{Transaction, TransactionFlag, TransactionFlags, TransactionInfo},
3535
BinderReturnWriter, DArc, DLArc, DTRWrap, DeliverCode, DeliverToRead,
3636
};
3737

@@ -1226,7 +1226,7 @@ impl Thread {
12261226
info.from_pid = self.process.task.pid();
12271227
info.from_tid = self.id;
12281228
info.code = td.transaction_data.code;
1229-
info.flags = td.transaction_data.flags;
1229+
info.flags = TransactionFlags::from_bits(td.transaction_data.flags);
12301230
info.data_ptr = UserPtr::from_addr(trd_data_ptr.buffer as usize);
12311231
info.data_size = td.transaction_data.data_size as usize;
12321232
info.offsets_ptr = UserPtr::from_addr(trd_data_ptr.offsets as usize);
@@ -1329,7 +1329,7 @@ impl Thread {
13291329
let out = (|| -> BinderResult<_> {
13301330
let completion = DTRWrap::arc_try_new(DeliverCode::new(BR_TRANSACTION_COMPLETE))?;
13311331
let process = orig.from.process.clone();
1332-
let allow_fds = orig.flags & TF_ACCEPT_FDS != 0;
1332+
let allow_fds = orig.flags.contains(TransactionFlag::AcceptFds);
13331333
let reply = Transaction::new_reply(self, process, info, allow_fds)?;
13341334
self.inner.lock().push_work(completion);
13351335
orig.from.deliver_reply(Ok(reply), &orig);

drivers/android/binder/transaction.rs

Lines changed: 42 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,41 @@ use crate::{
2525
BinderReturnWriter, DArc, DLArc, DTRWrap, DeliverToRead,
2626
};
2727

28+
kernel::impl_flags!(
29+
/// Represents multiple transaction flags.
30+
#[derive(Debug, Clone, Default, Copy, PartialEq, Eq, Zeroable)]
31+
pub struct TransactionFlags(u32);
32+
33+
/// Represents a single transaction flag.
34+
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
35+
pub enum TransactionFlag {
36+
OneWay = TF_ONE_WAY,
37+
AcceptFds = TF_ACCEPT_FDS,
38+
ClearBuf = TF_CLEAR_BUF,
39+
UpdateTxn = TF_UPDATE_TXN,
40+
}
41+
);
42+
43+
impl TransactionFlags {
44+
/// Creates a `TransactionFlags` from a raw `u32` value.
45+
pub(crate) fn from_bits(bits: u32) -> Self {
46+
Self(bits)
47+
}
48+
49+
/// Checks if the Oneway flag is set.
50+
pub(crate) fn is_oneway(self) -> bool {
51+
self.contains(TransactionFlag::OneWay)
52+
}
53+
}
54+
2855
#[derive(Zeroable)]
2956
pub(crate) struct TransactionInfo {
3057
pub(crate) from_pid: Pid,
3158
pub(crate) from_tid: Pid,
3259
pub(crate) to_pid: Pid,
3360
pub(crate) to_tid: Pid,
3461
pub(crate) code: u32,
35-
pub(crate) flags: u32,
62+
pub(crate) flags: TransactionFlags,
3663
pub(crate) data_ptr: UserPtr,
3764
pub(crate) data_size: usize,
3865
pub(crate) offsets_ptr: UserPtr,
@@ -48,7 +75,7 @@ pub(crate) struct TransactionInfo {
4875
impl TransactionInfo {
4976
#[inline]
5077
pub(crate) fn is_oneway(&self) -> bool {
51-
self.flags & TF_ONE_WAY != 0
78+
self.flags.is_oneway()
5279
}
5380
}
5481

@@ -74,7 +101,7 @@ pub(crate) struct Transaction {
74101
allocation: SpinLock<Option<Allocation>>,
75102
is_outstanding: Atomic<bool>,
76103
code: u32,
77-
pub(crate) flags: u32,
104+
pub(crate) flags: TransactionFlags,
78105
data_size: usize,
79106
offsets_size: usize,
80107
data_address: usize,
@@ -121,7 +148,7 @@ impl Transaction {
121148
}
122149
alloc.set_info_oneway_node(node_ref.node.clone());
123150
}
124-
if info.flags & TF_CLEAR_BUF != 0 {
151+
if info.flags.contains(TransactionFlag::ClearBuf) {
125152
alloc.set_info_clear_on_drop();
126153
}
127154
let target_node = node_ref.node.clone();
@@ -162,7 +189,7 @@ impl Transaction {
162189
return Err(err);
163190
}
164191
};
165-
if info.flags & TF_CLEAR_BUF != 0 {
192+
if info.flags.contains(TransactionFlag::ClearBuf) {
166193
alloc.set_info_clear_on_drop();
167194
}
168195
Ok(DTRWrap::arc_pin_init(pin_init!(Transaction {
@@ -195,7 +222,7 @@ impl Transaction {
195222
self.from.id,
196223
self.to.task.pid(),
197224
self.code,
198-
self.flags,
225+
u32::from(self.flags),
199226
self.start_time.elapsed().as_millis(),
200227
);
201228
if let Some(target_node) = &self.target_node {
@@ -274,7 +301,7 @@ impl Transaction {
274301
let _t_outdated;
275302
let _oneway_node;
276303

277-
let oneway = self.flags & TF_ONE_WAY != 0;
304+
let oneway = self.flags.is_oneway();
278305
let process = self.to.clone();
279306
let mut process_inner = process.inner.lock();
280307

@@ -285,7 +312,7 @@ impl Transaction {
285312
crate::trace::trace_transaction(false, &self, None);
286313
if process_inner.is_frozen.is_frozen() {
287314
process_inner.async_recv = true;
288-
if self.flags & TF_UPDATE_TXN != 0 {
315+
if self.flags.contains(TransactionFlag::UpdateTxn) {
289316
if let Some(t_outdated) =
290317
target_node.take_outdated_transaction(&self, &mut process_inner)
291318
{
@@ -356,7 +383,8 @@ impl Transaction {
356383
return false;
357384
}
358385

359-
if self.flags & old.flags & (TF_ONE_WAY | TF_UPDATE_TXN) != (TF_ONE_WAY | TF_UPDATE_TXN) {
386+
let required = TransactionFlag::OneWay | TransactionFlag::UpdateTxn;
387+
if !(self.flags.contains_all(required) && old.flags.contains_all(required)) {
360388
return false;
361389
}
362390

@@ -393,7 +421,7 @@ impl DeliverToRead for Transaction {
393421
writer: &mut BinderReturnWriter<'_>,
394422
) -> Result<bool> {
395423
let send_failed_reply = ScopeGuard::new(|| {
396-
if self.target_node.is_some() && self.flags & TF_ONE_WAY == 0 {
424+
if self.target_node.is_some() && !self.flags.is_oneway() {
397425
let reply = Err(BR_FAILED_REPLY);
398426
self.from.deliver_reply(reply, &self);
399427
}
@@ -416,7 +444,7 @@ impl DeliverToRead for Transaction {
416444
tr.cookie = cookie as uapi::binder_uintptr_t;
417445
};
418446
tr.code = self.code;
419-
tr.flags = self.flags;
447+
tr.flags = u32::from(self.flags);
420448
tr.data_size = self.data_size as uapi::binder_size_t;
421449
tr.data.ptr.buffer = self.data_address as uapi::binder_uintptr_t;
422450
tr.offsets_size = self.offsets_size as uapi::binder_size_t;
@@ -426,7 +454,7 @@ impl DeliverToRead for Transaction {
426454
}
427455
tr.sender_euid = self.sender_euid.into_uid_in_current_ns();
428456
tr.sender_pid = 0;
429-
if self.target_node.is_some() && self.flags & TF_ONE_WAY == 0 {
457+
if self.target_node.is_some() && !self.flags.is_oneway() {
430458
// Not a reply and not one-way.
431459
tr.sender_pid = self.from.process.pid_in_current_ns();
432460
}
@@ -478,7 +506,7 @@ impl DeliverToRead for Transaction {
478506
drop(allocation);
479507

480508
// If this is not a reply or oneway transaction, then send a dead reply.
481-
if self.target_node.is_some() && self.flags & TF_ONE_WAY == 0 {
509+
if self.target_node.is_some() && !self.flags.is_oneway() {
482510
let reply = Err(BR_DEAD_REPLY);
483511
self.from.deliver_reply(reply, &self);
484512
}
@@ -487,7 +515,7 @@ impl DeliverToRead for Transaction {
487515
}
488516

489517
fn should_sync_wakeup(&self) -> bool {
490-
self.flags & TF_ONE_WAY == 0
518+
!self.flags.is_oneway()
491519
}
492520

493521
fn debug_print(&self, m: &SeqFile, _prefix: &str, tprefix: &str) -> Result<()> {

0 commit comments

Comments
 (0)