Skip to content

Commit ccf9e07

Browse files
Darksonnfbq
authored andcommitted
rust: sync: Inline various lock related methods
While debugging a different issue [1], the following relocation was noticed in the rust_binder.ko file: R_AARCH64_CALL26 _RNvXNtNtNtCsdfZWD8DztAw_6kernel4sync4lock8spinlockNtB2_15SpinLockBackendNtB4_7Backend6unlock This relocation (and a similar one for lock) occurred many times throughout the module. That is not really useful because all this function does is call spin_unlock(), so what we actually want here is that a call to spin_unlock() dirctly is generated in favor of this wrapper method. Thus, mark these methods inline. [boqun: Reword the commit message a bit] Link: https://lore.kernel.org/p/20251111-binder-fix-list-remove-v1-0-8ed14a0da63d@google.com Signed-off-by: Alice Ryhl <aliceryhl@google.com> Reviewed-by: Gary Guo <gary@garyguo.net> Reviewed-by: Daniel Almeida <daniel.almeida@collabora.com> Signed-off-by: Boqun Feng <boqun.feng@gmail.com> Link: https://patch.msgid.link/20251218-inline-lock-unlock-v2-1-fbadac8bd61b@google.com
1 parent abf2111 commit ccf9e07

4 files changed

Lines changed: 19 additions & 0 deletions

File tree

rust/kernel/sync/lock.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,7 @@ impl<B: Backend> Lock<(), B> {
156156
/// the whole lifetime of `'a`.
157157
///
158158
/// [`State`]: Backend::State
159+
#[inline]
159160
pub unsafe fn from_raw<'a>(ptr: *mut B::State) -> &'a Self {
160161
// SAFETY:
161162
// - By the safety contract `ptr` must point to a valid initialised instance of `B::State`
@@ -169,6 +170,7 @@ impl<B: Backend> Lock<(), B> {
169170

170171
impl<T: ?Sized, B: Backend> Lock<T, B> {
171172
/// Acquires the lock and gives the caller access to the data protected by it.
173+
#[inline]
172174
pub fn lock(&self) -> Guard<'_, T, B> {
173175
// SAFETY: The constructor of the type calls `init`, so the existence of the object proves
174176
// that `init` was called.
@@ -182,6 +184,7 @@ impl<T: ?Sized, B: Backend> Lock<T, B> {
182184
/// Returns a guard that can be used to access the data protected by the lock if successful.
183185
// `Option<T>` is not `#[must_use]` even if `T` is, thus the attribute is needed here.
184186
#[must_use = "if unused, the lock will be immediately unlocked"]
187+
#[inline]
185188
pub fn try_lock(&self) -> Option<Guard<'_, T, B>> {
186189
// SAFETY: The constructor of the type calls `init`, so the existence of the object proves
187190
// that `init` was called.
@@ -275,6 +278,7 @@ impl<'a, T: ?Sized, B: Backend> Guard<'a, T, B> {
275278
impl<T: ?Sized, B: Backend> core::ops::Deref for Guard<'_, T, B> {
276279
type Target = T;
277280

281+
#[inline]
278282
fn deref(&self) -> &Self::Target {
279283
// SAFETY: The caller owns the lock, so it is safe to deref the protected data.
280284
unsafe { &*self.lock.data.get() }
@@ -285,13 +289,15 @@ impl<T: ?Sized, B: Backend> core::ops::DerefMut for Guard<'_, T, B>
285289
where
286290
T: Unpin,
287291
{
292+
#[inline]
288293
fn deref_mut(&mut self) -> &mut Self::Target {
289294
// SAFETY: The caller owns the lock, so it is safe to deref the protected data.
290295
unsafe { &mut *self.lock.data.get() }
291296
}
292297
}
293298

294299
impl<T: ?Sized, B: Backend> Drop for Guard<'_, T, B> {
300+
#[inline]
295301
fn drop(&mut self) {
296302
// SAFETY: The caller owns the lock, so it is safe to unlock it.
297303
unsafe { B::unlock(self.lock.state.get(), &self.state) };
@@ -304,6 +310,7 @@ impl<'a, T: ?Sized, B: Backend> Guard<'a, T, B> {
304310
/// # Safety
305311
///
306312
/// The caller must ensure that it owns the lock.
313+
#[inline]
307314
pub unsafe fn new(lock: &'a Lock<T, B>, state: B::GuardState) -> Self {
308315
// SAFETY: The caller can only hold the lock if `Backend::init` has already been called.
309316
unsafe { B::assert_is_held(lock.state.get()) };

rust/kernel/sync/lock/global.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,13 +77,15 @@ impl<B: GlobalLockBackend> GlobalLock<B> {
7777
}
7878

7979
/// Lock this global lock.
80+
#[inline]
8081
pub fn lock(&'static self) -> GlobalGuard<B> {
8182
GlobalGuard {
8283
inner: self.inner.lock(),
8384
}
8485
}
8586

8687
/// Try to lock this global lock.
88+
#[inline]
8789
pub fn try_lock(&'static self) -> Option<GlobalGuard<B>> {
8890
Some(GlobalGuard {
8991
inner: self.inner.try_lock()?,

rust/kernel/sync/lock/mutex.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ unsafe impl super::Backend for MutexBackend {
102102
type State = bindings::mutex;
103103
type GuardState = ();
104104

105+
#[inline]
105106
unsafe fn init(
106107
ptr: *mut Self::State,
107108
name: *const crate::ffi::c_char,
@@ -112,18 +113,21 @@ unsafe impl super::Backend for MutexBackend {
112113
unsafe { bindings::__mutex_init(ptr, name, key) }
113114
}
114115

116+
#[inline]
115117
unsafe fn lock(ptr: *mut Self::State) -> Self::GuardState {
116118
// SAFETY: The safety requirements of this function ensure that `ptr` points to valid
117119
// memory, and that it has been initialised before.
118120
unsafe { bindings::mutex_lock(ptr) };
119121
}
120122

123+
#[inline]
121124
unsafe fn unlock(ptr: *mut Self::State, _guard_state: &Self::GuardState) {
122125
// SAFETY: The safety requirements of this function ensure that `ptr` is valid and that the
123126
// caller is the owner of the mutex.
124127
unsafe { bindings::mutex_unlock(ptr) };
125128
}
126129

130+
#[inline]
127131
unsafe fn try_lock(ptr: *mut Self::State) -> Option<Self::GuardState> {
128132
// SAFETY: The `ptr` pointer is guaranteed to be valid and initialized before use.
129133
let result = unsafe { bindings::mutex_trylock(ptr) };
@@ -135,6 +139,7 @@ unsafe impl super::Backend for MutexBackend {
135139
}
136140
}
137141

142+
#[inline]
138143
unsafe fn assert_is_held(ptr: *mut Self::State) {
139144
// SAFETY: The `ptr` pointer is guaranteed to be valid and initialized before use.
140145
unsafe { bindings::mutex_assert_is_held(ptr) }

rust/kernel/sync/lock/spinlock.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ unsafe impl super::Backend for SpinLockBackend {
101101
type State = bindings::spinlock_t;
102102
type GuardState = ();
103103

104+
#[inline]
104105
unsafe fn init(
105106
ptr: *mut Self::State,
106107
name: *const crate::ffi::c_char,
@@ -111,18 +112,21 @@ unsafe impl super::Backend for SpinLockBackend {
111112
unsafe { bindings::__spin_lock_init(ptr, name, key) }
112113
}
113114

115+
#[inline]
114116
unsafe fn lock(ptr: *mut Self::State) -> Self::GuardState {
115117
// SAFETY: The safety requirements of this function ensure that `ptr` points to valid
116118
// memory, and that it has been initialised before.
117119
unsafe { bindings::spin_lock(ptr) }
118120
}
119121

122+
#[inline]
120123
unsafe fn unlock(ptr: *mut Self::State, _guard_state: &Self::GuardState) {
121124
// SAFETY: The safety requirements of this function ensure that `ptr` is valid and that the
122125
// caller is the owner of the spinlock.
123126
unsafe { bindings::spin_unlock(ptr) }
124127
}
125128

129+
#[inline]
126130
unsafe fn try_lock(ptr: *mut Self::State) -> Option<Self::GuardState> {
127131
// SAFETY: The `ptr` pointer is guaranteed to be valid and initialized before use.
128132
let result = unsafe { bindings::spin_trylock(ptr) };
@@ -134,6 +138,7 @@ unsafe impl super::Backend for SpinLockBackend {
134138
}
135139
}
136140

141+
#[inline]
137142
unsafe fn assert_is_held(ptr: *mut Self::State) {
138143
// SAFETY: The `ptr` pointer is guaranteed to be valid and initialized before use.
139144
unsafe { bindings::spin_assert_is_held(ptr) }

0 commit comments

Comments
 (0)