From: Leo Martins <loemra.dev@gmail.com>
To: linux-btrfs@vger.kernel.org, kernel-team@fb.com
Subject: [PATCH 1/2] btrfs: use __free in linux/cleanup.h to reduce btrfs_free_path boilerplate
Date: Fri, 9 Aug 2024 16:11:48 -0700 [thread overview]
Message-ID: <7e5f9aebbfdd190cdb4f12589a779d873f3fd91e.1723245033.git.loemra.dev@gmail.com> (raw)
In-Reply-To: <cover.1723245033.git.loemra.dev@gmail.com>
This patch lays the groundwork for future improvements to the btrfs file
system code by introducing the __free(btrfs_free_path) attribute. This
attribute allows the kernel to automatically call btrfs_free_path() on
variables marked with it when they go out of scope, ensuring proper
memory management and preventing potential memory leaks.
Test Plan:
Built and booted the kernel with patch applied.
Ran btrfs/fstests to make sure that no regressions were introduced.
Signed-off-by: Leo Martins <loemra.dev@gmail.com>
---
fs/btrfs/ctree.c | 3 ++-
fs/btrfs/ctree.h | 1 +
2 files changed, 3 insertions(+), 1 deletion(-)
diff --git a/fs/btrfs/ctree.c b/fs/btrfs/ctree.c
index 451203055bbf..9938664d7dbb 100644
--- a/fs/btrfs/ctree.c
+++ b/fs/btrfs/ctree.c
@@ -3,6 +3,7 @@
* Copyright (C) 2007,2008 Oracle. All rights reserved.
*/
+#include <linux/cleanup.h>
#include <linux/sched.h>
#include <linux/slab.h>
#include <linux/rbtree.h>
@@ -196,7 +197,7 @@ struct btrfs_path *btrfs_alloc_path(void)
/* this also releases the path */
void btrfs_free_path(struct btrfs_path *p)
{
- if (!p)
+ if (IS_ERR_OR_NULL(p))
return;
btrfs_release_path(p);
kmem_cache_free(btrfs_path_cachep, p);
diff --git a/fs/btrfs/ctree.h b/fs/btrfs/ctree.h
index 75fa563e4cac..be4e14b6e39a 100644
--- a/fs/btrfs/ctree.h
+++ b/fs/btrfs/ctree.h
@@ -599,6 +599,7 @@ int btrfs_search_slot_for_read(struct btrfs_root *root,
void btrfs_release_path(struct btrfs_path *p);
struct btrfs_path *btrfs_alloc_path(void);
void btrfs_free_path(struct btrfs_path *p);
+DEFINE_FREE(btrfs_free_path, struct btrfs_path *, if (_T) btrfs_free_path(_T));
int btrfs_del_items(struct btrfs_trans_handle *trans, struct btrfs_root *root,
struct btrfs_path *path, int slot, int nr);
--
2.43.5
next prev parent reply other threads:[~2024-08-09 23:13 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-08-09 23:11 [PATCH 0/2] btrfs: add __free attribute and improve xattr cleanup Leo Martins
2024-08-09 23:11 ` Leo Martins [this message]
2024-08-09 23:11 ` [PATCH 2/2] btrfs: use __free to automatically free btrfs_path on exit Leo Martins
2024-08-13 21:34 ` David Sterba
2024-08-13 21:29 ` [PATCH 0/2] btrfs: add __free attribute and improve xattr cleanup David Sterba
2024-08-15 17:38 ` Leo Martins
2024-08-15 18:46 ` 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=7e5f9aebbfdd190cdb4f12589a779d873f3fd91e.1723245033.git.loemra.dev@gmail.com \
--to=loemra.dev@gmail.com \
--cc=kernel-team@fb.com \
--cc=linux-btrfs@vger.kernel.org \
/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