spinlocks.txt 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. UPDATE March 21 2005 Amit Gud <gud@eth.net>
  2. Macros SPIN_LOCK_UNLOCKED and RW_LOCK_UNLOCKED are deprecated and will be
  3. removed soon. So for any new code dynamic initialization should be used:
  4. spinlock_t xxx_lock;
  5. rwlock_t xxx_rw_lock;
  6. static int __init xxx_init(void)
  7. {
  8. spin_lock_init(&xxx_lock);
  9. rwlock_init(&xxx_rw_lock);
  10. ...
  11. }
  12. module_init(xxx_init);
  13. Reasons for deprecation
  14. - it hurts automatic lock validators
  15. - it becomes intrusive for the realtime preemption patches
  16. Following discussion is still valid, however, with the dynamic initialization
  17. of spinlocks instead of static.
  18. -----------------------
  19. On Fri, 2 Jan 1998, Doug Ledford wrote:
  20. >
  21. > I'm working on making the aic7xxx driver more SMP friendly (as well as
  22. > importing the latest FreeBSD sequencer code to have 7895 support) and wanted
  23. > to get some info from you. The goal here is to make the various routines
  24. > SMP safe as well as UP safe during interrupts and other manipulating
  25. > routines. So far, I've added a spin_lock variable to things like my queue
  26. > structs. Now, from what I recall, there are some spin lock functions I can
  27. > use to lock these spin locks from other use as opposed to a (nasty)
  28. > save_flags(); cli(); stuff; restore_flags(); construct. Where do I find
  29. > these routines and go about making use of them? Do they only lock on a
  30. > per-processor basis or can they also lock say an interrupt routine from
  31. > mucking with a queue if the queue routine was manipulating it when the
  32. > interrupt occurred, or should I still use a cli(); based construct on that
  33. > one?
  34. See <asm/spinlock.h>. The basic version is:
  35. spinlock_t xxx_lock = SPIN_LOCK_UNLOCKED;
  36. unsigned long flags;
  37. spin_lock_irqsave(&xxx_lock, flags);
  38. ... critical section here ..
  39. spin_unlock_irqrestore(&xxx_lock, flags);
  40. and the above is always safe. It will disable interrupts _locally_, but the
  41. spinlock itself will guarantee the global lock, so it will guarantee that
  42. there is only one thread-of-control within the region(s) protected by that
  43. lock.
  44. Note that it works well even under UP - the above sequence under UP
  45. essentially is just the same as doing a
  46. unsigned long flags;
  47. save_flags(flags); cli();
  48. ... critical section ...
  49. restore_flags(flags);
  50. so the code does _not_ need to worry about UP vs SMP issues: the spinlocks
  51. work correctly under both (and spinlocks are actually more efficient on
  52. architectures that allow doing the "save_flags + cli" in one go because I
  53. don't export that interface normally).
  54. NOTE NOTE NOTE! The reason the spinlock is so much faster than a global
  55. interrupt lock under SMP is exactly because it disables interrupts only on
  56. the local CPU. The spin-lock is safe only when you _also_ use the lock
  57. itself to do locking across CPU's, which implies that EVERYTHING that
  58. touches a shared variable has to agree about the spinlock they want to
  59. use.
  60. The above is usually pretty simple (you usually need and want only one
  61. spinlock for most things - using more than one spinlock can make things a
  62. lot more complex and even slower and is usually worth it only for
  63. sequences that you _know_ need to be split up: avoid it at all cost if you
  64. aren't sure). HOWEVER, it _does_ mean that if you have some code that does
  65. cli();
  66. .. critical section ..
  67. sti();
  68. and another sequence that does
  69. spin_lock_irqsave(flags);
  70. .. critical section ..
  71. spin_unlock_irqrestore(flags);
  72. then they are NOT mutually exclusive, and the critical regions can happen
  73. at the same time on two different CPU's. That's fine per se, but the
  74. critical regions had better be critical for different things (ie they
  75. can't stomp on each other).
  76. The above is a problem mainly if you end up mixing code - for example the
  77. routines in ll_rw_block() tend to use cli/sti to protect the atomicity of
  78. their actions, and if a driver uses spinlocks instead then you should
  79. think about issues like the above..
  80. This is really the only really hard part about spinlocks: once you start
  81. using spinlocks they tend to expand to areas you might not have noticed
  82. before, because you have to make sure the spinlocks correctly protect the
  83. shared data structures _everywhere_ they are used. The spinlocks are most
  84. easily added to places that are completely independent of other code (ie
  85. internal driver data structures that nobody else ever touches, for
  86. example).
  87. ----
  88. Lesson 2: reader-writer spinlocks.
  89. If your data accesses have a very natural pattern where you usually tend
  90. to mostly read from the shared variables, the reader-writer locks
  91. (rw_lock) versions of the spinlocks are often nicer. They allow multiple
  92. readers to be in the same critical region at once, but if somebody wants
  93. to change the variables it has to get an exclusive write lock. The
  94. routines look the same as above:
  95. rwlock_t xxx_lock = RW_LOCK_UNLOCKED;
  96. unsigned long flags;
  97. read_lock_irqsave(&xxx_lock, flags);
  98. .. critical section that only reads the info ...
  99. read_unlock_irqrestore(&xxx_lock, flags);
  100. write_lock_irqsave(&xxx_lock, flags);
  101. .. read and write exclusive access to the info ...
  102. write_unlock_irqrestore(&xxx_lock, flags);
  103. The above kind of lock is useful for complex data structures like linked
  104. lists etc, especially when you know that most of the work is to just
  105. traverse the list searching for entries without changing the list itself,
  106. for example. Then you can use the read lock for that kind of list
  107. traversal, which allows many concurrent readers. Anything that _changes_
  108. the list will have to get the write lock.
  109. Note: you cannot "upgrade" a read-lock to a write-lock, so if you at _any_
  110. time need to do any changes (even if you don't do it every time), you have
  111. to get the write-lock at the very beginning. I could fairly easily add a
  112. primitive to create a "upgradeable" read-lock, but it hasn't been an issue
  113. yet. Tell me if you'd want one.
  114. ----
  115. Lesson 3: spinlocks revisited.
  116. The single spin-lock primitives above are by no means the only ones. They
  117. are the most safe ones, and the ones that work under all circumstances,
  118. but partly _because_ they are safe they are also fairly slow. They are
  119. much faster than a generic global cli/sti pair, but slower than they'd
  120. need to be, because they do have to disable interrupts (which is just a
  121. single instruction on a x86, but it's an expensive one - and on other
  122. architectures it can be worse).
  123. If you have a case where you have to protect a data structure across
  124. several CPU's and you want to use spinlocks you can potentially use
  125. cheaper versions of the spinlocks. IFF you know that the spinlocks are
  126. never used in interrupt handlers, you can use the non-irq versions:
  127. spin_lock(&lock);
  128. ...
  129. spin_unlock(&lock);
  130. (and the equivalent read-write versions too, of course). The spinlock will
  131. guarantee the same kind of exclusive access, and it will be much faster.
  132. This is useful if you know that the data in question is only ever
  133. manipulated from a "process context", ie no interrupts involved.
  134. The reasons you mustn't use these versions if you have interrupts that
  135. play with the spinlock is that you can get deadlocks:
  136. spin_lock(&lock);
  137. ...
  138. <- interrupt comes in:
  139. spin_lock(&lock);
  140. where an interrupt tries to lock an already locked variable. This is ok if
  141. the other interrupt happens on another CPU, but it is _not_ ok if the
  142. interrupt happens on the same CPU that already holds the lock, because the
  143. lock will obviously never be released (because the interrupt is waiting
  144. for the lock, and the lock-holder is interrupted by the interrupt and will
  145. not continue until the interrupt has been processed).
  146. (This is also the reason why the irq-versions of the spinlocks only need
  147. to disable the _local_ interrupts - it's ok to use spinlocks in interrupts
  148. on other CPU's, because an interrupt on another CPU doesn't interrupt the
  149. CPU that holds the lock, so the lock-holder can continue and eventually
  150. releases the lock).
  151. Note that you can be clever with read-write locks and interrupts. For
  152. example, if you know that the interrupt only ever gets a read-lock, then
  153. you can use a non-irq version of read locks everywhere - because they
  154. don't block on each other (and thus there is no dead-lock wrt interrupts.
  155. But when you do the write-lock, you have to use the irq-safe version.
  156. For an example of being clever with rw-locks, see the "waitqueue_lock"
  157. handling in kernel/sched.c - nothing ever _changes_ a wait-queue from
  158. within an interrupt, they only read the queue in order to know whom to
  159. wake up. So read-locks are safe (which is good: they are very common
  160. indeed), while write-locks need to protect themselves against interrupts.
  161. Linus