From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-alma10-1.taild15c8.ts.net [100.103.45.18]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 706C23955D0 for ; Sun, 6 Sep 2026 15:57:09 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=100.103.45.18 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1788710232; cv=none; b=O1/sUAIcptBaAmHvqoqDYDhmHjbOtMbou1CsV16ahLCjAI+/82VZHYpMHvjg6azQ8gFTsXQTU2jV8FGZ72NMst/lUChLqVwVFF9VAz/QNN3Pbifcg5QPxM82auuRzUFAux8g8O0jNFq0GVh2hDoexg32tTKHyVwGmgO5PsCdf/g= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1788710232; c=relaxed/simple; bh=/UA/eEn/meJK9B65Stat30loek9369NMwg9WrZ551oc=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=iL2ELgihOrWsRTQIkfwFDvkW9/ZcL2wcK9VMB3zoFJqIEwu6zJB9Ddbe8UvoxeCgnjPHwVo9lfHcfhsroXKz+bKPs1avjlOsnqF2PqqVRS1012pMHV40QvfmT5taRpoLe3LiwAHjNNMGN76JpBVPQoJEymHq73cpNGjbOoGEAAU= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=MlfSevZw; arc=none smtp.client-ip=100.103.45.18 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="MlfSevZw" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 7A2451F00AC4; Sun, 6 Sep 2026 15:57:07 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1788710228; bh=adKCWt45Qua8bazBaeQpWPp7vj45AeCd4Qa4mkkdt/A=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=MlfSevZwKyNfoAY+ERzZQC5ljV4RHYq5bFBFBnC+yGDIIEH45keVYCxcOkj2Uegyc cnoXQNt7Fjz0M627zeaqdf4MGlCCruIUUJs2U+ji8nmlZsdhAwp2mSmLisFjeB9LmX m7br7xtyAvE9GeOO+s33gAnjHmx3/XuqKrSm6VvRawiAcjLqSnHioghzK+crig12dn kmS2t6c2vka62/yHuYfSrNOiV9c8VlOLl76AUkEw0glXsbiIKSj8xVpHsqYnrrYCWv iF+VuUxOtZuoCZ5lSDVnM2mX6er/meEIamL1wRGAZyvs/Sr3hWyuifLwzEkS8Vo525 qNTtD0DTjcYLA== From: Sasha Levin To: stable@vger.kernel.org Cc: Guanghui Yang <3497809730@qq.com>, David Sterba , Sasha Levin Subject: [PATCH 6.6.y 4/4] btrfs: drop recovered reloc root refs on recovery failure Date: Sun, 6 Sep 2026 11:57:03 -0400 Message-ID: <20260906155703.1520234-4-sashal@kernel.org> X-Mailer: git-send-email 2.53.0 In-Reply-To: <20260906155703.1520234-1-sashal@kernel.org> References: <2026090358-pursuit-extruding-d750@gregkh> <20260906155703.1520234-1-sashal@kernel.org> Precedence: bulk X-Mailing-List: stable@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit From: Guanghui Yang <3497809730@qq.com> [ Upstream commit 6d8ba4572922e336f0b59a80751b018e1e135164 ] During relocation recovery, each fs root gets a reference to its relocation root. If loading or adding a later root fails, or if the first transaction commit fails, btrfs_recover_relocation() jumps to out_unset before merge_reloc_roots() and clean_dirty_subvols(). put_reloc_control() drops the list-owned relocation root references, but it does not clear fs_root->reloc_root or drop the references owned by those pointers. Mount cleanup only drops them when BTRFS_FS_ERROR is set, so an error such as -ENOMEM while processing a later root can leave references behind. Keep temporary references to the fs roots associated during recovery. On failure, clear their reloc_root pointers and drop the corresponding references. Once the first transaction commit succeeds, drop only the temporary fs root references and let the normal merge and cleanup paths handle the relocation roots. Fault injection on a pending-relocation image confirmed the cleanup gap. With an injected first-commit failure, 25 fs roots had reloc_root set with fs_error=0. With this fix, the same failure path drops that count to 0 before mount fails. Fixes: f44deb7442ed ("btrfs: hold a ref on the root->reloc_root") CC: stable@vger.kernel.org Signed-off-by: Guanghui Yang <3497809730@qq.com> Reviewed-by: David Sterba Signed-off-by: David Sterba Signed-off-by: Sasha Levin --- fs/btrfs/relocation.c | 31 +++++++++++++++++++++++++++---- 1 file changed, 27 insertions(+), 4 deletions(-) diff --git a/fs/btrfs/relocation.c b/fs/btrfs/relocation.c index 8f5111f791f70..ef6dd9cc86fa2 100644 --- a/fs/btrfs/relocation.c +++ b/fs/btrfs/relocation.c @@ -4165,6 +4165,24 @@ static noinline_for_stack int mark_garbage_root(struct btrfs_root *root) return ret; } +static void release_recovered_fs_roots(struct list_head *roots, bool drop_reloc_refs) +{ + struct btrfs_root *root; + struct btrfs_root *next; + + list_for_each_entry_safe(root, next, roots, reloc_dirty_list) { + list_del_init(&root->reloc_dirty_list); + if (drop_reloc_refs) { + struct btrfs_root *reloc_root = root->reloc_root; + + ASSERT(reloc_root); + root->reloc_root = NULL; + btrfs_put_root(reloc_root); + } + btrfs_put_root(root); + } +} + /* * recover relocation interrupted by system crash. * @@ -4174,6 +4192,7 @@ static noinline_for_stack int mark_garbage_root(struct btrfs_root *root) int btrfs_recover_relocation(struct btrfs_fs_info *fs_info) { LIST_HEAD(reloc_roots); + LIST_HEAD(recovered_roots); struct btrfs_key key; struct btrfs_root *fs_root; struct btrfs_root *reloc_root; @@ -4286,7 +4305,7 @@ int btrfs_recover_relocation(struct btrfs_fs_info *fs_info) ret = PTR_ERR(fs_root); list_add_tail(&reloc_root->root_list, &reloc_roots); btrfs_end_transaction(trans); - goto out_unset; + goto out_drop_reloc_refs; } ret = __add_reloc_root(reloc_root); @@ -4295,15 +4314,17 @@ int btrfs_recover_relocation(struct btrfs_fs_info *fs_info) list_add_tail(&reloc_root->root_list, &reloc_roots); btrfs_put_root(fs_root); btrfs_end_transaction(trans); - goto out_unset; + goto out_drop_reloc_refs; } + ASSERT(list_empty(&fs_root->reloc_dirty_list)); fs_root->reloc_root = btrfs_grab_root(reloc_root); - btrfs_put_root(fs_root); + list_add_tail(&fs_root->reloc_dirty_list, &recovered_roots); } ret = btrfs_commit_transaction(trans); if (ret) - goto out_unset; + goto out_drop_reloc_refs; + release_recovered_fs_roots(&recovered_roots, false); merge_reloc_roots(rc); @@ -4319,6 +4340,8 @@ int btrfs_recover_relocation(struct btrfs_fs_info *fs_info) ret2 = clean_dirty_subvols(rc); if (ret2 < 0 && !ret) ret = ret2; +out_drop_reloc_refs: + release_recovered_fs_roots(&recovered_roots, true); out_unset: unset_reloc_control(rc); reloc_chunk_end(fs_info); -- 2.53.0