Skip to content

Commit 66d7e85

Browse files
committed
Btrfs: Check for a blocking lock before taking the spin
This reduces contention on the extent buffer spin locks by testing for a blocking lock before trying to take the spinlock. Signed-off-by: Chris Mason <chris.mason@oracle.com>
1 parent 7f366cf commit 66d7e85

File tree

1 file changed

+8
-2
lines changed

1 file changed

+8
-2
lines changed

fs/btrfs/locking.c

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,12 +71,13 @@ void btrfs_clear_lock_blocking(struct extent_buffer *eb)
7171
static int btrfs_spin_on_block(struct extent_buffer *eb)
7272
{
7373
int i;
74+
7475
for (i = 0; i < 512; i++) {
75-
cpu_relax();
7676
if (!test_bit(EXTENT_BUFFER_BLOCKING, &eb->bflags))
7777
return 1;
7878
if (need_resched())
7979
break;
80+
cpu_relax();
8081
}
8182
return 0;
8283
}
@@ -102,6 +103,7 @@ int btrfs_try_spin_lock(struct extent_buffer *eb)
102103

103104
/* spin for a bit on the BLOCKING flag */
104105
for (i = 0; i < 2; i++) {
106+
cpu_relax();
105107
if (!btrfs_spin_on_block(eb))
106108
break;
107109

@@ -148,6 +150,9 @@ int btrfs_tree_lock(struct extent_buffer *eb)
148150
DEFINE_WAIT(wait);
149151
wait.func = btrfs_wake_function;
150152

153+
if (!btrfs_spin_on_block(eb))
154+
goto sleep;
155+
151156
while(1) {
152157
spin_nested(eb);
153158

@@ -165,9 +170,10 @@ int btrfs_tree_lock(struct extent_buffer *eb)
165170
* spin for a bit, and if the blocking flag goes away,
166171
* loop around
167172
*/
173+
cpu_relax();
168174
if (btrfs_spin_on_block(eb))
169175
continue;
170-
176+
sleep:
171177
prepare_to_wait_exclusive(&eb->lock_wq, &wait,
172178
TASK_UNINTERRUPTIBLE);
173179

0 commit comments

Comments
 (0)