]> git.sesse.net Git - bcachefs-tools-debian/blob - include/linux/six.h
Update bcachefs sources to 24c6361e20 bcachefs: Fix a trans path overflow in bch2_btr...
[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 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.
59  */
60
61 #include <linux/lockdep.h>
62 #include <linux/sched.h>
63 #include <linux/types.h>
64
65 #define SIX_LOCK_SEPARATE_LOCKFNS
66
67 union six_lock_state {
68         struct {
69                 atomic64_t      counter;
70         };
71
72         struct {
73                 u64             v;
74         };
75
76         struct {
77                 /* for waitlist_bitnr() */
78                 unsigned long   l;
79         };
80
81         struct {
82                 unsigned        read_lock:27;
83                 unsigned        write_locking:1;
84                 unsigned        intent_lock:1;
85                 unsigned        waiters:3;
86                 /*
87                  * seq works much like in seqlocks: it's incremented every time
88                  * we lock and unlock for write.
89                  *
90                  * If it's odd write lock is held, even unlocked.
91                  *
92                  * Thus readers can unlock, and then lock again later iff it
93                  * hasn't been modified in the meantime.
94                  */
95                 u32             seq;
96         };
97 };
98
99 enum six_lock_type {
100         SIX_LOCK_read,
101         SIX_LOCK_intent,
102         SIX_LOCK_write,
103 };
104
105 struct six_lock {
106         union six_lock_state    state;
107         struct task_struct      *owner;
108         unsigned __percpu       *readers;
109         unsigned                intent_lock_recurse;
110         unsigned long           ip;
111         raw_spinlock_t          wait_lock;
112         struct list_head        wait_list;
113 #ifdef CONFIG_DEBUG_LOCK_ALLOC
114         struct lockdep_map      dep_map;
115 #endif
116 };
117
118 struct six_lock_waiter {
119         struct list_head        list;
120         struct task_struct      *task;
121         enum six_lock_type      lock_want;
122         bool                    lock_acquired;
123         u64                     start_time;
124 };
125
126 typedef int (*six_lock_should_sleep_fn)(struct six_lock *lock, void *);
127
128 static __always_inline void __six_lock_init(struct six_lock *lock,
129                                             const char *name,
130                                             struct lock_class_key *key)
131 {
132         atomic64_set(&lock->state.counter, 0);
133         raw_spin_lock_init(&lock->wait_lock);
134         INIT_LIST_HEAD(&lock->wait_list);
135 #ifdef CONFIG_DEBUG_LOCK_ALLOC
136         debug_check_no_locks_freed((void *) lock, sizeof(*lock));
137         lockdep_init_map(&lock->dep_map, name, key, 0);
138 #endif
139 }
140
141 #define six_lock_init(lock)                                             \
142 do {                                                                    \
143         static struct lock_class_key __key;                             \
144                                                                         \
145         __six_lock_init((lock), #lock, &__key);                         \
146 } while (0)
147
148 #define __SIX_VAL(field, _v)    (((union six_lock_state) { .field = _v }).v)
149
150 #define __SIX_LOCK(type)                                                \
151 bool six_trylock_##type(struct six_lock *);                             \
152 bool six_relock_##type(struct six_lock *, u32);                         \
153 int six_lock_##type(struct six_lock *, six_lock_should_sleep_fn, void *);\
154 int six_lock_waiter_##type(struct six_lock *, struct six_lock_waiter *, \
155                            six_lock_should_sleep_fn, void *);           \
156 void six_unlock_##type(struct six_lock *);
157
158 __SIX_LOCK(read)
159 __SIX_LOCK(intent)
160 __SIX_LOCK(write)
161 #undef __SIX_LOCK
162
163 #define SIX_LOCK_DISPATCH(type, fn, ...)                        \
164         switch (type) {                                         \
165         case SIX_LOCK_read:                                     \
166                 return fn##_read(__VA_ARGS__);                  \
167         case SIX_LOCK_intent:                                   \
168                 return fn##_intent(__VA_ARGS__);                \
169         case SIX_LOCK_write:                                    \
170                 return fn##_write(__VA_ARGS__);                 \
171         default:                                                \
172                 BUG();                                          \
173         }
174
175 static inline bool six_trylock_type(struct six_lock *lock, enum six_lock_type type)
176 {
177         SIX_LOCK_DISPATCH(type, six_trylock, lock);
178 }
179
180 static inline bool six_relock_type(struct six_lock *lock, enum six_lock_type type,
181                                    unsigned seq)
182 {
183         SIX_LOCK_DISPATCH(type, six_relock, lock, seq);
184 }
185
186 static inline int six_lock_type(struct six_lock *lock, enum six_lock_type type,
187                                 six_lock_should_sleep_fn should_sleep_fn, void *p)
188 {
189         SIX_LOCK_DISPATCH(type, six_lock, lock, should_sleep_fn, p);
190 }
191
192 static inline int six_lock_type_waiter(struct six_lock *lock, enum six_lock_type type,
193                                 struct six_lock_waiter *wait,
194                                 six_lock_should_sleep_fn should_sleep_fn, void *p)
195 {
196         SIX_LOCK_DISPATCH(type, six_lock_waiter, lock, wait, should_sleep_fn, p);
197 }
198
199 static inline void six_unlock_type(struct six_lock *lock, enum six_lock_type type)
200 {
201         SIX_LOCK_DISPATCH(type, six_unlock, lock);
202 }
203
204 void six_lock_downgrade(struct six_lock *);
205 bool six_lock_tryupgrade(struct six_lock *);
206 bool six_trylock_convert(struct six_lock *, enum six_lock_type,
207                          enum six_lock_type);
208
209 void six_lock_increment(struct six_lock *, enum six_lock_type);
210
211 void six_lock_wakeup_all(struct six_lock *);
212
213 void six_lock_pcpu_free(struct six_lock *);
214 void six_lock_pcpu_alloc(struct six_lock *);
215
216 struct six_lock_count {
217         unsigned n[3];
218 };
219
220 struct six_lock_count six_lock_counts(struct six_lock *);
221
222 #endif /* _LINUX_SIX_H */