kernel_lock.c 2.9 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4
/*
 * lib/kernel_lock.c
 *
 * This is the traditional BKL - big kernel lock. Largely
S
Simon Arlott 已提交
5
 * relegated to obsolescence, but used by various less
L
Linus Torvalds 已提交
6 7 8 9 10
 * important (or lazy) subsystems.
 */
#include <linux/smp_lock.h>
#include <linux/module.h>
#include <linux/kallsyms.h>
11
#include <linux/semaphore.h>
L
Linus Torvalds 已提交
12 13

/*
14
 * The 'big kernel lock'
L
Linus Torvalds 已提交
15
 *
16
 * This spinlock is taken and released recursively by lock_kernel()
A
Andreas Mohr 已提交
17
 * and unlock_kernel().  It is transparently dropped and reacquired
L
Linus Torvalds 已提交
18 19 20 21 22
 * over schedule().  It is used to protect legacy code that hasn't
 * been migrated to a proper locking design yet.
 *
 * Don't use in new code.
 */
23 24
static  __cacheline_aligned_in_smp DEFINE_SPINLOCK(kernel_flag);

L
Linus Torvalds 已提交
25 26

/*
27
 * Acquire/release the underlying lock from the scheduler.
L
Linus Torvalds 已提交
28
 *
29 30 31
 * This is called with preemption disabled, and should
 * return an error value if it cannot get the lock and
 * TIF_NEED_RESCHED gets set.
L
Linus Torvalds 已提交
32
 *
33 34 35 36 37
 * If it successfully gets the lock, it should increment
 * the preemption count like any spinlock does.
 *
 * (This works on UP too - _raw_spin_trylock will never
 * return false in that case)
L
Linus Torvalds 已提交
38 39 40
 */
int __lockfunc __reacquire_kernel_lock(void)
{
41 42 43 44 45
	while (!_raw_spin_trylock(&kernel_flag)) {
		if (test_thread_flag(TIF_NEED_RESCHED))
			return -EAGAIN;
		cpu_relax();
	}
L
Linus Torvalds 已提交
46 47 48 49 50 51
	preempt_disable();
	return 0;
}

void __lockfunc __release_kernel_lock(void)
{
52 53
	_raw_spin_unlock(&kernel_flag);
	preempt_enable_no_resched();
L
Linus Torvalds 已提交
54 55 56
}

/*
57 58 59
 * These are the BKL spinlocks - we try to be polite about preemption.
 * If SMP is not on (ie UP preemption), this all goes away because the
 * _raw_spin_trylock() will always succeed.
L
Linus Torvalds 已提交
60
 */
61 62
#ifdef CONFIG_PREEMPT
static inline void __lock_kernel(void)
L
Linus Torvalds 已提交
63
{
64 65 66 67 68 69 70 71 72 73 74
	preempt_disable();
	if (unlikely(!_raw_spin_trylock(&kernel_flag))) {
		/*
		 * If preemption was disabled even before this
		 * was called, there's nothing we can be polite
		 * about - just spin.
		 */
		if (preempt_count() > 1) {
			_raw_spin_lock(&kernel_flag);
			return;
		}
L
Linus Torvalds 已提交
75 76

		/*
77 78
		 * Otherwise, let's wait for the kernel lock
		 * with preemption enabled..
L
Linus Torvalds 已提交
79
		 */
80 81 82 83 84 85 86 87
		do {
			preempt_enable();
			while (spin_is_locked(&kernel_flag))
				cpu_relax();
			preempt_disable();
		} while (!_raw_spin_trylock(&kernel_flag));
	}
}
L
Linus Torvalds 已提交
88

89 90 91 92 93 94 95 96
#else

/*
 * Non-preemption case - just get the spinlock
 */
static inline void __lock_kernel(void)
{
	_raw_spin_lock(&kernel_flag);
L
Linus Torvalds 已提交
97
}
98
#endif
L
Linus Torvalds 已提交
99

100
static inline void __unlock_kernel(void)
L
Linus Torvalds 已提交
101
{
102 103 104 105 106 107 108
	/*
	 * the BKL is not covered by lockdep, so we open-code the
	 * unlocking sequence (and thus avoid the dep-chain ops):
	 */
	_raw_spin_unlock(&kernel_flag);
	preempt_enable();
}
L
Linus Torvalds 已提交
109

110 111 112 113 114 115 116 117 118 119 120 121 122
/*
 * Getting the big kernel lock.
 *
 * This cannot happen asynchronously, so we only need to
 * worry about other CPU's.
 */
void __lockfunc lock_kernel(void)
{
	int depth = current->lock_depth+1;
	if (likely(!depth))
		__lock_kernel();
	current->lock_depth = depth;
}
L
Linus Torvalds 已提交
123

124 125 126 127 128
void __lockfunc unlock_kernel(void)
{
	BUG_ON(current->lock_depth < 0);
	if (likely(--current->lock_depth < 0))
		__unlock_kernel();
L
Linus Torvalds 已提交
129 130 131 132 133
}

EXPORT_SYMBOL(lock_kernel);
EXPORT_SYMBOL(unlock_kernel);