tracing/bkl: Add bkl ftrace events
[firefly-linux-kernel-4.4.55.git] / lib / kernel_lock.c
1 /*
2  * lib/kernel_lock.c
3  *
4  * This is the traditional BKL - big kernel lock. Largely
5  * relegated to obsolescence, but used by various less
6  * important (or lazy) subsystems.
7  */
8 #include <linux/module.h>
9 #include <linux/kallsyms.h>
10 #include <linux/semaphore.h>
11 #define CREATE_TRACE_POINTS
12 #include <linux/smp_lock.h>
13
14 /*
15  * The 'big kernel lock'
16  *
17  * This spinlock is taken and released recursively by lock_kernel()
18  * and unlock_kernel().  It is transparently dropped and reacquired
19  * over schedule().  It is used to protect legacy code that hasn't
20  * been migrated to a proper locking design yet.
21  *
22  * Don't use in new code.
23  */
24 static  __cacheline_aligned_in_smp DEFINE_SPINLOCK(kernel_flag);
25
26
27 /*
28  * Acquire/release the underlying lock from the scheduler.
29  *
30  * This is called with preemption disabled, and should
31  * return an error value if it cannot get the lock and
32  * TIF_NEED_RESCHED gets set.
33  *
34  * If it successfully gets the lock, it should increment
35  * the preemption count like any spinlock does.
36  *
37  * (This works on UP too - _raw_spin_trylock will never
38  * return false in that case)
39  */
40 int __lockfunc __reacquire_kernel_lock(void)
41 {
42         while (!_raw_spin_trylock(&kernel_flag)) {
43                 if (need_resched())
44                         return -EAGAIN;
45                 cpu_relax();
46         }
47         preempt_disable();
48         return 0;
49 }
50
51 void __lockfunc __release_kernel_lock(void)
52 {
53         _raw_spin_unlock(&kernel_flag);
54         preempt_enable_no_resched();
55 }
56
57 /*
58  * These are the BKL spinlocks - we try to be polite about preemption.
59  * If SMP is not on (ie UP preemption), this all goes away because the
60  * _raw_spin_trylock() will always succeed.
61  */
62 #ifdef CONFIG_PREEMPT
63 static inline void __lock_kernel(void)
64 {
65         preempt_disable();
66         if (unlikely(!_raw_spin_trylock(&kernel_flag))) {
67                 /*
68                  * If preemption was disabled even before this
69                  * was called, there's nothing we can be polite
70                  * about - just spin.
71                  */
72                 if (preempt_count() > 1) {
73                         _raw_spin_lock(&kernel_flag);
74                         return;
75                 }
76
77                 /*
78                  * Otherwise, let's wait for the kernel lock
79                  * with preemption enabled..
80                  */
81                 do {
82                         preempt_enable();
83                         while (spin_is_locked(&kernel_flag))
84                                 cpu_relax();
85                         preempt_disable();
86                 } while (!_raw_spin_trylock(&kernel_flag));
87         }
88 }
89
90 #else
91
92 /*
93  * Non-preemption case - just get the spinlock
94  */
95 static inline void __lock_kernel(void)
96 {
97         _raw_spin_lock(&kernel_flag);
98 }
99 #endif
100
101 static inline void __unlock_kernel(void)
102 {
103         /*
104          * the BKL is not covered by lockdep, so we open-code the
105          * unlocking sequence (and thus avoid the dep-chain ops):
106          */
107         _raw_spin_unlock(&kernel_flag);
108         preempt_enable();
109 }
110
111 /*
112  * Getting the big kernel lock.
113  *
114  * This cannot happen asynchronously, so we only need to
115  * worry about other CPU's.
116  */
117 void __lockfunc _lock_kernel(void)
118 {
119         int depth = current->lock_depth+1;
120         if (likely(!depth))
121                 __lock_kernel();
122         current->lock_depth = depth;
123 }
124
125 void __lockfunc _unlock_kernel(void)
126 {
127         BUG_ON(current->lock_depth < 0);
128         if (likely(--current->lock_depth < 0))
129                 __unlock_kernel();
130 }
131
132 EXPORT_SYMBOL(_lock_kernel);
133 EXPORT_SYMBOL(_unlock_kernel);
134