kernel_lock.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. /*
  2. * lib/kernel_lock.c
  3. *
  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>
  11. #include <linux/semaphore.h>
  12. /*
  13. * The 'big kernel lock'
  14. *
  15. * This spinlock 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.
  19. *
  20. * Don't use in new code.
  21. */
  22. static __cacheline_aligned_in_smp DEFINE_SPINLOCK(kernel_flag);
  23. /*
  24. * Acquire/release the underlying lock from the scheduler.
  25. *
  26. * This is called with preemption disabled, and should
  27. * return an error value if it cannot get the lock and
  28. * TIF_NEED_RESCHED gets set.
  29. *
  30. * If it successfully gets the lock, it should increment
  31. * the preemption count like any spinlock does.
  32. *
  33. * (This works on UP too - _raw_spin_trylock will never
  34. * return false in that case)
  35. */
  36. int __lockfunc __reacquire_kernel_lock(void)
  37. {
  38. while (!_raw_spin_trylock(&kernel_flag)) {
  39. if (need_resched())
  40. return -EAGAIN;
  41. cpu_relax();
  42. }
  43. preempt_disable();
  44. return 0;
  45. }
  46. void __lockfunc __release_kernel_lock(void)
  47. {
  48. _raw_spin_unlock(&kernel_flag);
  49. preempt_enable_no_resched();
  50. }
  51. /*
  52. * These are the BKL spinlocks - we try to be polite about preemption.
  53. * If SMP is not on (ie UP preemption), this all goes away because the
  54. * _raw_spin_trylock() will always succeed.
  55. */
  56. #ifdef CONFIG_PREEMPT
  57. static inline void __lock_kernel(void)
  58. {
  59. preempt_disable();
  60. if (unlikely(!_raw_spin_trylock(&kernel_flag))) {
  61. /*
  62. * If preemption was disabled even before this
  63. * was called, there's nothing we can be polite
  64. * about - just spin.
  65. */
  66. if (preempt_count() > 1) {
  67. _raw_spin_lock(&kernel_flag);
  68. return;
  69. }
  70. /*
  71. * Otherwise, let's wait for the kernel lock
  72. * with preemption enabled..
  73. */
  74. do {
  75. preempt_enable();
  76. while (spin_is_locked(&kernel_flag))
  77. cpu_relax();
  78. preempt_disable();
  79. } while (!_raw_spin_trylock(&kernel_flag));
  80. }
  81. }
  82. #else
  83. /*
  84. * Non-preemption case - just get the spinlock
  85. */
  86. static inline void __lock_kernel(void)
  87. {
  88. _raw_spin_lock(&kernel_flag);
  89. }
  90. #endif
  91. static inline void __unlock_kernel(void)
  92. {
  93. /*
  94. * the BKL is not covered by lockdep, so we open-code the
  95. * unlocking sequence (and thus avoid the dep-chain ops):
  96. */
  97. _raw_spin_unlock(&kernel_flag);
  98. preempt_enable();
  99. }
  100. /*
  101. * Getting the big kernel lock.
  102. *
  103. * This cannot happen asynchronously, so we only need to
  104. * worry about other CPU's.
  105. */
  106. void __lockfunc lock_kernel(void)
  107. {
  108. int depth = current->lock_depth+1;
  109. if (likely(!depth))
  110. __lock_kernel();
  111. current->lock_depth = depth;
  112. }
  113. void __lockfunc unlock_kernel(void)
  114. {
  115. BUG_ON(current->lock_depth < 0);
  116. if (likely(--current->lock_depth < 0))
  117. __unlock_kernel();
  118. }
  119. EXPORT_SYMBOL(lock_kernel);
  120. EXPORT_SYMBOL(unlock_kernel);