Skip to contentSkip to conversation

Fix up some stupid delayed ref flushing behaviors

PATCH In review Josef Bacik 4 participants 14 messages Mar 13, 2020 – Apr 3, 2020 Btrfs file system

lore
Share & tools
lore
#0 lore raw

cover Fix up some stupid delayed ref flushing behaviors

While debugging Zygo's delayed ref problems it was clear there were a bunch of cases that we're running delayed refs when we don't need to be, and they result in a lot of weird latencies.

Each patch has their individual explanations. But the gist of it is we run delayed refs in a lot of arbitrary ways that have just accumulated throughout the years, so clean up all of these so we can have more consistent performance. Thanks,

Josef

#1 lore raw

PATCH 1/5 btrfs: set delayed_refs.flushing for the first delayed ref flushing

We need to stop any other delayed refs flushing operations from happening if we're flushing delayed refs for transaction commit as we don't want the lock contention to make everything go slower, especially the transaction commit.

---
 fs/btrfs/transaction.c | 14 +++++++-------
 1 file changed, 7 insertions(+), 7 deletions(-)
fs/btrfs/transaction.c +7 -7
index 53af0f55f5f9..cff767722a75 100644
@@ -2037,6 +2037,13 @@ int btrfs_commit_transaction(struct btrfs_trans_handle *trans)
 	btrfs_trans_release_metadata(trans);
 	trans->block_rsv = NULL;
 
+	/*
+	 * set the flushing flag so procs in this transaction have to
+	 * start sending their work down.
+	 */
+	cur_trans->delayed_refs.flushing = 1;
+	smp_wmb();
+
 	/* make a pass through all the delayed refs we have so far
 	 * any runnings procs may add more while we are here
 	 */
@@ -2048,13 +2055,6 @@ int btrfs_commit_transaction(struct btrfs_trans_handle *trans)
 
 	cur_trans = trans->transaction;
 
-	/*
-	 * set the flushing flag so procs in this transaction have to
-	 * start sending their work down.
-	 */
-	cur_trans->delayed_refs.flushing = 1;
-	smp_wmb();
-
 	btrfs_create_pending_block_groups(trans);
 
 	ret = btrfs_run_delayed_refs(trans, 0);
Signature · 1 line
2.24.1
#2 lore raw

PATCH 2/5 btrfs: delayed refs pre-flushing should only run the heads we have

In reply to 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.

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
index 8e5b49baad98..2925b3ad77a1 100644
@@ -2196,7 +2196,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
Reviewed-by on v1 2/5
#3 lore raw

Re: [PATCH 2/5] 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 3 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.

Indeed there is a mismatch between delayed_refs->num_entries and what we account in __btrfs_run_delayed_refs. In the function we count on a per-head (which can include multiple delayed refs ops) granularity not on per-refs-per-head. So this fix makes sense.

Quoted 20 lines
---
 fs/btrfs/extent-tree.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
fs/btrfs/extent-tree.c +1 -1
index 8e5b49baad98..2925b3ad77a1 100644
@@ -2196,7 +2196,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
#4 lore raw

PATCH 3/5 btrfs: only run delayed refs once before committing

In reply to Josef Bacik

We 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
index cff767722a75..3e7fd8a934c1 100644
@@ -2057,12 +2057,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
Reviewed-by on v1 3/5
#5 lore raw

Re: [PATCH 3/5] btrfs: only run delayed refs once before committing

Quoted 9 lines · Josef Bacik

We 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.

Quoted 4 lines

So simply stick to one pre-flush, and then continue the rest of the transaction commit.

Quoted 22 lines
---
 fs/btrfs/transaction.c | 6 ------
 1 file changed, 6 deletions(-)
fs/btrfs/transaction.c +0 -6
index cff767722a75..3e7fd8a934c1 100644
@@ -2057,12 +2057,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;
 
#6 lore raw

PATCH 4/5 btrfs: run delayed refs less often in commit_cowonly_roots

In reply to Josef Bacik

We 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 | 12 ------------
 1 file changed, 12 deletions(-)
fs/btrfs/transaction.c +0 -12
index 3e7fd8a934c1..c3b3b524b8c3 100644
@@ -1646,12 +1646,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.
@@ -1698,12 +1692,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
#7 lore raw

Re: [PATCH 4/5] btrfs: run delayed refs less often in commit_cowonly_roots

Quoted 1 line · Josef Bacik

We love running delayed refs in commit_cowonly_roots, but it is a bit

You refer to commit_cowonly_roots but in fact are changing create_pending_snapshot. Between calling create_pending_snapshots->create_pending_snapshot and commit_cowonly_roots we have 2 calls to btrfs_run_delayed_refs. My point is by the time commit_cowonly_roots is called we've already long flushed the delayed refs generated from create_pending_snapshot. IMO referring to commit_cowonly_roots in this commit is wrong?

Quoted 2 lines

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,

By "simply update all of the roots" I assume you meant the call to commit_fs_roots or the changes happening to the snapshoted roots in create_pending_snapshot? If it's the latter I'd rather you made the text more explicit by referring to the fact the refs are generated from snapshots e.g.

Instead simply run all snapshot-related work, then drain delayed refs resulting from this ...

Quoted 41 lines

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 | 12 ------------
 1 file changed, 12 deletions(-)
fs/btrfs/transaction.c +0 -12
index 3e7fd8a934c1..c3b3b524b8c3 100644
@@ -1646,12 +1646,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.
@@ -1698,12 +1692,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:
#8 lore raw

PATCH 5/5 btrfs: stop running all delayed refs during snapshot

In reply to Josef Bacik

This 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 | 23 ++++++++++++-----------
 1 file changed, 12 insertions(+), 11 deletions(-)
fs/btrfs/transaction.c +12 -11
index c3b3b524b8c3..8d34d7e0adb6 100644
@@ -1184,10 +1184,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;
 
@@ -1205,10 +1201,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;
@@ -1223,15 +1215,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
#9 lore raw

Re: [PATCH 5/5] btrfs: stop running all delayed refs during snapshot

Quoted 4 lines · Josef Bacik

This 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.

Oh I see, the changelog for this and the previous patch got switched, no ?

Quoted 61 lines
---
 fs/btrfs/transaction.c | 23 ++++++++++++-----------
 1 file changed, 12 insertions(+), 11 deletions(-)
fs/btrfs/transaction.c +12 -11
index c3b3b524b8c3..8d34d7e0adb6 100644
@@ -1184,10 +1184,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;
 
@@ -1205,10 +1201,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;
@@ -1223,15 +1215,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;
#10 lore raw
In reply to Josef Bacik
Quoted 7 lines · Josef Bacik

While debugging Zygo's delayed ref problems it was clear there were a bunch of cases that we're running delayed refs when we don't need to be, and they result in a lot of weird latencies.

Each patch has their individual explanations. But the gist of it is we run delayed refs in a lot of arbitrary ways that have just accumulated throughout the years, so clean up all of these so we can have more consistent performance.

It would be fine to remove the delayed refs being run from so many places but I vaguely remember some patches adding them with "we have to run delayed refs here or we will miss something and that would be a corruption". The changelogs in patches from 3 on don't point out any specific problems and I miss some reasoning about correctness, ideally for each line of btrfs_run_delayed_refs removed.

As a worst case I really don't want to get to a situation where we start getting reports that something broke because of the missing delayed refs, followed by series of "oh yeah I forgot we need it here, add it back".

The branch with this patchset is in for-next but I'm still not comfortable with adding it to misc-next as I can't convince myself it's safe, so more reviews are welcome.

#11 lore raw
Quoted 16 lines · David Sterba
Quoted 7 lines · Josef Bacik

While debugging Zygo's delayed ref problems it was clear there were a bunch of cases that we're running delayed refs when we don't need to be, and they result in a lot of weird latencies.

Each patch has their individual explanations. But the gist of it is we run delayed refs in a lot of arbitrary ways that have just accumulated throughout the years, so clean up all of these so we can have more consistent performance.

It would be fine to remove the delayed refs being run from so many places but I vaguely remember some patches adding them with "we have to run delayed refs here or we will miss something and that would be a corruption". The changelogs in patches from 3 on don't point out any specific problems and I miss some reasoning about correctness, ideally for each line of btrfs_run_delayed_refs removed.

Quoted 4 lines

As a worst case I really don't want to get to a situation where we start getting reports that something broke because of the missing delayed refs, followed by series of "oh yeah I forgot we need it here, add it back".

Yeah I went through and checked each of these spots to see why we had them. A lot of it had to do with how poorly delayed refs were run previously. You could end up with weird ordering cases and missing our flags.

These problems are all gone now, we no longer have to run delayed refs to work around ordering weirdness because I fixed all of those problems. Now these are just old relics of the past that need to die. The only case where I didn't touch them is for qgroups, likely because it still matters for the before/after lookups there.

But everywhere else it was working around some deficiency in how we ran delayed refs, either in the ordering issues or space related. Both those problems no longer exist, so we can drop these workarounds.

Quoted 5 lines

The branch with this patchset is in for-next but I'm still not comfortable with adding it to misc-next as I can't convince myself it's safe, so more reviews are welcome.

Yeah I'm targeting the merge window after the upcoming one with these, there's still a lot more testing I want to get done. I mostly threw them up because they were no longer blowing up constantly for Zygo, and I wanted Filipe to get an early look at them. Thanks,

Josef

#12 lore raw
Quoted 42 lines · Josef Bacik
Quoted 16 lines · David Sterba
Quoted 7 lines · Josef Bacik

While debugging Zygo's delayed ref problems it was clear there were a bunch of cases that we're running delayed refs when we don't need to be, and they result in a lot of weird latencies.

Each patch has their individual explanations. But the gist of it is we run delayed refs in a lot of arbitrary ways that have just accumulated throughout the years, so clean up all of these so we can have more consistent performance.

It would be fine to remove the delayed refs being run from so many places but I vaguely remember some patches adding them with "we have to run delayed refs here or we will miss something and that would be a corruption". The changelogs in patches from 3 on don't point out any specific problems and I miss some reasoning about correctness, ideally for each line of btrfs_run_delayed_refs removed.

Quoted 4 lines

As a worst case I really don't want to get to a situation where we start getting reports that something broke because of the missing delayed refs, followed by series of "oh yeah I forgot we need it here, add it back".

Yeah I went through and checked each of these spots to see why we had them. A lot of it had to do with how poorly delayed refs were run previously. You could end up with weird ordering cases and missing our flags.

These problems are all gone now, we no longer have to run delayed refs to work around ordering weirdness because I fixed all of those problems. Now these are just old relics of the past that need to die. The only case where I didn't touch them is for qgroups, likely because it still matters for the before/after lookups there.

But everywhere else it was working around some deficiency in how we ran delayed refs, either in the ordering issues or space related. Both those problems no longer exist, so we can drop these workarounds.

Quoted 5 lines

The branch with this patchset is in for-next but I'm still not comfortable with adding it to misc-next as I can't convince myself it's safe, so more reviews are welcome.

Quoted 4 lines

Yeah I'm targeting the merge window after the upcoming one with these, there's still a lot more testing I want to get done. I mostly threw them up because they were no longer blowing up constantly for Zygo, and I wanted Filipe to get an early look at them. Thanks,

No longer blowing up _constantly_, but there was definitely a 2-3 day cadence between blowups last time I rebased. Test runs were ending in splats due to KASAN UAF bugs and bad unlock balances. It doesn't seem to be corrupting on-disk metadata, but my test VMs can't get anywhere close to a week uptime under the full stress load yet.

I'd like to keep a test VM pointed at this as it makes it way upstream. It's an important set of changes, but it has a high regression risk. There are some big changes here, and that's going to expose all the gaps in developers' knowledge of how stuff really works.

Do I just keep rebasing on for-next-<date>?

Quoted 1 line

Josef

#13 lore raw
Quoted 54 lines · Zygo Blaxell
Quoted 42 lines · Josef Bacik
Quoted 16 lines · David Sterba
Quoted 7 lines · Josef Bacik

While debugging Zygo's delayed ref problems it was clear there were a bunch of cases that we're running delayed refs when we don't need to be, and they result in a lot of weird latencies.

Each patch has their individual explanations. But the gist of it is we run delayed refs in a lot of arbitrary ways that have just accumulated throughout the years, so clean up all of these so we can have more consistent performance.

It would be fine to remove the delayed refs being run from so many places but I vaguely remember some patches adding them with "we have to run delayed refs here or we will miss something and that would be a corruption". The changelogs in patches from 3 on don't point out any specific problems and I miss some reasoning about correctness, ideally for each line of btrfs_run_delayed_refs removed.

Quoted 4 lines

As a worst case I really don't want to get to a situation where we start getting reports that something broke because of the missing delayed refs, followed by series of "oh yeah I forgot we need it here, add it back".

Yeah I went through and checked each of these spots to see why we had them. A lot of it had to do with how poorly delayed refs were run previously. You could end up with weird ordering cases and missing our flags.

These problems are all gone now, we no longer have to run delayed refs to work around ordering weirdness because I fixed all of those problems. Now these are just old relics of the past that need to die. The only case where I didn't touch them is for qgroups, likely because it still matters for the before/after lookups there.

But everywhere else it was working around some deficiency in how we ran delayed refs, either in the ordering issues or space related. Both those problems no longer exist, so we can drop these workarounds.

Quoted 5 lines

The branch with this patchset is in for-next but I'm still not comfortable with adding it to misc-next as I can't convince myself it's safe, so more reviews are welcome.

Quoted 4 lines

Yeah I'm targeting the merge window after the upcoming one with these, there's still a lot more testing I want to get done. I mostly threw them up because they were no longer blowing up constantly for Zygo, and I wanted Filipe to get an early look at them. Thanks,

No longer blowing up _constantly_, but there was definitely a 2-3 day cadence between blowups last time I rebased. Test runs were ending in splats due to KASAN UAF bugs and bad unlock balances. It doesn't seem to be corrupting on-disk metadata, but my test VMs can't get anywhere close to a week uptime under the full stress load yet.

Quoted 7 lines

I'd like to keep a test VM pointed at this as it makes it way upstream. It's an important set of changes, but it has a high regression risk. There are some big changes here, and that's going to expose all the gaps in developers' knowledge of how stuff really works.

Do I just keep rebasing on for-next-<date>?

I think so, I'm not sure what Dave has merged so far. My own long term tests have uncovered a few bugs that I've been busy running down. Once my long term tests no longer fall over I'm going to rebase everything onto the most recent devel branch and we can go from there. Hopefully I'll have this last corner run down by tomorrow or early next week. Thanks,

Josef