Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

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

authored by

Alice Ryhl and committed by
Boqun Feng
ccf9e070 abf2111d

+19
+7
rust/kernel/sync/lock.rs
··· 156 156 /// the whole lifetime of `'a`. 157 157 /// 158 158 /// [`State`]: Backend::State 159 + #[inline] 159 160 pub unsafe fn from_raw<'a>(ptr: *mut B::State) -> &'a Self { 160 161 // SAFETY: 161 162 // - By the safety contract `ptr` must point to a valid initialised instance of `B::State` ··· 170 169 171 170 impl<T: ?Sized, B: Backend> Lock<T, B> { 172 171 /// Acquires the lock and gives the caller access to the data protected by it. 172 + #[inline] 173 173 pub fn lock(&self) -> Guard<'_, T, B> { 174 174 // SAFETY: The constructor of the type calls `init`, so the existence of the object proves 175 175 // that `init` was called. ··· 184 182 /// Returns a guard that can be used to access the data protected by the lock if successful. 185 183 // `Option<T>` is not `#[must_use]` even if `T` is, thus the attribute is needed here. 186 184 #[must_use = "if unused, the lock will be immediately unlocked"] 185 + #[inline] 187 186 pub fn try_lock(&self) -> Option<Guard<'_, T, B>> { 188 187 // SAFETY: The constructor of the type calls `init`, so the existence of the object proves 189 188 // that `init` was called. ··· 278 275 impl<T: ?Sized, B: Backend> core::ops::Deref for Guard<'_, T, B> { 279 276 type Target = T; 280 277 278 + #[inline] 281 279 fn deref(&self) -> &Self::Target { 282 280 // SAFETY: The caller owns the lock, so it is safe to deref the protected data. 283 281 unsafe { &*self.lock.data.get() } ··· 289 285 where 290 286 T: Unpin, 291 287 { 288 + #[inline] 292 289 fn deref_mut(&mut self) -> &mut Self::Target { 293 290 // SAFETY: The caller owns the lock, so it is safe to deref the protected data. 294 291 unsafe { &mut *self.lock.data.get() } ··· 297 292 } 298 293 299 294 impl<T: ?Sized, B: Backend> Drop for Guard<'_, T, B> { 295 + #[inline] 300 296 fn drop(&mut self) { 301 297 // SAFETY: The caller owns the lock, so it is safe to unlock it. 302 298 unsafe { B::unlock(self.lock.state.get(), &self.state) }; ··· 310 304 /// # Safety 311 305 /// 312 306 /// The caller must ensure that it owns the lock. 307 + #[inline] 313 308 pub unsafe fn new(lock: &'a Lock<T, B>, state: B::GuardState) -> Self { 314 309 // SAFETY: The caller can only hold the lock if `Backend::init` has already been called. 315 310 unsafe { B::assert_is_held(lock.state.get()) };
+2
rust/kernel/sync/lock/global.rs
··· 77 77 } 78 78 79 79 /// Lock this global lock. 80 + #[inline] 80 81 pub fn lock(&'static self) -> GlobalGuard<B> { 81 82 GlobalGuard { 82 83 inner: self.inner.lock(), ··· 85 84 } 86 85 87 86 /// Try to lock this global lock. 87 + #[inline] 88 88 pub fn try_lock(&'static self) -> Option<GlobalGuard<B>> { 89 89 Some(GlobalGuard { 90 90 inner: self.inner.try_lock()?,
+5
rust/kernel/sync/lock/mutex.rs
··· 102 102 type State = bindings::mutex; 103 103 type GuardState = (); 104 104 105 + #[inline] 105 106 unsafe fn init( 106 107 ptr: *mut Self::State, 107 108 name: *const crate::ffi::c_char, ··· 113 112 unsafe { bindings::__mutex_init(ptr, name, key) } 114 113 } 115 114 115 + #[inline] 116 116 unsafe fn lock(ptr: *mut Self::State) -> Self::GuardState { 117 117 // SAFETY: The safety requirements of this function ensure that `ptr` points to valid 118 118 // memory, and that it has been initialised before. 119 119 unsafe { bindings::mutex_lock(ptr) }; 120 120 } 121 121 122 + #[inline] 122 123 unsafe fn unlock(ptr: *mut Self::State, _guard_state: &Self::GuardState) { 123 124 // SAFETY: The safety requirements of this function ensure that `ptr` is valid and that the 124 125 // caller is the owner of the mutex. 125 126 unsafe { bindings::mutex_unlock(ptr) }; 126 127 } 127 128 129 + #[inline] 128 130 unsafe fn try_lock(ptr: *mut Self::State) -> Option<Self::GuardState> { 129 131 // SAFETY: The `ptr` pointer is guaranteed to be valid and initialized before use. 130 132 let result = unsafe { bindings::mutex_trylock(ptr) }; ··· 139 135 } 140 136 } 141 137 138 + #[inline] 142 139 unsafe fn assert_is_held(ptr: *mut Self::State) { 143 140 // SAFETY: The `ptr` pointer is guaranteed to be valid and initialized before use. 144 141 unsafe { bindings::mutex_assert_is_held(ptr) }
+5
rust/kernel/sync/lock/spinlock.rs
··· 101 101 type State = bindings::spinlock_t; 102 102 type GuardState = (); 103 103 104 + #[inline] 104 105 unsafe fn init( 105 106 ptr: *mut Self::State, 106 107 name: *const crate::ffi::c_char, ··· 112 111 unsafe { bindings::__spin_lock_init(ptr, name, key) } 113 112 } 114 113 114 + #[inline] 115 115 unsafe fn lock(ptr: *mut Self::State) -> Self::GuardState { 116 116 // SAFETY: The safety requirements of this function ensure that `ptr` points to valid 117 117 // memory, and that it has been initialised before. 118 118 unsafe { bindings::spin_lock(ptr) } 119 119 } 120 120 121 + #[inline] 121 122 unsafe fn unlock(ptr: *mut Self::State, _guard_state: &Self::GuardState) { 122 123 // SAFETY: The safety requirements of this function ensure that `ptr` is valid and that the 123 124 // caller is the owner of the spinlock. 124 125 unsafe { bindings::spin_unlock(ptr) } 125 126 } 126 127 128 + #[inline] 127 129 unsafe fn try_lock(ptr: *mut Self::State) -> Option<Self::GuardState> { 128 130 // SAFETY: The `ptr` pointer is guaranteed to be valid and initialized before use. 129 131 let result = unsafe { bindings::spin_trylock(ptr) }; ··· 138 134 } 139 135 } 140 136 137 + #[inline] 141 138 unsafe fn assert_is_held(ptr: *mut Self::State) { 142 139 // SAFETY: The `ptr` pointer is guaranteed to be valid and initialized before use. 143 140 unsafe { bindings::spin_assert_is_held(ptr) }