]> git.ipfire.org Git - thirdparty/kernel/linux.git/commitdiff
btrfs: add ASSERTs on prealloc in qgroup functions
authorMiquel Sabaté Solà <mssola@mssola.com>
Fri, 24 Oct 2025 10:21:43 +0000 (12:21 +0200)
committerDavid Sterba <dsterba@suse.com>
Mon, 24 Nov 2025 21:34:51 +0000 (22:34 +0100)
The prealloc variable in these functions is always initialized to
NULL. Whenever we allocate memory for it, if it fails then NULL is
preserved, otherwise we delegate the ownership of the pointer to
add_qgroup_rb() and set it right after to NULL.

Since in any case the pointer ends up being NULL at the end of its
usage, we can safely remove calls to kfree() for it, while adding an
ASSERT as an extra check.

Signed-off-by: Miquel Sabaté Solà <mssola@mssola.com>
Reviewed-by: David Sterba <dsterba@suse.com>
Signed-off-by: David Sterba <dsterba@suse.com>
fs/btrfs/qgroup.c

index 877a65e1794f02f7e586067a158ead7a989f5135..1956e4bf230220b8035447f5555e1b6c13fbbff2 100644 (file)
@@ -1263,7 +1263,14 @@ out:
                btrfs_end_transaction(trans);
        else if (trans)
                ret = btrfs_end_transaction(trans);
-       kfree(prealloc);
+
+       /*
+        * At this point we either failed at allocating prealloc, or we
+        * succeeded and passed the ownership to it to add_qgroup_rb(). In any
+        * case, this needs to be NULL or there is something wrong.
+        */
+       ASSERT(prealloc == NULL);
+
        return ret;
 }
 
@@ -1695,7 +1702,12 @@ int btrfs_create_qgroup(struct btrfs_trans_handle *trans, u64 qgroupid)
        ret = btrfs_sysfs_add_one_qgroup(fs_info, qgroup);
 out:
        mutex_unlock(&fs_info->qgroup_ioctl_lock);
-       kfree(prealloc);
+       /*
+        * At this point we either failed at allocating prealloc, or we
+        * succeeded and passed the ownership to it to add_qgroup_rb(). In any
+        * case, this needs to be NULL or there is something wrong.
+        */
+       ASSERT(prealloc == NULL);
        return ret;
 }
 
@@ -3303,7 +3315,7 @@ int btrfs_qgroup_inherit(struct btrfs_trans_handle *trans, u64 srcid,
        struct btrfs_root *quota_root;
        struct btrfs_qgroup *srcgroup;
        struct btrfs_qgroup *dstgroup;
-       struct btrfs_qgroup *prealloc;
+       struct btrfs_qgroup *prealloc = NULL;
        struct btrfs_qgroup_list **qlist_prealloc = NULL;
        bool free_inherit = false;
        bool need_rescan = false;
@@ -3544,7 +3556,14 @@ out:
        }
        if (free_inherit)
                kfree(inherit);
-       kfree(prealloc);
+
+       /*
+        * At this point we either failed at allocating prealloc, or we
+        * succeeded and passed the ownership to it to add_qgroup_rb(). In any
+        * case, this needs to be NULL or there is something wrong.
+        */
+       ASSERT(prealloc == NULL);
+
        return ret;
 }