Skip to content

Commit 7f4c8b4

Browse files
fujitafbq
authored andcommitted
rust_binder: Switch to kernel::sync atomic primitives
Convert uses of AtomicBool, AtomicUsize, and AtomicU32. Signed-off-by: FUJITA Tomonori <fujita.tomonori@gmail.com> Acked-by: Alice Ryhl <aliceryhl@google.com> Signed-off-by: Boqun Feng <boqun.feng@gmail.com> Link: https://patch.msgid.link/20251230093718.1852322-4-fujita.tomonori@gmail.com
1 parent 323e4bf commit 7f4c8b4

4 files changed

Lines changed: 32 additions & 36 deletions

File tree

drivers/android/binder/rust_binder_main.rs

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ use kernel::{
1818
prelude::*,
1919
seq_file::SeqFile,
2020
seq_print,
21+
sync::atomic::{ordering::Relaxed, Atomic},
2122
sync::poll::PollTable,
2223
sync::Arc,
2324
task::Pid,
@@ -28,10 +29,7 @@ use kernel::{
2829

2930
use crate::{context::Context, page_range::Shrinker, process::Process, thread::Thread};
3031

31-
use core::{
32-
ptr::NonNull,
33-
sync::atomic::{AtomicBool, AtomicUsize, Ordering},
34-
};
32+
use core::ptr::NonNull;
3533

3634
mod allocation;
3735
mod context;
@@ -90,9 +88,9 @@ module! {
9088
}
9189

9290
fn next_debug_id() -> usize {
93-
static NEXT_DEBUG_ID: AtomicUsize = AtomicUsize::new(0);
91+
static NEXT_DEBUG_ID: Atomic<usize> = Atomic::new(0);
9492

95-
NEXT_DEBUG_ID.fetch_add(1, Ordering::Relaxed)
93+
NEXT_DEBUG_ID.fetch_add(1, Relaxed)
9694
}
9795

9896
/// Provides a single place to write Binder return values via the
@@ -215,7 +213,7 @@ impl<T: ListArcSafe> DTRWrap<T> {
215213

216214
struct DeliverCode {
217215
code: u32,
218-
skip: AtomicBool,
216+
skip: Atomic<bool>,
219217
}
220218

221219
kernel::list::impl_list_arc_safe! {
@@ -226,7 +224,7 @@ impl DeliverCode {
226224
fn new(code: u32) -> Self {
227225
Self {
228226
code,
229-
skip: AtomicBool::new(false),
227+
skip: Atomic::new(false),
230228
}
231229
}
232230

@@ -235,7 +233,7 @@ impl DeliverCode {
235233
/// This is used instead of removing it from the work list, since `LinkedList::remove` is
236234
/// unsafe, whereas this method is not.
237235
fn skip(&self) {
238-
self.skip.store(true, Ordering::Relaxed);
236+
self.skip.store(true, Relaxed);
239237
}
240238
}
241239

@@ -245,7 +243,7 @@ impl DeliverToRead for DeliverCode {
245243
_thread: &Thread,
246244
writer: &mut BinderReturnWriter<'_>,
247245
) -> Result<bool> {
248-
if !self.skip.load(Ordering::Relaxed) {
246+
if !self.skip.load(Relaxed) {
249247
writer.write_code(self.code)?;
250248
}
251249
Ok(true)
@@ -259,7 +257,7 @@ impl DeliverToRead for DeliverCode {
259257

260258
fn debug_print(&self, m: &SeqFile, prefix: &str, _tprefix: &str) -> Result<()> {
261259
seq_print!(m, "{}", prefix);
262-
if self.skip.load(Ordering::Relaxed) {
260+
if self.skip.load(Relaxed) {
263261
seq_print!(m, "(skipped) ");
264262
}
265263
if self.code == defs::BR_TRANSACTION_COMPLETE {

drivers/android/binder/stats.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
//! Keep track of statistics for binder_logs.
66
77
use crate::defs::*;
8-
use core::sync::atomic::{AtomicU32, Ordering::Relaxed};
8+
use kernel::sync::atomic::{ordering::Relaxed, Atomic};
99
use kernel::{ioctl::_IOC_NR, seq_file::SeqFile, seq_print};
1010

1111
const BC_COUNT: usize = _IOC_NR(BC_REPLY_SG) as usize + 1;
@@ -14,14 +14,14 @@ const BR_COUNT: usize = _IOC_NR(BR_TRANSACTION_PENDING_FROZEN) as usize + 1;
1414
pub(crate) static GLOBAL_STATS: BinderStats = BinderStats::new();
1515

1616
pub(crate) struct BinderStats {
17-
bc: [AtomicU32; BC_COUNT],
18-
br: [AtomicU32; BR_COUNT],
17+
bc: [Atomic<u32>; BC_COUNT],
18+
br: [Atomic<u32>; BR_COUNT],
1919
}
2020

2121
impl BinderStats {
2222
pub(crate) const fn new() -> Self {
2323
#[expect(clippy::declare_interior_mutable_const)]
24-
const ZERO: AtomicU32 = AtomicU32::new(0);
24+
const ZERO: Atomic<u32> = Atomic::new(0);
2525

2626
Self {
2727
bc: [ZERO; BC_COUNT],

drivers/android/binder/thread.rs

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ use kernel::{
1515
security,
1616
seq_file::SeqFile,
1717
seq_print,
18+
sync::atomic::{ordering::Relaxed, Atomic},
1819
sync::poll::{PollCondVar, PollTable},
1920
sync::{Arc, SpinLock},
2021
task::Task,
@@ -34,10 +35,7 @@ use crate::{
3435
BinderReturnWriter, DArc, DLArc, DTRWrap, DeliverCode, DeliverToRead,
3536
};
3637

37-
use core::{
38-
mem::size_of,
39-
sync::atomic::{AtomicU32, Ordering},
40-
};
38+
use core::mem::size_of;
4139

4240
/// Stores the layout of the scatter-gather entries. This is used during the `translate_objects`
4341
/// call and is discarded when it returns.
@@ -273,8 +271,8 @@ const LOOPER_POLL: u32 = 0x40;
273271
impl InnerThread {
274272
fn new() -> Result<Self> {
275273
fn next_err_id() -> u32 {
276-
static EE_ID: AtomicU32 = AtomicU32::new(0);
277-
EE_ID.fetch_add(1, Ordering::Relaxed)
274+
static EE_ID: Atomic<u32> = Atomic::new(0);
275+
EE_ID.fetch_add(1, Relaxed)
278276
}
279277

280278
Ok(Self {
@@ -1537,26 +1535,26 @@ impl Thread {
15371535

15381536
#[pin_data]
15391537
struct ThreadError {
1540-
error_code: AtomicU32,
1538+
error_code: Atomic<u32>,
15411539
#[pin]
15421540
links_track: AtomicTracker,
15431541
}
15441542

15451543
impl ThreadError {
15461544
fn try_new() -> Result<DArc<Self>> {
15471545
DTRWrap::arc_pin_init(pin_init!(Self {
1548-
error_code: AtomicU32::new(BR_OK),
1546+
error_code: Atomic::new(BR_OK),
15491547
links_track <- AtomicTracker::new(),
15501548
}))
15511549
.map(ListArc::into_arc)
15521550
}
15531551

15541552
fn set_error_code(&self, code: u32) {
1555-
self.error_code.store(code, Ordering::Relaxed);
1553+
self.error_code.store(code, Relaxed);
15561554
}
15571555

15581556
fn is_unused(&self) -> bool {
1559-
self.error_code.load(Ordering::Relaxed) == BR_OK
1557+
self.error_code.load(Relaxed) == BR_OK
15601558
}
15611559
}
15621560

@@ -1566,8 +1564,8 @@ impl DeliverToRead for ThreadError {
15661564
_thread: &Thread,
15671565
writer: &mut BinderReturnWriter<'_>,
15681566
) -> Result<bool> {
1569-
let code = self.error_code.load(Ordering::Relaxed);
1570-
self.error_code.store(BR_OK, Ordering::Relaxed);
1567+
let code = self.error_code.load(Relaxed);
1568+
self.error_code.store(BR_OK, Relaxed);
15711569
writer.write_code(code)?;
15721570
Ok(true)
15731571
}
@@ -1583,7 +1581,7 @@ impl DeliverToRead for ThreadError {
15831581
m,
15841582
"{}transaction error: {}\n",
15851583
prefix,
1586-
self.error_code.load(Ordering::Relaxed)
1584+
self.error_code.load(Relaxed)
15871585
);
15881586
Ok(())
15891587
}

drivers/android/binder/transaction.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22

33
// Copyright (C) 2025 Google LLC.
44

5-
use core::sync::atomic::{AtomicBool, Ordering};
65
use kernel::{
76
prelude::*,
87
seq_file::SeqFile,
98
seq_print,
9+
sync::atomic::{ordering::Relaxed, Atomic},
1010
sync::{Arc, SpinLock},
1111
task::Kuid,
1212
time::{Instant, Monotonic},
@@ -33,7 +33,7 @@ pub(crate) struct Transaction {
3333
pub(crate) to: Arc<Process>,
3434
#[pin]
3535
allocation: SpinLock<Option<Allocation>>,
36-
is_outstanding: AtomicBool,
36+
is_outstanding: Atomic<bool>,
3737
code: u32,
3838
pub(crate) flags: u32,
3939
data_size: usize,
@@ -105,7 +105,7 @@ impl Transaction {
105105
offsets_size: trd.offsets_size as _,
106106
data_address,
107107
allocation <- kernel::new_spinlock!(Some(alloc.success()), "Transaction::new"),
108-
is_outstanding: AtomicBool::new(false),
108+
is_outstanding: Atomic::new(false),
109109
txn_security_ctx_off,
110110
oneway_spam_detected,
111111
start_time: Instant::now(),
@@ -145,7 +145,7 @@ impl Transaction {
145145
offsets_size: trd.offsets_size as _,
146146
data_address: alloc.ptr,
147147
allocation <- kernel::new_spinlock!(Some(alloc.success()), "Transaction::new"),
148-
is_outstanding: AtomicBool::new(false),
148+
is_outstanding: Atomic::new(false),
149149
txn_security_ctx_off: None,
150150
oneway_spam_detected,
151151
start_time: Instant::now(),
@@ -215,8 +215,8 @@ impl Transaction {
215215

216216
pub(crate) fn set_outstanding(&self, to_process: &mut ProcessInner) {
217217
// No race because this method is only called once.
218-
if !self.is_outstanding.load(Ordering::Relaxed) {
219-
self.is_outstanding.store(true, Ordering::Relaxed);
218+
if !self.is_outstanding.load(Relaxed) {
219+
self.is_outstanding.store(true, Relaxed);
220220
to_process.add_outstanding_txn();
221221
}
222222
}
@@ -227,8 +227,8 @@ impl Transaction {
227227
// destructor, which is guaranteed to not race with any other operations on the
228228
// transaction. It also cannot race with `set_outstanding`, since submission happens
229229
// before delivery.
230-
if self.is_outstanding.load(Ordering::Relaxed) {
231-
self.is_outstanding.store(false, Ordering::Relaxed);
230+
if self.is_outstanding.load(Relaxed) {
231+
self.is_outstanding.store(false, Relaxed);
232232
self.to.drop_outstanding_txn();
233233
}
234234
}

0 commit comments

Comments
 (0)