A variety of lock contention fixes
v2 cover A variety of lock contention fixes
v1->v2:
- Fixed the log messages that Nikolay pointed out.
- Added Nikolay's reviewed by for the first patch.
- Removed the unneeded mb for flushing.
--- Original email ---
Hello,
I've been running some stress tests recently in order to try and reproduce some problems I've tripped over in relocation. Most of this series is a reposting of patches I wrote when debugging related issues for Zygo that got lost. I've updated one of them to make the lock contention even better, making it so I have to ramp up my stress test loops because it now finishes way too fast. Thanks,
Josef
Josef Bacik (6):
btrfs: do not block on deleted bgs mutex in the cleaner
btrfs: only let one thread pre-flush delayed refs in commit
btrfs: delayed refs pre-flushing should only run the heads we have
btrfs: only run delayed refs once before committing
btrfs: stop running all delayed refs during snapshot
btrfs: run delayed refs less often in commit_cowonly_roots
fs/btrfs/block-group.c | 11 +++++-- fs/btrfs/delayed-ref.h | 12 +++---- fs/btrfs/extent-tree.c | 2 +- fs/btrfs/transaction.c | 74 ++++++++++++++++-------------------------- 4 files changed, 43 insertions(+), 56 deletions(-)
Signature · 1 line
2.24.1
PATCH v2 1/6 btrfs: do not block on deleted bgs mutex in the cleaner
While running some stress tests I started getting hung task messages. This is because the delete unused bg's code has to take the delete_unused_bgs_mutex to do it's work, which is taken by balance to make sure we don't delete block groups while we're balancing.
The problem is a balance can take a while, and so we were getting hung task warnings. We don't need to block and run these things, and the cleaner is needed to do other work, so trylock on this mutex and just bail if we can't acquire it right away.
--- fs/btrfs/block-group.c | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-)
fs/btrfs/block-group.c +8 -3
@@ -1333,6 +1333,13 @@ void btrfs_delete_unused_bgs(struct btrfs_fs_info *fs_info)if (!test_bit (BTRFS_FS_OPEN , &fs_info->flags))return ; +/* +* Long running balances can keep us blocked here for eternity, so +* simply skip deletion if we're unable to get the mutex. +*/ +if (!mutex_trylock (&fs_info->delete_unused_bgs_mutex)) +return ; +spin_lock (&fs_info->unused_bgs_lock);while (!list_empty (&fs_info->unused_bgs)) {int trimming;
@@ -1352,8 +1359,6 @@ void btrfs_delete_unused_bgs(struct btrfs_fs_info *fs_info)btrfs_discard_cancel_work (&fs_info->discard_ctl, block_group); -mutex_lock (&fs_info->delete_unused_bgs_mutex); -/* Don't want to race with allocators so take the groups_sem */ down_write (&space_info->groups_sem);
@@ -1499,11 +1504,11 @@ void btrfs_delete_unused_bgs(struct btrfs_fs_info *fs_info) end_trans:btrfs_end_transaction (trans); next: -mutex_unlock (&fs_info->delete_unused_bgs_mutex);btrfs_put_block_group (block_group);spin_lock (&fs_info->unused_bgs_lock); }spin_unlock (&fs_info->unused_bgs_lock); +mutex_unlock (&fs_info->delete_unused_bgs_mutex);return ; flip_async:
Signature · 1 line
2.24.1
PATCH v2 2/6 btrfs: only let one thread pre-flush delayed refs in commit
In reply to Josef BacikI've been running a stress test that runs 20 workers in their own subvolume, which are running an fsstress instance with 4 threads per worker, which is 80 total fsstress threads. In addition to this I'm running balance in the background as well as creating and deleting snapshots. This test takes around 12 hours to run normally, going slower and slower as the test goes on.
The reason for this is because fsstress is running fsync sometimes, and because we're messing with block groups we often fall through to btrfs_commit_transaction, so will often have 20-30 threads all calling btrfs_commit_transaction at the same time.
These all get stuck contending on the extent tree while they try to run delayed refs during the initial part of the commit.
This is suboptimal, really because the extent tree is a single point of failure we only want one thread acting on that tree at once to reduce lock contention. Fix this by making the flushing mechanism a bit operation, to make it easy to use test_and_set_bit() in order to make sure only one task does this initial flush.
Once we're into the transaction commit we only have one thread doing delayed ref running, it's just this initial pre-flush that is problematic. With this patch my stress test takes around 90 minutes to run, instead of 12 hours.
--- fs/btrfs/delayed-ref.h | 12 ++++++------ fs/btrfs/transaction.c | 33 ++++++++++++++++----------------- 2 files changed, 22 insertions(+), 23 deletions(-)
fs/btrfs/delayed-ref.h +6 -6
@@ -135,6 +135,11 @@ struct btrfs_delayed_data_ref {u64 offset; }; +enum btrfs_delayed_ref_flags { +/* Used to indicate that we are flushing delayed refs for the commit. */ +BTRFS_DELAYED_REFS_FLUSHING , +}; +struct btrfs_delayed_ref_root {/* head ref rbtree */ struct rb_root_cached href_root;
@@ -158,12 +163,7 @@ struct btrfs_delayed_ref_root {u64 pending_csums; -/* -* set when the tree is flushing before a transaction commit, -* used by the throttling code to decide if new updates need -* to be run right away -*/ -int flushing; +unsigned long flags;u64 run_delayed_start;
fs/btrfs/transaction.c +16 -17
@@ -870,9 +870,9 @@ int btrfs_should_end_transaction(struct btrfs_trans_handle *trans) {struct btrfs_transaction *cur_trans = trans->transaction; -smp_mb ();if (cur_trans->state >=TRANS_STATE_COMMIT_START || - cur_trans->delayed_refs.flushing) +test_bit (BTRFS_DELAYED_REFS_FLUSHING , + &cur_trans->delayed_refs.flags))return 1 ;return should_end_transaction (trans);
@@ -2042,23 +2042,22 @@ int btrfs_commit_transaction(struct btrfs_trans_handle *trans)btrfs_trans_release_metadata (trans); trans->block_rsv =NULL ; -/* make a pass through all the delayed refs we have so far -* any runnings procs may add more while we are here -*/ - ret =btrfs_run_delayed_refs (trans,0 ); -if (ret) { -btrfs_end_transaction (trans); -return ret; - } - - cur_trans = trans->transaction; -/* -* set the flushing flag so procs in this transaction have to -* start sending their work down. +* We only want one transaction commit doing the flushing so we do not +* waste a bunch of time on lock contention on the extent root node. */ - cur_trans->delayed_refs.flushing =1 ; -smp_wmb (); +if (!test_and_set_bit (BTRFS_DELAYED_REFS_FLUSHING , + &cur_trans->delayed_refs.flags)) { +/* +* make a pass through all the delayed refs we have so far +* any runnings procs may add more while we are here +*/ + ret =btrfs_run_delayed_refs (trans,0 ); +if (ret) { +btrfs_end_transaction (trans); +return ret; + } + }btrfs_create_pending_block_groups (trans);
Signature · 1 line
2.24.1
Re: [PATCH v2 2/6] btrfs: only let one thread pre-flush delayed refs in commit
Quoted 21 lines · Josef Bacik
I've been running a stress test that runs 20 workers in their own subvolume, which are running an fsstress instance with 4 threads per worker, which is 80 total fsstress threads. In addition to this I'm running balance in the background as well as creating and deleting snapshots. This test takes around 12 hours to run normally, going slower and slower as the test goes on.
The reason for this is because fsstress is running fsync sometimes, and because we're messing with block groups we often fall through to
btrfs_commit_transaction, so will often have 20-30 threads all callingbtrfs_commit_transactionat the same time.These all get stuck contending on the extent tree while they try to run delayed refs during the initial part of the commit.
This is suboptimal, really because the extent tree is a single point of failure we only want one thread acting on that tree at once to reduce lock contention. Fix this by making the flushing mechanism a bit operation, to make it easy to use
test_and_set_bit()in order to make sure only one task does this initial flush.
Quoted 6 lines
Once we're into the transaction commit we only have one thread doing delayed ref running, it's just this initial pre-flush that is problematic. With this patch my stress test takes around 90 minutes to run, instead of 12 hours.
Signed-off-by Josef Bacik <[email protected]>
PATCH v2 3/6 btrfs: delayed refs pre-flushing should only run the heads we have
In reply to Josef BacikPreviously our delayed ref running used the total number of items as the items to run. However we changed that to number of heads to run with the delayed_refs_rsv, as generally we want to run all of the operations for one bytenr.
But with btrfs_run_delayed_refs(trans, 0) we set our count to 2x the number of items that we have. This is generally fine, but if we have some operation generation loads of delayed refs while we're doing this pre-flushing in the transaction commit, we'll just spin forever doing delayed refs.
Fix this to simply pick the number of delayed refs we currently have, that way we do not end up doing a lot of extra work that's being generated in other threads.
--- fs/btrfs/extent-tree.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
fs/btrfs/extent-tree.c +1 -1
@@ -2180,7 +2180,7 @@ int btrfs_run_delayed_refs(struct btrfs_trans_handle *trans, delayed_refs = &trans->transaction->delayed_refs;if (count ==0 ) - count =atomic_read (&delayed_refs->num_entries) *2 ; + count = delayed_refs->num_heads_ready; again: #ifdef SCRAMBLE_DELAYED_REFS
Signature · 1 line
2.24.1
Re: [PATCH v2 3/6] btrfs: delayed refs pre-flushing should only run the heads we have
Quoted 11 lines · Josef Bacik
Previously our delayed ref running used the total number of items as the items to run. However we changed that to number of heads to run with the
delayed_refs_rsv, as generally we want to run all of the operations for one bytenr.But with
btrfs_run_delayed_refs(trans, 0)we set our count to 2x the number of items that we have. This is generally fine, but if we have some operation generation loads of delayed refs while we're doing this pre-flushing in the transaction commit, we'll just spin forever doing delayed refs.
Quoted 6 lines
Fix this to simply pick the number of delayed refs we currently have, that way we do not end up doing a lot of extra work that's being generated in other threads.
Signed-off-by Josef Bacik <[email protected]>Reviewed-by Nikolay Borisov <[email protected]>
Turns out this patch regresses on generic/371 so refrain from merging for the time being.
<snip>
PATCH v2 4/6 btrfs: only run delayed refs once before committing
In reply to Josef BacikWe try to pre-flush the delayed refs when committing, because we want to do as little work as possible in the critical section of the transaction commit.
However doing this twice can lead to very long transaction commit delays as other threads are allowed to continue to generate more delayed refs, which potentially delays the commit by multiple minutes in very extreme cases.
So simply stick to one pre-flush, and then continue the rest of the transaction commit.
--- fs/btrfs/transaction.c | 6 ------ 1 file changed, 6 deletions(-)
fs/btrfs/transaction.c +0 -6
@@ -2061,12 +2061,6 @@ int btrfs_commit_transaction(struct btrfs_trans_handle *trans)btrfs_create_pending_block_groups (trans); - ret =btrfs_run_delayed_refs (trans,0 ); -if (ret) { -btrfs_end_transaction (trans); -return ret; - } -if (!test_bit (BTRFS_TRANS_DIRTY_BG_RUN , &cur_trans->flags)) {int run_it =0 ;
Signature · 1 line
2.24.1
PATCH v2 5/6 btrfs: stop running all delayed refs during snapshot
In reply to Josef BacikThis was added a very long time ago to work around issues with delayed refs and with qgroups. Both of these issues have since been properly fixed, so all this does is cause a lot of lock contention with anybody else who is running delayed refs.
--- fs/btrfs/transaction.c | 12 ------------ 1 file changed, 12 deletions(-)
fs/btrfs/transaction.c +0 -12
@@ -1650,12 +1650,6 @@ static noinline int create_pending_snapshot(struct btrfs_trans_handle *trans, goto fail; } - ret =btrfs_run_delayed_refs (trans, (unsigned long )-1 ); -if (ret) { -btrfs_abort_transaction (trans, ret); - goto fail; - } -/* * Do special qgroup accounting for snapshot, as we do some qgroup * snapshot hack to do fast snapshot.
@@ -1703,12 +1697,6 @@ static noinline int create_pending_snapshot(struct btrfs_trans_handle *trans, } } - ret =btrfs_run_delayed_refs (trans, (unsigned long )-1 ); -if (ret) { -btrfs_abort_transaction (trans, ret); - goto fail; - } - fail: pending->error = ret; dir_item_existed:
Signature · 1 line
2.24.1
PATCH v2 6/6 btrfs: run delayed refs less often in commit_cowonly_roots
In reply to Josef BacikWe love running delayed refs in commit_cowonly_roots, but it is a bit excessive. I was seeing cases of running 3 or 4 refs a few times in a row during this time. Instead simply update all of the roots first, then run delayed refs, then handle the empty block groups case, and then if we have any more dirty roots do the whole thing again. This allows us to be much more efficient with our delayed ref running, as we can batch a few more operations at once.
--- fs/btrfs/transaction.c | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-)
fs/btrfs/transaction.c +12 -11
@@ -1188,10 +1188,6 @@ static noinline int commit_cowonly_roots(struct btrfs_trans_handle *trans)btrfs_tree_unlock (eb);free_extent_buffer (eb); -if (ret) -return ret; - - ret =btrfs_run_delayed_refs (trans, (unsigned long )-1 );if (ret)return ret;
@@ -1209,10 +1205,6 @@ static noinline int commit_cowonly_roots(struct btrfs_trans_handle *trans)if (ret)return ret; -/* run_qgroups might have added some more refs */ - ret =btrfs_run_delayed_refs (trans, (unsigned long )-1 ); -if (ret) -return ret; again:while (!list_empty (&fs_info->dirty_cowonly_roots)) {struct btrfs_root *root;
@@ -1227,15 +1219,24 @@ static noinline int commit_cowonly_roots(struct btrfs_trans_handle *trans) ret =update_cowonly_root (trans, root);if (ret)return ret; - ret =btrfs_run_delayed_refs (trans, (unsigned long )-1 ); -if (ret) -return ret; } +/* Now flush any delayed refs generated by updating all of the roots. */ + ret =btrfs_run_delayed_refs (trans, (unsigned long )-1 ); +if (ret) +return ret; +while (!list_empty (dirty_bgs) || !list_empty (io_bgs)) { ret =btrfs_write_dirty_block_groups (trans);if (ret)return ret; + +/* +* We're writing the dirty block groups, which could generate +* delayed refs, which could generate more dirty block groups, +* so we want to keep this flushing in this loop to make sure +* everything gets run. +*/ ret =btrfs_run_delayed_refs (trans, (unsigned long )-1 );if (ret)return ret;
Signature · 1 line
2.24.1