Skip to content

Block group caching fixes

Josef Bacik linux-btrfs 1 version In review

Get series with b4
b4 am [email protected]

Versions newest first

  1. v1 Latest8 patches 14 repliesIn review

Patches in v1 No applies recorded yet

#Subject and filesSizeRepliesApplied
1/8btrfs: do not shorten unpin len for caching block groupsfs/btrfs/extent-tree.c+4 -41 reply
2/8btrfs: update last_byte_to_unpin in switch_commit_rootsfs/btrfs/ctree.h fs/btrfs/extent-tree.c fs/btrfs/transaction.c+39 -282 replies
3/8btrfs: explicitly protect ->last_byte_to_unpin in unpin_extent_rangefs/btrfs/extent-tree.c+2 -01 reply
4/8btrfs: cleanup btrfs_discard_update_discardable usagefs/btrfs/discard.c fs/btrfs/discard.h fs/btrfs/free-space-cache.c+13 -115 replies
5/8btrfs: load free space cache into a temporary ctlfs/btrfs/block-group.c fs/btrfs/free-space-cache.c fs/btrfs/free-space-cache.h +1 more+70 -1191 reply
6/8btrfs: load the free space cache inode extents from commit rootfs/btrfs/inode.c+9 -11 reply
7/8btrfs: async load free space cachefs/btrfs/block-group.c+53 -701 reply
8/8btrfs: protect the fs_info->caching_block_groups differentlyfs/btrfs/block-group.c fs/btrfs/transaction.c+8 -61 reply

Reviews trailers per patch

All 8 patches have a Reviewed-by or Acked-by.

PatchFilipe Manana
1/8R-b
2/8R-b
3/8R-b
4/8R-b
5/8R-b
6/8R-b
7/8R-b
8/8R-b

Trailers given in replies are filled; those carried in the patches from earlier versions are outlined with dashes.

Read the v1 thread

Cover letter [PATCH 0/8] Block group caching fixes

Hello,

The lockdep fix I posted about protecting ->caching_block_groups got feedback that I needed to document how we're using the ->commit_root_sem to protect ->last_byte_to_unpin, which forced me to take a real hard look at how we're coordinating the caching threads. This lead to the discovery of a few pretty significant problems which has lead to this patchset. There are a few individual fixes, but the bulk of these patches are around making all caching of block groups asynchronous.

Previously we would load the v1 space cache at caching time, instead of doing it asynchronously. This was for speed, but also because there's a delicate balance that needs to be maintained with unpinning and the v1 space cache. With the slow caching we keep track of our progress in caching, so we can unpin anything prior to that progress. However since the space cache only knows the state of the block groupat that time, we have to load the cache in it's entirety so we are able to unpin ranges in that block group properly. Thus loading the space cache on demand and inline with the caller.

This resulted in a few weird special cases for the space cache

1) We don't actually use the ->commit_root_sem when doing the free space cache lookups. This is incorrect, and can actually race with a transaction commit. This in practice doesn't mean much, but is still functionally wrong and an outlier.
2) Instead of using the ->commit_root_sem and ->search_commit_root when looking up the extents for the free space cache inode, we use ->recurse and allow recursive read locks on nodes we may already hold write locks on. This happens in the case where we are modifying the tree_root when we need to cache the block group, we may already be holding the write lock on an upper node and then subsequently need to take the read lock on that node to read the extents for the free space cache inode. This is the only place we allow recursion, and we don't actually need it because we could easily use ->search_commit_root.

However we can't actually take the ->search_commit_root in this path, because it causes the lockdep splat that I fix in the last patch of this series. This means we need to move the loading of the v1 space cache to an async thread so we can take the proper locks for searching the commit root safely. This allows us to unify our modification of ->last_byte_to_unpin, and clean up the locking for everything else so it's consistent.

The other major fix is how we update ->last_byte_to_unpin. Previously we were recording this value, and then later switching the commit roots. This could result in a gap where we would not unpin a range, thus leaking the space. This leaked space would then end up in the space cache as we use the in-memory cache to write out the on disk space cache. This explains how sometimes we would see messages indicating that the space cache wasn't right and needed to be rebuilt.

The main fixes are

btrfs: do not shorten unpin len for caching block groups
btrfs: update last_byte_to_unpin in switch_commit_roots
btrfs: protect the fs_info->caching_block_groups differently

And the work to make space cache async is in the following patches

btrfs: cleanup btrfs_discard_update_discardable usage
btrfs: load free space cache into a temporary ctl
btrfs: load the free space cache inode extents from commit root
btrfs: async load free space cache

Thanks,

Josef

Josef Bacik (8):
btrfs: do not shorten unpin len for caching block groups
btrfs: update last_byte_to_unpin in switch_commit_roots
btrfs: explicitly protect ->last_byte_to_unpin in unpin_extent_range
btrfs: cleanup btrfs_discard_update_discardable usage
btrfs: load free space cache into a temporary ctl
btrfs: load the free space cache inode extents from commit root
btrfs: async load free space cache
btrfs: protect the fs_info->caching_block_groups differently

 fs/btrfs/block-group.c       | 164 +++++++++++++--------------------
 fs/btrfs/ctree.h             |   1 -
 fs/btrfs/discard.c           |   7 +-
 fs/btrfs/discard.h           |   3 +-
 fs/btrfs/extent-tree.c       |  35 ++------
 fs/btrfs/free-space-cache.c  | 169 +++++++++++++++--------------------
 fs/btrfs/free-space-cache.h  |   3 +-
 fs/btrfs/inode.c             |  10 ++-
 fs/btrfs/tests/btrfs-tests.c |   2 +-
 fs/btrfs/transaction.c       |  43 ++++++++-
 10 files changed, 198 insertions(+), 239 deletions(-)
Signature · 1 line
2.26.2