1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (2004) Linus Torvalds
5 * Author: Zwane Mwaikambo <zwane@fsmlabs.com>
7 * Copyright (2004, 2005) Ingo Molnar
9 * This file contains the spinlock/rwlock implementations for the
10 * SMP and the DEBUG_SPINLOCK cases. (UP-nondebug inlines them)
12 * Note that some architectures have special knowledge about the
13 * stack frames of these functions in their profile_pc. If you
14 * change anything significant here that could change the stack
15 * frame contact the architecture maintainers.
18 #include <linux/linkage.h>
19 #include <linux/preempt.h>
20 #include <linux/spinlock.h>
21 #include <linux/interrupt.h>
22 #include <linux/debug_locks.h>
23 #include <linux/export.h>
26 #ifndef arch_mmiowb_state
27 DEFINE_PER_CPU(struct mmiowb_state, __mmiowb_state);
28 EXPORT_PER_CPU_SYMBOL(__mmiowb_state);
33 * If lockdep is enabled then we use the non-preemption spin-ops
34 * even on CONFIG_PREEMPT, because lockdep assumes that interrupts are
35 * not re-enabled during lock-acquire (which the preempt-spin-ops do):
37 #if !defined(CONFIG_GENERIC_LOCKBREAK) || defined(CONFIG_DEBUG_LOCK_ALLOC)
39 * The __lock_function inlines are taken from
40 * spinlock : include/linux/spinlock_api_smp.h
41 * rwlock : include/linux/rwlock_api_smp.h
46 * Some architectures can relax in favour of the CPU owning the lock.
48 #ifndef arch_read_relax
49 # define arch_read_relax(l) cpu_relax()
51 #ifndef arch_write_relax
52 # define arch_write_relax(l) cpu_relax()
54 #ifndef arch_spin_relax
55 # define arch_spin_relax(l) cpu_relax()
59 * We build the __lock_function inlines here. They are too large for
60 * inlining all over the place, but here is only one user per function
61 * which embeds them into the calling _lock_function below.
63 * This could be a long-held lock. We both prepare to spin for a long
64 * time (making _this_ CPU preemptible if possible), and we also signal
65 * towards that other CPU that it should break the lock ASAP.
67 #define BUILD_LOCK_OPS(op, locktype) \
68 void __lockfunc __raw_##op##_lock(locktype##_t *lock) \
72 if (likely(do_raw_##op##_trylock(lock))) \
76 arch_##op##_relax(&lock->raw_lock); \
80 unsigned long __lockfunc __raw_##op##_lock_irqsave(locktype##_t *lock) \
82 unsigned long flags; \
86 local_irq_save(flags); \
87 if (likely(do_raw_##op##_trylock(lock))) \
89 local_irq_restore(flags); \
92 arch_##op##_relax(&lock->raw_lock); \
98 void __lockfunc __raw_##op##_lock_irq(locktype##_t *lock) \
100 _raw_##op##_lock_irqsave(lock); \
103 void __lockfunc __raw_##op##_lock_bh(locktype##_t *lock) \
105 unsigned long flags; \
108 /* Careful: we must exclude softirqs too, hence the */ \
109 /* irq-disabling. We use the generic preemption-aware */ \
112 flags = _raw_##op##_lock_irqsave(lock); \
113 local_bh_disable(); \
114 local_irq_restore(flags); \
118 * Build preemption-friendly versions of the following
119 * lock-spinning functions:
121 * __[spin|read|write]_lock()
122 * __[spin|read|write]_lock_irq()
123 * __[spin|read|write]_lock_irqsave()
124 * __[spin|read|write]_lock_bh()
126 BUILD_LOCK_OPS(spin, raw_spinlock);
128 #ifndef CONFIG_PREEMPT_RT
129 BUILD_LOCK_OPS(read, rwlock);
130 BUILD_LOCK_OPS(write, rwlock);
135 #ifndef CONFIG_INLINE_SPIN_TRYLOCK
136 noinline int __lockfunc _raw_spin_trylock(raw_spinlock_t *lock)
138 return __raw_spin_trylock(lock);
140 EXPORT_SYMBOL(_raw_spin_trylock);
143 #ifndef CONFIG_INLINE_SPIN_TRYLOCK_BH
144 noinline int __lockfunc _raw_spin_trylock_bh(raw_spinlock_t *lock)
146 return __raw_spin_trylock_bh(lock);
148 EXPORT_SYMBOL(_raw_spin_trylock_bh);
151 #ifndef CONFIG_INLINE_SPIN_LOCK
152 noinline void __lockfunc _raw_spin_lock(raw_spinlock_t *lock)
154 __raw_spin_lock(lock);
156 EXPORT_SYMBOL(_raw_spin_lock);
159 #ifndef CONFIG_INLINE_SPIN_LOCK_IRQSAVE
160 noinline unsigned long __lockfunc _raw_spin_lock_irqsave(raw_spinlock_t *lock)
162 return __raw_spin_lock_irqsave(lock);
164 EXPORT_SYMBOL(_raw_spin_lock_irqsave);
167 #ifndef CONFIG_INLINE_SPIN_LOCK_IRQ
168 noinline void __lockfunc _raw_spin_lock_irq(raw_spinlock_t *lock)
170 __raw_spin_lock_irq(lock);
172 EXPORT_SYMBOL(_raw_spin_lock_irq);
175 #ifndef CONFIG_INLINE_SPIN_LOCK_BH
176 noinline void __lockfunc _raw_spin_lock_bh(raw_spinlock_t *lock)
178 __raw_spin_lock_bh(lock);
180 EXPORT_SYMBOL(_raw_spin_lock_bh);
183 #ifdef CONFIG_UNINLINE_SPIN_UNLOCK
184 noinline void __lockfunc _raw_spin_unlock(raw_spinlock_t *lock)
186 __raw_spin_unlock(lock);
188 EXPORT_SYMBOL(_raw_spin_unlock);
191 #ifndef CONFIG_INLINE_SPIN_UNLOCK_IRQRESTORE
192 noinline void __lockfunc _raw_spin_unlock_irqrestore(raw_spinlock_t *lock, unsigned long flags)
194 __raw_spin_unlock_irqrestore(lock, flags);
196 EXPORT_SYMBOL(_raw_spin_unlock_irqrestore);
199 #ifndef CONFIG_INLINE_SPIN_UNLOCK_IRQ
200 noinline void __lockfunc _raw_spin_unlock_irq(raw_spinlock_t *lock)
202 __raw_spin_unlock_irq(lock);
204 EXPORT_SYMBOL(_raw_spin_unlock_irq);
207 #ifndef CONFIG_INLINE_SPIN_UNLOCK_BH
208 noinline void __lockfunc _raw_spin_unlock_bh(raw_spinlock_t *lock)
210 __raw_spin_unlock_bh(lock);
212 EXPORT_SYMBOL(_raw_spin_unlock_bh);
215 #ifndef CONFIG_PREEMPT_RT
217 #ifndef CONFIG_INLINE_READ_TRYLOCK
218 noinline int __lockfunc _raw_read_trylock(rwlock_t *lock)
220 return __raw_read_trylock(lock);
222 EXPORT_SYMBOL(_raw_read_trylock);
225 #ifndef CONFIG_INLINE_READ_LOCK
226 noinline void __lockfunc _raw_read_lock(rwlock_t *lock)
228 __raw_read_lock(lock);
230 EXPORT_SYMBOL(_raw_read_lock);
233 #ifndef CONFIG_INLINE_READ_LOCK_IRQSAVE
234 noinline unsigned long __lockfunc _raw_read_lock_irqsave(rwlock_t *lock)
236 return __raw_read_lock_irqsave(lock);
238 EXPORT_SYMBOL(_raw_read_lock_irqsave);
241 #ifndef CONFIG_INLINE_READ_LOCK_IRQ
242 noinline void __lockfunc _raw_read_lock_irq(rwlock_t *lock)
244 __raw_read_lock_irq(lock);
246 EXPORT_SYMBOL(_raw_read_lock_irq);
249 #ifndef CONFIG_INLINE_READ_LOCK_BH
250 noinline void __lockfunc _raw_read_lock_bh(rwlock_t *lock)
252 __raw_read_lock_bh(lock);
254 EXPORT_SYMBOL(_raw_read_lock_bh);
257 #ifndef CONFIG_INLINE_READ_UNLOCK
258 noinline void __lockfunc _raw_read_unlock(rwlock_t *lock)
260 __raw_read_unlock(lock);
262 EXPORT_SYMBOL(_raw_read_unlock);
265 #ifndef CONFIG_INLINE_READ_UNLOCK_IRQRESTORE
266 noinline void __lockfunc _raw_read_unlock_irqrestore(rwlock_t *lock, unsigned long flags)
268 __raw_read_unlock_irqrestore(lock, flags);
270 EXPORT_SYMBOL(_raw_read_unlock_irqrestore);
273 #ifndef CONFIG_INLINE_READ_UNLOCK_IRQ
274 noinline void __lockfunc _raw_read_unlock_irq(rwlock_t *lock)
276 __raw_read_unlock_irq(lock);
278 EXPORT_SYMBOL(_raw_read_unlock_irq);
281 #ifndef CONFIG_INLINE_READ_UNLOCK_BH
282 noinline void __lockfunc _raw_read_unlock_bh(rwlock_t *lock)
284 __raw_read_unlock_bh(lock);
286 EXPORT_SYMBOL(_raw_read_unlock_bh);
289 #ifndef CONFIG_INLINE_WRITE_TRYLOCK
290 noinline int __lockfunc _raw_write_trylock(rwlock_t *lock)
292 return __raw_write_trylock(lock);
294 EXPORT_SYMBOL(_raw_write_trylock);
297 #ifndef CONFIG_INLINE_WRITE_LOCK
298 noinline void __lockfunc _raw_write_lock(rwlock_t *lock)
300 __raw_write_lock(lock);
302 EXPORT_SYMBOL(_raw_write_lock);
304 #ifndef CONFIG_DEBUG_LOCK_ALLOC
305 #define __raw_write_lock_nested(lock, subclass) __raw_write_lock(((void)(subclass), (lock)))
308 void __lockfunc _raw_write_lock_nested(rwlock_t *lock, int subclass)
310 __raw_write_lock_nested(lock, subclass);
312 EXPORT_SYMBOL(_raw_write_lock_nested);
315 #ifndef CONFIG_INLINE_WRITE_LOCK_IRQSAVE
316 noinline unsigned long __lockfunc _raw_write_lock_irqsave(rwlock_t *lock)
318 return __raw_write_lock_irqsave(lock);
320 EXPORT_SYMBOL(_raw_write_lock_irqsave);
323 #ifndef CONFIG_INLINE_WRITE_LOCK_IRQ
324 noinline void __lockfunc _raw_write_lock_irq(rwlock_t *lock)
326 __raw_write_lock_irq(lock);
328 EXPORT_SYMBOL(_raw_write_lock_irq);
331 #ifndef CONFIG_INLINE_WRITE_LOCK_BH
332 noinline void __lockfunc _raw_write_lock_bh(rwlock_t *lock)
334 __raw_write_lock_bh(lock);
336 EXPORT_SYMBOL(_raw_write_lock_bh);
339 #ifndef CONFIG_INLINE_WRITE_UNLOCK
340 noinline void __lockfunc _raw_write_unlock(rwlock_t *lock)
342 __raw_write_unlock(lock);
344 EXPORT_SYMBOL(_raw_write_unlock);
347 #ifndef CONFIG_INLINE_WRITE_UNLOCK_IRQRESTORE
348 noinline void __lockfunc _raw_write_unlock_irqrestore(rwlock_t *lock, unsigned long flags)
350 __raw_write_unlock_irqrestore(lock, flags);
352 EXPORT_SYMBOL(_raw_write_unlock_irqrestore);
355 #ifndef CONFIG_INLINE_WRITE_UNLOCK_IRQ
356 noinline void __lockfunc _raw_write_unlock_irq(rwlock_t *lock)
358 __raw_write_unlock_irq(lock);
360 EXPORT_SYMBOL(_raw_write_unlock_irq);
363 #ifndef CONFIG_INLINE_WRITE_UNLOCK_BH
364 noinline void __lockfunc _raw_write_unlock_bh(rwlock_t *lock)
366 __raw_write_unlock_bh(lock);
368 EXPORT_SYMBOL(_raw_write_unlock_bh);
371 #endif /* !CONFIG_PREEMPT_RT */
373 #ifdef CONFIG_DEBUG_LOCK_ALLOC
375 void __lockfunc _raw_spin_lock_nested(raw_spinlock_t *lock, int subclass)
378 spin_acquire(&lock->dep_map, subclass, 0, _RET_IP_);
379 LOCK_CONTENDED(lock, do_raw_spin_trylock, do_raw_spin_lock);
381 EXPORT_SYMBOL(_raw_spin_lock_nested);
383 unsigned long __lockfunc _raw_spin_lock_irqsave_nested(raw_spinlock_t *lock,
388 local_irq_save(flags);
390 spin_acquire(&lock->dep_map, subclass, 0, _RET_IP_);
391 LOCK_CONTENDED(lock, do_raw_spin_trylock, do_raw_spin_lock);
394 EXPORT_SYMBOL(_raw_spin_lock_irqsave_nested);
396 void __lockfunc _raw_spin_lock_nest_lock(raw_spinlock_t *lock,
397 struct lockdep_map *nest_lock)
400 spin_acquire_nest(&lock->dep_map, 0, 0, nest_lock, _RET_IP_);
401 LOCK_CONTENDED(lock, do_raw_spin_trylock, do_raw_spin_lock);
403 EXPORT_SYMBOL(_raw_spin_lock_nest_lock);
407 notrace int in_lock_functions(unsigned long addr)
409 /* Linker adds these: start and end of __lockfunc functions */
410 extern char __lock_text_start[], __lock_text_end[];
412 return addr >= (unsigned long)__lock_text_start
413 && addr < (unsigned long)__lock_text_end;
415 EXPORT_SYMBOL(in_lock_functions);