~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/Documentation/locking/spinlocks.rst

Version: ~ [ linux-6.11.5 ] ~ [ linux-6.10.14 ] ~ [ linux-6.9.12 ] ~ [ linux-6.8.12 ] ~ [ linux-6.7.12 ] ~ [ linux-6.6.58 ] ~ [ linux-6.5.13 ] ~ [ linux-6.4.16 ] ~ [ linux-6.3.13 ] ~ [ linux-6.2.16 ] ~ [ linux-6.1.114 ] ~ [ linux-6.0.19 ] ~ [ linux-5.19.17 ] ~ [ linux-5.18.19 ] ~ [ linux-5.17.15 ] ~ [ linux-5.16.20 ] ~ [ linux-5.15.169 ] ~ [ linux-5.14.21 ] ~ [ linux-5.13.19 ] ~ [ linux-5.12.19 ] ~ [ linux-5.11.22 ] ~ [ linux-5.10.228 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.284 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.322 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.336 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.337 ] ~ [ linux-4.4.302 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.9 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

Diff markup

Differences between /Documentation/locking/spinlocks.rst (Version linux-6.11.5) and /Documentation/locking/spinlocks.rst (Version linux-4.9.337)


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

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

sflogo.php