]> git.sesse.net Git - bcachefs-tools-debian/blob - libbcachefs/btree_locking.h
Update bcachefs sources to a5da815430 bcachefs: Convert constants to consts
[bcachefs-tools-debian] / libbcachefs / btree_locking.h
1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _BCACHEFS_BTREE_LOCKING_H
3 #define _BCACHEFS_BTREE_LOCKING_H
4
5 /*
6  * Only for internal btree use:
7  *
8  * The btree iterator tracks what locks it wants to take, and what locks it
9  * currently has - here we have wrappers for locking/unlocking btree nodes and
10  * updating the iterator state
11  */
12
13 #include <linux/six.h>
14
15 #include "btree_iter.h"
16
17 extern struct lock_class_key bch2_btree_node_lock_key;
18
19 static inline bool is_btree_node(struct btree_path *path, unsigned l)
20 {
21         return l < BTREE_MAX_DEPTH && !IS_ERR_OR_NULL(path->l[l].b);
22 }
23
24 static inline struct btree_transaction_stats *btree_trans_stats(struct btree_trans *trans)
25 {
26         return trans->fn_idx < ARRAY_SIZE(trans->c->btree_transaction_stats)
27                 ? &trans->c->btree_transaction_stats[trans->fn_idx]
28                 : NULL;
29 }
30
31 /* matches six lock types */
32 enum btree_node_locked_type {
33         BTREE_NODE_UNLOCKED             = -1,
34         BTREE_NODE_READ_LOCKED          = SIX_LOCK_read,
35         BTREE_NODE_INTENT_LOCKED        = SIX_LOCK_intent,
36         BTREE_NODE_WRITE_LOCKED         = SIX_LOCK_write,
37 };
38
39 static inline int btree_node_locked_type(struct btree_path *path,
40                                          unsigned level)
41 {
42         return BTREE_NODE_UNLOCKED + ((path->nodes_locked >> (level << 1)) & 3);
43 }
44
45 static inline bool btree_node_write_locked(struct btree_path *path, unsigned l)
46 {
47         return btree_node_locked_type(path, l) == BTREE_NODE_WRITE_LOCKED;
48 }
49
50 static inline bool btree_node_intent_locked(struct btree_path *path, unsigned l)
51 {
52         return btree_node_locked_type(path, l) == BTREE_NODE_INTENT_LOCKED;
53 }
54
55 static inline bool btree_node_read_locked(struct btree_path *path, unsigned l)
56 {
57         return btree_node_locked_type(path, l) == BTREE_NODE_READ_LOCKED;
58 }
59
60 static inline bool btree_node_locked(struct btree_path *path, unsigned level)
61 {
62         return btree_node_locked_type(path, level) != BTREE_NODE_UNLOCKED;
63 }
64
65 static inline void mark_btree_node_locked_noreset(struct btree_path *path,
66                                                   unsigned level,
67                                                   enum btree_node_locked_type type)
68 {
69         /* relying on this to avoid a branch */
70         BUILD_BUG_ON(SIX_LOCK_read   != 0);
71         BUILD_BUG_ON(SIX_LOCK_intent != 1);
72
73         path->nodes_locked &= ~(3U << (level << 1));
74         path->nodes_locked |= (type + 1) << (level << 1);
75 }
76
77 static inline void mark_btree_node_unlocked(struct btree_path *path,
78                                             unsigned level)
79 {
80         EBUG_ON(btree_node_write_locked(path, level));
81         mark_btree_node_locked_noreset(path, level, BTREE_NODE_UNLOCKED);
82 }
83
84 static inline void mark_btree_node_locked(struct btree_trans *trans,
85                                           struct btree_path *path,
86                                           unsigned level,
87                                           enum six_lock_type type)
88 {
89         mark_btree_node_locked_noreset(path, level, type);
90 #ifdef CONFIG_BCACHEFS_LOCK_TIME_STATS
91         path->l[level].lock_taken_time = local_clock();
92 #endif
93 }
94
95 static inline enum six_lock_type __btree_lock_want(struct btree_path *path, int level)
96 {
97         return level < path->locks_want
98                 ? SIX_LOCK_intent
99                 : SIX_LOCK_read;
100 }
101
102 static inline enum btree_node_locked_type
103 btree_lock_want(struct btree_path *path, int level)
104 {
105         if (level < path->level)
106                 return BTREE_NODE_UNLOCKED;
107         if (level < path->locks_want)
108                 return BTREE_NODE_INTENT_LOCKED;
109         if (level == path->level)
110                 return BTREE_NODE_READ_LOCKED;
111         return BTREE_NODE_UNLOCKED;
112 }
113
114 static void btree_trans_lock_hold_time_update(struct btree_trans *trans,
115                                               struct btree_path *path, unsigned level)
116 {
117 #ifdef CONFIG_BCACHEFS_LOCK_TIME_STATS
118         struct btree_transaction_stats *s = btree_trans_stats(trans);
119
120         if (s)
121                 __bch2_time_stats_update(&s->lock_hold_times,
122                                          path->l[level].lock_taken_time,
123                                          local_clock());
124 #endif
125 }
126
127 /* unlock: */
128
129 static inline void btree_node_unlock(struct btree_trans *trans,
130                                      struct btree_path *path, unsigned level)
131 {
132         int lock_type = btree_node_locked_type(path, level);
133
134         EBUG_ON(level >= BTREE_MAX_DEPTH);
135
136         if (lock_type != BTREE_NODE_UNLOCKED) {
137                 six_unlock_type(&path->l[level].b->c.lock, lock_type);
138                 btree_trans_lock_hold_time_update(trans, path, level);
139         }
140         mark_btree_node_unlocked(path, level);
141 }
142
143 static inline int btree_path_lowest_level_locked(struct btree_path *path)
144 {
145         return __ffs(path->nodes_locked) >> 1;
146 }
147
148 static inline int btree_path_highest_level_locked(struct btree_path *path)
149 {
150         return __fls(path->nodes_locked) >> 1;
151 }
152
153 static inline void __bch2_btree_path_unlock(struct btree_trans *trans,
154                                             struct btree_path *path)
155 {
156         btree_path_set_dirty(path, BTREE_ITER_NEED_RELOCK);
157
158         while (path->nodes_locked)
159                 btree_node_unlock(trans, path, btree_path_lowest_level_locked(path));
160 }
161
162 /*
163  * Updates the saved lock sequence number, so that bch2_btree_node_relock() will
164  * succeed:
165  */
166 static inline void
167 bch2_btree_node_unlock_write_inlined(struct btree_trans *trans, struct btree_path *path,
168                                      struct btree *b)
169 {
170         struct btree_path *linked;
171
172         EBUG_ON(path->l[b->c.level].b != b);
173         EBUG_ON(path->l[b->c.level].lock_seq + 1 != b->c.lock.state.seq);
174         EBUG_ON(btree_node_locked_type(path, b->c.level) != SIX_LOCK_write);
175
176         mark_btree_node_locked_noreset(path, b->c.level, SIX_LOCK_intent);
177
178         trans_for_each_path_with_node(trans, b, linked)
179                 linked->l[b->c.level].lock_seq += 2;
180
181         six_unlock_write(&b->c.lock);
182 }
183
184 void bch2_btree_node_unlock_write(struct btree_trans *,
185                         struct btree_path *, struct btree *);
186
187 int bch2_six_check_for_deadlock(struct six_lock *lock, void *p);
188
189 /* lock: */
190
191 static inline int __btree_node_lock_nopath(struct btree_trans *trans,
192                                          struct btree_bkey_cached_common *b,
193                                          enum six_lock_type type,
194                                          bool lock_may_not_fail,
195                                          unsigned long ip)
196 {
197         int ret;
198
199         trans->lock_may_not_fail = lock_may_not_fail;
200         trans->lock_must_abort  = false;
201         trans->locking          = b;
202
203         ret = six_lock_type_ip_waiter(&b->lock, type, &trans->locking_wait,
204                                    bch2_six_check_for_deadlock, trans, ip);
205         WRITE_ONCE(trans->locking, NULL);
206         WRITE_ONCE(trans->locking_wait.start_time, 0);
207         return ret;
208 }
209
210 static inline int __must_check
211 btree_node_lock_nopath(struct btree_trans *trans,
212                        struct btree_bkey_cached_common *b,
213                        enum six_lock_type type,
214                        unsigned long ip)
215 {
216         return __btree_node_lock_nopath(trans, b, type, false, ip);
217 }
218
219 static inline void btree_node_lock_nopath_nofail(struct btree_trans *trans,
220                                          struct btree_bkey_cached_common *b,
221                                          enum six_lock_type type)
222 {
223         int ret = __btree_node_lock_nopath(trans, b, type, true, _THIS_IP_);
224
225         BUG_ON(ret);
226 }
227
228 /*
229  * Lock a btree node if we already have it locked on one of our linked
230  * iterators:
231  */
232 static inline bool btree_node_lock_increment(struct btree_trans *trans,
233                                              struct btree_bkey_cached_common *b,
234                                              unsigned level,
235                                              enum btree_node_locked_type want)
236 {
237         struct btree_path *path;
238
239         trans_for_each_path(trans, path)
240                 if (&path->l[level].b->c == b &&
241                     btree_node_locked_type(path, level) >= want) {
242                         six_lock_increment(&b->lock, want);
243                         return true;
244                 }
245
246         return false;
247 }
248
249 static inline int btree_node_lock(struct btree_trans *trans,
250                         struct btree_path *path,
251                         struct btree_bkey_cached_common *b,
252                         unsigned level,
253                         enum six_lock_type type,
254                         unsigned long ip)
255 {
256         int ret = 0;
257
258         EBUG_ON(level >= BTREE_MAX_DEPTH);
259         EBUG_ON(!(trans->paths_allocated & (1ULL << path->idx)));
260
261         if (likely(six_trylock_type(&b->lock, type)) ||
262             btree_node_lock_increment(trans, b, level, type) ||
263             !(ret = btree_node_lock_nopath(trans, b, type, btree_path_ip_allocated(path)))) {
264 #ifdef CONFIG_BCACHEFS_LOCK_TIME_STATS
265                 path->l[b->level].lock_taken_time = local_clock();
266 #endif
267         }
268
269         return ret;
270 }
271
272 int __bch2_btree_node_lock_write(struct btree_trans *, struct btree_path *,
273                                  struct btree_bkey_cached_common *b, bool);
274
275 static inline int __btree_node_lock_write(struct btree_trans *trans,
276                                           struct btree_path *path,
277                                           struct btree_bkey_cached_common *b,
278                                           bool lock_may_not_fail)
279 {
280         EBUG_ON(&path->l[b->level].b->c != b);
281         EBUG_ON(path->l[b->level].lock_seq != b->lock.state.seq);
282         EBUG_ON(!btree_node_intent_locked(path, b->level));
283
284         /*
285          * six locks are unfair, and read locks block while a thread wants a
286          * write lock: thus, we need to tell the cycle detector we have a write
287          * lock _before_ taking the lock:
288          */
289         mark_btree_node_locked_noreset(path, b->level, SIX_LOCK_write);
290
291         return likely(six_trylock_write(&b->lock))
292                 ? 0
293                 : __bch2_btree_node_lock_write(trans, path, b, lock_may_not_fail);
294 }
295
296 static inline void bch2_btree_node_lock_write_nofail(struct btree_trans *trans,
297                                               struct btree_path *path,
298                                               struct btree_bkey_cached_common *b)
299 {
300         int ret = __btree_node_lock_write(trans, path, b, true);
301
302         BUG_ON(ret);
303 }
304
305 static inline int __must_check
306 bch2_btree_node_lock_write(struct btree_trans *trans,
307                            struct btree_path *path,
308                            struct btree_bkey_cached_common *b)
309 {
310         return __btree_node_lock_write(trans, path, b, false);
311 }
312
313 /* relock: */
314
315 bool bch2_btree_path_relock_norestart(struct btree_trans *,
316                                       struct btree_path *, unsigned long);
317 int __bch2_btree_path_relock(struct btree_trans *,
318                              struct btree_path *, unsigned long);
319
320 static inline int bch2_btree_path_relock(struct btree_trans *trans,
321                                 struct btree_path *path, unsigned long trace_ip)
322 {
323         return btree_node_locked(path, path->level)
324                 ? 0
325                 : __bch2_btree_path_relock(trans, path, trace_ip);
326 }
327
328 bool __bch2_btree_node_relock(struct btree_trans *, struct btree_path *, unsigned, bool trace);
329
330 static inline bool bch2_btree_node_relock(struct btree_trans *trans,
331                                           struct btree_path *path, unsigned level)
332 {
333         EBUG_ON(btree_node_locked(path, level) &&
334                 !btree_node_write_locked(path, level) &&
335                 btree_node_locked_type(path, level) != __btree_lock_want(path, level));
336
337         return likely(btree_node_locked(path, level)) ||
338                 (!IS_ERR_OR_NULL(path->l[level].b) &&
339                  __bch2_btree_node_relock(trans, path, level, true));
340 }
341
342 static inline bool bch2_btree_node_relock_notrace(struct btree_trans *trans,
343                                                   struct btree_path *path, unsigned level)
344 {
345         EBUG_ON(btree_node_locked(path, level) &&
346                 !btree_node_write_locked(path, level) &&
347                 btree_node_locked_type(path, level) != __btree_lock_want(path, level));
348
349         return likely(btree_node_locked(path, level)) ||
350                 (!IS_ERR_OR_NULL(path->l[level].b) &&
351                  __bch2_btree_node_relock(trans, path, level, false));
352 }
353
354 /* upgrade */
355
356 bool bch2_btree_path_upgrade_noupgrade_sibs(struct btree_trans *,
357                                struct btree_path *, unsigned);
358 bool __bch2_btree_path_upgrade(struct btree_trans *,
359                                struct btree_path *, unsigned);
360
361 static inline int bch2_btree_path_upgrade(struct btree_trans *trans,
362                                           struct btree_path *path,
363                                           unsigned new_locks_want)
364 {
365         unsigned old_locks_want = path->locks_want;
366
367         new_locks_want = min(new_locks_want, BTREE_MAX_DEPTH);
368
369         if (path->locks_want < new_locks_want
370             ? __bch2_btree_path_upgrade(trans, path, new_locks_want)
371             : path->uptodate == BTREE_ITER_UPTODATE)
372                 return 0;
373
374         trace_and_count(trans->c, trans_restart_upgrade, trans, _THIS_IP_, path,
375                         old_locks_want, new_locks_want);
376         return btree_trans_restart(trans, BCH_ERR_transaction_restart_upgrade);
377 }
378
379 /* misc: */
380
381 static inline void btree_path_set_should_be_locked(struct btree_path *path)
382 {
383         EBUG_ON(!btree_node_locked(path, path->level));
384         EBUG_ON(path->uptodate);
385
386         path->should_be_locked = true;
387 }
388
389 static inline void __btree_path_set_level_up(struct btree_trans *trans,
390                                       struct btree_path *path,
391                                       unsigned l)
392 {
393         btree_node_unlock(trans, path, l);
394         path->l[l].b = ERR_PTR(-BCH_ERR_no_btree_node_up);
395 }
396
397 static inline void btree_path_set_level_up(struct btree_trans *trans,
398                                     struct btree_path *path)
399 {
400         __btree_path_set_level_up(trans, path, path->level++);
401         btree_path_set_dirty(path, BTREE_ITER_NEED_TRAVERSE);
402 }
403
404 /* debug */
405
406 struct six_lock_count bch2_btree_node_lock_counts(struct btree_trans *,
407                                 struct btree_path *,
408                                 struct btree_bkey_cached_common *b,
409                                 unsigned);
410
411 int bch2_check_for_deadlock(struct btree_trans *, struct printbuf *);
412
413 #ifdef CONFIG_BCACHEFS_DEBUG
414 void bch2_btree_path_verify_locks(struct btree_path *);
415 void bch2_trans_verify_locks(struct btree_trans *);
416 #else
417 static inline void bch2_btree_path_verify_locks(struct btree_path *path) {}
418 static inline void bch2_trans_verify_locks(struct btree_trans *trans) {}
419 #endif
420
421 #endif /* _BCACHEFS_BTREE_LOCKING_H */