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