4 * This is the traditional BKL - big kernel lock. Largely
5 * relegated to obsolescense, 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>
12 #if defined(CONFIG_PREEMPT) && defined(__smp_processor_id) && \
13 defined(CONFIG_DEBUG_PREEMPT)
18 unsigned int smp_processor_id(void)
20 unsigned long preempt_count
= preempt_count();
21 int this_cpu
= __smp_processor_id();
24 if (likely(preempt_count
))
31 * Kernel threads bound to a single CPU can safely use
34 this_mask
= cpumask_of_cpu(this_cpu
);
36 if (cpus_equal(current
->cpus_allowed
, this_mask
))
40 * It is valid to assume CPU-locality during early bootup:
42 if (system_state
!= SYSTEM_RUNNING
)
50 if (!printk_ratelimit())
53 printk(KERN_ERR
"BUG: using smp_processor_id() in preemptible [%08x] code: %s/%d\n", preempt_count(), current
->comm
, current
->pid
);
54 print_symbol("caller is %s\n", (long)__builtin_return_address(0));
58 preempt_enable_no_resched();
63 EXPORT_SYMBOL(smp_processor_id
);
65 #endif /* PREEMPT && __smp_processor_id && DEBUG_PREEMPT */
67 #ifdef CONFIG_PREEMPT_BKL
69 * The 'big kernel semaphore'
71 * This mutex is taken and released recursively by lock_kernel()
72 * and unlock_kernel(). It is transparently dropped and reaquired
73 * over schedule(). It is used to protect legacy code that hasn't
74 * been migrated to a proper locking design yet.
76 * Note: code locked by this semaphore will only be serialized against
77 * other code using the same locking facility. The code guarantees that
78 * the task remains on the same CPU.
80 * Don't use in new code.
82 static DECLARE_MUTEX(kernel_sem
);
85 * Re-acquire the kernel semaphore.
87 * This function is called with preemption off.
89 * We are executing in schedule() so the code must be extremely careful
90 * about recursion, both due to the down() and due to the enabling of
91 * preemption. schedule() will re-check the preemption flag after
92 * reacquiring the semaphore.
94 int __lockfunc
__reacquire_kernel_lock(void)
96 struct task_struct
*task
= current
;
97 int saved_lock_depth
= task
->lock_depth
;
99 BUG_ON(saved_lock_depth
< 0);
101 task
->lock_depth
= -1;
102 preempt_enable_no_resched();
107 task
->lock_depth
= saved_lock_depth
;
112 void __lockfunc
__release_kernel_lock(void)
118 * Getting the big kernel semaphore.
120 void __lockfunc
lock_kernel(void)
122 struct task_struct
*task
= current
;
123 int depth
= task
->lock_depth
+ 1;
127 * No recursion worries - we set up lock_depth _after_
131 task
->lock_depth
= depth
;
134 void __lockfunc
unlock_kernel(void)
136 struct task_struct
*task
= current
;
138 BUG_ON(task
->lock_depth
< 0);
140 if (likely(--task
->lock_depth
< 0))
147 * The 'big kernel lock'
149 * This spinlock is taken and released recursively by lock_kernel()
150 * and unlock_kernel(). It is transparently dropped and reaquired
151 * over schedule(). It is used to protect legacy code that hasn't
152 * been migrated to a proper locking design yet.
154 * Don't use in new code.
156 static __cacheline_aligned_in_smp
DEFINE_SPINLOCK(kernel_flag
);
160 * Acquire/release the underlying lock from the scheduler.
162 * This is called with preemption disabled, and should
163 * return an error value if it cannot get the lock and
164 * TIF_NEED_RESCHED gets set.
166 * If it successfully gets the lock, it should increment
167 * the preemption count like any spinlock does.
169 * (This works on UP too - _raw_spin_trylock will never
170 * return false in that case)
172 int __lockfunc
__reacquire_kernel_lock(void)
174 while (!_raw_spin_trylock(&kernel_flag
)) {
175 if (test_thread_flag(TIF_NEED_RESCHED
))
183 void __lockfunc
__release_kernel_lock(void)
185 _raw_spin_unlock(&kernel_flag
);
186 preempt_enable_no_resched();
190 * These are the BKL spinlocks - we try to be polite about preemption.
191 * If SMP is not on (ie UP preemption), this all goes away because the
192 * _raw_spin_trylock() will always succeed.
194 #ifdef CONFIG_PREEMPT
195 static inline void __lock_kernel(void)
198 if (unlikely(!_raw_spin_trylock(&kernel_flag
))) {
200 * If preemption was disabled even before this
201 * was called, there's nothing we can be polite
204 if (preempt_count() > 1) {
205 _raw_spin_lock(&kernel_flag
);
210 * Otherwise, let's wait for the kernel lock
211 * with preemption enabled..
215 while (spin_is_locked(&kernel_flag
))
218 } while (!_raw_spin_trylock(&kernel_flag
));
225 * Non-preemption case - just get the spinlock
227 static inline void __lock_kernel(void)
229 _raw_spin_lock(&kernel_flag
);
233 static inline void __unlock_kernel(void)
235 _raw_spin_unlock(&kernel_flag
);
240 * Getting the big kernel lock.
242 * This cannot happen asynchronously, so we only need to
243 * worry about other CPU's.
245 void __lockfunc
lock_kernel(void)
247 int depth
= current
->lock_depth
+1;
250 current
->lock_depth
= depth
;
253 void __lockfunc
unlock_kernel(void)
255 BUG_ON(current
->lock_depth
< 0);
256 if (likely(--current
->lock_depth
< 0))
262 EXPORT_SYMBOL(lock_kernel
);
263 EXPORT_SYMBOL(unlock_kernel
);