diff mbox series

[1/2] btrfs: use __free in linux/cleanup.h to reduce btrfs_free_path boilerplate

Message ID 7e5f9aebbfdd190cdb4f12589a779d873f3fd91e.1723245033.git.loemra.dev@gmail.com (mailing list archive)
State New, archived
Headers show
Series btrfs: add __free attribute and improve xattr cleanup | expand

Commit Message

Leo Martins Aug. 9, 2024, 11:11 p.m. UTC
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 mbox series

Patch

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);