* [PATCH 1/2] btrfs: make btrfs_backref_finish_upper_links() fail before touching the cache
2026-11-23 23:10 [PATCH 0/2] btrfs: error handling improvement to btrfs_backref_error_cleanup() Qu Wenruo
@ 2026-11-23 23:10 ` Qu Wenruo
2026-11-23 23:10 ` [PATCH 2/2] btrfs: fix leaked backref nodes in btrfs_backref_error_cleanup() Qu Wenruo
1 sibling, 0 replies; 3+ messages in thread
From: Qu Wenruo @ 2026-11-23 23:10 UTC (permalink / raw)
To: linux-btrfs
btrfs_backref_finish_upper_links() returns -EUCLEAN if it finds an
unchecked upper node, but it does so in the middle of the walk. By then
@start and some upper nodes are already inserted into the rb tree, and
the remaining edges are queued on a local pending_edge list on the stack.
This makes the error handling in btrfs_backref_error_cleanup() complex
and incomplete:
- The queued edges still point to the dead stack list head
btrfs_backref_error_cleanup() does list_del() on them and writes into
stale stack memory.
- Cached nodes have edges to uncached upper nodes
btrfs_backref_error_cleanup() stops at cached nodes, so the uncached
upper nodes above them are leaked.
Avoid the partial state by checking all the uncached upper nodes before
anything is modified. If the check fails, nothing is cached and no edge
is queued.
The old check inside the loop becomes an ASSERT().
Signed-off-by: Qu Wenruo <wqu@suse.com>
---
fs/btrfs/backref.c | 26 +++++++++++++++++++++-----
1 file changed, 21 insertions(+), 5 deletions(-)
diff --git a/fs/btrfs/backref.c b/fs/btrfs/backref.c
index a0fb90b7a9ed..9354eec71b35 100644
--- a/fs/btrfs/backref.c
+++ b/fs/btrfs/backref.c
@@ -3564,6 +3564,21 @@ int btrfs_backref_add_tree_node(struct btrfs_trans_handle *trans,
return ret;
}
+static bool backref_uppers_checked(struct btrfs_backref_node *node)
+{
+ struct btrfs_backref_edge *edge;
+
+ list_for_each_entry(edge, &node->upper, list[LOWER]) {
+ struct btrfs_backref_node *upper = edge->node[UPPER];
+
+ if (upper->detached || !RB_EMPTY_NODE(&upper->rb_node))
+ continue;
+ if (!upper->checked || !backref_uppers_checked(upper))
+ return false;
+ }
+ return true;
+}
+
/*
* Finish the upwards linkage created by btrfs_backref_add_tree_node()
*/
@@ -3577,6 +3592,11 @@ int btrfs_backref_finish_upper_links(struct btrfs_backref_cache *cache,
ASSERT(start->checked);
+ if (unlikely(!backref_uppers_checked(start))) {
+ DEBUG_WARN("we should not have any unchecked nodes");
+ return -EUCLEAN;
+ }
+
rb_node = rb_simple_insert(&cache->rb_root, &start->simple_node);
if (rb_node)
btrfs_backref_panic(cache->fs_info, start->bytenr, -EEXIST);
@@ -3622,11 +3642,7 @@ int btrfs_backref_finish_upper_links(struct btrfs_backref_cache *cache,
continue;
}
- /* Sanity check, we shouldn't have any unchecked nodes */
- if (unlikely(!upper->checked)) {
- DEBUG_WARN("we should not have any unchecked nodes");
- return -EUCLEAN;
- }
+ ASSERT(upper->checked);
rb_node = rb_simple_insert(&cache->rb_root, &upper->simple_node);
if (unlikely(rb_node))
--
2.56.1
^ permalink raw reply related [flat|nested] 3+ messages in thread* [PATCH 2/2] btrfs: fix leaked backref nodes in btrfs_backref_error_cleanup()
2026-11-23 23:10 [PATCH 0/2] btrfs: error handling improvement to btrfs_backref_error_cleanup() Qu Wenruo
2026-11-23 23:10 ` [PATCH 1/2] btrfs: make btrfs_backref_finish_upper_links() fail before touching the cache Qu Wenruo
@ 2026-11-23 23:10 ` Qu Wenruo
1 sibling, 0 replies; 3+ messages in thread
From: Qu Wenruo @ 2026-11-23 23:10 UTC (permalink / raw)
To: linux-btrfs
Sashiko reported a bug that if build_backref_tree() fails after
handle_indirect_tree_backref() has created new upper nodes, those nodes
are never freed.
btrfs_backref_error_cleanup() finds the new nodes only through
cache->pending_edge, which is not enough:
- Not all new edges are queued there
If the upper node doesn't need its backrefs checked (e.g. the tree
block can't be shared, upper->checked == 1), the edge is initialized
with INIT_LIST_HEAD(&edge->list[UPPER]) and never queued.
- Queued edges are removed before their upper node is processed
build_backref_tree() does list_del_init() on the edge before it uses
edge->node[UPPER] as the next @cur.
For example, take this tree, where C is the @node and B was created
before a snapshot so it can be shared, while the root A can not:
A (root)
|
\- B
|
\- C (@node)
build_backref_tree()
|- btrfs_backref_add_tree_node(@cur = C)
| \- handle_indirect_tree_backref(@cur = C)
| allocates B and A, and the edges C -> B and B -> A
| only C -> B is queued in pending_edge, A is a root and not shared
|
|- list_del_init()
| removes the edge C -> B from pending_edge, @cur becomes B
|
|- btrfs_backref_add_tree_node(@cur = B)
| fails, e.g. -ENOMEM
|
\- btrfs_backref_error_cleanup()
| pending_edge is empty, B and A are never found
|
\- btrfs_backref_cleanup_node(@node = C)
only frees the edge C -> B and C
B, A and the edge B -> A are in no list nor in the rb tree, so nothing
can free them anymore. A is only reachable through B.
Fix it by walking upwards from @node through the upper edges, which is
how all new nodes are linked, and stop at nodes already in the cache.
The walk is breadth first, so a node is dropped only after all of its
lower nodes are processed and no edge refers to it.
Nodes on useless_node and edges on pending_edge are not used to find the
nodes to free. They should be reachable from @node, which is checked by
the final ASSERT() that both lists are empty.
Link: https://sashiko.dev/#/bug/linux-6b8466f1-cacd-4417-b046-f02bd8a80dd2
Signed-off-by: Qu Wenruo <wqu@suse.com>
---
fs/btrfs/backref.c | 66 ++++++++++++++++------------------------------
1 file changed, 22 insertions(+), 44 deletions(-)
diff --git a/fs/btrfs/backref.c b/fs/btrfs/backref.c
index 9354eec71b35..8bba24dd1371 100644
--- a/fs/btrfs/backref.c
+++ b/fs/btrfs/backref.c
@@ -3663,53 +3663,31 @@ int btrfs_backref_finish_upper_links(struct btrfs_backref_cache *cache,
void btrfs_backref_error_cleanup(struct btrfs_backref_cache *cache,
struct btrfs_backref_node *node)
{
- struct btrfs_backref_node *lower;
- struct btrfs_backref_node *upper;
+ struct btrfs_backref_node *cur;
struct btrfs_backref_edge *edge;
+ LIST_HEAD(queue);
- while (!list_empty(&cache->useless_node)) {
- lower = list_first_entry(&cache->useless_node,
- struct btrfs_backref_node, list);
- list_del_init(&lower->list);
+ if (node)
+ list_move_tail(&node->list, &queue);
+
+ while (!list_empty(&queue)) {
+ cur = list_first_entry(&queue, struct btrfs_backref_node, list);
+
+ while (!list_empty(&cur->upper)) {
+ struct btrfs_backref_node *upper;
+
+ edge = list_first_entry(&cur->upper,
+ struct btrfs_backref_edge, list[LOWER]);
+ upper = edge->node[UPPER];
+ list_del(&edge->list[LOWER]);
+ list_del(&edge->list[UPPER]);
+ btrfs_backref_free_edge(cache, edge);
+
+ if (RB_EMPTY_NODE(&upper->rb_node))
+ list_move_tail(&upper->list, &queue);
+ }
+ btrfs_backref_drop_node(cache, cur);
}
- while (!list_empty(&cache->pending_edge)) {
- edge = list_first_entry(&cache->pending_edge,
- struct btrfs_backref_edge, list[UPPER]);
- list_del(&edge->list[UPPER]);
- list_del(&edge->list[LOWER]);
- lower = edge->node[LOWER];
- upper = edge->node[UPPER];
- btrfs_backref_free_edge(cache, edge);
-
- /*
- * Lower is no longer linked to any upper backref nodes and
- * isn't in the cache, we can free it ourselves.
- */
- if (list_empty(&lower->upper) &&
- RB_EMPTY_NODE(&lower->rb_node))
- list_add(&lower->list, &cache->useless_node);
-
- if (!RB_EMPTY_NODE(&upper->rb_node))
- continue;
-
- /* Add this guy's upper edges to the list to process */
- list_for_each_entry(edge, &upper->upper, list[LOWER])
- list_add_tail(&edge->list[UPPER],
- &cache->pending_edge);
- if (list_empty(&upper->upper))
- list_add(&upper->list, &cache->useless_node);
- }
-
- while (!list_empty(&cache->useless_node)) {
- lower = list_first_entry(&cache->useless_node,
- struct btrfs_backref_node, list);
- list_del_init(&lower->list);
- if (lower == node)
- node = NULL;
- btrfs_backref_drop_node(cache, lower);
- }
-
- btrfs_backref_cleanup_node(cache, node);
ASSERT(list_empty(&cache->useless_node) &&
list_empty(&cache->pending_edge));
}
--
2.56.1
^ permalink raw reply related [flat|nested] 3+ messages in thread