[PATCH 4/7] btrfs: check for log writers count first in wait_for_writer()

[email protected] Wed, 22 Jul 2026 13:41:40 +0100
Newsgroups org.kernel.vger.linux-btrfs
Message-ID <e8c75f644119b2ce31a5846c9f04ffe0f75bf3c5.1784656641.git.fdmanana@suse.com>
From: Filipe Manana <[email protected]>

In wait_for_writer() we first add the wait queue entry to the wait queue
and then check for the number of log writers. In case where right in the
first iteration of the loop we have no log writers, we end up wasting time
adding the wait queue entry to the wait queue (function call, locking the
queue with irq disable and enable on unlock, etc). So check first for the
number of log writers before adding to the wait queue.

Signed-off-by: Filipe Manana <[email protected]>
---
 fs/btrfs/tree-log.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/fs/btrfs/tree-log.c b/fs/btrfs/tree-log.c
index 191308982e9c..08af1e66c758 100644
--- a/fs/btrfs/tree-log.c
+++ b/fs/btrfs/tree-log.c
@@ -3202,11 +3202,11 @@ static void wait_for_writer(struct btrfs_root *root)
 	DEFINE_WAIT(wait);
 
 	for (;;) {
-		prepare_to_wait(&root->log_writer_wait, &wait,
-				TASK_UNINTERRUPTIBLE);
-		if (!atomic_read(&root->log_writers))
+		if (atomic_read(&root->log_writers) == 0)
 			break;
 
+		prepare_to_wait(&root->log_writer_wait, &wait,
+				TASK_UNINTERRUPTIBLE);
 		mutex_unlock(&root->log_mutex);
 		schedule();
 		mutex_lock(&root->log_mutex);
-- 
2.47.2