Skip to contentSkip to conversation

ocfs2: avoid potential ABBA deadlock by reordering tl_inode lock

PATCH Superseded Ivan Pravdin 2 participants 3 messages Jul 7, 2025 – Jul 8, 2025 OCFS2

v2 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 ocfs2: avoid potential ABBA deadlock by reordering tl_inode lock

In ocfs2_move_extent(), tl_inode is currently locked after the global bitmap inode. However, in ocfs2_flush_truncate_log(), the lock order is reversed: tl_inode is locked first, followed by the global bitmap inode.

This creates a classic ABBA deadlock scenario if two threads attempt these operations concurrently and acquire the locks in different orders.

To prevent this, move the tl_inode locking earlier in ocfs2_move_extent(), so that it always precedes the global bitmap inode lock.

No functional changes beyond lock ordering.

---
v1 -> v2: Fixed unlocking order in ocfs2_move_extent.

 fs/ocfs2/move_extents.c | 13 +++++++------
 1 file changed, 7 insertions(+), 6 deletions(-)
fs/ocfs2/move_extents.c +7 -6
index 369c7d27befd..aaf8eb2693a4 100644
@@ -617,6 +617,8 @@ static int ocfs2_move_extent(struct ocfs2_move_extents_context *context,
 	 */
 	credits += OCFS2_INODE_UPDATE_CREDITS + 1;
 
+	inode_lock(tl_inode);
+
 	/*
 	 * ocfs2_move_extent() didn't reserve any clusters in lock_allocators()
 	 * logic, while we still need to lock the global_bitmap.
@@ -637,13 +639,11 @@ static int ocfs2_move_extent(struct ocfs2_move_extents_context *context,
 		goto out_unlock_gb_mutex;
 	}
 
-	inode_lock(tl_inode);
-
 	handle = ocfs2_start_trans(osb, credits);
 	if (IS_ERR(handle)) {
 		ret = PTR_ERR(handle);
 		mlog_errno(ret);
-		goto out_unlock_tl_inode;
+		goto out_unlock_gb_inode;
 	}
 
 	new_phys_blkno = ocfs2_clusters_to_blocks(inode->i_sb, *new_phys_cpos);
@@ -704,12 +704,13 @@ static int ocfs2_move_extent(struct ocfs2_move_extents_context *context,
 	ocfs2_commit_trans(osb, handle);
 	brelse(gd_bh);
 
-out_unlock_tl_inode:
-	inode_unlock(tl_inode);
-
+out_unlock_gb_inode:
 	ocfs2_inode_unlock(gb_inode, 1);
+
 out_unlock_gb_mutex:
 	inode_unlock(gb_inode);
+
+	inode_unlock(tl_inode);
 	brelse(gb_bh);
 	iput(gb_inode);
 
Signature · 1 line
2.45.2
maintainer
#1 lore raw
Quoted 37 lines · Ivan Pravdin

In ocfs2_move_extent(), tl_inode is currently locked after the global bitmap inode. However, in ocfs2_flush_truncate_log(), the lock order is reversed: tl_inode is locked first, followed by the global bitmap inode.

This creates a classic ABBA deadlock scenario if two threads attempt these operations concurrently and acquire the locks in different orders.

To prevent this, move the tl_inode locking earlier in ocfs2_move_extent(), so that it always precedes the global bitmap inode lock.

No functional changes beyond lock ordering.

---
v1 -> v2: Fixed unlocking order in ocfs2_move_extent.

 fs/ocfs2/move_extents.c | 13 +++++++------
 1 file changed, 7 insertions(+), 6 deletions(-)
fs/ocfs2/move_extents.c +2 -0
index 369c7d27befd..aaf8eb2693a4 100644
@@ -617,6 +617,8 @@ static int ocfs2_move_extent(struct ocfs2_move_extents_context *context,
 	 */
 	credits += OCFS2_INODE_UPDATE_CREDITS + 1;
 
+	inode_lock(tl_inode);
+
 	/*
 	 * ocfs2_move_extent() didn't reserve any clusters in lock_allocators()
 	 * logic, while we still need to lock the global_bitmap.

fs/ocfs2/move_extents.c static int ocfs2_move_extent(struct ocfs2_move_extents_context *context, · line 641

Quoted 3 lines
fs/ocfs2/move_extents.c +0 -0
@@ -637,13 +639,11 @@ static int ocfs2_move_extent(struct ocfs2_move_extents_context *context,
 		goto out_unlock_gb_mutex;
 	}
 

You've missed unlock tl_inode when ocfs2_get_system_file_inode() fails.

Quoted 12 lines
fs/ocfs2/move_extents.c +1 -3
-	inode_lock(tl_inode);
-
 	handle = ocfs2_start_trans(osb, credits);
 	if (IS_ERR(handle)) {
 		ret = PTR_ERR(handle);
 		mlog_errno(ret);
-		goto out_unlock_tl_inode;
+		goto out_unlock_gb_inode;
 	}
 
 	new_phys_blkno = ocfs2_clusters_to_blocks(inode->i_sb, *new_phys_cpos);

fs/ocfs2/move_extents.c static int ocfs2_move_extent(struct ocfs2_move_extents_context *context, · line 710

Quoted 10 lines
fs/ocfs2/move_extents.c +2 -3
@@ -704,12 +704,13 @@ static int ocfs2_move_extent(struct ocfs2_move_extents_context *context,
 	ocfs2_commit_trans(osb, handle);
 	brelse(gd_bh);
 
-out_unlock_tl_inode:
-	inode_unlock(tl_inode);
-
+out_unlock_gb_inode:
 	ocfs2_inode_unlock(gb_inode, 1);
+
 out_unlock_gb_mutex:

inode lock has changed to rw_semaphore, so 'mutex' is no longer proper.

fs/ocfs2/move_extents.c static int ocfs2_move_extent(struct ocfs2_move_extents_context *context, · line 716

Quoted 6 lines
fs/ocfs2/move_extents.c +2 -0
 	inode_unlock(gb_inode);
+
+	inode_unlock(tl_inode);
 	brelse(gb_bh);
 	iput(gb_inode);
 

How about the following alternative:

fs/ocfs2/move_extents.c +9 -11
index 369c7d27befd..d56c337204f6 100644
@@ -611,6 +611,8 @@ static int ocfs2_move_extent(struct ocfs2_move_extents_context *context,
 		goto out;
 	}
 
+	inode_lock(tl_inode);
+
 	/*
 	 * need to count 2 extra credits for global_bitmap inode and
 	 * group descriptor.
@@ -626,7 +628,7 @@ static int ocfs2_move_extent(struct ocfs2_move_extents_context *context,
 	if (!gb_inode) {
 		mlog(ML_ERROR, "unable to get global_bitmap inode\n");
 		ret = -EIO;
-		goto out;
+		goto out_unlock_tl_inode;
 	}
 
 	inode_lock(gb_inode);
@@ -634,16 +636,14 @@ static int ocfs2_move_extent(struct ocfs2_move_extents_context *context,
 	ret = ocfs2_inode_lock(gb_inode, &gb_bh, 1);
 	if (ret) {
 		mlog_errno(ret);
-		goto out_unlock_gb_mutex;
+		goto out_unlock_gb_inode;
 	}
 
-	inode_lock(tl_inode);
-
 	handle = ocfs2_start_trans(osb, credits);
 	if (IS_ERR(handle)) {
 		ret = PTR_ERR(handle);
 		mlog_errno(ret);
-		goto out_unlock_tl_inode;
+		goto out_unlock;
 	}
 
 	new_phys_blkno = ocfs2_clusters_to_blocks(inode->i_sb, *new_phys_cpos);
@@ -703,16 +703,14 @@ static int ocfs2_move_extent(struct ocfs2_move_extents_context *context,
 out_commit:
 	ocfs2_commit_trans(osb, handle);
 	brelse(gd_bh);
-
-out_unlock_tl_inode:
-	inode_unlock(tl_inode);
-
+out_unlock:
 	ocfs2_inode_unlock(gb_inode, 1);
-out_unlock_gb_mutex:
+out_unlock_gb_inode:
 	inode_unlock(gb_inode);
 	brelse(gb_bh);
 	iput(gb_inode);
-
+out_unlock_tl_inode:
+	inode_unlock(tl_inode);
 out:
 	if (context->meta_ac) {
 		ocfs2_free_alloc_context(context->meta_ac);
#2 lore raw
Quoted 9 lines · Joseph Qi

Review static int ocfs2_move_extent(struct ocfs2_move_extents_context *context, · line 641

Quoted 7 lines
(quoted diff) +0 -0
 	/*
 	 * ocfs2_move_extent() didn't reserve any clusters in lock_allocators()
 	 * logic, while we still need to lock the global_bitmap.
@@ -637,13 +639,11 @@ static int ocfs2_move_extent(struct ocfs2_move_extents_context *context,
 		goto out_unlock_gb_mutex;
 	}
 

You've missed unlock tl_inode when ocfs2_get_system_file_inode() fails.

I will add it, thanks.

Quoted 90 lines
Quoted 14 lines
fs/ocfs2/move_extents.c +1 -3
-	inode_lock(tl_inode);
-
 	handle = ocfs2_start_trans(osb, credits);
 	if (IS_ERR(handle)) {
 		ret = PTR_ERR(handle);
 		mlog_errno(ret);
-		goto out_unlock_tl_inode;
+		goto out_unlock_gb_inode;
 	}
 
 	new_phys_blkno = ocfs2_clusters_to_blocks(inode->i_sb, *new_phys_cpos);

fs/ocfs2/move_extents.c static int ocfs2_move_extent(struct ocfs2_move_extents_context *context, · line 710

Quoted 10 lines
fs/ocfs2/move_extents.c +2 -3
@@ -704,12 +704,13 @@ static int ocfs2_move_extent(struct ocfs2_move_extents_context *context,
 	ocfs2_commit_trans(osb, handle);
 	brelse(gd_bh);
 
-out_unlock_tl_inode:
-	inode_unlock(tl_inode);
-
+out_unlock_gb_inode:
 	ocfs2_inode_unlock(gb_inode, 1);
+
 out_unlock_gb_mutex:

inode lock has changed to rw_semaphore, so 'mutex' is no longer proper.

fs/ocfs2/move_extents.c static int ocfs2_move_extent(struct ocfs2_move_extents_context *context,

Quoted 6 lines
fs/ocfs2/move_extents.c +2 -0
 	inode_unlock(gb_inode);
+
+	inode_unlock(tl_inode);
 	brelse(gb_bh);
 	iput(gb_inode);
 

How about the following alternative:

fs/ocfs2/move_extents.c +6 -10
index 369c7d27befd..d56c337204f6 100644
@@ -611,6 +611,8 @@ static int ocfs2_move_extent(struct ocfs2_move_extents_context *context,
 		goto out;
 	}
 
+	inode_lock(tl_inode);
+
 	/*
 	 * need to count 2 extra credits for global_bitmap inode and
 	 * group descriptor.
@@ -626,7 +628,7 @@ static int ocfs2_move_extent(struct ocfs2_move_extents_context *context,
 	if (!gb_inode) {
 		mlog(ML_ERROR, "unable to get global_bitmap inode\n");
 		ret = -EIO;
-		goto out;
+		goto out_unlock_tl_inode;
 	}
 
 	inode_lock(gb_inode);
@@ -634,16 +636,14 @@ static int ocfs2_move_extent(struct ocfs2_move_extents_context *context,
 	ret = ocfs2_inode_lock(gb_inode, &gb_bh, 1);
 	if (ret) {
 		mlog_errno(ret);
-		goto out_unlock_gb_mutex;
+		goto out_unlock_gb_inode;
 	}
 
-	inode_lock(tl_inode);
-
 	handle = ocfs2_start_trans(osb, credits);
 	if (IS_ERR(handle)) {
 		ret = PTR_ERR(handle);
 		mlog_errno(ret);
-		goto out_unlock_tl_inode;
+		goto out_unlock;
 	}
 
 	new_phys_blkno = ocfs2_clusters_to_blocks(inode->i_sb, *new_phys_cpos);
@@ -703,16 +703,14 @@ static int ocfs2_move_extent(struct ocfs2_move_extents_context *context,
 out_commit:
 	ocfs2_commit_trans(osb, handle);
 	brelse(gd_bh);
-
-out_unlock_tl_inode:
-	inode_unlock(tl_inode);
-
+out_unlock:
 	ocfs2_inode_unlock(gb_inode, 1);
-out_unlock_gb_mutex:

fs/ocfs2/move_extents.c static int ocfs2_move_extent(struct ocfs2_move_extents_context *context, · line 716

Quoted 10 lines
fs/ocfs2/move_extents.c +3 -1
+out_unlock_gb_inode:
 	inode_unlock(gb_inode);
 	brelse(gb_bh);
 	iput(gb_inode);
-
+out_unlock_tl_inode:
+	inode_unlock(tl_inode);
 out:
 	if (context->meta_ac) {
 		ocfs2_free_alloc_context(context->meta_ac);

This is definitely better. I will update the patch, thank you.

Ivan Pravdin