Blame view

Documentation/spinlocks.txt 6.49 KB
fb0bbb92d   William Allen Simpson   Documentation: rw...
1
  Lesson 1: Spin locks
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2

fb0bbb92d   William Allen Simpson   Documentation: rw...
3
  The most basic primitive for locking is spinlock.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4

fb0bbb92d   William Allen Simpson   Documentation: rw...
5
  static DEFINE_SPINLOCK(xxx_lock);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
6
7
8
9
10
11
  
  	unsigned long flags;
  
  	spin_lock_irqsave(&xxx_lock, flags);
  	... critical section here ..
  	spin_unlock_irqrestore(&xxx_lock, flags);
fb0bbb92d   William Allen Simpson   Documentation: rw...
12
  The above is always safe. It will disable interrupts _locally_, but the
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
13
14
  spinlock itself will guarantee the global lock, so it will guarantee that
  there is only one thread-of-control within the region(s) protected by that
058018178   Muthu Kumar   Documentation/spi...
15
16
  lock. This works well even under UP also, so the code does _not_ need to
  worry about UP vs SMP issues: the spinlocks work correctly under both.
fb0bbb92d   William Allen Simpson   Documentation: rw...
17
18
  
     NOTE! Implications of spin_locks for memory are further described in:
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
19

fb0bbb92d   William Allen Simpson   Documentation: rw...
20
21
22
       Documentation/memory-barriers.txt
         (5) LOCK operations.
         (6) UNLOCK operations.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
23
24
25
26
27
  
  The above is usually pretty simple (you usually need and want only one
  spinlock for most things - using more than one spinlock can make things a
  lot more complex and even slower and is usually worth it only for
  sequences that you _know_ need to be split up: avoid it at all cost if you
058018178   Muthu Kumar   Documentation/spi...
28
  aren't sure).
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
29
30
31
32
33
  
  This is really the only really hard part about spinlocks: once you start
  using spinlocks they tend to expand to areas you might not have noticed
  before, because you have to make sure the spinlocks correctly protect the
  shared data structures _everywhere_ they are used. The spinlocks are most
fb0bbb92d   William Allen Simpson   Documentation: rw...
34
35
36
37
38
39
40
  easily added to places that are completely independent of other code (for
  example, internal driver data structures that nobody else ever touches).
  
     NOTE! The spin-lock is safe only when you _also_ use the lock itself
     to do locking across CPU's, which implies that EVERYTHING that
     touches a shared variable has to agree about the spinlock they want
     to use.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
41
42
43
44
45
46
47
  
  ----
  
  Lesson 2: reader-writer spinlocks.
  
  If your data accesses have a very natural pattern where you usually tend
  to mostly read from the shared variables, the reader-writer locks
fb0bbb92d   William Allen Simpson   Documentation: rw...
48
  (rw_lock) versions of the spinlocks are sometimes useful. They allow multiple
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
49
  readers to be in the same critical region at once, but if somebody wants
fb0bbb92d   William Allen Simpson   Documentation: rw...
50
  to change the variables it has to get an exclusive write lock.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
51

fb0bbb92d   William Allen Simpson   Documentation: rw...
52
53
54
     NOTE! reader-writer locks require more atomic memory operations than
     simple spinlocks.  Unless the reader critical section is long, you
     are better off just using spinlocks.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
55

fb0bbb92d   William Allen Simpson   Documentation: rw...
56
  The routines look the same as above:
d04fa5a3b   Thomas Gleixner   locking: Remove d...
57
     rwlock_t xxx_lock = __RW_LOCK_UNLOCKED(xxx_lock);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
58
59
60
61
62
63
64
65
66
67
  
  	unsigned long flags;
  
  	read_lock_irqsave(&xxx_lock, flags);
  	.. critical section that only reads the info ...
  	read_unlock_irqrestore(&xxx_lock, flags);
  
  	write_lock_irqsave(&xxx_lock, flags);
  	.. read and write exclusive access to the info ...
  	write_unlock_irqrestore(&xxx_lock, flags);
fb0bbb92d   William Allen Simpson   Documentation: rw...
68
69
70
71
72
73
74
  The above kind of lock may be useful for complex data structures like
  linked lists, especially searching for entries without changing the list
  itself.  The read lock allows many concurrent readers.  Anything that
  _changes_ the list will have to get the write lock.
  
     NOTE! RCU is better for list traversal, but requires careful
     attention to design detail (see Documentation/RCU/listRCU.txt).
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
75

fb0bbb92d   William Allen Simpson   Documentation: rw...
76
  Also, you cannot "upgrade" a read-lock to a write-lock, so if you at _any_
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
77
  time need to do any changes (even if you don't do it every time), you have
fb0bbb92d   William Allen Simpson   Documentation: rw...
78
79
80
81
82
  to get the write-lock at the very beginning.
  
     NOTE! We are working hard to remove reader-writer spinlocks in most
     cases, so please don't add a new one without consensus.  (Instead, see
     Documentation/RCU/rcu.txt for complete information.)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
83
84
85
86
87
88
89
  
  ----
  
  Lesson 3: spinlocks revisited.
  
  The single spin-lock primitives above are by no means the only ones. They
  are the most safe ones, and the ones that work under all circumstances,
058018178   Muthu Kumar   Documentation/spi...
90
91
92
93
  but partly _because_ they are safe they are also fairly slow. They are slower
  than they'd need to be, because they do have to disable interrupts
  (which is just a single instruction on a x86, but it's an expensive one -
  and on other architectures it can be worse).
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
  
  If you have a case where you have to protect a data structure across
  several CPU's and you want to use spinlocks you can potentially use
  cheaper versions of the spinlocks. IFF you know that the spinlocks are
  never used in interrupt handlers, you can use the non-irq versions:
  
  	spin_lock(&lock);
  	...
  	spin_unlock(&lock);
  
  (and the equivalent read-write versions too, of course). The spinlock will
  guarantee the same kind of exclusive access, and it will be much faster. 
  This is useful if you know that the data in question is only ever
  manipulated from a "process context", ie no interrupts involved. 
  
  The reasons you mustn't use these versions if you have interrupts that
  play with the spinlock is that you can get deadlocks:
  
  	spin_lock(&lock);
  	...
  		<- interrupt comes in:
  			spin_lock(&lock);
  
  where an interrupt tries to lock an already locked variable. This is ok if
  the other interrupt happens on another CPU, but it is _not_ ok if the
  interrupt happens on the same CPU that already holds the lock, because the
  lock will obviously never be released (because the interrupt is waiting
  for the lock, and the lock-holder is interrupted by the interrupt and will
  not continue until the interrupt has been processed). 
  
  (This is also the reason why the irq-versions of the spinlocks only need
  to disable the _local_ interrupts - it's ok to use spinlocks in interrupts
  on other CPU's, because an interrupt on another CPU doesn't interrupt the
  CPU that holds the lock, so the lock-holder can continue and eventually
  releases the lock). 
  
  Note that you can be clever with read-write locks and interrupts. For
  example, if you know that the interrupt only ever gets a read-lock, then
  you can use a non-irq version of read locks everywhere - because they
  don't block on each other (and thus there is no dead-lock wrt interrupts. 
  But when you do the write-lock, you have to use the irq-safe version. 
  
  For an example of being clever with rw-locks, see the "waitqueue_lock" 
  handling in kernel/sched.c - nothing ever _changes_ a wait-queue from
  within an interrupt, they only read the queue in order to know whom to
  wake up. So read-locks are safe (which is good: they are very common
  indeed), while write-locks need to protect themselves against interrupts.
  
  		Linus
fb0bbb92d   William Allen Simpson   Documentation: rw...
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
  ----
  
  Reference information:
  
  For dynamic initialization, use spin_lock_init() or rwlock_init() as
  appropriate:
  
     spinlock_t xxx_lock;
     rwlock_t xxx_rw_lock;
  
     static int __init xxx_init(void)
     {
  	spin_lock_init(&xxx_lock);
  	rwlock_init(&xxx_rw_lock);
  	...
     }
  
     module_init(xxx_init);
  
  For static initialization, use DEFINE_SPINLOCK() / DEFINE_RWLOCK() or
  __SPIN_LOCK_UNLOCKED() / __RW_LOCK_UNLOCKED() as appropriate.