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/smp_lock.h>
9 #include <linux/module.h>
10 #include <linux/kallsyms.h>
11 #include <linux/semaphore.h>
14 * The 'big kernel semaphore'
16 * This mutex is taken and released recursively by lock_kernel()
17 * and unlock_kernel(). It is transparently dropped and reacquired
18 * over schedule(). It is used to protect legacy code that hasn't
19 * been migrated to a proper locking design yet.
21 * Note: code locked by this semaphore will only be serialized against
22 * other code using the same locking facility. The code guarantees that
23 * the task remains on the same CPU.
25 * Don't use in new code.
27 static DECLARE_MUTEX(kernel_sem
);
30 * Re-acquire the kernel semaphore.
32 * This function is called with preemption off.
34 * We are executing in schedule() so the code must be extremely careful
35 * about recursion, both due to the down() and due to the enabling of
36 * preemption. schedule() will re-check the preemption flag after
37 * reacquiring the semaphore.
39 int __lockfunc
__reacquire_kernel_lock(void)
41 struct task_struct
*task
= current
;
42 int saved_lock_depth
= task
->lock_depth
;
44 BUG_ON(saved_lock_depth
< 0);
46 task
->lock_depth
= -1;
47 preempt_enable_no_resched();
52 task
->lock_depth
= saved_lock_depth
;
57 void __lockfunc
__release_kernel_lock(void)
63 * Getting the big kernel semaphore.
65 void __lockfunc
lock_kernel(void)
67 struct task_struct
*task
= current
;
68 int depth
= task
->lock_depth
+ 1;
72 * No recursion worries - we set up lock_depth _after_
76 task
->lock_depth
= depth
;
79 void __lockfunc
unlock_kernel(void)
81 struct task_struct
*task
= current
;
83 BUG_ON(task
->lock_depth
< 0);
85 if (likely(--task
->lock_depth
< 0))
89 EXPORT_SYMBOL(lock_kernel
);
90 EXPORT_SYMBOL(unlock_kernel
);