Merge tag 'for_linus' of git://git.kernel.org/pub/scm/linux/kernel/git/mst/vhost
[cris-mirror.git] / kernel / locking / spinlock.c
blob936f3d14dd6bfeda3ef7921266fef5dc5b36e2a8
1 // SPDX-License-Identifier: GPL-2.0
2 /*
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 * If lockdep is enabled then we use the non-preemption spin-ops
27 * even on CONFIG_PREEMPT, because lockdep assumes that interrupts are
28 * not re-enabled during lock-acquire (which the preempt-spin-ops do):
30 #if !defined(CONFIG_GENERIC_LOCKBREAK) || defined(CONFIG_DEBUG_LOCK_ALLOC)
32 * The __lock_function inlines are taken from
33 * spinlock : include/linux/spinlock_api_smp.h
34 * rwlock : include/linux/rwlock_api_smp.h
36 #else
39 * Some architectures can relax in favour of the CPU owning the lock.
41 #ifndef arch_read_relax
42 # define arch_read_relax(l) cpu_relax()
43 #endif
44 #ifndef arch_write_relax
45 # define arch_write_relax(l) cpu_relax()
46 #endif
47 #ifndef arch_spin_relax
48 # define arch_spin_relax(l) cpu_relax()
49 #endif
52 * We build the __lock_function inlines here. They are too large for
53 * inlining all over the place, but here is only one user per function
54 * which embedds them into the calling _lock_function below.
56 * This could be a long-held lock. We both prepare to spin for a long
57 * time (making _this_ CPU preemptable if possible), and we also signal
58 * towards that other CPU that it should break the lock ASAP.
60 #define BUILD_LOCK_OPS(op, locktype) \
61 void __lockfunc __raw_##op##_lock(locktype##_t *lock) \
62 { \
63 for (;;) { \
64 preempt_disable(); \
65 if (likely(do_raw_##op##_trylock(lock))) \
66 break; \
67 preempt_enable(); \
69 arch_##op##_relax(&lock->raw_lock); \
70 } \
71 } \
73 unsigned long __lockfunc __raw_##op##_lock_irqsave(locktype##_t *lock) \
74 { \
75 unsigned long flags; \
77 for (;;) { \
78 preempt_disable(); \
79 local_irq_save(flags); \
80 if (likely(do_raw_##op##_trylock(lock))) \
81 break; \
82 local_irq_restore(flags); \
83 preempt_enable(); \
85 arch_##op##_relax(&lock->raw_lock); \
86 } \
88 return flags; \
89 } \
91 void __lockfunc __raw_##op##_lock_irq(locktype##_t *lock) \
92 { \
93 _raw_##op##_lock_irqsave(lock); \
94 } \
96 void __lockfunc __raw_##op##_lock_bh(locktype##_t *lock) \
97 { \
98 unsigned long flags; \
100 /* */ \
101 /* Careful: we must exclude softirqs too, hence the */ \
102 /* irq-disabling. We use the generic preemption-aware */ \
103 /* function: */ \
104 /**/ \
105 flags = _raw_##op##_lock_irqsave(lock); \
106 local_bh_disable(); \
107 local_irq_restore(flags); \
111 * Build preemption-friendly versions of the following
112 * lock-spinning functions:
114 * __[spin|read|write]_lock()
115 * __[spin|read|write]_lock_irq()
116 * __[spin|read|write]_lock_irqsave()
117 * __[spin|read|write]_lock_bh()
119 BUILD_LOCK_OPS(spin, raw_spinlock);
120 BUILD_LOCK_OPS(read, rwlock);
121 BUILD_LOCK_OPS(write, rwlock);
123 #endif
125 #ifndef CONFIG_INLINE_SPIN_TRYLOCK
126 int __lockfunc _raw_spin_trylock(raw_spinlock_t *lock)
128 return __raw_spin_trylock(lock);
130 EXPORT_SYMBOL(_raw_spin_trylock);
131 #endif
133 #ifndef CONFIG_INLINE_SPIN_TRYLOCK_BH
134 int __lockfunc _raw_spin_trylock_bh(raw_spinlock_t *lock)
136 return __raw_spin_trylock_bh(lock);
138 EXPORT_SYMBOL(_raw_spin_trylock_bh);
139 #endif
141 #ifndef CONFIG_INLINE_SPIN_LOCK
142 void __lockfunc _raw_spin_lock(raw_spinlock_t *lock)
144 __raw_spin_lock(lock);
146 EXPORT_SYMBOL(_raw_spin_lock);
147 #endif
149 #ifndef CONFIG_INLINE_SPIN_LOCK_IRQSAVE
150 unsigned long __lockfunc _raw_spin_lock_irqsave(raw_spinlock_t *lock)
152 return __raw_spin_lock_irqsave(lock);
154 EXPORT_SYMBOL(_raw_spin_lock_irqsave);
155 #endif
157 #ifndef CONFIG_INLINE_SPIN_LOCK_IRQ
158 void __lockfunc _raw_spin_lock_irq(raw_spinlock_t *lock)
160 __raw_spin_lock_irq(lock);
162 EXPORT_SYMBOL(_raw_spin_lock_irq);
163 #endif
165 #ifndef CONFIG_INLINE_SPIN_LOCK_BH
166 void __lockfunc _raw_spin_lock_bh(raw_spinlock_t *lock)
168 __raw_spin_lock_bh(lock);
170 EXPORT_SYMBOL(_raw_spin_lock_bh);
171 #endif
173 #ifdef CONFIG_UNINLINE_SPIN_UNLOCK
174 void __lockfunc _raw_spin_unlock(raw_spinlock_t *lock)
176 __raw_spin_unlock(lock);
178 EXPORT_SYMBOL(_raw_spin_unlock);
179 #endif
181 #ifndef CONFIG_INLINE_SPIN_UNLOCK_IRQRESTORE
182 void __lockfunc _raw_spin_unlock_irqrestore(raw_spinlock_t *lock, unsigned long flags)
184 __raw_spin_unlock_irqrestore(lock, flags);
186 EXPORT_SYMBOL(_raw_spin_unlock_irqrestore);
187 #endif
189 #ifndef CONFIG_INLINE_SPIN_UNLOCK_IRQ
190 void __lockfunc _raw_spin_unlock_irq(raw_spinlock_t *lock)
192 __raw_spin_unlock_irq(lock);
194 EXPORT_SYMBOL(_raw_spin_unlock_irq);
195 #endif
197 #ifndef CONFIG_INLINE_SPIN_UNLOCK_BH
198 void __lockfunc _raw_spin_unlock_bh(raw_spinlock_t *lock)
200 __raw_spin_unlock_bh(lock);
202 EXPORT_SYMBOL(_raw_spin_unlock_bh);
203 #endif
205 #ifndef CONFIG_INLINE_READ_TRYLOCK
206 int __lockfunc _raw_read_trylock(rwlock_t *lock)
208 return __raw_read_trylock(lock);
210 EXPORT_SYMBOL(_raw_read_trylock);
211 #endif
213 #ifndef CONFIG_INLINE_READ_LOCK
214 void __lockfunc _raw_read_lock(rwlock_t *lock)
216 __raw_read_lock(lock);
218 EXPORT_SYMBOL(_raw_read_lock);
219 #endif
221 #ifndef CONFIG_INLINE_READ_LOCK_IRQSAVE
222 unsigned long __lockfunc _raw_read_lock_irqsave(rwlock_t *lock)
224 return __raw_read_lock_irqsave(lock);
226 EXPORT_SYMBOL(_raw_read_lock_irqsave);
227 #endif
229 #ifndef CONFIG_INLINE_READ_LOCK_IRQ
230 void __lockfunc _raw_read_lock_irq(rwlock_t *lock)
232 __raw_read_lock_irq(lock);
234 EXPORT_SYMBOL(_raw_read_lock_irq);
235 #endif
237 #ifndef CONFIG_INLINE_READ_LOCK_BH
238 void __lockfunc _raw_read_lock_bh(rwlock_t *lock)
240 __raw_read_lock_bh(lock);
242 EXPORT_SYMBOL(_raw_read_lock_bh);
243 #endif
245 #ifndef CONFIG_INLINE_READ_UNLOCK
246 void __lockfunc _raw_read_unlock(rwlock_t *lock)
248 __raw_read_unlock(lock);
250 EXPORT_SYMBOL(_raw_read_unlock);
251 #endif
253 #ifndef CONFIG_INLINE_READ_UNLOCK_IRQRESTORE
254 void __lockfunc _raw_read_unlock_irqrestore(rwlock_t *lock, unsigned long flags)
256 __raw_read_unlock_irqrestore(lock, flags);
258 EXPORT_SYMBOL(_raw_read_unlock_irqrestore);
259 #endif
261 #ifndef CONFIG_INLINE_READ_UNLOCK_IRQ
262 void __lockfunc _raw_read_unlock_irq(rwlock_t *lock)
264 __raw_read_unlock_irq(lock);
266 EXPORT_SYMBOL(_raw_read_unlock_irq);
267 #endif
269 #ifndef CONFIG_INLINE_READ_UNLOCK_BH
270 void __lockfunc _raw_read_unlock_bh(rwlock_t *lock)
272 __raw_read_unlock_bh(lock);
274 EXPORT_SYMBOL(_raw_read_unlock_bh);
275 #endif
277 #ifndef CONFIG_INLINE_WRITE_TRYLOCK
278 int __lockfunc _raw_write_trylock(rwlock_t *lock)
280 return __raw_write_trylock(lock);
282 EXPORT_SYMBOL(_raw_write_trylock);
283 #endif
285 #ifndef CONFIG_INLINE_WRITE_LOCK
286 void __lockfunc _raw_write_lock(rwlock_t *lock)
288 __raw_write_lock(lock);
290 EXPORT_SYMBOL(_raw_write_lock);
291 #endif
293 #ifndef CONFIG_INLINE_WRITE_LOCK_IRQSAVE
294 unsigned long __lockfunc _raw_write_lock_irqsave(rwlock_t *lock)
296 return __raw_write_lock_irqsave(lock);
298 EXPORT_SYMBOL(_raw_write_lock_irqsave);
299 #endif
301 #ifndef CONFIG_INLINE_WRITE_LOCK_IRQ
302 void __lockfunc _raw_write_lock_irq(rwlock_t *lock)
304 __raw_write_lock_irq(lock);
306 EXPORT_SYMBOL(_raw_write_lock_irq);
307 #endif
309 #ifndef CONFIG_INLINE_WRITE_LOCK_BH
310 void __lockfunc _raw_write_lock_bh(rwlock_t *lock)
312 __raw_write_lock_bh(lock);
314 EXPORT_SYMBOL(_raw_write_lock_bh);
315 #endif
317 #ifndef CONFIG_INLINE_WRITE_UNLOCK
318 void __lockfunc _raw_write_unlock(rwlock_t *lock)
320 __raw_write_unlock(lock);
322 EXPORT_SYMBOL(_raw_write_unlock);
323 #endif
325 #ifndef CONFIG_INLINE_WRITE_UNLOCK_IRQRESTORE
326 void __lockfunc _raw_write_unlock_irqrestore(rwlock_t *lock, unsigned long flags)
328 __raw_write_unlock_irqrestore(lock, flags);
330 EXPORT_SYMBOL(_raw_write_unlock_irqrestore);
331 #endif
333 #ifndef CONFIG_INLINE_WRITE_UNLOCK_IRQ
334 void __lockfunc _raw_write_unlock_irq(rwlock_t *lock)
336 __raw_write_unlock_irq(lock);
338 EXPORT_SYMBOL(_raw_write_unlock_irq);
339 #endif
341 #ifndef CONFIG_INLINE_WRITE_UNLOCK_BH
342 void __lockfunc _raw_write_unlock_bh(rwlock_t *lock)
344 __raw_write_unlock_bh(lock);
346 EXPORT_SYMBOL(_raw_write_unlock_bh);
347 #endif
349 #ifdef CONFIG_DEBUG_LOCK_ALLOC
351 void __lockfunc _raw_spin_lock_nested(raw_spinlock_t *lock, int subclass)
353 preempt_disable();
354 spin_acquire(&lock->dep_map, subclass, 0, _RET_IP_);
355 LOCK_CONTENDED(lock, do_raw_spin_trylock, do_raw_spin_lock);
357 EXPORT_SYMBOL(_raw_spin_lock_nested);
359 unsigned long __lockfunc _raw_spin_lock_irqsave_nested(raw_spinlock_t *lock,
360 int subclass)
362 unsigned long flags;
364 local_irq_save(flags);
365 preempt_disable();
366 spin_acquire(&lock->dep_map, subclass, 0, _RET_IP_);
367 LOCK_CONTENDED_FLAGS(lock, do_raw_spin_trylock, do_raw_spin_lock,
368 do_raw_spin_lock_flags, &flags);
369 return flags;
371 EXPORT_SYMBOL(_raw_spin_lock_irqsave_nested);
373 void __lockfunc _raw_spin_lock_nest_lock(raw_spinlock_t *lock,
374 struct lockdep_map *nest_lock)
376 preempt_disable();
377 spin_acquire_nest(&lock->dep_map, 0, 0, nest_lock, _RET_IP_);
378 LOCK_CONTENDED(lock, do_raw_spin_trylock, do_raw_spin_lock);
380 EXPORT_SYMBOL(_raw_spin_lock_nest_lock);
382 #endif
384 notrace int in_lock_functions(unsigned long addr)
386 /* Linker adds these: start and end of __lockfunc functions */
387 extern char __lock_text_start[], __lock_text_end[];
389 return addr >= (unsigned long)__lock_text_start
390 && addr < (unsigned long)__lock_text_end;
392 EXPORT_SYMBOL(in_lock_functions);