From: "Miquel Sabaté Solà" <mssola@mssola.com>
To: linux-btrfs@vger.kernel.org
Cc: clm@fb.com, dsterba@suse.com, johannes.thumshirn@wdc.com,
fdmanana@suse.com, boris@bur.io, wqu@suse.com, neal@gompa.dev,
linux-kernel@vger.kernel.org,
"Miquel Sabaté Solà" <mssola@mssola.com>
Subject: [PATCH 4/4] btrfs: add ASSERTs on prealloc in qgroup functions
Date: Tue, 21 Oct 2025 16:27:49 +0200 [thread overview]
Message-ID: <20251021142749.642956-5-mssola@mssola.com> (raw)
In-Reply-To: <20251021142749.642956-1-mssola@mssola.com>
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>
---
fs/btrfs/qgroup.c | 27 +++++++++++++++++++++++----
1 file changed, 23 insertions(+), 4 deletions(-)
diff --git a/fs/btrfs/qgroup.c b/fs/btrfs/qgroup.c
index 6adb57d5c958..664135240803 100644
--- a/fs/btrfs/qgroup.c
+++ b/fs/btrfs/qgroup.c
@@ -1263,7 +1263,14 @@ int btrfs_quota_enable(struct btrfs_fs_info *fs_info,
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;
}
@@ -1693,7 +1700,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;
}
@@ -3301,7 +3313,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;
@@ -3542,7 +3554,14 @@ int btrfs_qgroup_inherit(struct btrfs_trans_handle *trans, u64 srcid,
}
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;
}
--
2.51.1
next prev parent reply other threads:[~2025-10-21 14:28 UTC|newest]
Thread overview: 9+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-10-21 14:27 [PATCH 0/4] btrfs: define and apply the AUTO_K(V)FREE_PTR macros Miquel Sabaté Solà
2025-10-21 14:27 ` [PATCH 1/4] btrfs: declare free_ipath() via DEFINE_FREE() instead Miquel Sabaté Solà
2025-10-22 7:31 ` David Sterba
2025-10-22 8:06 ` Daniel Vacek
2025-10-22 9:21 ` Miquel Sabaté Solà
2025-10-21 14:27 ` [PATCH 2/4] btrfs: define the AUTO_K(V)FREE_PTR helper macros Miquel Sabaté Solà
2025-10-21 14:27 ` [PATCH 3/4] btrfs: apply the AUTO_K(V)FREE_PTR macros throughout the tree Miquel Sabaté Solà
2025-10-21 14:27 ` Miquel Sabaté Solà [this message]
2025-10-22 7:22 ` [PATCH 0/4] btrfs: define and apply the AUTO_K(V)FREE_PTR macros 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=20251021142749.642956-5-mssola@mssola.com \
--to=mssola@mssola.com \
--cc=boris@bur.io \
--cc=clm@fb.com \
--cc=dsterba@suse.com \
--cc=fdmanana@suse.com \
--cc=johannes.thumshirn@wdc.com \
--cc=linux-btrfs@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=neal@gompa.dev \
--cc=wqu@suse.com \
/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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox