spinlocks.txt 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. Lesson 1: Spin locks
  2. The most basic primitive for locking is spinlock.
  3. static DEFINE_SPINLOCK(xxx_lock);
  4. unsigned long flags;
  5. spin_lock_irqsave(&xxx_lock, flags);
  6. ... critical section here ..
  7. spin_unlock_irqrestore(&xxx_lock, flags);
  8. The above is always safe. It will disable interrupts _locally_, but the
  9. spinlock itself will guarantee the global lock, so it will guarantee that
  10. there is only one thread-of-control within the region(s) protected by that
  11. lock. This works well even under UP also, so the code does _not_ need to
  12. worry about UP vs SMP issues: the spinlocks work correctly under both.
  13. NOTE! Implications of spin_locks for memory are further described in:
  14. Documentation/memory-barriers.txt
  15. (5) LOCK operations.
  16. (6) UNLOCK operations.
  17. The above is usually pretty simple (you usually need and want only one
  18. spinlock for most things - using more than one spinlock can make things a
  19. lot more complex and even slower and is usually worth it only for
  20. sequences that you _know_ need to be split up: avoid it at all cost if you
  21. aren't sure).
  22. This is really the only really hard part about spinlocks: once you start
  23. using spinlocks they tend to expand to areas you might not have noticed
  24. before, because you have to make sure the spinlocks correctly protect the
  25. shared data structures _everywhere_ they are used. The spinlocks are most
  26. easily added to places that are completely independent of other code (for
  27. example, internal driver data structures that nobody else ever touches).
  28. NOTE! The spin-lock is safe only when you _also_ use the lock itself
  29. to do locking across CPU's, which implies that EVERYTHING that
  30. touches a shared variable has to agree about the spinlock they want
  31. to use.
  32. ----
  33. Lesson 2: reader-writer spinlocks.
  34. If your data accesses have a very natural pattern where you usually tend
  35. to mostly read from the shared variables, the reader-writer locks
  36. (rw_lock) versions of the spinlocks are sometimes useful. They allow multiple
  37. readers to be in the same critical region at once, but if somebody wants
  38. to change the variables it has to get an exclusive write lock.
  39. NOTE! reader-writer locks require more atomic memory operations than
  40. simple spinlocks. Unless the reader critical section is long, you
  41. are better off just using spinlocks.
  42. The routines look the same as above:
  43. rwlock_t xxx_lock = __RW_LOCK_UNLOCKED(xxx_lock);
  44. unsigned long flags;
  45. read_lock_irqsave(&xxx_lock, flags);
  46. .. critical section that only reads the info ...
  47. read_unlock_irqrestore(&xxx_lock, flags);
  48. write_lock_irqsave(&xxx_lock, flags);
  49. .. read and write exclusive access to the info ...
  50. write_unlock_irqrestore(&xxx_lock, flags);
  51. The above kind of lock may be useful for complex data structures like
  52. linked lists, especially searching for entries without changing the list
  53. itself. The read lock allows many concurrent readers. Anything that
  54. _changes_ the list will have to get the write lock.
  55. NOTE! RCU is better for list traversal, but requires careful
  56. attention to design detail (see Documentation/RCU/listRCU.txt).
  57. Also, you cannot "upgrade" a read-lock to a write-lock, so if you at _any_
  58. time need to do any changes (even if you don't do it every time), you have
  59. to get the write-lock at the very beginning.
  60. NOTE! We are working hard to remove reader-writer spinlocks in most
  61. cases, so please don't add a new one without consensus. (Instead, see
  62. Documentation/RCU/rcu.txt for complete information.)
  63. ----
  64. Lesson 3: spinlocks revisited.
  65. The single spin-lock primitives above are by no means the only ones. They
  66. are the most safe ones, and the ones that work under all circumstances,
  67. but partly _because_ they are safe they are also fairly slow. They are slower
  68. than they'd need to be, because they do have to disable interrupts
  69. (which is just a single instruction on a x86, but it's an expensive one -
  70. and on other architectures it can be worse).
  71. If you have a case where you have to protect a data structure across
  72. several CPU's and you want to use spinlocks you can potentially use
  73. cheaper versions of the spinlocks. IFF you know that the spinlocks are
  74. never used in interrupt handlers, you can use the non-irq versions:
  75. spin_lock(&lock);
  76. ...
  77. spin_unlock(&lock);
  78. (and the equivalent read-write versions too, of course). The spinlock will
  79. guarantee the same kind of exclusive access, and it will be much faster.
  80. This is useful if you know that the data in question is only ever
  81. manipulated from a "process context", ie no interrupts involved.
  82. The reasons you mustn't use these versions if you have interrupts that
  83. play with the spinlock is that you can get deadlocks:
  84. spin_lock(&lock);
  85. ...
  86. <- interrupt comes in:
  87. spin_lock(&lock);
  88. where an interrupt tries to lock an already locked variable. This is ok if
  89. the other interrupt happens on another CPU, but it is _not_ ok if the
  90. interrupt happens on the same CPU that already holds the lock, because the
  91. lock will obviously never be released (because the interrupt is waiting
  92. for the lock, and the lock-holder is interrupted by the interrupt and will
  93. not continue until the interrupt has been processed).
  94. (This is also the reason why the irq-versions of the spinlocks only need
  95. to disable the _local_ interrupts - it's ok to use spinlocks in interrupts
  96. on other CPU's, because an interrupt on another CPU doesn't interrupt the
  97. CPU that holds the lock, so the lock-holder can continue and eventually
  98. releases the lock).
  99. Note that you can be clever with read-write locks and interrupts. For
  100. example, if you know that the interrupt only ever gets a read-lock, then
  101. you can use a non-irq version of read locks everywhere - because they
  102. don't block on each other (and thus there is no dead-lock wrt interrupts.
  103. But when you do the write-lock, you have to use the irq-safe version.
  104. For an example of being clever with rw-locks, see the "waitqueue_lock"
  105. handling in kernel/sched/core.c - nothing ever _changes_ a wait-queue from
  106. within an interrupt, they only read the queue in order to know whom to
  107. wake up. So read-locks are safe (which is good: they are very common
  108. indeed), while write-locks need to protect themselves against interrupts.
  109. Linus
  110. ----
  111. Reference information:
  112. For dynamic initialization, use spin_lock_init() or rwlock_init() as
  113. appropriate:
  114. spinlock_t xxx_lock;
  115. rwlock_t xxx_rw_lock;
  116. static int __init xxx_init(void)
  117. {
  118. spin_lock_init(&xxx_lock);
  119. rwlock_init(&xxx_rw_lock);
  120. ...
  121. }
  122. module_init(xxx_init);
  123. For static initialization, use DEFINE_SPINLOCK() / DEFINE_RWLOCK() or
  124. __SPIN_LOCK_UNLOCKED() / __RW_LOCK_UNLOCKED() as appropriate.