From: Qu Wenruo <quwenruo.btrfs@gmx.com>
To: Zilin Guan <zilin@seu.edu.cn>, clm@fb.com
Cc: dsterba@suse.com, sunk67188@gmail.com, dan.carpenter@linaro.org,
linux-btrfs@vger.kernel.org, linux-kernel@vger.kernel.org,
jianhao.xu@seu.edu.cn
Subject: Re: [PATCH] btrfs: tests: Fix memory leak in btrfs_test_qgroups()
Date: Fri, 26 Dec 2025 07:19:44 +1030 [thread overview]
Message-ID: <5ba227a9-034c-4e3e-964c-d13f8f3ec2b8@gmx.com> (raw)
In-Reply-To: <20251225102727.967360-1-zilin@seu.edu.cn>
在 2025/12/25 20:57, Zilin Guan 写道:
> btrfs_alloc_dummy_root() allocates a root with a reference count of 1.
> Then btrfs_insert_fs_root() is used to insert the root into the fs_info.
> On success, it increments the reference count. On failure, it does not.
>
> Currently, if btrfs_insert_fs_root() fails, the error handling path
> jumps to the out label immediately without decrementing the reference
> count of tmp_root, leading to a memory leak.
>
> Fix this by calling btrfs_put_root() unconditionally after
> btrfs_insert_fs_root(). This correctly handles both cases: on success,
> it drops the local reference, leaving the root with the reference held
> by fs_info; on failure, it drops the sole reference, freeing the root.
>
> Fixes: 4785e24fa5d23 ("btrfs: don't take an extra root ref at allocation time")
> Signed-off-by: Zilin Guan <zilin@seu.edu.cn>
> ---
> fs/btrfs/tests/qgroup-tests.c | 4 ++--
> 1 file changed, 2 insertions(+), 2 deletions(-)
>
> diff --git a/fs/btrfs/tests/qgroup-tests.c b/fs/btrfs/tests/qgroup-tests.c
> index e9124605974b..0d51e0abaeac 100644
> --- a/fs/btrfs/tests/qgroup-tests.c
> +++ b/fs/btrfs/tests/qgroup-tests.c
> @@ -517,11 +517,11 @@ int btrfs_test_qgroups(u32 sectorsize, u32 nodesize)
> tmp_root->root_key.objectid = BTRFS_FS_TREE_OBJECTID;
> root->fs_info->fs_root = tmp_root;
> ret = btrfs_insert_fs_root(root->fs_info, tmp_root);
> + btrfs_put_root(tmp_root);
> if (ret) {
> test_err("couldn't insert fs root %d", ret);
> goto out;
This will lead to double free.
If btrfs_insert_fs_root() failed, btrfs_put_root() will do the cleaning
and free the root.
Then btrfs_free_dummy_root() will call btrfs_put_root() again on the
root, cause use-after-free.
So your analyze is completely wrong.
Thanks,
Qu
> }
> - btrfs_put_root(tmp_root);
>
> tmp_root = btrfs_alloc_dummy_root(fs_info);
> if (IS_ERR(tmp_root)) {
> @@ -532,11 +532,11 @@ int btrfs_test_qgroups(u32 sectorsize, u32 nodesize)
>
> tmp_root->root_key.objectid = BTRFS_FIRST_FREE_OBJECTID;
> ret = btrfs_insert_fs_root(root->fs_info, tmp_root);
> + btrfs_put_root(tmp_root);
> if (ret) {
> test_err("couldn't insert fs root %d", ret);
> goto out;
> }
> - btrfs_put_root(tmp_root);
>
> test_msg("running qgroup tests");
> ret = test_no_shared_qgroup(root, sectorsize, nodesize);
next prev parent reply other threads:[~2025-12-25 20:49 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-12-25 10:27 [PATCH] btrfs: tests: Fix memory leak in btrfs_test_qgroups() Zilin Guan
2025-12-25 20:49 ` Qu Wenruo [this message]
2025-12-25 20:54 ` Qu Wenruo
2025-12-26 7:29 ` Zilin Guan
2025-12-26 8:03 ` Qu Wenruo
2025-12-26 11:24 ` Zilin Guan
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=5ba227a9-034c-4e3e-964c-d13f8f3ec2b8@gmx.com \
--to=quwenruo.btrfs@gmx.com \
--cc=clm@fb.com \
--cc=dan.carpenter@linaro.org \
--cc=dsterba@suse.com \
--cc=jianhao.xu@seu.edu.cn \
--cc=linux-btrfs@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=sunk67188@gmail.com \
--cc=zilin@seu.edu.cn \
/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