1 /* SPDX-License-Identifier: GPL-2.0 */
7 * Shared/intent/exclusive locks: sleepable read/write locks, much like rw
8 * semaphores, except with a third intermediate state, intent. Basic operations
11 * six_lock_read(&foo->lock);
12 * six_unlock_read(&foo->lock);
14 * six_lock_intent(&foo->lock);
15 * six_unlock_intent(&foo->lock);
17 * six_lock_write(&foo->lock);
18 * six_unlock_write(&foo->lock);
20 * Intent locks block other intent locks, but do not block read locks, and you
21 * must have an intent lock held before taking a write lock, like so:
23 * six_lock_intent(&foo->lock);
24 * six_lock_write(&foo->lock);
25 * six_unlock_write(&foo->lock);
26 * six_unlock_intent(&foo->lock);
31 * six_trylock_intent()
34 * six_lock_downgrade(): convert from intent to read
35 * six_lock_tryupgrade(): attempt to convert from read to intent
37 * Locks also embed a sequence number, which is incremented when the lock is
38 * locked or unlocked for write. The current sequence number can be grabbed
39 * while a lock is held from lock->state.seq; then, if you drop the lock you can
40 * use six_relock_(read|intent_write)(lock, seq) to attempt to retake the lock
41 * iff it hasn't been locked for write in the meantime.
43 * There are also operations that take the lock type as a parameter, where the
44 * type is one of SIX_LOCK_read, SIX_LOCK_intent, or SIX_LOCK_write:
46 * six_lock_type(lock, type)
47 * six_unlock_type(lock, type)
48 * six_relock(lock, type, seq)
49 * six_trylock_type(lock, type)
50 * six_trylock_convert(lock, from, to)
52 * A lock may be held multiple types by the same thread (for read or intent,
53 * not write). However, the six locks code does _not_ implement the actual
54 * recursive checks itself though - rather, if your code (e.g. btree iterator
55 * code) knows that the current thread already has a lock held, and for the
56 * correct type, six_lock_increment() may be used to bump up the counter for
57 * that type - the only effect is that one more call to unlock will be required
58 * before the lock is unlocked.
61 #include <linux/lockdep.h>
62 #include <linux/osq_lock.h>
63 #include <linux/sched.h>
64 #include <linux/types.h>
66 #define SIX_LOCK_SEPARATE_LOCKFNS
68 union six_lock_state {
78 /* for waitlist_bitnr() */
83 unsigned read_lock:27;
84 unsigned write_locking:1;
85 unsigned intent_lock:1;
88 * seq works much like in seqlocks: it's incremented every time
89 * we lock and unlock for write.
91 * If it's odd write lock is held, even unlocked.
93 * Thus readers can unlock, and then lock again later iff it
94 * hasn't been modified in the meantime.
107 union six_lock_state state;
108 unsigned intent_lock_recurse;
109 struct task_struct *owner;
110 struct optimistic_spin_queue osq;
111 unsigned __percpu *readers;
113 raw_spinlock_t wait_lock;
114 struct list_head wait_list[2];
115 #ifdef CONFIG_DEBUG_LOCK_ALLOC
116 struct lockdep_map dep_map;
120 typedef int (*six_lock_should_sleep_fn)(struct six_lock *lock, void *);
122 static __always_inline void __six_lock_init(struct six_lock *lock,
124 struct lock_class_key *key)
126 atomic64_set(&lock->state.counter, 0);
127 raw_spin_lock_init(&lock->wait_lock);
128 INIT_LIST_HEAD(&lock->wait_list[SIX_LOCK_read]);
129 INIT_LIST_HEAD(&lock->wait_list[SIX_LOCK_intent]);
130 #ifdef CONFIG_DEBUG_LOCK_ALLOC
131 debug_check_no_locks_freed((void *) lock, sizeof(*lock));
132 lockdep_init_map(&lock->dep_map, name, key, 0);
136 #define six_lock_init(lock) \
138 static struct lock_class_key __key; \
140 __six_lock_init((lock), #lock, &__key); \
143 #define __SIX_VAL(field, _v) (((union six_lock_state) { .field = _v }).v)
145 #define __SIX_LOCK(type) \
146 bool six_trylock_##type(struct six_lock *); \
147 bool six_relock_##type(struct six_lock *, u32); \
148 int six_lock_##type(struct six_lock *, six_lock_should_sleep_fn, void *);\
149 void six_unlock_##type(struct six_lock *);
156 #define SIX_LOCK_DISPATCH(type, fn, ...) \
158 case SIX_LOCK_read: \
159 return fn##_read(__VA_ARGS__); \
160 case SIX_LOCK_intent: \
161 return fn##_intent(__VA_ARGS__); \
162 case SIX_LOCK_write: \
163 return fn##_write(__VA_ARGS__); \
168 static inline bool six_trylock_type(struct six_lock *lock, enum six_lock_type type)
170 SIX_LOCK_DISPATCH(type, six_trylock, lock);
173 static inline bool six_relock_type(struct six_lock *lock, enum six_lock_type type,
176 SIX_LOCK_DISPATCH(type, six_relock, lock, seq);
179 static inline int six_lock_type(struct six_lock *lock, enum six_lock_type type,
180 six_lock_should_sleep_fn should_sleep_fn, void *p)
182 SIX_LOCK_DISPATCH(type, six_lock, lock, should_sleep_fn, p);
185 static inline void six_unlock_type(struct six_lock *lock, enum six_lock_type type)
187 SIX_LOCK_DISPATCH(type, six_unlock, lock);
190 void six_lock_downgrade(struct six_lock *);
191 bool six_lock_tryupgrade(struct six_lock *);
192 bool six_trylock_convert(struct six_lock *, enum six_lock_type,
195 void six_lock_increment(struct six_lock *, enum six_lock_type);
197 void six_lock_wakeup_all(struct six_lock *);
199 void six_lock_pcpu_free_rcu(struct six_lock *);
200 void six_lock_pcpu_free(struct six_lock *);
201 void six_lock_pcpu_alloc(struct six_lock *);
203 struct six_lock_count {
208 struct six_lock_count six_lock_counts(struct six_lock *);
210 #endif /* _LINUX_SIX_H */