blob: d80e1226586233641a6ac84f8e2bfaf6b17b72f0 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * lib/kernel_lock.c
3 *
4 * This is the traditional BKL - big kernel lock. Largely
Simon Arlott5895df92007-10-20 01:29:18 +02005 * relegated to obsolescence, but used by various less
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 * important (or lazy) subsystems.
7 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07008#include <linux/module.h>
9#include <linux/kallsyms.h>
Matthew Wilcox6188e102008-04-18 22:21:05 -040010#include <linux/semaphore.h>
Frederic Weisbecker96a2c462009-08-01 01:34:24 +020011#include <linux/smp_lock.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070012
Linus Torvalds1da177e2005-04-16 15:20:36 -070013/*
Linus Torvalds8e3e0762008-05-10 20:58:02 -070014 * The 'big kernel lock'
Linus Torvalds1da177e2005-04-16 15:20:36 -070015 *
Linus Torvalds8e3e0762008-05-10 20:58:02 -070016 * This spinlock is taken and released recursively by lock_kernel()
Andreas Mohrd6e05ed2006-06-26 18:35:02 +020017 * and unlock_kernel(). It is transparently dropped and reacquired
Linus Torvalds1da177e2005-04-16 15:20:36 -070018 * over schedule(). It is used to protect legacy code that hasn't
19 * been migrated to a proper locking design yet.
20 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070021 * Don't use in new code.
22 */
Thomas Gleixnerfa4062e2009-11-17 14:45:06 +010023static __cacheline_aligned_in_smp DEFINE_RAW_SPINLOCK(kernel_flag);
Linus Torvalds8e3e0762008-05-10 20:58:02 -070024
Linus Torvalds1da177e2005-04-16 15:20:36 -070025
26/*
Linus Torvalds8e3e0762008-05-10 20:58:02 -070027 * Acquire/release the underlying lock from the scheduler.
Linus Torvalds1da177e2005-04-16 15:20:36 -070028 *
Linus Torvalds8e3e0762008-05-10 20:58:02 -070029 * This is called with preemption disabled, and should
30 * return an error value if it cannot get the lock and
31 * TIF_NEED_RESCHED gets set.
Linus Torvalds1da177e2005-04-16 15:20:36 -070032 *
Linus Torvalds8e3e0762008-05-10 20:58:02 -070033 * If it successfully gets the lock, it should increment
34 * the preemption count like any spinlock does.
35 *
Thomas Gleixner9828ea92009-12-03 20:55:53 +010036 * (This works on UP too - do_raw_spin_trylock will never
Linus Torvalds8e3e0762008-05-10 20:58:02 -070037 * return false in that case)
Linus Torvalds1da177e2005-04-16 15:20:36 -070038 */
39int __lockfunc __reacquire_kernel_lock(void)
40{
Thomas Gleixner9828ea92009-12-03 20:55:53 +010041 while (!do_raw_spin_trylock(&kernel_flag)) {
Lai Jiangshan5ed0cec2009-03-06 19:40:20 +080042 if (need_resched())
Linus Torvalds8e3e0762008-05-10 20:58:02 -070043 return -EAGAIN;
44 cpu_relax();
45 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070046 preempt_disable();
Linus Torvalds1da177e2005-04-16 15:20:36 -070047 return 0;
48}
49
50void __lockfunc __release_kernel_lock(void)
51{
Thomas Gleixner9828ea92009-12-03 20:55:53 +010052 do_raw_spin_unlock(&kernel_flag);
Linus Torvalds8e3e0762008-05-10 20:58:02 -070053 preempt_enable_no_resched();
Linus Torvalds1da177e2005-04-16 15:20:36 -070054}
55
56/*
Linus Torvalds8e3e0762008-05-10 20:58:02 -070057 * These are the BKL spinlocks - we try to be polite about preemption.
58 * If SMP is not on (ie UP preemption), this all goes away because the
Thomas Gleixner9828ea92009-12-03 20:55:53 +010059 * do_raw_spin_trylock() will always succeed.
Linus Torvalds8e3e0762008-05-10 20:58:02 -070060 */
61#ifdef CONFIG_PREEMPT
62static inline void __lock_kernel(void)
63{
64 preempt_disable();
Thomas Gleixner9828ea92009-12-03 20:55:53 +010065 if (unlikely(!do_raw_spin_trylock(&kernel_flag))) {
Linus Torvalds8e3e0762008-05-10 20:58:02 -070066 /*
67 * If preemption was disabled even before this
68 * was called, there's nothing we can be polite
69 * about - just spin.
70 */
71 if (preempt_count() > 1) {
Thomas Gleixner9828ea92009-12-03 20:55:53 +010072 do_raw_spin_lock(&kernel_flag);
Linus Torvalds8e3e0762008-05-10 20:58:02 -070073 return;
74 }
75
76 /*
77 * Otherwise, let's wait for the kernel lock
78 * with preemption enabled..
79 */
80 do {
81 preempt_enable();
Thomas Gleixnerfa4062e2009-11-17 14:45:06 +010082 while (raw_spin_is_locked(&kernel_flag))
Linus Torvalds8e3e0762008-05-10 20:58:02 -070083 cpu_relax();
84 preempt_disable();
Thomas Gleixner9828ea92009-12-03 20:55:53 +010085 } while (!do_raw_spin_trylock(&kernel_flag));
Linus Torvalds8e3e0762008-05-10 20:58:02 -070086 }
87}
88
89#else
90
91/*
92 * Non-preemption case - just get the spinlock
93 */
94static inline void __lock_kernel(void)
95{
Thomas Gleixner9828ea92009-12-03 20:55:53 +010096 do_raw_spin_lock(&kernel_flag);
Linus Torvalds8e3e0762008-05-10 20:58:02 -070097}
98#endif
99
100static inline void __unlock_kernel(void)
101{
102 /*
103 * the BKL is not covered by lockdep, so we open-code the
104 * unlocking sequence (and thus avoid the dep-chain ops):
105 */
Thomas Gleixner9828ea92009-12-03 20:55:53 +0100106 do_raw_spin_unlock(&kernel_flag);
Linus Torvalds8e3e0762008-05-10 20:58:02 -0700107 preempt_enable();
108}
109
110/*
111 * Getting the big kernel lock.
112 *
113 * This cannot happen asynchronously, so we only need to
114 * worry about other CPU's.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 */
Frederic Weisbecker925936e2009-09-28 17:12:49 +0200116void __lockfunc _lock_kernel(const char *func, const char *file, int line)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117{
Frederic Weisbecker925936e2009-09-28 17:12:49 +0200118 int depth = current->lock_depth + 1;
119
Linus Torvaldsf01eb362009-12-12 14:46:33 -0800120 if (likely(!depth)) {
121 might_sleep();
Linus Torvalds8e3e0762008-05-10 20:58:02 -0700122 __lock_kernel();
Linus Torvaldsf01eb362009-12-12 14:46:33 -0800123 }
Linus Torvalds8e3e0762008-05-10 20:58:02 -0700124 current->lock_depth = depth;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125}
126
Frederic Weisbecker925936e2009-09-28 17:12:49 +0200127void __lockfunc _unlock_kernel(const char *func, const char *file, int line)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128{
Linus Torvalds8e3e0762008-05-10 20:58:02 -0700129 BUG_ON(current->lock_depth < 0);
130 if (likely(--current->lock_depth < 0))
131 __unlock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132}
133
Frederic Weisbecker96a2c462009-08-01 01:34:24 +0200134EXPORT_SYMBOL(_lock_kernel);
135EXPORT_SYMBOL(_unlock_kernel);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136