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