Skip to contentSkip to conversation

btrfs: fix a livelock in wait_for_commit() on PREEMPT_RT

PATCH In review Daniel Vacek 3 participants 3 messages Oct 30 – Nov 2

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 btrfs: fix a livelock in wait_for_commit() on PREEMPT_RT

Peter reported [1] that running multiple RR tasks doing sync() on fully preemptible kernel may starve other/normal sync() tasks due to fs_info::trans_lock turned into rt_mutex.

This is caused by RR tasks contending over the mutex with priority over other tasks, which in turn lack progress and they are no longer able to remove TRANS_STATE_COMPLETED transactions from the fs_info::trans_list.

The offenders start looping in wait_for_commit() expecting the completed transactions to get off the trans_list but since they're bouncing the trans_lock the tasks with other/normal priority which are about to delist such transactions never get a chance to do so due to lower priority.

A simple reproducer can be found in the mentioned email thread [1].

The situation can be resolved by boosting the priority of the starved tasks or dropping priority of the offenders. Thought this fix prevents the situation from happening in the first place. We simply delist the transactions before changing them to completed state. That breaks the loop condition.

[1] https://lore.kernel.org/linux-btrfs/SJ0PR17MB4288C46458DCE2C015839D0FBB9E2@SJ0PR17MB4288.namprd17.prod.outlook.com/

Fixes 5fd76bf31ccfe ("btrfs: fix relocation crash due to premature return from btrfs_commit_transaction()")
---
 fs/btrfs/disk-io.c     | 18 +++++++-----------
 fs/btrfs/transaction.c | 15 +++++----------
 2 files changed, 12 insertions(+), 21 deletions(-)
fs/btrfs/disk-io.c +7 -11
index c628c5081d92..6afcd6ca2a8a 100644
@@ -4312,10 +4312,6 @@ static void warn_about_uncommitted_trans(struct btrfs_fs_info *fs_info)
 			   trans->transid, dirty_bytes);
 		btrfs_cleanup_one_transaction(trans);
 
-		if (trans == fs_info->running_transaction)
-			fs_info->running_transaction = NULL;
-		list_del_init(&trans->list);
-
 		btrfs_put_transaction(trans);
 	}
 	ASSERT(!found);
@@ -4937,7 +4933,13 @@ void btrfs_cleanup_one_transaction(struct btrfs_transaction *cur_trans)
 				     EXTENT_DIRTY);
 	btrfs_destroy_pinned_extent(fs_info, &cur_trans->pinned_extents);
 
-	cur_trans->state =TRANS_STATE_COMPLETED;
+	spin_lock(&fs_info->trans_lock);
+	if (cur_trans == fs_info->running_transaction)
+		fs_info->running_transaction = NULL;
+	list_del_init(&cur_trans->list);
+	spin_unlock(&fs_info->trans_lock);
+
+	cur_trans->state = TRANS_STATE_COMPLETED;
 	wake_up(&cur_trans->commit_wait);
 }
 
@@ -4973,12 +4975,6 @@ static int btrfs_cleanup_transaction(struct btrfs_fs_info *fs_info)
 		}
 		btrfs_cleanup_one_transaction(t);
 
-		spin_lock(&fs_info->trans_lock);
-		if (t == fs_info->running_transaction)
-			fs_info->running_transaction = NULL;
-		list_del_init(&t->list);
-		spin_unlock(&fs_info->trans_lock);
-
 		btrfs_put_transaction(t);
 		spin_lock(&fs_info->trans_lock);
 	}
fs/btrfs/transaction.c +5 -10
index d265fac023e5..cd7dff5ba59b 100644
@@ -2124,12 +2124,7 @@ static void cleanup_transaction(struct btrfs_trans_handle *trans, int err)
 
 	spin_unlock(&fs_info->trans_lock);
 
-	btrfs_cleanup_one_transaction(trans->transaction);
-
-	spin_lock(&fs_info->trans_lock);
-	if (cur_trans == fs_info->running_transaction)
-		fs_info->running_transaction = NULL;
-	spin_unlock(&fs_info->trans_lock);
+	btrfs_cleanup_one_transaction(cur_trans);
 
 	if (trans->type & TRANS_SB_INTWRITER_MASK)
 		sb_end_intwrite(fs_info->sb);
@@ -2641,6 +2636,10 @@ int btrfs_commit_transaction(struct btrfs_trans_handle *trans)
 		btrfs_clear_space_info_full(fs_info);
 
 	btrfs_set_last_trans_committed(fs_info, cur_trans->transid);
+
+	spin_lock(&fs_info->trans_lock);
+	list_del_init(&cur_trans->list);
+	spin_unlock(&fs_info->trans_lock);
 	/*
 	 * We needn't acquire the lock here because there is no other task
 	 * which can change it.
@@ -2650,10 +2649,6 @@ int btrfs_commit_transaction(struct btrfs_trans_handle *trans)
 	wake_up(&cur_trans->commit_wait);
 	btrfs_trans_state_lockdep_release(fs_info, BTRFS_LOCKDEP_TRANS_COMPLETED);
 
-	spin_lock(&fs_info->trans_lock);
-	list_del_init(&cur_trans->list);
-	spin_unlock(&fs_info->trans_lock);
-
 	btrfs_put_transaction(cur_trans);
 	btrfs_put_transaction(cur_trans);
 
Signature · 1 line
2.53.0
Reviewed-by on v1 1/1
#1 lore raw
Quoted 21 lines · Daniel Vacek

Peter reported [1] that running multiple RR tasks doing sync() on fully preemptible kernel may starve other/normal sync() tasks due to fs_info::trans_lock turned into rt_mutex.

This is caused by RR tasks contending over the mutex with priority over other tasks, which in turn lack progress and they are no longer able to remove TRANS_STATE_COMPLETED transactions from the fs_info::trans_list.

The offenders start looping in wait_for_commit() expecting the completed transactions to get off the trans_list but since they're bouncing the trans_lock the tasks with other/normal priority which are about to delist such transactions never get a chance to do so due to lower priority.

A simple reproducer can be found in the mentioned email thread [1].

The situation can be resolved by boosting the priority of the starved tasks or dropping priority of the offenders. Thought this fix prevents the situation from happening in the first place. We simply delist the transactions before changing them to completed state. That breaks the loop condition.

Quoted 7 lines

[1] https://lore.kernel.org/linux-btrfs/SJ0PR17MB4288C46458DCE2C015839D0FBB9E2@SJ0PR17MB4288.namprd17.prod.outlook.com/

Fixes 5fd76bf31ccfe ("btrfs: fix relocation crash due to premature return from btrfs_commit_transaction()")

Thanks,
Qu

Quoted 88 lines
---
  fs/btrfs/disk-io.c     | 18 +++++++-----------
  fs/btrfs/transaction.c | 15 +++++----------
  2 files changed, 12 insertions(+), 21 deletions(-)
fs/btrfs/disk-io.c +7 -11
index c628c5081d92..6afcd6ca2a8a 100644
@@ -4312,10 +4312,6 @@ static void warn_about_uncommitted_trans(struct btrfs_fs_info *fs_info)
  			   trans->transid, dirty_bytes);
  		btrfs_cleanup_one_transaction(trans);
 
-		if (trans == fs_info->running_transaction)
-			fs_info->running_transaction = NULL;
-		list_del_init(&trans->list);
-
  		btrfs_put_transaction(trans);
  	}
  	ASSERT(!found);
@@ -4937,7 +4933,13 @@ void btrfs_cleanup_one_transaction(struct btrfs_transaction *cur_trans)
  				     EXTENT_DIRTY);
  	btrfs_destroy_pinned_extent(fs_info, &cur_trans->pinned_extents);
 
-	cur_trans->state =TRANS_STATE_COMPLETED;
+	spin_lock(&fs_info->trans_lock);
+	if (cur_trans == fs_info->running_transaction)
+		fs_info->running_transaction = NULL;
+	list_del_init(&cur_trans->list);
+	spin_unlock(&fs_info->trans_lock);
+
+	cur_trans->state = TRANS_STATE_COMPLETED;
  	wake_up(&cur_trans->commit_wait);
  }
 
@@ -4973,12 +4975,6 @@ static int btrfs_cleanup_transaction(struct btrfs_fs_info *fs_info)
  		}
  		btrfs_cleanup_one_transaction(t);
 
-		spin_lock(&fs_info->trans_lock);
-		if (t == fs_info->running_transaction)
-			fs_info->running_transaction = NULL;
-		list_del_init(&t->list);
-		spin_unlock(&fs_info->trans_lock);
-
  		btrfs_put_transaction(t);
  		spin_lock(&fs_info->trans_lock);
  	}
fs/btrfs/transaction.c +5 -10
index d265fac023e5..cd7dff5ba59b 100644
@@ -2124,12 +2124,7 @@ static void cleanup_transaction(struct btrfs_trans_handle *trans, int err)
 
  	spin_unlock(&fs_info->trans_lock);
 
-	btrfs_cleanup_one_transaction(trans->transaction);
-
-	spin_lock(&fs_info->trans_lock);
-	if (cur_trans == fs_info->running_transaction)
-		fs_info->running_transaction = NULL;
-	spin_unlock(&fs_info->trans_lock);
+	btrfs_cleanup_one_transaction(cur_trans);
 
  	if (trans->type & TRANS_SB_INTWRITER_MASK)
  		sb_end_intwrite(fs_info->sb);
@@ -2641,6 +2636,10 @@ int btrfs_commit_transaction(struct btrfs_trans_handle *trans)
  		btrfs_clear_space_info_full(fs_info);
 
  	btrfs_set_last_trans_committed(fs_info, cur_trans->transid);
+
+	spin_lock(&fs_info->trans_lock);
+	list_del_init(&cur_trans->list);
+	spin_unlock(&fs_info->trans_lock);
  	/*
  	 * We needn't acquire the lock here because there is no other task
  	 * which can change it.
@@ -2650,10 +2649,6 @@ int btrfs_commit_transaction(struct btrfs_trans_handle *trans)
  	wake_up(&cur_trans->commit_wait);
  	btrfs_trans_state_lockdep_release(fs_info, BTRFS_LOCKDEP_TRANS_COMPLETED);
 
-	spin_lock(&fs_info->trans_lock);
-	list_del_init(&cur_trans->list);
-	spin_unlock(&fs_info->trans_lock);
-
  	btrfs_put_transaction(cur_trans);
  	btrfs_put_transaction(cur_trans);
 
#2 lore raw
In reply to Daniel Vacek
Quoted 3 lines · Daniel Vacek

Peter reported [1] that running multiple RR tasks doing sync() on fully preemptible kernel may starve other/normal sync() tasks due to fs_info::trans_lock turned into rt_mutex.

I don't want to nitpick here but
/fully preemptible/PREEMPT_RT
/RR/SCHED_FIFO or maybe better /RR/real-time priority

Quoted 3 lines

This is caused by RR tasks contending over the mutex with priority over other tasks, which in turn lack progress and they are no longer able to remove TRANS_STATE_COMPLETED transactions from the fs_info::trans_list.

Yes, the important detail is that rt_mutex gives the lock to the task with the highest priority even if it comes last and the long waiting SCHED_OTHER will remain waiting. In contrast to raw_spinlock_t (the spinlock_t on !PREEMPT_RT) will rotate over every waiter.

Quoted 13 lines

The offenders start looping in wait_for_commit() expecting the completed transactions to get off the trans_list but since they're bouncing the trans_lock the tasks with other/normal priority which are about to delist such transactions never get a chance to do so due to lower priority.

A simple reproducer can be found in the mentioned email thread [1].

The situation can be resolved by boosting the priority of the starved tasks or dropping priority of the offenders. Thought this fix prevents the situation from happening in the first place. We simply delist the transactions before changing them to completed state. That breaks the loop condition.

Quoted 7 lines

[1] https://lore.kernel.org/linux-btrfs/SJ0PR17MB4288C46458DCE2C015839D0FBB9E2@SJ0PR17MB4288.namprd17.prod.outlook.com/

Fixes 5fd76bf31ccfe ("btrfs: fix relocation crash due to premature return from btrfs_commit_transaction()")

Thank you for looking into this.

Sebastian