public inbox for linux-btrfs@vger.kernel.org
 help / color / mirror / Atom feed
From: Sun YangKai <sunk67188@gmail.com>
To: linux-btrfs@vger.kernel.org
Cc: Sun YangKai <sunk67188@gmail.com>
Subject: [PATCH 2/4] btrfs: optimize balance_level() path reference handling
Date: Fri, 14 Nov 2025 15:24:46 +0800	[thread overview]
Message-ID: <20251114072532.13205-3-sunk67188@gmail.com> (raw)
In-Reply-To: <20251114072532.13205-1-sunk67188@gmail.com>

Instead of incrementing refcount on left node when it's referenced by path,
simply transfer ownership to path and set left to NULL. This eliminates:

- Unnecessary refcount increment/decrement operations
- Redundant conditional checks for left node cleanup

The path now consistently owns the left node reference when used.

Signed-off-by: Sun YangKai <sunk67188@gmail.com>
---
 fs/btrfs/ctree.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/fs/btrfs/ctree.c b/fs/btrfs/ctree.c
index 2369aa8ab455..a477839cf22c 100644
--- a/fs/btrfs/ctree.c
+++ b/fs/btrfs/ctree.c
@@ -1127,11 +1127,12 @@ static noinline int balance_level(struct btrfs_trans_handle *trans,
 	/* update the path */
 	if (left) {
 		if (btrfs_header_nritems(left) > orig_slot) {
-			refcount_inc(&left->refs);
 			/* left was locked after cow */
 			path->nodes[level] = left;
 			path->slots[level + 1] -= 1;
 			path->slots[level] = orig_slot;
+			/* left is now owned by path */
+			left = NULL;
 			if (mid) {
 				btrfs_tree_unlock(mid);
 				free_extent_buffer(mid);
@@ -1151,8 +1152,7 @@ static noinline int balance_level(struct btrfs_trans_handle *trans,
 		free_extent_buffer(right);
 	}
 	if (left) {
-		if (path->nodes[level] != left)
-			btrfs_tree_unlock(left);
+		btrfs_tree_unlock(left);
 		free_extent_buffer(left);
 	}
 	return ret;
-- 
2.51.0


  parent reply	other threads:[~2025-11-14  7:27 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-11-14  7:24 [PATCH 0/4] btrfs: some code cleanups in ctree.c Sun YangKai
2025-11-14  7:24 ` [PATCH 1/4] btrfs: extract root promotion logic into promote_child_to_root() Sun YangKai
2025-11-14  7:24 ` Sun YangKai [this message]
2025-11-14  7:24 ` [PATCH 3/4] btrfs: simplify leaf traversal after path release in btrfs_next_old_leaf() Sun YangKai
2025-11-14  7:24 ` [PATCH 4/4] btrfs: remove redundant level reset in btrfs_del_items() Sun YangKai
2025-11-18 20:46 ` [PATCH 0/4] btrfs: some code cleanups in ctree.c David Sterba

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20251114072532.13205-3-sunk67188@gmail.com \
    --to=sunk67188@gmail.com \
    --cc=linux-btrfs@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox