Skip to content

btrfs: block_group refcounting fixes

Boris Burkov linux-btrfs 2 versions – In review

Get series with b4
b4 am [email protected]

Versions newest first · linked by author and title

  1. v2 Latest5 patches 3 repliesIn review
  2. v15 patches 15 repliesSuperseded

Changes in v2 from the cover letter

- fix mistaken placement of a btrfs_block_group put in the 2nd (locking) patch, when it ought to be in the 4th (ref-counting) patch.
- improve several commit messages with more details and using full function names instead of shorthand.
- add comments about over-paranoid locking.
- rename second patch to reflect that it is hardening rather than fixing any bugs.
- fix bad comment and variable names in btrfs_link_bg_list.

Patches in v2 No applies recorded yet

#Subject and filesSizeRepliesApplied
1/5btrfs: fix bg refcount race in btrfs_create_pending_block_groupsfs/btrfs/block-group.c+3 -01 reply
2/5btrfs: harden bg->bg_list against list_del racesfs/btrfs/extent-tree.c fs/btrfs/transaction.c+20 -01 reply
3/5btrfs: make discard_workfn block_group ref explicitfs/btrfs/discard.c+16 -180 replies
4/5btrfs: explicitly ref count block_group on new_bgs listfs/btrfs/block-group.c fs/btrfs/transaction.c+3 -00 replies
5/5btrfs: codify pattern for adding block_group to bg_listfs/btrfs/block-group.c+30 -241 reply

Reviews trailers per patch

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

PatchQu WenruoFilipe Manana
1/5R-b R-b
2/5R-b R-b
3/5R-b
4/5R-b
5/5R-b

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

Read the v2 thread

Cover letter [PATCH v2 0/5] btrfs: block_group refcounting fixes

We have observed a number of WARNINGs in the Meta fleet which are the result of a block_group refcount underflowing. The refcount error can happen at any point in the block group's lifetime, so it is hard to conclude that we have reproduced/fixed all the bugs, I believe I have found a few here that will hopefully improve things.

The main thrust of this patch series is that we need to take the fs_info->unused_bgs_lock spin lock when modifying the bg_list of a block_group. There are a number of code paths where we atomically check that list_head for emptiness and then add/del get/put appropriately. If any other thread messes with it in between without locking, then that logic gets messed up. This is most obviously evident with btrfs_mark_bg_unused.

I could imagine universally protecting bg_list's empty/not-empty nature with a lock with smaller scope, but this is already the locking strategy being used to synchronize reclaim/unused lists, so it seems reasonable to just re-use it.

In addition, I attempted to simplify the refcounting logic in the discard workfn, as the last time I fixed a bug in there, I made it far too subtle. Hopefully this more explicit variant is easier to analyze in the future.
--
Changelog
v2:
- fix mistaken placement of a btrfs_block_group put in the 2nd (locking) patch, when it ought to be in the 4th (ref-counting) patch.
- improve several commit messages with more details and using full function names instead of shorthand.
- add comments about over-paranoid locking.
- rename second patch to reflect that it is hardening rather than fixing any bugs.
- fix bad comment and variable names in btrfs_link_bg_list.

Boris Burkov (5):
btrfs: fix bg refcount race in btrfs_create_pending_block_groups
btrfs: harden bg->bg_list against list_del races
btrfs: make discard_workfn block_group ref explicit
btrfs: explicitly ref count block_group on new_bgs list
btrfs: codify pattern for adding block_group to bg_list

 fs/btrfs/block-group.c | 57 +++++++++++++++++++++++++-----------------
 fs/btrfs/discard.c     | 34 ++++++++++++-------------
 fs/btrfs/extent-tree.c |  8 ++++++
 fs/btrfs/transaction.c | 13 ++++++++++
 4 files changed, 71 insertions(+), 41 deletions(-)
Signature · 1 line
2.48.1