Use impl_flags! in Rust Binder#4
Conversation
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 # Describe the purpose of this series. The information you put here # will be used by the project maintainer to make a decision whether # your patches should be reviewed, and in what priority order. Please be # very detailed and link to any relevant discussions or sites that the # maintainer can review to better understand your proposed changes. If you # only have a single patch in your series, the contents of the cover # letter will be appended to the "under-the-cut" portion of the patch. # Lines starting with # will be removed from the cover letter. You can # use them to add notes or reminders to yourself. If you want to use # markdown headers in your cover letter, start the line with ">#". # You can add trailers to the cover letter. Any email addresses found in # these trailers will be added to the addresses specified/generated # during the b4 send stage. You can also run "b4 prep --auto-to-cc" to # auto-populate the To: and Cc: trailers based on the code being # modified. Change-Id: I8375178251d97e7ad784a6ccd8c108c0d2d2b2eb Signed-off-by: Jahnavi MN <jahnavimn@google.com> --- b4-submit-tracking --- # This section is used internally by b4 prep for tracking purposes. { "series": { "revision": 1, "change-id": "20260715-b4-rust_binder_impl_flags-e53b4ebca85d", "prefixes": [] } }
|
|
||
| if self.flags & old.flags & (TF_ONE_WAY | TF_UPDATE_TXN) != (TF_ONE_WAY | TF_UPDATE_TXN) { | ||
| let required = TransactionFlag::OneWay | TransactionFlag::UpdateTxn; | ||
| if !self.flags.contains_all(required) || !old.flags.contains_all(required) { |
There was a problem hiding this comment.
This is a super minor thing, but I would find it easier to read like this.
| if !self.flags.contains_all(required) || !old.flags.contains_all(required) { | |
| if !(self.flags.contains_all(required) && old.flags.contains_all(required)) { |
| #[inline] | ||
| pub(crate) fn is_oneway(&self) -> bool { | ||
| self.flags & TF_ONE_WAY != 0 | ||
| self.flags.contains(TransactionFlag::OneWay) |
There was a problem hiding this comment.
We check flags.contains(TransactionFlag::OneWay) super often. It might be nice to add a helper function called is_oneway() on TransactionFlags type to make these calls even shorter and easier to read.
- Define `DeferWorks(u8)` and `DeferWork` enum using `bit_u8` offsets. - Change `ProcessInner.defer_work` type from `u8` to `DeferWorks`. - Update `Process::release()` and `Process::flush()` to check for empty states using `DeferWorks::empty()`. - Update the workqueue runner to inspect flags using `.contains()`.
- 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`.
- 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.
c0f5662 to
3602828
Compare
Darksonn
left a comment
There was a problem hiding this comment.
You need to ensure that the commit messages:
- Have your Signed-off-by tag.
- Do not have a Change-Id.
| /// Represents a single deferred work category. | ||
| #[derive(Debug, Clone, Copy, PartialEq, Eq)] | ||
| pub enum DeferWork { | ||
| Flush = bit_u8(0), | ||
| Release = bit_u8(1), | ||
| } |
There was a problem hiding this comment.
Looking at this code I am thinking "why do you have to write 0 and 1 on these? Can't you just leave them out and let the macro pick bits for me". After all, with a normal macro you can just write:
enum MyMacro {
Variant1,
Variant2,
}I know the macro doesn't support this yet, but maybe it should?
Totally optional if you want to implement that or not.
No description provided.