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