blob: bec96f7a9f2d7a1fc28c2f2f01cd2fd4e2977a16 [file] [log] [blame]
Mauro Carvalho Chehab387b1462019-04-10 08:32:41 -03001===============
2Locking lessons
3===============
4
William Allen Simpsonfb0bbb92009-12-13 15:12:46 -05005Lesson 1: Spin locks
Mauro Carvalho Chehab387b1462019-04-10 08:32:41 -03006====================
Linus Torvalds1da177e2005-04-16 15:20:36 -07007
Mauro Carvalho Chehab387b1462019-04-10 08:32:41 -03008The most basic primitive for locking is spinlock::
Ed L. Cashin017f0212007-07-15 23:41:50 -07009
Mauro Carvalho Chehab387b1462019-04-10 08:32:41 -030010 static DEFINE_SPINLOCK(xxx_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011
12 unsigned long flags;
13
14 spin_lock_irqsave(&xxx_lock, flags);
15 ... critical section here ..
16 spin_unlock_irqrestore(&xxx_lock, flags);
17
William Allen Simpsonfb0bbb92009-12-13 15:12:46 -050018The above is always safe. It will disable interrupts _locally_, but the
Linus Torvalds1da177e2005-04-16 15:20:36 -070019spinlock 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
Muthu Kumar05801812011-07-11 11:04:58 -070021lock. 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.
Linus Torvalds1da177e2005-04-16 15:20:36 -070023
William Allen Simpsonfb0bbb92009-12-13 15:12:46 -050024 NOTE! Implications of spin_locks for memory are further described in:
25
26 Documentation/memory-barriers.txt
Mauro Carvalho Chehab387b1462019-04-10 08:32:41 -030027
SeongJae Park4bfdebd2020-01-31 21:52:33 +010028 (5) ACQUIRE operations.
Mauro Carvalho Chehab387b1462019-04-10 08:32:41 -030029
SeongJae Park4bfdebd2020-01-31 21:52:33 +010030 (6) RELEASE operations.
Linus Torvalds1da177e2005-04-16 15:20:36 -070031
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
Mauro Carvalho Chehab387b1462019-04-10 08:32:41 -030035sequences that you **know** need to be split up: avoid it at all cost if you
Muthu Kumar05801812011-07-11 11:04:58 -070036aren't sure).
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
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
Mauro Carvalho Chehab387b1462019-04-10 08:32:41 -030041shared data structures **everywhere** they are used. The spinlocks are most
William Allen Simpsonfb0bbb92009-12-13 15:12:46 -050042easily added to places that are completely independent of other code (for
43example, internal driver data structures that nobody else ever touches).
44
Mauro Carvalho Chehab387b1462019-04-10 08:32:41 -030045 NOTE! The spin-lock is safe only when you **also** use the lock itself
William Allen Simpsonfb0bbb92009-12-13 15:12:46 -050046 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.
Linus Torvalds1da177e2005-04-16 15:20:36 -070049
50----
51
52Lesson 2: reader-writer spinlocks.
Mauro Carvalho Chehab387b1462019-04-10 08:32:41 -030053==================================
Linus Torvalds1da177e2005-04-16 15:20:36 -070054
55If your data accesses have a very natural pattern where you usually tend
56to mostly read from the shared variables, the reader-writer locks
William Allen Simpsonfb0bbb92009-12-13 15:12:46 -050057(rw_lock) versions of the spinlocks are sometimes useful. They allow multiple
Linus Torvalds1da177e2005-04-16 15:20:36 -070058readers to be in the same critical region at once, but if somebody wants
William Allen Simpsonfb0bbb92009-12-13 15:12:46 -050059to change the variables it has to get an exclusive write lock.
60
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.
64
Mauro Carvalho Chehab387b1462019-04-10 08:32:41 -030065The routines look the same as above::
Linus Torvalds1da177e2005-04-16 15:20:36 -070066
Thomas Gleixnerd04fa5a2011-01-23 15:30:09 +010067 rwlock_t xxx_lock = __RW_LOCK_UNLOCKED(xxx_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070068
Linus Torvalds1da177e2005-04-16 15:20:36 -070069 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
William Allen Simpsonfb0bbb92009-12-13 15:12:46 -050079The 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
Mauro Carvalho Chehab387b1462019-04-10 08:32:41 -030082**changes** the list will have to get the write lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -070083
William Allen Simpsonfb0bbb92009-12-13 15:12:46 -050084 NOTE! RCU is better for list traversal, but requires careful
Mauro Carvalho Chehabbff9e342019-07-15 05:31:06 -030085 attention to design detail (see Documentation/RCU/listRCU.rst).
William Allen Simpsonfb0bbb92009-12-13 15:12:46 -050086
87Also, you cannot "upgrade" a read-lock to a write-lock, so if you at _any_
Linus Torvalds1da177e2005-04-16 15:20:36 -070088time need to do any changes (even if you don't do it every time), you have
William Allen Simpsonfb0bbb92009-12-13 15:12:46 -050089to 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
Mauro Carvalho Chehabbff9e342019-07-15 05:31:06 -030093 Documentation/RCU/rcu.rst for complete information.)
Linus Torvalds1da177e2005-04-16 15:20:36 -070094
95----
96
97Lesson 3: spinlocks revisited.
Mauro Carvalho Chehab387b1462019-04-10 08:32:41 -030098==============================
Linus Torvalds1da177e2005-04-16 15:20:36 -070099
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,
Mauro Carvalho Chehab387b1462019-04-10 08:32:41 -0300102but partly **because** they are safe they are also fairly slow. They are slower
Muthu Kumar05801812011-07-11 11:04:58 -0700103than 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).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106
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
Mauro Carvalho Chehab387b1462019-04-10 08:32:41 -0300110never used in interrupt handlers, you can use the non-irq versions::
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111
112 spin_lock(&lock);
113 ...
114 spin_unlock(&lock);
115
116(and the equivalent read-write versions too, of course). The spinlock will
Davidlohr Bueso214e0ae2014-07-30 13:41:55 -0700117guarantee the same kind of exclusive access, and it will be much faster.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118This is useful if you know that the data in question is only ever
Davidlohr Bueso214e0ae2014-07-30 13:41:55 -0700119manipulated from a "process context", ie no interrupts involved.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120
121The reasons you mustn't use these versions if you have interrupts that
Mauro Carvalho Chehab387b1462019-04-10 08:32:41 -0300122play with the spinlock is that you can get deadlocks::
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123
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
Davidlohr Bueso214e0ae2014-07-30 13:41:55 -0700134not continue until the interrupt has been processed).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135
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
Davidlohr Bueso214e0ae2014-07-30 13:41:55 -0700140releases the lock).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 Linus
143
William Allen Simpsonfb0bbb92009-12-13 15:12:46 -0500144----
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145
William Allen Simpsonfb0bbb92009-12-13 15:12:46 -0500146Reference information:
Mauro Carvalho Chehab387b1462019-04-10 08:32:41 -0300147======================
William Allen Simpsonfb0bbb92009-12-13 15:12:46 -0500148
149For dynamic initialization, use spin_lock_init() or rwlock_init() as
Mauro Carvalho Chehab387b1462019-04-10 08:32:41 -0300150appropriate::
William Allen Simpsonfb0bbb92009-12-13 15:12:46 -0500151
152 spinlock_t xxx_lock;
153 rwlock_t xxx_rw_lock;
154
155 static int __init xxx_init(void)
156 {
157 spin_lock_init(&xxx_lock);
158 rwlock_init(&xxx_rw_lock);
159 ...
160 }
161
162 module_init(xxx_init);
163
164For static initialization, use DEFINE_SPINLOCK() / DEFINE_RWLOCK() or
165__SPIN_LOCK_UNLOCKED() / __RW_LOCK_UNLOCKED() as appropriate.