rust_binder: Update bitmaps to use kernel::impl_flags!
Subsystems (2)
v2 cover rust_binder: Update bitmaps to use kernel::impl_flags!
In the current Rust Binder driver, internal state variables (thread looper states, deferred work, and transaction configurations) are represented as raw integers and manipulated using manual bitwise operations.
This approach lacks type safety. Because the compiler treats all integers identically, it is possible to pass a thread looper flag into a function expecting a transaction flag without triggering compile-time warnings. These cross-contamination errors compile cleanly but can cause runtime bugs or undefined behavior.
This patch series resolves this issue by migrating these raw integer bitmaps (defer_work, looper_flags, flags) to strongly-typed bitmasks using the kernel::impl_flags! macro. Functions now accept specific, distinct types rather than generic integers, preventing flags from being mixed up. This transition also replaces manual bitwise arithmetic with readable, safe methods.
Based on top of:
https://git.kernel.org/pub/scm/linux/kernel/git/gregkh/char-misc.git
---
Changes in v2:
- Drop Patch 1 (defer_work) as it was merged into char-misc-testing.
- Rebase remaining two patches on latest char-misc-testing tree.
- Link to v1: https://lore.kernel.org/r/[email protected]
---
Jahnavi MN (2):
rust_binder: Update looper_flags bitmaps to use kernel::impl_flags!
rust_binder: Update transaction flags to use kernel::impl_flags!
drivers/android/binder/thread.rs | 68 ++++++++++++++++++++--------------- drivers/android/binder/transaction.rs | 58 ++++++++++++++++++++++-------- 2 files changed, 83 insertions(+), 43 deletions(-)
---
base-commit: 2cedf2272f1bb42471e646868ac572cc5752bd91
change-id: 20260715-b4-rust_binder_impl_flags-e53b4ebca85d
Best regards,
Signature · 1 line
Jahnavi MN <[email protected]>
PATCH v2 1/2 rust_binder: Update looper_flags bitmaps to use kernel::impl_flags!
- Define LooperFlags(u32) and LooperFlag enum with 7 variants.
- Change InnerThread.looper_flags type to LooperFlags.
- Update looper state transitions and checks to use type-safe methods.
- Convert looper_flags to u32 for hex formatting in debug_print.
--- drivers/android/binder/thread.rs | 62 ++++++++++++++++++++++++---------------- 1 file changed, 37 insertions(+), 25 deletions(-)
drivers/android/binder/thread.rs +37 -25
@@ -9,6 +9,7 @@use kernel::{ bindings, + bits::bit_u32, fs::LocalFile , list::{AtomicTracker ,List ,ListArc ,ListLinks ,TryNewListArc }, prelude::*,
@@ -245,7 +246,7 @@ fn is_ok(&self) -> bool {struct InnerThread {/// Determines the looper state of the thread. It is a bit-wise combination of the constants /// prefixed with `LOOPER_`. - looper_flags:u32 , + looper_flags:LooperFlags ,/// Determines whether the looper should return. looper_need_return:bool ,
@@ -272,13 +273,23 @@ struct InnerThread { extended_error: ExtendedError, } -const LOOPER_REGISTERED :u32 =0x01 ; -const LOOPER_ENTERED :u32 =0x02 ; -const LOOPER_EXITED :u32 =0x04 ; -const LOOPER_INVALID :u32 =0x08 ; -const LOOPER_WAITING :u32 =0x10 ; -const LOOPER_WAITING_PROC :u32 =0x20 ; -const LOOPER_POLL :u32 =0x40 ; +kernel::impl_flags!( +/// Represents multiple looper flags. +#[derive( Debug , Clone , Default , Copy , PartialEq , Eq )] +pub struct LooperFlags (u32 ); + +/// Represents a single looper flag. +#[derive( Debug , Clone , Copy , PartialEq , Eq )] +pub enum LooperFlag { +Registered =bit_u32 (0 ), +Entered =bit_u32 (1 ), +Exited =bit_u32 (2 ), +Invalid =bit_u32 (3 ), +Waiting =bit_u32 (4 ), +WaitingProc =bit_u32 (5 ), +Poll =bit_u32 (6 ), + } +);impl InnerThread {fn new (pid:i32 ) ->Result <Self > {
@@ -288,7 +299,7 @@ fn next_err_id() -> u32 { }Ok (Self { - looper_flags:0 , + looper_flags:LooperFlags ::default (), looper_need_return:false , is_dead:false , process_work_list:false ,
@@ -316,7 +327,7 @@ fn push_work(&mut self, work: DLArc<dyn DeliverToRead>) -> PushWorkRes { } self.work_list.push_back (work); self.process_work_list =true ; -if self.looper_flags &LOOPER_POLL !=0 { +if self.looper_flags.contains (LooperFlag ::Poll ) {PushWorkRes ::OkNotifyPoll }else {PushWorkRes ::Ok
@@ -380,26 +391,27 @@ fn pop_transaction_replied(&mut self, transaction: &DArc<Transaction>) -> bool { }fn looper_enter (&mut self) { - self.looper_flags |=LOOPER_ENTERED ; -if self.looper_flags &LOOPER_REGISTERED !=0 { - self.looper_flags |=LOOPER_INVALID ; + self.looper_flags |=LooperFlag ::Entered ; +if self.looper_flags.contains (LooperFlag ::Registered ) { + self.looper_flags |=LooperFlag ::Invalid ; } }fn looper_register (&mut self, valid:bool ) { - self.looper_flags |=LOOPER_REGISTERED ; -if !valid || self.looper_flags &LOOPER_ENTERED !=0 { - self.looper_flags |=LOOPER_INVALID ; + self.looper_flags |=LooperFlag ::Registered ; +if !valid || self.looper_flags.contains (LooperFlag ::Entered ) { + self.looper_flags |=LooperFlag ::Invalid ; } }fn looper_exit (&mut self) { - self.looper_flags |=LOOPER_EXITED ; + self.looper_flags |=LooperFlag ::Exited ; }/// Determines whether the thread is part of a pool, i.e., if it is a looper. fn is_looper (&self) ->bool { - self.looper_flags & (LOOPER_ENTERED |LOOPER_REGISTERED ) !=0 + self.looper_flags + .contains_any (LooperFlag ::Entered |LooperFlag ::Registered ) }/// Determines whether the thread should attempt to fetch work items from the process queue.
@@ -411,7 +423,7 @@ fn should_use_process_work_queue(&self) -> bool { }fn poll (&mut self) ->u32 { - self.looper_flags |=LOOPER_POLL ; + self.looper_flags |=LooperFlag ::Poll ;if self.process_work_list || self.looper_need_return { bindings::POLLIN }else {
@@ -477,7 +489,7 @@ pub(crate) fn debug_print(self: &Arc<Self>, m: &SeqFile, print_all: bool) -> Res m, " thread {}: l {:02x} need_return {}\n", self.id, - inner.looper_flags, + u32::from(inner.looper_flags), inner.looper_need_return, ); }
@@ -550,9 +562,9 @@ fn get_work_local(self: &Arc<Self>, wait: bool) -> Result<Option<DLArc<dyn Delivreturn Ok (Some (work)); } - inner.looper_flags |=LOOPER_WAITING ; + inner.looper_flags |=LooperFlag ::Waiting ;let signal_pending = self.work_condvar.wait_interruptible_freezable (&mut inner); - inner.looper_flags &= !LOOPER_WAITING ; + inner.looper_flags &= !LooperFlag ::Waiting ;if signal_pending {return Err (EINTR );
@@ -604,9 +616,9 @@ fn get_work(self: &Arc<Self>, wait: bool) -> Result<Option<DLArc<dyn DeliverToRereturn Ok (Some (work)); } - inner.looper_flags |=LOOPER_WAITING |LOOPER_WAITING_PROC ; + inner.looper_flags |=LooperFlag ::Waiting |LooperFlag ::WaitingProc ;let signal_pending = self.work_condvar.wait_interruptible_freezable (&mut inner); - inner.looper_flags &= !(LOOPER_WAITING |LOOPER_WAITING_PROC ); + inner.looper_flags &= !(LooperFlag ::Waiting |LooperFlag ::WaitingProc );if signal_pending || inner.looper_need_return {// We need to return now. We need to pull the thread off the list of ready threads
@@ -1649,7 +1661,7 @@ pub(crate) fn poll(&self) -> Result<(bool, u32)> {/// Make the call to `get_work` or `get_work_local` return immediately, if any. pub (crate )fn exit_looper (&self) {let mut inner = self.inner.lock (); -let should_notify = inner.looper_flags &LOOPER_WAITING !=0 ; +let should_notify = inner.looper_flags.contains (LooperFlag ::Waiting );if should_notify { inner.looper_need_return =true ; }
Signature · 1 line
2.55.0.229.g6434b31f56-goog
Greg Kroah-Hartman
maintainerRe: [PATCH v2 1/2] rust_binder: Update looper_flags bitmaps to use kernel::impl_flags!
Quoted 6 lines · On Sun, Jul 19, 2026 at 09:56:58AM +0000, Jahnavi MN via B4 Relay wrote:
From: Jahnavi MN <[email protected]>
- Define
LooperFlags(u32)andLooperFlagenum with 7 variants.
- ChangeInnerThread.looper_flagstype toLooperFlags.
- Update looper state transitions and checks to use type-safe methods.
- Convertlooper_flagstou32for hex formatting indebug_print.
This is a list of things you did (i.e. what), but nothing about "why" you are doing this.
Take a look at the kernel documentation for how to write good changelog text. It's usually the hardest part of making a patch, and these two should be rewritten a bit to explain why you are doing this type of conversion (i.e. the information in your patch 0/X should be in here, right?)
thanks,
greg k-h
PATCH v2 2/2 rust_binder: Update transaction flags to use kernel::impl_flags!
In reply to Jahnavi MN- 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.
--- drivers/android/binder/thread.rs | 6 ++-- drivers/android/binder/transaction.rs | 58 ++++++++++++++++++++++++++--------- 2 files changed, 46 insertions(+), 18 deletions(-)
drivers/android/binder/thread.rs +3 -3
@@ -30,7 +30,7 @@ process::{GetWorkOrRegister, Process}, ptr_align, stats::GLOBAL_STATS, - transaction::{Transaction, TransactionInfo}, + transaction::{Transaction, TransactionFlag, TransactionFlags, TransactionInfo}, BinderReturnWriter, DArc, DLArc, DTRWrap, DeliverCode, DeliverToRead, };
@@ -1266,7 +1266,7 @@ fn read_transaction_info( info.from_pid = self.process.task.pid (); info.from_tid = self.id; info.code = td.transaction_data.code; - info.flags = td.transaction_data.flags; + info.flags =TransactionFlags ::from_bits (td.transaction_data.flags); info.data_ptr =UserPtr ::from_addr (trd_data_ptr.bufferas usize ); info.data_size = td.transaction_data.data_sizeas usize ; info.offsets_ptr =UserPtr ::from_addr (trd_data_ptr.offsetsas usize );
@@ -1408,7 +1408,7 @@ fn reply_inner(self: &Arc<Self>, info: &mut TransactionInfo) -> BinderResult { self.process.task.pid(), ))?;let process = orig.from.process.clone (); -let allow_fds = orig.flags &TF_ACCEPT_FDS !=0 ; +let allow_fds = orig.flags.contains (TransactionFlag ::AcceptFds );let reply =Transaction ::new_reply (self, process, info, allow_fds)?;// Not notifying: Reply to current thread. let _ = self.inner.lock ().push_work (completion);
drivers/android/binder/transaction.rs +43 -15
154 lines not on this page · raw message
Signature · 1 line
2.55.0.229.g6434b31f56-goog