btrfs: fix bytes_may_use underflow after a failed tree block allocation
Subsystems (2)
PATCH v2 1/1 btrfs: fix bytes_may_use underflow after a failed tree block allocation
When btrfs_alloc_tree_block() fails after btrfs_reserve_extent() has succeeded, its error path gives the block back to the block reserve, although the block has already left bytes_may_use. Each such failure leaves bytes_may_use a block short, and it underflows and warns in space-info.h when the global block reserve is released at unmount, or earlier.
btrfs_alloc_tree_block()
|- btrfs_use_block_rsv() |- btrfs_reserve_extent() | \- btrfs_add_reserved_bytes() <- bytes_may_use -= blocksize |- btrfs_init_new_buffer() | \- alloc_extent_buffer() <- fails with -ENOMEM |- btrfs_free_reserved_extent() <- bytes_reserved -= blocksize
\- btrfs_unuse_block_rsv() <- block rsv += blocksize again
Since commit 19d7f65f032f ("btrfs: convert the buffer_radix to an xarray") in 6.16, alloc_extent_buffer() can fail this way under memory cgroup pressure, because fs_info->buffer_tree is memcg-accounted.
Return without giving the block back once btrfs_reserve_extent() has succeeded.
---
v2:
- Shorter changelog, built around a call trace (Qu Wenruo).
- Dropped the code comment (Boris Burkov).
The code is v1's apart from the removed comment, so v1's testing stands:
its reproducer leaves bytes_may_use below the global reserve without the fix and not with it, and fstests btrfs/002, btrfs/220, generic/475 and generic/476 pass.
v1, with the reproducer:
https://lore.kernel.org/all/[email protected]/
fs/btrfs/extent-tree.c | 1 + 1 file changed, 1 insertion(+)
fs/btrfs/extent-tree.c +1 -0
@@ -5433,6 +5433,7 @@ struct extent_buffer *btrfs_alloc_tree_block(struct btrfs_trans_handle *trans,free_extent_buffer (buf); out_free_reserved:btrfs_free_reserved_extent (fs_info, ins.objectid, ins.offset, false); +return ERR_PTR (ret); out_unuse:btrfs_unuse_block_rsv (fs_info, block_rsv, blocksize);return ERR_PTR (ret);
Quoted 20 lines · Vlad Poenaru
When
btrfs_alloc_tree_block()fails afterbtrfs_reserve_extent()has succeeded, its error path gives the block back to the block reserve, although the block has already leftbytes_may_use. Each such failure leavesbytes_may_usea block short, and it underflows and warns inspace-info.hwhen the global block reserve is released at unmount, or earlier.
btrfs_alloc_tree_block()|- btrfs_use_block_rsv() |- btrfs_reserve_extent() | \- btrfs_add_reserved_bytes() <- bytes_may_use -= blocksize |- btrfs_init_new_buffer() | \- alloc_extent_buffer() <- fails with -ENOMEM |- btrfs_free_reserved_extent() <- bytes_reserved -= blocksize\-
btrfs_unuse_block_rsv()<- block rsv += blocksize againSince commit
19d7f65f032f("btrfs: convert thebuffer_radixto an xarray") in 6.16,alloc_extent_buffer()can fail this way under memory cgroup pressure, becausefs_info->buffer_treeis memcg-accounted.
Quoted 7 lines
Return without giving the block back once
btrfs_reserve_extent()has succeeded.Fixes 67b7859e9bfa ("btrfs: handle ENOMEM in btrfs_alloc_tree_block")Cc [email protected] # 6.16+Reviewed-by Boris Burkov <[email protected]>Signed-off-by Vlad Poenaru <[email protected]>
Thanks,
Qu
Quoted 27 lines
---
v2:
- Shorter changelog, built around a call trace (Qu Wenruo).
- Dropped the code comment (Boris Burkov).The code is v1's apart from the removed comment, so v1's testing stands:
its reproducer leavesbytes_may_usebelow the global reserve without the fix and not with it, and fstests btrfs/002, btrfs/220, generic/475 and generic/476 pass.v1, with the reproducer:
https://lore.kernel.org/all/[email protected]/fs/btrfs/extent-tree.c | 1 + 1 file changed, 1 insertion(+)fs/btrfs/extent-tree.c +1
-0@@ -5433,6 +5433,7 @@ struct extent_buffer *btrfs_alloc_tree_block(struct btrfs_trans_handle *trans,free_extent_buffer (buf); out_free_reserved:btrfs_free_reserved_extent (fs_info, ins.objectid, ins.offset, false); +return ERR_PTR (ret); out_unuse:btrfs_unuse_block_rsv (fs_info, block_rsv, blocksize);return ERR_PTR (ret);