->total_bytes_pinned fixes for early ENOSPC issues
v2 cover ->total_bytes_pinned fixes for early ENOSPC issues
v1->v2:
- Rebase onto latest misc-next.
- Added Nikolay's reviewed-by for the first patch.
--- Original email ---
Hello,
Nikolay discovered a regression with generic/371 with my delayed refs patches applied. This isn't actually a regression caused by those patches, but rather those patches uncovered a problem that has existed forever, we just have papered over it with our aggressive delayed ref flushing. Enter these two patches.
The first patch is more of a refactoring and a simplification. We've been passing in a &old_refs and a &new_refs into the delayed ref code, and duplicating the
if (old_refs >= 0 && new_refs < 0)
->total_bytes_pinned += bytes; else if (old_refs < 0 && new_refs >= 0) ->total_bytes_pinned -= bytes;
logic for data and metadata. This was made even more confusing by the fact that we clean up this accounting when we drop the delayed ref, but also include it when we actually pin the extents down properly. It took me quite a while to realize that we weren't mis-counting ->total_bytes_pinned because of how weirdly complicated the accounting was.
I've refactored this code to make the handling distinct. We modify it in the delayed refs code itself, which allows us to clean up a bunch of function arguments and duplicated code. It also unifies how we handle the delayed ref side of the ->total_bytes_pinned modification. Now it's a little easier to see who is responsible for the modification and where.
The second patch is the actual fix for the problem. Previously we had simply been assuming that ->total_ref_mod < 0 meant that we were freeing stuff. However if we allocate and free in the same transaction, ->total_ref_mod == 0 also means we're freeing. Adding that case is what fixes the problem Nikolay was seeing. Thanks,
Josef
Josef Bacik (2):
btrfs: handle ->total_bytes_pinned inside the delayed ref itself
btrfs: account for new extents being deleted in total_bytes_pinned
fs/btrfs/block-group.c | 11 ++-- fs/btrfs/delayed-ref.c | 60 ++++++++++++------- fs/btrfs/delayed-ref.h | 16 +++-- fs/btrfs/extent-tree.c | 129 ++++++++++------------------------------- fs/btrfs/space-info.h | 17 ++++++ 5 files changed, 103 insertions(+), 130 deletions(-)
Signature · 1 line
2.26.2
PATCH v2 1/2 btrfs: handle ->total_bytes_pinned inside the delayed ref itself
Currently we pass things around to figure out if we maybe free'ing data based on the state of the delayed refs head. This makes the accounting sort of confusing and hard to follow, as it's distinctly separate from the delayed ref heads stuff, but also depends on it entirely.
Fix this by explicitly adjusting the space_info->total_bytes_pinned in the delayed refs code. We now have two places where we modify this counter, once where we create the delayed and destroy the delayed refs, and once when we pin and unpin the extents. This means there is a slight overlap between delayed refs and the pin/unpin mechanisms, but this is simply used by the ENOSPC infrastructure to determine if we need to commit the transaction, so there's no adverse affect from this, we might simply commit thinking it will give us enough space when it might not.
--- fs/btrfs/block-group.c | 11 ++--- fs/btrfs/delayed-ref.c | 53 ++++++++++++++--------- fs/btrfs/delayed-ref.h | 16 +++++-- fs/btrfs/extent-tree.c | 97 ++++++------------------------------------ fs/btrfs/space-info.h | 17 ++++++++ 5 files changed, 77 insertions(+), 117 deletions(-)
fs/btrfs/block-group.c +4 -7
@@ -1363,9 +1363,8 @@ void btrfs_delete_unused_bgs(struct btrfs_fs_info *fs_info)btrfs_space_info_update_bytes_pinned (fs_info, space_info, -block_group->pinned); space_info->bytes_readonly += block_group->pinned; -percpu_counter_add_batch (&space_info->total_bytes_pinned, - -block_group->pinned, -BTRFS_TOTAL_BYTES_PINNED_BATCH ); +__btrfs_mod_total_bytes_pinned (space_info, + -block_group->pinned); block_group->pinned =0 ;spin_unlock (&block_group->lock);
@@ -2887,10 +2886,8 @@ int btrfs_update_block_group(struct btrfs_trans_handle *trans,spin_unlock (&cache->lock);spin_unlock (&cache->space_info->lock); -percpu_counter_add_batch ( - &cache->space_info->total_bytes_pinned, - num_bytes, -BTRFS_TOTAL_BYTES_PINNED_BATCH ); +__btrfs_mod_total_bytes_pinned (cache->space_info, + num_bytes);set_extent_dirty (&trans->transaction->pinned_extents, bytenr, bytenr + num_bytes -1 ,GFP_NOFS | __GFP_NOFAIL);
fs/btrfs/delayed-ref.c +32 -21
135 lines not on this page · raw message
fs/btrfs/delayed-ref.h +12 -4
@@ -326,6 +326,16 @@ static inline void btrfs_put_delayed_ref(struct btrfs_delayed_ref_node *ref) } } +static inline u64 btrfs_ref_head_to_space_flags ( +struct btrfs_delayed_ref_head *head_ref) +{ +if (head_ref->is_data) +return BTRFS_BLOCK_GROUP_DATA ; +else if (head_ref->is_system) +return BTRFS_BLOCK_GROUP_SYSTEM ; +return BTRFS_BLOCK_GROUP_METADATA ; +} +static inline void btrfs_put_delayed_ref_head (struct btrfs_delayed_ref_head *head) {if (refcount_dec_and_test (&head->refs))
@@ -334,12 +344,10 @@ static inline void btrfs_put_delayed_ref_head(struct btrfs_delayed_ref_head *heaint btrfs_add_delayed_tree_ref (struct btrfs_trans_handle *trans,struct btrfs_ref *generic_ref, -struct btrfs_delayed_extent_op *extent_op, -int *old_ref_mod,int *new_ref_mod); +struct btrfs_delayed_extent_op *extent_op);int btrfs_add_delayed_data_ref (struct btrfs_trans_handle *trans,struct btrfs_ref *generic_ref, -u64 reserved,int *old_ref_mod, -int *new_ref_mod); +u64 reserved);int btrfs_add_delayed_extent_op (struct btrfs_trans_handle *trans,u64 bytenr,u64 num_bytes,struct btrfs_delayed_extent_op *extent_op);
fs/btrfs/extent-tree.c +12 -85
202 lines not on this page · raw message
fs/btrfs/space-info.h +17 -0
@@ -152,4 +152,21 @@ static inline void btrfs_space_info_free_bytes_may_use(int btrfs_reserve_data_bytes (struct btrfs_fs_info *fs_info,u64 bytes,enum btrfs_reserve_flush_enum flush); +static inline void __btrfs_mod_total_bytes_pinned ( +struct btrfs_space_info *space_info, +s64 mod) +{ +percpu_counter_add_batch (&space_info->total_bytes_pinned, mod, +BTRFS_TOTAL_BYTES_PINNED_BATCH ); +} + +static inline void btrfs_mod_total_bytes_pinned (struct btrfs_fs_info *fs_info, +u64 flags,s64 mod) +{ +struct btrfs_space_info *space_info =btrfs_find_space_info (fs_info, + flags); +ASSERT (space_info); +__btrfs_mod_total_bytes_pinned (space_info, mod); +} +#endif /* BTRFS_SPACE_INFO_H */
Signature · 1 line
2.26.2
PATCH v2 2/2 btrfs: account for new extents being deleted in total_bytes_pinned
In reply to Josef BacikMy recent set of patches to reduce lock contention on the extent root by running delayed refs resulted in a regression in generic/371. This test fallocate()'s the fs until it's full, deletes all the files, and then tries to fallocate() until full again.
Before my delayed refs patches we would run all of the delayed refs during flushing, and then would commit the transaction because we had plenty of pinned space to recover in order to allocate. However my patches made it so we weren't running the delayed refs as aggressively, which meant that we appeared to have less pinned space when we were deciding to commit the transaction.
We use the space_info->total_bytes_pinned to approximate how much space we have pinned. It's approximate because if we remove a reference to an extent we may free it, but there may be more references to it than we know of at that point, but we account it as pinned at the creation time, and then it's properly accounted when the delayed ref runs.
The way we account for pinned space is if the delayed_ref_head->total_ref_mod is < 0, because that is clearly a free'ing option. However there is another case, and that is where ->total_ref_mod == 0 && ->must_insert_reserved == 1.
When we allocate a new extent, we have ->total_ref_mod == 1 and we have ->must_insert_reserved == 1. This is used to indicate that it is a brand new extent and will need to have its extent entry added before we modify any references on the delayed ref head. But if we subsequently remove that extent reference, our ->total_ref_mod will be 0, and that space will be pinned and freed. Accounting for this case properly allows for generic/371 to pass with my delayed refs patches applied.
It's important to note that this problem exists without my delayed refs patches, it just was uncovered by them.
--- fs/btrfs/delayed-ref.c | 7 +++++++ fs/btrfs/extent-tree.c | 34 ++++++++++++++++++++-------------- 2 files changed, 27 insertions(+), 14 deletions(-)
fs/btrfs/delayed-ref.c +7 -0
@@ -732,6 +732,9 @@ static noinline void update_existing_head_ref(struct btrfs_trans_handle *trans,* 2. We were negative and went to 0 or positive, so no longer can say * that the space would be pinned, decrement our counter from the * total_bytes_pinned counter. +* 3. We are now at 0 and have ->must_insert_reserved set, which means +* this was a new allocation and then we dropped it, and thus must +* add our space to the total_bytes_pinned counter. */ if (existing->total_ref_mod <0 && old_ref_mod >=0 ) btrfs_mod_total_bytes_pinned(fs_info, flags,
@@ -739,6 +742,10 @@ static noinline void update_existing_head_ref(struct btrfs_trans_handle *trans,else if (existing->total_ref_mod >=0 && old_ref_mod <0 )btrfs_mod_total_bytes_pinned (fs_info, flags, -existing->num_bytes); +else if (existing->total_ref_mod ==0 && + existing->must_insert_reserved) +btrfs_mod_total_bytes_pinned (fs_info, flags, + existing->num_bytes);spin_unlock (&existing->lock); }
fs/btrfs/extent-tree.c +20 -14
@@ -1754,23 +1754,29 @@ void btrfs_cleanup_ref_head_accounting(struct btrfs_fs_info *fs_info, {int nr_items =1 ;/* Dropping this ref head update. */ -if (head->total_ref_mod <0 ) { +/* +* We had csum deletions accounted for in our delayed refs rsv, we need +* to drop the csum leaves for this update from our delayed_refs_rsv. +*/ +if (head->total_ref_mod <0 && head->is_data) { +spin_lock (&delayed_refs->lock); + delayed_refs->pending_csums -= head->num_bytes; +spin_unlock (&delayed_refs->lock); + nr_items +=btrfs_csum_bytes_to_leaves (fs_info, + head->num_bytes); + } + +/* +* We were dropping refs, or had a new ref and dropped it, and thus must +* adjust down our total_bytes_pinned, the space may or may not have +* been pinned and so is accounted for properly in the pinned space by +* now. +*/ +if (head->total_ref_mod <0 || + (head->total_ref_mod ==0 && head->must_insert_reserved)) {u64 flags =btrfs_ref_head_to_space_flags (head);btrfs_mod_total_bytes_pinned (fs_info, flags, -head->num_bytes); - -/* -* We had csum deletions accounted for in our delayed refs rsv, -* we need to drop the csum leaves for this update from our -* delayed_refs_rsv. -*/ -if (head->is_data) { -spin_lock (&delayed_refs->lock); - delayed_refs->pending_csums -= head->num_bytes; -spin_unlock (&delayed_refs->lock); - nr_items +=btrfs_csum_bytes_to_leaves (fs_info, - head->num_bytes); - } }btrfs_delayed_refs_rsv_release (fs_info, nr_items);
Signature · 1 line
2.26.2
Re: [PATCH v2 2/2] btrfs: account for new extents being deleted in total_bytes_pinned
Quoted 31 lines · Josef Bacik
My recent set of patches to reduce lock contention on the extent root by running delayed refs resulted in a regression in generic/371. This test fallocate()'s the fs until it's full, deletes all the files, and then tries to
fallocate()until full again.Before my delayed refs patches we would run all of the delayed refs during flushing, and then would commit the transaction because we had plenty of pinned space to recover in order to allocate. However my patches made it so we weren't running the delayed refs as aggressively, which meant that we appeared to have less pinned space when we were deciding to commit the transaction.
We use the
space_info->total_bytes_pinnedto approximate how much space we have pinned. It's approximate because if we remove a reference to an extent we may free it, but there may be more references to it than we know of at that point, but we account it as pinned at the creation time, and then it's properly accounted when the delayed ref runs.The way we account for pinned space is if the
delayed_ref_head->total_ref_modis < 0, because that is clearly a free'ing option. However there is another case, and that is where ->total_ref_mod == 0 && ->must_insert_reserved == 1.When we allocate a new extent, we have ->total_ref_mod == 1 and we have ->must_insert_reserved == 1. This is used to indicate that it is a brand new extent and will need to have its extent entry added before we modify any references on the delayed ref head. But if we subsequently remove that extent reference, our ->total_ref_mod will be 0, and that space will be pinned and freed. Accounting for this case properly allows for generic/371 to pass with my delayed refs patches applied.
Quoted 4 lines
It's important to note that this problem exists without my delayed refs patches, it just was uncovered by them.
Signed-off-by Josef Bacik <[email protected]>
Re: [PATCH v2 2/2] btrfs: account for new extents being deleted in total_bytes_pinned
In reply to Josef BacikQuoted 2 lines · Josef Bacik
My recent set of patches to reduce lock contention on the extent root by running delayed refs resulted in a regression in generic/371.
As you write that often in patches, I want to point out that references to 'my recent patches doing something' are too vague and don't stand the test of time. Reading the patch in a year won't give me much clue what patches are that. Also as patches don't get merged in the order they've sent, 'recent' could be referring to something that would be in fact committed after this patch, making it more confusing and hard to find.
If the patches are yet to be merged, referencing by subject should be sufficient, Filipe has been doing that and I find it really convenient to get more background to understand the changes or put them to context in case there's some dependency.
Quoted 5 lines
This test
fallocate()'s the fs until it's full, deletes all the files, and then tries tofallocate()until full again.Before my delayed refs patches we would run all of the delayed refs
And another one. If there's a hard patchset-to-patchset dependency and they get merged in that order, that's fine to refer to it like that and we've been doing that. The difference is that the patches are serialized in git and going back in git history will end up in the referred patches eventually. Unlike when it's still in the mailinglist, but still a more specific reference is better.