Skip to contentSkip to conversation

bcachefs: Convert open-coded lock_graph_pop_all to helper

PATCH New Alan Huang 1 participant 4 messages Oct 2, 2024

lore
Share & tools
lore

Alan Huang

author
#0 lore raw

PATCH 1/4 bcachefs: Convert open-coded lock_graph_pop_all to helper

---
 fs/bcachefs/btree_locking.c | 6 ++----
 1 file changed, 2 insertions(+), 4 deletions(-)
fs/bcachefs/btree_locking.c +2 -4
index 617478fd5351..50d9366d49bd 100644
@@ -229,8 +229,7 @@ static noinline int break_cycle(struct lock_graph *g, struct printbuf *cycle)
 	ret = abort_lock(g, abort);
 out:
 	if (ret)
-		while (g->nr)
-			lock_graph_up(g);
+		lock_graph_pop_all(g);
 	return ret;
 }
 
@@ -252,8 +251,7 @@ static int lock_graph_descend(struct lock_graph *g, struct btree_trans *trans,
 		if (orig_trans->lock_may_not_fail)
 			return 0;
 
-		while (g->nr)
-			lock_graph_up(g);
+		lock_graph_pop_all(g);
 
 		if (cycle)
 			return 0;
Signature · 1 line
2.45.2
author
#1 lore raw

PATCH 2/4 bcachefs: Introduce lock_graph_pop_from

This patch introduces a helper function called lock_graph_pop_from, it pops the graph from i.

---
 fs/bcachefs/btree_locking.c | 9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)
fs/bcachefs/btree_locking.c +7 -2
index 50d9366d49bd..8cc648be7d29 100644
@@ -109,6 +109,12 @@ static noinline void lock_graph_pop_all(struct lock_graph *g)
 		lock_graph_up(g);
 }
 
+static noinline void lock_graph_pop_from(struct lock_graph *g, struct trans_waiting_for_lock *i)
+{
+	while (g->g + g->nr > i)
+		lock_graph_up(g);
+}
+
 static void __lock_graph_down(struct lock_graph *g, struct btree_trans *trans)
 {
 	g->g[g->nr++] = (struct trans_waiting_for_lock) {
@@ -131,8 +137,7 @@ static bool lock_graph_remove_non_waiters(struct lock_graph *g)
 	for (i = g->g + 1; i < g->g + g->nr; i++)
 		if (i->trans->locking != i->node_want ||
 		    i->trans->locking_wait.start_time != i[-1].lock_start_time) {
-			while (g->g + g->nr > i)
-				lock_graph_up(g);
+			lock_graph_pop_from(g, i);
 			return true;
 		}
 
Signature · 1 line
2.45.2
author
#2 lore raw

PATCH 3/4 bcachefs: Only abort the transactions in the cycle

In reply to Alan Huang

When the cycle doesn't involve the initiator of the cycle detection, we might choose a transaction that is not involved in the cycle to abort. It shouldn't be that since it won't break the cycle, this patch therefore chooses the transaction in the cycle to abort.

---
 fs/bcachefs/btree_locking.c | 21 ++++++++++++++-------
 1 file changed, 14 insertions(+), 7 deletions(-)
fs/bcachefs/btree_locking.c +14 -7
index 8cc648be7d29..70f13ab5ba7b 100644
@@ -130,11 +130,17 @@ static void lock_graph_down(struct lock_graph *g, struct btree_trans *trans)
 	__lock_graph_down(g, trans);
 }
 
-static bool lock_graph_remove_non_waiters(struct lock_graph *g)
+static bool lock_graph_remove_non_waiters(struct lock_graph *g,
+					  struct trans_waiting_for_lock *from)
 {
 	struct trans_waiting_for_lock *i;
 
-	for (i = g->g + 1; i < g->g + g->nr; i++)
+	if (from->trans->locking != from->node_want) {
+		lock_graph_pop_from(g, from);
+		return true;
+	}
+
+	for (i = from + 1; i < g->g + g->nr; i++)
 		if (i->trans->locking != i->node_want ||
 		    i->trans->locking_wait.start_time != i[-1].lock_start_time) {
 			lock_graph_pop_from(g, i);
@@ -184,13 +190,14 @@ static int btree_trans_abort_preference(struct btree_trans *trans)
 	return 3;
 }
 
-static noinline int break_cycle(struct lock_graph *g, struct printbuf *cycle)
+static noinline int break_cycle(struct lock_graph *g, struct printbuf *cycle,
+				struct trans_waiting_for_lock *from)
 {
 	struct trans_waiting_for_lock *i, *abort = NULL;
 	unsigned best = 0, pref;
 	int ret;
 
-	if (lock_graph_remove_non_waiters(g))
+	if (lock_graph_remove_non_waiters(g, from))
 		return 0;
 
 	/* Only checking, for debugfs: */
@@ -200,7 +207,7 @@ static noinline int break_cycle(struct lock_graph *g, struct printbuf *cycle)
 		goto out;
 	}
 
-	for (i = g->g; i < g->g + g->nr; i++) {
+	for (i = from; i < g->g + g->nr; i++) {
 		pref = btree_trans_abort_preference(i->trans);
 		if (pref > best) {
 			abort = i;
@@ -247,7 +254,7 @@ static int lock_graph_descend(struct lock_graph *g, struct btree_trans *trans,
 	for (i = g->g; i < g->g + g->nr; i++)
 		if (i->trans == trans) {
 			closure_put(&trans->ref);
-			return break_cycle(g, cycle);
+			return break_cycle(g, cycle, i);
 		}
 
 	if (g->nr == ARRAY_SIZE(g->g)) {
@@ -339,7 +346,7 @@ int bch2_check_for_deadlock(struct btree_trans *trans, struct printbuf *cycle)
 				 * structures - which means it can't be blocked
 				 * waiting on a lock:
 				 */
-				if (!lock_graph_remove_non_waiters(&g)) {
+				if (!lock_graph_remove_non_waiters(&g, g.g)) {
 					/*
 					 * If lock_graph_remove_non_waiters()
 					 * didn't do anything, it must be
Signature · 1 line
2.45.2
author
#3 lore raw

PATCH 4/4 bcachefs: Pop all the transactions from the abort one

In reply to Alan Huang

The transaction is going to abort, so there will be no cycle involving this transaction anymore.

---
 fs/bcachefs/btree_locking.c | 2 ++
 1 file changed, 2 insertions(+)
fs/bcachefs/btree_locking.c +2 -0
index 70f13ab5ba7b..bf25fd0a1cca 100644
@@ -242,6 +242,8 @@ static noinline int break_cycle(struct lock_graph *g, struct printbuf *cycle,
 out:
 	if (ret)
 		lock_graph_pop_all(g);
+	else
+		lock_graph_pop_from(g, abort);
 	return ret;
 }
 
Signature · 1 line
2.45.2