Skip to content

Commit b4ce94d

Browse files
committed
Btrfs: Change btree locking to use explicit blocking points
Most of the btrfs metadata operations can be protected by a spinlock, but some operations still need to schedule. So far, btrfs has been using a mutex along with a trylock loop, most of the time it is able to avoid going for the full mutex, so the trylock loop is a big performance gain. This commit is step one for getting rid of the blocking locks entirely. btrfs_tree_lock takes a spinlock, and the code explicitly switches to a blocking lock when it starts an operation that can schedule. We'll be able get rid of the blocking locks in smaller pieces over time. Tracing allows us to find the most common cause of blocking, so we can start with the hot spots first. The basic idea is: btrfs_tree_lock() returns with the spin lock held btrfs_set_lock_blocking() sets the EXTENT_BUFFER_BLOCKING bit in the extent buffer flags, and then drops the spin lock. The buffer is still considered locked by all of the btrfs code. If btrfs_tree_lock gets the spinlock but finds the blocking bit set, it drops the spin lock and waits on a wait queue for the blocking bit to go away. Much of the code that needs to set the blocking bit finishes without actually blocking a good percentage of the time. So, an adaptive spin is still used against the blocking bit to avoid very high context switch rates. btrfs_clear_lock_blocking() clears the blocking bit and returns with the spinlock held again. btrfs_tree_unlock() can be called on either blocking or spinning locks, it does the right thing based on the blocking bit. ctree.c has a helper function to set/clear all the locked buffers in a path as blocking. Signed-off-by: Chris Mason <chris.mason@oracle.com>
1 parent c487685 commit b4ce94d

11 files changed

Lines changed: 470 additions & 39 deletions

File tree

‎fs/btrfs/ctree.c‎

Lines changed: 226 additions & 8 deletions
Large diffs are not rendered by default.

‎fs/btrfs/ctree.h‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1835,6 +1835,10 @@ void btrfs_release_path(struct btrfs_root *root, struct btrfs_path *p);
18351835
struct btrfs_path *btrfs_alloc_path(void);
18361836
void btrfs_free_path(struct btrfs_path *p);
18371837
void btrfs_init_path(struct btrfs_path *p);
1838+
void btrfs_set_path_blocking(struct btrfs_path *p);
1839+
void btrfs_clear_path_blocking(struct btrfs_path *p);
1840+
void btrfs_unlock_up_safe(struct btrfs_path *p, int level);
1841+
18381842
int btrfs_del_items(struct btrfs_trans_handle *trans, struct btrfs_root *root,
18391843
struct btrfs_path *path, int slot, int nr);
18401844
int btrfs_del_leaf(struct btrfs_trans_handle *trans,

‎fs/btrfs/disk-io.c‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -799,7 +799,7 @@ struct extent_buffer *read_tree_block(struct btrfs_root *root, u64 bytenr,
799799
ret = btree_read_extent_buffer_pages(root, buf, 0, parent_transid);
800800

801801
if (ret == 0)
802-
buf->flags |= EXTENT_UPTODATE;
802+
set_bit(EXTENT_BUFFER_UPTODATE, &buf->bflags);
803803
else
804804
WARN_ON(1);
805805
return buf;
@@ -813,6 +813,10 @@ int clean_tree_block(struct btrfs_trans_handle *trans, struct btrfs_root *root,
813813
if (btrfs_header_generation(buf) ==
814814
root->fs_info->running_transaction->transid) {
815815
WARN_ON(!btrfs_tree_locked(buf));
816+
817+
/* ugh, clear_extent_buffer_dirty can be expensive */
818+
btrfs_set_lock_blocking(buf);
819+
816820
clear_extent_buffer_dirty(&BTRFS_I(btree_inode)->io_tree,
817821
buf);
818822
}
@@ -2311,6 +2315,8 @@ void btrfs_mark_buffer_dirty(struct extent_buffer *buf)
23112315
u64 transid = btrfs_header_generation(buf);
23122316
struct inode *btree_inode = root->fs_info->btree_inode;
23132317

2318+
btrfs_set_lock_blocking(buf);
2319+
23142320
WARN_ON(!btrfs_tree_locked(buf));
23152321
if (transid != root->fs_info->generation) {
23162322
printk(KERN_CRIT "btrfs transid mismatch buffer %llu, "
@@ -2353,7 +2359,7 @@ int btrfs_read_buffer(struct extent_buffer *buf, u64 parent_transid)
23532359
int ret;
23542360
ret = btree_read_extent_buffer_pages(root, buf, 0, parent_transid);
23552361
if (ret == 0)
2356-
buf->flags |= EXTENT_UPTODATE;
2362+
set_bit(EXTENT_BUFFER_UPTODATE, &buf->bflags);
23572363
return ret;
23582364
}
23592365

‎fs/btrfs/extent-tree.c‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3407,7 +3407,10 @@ struct extent_buffer *btrfs_init_new_buffer(struct btrfs_trans_handle *trans,
34073407
btrfs_set_header_generation(buf, trans->transid);
34083408
btrfs_tree_lock(buf);
34093409
clean_tree_block(trans, root, buf);
3410+
3411+
btrfs_set_lock_blocking(buf);
34103412
btrfs_set_buffer_uptodate(buf);
3413+
34113414
if (root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID) {
34123415
set_extent_dirty(&root->dirty_log_pages, buf->start,
34133416
buf->start + buf->len - 1, GFP_NOFS);
@@ -3416,6 +3419,7 @@ struct extent_buffer *btrfs_init_new_buffer(struct btrfs_trans_handle *trans,
34163419
buf->start + buf->len - 1, GFP_NOFS);
34173420
}
34183421
trans->blocks_used++;
3422+
/* this returns a buffer locked for blocking */
34193423
return buf;
34203424
}
34213425

@@ -3752,6 +3756,7 @@ static noinline int walk_down_subtree(struct btrfs_trans_handle *trans,
37523756

37533757
next = read_tree_block(root, bytenr, blocksize, ptr_gen);
37543758
btrfs_tree_lock(next);
3759+
btrfs_set_lock_blocking(next);
37553760

37563761
ret = btrfs_lookup_extent_ref(trans, root, bytenr, blocksize,
37573762
&refs);

‎fs/btrfs/extent_io.c‎

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2990,7 +2990,9 @@ static struct extent_buffer *__alloc_extent_buffer(struct extent_io_tree *tree,
29902990
eb = kmem_cache_zalloc(extent_buffer_cache, mask);
29912991
eb->start = start;
29922992
eb->len = len;
2993-
mutex_init(&eb->mutex);
2993+
spin_lock_init(&eb->lock);
2994+
init_waitqueue_head(&eb->lock_wq);
2995+
29942996
#if LEAK_DEBUG
29952997
spin_lock_irqsave(&leak_lock, flags);
29962998
list_add(&eb->leak_list, &buffers);
@@ -3071,8 +3073,7 @@ struct extent_buffer *alloc_extent_buffer(struct extent_io_tree *tree,
30713073
unlock_page(p);
30723074
}
30733075
if (uptodate)
3074-
eb->flags |= EXTENT_UPTODATE;
3075-
eb->flags |= EXTENT_BUFFER_FILLED;
3076+
set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
30763077

30773078
spin_lock(&tree->buffer_lock);
30783079
exists = buffer_tree_insert(tree, start, &eb->rb_node);
@@ -3226,7 +3227,7 @@ int clear_extent_buffer_uptodate(struct extent_io_tree *tree,
32263227
unsigned long num_pages;
32273228

32283229
num_pages = num_extent_pages(eb->start, eb->len);
3229-
eb->flags &= ~EXTENT_UPTODATE;
3230+
clear_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
32303231

32313232
clear_extent_uptodate(tree, eb->start, eb->start + eb->len - 1,
32323233
GFP_NOFS);
@@ -3297,7 +3298,7 @@ int extent_buffer_uptodate(struct extent_io_tree *tree,
32973298
struct page *page;
32983299
int pg_uptodate = 1;
32993300

3300-
if (eb->flags & EXTENT_UPTODATE)
3301+
if (test_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags))
33013302
return 1;
33023303

33033304
ret = test_range_bit(tree, eb->start, eb->start + eb->len - 1,
@@ -3333,7 +3334,7 @@ int read_extent_buffer_pages(struct extent_io_tree *tree,
33333334
struct bio *bio = NULL;
33343335
unsigned long bio_flags = 0;
33353336

3336-
if (eb->flags & EXTENT_UPTODATE)
3337+
if (test_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags))
33373338
return 0;
33383339

33393340
if (test_range_bit(tree, eb->start, eb->start + eb->len - 1,
@@ -3364,7 +3365,7 @@ int read_extent_buffer_pages(struct extent_io_tree *tree,
33643365
}
33653366
if (all_uptodate) {
33663367
if (start_i == 0)
3367-
eb->flags |= EXTENT_UPTODATE;
3368+
set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
33683369
goto unlock_exit;
33693370
}
33703371

@@ -3400,7 +3401,7 @@ int read_extent_buffer_pages(struct extent_io_tree *tree,
34003401
}
34013402

34023403
if (!ret)
3403-
eb->flags |= EXTENT_UPTODATE;
3404+
set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
34043405
return ret;
34053406

34063407
unlock_exit:
@@ -3497,7 +3498,6 @@ int map_extent_buffer(struct extent_buffer *eb, unsigned long start,
34973498
unmap_extent_buffer(eb, eb->map_token, km);
34983499
eb->map_token = NULL;
34993500
save = 1;
3500-
WARN_ON(!mutex_is_locked(&eb->mutex));
35013501
}
35023502
err = map_private_extent_buffer(eb, start, min_len, token, map,
35033503
map_start, map_len, km);

‎fs/btrfs/extent_io.h‎

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@
2222
/* flags for bio submission */
2323
#define EXTENT_BIO_COMPRESSED 1
2424

25+
/* these are bit numbers for test/set bit */
26+
#define EXTENT_BUFFER_UPTODATE 0
27+
#define EXTENT_BUFFER_BLOCKING 1
28+
2529
/*
2630
* page->private values. Every page that is controlled by the extent
2731
* map has page->private set to one.
@@ -95,11 +99,19 @@ struct extent_buffer {
9599
unsigned long map_start;
96100
unsigned long map_len;
97101
struct page *first_page;
102+
unsigned long bflags;
98103
atomic_t refs;
99-
int flags;
100104
struct list_head leak_list;
101105
struct rb_node rb_node;
102-
struct mutex mutex;
106+
107+
/* the spinlock is used to protect most operations */
108+
spinlock_t lock;
109+
110+
/*
111+
* when we keep the lock held while blocking, waiters go onto
112+
* the wq
113+
*/
114+
wait_queue_head_t lock_wq;
103115
};
104116

105117
struct extent_map_tree;

‎fs/btrfs/inode.c‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
#include "tree-log.h"
5151
#include "ref-cache.h"
5252
#include "compression.h"
53+
#include "locking.h"
5354

5455
struct btrfs_iget_args {
5556
u64 ino;
@@ -2021,6 +2022,7 @@ void btrfs_read_locked_inode(struct inode *inode)
20212022
BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item);
20222023

20232024
alloc_group_block = btrfs_inode_block_group(leaf, inode_item);
2025+
20242026
BTRFS_I(inode)->block_group = btrfs_find_block_group(root, 0,
20252027
alloc_group_block, 0);
20262028
btrfs_free_path(path);
@@ -2117,6 +2119,7 @@ noinline int btrfs_update_inode(struct btrfs_trans_handle *trans,
21172119
goto failed;
21182120
}
21192121

2122+
btrfs_unlock_up_safe(path, 1);
21202123
leaf = path->nodes[0];
21212124
inode_item = btrfs_item_ptr(leaf, path->slots[0],
21222125
struct btrfs_inode_item);

0 commit comments

Comments
 (0)