mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Qu Wenruo <wqu@suse.com>
To: Vlad Poenaru <vlad.wing@gmail.com>,
	dsterba@suse.com, linux-btrfs@vger.kernel.org
Cc: mason@kernel.org, osandov@osandov.com, josef@toxicpanda.com,
	fdmanana@suse.com, boris@bur.io, quwenruo.btrfs@gmx.com,
	linux-kernel@vger.kernel.org, kernel-team@meta.com,
	stable@vger.kernel.org
Subject: Re: [PATCH v2] btrfs: fix bytes_may_use underflow after a failed tree block allocation
Date: Thu, 19 Nov 2026 07:08:22 +1030	[thread overview]
Message-ID: <da879d7e-7790-4507-bfbc-55a78fe20ddc@suse.com> (raw)
In-Reply-To: <20261118170332.992625-1-vlad.wing@gmail.com>



在 2026/11/19 03:33, Vlad Poenaru 写道:
> 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.
> 
> Fixes: 67b7859e9bfa ("btrfs: handle ENOMEM in btrfs_alloc_tree_block")
> Cc: stable@vger.kernel.org # 6.16+
> Reviewed-by: Boris Burkov <boris@bur.io>
> Signed-off-by: Vlad Poenaru <vlad.wing@gmail.com>

Reviewed-by: Qu Wenruo <wqu@suse.com>

Thanks,
Qu

> ---
> 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/20261113170926.1468850-1-vlad.wing@gmail.com/
>   fs/btrfs/extent-tree.c | 1 +
>   1 file changed, 1 insertion(+)
> 
> diff --git a/fs/btrfs/extent-tree.c b/fs/btrfs/extent-tree.c
> index 9b0a5c6b96f3c..8d4c66bf850bc 100644
> --- a/fs/btrfs/extent-tree.c
> +++ b/fs/btrfs/extent-tree.c
> @@ -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);


      reply	other threads:[~2026-11-18 20:38 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-18 17:03 Vlad Poenaru
2026-11-18 20:38 ` Qu Wenruo [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=da879d7e-7790-4507-bfbc-55a78fe20ddc@suse.com \
    --to=wqu@suse.com \
    --cc=boris@bur.io \
    --cc=dsterba@suse.com \
    --cc=fdmanana@suse.com \
    --cc=josef@toxicpanda.com \
    --cc=kernel-team@meta.com \
    --cc=linux-btrfs@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mason@kernel.org \
    --cc=osandov@osandov.com \
    --cc=quwenruo.btrfs@gmx.com \
    --cc=stable@vger.kernel.org \
    --cc=vlad.wing@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®