Committer: Michael Beasley <mike@snafu.setup>
[mikesnafu-overlay.git] / lib / kernel_lock.c
blob812dbf00844bc08a4a844bda6729377b1923a5c5
1 /*
2 * lib/kernel_lock.c
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/smp_lock.h>
9 #include <linux/module.h>
10 #include <linux/kallsyms.h>
13 * The 'big kernel semaphore'
15 * This mutex is taken and released recursively by lock_kernel()
16 * and unlock_kernel(). It is transparently dropped and reacquired
17 * over schedule(). It is used to protect legacy code that hasn't
18 * been migrated to a proper locking design yet.
20 * Note: code locked by this semaphore will only be serialized against
21 * other code using the same locking facility. The code guarantees that
22 * the task remains on the same CPU.
24 * Don't use in new code.
26 static DECLARE_MUTEX(kernel_sem);
29 * Re-acquire the kernel semaphore.
31 * This function is called with preemption off.
33 * We are executing in schedule() so the code must be extremely careful
34 * about recursion, both due to the down() and due to the enabling of
35 * preemption. schedule() will re-check the preemption flag after
36 * reacquiring the semaphore.
38 int __lockfunc __reacquire_kernel_lock(void)
40 struct task_struct *task = current;
41 int saved_lock_depth = task->lock_depth;
43 BUG_ON(saved_lock_depth < 0);
45 task->lock_depth = -1;
46 preempt_enable_no_resched();
48 down(&kernel_sem);
50 preempt_disable();
51 task->lock_depth = saved_lock_depth;
53 return 0;
56 void __lockfunc __release_kernel_lock(void)
58 up(&kernel_sem);
62 * Getting the big kernel semaphore.
64 void __lockfunc lock_kernel(void)
66 struct task_struct *task = current;
67 int depth = task->lock_depth + 1;
69 if (likely(!depth))
71 * No recursion worries - we set up lock_depth _after_
73 down(&kernel_sem);
75 task->lock_depth = depth;
78 void __lockfunc unlock_kernel(void)
80 struct task_struct *task = current;
82 BUG_ON(task->lock_depth < 0);
84 if (likely(--task->lock_depth < 0))
85 up(&kernel_sem);
88 EXPORT_SYMBOL(lock_kernel);
89 EXPORT_SYMBOL(unlock_kernel);