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