Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * lib/kernel_lock.c |
| 3 | * |
| 4 | * This is the traditional BKL - big kernel lock. Largely |
Simon Arlott | 5895df9 | 2007-10-20 01:29:18 +0200 | [diff] [blame] | 5 | * relegated to obsolescence, but used by various less |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6 | * important (or lazy) subsystems. |
| 7 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8 | #include <linux/module.h> |
| 9 | #include <linux/kallsyms.h> |
Matthew Wilcox | 6188e10 | 2008-04-18 22:21:05 -0400 | [diff] [blame] | 10 | #include <linux/semaphore.h> |
Frederic Weisbecker | 96a2c46 | 2009-08-01 01:34:24 +0200 | [diff] [blame] | 11 | #include <linux/smp_lock.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 12 | |
Frederic Weisbecker | 925936e | 2009-09-28 17:12:49 +0200 | [diff] [blame] | 13 | #define CREATE_TRACE_POINTS |
| 14 | #include <trace/events/bkl.h> |
| 15 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 16 | /* |
Linus Torvalds | 8e3e076 | 2008-05-10 20:58:02 -0700 | [diff] [blame] | 17 | * The 'big kernel lock' |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 18 | * |
Linus Torvalds | 8e3e076 | 2008-05-10 20:58:02 -0700 | [diff] [blame] | 19 | * This spinlock is taken and released recursively by lock_kernel() |
Andreas Mohr | d6e05ed | 2006-06-26 18:35:02 +0200 | [diff] [blame] | 20 | * and unlock_kernel(). It is transparently dropped and reacquired |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 21 | * over schedule(). It is used to protect legacy code that hasn't |
| 22 | * been migrated to a proper locking design yet. |
| 23 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 24 | * Don't use in new code. |
| 25 | */ |
Linus Torvalds | 8e3e076 | 2008-05-10 20:58:02 -0700 | [diff] [blame] | 26 | static __cacheline_aligned_in_smp DEFINE_SPINLOCK(kernel_flag); |
| 27 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 28 | |
| 29 | /* |
Linus Torvalds | 8e3e076 | 2008-05-10 20:58:02 -0700 | [diff] [blame] | 30 | * Acquire/release the underlying lock from the scheduler. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 31 | * |
Linus Torvalds | 8e3e076 | 2008-05-10 20:58:02 -0700 | [diff] [blame] | 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. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 35 | * |
Linus Torvalds | 8e3e076 | 2008-05-10 20:58:02 -0700 | [diff] [blame] | 36 | * If it successfully gets the lock, it should increment |
| 37 | * the preemption count like any spinlock does. |
| 38 | * |
| 39 | * (This works on UP too - _raw_spin_trylock will never |
| 40 | * return false in that case) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 41 | */ |
| 42 | int __lockfunc __reacquire_kernel_lock(void) |
| 43 | { |
Linus Torvalds | 8e3e076 | 2008-05-10 20:58:02 -0700 | [diff] [blame] | 44 | while (!_raw_spin_trylock(&kernel_flag)) { |
Lai Jiangshan | 5ed0cec | 2009-03-06 19:40:20 +0800 | [diff] [blame] | 45 | if (need_resched()) |
Linus Torvalds | 8e3e076 | 2008-05-10 20:58:02 -0700 | [diff] [blame] | 46 | return -EAGAIN; |
| 47 | cpu_relax(); |
| 48 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 49 | preempt_disable(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 50 | return 0; |
| 51 | } |
| 52 | |
| 53 | void __lockfunc __release_kernel_lock(void) |
| 54 | { |
Linus Torvalds | 8e3e076 | 2008-05-10 20:58:02 -0700 | [diff] [blame] | 55 | _raw_spin_unlock(&kernel_flag); |
| 56 | preempt_enable_no_resched(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | /* |
Linus Torvalds | 8e3e076 | 2008-05-10 20:58:02 -0700 | [diff] [blame] | 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. |
| 63 | */ |
| 64 | #ifdef CONFIG_PREEMPT |
| 65 | static inline void __lock_kernel(void) |
| 66 | { |
| 67 | preempt_disable(); |
| 68 | if (unlikely(!_raw_spin_trylock(&kernel_flag))) { |
| 69 | /* |
| 70 | * If preemption was disabled even before this |
| 71 | * was called, there's nothing we can be polite |
| 72 | * about - just spin. |
| 73 | */ |
| 74 | if (preempt_count() > 1) { |
| 75 | _raw_spin_lock(&kernel_flag); |
| 76 | return; |
| 77 | } |
| 78 | |
| 79 | /* |
| 80 | * Otherwise, let's wait for the kernel lock |
| 81 | * with preemption enabled.. |
| 82 | */ |
| 83 | do { |
| 84 | preempt_enable(); |
| 85 | while (spin_is_locked(&kernel_flag)) |
| 86 | cpu_relax(); |
| 87 | preempt_disable(); |
| 88 | } while (!_raw_spin_trylock(&kernel_flag)); |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | #else |
| 93 | |
| 94 | /* |
| 95 | * Non-preemption case - just get the spinlock |
| 96 | */ |
| 97 | static inline void __lock_kernel(void) |
| 98 | { |
| 99 | _raw_spin_lock(&kernel_flag); |
| 100 | } |
| 101 | #endif |
| 102 | |
| 103 | static inline void __unlock_kernel(void) |
| 104 | { |
| 105 | /* |
| 106 | * the BKL is not covered by lockdep, so we open-code the |
| 107 | * unlocking sequence (and thus avoid the dep-chain ops): |
| 108 | */ |
| 109 | _raw_spin_unlock(&kernel_flag); |
| 110 | preempt_enable(); |
| 111 | } |
| 112 | |
| 113 | /* |
| 114 | * Getting the big kernel lock. |
| 115 | * |
| 116 | * This cannot happen asynchronously, so we only need to |
| 117 | * worry about other CPU's. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 118 | */ |
Frederic Weisbecker | 925936e | 2009-09-28 17:12:49 +0200 | [diff] [blame] | 119 | void __lockfunc _lock_kernel(const char *func, const char *file, int line) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 120 | { |
Frederic Weisbecker | 925936e | 2009-09-28 17:12:49 +0200 | [diff] [blame] | 121 | int depth = current->lock_depth + 1; |
| 122 | |
| 123 | trace_lock_kernel(func, file, line); |
| 124 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 125 | if (likely(!depth)) |
Linus Torvalds | 8e3e076 | 2008-05-10 20:58:02 -0700 | [diff] [blame] | 126 | __lock_kernel(); |
| 127 | current->lock_depth = depth; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 128 | } |
| 129 | |
Frederic Weisbecker | 925936e | 2009-09-28 17:12:49 +0200 | [diff] [blame] | 130 | void __lockfunc _unlock_kernel(const char *func, const char *file, int line) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 131 | { |
Linus Torvalds | 8e3e076 | 2008-05-10 20:58:02 -0700 | [diff] [blame] | 132 | BUG_ON(current->lock_depth < 0); |
| 133 | if (likely(--current->lock_depth < 0)) |
| 134 | __unlock_kernel(); |
Frederic Weisbecker | 925936e | 2009-09-28 17:12:49 +0200 | [diff] [blame] | 135 | |
| 136 | trace_unlock_kernel(func, file, line); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 137 | } |
| 138 | |
Frederic Weisbecker | 96a2c46 | 2009-08-01 01:34:24 +0200 | [diff] [blame] | 139 | EXPORT_SYMBOL(_lock_kernel); |
| 140 | EXPORT_SYMBOL(_unlock_kernel); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 141 | |