Skip to contentSkip to conversation

btrfs: Remove superflous lock acquisition in __del_reloc_root

PATCH In review Nikolay Borisov 3 participants 7 messages Feb 21, 2020 – Apr 10, 2020 Btrfs file system

lore
Share & tools
lore
#0 lore raw

PATCH 1/2 btrfs: Remove superflous lock acquisition in __del_reloc_root

__del_reloc_root is called from:

a) Transaction commit via:
btrfs_transaction_commit
commit_fs_roots
btrfs_update_reloc_root
__del_reloc_root

b) From the relocation thread with the following call chains:

relocate_block_group
merge_reloc_roots
insert_dirty_subvol
btrfs_update_reloc_root
__del_reloc_root

c) merge_reloc_roots
free_reloc_roots
__del_reloc_roots

(The above call chain can called from btrfs_recover_relocation as well but for the purpose of this fix this is irrelevant).

The commont data structure that needs protecting is fs_info->reloc_ctl->reloc_list as reloc roots are anchored at this list. Turns out it's no needed to hold the trans_lock in __del_reloc_root since consistency is already guaranteed by call chain b) above holding a transaction while calling insert_dirty_subvol, meaning we cannot have a concurrent transaction commit. For call chain c) above a snapshot of the fs_info->reloc_ctl->reloc_list is taken with reloc_mutex held and free_reloc_roots is called on this local snapshot.

Those invariants are sufficient to prevent racing calls to __del_reloc_root alongside other users of the list, as such it's fine to drop the lock acquisition.

---
 fs/btrfs/relocation.c | 2 --
 1 file changed, 2 deletions(-)
fs/btrfs/relocation.c +0 -2
index 8076c340749f..e5cb64409f7c 100644
@@ -1381,9 +1381,7 @@ static void __del_reloc_root(struct btrfs_root *root)
 		BUG_ON((struct btrfs_root *)node->data != root);
 	}
 
-	spin_lock(&fs_info->trans_lock);
 	list_del_init(&root->root_list);
-	spin_unlock(&fs_info->trans_lock);
 	kfree(node);
 }
 
Signature · 1 line
2.17.1
#1 lore raw

PATCH 2/2 btrfs: Use list_for_each_entry_safe in free_reloc_roots

The function always works on a local copy of the reloc root list, which cannot be modified outside of it so using list_for_each_entry is fine. Additionally the macro handles empty lists so drop list_empty checks of callers. No semantic changes.

---
 fs/btrfs/relocation.c | 16 +++++-----------
 1 file changed, 5 insertions(+), 11 deletions(-)
fs/btrfs/relocation.c +5 -11
index e5cb64409f7c..f13b79adf6b0 100644
@@ -2523,13 +2523,10 @@ int prepare_to_merge(struct reloc_control *rc, int err)
 static noinline_for_stack
 void free_reloc_roots(struct list_head *list)
 {
-	struct btrfs_root *reloc_root;
+	struct btrfs_root *reloc_root, *tmp;
 
-	while (!list_empty(list)) {
-		reloc_root = list_entry(list->next, struct btrfs_root,
-					root_list);
+	list_for_each_entry_safe(reloc_root, tmp, list, root_list)
 		__del_reloc_root(reloc_root);
-	}
 }
 
 static noinline_for_stack
@@ -2588,15 +2585,13 @@ void merge_reloc_roots(struct reloc_control *rc)
 out:
 	if (ret) {
 		btrfs_handle_fs_error(fs_info, ret, NULL);
-		if (!list_empty(&reloc_roots))
-			free_reloc_roots(&reloc_roots);
+		free_reloc_roots(&reloc_roots);
 
 		/* new reloc root may be added */
 		mutex_lock(&fs_info->reloc_mutex);
 		list_splice_init(&rc->reloc_roots, &reloc_roots);
 		mutex_unlock(&fs_info->reloc_mutex);
-		if (!list_empty(&reloc_roots))
-			free_reloc_roots(&reloc_roots);
+		free_reloc_roots(&reloc_roots);
 	}
 
 	BUG_ON(!RB_EMPTY_ROOT(&rc->reloc_root_tree.rb_root));
@@ -4689,8 +4684,7 @@ int btrfs_recover_relocation(struct btrfs_root *root)
 out_free:
 	kfree(rc);
 out:
-	if (!list_empty(&reloc_roots))
-		free_reloc_roots(&reloc_roots);
+	free_reloc_roots(&reloc_roots);
 
 	btrfs_free_path(path);
 
Signature · 1 line
2.17.1
#2 lore raw

PATCH v2 2/2 btrfs: Use list_for_each_entry_safe in free_reloc_roots

The function always works on a local copy of the reloc root list, which cannot be modified outside of it so using list_for_each_entry is fine. Additionally the macro handles empty lists so drop list_empty checks of callers. No semantic changes.
---

V2: Base the patch on current misc-next rather than on a branch with Josef's subvol cleanups part 2.

 fs/btrfs/relocation.c | 15 +++++----------
 1 file changed, 5 insertions(+), 10 deletions(-)
fs/btrfs/relocation.c +5 -10
index 4b8c80de925b..a8898894f443 100644
@@ -2523,11 +2523,9 @@ int prepare_to_merge(struct reloc_control *rc, int err)
 static noinline_for_stack
 void free_reloc_roots(struct list_head *list)
 {
-	struct btrfs_root *reloc_root;
+	struct btrfs_root *reloc_root, *tmp;
 
-	while (!list_empty(list)) {
-		reloc_root = list_entry(list->next, struct btrfs_root,
-					root_list);
+	list_for_each_entry_safe(reloc_root, tmp, list, root_list) {
 		__del_reloc_root(reloc_root);
 		free_extent_buffer(reloc_root->node);
 		free_extent_buffer(reloc_root->commit_root);
@@ -2592,15 +2590,13 @@ void merge_reloc_roots(struct reloc_control *rc)
 out:
 	if (ret) {
 		btrfs_handle_fs_error(fs_info, ret, NULL);
-		if (!list_empty(&reloc_roots))
-			free_reloc_roots(&reloc_roots);
+		free_reloc_roots(&reloc_roots);
 
 		/* new reloc root may be added */
 		mutex_lock(&fs_info->reloc_mutex);
 		list_splice_init(&rc->reloc_roots, &reloc_roots);
 		mutex_unlock(&fs_info->reloc_mutex);
-		if (!list_empty(&reloc_roots))
-			free_reloc_roots(&reloc_roots);
+		free_reloc_roots(&reloc_roots);
 	}
 
 	BUG_ON(!RB_EMPTY_ROOT(&rc->reloc_root_tree.rb_root));
@@ -4693,8 +4689,7 @@ int btrfs_recover_relocation(struct btrfs_root *root)
 out_free:
 	kfree(rc);
 out:
-	if (!list_empty(&reloc_roots))
-		free_reloc_roots(&reloc_roots);
+	free_reloc_roots(&reloc_roots);
 
 	btrfs_free_path(path);
 
Signature · 1 line
2.17.1
Reviewed-by on v1 2/2
#3 lore raw

Re: [PATCH 2/2] btrfs: Use list_for_each_entry_safe in free_reloc_roots

In reply to Nikolay Borisov
Quoted 6 lines · Nikolay Borisov

The function always works on a local copy of the reloc root list, which cannot be modified outside of it so using list_for_each_entry is fine. Additionally the macro handles empty lists so drop list_empty checks of callers. No semantic changes.

Thanks,
Qu

Quoted 53 lines
---
 fs/btrfs/relocation.c | 16 +++++-----------
 1 file changed, 5 insertions(+), 11 deletions(-)
fs/btrfs/relocation.c +5 -11
index e5cb64409f7c..f13b79adf6b0 100644
@@ -2523,13 +2523,10 @@ int prepare_to_merge(struct reloc_control *rc, int err)
 static noinline_for_stack
 void free_reloc_roots(struct list_head *list)
 {
-	struct btrfs_root *reloc_root;
+	struct btrfs_root *reloc_root, *tmp;
 
-	while (!list_empty(list)) {
-		reloc_root = list_entry(list->next, struct btrfs_root,
-					root_list);
+	list_for_each_entry_safe(reloc_root, tmp, list, root_list)
 		__del_reloc_root(reloc_root);
-	}
 }
 
 static noinline_for_stack
@@ -2588,15 +2585,13 @@ void merge_reloc_roots(struct reloc_control *rc)
 out:
 	if (ret) {
 		btrfs_handle_fs_error(fs_info, ret, NULL);
-		if (!list_empty(&reloc_roots))
-			free_reloc_roots(&reloc_roots);
+		free_reloc_roots(&reloc_roots);
 
 		/* new reloc root may be added */
 		mutex_lock(&fs_info->reloc_mutex);
 		list_splice_init(&rc->reloc_roots, &reloc_roots);
 		mutex_unlock(&fs_info->reloc_mutex);
-		if (!list_empty(&reloc_roots))
-			free_reloc_roots(&reloc_roots);
+		free_reloc_roots(&reloc_roots);
 	}
 
 	BUG_ON(!RB_EMPTY_ROOT(&rc->reloc_root_tree.rb_root));
@@ -4689,8 +4684,7 @@ int btrfs_recover_relocation(struct btrfs_root *root)
 out_free:
 	kfree(rc);
 out:
-	if (!list_empty(&reloc_roots))
-		free_reloc_roots(&reloc_roots);
+	free_reloc_roots(&reloc_roots);
 
 	btrfs_free_path(path);
 
#4 lore raw

Re: [PATCH 2/2] btrfs: Use list_for_each_entry_safe in free_reloc_roots

In reply to Nikolay Borisov
Quoted 6 lines · Nikolay Borisov

The function always works on a local copy of the reloc root list, which cannot be modified outside of it so using list_for_each_entry is fine. Additionally the macro handles empty lists so drop list_empty checks of callers. No semantic changes.

This is still relevant for misc-next, so I'm applying it to misc-next.

Reviewed-by on v1 1/2
#5 lore raw
In reply to Nikolay Borisov
Quoted 32 lines · Nikolay Borisov

__del_reloc_root is called from:

a) Transaction commit via:
btrfs_transaction_commit
commit_fs_roots
btrfs_update_reloc_root
__del_reloc_root

b) From the relocation thread with the following call chains:

relocate_block_group
merge_reloc_roots
insert_dirty_subvol
btrfs_update_reloc_root
__del_reloc_root

c) merge_reloc_roots
free_reloc_roots
__del_reloc_roots

(The above call chain can called from btrfs_recover_relocation as well but for the purpose of this fix this is irrelevant).

The commont data structure that needs protecting is fs_info->reloc_ctl->reloc_list as reloc roots are anchored at this list. Turns out it's no needed to hold the trans_lock in __del_reloc_root since consistency is already guaranteed by call chain b) above holding a transaction while calling insert_dirty_subvol, meaning we cannot have a concurrent transaction commit. For call chain c) above a snapshot of the fs_info->reloc_ctl->reloc_list is taken with reloc_mutex held and free_reloc_roots is called on this local snapshot.

Quoted 5 lines

Those invariants are sufficient to prevent racing calls to __del_reloc_root alongside other users of the list, as such it's fine to drop the lock acquisition.

Thanks,
Qu

Quoted 19 lines
---
 fs/btrfs/relocation.c | 2 --
 1 file changed, 2 deletions(-)
fs/btrfs/relocation.c +0 -2
index 8076c340749f..e5cb64409f7c 100644
@@ -1381,9 +1381,7 @@ static void __del_reloc_root(struct btrfs_root *root)
 		BUG_ON((struct btrfs_root *)node->data != root);
 	}
 
-	spin_lock(&fs_info->trans_lock);
 	list_del_init(&root->root_list);
-	spin_unlock(&fs_info->trans_lock);
 	kfree(node);
 }
 
#6 lore raw
In reply to Nikolay Borisov
Quoted 46 lines · Nikolay Borisov

__del_reloc_root is called from:

a) Transaction commit via:
btrfs_transaction_commit
commit_fs_roots
btrfs_update_reloc_root
__del_reloc_root

b) From the relocation thread with the following call chains:

relocate_block_group
merge_reloc_roots
insert_dirty_subvol
btrfs_update_reloc_root
__del_reloc_root

c) merge_reloc_roots
free_reloc_roots
__del_reloc_roots

(The above call chain can called from btrfs_recover_relocation as well but for the purpose of this fix this is irrelevant).

The commont data structure that needs protecting is fs_info->reloc_ctl->reloc_list as reloc roots are anchored at this list. Turns out it's no needed to hold the trans_lock in __del_reloc_root since consistency is already guaranteed by call chain b) above holding a transaction while calling insert_dirty_subvol, meaning we cannot have a concurrent transaction commit. For call chain c) above a snapshot of the fs_info->reloc_ctl->reloc_list is taken with reloc_mutex held and free_reloc_roots is called on this local snapshot.

Those invariants are sufficient to prevent racing calls to __del_reloc_root alongside other users of the list, as such it's fine to drop the lock acquisition.

---
 fs/btrfs/relocation.c | 2 --
 1 file changed, 2 deletions(-)
fs/btrfs/relocation.c +0 -0
index 8076c340749f..e5cb64409f7c 100644

fs/btrfs/relocation.c static void __del_reloc_root(struct btrfs_root *root) · line 1386

Quoted 6 lines
fs/btrfs/relocation.c +0 -2
@@ -1381,9 +1381,7 @@ static void __del_reloc_root(struct btrfs_root *root)
 		BUG_ON((struct btrfs_root *)node->data != root);
 	}
 
-	spin_lock(&fs_info->trans_lock);
 	list_del_init(&root->root_list);
-	spin_unlock(&fs_info->trans_lock);

This has been obsoleted by patch f44deb7442edf42abee6 ("btrfs: hold a ref on the root->reloc_root"), I think the locks are still needed but you may want take a look.