]> git.sesse.net Git - bcachefs-tools-debian/blob - libbcachefs/btree_locking.c
Update bcachefs sources to dab31ca168 bcachefs: Add some logging for btree node rewri...
[bcachefs-tools-debian] / libbcachefs / btree_locking.c
1 // SPDX-License-Identifier: GPL-2.0
2
3 #include "bcachefs.h"
4 #include "btree_locking.h"
5 #include "btree_types.h"
6
7 struct lock_class_key bch2_btree_node_lock_key;
8
9 /* Btree node locking: */
10
11 static inline void six_lock_readers_add(struct six_lock *lock, int nr)
12 {
13         if (lock->readers)
14                 this_cpu_add(*lock->readers, nr);
15         else if (nr > 0)
16                 atomic64_add(__SIX_VAL(read_lock, nr), &lock->state.counter);
17         else
18                 atomic64_sub(__SIX_VAL(read_lock, -nr), &lock->state.counter);
19 }
20
21 struct six_lock_count bch2_btree_node_lock_counts(struct btree_trans *trans,
22                                                   struct btree_path *skip,
23                                                   struct btree_bkey_cached_common *b,
24                                                   unsigned level)
25 {
26         struct btree_path *path;
27         struct six_lock_count ret;
28
29         memset(&ret, 0, sizeof(ret));
30
31         if (IS_ERR_OR_NULL(b))
32                 return ret;
33
34         trans_for_each_path(trans, path)
35                 if (path != skip && &path->l[level].b->c == b) {
36                         int t = btree_node_locked_type(path, level);
37
38                         if (t != BTREE_NODE_UNLOCKED)
39                                 ret.n[t]++;
40                 }
41
42         return ret;
43 }
44
45 /* unlock */
46
47 void bch2_btree_node_unlock_write(struct btree_trans *trans,
48                         struct btree_path *path, struct btree *b)
49 {
50         bch2_btree_node_unlock_write_inlined(trans, path, b);
51 }
52
53 /* lock */
54
55 /*
56  * @trans wants to lock @b with type @type
57  */
58 struct trans_waiting_for_lock {
59         struct btree_trans              *trans;
60         struct btree_bkey_cached_common *node_want;
61         enum six_lock_type              lock_want;
62
63         /* for iterating over held locks :*/
64         u8                              path_idx;
65         u8                              level;
66         u64                             lock_start_time;
67 };
68
69 struct lock_graph {
70         struct trans_waiting_for_lock   g[8];
71         unsigned                        nr;
72 };
73
74 static noinline void print_cycle(struct printbuf *out, struct lock_graph *g)
75 {
76         struct trans_waiting_for_lock *i;
77
78         prt_printf(out, "Found lock cycle (%u entries):", g->nr);
79         prt_newline(out);
80
81         for (i = g->g; i < g->g + g->nr; i++)
82                 bch2_btree_trans_to_text(out, i->trans);
83 }
84
85 static noinline void print_chain(struct printbuf *out, struct lock_graph *g)
86 {
87         struct trans_waiting_for_lock *i;
88
89         for (i = g->g; i != g->g + g->nr; i++) {
90                 if (i != g->g)
91                         prt_str(out, "<- ");
92                 prt_printf(out, "%u ", i->trans->locking_wait.task->pid);
93         }
94         prt_newline(out);
95 }
96
97 static void lock_graph_up(struct lock_graph *g)
98 {
99         closure_put(&g->g[--g->nr].trans->ref);
100 }
101
102 static noinline void lock_graph_pop_all(struct lock_graph *g)
103 {
104         while (g->nr)
105                 lock_graph_up(g);
106 }
107
108 static void lock_graph_down(struct lock_graph *g, struct btree_trans *trans)
109 {
110         closure_get(&trans->ref);
111
112         g->g[g->nr++] = (struct trans_waiting_for_lock) {
113                 .trans          = trans,
114                 .node_want      = trans->locking,
115                 .lock_want      = trans->locking_wait.lock_want,
116         };
117 }
118
119 static bool lock_graph_remove_non_waiters(struct lock_graph *g)
120 {
121         struct trans_waiting_for_lock *i;
122
123         for (i = g->g + 1; i < g->g + g->nr; i++)
124                 if (i->trans->locking != i->node_want ||
125                     i->trans->locking_wait.start_time != i[-1].lock_start_time) {
126                         while (g->g + g->nr > i)
127                                 lock_graph_up(g);
128                         return true;
129                 }
130
131         return false;
132 }
133
134 static int abort_lock(struct lock_graph *g, struct trans_waiting_for_lock *i)
135 {
136         if (i == g->g) {
137                 trace_and_count(i->trans->c, trans_restart_would_deadlock, i->trans, _RET_IP_);
138                 return btree_trans_restart(i->trans, BCH_ERR_transaction_restart_would_deadlock);
139         } else {
140                 i->trans->lock_must_abort = true;
141                 wake_up_process(i->trans->locking_wait.task);
142                 return 0;
143         }
144 }
145
146 static int btree_trans_abort_preference(struct btree_trans *trans)
147 {
148         if (trans->lock_may_not_fail)
149                 return 0;
150         if (trans->locking_wait.lock_want == SIX_LOCK_write)
151                 return 1;
152         if (!trans->in_traverse_all)
153                 return 2;
154         return 3;
155 }
156
157 static noinline int break_cycle(struct lock_graph *g, struct printbuf *cycle)
158 {
159         struct trans_waiting_for_lock *i, *abort = NULL;
160         unsigned best = 0, pref;
161         int ret;
162
163         if (lock_graph_remove_non_waiters(g))
164                 return 0;
165
166         /* Only checking, for debugfs: */
167         if (cycle) {
168                 print_cycle(cycle, g);
169                 ret = -1;
170                 goto out;
171         }
172
173         for (i = g->g; i < g->g + g->nr; i++) {
174                 pref = btree_trans_abort_preference(i->trans);
175                 if (pref > best) {
176                         abort = i;
177                         best = pref;
178                 }
179         }
180
181         if (unlikely(!best)) {
182                 struct printbuf buf = PRINTBUF;
183
184                 prt_printf(&buf, bch2_fmt(g->g->trans->c, "cycle of nofail locks"));
185
186                 for (i = g->g; i < g->g + g->nr; i++) {
187                         struct btree_trans *trans = i->trans;
188
189                         bch2_btree_trans_to_text(&buf, trans);
190
191                         prt_printf(&buf, "backtrace:");
192                         prt_newline(&buf);
193                         printbuf_indent_add(&buf, 2);
194                         bch2_prt_backtrace(&buf, trans->locking_wait.task);
195                         printbuf_indent_sub(&buf, 2);
196                         prt_newline(&buf);
197                 }
198
199                 bch2_print_string_as_lines(KERN_ERR, buf.buf);
200                 printbuf_exit(&buf);
201                 BUG();
202         }
203
204         ret = abort_lock(g, abort);
205 out:
206         if (ret)
207                 while (g->nr)
208                         lock_graph_up(g);
209         return ret;
210 }
211
212 static int lock_graph_descend(struct lock_graph *g, struct btree_trans *trans,
213                               struct printbuf *cycle)
214 {
215         struct btree_trans *orig_trans = g->g->trans;
216         struct trans_waiting_for_lock *i;
217
218         for (i = g->g; i < g->g + g->nr; i++)
219                 if (i->trans == trans)
220                         return break_cycle(g, cycle);
221
222         if (g->nr == ARRAY_SIZE(g->g)) {
223                 if (orig_trans->lock_may_not_fail)
224                         return 0;
225
226                 while (g->nr)
227                         lock_graph_up(g);
228                 trace_and_count(trans->c, trans_restart_would_deadlock_recursion_limit, trans, _RET_IP_);
229                 return btree_trans_restart(orig_trans, BCH_ERR_transaction_restart_deadlock_recursion_limit);
230         }
231
232         lock_graph_down(g, trans);
233         return 0;
234 }
235
236 static bool lock_type_conflicts(enum six_lock_type t1, enum six_lock_type t2)
237 {
238         return t1 + t2 > 1;
239 }
240
241 int bch2_check_for_deadlock(struct btree_trans *trans, struct printbuf *cycle)
242 {
243         struct lock_graph g;
244         struct trans_waiting_for_lock *top;
245         struct btree_bkey_cached_common *b;
246         struct btree_path *path;
247         int ret;
248
249         if (trans->lock_must_abort) {
250                 trace_and_count(trans->c, trans_restart_would_deadlock, trans, _RET_IP_);
251                 return btree_trans_restart(trans, BCH_ERR_transaction_restart_would_deadlock);
252         }
253
254         g.nr = 0;
255         lock_graph_down(&g, trans);
256 next:
257         if (!g.nr)
258                 return 0;
259
260         top = &g.g[g.nr - 1];
261
262         trans_for_each_path_from(top->trans, path, top->path_idx) {
263                 if (!path->nodes_locked)
264                         continue;
265
266                 if (top->path_idx != path->idx) {
267                         top->path_idx           = path->idx;
268                         top->level              = 0;
269                         top->lock_start_time    = 0;
270                 }
271
272                 for (;
273                      top->level < BTREE_MAX_DEPTH;
274                      top->level++, top->lock_start_time = 0) {
275                         int lock_held = btree_node_locked_type(path, top->level);
276
277                         if (lock_held == BTREE_NODE_UNLOCKED)
278                                 continue;
279
280                         b = &READ_ONCE(path->l[top->level].b)->c;
281
282                         if (IS_ERR_OR_NULL(b)) {
283                                 /*
284                                  * If we get here, it means we raced with the
285                                  * other thread updating its btree_path
286                                  * structures - which means it can't be blocked
287                                  * waiting on a lock:
288                                  */
289                                 if (!lock_graph_remove_non_waiters(&g)) {
290                                         /*
291                                          * If lock_graph_remove_non_waiters()
292                                          * didn't do anything, it must be
293                                          * because we're being called by debugfs
294                                          * checking for lock cycles, which
295                                          * invokes us on btree_transactions that
296                                          * aren't actually waiting on anything.
297                                          * Just bail out:
298                                          */
299                                         lock_graph_pop_all(&g);
300                                 }
301
302                                 goto next;
303                         }
304
305                         if (list_empty_careful(&b->lock.wait_list))
306                                 continue;
307
308                         raw_spin_lock(&b->lock.wait_lock);
309                         list_for_each_entry(trans, &b->lock.wait_list, locking_wait.list) {
310                                 BUG_ON(b != trans->locking);
311
312                                 if (top->lock_start_time &&
313                                     time_after_eq64(top->lock_start_time, trans->locking_wait.start_time))
314                                         continue;
315
316                                 top->lock_start_time = trans->locking_wait.start_time;
317
318                                 /* Don't check for self deadlock: */
319                                 if (trans == top->trans ||
320                                     !lock_type_conflicts(lock_held, trans->locking_wait.lock_want))
321                                         continue;
322
323                                 ret = lock_graph_descend(&g, trans, cycle);
324                                 raw_spin_unlock(&b->lock.wait_lock);
325
326                                 if (ret)
327                                         return ret;
328                                 goto next;
329
330                         }
331                         raw_spin_unlock(&b->lock.wait_lock);
332                 }
333         }
334
335         if (g.nr > 1 && cycle)
336                 print_chain(cycle, &g);
337         lock_graph_up(&g);
338         goto next;
339 }
340
341 int bch2_six_check_for_deadlock(struct six_lock *lock, void *p)
342 {
343         struct btree_trans *trans = p;
344
345         return bch2_check_for_deadlock(trans, NULL);
346 }
347
348 int __bch2_btree_node_lock_write(struct btree_trans *trans, struct btree_path *path,
349                                  struct btree_bkey_cached_common *b,
350                                  bool lock_may_not_fail)
351 {
352         int readers = bch2_btree_node_lock_counts(trans, NULL, b, b->level).n[SIX_LOCK_read];
353         int ret;
354
355         /*
356          * Must drop our read locks before calling six_lock_write() -
357          * six_unlock() won't do wakeups until the reader count
358          * goes to 0, and it's safe because we have the node intent
359          * locked:
360          */
361         six_lock_readers_add(&b->lock, -readers);
362         ret = __btree_node_lock_nopath(trans, b, SIX_LOCK_write,
363                                        lock_may_not_fail, _RET_IP_);
364         six_lock_readers_add(&b->lock, readers);
365
366         if (ret)
367                 mark_btree_node_locked_noreset(path, b->level, SIX_LOCK_intent);
368
369         return ret;
370 }
371
372 /* relock */
373
374 static inline bool btree_path_get_locks(struct btree_trans *trans,
375                                         struct btree_path *path,
376                                         bool upgrade)
377 {
378         unsigned l = path->level;
379         int fail_idx = -1;
380
381         do {
382                 if (!btree_path_node(path, l))
383                         break;
384
385                 if (!(upgrade
386                       ? bch2_btree_node_upgrade(trans, path, l)
387                       : bch2_btree_node_relock(trans, path, l)))
388                         fail_idx = l;
389
390                 l++;
391         } while (l < path->locks_want);
392
393         /*
394          * When we fail to get a lock, we have to ensure that any child nodes
395          * can't be relocked so bch2_btree_path_traverse has to walk back up to
396          * the node that we failed to relock:
397          */
398         if (fail_idx >= 0) {
399                 __bch2_btree_path_unlock(trans, path);
400                 btree_path_set_dirty(path, BTREE_ITER_NEED_TRAVERSE);
401
402                 do {
403                         path->l[fail_idx].b = upgrade
404                                 ? ERR_PTR(-BCH_ERR_no_btree_node_upgrade)
405                                 : ERR_PTR(-BCH_ERR_no_btree_node_relock);
406                         --fail_idx;
407                 } while (fail_idx >= 0);
408         }
409
410         if (path->uptodate == BTREE_ITER_NEED_RELOCK)
411                 path->uptodate = BTREE_ITER_UPTODATE;
412
413         bch2_trans_verify_locks(trans);
414
415         return path->uptodate < BTREE_ITER_NEED_RELOCK;
416 }
417
418 bool __bch2_btree_node_relock(struct btree_trans *trans,
419                               struct btree_path *path, unsigned level,
420                               bool trace)
421 {
422         struct btree *b = btree_path_node(path, level);
423         int want = __btree_lock_want(path, level);
424
425         if (race_fault())
426                 goto fail;
427
428         if (six_relock_type(&b->c.lock, want, path->l[level].lock_seq) ||
429             (btree_node_lock_seq_matches(path, b, level) &&
430              btree_node_lock_increment(trans, &b->c, level, want))) {
431                 mark_btree_node_locked(trans, path, level, want);
432                 return true;
433         }
434 fail:
435         if (trace && !trans->notrace_relock_fail)
436                 trace_and_count(trans->c, btree_path_relock_fail, trans, _RET_IP_, path, level);
437         return false;
438 }
439
440 /* upgrade */
441
442 bool bch2_btree_node_upgrade(struct btree_trans *trans,
443                              struct btree_path *path, unsigned level)
444 {
445         struct btree *b = path->l[level].b;
446         struct six_lock_count count = bch2_btree_node_lock_counts(trans, path, &b->c, level);
447
448         if (!is_btree_node(path, level))
449                 return false;
450
451         switch (btree_lock_want(path, level)) {
452         case BTREE_NODE_UNLOCKED:
453                 BUG_ON(btree_node_locked(path, level));
454                 return true;
455         case BTREE_NODE_READ_LOCKED:
456                 BUG_ON(btree_node_intent_locked(path, level));
457                 return bch2_btree_node_relock(trans, path, level);
458         case BTREE_NODE_INTENT_LOCKED:
459                 break;
460         case BTREE_NODE_WRITE_LOCKED:
461                 BUG();
462         }
463
464         if (btree_node_intent_locked(path, level))
465                 return true;
466
467         if (race_fault())
468                 return false;
469
470         if (btree_node_locked(path, level)) {
471                 bool ret;
472
473                 six_lock_readers_add(&b->c.lock, -count.n[SIX_LOCK_read]);
474                 ret = six_lock_tryupgrade(&b->c.lock);
475                 six_lock_readers_add(&b->c.lock, count.n[SIX_LOCK_read]);
476
477                 if (ret)
478                         goto success;
479         } else {
480                 if (six_relock_type(&b->c.lock, SIX_LOCK_intent, path->l[level].lock_seq))
481                         goto success;
482         }
483
484         /*
485          * Do we already have an intent lock via another path? If so, just bump
486          * lock count:
487          */
488         if (btree_node_lock_seq_matches(path, b, level) &&
489             btree_node_lock_increment(trans, &b->c, level, BTREE_NODE_INTENT_LOCKED)) {
490                 btree_node_unlock(trans, path, level);
491                 goto success;
492         }
493
494         trace_and_count(trans->c, btree_path_upgrade_fail, trans, _RET_IP_, path, level);
495         return false;
496 success:
497         mark_btree_node_locked_noreset(path, level, SIX_LOCK_intent);
498         return true;
499 }
500
501 /* Btree path locking: */
502
503 /*
504  * Only for btree_cache.c - only relocks intent locks
505  */
506 int bch2_btree_path_relock_intent(struct btree_trans *trans,
507                                   struct btree_path *path)
508 {
509         unsigned l;
510
511         for (l = path->level;
512              l < path->locks_want && btree_path_node(path, l);
513              l++) {
514                 if (!bch2_btree_node_relock(trans, path, l)) {
515                         __bch2_btree_path_unlock(trans, path);
516                         btree_path_set_dirty(path, BTREE_ITER_NEED_TRAVERSE);
517                         trace_and_count(trans->c, trans_restart_relock_path_intent, trans, _RET_IP_, path);
518                         return btree_trans_restart(trans, BCH_ERR_transaction_restart_relock_path_intent);
519                 }
520         }
521
522         return 0;
523 }
524
525 __flatten
526 bool bch2_btree_path_relock_norestart(struct btree_trans *trans,
527                         struct btree_path *path, unsigned long trace_ip)
528 {
529         return btree_path_get_locks(trans, path, false);
530 }
531
532 int __bch2_btree_path_relock(struct btree_trans *trans,
533                         struct btree_path *path, unsigned long trace_ip)
534 {
535         if (!bch2_btree_path_relock_norestart(trans, path, trace_ip)) {
536                 trace_and_count(trans->c, trans_restart_relock_path, trans, trace_ip, path);
537                 return btree_trans_restart(trans, BCH_ERR_transaction_restart_relock_path);
538         }
539
540         return 0;
541 }
542
543 __flatten
544 bool bch2_btree_path_upgrade_norestart(struct btree_trans *trans,
545                         struct btree_path *path, unsigned long trace_ip)
546 {
547         return btree_path_get_locks(trans, path, true);
548 }
549
550 bool bch2_btree_path_upgrade_noupgrade_sibs(struct btree_trans *trans,
551                                struct btree_path *path,
552                                unsigned new_locks_want)
553 {
554         EBUG_ON(path->locks_want >= new_locks_want);
555
556         path->locks_want = new_locks_want;
557
558         return btree_path_get_locks(trans, path, true);
559 }
560
561 bool __bch2_btree_path_upgrade(struct btree_trans *trans,
562                                struct btree_path *path,
563                                unsigned new_locks_want)
564 {
565         struct btree_path *linked;
566
567         if (bch2_btree_path_upgrade_noupgrade_sibs(trans, path, new_locks_want))
568                 return true;
569
570         /*
571          * XXX: this is ugly - we'd prefer to not be mucking with other
572          * iterators in the btree_trans here.
573          *
574          * On failure to upgrade the iterator, setting iter->locks_want and
575          * calling get_locks() is sufficient to make bch2_btree_path_traverse()
576          * get the locks we want on transaction restart.
577          *
578          * But if this iterator was a clone, on transaction restart what we did
579          * to this iterator isn't going to be preserved.
580          *
581          * Possibly we could add an iterator field for the parent iterator when
582          * an iterator is a copy - for now, we'll just upgrade any other
583          * iterators with the same btree id.
584          *
585          * The code below used to be needed to ensure ancestor nodes get locked
586          * before interior nodes - now that's handled by
587          * bch2_btree_path_traverse_all().
588          */
589         if (!path->cached && !trans->in_traverse_all)
590                 trans_for_each_path(trans, linked)
591                         if (linked != path &&
592                             linked->cached == path->cached &&
593                             linked->btree_id == path->btree_id &&
594                             linked->locks_want < new_locks_want) {
595                                 linked->locks_want = new_locks_want;
596                                 btree_path_get_locks(trans, linked, true);
597                         }
598
599         return false;
600 }
601
602 void __bch2_btree_path_downgrade(struct btree_trans *trans,
603                                  struct btree_path *path,
604                                  unsigned new_locks_want)
605 {
606         unsigned l;
607
608         EBUG_ON(path->locks_want < new_locks_want);
609
610         path->locks_want = new_locks_want;
611
612         while (path->nodes_locked &&
613                (l = btree_path_highest_level_locked(path)) >= path->locks_want) {
614                 if (l > path->level) {
615                         btree_node_unlock(trans, path, l);
616                 } else {
617                         if (btree_node_intent_locked(path, l)) {
618                                 six_lock_downgrade(&path->l[l].b->c.lock);
619                                 mark_btree_node_locked_noreset(path, l, SIX_LOCK_read);
620                         }
621                         break;
622                 }
623         }
624
625         bch2_btree_path_verify_locks(path);
626 }
627
628 /* Btree transaction locking: */
629
630 void bch2_trans_downgrade(struct btree_trans *trans)
631 {
632         struct btree_path *path;
633
634         trans_for_each_path(trans, path)
635                 bch2_btree_path_downgrade(trans, path);
636 }
637
638 int bch2_trans_relock(struct btree_trans *trans)
639 {
640         struct btree_path *path;
641
642         if (unlikely(trans->restarted))
643                 return -((int) trans->restarted);
644
645         trans_for_each_path(trans, path)
646                 if (path->should_be_locked &&
647                     !bch2_btree_path_relock_norestart(trans, path, _RET_IP_)) {
648                         trace_and_count(trans->c, trans_restart_relock, trans, _RET_IP_, path);
649                         return btree_trans_restart(trans, BCH_ERR_transaction_restart_relock);
650                 }
651         return 0;
652 }
653
654 int bch2_trans_relock_notrace(struct btree_trans *trans)
655 {
656         struct btree_path *path;
657
658         if (unlikely(trans->restarted))
659                 return -((int) trans->restarted);
660
661         trans_for_each_path(trans, path)
662                 if (path->should_be_locked &&
663                     !bch2_btree_path_relock_norestart(trans, path, _RET_IP_)) {
664                         return btree_trans_restart(trans, BCH_ERR_transaction_restart_relock);
665                 }
666         return 0;
667 }
668
669 void bch2_trans_unlock(struct btree_trans *trans)
670 {
671         struct btree_path *path;
672
673         trans_for_each_path(trans, path)
674                 __bch2_btree_path_unlock(trans, path);
675
676         /*
677          * bch2_gc_btree_init_recurse() doesn't use btree iterators for walking
678          * btree nodes, it implements its own walking:
679          */
680         EBUG_ON(!trans->is_initial_gc &&
681                 lock_class_is_held(&bch2_btree_node_lock_key));
682 }
683
684 bool bch2_trans_locked(struct btree_trans *trans)
685 {
686         struct btree_path *path;
687
688         trans_for_each_path(trans, path)
689                 if (path->nodes_locked)
690                         return true;
691         return false;
692 }
693
694 /* Debug */
695
696 #ifdef CONFIG_BCACHEFS_DEBUG
697
698 void bch2_btree_path_verify_locks(struct btree_path *path)
699 {
700         unsigned l;
701
702         if (!path->nodes_locked) {
703                 BUG_ON(path->uptodate == BTREE_ITER_UPTODATE &&
704                        btree_path_node(path, path->level));
705                 return;
706         }
707
708         for (l = 0; l < BTREE_MAX_DEPTH; l++) {
709                 int want = btree_lock_want(path, l);
710                 int have = btree_node_locked_type(path, l);
711
712                 BUG_ON(!is_btree_node(path, l) && have != BTREE_NODE_UNLOCKED);
713
714                 BUG_ON(is_btree_node(path, l) &&
715                        (want == BTREE_NODE_UNLOCKED ||
716                         have != BTREE_NODE_WRITE_LOCKED) &&
717                        want != have);
718         }
719 }
720
721 void bch2_trans_verify_locks(struct btree_trans *trans)
722 {
723         struct btree_path *path;
724
725         trans_for_each_path(trans, path)
726                 bch2_btree_path_verify_locks(path);
727 }
728
729 #endif