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