]> git.sesse.net Git - bcachefs-tools-debian/blob - include/linux/six.h
Update bcachefs sources to 400f275d46 bcachefs: Fix check_overlapping_extents()
[bcachefs-tools-debian] / include / linux / six.h
1 /* SPDX-License-Identifier: GPL-2.0 */
2
3 #ifndef _LINUX_SIX_H
4 #define _LINUX_SIX_H
5
6 /*
7  * Shared/intent/exclusive locks: sleepable read/write locks, much like rw
8  * semaphores, except with a third intermediate state, intent. Basic operations
9  * are:
10  *
11  * six_lock_read(&foo->lock);
12  * six_unlock_read(&foo->lock);
13  *
14  * six_lock_intent(&foo->lock);
15  * six_unlock_intent(&foo->lock);
16  *
17  * six_lock_write(&foo->lock);
18  * six_unlock_write(&foo->lock);
19  *
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:
22  *
23  * six_lock_intent(&foo->lock);
24  * six_lock_write(&foo->lock);
25  * six_unlock_write(&foo->lock);
26  * six_unlock_intent(&foo->lock);
27  *
28  * Other operations:
29  *
30  *   six_trylock_read()
31  *   six_trylock_intent()
32  *   six_trylock_write()
33  *
34  *   six_lock_downgrade():      convert from intent to read
35  *   six_lock_tryupgrade():     attempt to convert from read to intent
36  *
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.
42  *
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:
45  *
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)
51  *
52  * A lock may be held multiple times 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.
59  */
60
61 #include <linux/lockdep.h>
62 #include <linux/osq_lock.h>
63 #include <linux/sched.h>
64 #include <linux/types.h>
65
66 #define SIX_LOCK_SEPARATE_LOCKFNS
67
68 union six_lock_state {
69         struct {
70                 atomic64_t      counter;
71         };
72
73         struct {
74                 u64             v;
75         };
76
77         struct {
78                 /* for waitlist_bitnr() */
79                 unsigned long   l;
80         };
81
82         struct {
83                 unsigned        read_lock:26;
84                 unsigned        write_locking:1;
85                 unsigned        intent_lock:1;
86                 unsigned        nospin:1;
87                 unsigned        waiters:3;
88                 /*
89                  * seq works much like in seqlocks: it's incremented every time
90                  * we lock and unlock for write.
91                  *
92                  * If it's odd write lock is held, even unlocked.
93                  *
94                  * Thus readers can unlock, and then lock again later iff it
95                  * hasn't been modified in the meantime.
96                  */
97                 u32             seq;
98         };
99 };
100
101 enum six_lock_type {
102         SIX_LOCK_read,
103         SIX_LOCK_intent,
104         SIX_LOCK_write,
105 };
106
107 struct six_lock {
108         union six_lock_state    state;
109         unsigned                intent_lock_recurse;
110         struct task_struct      *owner;
111         unsigned __percpu       *readers;
112         struct optimistic_spin_queue osq;
113         raw_spinlock_t          wait_lock;
114         struct list_head        wait_list;
115 #ifdef CONFIG_DEBUG_LOCK_ALLOC
116         struct lockdep_map      dep_map;
117 #endif
118 };
119
120 struct six_lock_waiter {
121         struct list_head        list;
122         struct task_struct      *task;
123         enum six_lock_type      lock_want;
124         bool                    lock_acquired;
125         u64                     start_time;
126 };
127
128 typedef int (*six_lock_should_sleep_fn)(struct six_lock *lock, void *);
129
130 static __always_inline void __six_lock_init(struct six_lock *lock,
131                                             const char *name,
132                                             struct lock_class_key *key)
133 {
134         atomic64_set(&lock->state.counter, 0);
135         raw_spin_lock_init(&lock->wait_lock);
136         INIT_LIST_HEAD(&lock->wait_list);
137 #ifdef CONFIG_DEBUG_LOCK_ALLOC
138         debug_check_no_locks_freed((void *) lock, sizeof(*lock));
139         lockdep_init_map(&lock->dep_map, name, key, 0);
140 #endif
141 }
142
143 #define six_lock_init(lock)                                             \
144 do {                                                                    \
145         static struct lock_class_key __key;                             \
146                                                                         \
147         __six_lock_init((lock), #lock, &__key);                         \
148 } while (0)
149
150 #define __SIX_VAL(field, _v)    (((union six_lock_state) { .field = _v }).v)
151
152 #define __SIX_LOCK(type)                                                \
153 bool six_trylock_ip_##type(struct six_lock *, unsigned long);           \
154 bool six_relock_ip_##type(struct six_lock *, u32, unsigned long);       \
155 int six_lock_ip_##type(struct six_lock *, six_lock_should_sleep_fn,     \
156                        void *, unsigned long);                          \
157 int six_lock_ip_waiter_##type(struct six_lock *, struct six_lock_waiter *,\
158                         six_lock_should_sleep_fn, void *, unsigned long);\
159 void six_unlock_ip_##type(struct six_lock *, unsigned long);            \
160                                                                         \
161 static inline bool six_trylock_##type(struct six_lock *lock)            \
162 {                                                                       \
163         return six_trylock_ip_##type(lock, _THIS_IP_);                  \
164 }                                                                       \
165 static inline bool six_relock_##type(struct six_lock *lock, u32 seq)    \
166 {                                                                       \
167         return six_relock_ip_##type(lock, seq, _THIS_IP_);              \
168 }                                                                       \
169 static inline int six_lock_##type(struct six_lock *lock,                \
170                                   six_lock_should_sleep_fn fn, void *p)\
171 {                                                                       \
172         return six_lock_ip_##type(lock, fn, p, _THIS_IP_);              \
173 }                                                                       \
174 static inline int six_lock_waiter_##type(struct six_lock *lock,         \
175                         struct six_lock_waiter *wait,                   \
176                         six_lock_should_sleep_fn fn, void *p)           \
177 {                                                                       \
178         return six_lock_ip_waiter_##type(lock, wait, fn, p, _THIS_IP_); \
179 }                                                                       \
180 static inline void six_unlock_##type(struct six_lock *lock)             \
181 {                                                                       \
182         return six_unlock_ip_##type(lock, _THIS_IP_);                   \
183 }
184
185 __SIX_LOCK(read)
186 __SIX_LOCK(intent)
187 __SIX_LOCK(write)
188 #undef __SIX_LOCK
189
190 #define SIX_LOCK_DISPATCH(type, fn, ...)                        \
191         switch (type) {                                         \
192         case SIX_LOCK_read:                                     \
193                 return fn##_read(__VA_ARGS__);                  \
194         case SIX_LOCK_intent:                                   \
195                 return fn##_intent(__VA_ARGS__);                \
196         case SIX_LOCK_write:                                    \
197                 return fn##_write(__VA_ARGS__);                 \
198         default:                                                \
199                 BUG();                                          \
200         }
201
202 static inline bool six_trylock_type(struct six_lock *lock, enum six_lock_type type)
203 {
204         SIX_LOCK_DISPATCH(type, six_trylock, lock);
205 }
206
207 static inline bool six_relock_type(struct six_lock *lock, enum six_lock_type type,
208                                    unsigned seq)
209 {
210         SIX_LOCK_DISPATCH(type, six_relock, lock, seq);
211 }
212
213 static inline int six_lock_type(struct six_lock *lock, enum six_lock_type type,
214                                 six_lock_should_sleep_fn should_sleep_fn, void *p)
215 {
216         SIX_LOCK_DISPATCH(type, six_lock, lock, should_sleep_fn, p);
217 }
218
219 static inline int six_lock_type_ip_waiter(struct six_lock *lock, enum six_lock_type type,
220                                 struct six_lock_waiter *wait,
221                                 six_lock_should_sleep_fn should_sleep_fn, void *p,
222                                 unsigned long ip)
223 {
224         SIX_LOCK_DISPATCH(type, six_lock_ip_waiter, lock, wait, should_sleep_fn, p, ip);
225 }
226
227 static inline int six_lock_type_waiter(struct six_lock *lock, enum six_lock_type type,
228                                 struct six_lock_waiter *wait,
229                                 six_lock_should_sleep_fn should_sleep_fn, void *p)
230 {
231         SIX_LOCK_DISPATCH(type, six_lock_waiter, lock, wait, should_sleep_fn, p);
232 }
233
234 static inline void six_unlock_type(struct six_lock *lock, enum six_lock_type type)
235 {
236         SIX_LOCK_DISPATCH(type, six_unlock, lock);
237 }
238
239 void six_lock_downgrade(struct six_lock *);
240 bool six_lock_tryupgrade(struct six_lock *);
241 bool six_trylock_convert(struct six_lock *, enum six_lock_type,
242                          enum six_lock_type);
243
244 void six_lock_increment(struct six_lock *, enum six_lock_type);
245
246 void six_lock_wakeup_all(struct six_lock *);
247
248 void six_lock_pcpu_free(struct six_lock *);
249 void six_lock_pcpu_alloc(struct six_lock *);
250
251 struct six_lock_count {
252         unsigned n[3];
253 };
254
255 struct six_lock_count six_lock_counts(struct six_lock *);
256
257 #endif /* _LINUX_SIX_H */