Skip to contentSkip to conversation

delayed_node leak bug

PATCH Superseded Leo Martins 3 participants 7 messages May 27, 2025 Btrfs file system

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

cover delayed_node leak bug

Currently investigating a bug I believe is caused by leaked delayed_nodes. The following patches fix a potential delayed_node leak in an assert function (I don't believe this is the cause of the bug) and add a warning if a root still contains delayed_nodes when it is freed.

A little more on the bug I'm investigating in case anyone has seen something similar...

Started seeing soft lockups in btrfs_kill_all_delayed_nodes due to an infinte loop. Further investigation showed that there was a delayed_node that was not being erased from the root->delayed_nodes xarray. The delayed_node had a reference count of one meaning that it is failing to be released somewhere.

Leo Martins (2):
btrfs: fix refcount leak in debug assertion
btrfs: warn if leaking delayed_nodes

 fs/btrfs/delayed-inode.c | 7 ++++++-
 fs/btrfs/disk-io.c       | 2 ++
 2 files changed, 8 insertions(+), 1 deletion(-)
Signature · 1 line
2.47.1
#1 lore raw

PATCH 1/2 btrfs: fix refcount leak in debug assertion

If the delayed_root is not empty we are increasing the number of references to a delayed_node without decreasing it, causing a leak. Fix by decrementing the delayed_node reference count.

---
 fs/btrfs/delayed-inode.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
fs/btrfs/delayed-inode.c +6 -1
index c7cc24a5dd5e..f4e47bfc603b 100644
@@ -1377,7 +1377,12 @@ static int btrfs_wq_run_delayed_node(struct btrfs_delayed_root *delayed_root,
 
 void btrfs_assert_delayed_root_empty(struct btrfs_fs_info *fs_info)
 {
-	WARN_ON(btrfs_first_delayed_node(fs_info->delayed_root));
+	struct btrfs_delayed_node *node = btrfs_first_delayed_node(fs_info->delayed_root);
+
+	WARN_ON(node);
+
+	if (node)
+		refcount_dec(&node->refs);
 }
 
 static bool could_end_wait(struct btrfs_delayed_root *delayed_root, int seq)
Signature · 1 line
2.47.1
#4 lore raw

PATCH 2/2 btrfs: warn if leaking delayed_nodes

In reply to Leo Martins

Add a warning for leaked delayed_nodes when putting a root. We currently do this for inodes, but not delayed_nodes.

---
 fs/btrfs/disk-io.c | 2 ++
 1 file changed, 2 insertions(+)
fs/btrfs/disk-io.c +2 -0
index 1beb9458f622..3def93016963 100644
@@ -1835,6 +1835,8 @@ void btrfs_put_root(struct btrfs_root *root)
 	if (refcount_dec_and_test(&root->refs)) {
 		if (WARN_ON(!xa_empty(&root->inodes)))
 			xa_destroy(&root->inodes);
+		if (WARN_ON(!xa_empty(&root->delayed_nodes)))
+			xa_destroy(&root->delayed_nodes);
 		WARN_ON(test_bit(BTRFS_ROOT_DEAD_RELOC_TREE, &root->state));
 		if (root->anon_dev)
 			free_anon_bdev(root->anon_dev);
Signature · 1 line
2.47.1
Reviewed-by on v1 1/2
#2 lore raw

Re: [PATCH 1/2] btrfs: fix refcount leak in debug assertion

In reply to Leo Martins
Quoted 15 lines · Leo Martins

If the delayed_root is not empty we are increasing the number of references to a delayed_node without decreasing it, causing a leak. Fix by decrementing the delayed_node reference count.

---
 fs/btrfs/delayed-inode.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
fs/btrfs/delayed-inode.c +0 -0
index c7cc24a5dd5e..f4e47bfc603b 100644

fs/btrfs/delayed-inode.c static int btrfs_wq_run_delayed_node(struct btrfs_delayed_root *delayed_root, · line 1384

Quoted 9 lines
fs/btrfs/delayed-inode.c +5 -1
@@ -1377,7 +1377,12 @@ static int btrfs_wq_run_delayed_node(struct btrfs_delayed_root *delayed_root,
 
 void btrfs_assert_delayed_root_empty(struct btrfs_fs_info *fs_info)
 {
-       WARN_ON(btrfs_first_delayed_node(fs_info->delayed_root));
+       struct btrfs_delayed_node *node = btrfs_first_delayed_node(fs_info->delayed_root);
+
+       WARN_ON(node);
+
+       if (node)

This can shortened to:

if (WARN_ON(node))

Besides making the code shorter, it also allows the compiler to generate better code since the WARN_ON() includes the unlikely tag and this is an unexpected branch, so we benefit from doing it like that.

With that change:

Thanks.

Quoted 8 lines
fs/btrfs/delayed-inode.c +1 -1
+               refcount_dec(&node->refs);
 }
 
 static bool could_end_wait(struct btrfs_delayed_root *delayed_root, int seq)
--

2.47.1

Reviewed-by on v1 2/2
#5 lore raw

Re: [PATCH 2/2] btrfs: warn if leaking delayed_nodes

In reply to Leo Martins
Quoted 5 lines · Leo Martins

Add a warning for leaked delayed_nodes when putting a root. We currently do this for inodes, but not delayed_nodes.

Looks good, thanks.

Quoted 21 lines
---
 fs/btrfs/disk-io.c | 2 ++
 1 file changed, 2 insertions(+)
fs/btrfs/disk-io.c +2 -1
index 1beb9458f622..3def93016963 100644
@@ -1835,6 +1835,8 @@ void btrfs_put_root(struct btrfs_root *root)
        if (refcount_dec_and_test(&root->refs)) {
                if (WARN_ON(!xa_empty(&root->inodes)))
                        xa_destroy(&root->inodes);
+               if (WARN_ON(!xa_empty(&root->delayed_nodes)))
+                       xa_destroy(&root->delayed_nodes);
                WARN_ON(test_bit(BTRFS_ROOT_DEAD_RELOC_TREE, &root->state));
                if (root->anon_dev)
                        free_anon_bdev(root->anon_dev);
--

2.47.1

Reviewed-by on v1 1/2
#3 lore raw

Re: [PATCH 1/2] btrfs: fix refcount leak in debug assertion

In reply to Leo Martins
Quoted 14 lines · Leo Martins

If the delayed_root is not empty we are increasing the number of references to a delayed_node without decreasing it, causing a leak. Fix by decrementing the delayed_node reference count.

---
  fs/btrfs/delayed-inode.c | 7 ++++++-
  1 file changed, 6 insertions(+), 1 deletion(-)
fs/btrfs/delayed-inode.c +0 -0
index c7cc24a5dd5e..f4e47bfc603b 100644

fs/btrfs/delayed-inode.c static int btrfs_wq_run_delayed_node(struct btrfs_delayed_root *delayed_root, · line 1384

Quoted 9 lines
fs/btrfs/delayed-inode.c +5 -1
@@ -1377,7 +1377,12 @@ static int btrfs_wq_run_delayed_node(struct btrfs_delayed_root *delayed_root,
 
  void btrfs_assert_delayed_root_empty(struct btrfs_fs_info *fs_info)
  {
-	WARN_ON(btrfs_first_delayed_node(fs_info->delayed_root));
+	struct btrfs_delayed_node *node = btrfs_first_delayed_node(fs_info->delayed_root);
+
+	WARN_ON(node);
+
+	if (node)

Just like what Filipe has pointed out, one line "if (WARN_ON(node))" will be good enough.

Otherwise looks good to me.

Thanks,
Qu> + refcount_dec(&node->refs);

Quoted 3 lines
fs/btrfs/delayed-inode.c +0 -0
  }
 
  static bool could_end_wait(struct btrfs_delayed_root *delayed_root, int seq)
Reviewed-by on v1 2/2
#6 lore raw

Re: [PATCH 2/2] btrfs: warn if leaking delayed_nodes

In reply to Leo Martins
Quoted 4 lines · Leo Martins

Add a warning for leaked delayed_nodes when putting a root. We currently do this for inodes, but not delayed_nodes.

Thanks,
Qu

Quoted 17 lines
---
  fs/btrfs/disk-io.c | 2 ++
  1 file changed, 2 insertions(+)
fs/btrfs/disk-io.c +2 -0
index 1beb9458f622..3def93016963 100644
@@ -1835,6 +1835,8 @@ void btrfs_put_root(struct btrfs_root *root)
  	if (refcount_dec_and_test(&root->refs)) {
  		if (WARN_ON(!xa_empty(&root->inodes)))
  			xa_destroy(&root->inodes);
+		if (WARN_ON(!xa_empty(&root->delayed_nodes)))
+			xa_destroy(&root->delayed_nodes);
  		WARN_ON(test_bit(BTRFS_ROOT_DEAD_RELOC_TREE, &root->state));
  		if (root->anon_dev)
  			free_anon_bdev(root->anon_dev)