kernel_lock.c 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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 semaphore'
  14. *
  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.
  19. *
  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.
  23. *
  24. * Don't use in new code.
  25. */
  26. static DECLARE_MUTEX(kernel_sem);
  27. /*
  28. * Re-acquire the kernel semaphore.
  29. *
  30. * This function is called with preemption off.
  31. *
  32. * We are executing in schedule() so the code must be extremely careful
  33. * about recursion, both due to the down() and due to the enabling of
  34. * preemption. schedule() will re-check the preemption flag after
  35. * reacquiring the semaphore.
  36. */
  37. int __lockfunc __reacquire_kernel_lock(void)
  38. {
  39. struct task_struct *task = current;
  40. int saved_lock_depth = task->lock_depth;
  41. BUG_ON(saved_lock_depth < 0);
  42. task->lock_depth = -1;
  43. preempt_enable_no_resched();
  44. down(&kernel_sem);
  45. preempt_disable();
  46. task->lock_depth = saved_lock_depth;
  47. return 0;
  48. }
  49. void __lockfunc __release_kernel_lock(void)
  50. {
  51. up(&kernel_sem);
  52. }
  53. /*
  54. * Getting the big kernel semaphore.
  55. */
  56. void __lockfunc lock_kernel(void)
  57. {
  58. struct task_struct *task = current;
  59. int depth = task->lock_depth + 1;
  60. if (likely(!depth))
  61. /*
  62. * No recursion worries - we set up lock_depth _after_
  63. */
  64. down(&kernel_sem);
  65. task->lock_depth = depth;
  66. }
  67. void __lockfunc unlock_kernel(void)
  68. {
  69. struct task_struct *task = current;
  70. BUG_ON(task->lock_depth < 0);
  71. if (likely(--task->lock_depth < 0))
  72. up(&kernel_sem);
  73. }
  74. EXPORT_SYMBOL(lock_kernel);
  75. EXPORT_SYMBOL(unlock_kernel);