Skip to contentSkip to conversation

btrfs: more trivial BTRFS_PATH_AUTO_FREE conversions

PATCH Superseded Sun YangKai 4 participants 6 messages Aug 29, 2025 – Sep 1, 2025 Btrfs file system

v3 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

PATCH v3 1/1 btrfs: more trivial BTRFS_PATH_AUTO_FREE conversions

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

The following cases are considered trivial in this patch:
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.

---

Changelog:
v1 -> v3:
* Directly return 0 if info is NULL in send.c:get_inode_info()
* Limit the scope of the conversion to only what is described in the commit message.

---
 fs/btrfs/raid-stripe-tree.c |  15 +-
 fs/btrfs/ref-verify.c       |   3 +-
 fs/btrfs/reflink.c          |   3 +-
 fs/btrfs/relocation.c       |  47 ++----
 fs/btrfs/root-tree.c        |  52 +++---
 fs/btrfs/scrub.c            |  11 +-
 fs/btrfs/send.c             | 312 +++++++++++++-----------------------
 fs/btrfs/super.c            |   5 +-
 fs/btrfs/transaction.c      |   3 +-
 fs/btrfs/tree-log.c         | 107 +++++--------
 10 files changed, 200 insertions(+), 358 deletions(-)
fs/btrfs/raid-stripe-tree.c +5 -10
index cab0b291088c..a69eaf0cef47 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,6 @@ 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 +16 -31

128 lines not on this page · raw message

fs/btrfs/root-tree.c +19 -33

160 lines not on this page · raw message

fs/btrfs/scrub.c +3 -8
index 6776e6ab8d10..e23ce7887189 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 ret;
 
 	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 +114 -198

831 lines not on this page · raw message

fs/btrfs/super.c +1 -4
index a262b494a89f..a05128b0db31 100644
@@ -912,7 +912,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 +929,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 +937,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/transaction.c +1 -2
index c5c0d9cf1a80..1fea2aa7f81e 100644
@@ -1641,7 +1641,7 @@ static noinline int create_pending_snapshot(struct btrfs_trans_handle *trans,
 	struct btrfs_root *parent_root;
 	struct btrfs_block_rsv *rsv;
 	struct btrfs_inode *parent_inode = pending->dir;
-	struct btrfs_path *path;
+	BTRFS_PATH_AUTO_FREE(path);
 	struct btrfs_dir_item *dir_item;
 	struct extent_buffer *tmp;
 	struct extent_buffer *old;
@@ -1907,7 +1907,6 @@ static noinline int create_pending_snapshot(struct btrfs_trans_handle *trans,
 free_pending:
 	kfree(new_root_item);
 	pending->root_item = NULL;
-	btrfs_free_path(path);
 	pending->path = NULL;
 
 	return ret;
fs/btrfs/tree-log.c +39 -68

329 lines not on this page · raw message

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

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

The following cases are considered trivial in this patch:
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.

Quoted 7 lines

---

Changelog:
v1 -> v3:
* Directly return 0 if info is NULL in send.c:get_inode_info()
* Limit the scope of the conversion to only what is described in the commit message.

Thanks, this looks good. If you find more cases for conversion, please send them. There were some minor conflicts with this patch so please use https://github.com/btrfs/linux branch for-next as base.

#2 lore raw
Quoted 21 lines · David Sterba
Quoted 12 lines · Sun YangKai

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

The following cases are considered trivial in this patch:
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.

Quoted 7 lines

---

Changelog:
v1 -> v3:
* Directly return 0 if info is NULL in send.c:get_inode_info()
* Limit the scope of the conversion to only what is described in the commit message.

Quoted 4 lines

Thanks, this looks good. If you find more cases for conversion, please send them. There were some minor conflicts with this patch so please use https://github.com/btrfs/linux branch for-next as base.

I guess no one has yet tried to run any test case with this seemingly safe conversion.

It will easily hang the fs, with the following blocked processes:

[   85.124607] sysrq: Show Blocked State
[   85.125981] task:btrfs-transacti state:D stack:0     pid:1302 tgid:1302  ppid:2      task_flags:0x208040 flags:0x00004000
[   85.129163] Call Trace:
[   85.130115]  <TASK>
[   85.130813]  __schedule+0x40d/0x12c0
[   85.131883]  ? psi_task_switch+0x113/0x290
[   85.133119]  ? finish_task_switch.isra.0+0x92/0x290
[   85.134584]  schedule+0x27/0xd0
[   85.135567]  wait_current_trans+0x107/0x170 [btrfs 72a0a5602f44912ca5c15cad9f44c855141eb851]
[   85.138128]  ? __sched_core_set+0x1e0/0x1e0
[   85.139375]  start_transaction+0x383/0x830 [btrfs 72a0a5602f44912ca5c15cad9f44c855141eb851]
[   85.141872]  transaction_kthread+0xb8/0x1b0 [btrfs 72a0a5602f44912ca5c15cad9f44c855141eb851]
[   85.144361]  ? btrfs_cleanup_transaction.isra.0+0x5d0/0x5d0 [btrfs 72a0a5602f44912ca5c15cad9f44c855141eb851]
[   85.147191]  kthread+0xf9/0x240
[   85.148207]  ? kthreads_online_cpu+0x130/0x130
[   85.149517]  ret_from_fork+0x17b/0x1a0
[   85.150580]  ? kthreads_online_cpu+0x130/0x130
[   85.151828]  ret_from_fork_asm+0x11/0x20
[   85.153028]  </TASK>
[   85.153682] task:sync            state:D stack:0     pid:1311 tgid:1311  ppid:1310   task_flags:0x400100 flags:0x00004002
[   85.156609] Call Trace:
[   85.157388]  <TASK>
[   85.158041]  __schedule+0x40d/0x12c0
[   85.159143]  ? asm_sysvec_apic_timer_interrupt+0x1a/0x20
[   85.160732]  schedule+0x27/0xd0
[   85.161728]  schedule_preempt_disabled+0x15/0x20
[   85.163141]  rwsem_down_write_slowpath+0x1fe/0x760
[   85.164563]  ? xas_load+0x9/0xc0
[   85.165531]  ? xas_find+0x136/0x180
[   85.166625]  down_write+0x52/0x60
[   85.167713]  btrfs_tree_lock_nested+0x1d/0x90 [btrfs 72a0a5602f44912ca5c15cad9f44c855141eb851]
[   85.170405]  btrfs_lock_root_node+0x35/0x50 [btrfs 72a0a5602f44912ca5c15cad9f44c855141eb851]
[   85.172992]  commit_cowonly_roots+0x49/0x260 [btrfs 72a0a5602f44912ca5c15cad9f44c855141eb851]
[   85.175405]  btrfs_commit_transaction+0x326/0xd70 [btrfs 72a0a5602f44912ca5c15cad9f44c855141eb851]
[   85.178077]  ? vfs_fsync_range+0x90/0x90
[   85.179206]  ? btrfs_attach_transaction_barrier+0x25/0x60 [btrfs 72a0a5602f44912ca5c15cad9f44c855141eb851]
[   85.182205]  ? vfs_fsync_range+0x90/0x90
[   85.183431]  __iterate_supers+0xd6/0x160
[   85.184616]  ksys_sync+0x63/0xa0
[   85.185608]  __do_sys_sync+0xe/0x20
[   85.186719]  do_syscall_64+0x82/0xae0
[   85.187858]  ? obj_cgroup_charge_account+0x145/0x410
[   85.189343]  ? ___pte_offset_map+0x1b/0x150
[   85.190601]  ? set_pte_range+0xf2/0x290
[   85.191775]  ? next_uptodate_folio+0x89/0x2a0
[   85.193094]  ? filemap_map_pages+0x413/0x680
[   85.194383]  ? __alloc_frozen_pages_noprof+0x19c/0x370
[   85.195903]  ? do_fault+0x342/0x5a0
[   85.196968]  ? __handle_mm_fault+0x8c4/0xf00
[   85.198265]  ? count_memcg_events+0xba/0x180
[   85.199570]  ? handle_mm_fault+0x1d3/0x2d0
[   85.200865]  ? do_user_addr_fault+0x216/0x690
[   85.202156]  ? exc_page_fault+0x7e/0x1a0
[   85.203308]  entry_SYSCALL_64_after_hwframe+0x4b/0x53
[   85.204739] RIP: 0033:0x7f4e4634ce7b
[   85.205813] RSP: 002b:00007ffd7485f3c8 EFLAGS: 00000246 ORIG_RAX: 00000000000000a2
[   85.208142] RAX: ffffffffffffffda RBX: 00007ffd7485f5c8 RCX: 00007f4e4634ce7b
[   85.210268] RDX: 00007f4e4641c201 RSI: 00007f4e4641c248 RDI: 00007f4e463e65c9
[   85.212549] RBP: 00007ffd7485f4a0 R08: 0000000000000000 R09: 0000000000000000
[   85.214726] R10: 0000000000000004 R11: 0000000000000246 R12: 00007f4e4641c248
[   85.216901] R13: 0000000000000001 R14: 0000559279cf61d0 R15: 0000559279cf7b60
[   85.218907]  </TASK>

I believe it's some path not properly released.

So I have removed it from for-next for now.

Thanks,
Qu

#3 lore raw

My bad.

I mistakenly removed some cleanup code without converting it to use BTRFS_PATH_AUTO_FREE. Thanks a lot for catching this.

#4 lore raw
Quoted 4 lines

My bad.

I mistakenly removed some cleanup code without converting it to use BTRFS_PATH_AUTO_FREE. Thanks a lot for catching this.

Given the number of code lines, it's difficult to identify exactly what was missing, so I wrote a Python script to help verify the patch. Hopefully, we haven't missed anything this time.

Here are the missing parts:

fs/btrfs/root-tree.c +0 -0
index c7f7c55b7c28..d1f5f6c42ef3 100644
@@ -130,7 +130,7 @@ int btrfs_update_root(struct btrfs_trans_handle *trans, 

struct btrfs_root

                      *item)
 {
        struct btrfs_fs_info *fs_info = root->fs_info;
-       struct btrfs_path *path;
+       BTRFS_PATH_AUTO_FREE(path);
        struct extent_buffer *l;
        int ret;
        int slot;
fs/btrfs/tree-log.c +0 -0
index b32e2f2e5436..06d45890df85 100644
@@ -1717,7 +1717,7 @@ static noinline int fixup_inode_link_count(struct 

btrfs_trans_handle *trans,
struct btrfs_inode *inode)

 {
        struct btrfs_root *root = inode->root;
-       struct btrfs_path *path;
+       BTRFS_PATH_AUTO_FREE(path);
        int ret;
        u64 nlink = 0;
        const u64 ino = btrfs_ino(inode);
#5 lore raw
Quoted 9 lines · Sun YangKai
Quoted 5 lines

My bad.

I mistakenly removed some cleanup code without converting it to use BTRFS_PATH_AUTO_FREE. Thanks a lot for catching this.

Given the number of code lines, it's difficult to identify exactly what was missing, so I wrote a Python script to help verify the patch. Hopefully, we haven't missed anything this time.

Suggestion: test your patches with fstests - it would have avoided this problem as well as the previous one with the send change reported by the test robot [1].

Thanks.

[1] https://lore.kernel.org/linux-btrfs/[email protected]/

Quoted 36 lines

Here are the missing parts:

fs/btrfs/root-tree.c +0 -0
index c7f7c55b7c28..d1f5f6c42ef3 100644
@@ -130,7 +130,7 @@ int btrfs_update_root(struct btrfs_trans_handle *trans,

struct btrfs_root

                      *item)
 {
        struct btrfs_fs_info *fs_info = root->fs_info;
fs/btrfs/root-tree.c +1 -1
-       struct btrfs_path *path;
+       BTRFS_PATH_AUTO_FREE(path);
        struct extent_buffer *l;
        int ret;
        int slot;
fs/btrfs/tree-log.c +1 -1
index b32e2f2e5436..06d45890df85 100644
@@ -1717,7 +1717,7 @@ static noinline int fixup_inode_link_count(struct
 btrfs_trans_handle *trans,
                                           struct btrfs_inode *inode)
 {
        struct btrfs_root *root = inode->root;
-       struct btrfs_path *path;
+       BTRFS_PATH_AUTO_FREE(path);
        int ret;
        u64 nlink = 0;
        const u64 ino = btrfs_ino(inode);