Skip to content

Commit 49958fd

Browse files
Josef Bacikchrismason-xx
authored andcommitted
Btrfs: change the ordered tree to use a spinlock instead of a mutex
The ordered tree used to need a mutex, but currently all we use it for is to protect the rb_tree, and a spin_lock is just fine for that. Using a spin_lock instead makes dbench run a little faster, 58 mb/s instead of 51 mb/s, and have less latency, 3445.138 ms instead of 3820.633 ms. Signed-off-by: Josef Bacik <josef@redhat.com> Signed-off-by: Chris Mason <chris.mason@oracle.com>
1 parent 4125bf7 commit 49958fd

2 files changed

Lines changed: 19 additions & 19 deletions

File tree

‎fs/btrfs/ordered-data.c‎

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,6 @@ int btrfs_add_ordered_extent(struct inode *inode, u64 file_offset,
174174
if (!entry)
175175
return -ENOMEM;
176176

177-
mutex_lock(&tree->mutex);
178177
entry->file_offset = file_offset;
179178
entry->start = start;
180179
entry->len = len;
@@ -190,16 +189,17 @@ int btrfs_add_ordered_extent(struct inode *inode, u64 file_offset,
190189
INIT_LIST_HEAD(&entry->list);
191190
INIT_LIST_HEAD(&entry->root_extent_list);
192191

192+
spin_lock(&tree->lock);
193193
node = tree_insert(&tree->tree, file_offset,
194194
&entry->rb_node);
195195
BUG_ON(node);
196+
spin_unlock(&tree->lock);
196197

197198
spin_lock(&BTRFS_I(inode)->root->fs_info->ordered_extent_lock);
198199
list_add_tail(&entry->root_extent_list,
199200
&BTRFS_I(inode)->root->fs_info->ordered_extents);
200201
spin_unlock(&BTRFS_I(inode)->root->fs_info->ordered_extent_lock);
201202

202-
mutex_unlock(&tree->mutex);
203203
BUG_ON(node);
204204
return 0;
205205
}
@@ -216,9 +216,9 @@ int btrfs_add_ordered_sum(struct inode *inode,
216216
struct btrfs_ordered_inode_tree *tree;
217217

218218
tree = &BTRFS_I(inode)->ordered_tree;
219-
mutex_lock(&tree->mutex);
219+
spin_lock(&tree->lock);
220220
list_add_tail(&sum->list, &entry->list);
221-
mutex_unlock(&tree->mutex);
221+
spin_unlock(&tree->lock);
222222
return 0;
223223
}
224224

@@ -240,7 +240,7 @@ int btrfs_dec_test_ordered_pending(struct inode *inode,
240240
int ret;
241241

242242
tree = &BTRFS_I(inode)->ordered_tree;
243-
mutex_lock(&tree->mutex);
243+
spin_lock(&tree->lock);
244244
node = tree_search(tree, file_offset);
245245
if (!node) {
246246
ret = 1;
@@ -264,7 +264,7 @@ int btrfs_dec_test_ordered_pending(struct inode *inode,
264264
else
265265
ret = 1;
266266
out:
267-
mutex_unlock(&tree->mutex);
267+
spin_unlock(&tree->lock);
268268
return ret == 0;
269269
}
270270

@@ -291,7 +291,7 @@ int btrfs_put_ordered_extent(struct btrfs_ordered_extent *entry)
291291

292292
/*
293293
* remove an ordered extent from the tree. No references are dropped
294-
* and you must wake_up entry->wait. You must hold the tree mutex
294+
* and you must wake_up entry->wait. You must hold the tree lock
295295
* while you call this function.
296296
*/
297297
static int __btrfs_remove_ordered_extent(struct inode *inode,
@@ -340,9 +340,9 @@ int btrfs_remove_ordered_extent(struct inode *inode,
340340
int ret;
341341

342342
tree = &BTRFS_I(inode)->ordered_tree;
343-
mutex_lock(&tree->mutex);
343+
spin_lock(&tree->lock);
344344
ret = __btrfs_remove_ordered_extent(inode, entry);
345-
mutex_unlock(&tree->mutex);
345+
spin_unlock(&tree->lock);
346346
wake_up(&entry->wait);
347347

348348
return ret;
@@ -567,7 +567,7 @@ struct btrfs_ordered_extent *btrfs_lookup_ordered_extent(struct inode *inode,
567567
struct btrfs_ordered_extent *entry = NULL;
568568

569569
tree = &BTRFS_I(inode)->ordered_tree;
570-
mutex_lock(&tree->mutex);
570+
spin_lock(&tree->lock);
571571
node = tree_search(tree, file_offset);
572572
if (!node)
573573
goto out;
@@ -578,7 +578,7 @@ struct btrfs_ordered_extent *btrfs_lookup_ordered_extent(struct inode *inode,
578578
if (entry)
579579
atomic_inc(&entry->refs);
580580
out:
581-
mutex_unlock(&tree->mutex);
581+
spin_unlock(&tree->lock);
582582
return entry;
583583
}
584584

@@ -594,15 +594,15 @@ btrfs_lookup_first_ordered_extent(struct inode *inode, u64 file_offset)
594594
struct btrfs_ordered_extent *entry = NULL;
595595

596596
tree = &BTRFS_I(inode)->ordered_tree;
597-
mutex_lock(&tree->mutex);
597+
spin_lock(&tree->lock);
598598
node = tree_search(tree, file_offset);
599599
if (!node)
600600
goto out;
601601

602602
entry = rb_entry(node, struct btrfs_ordered_extent, rb_node);
603603
atomic_inc(&entry->refs);
604604
out:
605-
mutex_unlock(&tree->mutex);
605+
spin_unlock(&tree->lock);
606606
return entry;
607607
}
608608

@@ -629,7 +629,7 @@ int btrfs_ordered_update_i_size(struct inode *inode, u64 offset,
629629
else
630630
offset = ALIGN(offset, BTRFS_I(inode)->root->sectorsize);
631631

632-
mutex_lock(&tree->mutex);
632+
spin_lock(&tree->lock);
633633
disk_i_size = BTRFS_I(inode)->disk_i_size;
634634

635635
/* truncate file */
@@ -735,7 +735,7 @@ int btrfs_ordered_update_i_size(struct inode *inode, u64 offset,
735735
*/
736736
if (ordered)
737737
__btrfs_remove_ordered_extent(inode, ordered);
738-
mutex_unlock(&tree->mutex);
738+
spin_unlock(&tree->lock);
739739
if (ordered)
740740
wake_up(&ordered->wait);
741741
return ret;
@@ -762,7 +762,7 @@ int btrfs_find_ordered_sum(struct inode *inode, u64 offset, u64 disk_bytenr,
762762
if (!ordered)
763763
return 1;
764764

765-
mutex_lock(&tree->mutex);
765+
spin_lock(&tree->lock);
766766
list_for_each_entry_reverse(ordered_sum, &ordered->list, list) {
767767
if (disk_bytenr >= ordered_sum->bytenr) {
768768
num_sectors = ordered_sum->len / sectorsize;
@@ -777,7 +777,7 @@ int btrfs_find_ordered_sum(struct inode *inode, u64 offset, u64 disk_bytenr,
777777
}
778778
}
779779
out:
780-
mutex_unlock(&tree->mutex);
780+
spin_unlock(&tree->lock);
781781
btrfs_put_ordered_extent(ordered);
782782
return ret;
783783
}

‎fs/btrfs/ordered-data.h‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121

2222
/* one of these per inode */
2323
struct btrfs_ordered_inode_tree {
24-
struct mutex mutex;
24+
spinlock_t lock;
2525
struct rb_root tree;
2626
struct rb_node *last;
2727
};
@@ -128,7 +128,7 @@ static inline int btrfs_ordered_sum_size(struct btrfs_root *root,
128128
static inline void
129129
btrfs_ordered_inode_tree_init(struct btrfs_ordered_inode_tree *t)
130130
{
131-
mutex_init(&t->mutex);
131+
spin_lock_init(&t->lock);
132132
t->tree = RB_ROOT;
133133
t->last = NULL;
134134
}

0 commit comments

Comments
 (0)