All of lore.kernel.org
 help / color / mirror / Atom feed
From: fdmanana@kernel.org
To: linux-btrfs@vger.kernel.org
Subject: [PATCH 01/18] btrfs: fix inode list leak during backref walking at resolve_indirect_refs()
Date: Tue,  1 Nov 2022 16:15:37 +0000	[thread overview]
Message-ID: <87e27f27fb2ebf760e574f97fd2d2f7d37f50350.1667315100.git.fdmanana@suse.com> (raw)
In-Reply-To: <cover.1667315100.git.fdmanana@suse.com>

From: Filipe Manana <fdmanana@suse.com>

During backref walking, at resolve_indirect_refs(), if we get an error
we jump to the 'out' label and call ulist_free() on the 'parents' ulist,
which frees all the elements in the ulist - however that does not free
any inode lists that may be attached to elements, through the 'aux' field
of a ulist node, so we end up leaking lists if we have any attached to
the unodes.

Fix this by calling free_leaf_list() instead of ulist_free() when we exit
from resolve_indirect_refs(). The static function free_leaf_list() is
moved up for this to be possible and it's slightly simplified by removing
unnecessary code.

Fixes: 3301958b7c1d ("Btrfs: add inodes before dropping the extent lock in find_all_leafs")
Signed-off-by: Filipe Manana <fdmanana@suse.com>
---
 fs/btrfs/backref.c | 36 +++++++++++++++++-------------------
 1 file changed, 17 insertions(+), 19 deletions(-)

diff --git a/fs/btrfs/backref.c b/fs/btrfs/backref.c
index 013c2c085229..82c4cad0ca00 100644
--- a/fs/btrfs/backref.c
+++ b/fs/btrfs/backref.c
@@ -678,6 +678,18 @@ unode_aux_to_inode_list(struct ulist_node *node)
 	return (struct extent_inode_elem *)(uintptr_t)node->aux;
 }
 
+static void free_leaf_list(struct ulist *ulist)
+{
+	struct ulist_node *node;
+	struct ulist_iterator uiter;
+
+	ULIST_ITER_INIT(&uiter);
+	while ((node = ulist_next(ulist, &uiter)))
+		free_inode_elem_list(unode_aux_to_inode_list(node));
+
+	ulist_free(ulist);
+}
+
 /*
  * We maintain three separate rbtrees: one for direct refs, one for
  * indirect refs which have a key, and one for indirect refs which do not
@@ -791,7 +803,11 @@ static int resolve_indirect_refs(struct btrfs_fs_info *fs_info,
 		cond_resched();
 	}
 out:
-	ulist_free(parents);
+	/*
+	 * We may have inode lists attached to refs in the parents ulist, so we
+	 * must free them before freeing the ulist and its refs.
+	 */
+	free_leaf_list(parents);
 	return ret;
 }
 
@@ -1621,24 +1637,6 @@ static int find_parent_nodes(struct btrfs_trans_handle *trans,
 	return ret;
 }
 
-static void free_leaf_list(struct ulist *blocks)
-{
-	struct ulist_node *node = NULL;
-	struct extent_inode_elem *eie;
-	struct ulist_iterator uiter;
-
-	ULIST_ITER_INIT(&uiter);
-	while ((node = ulist_next(blocks, &uiter))) {
-		if (!node->aux)
-			continue;
-		eie = unode_aux_to_inode_list(node);
-		free_inode_elem_list(eie);
-		node->aux = 0;
-	}
-
-	ulist_free(blocks);
-}
-
 /*
  * Finds all leafs with a reference to the specified combination of bytenr and
  * offset. key_list_head will point to a list of corresponding keys (caller must
-- 
2.35.1


  reply	other threads:[~2022-11-01 16:16 UTC|newest]

Thread overview: 20+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-11-01 16:15 [PATCH 00/18] btrfs: make send scale and perform better with shared extents fdmanana
2022-11-01 16:15 ` fdmanana [this message]
2022-11-01 16:15 ` [PATCH 02/18] btrfs: fix inode list leak during backref walking at find_parent_nodes() fdmanana
2022-11-01 16:15 ` [PATCH 03/18] btrfs: fix ulist leaks in error paths of qgroup self tests fdmanana
2022-11-01 16:15 ` [PATCH 04/18] btrfs: remove pointless and double ulist frees in error paths of qgroup tests fdmanana
2022-11-01 16:15 ` [PATCH 05/18] btrfs: send: avoid unnecessary path allocations when finding extent clone fdmanana
2022-11-01 16:15 ` [PATCH 06/18] btrfs: send: update comment at find_extent_clone() fdmanana
2022-11-01 16:15 ` [PATCH 07/18] btrfs: send: drop unnecessary backref context field initializations fdmanana
2022-11-01 16:15 ` [PATCH 08/18] btrfs: send: avoid unnecessary backref lookups when finding clone source fdmanana
2022-11-01 16:15 ` [PATCH 09/18] btrfs: send: optimize clone detection to increase extent sharing fdmanana
2022-11-01 16:15 ` [PATCH 10/18] btrfs: use a single argument for extent offset in backref walking functions fdmanana
2022-11-01 16:15 ` [PATCH 11/18] btrfs: use a structure to pass arguments to " fdmanana
2022-11-01 16:15 ` [PATCH 12/18] btrfs: reuse roots ulist on each leaf iteration for iterate_extent_inodes() fdmanana
2022-11-01 16:15 ` [PATCH 13/18] btrfs: constify ulist parameter of ulist_next() fdmanana
2022-11-01 16:15 ` [PATCH 14/18] btrfs: send: cache leaf to roots mapping during backref walking fdmanana
2022-11-01 16:15 ` [PATCH 15/18] btrfs: send: skip unnecessary backref iterations fdmanana
2022-11-01 16:15 ` [PATCH 16/18] btrfs: send: avoid double extent tree search when finding clone source fdmanana
2022-11-01 16:15 ` [PATCH 17/18] btrfs: send: skip resolution of our own backref " fdmanana
2022-11-01 16:15 ` [PATCH 18/18] btrfs: send: bump the extent reference count limit for backref walking fdmanana
2022-11-02 16:01 ` [PATCH 00/18] btrfs: make send scale and perform better with shared extents David Sterba

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=87e27f27fb2ebf760e574f97fd2d2f7d37f50350.1667315100.git.fdmanana@suse.com \
    --to=fdmanana@kernel.org \
    --cc=linux-btrfs@vger.kernel.org \
    /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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.