0001 ===============
0002 Locking lessons
0003 ===============
0004
0005 Lesson 1: Spin locks
0006 ====================
0007
0008 The most basic primitive for locking is spinlock::
0009
0010 static DEFINE_SPINLOCK(xxx_lock);
0011
0012 unsigned long flags;
0013
0014 spin_lock_irqsave(&xxx_lock, flags);
0015 ... critical section here ..
0016 spin_unlock_irqrestore(&xxx_lock, flags);
0017
0018 The above is always safe. It will disable interrupts _locally_, but the
0019 spinlock itself will guarantee the global lock, so it will guarantee that
0020 there is only one thread-of-control within the region(s) protected by that
0021 lock. This works well even under UP also, so the code does _not_ need to
0022 worry about UP vs SMP issues: the spinlocks work correctly under both.
0023
0024 NOTE! Implications of spin_locks for memory are further described in:
0025
0026 Documentation/memory-barriers.txt
0027
0028 (5) ACQUIRE operations.
0029
0030 (6) RELEASE operations.
0031
0032 The above is usually pretty simple (you usually need and want only one
0033 spinlock for most things - using more than one spinlock can make things a
0034 lot more complex and even slower and is usually worth it only for
0035 sequences that you **know** need to be split up: avoid it at all cost if you
0036 aren't sure).
0037
0038 This is really the only really hard part about spinlocks: once you start
0039 using spinlocks they tend to expand to areas you might not have noticed
0040 before, because you have to make sure the spinlocks correctly protect the
0041 shared data structures **everywhere** they are used. The spinlocks are most
0042 easily added to places that are completely independent of other code (for
0043 example, internal driver data structures that nobody else ever touches).
0044
0045 NOTE! The spin-lock is safe only when you **also** use the lock itself
0046 to do locking across CPU's, which implies that EVERYTHING that
0047 touches a shared variable has to agree about the spinlock they want
0048 to use.
0049
0050 ----
0051
0052 Lesson 2: reader-writer spinlocks.
0053 ==================================
0054
0055 If your data accesses have a very natural pattern where you usually tend
0056 to mostly read from the shared variables, the reader-writer locks
0057 (rw_lock) versions of the spinlocks are sometimes useful. They allow multiple
0058 readers to be in the same critical region at once, but if somebody wants
0059 to change the variables it has to get an exclusive write lock.
0060
0061 NOTE! reader-writer locks require more atomic memory operations than
0062 simple spinlocks. Unless the reader critical section is long, you
0063 are better off just using spinlocks.
0064
0065 The routines look the same as above::
0066
0067 rwlock_t xxx_lock = __RW_LOCK_UNLOCKED(xxx_lock);
0068
0069 unsigned long flags;
0070
0071 read_lock_irqsave(&xxx_lock, flags);
0072 .. critical section that only reads the info ...
0073 read_unlock_irqrestore(&xxx_lock, flags);
0074
0075 write_lock_irqsave(&xxx_lock, flags);
0076 .. read and write exclusive access to the info ...
0077 write_unlock_irqrestore(&xxx_lock, flags);
0078
0079 The above kind of lock may be useful for complex data structures like
0080 linked lists, especially searching for entries without changing the list
0081 itself. The read lock allows many concurrent readers. Anything that
0082 **changes** the list will have to get the write lock.
0083
0084 NOTE! RCU is better for list traversal, but requires careful
0085 attention to design detail (see Documentation/RCU/listRCU.rst).
0086
0087 Also, you cannot "upgrade" a read-lock to a write-lock, so if you at _any_
0088 time need to do any changes (even if you don't do it every time), you have
0089 to get the write-lock at the very beginning.
0090
0091 NOTE! We are working hard to remove reader-writer spinlocks in most
0092 cases, so please don't add a new one without consensus. (Instead, see
0093 Documentation/RCU/rcu.rst for complete information.)
0094
0095 ----
0096
0097 Lesson 3: spinlocks revisited.
0098 ==============================
0099
0100 The single spin-lock primitives above are by no means the only ones. They
0101 are the most safe ones, and the ones that work under all circumstances,
0102 but partly **because** they are safe they are also fairly slow. They are slower
0103 than they'd need to be, because they do have to disable interrupts
0104 (which is just a single instruction on a x86, but it's an expensive one -
0105 and on other architectures it can be worse).
0106
0107 If you have a case where you have to protect a data structure across
0108 several CPU's and you want to use spinlocks you can potentially use
0109 cheaper versions of the spinlocks. IFF you know that the spinlocks are
0110 never used in interrupt handlers, you can use the non-irq versions::
0111
0112 spin_lock(&lock);
0113 ...
0114 spin_unlock(&lock);
0115
0116 (and the equivalent read-write versions too, of course). The spinlock will
0117 guarantee the same kind of exclusive access, and it will be much faster.
0118 This is useful if you know that the data in question is only ever
0119 manipulated from a "process context", ie no interrupts involved.
0120
0121 The reasons you mustn't use these versions if you have interrupts that
0122 play with the spinlock is that you can get deadlocks::
0123
0124 spin_lock(&lock);
0125 ...
0126 <- interrupt comes in:
0127 spin_lock(&lock);
0128
0129 where an interrupt tries to lock an already locked variable. This is ok if
0130 the other interrupt happens on another CPU, but it is _not_ ok if the
0131 interrupt happens on the same CPU that already holds the lock, because the
0132 lock will obviously never be released (because the interrupt is waiting
0133 for the lock, and the lock-holder is interrupted by the interrupt and will
0134 not continue until the interrupt has been processed).
0135
0136 (This is also the reason why the irq-versions of the spinlocks only need
0137 to disable the _local_ interrupts - it's ok to use spinlocks in interrupts
0138 on other CPU's, because an interrupt on another CPU doesn't interrupt the
0139 CPU that holds the lock, so the lock-holder can continue and eventually
0140 releases the lock).
0141
0142 Linus
0143
0144 ----
0145
0146 Reference information:
0147 ======================
0148
0149 For dynamic initialization, use spin_lock_init() or rwlock_init() as
0150 appropriate::
0151
0152 spinlock_t xxx_lock;
0153 rwlock_t xxx_rw_lock;
0154
0155 static int __init xxx_init(void)
0156 {
0157 spin_lock_init(&xxx_lock);
0158 rwlock_init(&xxx_rw_lock);
0159 ...
0160 }
0161
0162 module_init(xxx_init);
0163
0164 For static initialization, use DEFINE_SPINLOCK() / DEFINE_RWLOCK() or
0165 __SPIN_LOCK_UNLOCKED() / __RW_LOCK_UNLOCKED() as appropriate.