Skip to contentSkip to conversation

Btrfs: fix deadlock when throttling transactions

PATCH In review Josef Bacik 2 participants 3 messages Jul 14, 2011 – Jul 15, 2011 Btrfs file system

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Btrfs: fix deadlock when throttling transactions

Hit this nice little deadlock. What happens is this

__btrfs_end_transaction with throttle set, --use_count so it equals 0
btrfs_commit_transaction
<somebody else actually manages to start the commit> btrfs_end_transaction --use_count so now its -1 <== BAD
we just return and wait on the transaction

This is bad because we just return after our use_count is -1 and don't let go of our num_writer count on the transaction, so the guy committing the transaction just sits there forever. Fix this by inc'ing our use_count if we're going to call commit_transaction so that if we call btrfs_end_transaction it's valid. Thanks,

---
 fs/btrfs/transaction.c |   13 ++++++++++---
 1 files changed, 10 insertions(+), 3 deletions(-)
fs/btrfs/transaction.c +10 -3
index 654755b..00b81fb5 100644
@@ -497,10 +497,17 @@ static int __btrfs_end_transaction(struct btrfs_trans_handle *trans,
 	}
 
 	if (lock && cur_trans->blocked && !cur_trans->in_commit) {
-		if (throttle)
+		if (throttle) {
+			/*
+			 * We may race with somebody else here so end up having
+			 * to call end_transaction on ourselves again, so inc
+			 * our use_count.
+			 */
+			trans->use_count++;
 			return btrfs_commit_transaction(trans, root);
-		else
+		} else {
 			wake_up_process(info->transaction_kthread);
+		}
 	}
 
 	WARN_ON(cur_trans != info->running_transaction);
@@ -1225,7 +1232,7 @@ int btrfs_commit_transaction(struct btrfs_trans_handle *trans,
 	if (cur_trans->in_commit) {
 		spin_unlock(&cur_trans->commit_lock);
 		atomic_inc(&cur_trans->use_count);
-		btrfs_end_transaction(trans, root);
+		__btrfs_end_transaction(trans, root, 0, 1);
 
 		ret = wait_for_commit(root, cur_trans);
 		BUG_ON(ret);
Signature · 1 line
1.7.5.2
#1 lore raw
Quoted 44 lines · Josef Bacik

Hit this nice little deadlock. What happens is this

__btrfs_end_transaction with throttle set, --use_count so it equals 0
btrfs_commit_transaction
<somebody else actually manages to start the commit> btrfs_end_transaction --use_count so now its -1 <== BAD
we just return and wait on the transaction

This is bad because we just return after our use_count is -1 and don't let go of our num_writer count on the transaction, so the guy committing the transaction just sits there forever. Fix this by inc'ing our use_count if we're going to call commit_transaction so that if we call btrfs_end_transaction it's valid. Thanks,

---
 fs/btrfs/transaction.c |   13 ++++++++++---
 1 files changed, 10 insertions(+), 3 deletions(-)
fs/btrfs/transaction.c +9 -2
index 654755b..00b81fb5 100644
@@ -497,10 +497,17 @@ static int __btrfs_end_transaction(struct btrfs_trans_handle *trans,
 	}
 
 	if (lock && cur_trans->blocked && !cur_trans->in_commit) {
-		if (throttle)
+		if (throttle) {
+			/*
+			 * We may race with somebody else here so end up having
+			 * to call end_transaction on ourselves again, so inc
+			 * our use_count.
+			 */
+			trans->use_count++;
 			return btrfs_commit_transaction(trans, root);
-		else
+		} else {
 			wake_up_process(info->transaction_kthread);
+		}
 	}
 
 	WARN_ON(cur_trans != info->running_transaction);

fs/btrfs/transaction.c int btrfs_commit_transaction(struct btrfs_trans_handle *trans, · line 1236

Quoted 6 lines
fs/btrfs/transaction.c +1 -1
@@ -1225,7 +1232,7 @@ int btrfs_commit_transaction(struct btrfs_trans_handle *trans,
 	if (cur_trans->in_commit) {
 		spin_unlock(&cur_trans->commit_lock);
 		atomic_inc(&cur_trans->use_count);
-		btrfs_end_transaction(trans, root);
+		__btrfs_end_transaction(trans, root, 0, 1);
 

Looks good.

BTW, btrfs_end_transaction(trans, root) is just __btrfs_end_transaction(trans, root, 0, 1).

thanks,
liubo

Quoted 2 lines
fs/btrfs/transaction.c +0 -0
 		ret = wait_for_commit(root, cur_trans);
 		BUG_ON(ret);
#2 lore raw
Quoted 54 lines · liubo
Quoted 44 lines · Josef Bacik

Hit this nice little deadlock. What happens is this

__btrfs_end_transaction with throttle set, --use_count so it equals 0
btrfs_commit_transaction
<somebody else actually manages to start the commit> btrfs_end_transaction --use_count so now its -1 <== BAD
we just return and wait on the transaction

This is bad because we just return after our use_count is -1 and don't let go of our num_writer count on the transaction, so the guy committing the transaction just sits there forever. Fix this by inc'ing our use_count if we're going to call commit_transaction so that if we call btrfs_end_transaction it's valid. Thanks,

---
 fs/btrfs/transaction.c |   13 ++++++++++---
 1 files changed, 10 insertions(+), 3 deletions(-)
fs/btrfs/transaction.c +9 -2
index 654755b..00b81fb5 100644
@@ -497,10 +497,17 @@ static int __btrfs_end_transaction(struct btrfs_trans_handle *trans,
 	}
 
 	if (lock && cur_trans->blocked && !cur_trans->in_commit) {
-		if (throttle)
+		if (throttle) {
+			/*
+			 * We may race with somebody else here so end up having
+			 * to call end_transaction on ourselves again, so inc
+			 * our use_count.
+			 */
+			trans->use_count++;
 			return btrfs_commit_transaction(trans, root);
-		else
+		} else {
 			wake_up_process(info->transaction_kthread);
+		}
 	}
 
 	WARN_ON(cur_trans != info->running_transaction);

fs/btrfs/transaction.c int btrfs_commit_transaction(struct btrfs_trans_handle *trans, · line 1236

Quoted 6 lines
fs/btrfs/transaction.c +1 -1
@@ -1225,7 +1232,7 @@ int btrfs_commit_transaction(struct btrfs_trans_handle *trans,
 	if (cur_trans->in_commit) {
 		spin_unlock(&cur_trans->commit_lock);
 		atomic_inc(&cur_trans->use_count);
-		btrfs_end_transaction(trans, root);
+		__btrfs_end_transaction(trans, root, 0, 1);
 

Looks good.

Quoted 2 lines

BTW, btrfs_end_transaction(trans, root) is just __btrfs_end_transaction(trans, root, 0, 1).

Oops you're right, I saw the 1 for lock and thought it was for throttle.
Thanks,

Josef