]> git.hungrycats.org Git - linux/commitdiff
btrfs: fix a livelock in wait_for_commit() on PREEMPT_RT
authorDaniel Vacek <neelx@suse.com>
Fri, 30 Oct 2026 14:28:03 +0000 (15:28 +0100)
committerDaniel Vacek <neelx@suse.com>
Sat, 31 Oct 2026 08:07:09 +0000 (09:07 +0100)
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/

Reported-by: Naulls, Peter <PeterNaulls@eaton.com>
Fixes: 5fd76bf31ccfe ("btrfs: fix relocation crash due to premature return from btrfs_commit_transaction()")
CC: stable@vger.kernel.org # 5.15+
Tested-by: Naulls, Peter <PeterNaulls@eaton.com>
Reviewed-by: Qu Wenruo <wqu@suse.com>
Signed-off-by: Daniel Vacek <neelx@suse.com>
fs/btrfs/disk-io.c
fs/btrfs/transaction.c

index c628c5081d923856b28f73d66d114478dc1f72d2..6afcd6ca2a8a83f26aeb2279aaac0802b15afce9 100644 (file)
@@ -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);
        }
index d265fac023e58ebe110c8703b2adcd167c2992fb..cd7dff5ba59b6d48ec97f4d9b404dbad91068928 100644 (file)
@@ -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);