4 * This is the traditional BKL - big kernel lock. Largely
5 * relegated to obsolescence, but used by various less
6 * important (or lazy) subsystems.
8 #include <linux/module.h>
9 #include <linux/kallsyms.h>
10 #include <linux/semaphore.h>
11 #include <linux/smp_lock.h>
13 #define CREATE_TRACE_POINTS
14 #include <trace/events/bkl.h>
17 * The 'big kernel lock'
19 * This spinlock is taken and released recursively by lock_kernel()
20 * and unlock_kernel(). It is transparently dropped and reacquired
21 * over schedule(). It is used to protect legacy code that hasn't
22 * been migrated to a proper locking design yet.
24 * Don't use in new code.
26 static __cacheline_aligned_in_smp
DEFINE_SPINLOCK(kernel_flag
);
30 * Acquire/release the underlying lock from the scheduler.
32 * This is called with preemption disabled, and should
33 * return an error value if it cannot get the lock and
34 * TIF_NEED_RESCHED gets set.
36 * If it successfully gets the lock, it should increment
37 * the preemption count like any spinlock does.
39 * (This works on UP too - _raw_spin_trylock will never
40 * return false in that case)
42 int __lockfunc
__reacquire_kernel_lock(void)
44 while (!_raw_spin_trylock(&kernel_flag
)) {
53 void __lockfunc
__release_kernel_lock(void)
55 _raw_spin_unlock(&kernel_flag
);
56 preempt_enable_no_resched();
60 * These are the BKL spinlocks - we try to be polite about preemption.
61 * If SMP is not on (ie UP preemption), this all goes away because the
62 * _raw_spin_trylock() will always succeed.
65 static inline void __lock_kernel(void)
68 if (unlikely(!_raw_spin_trylock(&kernel_flag
))) {
70 * If preemption was disabled even before this
71 * was called, there's nothing we can be polite
74 if (preempt_count() > 1) {
75 _raw_spin_lock(&kernel_flag
);
80 * Otherwise, let's wait for the kernel lock
81 * with preemption enabled..
85 while (spin_is_locked(&kernel_flag
))
88 } while (!_raw_spin_trylock(&kernel_flag
));
95 * Non-preemption case - just get the spinlock
97 static inline void __lock_kernel(void)
99 _raw_spin_lock(&kernel_flag
);
103 static inline void __unlock_kernel(void)
106 * the BKL is not covered by lockdep, so we open-code the
107 * unlocking sequence (and thus avoid the dep-chain ops):
109 _raw_spin_unlock(&kernel_flag
);
114 * Getting the big kernel lock.
116 * This cannot happen asynchronously, so we only need to
117 * worry about other CPU's.
119 void __lockfunc
_lock_kernel(const char *func
, const char *file
, int line
)
121 int depth
= current
->lock_depth
+ 1;
123 trace_lock_kernel(func
, file
, line
);
125 if (likely(!depth
)) {
129 current
->lock_depth
= depth
;
132 void __lockfunc
_unlock_kernel(const char *func
, const char *file
, int line
)
134 BUG_ON(current
->lock_depth
< 0);
135 if (likely(--current
->lock_depth
< 0))
138 trace_unlock_kernel(func
, file
, line
);
141 EXPORT_SYMBOL(_lock_kernel
);
142 EXPORT_SYMBOL(_unlock_kernel
);