Skip to contentSkip to conversation

btrfs: more trivial BTRFS_PATH_AUTO_FREE conversions

PATCH Superseded Sun YangKai 3 participants 12 messages Aug 21, 2025 – Aug 30, 2025 Btrfs file system

v2 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

v2 cover btrfs: more trivial BTRFS_PATH_AUTO_FREE conversions

Trival pattern for the auto freeing with goto -> return convertions if possible.

---
Changelog
v1 -> v2: check NULL info parameter early in send.c:get_inode_info()

Sun YangKai (2):
btrfs: get_inode_info(): check NULL info parameter early
btrfs: more trivial BTRFS_PATH_AUTO_FREE conversions

 fs/btrfs/raid-stripe-tree.c |  16 +-
 fs/btrfs/ref-verify.c       |   3 +-
 fs/btrfs/reflink.c          |   3 +-
 fs/btrfs/relocation.c       |  66 +++-----
 fs/btrfs/root-tree.c        |  49 +++---
 fs/btrfs/scrub.c            |  11 +-
 fs/btrfs/send.c             | 313 +++++++++++++-----------------------
 fs/btrfs/super.c            |   9 +-
 fs/btrfs/tree-log.c         | 124 +++++---------
 9 files changed, 207 insertions(+), 387 deletions(-)
Signature · 1 line
2.50.1
#1 lore raw

PATCH v2 1/2 btrfs: get_inode_info(): check NULL info parameter early

Move the NULL pointer check for the @info parameter to the beginning of get_inode_info() function to avoid unnecessary operations when no output is required.

This change provides two benefits:
1. Avoids allocating path and performing tree lookups when @info is NULL
2. Simplifies the control flow by eliminating the redundant check before writing output

The functional behavior remains unchanged except for the performance improvement when called with NULL info parameter.

---
 fs/btrfs/send.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
fs/btrfs/send.c +3 -3
index 7664025a5af4..5010d17878f9 100644
@@ -913,6 +913,9 @@ static int get_inode_info(struct btrfs_root *root, u64 ino,
 	struct btrfs_inode_item *ii;
 	struct btrfs_key key;
 
+	if (!info)
+		return 0;
+
 	path = alloc_path_for_send();
 	if (!path)
 		return -ENOMEM;
@@ -927,9 +930,6 @@ static int get_inode_info(struct btrfs_root *root, u64 ino,
 		goto out;
 	}
 
-	if (!info)
-		goto out;
-
 	ii = btrfs_item_ptr(path->nodes[0], path->slots[0],
 			struct btrfs_inode_item);
 	info->size = btrfs_inode_size(path->nodes[0], ii);
Signature · 1 line
2.50.1
#2 lore raw

Re: [PATCH 1/2] btrfs: get_inode_info(): check NULL info parameter early

Quoted 11 lines · Sun YangKai

Move the NULL pointer check for the @info parameter to the beginning of get_inode_info() function to avoid unnecessary operations when no output is required.

This change provides two benefits:
1. Avoids allocating path and performing tree lookups when @info is NULL
2. Simplifies the control flow by eliminating the redundant check before writing output

The functional behavior remains unchanged except for the performance improvement when called with NULL info parameter.

Reordering conditional usually leads to behaviour changes and I think it does in this case as well.

Quoted 10 lines
---
 fs/btrfs/send.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
fs/btrfs/send.c +0 -0
index 7664025a5af4..5010d17878f9 100644

fs/btrfs/send.c static int get_inode_info(struct btrfs_root *root, u64 ino, · line 917

Quoted 5 lines
fs/btrfs/send.c +2 -0
@@ -913,6 +913,9 @@ static int get_inode_info(struct btrfs_root *root, u64 ino,
 	struct btrfs_inode_item *ii;
 	struct btrfs_key key;
 
+	if (!info)
+		return 0;

Moving the check as the first statement will skip anything else. There's one call of get_inode_info() with info == NULL, in apply_dir_move(), otherwise all callers fill in a stack variable.

fs/btrfs/send.c static int get_inode_info(struct btrfs_root *root, u64 ino, · line 931

Quoted 7 lines
fs/btrfs/send.c +1 -0
+
 	path = alloc_path_for_send();
 	if (!path)
 		return -ENOMEM;
@@ -927,9 +930,6 @@ static int get_inode_info(struct btrfs_root *root, u64 ino,
 		goto out;
 	}

Right before this there's call to btrfs_search_slot() looking up INODE_ITEM for 'ino', there are 3 results, and two are still valid for the callers. Only if ret == 0 then the 'if (!info)' check is done. Otherwise it's either an error (ret < 0) or -ENOENT (when ret == 1).

So get_inode_info() is also has the semantics of that the inode exists, with the reordered 'info' check it never happens.

Quoted 10 lines
fs/btrfs/send.c +0 -3
-	if (!info)
-		goto out;
-
 	ii = btrfs_item_ptr(path->nodes[0], path->slots[0],
 			struct btrfs_inode_item);
 	info->size = btrfs_inode_size(path->nodes[0], ii);
Signature · 1 line
2.50.1
#3 lore raw

Re: [PATCH 1/2] btrfs: get_inode_info(): check NULL info parameter early

Quoted 57 lines · Sun YangKai
Quoted 4 lines

Move the NULL pointer check for the @info parameter to the beginning of get_inode_info() function to avoid unnecessary operations when no output is required.

Quoted 8 lines

This change provides two benefits:
1. Avoids allocating path and performing tree lookups when @info is NULL
2. Simplifies the control flow by eliminating the redundant check

before writing output

The functional behavior remains unchanged except for the performance improvement when called with NULL info parameter.

Reordering conditional usually leads to behaviour changes and I think it does in this case as well.

Quoted 12 lines

---

 fs/btrfs/send.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
fs/btrfs/send.c +0 -0
index 7664025a5af4..5010d17878f9 100644
@@ -913,6 +913,9 @@ static int get_inode_info(struct btrfs_root *root, u64

ino,>

fs/btrfs/send.c static int get_inode_info(struct btrfs_root *root, u64 ino, · line 917

Quoted 5 lines
fs/btrfs/send.c +2 -0
 	struct btrfs_inode_item *ii;
 	struct btrfs_key key;
 
+	if (!info)
+		return 0;

Moving the check as the first statement will skip anything else. There's one call of get_inode_info() with info == NULL, in apply_dir_move(), otherwise all callers fill in a stack variable.

fs/btrfs/send.c static int get_inode_info(struct btrfs_root *root, u64 ino,

Quoted 12 lines
fs/btrfs/send.c +1 -0
+
 
 	path = alloc_path_for_send();
 	if (!path)
 
 		return -ENOMEM;
@@ -927,9 +930,6 @@ static int get_inode_info(struct btrfs_root *root, u64

ino,>

fs/btrfs/send.c +0 -0
 		goto out;
 
 	}

Right before this there's call to btrfs_search_slot() looking up INODE_ITEM for 'ino', there are 3 results, and two are still valid for the callers. Only if ret == 0 then the 'if (!info)' check is done. Otherwise it's either an error (ret < 0) or -ENOENT (when ret == 1).

Quoted 2 lines

So get_inode_info() is also has the semantics of that the inode exists, with the reordered 'info' check it never happens.

I got it. Sorry. My bad. I'll fix it in patch v3.

Quoted 10 lines

fs/btrfs/send.c static int get_inode_info(struct btrfs_root *root, u64 ino, · line 935

Quoted 10 lines
fs/btrfs/send.c +0 -3
-	if (!info)
-		goto out;
-
 
 	ii = btrfs_item_ptr(path->nodes[0], path->slots[0],
 
 			struct btrfs_inode_item);
 
 	info->size = btrfs_inode_size(path->nodes[0], ii);

Thanks,
Sun YangKai

#4 lore raw

Re: [PATCH 1/2] btrfs: get_inode_info(): check NULL info parameter early

In reply to Sun YangKai
kernel test robot noticed "xfstests.btrfs.129.fail" on:

Hello,

kernel test robot noticed "xfstests.btrfs.129.fail" on:

commit: 0f755c2d1c496756f81bbe6330e4a3cb9252c89d ("[PATCH 1/2] btrfs: get_inode_info(): check NULL info parameter early")
url: https://github.com/intel-lab-lkp/linux/commits/Sun-YangKai/btrfs-get_inode_info-check-NULL-info-parameter-early/20250821-212815
base: v6.17-rc2
patch link: https://lore.kernel.org/all/[email protected]/
patch subject: [PATCH 1/2] btrfs: get_inode_info(): check NULL info parameter early

in testcase: xfstests
version: xfstests-x86_64-e1e4a0ea-1_20250714
with following parameters:

	disk: 6HDD
	fs: btrfs
	test: btrfs-129

config: x86_64-rhel-9.4-func
compiler: gcc-12
test machine: 8 threads 1 sockets Intel(R) Core(TM) i7-4770 CPU @ 3.40GHz (Haswell) with 8G memory

(please refer to attached dmesg/kmsg for entire log/backtrace)

If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-lkp/[email protected]

2025-08-28 18:04:50 export TEST_DIR=/fs/sdb1
2025-08-28 18:04:50 export TEST_DEV=/dev/sdb1
2025-08-28 18:04:50 export FSTYP=btrfs
2025-08-28 18:04:50 export SCRATCH_MNT=/fs/scratch
2025-08-28 18:04:50 mkdir /fs/scratch -p
2025-08-28 18:04:50 export SCRATCH_DEV_POOL="/dev/sdb2 /dev/sdb3 /dev/sdb4 /dev/sdb5 /dev/sdb6"
2025-08-28 18:04:50 echo btrfs/129
2025-08-28 18:04:50 ./check btrfs/129
FSTYP         -- btrfs
PLATFORM      -- Linux/x86_64 lkp-hsw-d01 6.17.0-rc2-00001-g0f755c2d1c49 #1 SMP PREEMPT_DYNAMIC Fri Aug 29 01:46:03 CST 2025
MKFS_OPTIONS  -- /dev/sdb2

MOUNT_OPTIONS -- /dev/sdb2 /fs/scratch

btrfs/129 [failed, exit status 1]- output mismatch (see /lkp/benchmarks/xfstests/results//btrfs/129.out.bad)

    --- tests/btrfs/129.out	2025-07-14 17:48:52.000000000 +0000
    +++ /lkp/benchmarks/xfstests/results//btrfs/129.out.bad	2025-08-28 18:04:52.493243659 +0000
    @@ -1,2 +1,3 @@
     QA output created by 129
    -Silence is golden
    +failed: '/bin/btrfs send -p /fs/scratch/mysnap1 -f /fs/sdb1/btrfs-test-129/2.snap /fs/scratch/mysnap2'
    +(see /lkp/benchmarks/xfstests/results//btrfs/129.full for details)
    ...
    (Run 'diff -u /lkp/benchmarks/xfstests/tests/btrfs/129.out /lkp/benchmarks/xfstests/results//btrfs/129.out.bad'  to see the entire diff)

Ran: btrfs/129
Failures: btrfs/129
Failed 1 of 1 tests

The kernel config and materials to reproduce are available at:
https://download.01.org/0day-ci/archive/20250829/[email protected]

Signature · 2 lines
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki
#5 lore raw

PATCH v2 2/2 btrfs: more trivial BTRFS_PATH_AUTO_FREE conversions

In reply to Sun YangKai

Trival pattern for the auto freeing with goto -> return convertions if possible. No other function cleanup.

---
 fs/btrfs/raid-stripe-tree.c |  16 +-
 fs/btrfs/ref-verify.c       |   3 +-
 fs/btrfs/reflink.c          |   3 +-
 fs/btrfs/relocation.c       |  66 +++-----
 fs/btrfs/root-tree.c        |  49 +++---
 fs/btrfs/scrub.c            |  11 +-
 fs/btrfs/send.c             | 307 +++++++++++++-----------------------
 fs/btrfs/super.c            |   9 +-
 fs/btrfs/tree-log.c         | 124 +++++----------
 9 files changed, 204 insertions(+), 384 deletions(-)
fs/btrfs/raid-stripe-tree.c +5 -11
index cab0b291088c..231107cafab2 100644
@@ -67,7 +67,7 @@ int btrfs_delete_raid_extent(struct btrfs_trans_handle *trans, u64 start, u64 le
 {
 	struct btrfs_fs_info *fs_info = trans->fs_info;
 	struct btrfs_root *stripe_root = fs_info->stripe_root;
-	struct btrfs_path *path;
+	BTRFS_PATH_AUTO_FREE(path);
 	struct btrfs_key key;
 	struct extent_buffer *leaf;
 	u64 found_start;
@@ -260,7 +260,6 @@ int btrfs_delete_raid_extent(struct btrfs_trans_handle *trans, u64 start, u64 le
 		btrfs_release_path(path);
 	}
 
-	btrfs_free_path(path);
 	return ret;
 }
 
@@ -269,7 +268,7 @@ static int update_raid_extent_item(struct btrfs_trans_handle *trans,
 				   struct btrfs_stripe_extent *stripe_extent,
 				   const size_t item_size)
 {
-	struct btrfs_path *path;
+	BTRFS_PATH_AUTO_FREE(path);
 	struct extent_buffer *leaf;
 	int ret;
 	int slot;
@@ -288,7 +287,6 @@ static int update_raid_extent_item(struct btrfs_trans_handle *trans,
 
 	write_extent_buffer(leaf, stripe_extent, btrfs_item_ptr_offset(leaf, slot),
 			    item_size);
-	btrfs_free_path(path);
 
 	return ret;
 }
@@ -376,7 +374,7 @@ int btrfs_get_raid_extent_offset(struct btrfs_fs_info *fs_info,
 	struct btrfs_stripe_extent *stripe_extent;
 	struct btrfs_key stripe_key;
 	struct btrfs_key found_key;
-	struct btrfs_path *path;
+	BTRFS_PATH_AUTO_FREE(path);
 	struct extent_buffer *leaf;
 	const u64 end = logical + *length;
 	int num_stripes;
@@ -402,7 +400,7 @@ int btrfs_get_raid_extent_offset(struct btrfs_fs_info *fs_info,
 
 	ret = btrfs_search_slot(NULL, stripe_root, &stripe_key, path, 0, 0);
 	if (ret < 0)
-		goto free_path;
+		return ret;
 	if (ret) {
 		if (path->slots[0] != 0)
 			path->slots[0]--;
@@ -459,8 +457,7 @@ int btrfs_get_raid_extent_offset(struct btrfs_fs_info *fs_info,
 		trace_btrfs_get_raid_extent_offset(fs_info, logical, *length,
 						   stripe->physical, devid);
 
-		ret = 0;
-		goto free_path;
+		return 0;
 	}
 
 	/* If we're here, we haven't found the requested devid in the stripe. */
@@ -474,8 +471,5 @@ int btrfs_get_raid_extent_offset(struct btrfs_fs_info *fs_info,
 			  logical, logical + *length, stripe->dev->devid,
 			  btrfs_bg_type_to_raid_name(map_type));
 	}
-free_path:
-	btrfs_free_path(path);
-
 	return ret;
 }
fs/btrfs/ref-verify.c +1 -2
index 3871c3a6c743..cba14eb75c0a 100644
@@ -971,7 +971,7 @@ void btrfs_free_ref_tree_range(struct btrfs_fs_info *fs_info, u64 start,
 int btrfs_build_ref_tree(struct btrfs_fs_info *fs_info)
 {
 	struct btrfs_root *extent_root;
-	struct btrfs_path *path;
+	BTRFS_PATH_AUTO_FREE(path);
 	struct extent_buffer *eb;
 	int tree_block_level = 0;
 	u64 bytenr = 0, num_bytes = 0;
@@ -1014,6 +1014,5 @@ int btrfs_build_ref_tree(struct btrfs_fs_info *fs_info)
 		btrfs_free_ref_cache(fs_info);
 		btrfs_clear_opt(fs_info->mount_opt, REF_VERIFY);
 	}
-	btrfs_free_path(path);
 	return ret;
 }
fs/btrfs/reflink.c +1 -2
index ce25ab7f0e99..98def02ce5cc 100644
@@ -340,7 +340,7 @@ static int btrfs_clone(struct inode *src, struct inode *inode,
 		       const u64 destoff, int no_time_update)
 {
 	struct btrfs_fs_info *fs_info = inode_to_fs_info(inode);
-	struct btrfs_path *path = NULL;
+	BTRFS_PATH_AUTO_FREE(path);
 	struct extent_buffer *leaf;
 	struct btrfs_trans_handle *trans;
 	char *buf = NULL;
@@ -611,7 +611,6 @@ static int btrfs_clone(struct inode *src, struct inode *inode,
 	}
 
 out:
-	btrfs_free_path(path);
 	kvfree(buf);
 	clear_bit(BTRFS_INODE_NO_DELALLOC_FLUSH, &BTRFS_I(inode)->runtime_flags);
 
fs/btrfs/relocation.c +21 -45

209 lines not on this page · raw message

fs/btrfs/root-tree.c +18 -31

166 lines not on this page · raw message

fs/btrfs/scrub.c +3 -8
index 6776e6ab8d10..025a91d267b4 100644
@@ -585,7 +585,7 @@ static void scrub_print_common_warning(const char *errstr, struct btrfs_device *
 				       bool is_super, u64 logical, u64 physical)
 {
 	struct btrfs_fs_info *fs_info = dev->fs_info;
-	struct btrfs_path *path;
+	BTRFS_PATH_AUTO_FREE(path);
 	struct btrfs_key found_key;
 	struct extent_buffer *eb;
 	struct btrfs_extent_item *ei;
@@ -612,7 +612,7 @@ static void scrub_print_common_warning(const char *errstr, struct btrfs_device *
 	ret = extent_from_logical(fs_info, swarn.logical, path, &found_key,
 				  &flags);
 	if (ret < 0)
-		goto out;
+		return;
 
 	swarn.extent_item_size = found_key.offset;
 
@@ -658,9 +658,6 @@ static void scrub_print_common_warning(const char *errstr, struct btrfs_device *
 
 		iterate_extent_inodes(&ctx, true, scrub_print_warning_inode, &swarn);
 	}
-
-out:
-	btrfs_free_path(path);
 }
 
 static int fill_writer_pointer_gap(struct scrub_ctx *sctx, u64 physical)
@@ -2586,7 +2583,7 @@ int scrub_enumerate_chunks(struct scrub_ctx *sctx,
 			   struct btrfs_device *scrub_dev, u64 start, u64 end)
 {
 	struct btrfs_dev_extent *dev_extent = NULL;
-	struct btrfs_path *path;
+	BTRFS_PATH_AUTO_FREE(path);
 	struct btrfs_fs_info *fs_info = sctx->fs_info;
 	struct btrfs_root *root = fs_info->dev_root;
 	u64 chunk_offset;
@@ -2872,8 +2869,6 @@ int scrub_enumerate_chunks(struct scrub_ctx *sctx,
 		btrfs_release_path(path);
 	}
 
-	btrfs_free_path(path);
-
 	return ret;
 }
 
fs/btrfs/send.c +110 -197

843 lines not on this page · raw message

fs/btrfs/super.c +2 -7
index a262b494a89f..5e6eccd11fbe 100644
@@ -794,7 +794,7 @@ char *btrfs_get_subvol_name_from_objectid(struct btrfs_fs_info *fs_info,
 	struct btrfs_root_ref *root_ref;
 	struct btrfs_inode_ref *inode_ref;
 	struct btrfs_key key;
-	struct btrfs_path *path = NULL;
+	BTRFS_PATH_AUTO_FREE(path);
 	char *name = NULL, *ptr;
 	u64 dirid;
 	int len;
@@ -892,7 +892,6 @@ char *btrfs_get_subvol_name_from_objectid(struct btrfs_fs_info *fs_info,
 		fs_root = NULL;
 	}
 
-	btrfs_free_path(path);
 	if (ptr == name + PATH_MAX - 1) {
 		name[0] = '/';
 		name[1] = '\0';
@@ -903,7 +902,6 @@ char *btrfs_get_subvol_name_from_objectid(struct btrfs_fs_info *fs_info,
 
 err:
 	btrfs_put_root(fs_root);
-	btrfs_free_path(path);
 	kfree(name);
 	return ERR_PTR(ret);
 }
@@ -912,7 +910,7 @@ static int get_default_subvol_objectid(struct btrfs_fs_info *fs_info, u64 *objec
 {
 	struct btrfs_root *root = fs_info->tree_root;
 	struct btrfs_dir_item *di;
-	struct btrfs_path *path;
+	BTRFS_PATH_AUTO_FREE(path);
 	struct btrfs_key location;
 	struct fscrypt_str name = FSTR_INIT("default", 7);
 	u64 dir_id;
@@ -929,7 +927,6 @@ static int get_default_subvol_objectid(struct btrfs_fs_info *fs_info, u64 *objec
 	dir_id = btrfs_super_root_dir(fs_info->super_copy);
 	di = btrfs_lookup_dir_item(NULL, root, path, dir_id, &name, 0);
 	if (IS_ERR(di)) {
-		btrfs_free_path(path);
 		return PTR_ERR(di);
 	}
 	if (!di) {
@@ -938,13 +935,11 @@ static int get_default_subvol_objectid(struct btrfs_fs_info *fs_info, u64 *objec
 		 * it's always been there, but don't freak out, just try and
 		 * mount the top-level subvolume.
 		 */
-		btrfs_free_path(path);
 		*objectid = BTRFS_FS_TREE_OBJECTID;
 		return 0;
 	}
 
 	btrfs_dir_item_key_to_cpu(path->nodes[0], di, &location);
-	btrfs_free_path(path);
 	*objectid = location.objectid;
 	return 0;
 }
fs/btrfs/tree-log.c +43 -81

390 lines not on this page · raw message

Signature · 1 line
2.50.1
#6 lore raw
Quoted 2 lines · Sun YangKai

Trival pattern for the auto freeing with goto -> return convertions if possible. No other function cleanup.

Not all the changes match the trivial pattern described in the patches.

Quoted 27 lines
---
 fs/btrfs/raid-stripe-tree.c |  16 +-
 fs/btrfs/ref-verify.c       |   3 +-
 fs/btrfs/reflink.c          |   3 +-
 fs/btrfs/relocation.c       |  66 +++-----
 fs/btrfs/root-tree.c        |  49 +++---
 fs/btrfs/scrub.c            |  11 +-
 fs/btrfs/send.c             | 307 +++++++++++++-----------------------
 fs/btrfs/super.c            |   9 +-
 fs/btrfs/tree-log.c         | 124 +++++----------
 9 files changed, 204 insertions(+), 384 deletions(-)
fs/btrfs/raid-stripe-tree.c +1 -1
index cab0b291088c..231107cafab2 100644
@@ -67,7 +67,7 @@ int btrfs_delete_raid_extent(struct btrfs_trans_handle *trans, u64 start, u64 le
 {
 	struct btrfs_fs_info *fs_info = trans->fs_info;
 	struct btrfs_root *stripe_root = fs_info->stripe_root;
-	struct btrfs_path *path;
+	BTRFS_PATH_AUTO_FREE(path);
 	struct btrfs_key key;
 	struct extent_buffer *leaf;
 	u64 found_start;

fs/btrfs/raid-stripe-tree.c int btrfs_delete_raid_extent(struct btrfs_trans_handle *trans, u64 start, u64 le · line 264

Quoted 6 lines
fs/btrfs/raid-stripe-tree.c +0 -1
@@ -260,7 +260,6 @@ int btrfs_delete_raid_extent(struct btrfs_trans_handle *trans, u64 start, u64 le
 		btrfs_release_path(path);
 	}
 
-	btrfs_free_path(path);
 	return ret;
 }

As an example, this is the pattern, just declare, allocate and free at the end.

Quoted 29 lines
fs/btrfs/raid-stripe-tree.c +3 -4
@@ -376,7 +374,7 @@ int btrfs_get_raid_extent_offset(struct btrfs_fs_info *fs_info,
 	struct btrfs_stripe_extent *stripe_extent;
 	struct btrfs_key stripe_key;
 	struct btrfs_key found_key;
-	struct btrfs_path *path;
+	BTRFS_PATH_AUTO_FREE(path);
 	struct extent_buffer *leaf;
 	const u64 end = logical + *length;
 	int num_stripes;
@@ -402,7 +400,7 @@ int btrfs_get_raid_extent_offset(struct btrfs_fs_info *fs_info,
 
 	ret = btrfs_search_slot(NULL, stripe_root, &stripe_key, path, 0, 0);
 	if (ret < 0)
-		goto free_path;
+		return ret;
 	if (ret) {
 		if (path->slots[0] != 0)
 			path->slots[0]--;
@@ -459,8 +457,7 @@ int btrfs_get_raid_extent_offset(struct btrfs_fs_info *fs_info,
 		trace_btrfs_get_raid_extent_offset(fs_info, logical, *length,
 						   stripe->physical, devid);
 
-		ret = 0;
-		goto free_path;
+		return 0;
 	}
 
 	/* If we're here, we haven't found the requested devid in the stripe. */

fs/btrfs/raid-stripe-tree.c int btrfs_get_raid_extent_offset(struct btrfs_fs_info *fs_info, · line 475

Quoted 8 lines
fs/btrfs/raid-stripe-tree.c +0 -3
@@ -474,8 +471,5 @@ int btrfs_get_raid_extent_offset(struct btrfs_fs_info *fs_info,
 			  logical, logical + *length, stripe->dev->devid,
 			  btrfs_bg_type_to_raid_name(map_type));
 	}
-free_path:
-	btrfs_free_path(path);
-
 	return ret;
 }

This is also still trivial with the goto/return conversion as long as there's not additional logic around that.

Quoted 14 lines
fs/btrfs/relocation.c +1 -1
index 7256f6748c8f..8b08d6e4cb2b 100644
@@ -409,7 +409,7 @@ static noinline_for_stack struct btrfs_backref_node *build_backref_tree(
 	struct btrfs_backref_iter *iter;
 	struct btrfs_backref_cache *cache = &rc->backref_cache;
 	/* For searching parent of TREE_BLOCK_REF */
-	struct btrfs_path *path;
+	BTRFS_PATH_AUTO_FREE(path);
 	struct btrfs_backref_node *cur;
 	struct btrfs_backref_node *node = NULL;
 	struct btrfs_backref_edge *edge;

fs/btrfs/relocation.c static noinline_for_stack struct btrfs_backref_node *build_backref_tree( · line 464

Quoted 4 lines
fs/btrfs/relocation.c +0 -1
@@ -461,7 +461,6 @@ static noinline_for_stack struct btrfs_backref_node *build_backref_tree(
 out:
 	btrfs_free_path(iter->path);
 	kfree(iter);
-	btrfs_free_path(path);

In this case it depends, there are a few more statements after the freeing which means the path is still allocated until the function ends. If the statements are something quick, like in this case then it's still OK and considered trivial.

fs/btrfs/relocation.c static noinline_for_stack int merge_reloc_root(struct reloc_control *rc, · line 1664

Quoted 9 lines
fs/btrfs/relocation.c +0 -1
 	if (ret) {
 		btrfs_backref_error_cleanup(cache, node);
 		return ERR_PTR(ret);
@@ -1661,8 +1651,6 @@ static noinline_for_stack int merge_reloc_root(struct reloc_control *rc,
 	btrfs_tree_unlock(leaf);
 	free_extent_buffer(leaf);
 out:
-	btrfs_free_path(path);

Similar to the previous one but is not trivial, due to calls to insert_dirty_subvol(), btrfs_end_transaction_throttle(), btrfs_btree_balance_dirty() and invalidate_extent_cache(). Please have a look what the functions do.

Quoted 15 lines
fs/btrfs/relocation.c +1 -2
-
 	if (ret == 0) {
 		ret = insert_dirty_subvol(trans, rc, root);
 		if (ret)
@@ -4080,7 +4058,7 @@ int btrfs_recover_relocation(struct btrfs_fs_info *fs_info)
 	struct btrfs_key key;
 	struct btrfs_root *fs_root;
 	struct btrfs_root *reloc_root;
-	struct btrfs_path *path;
+	BTRFS_PATH_AUTO_FREE(path);
 	struct extent_buffer *leaf;
 	struct reloc_control *rc = NULL;
 	struct btrfs_trans_handle *trans;

fs/btrfs/relocation.c int btrfs_recover_relocation(struct btrfs_fs_info *fs_info) · line 4232

Quoted 4 lines
fs/btrfs/relocation.c +0 -1
@@ -4229,8 +4207,6 @@ int btrfs_recover_relocation(struct btrfs_fs_info *fs_info)
 out:
 	free_reloc_roots(&reloc_roots);
 
-	btrfs_free_path(path);

Same non-trivial case, there's btrfs_orphan_cleanup() after that.

fs/btrfs/relocation.c int btrfs_recover_relocation(struct btrfs_fs_info *fs_info) · line 4212

Quoted 4 lines
fs/btrfs/relocation.c +0 -1
-
 	if (ret == 0) {
 		/* cleanup orphan inode in data relocation tree */
 		fs_root = btrfs_grab_root(fs_info->data_reloc_root);

Please split the patch to parts that have the described trivial changes, and then one patch per function in case it's not trivial and needs some adjustments.

The freeing followed by other code can be still converted to auto cleaning but there must be an explicit path = NULL after the free.

#7 lore raw
Quoted 86 lines · Sun YangKai
Quoted 2 lines

Trival pattern for the auto freeing with goto -> return convertions if possible. No other function cleanup.

Not all the changes match the trivial pattern described in the patches.

Quoted 42 lines

---

 fs/btrfs/raid-stripe-tree.c |  16 +-
 fs/btrfs/ref-verify.c       |   3 +-
 fs/btrfs/reflink.c          |   3 +-
 fs/btrfs/relocation.c       |  66 +++-----
 fs/btrfs/root-tree.c        |  49 +++---
 fs/btrfs/scrub.c            |  11 +-
 fs/btrfs/send.c             | 307 +++++++++++++-----------------------
 fs/btrfs/super.c            |   9 +-
 fs/btrfs/tree-log.c         | 124 +++++----------
 9 files changed, 204 insertions(+), 384 deletions(-)
fs/btrfs/raid-stripe-tree.c +1 -1
index cab0b291088c..231107cafab2 100644
@@ -67,7 +67,7 @@ int btrfs_delete_raid_extent(struct btrfs_trans_handle
 *trans, u64 start, u64 le> 
 {
 
 	struct btrfs_fs_info *fs_info = trans->fs_info;
 	struct btrfs_root *stripe_root = fs_info->stripe_root;
 
-	struct btrfs_path *path;
+	BTRFS_PATH_AUTO_FREE(path);
fs/btrfs/raid-stripe-tree.c +0 -1
 	struct btrfs_key key;
 	struct extent_buffer *leaf;
 	u64 found_start;
@@ -260,7 +260,6 @@ int btrfs_delete_raid_extent(struct btrfs_trans_handle
 *trans, u64 start, u64 le> 
 		btrfs_release_path(path);
 
 	}
 
-	btrfs_free_path(path);
 

return ret;

Quoted 1 line

}

As an example, this is the pattern, just declare, allocate and free at the end.

Quoted 33 lines
fs/btrfs/raid-stripe-tree.c +1 -1
@@ -376,7 +374,7 @@ int btrfs_get_raid_extent_offset(struct btrfs_fs_info
 *fs_info,> 
 	struct btrfs_stripe_extent *stripe_extent;
 	struct btrfs_key stripe_key;
 	struct btrfs_key found_key;
 
-	struct btrfs_path *path;
+	BTRFS_PATH_AUTO_FREE(path);
 
fs/btrfs/raid-stripe-tree.c +1 -1
 	struct extent_buffer *leaf;
 	const u64 end = logical + *length;
 	int num_stripes;
@@ -402,7 +400,7 @@ int btrfs_get_raid_extent_offset(struct btrfs_fs_info
 *fs_info,> 
 	ret = btrfs_search_slot(NULL, stripe_root, &stripe_key, path, 0, 0);
 	if (ret < 0)
 
-		goto free_path;
+		return ret;
 
 	if (ret) {
fs/btrfs/raid-stripe-tree.c +0 -0
 		if (path->slots[0] != 0)
 
 			path->slots[0]--;
@@ -459,8 +457,7 @@ int btrfs_get_raid_extent_offset(struct btrfs_fs_info
 *fs_info,> 
 		trace_btrfs_get_raid_extent_offset(fs_info, logical, *length,
 
 						   stripe->physical, devid);
 
Quoted 7 lines

fs/btrfs/raid-stripe-tree.c

Quoted 7 lines
fs/btrfs/raid-stripe-tree.c +1 -2
-		ret = 0;
-		goto free_path;
+		return 0;
 
fs/btrfs/raid-stripe-tree.c +0 -0
 	}
 
 	/* If we're here, we haven't found the requested devid in the stripe. 

*/

Quoted 103 lines
Quoted 5 lines
fs/btrfs/raid-stripe-tree.c +0 -0
@@ -474,8 +471,5 @@ int btrfs_get_raid_extent_offset(struct btrfs_fs_info
 *fs_info,> 
 			  logical, logical + *length, stripe->dev->devid,
 			  btrfs_bg_type_to_raid_name(map_type));

fs/btrfs/raid-stripe-tree.c int btrfs_get_raid_extent_offset(struct btrfs_fs_info · line 475

Quoted 10 lines
fs/btrfs/raid-stripe-tree.c +0 -3
 
 	}
 
-free_path:
-	btrfs_free_path(path);
-
 
 	return ret;
 
 }

This is also still trivial with the goto/return conversion as long as there's not additional logic around that.

Quoted 18 lines
fs/btrfs/relocation.c +1 -1
index 7256f6748c8f..8b08d6e4cb2b 100644
@@ -409,7 +409,7 @@ static noinline_for_stack struct btrfs_backref_node
 *build_backref_tree(> 
 	struct btrfs_backref_iter *iter;
 	struct btrfs_backref_cache *cache = &rc->backref_cache;
 	/* For searching parent of TREE_BLOCK_REF */
 
-	struct btrfs_path *path;
+	BTRFS_PATH_AUTO_FREE(path);
 
fs/btrfs/relocation.c +0 -0
 	struct btrfs_backref_node *cur;
 	struct btrfs_backref_node *node = NULL;
 	struct btrfs_backref_edge *edge;

fs/btrfs/relocation.c static noinline_for_stack struct btrfs_backref_node · line 466

Quoted 6 lines
fs/btrfs/relocation.c +0 -1
@@ -461,7 +461,6 @@ static noinline_for_stack struct btrfs_backref_node
 *build_backref_tree(> 
 out:
 	btrfs_free_path(iter->path);
 	kfree(iter);
 
-	btrfs_free_path(path);

In this case it depends, there are a few more statements after the freeing which means the path is still allocated until the function ends. If the statements are something quick, like in this case then it's still OK and considered trivial.

fs/btrfs/relocation.c static noinline_for_stack int merge_reloc_root(struct reloc_control *rc, · line 1664

Quoted 12 lines
fs/btrfs/relocation.c +0 -0
 	if (ret) {
fs/btrfs/relocation.c +0 -0
 		btrfs_backref_error_cleanup(cache, node);
 		return ERR_PTR(ret);
@@ -1661,8 +1651,6 @@ static noinline_for_stack int

merge_reloc_root(struct reloc_control *rc,>

fs/btrfs/relocation.c +0 -1
 	btrfs_tree_unlock(leaf);
 	free_extent_buffer(leaf);
 
 out:
-	btrfs_free_path(path);

Similar to the previous one but is not trivial, due to calls to insert_dirty_subvol(), btrfs_end_transaction_throttle(), btrfs_btree_balance_dirty() and invalidate_extent_cache(). Please have a look what the functions do.

Quoted 21 lines
fs/btrfs/relocation.c +1 -2
-
 
 	if (ret == 0) {
 
 		ret = insert_dirty_subvol(trans, rc, root);
 		if (ret)
@@ -4080,7 +4058,7 @@ int btrfs_recover_relocation(struct btrfs_fs_info
 *fs_info)> 
 	struct btrfs_key key;
 	struct btrfs_root *fs_root;
 	struct btrfs_root *reloc_root;
 
-	struct btrfs_path *path;
+	BTRFS_PATH_AUTO_FREE(path);
 
fs/btrfs/relocation.c +0 -0
 	struct extent_buffer *leaf;
 	struct reloc_control *rc = NULL;
 	struct btrfs_trans_handle *trans;

fs/btrfs/relocation.c int btrfs_recover_relocation(struct btrfs_fs_info · line 4233

Quoted 5 lines
fs/btrfs/relocation.c +0 -1
@@ -4229,8 +4207,6 @@ int btrfs_recover_relocation(struct btrfs_fs_info
 *fs_info)> 
 out:
 	free_reloc_roots(&reloc_roots);
 
-	btrfs_free_path(path);

Same non-trivial case, there's btrfs_orphan_cleanup() after that.

fs/btrfs/relocation.c int btrfs_recover_relocation(struct btrfs_fs_info *fs_info) · line 4212

Quoted 6 lines
fs/btrfs/relocation.c +0 -1
-
 
 	if (ret == 0) {
fs/btrfs/relocation.c +0 -0
 		/* cleanup orphan inode in data relocation tree */
 		fs_root = btrfs_grab_root(fs_info->data_reloc_root);
Quoted 6 lines

Please split the patch to parts that have the described trivial changes, and then one patch per function in case it's not trivial and needs some adjustments.

The freeing followed by other code can be still converted to auto cleaning but there must be an explicit path = NULL after the free.

Got it. Thank you for your patience and kind reply. I'll go over those changes again.

Best regards,
Sun YangKai

#8 lore raw
In reply to David Sterba
Quoted 4 lines

Please split the patch to parts that have the described trivial changes, and then one patch per function in case it's not trivial and needs some adjustments.

After learning more about the auto-free/cleanup mechanism, I realized that its only advantage is to eliminate the need for the goto out; pattern. Therefore, it seems unnecessary to apply this conversion in non-trivial cases.

Moreover, if the cleanup code contains other logic, it might be better to leave it unchanged even in trivial cases.

Quoted 2 lines

The freeing followed by other code can be still converted to auto cleaning but there must be an explicit path = NULL after the free.

I'm sorry, I didn't understand. If the freeing is followed by other code, maybe we could just leave them untouched?

Please let me know if I've got anything wrong.

Best regards,
Sun Yangkai

#9 lore raw
Quoted 7 lines · Sun YangKai
Quoted 3 lines

Please split the patch to parts that have the described trivial changes, and then one patch per function in case it's not trivial and needs some adjustments.

After learning more about the auto-free/cleanup mechanism, I realized that its only advantage is to eliminate the need for the goto out; pattern. Therefore, it seems unnecessary to apply this conversion in non-trivial cases.

I wouldn't say it's the only advantage, the code readability is also improved. The path is an auxiliary object and if the freeing is handled automatically then it reduces the cognitive load and the error cleanup paths.

Quoted 2 lines

Moreover, if the cleanup code contains other logic, it might be better to leave it unchanged even in trivial cases.

Depends on what we want. So far we've started with the path auto cleaning but there are more possibilities like using the raw __free cleanup with kfree. If this is combined and leads to simpler exit and cleanup blocks I think it's worth. In the trivial cases it's clear it does not interfere with the rest of the code and does not complicate any logic there.

Quoted 5 lines
Quoted 2 lines

The freeing followed by other code can be still converted to auto cleaning but there must be an explicit path = NULL after the free.

I'm sorry, I didn't understand. If the freeing is followed by other code, maybe we could just leave them untouched?

Maybe yes, this is up to consideration on a per site basis, I've seen examples where conversion to auto path cleaning would not hurt.

Let me know if you want to continue with this because I think you don't seem to see the value in the conversions (which is fine of course).

#10 lore raw
Quoted 27 lines · Sun YangKai
Quoted 8 lines
Quoted 3 lines

Please split the patch to parts that have the described trivial changes, and then one patch per function in case it's not trivial and needs some adjustments.

After learning more about the auto-free/cleanup mechanism, I realized that its only advantage is to eliminate the need for the goto out; pattern. Therefore, it seems unnecessary to apply this conversion in non-trivial cases.

I wouldn't say it's the only advantage, the code readability is also improved. The path is an auxiliary object and if the freeing is handled automatically then it reduces the cognitive load and the error cleanup paths.

Quoted 2 lines

Moreover, if the cleanup code contains other logic, it might be better to leave it unchanged even in trivial cases.

Depends on what we want. So far we've started with the path auto cleaning but there are more possibilities like using the raw __free cleanup with kfree. If this is combined and leads to simpler exit and cleanup blocks I think it's worth. In the trivial cases it's clear it does not interfere with the rest of the code and does not complicate any logic there.

Quoted 3 lines
Quoted 2 lines

The freeing followed by other code can be still converted to auto cleaning but there must be an explicit path = NULL after the free.

Quoted 8 lines
Quoted 2 lines

I'm sorry, I didn't understand. If the freeing is followed by other code, maybe we could just leave them untouched?

Maybe yes, this is up to consideration on a per site basis, I've seen examples where conversion to auto path cleaning would not hurt.

Let me know if you want to continue with this because I think you don't seem to see the value in the conversions (which is fine of course).

I apologize if my previous comments came across as unclear — I certainly see the value in the conversions. My intention was actually to discuss in which specific scenarios these conversions would be most proper, so that we can align on how to apply them.

Based on your suggestions, I'll restrict the conversions to the following scenarios:

1. Cases where there are no operations between btrfs_free_path and the function return.
2. Cases where only simple cleanup operations (such as kfree, kvfree, clear_bit, and fs_path_free) are present between btrfs_free_path and the function return.

Please let me know if these criteria align with what you had in mind.

Thanks,
Sun Yangkai

#11 lore raw
Quoted 41 lines · Sun YangKai
Quoted 27 lines · Sun YangKai
Quoted 8 lines
Quoted 3 lines

Please split the patch to parts that have the described trivial changes, and then one patch per function in case it's not trivial and needs some adjustments.

After learning more about the auto-free/cleanup mechanism, I realized that its only advantage is to eliminate the need for the goto out; pattern. Therefore, it seems unnecessary to apply this conversion in non-trivial cases.

I wouldn't say it's the only advantage, the code readability is also improved. The path is an auxiliary object and if the freeing is handled automatically then it reduces the cognitive load and the error cleanup paths.

Quoted 2 lines

Moreover, if the cleanup code contains other logic, it might be better to leave it unchanged even in trivial cases.

Depends on what we want. So far we've started with the path auto cleaning but there are more possibilities like using the raw __free cleanup with kfree. If this is combined and leads to simpler exit and cleanup blocks I think it's worth. In the trivial cases it's clear it does not interfere with the rest of the code and does not complicate any logic there.

Quoted 3 lines
Quoted 2 lines

The freeing followed by other code can be still converted to auto cleaning but there must be an explicit path = NULL after the free.

Quoted 8 lines
Quoted 2 lines

I'm sorry, I didn't understand. If the freeing is followed by other code, maybe we could just leave them untouched?

Maybe yes, this is up to consideration on a per site basis, I've seen examples where conversion to auto path cleaning would not hurt.

Let me know if you want to continue with this because I think you don't seem to see the value in the conversions (which is fine of course).

I apologize if my previous comments came across as unclear — I certainly see the value in the conversions. My intention was actually to discuss in which specific scenarios these conversions would be most proper, so that we can align on how to apply them.

Quoted 5 lines

Based on your suggestions, I'll restrict the conversions to the following scenarios:

1. Cases where there are no operations between btrfs_free_path and the function return.

OK

Quoted 3 lines

2. Cases where only simple cleanup operations (such as kfree, kvfree, clear_bit, and fs_path_free) are present between btrfs_free_path and the function return.

Also OK

Quoted 1 line

Please let me know if these criteria align with what you had in mind.

Yes please do the trivial ones described above, we'll see what's left after that.