]> git.hungrycats.org Git - linux/commitdiff
btrfs: cleanup dirty_cowonly_roots during btrfs_free_fs_roots()
authorQu Wenruo <wqu@suse.com>
Tue, 13 Oct 2026 05:41:20 +0000 (16:11 +1030)
committerDavid Sterba <dsterba@suse.com>
Fri, 30 Oct 2026 20:36:16 +0000 (21:36 +0100)
[EXISTING ERROR HANDLING QUIRK]
Sashiko mentioned an existing problem that, if btrfs_abort_transaction()
is triggered during the following call site, some roots will be attached
to dirty_cowonly_roots without being cleaned up:

 btrfs_commit_transaction()
 |- commit_cowonly_roots()
    |- list_move_tail();
    |  One root's dirty_list is moved from dirty_cowonly_roots to
    |  switch_commits.
    |  But there are still other dirty roots in dirty_cowonly_roots.
    |
    |- update_cowonly_root()
    |  |- btrfs_update_root()
    |     |- btrfs_abort_transaction()
    |
    |- return ret;

In that case, those dirty roots linked into dirty_cowonly_roots will
never be removed from that list.
As normally we require a transaction commit to move them, but since the
fs is already marked error, no new transaction can be started.

For roots that are already moved to switch_commits list, they are
cleaned up by btrfs_cleanup_one_transaction().

This is not going to cause memory leak on the btrfs_root structure
itself, as after a transaction is aborted, no one will read
dirty_cowonly_roots.

Furthermore dirty_cowonly_roots only tracks trees that are not
subvolume or log trees. They should not have root->dirty_log_pages.
And qgroup swapped blocks are going to be released during
btrfs_put_root().

Thus no memory leak from those two members either.

But we're still relying on a hidden behavior to keep us from leaking
memory.

[ENHANCEMENT]
Unlink all roots that are still linked to dirty_cowonly_roots during
btrfs_free_fs_roots(), and do one extra ASSERT() to make sure that their
dirty_log_pages are empty.

At that timing there is no other thread thus we're safe to unlink roots
without holding trans_lock.

Reviewed-by: Filipe Manana <fdmanana@suse.com>
Signed-off-by: Qu Wenruo <wqu@suse.com>
Signed-off-by: David Sterba <dsterba@suse.com>
fs/btrfs/disk-io.c

index 71a344b564070ded34d2b740596f5b315875c9c6..c628c5081d923856b28f73d66d114478dc1f72d2 100644 (file)
@@ -1863,6 +1863,25 @@ void btrfs_free_fs_roots(struct btrfs_fs_info *fs_info)
        struct btrfs_root *gang[8];
        int i;
 
+       /*
+        * Some dirty roots may still be linked to dirty_cowonly_roots after
+        * the transaction is aborted. Cleanup them here.
+        */
+       while (!list_empty(&fs_info->dirty_cowonly_roots)) {
+               struct btrfs_root *root;
+
+               root = list_first_entry(&fs_info->dirty_cowonly_roots,
+                                       struct btrfs_root, dirty_list);
+               list_del_init(&root->dirty_list);
+               /*
+                * The dirty_log_pages should never be populated for
+                * cowonly trees.
+                */
+               spin_lock(&root->dirty_log_pages.lock);
+               ASSERT(RB_EMPTY_ROOT(&root->dirty_log_pages.state));
+               spin_unlock(&root->dirty_log_pages.lock);
+       }
+
        while (!list_empty(&fs_info->dead_roots)) {
                gang[0] = list_first_entry(&fs_info->dead_roots,
                                           struct btrfs_root, root_list);