Skip to contentSkip to conversation

btrfs: some list extraction and disposal cleanups

PATCH In review [email protected] 3 participants 11 messages May 2, 2025 – May 5, 2025 Btrfs file system

lore
Share & tools
lore
#0 lore raw

cover btrfs: some list extraction and disposal cleanups

From: Filipe Manana <[email protected]>

Simplify some list element extractions and list disposals. More details in the changelogs.

Filipe Manana (8):
btrfs: simplify getting and extracting previous transaction during commit
btrfs: simplify getting and extracting previous transaction at clean_pinned_extents()
btrfs: simplify cow only root list extraction during transaction commit
btrfs: raid56: use list_last_entry() at cache_rbio()
btrfs: simplify extracting delayed node at btrfs_first_delayed_node()
btrfs: simplify extracting delayed node at btrfs_first_prepared_delayed_node()
btrfs: simplify csum list release at btrfs_put_ordered_extent()
btrfs: defrag: use list_last_entry() at defrag_collect_targets()

 fs/btrfs/block-group.c   |  5 ++---
 fs/btrfs/defrag.c        |  8 ++++----
 fs/btrfs/delayed-inode.c | 31 ++++++++++++-------------------
 fs/btrfs/ordered-data.c  | 12 ++++--------
 fs/btrfs/raid56.c        |  6 +++---
 fs/btrfs/transaction.c   | 16 +++++++---------
 6 files changed, 32 insertions(+), 46 deletions(-)
Signature · 1 line
2.47.2
#1 lore raw

PATCH 1/8 btrfs: simplify getting and extracting previous transaction during commit

From: Filipe Manana <[email protected]>

Instead of detecting if there is a previous transaction by comparing the current transaction's list prev member to the head of the transaction list (fs_info->trans_list), use the list_is_first() helper which contains that logic and the naming makes sense since a new transaction is always added to the end of the list fs_info->trans_list with list_add_tail().

And instead of extracting the previous transaction with the more generic list_entry() helper against the current transaction's list prev member, use the more specific list_prev_entry() helper, which makes it clear what we are doing and is shorter.

---
 fs/btrfs/transaction.c | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)
fs/btrfs/transaction.c +2 -3
index 5a58d97a5dfc..fe79d65c8635 100644
@@ -2270,14 +2270,13 @@ int btrfs_commit_transaction(struct btrfs_trans_handle *trans)
 	wake_up(&fs_info->transaction_blocked_wait);
 	btrfs_trans_state_lockdep_release(fs_info, BTRFS_LOCKDEP_TRANS_COMMIT_PREP);
 
-	if (cur_trans->list.prev != &fs_info->trans_list) {
+	if (!list_is_first(&cur_trans->list, &fs_info->trans_list)) {
 		enum btrfs_trans_state want_state = TRANS_STATE_COMPLETED;
 
 		if (trans->in_fsync)
 			want_state = TRANS_STATE_SUPER_COMMITTED;
 
-		prev_trans = list_entry(cur_trans->list.prev,
-					struct btrfs_transaction, list);
+		prev_trans = list_prev_entry(cur_trans, list);
 		if (prev_trans->state < want_state) {
 			refcount_inc(&prev_trans->use_count);
 			spin_unlock(&fs_info->trans_lock);
Signature · 1 line
2.47.2
#2 lore raw

PATCH 2/8 btrfs: simplify getting and extracting previous transaction at clean_pinned_extents()

In reply to [email protected]

From: Filipe Manana <[email protected]>

Instead of detecting if there is a previous transaction by comparing the current transaction's list prev member to the head of the transaction list (fs_info->trans_list), use the list_is_first() helper which contains that logic and the naming makes sense since a new transaction is always added to the end of the list fs_info->trans_list with list_add_tail().

We are also extracting the previous transaction with list_last_entry() against the transaction, which is correct but confusing because that function is usually meant to be used against a pointer to the start of a list and not a member of a list. It is easier to reason by either calling list_first_entry() against the list fs_info->trans_list, since we can never have more than two transactions in the list, or by calling list_prev_entry() against the transaction. So change that to use the later method.

---
 fs/btrfs/block-group.c | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)
fs/btrfs/block-group.c +2 -3
index e3b912d539e2..a41ca673ad1a 100644
@@ -1418,9 +1418,8 @@ static bool clean_pinned_extents(struct btrfs_trans_handle *trans,
 	int ret;
 
 	spin_lock(&fs_info->trans_lock);
-	if (trans->transaction->list.prev != &fs_info->trans_list) {
-		prev_trans = list_last_entry(&trans->transaction->list,
-					     struct btrfs_transaction, list);
+	if (!list_is_first(&trans->transaction->list, &fs_info->trans_list)) {
+		prev_trans = list_prev_entry(trans->transaction, list);
 		refcount_inc(&prev_trans->use_count);
 	}
 	spin_unlock(&fs_info->trans_lock);
Signature · 1 line
2.47.2
#3 lore raw

PATCH 3/8 btrfs: simplify cow only root list extraction during transaction commit

In reply to [email protected]

From: Filipe Manana <[email protected]>

There's no need to keep a local variable to extract the first member of the list and then do a list_entry() call, we can use list_first_entry() instead, removing the need for the temporary variable and extracting the first element in a single step.

Also, there's no need to do a list_del_init() followed by list_add_tail(), instead we can use list_move_tail(). We are in transaction commit critical section where we don't need to worry about concurrency and that's why we don't take any locks and can use list_move_tail() (we do assert early at commit_cowonly_roots() that we are in the critical section, that the transaction's state is TRANS_STATE_COMMIT_DOING).

---
 fs/btrfs/transaction.c | 11 +++++------
 1 file changed, 5 insertions(+), 6 deletions(-)
fs/btrfs/transaction.c +5 -6
index fe79d65c8635..e0256eecf176 100644
@@ -1326,7 +1326,6 @@ static noinline int commit_cowonly_roots(struct btrfs_trans_handle *trans)
 	struct btrfs_fs_info *fs_info = trans->fs_info;
 	struct list_head *dirty_bgs = &trans->transaction->dirty_bgs;
 	struct list_head *io_bgs = &trans->transaction->io_bgs;
-	struct list_head *next;
 	struct extent_buffer *eb;
 	int ret;
 
@@ -1362,13 +1361,13 @@ static noinline int commit_cowonly_roots(struct btrfs_trans_handle *trans)
 again:
 	while (!list_empty(&fs_info->dirty_cowonly_roots)) {
 		struct btrfs_root *root;
-		next = fs_info->dirty_cowonly_roots.next;
-		list_del_init(next);
-		root = list_entry(next, struct btrfs_root, dirty_list);
+
+		root = list_first_entry(&fs_info->dirty_cowonly_roots,
+					struct btrfs_root, dirty_list);
 		clear_bit(BTRFS_ROOT_DIRTY, &root->state);
+		list_move_tail(&root->dirty_list,
+			       &trans->transaction->switch_commits);
 
-		list_add_tail(&root->dirty_list,
-			      &trans->transaction->switch_commits);
 		ret = update_cowonly_root(trans, root);
 		if (ret)
 			return ret;
Signature · 1 line
2.47.2
#4 lore raw

PATCH 4/8 btrfs: raid56: use list_last_entry() at cache_rbio()

In reply to [email protected]

From: Filipe Manana <[email protected]>

Instead of using list_entry() against the list's prev entry, use list_last_entry(), which removes the need to know the last member is accessed through the prev list pointer and the naming makes it easier to reason about what we are doing.

---
 fs/btrfs/raid56.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
fs/btrfs/raid56.c +3 -3
index fd96b5040584..c01d0ab80f3b 100644
@@ -577,9 +577,9 @@ static void cache_rbio(struct btrfs_raid_bio *rbio)
 	if (table->cache_size > RBIO_CACHE_SIZE) {
 		struct btrfs_raid_bio *found;
 
-		found = list_entry(table->stripe_cache.prev,
-				  struct btrfs_raid_bio,
-				  stripe_cache);
+		found = list_last_entry(&table->stripe_cache,
+					struct btrfs_raid_bio,
+					stripe_cache);
 
 		if (found != rbio)
 			__remove_rbio_from_cache(found);
Signature · 1 line
2.47.2
#5 lore raw

PATCH 5/8 btrfs: simplify extracting delayed node at btrfs_first_delayed_node()

In reply to [email protected]

From: Filipe Manana <[email protected]>

Instead of grabbing the next pointer from the list and then doing a list_entry() call, we can simply use list_first_entry(), removing the need for list_head variable.

Also there's no need to check if the list is empty before attempting to extract the first element, we can use list_first_entry_or_null(), removing the need for a special if statement and the 'out' label.

---
 fs/btrfs/delayed-inode.c | 14 +++++---------
 1 file changed, 5 insertions(+), 9 deletions(-)
fs/btrfs/delayed-inode.c +5 -9
index 206d39e5ce57..a1ac35bc789a 100644
@@ -216,17 +216,13 @@ static void btrfs_dequeue_delayed_node(struct btrfs_delayed_root *root,
 static struct btrfs_delayed_node *btrfs_first_delayed_node(
 			struct btrfs_delayed_root *delayed_root)
 {
-	struct list_head *p;
-	struct btrfs_delayed_node *node = NULL;
+	struct btrfs_delayed_node *node;
 
 	spin_lock(&delayed_root->lock);
-	if (list_empty(&delayed_root->node_list))
-		goto out;
-
-	p = delayed_root->node_list.next;
-	node = list_entry(p, struct btrfs_delayed_node, n_list);
-	refcount_inc(&node->refs);
-out:
+	node = list_first_entry_or_null(&delayed_root->node_list,
+					struct btrfs_delayed_node, n_list);
+	if (node)
+		refcount_inc(&node->refs);
 	spin_unlock(&delayed_root->lock);
 
 	return node;
Signature · 1 line
2.47.2
#6 lore raw

PATCH 6/8 btrfs: simplify extracting delayed node at btrfs_first_prepared_delayed_node()

In reply to [email protected]

From: Filipe Manana <[email protected]>

Instead of grabbing the next pointer from the list and then doing a list_entry() call, we can simply use list_first_entry(), removing the need for list_head variable.

Also there's no need to check if the list is empty before attempting to extract the first element, we can use list_first_entry_or_null(), removing the need for a special if statement and the 'out' label.

---
 fs/btrfs/delayed-inode.c | 17 +++++++----------
 1 file changed, 7 insertions(+), 10 deletions(-)
fs/btrfs/delayed-inode.c +7 -10
index a1ac35bc789a..c7cc24a5dd5e 100644
@@ -294,18 +294,15 @@ static inline void btrfs_release_delayed_node(struct btrfs_delayed_node *node)
 static struct btrfs_delayed_node *btrfs_first_prepared_delayed_node(
 					struct btrfs_delayed_root *delayed_root)
 {
-	struct list_head *p;
-	struct btrfs_delayed_node *node = NULL;
+	struct btrfs_delayed_node *node;
 
 	spin_lock(&delayed_root->lock);
-	if (list_empty(&delayed_root->prepare_list))
-		goto out;
-
-	p = delayed_root->prepare_list.next;
-	list_del_init(p);
-	node = list_entry(p, struct btrfs_delayed_node, p_list);
-	refcount_inc(&node->refs);
-out:
+	node = list_first_entry_or_null(&delayed_root->prepare_list,
+					struct btrfs_delayed_node, p_list);
+	if (node) {
+		list_del_init(&node->p_list);
+		refcount_inc(&node->refs);
+	}
 	spin_unlock(&delayed_root->lock);
 
 	return node;
Signature · 1 line
2.47.2
#7 lore raw

PATCH 7/8 btrfs: simplify csum list release at btrfs_put_ordered_extent()

In reply to [email protected]

From: Filipe Manana <[email protected]>

Instead of extracting each element by grabbing the list's first member in a local list_head variable, then extracting the csum with list_entry() and iterating with a while loop checking for list emptyness, use the iteration helper list_for_each_entry_safe(). This also removes the need to delete elements from the list with list_del() since the ordered extent is freed immediately after.

---
 fs/btrfs/ordered-data.c | 12 ++++--------
 1 file changed, 4 insertions(+), 8 deletions(-)
fs/btrfs/ordered-data.c +4 -8
index 6151d32704d2..ae49f87b27e8 100644
@@ -607,23 +607,19 @@ bool btrfs_dec_test_ordered_pending(struct btrfs_inode *inode,
  */
 void btrfs_put_ordered_extent(struct btrfs_ordered_extent *entry)
 {
-	struct list_head *cur;
-	struct btrfs_ordered_sum *sum;
-
 	trace_btrfs_ordered_extent_put(entry->inode, entry);
 
 	if (refcount_dec_and_test(&entry->refs)) {
+		struct btrfs_ordered_sum *sum;
+		struct btrfs_ordered_sum *tmp;
+
 		ASSERT(list_empty(&entry->root_extent_list));
 		ASSERT(list_empty(&entry->log_list));
 		ASSERT(RB_EMPTY_NODE(&entry->rb_node));
 		if (entry->inode)
 			btrfs_add_delayed_iput(entry->inode);
-		while (!list_empty(&entry->list)) {
-			cur = entry->list.next;
-			sum = list_entry(cur, struct btrfs_ordered_sum, list);
-			list_del(&sum->list);
+		list_for_each_entry_safe(sum, tmp, &entry->list, list)
 			kvfree(sum);
-		}
 		kmem_cache_free(btrfs_ordered_extent_cache, entry);
 	}
 }
Signature · 1 line
2.47.2
#8 lore raw

PATCH 8/8 btrfs: defrag: use list_last_entry() at defrag_collect_targets()

In reply to [email protected]

From: Filipe Manana <[email protected]>

Instead of using list_entry() against the list's prev entry, use list_last_entry(), which removes the need to know the last member is accessed through the prev list pointer and the naming makes it easier to reason about what we are doing.

---
 fs/btrfs/defrag.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
fs/btrfs/defrag.c +4 -4
index 48e12c8a90a7..1831618579cb 100644
@@ -1068,8 +1068,8 @@ static int defrag_collect_targets(struct btrfs_inode *inode,
 			/* Empty target list, no way to merge with last entry */
 			if (list_empty(target_list))
 				goto next;
-			last = list_entry(target_list->prev,
-					  struct defrag_target_range, list);
+			last = list_last_entry(target_list,
+					       struct defrag_target_range, list);
 			/* Not mergeable with last entry */
 			if (last->start + last->len != cur)
 				goto next;
@@ -1087,8 +1087,8 @@ static int defrag_collect_targets(struct btrfs_inode *inode,
 		if (!list_empty(target_list)) {
 			struct defrag_target_range *last;
 
-			last = list_entry(target_list->prev,
-					  struct defrag_target_range, list);
+			last = list_last_entry(target_list,
+					       struct defrag_target_range, list);
 			ASSERT(last->start + last->len <= cur);
 			if (last->start + last->len == cur) {
 				/* Mergeable, enlarge the last entry */
Signature · 1 line
2.47.2
Reviewed-by on the v1 series
#9 lore raw
In reply to [email protected]
Quoted 5 lines · 在 2025/5/2 20:00, [email protected] 写道:

From: Filipe Manana <[email protected]>

Simplify some list element extractions and list disposals. More details in the changelogs.

Thanks,
Qu

Quoted 18 lines

Filipe Manana (8):
btrfs: simplify getting and extracting previous transaction during commit
btrfs: simplify getting and extracting previous transaction at clean_pinned_extents()
btrfs: simplify cow only root list extraction during transaction commit
btrfs: raid56: use list_last_entry() at cache_rbio()
btrfs: simplify extracting delayed node at btrfs_first_delayed_node()
btrfs: simplify extracting delayed node at btrfs_first_prepared_delayed_node()
btrfs: simplify csum list release at btrfs_put_ordered_extent()
btrfs: defrag: use list_last_entry() at defrag_collect_targets()

  fs/btrfs/block-group.c   |  5 ++---
  fs/btrfs/defrag.c        |  8 ++++----
  fs/btrfs/delayed-inode.c | 31 ++++++++++++-------------------
  fs/btrfs/ordered-data.c  | 12 ++++--------
  fs/btrfs/raid56.c        |  6 +++---
  fs/btrfs/transaction.c   | 16 +++++++---------
  6 files changed, 32 insertions(+), 46 deletions(-)